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 "core/dom/DOMTypedArray.h" |
35 #include "public/platform/WebCryptoAlgorithmParams.h" | 36 #include "public/platform/WebCryptoAlgorithmParams.h" |
36 #include "public/platform/WebString.h" | 37 #include "public/platform/WebString.h" |
37 #include "wtf/ArrayBuffer.h" | |
38 #include "wtf/ArrayBufferView.h" | |
39 #include "wtf/MathExtras.h" | 38 #include "wtf/MathExtras.h" |
40 #include "wtf/Uint8Array.h" | |
41 #include "wtf/Vector.h" | 39 #include "wtf/Vector.h" |
42 #include "wtf/text/StringBuilder.h" | 40 #include "wtf/text/StringBuilder.h" |
43 #include <algorithm> | 41 #include <algorithm> |
44 | 42 |
45 namespace blink { | 43 namespace blink { |
46 | 44 |
47 namespace { | 45 namespace { |
48 | 46 |
49 struct AlgorithmNameMapping { | 47 struct AlgorithmNameMapping { |
50 // Must be an upper case ASCII string. | 48 // Must be an upper case ASCII string. |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 // This inline size is large enough to avoid having to grow the Vector in | 257 // This inline size is large enough to avoid having to grow the Vector in |
260 // the majority of cases (up to 1 nested algorithm identifier). | 258 // the majority of cases (up to 1 nested algorithm identifier). |
261 Vector<const char*, 10> m_messages; | 259 Vector<const char*, 10> m_messages; |
262 }; | 260 }; |
263 | 261 |
264 // Defined by the WebCrypto spec as: | 262 // Defined by the WebCrypto spec as: |
265 // | 263 // |
266 // typedef (ArrayBuffer or ArrayBufferView) CryptoOperationData; | 264 // typedef (ArrayBuffer or ArrayBufferView) CryptoOperationData; |
267 // | 265 // |
268 // FIXME: Currently only supports ArrayBufferView. | 266 // FIXME: Currently only supports ArrayBufferView. |
269 bool getOptionalCryptoOperationData(const Dictionary& raw, const char* propertyN
ame, bool& hasProperty, RefPtr<ArrayBufferView>& buffer, const ErrorContext& con
text, AlgorithmError* error) | 267 bool getOptionalCryptoOperationData(const Dictionary& raw, const char* propertyN
ame, bool& hasProperty, RefPtr<DOMArrayBufferView>& buffer, const ErrorContext&
context, AlgorithmError* error) |
270 { | 268 { |
271 if (!DictionaryHelper::get(raw, propertyName, buffer)) { | 269 if (!DictionaryHelper::get(raw, propertyName, buffer)) { |
272 hasProperty = false; | 270 hasProperty = false; |
273 return true; | 271 return true; |
274 } | 272 } |
275 | 273 |
276 hasProperty = true; | 274 hasProperty = true; |
277 | 275 |
278 if (!buffer) { | 276 if (!buffer) { |
279 setSyntaxError(context.toString(propertyName, "Not an ArrayBufferView"),
error); | 277 setSyntaxError(context.toString(propertyName, "Not an ArrayBufferView"),
error); |
280 return false; | 278 return false; |
281 } | 279 } |
282 | 280 |
283 return true; | 281 return true; |
284 } | 282 } |
285 | 283 |
286 // Defined by the WebCrypto spec as: | 284 // Defined by the WebCrypto spec as: |
287 // | 285 // |
288 // typedef (ArrayBuffer or ArrayBufferView) CryptoOperationData; | 286 // typedef (ArrayBuffer or ArrayBufferView) CryptoOperationData; |
289 // | 287 // |
290 // FIXME: Currently only supports ArrayBufferView. | 288 // FIXME: Currently only supports ArrayBufferView. |
291 bool getCryptoOperationData(const Dictionary& raw, const char* propertyName, Ref
Ptr<ArrayBufferView>& buffer, const ErrorContext& context, AlgorithmError* error
) | 289 bool getCryptoOperationData(const Dictionary& raw, const char* propertyName, Ref
Ptr<DOMArrayBufferView>& buffer, const ErrorContext& context, AlgorithmError* er
ror) |
292 { | 290 { |
293 bool hasProperty; | 291 bool hasProperty; |
294 bool ok = getOptionalCryptoOperationData(raw, propertyName, hasProperty, buf
fer, context, error); | 292 bool ok = getOptionalCryptoOperationData(raw, propertyName, hasProperty, buf
fer, context, error); |
295 if (!hasProperty) { | 293 if (!hasProperty) { |
296 setSyntaxError(context.toString(propertyName, "Missing required property
"), error); | 294 setSyntaxError(context.toString(propertyName, "Missing required property
"), error); |
297 return false; | 295 return false; |
298 } | 296 } |
299 return ok; | 297 return ok; |
300 } | 298 } |
301 | 299 |
302 bool getUint8Array(const Dictionary& raw, const char* propertyName, RefPtr<Uint8
Array>& array, const ErrorContext& context, AlgorithmError* error) | 300 bool getUint8Array(const Dictionary& raw, const char* propertyName, RefPtr<DOMUi
nt8Array>& array, const ErrorContext& context, AlgorithmError* error) |
303 { | 301 { |
304 if (!DictionaryHelper::get(raw, propertyName, array) || !array) { | 302 if (!DictionaryHelper::get(raw, propertyName, array) || !array) { |
305 setSyntaxError(context.toString(propertyName, "Missing or not a Uint8Arr
ay"), error); | 303 setSyntaxError(context.toString(propertyName, "Missing or not a Uint8Arr
ay"), error); |
306 return false; | 304 return false; |
307 } | 305 } |
308 return true; | 306 return true; |
309 } | 307 } |
310 | 308 |
311 // Defined by the WebCrypto spec as: | 309 // Defined by the WebCrypto spec as: |
312 // | 310 // |
313 // typedef Uint8Array BigInteger; | 311 // typedef Uint8Array BigInteger; |
314 bool getBigInteger(const Dictionary& raw, const char* propertyName, RefPtr<Uint8
Array>& array, const ErrorContext& context, AlgorithmError* error) | 312 bool getBigInteger(const Dictionary& raw, const char* propertyName, RefPtr<DOMUi
nt8Array>& array, const ErrorContext& context, AlgorithmError* error) |
315 { | 313 { |
316 if (!getUint8Array(raw, propertyName, array, context, error)) | 314 if (!getUint8Array(raw, propertyName, array, context, error)) |
317 return false; | 315 return false; |
318 | 316 |
319 if (!array->byteLength()) { | 317 if (!array->byteLength()) { |
320 // Empty BigIntegers represent 0 according to the spec | 318 // Empty BigIntegers represent 0 according to the spec |
321 array = Uint8Array::create(1); | 319 array = DOMUint8Array::create(1); |
322 } | 320 } |
323 | 321 |
324 return true; | 322 return true; |
325 } | 323 } |
326 | 324 |
327 // Gets an integer according to WebIDL's [EnforceRange]. | 325 // Gets an integer according to WebIDL's [EnforceRange]. |
328 bool getOptionalInteger(const Dictionary& raw, const char* propertyName, bool& h
asProperty, double& value, double minValue, double maxValue, const ErrorContext&
context, AlgorithmError* error) | 326 bool getOptionalInteger(const Dictionary& raw, const char* propertyName, bool& h
asProperty, double& value, double minValue, double maxValue, const ErrorContext&
context, AlgorithmError* error) |
329 { | 327 { |
330 double number; | 328 double number; |
331 bool ok = DictionaryHelper::get(raw, propertyName, number, hasProperty); | 329 bool ok = DictionaryHelper::get(raw, propertyName, number, hasProperty); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 return true; | 398 return true; |
401 } | 399 } |
402 | 400 |
403 // Defined by the WebCrypto spec as: | 401 // Defined by the WebCrypto spec as: |
404 // | 402 // |
405 // dictionary AesCbcParams : Algorithm { | 403 // dictionary AesCbcParams : Algorithm { |
406 // CryptoOperationData iv; | 404 // CryptoOperationData iv; |
407 // }; | 405 // }; |
408 bool parseAesCbcParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithmParams>&
params, const ErrorContext& context, AlgorithmError* error) | 406 bool parseAesCbcParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithmParams>&
params, const ErrorContext& context, AlgorithmError* error) |
409 { | 407 { |
410 RefPtr<ArrayBufferView> iv; | 408 RefPtr<DOMArrayBufferView> iv; |
411 if (!getCryptoOperationData(raw, "iv", iv, context, error)) | 409 if (!getCryptoOperationData(raw, "iv", iv, context, error)) |
412 return false; | 410 return false; |
413 | 411 |
414 if (iv->byteLength() != 16) { | 412 if (iv->byteLength() != 16) { |
415 setDataError(context.toString("iv", "Must be 16 bytes"), error); | 413 setDataError(context.toString("iv", "Must be 16 bytes"), error); |
416 return false; | 414 return false; |
417 } | 415 } |
418 | 416 |
419 params = adoptPtr(new WebCryptoAesCbcParams(static_cast<unsigned char*>(iv->
baseAddress()), iv->byteLength())); | 417 params = adoptPtr(new WebCryptoAesCbcParams(static_cast<unsigned char*>(iv->
baseAddress()), iv->byteLength())); |
420 return true; | 418 return true; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
512 // dictionary RsaKeyGenParams : Algorithm { | 510 // dictionary RsaKeyGenParams : Algorithm { |
513 // unsigned long modulusLength; | 511 // unsigned long modulusLength; |
514 // BigInteger publicExponent; | 512 // BigInteger publicExponent; |
515 // }; | 513 // }; |
516 bool parseRsaHashedKeyGenParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithm
Params>& params, const ErrorContext& context, AlgorithmError* error) | 514 bool parseRsaHashedKeyGenParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithm
Params>& params, const ErrorContext& context, AlgorithmError* error) |
517 { | 515 { |
518 uint32_t modulusLength; | 516 uint32_t modulusLength; |
519 if (!getUint32(raw, "modulusLength", modulusLength, context, error)) | 517 if (!getUint32(raw, "modulusLength", modulusLength, context, error)) |
520 return false; | 518 return false; |
521 | 519 |
522 RefPtr<Uint8Array> publicExponent; | 520 RefPtr<DOMUint8Array> publicExponent; |
523 if (!getBigInteger(raw, "publicExponent", publicExponent, context, error)) | 521 if (!getBigInteger(raw, "publicExponent", publicExponent, context, error)) |
524 return false; | 522 return false; |
525 | 523 |
526 WebCryptoAlgorithm hash; | 524 WebCryptoAlgorithm hash; |
527 if (!parseHash(raw, hash, context, error)) | 525 if (!parseHash(raw, hash, context, error)) |
528 return false; | 526 return false; |
529 | 527 |
530 params = adoptPtr(new WebCryptoRsaHashedKeyGenParams(hash, modulusLength, st
atic_cast<const unsigned char*>(publicExponent->baseAddress()), publicExponent->
byteLength())); | 528 params = adoptPtr(new WebCryptoRsaHashedKeyGenParams(hash, modulusLength, st
atic_cast<const unsigned char*>(publicExponent->baseAddress()), publicExponent->
byteLength())); |
531 return true; | 529 return true; |
532 } | 530 } |
533 | 531 |
534 // Defined by the WebCrypto spec as: | 532 // Defined by the WebCrypto spec as: |
535 // | 533 // |
536 // dictionary AesCtrParams : Algorithm { | 534 // dictionary AesCtrParams : Algorithm { |
537 // CryptoOperationData counter; | 535 // CryptoOperationData counter; |
538 // [EnforceRange] octet length; | 536 // [EnforceRange] octet length; |
539 // }; | 537 // }; |
540 bool parseAesCtrParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithmParams>&
params, const ErrorContext& context, AlgorithmError* error) | 538 bool parseAesCtrParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithmParams>&
params, const ErrorContext& context, AlgorithmError* error) |
541 { | 539 { |
542 RefPtr<ArrayBufferView> counter; | 540 RefPtr<DOMArrayBufferView> counter; |
543 if (!getCryptoOperationData(raw, "counter", counter, context, error)) | 541 if (!getCryptoOperationData(raw, "counter", counter, context, error)) |
544 return false; | 542 return false; |
545 | 543 |
546 uint8_t length; | 544 uint8_t length; |
547 if (!getUint8(raw, "length", length, context, error)) | 545 if (!getUint8(raw, "length", length, context, error)) |
548 return false; | 546 return false; |
549 | 547 |
550 params = adoptPtr(new WebCryptoAesCtrParams(length, static_cast<const unsign
ed char*>(counter->baseAddress()), counter->byteLength())); | 548 params = adoptPtr(new WebCryptoAesCtrParams(length, static_cast<const unsign
ed char*>(counter->baseAddress()), counter->byteLength())); |
551 return true; | 549 return true; |
552 } | 550 } |
553 | 551 |
554 // Defined by the WebCrypto spec as: | 552 // Defined by the WebCrypto spec as: |
555 // | 553 // |
556 // dictionary AesGcmParams : Algorithm { | 554 // dictionary AesGcmParams : Algorithm { |
557 // CryptoOperationData iv; | 555 // CryptoOperationData iv; |
558 // CryptoOperationData? additionalData; | 556 // CryptoOperationData? additionalData; |
559 // [EnforceRange] octet? tagLength; // May be 0-128 | 557 // [EnforceRange] octet? tagLength; // May be 0-128 |
560 // } | 558 // } |
561 bool parseAesGcmParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithmParams>&
params, const ErrorContext& context, AlgorithmError* error) | 559 bool parseAesGcmParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithmParams>&
params, const ErrorContext& context, AlgorithmError* error) |
562 { | 560 { |
563 RefPtr<ArrayBufferView> iv; | 561 RefPtr<DOMArrayBufferView> iv; |
564 if (!getCryptoOperationData(raw, "iv", iv, context, error)) | 562 if (!getCryptoOperationData(raw, "iv", iv, context, error)) |
565 return false; | 563 return false; |
566 | 564 |
567 bool hasAdditionalData; | 565 bool hasAdditionalData; |
568 RefPtr<ArrayBufferView> additionalData; | 566 RefPtr<DOMArrayBufferView> additionalData; |
569 if (!getOptionalCryptoOperationData(raw, "additionalData", hasAdditionalData
, additionalData, context, error)) | 567 if (!getOptionalCryptoOperationData(raw, "additionalData", hasAdditionalData
, additionalData, context, error)) |
570 return false; | 568 return false; |
571 | 569 |
572 double tagLength; | 570 double tagLength; |
573 bool hasTagLength; | 571 bool hasTagLength; |
574 if (!getOptionalInteger(raw, "tagLength", hasTagLength, tagLength, 0, 128, c
ontext, error)) | 572 if (!getOptionalInteger(raw, "tagLength", hasTagLength, tagLength, 0, 128, c
ontext, error)) |
575 return false; | 573 return false; |
576 | 574 |
577 const unsigned char* ivStart = static_cast<const unsigned char*>(iv->baseAdd
ress()); | 575 const unsigned char* ivStart = static_cast<const unsigned char*>(iv->baseAdd
ress()); |
578 unsigned ivLength = iv->byteLength(); | 576 unsigned ivLength = iv->byteLength(); |
579 | 577 |
580 const unsigned char* additionalDataStart = hasAdditionalData ? static_cast<c
onst unsigned char*>(additionalData->baseAddress()) : 0; | 578 const unsigned char* additionalDataStart = hasAdditionalData ? static_cast<c
onst unsigned char*>(additionalData->baseAddress()) : 0; |
581 unsigned additionalDataLength = hasAdditionalData ? additionalData->byteLeng
th() : 0; | 579 unsigned additionalDataLength = hasAdditionalData ? additionalData->byteLeng
th() : 0; |
582 | 580 |
583 params = adoptPtr(new WebCryptoAesGcmParams(ivStart, ivLength, hasAdditional
Data, additionalDataStart, additionalDataLength, hasTagLength, tagLength)); | 581 params = adoptPtr(new WebCryptoAesGcmParams(ivStart, ivLength, hasAdditional
Data, additionalDataStart, additionalDataLength, hasTagLength, tagLength)); |
584 return true; | 582 return true; |
585 } | 583 } |
586 | 584 |
587 // Defined by the WebCrypto spec as: | 585 // Defined by the WebCrypto spec as: |
588 // | 586 // |
589 // dictionary RsaOaepParams : Algorithm { | 587 // dictionary RsaOaepParams : Algorithm { |
590 // CryptoOperationData? label; | 588 // CryptoOperationData? label; |
591 // }; | 589 // }; |
592 bool parseRsaOaepParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithmParams>&
params, const ErrorContext& context, AlgorithmError* error) | 590 bool parseRsaOaepParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithmParams>&
params, const ErrorContext& context, AlgorithmError* error) |
593 { | 591 { |
594 bool hasLabel; | 592 bool hasLabel; |
595 RefPtr<ArrayBufferView> label; | 593 RefPtr<DOMArrayBufferView> label; |
596 if (!getOptionalCryptoOperationData(raw, "label", hasLabel, label, context,
error)) | 594 if (!getOptionalCryptoOperationData(raw, "label", hasLabel, label, context,
error)) |
597 return false; | 595 return false; |
598 | 596 |
599 const unsigned char* labelStart = hasLabel ? static_cast<const unsigned char
*>(label->baseAddress()) : 0; | 597 const unsigned char* labelStart = hasLabel ? static_cast<const unsigned char
*>(label->baseAddress()) : 0; |
600 unsigned labelLength = hasLabel ? label->byteLength() : 0; | 598 unsigned labelLength = hasLabel ? label->byteLength() : 0; |
601 | 599 |
602 params = adoptPtr(new WebCryptoRsaOaepParams(hasLabel, labelStart, labelLeng
th)); | 600 params = adoptPtr(new WebCryptoRsaOaepParams(hasLabel, labelStart, labelLeng
th)); |
603 return true; | 601 return true; |
604 } | 602 } |
605 | 603 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
732 } | 730 } |
733 | 731 |
734 } // namespace | 732 } // namespace |
735 | 733 |
736 bool normalizeAlgorithm(const Dictionary& raw, WebCryptoOperation op, WebCryptoA
lgorithm& algorithm, AlgorithmError* error) | 734 bool normalizeAlgorithm(const Dictionary& raw, WebCryptoOperation op, WebCryptoA
lgorithm& algorithm, AlgorithmError* error) |
737 { | 735 { |
738 return parseAlgorithm(raw, op, algorithm, ErrorContext(), error); | 736 return parseAlgorithm(raw, op, algorithm, ErrorContext(), error); |
739 } | 737 } |
740 | 738 |
741 } // namespace blink | 739 } // namespace blink |
OLD | NEW |