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/NormalizeAlgorithm.h" | 32 #include "modules/crypto/NormalizeAlgorithm.h" |
33 | 33 |
34 #include "bindings/core/v8/Dictionary.h" | 34 #include "bindings/core/v8/Dictionary.h" |
| 35 #include "bindings/core/v8/DictionaryHelper.h" |
35 #include "platform/NotImplemented.h" | 36 #include "platform/NotImplemented.h" |
36 #include "public/platform/WebCryptoAlgorithmParams.h" | 37 #include "public/platform/WebCryptoAlgorithmParams.h" |
37 #include "public/platform/WebString.h" | 38 #include "public/platform/WebString.h" |
38 #include "wtf/ArrayBuffer.h" | 39 #include "wtf/ArrayBuffer.h" |
39 #include "wtf/ArrayBufferView.h" | 40 #include "wtf/ArrayBufferView.h" |
40 #include "wtf/MathExtras.h" | 41 #include "wtf/MathExtras.h" |
41 #include "wtf/Uint8Array.h" | 42 #include "wtf/Uint8Array.h" |
42 #include "wtf/Vector.h" | 43 #include "wtf/Vector.h" |
43 #include "wtf/text/StringBuilder.h" | 44 #include "wtf/text/StringBuilder.h" |
44 #include <algorithm> | 45 #include <algorithm> |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 Vector<const char*, 10> m_messages; | 263 Vector<const char*, 10> m_messages; |
263 }; | 264 }; |
264 | 265 |
265 // Defined by the WebCrypto spec as: | 266 // Defined by the WebCrypto spec as: |
266 // | 267 // |
267 // typedef (ArrayBuffer or ArrayBufferView) CryptoOperationData; | 268 // typedef (ArrayBuffer or ArrayBufferView) CryptoOperationData; |
268 // | 269 // |
269 // FIXME: Currently only supports ArrayBufferView. | 270 // FIXME: Currently only supports ArrayBufferView. |
270 bool getOptionalCryptoOperationData(const Dictionary& raw, const char* propertyN
ame, bool& hasProperty, RefPtr<ArrayBufferView>& buffer, const ErrorContext& con
text, AlgorithmError* error) | 271 bool getOptionalCryptoOperationData(const Dictionary& raw, const char* propertyN
ame, bool& hasProperty, RefPtr<ArrayBufferView>& buffer, const ErrorContext& con
text, AlgorithmError* error) |
271 { | 272 { |
272 if (!raw.get(propertyName, buffer)) { | 273 if (!DictionaryHelper::get(raw, propertyName, buffer)) { |
273 hasProperty = false; | 274 hasProperty = false; |
274 return true; | 275 return true; |
275 } | 276 } |
276 | 277 |
277 hasProperty = true; | 278 hasProperty = true; |
278 | 279 |
279 if (!buffer) { | 280 if (!buffer) { |
280 setSyntaxError(context.toString(propertyName, "Not an ArrayBufferView"),
error); | 281 setSyntaxError(context.toString(propertyName, "Not an ArrayBufferView"),
error); |
281 return false; | 282 return false; |
282 } | 283 } |
(...skipping 12 matching lines...) Expand all Loading... |
295 bool ok = getOptionalCryptoOperationData(raw, propertyName, hasProperty, buf
fer, context, error); | 296 bool ok = getOptionalCryptoOperationData(raw, propertyName, hasProperty, buf
fer, context, error); |
296 if (!hasProperty) { | 297 if (!hasProperty) { |
297 setSyntaxError(context.toString(propertyName, "Missing required property
"), error); | 298 setSyntaxError(context.toString(propertyName, "Missing required property
"), error); |
298 return false; | 299 return false; |
299 } | 300 } |
300 return ok; | 301 return ok; |
301 } | 302 } |
302 | 303 |
303 bool getUint8Array(const Dictionary& raw, const char* propertyName, RefPtr<Uint8
Array>& array, const ErrorContext& context, AlgorithmError* error) | 304 bool getUint8Array(const Dictionary& raw, const char* propertyName, RefPtr<Uint8
Array>& array, const ErrorContext& context, AlgorithmError* error) |
304 { | 305 { |
305 if (!raw.get(propertyName, array) || !array) { | 306 if (!DictionaryHelper::get(raw, propertyName, array) || !array) { |
306 setSyntaxError(context.toString(propertyName, "Missing or not a Uint8Arr
ay"), error); | 307 setSyntaxError(context.toString(propertyName, "Missing or not a Uint8Arr
ay"), error); |
307 return false; | 308 return false; |
308 } | 309 } |
309 return true; | 310 return true; |
310 } | 311 } |
311 | 312 |
312 // Defined by the WebCrypto spec as: | 313 // Defined by the WebCrypto spec as: |
313 // | 314 // |
314 // typedef Uint8Array BigInteger; | 315 // typedef Uint8Array BigInteger; |
315 bool getBigInteger(const Dictionary& raw, const char* propertyName, RefPtr<Uint8
Array>& array, const ErrorContext& context, AlgorithmError* error) | 316 bool getBigInteger(const Dictionary& raw, const char* propertyName, RefPtr<Uint8
Array>& array, const ErrorContext& context, AlgorithmError* error) |
316 { | 317 { |
317 if (!getUint8Array(raw, propertyName, array, context, error)) | 318 if (!getUint8Array(raw, propertyName, array, context, error)) |
318 return false; | 319 return false; |
319 | 320 |
320 if (!array->byteLength()) { | 321 if (!array->byteLength()) { |
321 setSyntaxError(context.toString(propertyName, "BigInteger should not be
empty"), error); | 322 setSyntaxError(context.toString(propertyName, "BigInteger should not be
empty"), error); |
322 return false; | 323 return false; |
323 } | 324 } |
324 | 325 |
325 if (!raw.get(propertyName, array) || !array) { | 326 if (!DictionaryHelper::get(raw, propertyName, array) || !array) { |
326 setSyntaxError(context.toString(propertyName, "Missing or not a Uint8Arr
ay"), error); | 327 setSyntaxError(context.toString(propertyName, "Missing or not a Uint8Arr
ay"), error); |
327 return false; | 328 return false; |
328 } | 329 } |
329 return true; | 330 return true; |
330 } | 331 } |
331 | 332 |
332 // Gets an integer according to WebIDL's [EnforceRange]. | 333 // Gets an integer according to WebIDL's [EnforceRange]. |
333 bool getOptionalInteger(const Dictionary& raw, const char* propertyName, bool& h
asProperty, double& value, double minValue, double maxValue, const ErrorContext&
context, AlgorithmError* error) | 334 bool getOptionalInteger(const Dictionary& raw, const char* propertyName, bool& h
asProperty, double& value, double minValue, double maxValue, const ErrorContext&
context, AlgorithmError* error) |
334 { | 335 { |
335 double number; | 336 double number; |
336 bool ok = raw.get(propertyName, number, hasProperty); | 337 bool ok = DictionaryHelper::get(raw, propertyName, number, hasProperty); |
337 | 338 |
338 if (!hasProperty) | 339 if (!hasProperty) |
339 return true; | 340 return true; |
340 | 341 |
341 if (!ok || std::isnan(number)) { | 342 if (!ok || std::isnan(number)) { |
342 setSyntaxError(context.toString(propertyName, "Is not a number"), error)
; | 343 setSyntaxError(context.toString(propertyName, "Is not a number"), error)
; |
343 return false; | 344 return false; |
344 } | 345 } |
345 | 346 |
346 number = trunc(number); | 347 number = trunc(number); |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 | 439 |
439 params = adoptPtr(new blink::WebCryptoAesKeyGenParams(length)); | 440 params = adoptPtr(new blink::WebCryptoAesKeyGenParams(length)); |
440 return true; | 441 return true; |
441 } | 442 } |
442 | 443 |
443 bool parseAlgorithm(const Dictionary&, blink::WebCryptoOperation, blink::WebCryp
toAlgorithm&, ErrorContext, AlgorithmError*); | 444 bool parseAlgorithm(const Dictionary&, blink::WebCryptoOperation, blink::WebCryp
toAlgorithm&, ErrorContext, AlgorithmError*); |
444 | 445 |
445 bool parseHash(const Dictionary& raw, blink::WebCryptoAlgorithm& hash, ErrorCont
ext context, AlgorithmError* error) | 446 bool parseHash(const Dictionary& raw, blink::WebCryptoAlgorithm& hash, ErrorCont
ext context, AlgorithmError* error) |
446 { | 447 { |
447 Dictionary rawHash; | 448 Dictionary rawHash; |
448 if (!raw.get("hash", rawHash)) { | 449 if (!DictionaryHelper::get(raw, "hash", rawHash)) { |
449 setSyntaxError(context.toString("hash", "Missing or not a dictionary"),
error); | 450 setSyntaxError(context.toString("hash", "Missing or not a dictionary"),
error); |
450 return false; | 451 return false; |
451 } | 452 } |
452 | 453 |
453 context.add("hash"); | 454 context.add("hash"); |
454 return parseAlgorithm(rawHash, blink::WebCryptoOperationDigest, hash, contex
t, error); | 455 return parseAlgorithm(rawHash, blink::WebCryptoOperationDigest, hash, contex
t, error); |
455 } | 456 } |
456 | 457 |
457 // Defined by the WebCrypto spec as: | 458 // Defined by the WebCrypto spec as: |
458 // | 459 // |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
678 bool parseAlgorithm(const Dictionary& raw, blink::WebCryptoOperation op, blink::
WebCryptoAlgorithm& algorithm, ErrorContext context, AlgorithmError* error) | 679 bool parseAlgorithm(const Dictionary& raw, blink::WebCryptoOperation op, blink::
WebCryptoAlgorithm& algorithm, ErrorContext context, AlgorithmError* error) |
679 { | 680 { |
680 context.add("Algorithm"); | 681 context.add("Algorithm"); |
681 | 682 |
682 if (!raw.isObject()) { | 683 if (!raw.isObject()) { |
683 setSyntaxError(context.toString("Not an object"), error); | 684 setSyntaxError(context.toString("Not an object"), error); |
684 return false; | 685 return false; |
685 } | 686 } |
686 | 687 |
687 String algorithmName; | 688 String algorithmName; |
688 if (!raw.get("name", algorithmName)) { | 689 if (!DictionaryHelper::get(raw, "name", algorithmName)) { |
689 setSyntaxError(context.toString("name", "Missing or not a string"), erro
r); | 690 setSyntaxError(context.toString("name", "Missing or not a string"), erro
r); |
690 return false; | 691 return false; |
691 } | 692 } |
692 | 693 |
693 blink::WebCryptoAlgorithmId algorithmId; | 694 blink::WebCryptoAlgorithmId algorithmId; |
694 if (!lookupAlgorithmIdByName(algorithmName, algorithmId)) { | 695 if (!lookupAlgorithmIdByName(algorithmName, algorithmId)) { |
695 // FIXME: The spec says to return a SyntaxError if the input contains | 696 // FIXME: The spec says to return a SyntaxError if the input contains |
696 // any non-ASCII characters. | 697 // any non-ASCII characters. |
697 setNotSupportedError(context.toString("Unrecognized name"), error); | 698 setNotSupportedError(context.toString("Unrecognized name"), error); |
698 return false; | 699 return false; |
(...skipping 21 matching lines...) Expand all Loading... |
720 } | 721 } |
721 | 722 |
722 } // namespace | 723 } // namespace |
723 | 724 |
724 bool normalizeAlgorithm(const Dictionary& raw, blink::WebCryptoOperation op, bli
nk::WebCryptoAlgorithm& algorithm, AlgorithmError* error) | 725 bool normalizeAlgorithm(const Dictionary& raw, blink::WebCryptoOperation op, bli
nk::WebCryptoAlgorithm& algorithm, AlgorithmError* error) |
725 { | 726 { |
726 return parseAlgorithm(raw, op, algorithm, ErrorContext(), error); | 727 return parseAlgorithm(raw, op, algorithm, ErrorContext(), error); |
727 } | 728 } |
728 | 729 |
729 } // namespace WebCore | 730 } // namespace WebCore |
OLD | NEW |