Bug 1037501 - Normalize WebCrypto algorithm names as they are defined in the spec r=rbarnes
authorTim Taubert <ttaubert@mozilla.com>
Fri, 25 Jul 2014 16:33:03 +0200
changeset 218684 93c52fb1e891c9bdfe24b5ff4043b529962a574c
parent 218683 e0e0ee180af340834ad9f3e1fd7b9d9f12930fcb
child 218685 47bba71f4c666a84a1301b19c154d730da6158f3
push id3979
push userraliiev@mozilla.com
push dateMon, 13 Oct 2014 16:35:44 +0000
treeherdermozilla-beta@30f2cc610691 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrbarnes
bugs1037501
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 1037501 - Normalize WebCrypto algorithm names as they are defined in the spec r=rbarnes
dom/crypto/WebCryptoTask.cpp
dom/crypto/test/tests.js
--- a/dom/crypto/WebCryptoTask.cpp
+++ b/dom/crypto/WebCryptoTask.cpp
@@ -127,16 +127,45 @@ GetAlgorithmName(JSContext* aCx, const O
 
     if (!alg.Init(aCx, value) || !alg.mName.WasPassed()) {
       return NS_ERROR_DOM_SYNTAX_ERR;
     }
 
     aName.Assign(alg.mName.Value());
   }
 
+  // Normalize algorithm names.
+  if (aName.EqualsIgnoreCase(WEBCRYPTO_ALG_AES_CBC)) {
+    aName.AssignLiteral(WEBCRYPTO_ALG_AES_CBC);
+  } else if (aName.EqualsIgnoreCase(WEBCRYPTO_ALG_AES_CTR)) {
+    aName.AssignLiteral(WEBCRYPTO_ALG_AES_CTR);
+  } else if (aName.EqualsIgnoreCase(WEBCRYPTO_ALG_AES_GCM)) {
+    aName.AssignLiteral(WEBCRYPTO_ALG_AES_GCM);
+  } else if (aName.EqualsIgnoreCase(WEBCRYPTO_ALG_AES_KW)) {
+    aName.AssignLiteral(WEBCRYPTO_ALG_AES_KW);
+  } else if (aName.EqualsIgnoreCase(WEBCRYPTO_ALG_SHA1)) {
+    aName.AssignLiteral(WEBCRYPTO_ALG_SHA1);
+  } else if (aName.EqualsIgnoreCase(WEBCRYPTO_ALG_SHA256)) {
+    aName.AssignLiteral(WEBCRYPTO_ALG_SHA256);
+  } else if (aName.EqualsIgnoreCase(WEBCRYPTO_ALG_SHA384)) {
+    aName.AssignLiteral(WEBCRYPTO_ALG_SHA384);
+  } else if (aName.EqualsIgnoreCase(WEBCRYPTO_ALG_SHA512)) {
+    aName.AssignLiteral(WEBCRYPTO_ALG_SHA512);
+  } else if (aName.EqualsIgnoreCase(WEBCRYPTO_ALG_HMAC)) {
+    aName.AssignLiteral(WEBCRYPTO_ALG_HMAC);
+  } else if (aName.EqualsIgnoreCase(WEBCRYPTO_ALG_PBKDF2)) {
+    aName.AssignLiteral(WEBCRYPTO_ALG_PBKDF2);
+  } else if (aName.EqualsIgnoreCase(WEBCRYPTO_ALG_RSAES_PKCS1)) {
+    aName.AssignLiteral(WEBCRYPTO_ALG_RSAES_PKCS1);
+  } else if (aName.EqualsIgnoreCase(WEBCRYPTO_ALG_RSASSA_PKCS1)) {
+    aName.AssignLiteral(WEBCRYPTO_ALG_RSASSA_PKCS1);
+  } else if (aName.EqualsIgnoreCase(WEBCRYPTO_ALG_RSA_OAEP)) {
+    aName.AssignLiteral(WEBCRYPTO_ALG_RSA_OAEP);
+  }
+
   return NS_OK;
 }
 
 template<class T, class OOS>
 static nsresult
 Coerce(JSContext* aCx, T& aTarget, const OOS& aAlgorithm)
 {
   ClearException ce(aCx);
@@ -1794,26 +1823,20 @@ public:
       RootedDictionary<HmacKeyGenParams> params(aCx);
       mEarlyRv = Coerce(aCx, params, aAlgorithm);
       if (NS_FAILED(mEarlyRv) || !params.mHash.WasPassed()) {
         mEarlyRv = NS_ERROR_DOM_SYNTAX_ERR;
         return;
       }
 
       nsString hashName;
-      if (params.mHash.Value().IsString()) {
-        hashName.Assign(params.mHash.Value().GetAsString());
-      } else {
-        Algorithm hashAlg;
-        mEarlyRv = Coerce(aCx, hashAlg, params.mHash.Value());
-        if (NS_FAILED(mEarlyRv) || !hashAlg.mName.WasPassed()) {
-          mEarlyRv = NS_ERROR_DOM_SYNTAX_ERR;
-          return;
-        }
-        hashName.Assign(hashAlg.mName.Value());
+      mEarlyRv = GetAlgorithmName(aCx, params.mHash.Value(), hashName);
+      if (NS_FAILED(mEarlyRv)) {
+        mEarlyRv = NS_ERROR_DOM_SYNTAX_ERR;
+        return;
       }
 
       if (params.mLength.WasPassed()) {
         mLength = params.mLength.Value();
       } else {
         mLength = MapHashAlgorithmNameToBlockSize(hashName);
       }
 
--- a/dom/crypto/test/tests.js
+++ b/dom/crypto/test/tests.js
@@ -1827,8 +1827,43 @@ TestArray.addTest(
     var that = this;
     // TODO Change the algorithm to "DH" once we support it.
     var alg = {name: "RSA-OAEP", hash: "SHA-1"};
 
     crypto.subtle.importKey("pkcs8", tv.broken_pkcs8.dh, alg, false, ["decrypt"])
       .then(error(that), complete(that));
   }
 );
+
+// -----------------------------------------------------------------------------
+TestArray.addTest(
+  "Test that we properly normalize algorithm names",
+  function() {
+    var that = this;
+    var alg = { name: "hmac", hash: {name: "sHa-256"} };
+
+    function doGenerateAesKey() {
+      var alg = { name: "AES-gcm", length: 192 };
+      return crypto.subtle.generateKey(alg, false, ["encrypt"]);
+    }
+
+    function doGenerateRsaOaepKey() {
+      var alg = {
+        name: "rsa-OAEP",
+        hash: "sha-1",
+        modulusLength: 2048,
+        publicExponent: new Uint8Array([0x01, 0x00, 0x01])
+      };
+      return crypto.subtle.generateKey(alg, false, ["encrypt"]);
+    }
+
+    function doGenerateRsaSsaPkcs1Key() {
+      var alg = { name: "RSASSA-pkcs1-V1_5", hash: "SHA-1" };
+      return crypto.subtle.importKey("pkcs8", tv.pkcs8, alg, true, ["sign"]);
+    }
+
+    crypto.subtle.generateKey(alg, false, ["sign"])
+      .then(doGenerateAesKey)
+      .then(doGenerateRsaOaepKey)
+      .then(doGenerateRsaSsaPkcs1Key)
+      .then(complete(that), error(that));
+  }
+);