@@ -532,6 +532,56 @@ ossl_pkey_initialize(VALUE self)
532532 return self ;
533533}
534534
535+ /*
536+ * call-seq:
537+ * OpenSSL::PKey.private_new(algo, string) -> PKey
538+ *
539+ * See the OpenSSL documentation for EVP_PKEY_new_raw_private_key()
540+ */
541+
542+ static VALUE
543+ ossl_pkey_initialize_private (VALUE self , VALUE type , VALUE key )
544+ {
545+ EVP_PKEY * pkey ;
546+ int nid ;
547+ size_t keylen ;
548+
549+ nid = OBJ_sn2nid (StringValueCStr (type ));
550+ if (!nid ) ossl_raise (ePKeyError , "unknown OID `%" PRIsVALUE "'" , type );
551+
552+ keylen = RSTRING_LEN (key );
553+ pkey = EVP_PKEY_new_raw_private_key (nid , NULL , (unsigned char * )RSTRING_PTR (key ), keylen );
554+ if (!pkey )
555+ ossl_raise (ePKeyError , "Could not parse PKey" );
556+
557+ return ossl_pkey_new (pkey );
558+ }
559+
560+ /*
561+ * call-seq:
562+ * OpenSSL::PKey.public_new(algo, string) -> PKey
563+ *
564+ * See the OpenSSL documentation for EVP_PKEY_new_raw_public_key()
565+ */
566+
567+ static VALUE
568+ ossl_pkey_initialize_public (VALUE self , VALUE type , VALUE key )
569+ {
570+ EVP_PKEY * pkey ;
571+ int nid ;
572+ size_t keylen ;
573+
574+ nid = OBJ_sn2nid (StringValueCStr (type ));
575+ if (!nid ) ossl_raise (ePKeyError , "unknown OID `%" PRIsVALUE "'" , type );
576+
577+ keylen = RSTRING_LEN (key );
578+ pkey = EVP_PKEY_new_raw_public_key (nid , NULL , (unsigned char * )RSTRING_PTR (key ), keylen );
579+ if (!pkey )
580+ ossl_raise (ePKeyError , "Could not parse PKey" );
581+
582+ return ossl_pkey_new (pkey );
583+ }
584+
535585/*
536586 * call-seq:
537587 * pkey.oid -> string
@@ -702,6 +752,30 @@ ossl_pkey_private_to_pem(int argc, VALUE *argv, VALUE self)
702752 return do_pkcs8_export (argc , argv , self , 0 );
703753}
704754
755+ /*
756+ * call-seq:
757+ * key.private_to_raw => string
758+ *
759+ * See the OpenSSL documentation for EVP_PKEY_get_raw_private_key()
760+ */
761+ static VALUE ossl_pkey_private_to_raw (VALUE self )
762+ {
763+ EVP_PKEY * pkey ;
764+ VALUE str ;
765+ size_t len ;
766+
767+ GetPKey (self , pkey );
768+ EVP_PKEY_get_raw_private_key (pkey , NULL , & len );
769+ str = rb_str_new (NULL , len );
770+
771+ if (EVP_PKEY_get_raw_private_key (pkey , (unsigned char * )RSTRING_PTR (str ), & len ) != 1 )
772+ ossl_raise (ePKeyError , "EVP_PKEY_get_raw_private_key" );
773+
774+ rb_str_set_len (str , len );
775+
776+ return str ;
777+ }
778+
705779VALUE
706780ossl_pkey_export_spki (VALUE self , int to_der )
707781{
@@ -770,6 +844,30 @@ ossl_pkey_public_to_pem(VALUE self)
770844 return ossl_pkey_export_spki (self , 0 );
771845}
772846
847+ /*
848+ * call-seq:
849+ * key.public_to_raw => string
850+ *
851+ * See the OpenSSL documentation for EVP_PKEY_get_raw_public_key()
852+ */
853+ static VALUE ossl_pkey_public_to_raw (VALUE self )
854+ {
855+ EVP_PKEY * pkey ;
856+ VALUE str ;
857+ size_t len ;
858+
859+ GetPKey (self , pkey );
860+ EVP_PKEY_get_raw_public_key (pkey , NULL , & len );
861+ str = rb_str_new (NULL , len );
862+
863+ if (EVP_PKEY_get_raw_public_key (pkey , (unsigned char * )RSTRING_PTR (str ), & len ) != 1 )
864+ ossl_raise (ePKeyError , "EVP_PKEY_get_raw_public_key" );
865+
866+ rb_str_set_len (str , len );
867+
868+ return str ;
869+ }
870+
773871/*
774872 * call-seq:
775873 * pkey.sign(digest, data) -> String
@@ -1060,6 +1158,8 @@ Init_ossl_pkey(void)
10601158 rb_define_module_function (mPKey , "read" , ossl_pkey_new_from_data , -1 );
10611159 rb_define_module_function (mPKey , "generate_parameters" , ossl_pkey_s_generate_parameters , -1 );
10621160 rb_define_module_function (mPKey , "generate_key" , ossl_pkey_s_generate_key , -1 );
1161+ rb_define_module_function (mPKey , "private_new" , ossl_pkey_initialize_private , 2 );
1162+ rb_define_module_function (mPKey , "public_new" , ossl_pkey_initialize_public , 2 );
10631163
10641164 rb_define_alloc_func (cPKey , ossl_pkey_alloc );
10651165 rb_define_method (cPKey , "initialize" , ossl_pkey_initialize , 0 );
@@ -1068,9 +1168,11 @@ Init_ossl_pkey(void)
10681168 rb_define_method (cPKey , "private?" , ossl_pkey_is_private , 0 );
10691169 rb_define_method (cPKey , "private_to_der" , ossl_pkey_private_to_der , -1 );
10701170 rb_define_method (cPKey , "private_to_pem" , ossl_pkey_private_to_pem , -1 );
1171+ rb_define_method (cPKey , "private_to_raw" , ossl_pkey_private_to_raw , 0 );
10711172 rb_define_method (cPKey , "public?" , ossl_pkey_is_public , 0 );
10721173 rb_define_method (cPKey , "public_to_der" , ossl_pkey_public_to_der , 0 );
10731174 rb_define_method (cPKey , "public_to_pem" , ossl_pkey_public_to_pem , 0 );
1175+ rb_define_method (cPKey , "public_to_raw" , ossl_pkey_public_to_raw , 0 );
10741176
10751177 rb_define_method (cPKey , "sign" , ossl_pkey_sign , 2 );
10761178 rb_define_method (cPKey , "verify" , ossl_pkey_verify , 3 );
0 commit comments