My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#import <Security/Security.h>
#import "SecAccess.h"
#import "SecKeychain.h"
#import "SecKeychainItem.h"
#import "SecKeychainSearch.h"
#import "SecTrustedApplication.h"

OSStatus SecKeychainFindGenericPassword(CFTypeRef keychainOrArray, UInt32 serviceNameLength, const char *serviceName, UInt32 accountNameLength, const char *accountName, UInt32 *passwordLength, void **passwordData, SecKeychainItemRef *itemRef)
{
SecKeychainAttributeList attributeList;
attributeList.count = 2;
attributeList.attr = malloc(sizeof(SecKeychainAttribute) * attributeList.count);
attributeList.attr[0].tag = kSecAccountItemAttr;
attributeList.attr[0].length = accountNameLength;
attributeList.attr[0].data = (void *)accountName;
attributeList.attr[1].tag = kSecServiceItemAttr;
attributeList.attr[1].length = serviceNameLength;
attributeList.attr[1].data = (void *)serviceName;

SecKeychainSearchRef search;
OSStatus status = SecKeychainSearchCreateFromAttributes(NULL, kSecGenericPasswordItemClass, &attributeList, &search);
if (status != 0)
{
free(attributeList.attr);
return status;
}

status = SecKeychainSearchCopyNext(search, itemRef);
if (status == 0 && *itemRef)
{
status = SecKeychainItemCopyAttributesAndData(*itemRef, NULL, NULL, NULL, passwordLength, passwordData);
}
else
{
status = errSecItemNotFound;
*itemRef = NULL;
*passwordData = NULL;
*passwordLength = 0;
}

free(attributeList.attr);
return status;
}

OSStatus SecKeychainAddGenericPassword (SecKeychainRef keychain, UInt32 serviceNameLength, const char *serviceName, UInt32 accountNameLength, const char *accountName, UInt32 passwordLength, void *passwordData, SecKeychainItemRef *itemRef)
{
SecKeychainAttributeList attributeList;
attributeList.count = 2;
attributeList.attr = malloc(sizeof(SecKeychainAttribute) * attributeList.count);
attributeList.attr[0].tag = kSecAccountItemAttr;
attributeList.attr[0].length = accountNameLength;
attributeList.attr[0].data = (void *)accountName;
attributeList.attr[1].tag = kSecServiceItemAttr;
attributeList.attr[1].length = serviceNameLength;
attributeList.attr[1].data = (void *)serviceName;

OSStatus status = SecKeychainItemCreateFromContent(kSecGenericPasswordItemClass, &attributeList, passwordLength, passwordData, keychain, NULL, itemRef);
free(attributeList.attr);
return status;
}

OSStatus SecKeychainSearchCreateFromAttributes(CFTypeRef keychainOrArray,SecItemClass itemClass,const SecKeychainAttributeList *attributeList,SecKeychainSearchRef *resultSearch) {
*resultSearch=[[SecKeychainSearch alloc] initWithKeychainOrArray:keychainOrArray itemClass:itemClass attributeList:attributeList];
return 0;
}

OSStatus SecKeychainSearchCopyNext(SecKeychainSearchRef search,SecKeychainItemRef *resultItem) {
*resultItem=[search copyNextItem];
return 0;
}

OSStatus SecKeychainItemCopyAttributesAndData(SecKeychainItemRef item,SecKeychainAttributeInfo *info,SecItemClass *itemClass,SecKeychainAttributeList **attributeList,UInt32 *length,void **resultBytes) {
[item copyAttributeInfo:info itemClass:itemClass attributeList:attributeList length:length bytes:resultBytes];
return 0;
}

OSStatus SecKeychainItemModifyAttributesAndData(SecKeychainItemRef item,const SecKeychainAttributeList *attributeList,UInt32 length,const void *bytes) {
[item modifyAttributeList:attributeList length:length bytes:bytes];
[[item keychain] modifyKeychainItem:item];
return 0;
}

OSStatus SecKeychainItemFreeAttributesAndData(SecKeychainAttributeList *attributeList,void *data) {
SecFreeAttributeList(attributeList);

if(data!=NULL)
NSZoneFree(NULL,data);

return 0;
}

OSStatus SecKeychainItemFreeContent(SecKeychainAttributeList *attributeList,void *data) {
SecFreeAttributeList(attributeList);

if(data!=NULL)
NSZoneFree(NULL,data);

return 0;
}

OSStatus SecTrustedApplicationCreateFromPath(const char *path,SecTrustedApplicationRef *resultApplication) {
*resultApplication=[[SecTrustedApplication alloc] init];
return 0;
}

OSStatus SecAccessCreate(CFStringRef descriptor,CFArrayRef trustedList,SecAccessRef *resultAccess) {
*resultAccess=[[SecAccess alloc] initWithDescriptor:descriptor trustedList:trustedList];
return 0;
}

OSStatus SecKeychainItemCreateFromContent(SecItemClass itemClass,SecKeychainAttributeList *attributeList,UInt32 length,const void *bytes,SecKeychainRef keychain,SecAccessRef initialAccess,SecKeychainItemRef *resultItem) {
SecKeychainItemRef item=[[SecKeychainItem alloc] initWithItemClass:itemClass attributeList:attributeList length:length bytes:bytes];

if(keychain==NULL)
keychain=[SecKeychain defaultUserKeychain];

[item setGUID:[keychain createGUID]];
[item setKeychain:keychain];
[item setAccess:initialAccess];

[keychain addKeychainItem:item];

if(resultItem!=NULL)
*resultItem=item;

return 0;
}


OSStatus SecKeychainItemDelete(SecKeychainItemRef item) {
[[item keychain] removeKeychainItem:item];
return 0;
}

void SecByteCopy(const void *srcVoid,void *dstVoid,size_t length){
const uint8_t *src=srcVoid;
uint8_t *dst=dstVoid;
size_t i;

for(i=0;i<length;i++)
dst[i]=src[i];
}

SecKeychainAttributeList *SecCopyAttributeList(const SecKeychainAttributeList *attributeList){
SecKeychainAttributeList *result=NSZoneMalloc(NULL,sizeof(SecKeychainAttributeList));

result->count=attributeList->count;
result->attr=NSZoneMalloc(NULL,sizeof(SecKeychainAttribute)*result->count);

int i;
for(i=0;i<result->count;i++){
result->attr[i].tag=attributeList->attr[i].tag;
result->attr[i].length=attributeList->attr[i].length;
result->attr[i].data=NSZoneMalloc(NULL,result->attr[i].length);
SecByteCopy(attributeList->attr[i].data,result->attr[i].data,result->attr[i].length);
}

return result;
}

void SecFreeAttributeList(SecKeychainAttributeList *list){
if(list!=NULL){
int i;

if(list->attr!=NULL){
for(i=0;i<list->count;i++)
if(list->attr[i].data!=NULL)
NSZoneFree(NULL,list->attr[i].data);

NSZoneFree(NULL,list->attr);
}

NSZoneFree(NULL,list);
}
}

Change log

bf8532963c26 by Christopher Lloyd <c...@objc.net> on Apr 29, 2010   Diff
removed use of noErr and added Debug
configuration
Go to: 
Project members, sign in to write a code review

Older revisions

d2e941fdda74 by Christopher Lloyd <c...@objc.net> on Apr 1, 2010   Diff
(fixes  issue #549 ) Security additions
by Matt Gallagher
48dc64747639 by cjwlloyd on Mar 4, 2010   Diff
initial Security framework
All revisions of this file

File info

Size: 6165 bytes, 176 lines
Powered by Google Project Hosting