This source file includes following definitions.
- ProcessUnsyncedChangesForEncryption
- VerifyUnsyncedChangesAreEncrypted
- EntryNeedsEncryption
- SpecificsNeedsEncryption
- VerifyDataTypeEncryptionForTest
- UpdateEntryWithEncryption
- UpdateNigoriFromEncryptedTypes
- GetEncryptedTypesFromNigori
#include "sync/syncable/nigori_util.h"
#include <queue>
#include <string>
#include <vector>
#include "base/json/json_writer.h"
#include "sync/syncable/directory.h"
#include "sync/syncable/entry.h"
#include "sync/syncable/nigori_handler.h"
#include "sync/syncable/mutable_entry.h"
#include "sync/syncable/syncable_util.h"
#include "sync/syncable/syncable_write_transaction.h"
#include "sync/util/cryptographer.h"
namespace syncer {
namespace syncable {
bool ProcessUnsyncedChangesForEncryption(
WriteTransaction* const trans) {
NigoriHandler* nigori_handler = trans->directory()->GetNigoriHandler();
ModelTypeSet encrypted_types = nigori_handler->GetEncryptedTypes(trans);
Cryptographer* cryptographer = trans->directory()->GetCryptographer(trans);
DCHECK(cryptographer->is_ready());
std::vector<int64> handles;
GetUnsyncedEntries(trans, &handles);
for (size_t i = 0; i < handles.size(); ++i) {
MutableEntry entry(trans, GET_BY_HANDLE, handles[i]);
const sync_pb::EntitySpecifics& specifics = entry.GetSpecifics();
if (!SpecificsNeedsEncryption(encrypted_types, specifics))
continue;
if (!UpdateEntryWithEncryption(trans, specifics, &entry))
return false;
}
return true;
}
bool VerifyUnsyncedChangesAreEncrypted(
BaseTransaction* const trans,
ModelTypeSet encrypted_types) {
std::vector<int64> handles;
GetUnsyncedEntries(trans, &handles);
for (size_t i = 0; i < handles.size(); ++i) {
Entry entry(trans, GET_BY_HANDLE, handles[i]);
if (!entry.good()) {
NOTREACHED();
return false;
}
if (EntryNeedsEncryption(encrypted_types, entry))
return false;
}
return true;
}
bool EntryNeedsEncryption(ModelTypeSet encrypted_types,
const Entry& entry) {
if (!entry.GetUniqueServerTag().empty())
return false;
ModelType type = entry.GetModelType();
if (type == PASSWORDS || IsControlType(type))
return false;
return SpecificsNeedsEncryption(encrypted_types, entry.GetSpecifics()) ||
(encrypted_types.Has(type) &&
entry.GetNonUniqueName() != kEncryptedString);
}
bool SpecificsNeedsEncryption(ModelTypeSet encrypted_types,
const sync_pb::EntitySpecifics& specifics) {
const ModelType type = GetModelTypeFromSpecifics(specifics);
if (type == PASSWORDS || IsControlType(type))
return false;
if (!encrypted_types.Has(type))
return false;
return !specifics.has_encrypted();
}
bool VerifyDataTypeEncryptionForTest(
BaseTransaction* const trans,
ModelType type,
bool is_encrypted) {
Cryptographer* cryptographer = trans->directory()->GetCryptographer(trans);
if (type == PASSWORDS || IsControlType(type)) {
NOTREACHED();
return true;
}
std::string type_tag = ModelTypeToRootTag(type);
Entry type_root(trans, GET_BY_SERVER_TAG, type_tag);
if (!type_root.good()) {
NOTREACHED();
return false;
}
std::queue<Id> to_visit;
Id id_string = type_root.GetFirstChildId();
to_visit.push(id_string);
while (!to_visit.empty()) {
id_string = to_visit.front();
to_visit.pop();
if (id_string.IsRoot())
continue;
Entry child(trans, GET_BY_ID, id_string);
if (!child.good()) {
NOTREACHED();
return false;
}
if (child.GetIsDir()) {
Id child_id_string = child.GetFirstChildId();
to_visit.push(child_id_string);
}
const sync_pb::EntitySpecifics& specifics = child.GetSpecifics();
DCHECK_EQ(type, child.GetModelType());
DCHECK_EQ(type, GetModelTypeFromSpecifics(specifics));
if (child.GetUniqueServerTag().empty()) {
if (specifics.has_encrypted() != is_encrypted)
return false;
if (specifics.has_encrypted()) {
if (child.GetNonUniqueName() != kEncryptedString)
return false;
if (!cryptographer->CanDecryptUsingDefaultKey(specifics.encrypted()))
return false;
}
}
to_visit.push(child.GetSuccessorId());
}
return true;
}
bool UpdateEntryWithEncryption(
BaseTransaction* const trans,
const sync_pb::EntitySpecifics& new_specifics,
syncable::MutableEntry* entry) {
NigoriHandler* nigori_handler = trans->directory()->GetNigoriHandler();
Cryptographer* cryptographer = trans->directory()->GetCryptographer(trans);
ModelType type = GetModelTypeFromSpecifics(new_specifics);
DCHECK_GE(type, FIRST_REAL_MODEL_TYPE);
const sync_pb::EntitySpecifics& old_specifics = entry->GetSpecifics();
const ModelTypeSet encrypted_types = nigori_handler->GetEncryptedTypes(trans);
bool was_encrypted = old_specifics.has_encrypted();
sync_pb::EntitySpecifics generated_specifics;
if (new_specifics.has_encrypted()) {
NOTREACHED() << "New specifics already has an encrypted blob.";
return false;
}
if ((!SpecificsNeedsEncryption(encrypted_types, new_specifics) &&
!was_encrypted) ||
!cryptographer->is_initialized()) {
generated_specifics.CopyFrom(new_specifics);
} else {
if (VLOG_IS_ON(2)) {
scoped_ptr<base::DictionaryValue> value(entry->ToValue(NULL));
std::string info;
base::JSONWriter::WriteWithOptions(value.get(),
base::JSONWriter::OPTIONS_PRETTY_PRINT,
&info);
DVLOG(2) << "Encrypting specifics of type "
<< ModelTypeToString(type)
<< " with content: "
<< info;
}
if (GetModelTypeFromSpecifics(old_specifics) == type &&
was_encrypted) {
generated_specifics.CopyFrom(old_specifics);
} else {
AddDefaultFieldValue(type, &generated_specifics);
}
if (!cryptographer->Encrypt(new_specifics,
generated_specifics.mutable_encrypted())) {
NOTREACHED() << "Could not encrypt data for node of type "
<< ModelTypeToString(type);
return false;
}
}
bool encrypted_without_overwriting_name = (was_encrypted &&
entry->GetNonUniqueName() != kEncryptedString);
if (!encrypted_without_overwriting_name &&
old_specifics.SerializeAsString() ==
generated_specifics.SerializeAsString()) {
DVLOG(2) << "Specifics of type " << ModelTypeToString(type)
<< " already match, dropping change.";
return true;
}
if (generated_specifics.has_encrypted()) {
entry->PutNonUniqueName(kEncryptedString);
if (type == BOOKMARKS) {
sync_pb::BookmarkSpecifics* bookmark_specifics =
generated_specifics.mutable_bookmark();
if (!entry->GetIsDir())
bookmark_specifics->set_url(kEncryptedString);
bookmark_specifics->set_title(kEncryptedString);
}
}
entry->PutSpecifics(generated_specifics);
DVLOG(1) << "Overwriting specifics of type "
<< ModelTypeToString(type)
<< " and marking for syncing.";
syncable::MarkForSyncing(entry);
return true;
}
void UpdateNigoriFromEncryptedTypes(ModelTypeSet encrypted_types,
bool encrypt_everything,
sync_pb::NigoriSpecifics* nigori) {
nigori->set_encrypt_everything(encrypt_everything);
COMPILE_ASSERT(32 == MODEL_TYPE_COUNT, UpdateEncryptedTypes);
nigori->set_encrypt_bookmarks(
encrypted_types.Has(BOOKMARKS));
nigori->set_encrypt_preferences(
encrypted_types.Has(PREFERENCES));
nigori->set_encrypt_autofill_profile(
encrypted_types.Has(AUTOFILL_PROFILE));
nigori->set_encrypt_autofill(encrypted_types.Has(AUTOFILL));
nigori->set_encrypt_themes(encrypted_types.Has(THEMES));
nigori->set_encrypt_typed_urls(
encrypted_types.Has(TYPED_URLS));
nigori->set_encrypt_extension_settings(
encrypted_types.Has(EXTENSION_SETTINGS));
nigori->set_encrypt_extensions(
encrypted_types.Has(EXTENSIONS));
nigori->set_encrypt_search_engines(
encrypted_types.Has(SEARCH_ENGINES));
nigori->set_encrypt_sessions(encrypted_types.Has(SESSIONS));
nigori->set_encrypt_app_settings(
encrypted_types.Has(APP_SETTINGS));
nigori->set_encrypt_apps(encrypted_types.Has(APPS));
nigori->set_encrypt_app_notifications(
encrypted_types.Has(APP_NOTIFICATIONS));
nigori->set_encrypt_dictionary(encrypted_types.Has(DICTIONARY));
nigori->set_encrypt_favicon_images(encrypted_types.Has(FAVICON_IMAGES));
nigori->set_encrypt_favicon_tracking(encrypted_types.Has(FAVICON_TRACKING));
nigori->set_encrypt_articles(encrypted_types.Has(ARTICLES));
nigori->set_encrypt_app_list(encrypted_types.Has(APP_LIST));
}
ModelTypeSet GetEncryptedTypesFromNigori(
const sync_pb::NigoriSpecifics& nigori) {
if (nigori.encrypt_everything())
return ModelTypeSet::All();
ModelTypeSet encrypted_types;
COMPILE_ASSERT(32 == MODEL_TYPE_COUNT, UpdateEncryptedTypes);
if (nigori.encrypt_bookmarks())
encrypted_types.Put(BOOKMARKS);
if (nigori.encrypt_preferences())
encrypted_types.Put(PREFERENCES);
if (nigori.encrypt_autofill_profile())
encrypted_types.Put(AUTOFILL_PROFILE);
if (nigori.encrypt_autofill())
encrypted_types.Put(AUTOFILL);
if (nigori.encrypt_themes())
encrypted_types.Put(THEMES);
if (nigori.encrypt_typed_urls())
encrypted_types.Put(TYPED_URLS);
if (nigori.encrypt_extension_settings())
encrypted_types.Put(EXTENSION_SETTINGS);
if (nigori.encrypt_extensions())
encrypted_types.Put(EXTENSIONS);
if (nigori.encrypt_search_engines())
encrypted_types.Put(SEARCH_ENGINES);
if (nigori.encrypt_sessions())
encrypted_types.Put(SESSIONS);
if (nigori.encrypt_app_settings())
encrypted_types.Put(APP_SETTINGS);
if (nigori.encrypt_apps())
encrypted_types.Put(APPS);
if (nigori.encrypt_app_notifications())
encrypted_types.Put(APP_NOTIFICATIONS);
if (nigori.encrypt_dictionary())
encrypted_types.Put(DICTIONARY);
if (nigori.encrypt_favicon_images())
encrypted_types.Put(FAVICON_IMAGES);
if (nigori.encrypt_favicon_tracking())
encrypted_types.Put(FAVICON_TRACKING);
if (nigori.encrypt_articles())
encrypted_types.Put(ARTICLES);
if (nigori.encrypt_app_list())
encrypted_types.Put(APP_LIST);
return encrypted_types;
}
}
}