structured-pkix 0.6.1

Borrowed X.509 certificates and provider-neutral certification path validation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
use super::{Admission, NameSpace};
use crate::constraints::NameConstraints;
use crate::fixtures::{
    CertificateParts, cat, context, extension, ia5, oid as oid_of, printable, seq, set, tlv, utf8,
};
use crate::{Certificate, oid};
use alloc::vec::Vec;
use structured_asn1::Parser;

#[test]
fn located_exclusion_identifies_the_authority_that_forbade_the_name() {
    // A permitted-set miss at an earlier authority must not hide a later
    // explicit exclusion. Report the actual constraining source to the caller.
    // RFC5280 §6.1.3(b,c)/§6.1.4(g): all accumulated constraints apply.
    let first_der = CertificateParts {
        extensions: Some(seq(&[extension(
            oid::NAME_CONSTRAINTS,
            true,
            &constraints(&[subtree(dns("allowed.example"))], &[]),
        )])),
        ..CertificateParts::default()
    }
    .build();
    let second_der = CertificateParts {
        extensions: Some(seq(&[extension(
            oid::NAME_CONSTRAINTS,
            true,
            &constraints(&[], &[subtree(dns("forbidden.example"))]),
        )])),
        ..CertificateParts::default()
    }
    .build();
    let target_der = CertificateParts {
        subject: seq(&[]),
        extensions: Some(seq(&[extension(
            oid::SUBJECT_ALT_NAME,
            true,
            &seq(&[dns("forbidden.example")]),
        )])),
        ..CertificateParts::default()
    }
    .build();
    let parser = Parser::der(crate::fixtures::memory());
    let first = Certificate::read(&parser, &first_der, 0).unwrap();
    let second = Certificate::read(&parser, &second_der, 0).unwrap();
    let target = Certificate::read(&parser, &target_der, 0).unwrap();
    let alternatives = target
        .tbs()
        .extensions()
        .subject_alt_name(parser.memory())
        .unwrap()
        .unwrap();
    let permitted_source = "permitting authority";
    let excluded_source = "excluding authority";
    let mut space = NameSpace::with_sources(parser.memory());
    space.narrow_from(of(&first), permitted_source).unwrap();
    space.narrow_from(of(&second), excluded_source).unwrap();
    assert_eq!(
        space
            .admits_located(target.tbs().subject(), &alternatives, &(), parser.memory())
            .unwrap(),
        (Admission::Excluded, Some(excluded_source))
    );
}

/// A distinguished name of one common name.
fn named(common: &str) -> Vec<u8> {
    seq(&[set(&[seq(&[oid_of(oid::COMMON_NAME), printable(common)])])])
}

/// A distinguished name of one organization, which is what an authority
/// constrained to one organization's names is written as.
fn organization(name: &str) -> Vec<u8> {
    seq(&[set(&[seq(&[oid_of(oid::ORGANIZATION), printable(name)])])])
}

/// A distinguished name of an organization and a common name under it.
fn under(organization: &str, common: &str) -> Vec<u8> {
    seq(&[
        set(&[seq(&[oid_of(oid::ORGANIZATION), printable(organization)])]),
        set(&[seq(&[oid_of(oid::COMMON_NAME), printable(common)])]),
    ])
}

/// A subtree of one base, which is the only field the profile permits.
fn subtree(base: Vec<u8>) -> Vec<u8> {
    seq(&[base])
}

/// The constraints one certificate carries, read through the crate's own
/// accessor.
fn of<'a>(certificate: &Certificate<'a>) -> NameConstraints<'a> {
    certificate
        .tbs()
        .extensions()
        .name_constraints(certificate.tbs().extensions().memory())
        .expect("the extension this test wrote")
        .expect("a value this test wrote")
}

/// The name-constraints extension value: the permitted subtrees, then the
/// excluded ones, each left out where the test names none.
fn constraints(permitted: &[Vec<u8>], excluded: &[Vec<u8>]) -> Vec<u8> {
    let mut parts = Vec::new();
    if !permitted.is_empty() {
        parts.push(context(0, permitted));
    }
    if !excluded.is_empty() {
        parts.push(context(1, excluded));
    }
    seq(&parts)
}

/// A mail name, as a subtree base or an alternative name.
fn mail(address: &str) -> Vec<u8> {
    tlv(0x81, address.as_bytes())
}

/// A domain name.
fn dns(host: &str) -> Vec<u8> {
    tlv(0x82, host.as_bytes())
}

/// A distinguished name, in the one form that is constructed.
fn directory(name: Vec<u8>) -> Vec<u8> {
    context(4, &[name])
}

/// A location.
fn uri(location: &str) -> Vec<u8> {
    tlv(0x86, location.as_bytes())
}

/// An address, or an address and its mask where the test writes one.
fn address(octets: &[u8]) -> Vec<u8> {
    tlv(0x87, octets)
}

/// A name form this crate matches no constraint against.
fn registered() -> Vec<u8> {
    tlv(0x88, oid::ANY_POLICY.as_bytes())
}

/// A name form defined elsewhere, kept as its identifier and its value.
fn other() -> Vec<u8> {
    tlv(
        0xa0,
        &cat(&[oid_of(oid::ANY_POLICY), context(0, &[utf8("x")])]),
    )
}

/// The verdict on one certificate carrying the given subject and
/// alternative names, against one set of constraints.
///
/// Everything is read here and nothing is handed back, because a certificate
/// and everything read from it borrow the bytes, and the bytes live for as
/// long as this call.
fn admission(
    permitted: &[Vec<u8>],
    excluded: &[Vec<u8>],
    subject: Vec<u8>,
    alternatives: &[Vec<u8>],
) -> Admission {
    let mut extensions = alloc::vec![extension(
        oid::NAME_CONSTRAINTS,
        true,
        &constraints(permitted, excluded),
    )];
    if !alternatives.is_empty() {
        // Critical, because a certificate whose subject is empty carries its
        // identity here and has to mark it so (RFC 5280 4.2.1.6). Whether it
        // is marked changes nothing about where the names sit.
        extensions.push(extension(oid::SUBJECT_ALT_NAME, true, &seq(alternatives)));
    }
    let der = CertificateParts {
        subject,
        extensions: Some(seq(&extensions)),
        ..CertificateParts::default()
    }
    .build();

    let parser = Parser::der(crate::fixtures::memory());
    let certificate = Certificate::read(&parser, &der, 0).expect("a certificate this test wrote");
    let read = certificate
        .tbs()
        .extensions()
        .name_constraints(certificate.tbs().extensions().memory())
        .expect("the extension this test wrote")
        .expect("a value this test wrote");
    let names = certificate
        .tbs()
        .extensions()
        .subject_alt_name(certificate.tbs().extensions().memory())
        .expect("a value this test wrote")
        .unwrap_or_else(|| parser.memory().vector());

    let mut space = NameSpace::new(parser.memory());
    space.narrow(read).unwrap();
    space
        .admits(certificate.tbs().subject(), &names, &(), parser.memory())
        .unwrap()
}

#[test]
fn a_space_nobody_narrowed_admits_every_name() {
    // The common case, and the one that has to cost nothing: no authority
    // above said anything about names, so nothing is out of place.
    let space = NameSpace::new(&crate::fixtures::memory());
    assert!(space.is_open());
    assert_eq!(space.regions(), 0);

    let der = CertificateParts::default().build();
    let parser = Parser::der(crate::fixtures::memory());
    let certificate = Certificate::read(&parser, &der, 0).unwrap();
    assert_eq!(
        space
            .admits(certificate.tbs().subject(), &[], &(), parser.memory())
            .unwrap(),
        Admission::Admitted
    );
}

#[test]
fn constraint_dispatch_is_admitted_before_a_short_name_comparison() {
    // A borrowed short DNS name has no forming scan; its actual constraint
    // dispatch must still fail after the one-unit presentation charge.
    use structured_asn1::memory::{Memory, MemoryAccount};
    let der = CertificateParts {
        subject: seq(&[]),
        extensions: Some(seq(&[
            extension(
                oid::NAME_CONSTRAINTS,
                true,
                &constraints(&[subtree(dns("example.ua"))], &[]),
            ),
            extension(oid::SUBJECT_ALT_NAME, true, &seq(&[dns("example.ua")])),
        ])),
        ..CertificateParts::default()
    }
    .build();
    let parser = Parser::der(crate::fixtures::memory());
    let certificate = Certificate::read(&parser, &der, 0).unwrap();
    let names = certificate
        .tbs()
        .extensions()
        .subject_alt_name(parser.memory())
        .unwrap()
        .unwrap();
    let mut space = NameSpace::new(parser.memory());
    space.narrow(of(&certificate)).unwrap();
    let execution = Memory::new(alloc::sync::Arc::new(MemoryAccount::new(0, 1)));
    let error = space
        .admits(certificate.tbs().subject(), &names, &(), &execution)
        .unwrap_err();
    assert!(matches!(error, crate::error::CallbackError::Processing(error) if error.is_resource()));
    assert_eq!(execution.work_used(), 1);
}

#[test]
fn constraint_bytes_are_admitted_before_a_retained_dns_comparison() {
    // Two metadata units permit presentation and dispatch, but must not
    // finance the actual case-insensitive byte comparison in a new account.
    use structured_asn1::memory::{Memory, MemoryAccount};
    let der = CertificateParts {
        subject: seq(&[]),
        extensions: Some(seq(&[
            extension(
                oid::NAME_CONSTRAINTS,
                true,
                &constraints(&[subtree(dns("example.ua"))], &[]),
            ),
            extension(oid::SUBJECT_ALT_NAME, true, &seq(&[dns("example.ua")])),
        ])),
        ..CertificateParts::default()
    }
    .build();
    let parser = Parser::der(crate::fixtures::memory());
    let certificate = Certificate::read(&parser, &der, 0).unwrap();
    let names = certificate
        .tbs()
        .extensions()
        .subject_alt_name(parser.memory())
        .unwrap()
        .unwrap();
    let mut space = NameSpace::new(parser.memory());
    space.narrow(of(&certificate)).unwrap();
    let execution = Memory::new(alloc::sync::Arc::new(MemoryAccount::new(0, 2)));
    let error = space
        .admits(certificate.tbs().subject(), &names, &(), &execution)
        .unwrap_err();
    assert!(matches!(error, crate::error::CallbackError::Processing(error) if error.is_resource()));
    assert_eq!(execution.work_used(), 2);
}

#[test]
fn a_name_below_the_permitted_one_is_admitted_and_one_beside_it_is_not() {
    // The directory form is the one the profile requires every validator to
    // process: the region is a name, and what is in it begins with that name.
    let base = subtree(directory(organization("Example")));
    assert_eq!(
        admission(
            core::slice::from_ref(&base),
            &[],
            under("Example", "Leaf"),
            &[]
        ),
        Admission::Admitted
    );
    // Its own base is in it, which is what lets an authority constrain a
    // path to itself.
    assert_eq!(
        admission(
            core::slice::from_ref(&base),
            &[],
            organization("Example"),
            &[]
        ),
        Admission::Admitted
    );
    assert_eq!(
        admission(&[base], &[], under("Other", "Leaf"), &[]),
        Admission::NotPermitted
    );
}

#[test]
fn an_excluded_name_is_refused_however_widely_it_was_permitted() {
    // Forbidding beats allowing: a region inside a permitted one that an
    // authority excluded is excluded (RFC 5280 6.1.4 (g)).
    let permitted = subtree(directory(organization("Example")));
    let excluded = subtree(directory(under("Example", "Secret")));
    assert_eq!(
        admission(&[permitted], &[excluded], under("Example", "Secret"), &[]),
        Admission::Excluded
    );
}

#[test]
fn a_constraint_of_one_form_says_nothing_about_a_name_of_another() {
    // A permitted mail region constrains mail names. A certificate that
    // carries no mail name is not outside it: it is not in that space at
    // all (RFC 5280 4.2.1.10).
    assert_eq!(
        admission(
            &[subtree(mail("example.com"))],
            &[],
            named("Example"),
            &[dns("host.example.org")]
        ),
        Admission::Admitted
    );
}

#[test]
fn a_domain_region_holds_what_is_under_it_and_not_what_merely_ends_with_it() {
    // The domain form is a subtree: the domain itself and everything below
    // it. `notexample.com` ends with the same letters and is a different
    // domain, which is the mistake a plain suffix test makes.
    let base = subtree(dns("example.com"));
    for host in ["example.com", "host.example.com", "a.b.example.com"] {
        assert_eq!(
            admission(
                core::slice::from_ref(&base),
                &[],
                named("Leaf"),
                &[dns(host)]
            ),
            Admission::Admitted,
            "{host}"
        );
    }
    for host in ["notexample.com", "example.com.evil.net", "example.co"] {
        assert_eq!(
            admission(
                core::slice::from_ref(&base),
                &[],
                named("Leaf"),
                &[dns(host)]
            ),
            Admission::NotPermitted,
            "{host}"
        );
    }
}

#[test]
fn a_domain_region_is_read_without_regard_to_case() {
    // Host names are compared without regard to case, and a certificate
    // that writes one in capitals is not outside the region because of it.
    assert_eq!(
        admission(
            &[subtree(dns("Example.COM"))],
            &[],
            named("Leaf"),
            &[dns("HOST.example.com")]
        ),
        Admission::Admitted
    );
}

#[test]
fn a_mail_region_naming_a_host_does_not_hold_the_hosts_below_it() {
    // The mail form is not the domain form: without a leading separator the
    // region is one host, and with one it is a domain
    // (RFC 5280 4.2.1.10). Reading the two the same way would let a
    // certificate constrained to one host stand for every host under it.
    assert_eq!(
        admission(
            &[subtree(mail("example.com"))],
            &[],
            named("Leaf"),
            &[mail("someone@example.com")]
        ),
        Admission::Admitted
    );
    assert_eq!(
        admission(
            &[subtree(mail("example.com"))],
            &[],
            named("Leaf"),
            &[mail("someone@mail.example.com")]
        ),
        Admission::NotPermitted
    );
    assert_eq!(
        admission(
            &[subtree(mail(".example.com"))],
            &[],
            named("Leaf"),
            &[mail("someone@mail.example.com")]
        ),
        Admission::Admitted
    );
    // A domain region written with the separator is strictly below it, so
    // the domain's own host is not in it.
    assert_eq!(
        admission(
            &[subtree(mail(".example.com"))],
            &[],
            named("Leaf"),
            &[mail("someone@example.com")]
        ),
        Admission::NotPermitted
    );
}

#[test]
fn a_mail_region_naming_one_mailbox_holds_that_mailbox_and_no_other() {
    // The local part is compared as it is written, because what it means
    // belongs to the host that issues it, and the host part without regard
    // to case.
    let base = subtree(mail("Someone@Example.com"));
    assert_eq!(
        admission(
            core::slice::from_ref(&base),
            &[],
            named("Leaf"),
            &[mail("Someone@EXAMPLE.COM")]
        ),
        Admission::Admitted
    );
    assert_eq!(
        admission(&[base], &[], named("Leaf"), &[mail("someone@example.com")]),
        Admission::NotPermitted
    );
}

#[test]
fn a_mailbox_written_into_the_subject_is_held_to_the_mail_regions() {
    // Certificates exist that carry the mail identity as an attribute of
    // the subject rather than as an alternative name (RFC 5280 4.2.1.10).
    // A constraint that did not reach it would constrain nothing on them.
    let subject = seq(&[set(&[seq(&[
        oid_of(oid::EMAIL_ADDRESS),
        ia5("someone@elsewhere.net"),
    ])])]);
    assert_eq!(
        admission(&[subtree(mail("example.com"))], &[], subject, &[]),
        Admission::NotPermitted
    );
}

#[test]
fn a_location_is_held_by_its_host_and_nothing_else_it_names() {
    // What a location constraint constrains is the host part, and the rest
    // of the location is text this crate never resolves.
    let base = subtree(uri(".example.com"));
    for location in [
        "http://host.example.com/path",
        "https://user@host.example.com:443/path",
        "https://a.b.example.com/",
    ] {
        assert_eq!(
            admission(
                core::slice::from_ref(&base),
                &[],
                named("Leaf"),
                &[uri(location)]
            ),
            Admission::Admitted,
            "{location}"
        );
    }
    // The host is what is compared, so a region's name appearing anywhere
    // else in the location does not put it in the region: not in the path,
    // not in the user information, and not as the beginning of a longer
    // name.
    for location in [
        "http://evil.net/host.example.com",
        "http://host.example.com@evil.net/",
        "http://host.example.com.evil.net/",
    ] {
        assert_eq!(
            admission(
                core::slice::from_ref(&base),
                &[],
                named("Leaf"),
                &[uri(location)]
            ),
            Admission::NotPermitted,
            "{location}"
        );
    }
}

#[test]
fn a_location_that_names_no_server_is_in_no_host_region() {
    // A location without an authority names a resource rather than a server
    // (RFC 3986 3.2), and the separator that introduces an authority is the
    // one right after the scheme. Finding it anywhere else reads a host out
    // of a path: `urn:a//host.example.com/x` names no host at all, and
    // admitting it under a region that permits that host would put a
    // certificate in a space its authority never allowed. Having no host is
    // what a location constraint cannot be applied to, so the refusal is the
    // constraint's (RFC 5280 4.2.1.10) rather than the region's.
    assert_eq!(
        admission(
            &[subtree(uri(".example.com"))],
            &[],
            named("Leaf"),
            &[uri("urn:a//host.example.com/x")]
        ),
        Admission::Unenforceable
    );
}

#[test]
fn a_location_host_written_to_the_root_is_the_same_host() {
    // The reader keeps the closing period of an absolute host name because
    // that is how the location was written (RFC 1034 3.1), and it qualifies
    // the name rather than changing it. A region naming the same host has to
    // hold it, or a certificate is refused over a spelling.
    assert_eq!(
        admission(
            &[subtree(uri("example.com"))],
            &[],
            named("Leaf"),
            &[uri("https://example.com./")]
        ),
        Admission::Admitted
    );
    assert_eq!(
        admission(
            &[subtree(uri(".example.com"))],
            &[],
            named("Leaf"),
            &[uri("https://host.example.com./")]
        ),
        Admission::Admitted
    );
}

#[test]
fn an_address_is_in_a_network_when_the_masked_bytes_agree() {
    let base = subtree(address(&[192, 0, 2, 0, 255, 255, 255, 0]));
    assert_eq!(
        admission(
            core::slice::from_ref(&base),
            &[],
            named("Leaf"),
            &[address(&[192, 0, 2, 77])]
        ),
        Admission::Admitted
    );
    assert_eq!(
        admission(
            core::slice::from_ref(&base),
            &[],
            named("Leaf"),
            &[address(&[192, 0, 3, 77])]
        ),
        Admission::NotPermitted
    );
    // A network of four bytes does not hold a sixteen-byte address, and
    // reading the first four of one as the other would put it in a network
    // nobody named. The widths are fixed by the profile and checked when
    // they were read, so a pair that does not match is two address families
    // and a definite answer rather than an unreadable comparison.
    assert_eq!(
        admission(
            &[base],
            &[],
            named("Leaf"),
            &[address(&[
                0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
            ])]
        ),
        Admission::NotPermitted
    );
}

#[test]
fn a_certificate_with_alternative_names_is_not_held_to_its_legacy_mailbox() {
    // "When constraints are imposed on the rfc822Name name form, but the
    // certificate does not include a subject alternative name, the
    // rfc822Name constraint MUST be applied to the attribute of type
    // emailAddress in the subject distinguished name" (RFC 5280 4.2.1.10).
    // The fallback is for certificates that carry their mail identity in
    // the subject because they carry no alternative names. Applying it to
    // one that does have them refuses a certificate whose actual mail
    // identity is where the region allows it.
    let subject = seq(&[set(&[seq(&[
        oid_of(oid::EMAIL_ADDRESS),
        ia5("someone@elsewhere.net"),
    ])])]);
    assert_eq!(
        admission(
            &[subtree(mail("example.com"))],
            &[],
            subject,
            &[mail("someone@example.com")]
        ),
        Admission::Admitted
    );
}

#[test]
fn a_location_a_uri_constraint_cannot_be_applied_to_lets_no_path_through() {
    // "If a constraint is applied to the uniformResourceIdentifier name form
    // and a subsequent certificate includes a subjectAltName extension with a
    // uniformResourceIdentifier that does not include an authority component
    // with a host name specified as a fully qualified domain name ... then the
    // application MUST reject the certificate" (RFC 5280 4.2.1.10). Reading
    // such a location as merely outside the region lets it past an excluded
    // subtree, which is where nothing else would catch it.
    for location in [
        "urn:example:value",
        "http://192.0.2.1/x",
        "http://[2001:db8::1]/x",
    ] {
        assert_eq!(
            admission(
                &[],
                &[subtree(uri(".example.com"))],
                named("Leaf"),
                &[uri(location)]
            ),
            Admission::Unenforceable,
            "excluded: {location}"
        );
        assert_eq!(
            admission(
                &[subtree(uri(".example.com"))],
                &[],
                named("Leaf"),
                &[uri(location)]
            ),
            Admission::Unenforceable,
            "permitted: {location}"
        );
    }
}

#[test]
fn an_address_of_the_other_family_is_outside_a_network_rather_than_unreadable() {
    // A network of four bytes and an address of sixteen are both well
    // formed, and the address is certainly not in that network. Answering
    // that the comparison could not be made refuses a certificate for
    // carrying an address of a family the exclusion never spoke about.
    let excluded = subtree(address(&[192, 0, 2, 0, 255, 255, 255, 0]));
    let six = address(&[0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);
    assert_eq!(
        admission(&[], &[excluded], named("Leaf"), core::slice::from_ref(&six)),
        Admission::Admitted
    );

    // Permitting only that network still leaves the address outside it,
    // which is a refusal for a reason rather than for want of an answer.
    let permitted = subtree(address(&[192, 0, 2, 0, 255, 255, 255, 0]));
    assert_eq!(
        admission(&[permitted], &[], named("Leaf"), &[six]),
        Admission::NotPermitted
    );
}

#[test]
fn a_region_of_a_form_this_crate_does_not_match_lets_no_path_through() {
    // An unenforceable constraint that let a path through would be a
    // constraint that did nothing, which is the opposite of what the
    // authority marked critical.
    assert_eq!(
        admission(
            &[subtree(registered())],
            &[],
            named("Leaf"),
            &[registered()]
        ),
        Admission::Unenforceable
    );
    assert_eq!(
        admission(&[subtree(other())], &[], named("Leaf"), &[other()]),
        Admission::Unenforceable
    );
}

#[test]
fn a_region_of_an_unmatched_form_still_says_nothing_about_other_forms() {
    // The name the certificate carries is of another form, so the
    // constraint this crate cannot match does not reach it and the answer
    // is not withheld.
    assert_eq!(
        admission(
            &[subtree(registered())],
            &[],
            named("Leaf"),
            &[dns("host.example.com")]
        ),
        Admission::Admitted
    );
}

#[test]
fn every_authority_that_spoke_has_to_admit_the_name() {
    // Two authorities each narrowing the space is an intersection: a name
    // one of them allows and the other does not is not in the space
    // (RFC 5280 6.1.4 (g)).
    let der = CertificateParts {
        subject: under("Example", "Leaf"),
        ..CertificateParts::default()
    }
    .build();
    let parser = Parser::der(crate::fixtures::memory());
    let certificate = Certificate::read(&parser, &der, 0).unwrap();

    let wide = extension(
        oid::NAME_CONSTRAINTS,
        true,
        &constraints(&[subtree(directory(organization("Example")))], &[]),
    );
    let narrow = extension(
        oid::NAME_CONSTRAINTS,
        true,
        &constraints(&[subtree(directory(organization("Elsewhere")))], &[]),
    );

    let carrying = |value: Vec<u8>| {
        CertificateParts {
            extensions: Some(seq(&[value])),
            ..CertificateParts::default()
        }
        .build()
    };
    let wide_der = carrying(wide);
    let narrow_der = carrying(narrow);
    let wide = Certificate::read(&parser, &wide_der, 0).unwrap();
    let narrow = Certificate::read(&parser, &narrow_der, 0).unwrap();
    let mut space = NameSpace::new(parser.memory());
    space.narrow(of(&wide)).unwrap();
    assert_eq!(
        space
            .admits(certificate.tbs().subject(), &[], &(), parser.memory())
            .unwrap(),
        Admission::Admitted
    );
    space.narrow(of(&narrow)).unwrap();
    assert_eq!(space.regions(), 2);
    assert_eq!(
        space
            .admits(certificate.tbs().subject(), &[], &(), parser.memory())
            .unwrap(),
        Admission::NotPermitted
    );
}

#[test]
fn an_empty_subject_carries_no_identity_to_hold_to_the_space() {
    // A certificate whose identity lives in its alternative name has an
    // empty subject (RFC 5280 4.2.1.6), and an empty name sits under every
    // base. Holding it to the space would put every such certificate in
    // every region an authority named.
    let empty = seq(&[]);
    assert_eq!(
        admission(
            &[subtree(directory(organization("Example")))],
            &[],
            empty,
            &[dns("host.example.com")]
        ),
        Admission::Admitted
    );
}

#[test]
fn a_legacy_mailbox_that_is_not_one_lets_no_path_through() {
    // "The format of an rfc822Name is a Mailbox as defined in Section 4.1.2
    // of [RFC2821]" (RFC 5280 4.2.1.6), and the reader holds an alternative
    // name to that. The attribute a subject may carry the same identity in
    // arrives as a directory string and is held to nothing, so a value that
    // is not a mailbox reaches the regions as though it were one. Reading
    // the host out of `user@example.com@evil.net` by its last separator
    // puts it under whoever holds `evil.net`, and a value with no host at
    // all is in no region, which walks it past an excluded subtree where
    // nothing else would catch it.
    for value in [
        "user@example.com@evil.net",
        "notamailbox",
        "@example.com",
        "someone@",
    ] {
        let subject = seq(&[set(&[seq(&[oid_of(oid::EMAIL_ADDRESS), ia5(value)])])]);
        assert_eq!(
            admission(&[], &[subtree(mail("example.com"))], subject, &[]),
            Admission::Unenforceable,
            "excluded: {value}"
        );
        let subject = seq(&[set(&[seq(&[oid_of(oid::EMAIL_ADDRESS), ia5(value)])])]);
        assert_eq!(
            admission(&[subtree(mail("evil.net"))], &[], subject, &[]),
            Admission::Unenforceable,
            "permitted: {value}"
        );
    }
}