From e7329e411441169f675a0f3850093f72356ba3d1 Mon Sep 17 00:00:00 2001 From: Tim Taubert Date: Fri, 8 Aug 2014 16:45:08 +0200 Subject: [PATCH] Bug 1050785 - RSA-OAEP encrypt/decrypt should accept strings as AlgorithmIdentifiers r=rbarnes --- dom/crypto/WebCryptoTask.cpp | 20 ++++++++++++-------- dom/crypto/test/tests.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/dom/crypto/WebCryptoTask.cpp b/dom/crypto/WebCryptoTask.cpp index 57e21676235..193ae329a35 100644 --- a/dom/crypto/WebCryptoTask.cpp +++ b/dom/crypto/WebCryptoTask.cpp @@ -863,15 +863,19 @@ public: mStrength = PK11_GetPrivateModulusLen(mPrivKey); } - RootedDictionary params(aCx); - mEarlyRv = Coerce(aCx, params, aAlgorithm); - if (NS_FAILED(mEarlyRv)) { - mEarlyRv = NS_ERROR_DOM_SYNTAX_ERR; - return; - } + // The algorithm could just be given as a string + // in which case there would be no label specified. + if (!aAlgorithm.IsString()) { + RootedDictionary params(aCx); + mEarlyRv = Coerce(aCx, params, aAlgorithm); + if (NS_FAILED(mEarlyRv)) { + mEarlyRv = NS_ERROR_DOM_SYNTAX_ERR; + return; + } - if (params.mLabel.WasPassed() && !params.mLabel.Value().IsNull()) { - ATTEMPT_BUFFER_INIT(mLabel, params.mLabel.Value().Value()); + if (params.mLabel.WasPassed() && !params.mLabel.Value().IsNull()) { + ATTEMPT_BUFFER_INIT(mLabel, params.mLabel.Value().Value()); + } } // Otherwise mLabel remains the empty octet string, as intended diff --git a/dom/crypto/test/tests.js b/dom/crypto/test/tests.js index 6681df8a550..008101c1fb0 100644 --- a/dom/crypto/test/tests.js +++ b/dom/crypto/test/tests.js @@ -1323,6 +1323,35 @@ TestArray.addTest( } ); +// ----------------------------------------------------------------------------- +TestArray.addTest( + "Test that RSA-OAEP encrypt/decrypt accepts strings as AlgorithmIdentifiers", + function () { + var that = this; + var alg = { + name: "RSA-OAEP", + hash: "SHA-256", + modulusLength: 2048, + publicExponent: new Uint8Array([0x01, 0x00, 0x01]) + }; + + var privKey, pubKey, data = crypto.getRandomValues(new Uint8Array(128)); + function setKey(x) { pubKey = x.publicKey; privKey = x.privateKey; } + function doEncrypt() { + return crypto.subtle.encrypt("RSA-OAEP", pubKey, data); + } + function doDecrypt(x) { + return crypto.subtle.decrypt("RSA-OAEP", privKey, x); + } + + crypto.subtle.generateKey(alg, false, ["encrypt", "decrypt"]) + .then(setKey) + .then(doEncrypt) + .then(doDecrypt) + .then(memcmp_complete(that, data), error(that)); + } +); + // ----------------------------------------------------------------------------- TestArray.addTest( "Key wrap known answer, using AES-GCM",