kc-identity-helpers.h [plain text]
#include "kc-helpers.h"
#include <Security/Security.h>
#ifndef kc_identity_helpers_h
#define kc_identity_helpers_h
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
#pragma clang diagnostic ignored "-Wunused-function"
static SecIdentityRef
copyFirstIdentity(SecKeychainRef kc)
{
CFMutableDictionaryRef query = CFDictionaryCreateMutable(NULL, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFDictionaryAddValue( query, kSecClass, kSecClassIdentity );
CFDictionaryAddValue( query, kSecMatchLimit, kSecMatchLimitAll );
CFDictionaryAddValue( query, kSecReturnRef, kCFBooleanTrue );
CFMutableArrayRef searchList = (CFMutableArrayRef) CFArrayCreateMutable(kCFAllocatorDefault, 1, &kCFTypeArrayCallBacks);
CFArrayAppendValue((CFMutableArrayRef)searchList, kc);
CFDictionarySetValue(query, kSecMatchSearchList, searchList);
CFTypeRef results = NULL;
OSStatus status = SecItemCopyMatching(query, &results);
ok_status(status, "%s: SecItemCopyMatching", testName);
CFRelease(query);
if (status) {
return NULL;
}
if (results) {
CFArrayRef resultArray = (CFArrayRef)results;
SecIdentityRef identity = (SecIdentityRef)CFArrayGetValueAtIndex(resultArray, 0);
CFRetain(identity); CFRelease(results);
return identity;
}
return NULL;
}
#define copyFirstIdentityTests 1
static SecIdentityRef
findIdentity(SecKeychainRef keychain, SecCertificateRef cert)
{
OSStatus status = noErr;
SecIdentitySearchRef searchRef = NULL;
CSSM_DATA certData = { 0, NULL };
SecIdentityRef outIdentity = NULL;
if (!keychain || !cert) {
return NULL;
}
status = SecCertificateGetData(cert, &certData);
ok_status(status, "%s: findIdentity: SecCertificateGetData", testName);
if (status) {
return NULL;
}
status = SecIdentitySearchCreate(keychain, (CSSM_KEYUSE)0, &searchRef);
while (!status) {
SecIdentityRef identityRef = NULL;
status = SecIdentitySearchCopyNext(searchRef, &identityRef);
if (!status) {
SecCertificateRef aCert = NULL;
status = SecIdentityCopyCertificate(identityRef, &aCert);
if (!status) {
CSSM_DATA aCertData = { 0, NULL };
status = SecCertificateGetData(aCert, &aCertData);
if (!status) {
if (aCertData.Length == certData.Length &&
!memcmp(aCertData.Data, certData.Data, certData.Length)) {
CFRelease(aCert);
outIdentity = identityRef;
break;
}
}
}
if (aCert) {
CFRelease(aCert);
}
}
if (identityRef) {
CFRelease(identityRef);
}
}
ok(outIdentity, "%s: findIdentity: found an identity", testName);
if (searchRef) {
CFRelease(searchRef);
}
return outIdentity;
}
#define findIdentityTests 2
#pragma clang diagnostic pop
#endif