blob: 48814e506c12603725e3bd67be8cde9e7f1e470d [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
5#include "content/child/webcrypto/status.h"
6
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 content {
11
[email protected]37be4cfa2014-03-20 05:39:3712namespace webcrypto {
13
[email protected]c5039362014-04-28 19:10:3414bool Status::IsError() const {
15 return type_ == TYPE_ERROR;
[email protected]37be4cfa2014-03-20 05:39:3716}
17
[email protected]c5039362014-04-28 19:10:3418bool Status::IsSuccess() const {
19 return type_ == TYPE_SUCCESS;
20}
[email protected]37be4cfa2014-03-20 05:39:3721
[email protected]c5039362014-04-28 19:10:3422Status Status::Success() {
23 return Status(TYPE_SUCCESS);
24}
25
26Status Status::OperationError() {
27 return Status(blink::WebCryptoErrorTypeOperation, "");
28}
29
30Status Status::DataError() {
31 return Status(blink::WebCryptoErrorTypeData, "");
32}
[email protected]37be4cfa2014-03-20 05:39:3733
34Status Status::ErrorJwkNotDictionary() {
[email protected]c5039362014-04-28 19:10:3435 return Status(blink::WebCryptoErrorTypeData,
36 "JWK input could not be parsed to a JSON dictionary");
[email protected]37be4cfa2014-03-20 05:39:3737}
38
eroman398e7e12014-11-08 03:05:1039Status Status::ErrorJwkMemberMissing(const std::string& member_name) {
eroman8c565ba2014-12-10 20:59:0640 return Status(blink::WebCryptoErrorTypeData,
41 "The required JWK member \"" + member_name + "\" was missing");
[email protected]37be4cfa2014-03-20 05:39:3742}
43
eroman398e7e12014-11-08 03:05:1044Status Status::ErrorJwkMemberWrongType(const std::string& member_name,
45 const std::string& expected_type) {
[email protected]c5039362014-04-28 19:10:3446 return Status(
47 blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:0648 "The JWK member \"" + member_name + "\" must be a " + expected_type);
eroman398e7e12014-11-08 03:05:1049}
50
51Status Status::ErrorJwkBase64Decode(const std::string& member_name) {
eroman8c565ba2014-12-10 20:59:0652 return Status(blink::WebCryptoErrorTypeData,
53 "The JWK member \"" + member_name +
54 "\" could not be base64url decoded or contained padding");
[email protected]37be4cfa2014-03-20 05:39:3755}
56
57Status Status::ErrorJwkExtInconsistent() {
58 return Status(
[email protected]c5039362014-04-28 19:10:3459 blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:0660 "The \"ext\" member of the JWK dictionary is inconsistent what that "
[email protected]37be4cfa2014-03-20 05:39:3761 "specified by the Web Crypto call");
62}
63
[email protected]37be4cfa2014-03-20 05:39:3764Status Status::ErrorJwkAlgorithmInconsistent() {
[email protected]c5039362014-04-28 19:10:3465 return Status(blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:0666 "The JWK \"alg\" member was inconsistent with that specified "
[email protected]c5039362014-04-28 19:10:3467 "by the Web Crypto call");
[email protected]37be4cfa2014-03-20 05:39:3768}
69
[email protected]37be4cfa2014-03-20 05:39:3770Status Status::ErrorJwkUnrecognizedUse() {
[email protected]c5039362014-04-28 19:10:3471 return Status(blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:0672 "The JWK \"use\" member could not be parsed");
[email protected]37be4cfa2014-03-20 05:39:3773}
74
75Status Status::ErrorJwkUnrecognizedKeyop() {
[email protected]c5039362014-04-28 19:10:3476 return Status(blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:0677 "The JWK \"key_ops\" member could not be parsed");
[email protected]37be4cfa2014-03-20 05:39:3778}
79
80Status Status::ErrorJwkUseInconsistent() {
[email protected]c5039362014-04-28 19:10:3481 return Status(blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:0682 "The JWK \"use\" member was inconsistent with that specified "
[email protected]c5039362014-04-28 19:10:3483 "by the Web Crypto call. The JWK usage must be a superset of "
84 "those requested");
[email protected]37be4cfa2014-03-20 05:39:3785}
86
87Status Status::ErrorJwkKeyopsInconsistent() {
[email protected]c5039362014-04-28 19:10:3488 return Status(blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:0689 "The JWK \"key_ops\" member was inconsistent with that "
[email protected]c5039362014-04-28 19:10:3490 "specified by the Web Crypto call. The JWK usage must be a "
91 "superset of those requested");
[email protected]37be4cfa2014-03-20 05:39:3792}
93
94Status Status::ErrorJwkUseAndKeyopsInconsistent() {
[email protected]c5039362014-04-28 19:10:3495 return Status(blink::WebCryptoErrorTypeData,
96 "The JWK \"use\" and \"key_ops\" properties were both found "
97 "but are inconsistent with each other.");
[email protected]37be4cfa2014-03-20 05:39:3798}
99
[email protected]38409aec2014-07-19 00:54:51100Status Status::ErrorJwkUnexpectedKty(const std::string& expected) {
[email protected]c5039362014-04-28 19:10:34101 return Status(blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:06102 "The JWK \"kty\" member was not \"" + expected + "\"");
[email protected]37be4cfa2014-03-20 05:39:37103}
104
105Status Status::ErrorJwkIncorrectKeyLength() {
[email protected]c5039362014-04-28 19:10:34106 return Status(blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:06107 "The JWK \"k\" member did not include the right length "
[email protected]c5039362014-04-28 19:10:34108 "of key data for the given algorithm.");
[email protected]37be4cfa2014-03-20 05:39:37109}
110
eroman398e7e12014-11-08 03:05:10111Status Status::ErrorJwkEmptyBigInteger(const std::string& member_name) {
[email protected]6f82af22014-05-21 15:26:14112 return Status(blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:06113 "The JWK \"" + member_name + "\" member was empty.");
[email protected]31130d6c2014-07-26 19:59:14114}
115
eroman398e7e12014-11-08 03:05:10116Status Status::ErrorJwkBigIntegerHasLeadingZero(
117 const std::string& member_name) {
[email protected]31130d6c2014-07-26 19:59:14118 return Status(
119 blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:06120 "The JWK \"" + member_name + "\" member contained a leading zero.");
[email protected]6f82af22014-05-21 15:26:14121}
122
eromanca92d1e2014-10-28 01:15:18123Status Status::ErrorJwkDuplicateKeyOps() {
124 return Status(blink::WebCryptoErrorTypeData,
eroman8c565ba2014-12-10 20:59:06125 "The \"key_ops\" member of the JWK dictionary contains "
eromanca92d1e2014-10-28 01:15:18126 "duplicate usages.");
127}
128
[email protected]37be4cfa2014-03-20 05:39:37129Status Status::ErrorImportEmptyKeyData() {
[email protected]c5039362014-04-28 19:10:34130 return Status(blink::WebCryptoErrorTypeData, "No key data was provided");
131}
132
[email protected]38409aec2014-07-19 00:54:51133Status Status::ErrorUnsupportedImportKeyFormat() {
134 return Status(blink::WebCryptoErrorTypeNotSupported,
135 "Unsupported import key format for algorithm");
136}
137
138Status Status::ErrorUnsupportedExportKeyFormat() {
139 return Status(blink::WebCryptoErrorTypeNotSupported,
140 "Unsupported export key format for algorithm");
141}
142
[email protected]c5039362014-04-28 19:10:34143Status Status::ErrorImportAesKeyLength() {
144 return Status(blink::WebCryptoErrorTypeData,
eromanbe884dce2014-11-26 21:23:29145 "AES key data must be 128 or 256 bits");
[email protected]37be4cfa2014-03-20 05:39:37146}
147
eromanf93fd5b2014-12-11 00:21:06148Status Status::ErrorGetAesKeyLength() {
149 return Status(blink::WebCryptoErrorTypeOperation,
150 "AES key length must be 128 or 256 bits");
151}
152
eroman9e9ed052014-11-26 03:00:40153Status Status::ErrorGenerateAesKeyLength() {
154 return Status(blink::WebCryptoErrorTypeOperation,
155 "AES key length must be 128 or 256 bits");
156}
157
[email protected]b05ba932014-06-13 08:49:15158Status Status::ErrorAes192BitUnsupported() {
159 return Status(blink::WebCryptoErrorTypeNotSupported,
160 "192-bit AES keys are not supported");
161}
162
[email protected]37be4cfa2014-03-20 05:39:37163Status Status::ErrorUnexpectedKeyType() {
[email protected]c5039362014-04-28 19:10:34164 return Status(blink::WebCryptoErrorTypeInvalidAccess,
165 "The key is not of the expected type");
[email protected]37be4cfa2014-03-20 05:39:37166}
167
168Status Status::ErrorIncorrectSizeAesCbcIv() {
xun.sun99cd2092014-12-04 17:41:02169 return Status(blink::WebCryptoErrorTypeOperation,
[email protected]c5039362014-04-28 19:10:34170 "The \"iv\" has an unexpected length -- must be 16 bytes");
[email protected]37be4cfa2014-03-20 05:39:37171}
172
eroman4d7a0e02014-08-27 00:30:33173Status Status::ErrorIncorrectSizeAesCtrCounter() {
xun.sun99cd2092014-12-04 17:41:02174 return Status(blink::WebCryptoErrorTypeOperation,
eroman4d7a0e02014-08-27 00:30:33175 "The \"counter\" has an unexpected length -- must be 16 bytes");
176}
177
178Status Status::ErrorInvalidAesCtrCounterLength() {
xun.sun99cd2092014-12-04 17:41:02179 return Status(blink::WebCryptoErrorTypeOperation,
eroman8c565ba2014-12-10 20:59:06180 "The \"length\" member must be >= 1 and <= 128");
eroman4d7a0e02014-08-27 00:30:33181}
182
183Status Status::ErrorAesCtrInputTooLongCounterRepeated() {
184 return Status(blink::WebCryptoErrorTypeData,
185 "The input is too large for the counter length.");
186}
187
[email protected]37be4cfa2014-03-20 05:39:37188Status Status::ErrorDataTooLarge() {
[email protected]c5039362014-04-28 19:10:34189 return Status(blink::WebCryptoErrorTypeData,
190 "The provided data is too large");
[email protected]37be4cfa2014-03-20 05:39:37191}
192
193Status Status::ErrorDataTooSmall() {
[email protected]c5039362014-04-28 19:10:34194 return Status(blink::WebCryptoErrorTypeData,
195 "The provided data is too small");
[email protected]37be4cfa2014-03-20 05:39:37196}
197
198Status Status::ErrorUnsupported() {
[email protected]cb7f3e32014-06-19 14:41:41199 return ErrorUnsupported("The requested operation is unsupported");
200}
201
202Status Status::ErrorUnsupported(const std::string& message) {
203 return Status(blink::WebCryptoErrorTypeNotSupported, message);
[email protected]37be4cfa2014-03-20 05:39:37204}
205
206Status Status::ErrorUnexpected() {
[email protected]c5039362014-04-28 19:10:34207 return Status(blink::WebCryptoErrorTypeUnknown,
208 "Something unexpected happened...");
[email protected]37be4cfa2014-03-20 05:39:37209}
210
211Status Status::ErrorInvalidAesGcmTagLength() {
212 return Status(
xun.sune5737552014-12-02 23:52:24213 blink::WebCryptoErrorTypeOperation,
[email protected]37be4cfa2014-03-20 05:39:37214 "The tag length is invalid: Must be 32, 64, 96, 104, 112, 120, or 128 "
215 "bits");
216}
217
218Status Status::ErrorInvalidAesKwDataLength() {
[email protected]c5039362014-04-28 19:10:34219 return Status(blink::WebCryptoErrorTypeData,
220 "The AES-KW input data length is invalid: not a multiple of 8 "
221 "bytes");
[email protected]37be4cfa2014-03-20 05:39:37222}
223
224Status Status::ErrorGenerateKeyPublicExponent() {
[email protected]c5039362014-04-28 19:10:34225 return Status(blink::WebCryptoErrorTypeData,
[email protected]c360f832014-06-12 13:21:50226 "The \"publicExponent\" must be either 3 or 65537");
[email protected]37be4cfa2014-03-20 05:39:37227}
228
[email protected]37be4cfa2014-03-20 05:39:37229Status Status::ErrorImportRsaEmptyModulus() {
[email protected]c5039362014-04-28 19:10:34230 return Status(blink::WebCryptoErrorTypeData, "The modulus is empty");
[email protected]37be4cfa2014-03-20 05:39:37231}
232
[email protected]e9aa71a2014-07-25 08:38:02233Status Status::ErrorGenerateRsaUnsupportedModulus() {
234 return Status(blink::WebCryptoErrorTypeNotSupported,
235 "The modulus length must be a multiple of 8 bits and >= 256 "
236 "and <= 16384");
[email protected]37be4cfa2014-03-20 05:39:37237}
238
239Status Status::ErrorImportRsaEmptyExponent() {
[email protected]c5039362014-04-28 19:10:34240 return Status(blink::WebCryptoErrorTypeData,
241 "No bytes for the exponent were provided");
[email protected]37be4cfa2014-03-20 05:39:37242}
243
244Status Status::ErrorKeyNotExtractable() {
[email protected]c5039362014-04-28 19:10:34245 return Status(blink::WebCryptoErrorTypeInvalidAccess,
246 "They key is not extractable");
[email protected]37be4cfa2014-03-20 05:39:37247}
248
eroman9e9ed052014-11-26 03:00:40249Status Status::ErrorGenerateHmacKeyLengthZero() {
250 return Status(blink::WebCryptoErrorTypeOperation,
xun.sune5737552014-12-02 23:52:24251 "HMAC key length must not be zero");
eroman9e9ed052014-11-26 03:00:40252}
253
eromanf93fd5b2014-12-11 00:21:06254Status Status::ErrorGetHmacKeyLengthZero() {
255 return Status(blink::WebCryptoErrorTypeType,
256 "HMAC key length must not be zero");
257}
258
eroman5d5199742014-12-10 22:18:07259Status Status::ErrorHmacImportBadLength() {
260 return Status(
261 blink::WebCryptoErrorTypeData,
262 "The optional HMAC key length must be shorter than the key data, and by "
263 "no more than 7 bits.");
264}
265
[email protected]9c2e9cf2014-05-23 23:13:47266Status Status::ErrorCreateKeyBadUsages() {
xun.sun60b6b982014-11-04 22:57:19267 return Status(blink::WebCryptoErrorTypeSyntax,
[email protected]9c2e9cf2014-05-23 23:13:47268 "Cannot create a key using the specified key usages.");
269}
270
nharper70f820a2014-12-03 20:36:07271Status Status::ErrorCreateKeyEmptyUsages() {
272 return Status(blink::WebCryptoErrorTypeSyntax,
273 "Usages cannot be empty when creating a key.");
274}
275
eromanb2ead6d2014-11-14 02:26:14276Status Status::ErrorImportedEcKeyIncorrectCurve() {
277 return Status(
278 blink::WebCryptoErrorTypeData,
279 "The imported EC key specifies a different curve than requested");
280}
281
282Status Status::ErrorJwkIncorrectCrv() {
283 return Status(
284 blink::WebCryptoErrorTypeData,
285 "The JWK's \"crv\" member specifies a different curve than requested");
286}
287
288Status Status::ErrorEcKeyInvalid() {
289 return Status(blink::WebCryptoErrorTypeData,
290 "The imported EC key is invalid");
291}
292
293Status Status::JwkOctetStringWrongLength(const std::string& member_name,
294 size_t expected_length,
295 size_t actual_length) {
296 return Status(
297 blink::WebCryptoErrorTypeData,
298 base::StringPrintf(
299 "The JWK's \"%s\" member defines an octet string of length %" PRIuS
300 " bytes but should be %" PRIuS,
301 member_name.c_str(), actual_length, expected_length));
302}
303
eromaned48e812014-11-28 19:59:13304Status Status::ErrorEcdhPublicKeyWrongType() {
305 return Status(
306 blink::WebCryptoErrorTypeInvalidAccess,
307 "The public parameter for ECDH key derivation is not a public EC key");
308}
309
310Status Status::ErrorEcdhPublicKeyWrongAlgorithm() {
311 return Status(
312 blink::WebCryptoErrorTypeInvalidAccess,
313 "The public parameter for ECDH key derivation must be for ECDH");
314}
315
316Status Status::ErrorEcdhCurveMismatch() {
317 return Status(blink::WebCryptoErrorTypeInvalidAccess,
318 "The public parameter for ECDH key derivation is for a "
319 "different named curve");
320}
321
322Status Status::ErrorEcdhLengthTooBig(unsigned int max_length_bits) {
eromancb4e417a2014-12-05 21:32:29323 return Status(blink::WebCryptoErrorTypeOperation,
eromaned48e812014-11-28 19:59:13324 base::StringPrintf(
325 "Length specified for ECDH key derivation is too large. "
326 "Maximum allowed is %u bits",
327 max_length_bits));
328}
329
[email protected]c5039362014-04-28 19:10:34330Status::Status(blink::WebCryptoErrorType error_type,
331 const std::string& error_details_utf8)
332 : type_(TYPE_ERROR),
333 error_type_(error_type),
334 error_details_(error_details_utf8) {
335}
[email protected]37be4cfa2014-03-20 05:39:37336
[email protected]c5039362014-04-28 19:10:34337Status::Status(Type type) : type_(type) {
338}
[email protected]37be4cfa2014-03-20 05:39:37339
340} // namespace webcrypto
341
342} // namespace content