diff --git a/src/ursaNative.cc b/src/ursaNative.cc index 003b604..16fab4a 100644 --- a/src/ursaNative.cc +++ b/src/ursaNative.cc @@ -410,7 +410,11 @@ Handle RsaWrap::GetPublicKeyPem(const Arguments& args) { return bioToBuffer(bio); } -// FIXME: Need documentation. +/** + * Perform decryption on the given buffer using the RSA key, which + * must be a private key. This always uses the padding mode + * RSA_PKCS1_OAEP_PADDING. + */ Handle RsaWrap::PrivateDecrypt(const Arguments& args) { HandleScope scope; @@ -421,8 +425,26 @@ Handle RsaWrap::PrivateDecrypt(const Arguments& args) { void *data = getArg0DataAndLength(args, &length); if (data == NULL) { return Undefined(); } - // FIXME: Need real implementation. - return scope.Close(String::New("world")); + int rsaLength = RSA_size(obj->rsa); + unsigned char buf[rsaLength]; + + int bufLength = RSA_private_decrypt(length, (unsigned char *) data, + buf, obj->rsa, RSA_PKCS1_OAEP_PADDING); + + if (bufLength < 0) { + scheduleSslException(); + return Undefined(); + } + + node::Buffer *result = node::Buffer::New(bufLength); + + if (result == NULL) { + scheduleAllocException(); + return Undefined(); + } + + memcpy(node::Buffer::Data(result), buf, bufLength); + return result->handle_; } // FIXME: Need documentation. diff --git a/test/fixture.js b/test/fixture.js index 87ad7ef..5f463c1 100644 --- a/test/fixture.js +++ b/test/fixture.js @@ -41,7 +41,15 @@ var MODULUS_HEX = "cfa70934d1c7b9e2e5a3c1897fb10f803af2998495db24511f2b2162f1fd8475"; var PLAINTEXT = "Muffins are tasty."; -var PRIVATE_CIPHERTEXT_HEX = "1234"; // FIXME +var PRIVATE_CIPHERTEXT_HEX = + "98a96084dc8dfad2c4e604dc20def71acbf784b8b34ecafeb2840e238ac8031c" + + "7559004fa8337d20889b8a582af4f7d3707ab41d0a81487f0d80fb82be49537c" + + "2b9cd8dbb3b772fe0306ff9b4b99faa7cc26d5c04b1e8e79505bac1e8f2cdad2" + + "d3d8680eee3c16db8742b61935fca9679070d278f988ce4d414ab49a544c9088" + + "17a0d340a41384f4b8d826e41031ddcd3f72c29dec2fee0355a8203ea0d381a1" + + "a0f0969804d4968fb2e6220db5cf02e2c2200ff9d0a5a5037ac859a55c005ecc" + + "52ce194a6a9624c71547c96cf90d911caa4097f9cdfded71d23c9f8f5551188c" + + "8326357d54224ab25b9f29c1efdbc960a0968e4c9027cd507ffadd8dff93256c"; var PUBLIC_CIPHERTEXT_HEX = "1234"; // FIXME /* diff --git a/test/native.js b/test/native.js index 6977476..4d2f024 100644 --- a/test/native.js +++ b/test/native.js @@ -216,28 +216,28 @@ function test_fail_privateDecrypt() { function f2() { rsa.privateDecrypt("x"); } - assert.throws(f2, /arg mumble FIXME/); + assert.throws(f2, /Expected a Buffer in args\[0]\./); function f3() { rsa.privateDecrypt(new Buffer("x")); } - assert.throws(f3, /mumble FIXME/); + assert.throws(f3, /decoding error/); } function test_publicEncrypt() { // No other reasonable way to test this than to do a round trip. var plainBuf = new Buffer(fixture.PLAINTEXT, fixture.UTF8); + var priv = new RsaWrap(); + priv.setPrivateKeyPem(fixture.PRIVATE_KEY); var rsa = new RsaWrap(); rsa.setPublicKeyPem(fixture.PUBLIC_KEY); var encoded = rsa.publicEncrypt(plainBuf); - var decoded = rsa.privateDecrypt(encoded).toString(fixture.UTF8); + var decoded = priv.privateDecrypt(encoded).toString(fixture.UTF8); assert.equal(decoded, fixture.PLAINTEXT); - rsa = new RsaWrap(); - rsa.setPrivateKeyPem(fixture.PRIVATE_KEY); - encoded = rsa.publicEncrypt(plainBuf); - decoded = rsa.privateDecrypt(encoded).toString(fixture.UTF8); + encoded = priv.publicEncrypt(plainBuf); + decoded = priv.privateDecrypt(encoded).toString(fixture.UTF8); assert.equal(decoded, fixture.PLAINTEXT); } @@ -286,6 +286,8 @@ function test() { test_getPublicKeyPem(); test_fail_getPublicKeyPem(); + test_publicEncrypt(); // remove! + test_privateDecrypt(); test_fail_privateDecrypt(); test_publicEncrypt();