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 <string> | 7 #include <string> |
8 | 8 |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 EXPECT_TRUE(cryptographer_clone.Encrypt(original, &encrypted_c)); | 257 EXPECT_TRUE(cryptographer_clone.Encrypt(original, &encrypted_c)); |
258 | 258 |
259 sync_pb::PasswordSpecificsData decrypted_c; | 259 sync_pb::PasswordSpecificsData decrypted_c; |
260 EXPECT_TRUE(cryptographer_.Decrypt(encrypted_c, &decrypted_c)); | 260 EXPECT_TRUE(cryptographer_.Decrypt(encrypted_c, &decrypted_c)); |
261 EXPECT_EQ(original.SerializeAsString(), decrypted_c.SerializeAsString()); | 261 EXPECT_EQ(original.SerializeAsString(), decrypted_c.SerializeAsString()); |
262 | 262 |
263 // The cloned cryptographer should be using the latest key. | 263 // The cloned cryptographer should be using the latest key. |
264 EXPECT_EQ(encrypted_c.key_name(), encrypted_k2.key_name()); | 264 EXPECT_EQ(encrypted_c.key_name(), encrypted_k2.key_name()); |
265 } | 265 } |
266 | 266 |
| 267 // Test verifies that GetBootstrapToken/Bootstrap only transfers default |
| 268 // key. Additional call to GetKeys/InstallKeys is needed to transfer keybag |
| 269 // to decrypt messages encrypted with old keys. |
| 270 TEST_F(CryptographerTest, GetKeysThenInstall) { |
| 271 sync_pb::PasswordSpecificsData original; |
| 272 original.set_origin("http://example.com"); |
| 273 original.set_username_value("luser"); |
| 274 original.set_password_value("p4ssw0rd"); |
| 275 |
| 276 // First, encrypt the same value using two different keys. |
| 277 KeyParams params1 = {"localhost", "dummy", "dummy"}; |
| 278 EXPECT_TRUE(cryptographer_.AddKey(params1)); |
| 279 EXPECT_TRUE(cryptographer_.is_ready()); |
| 280 |
| 281 sync_pb::EncryptedData encrypted_k1; |
| 282 EXPECT_TRUE(cryptographer_.Encrypt(original, &encrypted_k1)); |
| 283 |
| 284 KeyParams params2 = {"localhost", "dummy2", "dummy2"}; |
| 285 EXPECT_TRUE(cryptographer_.AddKey(params2)); |
| 286 EXPECT_TRUE(cryptographer_.is_ready()); |
| 287 |
| 288 sync_pb::EncryptedData encrypted_k2; |
| 289 EXPECT_TRUE(cryptographer_.Encrypt(original, &encrypted_k2)); |
| 290 |
| 291 // Then construct second cryptographer and bootstrap it from the first one. |
| 292 Cryptographer another_cryptographer(cryptographer_.encryptor()); |
| 293 std::string bootstrap_token; |
| 294 EXPECT_TRUE(cryptographer_.GetBootstrapToken(&bootstrap_token)); |
| 295 another_cryptographer.Bootstrap(bootstrap_token); |
| 296 |
| 297 // Before key installation, the second cryptographer should only be able |
| 298 // to decrypt using the last key. |
| 299 EXPECT_FALSE(another_cryptographer.CanDecrypt(encrypted_k1)); |
| 300 EXPECT_TRUE(another_cryptographer.CanDecrypt(encrypted_k2)); |
| 301 |
| 302 sync_pb::EncryptedData keys; |
| 303 EXPECT_TRUE(cryptographer_.GetKeys(&keys)); |
| 304 ASSERT_TRUE(another_cryptographer.CanDecrypt(keys)); |
| 305 another_cryptographer.InstallKeys(keys); |
| 306 |
| 307 // Verify that bootstrapped cryptographer decrypts succesfully using |
| 308 // all the keys after key installation. |
| 309 EXPECT_TRUE(another_cryptographer.CanDecrypt(encrypted_k1)); |
| 310 EXPECT_TRUE(another_cryptographer.CanDecrypt(encrypted_k2)); |
| 311 } |
| 312 |
267 } // namespace syncer | 313 } // namespace syncer |
OLD | NEW |