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 11 bool Status::IsError() const { 12 return type_ == TYPE_ERROR; 13 } 14 15 bool Status::IsSuccess() const { 16 return type_ == TYPE_SUCCESS; 17 } 18 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 } 30 31 Status Status::ErrorJwkNotDictionary() { 32 return Status(blink::WebCryptoErrorTypeData, 33 "JWK input could not be parsed to a JSON dictionary"); 34 } 35 36 Status Status::ErrorJwkPropertyMissing(const std::string& property) { 37 return Status(blink::WebCryptoErrorTypeData, 38 "The required JWK property \"" + property + "\" was missing"); 39 } 40 41 Status Status::ErrorJwkPropertyWrongType(const std::string& property, 42 const std::string& expected_type) { 43 return Status( 44 blink::WebCryptoErrorTypeData, 45 "The JWK property \"" + property + "\" must be a " + expected_type); 46 } 47 48 Status Status::ErrorJwkBase64Decode(const std::string& property) { 49 return Status( 50 blink::WebCryptoErrorTypeData, 51 "The JWK property \"" + property + "\" could not be base64 decoded"); 52 } 53 54 Status Status::ErrorJwkExtInconsistent() { 55 return Status( 56 blink::WebCryptoErrorTypeData, 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() { 62 return Status(blink::WebCryptoErrorTypeData, 63 "The JWK \"alg\" property was not recognized"); 64 } 65 66 Status Status::ErrorJwkAlgorithmInconsistent() { 67 return Status(blink::WebCryptoErrorTypeData, 68 "The JWK \"alg\" property was inconsistent with that specified " 69 "by the Web Crypto call"); 70 } 71 72 Status Status::ErrorJwkUnrecognizedUse() { 73 return Status(blink::WebCryptoErrorTypeData, 74 "The JWK \"use\" property could not be parsed"); 75 } 76 77 Status Status::ErrorJwkUnrecognizedKeyop() { 78 return Status(blink::WebCryptoErrorTypeData, 79 "The JWK \"key_ops\" property could not be parsed"); 80 } 81 82 Status Status::ErrorJwkUseInconsistent() { 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"); 87 } 88 89 Status Status::ErrorJwkKeyopsInconsistent() { 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"); 94 } 95 96 Status Status::ErrorJwkUseAndKeyopsInconsistent() { 97 return Status(blink::WebCryptoErrorTypeData, 98 "The JWK \"use\" and \"key_ops\" properties were both found " 99 "but are inconsistent with each other."); 100 } 101 102 Status Status::ErrorJwkUnrecognizedKty() { 103 return Status(blink::WebCryptoErrorTypeData, 104 "The JWK \"kty\" property was unrecognized"); 105 } 106 107 Status Status::ErrorJwkIncorrectKeyLength() { 108 return Status(blink::WebCryptoErrorTypeData, 109 "The JWK \"k\" property did not include the right length " 110 "of key data for the given algorithm."); 111 } 112 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 119 Status Status::ErrorImportEmptyKeyData() { 120 return Status(blink::WebCryptoErrorTypeData, "No key data was provided"); 121 } 122 123 Status Status::ErrorImportAesKeyLength() { 124 return Status(blink::WebCryptoErrorTypeData, 125 "AES key data must be 128, 192 or 256 bits"); 126 } 127 128 Status Status::ErrorAes192BitUnsupported() { 129 return Status(blink::WebCryptoErrorTypeNotSupported, 130 "192-bit AES keys are not supported"); 131 } 132 133 Status Status::ErrorUnexpectedKeyType() { 134 return Status(blink::WebCryptoErrorTypeInvalidAccess, 135 "The key is not of the expected type"); 136 } 137 138 Status Status::ErrorIncorrectSizeAesCbcIv() { 139 return Status(blink::WebCryptoErrorTypeData, 140 "The \"iv\" has an unexpected length -- must be 16 bytes"); 141 } 142 143 Status Status::ErrorDataTooLarge() { 144 return Status(blink::WebCryptoErrorTypeData, 145 "The provided data is too large"); 146 } 147 148 Status Status::ErrorDataTooSmall() { 149 return Status(blink::WebCryptoErrorTypeData, 150 "The provided data is too small"); 151 } 152 153 Status Status::ErrorUnsupported() { 154 return ErrorUnsupported("The requested operation is unsupported"); 155 } 156 157 Status Status::ErrorUnsupported(const std::string& message) { 158 return Status(blink::WebCryptoErrorTypeNotSupported, message); 159 } 160 161 Status Status::ErrorUnexpected() { 162 return Status(blink::WebCryptoErrorTypeUnknown, 163 "Something unexpected happened..."); 164 } 165 166 Status Status::ErrorInvalidAesGcmTagLength() { 167 return Status( 168 blink::WebCryptoErrorTypeData, 169 "The tag length is invalid: Must be 32, 64, 96, 104, 112, 120, or 128 " 170 "bits"); 171 } 172 173 Status Status::ErrorInvalidAesKwDataLength() { 174 return Status(blink::WebCryptoErrorTypeData, 175 "The AES-KW input data length is invalid: not a multiple of 8 " 176 "bytes"); 177 } 178 179 Status Status::ErrorGenerateKeyPublicExponent() { 180 return Status(blink::WebCryptoErrorTypeData, 181 "The \"publicExponent\" must be either 3 or 65537"); 182 } 183 184 Status Status::ErrorImportRsaEmptyModulus() { 185 return Status(blink::WebCryptoErrorTypeData, "The modulus is empty"); 186 } 187 188 Status Status::ErrorGenerateRsaZeroModulus() { 189 return Status(blink::WebCryptoErrorTypeData, 190 "The modulus bit length cannot be zero"); 191 } 192 193 Status Status::ErrorImportRsaEmptyExponent() { 194 return Status(blink::WebCryptoErrorTypeData, 195 "No bytes for the exponent were provided"); 196 } 197 198 Status Status::ErrorKeyNotExtractable() { 199 return Status(blink::WebCryptoErrorTypeInvalidAccess, 200 "They key is not extractable"); 201 } 202 203 Status Status::ErrorGenerateKeyLength() { 204 return Status(blink::WebCryptoErrorTypeData, 205 "Invalid key length: it is either zero or not a multiple of 8 " 206 "bits"); 207 } 208 209 Status Status::ErrorCreateKeyBadUsages() { 210 return Status(blink::WebCryptoErrorTypeData, 211 "Cannot create a key using the specified key usages."); 212 } 213 214 Status::Status(blink::WebCryptoErrorType error_type, 215 const std::string& error_details_utf8) 216 : type_(TYPE_ERROR), 217 error_type_(error_type), 218 error_details_(error_details_utf8) { 219 } 220 221 Status::Status(Type type) : type_(type) { 222 } 223 224 } // namespace webcrypto 225 226 } // namespace content 227