5#include <CoreFoundation/CoreFoundation.h>
6#include <Security/Security.h>
20 (CFReadStreamRef inputStream);
37#define MAC_OS_X_VERSION_10_7_HEX 0x00001070
44 SInt32 major = 0, minor = 0;
47 CFURLCreateWithString(kCFAllocatorDefault,
48 CFSTR(
"file:///System/Library/CoreServices/SystemVersion.plist"),
51 CFReadStreamCreateWithFile(kCFAllocatorDefault, url);
53 CFDictionaryRef sysVersionPlist = (CFDictionaryRef)
54 CFPropertyListCreateWithStream(kCFAllocatorDefault,
55 stream, 0, kCFPropertyListImmutable,
61 CFStringRef versionString = (CFStringRef)
62 CFDictionaryGetValue(sysVersionPlist, CFSTR(
"ProductVersion"));
64 CFStringCreateArrayBySeparatingStrings(kCFAllocatorDefault,
65 versionString, CFSTR(
"."));
66 CFIndex
count = CFArrayGetCount(versions);
68 CFStringRef component = (CFStringRef) CFArrayGetValueAtIndex(versions, 0);
69 major = CFStringGetIntValue(component);
71 component = (CFStringRef) CFArrayGetValueAtIndex(versions, 1);
72 minor = CFStringGetIntValue(component);
75 CFRelease(sysVersionPlist);
81 int version = 0x1000 + (minor << 4);
92 { 0x9243121f, 0x5820, 0x4b41,
93 { 0xa6, 0x52, 0xba, 0xb6, 0x3f, 0x9d, 0x3d, 0x7f }};
109 return realloc(aPtr, aSize);
112void*
cssmCalloc (uint32 aNum, CSSM_SIZE aSize,
void* aAllocRef) {
114 return calloc(aNum, aSize);
135 dlsym(RTLD_DEFAULT,
"SecTransformCreateReadTransformWithReadStream");
139 dlsym(RTLD_DEFAULT,
"SecTransformExecute");
143 dlsym(RTLD_DEFAULT,
"SecVerifyTransformCreate");
147 dlsym(RTLD_DEFAULT,
"SecTransformSetAttribute");
165 void* inputData = CFDataCreateMutable(kCFAllocatorDefault, 0);
171 CSSM_DATA_PTR cssmData = (CSSM_DATA_PTR)malloc(
sizeof(CSSM_DATA));
173 CFRelease(inputData);
176 cssmData->Data = (uint8*)inputData;
177 cssmData->Length = 0;
178 *aInputData = cssmData;
182 *aInputData = inputData;
193 if (!aInputData || !*aInputData) {
197 CFMutableDataRef inputData;
199 inputData = (CFMutableDataRef)((CSSM_DATA_PTR)*aInputData)->
Data;
200 ((CSSM_DATA_PTR)*aInputData)->Length += aLen;
202 inputData = (CFMutableDataRef)*aInputData;
205 CFDataAppendBytes(inputData, (
const uint8*)
aBuf, aLen);
211 unsigned int aDataSize,
214 if (!aCertData || aDataSize == 0 || !aPublicKey) {
223 CSSM_PVC_MODE pvcPolicy = CSSM_PVC_NONE;
225 CSSM_PRIVILEGE_SCOPE_PROCESS,
227 CSSM_KEY_HIERARCHY_NONE,
236 rv = CSSM_ModuleLoad(&gGuidAppleCSP,
237 CSSM_KEY_HIERARCHY_NONE,
244 CSSM_CSP_HANDLE cspHandle;
245 rv = CSSM_ModuleAttach(&gGuidAppleCSP,
251 CSSM_KEY_HIERARCHY_NONE,
263 CFDataRef certData = CFDataCreate(kCFAllocatorDefault,
270 SecCertificateRef cert = SecCertificateCreateWithData(kCFAllocatorDefault,
277 OSStatus status = SecCertificateCopyPublicKey(cert,
290 const unsigned char* aSignature,
291 unsigned int aSignatureLen)
293 if (!aInputData || !*aInputData || !aPublicKey || !*aPublicKey ||
294 !aSignature || aSignatureLen == 0) {
304 OSStatus status = SecKeyGetCSSMKey((
SecKeyRef)*aPublicKey,
305 (
const CSSM_KEY**)&publicKey);
310 CSSM_CC_HANDLE ccHandle;
311 if (CSSM_CSP_CreateSignatureContext(
sCspHandle,
312 CSSM_ALGID_SHA1WithRSA,
315 &ccHandle) != CSSM_OK) {
320 CSSM_DATA signatureData;
321 signatureData.Data = (uint8*)aSignature;
322 signatureData.Length = aSignatureLen;
325 CFDataGetMutableBytePtr((CFMutableDataRef)
326 (((CSSM_DATA_PTR)*aInputData)->
Data));
327 inputData.Length = ((CSSM_DATA_PTR)*aInputData)->Length;
328 if (CSSM_VerifyData(ccHandle,
332 &signatureData) == CSSM_OK) {
338 CFDataRef signatureData = CFDataCreate(kCFAllocatorDefault,
339 aSignature, aSignatureLen);
340 if (!signatureData) {
349 if (!verifier || error) {
350 CFRelease(signatureData);
356 (CFDataRef)*aInputData,
359 CFRelease(signatureData);
367 CFRelease(signatureData);
372 if (CFGetTypeID(rv) == CFBooleanGetTypeID() &&
373 CFBooleanGetValue((CFBooleanRef)rv) ==
true) {
377 CFRelease(signatureData);
386 if (!aInputData || !*aInputData) {
390 CFMutableDataRef inputData =
NULL;
392 inputData = (CFMutableDataRef)*aInputData;
394 inputData = (CFMutableDataRef)((CSSM_DATA_PTR)*aInputData)->
Data;
397 CFRelease(inputData);
399 free((CSSM_DATA_PTR)*aInputData);
406 if (!aPublicKey || !*aPublicKey) {
CryptoX_Result CryptoMac_LoadPublicKey(const unsigned char *aCertData, unsigned int aDataSize, CryptoX_PublicKey *aPublicKey)
SecVerifyTransformCreateFunc SecVerifyTransformCreatePtr
static CSSM_API_MEMORY_FUNCS cssmMemFuncs
SecTransformRef(* SecTransformCreateReadTransformWithReadStreamFunc)(CFReadStreamRef inputStream)
struct OpaqueSecKeyRef * SecKeyRef
SecTransformCreateReadTransformWithReadStreamFunc SecTransformCreateReadTransformWithReadStreamPtr
void CryptoMac_FreePublicKey(CryptoX_PublicKey *aPublicKey)
static const CSSM_GUID sMozCssmGuid
const CFStringRef kSecTransformInputAttributeName
static int sOnLionOrLater
CFTypeRef(* SecTransformExecuteFunc)(SecTransformRef transform, CFErrorRef *error)
static bool OnLionOrLater()
static CSSM_CSP_HANDLE sCspHandle
SecTransformRef(* SecVerifyTransformCreateFunc)(SecKeyRef key, CFDataRef signature, CFErrorRef *error)
CryptoX_Result CryptoMac_VerifyUpdate(CryptoX_SignatureHandle *aInputData, void *aBuf, unsigned int aLen)
static CSSM_VERSION sCssmVersion
void * cssmRealloc(void *aPtr, CSSM_SIZE aSize, void *aAllocRef)
void CryptoMac_FreeSignatureHandle(CryptoX_SignatureHandle *aInputData)
SecTransformExecuteFunc SecTransformExecutePtr
CryptoX_Result CryptoMac_InitCryptoProvider()
static bool sCssmInitialized
CFTypeRef SecTransformRef
#define MAC_OS_X_VERSION_10_7_HEX
void cssmFree(void *aPtr, void *aAllocRef)
CryptoX_Result CryptoMac_VerifySignature(CryptoX_SignatureHandle *aInputData, CryptoX_PublicKey *aPublicKey, const unsigned char *aSignature, unsigned int aSignatureLen)
void * cssmMalloc(CSSM_SIZE aSize, void *aAllocRef)
SecTransformSetAttributeFunc SecTransformSetAttributePtr
CryptoX_Result CryptoMac_VerifyBegin(CryptoX_SignatureHandle *aInputData)
Boolean(* SecTransformSetAttributeFunc)(SecTransformRef transform, CFStringRef key, CFTypeRef value, CFErrorRef *error)
void * cssmCalloc(uint32 aNum, CSSM_SIZE aSize, void *aAllocRef)
#define CryptoX_PublicKey
#define CryptoX_SignatureHandle
Reference< XOutputStream > stream