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" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 } | 42 } |
43 | 43 |
44 void Cryptographer::Bootstrap(const std::string& restored_bootstrap_token) { | 44 void Cryptographer::Bootstrap(const std::string& restored_bootstrap_token) { |
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(), false); |
| 53 } |
| 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 AddKeyImpl(nigori.release(), true); |
53 } | 65 } |
54 | 66 |
55 bool Cryptographer::CanDecrypt(const sync_pb::EncryptedData& data) const { | 67 bool Cryptographer::CanDecrypt(const sync_pb::EncryptedData& data) const { |
56 return nigoris_.end() != nigoris_.find(data.key_name()); | 68 return nigoris_.end() != nigoris_.find(data.key_name()); |
57 } | 69 } |
58 | 70 |
59 bool Cryptographer::CanDecryptUsingDefaultKey( | 71 bool Cryptographer::CanDecryptUsingDefaultKey( |
60 const sync_pb::EncryptedData& data) const { | 72 const sync_pb::EncryptedData& data) const { |
61 return default_nigori_ && (data.key_name() == default_nigori_->first); | 73 return default_nigori_ && (data.key_name() == default_nigori_->first); |
62 } | 74 } |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 | 150 |
139 bool Cryptographer::AddKey(const KeyParams& params) { | 151 bool Cryptographer::AddKey(const KeyParams& params) { |
140 // Create the new Nigori and make it the default encryptor. | 152 // Create the new Nigori and make it the default encryptor. |
141 scoped_ptr<Nigori> nigori(new Nigori); | 153 scoped_ptr<Nigori> nigori(new Nigori); |
142 if (!nigori->InitByDerivation(params.hostname, | 154 if (!nigori->InitByDerivation(params.hostname, |
143 params.username, | 155 params.username, |
144 params.password)) { | 156 params.password)) { |
145 NOTREACHED(); // Invalid username or password. | 157 NOTREACHED(); // Invalid username or password. |
146 return false; | 158 return false; |
147 } | 159 } |
148 return AddKeyImpl(nigori.release()); | 160 return AddKeyImpl(nigori.release(), false); |
149 } | 161 } |
150 | 162 |
151 bool Cryptographer::AddKeyFromBootstrapToken( | 163 bool Cryptographer::AddKeyFromBootstrapToken( |
152 const std::string restored_bootstrap_token) { | 164 const std::string restored_bootstrap_token) { |
153 // Create the new Nigori and make it the default encryptor. | 165 // Create the new Nigori and make it the default encryptor. |
154 scoped_ptr<Nigori> nigori(UnpackBootstrapToken(restored_bootstrap_token)); | 166 scoped_ptr<Nigori> nigori(UnpackBootstrapToken(restored_bootstrap_token)); |
155 if (!nigori.get()) | 167 if (!nigori.get()) |
156 return false; | 168 return false; |
157 return AddKeyImpl(nigori.release()); | 169 return AddKeyImpl(nigori.release(), false); |
158 } | 170 } |
159 | 171 |
160 bool Cryptographer::AddKeyImpl(Nigori* initialized_nigori) { | 172 bool Cryptographer::AddKeyImpl(Nigori* initialized_nigori, |
| 173 bool is_keystore_key) { |
161 scoped_ptr<Nigori> nigori(initialized_nigori); | 174 scoped_ptr<Nigori> nigori(initialized_nigori); |
162 std::string name; | 175 std::string name; |
163 if (!nigori->Permute(Nigori::Password, kNigoriKeyName, &name)) { | 176 if (!nigori->Permute(Nigori::Password, kNigoriKeyName, &name)) { |
164 NOTREACHED(); | 177 NOTREACHED(); |
165 return false; | 178 return false; |
166 } | 179 } |
167 nigoris_[name] = make_linked_ptr(nigori.release()); | 180 nigoris_[name] = make_linked_ptr(nigori.release()); |
168 default_nigori_ = &*nigoris_.find(name); | 181 if (is_keystore_key) |
| 182 keystore_nigori_ = &*nigoris_.find(name); |
| 183 else |
| 184 default_nigori_ = &*nigoris_.find(name); |
169 return true; | 185 return true; |
170 } | 186 } |
171 | 187 |
172 void Cryptographer::InstallKeys(const sync_pb::EncryptedData& encrypted) { | 188 void Cryptographer::InstallKeys(const sync_pb::EncryptedData& encrypted) { |
173 DCHECK(CanDecrypt(encrypted)); | 189 DCHECK(CanDecrypt(encrypted)); |
174 | 190 |
175 sync_pb::NigoriKeyBag bag; | 191 sync_pb::NigoriKeyBag bag; |
176 if (!Decrypt(encrypted, &bag)) | 192 if (!Decrypt(encrypted, &bag)) |
177 return; | 193 return; |
178 InstallKeyBag(bag); | 194 InstallKeyBag(bag); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 } | 231 } |
216 | 232 |
217 bool Cryptographer::GetBootstrapToken(std::string* token) const { | 233 bool Cryptographer::GetBootstrapToken(std::string* token) const { |
218 DCHECK(token); | 234 DCHECK(token); |
219 if (!is_initialized()) | 235 if (!is_initialized()) |
220 return false; | 236 return false; |
221 | 237 |
222 return PackBootstrapToken(default_nigori_->second.get(), token); | 238 return PackBootstrapToken(default_nigori_->second.get(), token); |
223 } | 239 } |
224 | 240 |
| 241 bool Cryptographer::GetKeystoreKeyBootstrapToken( |
| 242 std::string* token) const { |
| 243 DCHECK(token); |
| 244 if (!HasKeystoreKey()) |
| 245 return false; |
| 246 |
| 247 return PackBootstrapToken(keystore_nigori_->second.get(), token); |
| 248 } |
| 249 |
225 bool Cryptographer::PackBootstrapToken(const Nigori* nigori, | 250 bool Cryptographer::PackBootstrapToken(const Nigori* nigori, |
226 std::string* pack_into) const { | 251 std::string* pack_into) const { |
227 DCHECK(pack_into); | 252 DCHECK(pack_into); |
228 DCHECK(nigori); | 253 DCHECK(nigori); |
229 | 254 |
230 sync_pb::NigoriKey key; | 255 sync_pb::NigoriKey key; |
231 if (!nigori->ExportKeys(key.mutable_user_key(), | 256 if (!nigori->ExportKeys(key.mutable_user_key(), |
232 key.mutable_encryption_key(), | 257 key.mutable_encryption_key(), |
233 key.mutable_mac_key())) { | 258 key.mutable_mac_key())) { |
234 NOTREACHED(); | 259 NOTREACHED(); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 } | 332 } |
308 } | 333 } |
309 return Cryptographer::SUCCESS; | 334 return Cryptographer::SUCCESS; |
310 } | 335 } |
311 | 336 |
312 bool Cryptographer::SetKeystoreKey(const std::string& keystore_key) { | 337 bool Cryptographer::SetKeystoreKey(const std::string& keystore_key) { |
313 if (keystore_key.empty()) | 338 if (keystore_key.empty()) |
314 return false; | 339 return false; |
315 KeyParams params = {"localhost", "dummy", keystore_key}; | 340 KeyParams params = {"localhost", "dummy", keystore_key}; |
316 | 341 |
317 // AddKey updates the default nigori, so we save the current default and | 342 // Create the new Nigori and make it the default keystore encryptor. |
318 // make sure the keystore_nigori_ gets updated instead. | 343 scoped_ptr<Nigori> nigori(new Nigori); |
319 NigoriMap::value_type* old_default = default_nigori_; | 344 if (!nigori->InitByDerivation(params.hostname, |
320 if (AddKey(params)) { | 345 params.username, |
321 keystore_nigori_ = default_nigori_; | 346 params.password)) { |
322 default_nigori_ = old_default; | 347 NOTREACHED(); // Invalid username or password. |
323 return true; | 348 return false; |
324 } | 349 } |
325 return false; | 350 |
| 351 return AddKeyImpl(nigori.release(), true); |
326 } | 352 } |
327 | 353 |
328 bool Cryptographer::HasKeystoreKey() { | 354 bool Cryptographer::HasKeystoreKey() const { |
329 return keystore_nigori_ != NULL; | 355 return keystore_nigori_ != NULL; |
330 } | 356 } |
331 | 357 |
332 // Static | 358 // Static |
333 ModelTypeSet Cryptographer::SensitiveTypes() { | 359 ModelTypeSet Cryptographer::SensitiveTypes() { |
334 // Both of these have their own encryption schemes, but we include them | 360 // Both of these have their own encryption schemes, but we include them |
335 // anyways. | 361 // anyways. |
336 ModelTypeSet types; | 362 ModelTypeSet types; |
337 types.Put(PASSWORDS); | 363 types.Put(PASSWORDS); |
338 types.Put(NIGORI); | 364 types.Put(NIGORI); |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 key.mac_key())) { | 489 key.mac_key())) { |
464 NOTREACHED(); | 490 NOTREACHED(); |
465 continue; | 491 continue; |
466 } | 492 } |
467 nigoris_[key.name()] = make_linked_ptr(new_nigori.release()); | 493 nigoris_[key.name()] = make_linked_ptr(new_nigori.release()); |
468 } | 494 } |
469 } | 495 } |
470 } | 496 } |
471 | 497 |
472 } // namespace syncer | 498 } // namespace syncer |
OLD | NEW |