Skip to content

Commit ff4ee3e

Browse files
committed
crypto: fail early if passphrase is too long
This causes OpenSSL to fail early if the decryption passphrase is too long, and produces a somewhat helpful error message. Refs: nodejs#25208
1 parent 2e2c015 commit ff4ee3e

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

doc/api/crypto.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,6 +1821,9 @@ Creates and returns a new key object containing a private key. If `key` is a
18211821
string or `Buffer`, `format` is assumed to be `'pem'`; otherwise, `key`
18221822
must be an object with the properties described above.
18231823

1824+
If the private key is encrypted, a `passphrase` must be specified. The length
1825+
of the passphrase is limited.
1826+
18241827
### crypto.createPublicKey(key)
18251828
<!-- YAML
18261829
added: v11.6.0

src/node_crypto.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ static int PasswordCallback(char* buf, int size, int rwflag, void* u) {
178178
if (passphrase != nullptr) {
179179
size_t buflen = static_cast<size_t>(size);
180180
size_t len = strlen(passphrase);
181-
len = len > buflen ? buflen : len;
181+
if (buflen <= len)
182+
return -1;
182183
memcpy(buf, passphrase, len);
183184
return len;
184185
}

test/parallel/test-crypto-key-objects.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,17 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
223223
message: 'Passphrase required for encrypted key'
224224
});
225225

226+
// Reading an encrypted key with a passphrase that exceeds OpenSSL's buffer
227+
// size limit should fail with an appropriate error code.
228+
common.expectsError(() => createPrivateKey({
229+
key: privateDsa,
230+
format: 'pem',
231+
passphrase: Buffer.alloc(16 * 1024, 'a')
232+
}), {
233+
code: 'ERR_OSSL_PEM_BAD_PASSWORD_READ',
234+
type: Error
235+
});
236+
226237
const publicKey = createPublicKey(publicDsa);
227238
assert.strictEqual(publicKey.type, 'public');
228239
assert.strictEqual(publicKey.asymmetricKeyType, 'dsa');

0 commit comments

Comments
 (0)