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 "crypto/apple_keychain.h" | 5 #include "crypto/apple_keychain.h" |
6 | 6 |
7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
8 | 8 |
9 #include "base/mac/foundation_util.h" | 9 #include "base/mac/foundation_util.h" |
10 #include "base/mac/scoped_cftyperef.h" | 10 #include "base/mac/scoped_cftyperef.h" |
11 #include "base/memory/scoped_nsobject.h" | 11 #include "base/mac/scoped_nsobject.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 enum KeychainAction { | 15 enum KeychainAction { |
16 kKeychainActionCreate, | 16 kKeychainActionCreate, |
17 kKeychainActionUpdate | 17 kKeychainActionUpdate |
18 }; | 18 }; |
19 | 19 |
20 // Creates a dictionary that can be used to query the keystore. | 20 // Creates a dictionary that can be used to query the keystore. |
21 // Ownership follows the Create rule. | 21 // Ownership follows the Create rule. |
22 CFDictionaryRef CreateGenericPasswordQuery(UInt32 serviceNameLength, | 22 CFDictionaryRef CreateGenericPasswordQuery(UInt32 serviceNameLength, |
23 const char* serviceName, | 23 const char* serviceName, |
24 UInt32 accountNameLength, | 24 UInt32 accountNameLength, |
25 const char* accountName) { | 25 const char* accountName) { |
26 CFMutableDictionaryRef query = | 26 CFMutableDictionaryRef query = |
27 CFDictionaryCreateMutable(NULL, | 27 CFDictionaryCreateMutable(NULL, |
28 5, | 28 5, |
29 &kCFTypeDictionaryKeyCallBacks, | 29 &kCFTypeDictionaryKeyCallBacks, |
30 &kCFTypeDictionaryValueCallBacks); | 30 &kCFTypeDictionaryValueCallBacks); |
31 // Type of element is generic password. | 31 // Type of element is generic password. |
32 CFDictionarySetValue(query, kSecClass, kSecClassGenericPassword); | 32 CFDictionarySetValue(query, kSecClass, kSecClassGenericPassword); |
33 | 33 |
34 // Set the service name. | 34 // Set the service name. |
35 scoped_nsobject<NSString> service_name_ns( | 35 base::scoped_nsobject<NSString> service_name_ns( |
36 [[NSString alloc] initWithBytes:serviceName | 36 [[NSString alloc] initWithBytes:serviceName |
37 length:serviceNameLength | 37 length:serviceNameLength |
38 encoding:NSUTF8StringEncoding]); | 38 encoding:NSUTF8StringEncoding]); |
39 CFDictionarySetValue(query, kSecAttrService, | 39 CFDictionarySetValue(query, kSecAttrService, |
40 base::mac::NSToCFCast(service_name_ns)); | 40 base::mac::NSToCFCast(service_name_ns)); |
41 | 41 |
42 // Set the account name. | 42 // Set the account name. |
43 scoped_nsobject<NSString> account_name_ns( | 43 base::scoped_nsobject<NSString> account_name_ns( |
44 [[NSString alloc] initWithBytes:accountName | 44 [[NSString alloc] initWithBytes:accountName |
45 length:accountNameLength | 45 length:accountNameLength |
46 encoding:NSUTF8StringEncoding]); | 46 encoding:NSUTF8StringEncoding]); |
47 CFDictionarySetValue(query, kSecAttrAccount, | 47 CFDictionarySetValue(query, kSecAttrAccount, |
48 base::mac::NSToCFCast(account_name_ns)); | 48 base::mac::NSToCFCast(account_name_ns)); |
49 | 49 |
50 // Use the proper search constants, return only the data of the first match. | 50 // Use the proper search constants, return only the data of the first match. |
51 CFDictionarySetValue(query, kSecMatchLimit, kSecMatchLimitOne); | 51 CFDictionarySetValue(query, kSecMatchLimit, kSecMatchLimitOne); |
52 CFDictionarySetValue(query, kSecReturnData, kCFBooleanTrue); | 52 CFDictionarySetValue(query, kSecReturnData, kCFBooleanTrue); |
53 return query; | 53 return query; |
(...skipping 25 matching lines...) Expand all Loading... |
79 | 79 |
80 // Set the type of the data. | 80 // Set the type of the data. |
81 CFDictionarySetValue(keychain_data, kSecClass, kSecClassGenericPassword); | 81 CFDictionarySetValue(keychain_data, kSecClass, kSecClassGenericPassword); |
82 | 82 |
83 // Only allow access when the device has been unlocked. | 83 // Only allow access when the device has been unlocked. |
84 CFDictionarySetValue(keychain_data, | 84 CFDictionarySetValue(keychain_data, |
85 kSecAttrAccessible, | 85 kSecAttrAccessible, |
86 kSecAttrAccessibleWhenUnlocked); | 86 kSecAttrAccessibleWhenUnlocked); |
87 | 87 |
88 // Set the service name. | 88 // Set the service name. |
89 scoped_nsobject<NSString> service_name_ns( | 89 base::scoped_nsobject<NSString> service_name_ns( |
90 [[NSString alloc] initWithBytes:serviceName | 90 [[NSString alloc] initWithBytes:serviceName |
91 length:serviceNameLength | 91 length:serviceNameLength |
92 encoding:NSUTF8StringEncoding]); | 92 encoding:NSUTF8StringEncoding]); |
93 CFDictionarySetValue(keychain_data, kSecAttrService, | 93 CFDictionarySetValue(keychain_data, kSecAttrService, |
94 base::mac::NSToCFCast(service_name_ns)); | 94 base::mac::NSToCFCast(service_name_ns)); |
95 | 95 |
96 // Set the account name. | 96 // Set the account name. |
97 scoped_nsobject<NSString> account_name_ns( | 97 base::scoped_nsobject<NSString> account_name_ns( |
98 [[NSString alloc] initWithBytes:accountName | 98 [[NSString alloc] initWithBytes:accountName |
99 length:accountNameLength | 99 length:accountNameLength |
100 encoding:NSUTF8StringEncoding]); | 100 encoding:NSUTF8StringEncoding]); |
101 CFDictionarySetValue(keychain_data, kSecAttrAccount, | 101 CFDictionarySetValue(keychain_data, kSecAttrAccount, |
102 base::mac::NSToCFCast(account_name_ns)); | 102 base::mac::NSToCFCast(account_name_ns)); |
103 | 103 |
104 return keychain_data; | 104 return keychain_data; |
105 } | 105 } |
106 | 106 |
107 } // namespace | 107 } // namespace |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 CFDataRef data = base::mac::CFCast<CFDataRef>(result); | 187 CFDataRef data = base::mac::CFCast<CFDataRef>(result); |
188 NSUInteger length = CFDataGetLength(data); | 188 NSUInteger length = CFDataGetLength(data); |
189 *passwordData = malloc(length * sizeof(UInt8)); | 189 *passwordData = malloc(length * sizeof(UInt8)); |
190 CFDataGetBytes(data, CFRangeMake(0, length), (UInt8*)*passwordData); | 190 CFDataGetBytes(data, CFRangeMake(0, length), (UInt8*)*passwordData); |
191 *passwordLength = length; | 191 *passwordLength = length; |
192 } | 192 } |
193 return status; | 193 return status; |
194 } | 194 } |
195 | 195 |
196 } // namespace crypto | 196 } // namespace crypto |
OLD | NEW |