Skip to content

Commit 2862335

Browse files
committed
added read_derpub, to only attempt to load DER encoded public keys
1 parent 84b51cd commit 2862335

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

ext/openssl/ossl_pkey.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,40 @@ ossl_pkey_new_from_data(int argc, VALUE *argv, VALUE self)
185185
return ossl_pkey_new(pkey);
186186
}
187187

188+
/*
189+
* call-seq:
190+
* OpenSSL::PKey.read_derpub(string [, pwd ]) -> PKey
191+
* OpenSSL::PKey.read_derpub(io [, pwd ]) -> PKey
192+
*
193+
* Reads a DER encoded string from _string_ or _io_ and returns an
194+
* instance of the a public key object.
195+
*
196+
* === Parameters
197+
* * _string+ is a DER-encoded string containing an arbitrary public key.
198+
* * _io_ is an instance of IO containing a DER-encoded
199+
* arbitrary public key.
200+
*/
201+
static VALUE
202+
ossl_pkey_new_pub_from_data(int argc, VALUE *argv, VALUE self)
203+
{
204+
EVP_PKEY *pkey;
205+
BIO *bio;
206+
VALUE data;
207+
208+
rb_scan_args(argc, argv, "1", &data);
209+
210+
bio = ossl_obj2bio(&data);
211+
if (!(pkey = d2i_PUBKEY_bio(bio, NULL))) {
212+
OSSL_BIO_reset(bio);
213+
}
214+
215+
BIO_free(bio);
216+
if (!pkey)
217+
ossl_raise(ePKeyError, "Could not parse PKey");
218+
219+
return ossl_pkey_new(pkey);
220+
}
221+
188222
void
189223
ossl_pkey_check_public_key(const EVP_PKEY *pkey)
190224
{
@@ -488,6 +522,7 @@ Init_ossl_pkey(void)
488522
cPKey = rb_define_class_under(mPKey, "PKey", rb_cObject);
489523

490524
rb_define_module_function(mPKey, "read", ossl_pkey_new_from_data, -1);
525+
rb_define_module_function(mPKey, "read_derpub", ossl_pkey_new_pub_from_data, -1);
491526

492527
rb_define_alloc_func(cPKey, ossl_pkey_alloc);
493528
rb_define_method(cPKey, "initialize", ossl_pkey_initialize, 0);

0 commit comments

Comments
 (0)