OLD | NEW |
| (Empty) |
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 #include "content/child/webcrypto/status.h" | |
6 | |
7 #include "base/format_macros.h" | |
8 #include "base/strings/stringprintf.h" | |
9 | |
10 namespace content { | |
11 | |
12 namespace webcrypto { | |
13 | |
14 bool Status::IsError() const { | |
15 return type_ == TYPE_ERROR; | |
16 } | |
17 | |
18 bool Status::IsSuccess() const { | |
19 return type_ == TYPE_SUCCESS; | |
20 } | |
21 | |
22 Status Status::Success() { | |
23 return Status(TYPE_SUCCESS); | |
24 } | |
25 | |
26 Status Status::OperationError() { | |
27 return Status(blink::WebCryptoErrorTypeOperation, ""); | |
28 } | |
29 | |
30 Status Status::DataError() { | |
31 return Status(blink::WebCryptoErrorTypeData, ""); | |
32 } | |
33 | |
34 Status Status::ErrorJwkNotDictionary() { | |
35 return Status(blink::WebCryptoErrorTypeData, | |
36 "JWK input could not be parsed to a JSON dictionary"); | |
37 } | |
38 | |
39 Status Status::ErrorJwkMemberMissing(const std::string& member_name) { | |
40 return Status(blink::WebCryptoErrorTypeData, | |
41 "The required JWK member \"" + member_name + "\" was missing"); | |
42 } | |
43 | |
44 Status Status::ErrorJwkMemberWrongType(const std::string& member_name, | |
45 const std::string& expected_type) { | |
46 return Status( | |
47 blink::WebCryptoErrorTypeData, | |
48 "The JWK member \"" + member_name + "\" must be a " + expected_type); | |
49 } | |
50 | |
51 Status Status::ErrorJwkBase64Decode(const std::string& member_name) { | |
52 return Status(blink::WebCryptoErrorTypeData, | |
53 "The JWK member \"" + member_name + | |
54 "\" could not be base64url decoded or contained padding"); | |
55 } | |
56 | |
57 Status Status::ErrorJwkExtInconsistent() { | |
58 return Status( | |
59 blink::WebCryptoErrorTypeData, | |
60 "The \"ext\" member of the JWK dictionary is inconsistent what that " | |
61 "specified by the Web Crypto call"); | |
62 } | |
63 | |
64 Status Status::ErrorJwkAlgorithmInconsistent() { | |
65 return Status(blink::WebCryptoErrorTypeData, | |
66 "The JWK \"alg\" member was inconsistent with that specified " | |
67 "by the Web Crypto call"); | |
68 } | |
69 | |
70 Status Status::ErrorJwkUnrecognizedUse() { | |
71 return Status(blink::WebCryptoErrorTypeData, | |
72 "The JWK \"use\" member could not be parsed"); | |
73 } | |
74 | |
75 Status Status::ErrorJwkUnrecognizedKeyop() { | |
76 return Status(blink::WebCryptoErrorTypeData, | |
77 "The JWK \"key_ops\" member could not be parsed"); | |
78 } | |
79 | |
80 Status Status::ErrorJwkUseInconsistent() { | |
81 return Status(blink::WebCryptoErrorTypeData, | |
82 "The JWK \"use\" member was inconsistent with that specified " | |
83 "by the Web Crypto call. The JWK usage must be a superset of " | |
84 "those requested"); | |
85 } | |
86 | |
87 Status Status::ErrorJwkKeyopsInconsistent() { | |
88 return Status(blink::WebCryptoErrorTypeData, | |
89 "The JWK \"key_ops\" member was inconsistent with that " | |
90 "specified by the Web Crypto call. The JWK usage must be a " | |
91 "superset of those requested"); | |
92 } | |
93 | |
94 Status Status::ErrorJwkUseAndKeyopsInconsistent() { | |
95 return Status(blink::WebCryptoErrorTypeData, | |
96 "The JWK \"use\" and \"key_ops\" properties were both found " | |
97 "but are inconsistent with each other."); | |
98 } | |
99 | |
100 Status Status::ErrorJwkUnexpectedKty(const std::string& expected) { | |
101 return Status(blink::WebCryptoErrorTypeData, | |
102 "The JWK \"kty\" member was not \"" + expected + "\""); | |
103 } | |
104 | |
105 Status Status::ErrorJwkIncorrectKeyLength() { | |
106 return Status(blink::WebCryptoErrorTypeData, | |
107 "The JWK \"k\" member did not include the right length " | |
108 "of key data for the given algorithm."); | |
109 } | |
110 | |
111 Status Status::ErrorJwkEmptyBigInteger(const std::string& member_name) { | |
112 return Status(blink::WebCryptoErrorTypeData, | |
113 "The JWK \"" + member_name + "\" member was empty."); | |
114 } | |
115 | |
116 Status Status::ErrorJwkBigIntegerHasLeadingZero( | |
117 const std::string& member_name) { | |
118 return Status( | |
119 blink::WebCryptoErrorTypeData, | |
120 "The JWK \"" + member_name + "\" member contained a leading zero."); | |
121 } | |
122 | |
123 Status Status::ErrorJwkDuplicateKeyOps() { | |
124 return Status(blink::WebCryptoErrorTypeData, | |
125 "The \"key_ops\" member of the JWK dictionary contains " | |
126 "duplicate usages."); | |
127 } | |
128 | |
129 Status Status::ErrorUnsupportedImportKeyFormat() { | |
130 return Status(blink::WebCryptoErrorTypeNotSupported, | |
131 "Unsupported import key format for algorithm"); | |
132 } | |
133 | |
134 Status Status::ErrorUnsupportedExportKeyFormat() { | |
135 return Status(blink::WebCryptoErrorTypeNotSupported, | |
136 "Unsupported export key format for algorithm"); | |
137 } | |
138 | |
139 Status Status::ErrorImportAesKeyLength() { | |
140 return Status(blink::WebCryptoErrorTypeData, | |
141 "AES key data must be 128 or 256 bits"); | |
142 } | |
143 | |
144 Status Status::ErrorGetAesKeyLength() { | |
145 return Status(blink::WebCryptoErrorTypeOperation, | |
146 "AES key length must be 128 or 256 bits"); | |
147 } | |
148 | |
149 Status Status::ErrorGenerateAesKeyLength() { | |
150 return Status(blink::WebCryptoErrorTypeOperation, | |
151 "AES key length must be 128 or 256 bits"); | |
152 } | |
153 | |
154 Status Status::ErrorAes192BitUnsupported() { | |
155 return Status(blink::WebCryptoErrorTypeOperation, | |
156 "192-bit AES keys are not supported"); | |
157 } | |
158 | |
159 Status Status::ErrorUnexpectedKeyType() { | |
160 return Status(blink::WebCryptoErrorTypeInvalidAccess, | |
161 "The key is not of the expected type"); | |
162 } | |
163 | |
164 Status Status::ErrorIncorrectSizeAesCbcIv() { | |
165 return Status(blink::WebCryptoErrorTypeOperation, | |
166 "The \"iv\" has an unexpected length -- must be 16 bytes"); | |
167 } | |
168 | |
169 Status Status::ErrorIncorrectSizeAesCtrCounter() { | |
170 return Status(blink::WebCryptoErrorTypeOperation, | |
171 "The \"counter\" has an unexpected length -- must be 16 bytes"); | |
172 } | |
173 | |
174 Status Status::ErrorInvalidAesCtrCounterLength() { | |
175 return Status(blink::WebCryptoErrorTypeOperation, | |
176 "The \"length\" member must be >= 1 and <= 128"); | |
177 } | |
178 | |
179 Status Status::ErrorAesCtrInputTooLongCounterRepeated() { | |
180 return Status(blink::WebCryptoErrorTypeData, | |
181 "The input is too large for the counter length."); | |
182 } | |
183 | |
184 Status Status::ErrorDataTooLarge() { | |
185 return Status(blink::WebCryptoErrorTypeOperation, | |
186 "The provided data is too large"); | |
187 } | |
188 | |
189 Status Status::ErrorDataTooSmall() { | |
190 return Status(blink::WebCryptoErrorTypeOperation, | |
191 "The provided data is too small"); | |
192 } | |
193 | |
194 Status Status::ErrorUnsupported() { | |
195 return ErrorUnsupported("The requested operation is unsupported"); | |
196 } | |
197 | |
198 Status Status::ErrorUnsupported(const std::string& message) { | |
199 return Status(blink::WebCryptoErrorTypeNotSupported, message); | |
200 } | |
201 | |
202 Status Status::ErrorUnexpected() { | |
203 return Status(blink::WebCryptoErrorTypeOperation, | |
204 "Something unexpected happened..."); | |
205 } | |
206 | |
207 Status Status::ErrorInvalidAesGcmTagLength() { | |
208 return Status( | |
209 blink::WebCryptoErrorTypeOperation, | |
210 "The tag length is invalid: Must be 32, 64, 96, 104, 112, 120, or 128 " | |
211 "bits"); | |
212 } | |
213 | |
214 Status Status::ErrorInvalidAesKwDataLength() { | |
215 return Status(blink::WebCryptoErrorTypeData, | |
216 "The AES-KW input data length is invalid: not a multiple of 8 " | |
217 "bytes"); | |
218 } | |
219 | |
220 Status Status::ErrorGenerateKeyPublicExponent() { | |
221 return Status(blink::WebCryptoErrorTypeOperation, | |
222 "The \"publicExponent\" must be either 3 or 65537"); | |
223 } | |
224 | |
225 Status Status::ErrorImportRsaEmptyModulus() { | |
226 return Status(blink::WebCryptoErrorTypeData, "The modulus is empty"); | |
227 } | |
228 | |
229 Status Status::ErrorGenerateRsaUnsupportedModulus() { | |
230 return Status(blink::WebCryptoErrorTypeOperation, | |
231 "The modulus length must be a multiple of 8 bits and >= 256 " | |
232 "and <= 16384"); | |
233 } | |
234 | |
235 Status Status::ErrorImportRsaEmptyExponent() { | |
236 return Status(blink::WebCryptoErrorTypeData, | |
237 "No bytes for the exponent were provided"); | |
238 } | |
239 | |
240 Status Status::ErrorKeyNotExtractable() { | |
241 return Status(blink::WebCryptoErrorTypeInvalidAccess, | |
242 "They key is not extractable"); | |
243 } | |
244 | |
245 Status Status::ErrorGenerateHmacKeyLengthZero() { | |
246 return Status(blink::WebCryptoErrorTypeOperation, | |
247 "HMAC key length must not be zero"); | |
248 } | |
249 | |
250 Status Status::ErrorHmacImportEmptyKey() { | |
251 return Status(blink::WebCryptoErrorTypeData, | |
252 "HMAC key data must not be empty"); | |
253 } | |
254 | |
255 Status Status::ErrorGetHmacKeyLengthZero() { | |
256 return Status(blink::WebCryptoErrorTypeType, | |
257 "HMAC key length must not be zero"); | |
258 } | |
259 | |
260 Status Status::ErrorHmacImportBadLength() { | |
261 return Status( | |
262 blink::WebCryptoErrorTypeData, | |
263 "The optional HMAC key length must be shorter than the key data, and by " | |
264 "no more than 7 bits."); | |
265 } | |
266 | |
267 Status Status::ErrorCreateKeyBadUsages() { | |
268 return Status(blink::WebCryptoErrorTypeSyntax, | |
269 "Cannot create a key using the specified key usages."); | |
270 } | |
271 | |
272 Status Status::ErrorCreateKeyEmptyUsages() { | |
273 return Status(blink::WebCryptoErrorTypeSyntax, | |
274 "Usages cannot be empty when creating a key."); | |
275 } | |
276 | |
277 Status Status::ErrorImportedEcKeyIncorrectCurve() { | |
278 return Status( | |
279 blink::WebCryptoErrorTypeData, | |
280 "The imported EC key specifies a different curve than requested"); | |
281 } | |
282 | |
283 Status Status::ErrorJwkIncorrectCrv() { | |
284 return Status( | |
285 blink::WebCryptoErrorTypeData, | |
286 "The JWK's \"crv\" member specifies a different curve than requested"); | |
287 } | |
288 | |
289 Status Status::ErrorEcKeyInvalid() { | |
290 return Status(blink::WebCryptoErrorTypeData, | |
291 "The imported EC key is invalid"); | |
292 } | |
293 | |
294 Status Status::JwkOctetStringWrongLength(const std::string& member_name, | |
295 size_t expected_length, | |
296 size_t actual_length) { | |
297 return Status( | |
298 blink::WebCryptoErrorTypeData, | |
299 base::StringPrintf( | |
300 "The JWK's \"%s\" member defines an octet string of length %" PRIuS | |
301 " bytes but should be %" PRIuS, | |
302 member_name.c_str(), actual_length, expected_length)); | |
303 } | |
304 | |
305 Status Status::ErrorEcdhPublicKeyWrongType() { | |
306 return Status( | |
307 blink::WebCryptoErrorTypeInvalidAccess, | |
308 "The public parameter for ECDH key derivation is not a public EC key"); | |
309 } | |
310 | |
311 Status Status::ErrorEcdhPublicKeyWrongAlgorithm() { | |
312 return Status( | |
313 blink::WebCryptoErrorTypeInvalidAccess, | |
314 "The public parameter for ECDH key derivation must be for ECDH"); | |
315 } | |
316 | |
317 Status Status::ErrorEcdhCurveMismatch() { | |
318 return Status(blink::WebCryptoErrorTypeInvalidAccess, | |
319 "The public parameter for ECDH key derivation is for a " | |
320 "different named curve"); | |
321 } | |
322 | |
323 Status Status::ErrorEcdhLengthTooBig(unsigned int max_length_bits) { | |
324 return Status(blink::WebCryptoErrorTypeOperation, | |
325 base::StringPrintf( | |
326 "Length specified for ECDH key derivation is too large. " | |
327 "Maximum allowed is %u bits", | |
328 max_length_bits)); | |
329 } | |
330 | |
331 Status Status::ErrorHkdfLengthTooLong() { | |
332 return Status(blink::WebCryptoErrorTypeOperation, | |
333 "The length provided for HKDF is too large."); | |
334 } | |
335 | |
336 Status Status::ErrorHkdfDeriveBitsLengthNotSpecified() { | |
337 // TODO(nharper): The spec might change so that an OperationError should be | |
338 // thrown here instead of a TypeError. | |
339 // (https://www.w3.org/Bugs/Public/show_bug.cgi?id=27771) | |
340 return Status(blink::WebCryptoErrorTypeType, | |
341 "No length was specified for the HKDF Derive Bits operation."); | |
342 } | |
343 | |
344 Status Status::ErrorPbkdf2InvalidLength() { | |
345 return Status( | |
346 blink::WebCryptoErrorTypeOperation, | |
347 "Length for PBKDF2 key derivation must be a multiple of 8 bits."); | |
348 } | |
349 | |
350 Status Status::ErrorPbkdf2DeriveBitsLengthNotSpecified() { | |
351 return Status( | |
352 blink::WebCryptoErrorTypeOperation, | |
353 "No length was specified for the PBKDF2 Derive Bits operation."); | |
354 } | |
355 | |
356 Status::Status(blink::WebCryptoErrorType error_type, | |
357 const std::string& error_details_utf8) | |
358 : type_(TYPE_ERROR), | |
359 error_type_(error_type), | |
360 error_details_(error_details_utf8) { | |
361 } | |
362 | |
363 Status::Status(Type type) : type_(type) { | |
364 } | |
365 | |
366 } // namespace webcrypto | |
367 | |
368 } // namespace content | |
OLD | NEW |