1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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"
13 namespace mozilla::dom
{
15 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(SubtleCrypto
, mParent
)
16 NS_IMPL_CYCLE_COLLECTING_ADDREF(SubtleCrypto
)
17 NS_IMPL_CYCLE_COLLECTING_RELEASE(SubtleCrypto
)
18 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SubtleCrypto
)
19 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
20 NS_INTERFACE_MAP_ENTRY(nsISupports
)
23 SubtleCrypto::SubtleCrypto(nsIGlobalObject
* aParent
) : mParent(aParent
) {}
25 JSObject
* SubtleCrypto::WrapObject(JSContext
* aCx
,
26 JS::Handle
<JSObject
*> aGivenProto
) {
27 return SubtleCrypto_Binding::Wrap(aCx
, this, aGivenProto
);
30 #define SUBTLECRYPTO_METHOD_BODY(Operation, aRv, ...) \
31 MOZ_ASSERT(mParent); \
32 RefPtr<Promise> p = Promise::Create(mParent, aRv); \
36 RefPtr<WebCryptoTask> task = \
37 WebCryptoTask::Create##Operation##Task(__VA_ARGS__); \
39 aRv.Throw(NS_ERROR_NULL_POINTER); \
42 task->DispatchWithPromise(p); \
45 already_AddRefed
<Promise
> SubtleCrypto::Encrypt(JSContext
* cx
,
46 const ObjectOrString
& algorithm
,
48 const CryptoOperationData
& data
,
50 SUBTLECRYPTO_METHOD_BODY(Encrypt
, aRv
, cx
, algorithm
, key
, data
)}
52 already_AddRefed
<Promise
> SubtleCrypto::Decrypt(JSContext
* cx
,
53 const ObjectOrString
& algorithm
,
55 const CryptoOperationData
& data
,
57 SUBTLECRYPTO_METHOD_BODY(Decrypt
, aRv
, cx
, algorithm
, key
, data
)}
59 already_AddRefed
<Promise
> SubtleCrypto::Sign(JSContext
* cx
,
60 const ObjectOrString
& algorithm
,
62 const CryptoOperationData
& data
,
64 SUBTLECRYPTO_METHOD_BODY(Sign
, aRv
, cx
, algorithm
, key
, data
)}
66 already_AddRefed
<Promise
> SubtleCrypto::Verify(
67 JSContext
* cx
, const ObjectOrString
& algorithm
, CryptoKey
& key
,
68 const CryptoOperationData
& signature
, const CryptoOperationData
& data
,
70 SUBTLECRYPTO_METHOD_BODY(Verify
, aRv
, cx
, algorithm
, key
, signature
, data
)}
72 already_AddRefed
<Promise
> SubtleCrypto::Digest(JSContext
* cx
,
73 const ObjectOrString
& algorithm
,
74 const CryptoOperationData
& data
,
76 SUBTLECRYPTO_METHOD_BODY(Digest
, aRv
, cx
, algorithm
, data
)}
78 already_AddRefed
<Promise
> SubtleCrypto::ImportKey(
79 JSContext
* cx
, const nsAString
& format
, JS::Handle
<JSObject
*> keyData
,
80 const ObjectOrString
& algorithm
, bool extractable
,
81 const Sequence
<nsString
>& keyUsages
, ErrorResult
& aRv
){
82 SUBTLECRYPTO_METHOD_BODY(ImportKey
, aRv
, mParent
, cx
, format
, keyData
,
83 algorithm
, extractable
, keyUsages
)}
85 already_AddRefed
<Promise
> SubtleCrypto::ExportKey(const nsAString
& format
,
88 SUBTLECRYPTO_METHOD_BODY(ExportKey
, aRv
, format
, key
)}
90 already_AddRefed
<Promise
> SubtleCrypto::GenerateKey(
91 JSContext
* cx
, const ObjectOrString
& algorithm
, bool extractable
,
92 const Sequence
<nsString
>& keyUsages
, ErrorResult
& aRv
){
93 SUBTLECRYPTO_METHOD_BODY(GenerateKey
, aRv
, mParent
, cx
, algorithm
,
94 extractable
, keyUsages
)}
96 already_AddRefed
<Promise
> SubtleCrypto::DeriveKey(
97 JSContext
* cx
, const ObjectOrString
& algorithm
, CryptoKey
& baseKey
,
98 const ObjectOrString
& derivedKeyType
, bool extractable
,
99 const Sequence
<nsString
>& keyUsages
, ErrorResult
& aRv
){
100 SUBTLECRYPTO_METHOD_BODY(DeriveKey
, aRv
, mParent
, cx
, algorithm
, baseKey
,
101 derivedKeyType
, extractable
, keyUsages
)}
103 already_AddRefed
<Promise
> SubtleCrypto::DeriveBits(
104 JSContext
* cx
, const ObjectOrString
& algorithm
, CryptoKey
& baseKey
,
105 uint32_t length
, ErrorResult
& aRv
){
106 SUBTLECRYPTO_METHOD_BODY(DeriveBits
, aRv
, cx
, algorithm
, baseKey
, length
)}
108 already_AddRefed
<Promise
> SubtleCrypto::WrapKey(
109 JSContext
* cx
, const nsAString
& format
, CryptoKey
& key
,
110 CryptoKey
& wrappingKey
, const ObjectOrString
& wrapAlgorithm
,
111 ErrorResult
& aRv
){SUBTLECRYPTO_METHOD_BODY(WrapKey
, aRv
, cx
, format
, key
,
112 wrappingKey
, wrapAlgorithm
)}
114 already_AddRefed
<Promise
> SubtleCrypto::UnwrapKey(
115 JSContext
* cx
, const nsAString
& format
,
116 const ArrayBufferViewOrArrayBuffer
& wrappedKey
, CryptoKey
& unwrappingKey
,
117 const ObjectOrString
& unwrapAlgorithm
,
118 const ObjectOrString
& unwrappedKeyAlgorithm
, bool extractable
,
119 const Sequence
<nsString
>& keyUsages
, ErrorResult
& aRv
) {
120 SUBTLECRYPTO_METHOD_BODY(UnwrapKey
, aRv
, mParent
, cx
, format
, wrappedKey
,
121 unwrappingKey
, unwrapAlgorithm
,
122 unwrappedKeyAlgorithm
, extractable
, keyUsages
)
125 } // namespace mozilla::dom