| 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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 #if ENABLE(ASSERT) | 54 #if ENABLE(ASSERT) |
| 55 bool operator<(const AlgorithmNameMapping&) const; | 55 bool operator<(const AlgorithmNameMapping&) const; |
| 56 #endif | 56 #endif |
| 57 }; | 57 }; |
| 58 | 58 |
| 59 // Must be sorted by length, and then by reverse string. | 59 // Must be sorted by length, and then by reverse string. |
| 60 // Also all names must be upper case ASCII. | 60 // Also all names must be upper case ASCII. |
| 61 const AlgorithmNameMapping algorithmNameMappings[] = { | 61 const AlgorithmNameMapping algorithmNameMappings[] = { |
| 62 {"HMAC", 4, WebCryptoAlgorithmIdHmac}, | 62 {"HMAC", 4, WebCryptoAlgorithmIdHmac}, |
| 63 {"SHA-1", 5, WebCryptoAlgorithmIdSha1}, | 63 {"SHA-1", 5, WebCryptoAlgorithmIdSha1}, |
| 64 {"ECDSA", 5, WebCryptoAlgorithmIdEcdsa}, |
| 64 {"AES-KW", 6, WebCryptoAlgorithmIdAesKw}, | 65 {"AES-KW", 6, WebCryptoAlgorithmIdAesKw}, |
| 65 {"SHA-512", 7, WebCryptoAlgorithmIdSha512}, | 66 {"SHA-512", 7, WebCryptoAlgorithmIdSha512}, |
| 66 {"SHA-384", 7, WebCryptoAlgorithmIdSha384}, | 67 {"SHA-384", 7, WebCryptoAlgorithmIdSha384}, |
| 67 {"SHA-256", 7, WebCryptoAlgorithmIdSha256}, | 68 {"SHA-256", 7, WebCryptoAlgorithmIdSha256}, |
| 68 {"AES-CBC", 7, WebCryptoAlgorithmIdAesCbc}, | 69 {"AES-CBC", 7, WebCryptoAlgorithmIdAesCbc}, |
| 69 {"AES-GCM", 7, WebCryptoAlgorithmIdAesGcm}, | 70 {"AES-GCM", 7, WebCryptoAlgorithmIdAesGcm}, |
| 70 {"AES-CTR", 7, WebCryptoAlgorithmIdAesCtr}, | 71 {"AES-CTR", 7, WebCryptoAlgorithmIdAesCtr}, |
| 71 {"RSA-PSS", 7, WebCryptoAlgorithmIdRsaPss}, | 72 {"RSA-PSS", 7, WebCryptoAlgorithmIdRsaPss}, |
| 72 {"RSA-OAEP", 8, WebCryptoAlgorithmIdRsaOaep}, | 73 {"RSA-OAEP", 8, WebCryptoAlgorithmIdRsaOaep}, |
| 73 {"RSASSA-PKCS1-V1_5", 17, WebCryptoAlgorithmIdRsaSsaPkcs1v1_5}, | 74 {"RSASSA-PKCS1-V1_5", 17, WebCryptoAlgorithmIdRsaSsaPkcs1v1_5}, |
| (...skipping 540 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 bool parseRsaPssParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithmParams>&
params, const ErrorContext& context, AlgorithmError* error) | 615 bool parseRsaPssParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithmParams>&
params, const ErrorContext& context, AlgorithmError* error) |
| 615 { | 616 { |
| 616 uint32_t saltLengthBytes; | 617 uint32_t saltLengthBytes; |
| 617 if (!getUint32(raw, "saltLength", saltLengthBytes, context, error)) | 618 if (!getUint32(raw, "saltLength", saltLengthBytes, context, error)) |
| 618 return false; | 619 return false; |
| 619 | 620 |
| 620 params = adoptPtr(new WebCryptoRsaPssParams(saltLengthBytes)); | 621 params = adoptPtr(new WebCryptoRsaPssParams(saltLengthBytes)); |
| 621 return true; | 622 return true; |
| 622 } | 623 } |
| 623 | 624 |
| 625 // Defined by the WebCrypto spec as: |
| 626 // |
| 627 // dictionary EcdsaParams : Algorithm { |
| 628 // required HashAlgorithmIdentifier hash; |
| 629 // }; |
| 630 bool parseEcdsaParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithmParams>& p
arams, const ErrorContext& context, AlgorithmError* error) |
| 631 { |
| 632 WebCryptoAlgorithm hash; |
| 633 if (!parseHash(raw, hash, context, error)) |
| 634 return false; |
| 635 |
| 636 params = adoptPtr(new WebCryptoEcdsaParams(hash)); |
| 637 return true; |
| 638 } |
| 639 |
| 640 struct CurveNameMapping { |
| 641 const char* const name; |
| 642 WebCryptoNamedCurve value; |
| 643 }; |
| 644 |
| 645 const CurveNameMapping curveNameMappings[] = { |
| 646 { "P-256", WebCryptoNamedCurveP256 }, |
| 647 { "P-384", WebCryptoNamedCurveP384 }, |
| 648 { "P-521", WebCryptoNamedCurveP521 } |
| 649 }; |
| 650 |
| 651 // Reminder to update curveNameMappings when adding a new curve. |
| 652 COMPILE_ASSERT(WebCryptoNamedCurveLast + 1 == WTF_ARRAY_LENGTH(curveNameMappings
), UPDATE_curveNameMappings); |
| 653 |
| 654 bool parseNamedCurve(const Dictionary& raw, WebCryptoNamedCurve& namedCurve, Err
orContext context, AlgorithmError* error) |
| 655 { |
| 656 String namedCurveString; |
| 657 if (!DictionaryHelper::get(raw, "namedCurve", namedCurveString)) { |
| 658 setSyntaxError(context.toString("namedCurve", "Missing or not a string")
, error); |
| 659 return false; |
| 660 } |
| 661 |
| 662 for (size_t i = 0; i < WTF_ARRAY_LENGTH(curveNameMappings); ++i) { |
| 663 if (curveNameMappings[i].name == namedCurveString) { |
| 664 namedCurve = curveNameMappings[i].value; |
| 665 return true; |
| 666 } |
| 667 } |
| 668 |
| 669 setNotSupportedError(context.toString("Unrecognized namedCurve"), error); |
| 670 return false; |
| 671 } |
| 672 |
| 673 // Defined by the WebCrypto spec as: |
| 674 // |
| 675 // dictionary EcKeyGenParams : Algorithm { |
| 676 // required NamedCurve namedCurve; |
| 677 // }; |
| 678 bool parseEcKeyGenParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithmParams>
& params, const ErrorContext& context, AlgorithmError* error) |
| 679 { |
| 680 WebCryptoNamedCurve namedCurve; |
| 681 if (!parseNamedCurve(raw, namedCurve, context, error)) |
| 682 return false; |
| 683 |
| 684 params = adoptPtr(new WebCryptoEcKeyGenParams(namedCurve)); |
| 685 return true; |
| 686 } |
| 687 |
| 688 // Defined by the WebCrypto spec as: |
| 689 // |
| 690 // dictionary EcKeyImportParams : Algorithm { |
| 691 // required NamedCurve namedCurve; |
| 692 // }; |
| 693 bool parseEcKeyImportParams(const Dictionary& raw, OwnPtr<WebCryptoAlgorithmPara
ms>& params, const ErrorContext& context, AlgorithmError* error) |
| 694 { |
| 695 WebCryptoNamedCurve namedCurve; |
| 696 if (!parseNamedCurve(raw, namedCurve, context, error)) |
| 697 return false; |
| 698 |
| 699 params = adoptPtr(new WebCryptoEcKeyImportParams(namedCurve)); |
| 700 return true; |
| 701 } |
| 702 |
| 624 bool parseAlgorithmParams(const Dictionary& raw, WebCryptoAlgorithmParamsType ty
pe, OwnPtr<WebCryptoAlgorithmParams>& params, ErrorContext& context, AlgorithmEr
ror* error) | 703 bool parseAlgorithmParams(const Dictionary& raw, WebCryptoAlgorithmParamsType ty
pe, OwnPtr<WebCryptoAlgorithmParams>& params, ErrorContext& context, AlgorithmEr
ror* error) |
| 625 { | 704 { |
| 626 switch (type) { | 705 switch (type) { |
| 627 case WebCryptoAlgorithmParamsTypeNone: | 706 case WebCryptoAlgorithmParamsTypeNone: |
| 628 return true; | 707 return true; |
| 629 case WebCryptoAlgorithmParamsTypeAesCbcParams: | 708 case WebCryptoAlgorithmParamsTypeAesCbcParams: |
| 630 context.add("AesCbcParams"); | 709 context.add("AesCbcParams"); |
| 631 return parseAesCbcParams(raw, params, context, error); | 710 return parseAesCbcParams(raw, params, context, error); |
| 632 case WebCryptoAlgorithmParamsTypeAesKeyGenParams: | 711 case WebCryptoAlgorithmParamsTypeAesKeyGenParams: |
| 633 context.add("AesKeyGenParams"); | 712 context.add("AesKeyGenParams"); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 649 return parseAesCtrParams(raw, params, context, error); | 728 return parseAesCtrParams(raw, params, context, error); |
| 650 case WebCryptoAlgorithmParamsTypeAesGcmParams: | 729 case WebCryptoAlgorithmParamsTypeAesGcmParams: |
| 651 context.add("AesGcmParams"); | 730 context.add("AesGcmParams"); |
| 652 return parseAesGcmParams(raw, params, context, error); | 731 return parseAesGcmParams(raw, params, context, error); |
| 653 case WebCryptoAlgorithmParamsTypeRsaOaepParams: | 732 case WebCryptoAlgorithmParamsTypeRsaOaepParams: |
| 654 context.add("RsaOaepParams"); | 733 context.add("RsaOaepParams"); |
| 655 return parseRsaOaepParams(raw, params, context, error); | 734 return parseRsaOaepParams(raw, params, context, error); |
| 656 case WebCryptoAlgorithmParamsTypeRsaPssParams: | 735 case WebCryptoAlgorithmParamsTypeRsaPssParams: |
| 657 context.add("RsaPssParams"); | 736 context.add("RsaPssParams"); |
| 658 return parseRsaPssParams(raw, params, context, error); | 737 return parseRsaPssParams(raw, params, context, error); |
| 738 case WebCryptoAlgorithmParamsTypeEcdsaParams: |
| 739 context.add("EcdsaParams"); |
| 740 return parseEcdsaParams(raw, params, context, error); |
| 741 case WebCryptoAlgorithmParamsTypeEcKeyGenParams: |
| 742 context.add("EcKeyGenParams"); |
| 743 return parseEcKeyGenParams(raw, params, context, error); |
| 744 case WebCryptoAlgorithmParamsTypeEcKeyImportParams: |
| 745 context.add("EcKeyImportParams"); |
| 746 return parseEcKeyImportParams(raw, params, context, error); |
| 659 } | 747 } |
| 660 ASSERT_NOT_REACHED(); | 748 ASSERT_NOT_REACHED(); |
| 661 return false; | 749 return false; |
| 662 } | 750 } |
| 663 | 751 |
| 664 const char* operationToString(WebCryptoOperation op) | 752 const char* operationToString(WebCryptoOperation op) |
| 665 { | 753 { |
| 666 switch (op) { | 754 switch (op) { |
| 667 case WebCryptoOperationEncrypt: | 755 case WebCryptoOperationEncrypt: |
| 668 return "encrypt"; | 756 return "encrypt"; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 735 } | 823 } |
| 736 | 824 |
| 737 } // namespace | 825 } // namespace |
| 738 | 826 |
| 739 bool normalizeAlgorithm(const Dictionary& raw, WebCryptoOperation op, WebCryptoA
lgorithm& algorithm, AlgorithmError* error) | 827 bool normalizeAlgorithm(const Dictionary& raw, WebCryptoOperation op, WebCryptoA
lgorithm& algorithm, AlgorithmError* error) |
| 740 { | 828 { |
| 741 return parseAlgorithm(raw, op, algorithm, ErrorContext(), error); | 829 return parseAlgorithm(raw, op, algorithm, ErrorContext(), error); |
| 742 } | 830 } |
| 743 | 831 |
| 744 } // namespace blink | 832 } // namespace blink |
| OLD | NEW |