Skip to content

Commit a896c3d

Browse files
committed
ossl_pem_passwd_cb: relax passphrase length constraint
The minimum passphrase length of 4 bytes is only a limitation of PEM_def_callback() which isn't relevant here. Commit f385012 introduced this bug.
1 parent 26f928b commit a896c3d

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

ext/openssl/ossl.c

Lines changed: 1 addition & 14 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)
@@ -178,7 +169,7 @@ ossl_pem_passwd_cb(char *buf, int max_len, int flag, void *pwd_)
178169
* bytes silently if the input is over 1024 bytes */
179170
if (RB_TYPE_P(pass, T_STRING)) {
180171
len = RSTRING_LEN(pass);
181-
if (len >= OSSL_MIN_PWD_LEN && len <= max_len) {
172+
if (len <= max_len) {
182173
memcpy(buf, RSTRING_PTR(pass), len);
183174
return (int)len;
184175
}
@@ -205,10 +196,6 @@ ossl_pem_passwd_cb(char *buf, int max_len, int flag, void *pwd_)
205196
return -1;
206197
}
207198
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-
}
212199
if (len > max_len) {
213200
rb_warning("password must not be longer than %d bytes", max_len);
214201
continue;

test/test_pkey_rsa.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,14 @@ 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+
end
252+
245253
def test_dup
246254
key = OpenSSL::PKey::RSA.generate(256, 17)
247255
key2 = key.dup

0 commit comments

Comments
 (0)