OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/child/webcrypto/status.h" | 5 #include "content/child/webcrypto/status.h" |
6 | 6 |
7 namespace content { | 7 namespace content { |
8 | 8 |
9 namespace webcrypto { | 9 namespace webcrypto { |
10 | 10 |
11 bool Status::IsError() const { return type_ == TYPE_ERROR; } | 11 bool Status::IsError() const { |
12 | 12 return type_ == TYPE_ERROR; |
13 bool Status::IsSuccess() const { return type_ == TYPE_SUCCESS; } | 13 } |
14 | 14 |
15 bool Status::HasErrorDetails() const { return !error_details_.empty(); } | 15 bool Status::IsSuccess() const { |
16 | 16 return type_ == TYPE_SUCCESS; |
17 std::string Status::ToString() const { | 17 } |
18 return IsSuccess() ? "Success" : error_details_; | 18 |
19 } | 19 Status Status::Success() { |
20 | 20 return Status(TYPE_SUCCESS); |
21 Status Status::Success() { return Status(TYPE_SUCCESS); } | 21 } |
22 | 22 |
23 Status Status::Error() { return Status(TYPE_ERROR); } | 23 Status Status::OperationError() { |
| 24 return Status(blink::WebCryptoErrorTypeOperation, ""); |
| 25 } |
| 26 |
| 27 Status Status::DataError() { |
| 28 return Status(blink::WebCryptoErrorTypeData, ""); |
| 29 } |
24 | 30 |
25 Status Status::ErrorJwkNotDictionary() { | 31 Status Status::ErrorJwkNotDictionary() { |
26 return Status("JWK input could not be parsed to a JSON dictionary"); | 32 return Status(blink::WebCryptoErrorTypeData, |
| 33 "JWK input could not be parsed to a JSON dictionary"); |
27 } | 34 } |
28 | 35 |
29 Status Status::ErrorJwkPropertyMissing(const std::string& property) { | 36 Status Status::ErrorJwkPropertyMissing(const std::string& property) { |
30 return Status("The required JWK property \"" + property + "\" was missing"); | 37 return Status(blink::WebCryptoErrorTypeData, |
| 38 "The required JWK property \"" + property + "\" was missing"); |
31 } | 39 } |
32 | 40 |
33 Status Status::ErrorJwkPropertyWrongType(const std::string& property, | 41 Status Status::ErrorJwkPropertyWrongType(const std::string& property, |
34 const std::string& expected_type) { | 42 const std::string& expected_type) { |
35 return Status("The JWK property \"" + property + "\" must be a " + | 43 return Status( |
36 expected_type); | 44 blink::WebCryptoErrorTypeData, |
| 45 "The JWK property \"" + property + "\" must be a " + expected_type); |
37 } | 46 } |
38 | 47 |
39 Status Status::ErrorJwkBase64Decode(const std::string& property) { | 48 Status Status::ErrorJwkBase64Decode(const std::string& property) { |
40 return Status("The JWK property \"" + property + | 49 return Status( |
41 "\" could not be base64 decoded"); | 50 blink::WebCryptoErrorTypeData, |
| 51 "The JWK property \"" + property + "\" could not be base64 decoded"); |
42 } | 52 } |
43 | 53 |
44 Status Status::ErrorJwkExtInconsistent() { | 54 Status Status::ErrorJwkExtInconsistent() { |
45 return Status( | 55 return Status( |
| 56 blink::WebCryptoErrorTypeData, |
46 "The \"ext\" property of the JWK dictionary is inconsistent what that " | 57 "The \"ext\" property of the JWK dictionary is inconsistent what that " |
47 "specified by the Web Crypto call"); | 58 "specified by the Web Crypto call"); |
48 } | 59 } |
49 | 60 |
50 Status Status::ErrorJwkUnrecognizedAlgorithm() { | 61 Status Status::ErrorJwkUnrecognizedAlgorithm() { |
51 return Status("The JWK \"alg\" property was not recognized"); | 62 return Status(blink::WebCryptoErrorTypeData, |
| 63 "The JWK \"alg\" property was not recognized"); |
52 } | 64 } |
53 | 65 |
54 Status Status::ErrorJwkAlgorithmInconsistent() { | 66 Status Status::ErrorJwkAlgorithmInconsistent() { |
55 return Status( | 67 return Status(blink::WebCryptoErrorTypeData, |
56 "The JWK \"alg\" property was inconsistent with that specified " | 68 "The JWK \"alg\" property was inconsistent with that specified " |
57 "by the Web Crypto call"); | 69 "by the Web Crypto call"); |
58 } | 70 } |
59 | 71 |
60 Status Status::ErrorJwkUnrecognizedUse() { | 72 Status Status::ErrorJwkUnrecognizedUse() { |
61 return Status("The JWK \"use\" property could not be parsed"); | 73 return Status(blink::WebCryptoErrorTypeData, |
| 74 "The JWK \"use\" property could not be parsed"); |
62 } | 75 } |
63 | 76 |
64 Status Status::ErrorJwkUnrecognizedKeyop() { | 77 Status Status::ErrorJwkUnrecognizedKeyop() { |
65 return Status("The JWK \"key_ops\" property could not be parsed"); | 78 return Status(blink::WebCryptoErrorTypeData, |
| 79 "The JWK \"key_ops\" property could not be parsed"); |
66 } | 80 } |
67 | 81 |
68 Status Status::ErrorJwkUseInconsistent() { | 82 Status Status::ErrorJwkUseInconsistent() { |
69 return Status( | 83 return Status(blink::WebCryptoErrorTypeData, |
70 "The JWK \"use\" property was inconsistent with that specified " | 84 "The JWK \"use\" property was inconsistent with that specified " |
71 "by the Web Crypto call. The JWK usage must be a superset of " | 85 "by the Web Crypto call. The JWK usage must be a superset of " |
72 "those requested"); | 86 "those requested"); |
73 } | 87 } |
74 | 88 |
75 Status Status::ErrorJwkKeyopsInconsistent() { | 89 Status Status::ErrorJwkKeyopsInconsistent() { |
76 return Status( | 90 return Status(blink::WebCryptoErrorTypeData, |
77 "The JWK \"key_ops\" property was inconsistent with that " | 91 "The JWK \"key_ops\" property was inconsistent with that " |
78 "specified by the Web Crypto call. The JWK usage must be a " | 92 "specified by the Web Crypto call. The JWK usage must be a " |
79 "superset of those requested"); | 93 "superset of those requested"); |
80 } | 94 } |
81 | 95 |
82 Status Status::ErrorJwkUseAndKeyopsInconsistent() { | 96 Status Status::ErrorJwkUseAndKeyopsInconsistent() { |
83 return Status( | 97 return Status(blink::WebCryptoErrorTypeData, |
84 "The JWK \"use\" and \"key_ops\" properties were both found " | 98 "The JWK \"use\" and \"key_ops\" properties were both found " |
85 "but are inconsistent with each other."); | 99 "but are inconsistent with each other."); |
86 } | 100 } |
87 | 101 |
88 Status Status::ErrorJwkRsaPrivateKeyUnsupported() { | 102 Status Status::ErrorJwkRsaPrivateKeyUnsupported() { |
89 return Status( | 103 return Status(blink::WebCryptoErrorTypeNotSupported, |
90 "JWK RSA key contained \"d\" property: Private key import is " | 104 "JWK RSA key contained \"d\" property: Private key import is " |
91 "not yet supported"); | 105 "not yet supported"); |
92 } | 106 } |
93 | 107 |
94 Status Status::ErrorJwkUnrecognizedKty() { | 108 Status Status::ErrorJwkUnrecognizedKty() { |
95 return Status("The JWK \"kty\" property was unrecognized"); | 109 return Status(blink::WebCryptoErrorTypeData, |
| 110 "The JWK \"kty\" property was unrecognized"); |
96 } | 111 } |
97 | 112 |
98 Status Status::ErrorJwkIncorrectKeyLength() { | 113 Status Status::ErrorJwkIncorrectKeyLength() { |
99 return Status( | 114 return Status(blink::WebCryptoErrorTypeData, |
100 "The JWK \"k\" property did not include the right length " | 115 "The JWK \"k\" property did not include the right length " |
101 "of key data for the given algorithm."); | 116 "of key data for the given algorithm."); |
102 } | 117 } |
103 | 118 |
104 Status Status::ErrorImportEmptyKeyData() { | 119 Status Status::ErrorImportEmptyKeyData() { |
105 return Status("No key data was provided"); | 120 return Status(blink::WebCryptoErrorTypeData, "No key data was provided"); |
| 121 } |
| 122 |
| 123 Status Status::ErrorImportAesKeyLength() { |
| 124 return Status(blink::WebCryptoErrorTypeData, |
| 125 #if defined(WEBCRYPTO_HAS_ERROR_TYPE) |
| 126 "AES key data must be 128, 192 or 256 bits"); |
| 127 #else |
| 128 ""); |
| 129 #endif |
106 } | 130 } |
107 | 131 |
108 Status Status::ErrorUnexpectedKeyType() { | 132 Status Status::ErrorUnexpectedKeyType() { |
109 return Status("The key is not of the expected type"); | 133 return Status(blink::WebCryptoErrorTypeInvalidAccess, |
| 134 "The key is not of the expected type"); |
110 } | 135 } |
111 | 136 |
112 Status Status::ErrorIncorrectSizeAesCbcIv() { | 137 Status Status::ErrorIncorrectSizeAesCbcIv() { |
113 return Status("The \"iv\" has an unexpected length -- must be 16 bytes"); | 138 return Status(blink::WebCryptoErrorTypeData, |
| 139 "The \"iv\" has an unexpected length -- must be 16 bytes"); |
114 } | 140 } |
115 | 141 |
116 Status Status::ErrorDataTooLarge() { | 142 Status Status::ErrorDataTooLarge() { |
117 return Status("The provided data is too large"); | 143 return Status(blink::WebCryptoErrorTypeData, |
| 144 "The provided data is too large"); |
118 } | 145 } |
119 | 146 |
120 Status Status::ErrorDataTooSmall() { | 147 Status Status::ErrorDataTooSmall() { |
121 return Status("The provided data is too small"); | 148 return Status(blink::WebCryptoErrorTypeData, |
| 149 "The provided data is too small"); |
122 } | 150 } |
123 | 151 |
124 Status Status::ErrorUnsupported() { | 152 Status Status::ErrorUnsupported() { |
125 return Status("The requested operation is unsupported"); | 153 return Status(blink::WebCryptoErrorTypeNotSupported, |
| 154 "The requested operation is unsupported"); |
126 } | 155 } |
127 | 156 |
128 Status Status::ErrorUnexpected() { | 157 Status Status::ErrorUnexpected() { |
129 return Status("Something unexpected happened..."); | 158 return Status(blink::WebCryptoErrorTypeUnknown, |
| 159 "Something unexpected happened..."); |
130 } | 160 } |
131 | 161 |
132 Status Status::ErrorInvalidAesGcmTagLength() { | 162 Status Status::ErrorInvalidAesGcmTagLength() { |
133 return Status( | 163 return Status( |
| 164 blink::WebCryptoErrorTypeData, |
134 "The tag length is invalid: Must be 32, 64, 96, 104, 112, 120, or 128 " | 165 "The tag length is invalid: Must be 32, 64, 96, 104, 112, 120, or 128 " |
135 "bits"); | 166 "bits"); |
136 } | 167 } |
137 | 168 |
138 Status Status::ErrorInvalidAesKwDataLength() { | 169 Status Status::ErrorInvalidAesKwDataLength() { |
139 return Status( | 170 return Status(blink::WebCryptoErrorTypeData, |
140 "The AES-KW input data length is invalid: not a multiple of 8 " | 171 "The AES-KW input data length is invalid: not a multiple of 8 " |
141 "bytes"); | 172 "bytes"); |
142 } | 173 } |
143 | 174 |
144 Status Status::ErrorGenerateKeyPublicExponent() { | 175 Status Status::ErrorGenerateKeyPublicExponent() { |
145 return Status("The \"publicExponent\" is either empty, zero, or too large"); | 176 return Status(blink::WebCryptoErrorTypeData, |
| 177 "The \"publicExponent\" is either empty, zero, or too large"); |
146 } | 178 } |
147 | 179 |
148 Status Status::ErrorImportRsaEmptyModulus() { | 180 Status Status::ErrorImportRsaEmptyModulus() { |
149 return Status("The modulus is empty"); | 181 return Status(blink::WebCryptoErrorTypeData, "The modulus is empty"); |
150 } | 182 } |
151 | 183 |
152 Status Status::ErrorGenerateRsaZeroModulus() { | 184 Status Status::ErrorGenerateRsaZeroModulus() { |
153 return Status("The modulus bit length cannot be zero"); | 185 return Status(blink::WebCryptoErrorTypeData, |
| 186 "The modulus bit length cannot be zero"); |
154 } | 187 } |
155 | 188 |
156 Status Status::ErrorImportRsaEmptyExponent() { | 189 Status Status::ErrorImportRsaEmptyExponent() { |
157 return Status("No bytes for the exponent were provided"); | 190 return Status(blink::WebCryptoErrorTypeData, |
| 191 "No bytes for the exponent were provided"); |
158 } | 192 } |
159 | 193 |
160 Status Status::ErrorKeyNotExtractable() { | 194 Status Status::ErrorKeyNotExtractable() { |
161 return Status("They key is not extractable"); | 195 return Status(blink::WebCryptoErrorTypeInvalidAccess, |
| 196 "They key is not extractable"); |
162 } | 197 } |
163 | 198 |
164 Status Status::ErrorGenerateKeyLength() { | 199 Status Status::ErrorGenerateKeyLength() { |
165 return Status( | 200 return Status(blink::WebCryptoErrorTypeData, |
166 "Invalid key length: it is either zero or not a multiple of 8 " | 201 "Invalid key length: it is either zero or not a multiple of 8 " |
167 "bits"); | 202 "bits"); |
168 } | 203 } |
169 | 204 |
170 Status::Status(const std::string& error_details_utf8) | 205 Status::Status(blink::WebCryptoErrorType error_type, |
171 : type_(TYPE_ERROR), error_details_(error_details_utf8) {} | 206 const std::string& error_details_utf8) |
172 | 207 : type_(TYPE_ERROR), |
173 Status::Status(Type type) : type_(type) {} | 208 error_type_(error_type), |
174 | 209 error_details_(error_details_utf8) { |
| 210 } |
| 211 |
| 212 Status::Status(Type type) : type_(type) { |
| 213 } |
175 | 214 |
176 } // namespace webcrypto | 215 } // namespace webcrypto |
177 | 216 |
178 } // namespace content | 217 } // namespace content |
OLD | NEW |