Don't allow importing empty HMAC keys.
[chromium-blink-merge.git] / content / child / webcrypto / status.cc
blobc3862729bb9af973b48d3fe35ee7d90e0f3a2c07
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.
5 #include "content/child/webcrypto/status.h"
7 #include "base/format_macros.h"
8 #include "base/strings/stringprintf.h"
10 namespace content {
12 namespace webcrypto {
14 bool Status::IsError() const {
15 return type_ == TYPE_ERROR;
18 bool Status::IsSuccess() const {
19 return type_ == TYPE_SUCCESS;
22 Status Status::Success() {
23 return Status(TYPE_SUCCESS);
26 Status Status::OperationError() {
27 return Status(blink::WebCryptoErrorTypeOperation, "");
30 Status Status::DataError() {
31 return Status(blink::WebCryptoErrorTypeData, "");
34 Status Status::ErrorJwkNotDictionary() {
35 return Status(blink::WebCryptoErrorTypeData,
36 "JWK input could not be parsed to a JSON dictionary");
39 Status Status::ErrorJwkMemberMissing(const std::string& member_name) {
40 return Status(blink::WebCryptoErrorTypeData,
41 "The required JWK member \"" + member_name + "\" was missing");
44 Status Status::ErrorJwkMemberWrongType(const std::string& member_name,
45 const std::string& expected_type) {
46 return Status(
47 blink::WebCryptoErrorTypeData,
48 "The JWK member \"" + member_name + "\" must be a " + expected_type);
51 Status Status::ErrorJwkBase64Decode(const std::string& member_name) {
52 return Status(blink::WebCryptoErrorTypeData,
53 "The JWK member \"" + member_name +
54 "\" could not be base64url decoded or contained padding");
57 Status Status::ErrorJwkExtInconsistent() {
58 return Status(
59 blink::WebCryptoErrorTypeData,
60 "The \"ext\" member of the JWK dictionary is inconsistent what that "
61 "specified by the Web Crypto call");
64 Status Status::ErrorJwkAlgorithmInconsistent() {
65 return Status(blink::WebCryptoErrorTypeData,
66 "The JWK \"alg\" member was inconsistent with that specified "
67 "by the Web Crypto call");
70 Status Status::ErrorJwkUnrecognizedUse() {
71 return Status(blink::WebCryptoErrorTypeData,
72 "The JWK \"use\" member could not be parsed");
75 Status Status::ErrorJwkUnrecognizedKeyop() {
76 return Status(blink::WebCryptoErrorTypeData,
77 "The JWK \"key_ops\" member could not be parsed");
80 Status Status::ErrorJwkUseInconsistent() {
81 return Status(blink::WebCryptoErrorTypeData,
82 "The JWK \"use\" member was inconsistent with that specified "
83 "by the Web Crypto call. The JWK usage must be a superset of "
84 "those requested");
87 Status Status::ErrorJwkKeyopsInconsistent() {
88 return Status(blink::WebCryptoErrorTypeData,
89 "The JWK \"key_ops\" member was inconsistent with that "
90 "specified by the Web Crypto call. The JWK usage must be a "
91 "superset of those requested");
94 Status Status::ErrorJwkUseAndKeyopsInconsistent() {
95 return Status(blink::WebCryptoErrorTypeData,
96 "The JWK \"use\" and \"key_ops\" properties were both found "
97 "but are inconsistent with each other.");
100 Status Status::ErrorJwkUnexpectedKty(const std::string& expected) {
101 return Status(blink::WebCryptoErrorTypeData,
102 "The JWK \"kty\" member was not \"" + expected + "\"");
105 Status Status::ErrorJwkIncorrectKeyLength() {
106 return Status(blink::WebCryptoErrorTypeData,
107 "The JWK \"k\" member did not include the right length "
108 "of key data for the given algorithm.");
111 Status Status::ErrorJwkEmptyBigInteger(const std::string& member_name) {
112 return Status(blink::WebCryptoErrorTypeData,
113 "The JWK \"" + member_name + "\" member was empty.");
116 Status Status::ErrorJwkBigIntegerHasLeadingZero(
117 const std::string& member_name) {
118 return Status(
119 blink::WebCryptoErrorTypeData,
120 "The JWK \"" + member_name + "\" member contained a leading zero.");
123 Status Status::ErrorJwkDuplicateKeyOps() {
124 return Status(blink::WebCryptoErrorTypeData,
125 "The \"key_ops\" member of the JWK dictionary contains "
126 "duplicate usages.");
129 Status Status::ErrorUnsupportedImportKeyFormat() {
130 return Status(blink::WebCryptoErrorTypeNotSupported,
131 "Unsupported import key format for algorithm");
134 Status Status::ErrorUnsupportedExportKeyFormat() {
135 return Status(blink::WebCryptoErrorTypeNotSupported,
136 "Unsupported export key format for algorithm");
139 Status Status::ErrorImportAesKeyLength() {
140 return Status(blink::WebCryptoErrorTypeData,
141 "AES key data must be 128 or 256 bits");
144 Status Status::ErrorGetAesKeyLength() {
145 return Status(blink::WebCryptoErrorTypeOperation,
146 "AES key length must be 128 or 256 bits");
149 Status Status::ErrorGenerateAesKeyLength() {
150 return Status(blink::WebCryptoErrorTypeOperation,
151 "AES key length must be 128 or 256 bits");
154 Status Status::ErrorAes192BitUnsupported() {
155 return Status(blink::WebCryptoErrorTypeOperation,
156 "192-bit AES keys are not supported");
159 Status Status::ErrorUnexpectedKeyType() {
160 return Status(blink::WebCryptoErrorTypeInvalidAccess,
161 "The key is not of the expected type");
164 Status Status::ErrorIncorrectSizeAesCbcIv() {
165 return Status(blink::WebCryptoErrorTypeOperation,
166 "The \"iv\" has an unexpected length -- must be 16 bytes");
169 Status Status::ErrorIncorrectSizeAesCtrCounter() {
170 return Status(blink::WebCryptoErrorTypeOperation,
171 "The \"counter\" has an unexpected length -- must be 16 bytes");
174 Status Status::ErrorInvalidAesCtrCounterLength() {
175 return Status(blink::WebCryptoErrorTypeOperation,
176 "The \"length\" member must be >= 1 and <= 128");
179 Status Status::ErrorAesCtrInputTooLongCounterRepeated() {
180 return Status(blink::WebCryptoErrorTypeData,
181 "The input is too large for the counter length.");
184 Status Status::ErrorDataTooLarge() {
185 return Status(blink::WebCryptoErrorTypeData,
186 "The provided data is too large");
189 Status Status::ErrorDataTooSmall() {
190 return Status(blink::WebCryptoErrorTypeData,
191 "The provided data is too small");
194 Status Status::ErrorUnsupported() {
195 return ErrorUnsupported("The requested operation is unsupported");
198 Status Status::ErrorUnsupported(const std::string& message) {
199 return Status(blink::WebCryptoErrorTypeNotSupported, message);
202 Status Status::ErrorUnexpected() {
203 return Status(blink::WebCryptoErrorTypeOperation,
204 "Something unexpected happened...");
207 Status Status::ErrorInvalidAesGcmTagLength() {
208 return Status(
209 blink::WebCryptoErrorTypeOperation,
210 "The tag length is invalid: Must be 32, 64, 96, 104, 112, 120, or 128 "
211 "bits");
214 Status Status::ErrorInvalidAesKwDataLength() {
215 return Status(blink::WebCryptoErrorTypeData,
216 "The AES-KW input data length is invalid: not a multiple of 8 "
217 "bytes");
220 Status Status::ErrorGenerateKeyPublicExponent() {
221 return Status(blink::WebCryptoErrorTypeOperation,
222 "The \"publicExponent\" must be either 3 or 65537");
225 Status Status::ErrorImportRsaEmptyModulus() {
226 return Status(blink::WebCryptoErrorTypeData, "The modulus is empty");
229 Status Status::ErrorGenerateRsaUnsupportedModulus() {
230 return Status(blink::WebCryptoErrorTypeOperation,
231 "The modulus length must be a multiple of 8 bits and >= 256 "
232 "and <= 16384");
235 Status Status::ErrorImportRsaEmptyExponent() {
236 return Status(blink::WebCryptoErrorTypeData,
237 "No bytes for the exponent were provided");
240 Status Status::ErrorKeyNotExtractable() {
241 return Status(blink::WebCryptoErrorTypeInvalidAccess,
242 "They key is not extractable");
245 Status Status::ErrorGenerateHmacKeyLengthZero() {
246 return Status(blink::WebCryptoErrorTypeOperation,
247 "HMAC key length must not be zero");
250 Status Status::ErrorHmacImportEmptyKey() {
251 return Status(blink::WebCryptoErrorTypeData,
252 "HMAC key data must not be empty");
255 Status Status::ErrorGetHmacKeyLengthZero() {
256 return Status(blink::WebCryptoErrorTypeType,
257 "HMAC key length must not be zero");
260 Status Status::ErrorHmacImportBadLength() {
261 return Status(
262 blink::WebCryptoErrorTypeData,
263 "The optional HMAC key length must be shorter than the key data, and by "
264 "no more than 7 bits.");
267 Status Status::ErrorCreateKeyBadUsages() {
268 return Status(blink::WebCryptoErrorTypeSyntax,
269 "Cannot create a key using the specified key usages.");
272 Status Status::ErrorCreateKeyEmptyUsages() {
273 return Status(blink::WebCryptoErrorTypeSyntax,
274 "Usages cannot be empty when creating a key.");
277 Status Status::ErrorImportedEcKeyIncorrectCurve() {
278 return Status(
279 blink::WebCryptoErrorTypeData,
280 "The imported EC key specifies a different curve than requested");
283 Status Status::ErrorJwkIncorrectCrv() {
284 return Status(
285 blink::WebCryptoErrorTypeData,
286 "The JWK's \"crv\" member specifies a different curve than requested");
289 Status Status::ErrorEcKeyInvalid() {
290 return Status(blink::WebCryptoErrorTypeData,
291 "The imported EC key is invalid");
294 Status Status::JwkOctetStringWrongLength(const std::string& member_name,
295 size_t expected_length,
296 size_t actual_length) {
297 return Status(
298 blink::WebCryptoErrorTypeData,
299 base::StringPrintf(
300 "The JWK's \"%s\" member defines an octet string of length %" PRIuS
301 " bytes but should be %" PRIuS,
302 member_name.c_str(), actual_length, expected_length));
305 Status Status::ErrorEcdhPublicKeyWrongType() {
306 return Status(
307 blink::WebCryptoErrorTypeInvalidAccess,
308 "The public parameter for ECDH key derivation is not a public EC key");
311 Status Status::ErrorEcdhPublicKeyWrongAlgorithm() {
312 return Status(
313 blink::WebCryptoErrorTypeInvalidAccess,
314 "The public parameter for ECDH key derivation must be for ECDH");
317 Status Status::ErrorEcdhCurveMismatch() {
318 return Status(blink::WebCryptoErrorTypeInvalidAccess,
319 "The public parameter for ECDH key derivation is for a "
320 "different named curve");
323 Status Status::ErrorEcdhLengthTooBig(unsigned int max_length_bits) {
324 return Status(blink::WebCryptoErrorTypeOperation,
325 base::StringPrintf(
326 "Length specified for ECDH key derivation is too large. "
327 "Maximum allowed is %u bits",
328 max_length_bits));
331 Status::Status(blink::WebCryptoErrorType error_type,
332 const std::string& error_details_utf8)
333 : type_(TYPE_ERROR),
334 error_type_(error_type),
335 error_details_(error_details_utf8) {
338 Status::Status(Type type) : type_(type) {
341 } // namespace webcrypto
343 } // namespace content