Skip to content

feat: added read_derpub, to only attempt to load DER encoded public keys #918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions ext/openssl/ossl_pkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Loading