123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- // Copyright 2019 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "components/sync/nigori/cryptographer_impl.h"
- #include <utility>
- #include "base/check.h"
- #include "base/memory/ptr_util.h"
- namespace syncer {
- // static
- std::unique_ptr<CryptographerImpl> CryptographerImpl::CreateEmpty() {
- return base::WrapUnique(
- new CryptographerImpl(NigoriKeyBag::CreateEmpty(),
- /*default_encryption_key_name=*/std::string()));
- }
- // static
- std::unique_ptr<CryptographerImpl> CryptographerImpl::FromSingleKeyForTesting(
- const std::string& passphrase,
- const KeyDerivationParams& derivation_params) {
- std::unique_ptr<CryptographerImpl> cryptographer = CreateEmpty();
- std::string key_name =
- cryptographer->EmplaceKey(passphrase, derivation_params);
- cryptographer->SelectDefaultEncryptionKey(key_name);
- return cryptographer;
- }
- // static
- std::unique_ptr<CryptographerImpl> CryptographerImpl::FromProto(
- const sync_pb::CryptographerData& proto) {
- NigoriKeyBag key_bag = NigoriKeyBag::CreateFromProto(proto.key_bag());
- // TODO(crbug.com/1109221): An invalid local state should be handled in the
- // caller instead of CHECK-ing here, e.g. by resetting the local state.
- CHECK(proto.default_key_name().empty() ||
- key_bag.HasKey(proto.default_key_name()));
- return base::WrapUnique(
- new CryptographerImpl(std::move(key_bag), proto.default_key_name()));
- }
- CryptographerImpl::CryptographerImpl(NigoriKeyBag key_bag,
- std::string default_encryption_key_name)
- : key_bag_(std::move(key_bag)),
- default_encryption_key_name_(std::move(default_encryption_key_name)) {
- DCHECK(default_encryption_key_name_.empty() ||
- key_bag_.HasKey(default_encryption_key_name_));
- }
- CryptographerImpl::~CryptographerImpl() = default;
- sync_pb::CryptographerData CryptographerImpl::ToProto() const {
- sync_pb::CryptographerData proto;
- *proto.mutable_key_bag() = key_bag_.ToProto();
- proto.set_default_key_name(default_encryption_key_name_);
- return proto;
- }
- std::string CryptographerImpl::EmplaceKey(
- const std::string& passphrase,
- const KeyDerivationParams& derivation_params) {
- return key_bag_.AddKey(
- Nigori::CreateByDerivation(derivation_params, passphrase));
- }
- void CryptographerImpl::EmplaceKeysFrom(const NigoriKeyBag& key_bag) {
- key_bag_.AddAllUnknownKeysFrom(key_bag);
- }
- void CryptographerImpl::SelectDefaultEncryptionKey(
- const std::string& key_name) {
- DCHECK(!key_name.empty());
- DCHECK(key_bag_.HasKey(key_name));
- default_encryption_key_name_ = key_name;
- }
- void CryptographerImpl::EmplaceKeysAndSelectDefaultKeyFrom(
- const CryptographerImpl& other) {
- EmplaceKeysFrom(other.key_bag_);
- SelectDefaultEncryptionKey(other.default_encryption_key_name_);
- }
- void CryptographerImpl::ClearDefaultEncryptionKey() {
- default_encryption_key_name_.clear();
- }
- void CryptographerImpl::ClearAllKeys() {
- default_encryption_key_name_.clear();
- key_bag_ = NigoriKeyBag::CreateEmpty();
- }
- bool CryptographerImpl::HasKey(const std::string& key_name) const {
- return key_bag_.HasKey(key_name);
- }
- sync_pb::NigoriKey CryptographerImpl::ExportDefaultKey() const {
- DCHECK(CanEncrypt());
- return key_bag_.ExportKey(default_encryption_key_name_);
- }
- std::unique_ptr<CryptographerImpl> CryptographerImpl::Clone() const {
- return base::WrapUnique(
- new CryptographerImpl(key_bag_.Clone(), default_encryption_key_name_));
- }
- size_t CryptographerImpl::KeyBagSizeForTesting() const {
- return key_bag_.size();
- }
- bool CryptographerImpl::CanEncrypt() const {
- return !default_encryption_key_name_.empty();
- }
- bool CryptographerImpl::CanDecrypt(
- const sync_pb::EncryptedData& encrypted) const {
- return key_bag_.HasKey(encrypted.key_name());
- }
- std::string CryptographerImpl::GetDefaultEncryptionKeyName() const {
- return default_encryption_key_name_;
- }
- bool CryptographerImpl::EncryptString(const std::string& decrypted,
- sync_pb::EncryptedData* encrypted) const {
- DCHECK(encrypted);
- encrypted->Clear();
- if (!CanEncrypt()) {
- return false;
- }
- return key_bag_.EncryptWithKey(default_encryption_key_name_, decrypted,
- encrypted);
- }
- bool CryptographerImpl::DecryptToString(const sync_pb::EncryptedData& encrypted,
- std::string* decrypted) const {
- DCHECK(decrypted);
- return key_bag_.Decrypt(encrypted, decrypted);
- }
- } // namespace syncer
|