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 CONTENT_CHILD_WEBCRYPTO_STATUS_H_
6 #define CONTENT_CHILD_WEBCRYPTO_STATUS_H_
9 #include "content/common/content_export.h"
10 #include "third_party/WebKit/public/platform/WebCrypto.h"
16 // Status indicates whether an operation completed successfully, or with an
17 // error. The error is used for verification in unit-tests, as well as for
18 // display to the user.
20 // As such, it is important that errors DO NOT reveal any sensitive material
22 class CONTENT_EXPORT Status
{
24 Status() : type_(TYPE_ERROR
) {}
26 // Returns true if the Status represents an error (any one of them).
29 // Returns true if the Status represent success.
30 bool IsSuccess() const;
32 // Returns a UTF-8 error message (non-localized) describing the error.
33 const std::string
& error_details() const { return error_details_
; }
35 blink::WebCryptoErrorType
error_type() const { return error_type_
; }
37 // Constructs a status representing success.
38 static Status
Success();
40 // Constructs a status representing a generic operation error. It contains no
42 static Status
OperationError();
44 // Constructs a status representing a generic data error. It contains no
46 static Status
DataError();
48 // ------------------------------------
49 // Errors when importing a JWK formatted key
50 // ------------------------------------
52 // The key bytes could not parsed as JSON dictionary. This either
53 // means there was a parsing error, or the JSON object was not
54 // convertable to a dictionary.
55 static Status
ErrorJwkNotDictionary();
57 // The required property |property| was missing.
58 static Status
ErrorJwkPropertyMissing(const std::string
& property
);
60 // The property |property| was not of type |expected_type|.
61 static Status
ErrorJwkPropertyWrongType(const std::string
& property
,
62 const std::string
& expected_type
);
64 // The property |property| was a string, however could not be successfully
66 static Status
ErrorJwkBase64Decode(const std::string
& property
);
68 // The "ext" parameter was specified but was
69 // incompatible with the value requested by the Web Crypto call.
70 static Status
ErrorJwkExtInconsistent();
72 // The "alg" parameter could not be converted to an equivalent
73 // WebCryptoAlgorithm. Either it was malformed or unrecognized.
74 static Status
ErrorJwkUnrecognizedAlgorithm();
76 // The "alg" parameter is incompatible with the (optional) Algorithm
77 // specified by the Web Crypto import operation.
78 static Status
ErrorJwkAlgorithmInconsistent();
80 // The "use" parameter was specified, however it couldn't be converted to an
81 // equivalent Web Crypto usage.
82 static Status
ErrorJwkUnrecognizedUse();
84 // The "key_ops" parameter was specified, however one of the values in the
85 // array couldn't be converted to an equivalent Web Crypto usage.
86 static Status
ErrorJwkUnrecognizedKeyop();
88 // The "use" parameter was specified, however it is incompatible with that
89 // specified by the Web Crypto import operation.
90 static Status
ErrorJwkUseInconsistent();
92 // The "key_ops" parameter was specified, however it is incompatible with that
93 // specified by the Web Crypto import operation.
94 static Status
ErrorJwkKeyopsInconsistent();
96 // Both the "key_ops" and the "use" parameters were specified, however they
97 // are incompatible with each other.
98 static Status
ErrorJwkUseAndKeyopsInconsistent();
100 // The "kty" parameter was given and was a string, however it was
102 static Status
ErrorJwkUnrecognizedKty();
104 // The amount of key data provided was incompatible with the selected
105 // algorithm. For instance if the algorith name was A128CBC then EXACTLY
106 // 128-bits of key data must have been provided. If 192-bits of key data were
107 // given that is an error.
108 static Status
ErrorJwkIncorrectKeyLength();
110 // The JWK was for an RSA private key but only partially provided the optional
111 // parameters (p, q, dq, dq, qi).
112 static Status
ErrorJwkIncompleteOptionalRsaPrivateKey();
114 // ------------------------------------
116 // ------------------------------------
118 // No key data was provided when importing an spki, pkcs8, or jwk formatted
119 // key. This does not apply to raw format, since it is possible to have empty
121 static Status
ErrorImportEmptyKeyData();
123 // The key data buffer provided for importKey() is an incorrect length for
125 static Status
ErrorImportAesKeyLength();
127 // The wrong key was used for the operation. For instance, a public key was
128 // used to verify a RsaSsaPkcs1v1_5 signature, or tried exporting a private
129 // key using spki format.
130 static Status
ErrorUnexpectedKeyType();
132 // When doing an AES-CBC encryption/decryption, the "iv" parameter was not 16
134 static Status
ErrorIncorrectSizeAesCbcIv();
136 // The data provided to an encrypt/decrypt/sign/verify operation was too
137 // large. This can either represent an internal limitation (for instance
138 // representing buffer lengths as uints).
139 static Status
ErrorDataTooLarge();
141 // The data provided to an encrypt/decrypt/sign/verify operation was too
142 // small. This usually represents an algorithm restriction (for instance
143 // AES-KW requires a minimum of 24 bytes input data).
144 static Status
ErrorDataTooSmall();
146 // Something was unsupported or unimplemented. This can mean the algorithm in
147 // question was unsupported, some parameter combination was unsupported, or
148 // something has not yet been implemented.
149 static Status
ErrorUnsupported();
151 // Something unexpected happened in the code, which implies there is a
152 // source-level bug. These should not happen, but safer to fail than simply
154 static Status
ErrorUnexpected();
156 // The authentication tag length specified for AES-GCM encrypt/decrypt was
157 // not 32, 64, 96, 104, 112, 120, or 128.
158 static Status
ErrorInvalidAesGcmTagLength();
160 // The input data given to an AES-KW encrypt/decrypt operation was not a
161 // multiple of 8 bytes, as required by RFC 3394.
162 static Status
ErrorInvalidAesKwDataLength();
164 // The "publicExponent" used to generate a key was invalid: either no bytes
165 // were specified, or the number was too large to fit into an "unsigned long"
166 // (implemention limitation), or the exponent was zero.
167 static Status
ErrorGenerateKeyPublicExponent();
169 // The modulus bytes were empty when importing an RSA public key.
170 static Status
ErrorImportRsaEmptyModulus();
172 // The the modulus length was zero bits when generating an RSA public key.
173 static Status
ErrorGenerateRsaZeroModulus();
175 // The exponent bytes were empty when importing an RSA public key.
176 static Status
ErrorImportRsaEmptyExponent();
178 // An unextractable key was used by an operation which exports the key data.
179 static Status
ErrorKeyNotExtractable();
181 // The key length specified when generating a key was invalid. Either it was
182 // zero, or it was not a multiple of 8 bits.
183 static Status
ErrorGenerateKeyLength();
185 // Attempted to create a key (either by importKey(), generateKey(), or
186 // unwrapKey()) however the key usages were not applicable for the key type
188 static Status
ErrorCreateKeyBadUsages();
191 enum Type
{ TYPE_ERROR
, TYPE_SUCCESS
};
193 // Constructs an error with the specified error type and message.
194 Status(blink::WebCryptoErrorType error_type
,
195 const std::string
& error_details_utf8
);
197 // Constructs a success or error without any details.
198 explicit Status(Type type
);
201 blink::WebCryptoErrorType error_type_
;
202 std::string error_details_
;
205 } // namespace webcrypto
207 } // namespace content
209 #endif // CONTENT_CHILD_WEBCRYPTO_STATUS_H_