33
44#import " PlistObjectConverter.h"
55
6-
6+ extern NSString *objectTypeNotSupported;
77
88
99@implementation PlistObjectConverter
@@ -216,6 +216,86 @@ + (NSDictionary *) convertSecCertificateRef: (SecCertificateRef) certificate {
216216 return credentialDict;
217217}
218218
219+ #if 0
220+ + (id) convertSecItemResult: (CFTypeRef*) result withQuery: (CFDictionaryRef) query {
221+ if (result == NULL) {
222+ return [PlistObjectConverter getSerializedNilValue];
223+ }
224+ int resultNb = 0;
225+
226+
227+ // What an awful API
228+ if ((CFDictionaryContainsKey(query, kSecReturnData)) && (CFDictionaryGetValue(query, kSecReturnData) == kCFBooleanTrue)) {
229+ resultNb++;
230+ }
231+ else if ((CFDictionaryContainsKey(query, kSecReturnAttributes)) && (CFDictionaryGetValue(query, kSecReturnAttributes) == kCFBooleanTrue)) {
232+ resultNb++;
233+ }
234+ else if ((CFDictionaryContainsKey(query, kSecReturnRef)) && (CFDictionaryGetValue(query, kSecReturnRef) == kCFBooleanTrue)) {
235+ resultNb++;
236+ }
237+ else if ((CFDictionaryContainsKey(query, kSecReturnPersistentRef)) && (CFDictionaryGetValue(query, kSecReturnPersistentRef) == kCFBooleanTrue)) {
238+ resultNb++;
239+ }
240+ NSLog(@"========================================RESULT NB %d", resultNb);
241+ if (resultNb == 1) {
242+ if ((CFDictionaryContainsKey(query, kSecReturnData)) && (CFDictionaryGetValue(query, kSecReturnData) == kCFBooleanTrue)) {
243+
244+ NSLog(@"========================================PASSWORD ");
245+ NSLog(@"========================================LENGTH %ld", CFDataGetLength((CFDataRef)result));
246+ //NSData *theData = (NSData *)result;
247+ //NSLog(@"========================================LOL %@ ", [theData base64EncodedStringWithOptions:0]);
248+ //return (NSData *)result;
249+ return [NSData data];
250+ }
251+ else if ((CFDictionaryContainsKey(query, kSecReturnAttributes)) && (CFDictionaryGetValue(query, kSecReturnAttributes) == kCFBooleanTrue)) {
252+ NSLog(@"======================================== DICT ");
253+ return (NSDictionary*) result;
254+ }
255+ else if ( ((CFDictionaryContainsKey(query, kSecReturnRef)) && (CFDictionaryGetValue(query, kSecReturnRef) == kCFBooleanTrue)) || ((CFDictionaryContainsKey(query, kSecReturnPersistentRef)) && (CFDictionaryGetValue(query, kSecReturnPersistentRef) == kCFBooleanTrue)) ) {
256+ NSLog(@"======================================== SECITEM ");
257+
258+ CFTypeRef secClass = CFDictionaryGetValue(query, kSecClass);
259+ if ((secClass == kSecClassGenericPassword) || (secClass == kSecClassGenericPassword)) {
260+ return (NSData *)result;
261+ }
262+ else if (secClass == kSecClassCertificate) {
263+ if (CFDictionaryContainsKey(query, kSecValueRef)) {
264+ return [PlistObjectConverter convertSecCertificateRef:(SecCertificateRef)result];
265+ }
266+ else if (CFDictionaryContainsKey(query, kSecValuePersistentRef)) {
267+ return [PlistObjectConverter convertSecCertificateRef:(SecCertificateRef)result];
268+ }
269+ }
270+ else if (secClass == kSecClassIdentity) {
271+ if (CFDictionaryContainsKey(query, kSecValueRef)) {
272+ return [PlistObjectConverter convertSecIdentityRef:(SecIdentityRef) result];
273+ }
274+ else if (CFDictionaryContainsKey(query, kSecValuePersistentRef)) {
275+ return [PlistObjectConverter convertSecIdentityRef:(SecIdentityRef) result];
276+ }
277+ }
278+ else if (secClass == kSecClassKey) {
279+ if (CFDictionaryContainsKey(query, kSecValueRef)) {
280+ return [PlistObjectConverter convertSecKeyRef:(SecKeyRef) result];
281+ }
282+ else if (CFDictionaryContainsKey(query, kSecValuePersistentRef)) {
283+ return [PlistObjectConverter convertSecKeyRef:(SecKeyRef) result];
284+ }
285+
286+ }
287+
288+ }
289+ }
290+
291+ else if (resultNb > 1) {
292+ //Give up for now
293+ // TODO: support queries for multiple items
294+ }
295+
296+ return objectTypeNotSupported;
297+ }
298+ #endif
219299
220300// attributes dictionnary when calling SecItemAdd() and SecItemUpdate()
221301+ (NSDictionary *) convertSecItemAttributesDict : (CFDictionaryRef) attributes {
@@ -224,39 +304,51 @@ + (NSDictionary *) convertSecItemAttributesDict: (CFDictionaryRef) attributes {
224304 NSMutableDictionary *attributesPlist = [NSMutableDictionary dictionaryWithDictionary: (NSDictionary *) attributes];
225305
226306 CFTypeRef secClass = CFDictionaryGetValue (attributes, kSecClass );
307+
308+ if (CFDictionaryContainsKey (attributes, kSecValueData )) {
309+ NSData *theData = (NSData *)CFDictionaryGetValue (attributes, kSecValueData );
310+ [attributesPlist setObject: theData forKey: @" kSecValueData" ];
311+ [attributesPlist removeObjectForKey: (id )kSecValueData ];
312+ }
313+
314+
227315 if ((secClass == kSecClassGenericPassword ) || (secClass == kSecClassGenericPassword )) {
228316 // Nothing to do for passwords
229317 }
230318 else if (secClass == kSecClassCertificate ) {
231319 if (CFDictionaryContainsKey (attributes, kSecValueRef )) {
232320 [attributesPlist setObject: [PlistObjectConverter convertSecCertificateRef: (SecCertificateRef)CFDictionaryGetValue (attributes, kSecValueRef )]
233- forKey: (id )kSecValueRef ];
321+ forKey: @" kSecValueRef" ];
322+ [attributesPlist removeObjectForKey: (id )kSecValueRef ];
234323 }
235324 else if (CFDictionaryContainsKey (attributes, kSecValuePersistentRef )) {
236325 [attributesPlist setObject: [PlistObjectConverter convertSecCertificateRef: (SecCertificateRef)CFDictionaryGetValue (attributes, kSecValuePersistentRef )]
237- forKey: (id )kSecValuePersistentRef ];
326+ forKey: @" kSecValuePersistentRef" ];
327+ [attributesPlist removeObjectForKey: (id )kSecValuePersistentRef ];
238328 }
239329 }
240330 else if (secClass == kSecClassIdentity ) {
241331 if (CFDictionaryContainsKey (attributes, kSecValueRef )) {
242- SecIdentityRef identity;
243- identity = (SecIdentityRef)CFDictionaryGetValue (attributes, kSecValueRef );
244332 [attributesPlist setObject: [PlistObjectConverter convertSecIdentityRef: (SecIdentityRef)CFDictionaryGetValue (attributes, kSecValueRef )]
245- forKey: (id )kSecValueRef ];
333+ forKey: @" kSecValueRef" ];
334+ [attributesPlist removeObjectForKey: (id )kSecValueRef ];
246335 }
247336 else if (CFDictionaryContainsKey (attributes, kSecValuePersistentRef )) {
248337 [attributesPlist setObject: [PlistObjectConverter convertSecIdentityRef: (SecIdentityRef)CFDictionaryGetValue (attributes, kSecValuePersistentRef )]
249- forKey: (id )kSecValuePersistentRef ];
338+ forKey: @" kSecValuePersistentRef" ];
339+ [attributesPlist removeObjectForKey: (id )kSecValuePersistentRef ];
250340 }
251341 }
252342 else if (secClass == kSecClassKey ) {
253343 if (CFDictionaryContainsKey (attributes, kSecValueRef )) {
254344 [attributesPlist setObject: [PlistObjectConverter convertSecKeyRef: (SecKeyRef)CFDictionaryGetValue (attributes, kSecValueRef )]
255- forKey: (id )kSecValueRef ];
345+ forKey: @" kSecValueRef" ];
346+ [attributesPlist removeObjectForKey: (id )kSecValueRef ];
256347 }
257348 else if (CFDictionaryContainsKey (attributes, kSecValuePersistentRef )) {
258349 [attributesPlist setObject: [PlistObjectConverter convertSecKeyRef: (SecKeyRef)CFDictionaryGetValue (attributes, kSecValuePersistentRef )]
259- forKey: (id )kSecValuePersistentRef ];
350+ forKey: @" kSecValuePersistentRef" ];
351+ [attributesPlist removeObjectForKey: (id )kSecValuePersistentRef ];
260352 }
261353 }
262354
@@ -298,7 +390,7 @@ + (NSDictionary *) convertSecKeyRef:(SecKeyRef) key {
298390 // TODO: Dump private keys
299391 NSDictionary *keyDict = [NSDictionary dictionaryWithObjects:
300392 [NSArray arrayWithObjects:
301- [ NSNumber numberWithUnsignedInt: ( unsigned int )key] ,
393+ objectTypeNotSupported ,
302394 nil ]
303395 forKeys:
304396 [NSArray arrayWithObjects:
0 commit comments