Bug 1289330 - Check for success when calling ToKeyAlgorithm() r=bz
authorTim Taubert <ttaubert@mozilla.com>
Thu, 28 Jul 2016 10:21:23 +0200
changeset 332086 2b89d0c341aa54b599136c208d772583ea08ccd4
parent 332085 0e6a8f2f83bc78796124d8711af413a0120d8fda
child 332087 b12ffedac68509b923e9df35ef392a84bda99a73
push id9858
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 14:37:10 +0000
treeherdermozilla-aurora@203106ef6cb6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs1289330
milestone50.0a1
Bug 1289330 - Check for success when calling ToKeyAlgorithm() r=bz
dom/crypto/CryptoKey.cpp
dom/crypto/KeyAlgorithmProxy.h
--- a/dom/crypto/CryptoKey.cpp
+++ b/dom/crypto/CryptoKey.cpp
@@ -199,27 +199,31 @@ CryptoKey::GetAlgorithm(JSContext* cx, J
     case KeyAlgorithmProxy::AES:
       converted = ToJSValue(cx, mAlgorithm.mAes, &val);
       break;
     case KeyAlgorithmProxy::HMAC:
       converted = ToJSValue(cx, mAlgorithm.mHmac, &val);
       break;
     case KeyAlgorithmProxy::RSA: {
       RootedDictionary<RsaHashedKeyAlgorithm> rsa(cx);
-      mAlgorithm.mRsa.ToKeyAlgorithm(cx, rsa);
-      converted = ToJSValue(cx, rsa, &val);
+      converted = mAlgorithm.mRsa.ToKeyAlgorithm(cx, rsa);
+      if (converted) {
+        converted = ToJSValue(cx, rsa, &val);
+      }
       break;
     }
     case KeyAlgorithmProxy::EC:
       converted = ToJSValue(cx, mAlgorithm.mEc, &val);
       break;
     case KeyAlgorithmProxy::DH: {
       RootedDictionary<DhKeyAlgorithm> dh(cx);
-      mAlgorithm.mDh.ToKeyAlgorithm(cx, dh);
-      converted = ToJSValue(cx, dh, &val);
+      converted = mAlgorithm.mDh.ToKeyAlgorithm(cx, dh);
+      if (converted) {
+        converted = ToJSValue(cx, dh, &val);
+      }
       break;
     }
   }
   if (!converted) {
     aRv.Throw(NS_ERROR_DOM_OPERATION_ERR);
     return;
   }
 
--- a/dom/crypto/KeyAlgorithmProxy.h
+++ b/dom/crypto/KeyAlgorithmProxy.h
@@ -20,42 +20,61 @@ namespace dom {
 // A heap-safe variant of RsaHashedKeyAlgorithm
 // The only difference is that it uses CryptoBuffer instead of Uint8Array
 struct RsaHashedKeyAlgorithmStorage {
   nsString mName;
   KeyAlgorithm mHash;
   uint16_t mModulusLength;
   CryptoBuffer mPublicExponent;
 
-  void
+  bool
   ToKeyAlgorithm(JSContext* aCx, RsaHashedKeyAlgorithm& aRsa) const
   {
+    JS::Rooted<JSObject*> exponent(aCx, mPublicExponent.ToUint8Array(aCx));
+    if (!exponent) {
+      return false;
+    }
+
     aRsa.mName = mName;
     aRsa.mModulusLength = mModulusLength;
     aRsa.mHash.mName = mHash.mName;
-    aRsa.mPublicExponent.Init(mPublicExponent.ToUint8Array(aCx));
+    aRsa.mPublicExponent.Init(exponent);
     aRsa.mPublicExponent.ComputeLengthAndData();
+
+    return true;
   }
 };
 
 // A heap-safe variant of DhKeyAlgorithm
 // The only difference is that it uses CryptoBuffers instead of Uint8Arrays
 struct DhKeyAlgorithmStorage {
   nsString mName;
   CryptoBuffer mPrime;
   CryptoBuffer mGenerator;
 
-  void
+  bool
   ToKeyAlgorithm(JSContext* aCx, DhKeyAlgorithm& aDh) const
   {
+    JS::Rooted<JSObject*> prime(aCx, mPrime.ToUint8Array(aCx));
+    if (!prime) {
+      return false;
+    }
+
+    JS::Rooted<JSObject*> generator(aCx, mGenerator.ToUint8Array(aCx));
+    if (!generator) {
+      return false;
+    }
+
     aDh.mName = mName;
-    aDh.mPrime.Init(mPrime.ToUint8Array(aCx));
+    aDh.mPrime.Init(prime);
     aDh.mPrime.ComputeLengthAndData();
-    aDh.mGenerator.Init(mGenerator.ToUint8Array(aCx));
+    aDh.mGenerator.Init(generator);
     aDh.mGenerator.ComputeLengthAndData();
+
+    return true;
   }
 };
 
 // This class encapuslates a KeyAlgorithm object, and adds several
 // methods that make WebCrypto operations simpler.
 struct KeyAlgorithmProxy
 {
   enum KeyAlgorithmType {