2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "modules/crypto/SubtleCrypto.h"
34 #include "bindings/v8/Dictionary.h"
35 #include "bindings/v8/ExceptionState.h"
36 #include "core/dom/ExceptionCode.h"
37 #include "modules/crypto/CryptoResultImpl.h"
38 #include "modules/crypto/Key.h"
39 #include "modules/crypto/NormalizeAlgorithm.h"
40 #include "public/platform/Platform.h"
41 #include "public/platform/WebCrypto.h"
42 #include "public/platform/WebCryptoAlgorithm.h"
43 #include "wtf/ArrayBufferView.h"
47 DEFINE_GC_INFO(SubtleCrypto);
51 bool parseAlgorithm(const Dictionary& rawAlgorithm, AlgorithmOperation operationType, blink::WebCryptoAlgorithm &algorithm, ExceptionState& exceptionState, CryptoResult* result)
54 if (parseAlgorithm(rawAlgorithm, operationType, algorithm, errorDetails, exceptionState))
57 if (!exceptionState.hadException())
58 result->completeWithError(errorDetails);
63 ScriptPromise startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, AlgorithmOperation operationType, ArrayBufferView* signature, ArrayBufferView* dataBuffer, ExceptionState& exceptionState)
65 bool requiresKey = operationType != Digest;
67 // Seems like the generated bindings should take care of these however it
68 // currently doesn't. See also http://crbugh.com/264520
69 if (requiresKey && !key) {
70 exceptionState.throwTypeError("Invalid key argument");
71 return ScriptPromise();
73 if (operationType == Verify && !signature) {
74 exceptionState.throwTypeError("Invalid signature argument");
75 return ScriptPromise();
78 exceptionState.throwTypeError("Invalid dataBuffer argument");
79 return ScriptPromise();
82 ScriptPromise promise = ScriptPromise::createPending();
83 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
85 blink::WebCryptoAlgorithm algorithm;
86 if (!parseAlgorithm(rawAlgorithm, operationType, algorithm, exceptionState, result.get()))
90 if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, errorDetails)) {
91 result->completeWithError(errorDetails);
95 const unsigned char* data = static_cast<const unsigned char*>(dataBuffer->baseAddress());
96 unsigned dataSize = dataBuffer->byteLength();
98 switch (operationType) {
100 blink::Platform::current()->crypto()->encrypt(algorithm, key->key(), data, dataSize, result->result());
103 blink::Platform::current()->crypto()->decrypt(algorithm, key->key(), data, dataSize, result->result());
106 blink::Platform::current()->crypto()->sign(algorithm, key->key(), data, dataSize, result->result());
109 blink::Platform::current()->crypto()->verifySignature(algorithm, key->key(), reinterpret_cast<const unsigned char*>(signature->baseAddress()), signature->byteLength(), data, dataSize, result->result());
112 blink::Platform::current()->crypto()->digest(algorithm, data, dataSize, result->result());
115 ASSERT_NOT_REACHED();
116 return ScriptPromise();
124 SubtleCrypto::SubtleCrypto()
126 ScriptWrappable::init(this);
129 ScriptPromise SubtleCrypto::encrypt(const Dictionary& rawAlgorithm, Key* key, ArrayBufferView* data, ExceptionState& exceptionState)
131 return startCryptoOperation(rawAlgorithm, key, Encrypt, 0, data, exceptionState);
134 ScriptPromise SubtleCrypto::decrypt(const Dictionary& rawAlgorithm, Key* key, ArrayBufferView* data, ExceptionState& exceptionState)
136 return startCryptoOperation(rawAlgorithm, key, Decrypt, 0, data, exceptionState);
139 ScriptPromise SubtleCrypto::sign(const Dictionary& rawAlgorithm, Key* key, ArrayBufferView* data, ExceptionState& exceptionState)
141 return startCryptoOperation(rawAlgorithm, key, Sign, 0, data, exceptionState);
144 ScriptPromise SubtleCrypto::verifySignature(const Dictionary& rawAlgorithm, Key* key, ArrayBufferView* signature, ArrayBufferView* data, ExceptionState& exceptionState)
146 return startCryptoOperation(rawAlgorithm, key, Verify, signature, data, exceptionState);
149 ScriptPromise SubtleCrypto::digest(const Dictionary& rawAlgorithm, ArrayBufferView* data, ExceptionState& exceptionState)
151 return startCryptoOperation(rawAlgorithm, 0, Digest, 0, data, exceptionState);
154 ScriptPromise SubtleCrypto::generateKey(const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages, ExceptionState& exceptionState)
156 blink::WebCryptoKeyUsageMask keyUsages;
157 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, exceptionState))
158 return ScriptPromise();
160 ScriptPromise promise = ScriptPromise::createPending();
161 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
163 blink::WebCryptoAlgorithm algorithm;
164 if (!parseAlgorithm(rawAlgorithm, GenerateKey, algorithm, exceptionState, result.get()))
167 blink::Platform::current()->crypto()->generateKey(algorithm, extractable, keyUsages, result->result());
171 ScriptPromise SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView* keyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages, ExceptionState& exceptionState)
173 blink::WebCryptoKeyFormat format;
174 if (!Key::parseFormat(rawFormat, format, exceptionState))
175 return ScriptPromise();
178 exceptionState.throwTypeError("Invalid keyData argument");
179 return ScriptPromise();
182 blink::WebCryptoKeyUsageMask keyUsages;
183 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, exceptionState))
184 return ScriptPromise();
186 ScriptPromise promise = ScriptPromise::createPending();
187 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
189 // The algorithm is optional.
190 blink::WebCryptoAlgorithm algorithm;
191 if (!rawAlgorithm.isUndefinedOrNull() && !parseAlgorithm(rawAlgorithm, ImportKey, algorithm, exceptionState, result.get()))
194 const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->baseAddress());
196 blink::Platform::current()->crypto()->importKey(format, keyDataBytes, keyData->byteLength(), algorithm, extractable, keyUsages, result->result());
200 ScriptPromise SubtleCrypto::exportKey(const String& rawFormat, Key* key, ExceptionState& exceptionState)
202 blink::WebCryptoKeyFormat format;
203 if (!Key::parseFormat(rawFormat, format, exceptionState))
204 return ScriptPromise();
207 exceptionState.throwTypeError("Invalid key argument");
208 return ScriptPromise();
211 ScriptPromise promise = ScriptPromise::createPending();
212 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
214 if (!key->extractable()) {
215 result->completeWithError("key is not extractable");
219 blink::Platform::current()->crypto()->exportKey(format, key->key(), result->result());
223 ScriptPromise SubtleCrypto::wrapKey(const String& rawFormat, Key* key, Key* wrappingKey, const Dictionary& rawWrapAlgorithm, ExceptionState& exceptionState)
225 blink::WebCryptoKeyFormat format;
226 if (!Key::parseFormat(rawFormat, format, exceptionState))
227 return ScriptPromise();
230 exceptionState.throwTypeError("Invalid key argument");
231 return ScriptPromise();
235 exceptionState.throwTypeError("Invalid wrappingKey argument");
236 return ScriptPromise();
239 ScriptPromise promise = ScriptPromise::createPending();
240 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
242 blink::WebCryptoAlgorithm wrapAlgorithm;
243 if (!parseAlgorithm(rawWrapAlgorithm, WrapKey, wrapAlgorithm, exceptionState, result.get()))
246 if (!key->extractable()) {
247 result->completeWithError("key is not extractable");
252 if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, WrapKey, errorDetails)) {
253 result->completeWithError(errorDetails);
257 blink::Platform::current()->crypto()->wrapKey(format, key->key(), wrappingKey->key(), wrapAlgorithm, result->result());
261 ScriptPromise SubtleCrypto::unwrapKey(const String& rawFormat, ArrayBufferView* wrappedKey, Key* unwrappingKey, const Dictionary& rawUnwrapAlgorithm, const Dictionary& rawUnwrappedKeyAlgorithm, bool extractable, const Vector<String>& rawKeyUsages, ExceptionState& exceptionState)
263 blink::WebCryptoKeyFormat format;
264 if (!Key::parseFormat(rawFormat, format, exceptionState))
265 return ScriptPromise();
268 exceptionState.throwTypeError("Invalid wrappedKey argument");
269 return ScriptPromise();
272 if (!unwrappingKey) {
273 exceptionState.throwTypeError("Invalid unwrappingKey argument");
274 return ScriptPromise();
277 blink::WebCryptoKeyUsageMask keyUsages;
278 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, exceptionState))
279 return ScriptPromise();
281 ScriptPromise promise = ScriptPromise::createPending();
282 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
284 blink::WebCryptoAlgorithm unwrapAlgorithm;
285 if (!parseAlgorithm(rawUnwrapAlgorithm, UnwrapKey, unwrapAlgorithm, exceptionState, result.get()))
288 // The unwrappedKeyAlgorithm is optional.
289 blink::WebCryptoAlgorithm unwrappedKeyAlgorithm;
290 if (!rawUnwrappedKeyAlgorithm.isUndefinedOrNull() && !parseAlgorithm(rawUnwrappedKeyAlgorithm, ImportKey, unwrappedKeyAlgorithm, exceptionState, result.get()))
294 if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, UnwrapKey, errorDetails)) {
295 result->completeWithError(errorDetails);
299 const unsigned char* wrappedKeyData = static_cast<const unsigned char*>(wrappedKey->baseAddress());
300 unsigned wrappedKeyDataSize = wrappedKey->byteLength();
302 blink::Platform::current()->crypto()->unwrapKey(format, wrappedKeyData, wrappedKeyDataSize, unwrappingKey->key(), unwrapAlgorithm, unwrappedKeyAlgorithm, extractable, keyUsages, result->result());
306 } // namespace WebCore