Skip to content

Commit f4c717b

Browse files
committed
pkey: assume generic PKeys contain private components
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.
1 parent 41587f6 commit f4c717b

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

ext/openssl/ossl_pkey.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,19 @@ GetPrivPKeyPtr(VALUE obj)
246246
{
247247
EVP_PKEY *pkey;
248248

249-
if (rb_funcallv(obj, id_private_q, 0, NULL) != Qtrue) {
250-
ossl_raise(rb_eArgError, "Private key is needed.");
251-
}
252249
GetPKey(obj, pkey);
250+
if (OSSL_PKEY_IS_PRIVATE(obj))
251+
return pkey;
252+
/*
253+
* The EVP API does not provide a way to check if the EVP_PKEY has private
254+
* components. Assuming it does...
255+
*/
256+
if (!rb_respond_to(obj, id_private_q))
257+
return pkey;
258+
if (RTEST(rb_funcallv(obj, id_private_q, 0, NULL)))
259+
return pkey;
253260

254-
return pkey;
261+
rb_raise(rb_eArgError, "private key is needed");
255262
}
256263

257264
EVP_PKEY *

0 commit comments

Comments
 (0)