xml-sec 0.1.13

Pure Rust XML Security: XMLDSig, XMLEnc, C14N. Drop-in replacement for libxmlsec1.
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
//! End-to-end coverage for the upstream Merlin XMLDSig interoperability corpus.

use std::{
    collections::HashMap,
    path::PathBuf,
    time::{Duration, SystemTime},
};

use x509_parser::prelude::{FromDer, X509Certificate};
use xml_sec::policy::KeyTrustPolicy;
use xml_sec::xmldsig::{
    DefaultKeyResolver, DsigError, DsigStatus, FailureReason, HmacSha1VerificationKey,
    KeyResolutionError, KeyResolverConfig, ParseError, SignatureAlgorithm, UriTypeSet,
    VerificationKey, VerifyContext, X509ChainError, XPathHereSemantics,
};

const MERLIN: &str = "tests/fixtures/xmldsig/merlin-xmldsig-twenty-three";
const DONOR_EXTERNAL: &str = "tests/fixtures/xmldsig/external-data";
const VERIFY_2005: u64 = 1_104_580_800;

fn chain_policy(check_crls: bool) -> KeyTrustPolicy {
    KeyTrustPolicy {
        verify_x509_chains: true,
        check_crls,
        verification_time: Some(SystemTime::UNIX_EPOCH + Duration::from_secs(VERIFY_2005)),
        ..KeyTrustPolicy::default()
    }
}

fn legacy_policy(algorithm: SignatureAlgorithm) -> xml_sec::policy::VerificationPolicy {
    let mut policy = xml_sec::policy::VerificationPolicy::default();
    policy
        .key_trust
        .allowed_legacy_signature_algorithms
        .insert(algorithm);
    if algorithm == SignatureAlgorithm::DsaSha1 {
        policy.key_trust.dsa_keys.minimum_modulus_bits = 1024;
    }
    policy
}

fn legacy_chain_policy(
    algorithm: SignatureAlgorithm,
    check_crls: bool,
) -> xml_sec::policy::VerificationPolicy {
    let mut policy = legacy_policy(algorithm);
    policy.key_trust = chain_policy(check_crls);
    policy
        .key_trust
        .allowed_legacy_signature_algorithms
        .insert(algorithm);
    if algorithm == SignatureAlgorithm::DsaSha1 {
        policy.key_trust.dsa_keys.minimum_modulus_bits = 1024;
    }
    policy
}

fn root() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
}

fn bytes(path: &str) -> Vec<u8> {
    std::fs::read(root().join(path)).unwrap_or_else(|error| panic!("read {path}: {error}"))
}

fn xml(name: &str) -> String {
    String::from_utf8(bytes(&format!("{MERLIN}/{name}.xml"))).expect("fixture is UTF-8")
}

fn cert(name: &str) -> Vec<u8> {
    let path = format!("{MERLIN}/certs/{name}");
    let data = bytes(&path);
    if name.ends_with(".der") {
        return data;
    }
    let (rest, pem) = x509_parser::pem::parse_x509_pem(&data).expect("certificate PEM");
    assert!(rest.iter().all(u8::is_ascii_whitespace));
    pem.contents
}

fn verification_key(name: &str, algorithm: SignatureAlgorithm) -> VerificationKey {
    let der = cert(name);
    let (rest, certificate) = X509Certificate::from_der(&der).expect("certificate DER");
    assert!(rest.is_empty());
    VerificationKey {
        algorithm,
        public_key_bytes: certificate.public_key().raw.to_vec(),
        certificate_der: Some(der),
        name: None,
    }
}

fn external_resources() -> HashMap<String, Vec<u8>> {
    HashMap::from([
        (
            "http://www.w3.org/TR/xml-stylesheet".into(),
            bytes(&format!("{DONOR_EXTERNAL}/xml-stylesheet-2005")),
        ),
        (
            "http://www.w3.org/Signature/2002/04/xml-stylesheet.b64".into(),
            bytes(&format!("{DONOR_EXTERNAL}/xml-stylesheet-2005.b64")),
        ),
        (
            "tests/merlin-xmldsig-twenty-three/certs/balor.der".into(),
            cert("balor.der"),
        ),
    ])
}

fn assert_valid(
    name: &str,
    result: Result<xml_sec::xmldsig::VerifyResult, xml_sec::xmldsig::DsigError>,
) {
    let result = result.unwrap_or_else(|error| panic!("{name}: {error}"));
    assert_eq!(result.status, DsigStatus::Valid, "{name}");
    assert!(
        result
            .signed_info_references
            .iter()
            .all(|reference| reference.status == DsigStatus::Valid),
        "{name}: SignedInfo reference failure"
    );
}

#[test]
fn verifies_all_merlin_documents_with_upstream_expectations() {
    // Every signed document used by xmlsec's Merlin runner is classified here.
    let default = DefaultKeyResolver::default();
    for name in [
        "signature-enveloped-dsa",
        "signature-enveloping-dsa",
        "signature-enveloping-b64-dsa",
    ] {
        assert_valid(
            name,
            VerifyContext::new()
                .policy(legacy_policy(SignatureAlgorithm::DsaSha1))
                .key_resolver(&default)
                .verify(&xml(name)),
        );
    }
    let legacy_rsa = DefaultKeyResolver::default();
    let mut rsa_policy = xml_sec::policy::VerificationPolicy::default();
    rsa_policy
        .key_trust
        .allowed_legacy_signature_algorithms
        .insert(SignatureAlgorithm::RsaSha1);
    rsa_policy.key_trust.rsa_keys.minimum_modulus_bits = 1024;
    assert_valid(
        "signature-enveloping-rsa",
        VerifyContext::new()
            .policy(rsa_policy)
            .key_resolver(&legacy_rsa)
            .verify(&xml("signature-enveloping-rsa")),
    );

    let hmac = HmacSha1VerificationKey::new(b"secret".to_vec()).expect("valid HMAC key");
    assert_valid(
        "signature-enveloping-hmac-sha1",
        VerifyContext::new()
            .policy(legacy_policy(SignatureAlgorithm::HmacSha1))
            .key(&hmac)
            .verify(&xml("signature-enveloping-hmac-sha1")),
    );
    let truncated_hmac = HmacSha1VerificationKey::new(b"secret".to_vec())
        .expect("valid HMAC key")
        .with_output_length_bits(80)
        .expect("valid XMLDSig truncation");
    assert_valid(
        "signature-enveloping-hmac-sha1-80",
        VerifyContext::new()
            .policy(legacy_policy(SignatureAlgorithm::HmacSha1))
            .key(&truncated_hmac)
            .verify(&xml("signature-enveloping-hmac-sha1-80")),
    );

    let resources = external_resources();
    for name in ["signature-external-dsa", "signature-external-b64-dsa"] {
        assert_valid(
            name,
            VerifyContext::new()
                .policy(legacy_policy(SignatureAlgorithm::DsaSha1))
                .key_resolver(&default)
                .allowed_uri_types(UriTypeSet::ALL)
                .external_resources(&resources)
                .verify(&xml(name)),
        );
    }

    let mut named = KeyResolverConfig::default();
    named.named_keys.insert(
        "Lugh".into(),
        verification_key("lugh-cert.pem", SignatureAlgorithm::DsaSha1),
    );
    let named = DefaultKeyResolver::new(named);
    assert_valid(
        "signature-keyname",
        VerifyContext::new()
            .policy(legacy_policy(SignatureAlgorithm::DsaSha1))
            .key_resolver(&named)
            .allowed_uri_types(UriTypeSet::ALL)
            .external_resources(&resources)
            .verify(&xml("signature-keyname")),
    );

    for (name, selected) in [
        ("signature-x509-crt", None),
        ("signature-x509-sn", Some("badb.pem")),
        ("signature-x509-is", Some("macha.pem")),
        ("signature-x509-ski", Some("nemain.pem")),
    ] {
        let lookup_certs = selected.into_iter().map(cert).collect();
        let resolver = DefaultKeyResolver::new(KeyResolverConfig {
            lookup_certs,
            trusted_certs: vec![cert("ca.pem")],
            ..KeyResolverConfig::default()
        });
        assert_valid(
            name,
            VerifyContext::new()
                .policy(legacy_chain_policy(SignatureAlgorithm::DsaSha1, false))
                .key_resolver(&resolver)
                .allowed_uri_types(UriTypeSet::ALL)
                .external_resources(&resources)
                .verify(&xml(name)),
        );
    }

    let retrieval = DefaultKeyResolver::new(KeyResolverConfig {
        lookup_certs: vec![cert("balor.pem")],
        trusted_certs: vec![cert("ca.pem")],
        ..KeyResolverConfig::default()
    });
    assert_valid(
        "signature-retrievalmethod-rawx509crt",
        VerifyContext::new()
            .policy(legacy_chain_policy(SignatureAlgorithm::DsaSha1, false))
            .key_resolver(&retrieval)
            .allowed_uri_types(UriTypeSet::ALL)
            .allowed_retrieval_method_uri_types(UriTypeSet::ALL)
            .external_resources(&resources)
            .verify(&xml("signature-retrievalmethod-rawx509crt")),
    );

    // The upstream runner's newer detached resource also mismatches the old
    // digest. Use the signed 2005 bytes and a certificate-valid timestamp so
    // this assertion reaches and proves the embedded CRL decision itself.
    let revoked_resources = external_resources();
    let revoked = DefaultKeyResolver::new(KeyResolverConfig {
        trusted_certs: vec![cert("ca.pem")],
        ..KeyResolverConfig::default()
    });
    let revoked_error = VerifyContext::new()
        .policy(legacy_chain_policy(SignatureAlgorithm::DsaSha1, true))
        .key_resolver(&revoked)
        .allowed_uri_types(UriTypeSet::ALL)
        .external_resources(&revoked_resources)
        .verify(&xml("signature-x509-crt-crl"))
        .expect_err("the donor CRL revokes the signing certificate");
    // Merlin's CA restricts KeyUsage to keyCertSign, so RFC 5280 requires
    // rejecting its CRL before trusting the listed revoked serial. Dedicated
    // chain tests cover the Revoked result for an authorized cRLSign issuer.
    assert!(
        matches!(
            revoked_error,
            DsigError::KeyResolution(KeyResolutionError::Chain(X509ChainError::InvalidKeyUsage {
                position: 1,
                required: "cRLSign"
            }))
        ),
        "unexpected revoked-vector error: {revoked_error:?}"
    );

    let complex = DefaultKeyResolver::new(KeyResolverConfig {
        trusted_certs: vec![cert("merlin.pem")],
        ..KeyResolverConfig::default()
    });
    let mut complex_policy = legacy_policy(SignatureAlgorithm::DsaSha1);
    complex_policy.key_trust.verification_time =
        Some(SystemTime::UNIX_EPOCH + Duration::from_secs(VERIFY_2005));
    let result = VerifyContext::new()
        .policy(complex_policy)
        .key_resolver(&complex)
        .allowed_uri_types(UriTypeSet::ALL)
        .external_resources(&resources)
        .process_manifests(true)
        .store_pre_digest(true)
        .allow_internal_dtd(true)
        .xpath_here_semantics(XPathHereSemantics::XmlSecLegacy)
        .verify(&xml("signature"))
        .expect("complex signature pipeline");
    assert_eq!(result.status, DsigStatus::Valid);
    let expected_signed_info_uris = [
        "http://www.w3.org/TR/xml-stylesheet",
        "http://www.w3.org/Signature/2002/04/xml-stylesheet.b64",
        "#object-1",
        "",
        "#object-2",
        "#manifest-1",
        "#signature-properties-1",
        "",
        "",
        "#xpointer(/)",
        "#xpointer(/)",
        "#object-3",
        "#object-3",
        "#xpointer(id('object-3'))",
        "#xpointer(id('object-3'))",
        "#reference-2",
        "#manifest-reference-1",
        "#reference-1",
    ];
    assert_eq!(
        result.signed_info_references.len(),
        expected_signed_info_uris.len()
    );
    for (reference, expected_uri) in result
        .signed_info_references
        .iter()
        .zip(expected_signed_info_uris)
    {
        assert_eq!(reference.uri, expected_uri);
        assert_eq!(reference.status, DsigStatus::Valid, "{expected_uri}");
    }

    let expected_manifest = [
        ("http://www.w3.org/TR/xml-stylesheet", DsigStatus::Valid),
        ("#reference-1", DsigStatus::Valid),
        (
            "#notaries",
            // The donor uses an XSLT transform, which this pure-Rust profile
            // intentionally does not execute; failure occurs before digest comparison.
            DsigStatus::Invalid(FailureReason::ReferenceProcessingFailure { ref_index: 2 }),
        ),
    ];
    assert_eq!(result.manifest_references.len(), expected_manifest.len());
    for (reference, (expected_uri, expected_status)) in
        result.manifest_references.iter().zip(expected_manifest)
    {
        assert_eq!(reference.uri, expected_uri);
        assert_eq!(reference.status, expected_status, "{expected_uri}");
    }
}

#[test]
fn pre_resolved_dsa_key_obeys_the_relaxed_operation_strength_policy() {
    // Compatibility policy must apply identically to direct and resolved keys;
    // the built-in key must not reapply a stricter hidden default afterward.
    let key = verification_key("lugh-cert.pem", SignatureAlgorithm::DsaSha1);
    assert_valid(
        "direct legacy DSA key",
        VerifyContext::new()
            .policy(legacy_policy(SignatureAlgorithm::DsaSha1))
            .key(&key)
            .allowed_uri_types(UriTypeSet::ALL)
            .external_resources(&external_resources())
            .verify(&xml("signature-keyname")),
    );
}

#[test]
fn rejects_missing_or_tampered_external_resources() {
    // Detached references cannot trigger I/O and must fail on absent or altered caller bytes.
    let default = DefaultKeyResolver::default();
    let document = xml("signature-external-dsa");
    let missing = HashMap::new();
    assert!(matches!(
        VerifyContext::new()
            .policy(legacy_policy(SignatureAlgorithm::DsaSha1))
            .key_resolver(&default)
            .allowed_uri_types(UriTypeSet::ALL)
            .external_resources(&missing)
            .verify(&document),
        Err(DsigError::Reference(
            xml_sec::xmldsig::ReferenceProcessingError::UriDereference(
                xml_sec::xmldsig::TransformError::UnsupportedUri(uri)
            )
        )) if uri == "http://www.w3.org/TR/xml-stylesheet"
    ));

    let mut tampered = external_resources();
    tampered.insert(
        "http://www.w3.org/TR/xml-stylesheet".into(),
        b"tampered".to_vec(),
    );
    let result = VerifyContext::new()
        .policy(legacy_policy(SignatureAlgorithm::DsaSha1))
        .key_resolver(&default)
        .allowed_uri_types(UriTypeSet::ALL)
        .external_resources(&tampered)
        .verify(&document)
        .expect("tampering is a validation result");
    assert_ne!(result.status, DsigStatus::Valid);
}

#[test]
fn bounds_external_resources_before_dereference() {
    // Resource limits are enforced for the complete caller map, not only the referenced entry.
    let default = DefaultKeyResolver::default();
    let mut oversized = external_resources();
    oversized.insert("urn:oversized".into(), vec![0; 8 * 1024 * 1024 + 1]);
    assert!(matches!(
        VerifyContext::new()
            .policy(legacy_policy(SignatureAlgorithm::DsaSha1))
            .key_resolver(&default)
            .allowed_uri_types(UriTypeSet::ALL)
            .external_resources(&oversized)
            .verify(&xml("signature-external-dsa")),
        Err(DsigError::Policy(
            xml_sec::policy::PolicyViolation::ResourceLimit {
                resource: "external resource bytes",
                ..
            }
        ))
    ));

    let mut aggregate = external_resources();
    aggregate
        .extend((0..5).map(|index| (format!("urn:aggregate:{index}"), vec![0; 7 * 1024 * 1024])));
    assert!(matches!(
        VerifyContext::new()
            .policy(legacy_policy(SignatureAlgorithm::DsaSha1))
            .key_resolver(&default)
            .allowed_uri_types(UriTypeSet::ALL)
            .external_resources(&aggregate)
            .verify(&xml("signature-external-dsa")),
        Err(DsigError::Policy(
            xml_sec::policy::PolicyViolation::ResourceLimit {
                resource: "aggregate external resource bytes",
                ..
            }
        ))
    ));
}

#[test]
fn rejects_wrong_hmac_key_and_invalid_output_length() {
    // MAC mismatch is an invalid status; malformed truncation is a processing error.
    let wrong = HmacSha1VerificationKey::new(b"wrong".to_vec()).expect("valid HMAC key");
    let result = VerifyContext::new()
        .policy(legacy_policy(SignatureAlgorithm::HmacSha1))
        .key(&wrong)
        .verify(&xml("signature-enveloping-hmac-sha1"))
        .expect("wrong MAC is a validation result");
    assert_ne!(result.status, DsigStatus::Valid);

    let malformed = xml("signature-enveloping-hmac-sha1-80").replacen(
        "<HMACOutputLength>80</HMACOutputLength>",
        "<HMACOutputLength>72</HMACOutputLength>",
        1,
    );
    assert!(malformed.contains("<HMACOutputLength>72</HMACOutputLength>"));
    assert!(matches!(
        VerifyContext::new()
            .policy(legacy_policy(SignatureAlgorithm::HmacSha1))
            .key(&wrong)
            .verify(&malformed),
        Err(DsigError::ParseSignedInfo(ParseError::InvalidStructure(reason)))
            if reason == "HMACOutputLength must be a byte-aligned value from 80 through 160"
    ));

    let implicit_full_length = xml("signature-enveloping-hmac-sha1-80").replacen(
        "<HMACOutputLength>80</HMACOutputLength>",
        "",
        1,
    );
    assert!(matches!(
        VerifyContext::new()
            .policy(legacy_policy(SignatureAlgorithm::HmacSha1))
            .key(&wrong)
            .verify(&implicit_full_length),
        Err(DsigError::InvalidStructure {
            reason: "SignatureValue length does not match HMACOutputLength"
        })
    ));
}

#[test]
fn rejects_malformed_dsa_key_value() {
    // Invalid CryptoBinary input must be rejected before DSA key construction.
    let malformed = xml("signature-enveloped-dsa").replacen("cfYpihpAQeep", "!!!!ihpAQeep", 1);
    assert!(malformed.contains("!!!!ihpAQeep"));
    assert!(matches!(
        VerifyContext::new()
            .policy(legacy_policy(SignatureAlgorithm::DsaSha1))
            .key_resolver(&DefaultKeyResolver::default())
            .verify(&malformed),
        Err(DsigError::ParseKeyInfo(ParseError::Base64(_)))
    ));
}

#[test]
fn partial_dsa_key_value_falls_back_to_later_complete_key() {
    // XMLDSig permits Y-only DSAKeyValue sources; an unusable first source must
    // not prevent a later complete DSAKeyValue from verifying the signature.
    let document = xml("signature-enveloped-dsa").replacen(
        "<KeyInfo>\n      <KeyValue>",
        "<KeyInfo>\n      <KeyValue><DSAKeyValue><Y>AQ==</Y></DSAKeyValue></KeyValue>\n      <KeyValue>",
        1,
    );
    assert!(document.contains("<DSAKeyValue><Y>AQ==</Y></DSAKeyValue>"));

    assert_valid(
        "partial DSAKeyValue fallback",
        VerifyContext::new()
            .policy(legacy_policy(SignatureAlgorithm::DsaSha1))
            .key_resolver(&DefaultKeyResolver::default())
            .verify(&document),
    );
}

#[test]
fn rejects_missing_ambiguous_and_weak_key_resolution() {
    // KeyName, RetrievalMethod IDs, and legacy RSA policy each fail closed.
    let resources = external_resources();
    let missing = VerifyContext::new()
        .policy(legacy_policy(SignatureAlgorithm::DsaSha1))
        .key_resolver(&DefaultKeyResolver::default())
        .allowed_uri_types(UriTypeSet::ALL)
        .external_resources(&resources)
        .verify(&xml("signature-keyname"));
    assert!(matches!(
        missing,
        Ok(result) if result.status == DsigStatus::Invalid(FailureReason::KeyNotFound)
    ));

    let ambiguous = xml("signature").replacen(
        "<Object Id=\"object-4\">",
        "<Object Id=\"object-4\"/><Object Id=\"object-4\">",
        1,
    );
    let ambiguous_error = VerifyContext::new()
        .policy(legacy_policy(SignatureAlgorithm::DsaSha1))
        .key_resolver(&DefaultKeyResolver::default())
        .allow_internal_dtd(true)
        .allowed_uri_types(UriTypeSet::ALL)
        .external_resources(&resources)
        .verify(&ambiguous)
        .expect_err("duplicate ID must fail before key resolution");
    assert!(
        matches!(
            ambiguous_error,
            DsigError::InvalidStructure {
                reason: "X509Data RetrievalMethod target is missing or ambiguous"
            }
        ),
        "unexpected duplicate-ID error: {ambiguous_error:?}"
    );

    let weak = VerifyContext::new()
        .key_resolver(&DefaultKeyResolver::default())
        .verify(&xml("signature-enveloping-rsa"));
    assert!(matches!(
        weak,
        Err(DsigError::Policy(
            xml_sec::policy::PolicyViolation::Algorithm {
                operation: "verification",
                ..
            }
        ))
    ));
}

#[test]
fn rejects_dtd_and_unsupported_retrieval_defaults() {
    // Internal DTD parsing and RetrievalMethod transform compatibility require exact opt-ins.
    assert!(matches!(
        VerifyContext::new()
            .policy(legacy_policy(SignatureAlgorithm::DsaSha1))
            .key_resolver(&DefaultKeyResolver::default())
            .verify(&xml("signature")),
        Err(DsigError::XmlParse(_))
    ));

    let unsupported = xml("signature").replacen(
        "ancestor-or-self::dsig:X509Data",
        "descendant-or-self::dsig:X509Data",
        1,
    );
    let resources = external_resources();
    let unsupported_error = VerifyContext::new()
        .policy(legacy_policy(SignatureAlgorithm::DsaSha1))
        .key_resolver(&DefaultKeyResolver::default())
        .allow_internal_dtd(true)
        .allowed_uri_types(UriTypeSet::ALL)
        .external_resources(&resources)
        .verify(&unsupported)
        .expect_err("unsupported RetrievalMethod XPath must fail closed");
    assert!(matches!(
        unsupported_error,
        DsigError::ParseKeyInfo(ParseError::InvalidStructure(reason))
            if reason == "unsupported RetrievalMethod XPath selection"
    ));

    let retrieval = DefaultKeyResolver::new(KeyResolverConfig {
        lookup_certs: vec![cert("balor.pem")],
        trusted_certs: vec![cert("ca.pem")],
        ..KeyResolverConfig::default()
    });
    let reference_error = VerifyContext::new()
        .policy(legacy_chain_policy(SignatureAlgorithm::DsaSha1, false))
        .key_resolver(&retrieval)
        .external_resources(&resources)
        .verify(&xml("signature-retrievalmethod-rawx509crt"))
        .expect_err("external SignedInfo reference must require an explicit opt-in");
    assert!(matches!(
        reference_error,
        DsigError::Policy(xml_sec::policy::PolicyViolation::Uri {
            operation: "verification",
            reason: "reference URI class is not permitted",
        })
    ));

    let retrieval_error = VerifyContext::new()
        .policy(legacy_policy(SignatureAlgorithm::DsaSha1))
        .key_resolver(&retrieval)
        .allowed_uri_types(UriTypeSet::ALL)
        .external_resources(&resources)
        .verify(&xml("signature-retrievalmethod-rawx509crt"))
        .expect_err("external key retrieval must require its own explicit opt-in");
    assert!(matches!(
        retrieval_error,
        DsigError::Policy(xml_sec::policy::PolicyViolation::Uri {
            operation: "verification",
            reason: "retrieval method URI class is not permitted",
        })
    ));
}