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 "jwk.h" | 5 #include "jwk.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <functional> | 8 #include <functional> |
9 #include <map> | 9 #include <map> |
10 | 10 |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 dict_.SetString(key, value); | 234 dict_.SetString(key, value); |
235 } | 235 } |
236 | 236 |
237 void SetBase64Encoded(const std::string& key, const CryptoData& value) { | 237 void SetBase64Encoded(const std::string& key, const CryptoData& value) { |
238 dict_.SetString(key, | 238 dict_.SetString(key, |
239 Base64EncodeUrlSafe(base::StringPiece( | 239 Base64EncodeUrlSafe(base::StringPiece( |
240 reinterpret_cast<const char*>(value.bytes()), | 240 reinterpret_cast<const char*>(value.bytes()), |
241 value.byte_length()))); | 241 value.byte_length()))); |
242 } | 242 } |
243 | 243 |
244 void ToBytes(std::vector<uint8>* utf8_bytes) { | 244 void ToBytes(std::vector<uint8_t>* utf8_bytes) { |
245 std::string json; | 245 std::string json; |
246 base::JSONWriter::Write(&dict_, &json); | 246 base::JSONWriter::Write(&dict_, &json); |
247 utf8_bytes->assign(json.begin(), json.end()); | 247 utf8_bytes->assign(json.begin(), json.end()); |
248 } | 248 } |
249 | 249 |
250 private: | 250 private: |
251 base::DictionaryValue dict_; | 251 base::DictionaryValue dict_; |
252 }; | 252 }; |
253 | 253 |
254 // Extracts the required string property with key |path| from |dict| and saves | 254 // Extracts the required string property with key |path| from |dict| and saves |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 if (status.IsError()) | 475 if (status.IsError()) |
476 return status; | 476 return status; |
477 | 477 |
478 return Status::Success(); | 478 return Status::Success(); |
479 } | 479 } |
480 | 480 |
481 Status ReadSecretKeyNoExpectedAlg( | 481 Status ReadSecretKeyNoExpectedAlg( |
482 const CryptoData& key_data, | 482 const CryptoData& key_data, |
483 bool expected_extractable, | 483 bool expected_extractable, |
484 blink::WebCryptoKeyUsageMask expected_usage_mask, | 484 blink::WebCryptoKeyUsageMask expected_usage_mask, |
485 std::vector<uint8>* raw_key_data, | 485 std::vector<uint8_t>* raw_key_data, |
486 scoped_ptr<base::DictionaryValue>* dict) { | 486 scoped_ptr<base::DictionaryValue>* dict) { |
487 if (!key_data.byte_length()) | 487 if (!key_data.byte_length()) |
488 return Status::ErrorImportEmptyKeyData(); | 488 return Status::ErrorImportEmptyKeyData(); |
489 | 489 |
490 std::string kty; | 490 std::string kty; |
491 Status status = ParseJwkCommon( | 491 Status status = ParseJwkCommon( |
492 key_data, expected_extractable, expected_usage_mask, &kty, dict); | 492 key_data, expected_extractable, expected_usage_mask, &kty, dict); |
493 if (status.IsError()) | 493 if (status.IsError()) |
494 return status; | 494 return status; |
495 | 495 |
496 if (kty != "oct") | 496 if (kty != "oct") |
497 return Status::ErrorJwkUnexpectedKty("oct"); | 497 return Status::ErrorJwkUnexpectedKty("oct"); |
498 | 498 |
499 std::string jwk_k_value; | 499 std::string jwk_k_value; |
500 status = GetJwkBytes(dict->get(), "k", &jwk_k_value); | 500 status = GetJwkBytes(dict->get(), "k", &jwk_k_value); |
501 if (status.IsError()) | 501 if (status.IsError()) |
502 return status; | 502 return status; |
503 raw_key_data->assign(jwk_k_value.begin(), jwk_k_value.end()); | 503 raw_key_data->assign(jwk_k_value.begin(), jwk_k_value.end()); |
504 | 504 |
505 return Status::Success(); | 505 return Status::Success(); |
506 } | 506 } |
507 | 507 |
508 } // namespace | 508 } // namespace |
509 | 509 |
510 void WriteSecretKeyJwk(const CryptoData& raw_key_data, | 510 void WriteSecretKeyJwk(const CryptoData& raw_key_data, |
511 const std::string& algorithm, | 511 const std::string& algorithm, |
512 bool extractable, | 512 bool extractable, |
513 blink::WebCryptoKeyUsageMask usage_mask, | 513 blink::WebCryptoKeyUsageMask usage_mask, |
514 std::vector<uint8>* jwk_key_data) { | 514 std::vector<uint8_t>* jwk_key_data) { |
515 JwkWriter writer(algorithm, extractable, usage_mask, "oct"); | 515 JwkWriter writer(algorithm, extractable, usage_mask, "oct"); |
516 writer.SetBase64Encoded("k", raw_key_data); | 516 writer.SetBase64Encoded("k", raw_key_data); |
517 writer.ToBytes(jwk_key_data); | 517 writer.ToBytes(jwk_key_data); |
518 } | 518 } |
519 | 519 |
520 Status ReadSecretKeyJwk(const CryptoData& key_data, | 520 Status ReadSecretKeyJwk(const CryptoData& key_data, |
521 const std::string& expected_algorithm, | 521 const std::string& expected_algorithm, |
522 bool expected_extractable, | 522 bool expected_extractable, |
523 blink::WebCryptoKeyUsageMask expected_usage_mask, | 523 blink::WebCryptoKeyUsageMask expected_usage_mask, |
524 std::vector<uint8>* raw_key_data) { | 524 std::vector<uint8_t>* raw_key_data) { |
525 scoped_ptr<base::DictionaryValue> dict; | 525 scoped_ptr<base::DictionaryValue> dict; |
526 Status status = ReadSecretKeyNoExpectedAlg( | 526 Status status = ReadSecretKeyNoExpectedAlg( |
527 key_data, expected_extractable, expected_usage_mask, raw_key_data, &dict); | 527 key_data, expected_extractable, expected_usage_mask, raw_key_data, &dict); |
528 if (status.IsError()) | 528 if (status.IsError()) |
529 return status; | 529 return status; |
530 return VerifyAlg(dict.get(), expected_algorithm); | 530 return VerifyAlg(dict.get(), expected_algorithm); |
531 } | 531 } |
532 | 532 |
533 std::string MakeJwkAesAlgorithmName(const std::string& suffix, | 533 std::string MakeJwkAesAlgorithmName(const std::string& suffix, |
534 unsigned int keylen_bytes) { | 534 unsigned int keylen_bytes) { |
535 if (keylen_bytes == 16) | 535 if (keylen_bytes == 16) |
536 return std::string("A128") + suffix; | 536 return std::string("A128") + suffix; |
537 if (keylen_bytes == 24) | 537 if (keylen_bytes == 24) |
538 return std::string("A192") + suffix; | 538 return std::string("A192") + suffix; |
539 if (keylen_bytes == 32) | 539 if (keylen_bytes == 32) |
540 return std::string("A256") + suffix; | 540 return std::string("A256") + suffix; |
541 return std::string(); | 541 return std::string(); |
542 } | 542 } |
543 | 543 |
544 Status ReadAesSecretKeyJwk(const CryptoData& key_data, | 544 Status ReadAesSecretKeyJwk(const CryptoData& key_data, |
545 const std::string& algorithm_name_suffix, | 545 const std::string& algorithm_name_suffix, |
546 bool expected_extractable, | 546 bool expected_extractable, |
547 blink::WebCryptoKeyUsageMask expected_usage_mask, | 547 blink::WebCryptoKeyUsageMask expected_usage_mask, |
548 std::vector<uint8>* raw_key_data) { | 548 std::vector<uint8_t>* raw_key_data) { |
549 scoped_ptr<base::DictionaryValue> dict; | 549 scoped_ptr<base::DictionaryValue> dict; |
550 Status status = ReadSecretKeyNoExpectedAlg( | 550 Status status = ReadSecretKeyNoExpectedAlg( |
551 key_data, expected_extractable, expected_usage_mask, raw_key_data, &dict); | 551 key_data, expected_extractable, expected_usage_mask, raw_key_data, &dict); |
552 if (status.IsError()) | 552 if (status.IsError()) |
553 return status; | 553 return status; |
554 | 554 |
555 bool has_jwk_alg; | 555 bool has_jwk_alg; |
556 std::string jwk_alg; | 556 std::string jwk_alg; |
557 status = GetOptionalJwkString(dict.get(), "alg", &jwk_alg, &has_jwk_alg); | 557 status = GetOptionalJwkString(dict.get(), "alg", &jwk_alg, &has_jwk_alg); |
558 if (status.IsError()) | 558 if (status.IsError()) |
(...skipping 16 matching lines...) Expand all Loading... |
575 | 575 |
576 return Status::Success(); | 576 return Status::Success(); |
577 } | 577 } |
578 | 578 |
579 // Writes an RSA public key to a JWK dictionary | 579 // Writes an RSA public key to a JWK dictionary |
580 void WriteRsaPublicKeyJwk(const CryptoData& n, | 580 void WriteRsaPublicKeyJwk(const CryptoData& n, |
581 const CryptoData& e, | 581 const CryptoData& e, |
582 const std::string& algorithm, | 582 const std::string& algorithm, |
583 bool extractable, | 583 bool extractable, |
584 blink::WebCryptoKeyUsageMask usage_mask, | 584 blink::WebCryptoKeyUsageMask usage_mask, |
585 std::vector<uint8>* jwk_key_data) { | 585 std::vector<uint8_t>* jwk_key_data) { |
586 JwkWriter writer(algorithm, extractable, usage_mask, "RSA"); | 586 JwkWriter writer(algorithm, extractable, usage_mask, "RSA"); |
587 writer.SetBase64Encoded("n", n); | 587 writer.SetBase64Encoded("n", n); |
588 writer.SetBase64Encoded("e", e); | 588 writer.SetBase64Encoded("e", e); |
589 writer.ToBytes(jwk_key_data); | 589 writer.ToBytes(jwk_key_data); |
590 } | 590 } |
591 | 591 |
592 // Writes an RSA private key to a JWK dictionary | 592 // Writes an RSA private key to a JWK dictionary |
593 void WriteRsaPrivateKeyJwk(const CryptoData& n, | 593 void WriteRsaPrivateKeyJwk(const CryptoData& n, |
594 const CryptoData& e, | 594 const CryptoData& e, |
595 const CryptoData& d, | 595 const CryptoData& d, |
596 const CryptoData& p, | 596 const CryptoData& p, |
597 const CryptoData& q, | 597 const CryptoData& q, |
598 const CryptoData& dp, | 598 const CryptoData& dp, |
599 const CryptoData& dq, | 599 const CryptoData& dq, |
600 const CryptoData& qi, | 600 const CryptoData& qi, |
601 const std::string& algorithm, | 601 const std::string& algorithm, |
602 bool extractable, | 602 bool extractable, |
603 blink::WebCryptoKeyUsageMask usage_mask, | 603 blink::WebCryptoKeyUsageMask usage_mask, |
604 std::vector<uint8>* jwk_key_data) { | 604 std::vector<uint8_t>* jwk_key_data) { |
605 JwkWriter writer(algorithm, extractable, usage_mask, "RSA"); | 605 JwkWriter writer(algorithm, extractable, usage_mask, "RSA"); |
606 | 606 |
607 writer.SetBase64Encoded("n", n); | 607 writer.SetBase64Encoded("n", n); |
608 writer.SetBase64Encoded("e", e); | 608 writer.SetBase64Encoded("e", e); |
609 writer.SetBase64Encoded("d", d); | 609 writer.SetBase64Encoded("d", d); |
610 // Although these are "optional" in the JWA, WebCrypto spec requires them to | 610 // Although these are "optional" in the JWA, WebCrypto spec requires them to |
611 // be emitted. | 611 // be emitted. |
612 writer.SetBase64Encoded("p", p); | 612 writer.SetBase64Encoded("p", p); |
613 writer.SetBase64Encoded("q", q); | 613 writer.SetBase64Encoded("q", q); |
614 writer.SetBase64Encoded("dp", dp); | 614 writer.SetBase64Encoded("dp", dp); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
712 case blink::WebCryptoAlgorithmIdSha512: | 712 case blink::WebCryptoAlgorithmIdSha512: |
713 return "HS512"; | 713 return "HS512"; |
714 default: | 714 default: |
715 return NULL; | 715 return NULL; |
716 } | 716 } |
717 } | 717 } |
718 | 718 |
719 } // namespace webcrypto | 719 } // namespace webcrypto |
720 | 720 |
721 } // namespace content | 721 } // namespace content |
OLD | NEW |