@@ -4,6 +4,7 @@ use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
4
4
use quick_xml:: Writer ;
5
5
use serde:: Deserialize ;
6
6
use std:: io:: Cursor ;
7
+ use std:: str:: FromStr ;
7
8
8
9
const NAME : & str = "ds:Signature" ;
9
10
const SCHEMA : ( & str , & str ) = ( "xmlns:ds" , "http://www.w3.org/2000/09/xmldsig#" ) ;
@@ -33,32 +34,30 @@ impl Signature {
33
34
algorithm : SignatureAlgorithm :: RsaSha256 ,
34
35
hmac_output_length : None ,
35
36
} ,
36
- reference : vec ! [
37
- Reference {
38
- transforms: Some ( Transforms {
39
- transforms: vec![
40
- Transform {
41
- algorithm: "http://www.w3.org/2000/09/xmldsig#enveloped-signature"
42
- . to_string( ) ,
43
- xpath: None ,
44
- } ,
45
- Transform {
46
- algorithm: "http://www.w3.org/2001/10/xml-exc-c14n#" . to_string( ) ,
47
- xpath: None ,
48
- } ,
49
- ] ,
50
- } ) ,
51
- digest_method: DigestMethod {
52
- algorithm: DigestAlgorithm :: Sha1 ,
53
- } ,
54
- digest_value: Some ( DigestValue {
55
- base64_content: Some ( "" . to_string( ) ) ,
56
- } ) ,
57
- uri: Some ( format!( "#{}" , ref_id) ) ,
58
- reference_type: None ,
59
- id: None ,
60
- }
61
- ] ,
37
+ reference : vec ! [ Reference {
38
+ transforms: Some ( Transforms {
39
+ transforms: vec![
40
+ Transform {
41
+ algorithm: "http://www.w3.org/2000/09/xmldsig#enveloped-signature"
42
+ . to_string( ) ,
43
+ xpath: None ,
44
+ } ,
45
+ Transform {
46
+ algorithm: "http://www.w3.org/2001/10/xml-exc-c14n#" . to_string( ) ,
47
+ xpath: None ,
48
+ } ,
49
+ ] ,
50
+ } ) ,
51
+ digest_method: DigestMethod {
52
+ algorithm: DigestAlgorithm :: Sha1 ,
53
+ } ,
54
+ digest_value: Some ( DigestValue {
55
+ base64_content: Some ( "" . to_string( ) ) ,
56
+ } ) ,
57
+ uri: Some ( format!( "#{}" , ref_id) ) ,
58
+ reference_type: None ,
59
+ id: None ,
60
+ } ] ,
62
61
} ,
63
62
signature_value : SignatureValue {
64
63
id : None ,
@@ -294,22 +293,43 @@ impl TryFrom<&SignatureMethod> for Event<'_> {
294
293
295
294
#[ derive( Clone , Debug , Deserialize , Hash , Eq , PartialEq , Ord , PartialOrd ) ]
296
295
pub enum SignatureAlgorithm {
297
- #[ serde( rename= "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" ) ]
296
+ #[ serde( rename = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" ) ]
298
297
RsaSha256 ,
299
- #[ serde( rename= "http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1" ) ]
298
+ #[ serde( rename = "http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1" ) ]
300
299
Sha256RsaMGF1 ,
300
+ #[ serde( rename = "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256" ) ]
301
+ EcdsaSha256 ,
301
302
#[ serde( untagged) ]
302
303
Unsupported ( String ) ,
303
304
}
304
305
306
+ impl FromStr for SignatureAlgorithm {
307
+ type Err = Box < dyn std:: error:: Error > ;
308
+
309
+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
310
+ Ok ( match s {
311
+ "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" => SignatureAlgorithm :: RsaSha256 ,
312
+ "http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1" => {
313
+ SignatureAlgorithm :: Sha256RsaMGF1
314
+ }
315
+ "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256" => {
316
+ SignatureAlgorithm :: EcdsaSha256
317
+ }
318
+ i => SignatureAlgorithm :: Unsupported ( i. to_string ( ) ) ,
319
+ } )
320
+ }
321
+ }
322
+
305
323
impl SignatureAlgorithm {
306
324
const RSA_SHA256 : & ' static str = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" ;
307
325
const SHA256_RSA_MGF1 : & ' static str = "http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1" ;
326
+ const SHA256_ECDSA : & ' static str = "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256" ;
308
327
309
328
pub fn value ( & self ) -> & str {
310
329
match self {
311
330
SignatureAlgorithm :: RsaSha256 => Self :: RSA_SHA256 ,
312
331
SignatureAlgorithm :: Sha256RsaMGF1 => Self :: SHA256_RSA_MGF1 ,
332
+ SignatureAlgorithm :: EcdsaSha256 => Self :: SHA256_ECDSA ,
313
333
SignatureAlgorithm :: Unsupported ( algo) => algo,
314
334
}
315
335
}
@@ -430,9 +450,9 @@ impl TryFrom<&DigestMethod> for Event<'_> {
430
450
431
451
#[ derive( Clone , Debug , Deserialize , Hash , Eq , PartialEq , Ord , PartialOrd ) ]
432
452
pub enum DigestAlgorithm {
433
- #[ serde( rename= "http://www.w3.org/2000/09/xmldsig#sha1" ) ]
453
+ #[ serde( rename = "http://www.w3.org/2000/09/xmldsig#sha1" ) ]
434
454
Sha1 ,
435
- #[ serde( rename= "http://www.w3.org/2001/04/xmlenc#sha256" ) ]
455
+ #[ serde( rename = "http://www.w3.org/2001/04/xmlenc#sha256" ) ]
436
456
Sha256 ,
437
457
#[ serde( untagged) ]
438
458
Unsupported ( String ) ,
@@ -588,8 +608,10 @@ mod test {
588
608
589
609
#[ test]
590
610
pub fn test_canonicalizationmethod_deserialization ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
591
- let canonicalization_method = r#"<ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>"# ;
592
- let deserialized: CanonicalizationMethod = quick_xml:: de:: from_str ( canonicalization_method) ?;
611
+ let canonicalization_method =
612
+ r#"<ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>"# ;
613
+ let deserialized: CanonicalizationMethod =
614
+ quick_xml:: de:: from_str ( canonicalization_method) ?;
593
615
let serialized = deserialized. to_xml ( ) ?;
594
616
let re_deserialized: CanonicalizationMethod = quick_xml:: de:: from_str ( & serialized) ?;
595
617
assert_eq ! ( deserialized, re_deserialized) ;
@@ -627,7 +649,8 @@ mod test {
627
649
628
650
#[ test]
629
651
pub fn test_digestmethod_deserialization ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
630
- let digest_method = r#"<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />"# ;
652
+ let digest_method =
653
+ r#"<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />"# ;
631
654
let deserialized: DigestMethod = quick_xml:: de:: from_str ( digest_method) ?;
632
655
let serialized = deserialized. to_xml ( ) ?;
633
656
let re_deserialized: DigestMethod = quick_xml:: de:: from_str ( & serialized) ?;
0 commit comments