OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "sync/util/cryptographer.h" | 5 #include "sync/util/cryptographer.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "sync/protocol/nigori_specifics.pb.h" | 11 #include "sync/protocol/nigori_specifics.pb.h" |
12 #include "sync/syncable/nigori_handler.h" | |
13 #include "sync/util/encryptor.h" | 12 #include "sync/util/encryptor.h" |
14 | 13 |
15 namespace syncer { | 14 namespace syncer { |
16 | 15 |
17 const char kNigoriTag[] = "google_chrome_nigori"; | 16 const char kNigoriTag[] = "google_chrome_nigori"; |
18 | 17 |
19 // We name a particular Nigori instance (ie. a triplet consisting of a hostname, | 18 // We name a particular Nigori instance (ie. a triplet consisting of a hostname, |
20 // a username, and a password) by calling Permute on this string. Since the | 19 // a username, and a password) by calling Permute on this string. Since the |
21 // output of Permute is always the same for a given triplet, clients will always | 20 // output of Permute is always the same for a given triplet, clients will always |
22 // assign the same name to a particular triplet. | 21 // assign the same name to a particular triplet. |
23 const char kNigoriKeyName[] = "nigori-key"; | 22 const char kNigoriKeyName[] = "nigori-key"; |
24 | 23 |
25 Cryptographer::Cryptographer(Encryptor* encryptor) | 24 Cryptographer::Cryptographer(Encryptor* encryptor) |
26 : encryptor_(encryptor), | 25 : encryptor_(encryptor), |
27 default_nigori_(NULL), | 26 default_nigori_(NULL), |
28 keystore_nigori_(NULL), | 27 keystore_nigori_(NULL) { |
29 nigori_node_handler_(NULL) { | |
30 DCHECK(encryptor); | 28 DCHECK(encryptor); |
31 } | 29 } |
32 | 30 |
33 Cryptographer::~Cryptographer() {} | 31 Cryptographer::~Cryptographer() {} |
34 | 32 |
35 void Cryptographer::SetNigoriHandler(syncable::NigoriHandler* delegate) { | |
36 nigori_node_handler_ = delegate; | |
37 } | |
38 | |
39 void Cryptographer::ApplyNigoriUpdate( | |
40 const sync_pb::NigoriSpecifics& nigori, | |
41 syncable::BaseTransaction* const trans) { | |
42 nigori_node_handler_->ApplyNigoriUpdate(nigori, trans); | |
43 } | |
44 | |
45 ModelTypeSet Cryptographer::GetEncryptedTypes() const { | |
46 return nigori_node_handler_->GetEncryptedTypes(); | |
47 } | |
48 | |
49 void Cryptographer::UpdateNigoriFromEncryptedTypes( | |
50 sync_pb::NigoriSpecifics* nigori, | |
51 syncable::BaseTransaction* const trans) const { | |
52 nigori_node_handler_->UpdateNigoriFromEncryptedTypes(nigori, trans); | |
53 } | |
54 | |
55 | 33 |
56 void Cryptographer::Bootstrap(const std::string& restored_bootstrap_token) { | 34 void Cryptographer::Bootstrap(const std::string& restored_bootstrap_token) { |
57 if (is_initialized()) { | 35 if (is_initialized()) { |
58 NOTREACHED(); | 36 NOTREACHED(); |
59 return; | 37 return; |
60 } | 38 } |
61 | 39 |
62 scoped_ptr<Nigori> nigori(UnpackBootstrapToken(restored_bootstrap_token)); | 40 scoped_ptr<Nigori> nigori(UnpackBootstrapToken(restored_bootstrap_token)); |
63 if (nigori.get()) | 41 if (nigori.get()) |
64 AddKeyImpl(nigori.release(), false); | 42 AddKeyImpl(nigori.release(), false); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 InstallKeyBag(bag); | 184 InstallKeyBag(bag); |
207 } | 185 } |
208 | 186 |
209 void Cryptographer::SetDefaultKey(const std::string& key_name) { | 187 void Cryptographer::SetDefaultKey(const std::string& key_name) { |
210 DCHECK(nigoris_.end() != nigoris_.find(key_name)); | 188 DCHECK(nigoris_.end() != nigoris_.find(key_name)); |
211 default_nigori_ = &*nigoris_.find(key_name); | 189 default_nigori_ = &*nigoris_.find(key_name); |
212 } | 190 } |
213 | 191 |
214 void Cryptographer::SetPendingKeys(const sync_pb::EncryptedData& encrypted) { | 192 void Cryptographer::SetPendingKeys(const sync_pb::EncryptedData& encrypted) { |
215 DCHECK(!CanDecrypt(encrypted)); | 193 DCHECK(!CanDecrypt(encrypted)); |
| 194 DCHECK(!encrypted.blob().empty()); |
216 pending_keys_.reset(new sync_pb::EncryptedData(encrypted)); | 195 pending_keys_.reset(new sync_pb::EncryptedData(encrypted)); |
217 } | 196 } |
218 | 197 |
219 const sync_pb::EncryptedData& Cryptographer::GetPendingKeys() const { | 198 const sync_pb::EncryptedData& Cryptographer::GetPendingKeys() const { |
220 DCHECK(has_pending_keys()); | 199 DCHECK(has_pending_keys()); |
221 return *(pending_keys_.get()); | 200 return *(pending_keys_.get()); |
222 } | 201 } |
223 | 202 |
224 bool Cryptographer::DecryptPendingKeys(const KeyParams& params) { | 203 bool Cryptographer::DecryptPendingKeys(const KeyParams& params) { |
225 Nigori nigori; | 204 Nigori nigori; |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 key.mac_key())) { | 340 key.mac_key())) { |
362 NOTREACHED(); | 341 NOTREACHED(); |
363 continue; | 342 continue; |
364 } | 343 } |
365 nigoris_[key.name()] = make_linked_ptr(new_nigori.release()); | 344 nigoris_[key.name()] = make_linked_ptr(new_nigori.release()); |
366 } | 345 } |
367 } | 346 } |
368 } | 347 } |
369 | 348 |
370 } // namespace syncer | 349 } // namespace syncer |
OLD | NEW |