OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 14 matching lines...) Expand all Loading... | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "modules/crypto/SubtleCrypto.h" | 32 #include "modules/crypto/SubtleCrypto.h" |
33 | 33 |
34 #include "bindings/v8/Dictionary.h" | 34 #include "bindings/v8/Dictionary.h" |
35 #include "bindings/v8/ExceptionState.h" | |
36 #include "modules/crypto/CryptoResultImpl.h" | 35 #include "modules/crypto/CryptoResultImpl.h" |
37 #include "modules/crypto/Key.h" | 36 #include "modules/crypto/Key.h" |
38 #include "modules/crypto/NormalizeAlgorithm.h" | 37 #include "modules/crypto/NormalizeAlgorithm.h" |
39 #include "public/platform/Platform.h" | 38 #include "public/platform/Platform.h" |
40 #include "public/platform/WebCrypto.h" | 39 #include "public/platform/WebCrypto.h" |
41 #include "public/platform/WebCryptoAlgorithm.h" | 40 #include "public/platform/WebCryptoAlgorithm.h" |
42 #include "wtf/ArrayBufferView.h" | 41 #include "wtf/ArrayBufferView.h" |
42 #include "wtf/text/StringBuilder.h" | |
43 | 43 |
44 namespace WebCore { | 44 namespace WebCore { |
45 | 45 |
46 namespace { | 46 namespace { |
47 | 47 |
48 bool parseAlgorithm(const Dictionary& rawAlgorithm, AlgorithmOperation operation Type, blink::WebCryptoAlgorithm &algorithm, ExceptionState& exceptionState, Cryp toResult* result) | 48 // Seems like the generated bindings should take care of these however it |
49 // currently doesn't. See also http://crbug.com/264520 | |
abarth-chromium
2014/04/02 20:42:40
Can we fix the code generator instead of working a
eroman
2014/04/02 23:46:33
Absolutely.
I will take a look at the generator n
| |
50 template <typename T> | |
51 bool ensureNotNull(T* x, const char* paramName, CryptoResult* result) | |
49 { | 52 { |
50 if (!rawAlgorithm.isObject()) { | 53 if (!x) { |
51 exceptionState.throwTypeError("Algorithm: Not an object"); | 54 StringBuilder strBuilder; |
abarth-chromium
2014/04/02 20:42:40
strBuilder -> builder
Please use complete words i
eroman
2014/04/02 23:46:33
Done.
| |
55 strBuilder.append("Invalid "); | |
56 strBuilder.append(paramName); | |
57 strBuilder.append(" argument"); | |
58 result->completeWithError(blink::WebString(String(strBuilder.toString()) )); | |
abarth-chromium
2014/04/02 20:42:40
You can just use operator+ instead of string build
eroman
2014/04/02 23:46:33
Done.
| |
52 return false; | 59 return false; |
53 } | 60 } |
54 return parseAlgorithm(rawAlgorithm, operationType, algorithm, result); | 61 return true; |
55 } | 62 } |
56 | 63 |
57 ScriptPromise startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, Alg orithmOperation operationType, ArrayBufferView* signature, ArrayBufferView* data Buffer, ExceptionState& exceptionState) | 64 ScriptPromise startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, Alg orithmOperation operationType, ArrayBufferView* signature, ArrayBufferView* data Buffer) |
58 { | 65 { |
59 bool requiresKey = operationType != Digest; | |
60 | |
61 // Seems like the generated bindings should take care of these however it | |
62 // currently doesn't. See also http://crbugh.com/264520 | |
63 if (requiresKey && !key) { | |
64 exceptionState.throwTypeError("Invalid key argument"); | |
65 return ScriptPromise(); | |
66 } | |
67 if (operationType == Verify && !signature) { | |
68 exceptionState.throwTypeError("Invalid signature argument"); | |
69 return ScriptPromise(); | |
70 } | |
71 if (!dataBuffer) { | |
72 exceptionState.throwTypeError("Invalid dataBuffer argument"); | |
73 return ScriptPromise(); | |
74 } | |
75 | |
76 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); | 66 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); |
77 ScriptPromise promise = result->promise(); | 67 ScriptPromise promise = result->promise(); |
78 | 68 |
69 bool requiresKey = operationType != Digest; | |
70 | |
71 if (requiresKey && !ensureNotNull(key, "key", result.get())) | |
72 return promise; | |
73 if (operationType == Verify && !ensureNotNull(signature, "signature", result .get())) | |
74 return promise; | |
75 if (!ensureNotNull(dataBuffer, "dataBuffer", result.get())) | |
76 return promise; | |
77 | |
79 blink::WebCryptoAlgorithm algorithm; | 78 blink::WebCryptoAlgorithm algorithm; |
80 if (!parseAlgorithm(rawAlgorithm, operationType, algorithm, exceptionState, result.get())) | 79 if (!parseAlgorithm(rawAlgorithm, operationType, algorithm, result.get())) |
81 return promise; | 80 return promise; |
82 | 81 |
83 if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, res ult.get())) | 82 if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, res ult.get())) |
84 return promise; | 83 return promise; |
85 | 84 |
86 const unsigned char* data = static_cast<const unsigned char*>(dataBuffer->ba seAddress()); | 85 const unsigned char* data = static_cast<const unsigned char*>(dataBuffer->ba seAddress()); |
87 unsigned dataSize = dataBuffer->byteLength(); | 86 unsigned dataSize = dataBuffer->byteLength(); |
88 | 87 |
89 switch (operationType) { | 88 switch (operationType) { |
90 case Encrypt: | 89 case Encrypt: |
(...skipping 19 matching lines...) Expand all Loading... | |
110 return promise; | 109 return promise; |
111 } | 110 } |
112 | 111 |
113 } // namespace | 112 } // namespace |
114 | 113 |
115 SubtleCrypto::SubtleCrypto() | 114 SubtleCrypto::SubtleCrypto() |
116 { | 115 { |
117 ScriptWrappable::init(this); | 116 ScriptWrappable::init(this); |
118 } | 117 } |
119 | 118 |
120 ScriptPromise SubtleCrypto::encrypt(const Dictionary& rawAlgorithm, Key* key, Ar rayBufferView* data, ExceptionState& exceptionState) | 119 ScriptPromise SubtleCrypto::encrypt(const Dictionary& rawAlgorithm, Key* key, Ar rayBufferView* data) |
121 { | 120 { |
122 return startCryptoOperation(rawAlgorithm, key, Encrypt, 0, data, exceptionSt ate); | 121 return startCryptoOperation(rawAlgorithm, key, Encrypt, 0, data); |
123 } | 122 } |
124 | 123 |
125 ScriptPromise SubtleCrypto::decrypt(const Dictionary& rawAlgorithm, Key* key, Ar rayBufferView* data, ExceptionState& exceptionState) | 124 ScriptPromise SubtleCrypto::decrypt(const Dictionary& rawAlgorithm, Key* key, Ar rayBufferView* data) |
126 { | 125 { |
127 return startCryptoOperation(rawAlgorithm, key, Decrypt, 0, data, exceptionSt ate); | 126 return startCryptoOperation(rawAlgorithm, key, Decrypt, 0, data); |
128 } | 127 } |
129 | 128 |
130 ScriptPromise SubtleCrypto::sign(const Dictionary& rawAlgorithm, Key* key, Array BufferView* data, ExceptionState& exceptionState) | 129 ScriptPromise SubtleCrypto::sign(const Dictionary& rawAlgorithm, Key* key, Array BufferView* data) |
131 { | 130 { |
132 return startCryptoOperation(rawAlgorithm, key, Sign, 0, data, exceptionState ); | 131 return startCryptoOperation(rawAlgorithm, key, Sign, 0, data); |
133 } | 132 } |
134 | 133 |
135 ScriptPromise SubtleCrypto::verifySignature(const Dictionary& rawAlgorithm, Key* key, ArrayBufferView* signature, ArrayBufferView* data, ExceptionState& excepti onState) | 134 ScriptPromise SubtleCrypto::verifySignature(const Dictionary& rawAlgorithm, Key* key, ArrayBufferView* signature, ArrayBufferView* data) |
136 { | 135 { |
137 return startCryptoOperation(rawAlgorithm, key, Verify, signature, data, exce ptionState); | 136 return startCryptoOperation(rawAlgorithm, key, Verify, signature, data); |
138 } | 137 } |
139 | 138 |
140 ScriptPromise SubtleCrypto::digest(const Dictionary& rawAlgorithm, ArrayBufferVi ew* data, ExceptionState& exceptionState) | 139 ScriptPromise SubtleCrypto::digest(const Dictionary& rawAlgorithm, ArrayBufferVi ew* data) |
141 { | 140 { |
142 return startCryptoOperation(rawAlgorithm, 0, Digest, 0, data, exceptionState ); | 141 return startCryptoOperation(rawAlgorithm, 0, Digest, 0, data); |
143 } | 142 } |
144 | 143 |
145 ScriptPromise SubtleCrypto::generateKey(const Dictionary& rawAlgorithm, bool ext ractable, const Vector<String>& rawKeyUsages, ExceptionState& exceptionState) | 144 ScriptPromise SubtleCrypto::generateKey(const Dictionary& rawAlgorithm, bool ext ractable, const Vector<String>& rawKeyUsages) |
146 { | 145 { |
147 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); | 146 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); |
148 ScriptPromise promise = result->promise(); | 147 ScriptPromise promise = result->promise(); |
149 | 148 |
150 blink::WebCryptoKeyUsageMask keyUsages; | 149 blink::WebCryptoKeyUsageMask keyUsages; |
151 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) | 150 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) |
152 return promise; | 151 return promise; |
153 | 152 |
154 blink::WebCryptoAlgorithm algorithm; | 153 blink::WebCryptoAlgorithm algorithm; |
155 if (!parseAlgorithm(rawAlgorithm, GenerateKey, algorithm, exceptionState, re sult.get())) | 154 if (!parseAlgorithm(rawAlgorithm, GenerateKey, algorithm, result.get())) |
156 return promise; | 155 return promise; |
157 | 156 |
158 blink::Platform::current()->crypto()->generateKey(algorithm, extractable, ke yUsages, result->result()); | 157 blink::Platform::current()->crypto()->generateKey(algorithm, extractable, ke yUsages, result->result()); |
159 return promise; | 158 return promise; |
160 } | 159 } |
161 | 160 |
162 ScriptPromise SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView* keyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages, ExceptionState& exceptionState) | 161 ScriptPromise SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView* keyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages) |
163 { | 162 { |
164 if (!keyData) { | |
165 exceptionState.throwTypeError("Invalid keyData argument"); | |
166 return ScriptPromise(); | |
167 } | |
168 | |
169 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); | 163 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); |
170 ScriptPromise promise = result->promise(); | 164 ScriptPromise promise = result->promise(); |
171 | 165 |
166 if (!ensureNotNull(keyData, "keyData", result.get())) | |
167 return promise; | |
168 | |
172 blink::WebCryptoKeyFormat format; | 169 blink::WebCryptoKeyFormat format; |
173 if (!Key::parseFormat(rawFormat, format, result.get())) | 170 if (!Key::parseFormat(rawFormat, format, result.get())) |
174 return promise; | 171 return promise; |
175 | 172 |
176 blink::WebCryptoKeyUsageMask keyUsages; | 173 blink::WebCryptoKeyUsageMask keyUsages; |
177 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) | 174 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) |
178 return promise; | 175 return promise; |
179 | 176 |
180 blink::WebCryptoAlgorithm algorithm; | 177 blink::WebCryptoAlgorithm algorithm; |
181 if (!parseAlgorithm(rawAlgorithm, ImportKey, algorithm, exceptionState, resu lt.get())) | 178 if (!parseAlgorithm(rawAlgorithm, ImportKey, algorithm, result.get())) |
182 return promise; | 179 return promise; |
183 | 180 |
184 const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->bas eAddress()); | 181 const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->bas eAddress()); |
185 | 182 |
186 blink::Platform::current()->crypto()->importKey(format, keyDataBytes, keyDat a->byteLength(), algorithm, extractable, keyUsages, result->result()); | 183 blink::Platform::current()->crypto()->importKey(format, keyDataBytes, keyDat a->byteLength(), algorithm, extractable, keyUsages, result->result()); |
187 return promise; | 184 return promise; |
188 } | 185 } |
189 | 186 |
190 ScriptPromise SubtleCrypto::exportKey(const String& rawFormat, Key* key, Excepti onState& exceptionState) | 187 ScriptPromise SubtleCrypto::exportKey(const String& rawFormat, Key* key) |
191 { | 188 { |
192 if (!key) { | |
193 exceptionState.throwTypeError("Invalid key argument"); | |
194 return ScriptPromise(); | |
195 } | |
196 | |
197 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); | 189 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); |
198 ScriptPromise promise = result->promise(); | 190 ScriptPromise promise = result->promise(); |
199 | 191 |
192 if (!ensureNotNull(key, "key", result.get())) | |
193 return promise; | |
194 | |
200 blink::WebCryptoKeyFormat format; | 195 blink::WebCryptoKeyFormat format; |
201 if (!Key::parseFormat(rawFormat, format, result.get())) | 196 if (!Key::parseFormat(rawFormat, format, result.get())) |
202 return promise; | 197 return promise; |
203 | 198 |
204 if (!key->extractable()) { | 199 if (!key->extractable()) { |
205 result->completeWithError("key is not extractable"); | 200 result->completeWithError("key is not extractable"); |
206 return promise; | 201 return promise; |
207 } | 202 } |
208 | 203 |
209 blink::Platform::current()->crypto()->exportKey(format, key->key(), result-> result()); | 204 blink::Platform::current()->crypto()->exportKey(format, key->key(), result-> result()); |
210 return promise; | 205 return promise; |
211 } | 206 } |
212 | 207 |
213 ScriptPromise SubtleCrypto::wrapKey(const String& rawFormat, Key* key, Key* wrap pingKey, const Dictionary& rawWrapAlgorithm, ExceptionState& exceptionState) | 208 ScriptPromise SubtleCrypto::wrapKey(const String& rawFormat, Key* key, Key* wrap pingKey, const Dictionary& rawWrapAlgorithm) |
214 { | 209 { |
215 if (!key) { | |
216 exceptionState.throwTypeError("Invalid key argument"); | |
217 return ScriptPromise(); | |
218 } | |
219 | |
220 if (!wrappingKey) { | |
221 exceptionState.throwTypeError("Invalid wrappingKey argument"); | |
222 return ScriptPromise(); | |
223 } | |
224 | |
225 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); | 210 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); |
226 ScriptPromise promise = result->promise(); | 211 ScriptPromise promise = result->promise(); |
227 | 212 |
213 if (!ensureNotNull(key, "key", result.get())) | |
214 return promise; | |
215 | |
216 if (!ensureNotNull(wrappingKey, "wrappingKey", result.get())) | |
217 return promise; | |
218 | |
228 blink::WebCryptoKeyFormat format; | 219 blink::WebCryptoKeyFormat format; |
229 if (!Key::parseFormat(rawFormat, format, result.get())) | 220 if (!Key::parseFormat(rawFormat, format, result.get())) |
230 return promise; | 221 return promise; |
231 | 222 |
232 blink::WebCryptoAlgorithm wrapAlgorithm; | 223 blink::WebCryptoAlgorithm wrapAlgorithm; |
233 if (!parseAlgorithm(rawWrapAlgorithm, WrapKey, wrapAlgorithm, exceptionState , result.get())) | 224 if (!parseAlgorithm(rawWrapAlgorithm, WrapKey, wrapAlgorithm, result.get())) |
234 return promise; | 225 return promise; |
235 | 226 |
236 if (!key->extractable()) { | 227 if (!key->extractable()) { |
237 result->completeWithError("key is not extractable"); | 228 result->completeWithError("key is not extractable"); |
238 return promise; | 229 return promise; |
239 } | 230 } |
240 | 231 |
241 if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, WrapKey, result.get() )) | 232 if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, WrapKey, result.get() )) |
242 return promise; | 233 return promise; |
243 | 234 |
244 blink::Platform::current()->crypto()->wrapKey(format, key->key(), wrappingKe y->key(), wrapAlgorithm, result->result()); | 235 blink::Platform::current()->crypto()->wrapKey(format, key->key(), wrappingKe y->key(), wrapAlgorithm, result->result()); |
245 return promise; | 236 return promise; |
246 } | 237 } |
247 | 238 |
248 ScriptPromise SubtleCrypto::unwrapKey(const String& rawFormat, ArrayBufferView* wrappedKey, Key* unwrappingKey, const Dictionary& rawUnwrapAlgorithm, const Dict ionary& rawUnwrappedKeyAlgorithm, bool extractable, const Vector<String>& rawKey Usages, ExceptionState& exceptionState) | 239 ScriptPromise SubtleCrypto::unwrapKey(const String& rawFormat, ArrayBufferView* wrappedKey, Key* unwrappingKey, const Dictionary& rawUnwrapAlgorithm, const Dict ionary& rawUnwrappedKeyAlgorithm, bool extractable, const Vector<String>& rawKey Usages) |
249 { | 240 { |
250 if (!wrappedKey) { | |
251 exceptionState.throwTypeError("Invalid wrappedKey argument"); | |
252 return ScriptPromise(); | |
253 } | |
254 | |
255 if (!unwrappingKey) { | |
256 exceptionState.throwTypeError("Invalid unwrappingKey argument"); | |
257 return ScriptPromise(); | |
258 } | |
259 | |
260 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); | 241 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); |
261 ScriptPromise promise = result->promise(); | 242 ScriptPromise promise = result->promise(); |
262 | 243 |
244 if (!ensureNotNull(wrappedKey, "wrappedKey", result.get())) | |
245 return promise; | |
246 if (!ensureNotNull(unwrappingKey, "unwrappingKey", result.get())) | |
247 return promise; | |
248 | |
263 blink::WebCryptoKeyFormat format; | 249 blink::WebCryptoKeyFormat format; |
264 if (!Key::parseFormat(rawFormat, format, result.get())) | 250 if (!Key::parseFormat(rawFormat, format, result.get())) |
265 return promise; | 251 return promise; |
266 | 252 |
267 blink::WebCryptoKeyUsageMask keyUsages; | 253 blink::WebCryptoKeyUsageMask keyUsages; |
268 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) | 254 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) |
269 return promise; | 255 return promise; |
270 | 256 |
271 blink::WebCryptoAlgorithm unwrapAlgorithm; | 257 blink::WebCryptoAlgorithm unwrapAlgorithm; |
272 if (!parseAlgorithm(rawUnwrapAlgorithm, UnwrapKey, unwrapAlgorithm, exceptio nState, result.get())) | 258 if (!parseAlgorithm(rawUnwrapAlgorithm, UnwrapKey, unwrapAlgorithm, result.g et())) |
273 return promise; | 259 return promise; |
274 | 260 |
275 blink::WebCryptoAlgorithm unwrappedKeyAlgorithm; | 261 blink::WebCryptoAlgorithm unwrappedKeyAlgorithm; |
276 if (!parseAlgorithm(rawUnwrappedKeyAlgorithm, ImportKey, unwrappedKeyAlgorit hm, exceptionState, result.get())) | 262 if (!parseAlgorithm(rawUnwrappedKeyAlgorithm, ImportKey, unwrappedKeyAlgorit hm, result.get())) |
277 return promise; | 263 return promise; |
278 | 264 |
279 if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, UnwrapKey, result .get())) | 265 if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, UnwrapKey, result .get())) |
280 return promise; | 266 return promise; |
281 | 267 |
282 const unsigned char* wrappedKeyData = static_cast<const unsigned char*>(wrap pedKey->baseAddress()); | 268 const unsigned char* wrappedKeyData = static_cast<const unsigned char*>(wrap pedKey->baseAddress()); |
283 unsigned wrappedKeyDataSize = wrappedKey->byteLength(); | 269 unsigned wrappedKeyDataSize = wrappedKey->byteLength(); |
284 | 270 |
285 blink::Platform::current()->crypto()->unwrapKey(format, wrappedKeyData, wrap pedKeyDataSize, unwrappingKey->key(), unwrapAlgorithm, unwrappedKeyAlgorithm, ex tractable, keyUsages, result->result()); | 271 blink::Platform::current()->crypto()->unwrapKey(format, wrappedKeyData, wrap pedKeyDataSize, unwrappingKey->key(), unwrapAlgorithm, unwrappedKeyAlgorithm, ex tractable, keyUsages, result->result()); |
286 return promise; | 272 return promise; |
287 } | 273 } |
288 | 274 |
289 } // namespace WebCore | 275 } // namespace WebCore |
OLD | NEW |