#![cfg(test)]
use super::*;
use crate::delegation::artifact::{DelegationRole, DelegationSubjectKind};
use crate::merkle::tree::{AppendOnlyTree, RawLeaf};
use crate::receipt;
#[path = "sign_tests/envelope.rs"]
mod envelope;
#[path = "sign_tests/issuance.rs"]
mod issuance;
#[path = "sign_tests/sequence.rs"]
mod sequence;
#[path = "sign_tests/subject.rs"]
mod subject;
#[path = "sign_tests/uniform_failure.rs"]
mod uniform_failure;
const DOMAIN: DelegationSubjectKind = DelegationSubjectKind::Domain;
const SEAT: DelegationSubjectKind = DelegationSubjectKind::Seat;
const ORIGIN: &str = "example.test";
const OTHER_ORIGIN: &str = "attacker.test";
fn identity(seed: &[u8; 32]) -> Ed25519Identity {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("root.key");
std::fs::write(&path, seed).unwrap();
Ed25519Identity::load(&path).unwrap()
}
fn root() -> Ed25519Identity {
identity(b"lys-anchor-delegation-root-seed1")
}
fn attacker() -> Ed25519Identity {
identity(b"lys-anchor-delegation-evil-seed2")
}
fn operational() -> Ed25519Identity {
identity(b"lys-anchor-delegation-oper-seed3")
}
fn claim() -> DelegationClaim {
claim_for(ORIGIN)
}
fn claim_for(origin: &str) -> DelegationClaim {
claim_at(origin, 300)
}
fn claim_at(subject_value: &str, sequence: u64) -> DelegationClaim {
DelegationClaim {
subject_kind: DelegationSubjectKind::Domain,
subject_value: subject_value.to_string(),
delegated_public_key: operational().public_key_bytes(),
role: DelegationRole::Operational,
not_before_unix_ms: 1_700_000_000_000,
sequence,
}
}
fn seat_claim_for(subject_value: &str) -> DelegationClaim {
DelegationClaim {
subject_kind: DelegationSubjectKind::Seat,
subject_value: subject_value.to_string(),
delegated_public_key: operational().public_key_bytes(),
role: DelegationRole::SpeaksFor,
not_before_unix_ms: 1_700_000_000_000,
sequence: 300,
}
}
fn role_offset(payload: &[u8]) -> usize {
let at = payload.len() - 4 - 10 - 2;
assert_eq!(payload[at], 0x04, "label 4 must be where the tail puts it");
assert_eq!(payload[at + 2], 0x05, "label 5 must follow the role value");
at
}
fn signed_envelope(protected: &[u8], payload: &[u8], key: &Ed25519Identity) -> Vec<u8> {
let signature = key.sign(&cbor::sig_structure_bytes(protected, payload));
encoding::envelope_bytes(protected, payload, &signature)
}
fn protected_for(key: &Ed25519Identity) -> Vec<u8> {
encoding::protected_bytes(encoding::CONTENT_TYPE, &key.public_key_bytes())
}
fn protected_with_kid_len(len: u8) -> Vec<u8> {
let mut out = vec![0xa3, 0x01, 0x27, 0x03, 0x78, 0x26];
out.extend_from_slice(encoding::CONTENT_TYPE.as_bytes());
out.extend_from_slice(&[0x04, 0x58, len]);
out.extend(std::iter::repeat_n(0x11u8, usize::from(len)));
out
}