aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2017-07-21 15:35:46 +0900
committerKazuki Yamaguchi <k@rhe.jp>2017-07-22 21:58:56 +0900
commit96211a3e4ed8242832b74f166d6435144438bd43 (patch)
tree702ea51e49c0dc9dc753e0a7cadffd631f9d60af
parent2a5ae3c7a53978145122a163e63a490a6a6c9993 (diff)
downloadruby-openssl-ky/pem-passwd-cb-get-rid-of-minlen.tar.gz
ossl_pem_passwd_cb: handle nil from the block explicitlyky/pem-passwd-cb-get-rid-of-minlen
There is code that returns nil in the passphrase block on purpose (to prevent OpenSSL from prompting on stdin): OpenSSL::PKey.read(File.read("file.pem")) { nil } This is working just by chance because the TypeError from StringValue() is silently ignored. Let's short circuit in that case and save raising a needless exception, as this pattern has become too common.
-rw-r--r--ext/openssl/ossl.c9
-rw-r--r--test/test_pkey_rsa.rb3
2 files changed, 8 insertions, 4 deletions
diff --git a/ext/openssl/ossl.c b/ext/openssl/ossl.c
index 562241c5..c22966df 100644
--- a/ext/openssl/ossl.c
+++ b/ext/openssl/ossl.c
@@ -148,11 +148,10 @@ ossl_pem_passwd_value(VALUE pass)
static VALUE
ossl_pem_passwd_cb0(VALUE flag)
{
- VALUE pass;
-
- pass = rb_yield(flag);
+ VALUE pass = rb_yield(flag);
+ if (NIL_P(pass))
+ return Qnil;
StringValue(pass);
-
return pass;
}
@@ -195,6 +194,8 @@ ossl_pem_passwd_cb(char *buf, int max_len, int flag, void *pwd_)
rb_set_errinfo(Qnil);
return -1;
}
+ if (NIL_P(pass))
+ return -1;
len = RSTRING_LEN(pass);
if (len > max_len) {
rb_warning("password must not be longer than %d bytes", max_len);
diff --git a/test/test_pkey_rsa.rb b/test/test_pkey_rsa.rb
index 381e7603..93760f74 100644
--- a/test/test_pkey_rsa.rb
+++ b/test/test_pkey_rsa.rb
@@ -248,6 +248,9 @@ class OpenSSL::TestPKeyRSA < OpenSSL::PKeyTestCase
assert_match (/ENCRYPTED/), pem3c
assert_equal key.to_der, OpenSSL::PKey.read(pem3c, "key").to_der
assert_equal key.to_der, OpenSSL::PKey.read(pem3c) { "key" }.to_der
+ assert_raise(OpenSSL::PKey::PKeyError) {
+ OpenSSL::PKey.read(pem3c) { nil }
+ }
end
def test_dup