[email protected] | b9d3d00 | 2012-03-17 12:14:50 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 32d0ef5 | 2009-05-26 20:17:50 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 982f1ab | 2012-08-30 13:03:46 | [diff] [blame] | 5 | #include "crypto/apple_keychain.h" |
| 6 | |
| 7 | #import <Foundation/Foundation.h> |
[email protected] | b9d3d00 | 2012-03-17 12:14:50 | [diff] [blame] | 8 | |
[email protected] | d6e8fe6 | 2012-10-03 05:46:45 | [diff] [blame^] | 9 | #include "base/synchronization/lock.h" |
| 10 | #include "crypto/mac_security_services_lock.h" |
| 11 | |
[email protected] | b9d3d00 | 2012-03-17 12:14:50 | [diff] [blame] | 12 | namespace crypto { |
| 13 | |
[email protected] | 982f1ab | 2012-08-30 13:03:46 | [diff] [blame] | 14 | AppleKeychain::AppleKeychain() {} |
[email protected] | b9d3d00 | 2012-03-17 12:14:50 | [diff] [blame] | 15 | |
[email protected] | 982f1ab | 2012-08-30 13:03:46 | [diff] [blame] | 16 | AppleKeychain::~AppleKeychain() {} |
[email protected] | 32d0ef5 | 2009-05-26 20:17:50 | [diff] [blame] | 17 | |
[email protected] | 982f1ab | 2012-08-30 13:03:46 | [diff] [blame] | 18 | OSStatus AppleKeychain::ItemCopyAttributesAndData( |
[email protected] | b9d3d00 | 2012-03-17 12:14:50 | [diff] [blame] | 19 | SecKeychainItemRef itemRef, |
| 20 | SecKeychainAttributeInfo* info, |
| 21 | SecItemClass* itemClass, |
| 22 | SecKeychainAttributeList** attrList, |
| 23 | UInt32* length, |
| 24 | void** outData) const { |
[email protected] | d6e8fe6 | 2012-10-03 05:46:45 | [diff] [blame^] | 25 | base::AutoLock lock(GetMacSecurityServicesLock()); |
[email protected] | 32d0ef5 | 2009-05-26 20:17:50 | [diff] [blame] | 26 | return SecKeychainItemCopyAttributesAndData(itemRef, info, itemClass, |
| 27 | attrList, length, outData); |
| 28 | } |
| 29 | |
[email protected] | 982f1ab | 2012-08-30 13:03:46 | [diff] [blame] | 30 | OSStatus AppleKeychain::ItemModifyAttributesAndData( |
[email protected] | b9d3d00 | 2012-03-17 12:14:50 | [diff] [blame] | 31 | SecKeychainItemRef itemRef, |
| 32 | const SecKeychainAttributeList* attrList, |
| 33 | UInt32 length, |
| 34 | const void* data) const { |
[email protected] | d6e8fe6 | 2012-10-03 05:46:45 | [diff] [blame^] | 35 | base::AutoLock lock(GetMacSecurityServicesLock()); |
[email protected] | 06139a2 | 2009-06-23 16:36:20 | [diff] [blame] | 36 | return SecKeychainItemModifyAttributesAndData(itemRef, attrList, length, |
| 37 | data); |
| 38 | } |
| 39 | |
[email protected] | 982f1ab | 2012-08-30 13:03:46 | [diff] [blame] | 40 | OSStatus AppleKeychain::ItemFreeAttributesAndData( |
[email protected] | b9d3d00 | 2012-03-17 12:14:50 | [diff] [blame] | 41 | SecKeychainAttributeList* attrList, |
| 42 | void* data) const { |
[email protected] | d6e8fe6 | 2012-10-03 05:46:45 | [diff] [blame^] | 43 | base::AutoLock lock(GetMacSecurityServicesLock()); |
[email protected] | 32d0ef5 | 2009-05-26 20:17:50 | [diff] [blame] | 44 | return SecKeychainItemFreeAttributesAndData(attrList, data); |
| 45 | } |
| 46 | |
[email protected] | 982f1ab | 2012-08-30 13:03:46 | [diff] [blame] | 47 | OSStatus AppleKeychain::ItemDelete(SecKeychainItemRef itemRef) const { |
[email protected] | d6e8fe6 | 2012-10-03 05:46:45 | [diff] [blame^] | 48 | base::AutoLock lock(GetMacSecurityServicesLock()); |
[email protected] | 0a21fde | 2009-07-13 23:44:08 | [diff] [blame] | 49 | return SecKeychainItemDelete(itemRef); |
| 50 | } |
| 51 | |
[email protected] | 982f1ab | 2012-08-30 13:03:46 | [diff] [blame] | 52 | OSStatus AppleKeychain::SearchCreateFromAttributes( |
[email protected] | b9d3d00 | 2012-03-17 12:14:50 | [diff] [blame] | 53 | CFTypeRef keychainOrArray, |
| 54 | SecItemClass itemClass, |
| 55 | const SecKeychainAttributeList* attrList, |
| 56 | SecKeychainSearchRef* searchRef) const { |
[email protected] | d6e8fe6 | 2012-10-03 05:46:45 | [diff] [blame^] | 57 | base::AutoLock lock(GetMacSecurityServicesLock()); |
[email protected] | 32d0ef5 | 2009-05-26 20:17:50 | [diff] [blame] | 58 | return SecKeychainSearchCreateFromAttributes(keychainOrArray, itemClass, |
| 59 | attrList, searchRef); |
| 60 | } |
| 61 | |
[email protected] | 982f1ab | 2012-08-30 13:03:46 | [diff] [blame] | 62 | OSStatus AppleKeychain::SearchCopyNext(SecKeychainSearchRef searchRef, |
| 63 | SecKeychainItemRef* itemRef) const { |
[email protected] | d6e8fe6 | 2012-10-03 05:46:45 | [diff] [blame^] | 64 | base::AutoLock lock(GetMacSecurityServicesLock()); |
[email protected] | 32d0ef5 | 2009-05-26 20:17:50 | [diff] [blame] | 65 | return SecKeychainSearchCopyNext(searchRef, itemRef); |
| 66 | } |
| 67 | |
[email protected] | 982f1ab | 2012-08-30 13:03:46 | [diff] [blame] | 68 | OSStatus AppleKeychain::AddInternetPassword( |
[email protected] | 06139a2 | 2009-06-23 16:36:20 | [diff] [blame] | 69 | SecKeychainRef keychain, |
[email protected] | b9d3d00 | 2012-03-17 12:14:50 | [diff] [blame] | 70 | UInt32 serverNameLength, |
| 71 | const char* serverName, |
| 72 | UInt32 securityDomainLength, |
| 73 | const char* securityDomain, |
| 74 | UInt32 accountNameLength, |
| 75 | const char* accountName, |
| 76 | UInt32 pathLength, |
| 77 | const char* path, |
| 78 | UInt16 port, |
| 79 | SecProtocolType protocol, |
[email protected] | 06139a2 | 2009-06-23 16:36:20 | [diff] [blame] | 80 | SecAuthenticationType authenticationType, |
[email protected] | b9d3d00 | 2012-03-17 12:14:50 | [diff] [blame] | 81 | UInt32 passwordLength, |
| 82 | const void* passwordData, |
| 83 | SecKeychainItemRef* itemRef) const { |
[email protected] | d6e8fe6 | 2012-10-03 05:46:45 | [diff] [blame^] | 84 | base::AutoLock lock(GetMacSecurityServicesLock()); |
[email protected] | 06139a2 | 2009-06-23 16:36:20 | [diff] [blame] | 85 | return SecKeychainAddInternetPassword(keychain, |
| 86 | serverNameLength, serverName, |
| 87 | securityDomainLength, securityDomain, |
| 88 | accountNameLength, accountName, |
| 89 | pathLength, path, |
| 90 | port, protocol, authenticationType, |
| 91 | passwordLength, passwordData, |
| 92 | itemRef); |
| 93 | } |
| 94 | |
[email protected] | 982f1ab | 2012-08-30 13:03:46 | [diff] [blame] | 95 | OSStatus AppleKeychain::FindGenericPassword(CFTypeRef keychainOrArray, |
| 96 | UInt32 serviceNameLength, |
| 97 | const char* serviceName, |
| 98 | UInt32 accountNameLength, |
| 99 | const char* accountName, |
| 100 | UInt32* passwordLength, |
| 101 | void** passwordData, |
| 102 | SecKeychainItemRef* itemRef) const { |
[email protected] | d6e8fe6 | 2012-10-03 05:46:45 | [diff] [blame^] | 103 | base::AutoLock lock(GetMacSecurityServicesLock()); |
[email protected] | ecbf289 | 2010-07-16 01:51:45 | [diff] [blame] | 104 | return SecKeychainFindGenericPassword(keychainOrArray, |
| 105 | serviceNameLength, |
| 106 | serviceName, |
| 107 | accountNameLength, |
| 108 | accountName, |
| 109 | passwordLength, |
| 110 | passwordData, |
| 111 | itemRef); |
| 112 | } |
| 113 | |
[email protected] | 982f1ab | 2012-08-30 13:03:46 | [diff] [blame] | 114 | OSStatus AppleKeychain::ItemFreeContent(SecKeychainAttributeList* attrList, |
| 115 | void* data) const { |
[email protected] | d6e8fe6 | 2012-10-03 05:46:45 | [diff] [blame^] | 116 | base::AutoLock lock(GetMacSecurityServicesLock()); |
[email protected] | ecbf289 | 2010-07-16 01:51:45 | [diff] [blame] | 117 | return SecKeychainItemFreeContent(attrList, data); |
| 118 | } |
| 119 | |
[email protected] | 982f1ab | 2012-08-30 13:03:46 | [diff] [blame] | 120 | OSStatus AppleKeychain::AddGenericPassword(SecKeychainRef keychain, |
| 121 | UInt32 serviceNameLength, |
| 122 | const char* serviceName, |
| 123 | UInt32 accountNameLength, |
| 124 | const char* accountName, |
| 125 | UInt32 passwordLength, |
| 126 | const void* passwordData, |
| 127 | SecKeychainItemRef* itemRef) const { |
[email protected] | d6e8fe6 | 2012-10-03 05:46:45 | [diff] [blame^] | 128 | base::AutoLock lock(GetMacSecurityServicesLock()); |
[email protected] | ecbf289 | 2010-07-16 01:51:45 | [diff] [blame] | 129 | return SecKeychainAddGenericPassword(keychain, |
| 130 | serviceNameLength, |
| 131 | serviceName, |
| 132 | accountNameLength, |
| 133 | accountName, |
| 134 | passwordLength, |
| 135 | passwordData, |
| 136 | itemRef); |
| 137 | } |
| 138 | |
[email protected] | 982f1ab | 2012-08-30 13:03:46 | [diff] [blame] | 139 | void AppleKeychain::Free(CFTypeRef ref) const { |
[email protected] | b9d3d00 | 2012-03-17 12:14:50 | [diff] [blame] | 140 | if (ref) |
[email protected] | 32d0ef5 | 2009-05-26 20:17:50 | [diff] [blame] | 141 | CFRelease(ref); |
[email protected] | 32d0ef5 | 2009-05-26 20:17:50 | [diff] [blame] | 142 | } |
[email protected] | b9d3d00 | 2012-03-17 12:14:50 | [diff] [blame] | 143 | |
| 144 | } // namespace crypto |