@@ -12,9 +12,17 @@ use parsec_client::core::interface::operations::psa_algorithm::Hash;
12
12
use parsec_client:: core:: interface:: operations:: psa_key_attributes:: { Attributes , EccFamily , Type } ;
13
13
use parsec_openssl2:: types:: VOID_PTR ;
14
14
use parsec_openssl2:: * ;
15
+ use picky_asn1:: wrapper:: IntegerAsn1 ;
16
+ use serde:: { Deserialize , Serialize } ;
15
17
use std:: ffi:: CStr ;
16
18
use std:: sync:: { Arc , RwLock } ;
17
19
20
+ #[ derive( Serialize , Deserialize ) ]
21
+ struct EccSignature {
22
+ r : IntegerAsn1 ,
23
+ s : IntegerAsn1 ,
24
+ }
25
+
18
26
struct ParsecProviderSignatureContext {
19
27
/* The key object is set in the signature context by calling OSSL_FUNC_signature_sign_init().
20
28
Before calling OSSL_FUNC_signature_sign_init(), the key object itself should have been set up
@@ -88,7 +96,7 @@ unsafe extern "C" fn parsec_provider_signature_digest_sign(
88
96
tbs : * const std:: os:: raw:: c_uchar ,
89
97
tbslen : std:: os:: raw:: c_uint ,
90
98
) -> std:: os:: raw:: c_int {
91
- let result = super :: r#catch ( Some ( || super :: Error :: PROVIDER_SIGNATURE_SIGN ) , || {
99
+ let result = super :: r#catch ( Some ( || super :: Error :: PROVIDER_SIGNATURE_DIGEST_SIGN ) , || {
92
100
if ctx. is_null ( ) || siglen. is_null ( ) {
93
101
return Err ( "Received unexpected NULL pointer as an argument." . into ( ) ) ;
94
102
}
@@ -155,19 +163,23 @@ unsafe extern "C" fn parsec_provider_signature_digest_sign(
155
163
. psa_hash_compute ( Hash :: Sha256 , tbs_slice)
156
164
. map_err ( |e| format ! ( "Parsec Client failed to hash: {:?}" , e) ) ?;
157
165
158
- let sign_res: Vec < u8 > = key_data
166
+ let mut sign_res: Vec < u8 > = key_data
159
167
. get_provctx ( )
160
168
. get_client ( )
161
169
. psa_sign_hash ( key_name, & hash_res, sign_algorithm)
162
170
. map_err ( |e| format ! ( "Parsec Client failed to sign: {:?}" , e) ) ?;
163
171
164
- if siglength != sign_res. len ( ) {
165
- Err ( format ! ( "Unexpected signature length: {}" , sign_res. len( ) ) . into ( ) )
166
- } else {
167
- std:: ptr:: copy ( sign_res. as_ptr ( ) , sig, sign_res. len ( ) ) ;
168
- * siglen = sign_res. len ( ) as u32 ;
169
- Ok ( OPENSSL_SUCCESS )
172
+ if sign_algorithm. is_ecc_alg ( ) {
173
+ let s = IntegerAsn1 :: from_bytes_be_unsigned ( sign_res. split_off ( sign_res. len ( ) / 2 ) ) ;
174
+ sign_res = picky_asn1_der:: to_vec ( & EccSignature {
175
+ r : IntegerAsn1 :: from_bytes_be_unsigned ( sign_res) ,
176
+ s,
177
+ } )
178
+ . map_err ( |e| format ! ( "Failed to convert ECC Signature: {:?}" , e) ) ?;
170
179
}
180
+ std:: ptr:: copy ( sign_res. as_ptr ( ) , sig, sign_res. len ( ) ) ;
181
+ * siglen = sign_res. len ( ) as u32 ;
182
+ Ok ( OPENSSL_SUCCESS )
171
183
} ) ;
172
184
173
185
match result {
0 commit comments