Skip to content

Commit d2a7938

Browse files
committed
Merge branch 'ky/pem-passwd-cb-get-rid-of-minlen' into maint
* ky/pem-passwd-cb-get-rid-of-minlen: ossl_pem_passwd_cb: handle nil from the block explicitly ossl_pem_passwd_cb: do not check for taintedness ossl_pem_passwd_cb: relax passphrase length constraint
2 parents 26f928b + 96211a3 commit d2a7938

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

ext/openssl/ossl.c

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,6 @@ ossl_bin2hex(unsigned char *in, char *out, size_t inlen)
129129
/*
130130
* our default PEM callback
131131
*/
132-
133-
/*
134-
* OpenSSL requires passwords for PEM-encoded files to be at least four
135-
* characters long. See crypto/pem/pem_lib.c (as of 1.0.2h)
136-
*/
137-
#define OSSL_MIN_PWD_LEN 4
138-
139132
VALUE
140133
ossl_pem_passwd_value(VALUE pass)
141134
{
@@ -144,8 +137,6 @@ ossl_pem_passwd_value(VALUE pass)
144137

145138
StringValue(pass);
146139

147-
if (RSTRING_LEN(pass) < OSSL_MIN_PWD_LEN)
148-
ossl_raise(eOSSLError, "password must be at least %d bytes", OSSL_MIN_PWD_LEN);
149140
/* PEM_BUFSIZE is currently used as the second argument of pem_password_cb,
150141
* that is +max_len+ of ossl_pem_passwd_cb() */
151142
if (RSTRING_LEN(pass) > PEM_BUFSIZE)
@@ -157,11 +148,10 @@ ossl_pem_passwd_value(VALUE pass)
157148
static VALUE
158149
ossl_pem_passwd_cb0(VALUE flag)
159150
{
160-
VALUE pass;
161-
162-
pass = rb_yield(flag);
163-
SafeStringValue(pass);
164-
151+
VALUE pass = rb_yield(flag);
152+
if (NIL_P(pass))
153+
return Qnil;
154+
StringValue(pass);
165155
return pass;
166156
}
167157

@@ -178,7 +168,7 @@ ossl_pem_passwd_cb(char *buf, int max_len, int flag, void *pwd_)
178168
* bytes silently if the input is over 1024 bytes */
179169
if (RB_TYPE_P(pass, T_STRING)) {
180170
len = RSTRING_LEN(pass);
181-
if (len >= OSSL_MIN_PWD_LEN && len <= max_len) {
171+
if (len <= max_len) {
182172
memcpy(buf, RSTRING_PTR(pass), len);
183173
return (int)len;
184174
}
@@ -204,11 +194,9 @@ ossl_pem_passwd_cb(char *buf, int max_len, int flag, void *pwd_)
204194
rb_set_errinfo(Qnil);
205195
return -1;
206196
}
197+
if (NIL_P(pass))
198+
return -1;
207199
len = RSTRING_LEN(pass);
208-
if (len < OSSL_MIN_PWD_LEN) {
209-
rb_warning("password must be at least %d bytes", OSSL_MIN_PWD_LEN);
210-
continue;
211-
}
212200
if (len > max_len) {
213201
rb_warning("password must not be longer than %d bytes", max_len);
214202
continue;

test/test_pkey_rsa.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,17 @@ def test_PUBKEY
242242
assert_equal pem, dup_public(RSA1024).export
243243
end
244244

245+
def test_pem_passwd
246+
key = RSA1024
247+
pem3c = key.to_pem("aes-128-cbc", "key")
248+
assert_match (/ENCRYPTED/), pem3c
249+
assert_equal key.to_der, OpenSSL::PKey.read(pem3c, "key").to_der
250+
assert_equal key.to_der, OpenSSL::PKey.read(pem3c) { "key" }.to_der
251+
assert_raise(OpenSSL::PKey::PKeyError) {
252+
OpenSSL::PKey.read(pem3c) { nil }
253+
}
254+
end
255+
245256
def test_dup
246257
key = OpenSSL::PKey::RSA.generate(256, 17)
247258
key2 = key.dup

0 commit comments

Comments
 (0)