Skip to content

Commit 6561274

Browse files
calvinmetcalfbnoordhuis
authored andcommitted
crypto: support passwords in publicEncrypt
Private keys may be used along with publicEncrypt since the private key includes the public one. This adds the ability to use encrypted private keys which previously threw an error. This commit also makes sure the user exposed functions have names. PR-URL: #626 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent e9eb2ec commit 6561274

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

doc/api/crypto.markdown

+3
Original file line numberDiff line numberDiff line change
@@ -678,10 +678,13 @@ Encrypts `buffer` with `public_key`. Only RSA is currently supported.
678678

679679
`public_key` can be an object or a string. If `public_key` is a string, it is
680680
treated as the key with no passphrase and will use `RSA_PKCS1_OAEP_PADDING`.
681+
Since RSA public keys may be derived from private keys you may pass a private
682+
key to this method.
681683

682684
`public_key`:
683685

684686
* `key` : A string holding the PEM encoded private key
687+
* `passphrase` : An optional string of passphrase for the private key
685688
* `padding` : An optional padding value, one of the following:
686689
* `constants.RSA_NO_PADDING`
687690
* `constants.RSA_PKCS1_PADDING`

lib/crypto.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,8 @@ function rsaPublic(method, defaultPadding) {
340340
return function(options, buffer) {
341341
var key = options.key || options;
342342
var padding = options.padding || defaultPadding;
343-
return method(toBuf(key), buffer, padding);
343+
var passphrase = options.passphrase || null;
344+
return method(toBuf(key), buffer, padding, passphrase);
344345
};
345346
}
346347

test/parallel/test-crypto.js

+41
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,28 @@ assert.equal(bad_dh.verifyError, constants.DH_NOT_SUITABLE_GENERATOR);
831831
}, encryptedBuffer);
832832
assert.equal(input, decryptedBufferWithPassword.toString());
833833

834+
encryptedBuffer = crypto.publicEncrypt({
835+
key: rsaKeyPemEncrypted,
836+
passphrase: 'password'
837+
}, bufferToEncrypt);
838+
839+
decryptedBufferWithPassword = crypto.privateDecrypt({
840+
key: rsaKeyPemEncrypted,
841+
passphrase: 'password'
842+
}, encryptedBuffer);
843+
assert.equal(input, decryptedBufferWithPassword.toString());
844+
845+
encryptedBuffer = crypto.privateEncrypt({
846+
key: rsaKeyPemEncrypted,
847+
passphrase: new Buffer('password')
848+
}, bufferToEncrypt);
849+
850+
decryptedBufferWithPassword = crypto.publicDecrypt({
851+
key: rsaKeyPemEncrypted,
852+
passphrase: new Buffer('password')
853+
}, encryptedBuffer);
854+
assert.equal(input, decryptedBufferWithPassword.toString());
855+
834856
encryptedBuffer = crypto.publicEncrypt(certPem, bufferToEncrypt);
835857

836858
decryptedBuffer = crypto.privateDecrypt(keyPem, encryptedBuffer);
@@ -850,6 +872,25 @@ assert.equal(bad_dh.verifyError, constants.DH_NOT_SUITABLE_GENERATOR);
850872
crypto.privateDecrypt({
851873
key: rsaKeyPemEncrypted,
852874
passphrase: 'wrong'
875+
}, bufferToEncrypt);
876+
});
877+
878+
assert.throws(function() {
879+
crypto.publicEncrypt({
880+
key: rsaKeyPemEncrypted,
881+
passphrase: 'wrong'
882+
}, encryptedBuffer);
883+
});
884+
885+
encryptedBuffer = crypto.privateEncrypt({
886+
key: rsaKeyPemEncrypted,
887+
passphrase: new Buffer('password')
888+
}, bufferToEncrypt);
889+
890+
assert.throws(function() {
891+
crypto.publicDecrypt({
892+
key: rsaKeyPemEncrypted,
893+
passphrase: [].concat.apply([], new Buffer('password'))
853894
}, encryptedBuffer);
854895
});
855896
})();

0 commit comments

Comments
 (0)