[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/child/webcrypto/status.h" |
| 6 | |
| 7 | namespace content { |
| 8 | |
| 9 | namespace webcrypto { |
| 10 | |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 11 | bool Status::IsError() const { |
| 12 | return type_ == TYPE_ERROR; |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 13 | } |
| 14 | |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 15 | bool Status::IsSuccess() const { |
| 16 | return type_ == TYPE_SUCCESS; |
| 17 | } |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 18 | |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 19 | Status Status::Success() { |
| 20 | return Status(TYPE_SUCCESS); |
| 21 | } |
| 22 | |
| 23 | Status Status::OperationError() { |
| 24 | return Status(blink::WebCryptoErrorTypeOperation, ""); |
| 25 | } |
| 26 | |
| 27 | Status Status::DataError() { |
| 28 | return Status(blink::WebCryptoErrorTypeData, ""); |
| 29 | } |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 30 | |
| 31 | Status Status::ErrorJwkNotDictionary() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 32 | return Status(blink::WebCryptoErrorTypeData, |
| 33 | "JWK input could not be parsed to a JSON dictionary"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | Status Status::ErrorJwkPropertyMissing(const std::string& property) { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 37 | return Status(blink::WebCryptoErrorTypeData, |
| 38 | "The required JWK property \"" + property + "\" was missing"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | Status Status::ErrorJwkPropertyWrongType(const std::string& property, |
| 42 | const std::string& expected_type) { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 43 | return Status( |
| 44 | blink::WebCryptoErrorTypeData, |
| 45 | "The JWK property \"" + property + "\" must be a " + expected_type); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | Status Status::ErrorJwkBase64Decode(const std::string& property) { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 49 | return Status( |
| 50 | blink::WebCryptoErrorTypeData, |
| 51 | "The JWK property \"" + property + "\" could not be base64 decoded"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | Status Status::ErrorJwkExtInconsistent() { |
| 55 | return Status( |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 56 | blink::WebCryptoErrorTypeData, |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 57 | "The \"ext\" property of the JWK dictionary is inconsistent what that " |
| 58 | "specified by the Web Crypto call"); |
| 59 | } |
| 60 | |
| 61 | Status Status::ErrorJwkUnrecognizedAlgorithm() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 62 | return Status(blink::WebCryptoErrorTypeData, |
| 63 | "The JWK \"alg\" property was not recognized"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | Status Status::ErrorJwkAlgorithmInconsistent() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 67 | return Status(blink::WebCryptoErrorTypeData, |
| 68 | "The JWK \"alg\" property was inconsistent with that specified " |
| 69 | "by the Web Crypto call"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 70 | } |
| 71 | |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 72 | Status Status::ErrorJwkUnrecognizedUse() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 73 | return Status(blink::WebCryptoErrorTypeData, |
| 74 | "The JWK \"use\" property could not be parsed"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | Status Status::ErrorJwkUnrecognizedKeyop() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 78 | return Status(blink::WebCryptoErrorTypeData, |
| 79 | "The JWK \"key_ops\" property could not be parsed"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | Status Status::ErrorJwkUseInconsistent() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 83 | return Status(blink::WebCryptoErrorTypeData, |
| 84 | "The JWK \"use\" property was inconsistent with that specified " |
| 85 | "by the Web Crypto call. The JWK usage must be a superset of " |
| 86 | "those requested"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | Status Status::ErrorJwkKeyopsInconsistent() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 90 | return Status(blink::WebCryptoErrorTypeData, |
| 91 | "The JWK \"key_ops\" property was inconsistent with that " |
| 92 | "specified by the Web Crypto call. The JWK usage must be a " |
| 93 | "superset of those requested"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | Status Status::ErrorJwkUseAndKeyopsInconsistent() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 97 | return Status(blink::WebCryptoErrorTypeData, |
| 98 | "The JWK \"use\" and \"key_ops\" properties were both found " |
| 99 | "but are inconsistent with each other."); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 100 | } |
| 101 | |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 102 | Status Status::ErrorJwkUnrecognizedKty() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 103 | return Status(blink::WebCryptoErrorTypeData, |
| 104 | "The JWK \"kty\" property was unrecognized"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | Status Status::ErrorJwkIncorrectKeyLength() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 108 | return Status(blink::WebCryptoErrorTypeData, |
| 109 | "The JWK \"k\" property did not include the right length " |
| 110 | "of key data for the given algorithm."); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 111 | } |
| 112 | |
[email protected] | 6f82af2 | 2014-05-21 15:26:14 | [diff] [blame] | 113 | Status Status::ErrorJwkIncompleteOptionalRsaPrivateKey() { |
| 114 | return Status(blink::WebCryptoErrorTypeData, |
| 115 | "The optional JWK properties p, q, dp, dq, qi must either all " |
| 116 | "be provided, or none provided"); |
| 117 | } |
| 118 | |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 119 | Status Status::ErrorImportEmptyKeyData() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 120 | return Status(blink::WebCryptoErrorTypeData, "No key data was provided"); |
| 121 | } |
| 122 | |
| 123 | Status Status::ErrorImportAesKeyLength() { |
| 124 | return Status(blink::WebCryptoErrorTypeData, |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 125 | "AES key data must be 128, 192 or 256 bits"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | Status Status::ErrorUnexpectedKeyType() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 129 | return Status(blink::WebCryptoErrorTypeInvalidAccess, |
| 130 | "The key is not of the expected type"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | Status Status::ErrorIncorrectSizeAesCbcIv() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 134 | return Status(blink::WebCryptoErrorTypeData, |
| 135 | "The \"iv\" has an unexpected length -- must be 16 bytes"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | Status Status::ErrorDataTooLarge() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 139 | return Status(blink::WebCryptoErrorTypeData, |
| 140 | "The provided data is too large"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | Status Status::ErrorDataTooSmall() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 144 | return Status(blink::WebCryptoErrorTypeData, |
| 145 | "The provided data is too small"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | Status Status::ErrorUnsupported() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 149 | return Status(blink::WebCryptoErrorTypeNotSupported, |
| 150 | "The requested operation is unsupported"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | Status Status::ErrorUnexpected() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 154 | return Status(blink::WebCryptoErrorTypeUnknown, |
| 155 | "Something unexpected happened..."); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | Status Status::ErrorInvalidAesGcmTagLength() { |
| 159 | return Status( |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 160 | blink::WebCryptoErrorTypeData, |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 161 | "The tag length is invalid: Must be 32, 64, 96, 104, 112, 120, or 128 " |
| 162 | "bits"); |
| 163 | } |
| 164 | |
| 165 | Status Status::ErrorInvalidAesKwDataLength() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 166 | return Status(blink::WebCryptoErrorTypeData, |
| 167 | "The AES-KW input data length is invalid: not a multiple of 8 " |
| 168 | "bytes"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | Status Status::ErrorGenerateKeyPublicExponent() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 172 | return Status(blink::WebCryptoErrorTypeData, |
[email protected] | c360f83 | 2014-06-12 13:21:50 | [diff] [blame^] | 173 | "The \"publicExponent\" must be either 3 or 65537"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 174 | } |
| 175 | |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 176 | Status Status::ErrorImportRsaEmptyModulus() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 177 | return Status(blink::WebCryptoErrorTypeData, "The modulus is empty"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | Status Status::ErrorGenerateRsaZeroModulus() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 181 | return Status(blink::WebCryptoErrorTypeData, |
| 182 | "The modulus bit length cannot be zero"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | Status Status::ErrorImportRsaEmptyExponent() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 186 | return Status(blink::WebCryptoErrorTypeData, |
| 187 | "No bytes for the exponent were provided"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | Status Status::ErrorKeyNotExtractable() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 191 | return Status(blink::WebCryptoErrorTypeInvalidAccess, |
| 192 | "They key is not extractable"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | Status Status::ErrorGenerateKeyLength() { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 196 | return Status(blink::WebCryptoErrorTypeData, |
| 197 | "Invalid key length: it is either zero or not a multiple of 8 " |
| 198 | "bits"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 199 | } |
| 200 | |
[email protected] | 9c2e9cf | 2014-05-23 23:13:47 | [diff] [blame] | 201 | Status Status::ErrorCreateKeyBadUsages() { |
| 202 | return Status(blink::WebCryptoErrorTypeData, |
| 203 | "Cannot create a key using the specified key usages."); |
| 204 | } |
| 205 | |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 206 | Status::Status(blink::WebCryptoErrorType error_type, |
| 207 | const std::string& error_details_utf8) |
| 208 | : type_(TYPE_ERROR), |
| 209 | error_type_(error_type), |
| 210 | error_details_(error_details_utf8) { |
| 211 | } |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 212 | |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 213 | Status::Status(Type type) : type_(type) { |
| 214 | } |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 215 | |
| 216 | } // namespace webcrypto |
| 217 | |
| 218 | } // namespace content |