4c1aae6e1857a1d047839c2e1516d52c9d24cfbd
[platform/framework/web/crosswalk.git] / src / content / child / webcrypto / status.h
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.
4
5 #ifndef CONTENT_CHILD_WEBCRYPTO_STATUS_H_
6 #define CONTENT_CHILD_WEBCRYPTO_STATUS_H_
7
8 #include <string>
9 #include "content/common/content_export.h"
10 #include "third_party/WebKit/public/platform/WebCrypto.h"
11
12 namespace content {
13
14 namespace webcrypto {
15
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.
19 //
20 // As such, it is important that errors DO NOT reveal any sensitive material
21 // (like key bytes).
22 class CONTENT_EXPORT Status {
23  public:
24   Status() : type_(TYPE_ERROR) {}
25
26   // Returns true if the Status represents an error (any one of them).
27   bool IsError() const;
28
29   // Returns true if the Status represent success.
30   bool IsSuccess() const;
31
32   // Returns a UTF-8 error message (non-localized) describing the error.
33   const std::string& error_details() const { return error_details_; }
34
35   blink::WebCryptoErrorType error_type() const { return error_type_; }
36
37   // Constructs a status representing success.
38   static Status Success();
39
40   // Constructs a status representing a generic operation error. It contains no
41   // extra details.
42   static Status OperationError();
43
44   // Constructs a status representing a generic data error. It contains no
45   // extra details.
46   static Status DataError();
47
48   // ------------------------------------
49   // Errors when importing a JWK formatted key
50   // ------------------------------------
51
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();
56
57   // The required property |property| was missing.
58   static Status ErrorJwkPropertyMissing(const std::string& property);
59
60   // The property |property| was not of type |expected_type|.
61   static Status ErrorJwkPropertyWrongType(const std::string& property,
62                                           const std::string& expected_type);
63
64   // The property |property| was a string, however could not be successfully
65   // base64 decoded.
66   static Status ErrorJwkBase64Decode(const std::string& property);
67
68   // The "ext" parameter was specified but was
69   // incompatible with the value requested by the Web Crypto call.
70   static Status ErrorJwkExtInconsistent();
71
72   // The "alg" parameter is incompatible with the (optional) Algorithm
73   // specified by the Web Crypto import operation.
74   static Status ErrorJwkAlgorithmInconsistent();
75
76   // The "use" parameter was specified, however it couldn't be converted to an
77   // equivalent Web Crypto usage.
78   static Status ErrorJwkUnrecognizedUse();
79
80   // The "key_ops" parameter was specified, however one of the values in the
81   // array couldn't be converted to an equivalent Web Crypto usage.
82   static Status ErrorJwkUnrecognizedKeyop();
83
84   // The "use" parameter was specified, however it is incompatible with that
85   // specified by the Web Crypto import operation.
86   static Status ErrorJwkUseInconsistent();
87
88   // The "key_ops" parameter was specified, however it is incompatible with that
89   // specified by the Web Crypto import operation.
90   static Status ErrorJwkKeyopsInconsistent();
91
92   // Both the "key_ops" and the "use" parameters were specified, however they
93   // are incompatible with each other.
94   static Status ErrorJwkUseAndKeyopsInconsistent();
95
96   // The "kty" parameter was given and was a string, however it was not the
97   // expected value.
98   static Status ErrorJwkUnexpectedKty(const std::string& expected);
99
100   // The amount of key data provided was incompatible with the selected
101   // algorithm. For instance if the algorith name was A128CBC then EXACTLY
102   // 128-bits of key data must have been provided. If 192-bits of key data were
103   // given that is an error.
104   static Status ErrorJwkIncorrectKeyLength();
105
106   // The JWK property |property| is supposed to represent a big-endian unsigned
107   // integer, however was the empty string.
108   static Status ErrorJwkEmptyBigInteger(const std::string& property);
109
110   // The big-endian unsigned integer |property| contained leading zeros. This
111   // violates the JWA requirement that such octet strings be minimal.
112   static Status ErrorJwkBigIntegerHasLeadingZero(const std::string& property);
113
114   // The key_ops lists a usage more than once.
115   static Status ErrorJwkDuplicateKeyOps();
116
117   // ------------------------------------
118   // Other errors
119   // ------------------------------------
120
121   // No key data was provided when importing an spki, pkcs8, or jwk formatted
122   // key. This does not apply to raw format, since it is possible to have empty
123   // key data there.
124   static Status ErrorImportEmptyKeyData();
125
126   // Tried importing a key using an unsupported format for the key type (for
127   // instance importing an HMAC key using format=spki).
128   static Status ErrorUnsupportedImportKeyFormat();
129
130   // Tried exporting a key using an unsupported format for the key type (for
131   // instance exporting an HMAC key using format=spki).
132   static Status ErrorUnsupportedExportKeyFormat();
133
134   // The key data buffer provided for importKey() is an incorrect length for
135   // AES.
136   static Status ErrorImportAesKeyLength();
137
138   // 192-bit AES keys are valid, however unsupported.
139   static Status ErrorAes192BitUnsupported();
140
141   // The wrong key was used for the operation. For instance, a public key was
142   // used to verify a RsaSsaPkcs1v1_5 signature, or tried exporting a private
143   // key using spki format.
144   static Status ErrorUnexpectedKeyType();
145
146   // When doing an AES-CBC encryption/decryption, the "iv" parameter was not 16
147   // bytes.
148   static Status ErrorIncorrectSizeAesCbcIv();
149
150   // When doing AES-CTR encryption/decryption, the "counter" parameter was not
151   // 16 bytes.
152   static Status ErrorIncorrectSizeAesCtrCounter();
153
154   // When doing AES-CTR encryption/decryption, the "length" parameter for the
155   // counter was out of range.
156   static Status ErrorInvalidAesCtrCounterLength();
157
158   // The input to encrypt/decrypt was too large. Based on the counter size, it
159   // would cause the counter to wraparound and repeat earlier values.
160   static Status ErrorAesCtrInputTooLongCounterRepeated();
161
162   // The data provided to an encrypt/decrypt/sign/verify operation was too
163   // large. This can either represent an internal limitation (for instance
164   // representing buffer lengths as uints).
165   static Status ErrorDataTooLarge();
166
167   // The data provided to an encrypt/decrypt/sign/verify operation was too
168   // small. This usually represents an algorithm restriction (for instance
169   // AES-KW requires a minimum of 24 bytes input data).
170   static Status ErrorDataTooSmall();
171
172   // Something was unsupported or unimplemented. This can mean the algorithm in
173   // question was unsupported, some parameter combination was unsupported, or
174   // something has not yet been implemented.
175   static Status ErrorUnsupported();
176   static Status ErrorUnsupported(const std::string& message);
177
178   // Something unexpected happened in the code, which implies there is a
179   // source-level bug. These should not happen, but safer to fail than simply
180   // DCHECK.
181   static Status ErrorUnexpected();
182
183   // The authentication tag length specified for AES-GCM encrypt/decrypt was
184   // not 32, 64, 96, 104, 112, 120, or 128.
185   static Status ErrorInvalidAesGcmTagLength();
186
187   // The input data given to an AES-KW encrypt/decrypt operation was not a
188   // multiple of 8 bytes, as required by RFC 3394.
189   static Status ErrorInvalidAesKwDataLength();
190
191   // The "publicExponent" used to generate a key was invalid or unsupported.
192   // Only values of 3 and 65537 are allowed.
193   static Status ErrorGenerateKeyPublicExponent();
194
195   // The modulus bytes were empty when importing an RSA public key.
196   static Status ErrorImportRsaEmptyModulus();
197
198   // The modulus length was unsupported when generating an RSA key pair.
199   static Status ErrorGenerateRsaUnsupportedModulus();
200
201   // The exponent bytes were empty when importing an RSA public key.
202   static Status ErrorImportRsaEmptyExponent();
203
204   // An unextractable key was used by an operation which exports the key data.
205   static Status ErrorKeyNotExtractable();
206
207   // The key length specified when generating a key was invalid. Either it was
208   // zero, or it was not a multiple of 8 bits.
209   static Status ErrorGenerateKeyLength();
210
211   // Attempted to create a key (either by importKey(), generateKey(), or
212   // unwrapKey()) however the key usages were not applicable for the key type
213   // and algorithm.
214   static Status ErrorCreateKeyBadUsages();
215
216  private:
217   enum Type { TYPE_ERROR, TYPE_SUCCESS };
218
219   // Constructs an error with the specified error type and message.
220   Status(blink::WebCryptoErrorType error_type,
221          const std::string& error_details_utf8);
222
223   // Constructs a success or error without any details.
224   explicit Status(Type type);
225
226   Type type_;
227   blink::WebCryptoErrorType error_type_;
228   std::string error_details_;
229 };
230
231 }  // namespace webcrypto
232
233 }  // namespace content
234
235 #endif  // CONTENT_CHILD_WEBCRYPTO_STATUS_H_