Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: sync/util/cryptographer.cc

Issue 10540149: [Sync] Persist keystore key across restarts (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 if (is_initialized()) { 45 if (is_initialized()) {
46 NOTREACHED(); 46 NOTREACHED();
47 return; 47 return;
48 } 48 }
49 49
50 scoped_ptr<Nigori> nigori(UnpackBootstrapToken(restored_bootstrap_token)); 50 scoped_ptr<Nigori> nigori(UnpackBootstrapToken(restored_bootstrap_token));
51 if (nigori.get()) 51 if (nigori.get())
52 AddKeyImpl(nigori.release()); 52 AddKeyImpl(nigori.release());
53 } 53 }
54 54
55 void Cryptographer::BootstrapKeystoreKey(
56 const std::string& restored_bootstrap_token) {
57 if (keystore_nigori_) {
58 NOTREACHED();
59 return;
60 }
61
62 scoped_ptr<Nigori> nigori(UnpackBootstrapToken(restored_bootstrap_token));
63 if (!nigori.get())
64 return;
65 // AddKeyImpl updates the default nigori, so we save the current default and
66 // make sure the keystore_nigori_ gets updated instead.
67 NigoriMap::value_type* old_default = default_nigori_;
rlarocque 2012/06/13 23:35:04 Why use linked_ptr<>*?
Nicolas Zea 2012/06/15 00:42:07 Because they're also contained within the nigori k
68 if (AddKeyImpl(nigori.release())) {
69 keystore_nigori_ = default_nigori_;
70 default_nigori_ = old_default;
71 }
72 }
73
55 bool Cryptographer::CanDecrypt(const sync_pb::EncryptedData& data) const { 74 bool Cryptographer::CanDecrypt(const sync_pb::EncryptedData& data) const {
56 return nigoris_.end() != nigoris_.find(data.key_name()); 75 return nigoris_.end() != nigoris_.find(data.key_name());
57 } 76 }
58 77
59 bool Cryptographer::CanDecryptUsingDefaultKey( 78 bool Cryptographer::CanDecryptUsingDefaultKey(
60 const sync_pb::EncryptedData& data) const { 79 const sync_pb::EncryptedData& data) const {
61 return default_nigori_ && (data.key_name() == default_nigori_->first); 80 return default_nigori_ && (data.key_name() == default_nigori_->first);
62 } 81 }
63 82
64 bool Cryptographer::Encrypt( 83 bool Cryptographer::Encrypt(
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 } 234 }
216 235
217 bool Cryptographer::GetBootstrapToken(std::string* token) const { 236 bool Cryptographer::GetBootstrapToken(std::string* token) const {
218 DCHECK(token); 237 DCHECK(token);
219 if (!is_initialized()) 238 if (!is_initialized())
220 return false; 239 return false;
221 240
222 return PackBootstrapToken(default_nigori_->second.get(), token); 241 return PackBootstrapToken(default_nigori_->second.get(), token);
223 } 242 }
224 243
244 bool Cryptographer::GetKeystoreKeyBootstrapToken(
245 std::string* token) const {
246 DCHECK(token);
247 if (!HasKeystoreKey())
248 return false;
249
250 return PackBootstrapToken(keystore_nigori_->second.get(), token);
251 }
252
225 bool Cryptographer::PackBootstrapToken(const Nigori* nigori, 253 bool Cryptographer::PackBootstrapToken(const Nigori* nigori,
226 std::string* pack_into) const { 254 std::string* pack_into) const {
227 DCHECK(pack_into); 255 DCHECK(pack_into);
228 DCHECK(nigori); 256 DCHECK(nigori);
229 257
230 sync_pb::NigoriKey key; 258 sync_pb::NigoriKey key;
231 if (!nigori->ExportKeys(key.mutable_user_key(), 259 if (!nigori->ExportKeys(key.mutable_user_key(),
232 key.mutable_encryption_key(), 260 key.mutable_encryption_key(),
233 key.mutable_mac_key())) { 261 key.mutable_mac_key())) {
234 NOTREACHED(); 262 NOTREACHED();
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 // make sure the keystore_nigori_ gets updated instead. 346 // make sure the keystore_nigori_ gets updated instead.
319 NigoriMap::value_type* old_default = default_nigori_; 347 NigoriMap::value_type* old_default = default_nigori_;
320 if (AddKey(params)) { 348 if (AddKey(params)) {
321 keystore_nigori_ = default_nigori_; 349 keystore_nigori_ = default_nigori_;
322 default_nigori_ = old_default; 350 default_nigori_ = old_default;
323 return true; 351 return true;
324 } 352 }
325 return false; 353 return false;
326 } 354 }
327 355
328 bool Cryptographer::HasKeystoreKey() { 356 bool Cryptographer::HasKeystoreKey() const {
329 return keystore_nigori_ != NULL; 357 return keystore_nigori_ != NULL;
330 } 358 }
331 359
332 // Static 360 // Static
333 syncable::ModelTypeSet Cryptographer::SensitiveTypes() { 361 syncable::ModelTypeSet Cryptographer::SensitiveTypes() {
334 // Both of these have their own encryption schemes, but we include them 362 // Both of these have their own encryption schemes, but we include them
335 // anyways. 363 // anyways.
336 syncable::ModelTypeSet types; 364 syncable::ModelTypeSet types;
337 types.Put(syncable::PASSWORDS); 365 types.Put(syncable::PASSWORDS);
338 types.Put(syncable::NIGORI); 366 types.Put(syncable::NIGORI);
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
465 key.mac_key())) { 493 key.mac_key())) {
466 NOTREACHED(); 494 NOTREACHED();
467 continue; 495 continue;
468 } 496 }
469 nigoris_[key.name()] = make_linked_ptr(new_nigori.release()); 497 nigoris_[key.name()] = make_linked_ptr(new_nigori.release());
470 } 498 }
471 } 499 }
472 } 500 }
473 501
474 } // namespace browser_sync 502 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698