@@ -532,6 +532,56 @@ ossl_pkey_initialize(VALUE self)
532
532
return self ;
533
533
}
534
534
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
+
535
585
/*
536
586
* call-seq:
537
587
* pkey.oid -> string
@@ -702,6 +752,30 @@ ossl_pkey_private_to_pem(int argc, VALUE *argv, VALUE self)
702
752
return do_pkcs8_export (argc , argv , self , 0 );
703
753
}
704
754
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
+
705
779
VALUE
706
780
ossl_pkey_export_spki (VALUE self , int to_der )
707
781
{
@@ -770,6 +844,30 @@ ossl_pkey_public_to_pem(VALUE self)
770
844
return ossl_pkey_export_spki (self , 0 );
771
845
}
772
846
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
+
773
871
/*
774
872
* call-seq:
775
873
* pkey.sign(digest, data) -> String
@@ -1060,6 +1158,8 @@ Init_ossl_pkey(void)
1060
1158
rb_define_module_function (mPKey , "read" , ossl_pkey_new_from_data , -1 );
1061
1159
rb_define_module_function (mPKey , "generate_parameters" , ossl_pkey_s_generate_parameters , -1 );
1062
1160
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 );
1063
1163
1064
1164
rb_define_alloc_func (cPKey , ossl_pkey_alloc );
1065
1165
rb_define_method (cPKey , "initialize" , ossl_pkey_initialize , 0 );
@@ -1068,9 +1168,11 @@ Init_ossl_pkey(void)
1068
1168
rb_define_method (cPKey , "private?" , ossl_pkey_is_private , 0 );
1069
1169
rb_define_method (cPKey , "private_to_der" , ossl_pkey_private_to_der , -1 );
1070
1170
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 );
1071
1172
rb_define_method (cPKey , "public?" , ossl_pkey_is_public , 0 );
1072
1173
rb_define_method (cPKey , "public_to_der" , ossl_pkey_public_to_der , 0 );
1073
1174
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 );
1074
1176
1075
1177
rb_define_method (cPKey , "sign" , ossl_pkey_sign , 2 );
1076
1178
rb_define_method (cPKey , "verify" , ossl_pkey_verify , 3 );
0 commit comments