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 setSyntaxError(context.toString(propertyName, "BigInteger should not be
empty"), error); | 318 setSyntaxError(context.toString(propertyName, "BigInteger should not be
empty"), error); |
321 return false; | 319 return false; |
322 } | 320 } |
323 | 321 |
324 if (!DictionaryHelper::get(raw, propertyName, array) || !array) { | 322 if (!DictionaryHelper::get(raw, propertyName, array) || !array) { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 return true; | 402 return true; |
405 } | 403 } |
406 | 404 |
407 // Defined by the WebCrypto spec as: | 405 // Defined by the WebCrypto spec as: |
408 // | 406 // |
409 // dictionary AesCbcParams : Algorithm { | 407 // dictionary AesCbcParams : Algorithm { |
410 // CryptoOperationData iv; | 408 // CryptoOperationData iv; |
411 // }; | 409 // }; |
412 bool parseAesCbcParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithmParams>&
params, const ErrorContext& context, AlgorithmError* error) | 410 bool parseAesCbcParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithmParams>&
params, const ErrorContext& context, AlgorithmError* error) |
413 { | 411 { |
414 RefPtr<ArrayBufferView> iv; | 412 RefPtr<DOMArrayBufferView> iv; |
415 if (!getCryptoOperationData(raw, "iv", iv, context, error)) | 413 if (!getCryptoOperationData(raw, "iv", iv, context, error)) |
416 return false; | 414 return false; |
417 | 415 |
418 if (iv->byteLength() != 16) { | 416 if (iv->byteLength() != 16) { |
419 setDataError(context.toString("iv", "Must be 16 bytes"), error); | 417 setDataError(context.toString("iv", "Must be 16 bytes"), error); |
420 return false; | 418 return false; |
421 } | 419 } |
422 | 420 |
423 params = adoptPtr(new WebCryptoAesCbcParams(static_cast<unsigned char*>(iv->
baseAddress()), iv->byteLength())); | 421 params = adoptPtr(new WebCryptoAesCbcParams(static_cast<unsigned char*>(iv->
baseAddress()), iv->byteLength())); |
424 return true; | 422 return true; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
516 // dictionary RsaKeyGenParams : Algorithm { | 514 // dictionary RsaKeyGenParams : Algorithm { |
517 // unsigned long modulusLength; | 515 // unsigned long modulusLength; |
518 // BigInteger publicExponent; | 516 // BigInteger publicExponent; |
519 // }; | 517 // }; |
520 bool parseRsaHashedKeyGenParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithm
Params>& params, const ErrorContext& context, AlgorithmError* error) | 518 bool parseRsaHashedKeyGenParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithm
Params>& params, const ErrorContext& context, AlgorithmError* error) |
521 { | 519 { |
522 uint32_t modulusLength; | 520 uint32_t modulusLength; |
523 if (!getUint32(raw, "modulusLength", modulusLength, context, error)) | 521 if (!getUint32(raw, "modulusLength", modulusLength, context, error)) |
524 return false; | 522 return false; |
525 | 523 |
526 RefPtr<Uint8Array> publicExponent; | 524 RefPtr<DOMUint8Array> publicExponent; |
527 if (!getBigInteger(raw, "publicExponent", publicExponent, context, error)) | 525 if (!getBigInteger(raw, "publicExponent", publicExponent, context, error)) |
528 return false; | 526 return false; |
529 | 527 |
530 WebCryptoAlgorithm hash; | 528 WebCryptoAlgorithm hash; |
531 if (!parseHash(raw, hash, context, error)) | 529 if (!parseHash(raw, hash, context, error)) |
532 return false; | 530 return false; |
533 | 531 |
534 params = adoptPtr(new WebCryptoRsaHashedKeyGenParams(hash, modulusLength, st
atic_cast<const unsigned char*>(publicExponent->baseAddress()), publicExponent->
byteLength())); | 532 params = adoptPtr(new WebCryptoRsaHashedKeyGenParams(hash, modulusLength, st
atic_cast<const unsigned char*>(publicExponent->baseAddress()), publicExponent->
byteLength())); |
535 return true; | 533 return true; |
536 } | 534 } |
537 | 535 |
538 // Defined by the WebCrypto spec as: | 536 // Defined by the WebCrypto spec as: |
539 // | 537 // |
540 // dictionary AesCtrParams : Algorithm { | 538 // dictionary AesCtrParams : Algorithm { |
541 // CryptoOperationData counter; | 539 // CryptoOperationData counter; |
542 // [EnforceRange] octet length; | 540 // [EnforceRange] octet length; |
543 // }; | 541 // }; |
544 bool parseAesCtrParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithmParams>&
params, const ErrorContext& context, AlgorithmError* error) | 542 bool parseAesCtrParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithmParams>&
params, const ErrorContext& context, AlgorithmError* error) |
545 { | 543 { |
546 RefPtr<ArrayBufferView> counter; | 544 RefPtr<DOMArrayBufferView> counter; |
547 if (!getCryptoOperationData(raw, "counter", counter, context, error)) | 545 if (!getCryptoOperationData(raw, "counter", counter, context, error)) |
548 return false; | 546 return false; |
549 | 547 |
550 uint8_t length; | 548 uint8_t length; |
551 if (!getUint8(raw, "length", length, context, error)) | 549 if (!getUint8(raw, "length", length, context, error)) |
552 return false; | 550 return false; |
553 | 551 |
554 params = adoptPtr(new WebCryptoAesCtrParams(length, static_cast<const unsign
ed char*>(counter->baseAddress()), counter->byteLength())); | 552 params = adoptPtr(new WebCryptoAesCtrParams(length, static_cast<const unsign
ed char*>(counter->baseAddress()), counter->byteLength())); |
555 return true; | 553 return true; |
556 } | 554 } |
557 | 555 |
558 // Defined by the WebCrypto spec as: | 556 // Defined by the WebCrypto spec as: |
559 // | 557 // |
560 // dictionary AesGcmParams : Algorithm { | 558 // dictionary AesGcmParams : Algorithm { |
561 // CryptoOperationData iv; | 559 // CryptoOperationData iv; |
562 // CryptoOperationData? additionalData; | 560 // CryptoOperationData? additionalData; |
563 // [EnforceRange] octet? tagLength; // May be 0-128 | 561 // [EnforceRange] octet? tagLength; // May be 0-128 |
564 // } | 562 // } |
565 bool parseAesGcmParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithmParams>&
params, const ErrorContext& context, AlgorithmError* error) | 563 bool parseAesGcmParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithmParams>&
params, const ErrorContext& context, AlgorithmError* error) |
566 { | 564 { |
567 RefPtr<ArrayBufferView> iv; | 565 RefPtr<DOMArrayBufferView> iv; |
568 if (!getCryptoOperationData(raw, "iv", iv, context, error)) | 566 if (!getCryptoOperationData(raw, "iv", iv, context, error)) |
569 return false; | 567 return false; |
570 | 568 |
571 bool hasAdditionalData; | 569 bool hasAdditionalData; |
572 RefPtr<ArrayBufferView> additionalData; | 570 RefPtr<DOMArrayBufferView> additionalData; |
573 if (!getOptionalCryptoOperationData(raw, "additionalData", hasAdditionalData
, additionalData, context, error)) | 571 if (!getOptionalCryptoOperationData(raw, "additionalData", hasAdditionalData
, additionalData, context, error)) |
574 return false; | 572 return false; |
575 | 573 |
576 double tagLength; | 574 double tagLength; |
577 bool hasTagLength; | 575 bool hasTagLength; |
578 if (!getOptionalInteger(raw, "tagLength", hasTagLength, tagLength, 0, 128, c
ontext, error)) | 576 if (!getOptionalInteger(raw, "tagLength", hasTagLength, tagLength, 0, 128, c
ontext, error)) |
579 return false; | 577 return false; |
580 | 578 |
581 const unsigned char* ivStart = static_cast<const unsigned char*>(iv->baseAdd
ress()); | 579 const unsigned char* ivStart = static_cast<const unsigned char*>(iv->baseAdd
ress()); |
582 unsigned ivLength = iv->byteLength(); | 580 unsigned ivLength = iv->byteLength(); |
583 | 581 |
584 const unsigned char* additionalDataStart = hasAdditionalData ? static_cast<c
onst unsigned char*>(additionalData->baseAddress()) : 0; | 582 const unsigned char* additionalDataStart = hasAdditionalData ? static_cast<c
onst unsigned char*>(additionalData->baseAddress()) : 0; |
585 unsigned additionalDataLength = hasAdditionalData ? additionalData->byteLeng
th() : 0; | 583 unsigned additionalDataLength = hasAdditionalData ? additionalData->byteLeng
th() : 0; |
586 | 584 |
587 params = adoptPtr(new WebCryptoAesGcmParams(ivStart, ivLength, hasAdditional
Data, additionalDataStart, additionalDataLength, hasTagLength, tagLength)); | 585 params = adoptPtr(new WebCryptoAesGcmParams(ivStart, ivLength, hasAdditional
Data, additionalDataStart, additionalDataLength, hasTagLength, tagLength)); |
588 return true; | 586 return true; |
589 } | 587 } |
590 | 588 |
591 // Defined by the WebCrypto spec as: | 589 // Defined by the WebCrypto spec as: |
592 // | 590 // |
593 // dictionary RsaOaepParams : Algorithm { | 591 // dictionary RsaOaepParams : Algorithm { |
594 // CryptoOperationData? label; | 592 // CryptoOperationData? label; |
595 // }; | 593 // }; |
596 bool parseRsaOaepParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithmParams>&
params, const ErrorContext& context, AlgorithmError* error) | 594 bool parseRsaOaepParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithmParams>&
params, const ErrorContext& context, AlgorithmError* error) |
597 { | 595 { |
598 bool hasLabel; | 596 bool hasLabel; |
599 RefPtr<ArrayBufferView> label; | 597 RefPtr<DOMArrayBufferView> label; |
600 if (!getOptionalCryptoOperationData(raw, "label", hasLabel, label, context,
error)) | 598 if (!getOptionalCryptoOperationData(raw, "label", hasLabel, label, context,
error)) |
601 return false; | 599 return false; |
602 | 600 |
603 const unsigned char* labelStart = hasLabel ? static_cast<const unsigned char
*>(label->baseAddress()) : 0; | 601 const unsigned char* labelStart = hasLabel ? static_cast<const unsigned char
*>(label->baseAddress()) : 0; |
604 unsigned labelLength = hasLabel ? label->byteLength() : 0; | 602 unsigned labelLength = hasLabel ? label->byteLength() : 0; |
605 | 603 |
606 params = adoptPtr(new WebCryptoRsaOaepParams(hasLabel, labelStart, labelLeng
th)); | 604 params = adoptPtr(new WebCryptoRsaOaepParams(hasLabel, labelStart, labelLeng
th)); |
607 return true; | 605 return true; |
608 } | 606 } |
609 | 607 |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
719 } | 717 } |
720 | 718 |
721 } // namespace | 719 } // namespace |
722 | 720 |
723 bool normalizeAlgorithm(const Dictionary& raw, WebCryptoOperation op, WebCryptoA
lgorithm& algorithm, AlgorithmError* error) | 721 bool normalizeAlgorithm(const Dictionary& raw, WebCryptoOperation op, WebCryptoA
lgorithm& algorithm, AlgorithmError* error) |
724 { | 722 { |
725 return parseAlgorithm(raw, op, algorithm, ErrorContext(), error); | 723 return parseAlgorithm(raw, op, algorithm, ErrorContext(), error); |
726 } | 724 } |
727 | 725 |
728 } // namespace blink | 726 } // namespace blink |
OLD | NEW |