Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(514)

Side by Side Diff: chrome/browser/password_manager/encryptor_password_mac.mm

Issue 12902030: Make Encryptor a component. Used by //chrome and (soon) //components/webdata. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove duplicate include. Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/password_manager/encryptor_password_mac.h"
6
7 #import <Security/Security.h>
8
9 #include "base/base64.h"
10 #include "base/mac/mac_logging.h"
11 #include "base/rand_util.h"
12 #include "crypto/apple_keychain.h"
13 #include "ui/base/l10n/l10n_util.h"
14
15 using crypto::AppleKeychain;
16
17 namespace {
18
19 // Generates a random password and adds it to the Keychain. The added password
20 // is returned from the function. If an error occurs, an empty password is
21 // returned.
22 std::string AddRandomPasswordToKeychain(const AppleKeychain& keychain,
23 const std::string& service_name,
24 const std::string& account_name) {
25 // Generate a password with 128 bits of randomness.
26 const int kBytes = 128 / 8;
27 std::string password;
28 base::Base64Encode(base::RandBytesAsString(kBytes), &password);
29 void* password_data =
30 const_cast<void*>(static_cast<const void*>(password.data()));
31
32 OSStatus error = keychain.AddGenericPassword(NULL,
33 service_name.size(),
34 service_name.data(),
35 account_name.size(),
36 account_name.data(),
37 password.size(),
38 password_data,
39 NULL);
40
41 if (error != noErr) {
42 OSSTATUS_DLOG(ERROR, error) << "Keychain add failed";
43 return std::string();
44 }
45
46 return password;
47 }
48
49 } // namespace
50
51 std::string EncryptorPassword::GetEncryptorPassword() const {
52 // These two strings ARE indeed user facing. But they are used to access
53 // the encryption keyword. So as to not lose encrypted data when system
54 // locale changes we DO NOT LOCALIZE.
55 const std::string service_name = "Chrome Safe Storage";
56 const std::string account_name = "Chrome";
57
58 UInt32 password_length = 0;
59 void* password_data = NULL;
60 OSStatus error = keychain_.FindGenericPassword(NULL,
61 service_name.size(),
62 service_name.data(),
63 account_name.size(),
64 account_name.data(),
65 &password_length,
66 &password_data,
67 NULL);
68
69 if (error == noErr) {
70 std::string password =
71 std::string(static_cast<char*>(password_data), password_length);
72 keychain_.ItemFreeContent(NULL, password_data);
73 return password;
74 } else if (error == errSecItemNotFound) {
75 return AddRandomPasswordToKeychain(keychain_, service_name, account_name);
76 } else {
77 OSSTATUS_DLOG(ERROR, error) << "Keychain lookup failed";
78 return std::string();
79 }
80 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698