| 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" |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 } | 119 } |
| 120 | 120 |
| 121 OSStatus AppleKeychain::AddGenericPassword(SecKeychainRef keychain, | 121 OSStatus AppleKeychain::AddGenericPassword(SecKeychainRef keychain, |
| 122 UInt32 serviceNameLength, | 122 UInt32 serviceNameLength, |
| 123 const char* serviceName, | 123 const char* serviceName, |
| 124 UInt32 accountNameLength, | 124 UInt32 accountNameLength, |
| 125 const char* accountName, | 125 const char* accountName, |
| 126 UInt32 passwordLength, | 126 UInt32 passwordLength, |
| 127 const void* passwordData, | 127 const void* passwordData, |
| 128 SecKeychainItemRef* itemRef) const { | 128 SecKeychainItemRef* itemRef) const { |
| 129 base::mac::ScopedCFTypeRef<CFDictionaryRef> query( | 129 base::ScopedCFTypeRef<CFDictionaryRef> query(CreateGenericPasswordQuery( |
| 130 CreateGenericPasswordQuery(serviceNameLength, | 130 serviceNameLength, serviceName, accountNameLength, accountName)); |
| 131 serviceName, | |
| 132 accountNameLength, | |
| 133 accountName)); | |
| 134 // Check that there is not already a password. | 131 // Check that there is not already a password. |
| 135 OSStatus status = SecItemCopyMatching(query, NULL); | 132 OSStatus status = SecItemCopyMatching(query, NULL); |
| 136 if (status == errSecItemNotFound) { | 133 if (status == errSecItemNotFound) { |
| 137 // A new entry must be created. | 134 // A new entry must be created. |
| 138 base::mac::ScopedCFTypeRef<CFDictionaryRef> keychain_data( | 135 base::ScopedCFTypeRef<CFDictionaryRef> keychain_data( |
| 139 CreateKeychainData(serviceNameLength, | 136 CreateKeychainData(serviceNameLength, |
| 140 serviceName, | 137 serviceName, |
| 141 accountNameLength, | 138 accountNameLength, |
| 142 accountName, | 139 accountName, |
| 143 passwordLength, | 140 passwordLength, |
| 144 passwordData, | 141 passwordData, |
| 145 kKeychainActionCreate)); | 142 kKeychainActionCreate)); |
| 146 status = SecItemAdd(keychain_data, NULL); | 143 status = SecItemAdd(keychain_data, NULL); |
| 147 } else if (status == noErr) { | 144 } else if (status == noErr) { |
| 148 // The entry must be updated. | 145 // The entry must be updated. |
| 149 base::mac::ScopedCFTypeRef<CFDictionaryRef> keychain_data( | 146 base::ScopedCFTypeRef<CFDictionaryRef> keychain_data( |
| 150 CreateKeychainData(serviceNameLength, | 147 CreateKeychainData(serviceNameLength, |
| 151 serviceName, | 148 serviceName, |
| 152 accountNameLength, | 149 accountNameLength, |
| 153 accountName, | 150 accountName, |
| 154 passwordLength, | 151 passwordLength, |
| 155 passwordData, | 152 passwordData, |
| 156 kKeychainActionUpdate)); | 153 kKeychainActionUpdate)); |
| 157 status = SecItemUpdate(query, keychain_data); | 154 status = SecItemUpdate(query, keychain_data); |
| 158 } | 155 } |
| 159 | 156 |
| 160 return status; | 157 return status; |
| 161 } | 158 } |
| 162 | 159 |
| 163 OSStatus AppleKeychain::FindGenericPassword(CFTypeRef keychainOrArray, | 160 OSStatus AppleKeychain::FindGenericPassword(CFTypeRef keychainOrArray, |
| 164 UInt32 serviceNameLength, | 161 UInt32 serviceNameLength, |
| 165 const char* serviceName, | 162 const char* serviceName, |
| 166 UInt32 accountNameLength, | 163 UInt32 accountNameLength, |
| 167 const char* accountName, | 164 const char* accountName, |
| 168 UInt32* passwordLength, | 165 UInt32* passwordLength, |
| 169 void** passwordData, | 166 void** passwordData, |
| 170 SecKeychainItemRef* itemRef) const { | 167 SecKeychainItemRef* itemRef) const { |
| 171 DCHECK((passwordData && passwordLength) || | 168 DCHECK((passwordData && passwordLength) || |
| 172 (!passwordData && !passwordLength)); | 169 (!passwordData && !passwordLength)); |
| 173 base::mac::ScopedCFTypeRef<CFDictionaryRef> query( | 170 base::ScopedCFTypeRef<CFDictionaryRef> query(CreateGenericPasswordQuery( |
| 174 CreateGenericPasswordQuery(serviceNameLength, | 171 serviceNameLength, serviceName, accountNameLength, accountName)); |
| 175 serviceName, | |
| 176 accountNameLength, | |
| 177 accountName)); | |
| 178 | 172 |
| 179 // Get the keychain item containing the password. | 173 // Get the keychain item containing the password. |
| 180 CFTypeRef resultRef = NULL; | 174 CFTypeRef resultRef = NULL; |
| 181 OSStatus status = SecItemCopyMatching(query, &resultRef); | 175 OSStatus status = SecItemCopyMatching(query, &resultRef); |
| 182 base::mac::ScopedCFTypeRef<CFTypeRef> result(resultRef); | 176 base::ScopedCFTypeRef<CFTypeRef> result(resultRef); |
| 183 | 177 |
| 184 if (status != noErr) { | 178 if (status != noErr) { |
| 185 if (passwordData) { | 179 if (passwordData) { |
| 186 *passwordData = NULL; | 180 *passwordData = NULL; |
| 187 *passwordLength = 0; | 181 *passwordLength = 0; |
| 188 } | 182 } |
| 189 return status; | 183 return status; |
| 190 } | 184 } |
| 191 | 185 |
| 192 if (passwordData) { | 186 if (passwordData) { |
| 193 CFDataRef data = base::mac::CFCast<CFDataRef>(result); | 187 CFDataRef data = base::mac::CFCast<CFDataRef>(result); |
| 194 NSUInteger length = CFDataGetLength(data); | 188 NSUInteger length = CFDataGetLength(data); |
| 195 *passwordData = malloc(length * sizeof(UInt8)); | 189 *passwordData = malloc(length * sizeof(UInt8)); |
| 196 CFDataGetBytes(data, CFRangeMake(0, length), (UInt8*)*passwordData); | 190 CFDataGetBytes(data, CFRangeMake(0, length), (UInt8*)*passwordData); |
| 197 *passwordLength = length; | 191 *passwordLength = length; |
| 198 } | 192 } |
| 199 return status; | 193 return status; |
| 200 } | 194 } |
| 201 | 195 |
| 202 } // namespace crypto | 196 } // namespace crypto |
| OLD | NEW |