/src/mozilla-central/dom/base/SubtleCrypto.cpp
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | |
7 | | #include "mozilla/dom/SubtleCrypto.h" |
8 | | |
9 | | #include "mozilla/dom/Promise.h" |
10 | | #include "mozilla/dom/SubtleCryptoBinding.h" |
11 | | #include "mozilla/dom/WebCryptoTask.h" |
12 | | |
13 | | namespace mozilla { |
14 | | namespace dom { |
15 | | |
16 | | NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(SubtleCrypto, mParent) |
17 | | NS_IMPL_CYCLE_COLLECTING_ADDREF(SubtleCrypto) |
18 | | NS_IMPL_CYCLE_COLLECTING_RELEASE(SubtleCrypto) |
19 | 0 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SubtleCrypto) |
20 | 0 | NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY |
21 | 0 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
22 | 0 | NS_INTERFACE_MAP_END |
23 | | |
24 | | |
25 | | |
26 | | SubtleCrypto::SubtleCrypto(nsIGlobalObject* aParent) |
27 | | : mParent(aParent) |
28 | 0 | { |
29 | 0 | } |
30 | | |
31 | | JSObject* |
32 | | SubtleCrypto::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) |
33 | 0 | { |
34 | 0 | return SubtleCrypto_Binding::Wrap(aCx, this, aGivenProto); |
35 | 0 | } |
36 | | |
37 | | #define SUBTLECRYPTO_METHOD_BODY(Operation, aRv, ...) \ |
38 | 0 | MOZ_ASSERT(mParent); \ |
39 | 0 | RefPtr<Promise> p = Promise::Create(mParent, aRv); \ |
40 | 0 | if (aRv.Failed()) { \ |
41 | 0 | return nullptr; \ |
42 | 0 | } \ |
43 | 0 | RefPtr<WebCryptoTask> task = \ |
44 | 0 | WebCryptoTask::Create ## Operation ## Task(__VA_ARGS__); \ |
45 | 0 | if (!task) { \ |
46 | 0 | aRv.Throw(NS_ERROR_NULL_POINTER); \ |
47 | 0 | return nullptr; \ |
48 | 0 | } \ |
49 | 0 | task->DispatchWithPromise(p); \ |
50 | 0 | return p.forget(); |
51 | | |
52 | | already_AddRefed<Promise> |
53 | | SubtleCrypto::Encrypt(JSContext* cx, |
54 | | const ObjectOrString& algorithm, |
55 | | CryptoKey& key, |
56 | | const CryptoOperationData& data, |
57 | | ErrorResult& aRv) |
58 | 0 | { |
59 | 0 | SUBTLECRYPTO_METHOD_BODY(Encrypt, aRv, cx, algorithm, key, data) |
60 | 0 | } |
61 | | |
62 | | already_AddRefed<Promise> |
63 | | SubtleCrypto::Decrypt(JSContext* cx, |
64 | | const ObjectOrString& algorithm, |
65 | | CryptoKey& key, |
66 | | const CryptoOperationData& data, |
67 | | ErrorResult& aRv) |
68 | 0 | { |
69 | 0 | SUBTLECRYPTO_METHOD_BODY(Decrypt, aRv, cx, algorithm, key, data) |
70 | 0 | } |
71 | | |
72 | | already_AddRefed<Promise> |
73 | | SubtleCrypto::Sign(JSContext* cx, |
74 | | const ObjectOrString& algorithm, |
75 | | CryptoKey& key, |
76 | | const CryptoOperationData& data, |
77 | | ErrorResult& aRv) |
78 | 0 | { |
79 | 0 | SUBTLECRYPTO_METHOD_BODY(Sign, aRv, cx, algorithm, key, data) |
80 | 0 | } |
81 | | |
82 | | already_AddRefed<Promise> |
83 | | SubtleCrypto::Verify(JSContext* cx, |
84 | | const ObjectOrString& algorithm, |
85 | | CryptoKey& key, |
86 | | const CryptoOperationData& signature, |
87 | | const CryptoOperationData& data, |
88 | | ErrorResult& aRv) |
89 | 0 | { |
90 | 0 | SUBTLECRYPTO_METHOD_BODY(Verify, aRv, cx, algorithm, key, signature, data) |
91 | 0 | } |
92 | | |
93 | | already_AddRefed<Promise> |
94 | | SubtleCrypto::Digest(JSContext* cx, |
95 | | const ObjectOrString& algorithm, |
96 | | const CryptoOperationData& data, |
97 | | ErrorResult& aRv) |
98 | 0 | { |
99 | 0 | SUBTLECRYPTO_METHOD_BODY(Digest, aRv, cx, algorithm, data) |
100 | 0 | } |
101 | | |
102 | | |
103 | | already_AddRefed<Promise> |
104 | | SubtleCrypto::ImportKey(JSContext* cx, |
105 | | const nsAString& format, |
106 | | JS::Handle<JSObject*> keyData, |
107 | | const ObjectOrString& algorithm, |
108 | | bool extractable, |
109 | | const Sequence<nsString>& keyUsages, |
110 | | ErrorResult& aRv) |
111 | 0 | { |
112 | 0 | SUBTLECRYPTO_METHOD_BODY(ImportKey, aRv, mParent, cx, format, keyData, |
113 | 0 | algorithm, extractable, keyUsages) |
114 | 0 | } |
115 | | |
116 | | already_AddRefed<Promise> |
117 | | SubtleCrypto::ExportKey(const nsAString& format, |
118 | | CryptoKey& key, |
119 | | ErrorResult& aRv) |
120 | 0 | { |
121 | 0 | SUBTLECRYPTO_METHOD_BODY(ExportKey, aRv, format, key) |
122 | 0 | } |
123 | | |
124 | | already_AddRefed<Promise> |
125 | | SubtleCrypto::GenerateKey(JSContext* cx, const ObjectOrString& algorithm, |
126 | | bool extractable, const Sequence<nsString>& keyUsages, |
127 | | ErrorResult& aRv) |
128 | 0 | { |
129 | 0 | SUBTLECRYPTO_METHOD_BODY(GenerateKey, aRv, mParent, cx, algorithm, |
130 | 0 | extractable, keyUsages) |
131 | 0 | } |
132 | | |
133 | | already_AddRefed<Promise> |
134 | | SubtleCrypto::DeriveKey(JSContext* cx, |
135 | | const ObjectOrString& algorithm, |
136 | | CryptoKey& baseKey, |
137 | | const ObjectOrString& derivedKeyType, |
138 | | bool extractable, const Sequence<nsString>& keyUsages, |
139 | | ErrorResult& aRv) |
140 | 0 | { |
141 | 0 | SUBTLECRYPTO_METHOD_BODY(DeriveKey, aRv, mParent, cx, algorithm, baseKey, |
142 | 0 | derivedKeyType, extractable, keyUsages) |
143 | 0 | } |
144 | | |
145 | | already_AddRefed<Promise> |
146 | | SubtleCrypto::DeriveBits(JSContext* cx, |
147 | | const ObjectOrString& algorithm, |
148 | | CryptoKey& baseKey, |
149 | | uint32_t length, |
150 | | ErrorResult& aRv) |
151 | 0 | { |
152 | 0 | SUBTLECRYPTO_METHOD_BODY(DeriveBits, aRv, cx, algorithm, baseKey, length) |
153 | 0 | } |
154 | | |
155 | | already_AddRefed<Promise> |
156 | | SubtleCrypto::WrapKey(JSContext* cx, |
157 | | const nsAString& format, |
158 | | CryptoKey& key, |
159 | | CryptoKey& wrappingKey, |
160 | | const ObjectOrString& wrapAlgorithm, |
161 | | ErrorResult& aRv) |
162 | 0 | { |
163 | 0 | SUBTLECRYPTO_METHOD_BODY(WrapKey, aRv, cx, format, key, wrappingKey, wrapAlgorithm) |
164 | 0 | } |
165 | | |
166 | | already_AddRefed<Promise> |
167 | | SubtleCrypto::UnwrapKey(JSContext* cx, |
168 | | const nsAString& format, |
169 | | const ArrayBufferViewOrArrayBuffer& wrappedKey, |
170 | | CryptoKey& unwrappingKey, |
171 | | const ObjectOrString& unwrapAlgorithm, |
172 | | const ObjectOrString& unwrappedKeyAlgorithm, |
173 | | bool extractable, |
174 | | const Sequence<nsString>& keyUsages, |
175 | | ErrorResult& aRv) |
176 | 0 | { |
177 | 0 | SUBTLECRYPTO_METHOD_BODY(UnwrapKey, aRv, mParent, cx, format, wrappedKey, |
178 | | unwrappingKey, unwrapAlgorithm, |
179 | | unwrappedKeyAlgorithm, extractable, keyUsages) |
180 | | } |
181 | | |
182 | | } // namespace dom |
183 | | } // namespace mozilla |