Skip to content

Commit

Permalink
crypto: fail early if passphrase is too long
Browse files Browse the repository at this point in the history
This causes OpenSSL to fail early if the decryption passphrase is too
long, and produces a somewhat helpful error message.

Refs: nodejs#25208
  • Loading branch information
tniessen committed Mar 30, 2019
1 parent 2e2c015 commit ff4ee3e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/api/crypto.md
Expand Up @@ -1821,6 +1821,9 @@ Creates and returns a new key object containing a private key. If `key` is a
string or `Buffer`, `format` is assumed to be `'pem'`; otherwise, `key`
must be an object with the properties described above.

If the private key is encrypted, a `passphrase` must be specified. The length
of the passphrase is limited.

### crypto.createPublicKey(key)
<!-- YAML
added: v11.6.0
Expand Down
3 changes: 2 additions & 1 deletion src/node_crypto.cc
Expand Up @@ -178,7 +178,8 @@ static int PasswordCallback(char* buf, int size, int rwflag, void* u) {
if (passphrase != nullptr) {
size_t buflen = static_cast<size_t>(size);
size_t len = strlen(passphrase);
len = len > buflen ? buflen : len;
if (buflen <= len)
return -1;
memcpy(buf, passphrase, len);
return len;
}
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-crypto-key-objects.js
Expand Up @@ -223,6 +223,17 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
message: 'Passphrase required for encrypted key'
});

// Reading an encrypted key with a passphrase that exceeds OpenSSL's buffer
// size limit should fail with an appropriate error code.
common.expectsError(() => createPrivateKey({
key: privateDsa,
format: 'pem',
passphrase: Buffer.alloc(16 * 1024, 'a')
}), {
code: 'ERR_OSSL_PEM_BAD_PASSWORD_READ',
type: Error
});

const publicKey = createPublicKey(publicDsa);
assert.strictEqual(publicKey.type, 'public');
assert.strictEqual(publicKey.asymmetricKeyType, 'dsa');
Expand Down

0 comments on commit ff4ee3e

Please sign in to comment.