11use base64:: engine:: general_purpose;
22use base64:: Engine ;
33use der:: { DecodePem , EncodePem } ;
4+ use openssl:: error:: ErrorStack ;
45use openssl:: hash:: { hash, MessageDigest } ;
56use openssl:: pkey:: PKey ;
67use openssl:: rsa:: Rsa ;
@@ -12,7 +13,9 @@ use rsa::RsaPrivateKey;
1213use std:: io:: Cursor ;
1314use std:: { fs, io} ;
1415use x509_cert:: Certificate ;
15- use xml_c14n:: { canonicalize_xml, CanonicalizationMode , CanonicalizationOptions } ;
16+ use xml_c14n:: {
17+ canonicalize_xml, CanonicalizationErrorCode , CanonicalizationMode , CanonicalizationOptions ,
18+ } ;
1619
1720#[ derive( Debug , thiserror:: Error ) ]
1821pub enum EncryptionError {
@@ -57,34 +60,14 @@ impl RsaKeyPair {
5760
5861#[ derive( Debug , thiserror:: Error ) ]
5962pub enum SignErr {
60- #[ error( "Error while signing" ) ]
61- Generic ,
62- #[ error( "Error `{0}`" ) ]
63- GenericWithMessage ( String ) ,
64- #[ error( "Error" ) ]
65- Std ( Box < dyn std:: error:: Error + Send + Sync > ) ,
6663 #[ error( transparent) ]
6764 Pkcs1 ( #[ from] rsa:: pkcs1:: Error ) ,
6865 #[ error( transparent) ]
69- Any ( #[ from] anyhow:: Error ) ,
70- }
71-
72- impl From < ( ) > for SignErr {
73- fn from ( _error : ( ) ) -> Self {
74- Self :: Generic
75- }
76- }
77-
78- impl From < String > for SignErr {
79- fn from ( error : String ) -> Self {
80- Self :: GenericWithMessage ( error)
81- }
82- }
83-
84- impl From < Box < dyn std:: error:: Error + Send + Sync > > for SignErr {
85- fn from ( error : Box < dyn std:: error:: Error + Send + Sync > ) -> Self {
86- Self :: Std ( error)
87- }
66+ Key ( #[ from] ErrorStack ) ,
67+ #[ error( transparent) ]
68+ IO ( #[ from] io:: Error ) ,
69+ #[ error( transparent) ]
70+ Canonicalization ( #[ from] CanonicalizationErrorCode ) ,
8871}
8972
9073pub struct XSigner {
@@ -103,12 +86,10 @@ impl XSigner {
10386 keep_comments : true ,
10487 inclusive_ns_prefixes : vec ! [ ] ,
10588 } ;
106- let xml_canonicalized = canonicalize_xml ( & self . xml_document , canonicalize_options. clone ( ) )
107- . expect ( "Could not canonicalize xml" ) ;
89+ let xml_canonicalize = canonicalize_xml ( & self . xml_document , canonicalize_options. clone ( ) ) ?;
10890
10991 // Generate digest
110- let digest = hash ( MessageDigest :: sha256 ( ) , xml_canonicalized. as_bytes ( ) )
111- . expect ( "Digest generation error" ) ;
92+ let digest = hash ( MessageDigest :: sha256 ( ) , xml_canonicalize. as_bytes ( ) ) ?;
11293 let digest_base64 = general_purpose:: STANDARD . encode ( digest) ;
11394
11495 // Sign
@@ -126,13 +107,12 @@ impl XSigner {
126107 </ds:SignedInfo>"
127108 ) ;
128109 let signed_info_canonicalize =
129- canonicalize_xml ( & signed_info_string, canonicalize_options. clone ( ) )
130- . expect ( "Could not canonicalize xml" ) ;
110+ canonicalize_xml ( & signed_info_string, canonicalize_options. clone ( ) ) ?;
131111
132112 // Sign <ds:SignedInfo>
133113 let pk_pem = key_pair. private_key_to_pem ( ) ?;
134- let rsa = Rsa :: private_key_from_pem ( pk_pem. as_bytes ( ) ) . expect ( "Failed to parse PK" ) ;
135- let pkey = PKey :: from_rsa ( rsa) . expect ( "Failed to convert RSA to PKey" ) ;
114+ let rsa = Rsa :: private_key_from_pem ( pk_pem. as_bytes ( ) ) ? ;
115+ let pkey = PKey :: from_rsa ( rsa) ? ;
136116
137117 let certificate_pem = key_pair. certificate_to_pem ( ) ?;
138118 let pem_contents = certificate_pem
@@ -141,12 +121,9 @@ impl XSigner {
141121 . collect :: < Vec < _ > > ( )
142122 . join ( "\n " ) ;
143123
144- let mut signer =
145- Signer :: new ( MessageDigest :: sha256 ( ) , & pkey) . expect ( "Signer creation error" ) ;
146- signer
147- . update ( signed_info_canonicalize. as_bytes ( ) )
148- . expect ( "Failed to update signer" ) ;
149- let signature = signer. sign_to_vec ( ) . expect ( "Error while signing" ) ;
124+ let mut signer = Signer :: new ( MessageDigest :: sha256 ( ) , & pkey) ?;
125+ signer. update ( signed_info_canonicalize. as_bytes ( ) ) ?;
126+ let signature = signer. sign_to_vec ( ) ?;
150127 let signature_base64 = general_purpose:: STANDARD . encode ( & signature) ;
151128
152129 // Signature
@@ -162,7 +139,7 @@ impl XSigner {
162139 </ds:Signature>"
163140 ) ;
164141
165- let mut xml_reader = quick_xml:: Reader :: from_str ( & xml_canonicalized ) ;
142+ let mut xml_reader = quick_xml:: Reader :: from_str ( & xml_canonicalize ) ;
166143 let mut xml_writer = quick_xml:: Writer :: new ( Cursor :: new ( Vec :: new ( ) ) ) ;
167144
168145 let mut inside_target_element = false ;
@@ -176,8 +153,7 @@ impl XSigner {
176153 requires_closing_extension_content_tag = true ;
177154
178155 xml_writer
179- . write_event ( Event :: Start ( BytesStart :: new ( "ext:ExtensionContent" ) ) )
180- . unwrap ( ) ;
156+ . write_event ( Event :: Start ( BytesStart :: new ( "ext:ExtensionContent" ) ) ) ?;
181157 } else {
182158 assert ! ( xml_writer. write_event( Event :: Start ( e. clone( ) ) ) . is_ok( ) ) ;
183159 }
@@ -207,8 +183,7 @@ impl XSigner {
207183
208184 if requires_closing_extension_content_tag {
209185 xml_writer
210- . write_event ( Event :: End ( BytesEnd :: new ( "ext:ExtensionContent" ) ) )
211- . unwrap ( ) ;
186+ . write_event ( Event :: End ( BytesEnd :: new ( "ext:ExtensionContent" ) ) ) ?;
212187 }
213188 }
214189 assert ! ( xml_writer. write_event( Event :: End ( e. clone( ) ) ) . is_ok( ) ) ;
0 commit comments