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