@@ -70,19 +70,22 @@ use crate::error::ErrorStack;
70
70
use crate :: md:: MdRef ;
71
71
use crate :: nid:: Nid ;
72
72
#[ cfg( ossl300) ]
73
+ use crate :: params:: ParamsRef ;
74
+ #[ cfg( ossl300) ]
73
75
use crate :: pkey:: Public ;
74
76
use crate :: pkey:: { HasPrivate , HasPublic , Id , PKey , PKeyRef , Params , Private } ;
75
77
use crate :: rsa:: Padding ;
76
78
use crate :: sign:: RsaPssSaltlen ;
77
79
use crate :: { cvt, cvt_p} ;
80
+ use cfg_if:: cfg_if;
78
81
use foreign_types:: { ForeignType , ForeignTypeRef } ;
79
82
#[ cfg( not( any( boringssl, awslc) ) ) ]
80
83
use libc:: c_int;
81
84
#[ cfg( ossl320) ]
82
85
use libc:: c_uint;
83
86
use openssl_macros:: corresponds;
84
87
use std:: convert:: TryFrom ;
85
- #[ cfg( ossl320 ) ]
88
+ #[ cfg( ossl300 ) ]
86
89
use std:: ffi:: CStr ;
87
90
use std:: ptr;
88
91
@@ -455,6 +458,31 @@ impl<T> PkeyCtxRef<T> {
455
458
Ok ( ( ) )
456
459
}
457
460
461
+ /// Prepares the context for creating a key from user data.
462
+ #[ corresponds( EVP_PKEY_fromdata_init ) ]
463
+ #[ inline]
464
+ #[ cfg( ossl300) ]
465
+ #[ allow( dead_code) ]
466
+ pub ( crate ) fn fromdata_init ( & mut self ) -> Result < ( ) , ErrorStack > {
467
+ cvt ( unsafe { ffi:: EVP_PKEY_fromdata_init ( self . as_ptr ( ) ) } ) . map ( |_| ( ) )
468
+ }
469
+
470
+ /// Convert a stack of Params into a PKey.
471
+ #[ corresponds( EVP_PKEY_fromdata ) ]
472
+ #[ inline]
473
+ #[ cfg( ossl300) ]
474
+ #[ allow( dead_code) ]
475
+ pub ( crate ) fn fromdata < K : Selection > (
476
+ & mut self ,
477
+ params : & ParamsRef < ' _ > ,
478
+ ) -> Result < PKey < K > , ErrorStack > {
479
+ let mut key_ptr = ptr:: null_mut ( ) ;
480
+ cvt ( unsafe {
481
+ ffi:: EVP_PKEY_fromdata ( self . as_ptr ( ) , & mut key_ptr, K :: SELECTION , params. as_ptr ( ) )
482
+ } ) ?;
483
+ Ok ( unsafe { PKey :: from_ptr ( key_ptr) } )
484
+ }
485
+
458
486
/// Sets which algorithm was used to compute the digest used in a
459
487
/// signature. With RSA signatures this causes the signature to be wrapped
460
488
/// in a `DigestInfo` structure. This is almost always what you want with
@@ -894,20 +922,38 @@ impl<T> PkeyCtxRef<T> {
894
922
}
895
923
}
896
924
925
+ /// Creates a new `PKey` from the given ID and parameters.
926
+ #[ cfg( ossl300) ]
927
+ #[ allow( dead_code) ]
928
+ pub ( crate ) fn pkey_from_params < K : Selection > (
929
+ id : Id ,
930
+ params : & ParamsRef < ' _ > ,
931
+ ) -> Result < PKey < K > , ErrorStack > {
932
+ let mut ctx = PkeyCtx :: new_id ( id) ?;
933
+ ctx. fromdata_init ( ) ?;
934
+ ctx. fromdata ( params)
935
+ }
936
+
897
937
#[ cfg( test) ]
898
938
mod test {
899
939
use super :: * ;
940
+ #[ cfg( ossl300) ]
941
+ use crate :: bn:: BigNum ;
900
942
#[ cfg( not( any( boringssl, awslc) ) ) ]
901
943
use crate :: cipher:: Cipher ;
902
944
use crate :: ec:: { EcGroup , EcKey } ;
903
945
use crate :: hash:: { hash, MessageDigest } ;
904
946
use crate :: md:: Md ;
905
947
use crate :: nid:: Nid ;
948
+ #[ cfg( ossl300) ]
949
+ use crate :: params:: ParamBuilder ;
906
950
use crate :: pkey:: PKey ;
907
951
use crate :: rsa:: Rsa ;
908
952
use crate :: sign:: Verifier ;
909
953
#[ cfg( not( boringssl) ) ]
910
954
use cfg_if:: cfg_if;
955
+ #[ cfg( ossl300) ]
956
+ use std:: cmp:: Ordering ;
911
957
912
958
#[ test]
913
959
fn rsa ( ) {
@@ -1267,4 +1313,30 @@ mxJ7imIrEg9nIQ==
1267
1313
assert_eq ! ( output, expected_output) ;
1268
1314
assert ! ( ErrorStack :: get( ) . errors( ) . is_empty( ) ) ;
1269
1315
}
1316
+
1317
+ #[ test]
1318
+ #[ cfg( ossl300) ]
1319
+ fn test_fromdata ( ) {
1320
+ let n = BigNum :: from_u32 ( 0xbc747fc5 ) . unwrap ( ) ;
1321
+ let e = BigNum :: from_u32 ( 0x10001 ) . unwrap ( ) ;
1322
+ let d = BigNum :: from_u32 ( 0x7b133399 ) . unwrap ( ) ;
1323
+
1324
+ let params = ParamBuilder :: new ( )
1325
+ . push_bignum ( CStr :: from_bytes_with_nul ( b"n\0 " ) . unwrap ( ) , & n)
1326
+ . unwrap ( )
1327
+ . push_bignum ( CStr :: from_bytes_with_nul ( b"e\0 " ) . unwrap ( ) , & e)
1328
+ . unwrap ( )
1329
+ . push_bignum ( CStr :: from_bytes_with_nul ( b"d\0 " ) . unwrap ( ) , & d)
1330
+ . unwrap ( )
1331
+ . build ( )
1332
+ . unwrap ( ) ;
1333
+
1334
+ let pkey: PKey < Private > = pkey_from_params ( Id :: RSA , & params) . unwrap ( ) ;
1335
+
1336
+ let rsa = pkey. rsa ( ) . unwrap ( ) ;
1337
+
1338
+ assert_eq ! ( rsa. n( ) . ucmp( & n) , Ordering :: Equal ) ;
1339
+ assert_eq ! ( rsa. e( ) . ucmp( & e) , Ordering :: Equal ) ;
1340
+ assert_eq ! ( rsa. d( ) . ucmp( & d) , Ordering :: Equal ) ;
1341
+ }
1270
1342
}
0 commit comments