Skip to content

Commit

Permalink
pkey: assume generic PKeys contain private components
Browse files Browse the repository at this point in the history
The EVP interface cannot tell whether if a pkey contains the private
components or not. Assume it does if it does not respond to #private?.
This fixes the NoMethodError on calling #sign on a generic PKey.
  • Loading branch information
rhenium committed May 13, 2020
1 parent 41587f6 commit f4c717b
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions ext/openssl/ossl_pkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,19 @@ GetPrivPKeyPtr(VALUE obj)
{
EVP_PKEY *pkey;

if (rb_funcallv(obj, id_private_q, 0, NULL) != Qtrue) {
ossl_raise(rb_eArgError, "Private key is needed.");
}
GetPKey(obj, pkey);
if (OSSL_PKEY_IS_PRIVATE(obj))
return pkey;
/*
* The EVP API does not provide a way to check if the EVP_PKEY has private
* components. Assuming it does...
*/
if (!rb_respond_to(obj, id_private_q))
return pkey;
if (RTEST(rb_funcallv(obj, id_private_q, 0, NULL)))
return pkey;

return pkey;
rb_raise(rb_eArgError, "private key is needed");
}

EVP_PKEY *
Expand Down

0 comments on commit f4c717b

Please sign in to comment.