Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (C) 2010 Stephen F. Booth <me@sbooth.org>
- * All Rights Reserved
- */
- #include "CopyAppleRootCertificate.h"
- #import <Security/Security.h>
- CFDataRef CopyAppleRootCertificate()
- {
- SecKeychainRef keychain = NULL;
- OSStatus status = SecKeychainOpen("/System/Library/Keychains/SystemRootCertificates.keychain", &keychain);
- if(noErr != status) {
- if(NULL != keychain)
- CFRelease(keychain), keychain = NULL;
- return NULL;
- }
- CFArrayRef searchList = CFArrayCreate(kCFAllocatorDefault, (const void **)&keychain, 1, &kCFTypeArrayCallBacks);
- CFRelease(keychain), keychain = NULL;
- SecKeychainSearchRef searchRef = nil;
- status = SecKeychainSearchCreateFromAttributes(searchList, kSecCertificateItemClass, NULL, &searchRef);
- if(noErr != status) {
- if(NULL != searchRef)
- CFRelease(searchRef), searchRef = NULL;
- if(NULL != searchList)
- CFRelease(searchList), searchList = NULL;
- return NULL;
- }
- SecKeychainItemRef itemRef = NULL;
- CFDataRef resultData = NULL;
- while(noErr == SecKeychainSearchCopyNext(searchRef, &itemRef)) {
- // Grab the name of the certificate
- SecKeychainAttributeList list;
- SecKeychainAttribute attributes [1];
- attributes[0].tag = kSecLabelItemAttr;
- list.count = 1;
- list.attr = attributes;
- SecKeychainItemCopyContent(itemRef, NULL, &list, NULL, NULL);
- CFDataRef nameData = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, attributes[0].data, attributes[0].length, kCFAllocatorNull);
- CFStringRef name = CFStringCreateFromExternalRepresentation(kCFAllocatorDefault, nameData, kCFStringEncodingUTF8);
- if(kCFCompareEqualTo == CFStringCompare(name, CFSTR("Apple Root CA"), 0)) {
- CSSM_DATA certData;
- status = SecCertificateGetData((SecCertificateRef)itemRef, &certData);
- if(noErr != status) {
- if(NULL != itemRef)
- CFRelease(itemRef), itemRef = NULL;
- }
- resultData = CFDataCreate(kCFAllocatorDefault, certData.Data, certData.Length);
- SecKeychainItemFreeContent(&list, NULL);
- if(NULL != itemRef)
- CFRelease(itemRef), itemRef = NULL;
- break;
- }
- CFRelease(name), name = NULL;
- }
- CFRelease(searchList), searchList = NULL;
- CFRelease(searchRef), searchRef = NULL;
- return resultData;
- }
Add Comment
Please, Sign In to add comment