xml-sec 0.2.0

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
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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
//! XMLDSig XSLT transform through the public signing and verification API.

use std::fs;

#[path = "support/cryptographic.rs"]
mod cryptographic;

use base64::Engine as _;
use xml_sec::XmlDomParseError;
use xml_sec::c14n::{C14nAlgorithm, C14nMode};
use xml_sec::document::XmlDocumentError;
use xml_sec::policy::{
    PolicyViolation, SigningPolicy, VerificationPolicy, XsltStylesheetSemantics,
};
use xml_sec::xmldsig::{
    DefaultKeyResolver, DigestAlgorithm, DsigError, DsigStatus, ReferenceBuilder,
    ReferenceProcessingError, RsaSigningKey, SignContext, SignatureAlgorithm, SignatureBuilder,
    SigningDigestError, SigningError, Transform, TransformError, X509CertificateKeyInfoWriter,
    XPathExpression, XSLT_TRANSFORM_URI, XsltStylesheet,
};

const XSL: &str = "http://www.w3.org/1999/XSL/Transform";

/// A stylesheet whose literal result elements have no namespace: it undeclares the
/// XMLDSig default namespace that the signature puts in scope at its `<Transform>`.
fn stylesheet(body: &str) -> XsltStylesheet {
    XsltStylesheet::new(format!(
        r#"<xsl:stylesheet version="1.0" xmlns:xsl="{XSL}" xmlns="">{body}</xsl:stylesheet>"#
    ))
    .expect("test stylesheet is valid markup")
}

/// Wraps every item value in `<v>` under `<values>`, sorted by value.
fn values() -> XsltStylesheet {
    stylesheet(concat!(
        r#"<xsl:template match="/"><values><xsl:for-each select="//item">"#,
        r#"<xsl:sort select="."/><v><xsl:value-of select="."/></v></xsl:for-each></values>"#,
        r#"</xsl:template>"#
    ))
}

fn exclusive_c14n() -> C14nAlgorithm {
    C14nAlgorithm::new(C14nMode::Exclusive1_0, false)
}

fn signing_material() -> (RsaSigningKey, X509CertificateKeyInfoWriter) {
    let private_key = fs::read_to_string("tests/fixtures/keys/rsa/rsa-2048-key.pem")
        .expect("RSA private-key fixture must load");
    let certificate = fs::read_to_string("tests/fixtures/keys/rsa/rsa-2048-cert.pem")
        .expect("RSA certificate fixture must load");
    (
        RsaSigningKey::from_pkcs8_pem(&private_key).expect("RSA private key must parse"),
        X509CertificateKeyInfoWriter::from_pem(&certificate).expect("RSA certificate must parse"),
    )
}

fn builder(reference: ReferenceBuilder) -> SignatureBuilder {
    SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .add_reference(reference)
        .key_info(true)
}

fn sign(document: &str, builder: &SignatureBuilder) -> Result<String, SigningError> {
    sign_with_policy(document, builder, SigningPolicy::default())
}

fn sign_with_policy(
    document: &str,
    builder: &SignatureBuilder,
    policy: SigningPolicy,
) -> Result<String, SigningError> {
    let (key, key_info) = signing_material();
    SignContext::new(&key)
        .key_info_writer(&key_info)
        .policy(policy)
        .sign_with_builder(document, builder)
}

/// Verify `signed` and return the pre-digest bytes of every SignedInfo reference.
fn verified_pre_digest(signed: &str) -> Vec<Vec<u8>> {
    let resolver = DefaultKeyResolver::default();
    let result = cryptographic::context()
        .key_resolver(&resolver)
        .store_pre_digest(true)
        .verify(signed)
        .expect("signature must verify");
    assert_eq!(result.status, DsigStatus::Valid);
    result
        .signed_info_references
        .into_iter()
        .map(|reference| {
            reference
                .pre_digest_data
                .expect("pre-digest data was requested")
        })
        .collect()
}

fn transform_error(error: SigningError) -> TransformError {
    match error {
        SigningError::Digest(SigningDigestError::Transform(error)) => error,
        other => panic!("expected a transform error, got {other:?}"),
    }
}

fn policy_error(error: SigningError) -> PolicyViolation {
    match error {
        SigningError::Policy(violation) => violation,
        other => panic!("expected a policy violation, got {other:?}"),
    }
}

const ITEMS: &str = r#"<root><item ID="payload">b</item><item>a</item></root>"#;

#[test]
fn detached_entity_uri_survives_sign_verify_and_current_query_replay() {
    // XSLT's unparsed entity map belongs to the detached XML resource. An
    // inherited XML Base on Reference resolves its identity before all three paths.
    use std::{collections::HashMap, sync::Arc};
    use structured_asn1::memory::{Memory, MemoryAccount};
    use xml_sec::xmldsig::{
        HmacSigningKey, HmacVerificationKey, ReplayMode, ReplayOutcome, UriTypeSet,
        VerificationRequest, VerifyContext, with_verified_request,
    };
    let resources = HashMap::from([(
        "https://example.org/data/input.xml".to_owned(),
        br#"<!DOCTYPE root [<!NOTATION image SYSTEM "image"><!ENTITY logo SYSTEM "images/logo.png" NDATA image>]><root/>"#.to_vec(),
    )]);
    let signing_key = HmacSigningKey::new(vec![0x31; 32]).unwrap();
    let verifying_key = HmacVerificationKey::new(vec![0x31; 32]).unwrap();
    let expected = b"https://example.org/data/images/logo.png";
    let sheet = stylesheet(
        r#"<xsl:output method="text"/><xsl:template match="/"><xsl:value-of select="unparsed-entity-uri('logo')"/></xsl:template>"#,
    );
    let builder = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::HmacSha256)
        .add_reference(
            ReferenceBuilder::new(DigestAlgorithm::Sha256)
                .uri("input.xml")
                .transform(Transform::Xslt(sheet)),
        );
    let mut signing = SigningPolicy::default();
    signing.uris.references = UriTypeSet::ALL;
    signing.xml.allow_internal_dtd = true;
    let signed = SignContext::new(&signing_key)
        .policy(signing)
        .external_resources(&resources)
        .sign_with_builder(r#"<root xml:base="https://example.org/data/"/>"#, &builder)
        .unwrap();
    let mut policy = VerificationPolicy::default();
    policy.uris.references = UriTypeSet::ALL;
    policy.xml.allow_internal_dtd = true;
    let context = VerifyContext::new()
        .key(&verifying_key)
        .policy(policy)
        .store_pre_digest(true);
    let request = VerificationRequest {
        external_resources: Some(&resources),
        ..Default::default()
    };
    let memory = Memory::new(Arc::new(MemoryAccount::new(1 << 24, 1 << 30)));
    for backend in xml_sec::XmlBackend::available() {
        let document = xml_sec::XmlDocument::parse_with_backend(signed.clone(), backend).unwrap();
        with_verified_request(&document, &context, &request, &memory, |session| {
            assert!(session.evidence().all_valid());
            let evidence = &session.evidence().signatures()[0];
            assert_eq!(
                evidence.result().as_ref().unwrap().signed_info_references[0]
                    .pre_digest_data
                    .as_deref(),
                Some(expected.as_slice())
            );
            let selected = session.select_signature(evidence.identity())?;
            selected
                .visit_authenticated_references(|reference| {
                    let mut replay = Vec::new();
                    assert_eq!(
                        selected
                            .replay_reference(
                                reference.identity,
                                ReplayMode::ReferenceOutput,
                                &mut |bytes: &[u8]| {
                                    replay.extend_from_slice(bytes);
                                    Ok::<_, ()>(())
                                }
                            )
                            .unwrap(),
                        ReplayOutcome::Complete
                    );
                    assert_eq!(replay, expected);
                    Ok::<_, ()>(())
                })
                .unwrap();
            Ok(())
        })
        .unwrap();
        assert_eq!(memory.used(), 0);
    }
}

#[test]
fn referenced_whitespace_beside_stylesheet_signs_and_verifies() {
    // XPath 1.0 section 5.1 excludes text children of a document root. CR
    // references accepted in Transform content must not become document markup.
    let (key, key_info) = signing_material();
    for backend in xml_sec::XmlBackend::available() {
        for semantics in [
            XsltStylesheetSemantics::Specification,
            XsltStylesheetSemantics::Libxmlsec1,
        ] {
            let sheet = XsltStylesheet::new(format!(
                r#"&#13;<?before alpha?><!--one--><xsl:stylesheet xmlns:xsl="{XSL}" version="1.0"><xsl:output method="text"/><xsl:template match="/"><xsl:value-of select="concat(count(document('')/processing-instruction()), ':', count(document('')/comment()), ':', count(document('')/text()))"/></xsl:template></xsl:stylesheet><!--two--><?after beta?>&#13;"#
            )).expect("CR is permitted whitespace in Transform content");
            let signature = builder(
                ReferenceBuilder::new(DigestAlgorithm::Sha256)
                    .uri("")
                    .transform(Transform::Enveloped)
                    .transform(Transform::Xslt(sheet)),
            );
            let mut signing = SigningPolicy::default();
            signing.transforms.xslt_stylesheet_semantics = semantics;
            let signed = SignContext::new(&key)
                .key_info_writer(&key_info)
                .policy(signing)
                .xml_backend(backend)
                .sign_with_builder(ITEMS, &signature)
                .expect("referenced stylesheet whitespace must sign");
            let mut verification = VerificationPolicy::default();
            verification.key_trust.mode = xml_sec::policy::VerificationTrustMode::CryptographicOnly;
            verification.transforms.xslt_stylesheet_semantics = semantics;
            let resolver = DefaultKeyResolver::default();
            let result = xml_sec::xmldsig::VerifyContext::new()
                .policy(verification)
                .xml_backend(backend)
                .key_resolver(&resolver)
                .store_pre_digest(true)
                .verify(&signed)
                .expect("referenced stylesheet whitespace must verify");
            assert_eq!(
                result.status,
                DsigStatus::Valid,
                "{backend:?}: {semantics:?}"
            );
            assert_eq!(
                result.signed_info_references[0].pre_digest_data.as_deref(),
                Some(b"2:2:0".as_slice()),
                "{backend:?}: {semantics:?}"
            );
        }
    }
}

#[test]
fn node_set_input_is_canonicalized_and_terminal_output_is_digested() {
    // XMLDSig 1.1 section 4.4.3.2 converts the node-set to octets with inclusive
    // C14N 1.0; the stylesheet then sees the whole document minus the Signature.
    let reference = ReferenceBuilder::new(DigestAlgorithm::Sha256)
        .uri("")
        .transform(Transform::Enveloped)
        .transform(Transform::Xslt(values()));
    let signed = sign(ITEMS, &builder(reference)).expect("node-set to XSLT must sign");
    assert!(signed.contains(&format!(r#"Algorithm="{XSLT_TRANSFORM_URI}""#)));
    assert_eq!(
        verified_pre_digest(&signed),
        [b"<?xml version=\"1.0\"?>\n<values><v>a</v><v>b</v></values>\n".to_vec()]
    );
}

#[test]
fn base64_output_feeds_the_stylesheet_and_xpath_reads_its_result() {
    // Binary input is used as is; a following XPath transform reparses the
    // serialized result tree into a node-set.
    let encoded =
        base64::engine::general_purpose::STANDARD.encode("<doc><item>z</item><item>y</item></doc>");
    let document = format!(r#"<root><data ID="payload">{encoded}</data></root>"#);
    let reference = ReferenceBuilder::new(DigestAlgorithm::Sha256)
        .uri("#payload")
        .transform(Transform::Base64Decode)
        .transform(Transform::Xslt(values()))
        .transform(Transform::XPath(XPathExpression::new(
            "ancestor-or-self::v[1]",
        )))
        .transform(Transform::C14n(exclusive_c14n()));
    let signed = sign(&document, &builder(reference)).expect("Base64 to XSLT must sign");
    assert_eq!(verified_pre_digest(&signed), [b"<v>y</v><v>z</v>".to_vec()]);
}

#[test]
fn chained_stylesheets_consume_each_others_output() {
    // The second stylesheet receives the first one's serialized document.
    let count = stylesheet(
        r#"<xsl:template match="/"><n><xsl:value-of select="count(//v)"/></n></xsl:template>"#,
    );
    let reference = ReferenceBuilder::new(DigestAlgorithm::Sha256)
        .uri("")
        .transform(Transform::Enveloped)
        .transform(Transform::Xslt(values()))
        .transform(Transform::Xslt(count));
    let signed = sign(ITEMS, &builder(reference)).expect("chained XSLT must sign");
    assert_eq!(
        verified_pre_digest(&signed),
        [b"<?xml version=\"1.0\"?>\n<n>2</n>\n".to_vec()]
    );
}

#[test]
fn output_allowance_is_shared_by_every_execution_in_an_operation() {
    // Each execution fits the output limit while their sum does not; the second
    // reference must see what the first one already produced. Verification runs
    // each reference once, so the operation total is exactly two outputs.
    let output = "<?xml version=\"1.0\"?>\n<values><v>a</v><v>b</v></values>\n".len();
    let reference = || {
        ReferenceBuilder::new(DigestAlgorithm::Sha256)
            .uri("")
            .transform(Transform::Enveloped)
            .transform(Transform::Xslt(values()))
    };
    let signed = sign(ITEMS, &builder(reference()).add_reference(reference()))
        .expect("two XSLT references sign");
    let verify = |maximum| {
        let mut policy = VerificationPolicy::default();
        policy.key_trust.mode = xml_sec::policy::VerificationTrustMode::CryptographicOnly;
        policy.resources.max_xslt_output_bytes = maximum;
        let resolver = DefaultKeyResolver::default();
        xml_sec::xmldsig::VerifyContext::new()
            .policy(policy)
            .key_resolver(&resolver)
            .verify(&signed)
    };

    let error = verify(output + output - 1).expect_err("second output must not fit");
    assert!(
        matches!(
            error,
            DsigError::Policy(PolicyViolation::ResourceLimit {
                resource: "XSLT output bytes",
                maximum,
                actual,
            }) if maximum == output + output - 1 && actual == output + output
        ),
        "{error:?}"
    );
    let result = verify(output + output).expect("both outputs fit exactly");
    assert_eq!(result.status, DsigStatus::Valid);
}

#[test]
fn work_limit_refuses_parameter_capture_before_execution() {
    // Native stylesheet capture belongs to the operation too; a one-unit
    // allowance refuses the second byte of the start-tag metadata scan,
    // before serialization/compilation/execution. No raw source
    // slice is copied before the tree is captured.
    let sheet = values();
    let reference = ReferenceBuilder::new(DigestAlgorithm::Sha256)
        .uri("")
        .transform(Transform::Enveloped)
        .transform(Transform::Xslt(sheet));
    let mut policy = SigningPolicy::default();
    policy.resources.max_xslt_work = 1;
    let error = policy_error(
        sign_with_policy(ITEMS, &builder(reference), policy).expect_err("work exceeds one unit"),
    );
    assert_eq!(
        error,
        PolicyViolation::ResourceLimit {
            resource: "XSLT work",
            maximum: 1,
            actual: 2,
        }
    );
}

#[test]
fn template_rejects_stylesheet_above_target_workspace() {
    // A stylesheet accepted by its constructor must still obey the policy of
    // the generated template, before the builder serializes its markup.
    let sheet = values();
    let bytes = sheet.source().len();
    let template = builder(
        ReferenceBuilder::new(DigestAlgorithm::Sha256)
            .uri("")
            .transform(Transform::Enveloped)
            .transform(Transform::Xslt(sheet)),
    );
    let mut policy = SigningPolicy::default();
    policy.resources.max_xslt_workspace_bytes = bytes - 1;
    let error = template
        .build_template_with_policy(&policy)
        .expect_err("target workspace must admit stylesheet markup");
    assert!(
        matches!(
            error,
            xml_sec::xmldsig::SignatureBuilderError::Policy(PolicyViolation::ResourceLimit {
                resource: "XSLT workspace bytes", maximum, actual,
            }) if maximum == bytes - 1 && actual == bytes
        ),
        "{error:?}"
    );
}

#[test]
fn workspace_limit_refuses_the_compilation() {
    let reference = ReferenceBuilder::new(DigestAlgorithm::Sha256)
        .uri("")
        .transform(Transform::Enveloped)
        .transform(Transform::Xslt(values()));
    let mut policy = SigningPolicy::default();
    policy.resources.max_xslt_workspace_bytes = 16;
    let error = policy_error(
        sign_with_policy(ITEMS, &builder(reference), policy)
            .expect_err("a stylesheet needs more than 16 bytes"),
    );
    assert!(
        matches!(
            error,
            PolicyViolation::ResourceLimit {
                resource: "XSLT workspace bytes",
                maximum: 16,
                actual,
            } if actual > 16
        ),
        "{error:?}"
    );
}

#[test]
fn policy_without_xslt_refuses_signing_and_verification() {
    // XSLT permission is the transform allowlist of the operation policy.
    let reference = ReferenceBuilder::new(DigestAlgorithm::Sha256)
        .uri("")
        .transform(Transform::Enveloped)
        .transform(Transform::Xslt(values()));
    let builder = builder(reference);
    let allowed = [
        "http://www.w3.org/2000/09/xmldsig#enveloped-signature",
        "http://www.w3.org/2001/10/xml-exc-c14n#",
    ];
    let mut signing = SigningPolicy::default();
    signing.transforms.allowed_algorithms = Some(allowed.iter().map(|uri| (*uri).into()).collect());
    let error = sign_with_policy(ITEMS, &builder, signing).expect_err("XSLT is not allowed");
    assert!(
        matches!(
            &error,
            SigningError::Policy(PolicyViolation::Algorithm { algorithm, .. })
                if algorithm == XSLT_TRANSFORM_URI
        ),
        "{error:?}"
    );

    let signed = sign(ITEMS, &builder).expect("default policy allows XSLT");
    let mut verification = VerificationPolicy::default();
    verification.key_trust.mode = xml_sec::policy::VerificationTrustMode::CryptographicOnly;
    verification.transforms.allowed_algorithms =
        Some(allowed.iter().map(|uri| (*uri).into()).collect());
    let resolver = DefaultKeyResolver::default();
    let error = xml_sec::xmldsig::VerifyContext::new()
        .policy(verification)
        .key_resolver(&resolver)
        .verify(&signed)
        .expect_err("XSLT is not allowed");
    assert!(
        matches!(
            &error,
            DsigError::Policy(PolicyViolation::Algorithm { algorithm, .. })
                if algorithm == XSLT_TRANSFORM_URI
        ),
        "{error:?}"
    );
}

#[test]
fn node_set_input_to_xslt_needs_the_implicit_c14n_permission() {
    // XMLDSig 1.1 section 4.4.3.2 converts a node-set given to XSLT with inclusive
    // C14N 1.0, so that algorithm runs and must be on the transform allowlist even
    // though the chain ends with octets.
    let reference = ReferenceBuilder::new(DigestAlgorithm::Sha256)
        .uri("")
        .transform(Transform::Enveloped)
        .transform(Transform::Xslt(values()));
    let builder = builder(reference);
    let allowed = [
        "http://www.w3.org/2000/09/xmldsig#enveloped-signature",
        "http://www.w3.org/2001/10/xml-exc-c14n#",
        XSLT_TRANSFORM_URI,
    ];
    let mut signing = SigningPolicy::default();
    signing.transforms.allowed_algorithms = Some(allowed.iter().map(|uri| (*uri).into()).collect());
    let error = sign_with_policy(ITEMS, &builder, signing).expect_err("inclusive C14N is excluded");
    assert!(
        matches!(
            &error,
            SigningError::Policy(PolicyViolation::Algorithm { algorithm, .. })
                if algorithm == xml_sec::xmldsig::DEFAULT_IMPLICIT_C14N_URI
        ),
        "{error:?}"
    );

    let signed = sign(ITEMS, &builder).expect("default policy allows the chain");
    let mut verification = VerificationPolicy::default();
    verification.key_trust.mode = xml_sec::policy::VerificationTrustMode::CryptographicOnly;
    verification.transforms.allowed_algorithms =
        Some(allowed.iter().map(|uri| (*uri).into()).collect());
    let resolver = DefaultKeyResolver::default();
    let error = xml_sec::xmldsig::VerifyContext::new()
        .policy(verification)
        .key_resolver(&resolver)
        .verify(&signed)
        .expect_err("inclusive C14N is excluded");
    assert!(
        matches!(
            &error,
            DsigError::Policy(PolicyViolation::Algorithm { algorithm, .. })
                if algorithm == xml_sec::xmldsig::DEFAULT_IMPLICIT_C14N_URI
        ),
        "{error:?}"
    );
}

#[test]
fn stylesheets_cannot_reach_external_resources() {
    // The transform has no resolver: imports and document() are policy refusals,
    // never empty results.
    for body in [
        r#"<xsl:import href="other.xsl"/><xsl:template match="/"><r/></xsl:template>"#,
        r#"<xsl:template match="/"><r><xsl:copy-of select="document('other.xml')"/></r></xsl:template>"#,
    ] {
        let reference = ReferenceBuilder::new(DigestAlgorithm::Sha256)
            .uri("")
            .transform(Transform::Enveloped)
            .transform(Transform::Xslt(stylesheet(body)));
        let error = policy_error(
            sign(ITEMS, &builder(reference)).expect_err("external resources are unavailable"),
        );
        assert!(
            matches!(
                error,
                PolicyViolation::Uri {
                    operation: "XSLT transform",
                    ..
                }
            ),
            "{body}: {error:?}"
        );
    }
}

#[test]
fn unbound_prefix_lists_follow_the_operation_policy() {
    // XSLT 1.0 section 7.1.1 makes an unbound prefix in exclude-result-prefixes an error by
    // default; the libxslt handling that libxmlsec1 applies is an explicit policy choice.
    let unbound = || {
        Transform::Xslt(
            XsltStylesheet::new(format!(
                r#"<xsl:stylesheet version="1.0" xmlns:xsl="{XSL}" exclude-result-prefixes="foo"><xsl:template match="/"><r/></xsl:template></xsl:stylesheet>"#
            ))
            .expect("well-formed stylesheet markup"),
        )
    };
    let builder = builder(
        ReferenceBuilder::new(DigestAlgorithm::Sha256)
            .uri("")
            .transform(Transform::Enveloped)
            .transform(unbound()),
    );
    let error = transform_error(sign(ITEMS, &builder).expect_err("specification rejects it"));
    assert!(matches!(error, TransformError::Xslt(_)), "{error:?}");

    let mut signing = SigningPolicy::default();
    signing.transforms.xslt_stylesheet_semantics = XsltStylesheetSemantics::Libxmlsec1;
    let signed = sign_with_policy(ITEMS, &builder, signing).expect("libxslt handling signs");
    let verify = |semantics| {
        let mut policy = VerificationPolicy::default();
        policy.key_trust.mode = xml_sec::policy::VerificationTrustMode::CryptographicOnly;
        policy.transforms.xslt_stylesheet_semantics = semantics;
        let resolver = DefaultKeyResolver::default();
        xml_sec::xmldsig::VerifyContext::new()
            .policy(policy)
            .key_resolver(&resolver)
            .store_pre_digest(true)
            .verify(&signed)
    };
    let result = verify(XsltStylesheetSemantics::Libxmlsec1).expect("libxslt handling verifies");
    assert_eq!(result.status, DsigStatus::Valid);
    assert_eq!(
        result.signed_info_references[0].pre_digest_data.as_deref(),
        Some(b"<?xml version=\"1.0\"?>\n<r/>\n".as_slice())
    );
    let error = verify(XsltStylesheetSemantics::Specification)
        .expect_err("specification rejects the stylesheet");
    assert!(
        matches!(
            error,
            DsigError::Reference(ReferenceProcessingError::Transform(TransformError::Xslt(_)))
        ),
        "{error:?}"
    );
}

#[test]
fn stylesheets_keep_the_namespaces_in_scope_at_their_transform() {
    // The stylesheet is an element of the signed document: a prefix declared on an
    // ancestor names the XSLT namespace (Namespaces in XML 1.0 section 6.1), and its
    // literal result element copies every namespace in scope but the XSLT one (XSLT 1.0
    // section 7.1.1), here `ds`. libxmlsec1 compiles the `<Transform>` content alone and
    // loses the `xsl` declaration.
    let template = format!(
        concat!(
            r#"<root xmlns:xsl="{xsl}"><item>a</item>"#,
            r#"<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"><ds:SignedInfo>"#,
            r#"<ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>"#,
            r#"<ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#hmac-sha256"/>"#,
            r#"<ds:Reference URI=""><ds:Transforms>"#,
            r#"<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>"#,
            r#"<ds:Transform Algorithm="{xslt}"><xsl:stylesheet version="1.0">"#,
            r#"<xsl:template match="/"><out><xsl:value-of select="//item"/></out></xsl:template>"#,
            r#"</xsl:stylesheet></ds:Transform></ds:Transforms>"#,
            r#"<ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>"#,
            r#"<ds:DigestValue/></ds:Reference></ds:SignedInfo><ds:SignatureValue/>"#,
            r#"</ds:Signature></root>"#
        ),
        xsl = XSL,
        xslt = XSLT_TRANSFORM_URI
    );
    let signer = xml_sec::xmldsig::HmacSigningKey::new(vec![7; 32]).unwrap();
    let verifier = xml_sec::xmldsig::HmacVerificationKey::new(vec![7; 32]).unwrap();
    let sign = |semantics| {
        let mut policy = SigningPolicy::default();
        policy.transforms.xslt_stylesheet_semantics = semantics;
        SignContext::new(&signer)
            .policy(policy)
            .sign_template(&template)
    };

    let signed = sign(XsltStylesheetSemantics::Specification).expect("the stylesheet compiles");
    let result = xml_sec::xmldsig::VerifyContext::new()
        .key(&verifier)
        .store_pre_digest(true)
        .verify(&signed)
        .expect("verification runs");
    assert_eq!(result.status, DsigStatus::Valid);
    assert_eq!(
        result.signed_info_references[0].pre_digest_data.as_deref(),
        Some(
            b"<?xml version=\"1.0\"?>\n<out xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\">a</out>\n"
                .as_slice()
        )
    );

    let error = transform_error(
        sign(XsltStylesheetSemantics::Libxmlsec1).expect_err("`xsl` is unbound in the content"),
    );
    assert!(matches!(error, TransformError::Xslt(_)), "{error:?}");
}

#[test]
fn entity_stylesheet_after_literal_misc_uses_expanded_children() {
    // XML 1.0 section 4.4 replaces entity markup at the reference. Its DOM
    // source range may precede a literal PI/comment sibling in the document.
    let sheet = format!(
        r#"<xsl:stylesheet xmlns:xsl="{XSL}" version="1.0" exclude-result-prefixes="ds"><xsl:template match="/"><out>expanded</out></xsl:template></xsl:stylesheet>"#
    );
    for (backend, misc) in xml_sec::XmlBackend::available().flat_map(|backend| {
        ["<?before stylesheet?>", "<!--before stylesheet-->"].map(|misc| (backend, misc))
    }) {
        let template = format!(
            concat!(
                r#"<!DOCTYPE root [<!ENTITY sheet '{sheet}'>]><root>"#,
                r#"<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"><ds:SignedInfo>"#,
                r#"<ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>"#,
                r#"<ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#hmac-sha256"/>"#,
                r#"<ds:Reference URI=""><ds:Transforms>"#,
                r#"<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>"#,
                r#"<ds:Transform Algorithm="{xslt}">{misc}&sheet;</ds:Transform>"#,
                r#"</ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>"#,
                r#"<ds:DigestValue/></ds:Reference></ds:SignedInfo><ds:SignatureValue/>"#,
                r#"</ds:Signature></root>"#
            ),
            sheet = sheet,
            misc = misc,
            xslt = XSLT_TRANSFORM_URI
        );
        let signer = xml_sec::xmldsig::HmacSigningKey::new(vec![7; 32]).unwrap();
        let verifier = xml_sec::xmldsig::HmacVerificationKey::new(vec![7; 32]).unwrap();
        let mut policy = SigningPolicy::default();
        policy.xml.allow_internal_dtd = true;
        let signed = SignContext::new(&signer)
            .xml_backend(backend)
            .policy(policy)
            .sign_template(&template)
            .expect("entity-supplied stylesheet signs after Misc");
        let mut policy = VerificationPolicy::default();
        policy.xml.allow_internal_dtd = true;
        let verified = xml_sec::xmldsig::VerifyContext::new()
            .xml_backend(backend)
            .key(&verifier)
            .policy(policy)
            .store_pre_digest(true)
            .verify(&signed)
            .expect("expanded stylesheet verifies");
        assert_eq!(verified.status, DsigStatus::Valid);
        assert_eq!(
            verified.signed_info_references[0]
                .pre_digest_data
                .as_deref(),
            Some(b"<?xml version=\"1.0\"?>\n<out>expanded</out>\n".as_slice())
        );
    }
}

#[test]
fn stylesheets_use_the_entities_of_their_document() {
    // An internal entity is replaced where it is referenced (XML 1.0 section 4.4), so a
    // stylesheet referring to one compiles with its replacement text, not as a fragment
    // detached from the document's DTD.
    let template = format!(
        concat!(
            r#"<!DOCTYPE root [<!ENTITY path "//item">]><root><item>a</item>"#,
            r#"<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"><ds:SignedInfo>"#,
            r#"<ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>"#,
            r#"<ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#hmac-sha256"/>"#,
            r#"<ds:Reference URI=""><ds:Transforms>"#,
            r#"<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>"#,
            r#"<ds:Transform Algorithm="{xslt}"><xsl:stylesheet version="1.0" xmlns:xsl="{xsl}" "#,
            r#"exclude-result-prefixes="ds"><xsl:template match="/"><out>"#,
            r#"<xsl:value-of select="&path;"/></out></xsl:template></xsl:stylesheet>"#,
            r#"</ds:Transform></ds:Transforms>"#,
            r#"<ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>"#,
            r#"<ds:DigestValue/></ds:Reference></ds:SignedInfo><ds:SignatureValue/>"#,
            r#"</ds:Signature></root>"#
        ),
        xsl = XSL,
        xslt = XSLT_TRANSFORM_URI
    );
    let signer = xml_sec::xmldsig::HmacSigningKey::new(vec![7; 32]).unwrap();
    let verifier = xml_sec::xmldsig::HmacVerificationKey::new(vec![7; 32]).unwrap();
    let mut signing = SigningPolicy::default();
    signing.xml.allow_internal_dtd = true;
    let signed = SignContext::new(&signer)
        .policy(signing)
        .sign_template(&template)
        .expect("the stylesheet compiles with the replaced entity");
    let mut verification = VerificationPolicy::default();
    verification.xml.allow_internal_dtd = true;
    let result = xml_sec::xmldsig::VerifyContext::new()
        .policy(verification)
        .key(&verifier)
        .store_pre_digest(true)
        .verify(&signed)
        .expect("verification runs");
    assert_eq!(result.status, DsigStatus::Valid);
    assert_eq!(
        result.signed_info_references[0].pre_digest_data.as_deref(),
        Some(b"<?xml version=\"1.0\"?>\n<out>a</out>\n".as_slice())
    );
}

#[cfg(feature = "xml-backend-xmloxide")]
#[test]
fn stylesheets_keep_namespace_declarations_defaulted_by_the_dtd() {
    // An attribute-list declaration can default an `xmlns:p` attribute (XML 1.0 section
    // 3.3.2); the stylesheet element then binds `p` as if it were written on it
    // (Namespaces in XML 1.0 section 3). The xmloxide parser applies such defaults;
    // roxmltree does not and rejects the document, so the backend is chosen here.
    let template = format!(
        concat!(
            r#"<!DOCTYPE root [<!ATTLIST xsl:stylesheet xmlns:p CDATA "urn:p">]>"#,
            r#"<root><item>a</item>"#,
            r#"<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"><ds:SignedInfo>"#,
            r#"<ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>"#,
            r#"<ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#hmac-sha256"/>"#,
            r#"<ds:Reference URI=""><ds:Transforms>"#,
            r#"<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>"#,
            r#"<ds:Transform Algorithm="{xslt}"><xsl:stylesheet version="1.0" xmlns:xsl="{xsl}" "#,
            r#"exclude-result-prefixes="ds"><xsl:template match="/"><p:out>"#,
            r#"<xsl:value-of select="//item"/></p:out></xsl:template></xsl:stylesheet>"#,
            r#"</ds:Transform></ds:Transforms>"#,
            r#"<ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>"#,
            r#"<ds:DigestValue/></ds:Reference></ds:SignedInfo><ds:SignatureValue/>"#,
            r#"</ds:Signature></root>"#
        ),
        xsl = XSL,
        xslt = XSLT_TRANSFORM_URI
    );
    let signer = xml_sec::xmldsig::HmacSigningKey::new(vec![7; 32]).unwrap();
    let verifier = xml_sec::xmldsig::HmacVerificationKey::new(vec![7; 32]).unwrap();
    let mut signing = SigningPolicy::default();
    signing.xml.allow_internal_dtd = true;
    let signed = SignContext::new(&signer)
        .policy(signing)
        .xml_backend(xml_sec::XmlBackend::Xmloxide)
        .sign_template(&template)
        .expect("the defaulted prefix is bound");
    let mut verification = VerificationPolicy::default();
    verification.xml.allow_internal_dtd = true;
    let result = xml_sec::xmldsig::VerifyContext::new()
        .policy(verification)
        .xml_backend(xml_sec::XmlBackend::Xmloxide)
        .key(&verifier)
        .store_pre_digest(true)
        .verify(&signed)
        .expect("verification runs");
    assert_eq!(result.status, DsigStatus::Valid);
    assert_eq!(
        result.signed_info_references[0].pre_digest_data.as_deref(),
        Some(b"<?xml version=\"1.0\"?>\n<p:out xmlns:p=\"urn:p\">a</p:out>\n".as_slice())
    );
}

#[test]
fn invalid_stylesheets_are_typed_errors() {
    assert!(matches!(
        XsltStylesheet::new("<xsl:stylesheet"),
        Err(TransformError::XmlParse(_))
    ));
    assert!(matches!(
        XsltStylesheet::new("<a/><b/>"),
        Err(TransformError::UnsupportedTransform(_))
    ));
    assert!(matches!(
        XsltStylesheet::new(r#"<?xml version="1.0"?><a/>"#),
        Err(TransformError::XmlParse(_))
    ));

    // Well-formed markup that is not a valid XSLT 1.0 stylesheet fails at execution.
    let reference = ReferenceBuilder::new(DigestAlgorithm::Sha256)
        .uri("")
        .transform(Transform::Enveloped)
        .transform(Transform::Xslt(stylesheet("<xsl:unknown/>")));
    let error = transform_error(sign(ITEMS, &builder(reference)).expect_err("unknown instruction"));
    assert!(matches!(error, TransformError::Xslt(_)), "{error:?}");
}

#[test]
fn the_stylesheet_must_be_the_sole_child_of_its_transform() {
    // XMLDSig 1.1 section 6.6.5: the stylesheet "MUST be the sole child of the
    // Transform element". Content beside it is refused when the transform is parsed,
    // not dropped from the stylesheet that is compiled.
    let parse = |content: &str| {
        let xml = format!(
            concat!(
                r#"<Transforms xmlns="http://www.w3.org/2000/09/xmldsig#">"#,
                r#"<Transform Algorithm="{xslt}">{content}</Transform></Transforms>"#
            ),
            xslt = XSLT_TRANSFORM_URI,
            content = content
        );
        let document = xml_sec::Document::parse(&xml).expect("fixed XML parses");
        xml_sec::xmldsig::parse_transforms(document.root_element()).map(|chain| chain.len())
    };
    let sheet = format!(r#"<xsl:stylesheet version="1.0" xmlns:xsl="{XSL}"/>"#);
    assert_eq!(parse(&format!("\n {sheet} <!--note-->\n")).ok(), Some(1));
    for content in [
        format!("{sheet}<extra/>"),
        format!("{sheet}text"),
        format!("text{sheet}"),
    ] {
        assert!(
            matches!(
                parse(&content),
                Err(TransformError::UnsupportedTransform(_))
            ),
            "{content}"
        );
    }
}

#[test]
fn the_stylesheet_root_is_an_xslt_stylesheet_element() {
    // XMLDSig 1.1 section 6.6.5 requires "the mandatory XSLT namespace-qualified
    // stylesheet element"; a literal result element used as a simplified stylesheet
    // (XSLT 1.0 section 2.3) is not one.
    let simplified = format!(r#"<out xsl:version="1.0" xmlns:xsl="{XSL}">x</out>"#);
    assert!(matches!(
        XsltStylesheet::new(simplified.clone()),
        Err(TransformError::UnsupportedTransform(_))
    ));
    let xml = format!(
        concat!(
            r#"<Transforms xmlns="http://www.w3.org/2000/09/xmldsig#">"#,
            r#"<Transform Algorithm="{xslt}">{content}</Transform></Transforms>"#
        ),
        xslt = XSLT_TRANSFORM_URI,
        content = simplified
    );
    let document = xml_sec::Document::parse(&xml).expect("fixed XML parses");
    assert!(matches!(
        xml_sec::xmldsig::parse_transforms(document.root_element()),
        Err(TransformError::UnsupportedTransform(_))
    ));
    let transform = format!(r#"<xsl:transform version="1.0" xmlns:xsl="{XSL}"/>"#);
    assert!(XsltStylesheet::new(transform).is_ok());
}

#[test]
fn stylesheet_size_is_checked_before_the_template_is_written() {
    // A stylesheet larger than the document limit is refused by its own size, before
    // it is copied into a signature template.
    let sheet = stylesheet(&format!(
        r#"<xsl:template match="/"><out>{}</out></xsl:template>"#,
        "x".repeat(4096)
    ));
    let mut policy = SigningPolicy::default();
    policy.resources.max_xml_document_bytes = 1024;
    let reference = ReferenceBuilder::new(DigestAlgorithm::Sha256)
        .uri("")
        .transform(Transform::Enveloped)
        .transform(Transform::Xslt(sheet.clone()));
    let violation = policy_error(
        sign_with_policy(ITEMS, &builder(reference), policy)
            .expect_err("the stylesheet exceeds the document limit"),
    );
    assert!(
        matches!(
            violation,
            PolicyViolation::ResourceLimit { maximum: 1024, actual, .. }
                if actual == sheet.source().len()
        ),
        "{violation:?}"
    );
}

#[test]
fn stylesheets_are_bounded_together_before_the_template_is_written() {
    // Each stylesheet fits the document limit, their sum does not: all of them are
    // refused by their combined size before any is copied into the template.
    let sheet = stylesheet(&format!(
        r#"<xsl:template match="/"><out>{}</out></xsl:template>"#,
        "x".repeat(600)
    ));
    let size = sheet.source().len();
    let mut policy = SigningPolicy::default();
    policy.resources.max_xml_document_bytes = size + size / 2;
    let reference = || {
        ReferenceBuilder::new(DigestAlgorithm::Sha256)
            .uri("")
            .transform(Transform::Enveloped)
            .transform(Transform::Xslt(sheet.clone()))
    };
    let violation = policy_error(
        sign_with_policy(
            ITEMS,
            &builder(reference()).add_reference(reference()),
            policy,
        )
        .expect_err("the stylesheets together exceed the document limit"),
    );
    assert!(
        matches!(
            violation,
            PolicyViolation::ResourceLimit { actual, .. } if actual == 2 * size
        ),
        "{violation:?}"
    );
}

#[test]
fn parsed_stylesheets_are_written_without_their_original_document() {
    // A stylesheet parsed from one document may use a prefix declared on an ancestor
    // of its `<Transform>` and an entity of that document's DTD. Written into another
    // signature, it must not depend on either.
    let source = format!(
        concat!(
            r#"<!DOCTYPE Transforms [<!ENTITY path "//item">]>"#,
            r#"<Transforms xmlns="http://www.w3.org/2000/09/xmldsig#" xmlns:xsl="{xsl}">"#,
            r#"<Transform Algorithm="{xslt}"><xsl:stylesheet version="1.0" xmlns="">"#,
            r#"<xsl:template match="/"><out><xsl:value-of select="&path;"/></out>"#,
            r#"</xsl:template></xsl:stylesheet></Transform></Transforms>"#
        ),
        xsl = XSL,
        xslt = XSLT_TRANSFORM_URI
    );
    let document = xml_sec::Document::parse_with_options(
        &source,
        xml_sec::ParsingOptions {
            allow_dtd: true,
            ..Default::default()
        },
    )
    .expect("fixed XML parses");
    let mut chain =
        xml_sec::xmldsig::parse_transforms(document.root_element()).expect("the stylesheet parses");
    let transform = chain.pop().expect("one transform");
    let reference = ReferenceBuilder::new(DigestAlgorithm::Sha256)
        .uri("")
        .transform(Transform::Enveloped)
        .transform(transform);
    let signed = sign(ITEMS, &builder(reference)).expect("the written stylesheet is complete");
    assert_eq!(
        verified_pre_digest(&signed),
        [b"<?xml version=\"1.0\"?>\n<out>b</out>\n".to_vec()]
    );
}

#[test]
fn binary_input_document_type_follows_the_xml_input_policy() {
    // Internal entities and attribute defaults change the transformed data, so a
    // DOCTYPE in decoded input is refused unless the operation allows internal DTDs,
    // with the same error as the binary-to-node-set adapter.
    let encoded = base64::engine::general_purpose::STANDARD
        .encode(r#"<!DOCTYPE doc [<!ENTITY e "y">]><doc><item>&e;</item></doc>"#);
    let document = format!(r#"<root><data ID="payload">{encoded}</data></root>"#);
    let reference = ReferenceBuilder::new(DigestAlgorithm::Sha256)
        .uri("#payload")
        .transform(Transform::Base64Decode)
        .transform(Transform::Xslt(values()));
    let error = transform_error(
        sign(&document, &builder(reference.clone())).expect_err("DTDs are not allowed"),
    );
    assert!(
        matches!(
            error,
            TransformError::XmlParse(XmlDocumentError::Parse(XmlDomParseError::DtdDetected))
        ),
        "{error:?}"
    );

    let mut policy = SigningPolicy::default();
    policy.xml.allow_internal_dtd = true;
    let signed =
        sign_with_policy(&document, &builder(reference), policy).expect("DTDs are allowed");
    let resolver = DefaultKeyResolver::default();
    let mut verification = VerificationPolicy::default();
    verification.key_trust.mode = xml_sec::policy::VerificationTrustMode::CryptographicOnly;
    verification.xml.allow_internal_dtd = true;
    let result = xml_sec::xmldsig::VerifyContext::new()
        .policy(verification)
        .key_resolver(&resolver)
        .store_pre_digest(true)
        .verify(&signed)
        .expect("signature with an allowed DTD verifies");
    assert_eq!(result.status, DsigStatus::Valid);
    assert_eq!(
        result.signed_info_references[0].pre_digest_data.as_deref(),
        Some(b"<?xml version=\"1.0\"?>\n<values><v>y</v></values>\n".as_slice())
    );
}

#[test]
fn verification_reports_xslt_failures_as_reference_errors() {
    // A stylesheet that compiles at signing but fails at verification is a
    // reference processing error, not a digest mismatch.
    let terminate = stylesheet(
        r#"<xsl:template match="/"><xsl:message terminate="yes">stop</xsl:message></xsl:template>"#,
    );
    let reference = ReferenceBuilder::new(DigestAlgorithm::Sha256)
        .uri("")
        .transform(Transform::Enveloped)
        .transform(Transform::Xslt(terminate));
    let error =
        transform_error(sign(ITEMS, &builder(reference)).expect_err("terminating message aborts"));
    assert!(matches!(error, TransformError::Xslt(_)), "{error:?}");

    let signed = sign(
        ITEMS,
        &builder(
            ReferenceBuilder::new(DigestAlgorithm::Sha256)
                .uri("")
                .transform(Transform::Enveloped)
                .transform(Transform::Xslt(values())),
        ),
    )
    .expect("valid stylesheet signs");
    let broken = signed.replacen("<xsl:sort select=\".\"/>", "<xsl:sort select=\"(\"/>", 1);
    assert_ne!(broken, signed);
    let resolver = DefaultKeyResolver::default();
    let error = cryptographic::context()
        .key_resolver(&resolver)
        .verify(&broken)
        .expect_err("invalid XPath in the stylesheet");
    assert!(
        matches!(
            error,
            DsigError::Reference(ReferenceProcessingError::Transform(TransformError::Xslt(_)))
        ),
        "{error:?}"
    );
}