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 #ifndef COMPONENTS_WEBCRYPTO_STATUS_H_
6 #define COMPONENTS_WEBCRYPTO_STATUS_H_
9 #include "third_party/WebKit/public/platform/WebCrypto.h"
13 // Status indicates whether an operation completed successfully, or with an
14 // error. The error is used for verification in unit-tests, as well as for
15 // display to the user.
17 // As such, it is important that errors DO NOT reveal any sensitive material
21 Status() : type_(TYPE_ERROR
) {}
23 // Returns true if the Status represents an error (any one of them).
26 // Returns true if the Status represent success.
27 bool IsSuccess() const;
29 // Returns a UTF-8 error message (non-localized) describing the error.
30 const std::string
& error_details() const { return error_details_
; }
32 blink::WebCryptoErrorType
error_type() const { return error_type_
; }
34 // Constructs a status representing success.
35 static Status
Success();
37 // Constructs a status representing a generic operation error. It contains no
39 static Status
OperationError();
41 // Constructs a status representing a generic data error. It contains no
43 static Status
DataError();
45 // ------------------------------------
46 // Errors when importing a JWK formatted key
47 // ------------------------------------
49 // The key bytes could not parsed as JSON dictionary. This either
50 // means there was a parsing error, or the JSON object was not
51 // convertable to a dictionary.
52 static Status
ErrorJwkNotDictionary();
54 // The required JWK member |member_name| was missing.
55 static Status
ErrorJwkMemberMissing(const std::string
& member_name
);
57 // The JWK member |member_name| was not of type |expected_type|.
58 static Status
ErrorJwkMemberWrongType(const std::string
& member_name
,
59 const std::string
& expected_type
);
61 // The JWK member |member_name| was a string, however could not be
62 // successfully base64 decoded.
63 static Status
ErrorJwkBase64Decode(const std::string
& member_name
);
65 // The "ext" parameter was specified but was
66 // incompatible with the value requested by the Web Crypto call.
67 static Status
ErrorJwkExtInconsistent();
69 // The "alg" parameter is incompatible with the (optional) Algorithm
70 // specified by the Web Crypto import operation.
71 static Status
ErrorJwkAlgorithmInconsistent();
73 // The "use" parameter was specified, however it couldn't be converted to an
74 // equivalent Web Crypto usage.
75 static Status
ErrorJwkUnrecognizedUse();
77 // The "key_ops" parameter was specified, however one of the values in the
78 // array couldn't be converted to an equivalent Web Crypto usage.
79 static Status
ErrorJwkUnrecognizedKeyop();
81 // The "use" parameter was specified, however it is incompatible with that
82 // specified by the Web Crypto import operation.
83 static Status
ErrorJwkUseInconsistent();
85 // The "key_ops" parameter was specified, however it is incompatible with that
86 // specified by the Web Crypto import operation.
87 static Status
ErrorJwkKeyopsInconsistent();
89 // Both the "key_ops" and the "use" parameters were specified, however they
90 // are incompatible with each other.
91 static Status
ErrorJwkUseAndKeyopsInconsistent();
93 // The "kty" parameter was given and was a string, however it was not the
95 static Status
ErrorJwkUnexpectedKty(const std::string
& expected
);
97 // The amount of key data provided was incompatible with the selected
98 // algorithm. For instance if the algorith name was A128CBC then EXACTLY
99 // 128-bits of key data must have been provided. If 192-bits of key data were
100 // given that is an error.
101 static Status
ErrorJwkIncorrectKeyLength();
103 // The JWK member |member_name| is supposed to represent a big-endian unsigned
104 // integer, however was the empty string.
105 static Status
ErrorJwkEmptyBigInteger(const std::string
& member_name
);
107 // The big-endian unsigned integer |member_name| contained leading zeros. This
108 // violates the JWA requirement that such octet strings be minimal.
109 static Status
ErrorJwkBigIntegerHasLeadingZero(
110 const std::string
& member_name
);
112 // The key_ops lists a usage more than once.
113 static Status
ErrorJwkDuplicateKeyOps();
115 // ------------------------------------
117 // ------------------------------------
119 // Tried importing a key using an unsupported format for the key type (for
120 // instance importing an HMAC key using format=spki).
121 static Status
ErrorUnsupportedImportKeyFormat();
123 // Tried exporting a key using an unsupported format for the key type (for
124 // instance exporting an HMAC key using format=spki).
125 static Status
ErrorUnsupportedExportKeyFormat();
127 // The key data buffer provided for importKey() is an incorrect length for
129 static Status
ErrorImportAesKeyLength();
131 // The length specified when deriving an AES key was not 128 or 256 bits.
132 static Status
ErrorGetAesKeyLength();
134 // Attempted to generate an AES key with an invalid length.
135 static Status
ErrorGenerateAesKeyLength();
137 // 192-bit AES keys are valid, however unsupported.
138 static Status
ErrorAes192BitUnsupported();
140 // The wrong key was used for the operation. For instance, a public key was
141 // used to verify a RsaSsaPkcs1v1_5 signature, or tried exporting a private
142 // key using spki format.
143 static Status
ErrorUnexpectedKeyType();
145 // When doing an AES-CBC encryption/decryption, the "iv" parameter was not 16
147 static Status
ErrorIncorrectSizeAesCbcIv();
149 // When doing AES-CTR encryption/decryption, the "counter" parameter was not
151 static Status
ErrorIncorrectSizeAesCtrCounter();
153 // When doing AES-CTR encryption/decryption, the "length" parameter for the
154 // counter was out of range.
155 static Status
ErrorInvalidAesCtrCounterLength();
157 // The input to encrypt/decrypt was too large. Based on the counter size, it
158 // would cause the counter to wraparound and repeat earlier values.
159 static Status
ErrorAesCtrInputTooLongCounterRepeated();
161 // The data provided to an encrypt/decrypt/sign/verify operation was too
162 // large. This can either represent an internal limitation (for instance
163 // representing buffer lengths as uints).
164 static Status
ErrorDataTooLarge();
166 // The data provided to an encrypt/decrypt/sign/verify operation was too
167 // small. This usually represents an algorithm restriction (for instance
168 // AES-KW requires a minimum of 24 bytes input data).
169 static Status
ErrorDataTooSmall();
171 // Something was unsupported or unimplemented. This can mean the algorithm in
172 // question was unsupported, some parameter combination was unsupported, or
173 // something has not yet been implemented.
174 static Status
ErrorUnsupported();
175 static Status
ErrorUnsupported(const std::string
& message
);
177 // Something unexpected happened in the code, which implies there is a
178 // source-level bug. These should not happen, but safer to fail than simply
180 static Status
ErrorUnexpected();
182 // The authentication tag length specified for AES-GCM encrypt/decrypt was
183 // not 32, 64, 96, 104, 112, 120, or 128.
184 static Status
ErrorInvalidAesGcmTagLength();
186 // The input data given to an AES-KW encrypt/decrypt operation was not a
187 // multiple of 8 bytes, as required by RFC 3394.
188 static Status
ErrorInvalidAesKwDataLength();
190 // The "publicExponent" used to generate a key was invalid or unsupported.
191 // Only values of 3 and 65537 are allowed.
192 static Status
ErrorGenerateKeyPublicExponent();
194 // The modulus bytes were empty when importing an RSA public key.
195 static Status
ErrorImportRsaEmptyModulus();
197 // The modulus length was unsupported when generating an RSA key pair.
198 static Status
ErrorGenerateRsaUnsupportedModulus();
200 // The exponent bytes were empty when importing an RSA public key.
201 static Status
ErrorImportRsaEmptyExponent();
203 // An unextractable key was used by an operation which exports the key data.
204 static Status
ErrorKeyNotExtractable();
206 // Attempted to generate an HMAC key using a key length of 0.
207 static Status
ErrorGenerateHmacKeyLengthZero();
209 // Attempted to import an HMAC key containing no data.
210 static Status
ErrorHmacImportEmptyKey();
212 // Attempted to derive an HMAC key with zero length.
213 static Status
ErrorGetHmacKeyLengthZero();
215 // Attempted to import an HMAC key using a bad optional length.
216 static Status
ErrorHmacImportBadLength();
218 // Attempted to create a key (either by importKey(), generateKey(), or
219 // unwrapKey()) however the key usages were not applicable for the key type
221 static Status
ErrorCreateKeyBadUsages();
223 // No usages were specified when generating/importing a secret or private key.
224 static Status
ErrorCreateKeyEmptyUsages();
226 // An EC key imported using SPKI/PKCS8 format had the wrong curve specified in
228 static Status
ErrorImportedEcKeyIncorrectCurve();
230 // The "crv" member for a JWK did not match the expectations from importKey()
231 static Status
ErrorJwkIncorrectCrv();
233 // The EC key failed validation (coordinates don't lie on curve, out of range,
235 static Status
ErrorEcKeyInvalid();
237 // The octet string |member_name| was expected to be |expected_length| bytes
238 // long, but was instead |actual_length| bytes long.
239 static Status
JwkOctetStringWrongLength(const std::string
& member_name
,
240 size_t expected_length
,
241 size_t actual_length
);
243 // The public key given for ECDH key derivation was not an EC public key.
244 static Status
ErrorEcdhPublicKeyWrongType();
246 // The public key's algorithm was not ECDH.
247 static Status
ErrorEcdhPublicKeyWrongAlgorithm();
249 // The public and private keys given to ECDH key derivation were not for the
251 static Status
ErrorEcdhCurveMismatch();
253 // The requested bit length for ECDH key derivation was too large.
254 static Status
ErrorEcdhLengthTooBig(unsigned int max_length_bits
);
256 // The requested length for HKDF was too large.
257 static Status
ErrorHkdfLengthTooLong();
259 // No length parameter was provided for HKDF's Derive Bits operation.
260 static Status
ErrorHkdfDeriveBitsLengthNotSpecified();
262 // The requested bit length for PBKDF2 key derivation was invalid.
263 static Status
ErrorPbkdf2InvalidLength();
265 // No length parameter was provided for PBKDF2's Derive Bits operation.
266 static Status
ErrorPbkdf2DeriveBitsLengthNotSpecified();
269 enum Type
{ TYPE_ERROR
, TYPE_SUCCESS
};
271 // Constructs an error with the specified error type and message.
272 Status(blink::WebCryptoErrorType error_type
,
273 const std::string
& error_details_utf8
);
275 // Constructs a success or error without any details.
276 explicit Status(Type type
);
279 blink::WebCryptoErrorType error_type_
;
280 std::string error_details_
;
283 } // namespace webcrypto
285 #endif // COMPONENTS_WEBCRYPTO_STATUS_H_