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