@@ -173,8 +173,8 @@ ossl_rsa_s_generate(int argc, VALUE *argv, VALUE klass)
173173static VALUE
174174ossl_rsa_initialize (int argc , VALUE * argv , VALUE self )
175175{
176- EVP_PKEY * pkey ;
177- RSA * rsa ;
176+ EVP_PKEY * pkey , * tmp ;
177+ RSA * rsa = NULL ;
178178 BIO * in ;
179179 VALUE arg , pass ;
180180
@@ -279,6 +279,21 @@ ossl_rsa_is_private(VALUE self)
279279 return RSA_PRIVATE (self , rsa ) ? Qtrue : Qfalse ;
280280}
281281
282+ static int
283+ can_export_rsaprivatekey (VALUE self )
284+ {
285+ RSA * rsa ;
286+ const BIGNUM * n , * e , * d , * p , * q , * dmp1 , * dmq1 , * iqmp ;
287+
288+ GetRSA (self , rsa );
289+
290+ RSA_get0_key (rsa , & n , & e , & d );
291+ RSA_get0_factors (rsa , & p , & q );
292+ RSA_get0_crt_params (rsa , & dmp1 , & dmq1 , & iqmp );
293+
294+ return n && e && d && p && q && dmp1 && dmq1 && iqmp ;
295+ }
296+
282297/*
283298 * call-seq:
284299 * rsa.export([cipher, pass_phrase]) => PEM-format String
@@ -292,41 +307,10 @@ ossl_rsa_is_private(VALUE self)
292307static VALUE
293308ossl_rsa_export (int argc , VALUE * argv , VALUE self )
294309{
295- RSA * rsa ;
296- const BIGNUM * n , * e , * d , * p , * q , * dmp1 , * dmq1 , * iqmp ;
297- BIO * out ;
298- const EVP_CIPHER * ciph = NULL ;
299- VALUE cipher , pass , str ;
300-
301- GetRSA (self , rsa );
302-
303- rb_scan_args (argc , argv , "02" , & cipher , & pass );
304-
305- if (!NIL_P (cipher )) {
306- ciph = ossl_evp_get_cipherbyname (cipher );
307- pass = ossl_pem_passwd_value (pass );
308- }
309- if (!(out = BIO_new (BIO_s_mem ()))) {
310- ossl_raise (eRSAError , NULL );
311- }
312- RSA_get0_key (rsa , & n , & e , & d );
313- RSA_get0_factors (rsa , & p , & q );
314- RSA_get0_crt_params (rsa , & dmp1 , & dmq1 , & iqmp );
315- if (n && e && d && p && q && dmp1 && dmq1 && iqmp ) {
316- if (!PEM_write_bio_RSAPrivateKey (out , rsa , ciph , NULL , 0 ,
317- ossl_pem_passwd_cb , (void * )pass )) {
318- BIO_free (out );
319- ossl_raise (eRSAError , NULL );
320- }
321- } else {
322- if (!PEM_write_bio_RSA_PUBKEY (out , rsa )) {
323- BIO_free (out );
324- ossl_raise (eRSAError , NULL );
325- }
326- }
327- str = ossl_membio2str (out );
328-
329- return str ;
310+ if (can_export_rsaprivatekey (self ))
311+ return ossl_pkey_export_traditional (argc , argv , self , 0 );
312+ else
313+ return ossl_pkey_export_spki (self , 0 );
330314}
331315
332316/*
@@ -338,30 +322,10 @@ ossl_rsa_export(int argc, VALUE *argv, VALUE self)
338322static VALUE
339323ossl_rsa_to_der (VALUE self )
340324{
341- RSA * rsa ;
342- const BIGNUM * n , * e , * d , * p , * q , * dmp1 , * dmq1 , * iqmp ;
343- int (* i2d_func )(const RSA * , unsigned char * * );
344- unsigned char * ptr ;
345- long len ;
346- VALUE str ;
347-
348- GetRSA (self , rsa );
349- RSA_get0_key (rsa , & n , & e , & d );
350- RSA_get0_factors (rsa , & p , & q );
351- RSA_get0_crt_params (rsa , & dmp1 , & dmq1 , & iqmp );
352- if (n && e && d && p && q && dmp1 && dmq1 && iqmp )
353- i2d_func = i2d_RSAPrivateKey ;
325+ if (can_export_rsaprivatekey (self ))
326+ return ossl_pkey_export_traditional (0 , NULL , self , 1 );
354327 else
355- i2d_func = (int (* )(const RSA * , unsigned char * * ))i2d_RSA_PUBKEY ;
356- if ((len = i2d_func (rsa , NULL )) <= 0 )
357- ossl_raise (eRSAError , NULL );
358- str = rb_str_new (0 , len );
359- ptr = (unsigned char * )RSTRING_PTR (str );
360- if (i2d_func (rsa , & ptr ) < 0 )
361- ossl_raise (eRSAError , NULL );
362- ossl_str_adjust (str , ptr );
363-
364- return str ;
328+ return ossl_pkey_export_spki (self , 1 );
365329}
366330
367331/*
0 commit comments