SOSKeyedPubKeyIdentifier.c [plain text]
#include "SOSKeyedPubKeyIdentifier.h"
#include "AssertMacros.h"
#include "keychain/SecureObjectSync/SOSInternal.h"
#include <utilities/debugging.h>
#define SEPARATOR CFSTR("-")
#define SEPLOC 2
bool SOSKeyedPubKeyIdentifierIsPrefixed(CFStringRef kpkid) {
CFRange seploc = CFStringFind(kpkid, SEPARATOR, 0);
return seploc.location == SEPLOC;
}
static CFStringRef SOSKeyedPubKeyIdentifierCreateWithPrefixAndID(CFStringRef prefix, CFStringRef id) {
CFMutableStringRef retval = NULL;
require_quiet(prefix, errOut);
require_quiet(id, errOut);
require_quiet(CFStringGetLength(prefix) == SEPLOC, errOut);
retval = CFStringCreateMutableCopy(kCFAllocatorDefault, 50, prefix);
CFStringAppend(retval, SEPARATOR);
CFStringAppend(retval, id);
errOut:
return retval;
}
CFStringRef SOSKeyedPubKeyIdentifierCreateWithData(CFStringRef prefix, CFDataRef pubKeyData) {
CFErrorRef localError = NULL;
CFStringRef id = SOSCopyIDOfDataBuffer(pubKeyData, &localError);
CFStringRef retval = SOSKeyedPubKeyIdentifierCreateWithPrefixAndID(prefix, id);
if(!id) secnotice("kpid", "Couldn't create kpid: %@", localError);
CFReleaseNull(id);
CFReleaseNull(localError);
return retval;
}
CFStringRef SOSKeyedPubKeyIdentifierCreateWithSecKey(CFStringRef prefix, SecKeyRef pubKey) {
CFErrorRef localError = NULL;
CFStringRef id = SOSCopyIDOfKey(pubKey, &localError);
CFStringRef retval = SOSKeyedPubKeyIdentifierCreateWithPrefixAndID(prefix, id);
if(!id) secnotice("kpid", "Couldn't create kpid: %@", localError);
CFReleaseNull(id);
CFReleaseNull(localError);
return retval;
}
CFStringRef SOSKeyedPubKeyIdentifierCopyPrefix(CFStringRef kpkid) {
CFRange seploc = CFStringFind(kpkid, SEPARATOR, 0);
if(seploc.location != SEPLOC) return NULL;
CFRange prefloc = CFRangeMake(0, SEPLOC);
return CFStringCreateWithSubstring(kCFAllocatorDefault, kpkid, prefloc);;
}
CFStringRef SOSKeyedPubKeyIdentifierCopyHpub(CFStringRef kpkid) {
CFRange seploc = CFStringFind(kpkid, SEPARATOR, 0);
if(seploc.location != SEPLOC) return NULL;
CFRange idloc = CFRangeMake(seploc.location+1, CFStringGetLength(kpkid) - (SEPLOC+1));
return CFStringCreateWithSubstring(kCFAllocatorDefault, kpkid, idloc);;
}