@@ -628,6 +628,72 @@ ossl_pkey_initialize_copy(VALUE self, VALUE other)
628
628
}
629
629
#endif
630
630
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
+
631
697
/*
632
698
* call-seq:
633
699
* pkey.oid -> string
@@ -816,6 +882,35 @@ ossl_pkey_private_to_pem(int argc, VALUE *argv, VALUE self)
816
882
return do_pkcs8_export (argc , argv , self , 0 );
817
883
}
818
884
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
+
819
914
VALUE
820
915
ossl_pkey_export_spki (VALUE self , int to_der )
821
916
{
@@ -865,6 +960,35 @@ ossl_pkey_public_to_pem(VALUE self)
865
960
return ossl_pkey_export_spki (self , 0 );
866
961
}
867
962
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
+
868
992
/*
869
993
* call-seq:
870
994
* pkey.compare?(another_pkey) -> true | false
@@ -1602,6 +1726,10 @@ Init_ossl_pkey(void)
1602
1726
rb_define_module_function (mPKey , "read" , ossl_pkey_new_from_data , -1 );
1603
1727
rb_define_module_function (mPKey , "generate_parameters" , ossl_pkey_s_generate_parameters , -1 );
1604
1728
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
1605
1733
1606
1734
rb_define_alloc_func (cPKey , ossl_pkey_alloc );
1607
1735
rb_define_method (cPKey , "initialize" , ossl_pkey_initialize , 0 );
@@ -1617,6 +1745,10 @@ Init_ossl_pkey(void)
1617
1745
rb_define_method (cPKey , "private_to_pem" , ossl_pkey_private_to_pem , -1 );
1618
1746
rb_define_method (cPKey , "public_to_der" , ossl_pkey_public_to_der , 0 );
1619
1747
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
1620
1752
rb_define_method (cPKey , "compare?" , ossl_pkey_compare , 1 );
1621
1753
1622
1754
rb_define_method (cPKey , "sign" , ossl_pkey_sign , -1 );
0 commit comments