From 7e737e1c71a4a036cb892b851587d01e885c0e64 Mon Sep 17 00:00:00 2001 From: Michael Richardson Date: Sat, 26 Jul 2025 11:22:51 +0200 Subject: [PATCH] feat: added read_derpub, to only attempt to load DER encoded public keys --- ext/openssl/ossl_pkey.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/ext/openssl/ossl_pkey.c b/ext/openssl/ossl_pkey.c index b00a3648d..2385d2f75 100644 --- a/ext/openssl/ossl_pkey.c +++ b/ext/openssl/ossl_pkey.c @@ -498,6 +498,41 @@ ossl_pkey_s_generate_key(int argc, VALUE *argv, VALUE self) return pkey_generate(argc, argv, self, 0); } +/* + * + * call-seq: + * OpenSSL::PKey.read_derpub(string [, pwd ]) -> PKey + * OpenSSL::PKey.read_derpub(io [, pwd ]) -> PKey + * + * Reads a DER encoded string from _string_ or _io_ and returns an + * instance of the a public key object. + * + * === Parameters + * * _string+ is a DER-encoded string containing an arbitrary public key. + * * _io_ is an instance of IO containing a DER-encoded + * arbitrary public key. + */ +static VALUE +ossl_pkey_new_pub_from_data(int argc, VALUE *argv, VALUE self) +{ + EVP_PKEY *pkey; + BIO *bio; + VALUE data; + + rb_scan_args(argc, argv, "1", &data); + + bio = ossl_obj2bio(&data); + if (!(pkey = d2i_PUBKEY_bio(bio, NULL))) { + OSSL_BIO_reset(bio); + } + + BIO_free(bio); + if (!pkey) + ossl_raise(ePKeyError, "Could not parse PKey"); + + return ossl_pkey_new(pkey); +} + /* * TODO: There is no convenient way to check the presence of public key * components on OpenSSL 3.0. But since keys are immutable on 3.0, pkeys without @@ -1737,6 +1772,7 @@ Init_ossl_pkey(void) rb_define_module_function(mPKey, "generate_key", ossl_pkey_s_generate_key, -1); rb_define_module_function(mPKey, "new_raw_private_key", ossl_pkey_new_raw_private_key, 2); rb_define_module_function(mPKey, "new_raw_public_key", ossl_pkey_new_raw_public_key, 2); + rb_define_module_function(mPKey, "read_derpub", ossl_pkey_new_pub_from_data, -1); rb_define_alloc_func(cPKey, ossl_pkey_alloc); rb_define_method(cPKey, "initialize", ossl_pkey_initialize, 0);