@@ -67,6 +67,8 @@ let cmac_key = ctx.keygen().unwrap();
67
67
#[ cfg( not( boringssl) ) ]
68
68
use crate :: cipher:: CipherRef ;
69
69
use crate :: error:: ErrorStack ;
70
+ #[ cfg( ossl300) ]
71
+ use crate :: lib_ctx:: LibCtxRef ;
70
72
use crate :: md:: MdRef ;
71
73
use crate :: pkey:: { HasPrivate , HasPublic , Id , PKey , PKeyRef , Private } ;
72
74
use crate :: rsa:: Padding ;
@@ -81,6 +83,8 @@ use openssl_macros::corresponds;
81
83
use std:: convert:: TryFrom ;
82
84
#[ cfg( ossl320) ]
83
85
use std:: ffi:: CStr ;
86
+ #[ cfg( ossl300) ]
87
+ use std:: ffi:: CString ;
84
88
use std:: ptr;
85
89
86
90
/// HKDF modes of operation.
@@ -156,6 +160,26 @@ impl PkeyCtx<()> {
156
160
Ok ( PkeyCtx :: from_ptr ( ptr) )
157
161
}
158
162
}
163
+
164
+ /// Creates a new pkey context from the algorithm name.
165
+ #[ corresponds( EVP_PKEY_CTX_new_from_name ) ]
166
+ #[ cfg( ossl300) ]
167
+ pub fn new_from_name (
168
+ libctx : Option < & LibCtxRef > ,
169
+ name : & str ,
170
+ propquery : Option < & str > ,
171
+ ) -> Result < Self , ErrorStack > {
172
+ unsafe {
173
+ let propquery = propquery. map ( |s| CString :: new ( s) . unwrap ( ) ) ;
174
+ let name = CString :: new ( name) . unwrap ( ) ;
175
+ let ptr = cvt_p ( ffi:: EVP_PKEY_CTX_new_from_name (
176
+ libctx. map_or ( ptr:: null_mut ( ) , ForeignTypeRef :: as_ptr) ,
177
+ name. as_ptr ( ) ,
178
+ propquery. map_or ( ptr:: null_mut ( ) , |s| s. as_ptr ( ) ) ,
179
+ ) ) ?;
180
+ Ok ( PkeyCtx :: from_ptr ( ptr) )
181
+ }
182
+ }
159
183
}
160
184
161
185
impl < T > PkeyCtxRef < T >
@@ -756,6 +780,20 @@ impl<T> PkeyCtxRef<T> {
756
780
Ok ( ( ) )
757
781
}
758
782
783
+ /// Generates a new public/private keypair.
784
+ ///
785
+ /// New OpenSSL 3.0 function, that should do the same thing as keygen()
786
+ #[ corresponds( EVP_PKEY_generate ) ]
787
+ #[ cfg( ossl300) ]
788
+ #[ inline]
789
+ pub fn generate ( & mut self ) -> Result < PKey < Private > , ErrorStack > {
790
+ unsafe {
791
+ let mut key = ptr:: null_mut ( ) ;
792
+ cvt ( ffi:: EVP_PKEY_generate ( self . as_ptr ( ) , & mut key) ) ?;
793
+ Ok ( PKey :: from_ptr ( key) )
794
+ }
795
+ }
796
+
759
797
/// Gets the nonce type for a private key context.
760
798
///
761
799
/// The nonce for DSA and ECDSA can be either random (the default) or deterministic (as defined by RFC 6979).
@@ -780,6 +818,14 @@ impl<T> PkeyCtxRef<T> {
780
818
}
781
819
Ok ( NonceType ( nonce_type) )
782
820
}
821
+
822
+ /// Initializes a conversion from `OsllParam` to `PKey` on given `PkeyCtx`.
823
+ #[ corresponds( EVP_PKEY_fromdata_init ) ]
824
+ #[ cfg( ossl300) ]
825
+ pub fn fromdata_init ( & mut self ) -> Result < ( ) , ErrorStack > {
826
+ unsafe { cvt ( ffi:: EVP_PKEY_fromdata_init ( self . as_ptr ( ) ) ) ? } ;
827
+ Ok ( ( ) )
828
+ }
783
829
}
784
830
785
831
#[ cfg( test) ]
@@ -1107,4 +1153,14 @@ mxJ7imIrEg9nIQ==
1107
1153
assert_eq ! ( output, expected_output) ;
1108
1154
assert ! ( ErrorStack :: get( ) . errors( ) . is_empty( ) ) ;
1109
1155
}
1156
+
1157
+ #[ test]
1158
+ #[ cfg( ossl300) ]
1159
+ fn test_pkeyctx_from_name ( ) {
1160
+ let lib_ctx = crate :: lib_ctx:: LibCtx :: new ( ) . unwrap ( ) ;
1161
+ let _: PkeyCtx < ( ) > = PkeyCtx :: new_from_name ( Some ( lib_ctx. as_ref ( ) ) , "RSA" , None ) . unwrap ( ) ;
1162
+
1163
+ /* no libctx is ok */
1164
+ let _: PkeyCtx < ( ) > = PkeyCtx :: new_from_name ( None , "RSA" , None ) . unwrap ( ) ;
1165
+ }
1110
1166
}
0 commit comments