blob: 4747a7f0864ab35fda0d8c76c5215302686da670 [file] [log] [blame]
[email protected]37be4cfa2014-03-20 05:39:371// 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
erg56f12322015-04-17 00:51:485#include "components/webcrypto/status.h"
[email protected]37be4cfa2014-03-20 05:39:376
eromanb2ead6d2014-11-14 02:26:147#include "base/format_macros.h"
8#include "base/strings/stringprintf.h"
9
[email protected]37be4cfa2014-03-20 05:39:3710namespace webcrypto {
11
[email protected]c5039362014-04-28 19:10:3412bool Status::IsError() const {
13 return type_ == TYPE_ERROR;
[email protected]37be4cfa2014-03-20 05:39:3714}
15
[email protected]c5039362014-04-28 19:10:3416bool Status::IsSuccess() const {
17 return type_ == TYPE_SUCCESS;
18}
[email protected]37be4cfa2014-03-20 05:39:3719
[email protected]c5039362014-04-28 19:10:3420Status Status::Success() {
21 return Status(TYPE_SUCCESS);
22}
23
24Status Status::OperationError() {
25 return Status(blink::WebCryptoErrorTypeOperation, "");
26}
27
28Status Status::DataError() {
29 return Status(blink::WebCryptoErrorTypeData, "");
30}
[email protected]37be4cfa2014-03-20 05:39:3731
32Status Status::ErrorJwkNotDictionary() {
[email protected]c5039362014-04-28 19:10:3433 return Status(blink::WebCryptoErrorTypeData,
34 "JWK input could not be parsed to a JSON dictionary");
[email protected]37be4cfa2014-03-20 05:39:3735}
36
eroman398e7e12014-11-08 03:05:1037Status Status::ErrorJwkMemberMissing(const std::string& member_name) {
eroman8c565ba2014-12-10 20:59:0638 return Status(blink::WebCryptoErrorTypeData,
39 "The required JWK member \"" + member_name + "\" was missing");
[email protected]37be4cfa2014-03-20 05:39:3740}
41
eroman398e7e12014-11-08 03:05:1042Status Status::ErrorJwkMemberWrongType(const std::string& member_name,
43 const std::string& expected_type) {
[email protected]c5039362014-04-28 19:10:3444 return Status(
45 blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:0646 "The JWK member \"" + member_name + "\" must be a " + expected_type);
eroman398e7e12014-11-08 03:05:1047}
48
49Status Status::ErrorJwkBase64Decode(const std::string& member_name) {
eroman8c565ba2014-12-10 20:59:0650 return Status(blink::WebCryptoErrorTypeData,
51 "The JWK member \"" + member_name +
52 "\" could not be base64url decoded or contained padding");
[email protected]37be4cfa2014-03-20 05:39:3753}
54
55Status Status::ErrorJwkExtInconsistent() {
56 return Status(
[email protected]c5039362014-04-28 19:10:3457 blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:0658 "The \"ext\" member of the JWK dictionary is inconsistent what that "
[email protected]37be4cfa2014-03-20 05:39:3759 "specified by the Web Crypto call");
60}
61
[email protected]37be4cfa2014-03-20 05:39:3762Status Status::ErrorJwkAlgorithmInconsistent() {
[email protected]c5039362014-04-28 19:10:3463 return Status(blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:0664 "The JWK \"alg\" member was inconsistent with that specified "
[email protected]c5039362014-04-28 19:10:3465 "by the Web Crypto call");
[email protected]37be4cfa2014-03-20 05:39:3766}
67
[email protected]37be4cfa2014-03-20 05:39:3768Status Status::ErrorJwkUnrecognizedUse() {
[email protected]c5039362014-04-28 19:10:3469 return Status(blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:0670 "The JWK \"use\" member could not be parsed");
[email protected]37be4cfa2014-03-20 05:39:3771}
72
73Status Status::ErrorJwkUnrecognizedKeyop() {
[email protected]c5039362014-04-28 19:10:3474 return Status(blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:0675 "The JWK \"key_ops\" member could not be parsed");
[email protected]37be4cfa2014-03-20 05:39:3776}
77
78Status Status::ErrorJwkUseInconsistent() {
[email protected]c5039362014-04-28 19:10:3479 return Status(blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:0680 "The JWK \"use\" member was inconsistent with that specified "
[email protected]c5039362014-04-28 19:10:3481 "by the Web Crypto call. The JWK usage must be a superset of "
82 "those requested");
[email protected]37be4cfa2014-03-20 05:39:3783}
84
85Status Status::ErrorJwkKeyopsInconsistent() {
[email protected]c5039362014-04-28 19:10:3486 return Status(blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:0687 "The JWK \"key_ops\" member was inconsistent with that "
[email protected]c5039362014-04-28 19:10:3488 "specified by the Web Crypto call. The JWK usage must be a "
89 "superset of those requested");
[email protected]37be4cfa2014-03-20 05:39:3790}
91
92Status Status::ErrorJwkUseAndKeyopsInconsistent() {
[email protected]c5039362014-04-28 19:10:3493 return Status(blink::WebCryptoErrorTypeData,
94 "The JWK \"use\" and \"key_ops\" properties were both found "
95 "but are inconsistent with each other.");
[email protected]37be4cfa2014-03-20 05:39:3796}
97
[email protected]38409aec2014-07-19 00:54:5198Status Status::ErrorJwkUnexpectedKty(const std::string& expected) {
[email protected]c5039362014-04-28 19:10:3499 return Status(blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:06100 "The JWK \"kty\" member was not \"" + expected + "\"");
[email protected]37be4cfa2014-03-20 05:39:37101}
102
103Status Status::ErrorJwkIncorrectKeyLength() {
[email protected]c5039362014-04-28 19:10:34104 return Status(blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:06105 "The JWK \"k\" member did not include the right length "
[email protected]c5039362014-04-28 19:10:34106 "of key data for the given algorithm.");
[email protected]37be4cfa2014-03-20 05:39:37107}
108
eroman398e7e12014-11-08 03:05:10109Status Status::ErrorJwkEmptyBigInteger(const std::string& member_name) {
[email protected]6f82af22014-05-21 15:26:14110 return Status(blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:06111 "The JWK \"" + member_name + "\" member was empty.");
[email protected]31130d6c2014-07-26 19:59:14112}
113
eroman398e7e12014-11-08 03:05:10114Status Status::ErrorJwkBigIntegerHasLeadingZero(
115 const std::string& member_name) {
[email protected]31130d6c2014-07-26 19:59:14116 return Status(
117 blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:06118 "The JWK \"" + member_name + "\" member contained a leading zero.");
[email protected]6f82af22014-05-21 15:26:14119}
120
eromanca92d1e2014-10-28 01:15:18121Status Status::ErrorJwkDuplicateKeyOps() {
122 return Status(blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:06123 "The \"key_ops\" member of the JWK dictionary contains "
eromanca92d1e2014-10-28 01:15:18124 "duplicate usages.");
125}
126
[email protected]38409aec2014-07-19 00:54:51127Status Status::ErrorUnsupportedImportKeyFormat() {
128 return Status(blink::WebCryptoErrorTypeNotSupported,
129 "Unsupported import key format for algorithm");
130}
131
132Status Status::ErrorUnsupportedExportKeyFormat() {
133 return Status(blink::WebCryptoErrorTypeNotSupported,
134 "Unsupported export key format for algorithm");
135}
136
[email protected]c5039362014-04-28 19:10:34137Status Status::ErrorImportAesKeyLength() {
138 return Status(blink::WebCryptoErrorTypeData,
eromanbe884dce2014-11-26 21:23:29139 "AES key data must be 128 or 256 bits");
[email protected]37be4cfa2014-03-20 05:39:37140}
141
eromanf93fd5b2014-12-11 00:21:06142Status Status::ErrorGetAesKeyLength() {
143 return Status(blink::WebCryptoErrorTypeOperation,
144 "AES key length must be 128 or 256 bits");
145}
146
eroman9e9ed052014-11-26 03:00:40147Status Status::ErrorGenerateAesKeyLength() {
148 return Status(blink::WebCryptoErrorTypeOperation,
149 "AES key length must be 128 or 256 bits");
150}
151
[email protected]b05ba932014-06-13 08:49:15152Status Status::ErrorAes192BitUnsupported() {
habib.virjicffa1efa2014-12-19 11:41:29153 return Status(blink::WebCryptoErrorTypeOperation,
[email protected]b05ba932014-06-13 08:49:15154 "192-bit AES keys are not supported");
155}
156
[email protected]37be4cfa2014-03-20 05:39:37157Status Status::ErrorUnexpectedKeyType() {
[email protected]c5039362014-04-28 19:10:34158 return Status(blink::WebCryptoErrorTypeInvalidAccess,
159 "The key is not of the expected type");
[email protected]37be4cfa2014-03-20 05:39:37160}
161
162Status Status::ErrorIncorrectSizeAesCbcIv() {
xun.sun99cd2092014-12-04 17:41:02163 return Status(blink::WebCryptoErrorTypeOperation,
[email protected]c5039362014-04-28 19:10:34164 "The \"iv\" has an unexpected length -- must be 16 bytes");
[email protected]37be4cfa2014-03-20 05:39:37165}
166
eroman4d7a0e02014-08-27 00:30:33167Status Status::ErrorIncorrectSizeAesCtrCounter() {
xun.sun99cd2092014-12-04 17:41:02168 return Status(blink::WebCryptoErrorTypeOperation,
eroman4d7a0e02014-08-27 00:30:33169 "The \"counter\" has an unexpected length -- must be 16 bytes");
170}
171
172Status Status::ErrorInvalidAesCtrCounterLength() {
xun.sun99cd2092014-12-04 17:41:02173 return Status(blink::WebCryptoErrorTypeOperation,
eroman8c565ba2014-12-10 20:59:06174 "The \"length\" member must be >= 1 and <= 128");
eroman4d7a0e02014-08-27 00:30:33175}
176
177Status Status::ErrorAesCtrInputTooLongCounterRepeated() {
178 return Status(blink::WebCryptoErrorTypeData,
179 "The input is too large for the counter length.");
180}
181
[email protected]37be4cfa2014-03-20 05:39:37182Status Status::ErrorDataTooLarge() {
eromande1486b792014-12-29 23:30:50183 return Status(blink::WebCryptoErrorTypeOperation,
[email protected]c5039362014-04-28 19:10:34184 "The provided data is too large");
[email protected]37be4cfa2014-03-20 05:39:37185}
186
187Status Status::ErrorDataTooSmall() {
eromande1486b792014-12-29 23:30:50188 return Status(blink::WebCryptoErrorTypeOperation,
[email protected]c5039362014-04-28 19:10:34189 "The provided data is too small");
[email protected]37be4cfa2014-03-20 05:39:37190}
191
192Status Status::ErrorUnsupported() {
[email protected]cb7f3e32014-06-19 14:41:41193 return ErrorUnsupported("The requested operation is unsupported");
194}
195
196Status Status::ErrorUnsupported(const std::string& message) {
197 return Status(blink::WebCryptoErrorTypeNotSupported, message);
[email protected]37be4cfa2014-03-20 05:39:37198}
199
200Status Status::ErrorUnexpected() {
eroman67893a32014-12-12 21:46:52201 return Status(blink::WebCryptoErrorTypeOperation,
[email protected]c5039362014-04-28 19:10:34202 "Something unexpected happened...");
[email protected]37be4cfa2014-03-20 05:39:37203}
204
205Status Status::ErrorInvalidAesGcmTagLength() {
206 return Status(
xun.sune5737552014-12-02 23:52:24207 blink::WebCryptoErrorTypeOperation,
[email protected]37be4cfa2014-03-20 05:39:37208 "The tag length is invalid: Must be 32, 64, 96, 104, 112, 120, or 128 "
209 "bits");
210}
211
212Status Status::ErrorInvalidAesKwDataLength() {
[email protected]c5039362014-04-28 19:10:34213 return Status(blink::WebCryptoErrorTypeData,
214 "The AES-KW input data length is invalid: not a multiple of 8 "
215 "bytes");
[email protected]37be4cfa2014-03-20 05:39:37216}
217
218Status Status::ErrorGenerateKeyPublicExponent() {
habib.virjid6e19c902014-12-19 19:59:28219 return Status(blink::WebCryptoErrorTypeOperation,
[email protected]c360f832014-06-12 13:21:50220 "The \"publicExponent\" must be either 3 or 65537");
[email protected]37be4cfa2014-03-20 05:39:37221}
222
[email protected]37be4cfa2014-03-20 05:39:37223Status Status::ErrorImportRsaEmptyModulus() {
[email protected]c5039362014-04-28 19:10:34224 return Status(blink::WebCryptoErrorTypeData, "The modulus is empty");
[email protected]37be4cfa2014-03-20 05:39:37225}
226
[email protected]e9aa71a2014-07-25 08:38:02227Status Status::ErrorGenerateRsaUnsupportedModulus() {
habib.virjicffa1efa2014-12-19 11:41:29228 return Status(blink::WebCryptoErrorTypeOperation,
[email protected]e9aa71a2014-07-25 08:38:02229 "The modulus length must be a multiple of 8 bits and >= 256 "
230 "and <= 16384");
[email protected]37be4cfa2014-03-20 05:39:37231}
232
233Status Status::ErrorImportRsaEmptyExponent() {
[email protected]c5039362014-04-28 19:10:34234 return Status(blink::WebCryptoErrorTypeData,
235 "No bytes for the exponent were provided");
[email protected]37be4cfa2014-03-20 05:39:37236}
237
238Status Status::ErrorKeyNotExtractable() {
[email protected]c5039362014-04-28 19:10:34239 return Status(blink::WebCryptoErrorTypeInvalidAccess,
240 "They key is not extractable");
[email protected]37be4cfa2014-03-20 05:39:37241}
242
eroman9e9ed052014-11-26 03:00:40243Status Status::ErrorGenerateHmacKeyLengthZero() {
244 return Status(blink::WebCryptoErrorTypeOperation,
xun.sune5737552014-12-02 23:52:24245 "HMAC key length must not be zero");
eroman9e9ed052014-11-26 03:00:40246}
247
eroman825de712014-12-29 22:34:10248Status Status::ErrorHmacImportEmptyKey() {
249 return Status(blink::WebCryptoErrorTypeData,
250 "HMAC key data must not be empty");
251}
252
eromanf93fd5b2014-12-11 00:21:06253Status Status::ErrorGetHmacKeyLengthZero() {
254 return Status(blink::WebCryptoErrorTypeType,
255 "HMAC key length must not be zero");
256}
257
eroman5d5199742014-12-10 22:18:07258Status Status::ErrorHmacImportBadLength() {
259 return Status(
260 blink::WebCryptoErrorTypeData,
261 "The optional HMAC key length must be shorter than the key data, and by "
262 "no more than 7 bits.");
263}
264
[email protected]9c2e9cf2014-05-23 23:13:47265Status Status::ErrorCreateKeyBadUsages() {
xun.sun60b6b982014-11-04 22:57:19266 return Status(blink::WebCryptoErrorTypeSyntax,
[email protected]9c2e9cf2014-05-23 23:13:47267 "Cannot create a key using the specified key usages.");
268}
269
nharper70f820a2014-12-03 20:36:07270Status Status::ErrorCreateKeyEmptyUsages() {
271 return Status(blink::WebCryptoErrorTypeSyntax,
272 "Usages cannot be empty when creating a key.");
273}
274
eromanb2ead6d2014-11-14 02:26:14275Status Status::ErrorImportedEcKeyIncorrectCurve() {
276 return Status(
277 blink::WebCryptoErrorTypeData,
278 "The imported EC key specifies a different curve than requested");
279}
280
281Status Status::ErrorJwkIncorrectCrv() {
282 return Status(
283 blink::WebCryptoErrorTypeData,
284 "The JWK's \"crv\" member specifies a different curve than requested");
285}
286
287Status Status::ErrorEcKeyInvalid() {
288 return Status(blink::WebCryptoErrorTypeData,
289 "The imported EC key is invalid");
290}
291
292Status Status::JwkOctetStringWrongLength(const std::string& member_name,
293 size_t expected_length,
294 size_t actual_length) {
295 return Status(
296 blink::WebCryptoErrorTypeData,
297 base::StringPrintf(
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));
301}
302
eromaned48e812014-11-28 19:59:13303Status Status::ErrorEcdhPublicKeyWrongType() {
304 return Status(
305 blink::WebCryptoErrorTypeInvalidAccess,
306 "The public parameter for ECDH key derivation is not a public EC key");
307}
308
309Status Status::ErrorEcdhPublicKeyWrongAlgorithm() {
310 return Status(
311 blink::WebCryptoErrorTypeInvalidAccess,
312 "The public parameter for ECDH key derivation must be for ECDH");
313}
314
315Status Status::ErrorEcdhCurveMismatch() {
316 return Status(blink::WebCryptoErrorTypeInvalidAccess,
317 "The public parameter for ECDH key derivation is for a "
318 "different named curve");
319}
320
321Status Status::ErrorEcdhLengthTooBig(unsigned int max_length_bits) {
eromancb4e417a2014-12-05 21:32:29322 return Status(blink::WebCryptoErrorTypeOperation,
eromaned48e812014-11-28 19:59:13323 base::StringPrintf(
324 "Length specified for ECDH key derivation is too large. "
325 "Maximum allowed is %u bits",
326 max_length_bits));
327}
328
nharper651031792015-01-13 18:10:39329Status Status::ErrorHkdfLengthTooLong() {
330 return Status(blink::WebCryptoErrorTypeOperation,
331 "The length provided for HKDF is too large.");
332}
333
334Status 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.");
340}
341
xun.sun22a80e72015-01-21 13:57:19342Status Status::ErrorPbkdf2InvalidLength() {
343 return Status(
344 blink::WebCryptoErrorTypeOperation,
345 "Length for PBKDF2 key derivation must be a multiple of 8 bits.");
346}
347
348Status Status::ErrorPbkdf2DeriveBitsLengthNotSpecified() {
349 return Status(
350 blink::WebCryptoErrorTypeOperation,
351 "No length was specified for the PBKDF2 Derive Bits operation.");
352}
353
eroman79616642015-10-05 21:18:14354Status Status::ErrorPbkdf2Iterations0() {
355 return Status(blink::WebCryptoErrorTypeOperation,
356 "PBKDF2 requires iterations > 0");
357}
358
[email protected]c5039362014-04-28 19:10:34359Status::Status(blink::WebCryptoErrorType error_type,
360 const std::string& error_details_utf8)
361 : type_(TYPE_ERROR),
362 error_type_(error_type),
363 error_details_(error_details_utf8) {
364}
[email protected]37be4cfa2014-03-20 05:39:37365
[email protected]c5039362014-04-28 19:10:34366Status::Status(Type type) : type_(type) {
367}
[email protected]37be4cfa2014-03-20 05:39:37368
369} // namespace webcrypto