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"
21 typedef ArrayBufferViewOrArrayBuffer CryptoOperationData
;
22 typedef ArrayBufferViewOrArrayBuffer KeyData
;
26 The execution of a WebCryptoTask happens in several phases
30 3. CalculateResult -> DoCrypto
32 5. Resolve or FailWithError
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
40 In general, the constructor should handle any parsing steps that
41 require JS context, and otherwise just cache information for later
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)) { \
61 class WebCryptoTask
: public CryptoTask
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
78 CallCallback(mEarlyRv
);
83 mEarlyRv
= Dispatch("SubtleCrypto");
84 MAYBE_EARLY_FAIL(mEarlyRv
)
88 static WebCryptoTask
* CreateEncryptDecryptTask(JSContext
* aCx
,
89 const ObjectOrString
& aAlgorithm
,
91 const CryptoOperationData
& aData
,
94 static WebCryptoTask
* CreateSignVerifyTask(JSContext
* aCx
,
95 const ObjectOrString
& aAlgorithm
,
97 const CryptoOperationData
& aSignature
,
98 const CryptoOperationData
& aData
,
102 static WebCryptoTask
* CreateEncryptTask(JSContext
* aCx
,
103 const ObjectOrString
& aAlgorithm
,
105 const CryptoOperationData
& aData
)
107 return CreateEncryptDecryptTask(aCx
, aAlgorithm
, aKey
, aData
, true);
110 static WebCryptoTask
* CreateDecryptTask(JSContext
* aCx
,
111 const ObjectOrString
& aAlgorithm
,
113 const CryptoOperationData
& aData
)
115 return CreateEncryptDecryptTask(aCx
, aAlgorithm
, aKey
, aData
, false);
118 static WebCryptoTask
* CreateSignTask(JSContext
* aCx
,
119 const ObjectOrString
& aAlgorithm
,
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
,
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
,
146 const Sequence
<nsString
>& aKeyUsages
);
147 static WebCryptoTask
* CreateExportKeyTask(const nsAString
& aFormat
,
149 static WebCryptoTask
* CreateGenerateKeyTask(JSContext
* aCx
,
150 const ObjectOrString
& aAlgorithm
,
152 const Sequence
<nsString
>& aKeyUsages
);
154 static WebCryptoTask
* CreateDeriveKeyTask(JSContext
* aCx
,
155 const ObjectOrString
& aAlgorithm
,
157 const ObjectOrString
& aDerivedKeyType
,
159 const Sequence
<nsString
>& aKeyUsages
);
160 static WebCryptoTask
* CreateDeriveBitsTask(JSContext
* aCx
,
161 const ObjectOrString
& aAlgorithm
,
165 static WebCryptoTask
* CreateWrapKeyTask(JSContext
* aCx
,
166 const nsAString
& aFormat
,
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
,
177 const Sequence
<nsString
>& aKeyUsages
);
180 nsRefPtr
<Promise
> mResultPromise
;
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
;
209 } // namespace mozilla
211 #endif // mozilla_dom_WebCryptoTask_h