Bumping gaia.json for 4 gaia revision(s) a=gaia-bump
[gecko.git] / dom / crypto / WebCryptoTask.h
blob55c531b2df230af5a710f95beef5c742455adbb3
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_WebCryptoTask_h
8 #define mozilla_dom_WebCryptoTask_h
10 #include "CryptoTask.h"
12 #include "nsIGlobalObject.h"
13 #include "mozilla/dom/Promise.h"
14 #include "mozilla/dom/DOMException.h"
15 #include "mozilla/dom/SubtleCryptoBinding.h"
16 #include "mozilla/dom/CryptoKey.h"
18 namespace mozilla {
19 namespace dom {
21 typedef ArrayBufferViewOrArrayBuffer CryptoOperationData;
22 typedef ArrayBufferViewOrArrayBuffer KeyData;
26 The execution of a WebCryptoTask happens in several phases
28 1. Constructor
29 2. BeforeCrypto
30 3. CalculateResult -> DoCrypto
31 4. AfterCrypto
32 5. Resolve or FailWithError
33 6. Cleanup
35 If any of these steps produces an error (setting mEarlyRv), then
36 subsequent steps will not proceed. If the constructor or BeforeCrypto
37 sets mEarlyComplete to true, then we will skip step 3, saving the
38 thread overhead.
40 In general, the constructor should handle any parsing steps that
41 require JS context, and otherwise just cache information for later
42 steps to use.
44 All steps besides step 3 occur on the main thread, so they should
45 avoid blocking operations.
47 Only step 3 is guarded to ensure that NSS has not been shutdown,
48 so all NSS interactions should occur in DoCrypto
50 Cleanup should execute regardless of what else happens.
54 #define MAYBE_EARLY_FAIL(rv) \
55 if (NS_FAILED(rv)) { \
56 FailWithError(rv); \
57 Skip(); \
58 return; \
61 class WebCryptoTask : public CryptoTask
63 public:
64 virtual void DispatchWithPromise(Promise* aResultPromise)
66 MOZ_ASSERT(NS_IsMainThread());
67 mResultPromise = aResultPromise;
69 // Fail if an error was set during the constructor
70 MAYBE_EARLY_FAIL(mEarlyRv)
72 // Perform pre-NSS operations, and fail if they fail
73 mEarlyRv = BeforeCrypto();
74 MAYBE_EARLY_FAIL(mEarlyRv)
76 // Skip NSS if we're already done, or launch a CryptoTask
77 if (mEarlyComplete) {
78 CallCallback(mEarlyRv);
79 Skip();
80 return;
83 mEarlyRv = Dispatch("SubtleCrypto");
84 MAYBE_EARLY_FAIL(mEarlyRv)
87 protected:
88 static WebCryptoTask* CreateEncryptDecryptTask(JSContext* aCx,
89 const ObjectOrString& aAlgorithm,
90 CryptoKey& aKey,
91 const CryptoOperationData& aData,
92 bool aEncrypt);
94 static WebCryptoTask* CreateSignVerifyTask(JSContext* aCx,
95 const ObjectOrString& aAlgorithm,
96 CryptoKey& aKey,
97 const CryptoOperationData& aSignature,
98 const CryptoOperationData& aData,
99 bool aSign);
101 public:
102 static WebCryptoTask* CreateEncryptTask(JSContext* aCx,
103 const ObjectOrString& aAlgorithm,
104 CryptoKey& aKey,
105 const CryptoOperationData& aData)
107 return CreateEncryptDecryptTask(aCx, aAlgorithm, aKey, aData, true);
110 static WebCryptoTask* CreateDecryptTask(JSContext* aCx,
111 const ObjectOrString& aAlgorithm,
112 CryptoKey& aKey,
113 const CryptoOperationData& aData)
115 return CreateEncryptDecryptTask(aCx, aAlgorithm, aKey, aData, false);
118 static WebCryptoTask* CreateSignTask(JSContext* aCx,
119 const ObjectOrString& aAlgorithm,
120 CryptoKey& aKey,
121 const CryptoOperationData& aData)
123 CryptoOperationData dummy;
124 dummy.SetAsArrayBuffer(aCx);
125 return CreateSignVerifyTask(aCx, aAlgorithm, aKey, dummy, aData, true);
128 static WebCryptoTask* CreateVerifyTask(JSContext* aCx,
129 const ObjectOrString& aAlgorithm,
130 CryptoKey& aKey,
131 const CryptoOperationData& aSignature,
132 const CryptoOperationData& aData)
134 return CreateSignVerifyTask(aCx, aAlgorithm, aKey, aSignature, aData, false);
137 static WebCryptoTask* CreateDigestTask(JSContext* aCx,
138 const ObjectOrString& aAlgorithm,
139 const CryptoOperationData& aData);
141 static WebCryptoTask* CreateImportKeyTask(JSContext* aCx,
142 const nsAString& aFormat,
143 JS::Handle<JSObject*> aKeyData,
144 const ObjectOrString& aAlgorithm,
145 bool aExtractable,
146 const Sequence<nsString>& aKeyUsages);
147 static WebCryptoTask* CreateExportKeyTask(const nsAString& aFormat,
148 CryptoKey& aKey);
149 static WebCryptoTask* CreateGenerateKeyTask(JSContext* aCx,
150 const ObjectOrString& aAlgorithm,
151 bool aExtractable,
152 const Sequence<nsString>& aKeyUsages);
154 static WebCryptoTask* CreateDeriveKeyTask(JSContext* aCx,
155 const ObjectOrString& aAlgorithm,
156 CryptoKey& aBaseKey,
157 const ObjectOrString& aDerivedKeyType,
158 bool extractable,
159 const Sequence<nsString>& aKeyUsages);
160 static WebCryptoTask* CreateDeriveBitsTask(JSContext* aCx,
161 const ObjectOrString& aAlgorithm,
162 CryptoKey& aKey,
163 uint32_t aLength);
165 static WebCryptoTask* CreateWrapKeyTask(JSContext* aCx,
166 const nsAString& aFormat,
167 CryptoKey& aKey,
168 CryptoKey& aWrappingKey,
169 const ObjectOrString& aWrapAlgorithm);
170 static WebCryptoTask* CreateUnwrapKeyTask(JSContext* aCx,
171 const nsAString& aFormat,
172 const ArrayBufferViewOrArrayBuffer& aWrappedKey,
173 CryptoKey& aUnwrappingKey,
174 const ObjectOrString& aUnwrapAlgorithm,
175 const ObjectOrString& aUnwrappedKeyAlgorithm,
176 bool aExtractable,
177 const Sequence<nsString>& aKeyUsages);
179 protected:
180 nsRefPtr<Promise> mResultPromise;
181 nsresult mEarlyRv;
182 bool mEarlyComplete;
184 WebCryptoTask()
185 : mEarlyRv(NS_OK)
186 , mEarlyComplete(false)
189 // For things that need to happen on the main thread
190 // either before or after CalculateResult
191 virtual nsresult BeforeCrypto() { return NS_OK; }
192 virtual nsresult DoCrypto() { return NS_OK; }
193 virtual nsresult AfterCrypto() { return NS_OK; }
194 virtual void Resolve() {}
195 virtual void Cleanup() {}
197 void FailWithError(nsresult aRv);
199 // Subclasses should override this method if they keep references to
200 // any NSS objects, e.g., SECKEYPrivateKey or PK11SymKey.
201 virtual void ReleaseNSSResources() MOZ_OVERRIDE {}
203 virtual nsresult CalculateResult() MOZ_OVERRIDE MOZ_FINAL;
205 virtual void CallCallback(nsresult rv) MOZ_OVERRIDE MOZ_FINAL;
208 } // namespace dom
209 } // namespace mozilla
211 #endif // mozilla_dom_WebCryptoTask_h