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 "base/base64.h" | 5 #include "base/base64.h" |
6 #include "chrome/browser/sync/util/cryptographer.h" | 6 #include "chrome/browser/sync/util/cryptographer.h" |
7 #include "chrome/browser/password_manager/encryptor.h" | 7 #include "chrome/browser/password_manager/encryptor.h" |
8 | 8 |
9 namespace browser_sync { | 9 namespace browser_sync { |
10 | 10 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 encrypted->mutable_blob())) { | 52 encrypted->mutable_blob())) { |
53 NOTREACHED(); // Encrypt should not fail. | 53 NOTREACHED(); // Encrypt should not fail. |
54 return false; | 54 return false; |
55 } | 55 } |
56 return true; | 56 return true; |
57 } | 57 } |
58 | 58 |
59 bool Cryptographer::Decrypt(const sync_pb::EncryptedData& encrypted, | 59 bool Cryptographer::Decrypt(const sync_pb::EncryptedData& encrypted, |
60 ::google::protobuf::MessageLite* message) const { | 60 ::google::protobuf::MessageLite* message) const { |
61 DCHECK(message); | 61 DCHECK(message); |
62 std::string plaintext = DecryptToString(encrypted); | |
63 return message->ParseFromString(plaintext); | |
64 } | |
65 | 62 |
66 std::string Cryptographer::DecryptToString( | |
67 const sync_pb::EncryptedData& encrypted) const { | |
68 NigoriMap::const_iterator it = nigoris_.find(encrypted.key_name()); | 63 NigoriMap::const_iterator it = nigoris_.find(encrypted.key_name()); |
69 if (nigoris_.end() == it) { | 64 if (nigoris_.end() == it) { |
70 NOTREACHED() << "Cannot decrypt message"; | 65 NOTREACHED() << "Cannot decrypt message"; |
71 return std::string(""); // Caller should have called CanDecrypt(encrypt). | 66 return false; // Caller should have called CanDecrypt(encrypt). |
72 } | 67 } |
73 | 68 |
74 std::string plaintext; | 69 std::string plaintext; |
75 if (!it->second->Decrypt(encrypted.blob(), &plaintext)) { | 70 if (!it->second->Decrypt(encrypted.blob(), &plaintext)) { |
76 return std::string(""); | 71 return false; |
77 } | 72 } |
78 | 73 |
79 return plaintext; | 74 return message->ParseFromString(plaintext); |
80 } | 75 } |
81 | 76 |
82 bool Cryptographer::GetKeys(sync_pb::EncryptedData* encrypted) const { | 77 bool Cryptographer::GetKeys(sync_pb::EncryptedData* encrypted) const { |
83 DCHECK(encrypted); | 78 DCHECK(encrypted); |
84 DCHECK(!nigoris_.empty()); | 79 DCHECK(!nigoris_.empty()); |
85 | 80 |
86 // Create a bag of all the Nigori parameters we know about. | 81 // Create a bag of all the Nigori parameters we know about. |
87 sync_pb::NigoriKeyBag bag; | 82 sync_pb::NigoriKeyBag bag; |
88 for (NigoriMap::const_iterator it = nigoris_.begin(); it != nigoris_.end(); | 83 for (NigoriMap::const_iterator it = nigoris_.begin(); it != nigoris_.end(); |
89 ++it) { | 84 ++it) { |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 return false; | 197 return false; |
203 } | 198 } |
204 return true; | 199 return true; |
205 } | 200 } |
206 | 201 |
207 Nigori* Cryptographer::UnpackBootstrapToken(const std::string& token) const { | 202 Nigori* Cryptographer::UnpackBootstrapToken(const std::string& token) const { |
208 if (token.empty()) | 203 if (token.empty()) |
209 return NULL; | 204 return NULL; |
210 | 205 |
211 std::string encrypted_data; | 206 std::string encrypted_data; |
212 if (!base::Base64Decode(token, &encrypted_data)) { | 207 if (!base::Base64Decode(token, &encrypted_data)){ |
213 DLOG(WARNING) << "Could not decode token."; | 208 DLOG(WARNING) << "Could not decode token."; |
214 return NULL; | 209 return NULL; |
215 } | 210 } |
216 | 211 |
217 std::string unencrypted_token; | 212 std::string unencrypted_token; |
218 if (!Encryptor::DecryptString(encrypted_data, &unencrypted_token)) { | 213 if (!Encryptor::DecryptString(encrypted_data, &unencrypted_token)) { |
219 DLOG(WARNING) << "Decryption of bootstrap token failed."; | 214 DLOG(WARNING) << "Decryption of bootstrap token failed."; |
220 return NULL; | 215 return NULL; |
221 } | 216 } |
222 | 217 |
(...skipping 28 matching lines...) Expand all Loading... |
251 continue; | 246 continue; |
252 } | 247 } |
253 nigoris_[key.name()] = make_linked_ptr(new_nigori.release()); | 248 nigoris_[key.name()] = make_linked_ptr(new_nigori.release()); |
254 } | 249 } |
255 } | 250 } |
256 DCHECK(nigoris_.end() != nigoris_.find(default_key_name)); | 251 DCHECK(nigoris_.end() != nigoris_.find(default_key_name)); |
257 default_nigori_ = &*nigoris_.find(default_key_name); | 252 default_nigori_ = &*nigoris_.find(default_key_name); |
258 } | 253 } |
259 | 254 |
260 } // namespace browser_sync | 255 } // namespace browser_sync |
OLD | NEW |