Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / crypto / SubtleCrypto.cpp
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "modules/crypto/SubtleCrypto.h"
33
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"
44
45 namespace WebCore {
46
47 DEFINE_GC_INFO(SubtleCrypto);
48
49 namespace {
50
51 bool parseAlgorithm(const Dictionary& rawAlgorithm, AlgorithmOperation operationType, blink::WebCryptoAlgorithm &algorithm, ExceptionState& exceptionState, CryptoResult* result)
52 {
53     String errorDetails;
54     if (parseAlgorithm(rawAlgorithm, operationType, algorithm, errorDetails, exceptionState))
55         return true;
56
57     if (!exceptionState.hadException())
58         result->completeWithError(errorDetails);
59
60     return false;
61 }
62
63 ScriptPromise startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, AlgorithmOperation operationType, ArrayBufferView* signature, ArrayBufferView* dataBuffer, ExceptionState& exceptionState)
64 {
65     bool requiresKey = operationType != Digest;
66
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();
72     }
73     if (operationType == Verify && !signature) {
74         exceptionState.throwTypeError("Invalid signature argument");
75         return ScriptPromise();
76     }
77     if (!dataBuffer) {
78         exceptionState.throwTypeError("Invalid dataBuffer argument");
79         return ScriptPromise();
80     }
81
82     ScriptPromise promise = ScriptPromise::createPending();
83     RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
84
85     blink::WebCryptoAlgorithm algorithm;
86     if (!parseAlgorithm(rawAlgorithm, operationType, algorithm, exceptionState, result.get()))
87         return promise;
88
89     String errorDetails;
90     if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, errorDetails)) {
91         result->completeWithError(errorDetails);
92         return promise;
93     }
94
95     const unsigned char* data = static_cast<const unsigned char*>(dataBuffer->baseAddress());
96     unsigned dataSize = dataBuffer->byteLength();
97
98     switch (operationType) {
99     case Encrypt:
100         blink::Platform::current()->crypto()->encrypt(algorithm, key->key(), data, dataSize, result->result());
101         break;
102     case Decrypt:
103         blink::Platform::current()->crypto()->decrypt(algorithm, key->key(), data, dataSize, result->result());
104         break;
105     case Sign:
106         blink::Platform::current()->crypto()->sign(algorithm, key->key(), data, dataSize, result->result());
107         break;
108     case Verify:
109         blink::Platform::current()->crypto()->verifySignature(algorithm, key->key(), reinterpret_cast<const unsigned char*>(signature->baseAddress()), signature->byteLength(), data, dataSize, result->result());
110         break;
111     case Digest:
112         blink::Platform::current()->crypto()->digest(algorithm, data, dataSize, result->result());
113         break;
114     default:
115         ASSERT_NOT_REACHED();
116         return ScriptPromise();
117     }
118
119     return promise;
120 }
121
122 } // namespace
123
124 SubtleCrypto::SubtleCrypto()
125 {
126     ScriptWrappable::init(this);
127 }
128
129 ScriptPromise SubtleCrypto::encrypt(const Dictionary& rawAlgorithm, Key* key, ArrayBufferView* data, ExceptionState& exceptionState)
130 {
131     return startCryptoOperation(rawAlgorithm, key, Encrypt, 0, data, exceptionState);
132 }
133
134 ScriptPromise SubtleCrypto::decrypt(const Dictionary& rawAlgorithm, Key* key, ArrayBufferView* data, ExceptionState& exceptionState)
135 {
136     return startCryptoOperation(rawAlgorithm, key, Decrypt, 0, data, exceptionState);
137 }
138
139 ScriptPromise SubtleCrypto::sign(const Dictionary& rawAlgorithm, Key* key, ArrayBufferView* data, ExceptionState& exceptionState)
140 {
141     return startCryptoOperation(rawAlgorithm, key, Sign, 0, data, exceptionState);
142 }
143
144 ScriptPromise SubtleCrypto::verifySignature(const Dictionary& rawAlgorithm, Key* key, ArrayBufferView* signature, ArrayBufferView* data, ExceptionState& exceptionState)
145 {
146     return startCryptoOperation(rawAlgorithm, key, Verify, signature, data, exceptionState);
147 }
148
149 ScriptPromise SubtleCrypto::digest(const Dictionary& rawAlgorithm, ArrayBufferView* data, ExceptionState& exceptionState)
150 {
151     return startCryptoOperation(rawAlgorithm, 0, Digest, 0, data, exceptionState);
152 }
153
154 ScriptPromise SubtleCrypto::generateKey(const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages, ExceptionState& exceptionState)
155 {
156     blink::WebCryptoKeyUsageMask keyUsages;
157     if (!Key::parseUsageMask(rawKeyUsages, keyUsages, exceptionState))
158         return ScriptPromise();
159
160     ScriptPromise promise = ScriptPromise::createPending();
161     RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
162
163     blink::WebCryptoAlgorithm algorithm;
164     if (!parseAlgorithm(rawAlgorithm, GenerateKey, algorithm, exceptionState, result.get()))
165         return promise;
166
167     blink::Platform::current()->crypto()->generateKey(algorithm, extractable, keyUsages, result->result());
168     return promise;
169 }
170
171 ScriptPromise SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView* keyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages, ExceptionState& exceptionState)
172 {
173     blink::WebCryptoKeyFormat format;
174     if (!Key::parseFormat(rawFormat, format, exceptionState))
175         return ScriptPromise();
176
177     if (!keyData) {
178         exceptionState.throwTypeError("Invalid keyData argument");
179         return ScriptPromise();
180     }
181
182     blink::WebCryptoKeyUsageMask keyUsages;
183     if (!Key::parseUsageMask(rawKeyUsages, keyUsages, exceptionState))
184         return ScriptPromise();
185
186     ScriptPromise promise = ScriptPromise::createPending();
187     RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
188
189     // The algorithm is optional.
190     blink::WebCryptoAlgorithm algorithm;
191     if (!rawAlgorithm.isUndefinedOrNull() && !parseAlgorithm(rawAlgorithm, ImportKey, algorithm, exceptionState, result.get()))
192         return promise;
193
194     const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->baseAddress());
195
196     blink::Platform::current()->crypto()->importKey(format, keyDataBytes, keyData->byteLength(), algorithm, extractable, keyUsages, result->result());
197     return promise;
198 }
199
200 ScriptPromise SubtleCrypto::exportKey(const String& rawFormat, Key* key, ExceptionState& exceptionState)
201 {
202     blink::WebCryptoKeyFormat format;
203     if (!Key::parseFormat(rawFormat, format, exceptionState))
204         return ScriptPromise();
205
206     if (!key) {
207         exceptionState.throwTypeError("Invalid key argument");
208         return ScriptPromise();
209     }
210
211     ScriptPromise promise = ScriptPromise::createPending();
212     RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
213
214     if (!key->extractable()) {
215         result->completeWithError("key is not extractable");
216         return promise;
217     }
218
219     blink::Platform::current()->crypto()->exportKey(format, key->key(), result->result());
220     return promise;
221 }
222
223 ScriptPromise SubtleCrypto::wrapKey(const String& rawFormat, Key* key, Key* wrappingKey, const Dictionary& rawWrapAlgorithm, ExceptionState& exceptionState)
224 {
225     blink::WebCryptoKeyFormat format;
226     if (!Key::parseFormat(rawFormat, format, exceptionState))
227         return ScriptPromise();
228
229     if (!key) {
230         exceptionState.throwTypeError("Invalid key argument");
231         return ScriptPromise();
232     }
233
234     if (!wrappingKey) {
235         exceptionState.throwTypeError("Invalid wrappingKey argument");
236         return ScriptPromise();
237     }
238
239     ScriptPromise promise = ScriptPromise::createPending();
240     RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
241
242     blink::WebCryptoAlgorithm wrapAlgorithm;
243     if (!parseAlgorithm(rawWrapAlgorithm, WrapKey, wrapAlgorithm, exceptionState, result.get()))
244         return promise;
245
246     if (!key->extractable()) {
247         result->completeWithError("key is not extractable");
248         return promise;
249     }
250
251     String errorDetails;
252     if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, WrapKey, errorDetails)) {
253         result->completeWithError(errorDetails);
254         return promise;
255     }
256
257     blink::Platform::current()->crypto()->wrapKey(format, key->key(), wrappingKey->key(), wrapAlgorithm, result->result());
258     return promise;
259 }
260
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)
262 {
263     blink::WebCryptoKeyFormat format;
264     if (!Key::parseFormat(rawFormat, format, exceptionState))
265         return ScriptPromise();
266
267     if (!wrappedKey) {
268         exceptionState.throwTypeError("Invalid wrappedKey argument");
269         return ScriptPromise();
270     }
271
272     if (!unwrappingKey) {
273         exceptionState.throwTypeError("Invalid unwrappingKey argument");
274         return ScriptPromise();
275     }
276
277     blink::WebCryptoKeyUsageMask keyUsages;
278     if (!Key::parseUsageMask(rawKeyUsages, keyUsages, exceptionState))
279         return ScriptPromise();
280
281     ScriptPromise promise = ScriptPromise::createPending();
282     RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(promise);
283
284     blink::WebCryptoAlgorithm unwrapAlgorithm;
285     if (!parseAlgorithm(rawUnwrapAlgorithm, UnwrapKey, unwrapAlgorithm, exceptionState, result.get()))
286         return promise;
287
288     // The unwrappedKeyAlgorithm is optional.
289     blink::WebCryptoAlgorithm unwrappedKeyAlgorithm;
290     if (!rawUnwrappedKeyAlgorithm.isUndefinedOrNull() && !parseAlgorithm(rawUnwrappedKeyAlgorithm, ImportKey, unwrappedKeyAlgorithm, exceptionState, result.get()))
291         return promise;
292
293     String errorDetails;
294     if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, UnwrapKey, errorDetails)) {
295         result->completeWithError(errorDetails);
296         return promise;
297     }
298
299     const unsigned char* wrappedKeyData = static_cast<const unsigned char*>(wrappedKey->baseAddress());
300     unsigned wrappedKeyDataSize = wrappedKey->byteLength();
301
302     blink::Platform::current()->crypto()->unwrapKey(format, wrappedKeyData, wrappedKeyDataSize, unwrappingKey->key(), unwrapAlgorithm, unwrappedKeyAlgorithm, extractable, keyUsages, result->result());
303     return promise;
304 }
305
306 } // namespace WebCore