DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_WebCryptoTask_h
#define mozilla_dom_WebCryptoTask_h

#include "ScopedNSSTypes.h"
#include "mozilla/dom/CryptoKey.h"
#include "mozilla/dom/DOMException.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/SubtleCryptoBinding.h"
#include "nsIGlobalObject.h"

namespace mozilla {
namespace dom {

class ThreadSafeWorkerRef;

typedef ArrayBufferViewOrArrayBuffer CryptoOperationData;
typedef ArrayBufferViewOrArrayBuffer KeyData;

/*

The execution of a WebCryptoTask happens in several phases

1. Constructor
2. BeforeCrypto
3. CalculateResult -> DoCrypto
4. AfterCrypto
5. Resolve or FailWithError
6. Cleanup

If any of these steps produces an error (setting mEarlyRv), then
subsequent steps will not proceed.  If the constructor or BeforeCrypto
sets mEarlyComplete to true, then we will skip step 3, saving the
thread overhead.

In general, the constructor should handle any parsing steps that
require JS context, and otherwise just cache information for later
steps to use.

All steps besides step 3 occur on the main thread, so they should
avoid blocking operations.

Only step 3 is guarded to ensure that NSS has not been shutdown,
so all NSS interactions should occur in DoCrypto

Cleanup should execute regardless of what else happens.

*/

#define MAYBE_EARLY_FAIL(rv) \
  if (NS_FAILED(rv)) {       \
    FailWithError(rv);       \
    return;                  \
  }

class WebCryptoTask : public CancelableRunnable {
 public:
  virtual void DispatchWithPromise(Promise* aResultPromise);

 protected:
  static WebCryptoTask* CreateEncryptDecryptTask(
      JSContext* aCx, const ObjectOrString& aAlgorithm, CryptoKey& aKey,
      const CryptoOperationData& aData, bool aEncrypt);

  static WebCryptoTask* CreateSignVerifyTask(
      JSContext* aCx, const ObjectOrString& aAlgorithm, CryptoKey& aKey,
      const CryptoOperationData& aSignature, const CryptoOperationData& aData,
      bool aSign);

 public:
  static WebCryptoTask* CreateEncryptTask(JSContext* aCx,
                                          const ObjectOrString& aAlgorithm,
                                          CryptoKey& aKey,
                                          const CryptoOperationData& aData) {
    return CreateEncryptDecryptTask(aCx, aAlgorithm, aKey, aData, true);
  }

  static WebCryptoTask* CreateDecryptTask(JSContext* aCx,
                                          const ObjectOrString& aAlgorithm,
                                          CryptoKey& aKey,
                                          const CryptoOperationData& aData) {
    return CreateEncryptDecryptTask(aCx, aAlgorithm, aKey, aData, false);
  }

  static WebCryptoTask* CreateSignTask(JSContext* aCx,
                                       const ObjectOrString& aAlgorithm,
                                       CryptoKey& aKey,
                                       const CryptoOperationData& aData) {
    CryptoOperationData dummy;
    dummy.SetAsArrayBuffer(aCx);
    return CreateSignVerifyTask(aCx, aAlgorithm, aKey, dummy, aData, true);
  }

  static WebCryptoTask* CreateVerifyTask(JSContext* aCx,
                                         const ObjectOrString& aAlgorithm,
                                         CryptoKey& aKey,
                                         const CryptoOperationData& aSignature,
                                         const CryptoOperationData& aData) {
    return CreateSignVerifyTask(aCx, aAlgorithm, aKey, aSignature, aData,
                                false);
  }

  static WebCryptoTask* CreateDigestTask(JSContext* aCx,
                                         const ObjectOrString& aAlgorithm,
                                         const CryptoOperationData& aData);

  static WebCryptoTask* CreateImportKeyTask(
      nsIGlobalObject* aGlobal, JSContext* aCx, const nsAString& aFormat,
      JS::Handle<JSObject*> aKeyData, const ObjectOrString& aAlgorithm,
      bool aExtractable, const Sequence<nsString>& aKeyUsages);
  static WebCryptoTask* CreateExportKeyTask(const nsAString& aFormat,
                                            CryptoKey& aKey);
  static WebCryptoTask* CreateGenerateKeyTask(
      nsIGlobalObject* aGlobal, JSContext* aCx,
      const ObjectOrString& aAlgorithm, bool aExtractable,
      const Sequence<nsString>& aKeyUsages);

  static WebCryptoTask* CreateDeriveKeyTask(
      nsIGlobalObject* aGlobal, JSContext* aCx,
      const ObjectOrString& aAlgorithm, CryptoKey& aBaseKey,
      const ObjectOrString& aDerivedKeyType, bool extractable,
      const Sequence<nsString>& aKeyUsages);
  static WebCryptoTask* CreateDeriveBitsTask(JSContext* aCx,
                                             const ObjectOrString& aAlgorithm,
                                             CryptoKey& aKey, uint32_t aLength);

  static WebCryptoTask* CreateWrapKeyTask(JSContext* aCx,
                                          const nsAString& aFormat,
                                          CryptoKey& aKey,
                                          CryptoKey& aWrappingKey,
                                          const ObjectOrString& aWrapAlgorithm);
  static WebCryptoTask* CreateUnwrapKeyTask(
      nsIGlobalObject* aGlobal, JSContext* aCx, const nsAString& aFormat,
      const ArrayBufferViewOrArrayBuffer& aWrappedKey,
      CryptoKey& aUnwrappingKey, const ObjectOrString& aUnwrapAlgorithm,
      const ObjectOrString& aUnwrappedKeyAlgorithm, bool aExtractable,
      const Sequence<nsString>& aKeyUsages);

 protected:
  RefPtr<Promise> mResultPromise;
  nsresult mEarlyRv;
  bool mEarlyComplete;

  WebCryptoTask();
  virtual ~WebCryptoTask();

  bool IsOnOriginalThread() {
    return !mOriginalEventTarget || mOriginalEventTarget->IsOnCurrentThread();
  }

  // For things that need to happen on the main thread
  // either before or after CalculateResult
  virtual nsresult BeforeCrypto() { return NS_OK; }
  virtual nsresult DoCrypto() { return NS_OK; }
  virtual nsresult AfterCrypto() { return NS_OK; }
  virtual void Resolve() {}
  virtual void Cleanup() {}

  void FailWithError(nsresult aRv);

  nsresult CalculateResult();

  void CallCallback(nsresult rv);

 private:
  NS_IMETHOD Run() final;
  nsresult Cancel() final;

  nsCOMPtr<nsISerialEventTarget> mOriginalEventTarget;
  RefPtr<ThreadSafeWorkerRef> mWorkerRef;
  nsresult mRv;
};

// XXX This class is declared here (unlike others) to enable reuse by WebRTC.
class GenerateAsymmetricKeyTask : public WebCryptoTask {
 public:
  GenerateAsymmetricKeyTask(nsIGlobalObject* aGlobal, JSContext* aCx,
                            const ObjectOrString& aAlgorithm, bool aExtractable,
                            const Sequence<nsString>& aKeyUsages);

 protected:
  UniquePLArenaPool mArena;
  UniquePtr<CryptoKeyPair> mKeyPair;
  nsString mAlgName;
  CK_MECHANISM_TYPE mMechanism;
  PK11RSAGenParams mRsaParams;
  SECKEYDHParams mDhParams;
  nsString mNamedCurve;

  virtual nsresult DoCrypto() override;
  virtual void Resolve() override;
  virtual void Cleanup() override;

 private:
  UniqueSECKEYPublicKey mPublicKey;
  UniqueSECKEYPrivateKey mPrivateKey;
};

}  // namespace dom
}  // namespace mozilla

#endif  // mozilla_dom_WebCryptoTask_h