OXYGENHUB

Coverage / crates/oxy-core/src/policy.rs 95.2%

418 of 439 lines run, 75 of 95 functions · run #356 on main · run missed

1
//! The trust policy (spec §14.5): who may update which references, signed
2
//! by a threshold of root keys. A repository without a policy is unverified.
3
//!
4
//! Encoding: `{0: version, 1: repo, 2: serial, 3: {0: root keys, 1: threshold},
5
//! 4: [rule], 5: [identity bytes], 6: [revoked], 7: expires}`; a rule is
6
//! `{0: pattern, 1: keys, 2: threshold, 3: require, 4: allow_conflicts}` with
7
//! `require` 0 = signed, 1 = sealed, 2 = any.
8
9
use crate::cbor::{self, Value};
10
use crate::hash::{hash_object, KeyId, ObjectRef, ObjectType};
11
use crate::identity::{self, Domain, Identity, Signature};
12
use crate::objects::{
13
    array, bytes, check_version, field, map, oref, text, uint, ObjectError, FORMAT_VERSION,
14
};
15
use std::collections::BTreeMap;
16
17
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
18
pub enum Require {
19
    Signed,
20
    Sealed,
21
    Any,
22
}
23
24
impl Require {
25282×
    fn tag(self) -> u64 {
26282×
        match self {
274×
            Require::Signed => 0,
280×
            Require::Sealed => 1,
29278×
            Require::Any => 2,
30
        }
31282×
    }
32698×
    fn from_tag(t: u64) -> Result<Require, ObjectError> {
33698×
        Ok(match t {
342×
            0 => Require::Signed,
350×
            1 => Require::Sealed,
36696×
            2 => Require::Any,
370×
            _ => return Err(ObjectError::Schema(format!("unknown require {t}"))),
38
        })
39698×
    }
402×
    pub fn name(self) -> &'static str {
412×
        match self {
420×
            Require::Signed => "signed",
430×
            Require::Sealed => "sealed",
442×
            Require::Any => "any",
45
        }
462×
    }
47
}
48
49
#[derive(Clone, Debug, PartialEq, Eq)]
50
pub struct Rule {
51
    /// Glob over reference names; `*` matches any run of characters.
52
    pub pattern: String,
53
    pub keys: Vec<KeyId>,
54
    pub threshold: u32,
55
    pub require: Require,
56
    pub allow_conflicts: bool,
57
    /// Who else may sign, by what an issuer certified (§14.7): a short-lived
58
    /// key whose certificate is the issuer's, for a subject the pattern
59
    /// matches, counts as one signer within the scope given.
60
    pub subjects: Vec<SubjectGrant>,
61
}
62
63
/// A rule's grant to the holders of certificates an issuer gives (§14.7):
64
/// `repo:andy/demo:bookmark:main`, `agent:claude@andy`, with `*` as in
65
/// reference patterns.
66
#[derive(Clone, Debug, PartialEq, Eq)]
67
pub struct SubjectGrant {
68
    pub pattern: String,
69
    pub issuer: KeyId,
70
    pub scope: identity::Scope,
71
}
72
73
impl SubjectGrant {
7448×
    fn to_value(&self) -> Value {
7548×
        Value::map([
7648×
            (0, Value::Text(self.pattern.clone())),
7748×
            (1, Value::Bytes(self.issuer.0.to_vec())),
78
            (
79
                2,
80
                Value::Array(
8148×
                    self.scope
8248×
                        .names()
8348×
                        .into_iter()
8491×
                        .map(|n| Value::Text(n.to_string()))
8548×
                        .collect(),
86
                ),
87
            ),
88
        ])
8948×
    }
90
91149×
    fn from_value(v: &Value) -> Result<SubjectGrant, ObjectError> {
92149×
        let m = map(v, "subject")?;
93149×
        let mut scope = identity::Scope::NONE;
94295×
        for n in array(field(m, 2, "subject scope")?, "subject scope")? {
95295×
            let n = text(n, "subject scope")?;
96295×
            scope = scope
97295×
                | identity::Scope::named(n)
98295×
                    .ok_or_else(|| ObjectError::Schema(format!("subject: unknown scope {n:?}")))?;
99
        }
100
        Ok(SubjectGrant {
101149×
            pattern: text(field(m, 0, "subject pattern")?, "subject pattern")?.to_owned(),
102149×
            issuer: oref(field(m, 1, "subject issuer")?, "subject issuer")?,
103149×
            scope,
104
        })
105149×
    }
106
}
107
108
#[derive(Clone, Debug, PartialEq, Eq)]
109
pub struct Policy {
110
    pub repo: [u8; 16],
111
    pub serial: u64,
112
    pub roots: Vec<KeyId>,
113
    pub root_threshold: u32,
114
    pub rules: Vec<Rule>,
115
    /// Embedded so verification needs no fetch.
116
    pub identities: Vec<Identity>,
117
    pub revoked: Vec<KeyId>,
118
    pub expires: u64,
119
}
120
121
/// `*` matches any run of characters (including `/`); everything else is literal.
122238×
pub fn glob_matches(pattern: &str, name: &str) -> bool {
1231813×
    fn rec(p: &[u8], n: &[u8]) -> bool {
1241813×
        match p.first() {
1251180×
            None => n.is_empty(),
1261167×
            Some(b'*') => (0..=n.len()).any(|i| rec(&p[1..], &n[i..])),
127423×
            Some(c) => n.first() == Some(c) && rec(&p[1..], &n[1..]),
128
        }
1291813×
    }
130238×
    rec(pattern.as_bytes(), name.as_bytes())
131238×
}
132
133
impl Policy {
134838×
    fn keys_value(keys: &[KeyId]) -> Value {
135838×
        Value::Array(keys.iter().map(|k| Value::Bytes(k.0.to_vec())).collect())
136838×
    }
1372090×
    fn keys_from(v: &Value, what: &str) -> Result<Vec<KeyId>, ObjectError> {
1382090×
        let mut ks = array(v, what)?
1392090×
            .iter()
1402090×
            .map(|k| oref(k, what))
1412090×
            .collect::<Result<Vec<_>, _>>()?;
1422090×
        ks.sort();
1432090×
        ks.dedup();
1442090×
        Ok(ks)
1452090×
    }
146
147278×
    pub fn to_value(&self) -> Value {
148278×
        Value::map([
149278×
            (0, Value::Uint(FORMAT_VERSION)),
150278×
            (1, Value::Bytes(self.repo.to_vec())),
151278×
            (2, Value::Uint(self.serial)),
152278×
            (
153278×
                3,
154278×
                Value::map([
155278×
                    (0, Self::keys_value(&self.roots)),
156278×
                    (1, Value::Uint(self.root_threshold as u64)),
157278×
                ]),
158278×
            ),
159
            (
160
                4,
161
                Value::Array(
162278×
                    self.rules
163278×
                        .iter()
164282×
                        .map(|r| {
165282×
                            let mut v = Value::map([
166282×
                                (0, Value::Text(r.pattern.clone())),
167282×
                                (1, Self::keys_value(&r.keys)),
168282×
                                (2, Value::Uint(r.threshold as u64)),
169282×
                                (3, Value::Uint(r.require.tag())),
170282×
                                (4, Value::Bool(r.allow_conflicts)),
171282×
                            ]);
172
                            // Only when there are any, so a policy without
173
                            // them encodes, and hashes, as before.
174282×
                            if !r.subjects.is_empty() {
17547×
                                if let Value::Map(m) = &mut v {
17647×
                                    m.insert(
17747×
                                        5,
17847×
                                        Value::Array(
17947×
                                            r.subjects.iter().map(SubjectGrant::to_value).collect(),
18047×
                                        ),
18147×
                                    );
18247×
                                }
183235×
                            }
184282×
                            v
185282×
                        })
186278×
                        .collect(),
187
                ),
188
            ),
189
            (
190
                5,
191
                Value::Array(
192278×
                    self.identities
193278×
                        .iter()
194565×
                        .map(|i| Value::Bytes(i.encode()))
195278×
                        .collect(),
196
                ),
197
            ),
198278×
            (6, Self::keys_value(&self.revoked)),
199278×
            (7, Value::Uint(self.expires)),
200
        ])
201278×
    }
202
203278×
    pub fn encode(&self) -> Vec<u8> {
204278×
        cbor::encode(&self.to_value())
205278×
    }
206
207160×
    pub fn hash(&self) -> ObjectRef {
208160×
        hash_object(ObjectType::Policy, &self.encode())
209160×
    }
210
211696×
    pub fn decode(b: &[u8]) -> Result<Policy, ObjectError> {
212696×
        let v = cbor::decode(b)?;
213696×
        let m = map(&v, "policy")?;
214696×
        check_version(m)?;
215696×
        let roots = map(field(m, 3, "roots")?, "roots")?;
216696×
        let mut rules = Vec::new();
217698×
        for r in array(field(m, 4, "rules")?, "rules")? {
218698×
            let rm = map(r, "rule")?;
219698×
            rules.push(Rule {
220698×
                pattern: text(field(rm, 0, "pattern")?, "pattern")?.to_owned(),
221698×
                keys: Self::keys_from(field(rm, 1, "rule keys")?, "rule keys")?,
222698×
                threshold: uint(field(rm, 2, "threshold")?, "threshold")? as u32,
223698×
                require: Require::from_tag(uint(field(rm, 3, "require")?, "require")?)?,
224698×
                allow_conflicts: field(rm, 4, "allow_conflicts")?
225698×
                    .as_bool()
226698×
                    .ok_or_else(|| ObjectError::Schema("allow_conflicts: expected bool".into()))?,
227698×
                subjects: match rm.get(&5) {
228550×
                    None => vec![],
229148×
                    Some(v) => array(v, "subjects")?
230148×
                        .iter()
231148×
                        .map(SubjectGrant::from_value)
232148×
                        .collect::<Result<_, _>>()?,
233
                },
234
            });
235
        }
236696×
        let identities = array(field(m, 5, "identities")?, "identities")?
237696×
            .iter()
2381360×
            .map(|i| Identity::decode(bytes(i, "identity")?))
239696×
            .collect::<Result<Vec<_>, _>>()?;
240696×
        let p = Policy {
241696×
            repo: bytes(field(m, 1, "repo")?, "repo")?
242696×
                .try_into()
243696×
                .map_err(|_| ObjectError::Schema("repo: expected 16 bytes".into()))?,
244696×
            serial: uint(field(m, 2, "serial")?, "serial")?,
245696×
            roots: Self::keys_from(field(roots, 0, "root keys")?, "root keys")?,
246696×
            root_threshold: uint(field(roots, 1, "root threshold")?, "root threshold")? as u32,
247696×
            rules,
248696×
            identities,
249696×
            revoked: Self::keys_from(field(m, 6, "revoked")?, "revoked")?,
250696×
            expires: uint(field(m, 7, "expires")?, "expires")?,
251
        };
252696×
        if p.root_threshold == 0 || p.root_threshold as usize > p.roots.len() {
2531×
            return Err(ObjectError::Schema(
2541×
                "root threshold must be between 1 and the number of root keys".into(),
2551×
            ));
256695×
        }
257696×
        for r in &p.rules {
258696×
            if r.threshold == 0 || r.threshold as usize > r.keys.len() + r.subjects.len() {
2591×
                return Err(ObjectError::Schema(format!(
2601×
                    "rule {:?}: threshold must be between 1 and the number of keys and subjects",
2611×
                    r.pattern
2621×
                )));
263695×
            }
264
        }
265694×
        Ok(p)
266696×
    }
267
268226×
    pub fn identity(&self, key: &KeyId) -> Option<&Identity> {
269367×
        self.identities.iter().find(|i| i.hash() == *key)
270226×
    }
271
272446×
    pub fn is_revoked(&self, key: &KeyId) -> bool {
273446×
        self.revoked.contains(key)
274446×
    }
275
2767×
    pub fn is_expired(&self, now_ms: u64) -> bool {
2777×
        now_ms > self.expires
2787×
    }
279
280
    /// The first rule whose pattern matches `reference`.
281205×
    pub fn rule_for(&self, reference: &str) -> Option<&Rule> {
282205×
        self.rules
283205×
            .iter()
284206×
            .find(|r| glob_matches(&r.pattern, reference))
285205×
    }
286
287
    /// Whether the rule for `reference` lets a certificate from `issuer`
288
    /// for `subject` sign within `scope` (§14.7). The certificate itself,
289
    /// and the time it is checked at, are the caller's to verify.
29026×
    pub fn subject_grant(
29126×
        &self,
29226×
        reference: &str,
29326×
        issuer: &KeyId,
29426×
        subject: &str,
29526×
        scope: identity::Scope,
29626×
    ) -> bool {
29726×
        !self.is_revoked(issuer)
29825×
            && self.rule_for(reference).is_some_and(|r| {
29929×
                r.subjects.iter().any(|g| {
30029×
                    g.issuer == *issuer && g.scope.has(scope) && glob_matches(&g.pattern, subject)
30129×
                })
30225×
            })
30326×
    }
304
305
    /// Whether `key` may update `reference`: listed in the matching rule and not revoked.
30679×
    pub fn authorizes(&self, key: &KeyId, reference: &str) -> bool {
30779×
        !self.is_revoked(key)
30878×
            && self
30978×
                .rule_for(reference)
31078×
                .is_some_and(|r| r.keys.contains(key))
31179×
    }
312
313
    /// Verify that `sigs` over this policy's hash meet `trusted`'s root
314
    /// threshold (`trusted` is the previous policy, or this one when
315
    /// bootstrapping). Returns the signing root keys.
31681×
    pub fn verify_signatures(
31781×
        &self,
31881×
        sigs: &[Signature],
31981×
        trusted: &Policy,
32081×
    ) -> Result<Vec<KeyId>, String> {
32181×
        let target = self.hash();
32281×
        let mut signers = Vec::new();
323112×
        for sig in sigs {
324112×
            if !trusted.roots.contains(&sig.key) || trusted.is_revoked(&sig.key) {
32525×
                continue;
32687×
            }
32787×
            let Some(id) = trusted
32887×
                .identity(&sig.key)
32987×
                .or_else(|| self.identity(&sig.key))
330
            else {
3310×
                continue;
332
            };
33387×
            if identity::verify(id, sig, Domain::Policy, &target).is_ok()
33487×
                && !signers.contains(&sig.key)
33586×
            {
33686×
                signers.push(sig.key);
33786×
            }
338
        }
33981×
        if (signers.len() as u32) < trusted.root_threshold {
3406×
            return Err(format!(
3416×
                "policy needs {} root signature(s), has {}",
3426×
                trusted.root_threshold,
3436×
                signers.len()
3446×
            ));
34575×
        }
34675×
        Ok(signers)
34781×
    }
348
349
    /// Bootstrap check for the first policy a repository sees (trust on
350
    /// first use): at least one valid signature by one of its own roots.
351
    /// The full threshold applies only to updates from a trusted policy,
352
    /// whose signers are the previous roots and need not be the new ones.
35326×
    pub fn verify_bootstrap(&self, sigs: &[Signature]) -> Result<KeyId, String> {
35426×
        let target = self.hash();
35526×
        for sig in sigs {
35626×
            if !self.roots.contains(&sig.key) || self.is_revoked(&sig.key) {
3570×
                continue;
35826×
            }
35926×
            if let Some(id) = self.identity(&sig.key) {
36026×
                if identity::verify(id, sig, Domain::Policy, &target).is_ok() {
36126×
                    return Ok(sig.key);
3620×
                }
3630×
            }
364
        }
3650×
        Err("policy carries no valid signature by one of its own root keys".into())
36626×
    }
367
368
    /// A single-root policy letting `root` do everything on every reference.
36920×
    pub fn single_root(repo: [u8; 16], root: Identity, expires: u64) -> Policy {
37020×
        let key = root.hash();
37120×
        Policy {
37220×
            repo,
37320×
            serial: 1,
37420×
            roots: vec![key],
37520×
            root_threshold: 1,
37620×
            rules: vec![Rule {
37720×
                pattern: "*".into(),
37820×
                keys: vec![key],
37920×
                threshold: 1,
38020×
                require: Require::Any,
38120×
                allow_conflicts: true,
38220×
                subjects: vec![],
38320×
            }],
38420×
            identities: vec![root],
38520×
            revoked: vec![],
38620×
            expires,
38720×
        }
38820×
    }
389
390
    /// Whether `key` appears anywhere in the policy (roots, rules, identities).
39110×
    pub fn mentions(&self, key: &KeyId) -> bool {
39210×
        self.roots.contains(key)
3931×
            || self.rules.iter().any(|r| r.keys.contains(key))
3940×
            || self.identity(key).is_some()
39510×
    }
396
397
    /// Whether `key` is `rule_key` or one of its predecessors in the
398
    /// rotation chain (`previous` links, spec §14.2), so that changes signed
399
    /// before a rotation remain authorized. Revoked keys never qualify.
40037×
    pub fn succeeds(&self, rule_key: &KeyId, key: &KeyId) -> bool {
40137×
        if self.is_revoked(key) {
4020×
            return false;
40337×
        }
40437×
        let mut cur = Some(*rule_key);
40537×
        for _ in 0..32 {
40648×
            match cur {
40748×
                Some(k) if k == *key => return true,
40823×
                Some(k) => cur = self.identity(&k).and_then(|i| i.previous),
40912×
                None => return false,
410
            }
411
        }
4120×
        false
41337×
    }
414
415
    /// Replace `old` by `new` in the roots and every rule and embed `new`'s
416
    /// identity (a rotation, spec §14.2). The old identity stays embedded so
417
    /// signatures made before the rotation still verify; it is superseded,
418
    /// not revoked.
4199×
    pub fn substitute_key(&mut self, old: &KeyId, new: Identity) {
4209×
        let nk = new.hash();
42118×
        for k in self
4229×
            .roots
4239×
            .iter_mut()
4249×
            .chain(self.rules.iter_mut().flat_map(|r| r.keys.iter_mut()))
425
        {
42618×
            if k == old {
42718×
                *k = nk;
42818×
            }
429
        }
4309×
        for r in &mut self.rules {
4319×
            r.keys.sort();
4329×
            r.keys.dedup();
4339×
        }
4349×
        self.roots.sort();
4359×
        self.roots.dedup();
4369×
        if self.identity(&nk).is_none() {
4379×
            self.identities.push(new);
4389×
        }
4399×
    }
440
441
    /// Identities keyed by KeyId.
4420×
    pub fn identity_map(&self) -> BTreeMap<KeyId, &Identity> {
4430×
        self.identities.iter().map(|i| (i.hash(), i)).collect()
4440×
    }
445
}
446
447
#[cfg(test)]
448
mod tests {
449
    use super::*;
450
    use crate::identity::SecretKey;
451
4527×
    fn sk(n: u8) -> SecretKey {
4537×
        SecretKey {
4547×
            ed_seed: [n; 32],
4557×
            ml_seed: [n + 100; 32],
4567×
        }
4577×
    }
458
459
    #[test]
4601×
    fn globs() {
4611×
        assert!(glob_matches("*", "main"));
4621×
        assert!(glob_matches("release/*", "release/1.0"));
4631×
        assert!(!glob_matches("release/*", "main"));
4641×
        assert!(glob_matches("main", "main"));
4651×
        assert!(!glob_matches("main", "main2"));
4661×
        assert!(glob_matches("*-rc*", "1.0-rc2"));
4671×
        assert!(glob_matches("", ""));
4681×
    }
469
470
    /// Subjects an issuer certifies (oxygenhub#241): kept through
471
    /// encoding, absent from a policy without them, and granted only for
472
    /// their issuer, pattern and scope, and not when the issuer is revoked.
473
    #[test]
4741×
    fn a_rule_grants_certified_subjects() {
475
        use crate::identity::Scope;
4761×
        let root = sk(1).identity("Root", "r@x", 0);
4771×
        let hub = sk(3).identity("Hub", "h@x", 0);
4781×
        let mut p = Policy::single_root([9; 16], root.clone(), 10);
4791×
        let plain = p.encode();
4801×
        p.rules[0].subjects.push(SubjectGrant {
4811×
            pattern: "repo:andy/demo:bookmark:main".into(),
4821×
            issuer: hub.hash(),
4831×
            scope: Scope::REFS | Scope::CHANGES,
4841×
        });
4851×
        p.rules[0].subjects.push(SubjectGrant {
4861×
            pattern: "agent:*@andy".into(),
4871×
            issuer: hub.hash(),
4881×
            scope: Scope::CHANGES,
4891×
        });
4901×
        let back = Policy::decode(&p.encode()).unwrap();
4911×
        assert_eq!(back, p);
4921×
        let mut again = back.clone();
4931×
        again.rules[0].subjects.clear();
4941×
        assert_eq!(
4951×
            again.encode(),
496
            plain,
497
            "no subjects, the same bytes as before"
498
        );
499
5001×
        let main = "repo:andy/demo:bookmark:main";
5011×
        assert!(p.subject_grant("main", &hub.hash(), main, Scope::REFS));
5021×
        assert!(p.subject_grant("main", &hub.hash(), "agent:claude@andy", Scope::CHANGES));
5031×
        assert!(
5041×
            !p.subject_grant("main", &hub.hash(), "agent:claude@andy", Scope::REFS),
505
            "not its scope"
506
        );
5071×
        assert!(
5081×
            !p.subject_grant(
5091×
                "main",
5101×
                &hub.hash(),
5111×
                "repo:andy/demo:bookmark:dev",
5121×
                Scope::REFS
5131×
            ),
514
            "not its pattern"
515
        );
5161×
        assert!(
5171×
            !p.subject_grant("main", &root.hash(), main, Scope::REFS),
518
            "not its issuer"
519
        );
5201×
        p.revoked.push(hub.hash());
5211×
        assert!(
5221×
            !p.subject_grant("main", &hub.hash(), main, Scope::REFS),
523
            "a revoked issuer grants nothing"
524
        );
525
526
        // A rule of subjects alone is a rule.
5271×
        let mut only = Policy::single_root([9; 16], root, 10);
5281×
        only.rules[0].keys.clear();
5291×
        only.rules[0].subjects.push(SubjectGrant {
5301×
            pattern: "*".into(),
5311×
            issuer: hub.hash(),
5321×
            scope: Scope::REFS,
5331×
        });
5341×
        assert!(Policy::decode(&only.encode()).is_ok());
5351×
        only.rules[0].subjects.clear();
5361×
        assert!(Policy::decode(&only.encode()).is_err(), "nobody to sign");
5371×
    }
538
539
    #[test]
5401×
    fn round_trip_and_rules() {
5411×
        let root = sk(1).identity("Root", "r@x", 0);
5421×
        let dev = sk(2).identity("Dev", "d@x", 0);
5431×
        let mut p = Policy::single_root([9; 16], root.clone(), 10);
5441×
        p.rules.insert(
545
            0,
5461×
            Rule {
5471×
                pattern: "release/*".into(),
5481×
                keys: vec![root.hash()],
5491×
                threshold: 1,
5501×
                require: Require::Signed,
5511×
                allow_conflicts: false,
5521×
                subjects: vec![],
5531×
            },
554
        );
5551×
        p.rules[1].keys.push(dev.hash());
5561×
        p.identities.push(dev.clone());
5571×
        p.revoked.push(ObjectRef([7; 32]));
5581×
        let d = Policy::decode(&p.encode()).unwrap();
5591×
        assert_eq!(d.rules.len(), 2);
5601×
        assert_eq!(d.rules[0].require, Require::Signed);
5611×
        assert_eq!(d.identity(&dev.hash()), Some(&dev));
5621×
        assert!(d.authorizes(&dev.hash(), "main"));
5631×
        assert!(!d.authorizes(&dev.hash(), "release/1"));
5641×
        assert!(d.authorizes(&root.hash(), "release/1"));
5651×
        assert!(d.is_expired(11) && !d.is_expired(10));
5661×
        assert_eq!(d.hash(), p.hash());
567
5681×
        let mut bad = p.clone();
5691×
        bad.root_threshold = 5;
5701×
        assert!(Policy::decode(&bad.encode()).is_err());
5711×
    }
572
573
    #[test]
5741×
    fn signatures_need_root_threshold() {
5751×
        let r1 = sk(1);
5761×
        let r2 = sk(2);
5771×
        let i1 = r1.identity("a", "a", 0);
5781×
        let i2 = r2.identity("b", "b", 0);
5791×
        let mut p = Policy::single_root([1; 16], i1.clone(), 100);
5801×
        p.roots.push(i2.hash());
5811×
        p.roots.sort();
5821×
        p.root_threshold = 2;
5831×
        p.identities.push(i2.clone());
5841×
        let h = p.hash();
5851×
        let s1 = r1.sign(Domain::Policy, &i1.hash(), &h);
5861×
        let s2 = r2.sign(Domain::Policy, &i2.hash(), &h);
5871×
        assert!(p.verify_signatures(std::slice::from_ref(&s1), &p).is_err());
5881×
        assert_eq!(
5891×
            p.verify_signatures(&[s1.clone(), s2.clone()], &p)
5901×
                .unwrap()
5911×
                .len(),
592
            2
593
        );
594
        // Duplicates do not count twice; a non-root signer does not count.
5951×
        assert!(p.verify_signatures(&[s1.clone(), s1.clone()], &p).is_err());
5961×
        let stranger = sk(3);
5971×
        let si = stranger.identity("s", "s", 0);
5981×
        let s3 = stranger.sign(Domain::Policy, &si.hash(), &h);
5991×
        assert!(p.verify_signatures(&[s1.clone(), s3], &p).is_err());
600
        // A successor policy is verified against the previous roots.
6011×
        let mut next = p.clone();
6021×
        next.serial = 2;
6031×
        next.revoked.push(i2.hash());
6041×
        let nh = next.hash();
6051×
        let n1 = r1.sign(Domain::Policy, &i1.hash(), &nh);
6061×
        let n2 = r2.sign(Domain::Policy, &i2.hash(), &nh);
6071×
        assert_eq!(next.verify_signatures(&[n1, n2], &p).unwrap().len(), 2);
6081×
    }
609
}