Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: implement privateEncrypt/publicDecrypt #625

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/api/crypto.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,14 @@ treated as the key with no passphrase and will use `RSA_PKCS1_OAEP_PADDING`.

NOTE: All paddings are defined in `constants` module.

## crypto.privateEncrypt(private_key, buffer)

See above for details. Has the same API as `crypto.privateDecrypt`.

## crypto.publicDecrypt(public_key, buffer)

See above for details. Has the same API as `crypto.publicEncrypt`.

## crypto.DEFAULT_ENCODING

The default encoding to use for functions that can take either strings
Expand Down
34 changes: 23 additions & 11 deletions lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,19 +337,31 @@ Verify.prototype.verify = function(object, signature, sigEncoding) {
return this._handle.verify(toBuf(object), toBuf(signature, sigEncoding));
};

exports.publicEncrypt = function(options, buffer) {
var key = options.key || options;
var padding = options.padding || constants.RSA_PKCS1_OAEP_PADDING;
return binding.publicEncrypt(toBuf(key), buffer, padding);
};
function rsaPublic(method, defaultPadding) {
return function(options, buffer) {
var key = options.key || options;
var padding = options.padding || defaultPadding;
return method(toBuf(key), buffer, padding);
};
}

exports.privateDecrypt = function(options, buffer) {
var key = options.key || options;
var passphrase = options.passphrase || null;
var padding = options.padding || constants.RSA_PKCS1_OAEP_PADDING;
return binding.privateDecrypt(toBuf(key), buffer, padding, passphrase);
};
function rsaPrivate(method, defaultPadding) {
return function(options, buffer) {
var key = options.key || options;
var passphrase = options.passphrase || null;
var padding = options.padding || defaultPadding;
return method(toBuf(key), buffer, padding, passphrase);
};
}

exports.publicEncrypt = rsaPublic(binding.publicEncrypt,
constants.RSA_PKCS1_OAEP_PADDING);
exports.publicDecrypt = rsaPublic(binding.publicDecrypt,
constants.RSA_PKCS1_PADDING);
exports.privateEncrypt = rsaPrivate(binding.privateEncrypt,
constants.RSA_PKCS1_PADDING);
exports.privateDecrypt = rsaPrivate(binding.privateDecrypt,
constants.RSA_PKCS1_OAEP_PADDING);


exports.createDiffieHellman = exports.DiffieHellman = DiffieHellman;
Expand Down
24 changes: 19 additions & 5 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3561,12 +3561,12 @@ bool PublicKeyCipher::Cipher(const char* key_pem,

// Check if this is a PKCS#8 or RSA public key before trying as X.509 and
// private key.
if (operation == kEncrypt &&
if (operation == kPublic &&
strncmp(key_pem, PUBLIC_KEY_PFX, PUBLIC_KEY_PFX_LEN) == 0) {
pkey = PEM_read_bio_PUBKEY(bp, nullptr, nullptr, nullptr);
if (pkey == nullptr)
goto exit;
} else if (operation == kEncrypt &&
} else if (operation == kPublic &&
strncmp(key_pem, PUBRSA_KEY_PFX, PUBRSA_KEY_PFX_LEN) == 0) {
RSA* rsa = PEM_read_bio_RSAPublicKey(bp, nullptr, nullptr, nullptr);
if (rsa) {
Expand All @@ -3577,7 +3577,7 @@ bool PublicKeyCipher::Cipher(const char* key_pem,
}
if (pkey == nullptr)
goto exit;
} else if (operation == kEncrypt &&
} else if (operation == kPublic &&
strncmp(key_pem, CERTIFICATE_PFX, CERTIFICATE_PFX_LEN) == 0) {
x509 = PEM_read_bio_X509(bp, nullptr, CryptoPemCallback, nullptr);
if (x509 == nullptr)
Expand All @@ -3603,6 +3603,12 @@ bool PublicKeyCipher::Cipher(const char* key_pem,
if (EVP_PKEY_CTX_set_rsa_padding(ctx, padding) <= 0)
goto exit;

/* Reset md for signatures, we are doing raw RSA ops anyway */
if (EVP_PKEY_CTX_get_operation(ctx) == EVP_PKEY_OP_SIGN) {
if (EVP_PKEY_CTX_set_signature_md(ctx, nullptr) <= 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I admit I don't understand why it's necessary to clear the signature hash function. Can you explain? Preferably with a longer, in-code comment? :-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops forgot to remove it, it is no longer needed. :) This piece was failing because PSS padding mode set sha1 md and rsa_pmeth.c was ensuring that the input length matches the md output length. However, PSS does not really work with all these shit, so I replaced it with PKCS1, and PKCS1 does not set md.

goto exit;
}

if (EVP_PKEY_cipher(ctx, nullptr, out_len, data, len) <= 0)
goto exit;

Expand Down Expand Up @@ -5038,13 +5044,21 @@ void InitCrypto(Handle<Object> target,
env->SetMethod(target, "getCiphers", GetCiphers);
env->SetMethod(target, "getHashes", GetHashes);
env->SetMethod(target, "publicEncrypt",
PublicKeyCipher::Cipher<PublicKeyCipher::kEncrypt,
PublicKeyCipher::Cipher<PublicKeyCipher::kPublic,
EVP_PKEY_encrypt_init,
EVP_PKEY_encrypt>);
env->SetMethod(target, "privateDecrypt",
PublicKeyCipher::Cipher<PublicKeyCipher::kDecrypt,
PublicKeyCipher::Cipher<PublicKeyCipher::kPrivate,
EVP_PKEY_decrypt_init,
EVP_PKEY_decrypt>);
env->SetMethod(target, "privateEncrypt",
PublicKeyCipher::Cipher<PublicKeyCipher::kPrivate,
EVP_PKEY_sign_init,
EVP_PKEY_sign>);
env->SetMethod(target, "publicDecrypt",
PublicKeyCipher::Cipher<PublicKeyCipher::kPublic,
EVP_PKEY_verify_recover_init,
EVP_PKEY_verify_recover>);
}

} // namespace crypto
Expand Down
4 changes: 2 additions & 2 deletions src/node_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,8 @@ class PublicKeyCipher {
const unsigned char *in, size_t inlen);

enum Operation {
kEncrypt,
kDecrypt
kPublic,
kPrivate
};

template <Operation operation,
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,11 @@ assert.equal(bad_dh.verifyError, constants.DH_NOT_SUITABLE_GENERATOR);
decryptedBuffer = crypto.privateDecrypt(keyPem, encryptedBuffer);
assert.equal(input, decryptedBuffer.toString());

encryptedBuffer = crypto.privateEncrypt(keyPem, bufferToEncrypt);

decryptedBuffer = crypto.publicDecrypt(keyPem, encryptedBuffer);
assert.equal(input, decryptedBuffer.toString());

assert.throws(function() {
crypto.privateDecrypt({
key: rsaKeyPemEncrypted,
Expand Down