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/shared_crypto.h" | 5 #include "content/child/webcrypto/shared_crypto.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "content/child/webcrypto/crypto_data.h" | 8 #include "content/child/webcrypto/crypto_data.h" |
9 #include "content/child/webcrypto/platform_crypto.h" | 9 #include "content/child/webcrypto/platform_crypto.h" |
10 #include "content/child/webcrypto/webcrypto_util.h" | 10 #include "content/child/webcrypto/webcrypto_util.h" |
(...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
490 if (wrapping_algorithm.id() != wrapping_key.algorithm().id()) | 490 if (wrapping_algorithm.id() != wrapping_key.algorithm().id()) |
491 return Status::ErrorUnexpected(); | 491 return Status::ErrorUnexpected(); |
492 | 492 |
493 // TODO (padolph): Handle formats other than raw | 493 // TODO (padolph): Handle formats other than raw |
494 if (format != blink::WebCryptoKeyFormatRaw) | 494 if (format != blink::WebCryptoKeyFormatRaw) |
495 return Status::ErrorUnsupported(); | 495 return Status::ErrorUnsupported(); |
496 // TODO (padolph): Handle key-to-wrap types other than secret/symmetric | 496 // TODO (padolph): Handle key-to-wrap types other than secret/symmetric |
497 if (key_to_wrap.type() != blink::WebCryptoKeyTypeSecret) | 497 if (key_to_wrap.type() != blink::WebCryptoKeyTypeSecret) |
498 return Status::ErrorUnsupported(); | 498 return Status::ErrorUnsupported(); |
499 | 499 |
500 platform::SymKey* platform_wrapping_key; | |
501 Status status = ToPlatformSymKey(wrapping_key, &platform_wrapping_key); | |
502 if (status.IsError()) | |
503 return status; | |
504 platform::SymKey* platform_key; | 500 platform::SymKey* platform_key; |
505 status = ToPlatformSymKey(key_to_wrap, &platform_key); | 501 Status status = ToPlatformSymKey(key_to_wrap, &platform_key); |
506 if (status.IsError()) | 502 if (status.IsError()) |
507 return status; | 503 return status; |
508 | 504 |
509 // TODO(padolph): Handle other wrapping algorithms | 505 // TODO(padolph): Handle other wrapping algorithms |
510 switch (wrapping_algorithm.id()) { | 506 switch (wrapping_algorithm.id()) { |
511 case blink::WebCryptoAlgorithmIdAesKw: | 507 case blink::WebCryptoAlgorithmIdAesKw: { |
| 508 platform::SymKey* platform_wrapping_key; |
| 509 status = ToPlatformSymKey(wrapping_key, &platform_wrapping_key); |
| 510 if (status.IsError()) |
| 511 return status; |
512 return platform::WrapSymKeyAesKw( | 512 return platform::WrapSymKeyAesKw( |
513 platform_wrapping_key, platform_key, buffer); | 513 platform_wrapping_key, platform_key, buffer); |
| 514 } |
| 515 case blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5: { |
| 516 platform::PublicKey* platform_wrapping_key; |
| 517 status = ToPlatformPublicKey(wrapping_key, &platform_wrapping_key); |
| 518 if (status.IsError()) |
| 519 return status; |
| 520 return platform::WrapSymKeyRsaEs( |
| 521 platform_wrapping_key, platform_key, buffer); |
| 522 } |
514 default: | 523 default: |
515 return Status::ErrorUnsupported(); | 524 return Status::ErrorUnsupported(); |
516 } | 525 } |
517 } | 526 } |
518 | 527 |
519 Status UnwrapKey(blink::WebCryptoKeyFormat format, | 528 Status UnwrapKey(blink::WebCryptoKeyFormat format, |
520 const CryptoData& wrapped_key_data, | 529 const CryptoData& wrapped_key_data, |
521 const blink::WebCryptoKey& wrapping_key, | 530 const blink::WebCryptoKey& wrapping_key, |
522 const blink::WebCryptoAlgorithm& wrapping_algorithm, | 531 const blink::WebCryptoAlgorithm& wrapping_algorithm, |
523 const blink::WebCryptoAlgorithm& algorithm_or_null, | 532 const blink::WebCryptoAlgorithm& algorithm_or_null, |
524 bool extractable, | 533 bool extractable, |
525 blink::WebCryptoKeyUsageMask usage_mask, | 534 blink::WebCryptoKeyUsageMask usage_mask, |
526 blink::WebCryptoKey* key) { | 535 blink::WebCryptoKey* key) { |
527 if (!KeyUsageAllows(wrapping_key, blink::WebCryptoKeyUsageUnwrapKey)) | 536 if (!KeyUsageAllows(wrapping_key, blink::WebCryptoKeyUsageUnwrapKey)) |
528 return Status::ErrorUnexpected(); | 537 return Status::ErrorUnexpected(); |
529 if (wrapping_algorithm.id() != wrapping_key.algorithm().id()) | 538 if (wrapping_algorithm.id() != wrapping_key.algorithm().id()) |
530 return Status::ErrorUnexpected(); | 539 return Status::ErrorUnexpected(); |
531 | 540 |
532 // TODO(padolph): Handle formats other than raw | 541 // TODO(padolph): Handle formats other than raw |
533 if (format != blink::WebCryptoKeyFormatRaw) | 542 if (format != blink::WebCryptoKeyFormatRaw) |
534 return Status::ErrorUnsupported(); | 543 return Status::ErrorUnsupported(); |
535 | 544 |
536 // Must provide an algorithm when unwrapping a raw key | 545 // Must provide an algorithm when unwrapping a raw key |
537 if (format == blink::WebCryptoKeyFormatRaw && algorithm_or_null.isNull()) | 546 if (format == blink::WebCryptoKeyFormatRaw && algorithm_or_null.isNull()) |
538 return Status::ErrorMissingAlgorithmUnwrapRawKey(); | 547 return Status::ErrorMissingAlgorithmUnwrapRawKey(); |
539 | 548 |
540 platform::SymKey* platform_wrapping_key; | |
541 Status status = ToPlatformSymKey(wrapping_key, &platform_wrapping_key); | |
542 if (status.IsError()) | |
543 return status; | |
544 | |
545 // TODO(padolph): Handle other wrapping algorithms | 549 // TODO(padolph): Handle other wrapping algorithms |
546 switch (wrapping_algorithm.id()) { | 550 switch (wrapping_algorithm.id()) { |
547 case blink::WebCryptoAlgorithmIdAesKw: { | 551 case blink::WebCryptoAlgorithmIdAesKw: { |
| 552 platform::SymKey* platform_wrapping_key; |
| 553 Status status = ToPlatformSymKey(wrapping_key, &platform_wrapping_key); |
| 554 if (status.IsError()) |
| 555 return status; |
548 // AES-KW requires the wrapped key data size must be at least 24 bytes and | 556 // AES-KW requires the wrapped key data size must be at least 24 bytes and |
549 // also a multiple of 8 bytes. | 557 // also a multiple of 8 bytes. |
550 if (wrapped_key_data.byte_length() < 24) | 558 if (wrapped_key_data.byte_length() < 24) |
551 return Status::ErrorDataTooSmall(); | 559 return Status::ErrorDataTooSmall(); |
552 if (wrapped_key_data.byte_length() % 8) | 560 if (wrapped_key_data.byte_length() % 8) |
553 return Status::ErrorInvalidAesKwDataLength(); | 561 return Status::ErrorInvalidAesKwDataLength(); |
554 return platform::UnwrapSymKeyAesKw(wrapped_key_data, | 562 return platform::UnwrapSymKeyAesKw(wrapped_key_data, |
555 platform_wrapping_key, | 563 platform_wrapping_key, |
556 algorithm_or_null, | 564 algorithm_or_null, |
557 extractable, | 565 extractable, |
558 usage_mask, | 566 usage_mask, |
559 key); | 567 key); |
560 } | 568 } |
| 569 case blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5: { |
| 570 platform::PrivateKey* platform_wrapping_key; |
| 571 Status status = |
| 572 ToPlatformPrivateKey(wrapping_key, &platform_wrapping_key); |
| 573 if (status.IsError()) |
| 574 return status; |
| 575 return platform::UnwrapSymKeyRsaEs(wrapped_key_data, |
| 576 platform_wrapping_key, |
| 577 algorithm_or_null, |
| 578 extractable, |
| 579 usage_mask, |
| 580 key); |
| 581 } |
561 default: | 582 default: |
562 return Status::ErrorUnsupported(); | 583 return Status::ErrorUnsupported(); |
563 } | 584 } |
564 } | 585 } |
565 | 586 |
566 } // namespace webcrypto | 587 } // namespace webcrypto |
567 | 588 |
568 } // namespace content | 589 } // namespace content |
OLD | NEW |