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 #include "mozilla/dom/SubtleCrypto.h"
9 #include "mozilla/dom/Promise.h"
10 #include "mozilla/dom/SubtleCryptoBinding.h"
11 #include "mozilla/dom/WebCryptoTask.h"
16 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(SubtleCrypto
, mWindow
)
17 NS_IMPL_CYCLE_COLLECTING_ADDREF(SubtleCrypto
)
18 NS_IMPL_CYCLE_COLLECTING_RELEASE(SubtleCrypto
)
19 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SubtleCrypto
)
20 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
21 NS_INTERFACE_MAP_ENTRY(nsISupports
)
26 SubtleCrypto::SubtleCrypto(nsPIDOMWindow
* aWindow
)
33 SubtleCrypto::WrapObject(JSContext
* aCx
)
35 return SubtleCryptoBinding::Wrap(aCx
, this);
38 #define SUBTLECRYPTO_METHOD_BODY(Operation, aRv, ...) \
39 nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(mWindow); \
41 nsRefPtr<Promise> p = Promise::Create(global, aRv); \
45 nsRefPtr<WebCryptoTask> task = WebCryptoTask::Create ## Operation ## Task(__VA_ARGS__); \
46 task->DispatchWithPromise(p); \
49 already_AddRefed
<Promise
>
50 SubtleCrypto::Encrypt(JSContext
* cx
,
51 const ObjectOrString
& algorithm
,
53 const CryptoOperationData
& data
,
56 SUBTLECRYPTO_METHOD_BODY(Encrypt
, aRv
, cx
, algorithm
, key
, data
)
59 already_AddRefed
<Promise
>
60 SubtleCrypto::Decrypt(JSContext
* cx
,
61 const ObjectOrString
& algorithm
,
63 const CryptoOperationData
& data
,
66 SUBTLECRYPTO_METHOD_BODY(Decrypt
, aRv
, cx
, algorithm
, key
, data
)
69 already_AddRefed
<Promise
>
70 SubtleCrypto::Sign(JSContext
* cx
,
71 const ObjectOrString
& algorithm
,
73 const CryptoOperationData
& data
,
76 SUBTLECRYPTO_METHOD_BODY(Sign
, aRv
, cx
, algorithm
, key
, data
)
79 already_AddRefed
<Promise
>
80 SubtleCrypto::Verify(JSContext
* cx
,
81 const ObjectOrString
& algorithm
,
83 const CryptoOperationData
& signature
,
84 const CryptoOperationData
& data
,
87 SUBTLECRYPTO_METHOD_BODY(Verify
, aRv
, cx
, algorithm
, key
, signature
, data
)
90 already_AddRefed
<Promise
>
91 SubtleCrypto::Digest(JSContext
* cx
,
92 const ObjectOrString
& algorithm
,
93 const CryptoOperationData
& data
,
96 SUBTLECRYPTO_METHOD_BODY(Digest
, aRv
, cx
, algorithm
, data
)
100 already_AddRefed
<Promise
>
101 SubtleCrypto::ImportKey(JSContext
* cx
,
102 const nsAString
& format
,
103 JS::Handle
<JSObject
*> keyData
,
104 const ObjectOrString
& algorithm
,
106 const Sequence
<nsString
>& keyUsages
,
109 SUBTLECRYPTO_METHOD_BODY(ImportKey
, aRv
, cx
, format
, keyData
, algorithm
,
110 extractable
, keyUsages
)
113 already_AddRefed
<Promise
>
114 SubtleCrypto::ExportKey(const nsAString
& format
,
118 SUBTLECRYPTO_METHOD_BODY(ExportKey
, aRv
, format
, key
)
121 already_AddRefed
<Promise
>
122 SubtleCrypto::GenerateKey(JSContext
* cx
, const ObjectOrString
& algorithm
,
123 bool extractable
, const Sequence
<nsString
>& keyUsages
,
126 SUBTLECRYPTO_METHOD_BODY(GenerateKey
, aRv
, cx
, algorithm
, extractable
, keyUsages
)
129 already_AddRefed
<Promise
>
130 SubtleCrypto::DeriveKey(JSContext
* cx
,
131 const ObjectOrString
& algorithm
,
133 const ObjectOrString
& derivedKeyType
,
134 bool extractable
, const Sequence
<nsString
>& keyUsages
,
137 SUBTLECRYPTO_METHOD_BODY(DeriveKey
, aRv
, cx
, algorithm
, baseKey
,
138 derivedKeyType
, extractable
, keyUsages
)
141 already_AddRefed
<Promise
>
142 SubtleCrypto::DeriveBits(JSContext
* cx
,
143 const ObjectOrString
& algorithm
,
148 SUBTLECRYPTO_METHOD_BODY(DeriveBits
, aRv
, cx
, algorithm
, baseKey
, length
)
151 already_AddRefed
<Promise
>
152 SubtleCrypto::WrapKey(JSContext
* cx
,
153 const nsAString
& format
,
155 CryptoKey
& wrappingKey
,
156 const ObjectOrString
& wrapAlgorithm
,
159 SUBTLECRYPTO_METHOD_BODY(WrapKey
, aRv
, cx
, format
, key
, wrappingKey
, wrapAlgorithm
)
162 already_AddRefed
<Promise
>
163 SubtleCrypto::UnwrapKey(JSContext
* cx
,
164 const nsAString
& format
,
165 const ArrayBufferViewOrArrayBuffer
& wrappedKey
,
166 CryptoKey
& unwrappingKey
,
167 const ObjectOrString
& unwrapAlgorithm
,
168 const ObjectOrString
& unwrappedKeyAlgorithm
,
170 const Sequence
<nsString
>& keyUsages
,
173 SUBTLECRYPTO_METHOD_BODY(UnwrapKey
, aRv
, cx
, format
, wrappedKey
, unwrappingKey
,
174 unwrapAlgorithm
, unwrappedKeyAlgorithm
,
175 extractable
, keyUsages
)
179 } // namespace mozilla