Verified Commit fca37cac authored by Tom Schuster's avatar Tom Schuster Committed by ma1
Browse files

Bug 2024443 - Make CryptoKey::GetAlgorithm more idiomatic. r=nkulatova

parent 7cf0eb67
Loading
Loading
Loading
Loading
+13 −12
Original line number Original line Diff line number Diff line
@@ -186,38 +186,39 @@ void CryptoKey::GetType(nsString& aRetVal) const {


bool CryptoKey::Extractable() const { return (mAttributes & EXTRACTABLE); }
bool CryptoKey::Extractable() const { return (mAttributes & EXTRACTABLE); }


void CryptoKey::GetAlgorithm(JSContext* cx,
void CryptoKey::GetAlgorithm(JSContext* aCx,
                             JS::MutableHandle<JSObject*> aRetVal,
                             JS::MutableHandle<JSObject*> aRetVal,
                             ErrorResult& aRv) const {
                             ErrorResult& aRv) const {
  bool converted = false;
  bool converted = false;
  JS::Rooted<JS::Value> val(cx);
  JS::Rooted<JS::Value> val(aCx);
  switch (mAlgorithm.mType) {
  switch (mAlgorithm.mType) {
    case KeyAlgorithmProxy::AES:
    case KeyAlgorithmProxy::AES:
      converted = ToJSValue(cx, mAlgorithm.mAes, &val);
      converted = ToJSValue(aCx, mAlgorithm.mAes, &val);
      break;
      break;
    case KeyAlgorithmProxy::KDF:
    case KeyAlgorithmProxy::KDF:
      converted = ToJSValue(cx, mAlgorithm.mKDF, &val);
      converted = ToJSValue(aCx, mAlgorithm.mKDF, &val);
      break;
      break;
    case KeyAlgorithmProxy::HMAC:
    case KeyAlgorithmProxy::HMAC:
      converted = ToJSValue(cx, mAlgorithm.mHmac, &val);
      converted = ToJSValue(aCx, mAlgorithm.mHmac, &val);
      break;
      break;
    case KeyAlgorithmProxy::RSA: {
    case KeyAlgorithmProxy::RSA: {
      RootedDictionary<RsaHashedKeyAlgorithm> rsa(cx);
      RootedDictionary<RsaHashedKeyAlgorithm> rsa(aCx);
      converted = mAlgorithm.mRsa.ToKeyAlgorithm(cx, rsa, aRv);
      mAlgorithm.mRsa.ToKeyAlgorithm(aCx, rsa, aRv);
      if (converted) {
      if (aRv.Failed()) {
        converted = ToJSValue(cx, rsa, &val);
        return;
      }
      }
      converted = ToJSValue(aCx, rsa, &val);
      break;
      break;
    }
    }
    case KeyAlgorithmProxy::EC:
    case KeyAlgorithmProxy::EC:
      converted = ToJSValue(cx, mAlgorithm.mEc, &val);
      converted = ToJSValue(aCx, mAlgorithm.mEc, &val);
      break;
      break;
    case KeyAlgorithmProxy::OKP:
    case KeyAlgorithmProxy::OKP:
      converted = ToJSValue(cx, mAlgorithm.mEd, &val);
      converted = ToJSValue(aCx, mAlgorithm.mEd, &val);
      break;
      break;
  }
  }
  if (!converted) {
  if (!converted) {
    aRv.Throw(NS_ERROR_DOM_OPERATION_ERR);
    aRv.NoteJSContextException(aCx);
    return;
    return;
  }
  }


+2 −4
Original line number Original line Diff line number Diff line
@@ -35,20 +35,18 @@ struct RsaHashedKeyAlgorithmStorage {
  uint16_t mModulusLength;
  uint16_t mModulusLength;
  CryptoBuffer mPublicExponent;
  CryptoBuffer mPublicExponent;


  bool ToKeyAlgorithm(JSContext* aCx, RsaHashedKeyAlgorithm& aRsa,
  void ToKeyAlgorithm(JSContext* aCx, RsaHashedKeyAlgorithm& aRsa,
                      ErrorResult& aError) const {
                      ErrorResult& aError) const {
    JS::Rooted<JSObject*> exponent(aCx,
    JS::Rooted<JSObject*> exponent(aCx,
                                   mPublicExponent.ToUint8Array(aCx, aError));
                                   mPublicExponent.ToUint8Array(aCx, aError));
    if (aError.Failed()) {
    if (aError.Failed()) {
      return false;
      return;
    }
    }


    aRsa.mName = mName;
    aRsa.mName = mName;
    aRsa.mModulusLength = mModulusLength;
    aRsa.mModulusLength = mModulusLength;
    aRsa.mHash.mName = mHash.mName;
    aRsa.mHash.mName = mHash.mName;
    aRsa.mPublicExponent.Init(exponent);
    aRsa.mPublicExponent.Init(exponent);

    return true;
  }
  }
};
};