Skip to content

Commit 73bca57

Browse files
tniessenBridgeAR
authored andcommittedApr 4, 2019
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. PR-URL: #27010 Refs: #25208 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Sam Roberts <[email protected]>
1 parent 608878c commit 73bca57

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed
 

‎doc/api/crypto.md

+3
Original file line numberDiff line numberDiff line change
@@ -1826,6 +1826,9 @@ Creates and returns a new key object containing a private key. If `key` is a
18261826
string or `Buffer`, `format` is assumed to be `'pem'`; otherwise, `key`
18271827
must be an object with the properties described above.
18281828

1829+
If the private key is encrypted, a `passphrase` must be specified. The length
1830+
of the passphrase is limited to 1024 bytes.
1831+
18291832
### crypto.createPublicKey(key)
18301833
<!-- YAML
18311834
added: v11.6.0

‎src/node_crypto.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ static int PasswordCallback(char* buf, int size, int rwflag, void* u) {
189189
if (passphrase != nullptr) {
190190
size_t buflen = static_cast<size_t>(size);
191191
size_t len = strlen(passphrase);
192-
len = len > buflen ? buflen : len;
192+
if (buflen < len)
193+
return -1;
193194
memcpy(buf, passphrase, len);
194195
return len;
195196
}

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

+21
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,27 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
230230
message: 'Passphrase required for encrypted key'
231231
});
232232

233+
// Reading an encrypted key with a passphrase that exceeds OpenSSL's buffer
234+
// size limit should fail with an appropriate error code.
235+
common.expectsError(() => createPrivateKey({
236+
key: privateDsa,
237+
format: 'pem',
238+
passphrase: Buffer.alloc(1025, 'a')
239+
}), {
240+
code: 'ERR_OSSL_PEM_BAD_PASSWORD_READ',
241+
type: Error
242+
});
243+
244+
// The buffer has a size of 1024 bytes, so this passphrase should be permitted
245+
// (but will fail decryption).
246+
common.expectsError(() => createPrivateKey({
247+
key: privateDsa,
248+
format: 'pem',
249+
passphrase: Buffer.alloc(1024, 'a')
250+
}), {
251+
message: /bad decrypt/
252+
});
253+
233254
const publicKey = createPublicKey(publicDsa);
234255
assert.strictEqual(publicKey.type, 'public');
235256
assert.strictEqual(publicKey.asymmetricKeyType, 'dsa');

0 commit comments

Comments
 (0)
Please sign in to comment.