Bug 1050785 - RSA-OAEP encrypt/decrypt should accept strings as AlgorithmIdentifiers r=rbarnes
authorTim Taubert <ttaubert@mozilla.com>
Fri, 08 Aug 2014 16:45:08 +0200
changeset 198779 8f65aea3ba06d0eab56775a269a634cec26b1262
parent 198778 cff7ed0ced431e4223625170fcb482d3aa326a31
child 198780 4496d5be532716419d505eaf3aee4b9da7407351
push id27286
push usernigelbabu@gmail.com
push dateMon, 11 Aug 2014 06:26:45 +0000
treeherdermozilla-central@8c4a1b3a2a8b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrbarnes
bugs1050785
milestone34.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1050785 - RSA-OAEP encrypt/decrypt should accept strings as AlgorithmIdentifiers r=rbarnes
dom/crypto/WebCryptoTask.cpp
dom/crypto/test/tests.js
--- a/dom/crypto/WebCryptoTask.cpp
+++ b/dom/crypto/WebCryptoTask.cpp
@@ -858,25 +858,29 @@ public:
     } else {
       if (!mPrivKey) {
         mEarlyRv = NS_ERROR_DOM_INVALID_ACCESS_ERR;
         return;
       }
       mStrength = PK11_GetPrivateModulusLen(mPrivKey);
     }
 
-    RootedDictionary<RsaOaepParams> 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());
+    // The algorithm could just be given as a string
+    // in which case there would be no label specified.
+    if (!aAlgorithm.IsString()) {
+      RootedDictionary<RsaOaepParams> 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());
+      }
     }
     // Otherwise mLabel remains the empty octet string, as intended
 
     // Look up the MGF based on the KeyAlgorithm.
     // static_cast is safe because we only get here if the algorithm name
     // is RSA-OAEP, and that only happens if we've constructed
     // an RsaHashedKeyAlgorithm.
     // TODO: Add As* methods to KeyAlgorithm (Bug 1036734)
--- a/dom/crypto/test/tests.js
+++ b/dom/crypto/test/tests.js
@@ -1320,16 +1320,45 @@ TestArray.addTest(
 
     crypto.subtle.importKey("pkcs8", tv.rsaoaep.pkcs8, alg, false, ['decrypt'])
       .then(error(that), complete(that));
   }
 );
 
 // -----------------------------------------------------------------------------
 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",
   function () {
     var that = this;
     var alg = {
       name: "AES-GCM",
       iv: tv.key_wrap_known_answer.wrapping_iv,
       tagLength: 128
     };