| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/webcrypto/webcrypto_util.h" | 5 #include "content/renderer/webcrypto/webcrypto_util.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 9 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| 10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 11 | 11 |
| 12 namespace content { | 12 namespace content { |
| 13 | 13 |
| 14 namespace webcrypto { | 14 namespace webcrypto { |
| 15 | 15 |
| 16 bool Status::IsError() const { | 16 bool Status::IsError() const { return type_ == TYPE_ERROR; } |
| 17 return type_ == TYPE_ERROR; | |
| 18 } | |
| 19 | 17 |
| 20 bool Status::IsSuccess() const { | 18 bool Status::IsSuccess() const { return type_ == TYPE_SUCCESS; } |
| 21 return type_ == TYPE_SUCCESS; | |
| 22 } | |
| 23 | 19 |
| 24 bool Status::HasErrorDetails() const { | 20 bool Status::HasErrorDetails() const { return !error_details_.empty(); } |
| 25 return !error_details_.empty(); | |
| 26 } | |
| 27 | 21 |
| 28 std::string Status::ToString() const { | 22 std::string Status::ToString() const { |
| 29 return IsSuccess() ? "Success" : error_details_; | 23 return IsSuccess() ? "Success" : error_details_; |
| 30 } | 24 } |
| 31 | 25 |
| 32 Status Status::Success() { | 26 Status Status::Success() { return Status(TYPE_SUCCESS); } |
| 33 return Status(TYPE_SUCCESS); | |
| 34 } | |
| 35 | 27 |
| 36 Status Status::Error() { | 28 Status Status::Error() { return Status(TYPE_ERROR); } |
| 37 return Status(TYPE_ERROR); | |
| 38 } | |
| 39 | 29 |
| 40 Status Status::ErrorJwkNotDictionary() { | 30 Status Status::ErrorJwkNotDictionary() { |
| 41 return Status("JWK input could not be parsed to a JSON dictionary"); | 31 return Status("JWK input could not be parsed to a JSON dictionary"); |
| 42 } | 32 } |
| 43 | 33 |
| 44 Status Status::ErrorJwkPropertyMissing(const std::string& property) { | 34 Status Status::ErrorJwkPropertyMissing(const std::string& property) { |
| 45 return Status("The required JWK property \"" + property + "\" was missing"); | 35 return Status("The required JWK property \"" + property + "\" was missing"); |
| 46 } | 36 } |
| 47 | 37 |
| 48 Status Status::ErrorJwkPropertyWrongType(const std::string& property, | 38 Status Status::ErrorJwkPropertyWrongType(const std::string& property, |
| 49 const std::string& expected_type) { | 39 const std::string& expected_type) { |
| 50 return Status("The JWK property \"" + property + "\" must be a " + | 40 return Status("The JWK property \"" + property + "\" must be a " + |
| 51 expected_type); | 41 expected_type); |
| 52 } | 42 } |
| 53 | 43 |
| 54 Status Status::ErrorJwkBase64Decode(const std::string& property) { | 44 Status Status::ErrorJwkBase64Decode(const std::string& property) { |
| 55 return Status("The JWK property \"" + property + | 45 return Status("The JWK property \"" + property + |
| 56 "\" could not be base64 decoded"); | 46 "\" could not be base64 decoded"); |
| 57 } | 47 } |
| 58 | 48 |
| 59 Status Status::ErrorJwkExtractableInconsistent() { | 49 Status Status::ErrorJwkExtractableInconsistent() { |
| 60 return Status("The \"extractable\" property of the JWK dictionary is " | 50 return Status( |
| 61 "inconsistent what that specified by the Web Crypto call"); | 51 "The \"extractable\" property of the JWK dictionary is " |
| 52 "inconsistent what that specified by the Web Crypto call"); |
| 62 } | 53 } |
| 63 | 54 |
| 64 Status Status::ErrorJwkUnrecognizedAlgorithm() { | 55 Status Status::ErrorJwkUnrecognizedAlgorithm() { |
| 65 return Status("The JWK \"alg\" property was not recognized"); | 56 return Status("The JWK \"alg\" property was not recognized"); |
| 66 } | 57 } |
| 67 | 58 |
| 68 Status Status::ErrorJwkAlgorithmInconsistent() { | 59 Status Status::ErrorJwkAlgorithmInconsistent() { |
| 69 return Status("The JWK \"alg\" property was inconsistent with that specified " | 60 return Status( |
| 70 "by the Web Crypto call"); | 61 "The JWK \"alg\" property was inconsistent with that specified " |
| 62 "by the Web Crypto call"); |
| 71 } | 63 } |
| 72 | 64 |
| 73 Status Status::ErrorJwkAlgorithmMissing() { | 65 Status Status::ErrorJwkAlgorithmMissing() { |
| 74 return Status("The JWK optional \"alg\" property is missing or not a string, " | 66 return Status( |
| 75 "and one wasn't specified by the Web Crypto call"); | 67 "The JWK optional \"alg\" property is missing or not a string, " |
| 68 "and one wasn't specified by the Web Crypto call"); |
| 76 } | 69 } |
| 77 | 70 |
| 78 Status Status::ErrorJwkUnrecognizedUsage() { | 71 Status Status::ErrorJwkUnrecognizedUsage() { |
| 79 return Status("The JWK \"use\" property could not be parsed"); | 72 return Status("The JWK \"use\" property could not be parsed"); |
| 80 } | 73 } |
| 81 | 74 |
| 82 Status Status::ErrorJwkUsageInconsistent() { | 75 Status Status::ErrorJwkUsageInconsistent() { |
| 83 return Status("The JWK \"use\" property was inconsistent with that specified " | 76 return Status( |
| 84 "by the Web Crypto call. The JWK usage must be a superset of " | 77 "The JWK \"use\" property was inconsistent with that specified " |
| 85 "those requested"); | 78 "by the Web Crypto call. The JWK usage must be a superset of " |
| 79 "those requested"); |
| 86 } | 80 } |
| 87 | 81 |
| 88 Status Status::ErrorJwkRsaPrivateKeyUnsupported() { | 82 Status Status::ErrorJwkRsaPrivateKeyUnsupported() { |
| 89 return Status("JWK RSA key contained \"d\" property: Private key import is " | 83 return Status( |
| 90 "not yet supported"); | 84 "JWK RSA key contained \"d\" property: Private key import is " |
| 85 "not yet supported"); |
| 91 } | 86 } |
| 92 | 87 |
| 93 Status Status::ErrorJwkUnrecognizedKty() { | 88 Status Status::ErrorJwkUnrecognizedKty() { |
| 94 return Status("The JWK \"kty\" property was unrecognized"); | 89 return Status("The JWK \"kty\" property was unrecognized"); |
| 95 } | 90 } |
| 96 | 91 |
| 97 Status Status::ErrorJwkIncorrectKeyLength() { | 92 Status Status::ErrorJwkIncorrectKeyLength() { |
| 98 return Status("The JWK \"k\" property did not include the right length " | 93 return Status( |
| 99 "of key data for the given algorithm."); | 94 "The JWK \"k\" property did not include the right length " |
| 95 "of key data for the given algorithm."); |
| 100 } | 96 } |
| 101 | 97 |
| 102 Status Status::ErrorImportEmptyKeyData() { | 98 Status Status::ErrorImportEmptyKeyData() { |
| 103 return Status("No key data was provided"); | 99 return Status("No key data was provided"); |
| 104 } | 100 } |
| 105 | 101 |
| 106 Status Status::ErrorUnexpectedKeyType() { | 102 Status Status::ErrorUnexpectedKeyType() { |
| 107 return Status("The key is not of the expected type"); | 103 return Status("The key is not of the expected type"); |
| 108 } | 104 } |
| 109 | 105 |
| 110 Status Status::ErrorIncorrectSizeAesCbcIv() { | 106 Status Status::ErrorIncorrectSizeAesCbcIv() { |
| 111 return Status("The \"iv\" has an unexpected length -- must be 16 bytes"); | 107 return Status("The \"iv\" has an unexpected length -- must be 16 bytes"); |
| 112 } | 108 } |
| 113 | 109 |
| 114 Status Status::ErrorDataTooLarge() { | 110 Status Status::ErrorDataTooLarge() { |
| 115 return Status("The provided data is too large"); | 111 return Status("The provided data is too large"); |
| 116 } | 112 } |
| 117 | 113 |
| 118 Status Status::ErrorUnsupported() { | 114 Status Status::ErrorUnsupported() { |
| 119 return Status("The requested operation is unsupported"); | 115 return Status("The requested operation is unsupported"); |
| 120 } | 116 } |
| 121 | 117 |
| 122 Status Status::ErrorUnexpected() { | 118 Status Status::ErrorUnexpected() { |
| 123 return Status("Something unexpected happened..."); | 119 return Status("Something unexpected happened..."); |
| 124 } | 120 } |
| 125 | 121 |
| 126 Status Status::ErrorInvalidAesGcmTagLength() { | 122 Status Status::ErrorInvalidAesGcmTagLength() { |
| 127 return Status("The tag length is invalid: either too large or not a multiple " | 123 return Status( |
| 128 "of 8 bits"); | 124 "The tag length is invalid: either too large or not a multiple " |
| 125 "of 8 bits"); |
| 129 } | 126 } |
| 130 | 127 |
| 131 Status Status::ErrorGenerateKeyPublicExponent() { | 128 Status Status::ErrorGenerateKeyPublicExponent() { |
| 132 return Status("The \"publicExponent\" is either empty, zero, or too large"); | 129 return Status("The \"publicExponent\" is either empty, zero, or too large"); |
| 133 } | 130 } |
| 134 | 131 |
| 135 Status Status::ErrorMissingAlgorithmImportRawKey() { | 132 Status Status::ErrorMissingAlgorithmImportRawKey() { |
| 136 return Status("The key's algorithm must be specified when importing " | 133 return Status( |
| 137 "raw-formatted key."); | 134 "The key's algorithm must be specified when importing " |
| 135 "raw-formatted key."); |
| 138 } | 136 } |
| 139 | 137 |
| 140 Status Status::ErrorImportRsaEmptyModulus() { | 138 Status Status::ErrorImportRsaEmptyModulus() { |
| 141 return Status("The modulus is empty"); | 139 return Status("The modulus is empty"); |
| 142 } | 140 } |
| 143 | 141 |
| 144 Status Status::ErrorGenerateRsaZeroModulus() { | 142 Status Status::ErrorGenerateRsaZeroModulus() { |
| 145 return Status("The modulus bit length cannot be zero"); | 143 return Status("The modulus bit length cannot be zero"); |
| 146 } | 144 } |
| 147 | 145 |
| 148 Status Status::ErrorImportRsaEmptyExponent() { | 146 Status Status::ErrorImportRsaEmptyExponent() { |
| 149 return Status("No bytes for the exponent were provided"); | 147 return Status("No bytes for the exponent were provided"); |
| 150 } | 148 } |
| 151 | 149 |
| 152 Status Status::ErrorKeyNotExtractable() { | 150 Status Status::ErrorKeyNotExtractable() { |
| 153 return Status("They key is not extractable"); | 151 return Status("They key is not extractable"); |
| 154 } | 152 } |
| 155 | 153 |
| 156 Status Status::ErrorGenerateKeyLength() { | 154 Status Status::ErrorGenerateKeyLength() { |
| 157 return Status("Invalid key length: it is either zero or not a multiple of 8 " | 155 return Status( |
| 158 "bits"); | 156 "Invalid key length: it is either zero or not a multiple of 8 " |
| 157 "bits"); |
| 159 } | 158 } |
| 160 | 159 |
| 161 Status::Status(const std::string& error_details_utf8) | 160 Status::Status(const std::string& error_details_utf8) |
| 162 : type_(TYPE_ERROR), error_details_(error_details_utf8) {} | 161 : type_(TYPE_ERROR), error_details_(error_details_utf8) {} |
| 163 | 162 |
| 164 Status::Status(Type type) : type_(type) {} | 163 Status::Status(Type type) : type_(type) {} |
| 165 | 164 |
| 166 const uint8* Uint8VectorStart(const std::vector<uint8>& data) { | 165 const uint8* Uint8VectorStart(const std::vector<uint8>& data) { |
| 167 if (data.empty()) | 166 if (data.empty()) |
| 168 return NULL; | 167 return NULL; |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 return 128; | 321 return 128; |
| 323 default: | 322 default: |
| 324 NOTREACHED(); | 323 NOTREACHED(); |
| 325 return 0; | 324 return 0; |
| 326 } | 325 } |
| 327 } | 326 } |
| 328 | 327 |
| 329 } // namespace webcrypto | 328 } // namespace webcrypto |
| 330 | 329 |
| 331 } // namespace content | 330 } // namespace content |
| OLD | NEW |