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 "components/webcrypto/status.h"
7 #include "base/format_macros.h"
8 #include "base/strings/stringprintf.h"
12 bool Status::IsError() const {
13 return type_
== TYPE_ERROR
;
16 bool Status::IsSuccess() const {
17 return type_
== TYPE_SUCCESS
;
20 Status
Status::Success() {
21 return Status(TYPE_SUCCESS
);
24 Status
Status::OperationError() {
25 return Status(blink::WebCryptoErrorTypeOperation
, "");
28 Status
Status::DataError() {
29 return Status(blink::WebCryptoErrorTypeData
, "");
32 Status
Status::ErrorJwkNotDictionary() {
33 return Status(blink::WebCryptoErrorTypeData
,
34 "JWK input could not be parsed to a JSON dictionary");
37 Status
Status::ErrorJwkMemberMissing(const std::string
& member_name
) {
38 return Status(blink::WebCryptoErrorTypeData
,
39 "The required JWK member \"" + member_name
+ "\" was missing");
42 Status
Status::ErrorJwkMemberWrongType(const std::string
& member_name
,
43 const std::string
& expected_type
) {
45 blink::WebCryptoErrorTypeData
,
46 "The JWK member \"" + member_name
+ "\" must be a " + expected_type
);
49 Status
Status::ErrorJwkBase64Decode(const std::string
& member_name
) {
50 return Status(blink::WebCryptoErrorTypeData
,
51 "The JWK member \"" + member_name
+
52 "\" could not be base64url decoded or contained padding");
55 Status
Status::ErrorJwkExtInconsistent() {
57 blink::WebCryptoErrorTypeData
,
58 "The \"ext\" member of the JWK dictionary is inconsistent what that "
59 "specified by the Web Crypto call");
62 Status
Status::ErrorJwkAlgorithmInconsistent() {
63 return Status(blink::WebCryptoErrorTypeData
,
64 "The JWK \"alg\" member was inconsistent with that specified "
65 "by the Web Crypto call");
68 Status
Status::ErrorJwkUnrecognizedUse() {
69 return Status(blink::WebCryptoErrorTypeData
,
70 "The JWK \"use\" member could not be parsed");
73 Status
Status::ErrorJwkUnrecognizedKeyop() {
74 return Status(blink::WebCryptoErrorTypeData
,
75 "The JWK \"key_ops\" member could not be parsed");
78 Status
Status::ErrorJwkUseInconsistent() {
79 return Status(blink::WebCryptoErrorTypeData
,
80 "The JWK \"use\" member was inconsistent with that specified "
81 "by the Web Crypto call. The JWK usage must be a superset of "
85 Status
Status::ErrorJwkKeyopsInconsistent() {
86 return Status(blink::WebCryptoErrorTypeData
,
87 "The JWK \"key_ops\" member was inconsistent with that "
88 "specified by the Web Crypto call. The JWK usage must be a "
89 "superset of those requested");
92 Status
Status::ErrorJwkUseAndKeyopsInconsistent() {
93 return Status(blink::WebCryptoErrorTypeData
,
94 "The JWK \"use\" and \"key_ops\" properties were both found "
95 "but are inconsistent with each other.");
98 Status
Status::ErrorJwkUnexpectedKty(const std::string
& expected
) {
99 return Status(blink::WebCryptoErrorTypeData
,
100 "The JWK \"kty\" member was not \"" + expected
+ "\"");
103 Status
Status::ErrorJwkIncorrectKeyLength() {
104 return Status(blink::WebCryptoErrorTypeData
,
105 "The JWK \"k\" member did not include the right length "
106 "of key data for the given algorithm.");
109 Status
Status::ErrorJwkEmptyBigInteger(const std::string
& member_name
) {
110 return Status(blink::WebCryptoErrorTypeData
,
111 "The JWK \"" + member_name
+ "\" member was empty.");
114 Status
Status::ErrorJwkBigIntegerHasLeadingZero(
115 const std::string
& member_name
) {
117 blink::WebCryptoErrorTypeData
,
118 "The JWK \"" + member_name
+ "\" member contained a leading zero.");
121 Status
Status::ErrorJwkDuplicateKeyOps() {
122 return Status(blink::WebCryptoErrorTypeData
,
123 "The \"key_ops\" member of the JWK dictionary contains "
124 "duplicate usages.");
127 Status
Status::ErrorUnsupportedImportKeyFormat() {
128 return Status(blink::WebCryptoErrorTypeNotSupported
,
129 "Unsupported import key format for algorithm");
132 Status
Status::ErrorUnsupportedExportKeyFormat() {
133 return Status(blink::WebCryptoErrorTypeNotSupported
,
134 "Unsupported export key format for algorithm");
137 Status
Status::ErrorImportAesKeyLength() {
138 return Status(blink::WebCryptoErrorTypeData
,
139 "AES key data must be 128 or 256 bits");
142 Status
Status::ErrorGetAesKeyLength() {
143 return Status(blink::WebCryptoErrorTypeOperation
,
144 "AES key length must be 128 or 256 bits");
147 Status
Status::ErrorGenerateAesKeyLength() {
148 return Status(blink::WebCryptoErrorTypeOperation
,
149 "AES key length must be 128 or 256 bits");
152 Status
Status::ErrorAes192BitUnsupported() {
153 return Status(blink::WebCryptoErrorTypeOperation
,
154 "192-bit AES keys are not supported");
157 Status
Status::ErrorUnexpectedKeyType() {
158 return Status(blink::WebCryptoErrorTypeInvalidAccess
,
159 "The key is not of the expected type");
162 Status
Status::ErrorIncorrectSizeAesCbcIv() {
163 return Status(blink::WebCryptoErrorTypeOperation
,
164 "The \"iv\" has an unexpected length -- must be 16 bytes");
167 Status
Status::ErrorIncorrectSizeAesCtrCounter() {
168 return Status(blink::WebCryptoErrorTypeOperation
,
169 "The \"counter\" has an unexpected length -- must be 16 bytes");
172 Status
Status::ErrorInvalidAesCtrCounterLength() {
173 return Status(blink::WebCryptoErrorTypeOperation
,
174 "The \"length\" member must be >= 1 and <= 128");
177 Status
Status::ErrorAesCtrInputTooLongCounterRepeated() {
178 return Status(blink::WebCryptoErrorTypeData
,
179 "The input is too large for the counter length.");
182 Status
Status::ErrorDataTooLarge() {
183 return Status(blink::WebCryptoErrorTypeOperation
,
184 "The provided data is too large");
187 Status
Status::ErrorDataTooSmall() {
188 return Status(blink::WebCryptoErrorTypeOperation
,
189 "The provided data is too small");
192 Status
Status::ErrorUnsupported() {
193 return ErrorUnsupported("The requested operation is unsupported");
196 Status
Status::ErrorUnsupported(const std::string
& message
) {
197 return Status(blink::WebCryptoErrorTypeNotSupported
, message
);
200 Status
Status::ErrorUnexpected() {
201 return Status(blink::WebCryptoErrorTypeOperation
,
202 "Something unexpected happened...");
205 Status
Status::ErrorInvalidAesGcmTagLength() {
207 blink::WebCryptoErrorTypeOperation
,
208 "The tag length is invalid: Must be 32, 64, 96, 104, 112, 120, or 128 "
212 Status
Status::ErrorInvalidAesKwDataLength() {
213 return Status(blink::WebCryptoErrorTypeData
,
214 "The AES-KW input data length is invalid: not a multiple of 8 "
218 Status
Status::ErrorGenerateKeyPublicExponent() {
219 return Status(blink::WebCryptoErrorTypeOperation
,
220 "The \"publicExponent\" must be either 3 or 65537");
223 Status
Status::ErrorImportRsaEmptyModulus() {
224 return Status(blink::WebCryptoErrorTypeData
, "The modulus is empty");
227 Status
Status::ErrorGenerateRsaUnsupportedModulus() {
228 return Status(blink::WebCryptoErrorTypeOperation
,
229 "The modulus length must be a multiple of 8 bits and >= 256 "
233 Status
Status::ErrorImportRsaEmptyExponent() {
234 return Status(blink::WebCryptoErrorTypeData
,
235 "No bytes for the exponent were provided");
238 Status
Status::ErrorKeyNotExtractable() {
239 return Status(blink::WebCryptoErrorTypeInvalidAccess
,
240 "They key is not extractable");
243 Status
Status::ErrorGenerateHmacKeyLengthZero() {
244 return Status(blink::WebCryptoErrorTypeOperation
,
245 "HMAC key length must not be zero");
248 Status
Status::ErrorHmacImportEmptyKey() {
249 return Status(blink::WebCryptoErrorTypeData
,
250 "HMAC key data must not be empty");
253 Status
Status::ErrorGetHmacKeyLengthZero() {
254 return Status(blink::WebCryptoErrorTypeType
,
255 "HMAC key length must not be zero");
258 Status
Status::ErrorHmacImportBadLength() {
260 blink::WebCryptoErrorTypeData
,
261 "The optional HMAC key length must be shorter than the key data, and by "
262 "no more than 7 bits.");
265 Status
Status::ErrorCreateKeyBadUsages() {
266 return Status(blink::WebCryptoErrorTypeSyntax
,
267 "Cannot create a key using the specified key usages.");
270 Status
Status::ErrorCreateKeyEmptyUsages() {
271 return Status(blink::WebCryptoErrorTypeSyntax
,
272 "Usages cannot be empty when creating a key.");
275 Status
Status::ErrorImportedEcKeyIncorrectCurve() {
277 blink::WebCryptoErrorTypeData
,
278 "The imported EC key specifies a different curve than requested");
281 Status
Status::ErrorJwkIncorrectCrv() {
283 blink::WebCryptoErrorTypeData
,
284 "The JWK's \"crv\" member specifies a different curve than requested");
287 Status
Status::ErrorEcKeyInvalid() {
288 return Status(blink::WebCryptoErrorTypeData
,
289 "The imported EC key is invalid");
292 Status
Status::JwkOctetStringWrongLength(const std::string
& member_name
,
293 size_t expected_length
,
294 size_t actual_length
) {
296 blink::WebCryptoErrorTypeData
,
298 "The JWK's \"%s\" member defines an octet string of length %" PRIuS
299 " bytes but should be %" PRIuS
,
300 member_name
.c_str(), actual_length
, expected_length
));
303 Status
Status::ErrorEcdhPublicKeyWrongType() {
305 blink::WebCryptoErrorTypeInvalidAccess
,
306 "The public parameter for ECDH key derivation is not a public EC key");
309 Status
Status::ErrorEcdhPublicKeyWrongAlgorithm() {
311 blink::WebCryptoErrorTypeInvalidAccess
,
312 "The public parameter for ECDH key derivation must be for ECDH");
315 Status
Status::ErrorEcdhCurveMismatch() {
316 return Status(blink::WebCryptoErrorTypeInvalidAccess
,
317 "The public parameter for ECDH key derivation is for a "
318 "different named curve");
321 Status
Status::ErrorEcdhLengthTooBig(unsigned int max_length_bits
) {
322 return Status(blink::WebCryptoErrorTypeOperation
,
324 "Length specified for ECDH key derivation is too large. "
325 "Maximum allowed is %u bits",
329 Status
Status::ErrorHkdfLengthTooLong() {
330 return Status(blink::WebCryptoErrorTypeOperation
,
331 "The length provided for HKDF is too large.");
334 Status
Status::ErrorHkdfDeriveBitsLengthNotSpecified() {
335 // TODO(nharper): The spec might change so that an OperationError should be
336 // thrown here instead of a TypeError.
337 // (https://www.w3.org/Bugs/Public/show_bug.cgi?id=27771)
338 return Status(blink::WebCryptoErrorTypeType
,
339 "No length was specified for the HKDF Derive Bits operation.");
342 Status
Status::ErrorPbkdf2InvalidLength() {
344 blink::WebCryptoErrorTypeOperation
,
345 "Length for PBKDF2 key derivation must be a multiple of 8 bits.");
348 Status
Status::ErrorPbkdf2DeriveBitsLengthNotSpecified() {
350 blink::WebCryptoErrorTypeOperation
,
351 "No length was specified for the PBKDF2 Derive Bits operation.");
354 Status::Status(blink::WebCryptoErrorType error_type
,
355 const std::string
& error_details_utf8
)
357 error_type_(error_type
),
358 error_details_(error_details_utf8
) {
361 Status::Status(Type type
) : type_(type
) {
364 } // namespace webcrypto