cryptographer_impl.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/sync/nigori/cryptographer_impl.h"
  5. #include <utility>
  6. #include "base/check.h"
  7. #include "base/memory/ptr_util.h"
  8. namespace syncer {
  9. // static
  10. std::unique_ptr<CryptographerImpl> CryptographerImpl::CreateEmpty() {
  11. return base::WrapUnique(
  12. new CryptographerImpl(NigoriKeyBag::CreateEmpty(),
  13. /*default_encryption_key_name=*/std::string()));
  14. }
  15. // static
  16. std::unique_ptr<CryptographerImpl> CryptographerImpl::FromSingleKeyForTesting(
  17. const std::string& passphrase,
  18. const KeyDerivationParams& derivation_params) {
  19. std::unique_ptr<CryptographerImpl> cryptographer = CreateEmpty();
  20. std::string key_name =
  21. cryptographer->EmplaceKey(passphrase, derivation_params);
  22. cryptographer->SelectDefaultEncryptionKey(key_name);
  23. return cryptographer;
  24. }
  25. // static
  26. std::unique_ptr<CryptographerImpl> CryptographerImpl::FromProto(
  27. const sync_pb::CryptographerData& proto) {
  28. NigoriKeyBag key_bag = NigoriKeyBag::CreateFromProto(proto.key_bag());
  29. // TODO(crbug.com/1109221): An invalid local state should be handled in the
  30. // caller instead of CHECK-ing here, e.g. by resetting the local state.
  31. CHECK(proto.default_key_name().empty() ||
  32. key_bag.HasKey(proto.default_key_name()));
  33. return base::WrapUnique(
  34. new CryptographerImpl(std::move(key_bag), proto.default_key_name()));
  35. }
  36. CryptographerImpl::CryptographerImpl(NigoriKeyBag key_bag,
  37. std::string default_encryption_key_name)
  38. : key_bag_(std::move(key_bag)),
  39. default_encryption_key_name_(std::move(default_encryption_key_name)) {
  40. DCHECK(default_encryption_key_name_.empty() ||
  41. key_bag_.HasKey(default_encryption_key_name_));
  42. }
  43. CryptographerImpl::~CryptographerImpl() = default;
  44. sync_pb::CryptographerData CryptographerImpl::ToProto() const {
  45. sync_pb::CryptographerData proto;
  46. *proto.mutable_key_bag() = key_bag_.ToProto();
  47. proto.set_default_key_name(default_encryption_key_name_);
  48. return proto;
  49. }
  50. std::string CryptographerImpl::EmplaceKey(
  51. const std::string& passphrase,
  52. const KeyDerivationParams& derivation_params) {
  53. return key_bag_.AddKey(
  54. Nigori::CreateByDerivation(derivation_params, passphrase));
  55. }
  56. void CryptographerImpl::EmplaceKeysFrom(const NigoriKeyBag& key_bag) {
  57. key_bag_.AddAllUnknownKeysFrom(key_bag);
  58. }
  59. void CryptographerImpl::SelectDefaultEncryptionKey(
  60. const std::string& key_name) {
  61. DCHECK(!key_name.empty());
  62. DCHECK(key_bag_.HasKey(key_name));
  63. default_encryption_key_name_ = key_name;
  64. }
  65. void CryptographerImpl::EmplaceKeysAndSelectDefaultKeyFrom(
  66. const CryptographerImpl& other) {
  67. EmplaceKeysFrom(other.key_bag_);
  68. SelectDefaultEncryptionKey(other.default_encryption_key_name_);
  69. }
  70. void CryptographerImpl::ClearDefaultEncryptionKey() {
  71. default_encryption_key_name_.clear();
  72. }
  73. void CryptographerImpl::ClearAllKeys() {
  74. default_encryption_key_name_.clear();
  75. key_bag_ = NigoriKeyBag::CreateEmpty();
  76. }
  77. bool CryptographerImpl::HasKey(const std::string& key_name) const {
  78. return key_bag_.HasKey(key_name);
  79. }
  80. sync_pb::NigoriKey CryptographerImpl::ExportDefaultKey() const {
  81. DCHECK(CanEncrypt());
  82. return key_bag_.ExportKey(default_encryption_key_name_);
  83. }
  84. std::unique_ptr<CryptographerImpl> CryptographerImpl::Clone() const {
  85. return base::WrapUnique(
  86. new CryptographerImpl(key_bag_.Clone(), default_encryption_key_name_));
  87. }
  88. size_t CryptographerImpl::KeyBagSizeForTesting() const {
  89. return key_bag_.size();
  90. }
  91. bool CryptographerImpl::CanEncrypt() const {
  92. return !default_encryption_key_name_.empty();
  93. }
  94. bool CryptographerImpl::CanDecrypt(
  95. const sync_pb::EncryptedData& encrypted) const {
  96. return key_bag_.HasKey(encrypted.key_name());
  97. }
  98. std::string CryptographerImpl::GetDefaultEncryptionKeyName() const {
  99. return default_encryption_key_name_;
  100. }
  101. bool CryptographerImpl::EncryptString(const std::string& decrypted,
  102. sync_pb::EncryptedData* encrypted) const {
  103. DCHECK(encrypted);
  104. encrypted->Clear();
  105. if (!CanEncrypt()) {
  106. return false;
  107. }
  108. return key_bag_.EncryptWithKey(default_encryption_key_name_, decrypted,
  109. encrypted);
  110. }
  111. bool CryptographerImpl::DecryptToString(const sync_pb::EncryptedData& encrypted,
  112. std::string* decrypted) const {
  113. DCHECK(decrypted);
  114. return key_bag_.Decrypt(encrypted, decrypted);
  115. }
  116. } // namespace syncer