OXYGENHUB

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

418 of 439 lines run, 78 of 95 functions · run #427 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 {
25396×
    fn tag(self) -> u64 {
26396×
        match self {
274×
            Require::Signed => 0,
280×
            Require::Sealed => 1,
29392×
            Require::Any => 2,
30
        }
31396×
    }
32982×
    fn from_tag(t: u64) -> Result<Require, ObjectError> {
33982×
        Ok(match t {
342×
            0 => Require::Signed,
350×
            1 => Require::Sealed,
36980×
            2 => Require::Any,
370×
            _ => return Err(ObjectError::Schema(format!("unknown require {t}"))),
38
        })
39982×
    }
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 {
74102×
    fn to_value(&self) -> Value {
75102×
        Value::map([
76102×
            (0, Value::Text(self.pattern.clone())),
77102×
            (1, Value::Bytes(self.issuer.0.to_vec())),
78
            (
79
                2,
80
                Value::Array(
81102×
                    self.scope
82102×
                        .names()
83102×
                        .into_iter()
84199×
                        .map(|n| Value::Text(n.to_string()))
85102×
                        .collect(),
86
                ),
87
            ),
88
        ])
89102×
    }
90
91301×
    fn from_value(v: &Value) -> Result<SubjectGrant, ObjectError> {
92301×
        let m = map(v, "subject")?;
93301×
        let mut scope = identity::Scope::NONE;
94599×
        for n in array(field(m, 2, "subject scope")?, "subject scope")? {
95599×
            let n = text(n, "subject scope")?;
96599×
            scope = scope
97599×
                | identity::Scope::named(n)
98599×
                    .ok_or_else(|| ObjectError::Schema(format!("subject: unknown scope {n:?}")))?;
99
        }
100
        Ok(SubjectGrant {
101301×
            pattern: text(field(m, 0, "subject pattern")?, "subject pattern")?.to_owned(),
102301×
            issuer: oref(field(m, 1, "subject issuer")?, "subject issuer")?,
103301×
            scope,
104
        })
105301×
    }
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.
122426×
pub fn glob_matches(pattern: &str, name: &str) -> bool {
1233459×
    fn rec(p: &[u8], n: &[u8]) -> bool {
1243459×
        match p.first() {
1252308×
            None => n.is_empty(),
1262273×
            Some(b'*') => (0..=n.len()).any(|i| rec(&p[1..], &n[i..])),
127775×
            Some(c) => n.first() == Some(c) && rec(&p[1..], &n[1..]),
128
        }
1293459×
    }
130426×
    rec(pattern.as_bytes(), name.as_bytes())
131426×
}
132
133
impl Policy {
1341180×
    fn keys_value(keys: &[KeyId]) -> Value {
1351180×
        Value::Array(keys.iter().map(|k| Value::Bytes(k.0.to_vec())).collect())
1361180×
    }
1372942×
    fn keys_from(v: &Value, what: &str) -> Result<Vec<KeyId>, ObjectError> {
1382942×
        let mut ks = array(v, what)?
1392942×
            .iter()
1402942×
            .map(|k| oref(k, what))
1412942×
            .collect::<Result<Vec<_>, _>>()?;
1422942×
        ks.sort();
1432942×
        ks.dedup();
1442942×
        Ok(ks)
1452942×
    }
146
147392×
    pub fn to_value(&self) -> Value {
148392×
        Value::map([
149392×
            (0, Value::Uint(FORMAT_VERSION)),
150392×
            (1, Value::Bytes(self.repo.to_vec())),
151392×
            (2, Value::Uint(self.serial)),
152392×
            (
153392×
                3,
154392×
                Value::map([
155392×
                    (0, Self::keys_value(&self.roots)),
156392×
                    (1, Value::Uint(self.root_threshold as u64)),
157392×
                ]),
158392×
            ),
159
            (
160
                4,
161
                Value::Array(
162392×
                    self.rules
163392×
                        .iter()
164396×
                        .map(|r| {
165396×
                            let mut v = Value::map([
166396×
                                (0, Value::Text(r.pattern.clone())),
167396×
                                (1, Self::keys_value(&r.keys)),
168396×
                                (2, Value::Uint(r.threshold as u64)),
169396×
                                (3, Value::Uint(r.require.tag())),
170396×
                                (4, Value::Bool(r.allow_conflicts)),
171396×
                            ]);
172
                            // Only when there are any, so a policy without
173
                            // them encodes, and hashes, as before.
174396×
                            if !r.subjects.is_empty() {
175101×
                                if let Value::Map(m) = &mut v {
176101×
                                    m.insert(
177101×
                                        5,
178101×
                                        Value::Array(
179101×
                                            r.subjects.iter().map(SubjectGrant::to_value).collect(),
180101×
                                        ),
181101×
                                    );
182101×
                                }
183295×
                            }
184396×
                            v
185396×
                        })
186392×
                        .collect(),
187
                ),
188
            ),
189
            (
190
                5,
191
                Value::Array(
192392×
                    self.identities
193392×
                        .iter()
194733×
                        .map(|i| Value::Bytes(i.encode()))
195392×
                        .collect(),
196
                ),
197
            ),
198392×
            (6, Self::keys_value(&self.revoked)),
199392×
            (7, Value::Uint(self.expires)),
200
        ])
201392×
    }
202
203392×
    pub fn encode(&self) -> Vec<u8> {
204392×
        cbor::encode(&self.to_value())
205392×
    }
206
207209×
    pub fn hash(&self) -> ObjectRef {
208209×
        hash_object(ObjectType::Policy, &self.encode())
209209×
    }
210
211980×
    pub fn decode(b: &[u8]) -> Result<Policy, ObjectError> {
212980×
        let v = cbor::decode(b)?;
213980×
        let m = map(&v, "policy")?;
214980×
        check_version(m)?;
215980×
        let roots = map(field(m, 3, "roots")?, "roots")?;
216980×
        let mut rules = Vec::new();
217982×
        for r in array(field(m, 4, "rules")?, "rules")? {
218982×
            let rm = map(r, "rule")?;
219982×
            rules.push(Rule {
220982×
                pattern: text(field(rm, 0, "pattern")?, "pattern")?.to_owned(),
221982×
                keys: Self::keys_from(field(rm, 1, "rule keys")?, "rule keys")?,
222982×
                threshold: uint(field(rm, 2, "threshold")?, "threshold")? as u32,
223982×
                require: Require::from_tag(uint(field(rm, 3, "require")?, "require")?)?,
224982×
                allow_conflicts: field(rm, 4, "allow_conflicts")?
225982×
                    .as_bool()
226982×
                    .ok_or_else(|| ObjectError::Schema("allow_conflicts: expected bool".into()))?,
227982×
                subjects: match rm.get(&5) {
228682×
                    None => vec![],
229300×
                    Some(v) => array(v, "subjects")?
230300×
                        .iter()
231300×
                        .map(SubjectGrant::from_value)
232300×
                        .collect::<Result<_, _>>()?,
233
                },
234
            });
235
        }
236980×
        let identities = array(field(m, 5, "identities")?, "identities")?
237980×
            .iter()
2381794×
            .map(|i| Identity::decode(bytes(i, "identity")?))
239980×
            .collect::<Result<Vec<_>, _>>()?;
240980×
        let p = Policy {
241980×
            repo: bytes(field(m, 1, "repo")?, "repo")?
242980×
                .try_into()
243980×
                .map_err(|_| ObjectError::Schema("repo: expected 16 bytes".into()))?,
244980×
            serial: uint(field(m, 2, "serial")?, "serial")?,
245980×
            roots: Self::keys_from(field(roots, 0, "root keys")?, "root keys")?,
246980×
            root_threshold: uint(field(roots, 1, "root threshold")?, "root threshold")? as u32,
247980×
            rules,
248980×
            identities,
249980×
            revoked: Self::keys_from(field(m, 6, "revoked")?, "revoked")?,
250980×
            expires: uint(field(m, 7, "expires")?, "expires")?,
251
        };
252980×
        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×
            ));
256979×
        }
257980×
        for r in &p.rules {
258980×
            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×
                )));
263979×
            }
264
        }
265978×
        Ok(p)
266980×
    }
267
268346×
    pub fn identity(&self, key: &KeyId) -> Option<&Identity> {
269515×
        self.identities.iter().find(|i| i.hash() == *key)
270346×
    }
271
272701×
    pub fn is_revoked(&self, key: &KeyId) -> bool {
273701×
        self.revoked.contains(key)
274701×
    }
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`.
281371×
    pub fn rule_for(&self, reference: &str) -> Option<&Rule> {
282371×
        self.rules
283371×
            .iter()
284372×
            .find(|r| glob_matches(&r.pattern, reference))
285371×
    }
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.
29048×
    pub fn subject_grant(
29148×
        &self,
29248×
        reference: &str,
29348×
        issuer: &KeyId,
29448×
        subject: &str,
29548×
        scope: identity::Scope,
29648×
    ) -> bool {
29748×
        !self.is_revoked(issuer)
29847×
            && self.rule_for(reference).is_some_and(|r| {
29951×
                r.subjects.iter().any(|g| {
30051×
                    g.issuer == *issuer && g.scope.has(scope) && glob_matches(&g.pattern, subject)
30151×
                })
30247×
            })
30348×
    }
304
305
    /// Whether `key` may update `reference`: listed in the matching rule and not revoked.
306149×
    pub fn authorizes(&self, key: &KeyId, reference: &str) -> bool {
307149×
        !self.is_revoked(key)
308148×
            && self
309148×
                .rule_for(reference)
310148×
                .is_some_and(|r| r.keys.contains(key))
311149×
    }
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.
31699×
    pub fn verify_signatures(
31799×
        &self,
31899×
        sigs: &[Signature],
31999×
        trusted: &Policy,
32099×
    ) -> Result<Vec<KeyId>, String> {
32199×
        let target = self.hash();
32299×
        let mut signers = Vec::new();
323130×
        for sig in sigs {
324130×
            if !trusted.roots.contains(&sig.key) || trusted.is_revoked(&sig.key) {
32525×
                continue;
326105×
            }
327105×
            let Some(id) = trusted
328105×
                .identity(&sig.key)
329105×
                .or_else(|| self.identity(&sig.key))
330
            else {
3310×
                continue;
332
            };
333105×
            if identity::verify(id, sig, Domain::Policy, &target).is_ok()
334105×
                && !signers.contains(&sig.key)
335104×
            {
336104×
                signers.push(sig.key);
337104×
            }
338
        }
33999×
        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×
            ));
34593×
        }
34693×
        Ok(signers)
34799×
    }
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.
35340×
    pub fn verify_bootstrap(&self, sigs: &[Signature]) -> Result<KeyId, String> {
35440×
        let target = self.hash();
35540×
        for sig in sigs {
35640×
            if !self.roots.contains(&sig.key) || self.is_revoked(&sig.key) {
3570×
                continue;
35840×
            }
35940×
            if let Some(id) = self.identity(&sig.key) {
36040×
                if identity::verify(id, sig, Domain::Policy, &target).is_ok() {
36140×
                    return Ok(sig.key);
3620×
                }
3630×
            }
364
        }
3650×
        Err("policy carries no valid signature by one of its own root keys".into())
36640×
    }
367
368
    /// A single-root policy letting `root` do everything on every reference.
36924×
    pub fn single_root(repo: [u8; 16], root: Identity, expires: u64) -> Policy {
37024×
        let key = root.hash();
37124×
        Policy {
37224×
            repo,
37324×
            serial: 1,
37424×
            roots: vec![key],
37524×
            root_threshold: 1,
37624×
            rules: vec![Rule {
37724×
                pattern: "*".into(),
37824×
                keys: vec![key],
37924×
                threshold: 1,
38024×
                require: Require::Any,
38124×
                allow_conflicts: true,
38224×
                subjects: vec![],
38324×
            }],
38424×
            identities: vec![root],
38524×
            revoked: vec![],
38624×
            expires,
38724×
        }
38824×
    }
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.
40042×
    pub fn succeeds(&self, rule_key: &KeyId, key: &KeyId) -> bool {
40142×
        if self.is_revoked(key) {
4020×
            return false;
40342×
        }
40442×
        let mut cur = Some(*rule_key);
40542×
        for _ in 0..32 {
40653×
            match cur {
40753×
                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
41342×
    }
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
}