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 "modules/crypto/CryptoResultImpl.h"
36 #include "modules/crypto/Key.h"
37 #include "modules/crypto/NormalizeAlgorithm.h"
38 #include "public/platform/Platform.h"
39 #include "public/platform/WebCrypto.h"
40 #include "public/platform/WebCryptoAlgorithm.h"
41 #include "wtf/ArrayBufferView.h"
47 // Seems like the generated bindings should take care of these however it
48 // currently doesn't. See also http://crbug.com/264520
49 bool ensureNotNull(const ArrayPiece& x, const char* paramName, CryptoResult* result)
52 String message = String("Invalid ") + paramName + String(" argument");
53 result->completeWithError(blink::WebCryptoErrorTypeType, blink::WebString(message));
59 bool ensureNotNull(Key* key, const char* paramName, CryptoResult* result)
62 String message = String("Invalid ") + paramName + String(" argument");
63 result->completeWithError(blink::WebCryptoErrorTypeType, blink::WebString(message));
69 ScriptPromise startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, AlgorithmOperation operationType, const ArrayPiece& signature, const ArrayPiece& dataBuffer)
71 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create();
72 ScriptPromise promise = result->promise();
74 bool requiresKey = operationType != Digest;
76 if (requiresKey && !ensureNotNull(key, "key", result.get()))
78 if (operationType == Verify && !ensureNotNull(signature, "signature", result.get()))
80 if (!ensureNotNull(dataBuffer, "dataBuffer", result.get()))
83 blink::WebCryptoAlgorithm algorithm;
84 if (!parseAlgorithm(rawAlgorithm, operationType, algorithm, result.get()))
87 if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, result.get()))
90 const unsigned char* data = dataBuffer.bytes();
91 unsigned dataSize = dataBuffer.byteLength();
93 switch (operationType) {
95 blink::Platform::current()->crypto()->encrypt(algorithm, key->key(), data, dataSize, result->result());
98 blink::Platform::current()->crypto()->decrypt(algorithm, key->key(), data, dataSize, result->result());
101 blink::Platform::current()->crypto()->sign(algorithm, key->key(), data, dataSize, result->result());
104 blink::Platform::current()->crypto()->verifySignature(algorithm, key->key(), signature.bytes(), signature.byteLength(), data, dataSize, result->result());
107 blink::Platform::current()->crypto()->digest(algorithm, data, dataSize, result->result());
110 ASSERT_NOT_REACHED();
111 return ScriptPromise();
119 SubtleCrypto::SubtleCrypto()
121 ScriptWrappable::init(this);
124 ScriptPromise SubtleCrypto::encrypt(const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& data)
126 return startCryptoOperation(rawAlgorithm, key, Encrypt, ArrayPiece(), data);
129 ScriptPromise SubtleCrypto::decrypt(const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& data)
131 return startCryptoOperation(rawAlgorithm, key, Decrypt, ArrayPiece(), data);
134 ScriptPromise SubtleCrypto::sign(const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& data)
136 return startCryptoOperation(rawAlgorithm, key, Sign, ArrayPiece(), data);
139 ScriptPromise SubtleCrypto::verifySignature(const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& signature, const ArrayPiece& data)
141 return startCryptoOperation(rawAlgorithm, key, Verify, signature, data);
144 ScriptPromise SubtleCrypto::digest(const Dictionary& rawAlgorithm, const ArrayPiece& data)
146 return startCryptoOperation(rawAlgorithm, 0, Digest, ArrayPiece(), data);
149 ScriptPromise SubtleCrypto::generateKey(const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages)
151 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create();
152 ScriptPromise promise = result->promise();
154 blink::WebCryptoKeyUsageMask keyUsages;
155 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get()))
158 blink::WebCryptoAlgorithm algorithm;
159 if (!parseAlgorithm(rawAlgorithm, GenerateKey, algorithm, result.get()))
162 blink::Platform::current()->crypto()->generateKey(algorithm, extractable, keyUsages, result->result());
166 ScriptPromise SubtleCrypto::importKey(const String& rawFormat, const ArrayPiece& keyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages)
168 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create();
169 ScriptPromise promise = result->promise();
171 if (!ensureNotNull(keyData, "keyData", result.get()))
174 blink::WebCryptoKeyFormat format;
175 if (!Key::parseFormat(rawFormat, format, result.get()))
178 blink::WebCryptoKeyUsageMask keyUsages;
179 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get()))
182 blink::WebCryptoAlgorithm algorithm;
183 if (!parseAlgorithm(rawAlgorithm, ImportKey, algorithm, result.get()))
186 blink::Platform::current()->crypto()->importKey(format, keyData.bytes(), keyData.byteLength(), algorithm, extractable, keyUsages, result->result());
190 ScriptPromise SubtleCrypto::exportKey(const String& rawFormat, Key* key)
192 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create();
193 ScriptPromise promise = result->promise();
195 if (!ensureNotNull(key, "key", result.get()))
198 blink::WebCryptoKeyFormat format;
199 if (!Key::parseFormat(rawFormat, format, result.get()))
202 if (!key->extractable()) {
203 result->completeWithError(blink::WebCryptoErrorTypeInvalidAccess, "key is not extractable");
207 blink::Platform::current()->crypto()->exportKey(format, key->key(), result->result());
211 ScriptPromise SubtleCrypto::wrapKey(const String& rawFormat, Key* key, Key* wrappingKey, const Dictionary& rawWrapAlgorithm)
213 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create();
214 ScriptPromise promise = result->promise();
216 if (!ensureNotNull(key, "key", result.get()))
219 if (!ensureNotNull(wrappingKey, "wrappingKey", result.get()))
222 blink::WebCryptoKeyFormat format;
223 if (!Key::parseFormat(rawFormat, format, result.get()))
226 blink::WebCryptoAlgorithm wrapAlgorithm;
227 if (!parseAlgorithm(rawWrapAlgorithm, WrapKey, wrapAlgorithm, result.get()))
230 if (!key->extractable()) {
231 result->completeWithError(blink::WebCryptoErrorTypeInvalidAccess, "key is not extractable");
235 if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, WrapKey, result.get()))
238 blink::Platform::current()->crypto()->wrapKey(format, key->key(), wrappingKey->key(), wrapAlgorithm, result->result());
242 ScriptPromise SubtleCrypto::unwrapKey(const String& rawFormat, const ArrayPiece& wrappedKey, Key* unwrappingKey, const Dictionary& rawUnwrapAlgorithm, const Dictionary& rawUnwrappedKeyAlgorithm, bool extractable, const Vector<String>& rawKeyUsages)
244 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create();
245 ScriptPromise promise = result->promise();
247 if (!ensureNotNull(wrappedKey, "wrappedKey", result.get()))
249 if (!ensureNotNull(unwrappingKey, "unwrappingKey", result.get()))
252 blink::WebCryptoKeyFormat format;
253 if (!Key::parseFormat(rawFormat, format, result.get()))
256 blink::WebCryptoKeyUsageMask keyUsages;
257 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get()))
260 blink::WebCryptoAlgorithm unwrapAlgorithm;
261 if (!parseAlgorithm(rawUnwrapAlgorithm, UnwrapKey, unwrapAlgorithm, result.get()))
264 blink::WebCryptoAlgorithm unwrappedKeyAlgorithm;
265 if (!parseAlgorithm(rawUnwrappedKeyAlgorithm, ImportKey, unwrappedKeyAlgorithm, result.get()))
268 if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, UnwrapKey, result.get()))
271 blink::Platform::current()->crypto()->unwrapKey(format, wrappedKey.bytes(), wrappedKey.byteLength(), unwrappingKey->key(), unwrapAlgorithm, unwrappedKeyAlgorithm, extractable, keyUsages, result->result());
275 } // namespace WebCore