@@ -628,6 +628,72 @@ ossl_pkey_initialize_copy(VALUE self, VALUE other)
628628}
629629#endif
630630
631+ #ifdef HAVE_EVP_PKEY_NEW_RAW_PRIVATE_KEY
632+ /*
633+ * call-seq:
634+ * OpenSSL::PKey.new_raw_private_key(algo, string) -> PKey
635+ *
636+ * See the OpenSSL documentation for EVP_PKEY_new_raw_private_key()
637+ */
638+
639+ static VALUE
640+ ossl_pkey_new_raw_private_key (VALUE self , VALUE type , VALUE key )
641+ {
642+ EVP_PKEY * pkey ;
643+ const EVP_PKEY_ASN1_METHOD * ameth ;
644+ int pkey_id ;
645+ size_t keylen ;
646+
647+ StringValue (type );
648+ StringValue (key );
649+ ameth = EVP_PKEY_asn1_find_str (NULL , RSTRING_PTR (type ), RSTRING_LENINT (type ));
650+ if (!ameth )
651+ ossl_raise (ePKeyError , "algorithm %" PRIsVALUE " not found" , type );
652+ EVP_PKEY_asn1_get0_info (& pkey_id , NULL , NULL , NULL , NULL , ameth );
653+
654+ keylen = RSTRING_LEN (key );
655+
656+ pkey = EVP_PKEY_new_raw_private_key (pkey_id , NULL , (unsigned char * )RSTRING_PTR (key ), keylen );
657+ if (!pkey )
658+ ossl_raise (ePKeyError , "EVP_PKEY_new_raw_private_key" );
659+
660+ return ossl_pkey_new (pkey );
661+ }
662+ #endif
663+
664+ #ifdef HAVE_EVP_PKEY_NEW_RAW_PRIVATE_KEY
665+ /*
666+ * call-seq:
667+ * OpenSSL::PKey.new_raw_public_key(algo, string) -> PKey
668+ *
669+ * See the OpenSSL documentation for EVP_PKEY_new_raw_public_key()
670+ */
671+
672+ static VALUE
673+ ossl_pkey_new_raw_public_key (VALUE self , VALUE type , VALUE key )
674+ {
675+ EVP_PKEY * pkey ;
676+ const EVP_PKEY_ASN1_METHOD * ameth ;
677+ int pkey_id ;
678+ size_t keylen ;
679+
680+ StringValue (type );
681+ StringValue (key );
682+ ameth = EVP_PKEY_asn1_find_str (NULL , RSTRING_PTR (type ), RSTRING_LENINT (type ));
683+ if (!ameth )
684+ ossl_raise (ePKeyError , "algorithm %" PRIsVALUE " not found" , type );
685+ EVP_PKEY_asn1_get0_info (& pkey_id , NULL , NULL , NULL , NULL , ameth );
686+
687+ keylen = RSTRING_LEN (key );
688+
689+ pkey = EVP_PKEY_new_raw_public_key (pkey_id , NULL , (unsigned char * )RSTRING_PTR (key ), keylen );
690+ if (!pkey )
691+ ossl_raise (ePKeyError , "EVP_PKEY_new_raw_public_key" );
692+
693+ return ossl_pkey_new (pkey );
694+ }
695+ #endif
696+
631697/*
632698 * call-seq:
633699 * pkey.oid -> string
@@ -816,6 +882,35 @@ ossl_pkey_private_to_pem(int argc, VALUE *argv, VALUE self)
816882 return do_pkcs8_export (argc , argv , self , 0 );
817883}
818884
885+ #ifdef HAVE_EVP_PKEY_NEW_RAW_PRIVATE_KEY
886+ /*
887+ * call-seq:
888+ * pkey.raw_private_key => string
889+ *
890+ * See the OpenSSL documentation for EVP_PKEY_get_raw_private_key()
891+ */
892+
893+ static VALUE
894+ ossl_pkey_raw_private_key (VALUE self )
895+ {
896+ EVP_PKEY * pkey ;
897+ VALUE str ;
898+ size_t len ;
899+
900+ GetPKey (self , pkey );
901+ if (EVP_PKEY_get_raw_private_key (pkey , NULL , & len ) != 1 )
902+ ossl_raise (ePKeyError , "EVP_PKEY_get_raw_private_key" );
903+ str = rb_str_new (NULL , len );
904+
905+ if (EVP_PKEY_get_raw_private_key (pkey , (unsigned char * )RSTRING_PTR (str ), & len ) != 1 )
906+ ossl_raise (ePKeyError , "EVP_PKEY_get_raw_private_key" );
907+
908+ rb_str_set_len (str , len );
909+
910+ return str ;
911+ }
912+ #endif
913+
819914VALUE
820915ossl_pkey_export_spki (VALUE self , int to_der )
821916{
@@ -865,6 +960,35 @@ ossl_pkey_public_to_pem(VALUE self)
865960 return ossl_pkey_export_spki (self , 0 );
866961}
867962
963+ #ifdef HAVE_EVP_PKEY_NEW_RAW_PRIVATE_KEY
964+ /*
965+ * call-seq:
966+ * pkey.raw_public_key => string
967+ *
968+ * See the OpenSSL documentation for EVP_PKEY_get_raw_public_key()
969+ */
970+
971+ static VALUE
972+ ossl_pkey_raw_public_key (VALUE self )
973+ {
974+ EVP_PKEY * pkey ;
975+ VALUE str ;
976+ size_t len ;
977+
978+ GetPKey (self , pkey );
979+ if (EVP_PKEY_get_raw_public_key (pkey , NULL , & len ) != 1 )
980+ ossl_raise (ePKeyError , "EVP_PKEY_get_raw_public_key" );
981+ str = rb_str_new (NULL , len );
982+
983+ if (EVP_PKEY_get_raw_public_key (pkey , (unsigned char * )RSTRING_PTR (str ), & len ) != 1 )
984+ ossl_raise (ePKeyError , "EVP_PKEY_get_raw_public_key" );
985+
986+ rb_str_set_len (str , len );
987+
988+ return str ;
989+ }
990+ #endif
991+
868992/*
869993 * call-seq:
870994 * pkey.compare?(another_pkey) -> true | false
@@ -1602,6 +1726,10 @@ Init_ossl_pkey(void)
16021726 rb_define_module_function (mPKey , "read" , ossl_pkey_new_from_data , -1 );
16031727 rb_define_module_function (mPKey , "generate_parameters" , ossl_pkey_s_generate_parameters , -1 );
16041728 rb_define_module_function (mPKey , "generate_key" , ossl_pkey_s_generate_key , -1 );
1729+ #ifdef HAVE_EVP_PKEY_NEW_RAW_PRIVATE_KEY
1730+ rb_define_module_function (mPKey , "new_raw_private_key" , ossl_pkey_new_raw_private_key , 2 );
1731+ rb_define_module_function (mPKey , "new_raw_public_key" , ossl_pkey_new_raw_public_key , 2 );
1732+ #endif
16051733
16061734 rb_define_alloc_func (cPKey , ossl_pkey_alloc );
16071735 rb_define_method (cPKey , "initialize" , ossl_pkey_initialize , 0 );
@@ -1617,6 +1745,10 @@ Init_ossl_pkey(void)
16171745 rb_define_method (cPKey , "private_to_pem" , ossl_pkey_private_to_pem , -1 );
16181746 rb_define_method (cPKey , "public_to_der" , ossl_pkey_public_to_der , 0 );
16191747 rb_define_method (cPKey , "public_to_pem" , ossl_pkey_public_to_pem , 0 );
1748+ #ifdef HAVE_EVP_PKEY_NEW_RAW_PRIVATE_KEY
1749+ rb_define_method (cPKey , "raw_private_key" , ossl_pkey_raw_private_key , 0 );
1750+ rb_define_method (cPKey , "raw_public_key" , ossl_pkey_raw_public_key , 0 );
1751+ #endif
16201752 rb_define_method (cPKey , "compare?" , ossl_pkey_compare , 1 );
16211753
16221754 rb_define_method (cPKey , "sign" , ossl_pkey_sign , -1 );
0 commit comments