OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/renderer/webcrypto/webcrypto_util.h" | 5 #include "content/renderer/webcrypto/webcrypto_util.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 9 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 11 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM |
| 12 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
| 13 #endif |
11 | 14 |
12 namespace content { | 15 namespace content { |
13 | 16 |
14 namespace webcrypto { | 17 namespace webcrypto { |
15 | 18 |
16 bool Status::IsError() const { return type_ == TYPE_ERROR; } | 19 bool Status::IsError() const { return type_ == TYPE_ERROR; } |
17 | 20 |
18 bool Status::IsSuccess() const { return type_ == TYPE_SUCCESS; } | 21 bool Status::IsSuccess() const { return type_ == TYPE_SUCCESS; } |
19 | 22 |
20 bool Status::HasErrorDetails() const { return !error_details_.empty(); } | 23 bool Status::HasErrorDetails() const { return !error_details_.empty(); } |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 } | 206 } |
204 | 207 |
205 bool IsHashAlgorithm(blink::WebCryptoAlgorithmId alg_id) { | 208 bool IsHashAlgorithm(blink::WebCryptoAlgorithmId alg_id) { |
206 return alg_id == blink::WebCryptoAlgorithmIdSha1 || | 209 return alg_id == blink::WebCryptoAlgorithmIdSha1 || |
207 alg_id == blink::WebCryptoAlgorithmIdSha224 || | 210 alg_id == blink::WebCryptoAlgorithmIdSha224 || |
208 alg_id == blink::WebCryptoAlgorithmIdSha256 || | 211 alg_id == blink::WebCryptoAlgorithmIdSha256 || |
209 alg_id == blink::WebCryptoAlgorithmIdSha384 || | 212 alg_id == blink::WebCryptoAlgorithmIdSha384 || |
210 alg_id == blink::WebCryptoAlgorithmIdSha512; | 213 alg_id == blink::WebCryptoAlgorithmIdSha512; |
211 } | 214 } |
212 | 215 |
| 216 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM |
213 blink::WebCryptoAlgorithm GetInnerHashAlgorithm( | 217 blink::WebCryptoAlgorithm GetInnerHashAlgorithm( |
214 const blink::WebCryptoAlgorithm& algorithm) { | 218 const blink::WebCryptoAlgorithm& algorithm) { |
215 DCHECK(!algorithm.isNull()); | 219 DCHECK(!algorithm.isNull()); |
| 220 switch (algorithm.paramsType()) { |
| 221 case blink::WebCryptoAlgorithmParamsTypeHmacImportParams: |
| 222 return algorithm.hmacImportParams()->hash(); |
| 223 case blink::WebCryptoAlgorithmParamsTypeHmacKeyGenParams: |
| 224 return algorithm.hmacKeyGenParams()->hash(); |
| 225 case blink::WebCryptoAlgorithmParamsTypeRsaHashedImportParams: |
| 226 return algorithm.rsaHashedImportParams()->hash(); |
| 227 case blink::WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams: |
| 228 return algorithm.rsaHashedKeyGenParams()->hash(); |
| 229 default: |
| 230 return blink::WebCryptoAlgorithm::createNull(); |
| 231 } |
| 232 } |
| 233 #else |
| 234 blink::WebCryptoAlgorithm GetInnerHashAlgorithm( |
| 235 const blink::WebCryptoAlgorithm& algorithm) { |
| 236 DCHECK(!algorithm.isNull()); |
216 switch (algorithm.id()) { | 237 switch (algorithm.id()) { |
217 case blink::WebCryptoAlgorithmIdHmac: | 238 case blink::WebCryptoAlgorithmIdHmac: |
218 if (algorithm.hmacParams()) | 239 if (algorithm.hmacParams()) |
219 return algorithm.hmacParams()->hash(); | 240 return algorithm.hmacParams()->hash(); |
220 else if (algorithm.hmacKeyParams()) | 241 else if (algorithm.hmacKeyParams()) |
221 return algorithm.hmacKeyParams()->hash(); | 242 return algorithm.hmacKeyParams()->hash(); |
222 break; | 243 break; |
223 case blink::WebCryptoAlgorithmIdRsaOaep: | 244 case blink::WebCryptoAlgorithmIdRsaOaep: |
224 if (algorithm.rsaOaepParams()) | 245 if (algorithm.rsaOaepParams()) |
225 return algorithm.rsaOaepParams()->hash(); | 246 return algorithm.rsaOaepParams()->hash(); |
226 break; | 247 break; |
227 case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5: | 248 case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5: |
228 if (algorithm.rsaSsaParams()) | 249 if (algorithm.rsaSsaParams()) |
229 return algorithm.rsaSsaParams()->hash(); | 250 return algorithm.rsaSsaParams()->hash(); |
230 break; | 251 break; |
231 default: | 252 default: |
232 break; | 253 break; |
233 } | 254 } |
234 return blink::WebCryptoAlgorithm::createNull(); | 255 return blink::WebCryptoAlgorithm::createNull(); |
235 } | 256 } |
| 257 #endif |
236 | 258 |
237 blink::WebCryptoAlgorithm CreateAlgorithm(blink::WebCryptoAlgorithmId id) { | 259 blink::WebCryptoAlgorithm CreateAlgorithm(blink::WebCryptoAlgorithmId id) { |
238 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL); | 260 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL); |
239 } | 261 } |
240 | 262 |
241 blink::WebCryptoAlgorithm CreateHmacAlgorithmByHashId( | 263 blink::WebCryptoAlgorithm CreateHmacImportAlgorithm( |
242 blink::WebCryptoAlgorithmId hash_id) { | 264 blink::WebCryptoAlgorithmId hash_id) { |
243 DCHECK(IsHashAlgorithm(hash_id)); | 265 DCHECK(IsHashAlgorithm(hash_id)); |
244 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 266 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
245 blink::WebCryptoAlgorithmIdHmac, | 267 blink::WebCryptoAlgorithmIdHmac, |
| 268 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM |
| 269 new blink::WebCryptoHmacImportParams(CreateAlgorithm(hash_id))); |
| 270 #else |
246 new blink::WebCryptoHmacParams(CreateAlgorithm(hash_id))); | 271 new blink::WebCryptoHmacParams(CreateAlgorithm(hash_id))); |
| 272 #endif |
247 } | 273 } |
248 | 274 |
249 blink::WebCryptoAlgorithm CreateHmacKeyGenAlgorithm( | 275 blink::WebCryptoAlgorithm CreateRsaSsaImportAlgorithm( |
250 blink::WebCryptoAlgorithmId hash_id, | |
251 unsigned int key_length_bytes) { | |
252 DCHECK(IsHashAlgorithm(hash_id)); | |
253 // key_length_bytes == 0 means unspecified | |
254 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | |
255 blink::WebCryptoAlgorithmIdHmac, | |
256 new blink::WebCryptoHmacKeyParams( | |
257 CreateAlgorithm(hash_id), (key_length_bytes != 0), key_length_bytes)); | |
258 } | |
259 | |
260 blink::WebCryptoAlgorithm CreateRsaSsaAlgorithm( | |
261 blink::WebCryptoAlgorithmId hash_id) { | 276 blink::WebCryptoAlgorithmId hash_id) { |
262 DCHECK(IsHashAlgorithm(hash_id)); | 277 DCHECK(IsHashAlgorithm(hash_id)); |
263 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 278 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
264 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | 279 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 280 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM |
| 281 new blink::WebCryptoRsaHashedImportParams(CreateAlgorithm(hash_id))); |
| 282 #else |
265 new blink::WebCryptoRsaSsaParams(CreateAlgorithm(hash_id))); | 283 new blink::WebCryptoRsaSsaParams(CreateAlgorithm(hash_id))); |
| 284 #endif |
266 } | 285 } |
267 | 286 |
268 blink::WebCryptoAlgorithm CreateRsaOaepAlgorithm( | 287 blink::WebCryptoAlgorithm CreateRsaOaepImportAlgorithm( |
269 blink::WebCryptoAlgorithmId hash_id) { | 288 blink::WebCryptoAlgorithmId hash_id) { |
270 DCHECK(IsHashAlgorithm(hash_id)); | 289 DCHECK(IsHashAlgorithm(hash_id)); |
271 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 290 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
272 blink::WebCryptoAlgorithmIdRsaOaep, | 291 blink::WebCryptoAlgorithmIdRsaOaep, |
| 292 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM |
| 293 new blink::WebCryptoRsaHashedImportParams( |
| 294 CreateAlgorithm(hash_id))); |
| 295 #else |
273 new blink::WebCryptoRsaOaepParams( | 296 new blink::WebCryptoRsaOaepParams( |
274 CreateAlgorithm(hash_id), false, NULL, 0)); | 297 CreateAlgorithm(hash_id), false, NULL, 0)); |
| 298 #endif |
275 } | 299 } |
276 | 300 |
277 blink::WebCryptoAlgorithm CreateRsaKeyGenAlgorithm( | |
278 blink::WebCryptoAlgorithmId algorithm_id, | |
279 unsigned int modulus_length, | |
280 const std::vector<uint8>& public_exponent) { | |
281 DCHECK(algorithm_id == blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 || | |
282 algorithm_id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || | |
283 algorithm_id == blink::WebCryptoAlgorithmIdRsaOaep); | |
284 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | |
285 algorithm_id, | |
286 new blink::WebCryptoRsaKeyGenParams( | |
287 modulus_length, | |
288 webcrypto::Uint8VectorStart(public_exponent), | |
289 public_exponent.size())); | |
290 } | |
291 | |
292 blink::WebCryptoAlgorithm CreateAesCbcAlgorithm(const std::vector<uint8>& iv) { | |
293 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | |
294 blink::WebCryptoAlgorithmIdAesCbc, | |
295 new blink::WebCryptoAesCbcParams(Uint8VectorStart(iv), iv.size())); | |
296 } | |
297 | |
298 blink::WebCryptoAlgorithm CreateAesGcmAlgorithm( | |
299 const std::vector<uint8>& iv, | |
300 const std::vector<uint8>& additional_data, | |
301 uint8 tag_length_bytes) { | |
302 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | |
303 blink::WebCryptoAlgorithmIdAesCbc, | |
304 new blink::WebCryptoAesGcmParams(Uint8VectorStart(iv), | |
305 iv.size(), | |
306 additional_data.size() != 0, | |
307 Uint8VectorStart(additional_data), | |
308 additional_data.size(), | |
309 tag_length_bytes != 0, | |
310 tag_length_bytes)); | |
311 } | |
312 | 301 |
313 unsigned int ShaBlockSizeBytes(blink::WebCryptoAlgorithmId hash_id) { | 302 unsigned int ShaBlockSizeBytes(blink::WebCryptoAlgorithmId hash_id) { |
314 switch (hash_id) { | 303 switch (hash_id) { |
315 case blink::WebCryptoAlgorithmIdSha1: | 304 case blink::WebCryptoAlgorithmIdSha1: |
316 case blink::WebCryptoAlgorithmIdSha224: | 305 case blink::WebCryptoAlgorithmIdSha224: |
317 case blink::WebCryptoAlgorithmIdSha256: | 306 case blink::WebCryptoAlgorithmIdSha256: |
318 return 64; | 307 return 64; |
319 case blink::WebCryptoAlgorithmIdSha384: | 308 case blink::WebCryptoAlgorithmIdSha384: |
320 case blink::WebCryptoAlgorithmIdSha512: | 309 case blink::WebCryptoAlgorithmIdSha512: |
321 return 128; | 310 return 128; |
322 default: | 311 default: |
323 NOTREACHED(); | 312 NOTREACHED(); |
324 return 0; | 313 return 0; |
325 } | 314 } |
326 } | 315 } |
327 | 316 |
| 317 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM |
| 318 bool CreateSecretKeyAlgorithm(const blink::WebCryptoAlgorithm& algorithm, |
| 319 unsigned keylen_bytes, |
| 320 blink::WebCryptoKeyAlgorithm* key_algorithm) { |
| 321 switch (algorithm.id()) { |
| 322 case blink::WebCryptoAlgorithmIdHmac: { |
| 323 blink::WebCryptoAlgorithm hash = GetInnerHashAlgorithm(algorithm); |
| 324 if (hash.isNull()) |
| 325 return false; |
| 326 *key_algorithm = blink::WebCryptoKeyAlgorithm::adoptParamsAndCreate( |
| 327 algorithm.id(), |
| 328 new blink::WebCryptoHmacKeyAlgorithmParams(hash)); |
| 329 return true; |
| 330 } |
| 331 case blink::WebCryptoAlgorithmIdAesKw: |
| 332 case blink::WebCryptoAlgorithmIdAesCbc: |
| 333 case blink::WebCryptoAlgorithmIdAesCtr: |
| 334 case blink::WebCryptoAlgorithmIdAesGcm: |
| 335 *key_algorithm = blink::WebCryptoKeyAlgorithm::adoptParamsAndCreate( |
| 336 algorithm.id(), |
| 337 new blink::WebCryptoAesKeyAlgorithmParams(keylen_bytes * 8)); |
| 338 return true; |
| 339 default: |
| 340 return false; |
| 341 } |
| 342 } |
| 343 #else |
| 344 bool CreateSecretKeyAlgorithm(const blink::WebCryptoAlgorithm& algorithm, |
| 345 unsigned keylen_bytes, |
| 346 blink::WebCryptoAlgorithm* key_algorithm) { |
| 347 *key_algorithm = algorithm; |
| 348 return true; |
| 349 } |
| 350 #endif |
| 351 |
328 } // namespace webcrypto | 352 } // namespace webcrypto |
329 | 353 |
330 } // namespace content | 354 } // namespace content |
OLD | NEW |