OLD | NEW |
1 // Copyright (c) 2011 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 "chrome/browser/password_manager/encryptor_password_mac.h" | 5 #include "chrome/browser/password_manager/encryptor_password_mac.h" |
6 | 6 |
7 #import <Security/Security.h> | 7 #import <Security/Security.h> |
8 | 8 |
| 9 #include "base/mac/mac_logging.h" |
9 #include "chrome/browser/keychain_mac.h" | 10 #include "chrome/browser/keychain_mac.h" |
10 #include "chrome/common/random.h" | 11 #include "chrome/common/random.h" |
11 #include "ui/base/l10n/l10n_util.h" | 12 #include "ui/base/l10n/l10n_util.h" |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
15 // Generates a random password and adds it to the Keychain. The added password | 16 // Generates a random password and adds it to the Keychain. The added password |
16 // is returned from the function. If an error occurs, an empty password is | 17 // is returned from the function. If an error occurs, an empty password is |
17 // returned. | 18 // returned. |
18 std::string AddRandomPasswordToKeychain(const MacKeychain& keychain, | 19 std::string AddRandomPasswordToKeychain(const MacKeychain& keychain, |
19 const std::string& service_name, | 20 const std::string& service_name, |
20 const std::string& account_name) { | 21 const std::string& account_name) { |
21 std::string password = Generate128BitRandomBase64String(); | 22 std::string password = Generate128BitRandomBase64String(); |
22 void* password_data = | 23 void* password_data = |
23 const_cast<void*>(static_cast<const void*>(password.data())); | 24 const_cast<void*>(static_cast<const void*>(password.data())); |
24 | 25 |
25 OSStatus error = keychain.AddGenericPassword(NULL, | 26 OSStatus error = keychain.AddGenericPassword(NULL, |
26 service_name.size(), | 27 service_name.size(), |
27 service_name.data(), | 28 service_name.data(), |
28 account_name.size(), | 29 account_name.size(), |
29 account_name.data(), | 30 account_name.data(), |
30 password.size(), | 31 password.size(), |
31 password_data, | 32 password_data, |
32 NULL); | 33 NULL); |
33 | 34 |
34 if (error != noErr) { | 35 if (error != noErr) { |
35 DLOG(ERROR) << "Keychain add failed with error " << error; | 36 OSSTATUS_DLOG(ERROR, error) << "Keychain add failed"; |
36 return std::string(); | 37 return std::string(); |
37 } | 38 } |
38 | 39 |
39 return password; | 40 return password; |
40 } | 41 } |
41 | 42 |
42 } // namespace | 43 } // namespace |
43 | 44 |
44 std::string EncryptorPassword::GetEncryptorPassword() const { | 45 std::string EncryptorPassword::GetEncryptorPassword() const { |
45 // These two strings ARE indeed user facing. But they are used to access | 46 // These two strings ARE indeed user facing. But they are used to access |
(...skipping 14 matching lines...) Expand all Loading... |
60 NULL); | 61 NULL); |
61 | 62 |
62 if (error == noErr) { | 63 if (error == noErr) { |
63 std::string password = | 64 std::string password = |
64 std::string(static_cast<char*>(password_data), password_length); | 65 std::string(static_cast<char*>(password_data), password_length); |
65 keychain_.ItemFreeContent(NULL, password_data); | 66 keychain_.ItemFreeContent(NULL, password_data); |
66 return password; | 67 return password; |
67 } else if (error == errSecItemNotFound) { | 68 } else if (error == errSecItemNotFound) { |
68 return AddRandomPasswordToKeychain(keychain_, service_name, account_name); | 69 return AddRandomPasswordToKeychain(keychain_, service_name, account_name); |
69 } else { | 70 } else { |
70 DLOG(ERROR) << "Keychain lookup failed with error " << error; | 71 OSSTATUS_DLOG(ERROR, error) << "Keychain lookup failed"; |
71 return std::string(); | 72 return std::string(); |
72 } | 73 } |
73 } | 74 } |
74 | |
75 | |
OLD | NEW |