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"
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
) {
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() {
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 "
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
) {
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::WebCryptoErrorTypeOperation
,
186 "The provided data is too large");
189 Status
Status::ErrorDataTooSmall() {
190 return Status(blink::WebCryptoErrorTypeOperation
,
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() {
209 blink::WebCryptoErrorTypeOperation
,
210 "The tag length is invalid: Must be 32, 64, 96, 104, 112, 120, or 128 "
214 Status
Status::ErrorInvalidAesKwDataLength() {
215 return Status(blink::WebCryptoErrorTypeData
,
216 "The AES-KW input data length is invalid: not a multiple of 8 "
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 "
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() {
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() {
279 blink::WebCryptoErrorTypeData
,
280 "The imported EC key specifies a different curve than requested");
283 Status
Status::ErrorJwkIncorrectCrv() {
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
) {
298 blink::WebCryptoErrorTypeData
,
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() {
307 blink::WebCryptoErrorTypeInvalidAccess
,
308 "The public parameter for ECDH key derivation is not a public EC key");
311 Status
Status::ErrorEcdhPublicKeyWrongAlgorithm() {
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
,
326 "Length specified for ECDH key derivation is too large. "
327 "Maximum allowed is %u bits",
331 Status
Status::ErrorHkdfLengthTooLong() {
332 return Status(blink::WebCryptoErrorTypeOperation
,
333 "The length provided for HKDF is too large.");
336 Status
Status::ErrorHkdfDeriveBitsLengthNotSpecified() {
337 // TODO(nharper): The spec might change so that an OperationError should be
338 // thrown here instead of a TypeError.
339 // (https://www.w3.org/Bugs/Public/show_bug.cgi?id=27771)
340 return Status(blink::WebCryptoErrorTypeType
,
341 "No length was specified for the HKDF Derive Bits operation.");
344 Status
Status::ErrorPbkdf2InvalidLength() {
346 blink::WebCryptoErrorTypeOperation
,
347 "Length for PBKDF2 key derivation must be a multiple of 8 bits.");
350 Status
Status::ErrorPbkdf2DeriveBitsLengthNotSpecified() {
352 blink::WebCryptoErrorTypeOperation
,
353 "No length was specified for the PBKDF2 Derive Bits operation.");
356 Status::Status(blink::WebCryptoErrorType error_type
,
357 const std::string
& error_details_utf8
)
359 error_type_(error_type
),
360 error_details_(error_details_utf8
) {
363 Status::Status(Type type
) : type_(type
) {
366 } // namespace webcrypto
368 } // namespace content