| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/sync/util/cryptographer.h" | 5 #include "chrome/browser/sync/util/cryptographer.h" |
| 6 | 6 |
| 7 namespace browser_sync { | 7 namespace browser_sync { |
| 8 | 8 |
| 9 const char kNigoriTag[] = "google_chrome_nigori"; | 9 const char kNigoriTag[] = "google_chrome_nigori"; |
| 10 | 10 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 } | 76 } |
| 77 | 77 |
| 78 // Encrypt the bag with the default Nigori. | 78 // Encrypt the bag with the default Nigori. |
| 79 return Encrypt(bag, encrypted); | 79 return Encrypt(bag, encrypted); |
| 80 } | 80 } |
| 81 | 81 |
| 82 bool Cryptographer::AddKey(const KeyParams& params) { | 82 bool Cryptographer::AddKey(const KeyParams& params) { |
| 83 DCHECK(NULL == pending_keys_.get()); | 83 DCHECK(NULL == pending_keys_.get()); |
| 84 | 84 |
| 85 // Create the new Nigori and make it the default encryptor. | 85 // Create the new Nigori and make it the default encryptor. |
| 86 scoped_ptr<Nigori> nigori(new Nigori(params.hostname)); | 86 scoped_ptr<Nigori> nigori(new Nigori); |
| 87 if (!nigori->Init(params.username, params.password)) { | 87 if (!nigori->InitByDerivation(params.hostname, |
| 88 params.username, |
| 89 params.password)) { |
| 88 NOTREACHED(); // Invalid username or password. | 90 NOTREACHED(); // Invalid username or password. |
| 89 return false; | 91 return false; |
| 90 } | 92 } |
| 91 std::string name; | 93 std::string name; |
| 92 if (!nigori->Permute(Nigori::Password, kNigoriKeyName, &name)) { | 94 if (!nigori->Permute(Nigori::Password, kNigoriKeyName, &name)) { |
| 93 NOTREACHED(); | 95 NOTREACHED(); |
| 94 return false; | 96 return false; |
| 95 } | 97 } |
| 96 nigoris_[name] = make_linked_ptr(nigori.release()); | 98 nigoris_[name] = make_linked_ptr(nigori.release()); |
| 97 default_nigori_ = &*nigoris_.find(name); | 99 default_nigori_ = &*nigoris_.find(name); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 108 InstallKeys(encrypted.key_name(), bag); | 110 InstallKeys(encrypted.key_name(), bag); |
| 109 return true; | 111 return true; |
| 110 } | 112 } |
| 111 | 113 |
| 112 void Cryptographer::SetPendingKeys(const sync_pb::EncryptedData& encrypted) { | 114 void Cryptographer::SetPendingKeys(const sync_pb::EncryptedData& encrypted) { |
| 113 DCHECK(!CanDecrypt(encrypted)); | 115 DCHECK(!CanDecrypt(encrypted)); |
| 114 pending_keys_.reset(new sync_pb::EncryptedData(encrypted)); | 116 pending_keys_.reset(new sync_pb::EncryptedData(encrypted)); |
| 115 } | 117 } |
| 116 | 118 |
| 117 bool Cryptographer::DecryptPendingKeys(const KeyParams& params) { | 119 bool Cryptographer::DecryptPendingKeys(const KeyParams& params) { |
| 118 Nigori nigori(params.hostname); | 120 Nigori nigori; |
| 119 if (!nigori.Init(params.username, params.password)) { | 121 if (!nigori.InitByDerivation(params.hostname, |
| 122 params.username, |
| 123 params.password)) { |
| 120 NOTREACHED(); | 124 NOTREACHED(); |
| 121 return false; | 125 return false; |
| 122 } | 126 } |
| 123 | 127 |
| 124 std::string plaintext; | 128 std::string plaintext; |
| 125 if (!nigori.Decrypt(pending_keys_->blob(), &plaintext)) | 129 if (!nigori.Decrypt(pending_keys_->blob(), &plaintext)) |
| 126 return false; | 130 return false; |
| 127 | 131 |
| 128 sync_pb::NigoriKeyBag bag; | 132 sync_pb::NigoriKeyBag bag; |
| 129 if (!bag.ParseFromString(plaintext)) { | 133 if (!bag.ParseFromString(plaintext)) { |
| 130 NOTREACHED(); | 134 NOTREACHED(); |
| 131 return false; | 135 return false; |
| 132 } | 136 } |
| 133 InstallKeys(pending_keys_->key_name(), bag); | 137 InstallKeys(pending_keys_->key_name(), bag); |
| 134 pending_keys_.reset(); | 138 pending_keys_.reset(); |
| 135 return true; | 139 return true; |
| 136 } | 140 } |
| 137 | 141 |
| 138 void Cryptographer::InstallKeys(const std::string& default_key_name, | 142 void Cryptographer::InstallKeys(const std::string& default_key_name, |
| 139 const sync_pb::NigoriKeyBag& bag) { | 143 const sync_pb::NigoriKeyBag& bag) { |
| 140 int key_size = bag.key_size(); | 144 int key_size = bag.key_size(); |
| 141 for (int i = 0; i < key_size; ++i) { | 145 for (int i = 0; i < key_size; ++i) { |
| 142 const sync_pb::NigoriKey key = bag.key(i); | 146 const sync_pb::NigoriKey key = bag.key(i); |
| 143 // Only use this key if we don't already know about it. | 147 // Only use this key if we don't already know about it. |
| 144 if (nigoris_.end() == nigoris_.find(key.name())) { | 148 if (nigoris_.end() == nigoris_.find(key.name())) { |
| 145 scoped_ptr<Nigori> new_nigori(new Nigori(key.hostname())); | 149 scoped_ptr<Nigori> new_nigori(new Nigori); |
| 146 if (!new_nigori->Init(key.username(), key.password())) { | 150 if (!new_nigori->InitByDerivation(key.hostname(), |
| 151 key.username(), |
| 152 key.password())) { |
| 147 NOTREACHED(); | 153 NOTREACHED(); |
| 148 continue; | 154 continue; |
| 149 } | 155 } |
| 150 nigoris_[key.name()] = make_linked_ptr(new_nigori.release()); | 156 nigoris_[key.name()] = make_linked_ptr(new_nigori.release()); |
| 151 } | 157 } |
| 152 } | 158 } |
| 153 DCHECK(nigoris_.end() != nigoris_.find(default_key_name)); | 159 DCHECK(nigoris_.end() != nigoris_.find(default_key_name)); |
| 154 default_nigori_ = &*nigoris_.find(default_key_name); | 160 default_nigori_ = &*nigoris_.find(default_key_name); |
| 155 } | 161 } |
| 156 | 162 |
| 157 } // namespace browser_sync | 163 } // namespace browser_sync |
| OLD | NEW |