/*
 *
 * SecCspHandleForAlgorithm
 * @@@ This function should get more parameters like keysize and operation required and use mds.
 *
 */
CSSM_CSP_HANDLE
SecCspHandleForAlgorithm(CSSM_ALGORITHMS algorithm)
{

    if (!gCsp)
    {
	CSSM_VERSION version = { 2, 0 };
	CSSM_RETURN rv;

	if (!gCssmInitialized)
	{
	    CSSM_GUID myGuid = { 0xFADE, 0, 0, { 1, 2, 3, 4, 5, 6, 7, 0 } };
	    CSSM_PVC_MODE pvcPolicy = CSSM_PVC_NONE;
    
	    rv = CSSM_Init (&version, CSSM_PRIVILEGE_SCOPE_NONE, &myGuid, CSSM_KEY_HIERARCHY_NONE, &pvcPolicy, NULL);
	    if (rv)
		goto loser;
	    gCssmInitialized = 1;
	}

	rv = CSSM_ModuleLoad(&gGuidAppleCSP, CSSM_KEY_HIERARCHY_NONE, NULL, NULL);
	if (rv)
	    goto loser;
	rv = CSSM_ModuleAttach(&gGuidAppleCSP, &version, &memFuncs, 0, CSSM_SERVICE_CSP, 0, CSSM_KEY_HIERARCHY_NONE, NULL, 0, NULL, &gCsp);
    }

loser:
    return gCsp;
}
CSSM_TP_HANDLE cuTpStartup()
{
	CSSM_TP_HANDLE tpHand;
	CSSM_RETURN crtn;
	
	if(cuCssmStartup() == CSSM_FALSE) {
		return 0;
	}
	crtn = CSSM_ModuleLoad(&gGuidAppleX509TP,
		CSSM_KEY_HIERARCHY_NONE,
		NULL,			// eventHandler
		NULL);			// AppNotifyCallbackCtx
	if(crtn) {
		cuPrintError("CSSM_ModuleLoad(AppleTP)", crtn);
		return 0;
	}
	crtn = CSSM_ModuleAttach (&gGuidAppleX509TP,
		&vers,
		&memFuncs,				// memFuncs
		0,						// SubserviceID
		CSSM_SERVICE_TP,		// SubserviceFlags
		0,						// AttachFlags
		CSSM_KEY_HIERARCHY_NONE,
		NULL,					// FunctionTable
		0,						// NumFuncTable
		NULL,					// reserved
		&tpHand);
	if(crtn) {
		cuPrintError("CSSM_ModuleAttach(AppleTP)", crtn);
		return 0;
	}
	else {
		return tpHand;
	}
}
Exemple #3
0
CSSM_CL_HANDLE pkiClStartup(void)
{
    CSSM_CL_HANDLE clHand;
    CSSM_RETURN crtn;

    if(cuCssmStartup() == CSSM_FALSE) {
        return 0;
    }
    crtn = CSSM_ModuleLoad(&gGuidAppleX509CL,
                           CSSM_KEY_HIERARCHY_NONE,
                           NULL,                   /* eventHandler */
                           NULL);                  /* AppNotifyCallbackCtx */
    if(crtn) {
        return 0;
    }
    crtn = CSSM_ModuleAttach (&gGuidAppleX509CL,
                              &vers,
                              &memFuncs,                  /* memFuncs */
                              0,                          /* SubserviceID */
                              CSSM_SERVICE_CL,            /* SubserviceFlags - Where is this used? */
                              0,                          /* AttachFlags */
                              CSSM_KEY_HIERARCHY_NONE,
                              NULL,                       /* FunctionTable */
                              0,                          /* NumFuncTable */
                              NULL,                       /* reserved */
                              &clHand);
    if(crtn) {
        return 0;
    }
    else {
        return clHand;
    }
}
void DotMacRelation::InitializeCertLibrary ()
{
	if (mCertificateLibrary != 0)
	{
		return;
	}
	
	// figure out which GUID to attach to
	const CSSM_GUID* attachGuid = &gGuidAppleX509CL;
	
	// Initialize CDSA
	CSSM_VERSION version = {2, 0};

	// load the CL
	CSSM_RETURN result = CSSM_ModuleLoad (attachGuid, CSSM_KEY_HIERARCHY_NONE, NULL, NULL);
	CheckResult (result);

	result = CSSM_ModuleAttach (attachGuid,
								&version,
								&memFuncs,
								0,					// no subservice ID
								CSSM_SERVICE_CL,	
								0,
								CSSM_KEY_HIERARCHY_NONE,
								NULL,
								0,
								NULL,
								&mCertificateLibrary);
	CheckResult (result);
}
/* Attach to DL side of CSPDL */
CSSM_DL_HANDLE cuDlStartup()
{
	CSSM_DL_HANDLE 	dlHand = 0;
	CSSM_RETURN		crtn;
	
	if(cuCssmStartup() == CSSM_FALSE) {
		return 0;
	}
	crtn = CSSM_ModuleLoad(&gGuidAppleCSPDL,
		CSSM_KEY_HIERARCHY_NONE,
		NULL,			// eventHandler
		NULL);			// AppNotifyCallbackCtx
	if(crtn) {
		cuPrintError("CSSM_ModuleLoad(Apple CSPDL)", crtn);
		return 0;
	}
	crtn = CSSM_ModuleAttach (&gGuidAppleCSPDL,
		&vers,
		&memFuncs,			// memFuncs
		0,					// SubserviceID
		CSSM_SERVICE_DL,	
		0,					// AttachFlags
		CSSM_KEY_HIERARCHY_NONE,
		NULL,				// FunctionTable
		0,					// NumFuncTable
		NULL,				// reserved
		&dlHand);
	if(crtn) {
		cuPrintError("CSSM_ModuleAttach(Apple CSPDL)", crtn);
		return 0;
	}
	return dlHand;
}
/*
 * Init CSSM and establish a session with the Apple CL.
 */
CSSM_CL_HANDLE clStartup()
{
	CSSM_CL_HANDLE clHand;
	CSSM_RETURN crtn;
	
	if(cssmStartup() == CSSM_FALSE) {
		return 0;
	}
	crtn = CSSM_ModuleLoad(&gGuidAppleX509CL,
		CSSM_KEY_HIERARCHY_NONE,
		NULL,			// eventHandler
		NULL);			// AppNotifyCallbackCtx
	if(crtn) {
		printError("CSSM_ModuleLoad(AppleCL)", crtn);
		return 0;
	}
	crtn = CSSM_ModuleAttach (&gGuidAppleX509CL,
		&vers,
		&memFuncs,				// memFuncs
		0,						// SubserviceID
		CSSM_SERVICE_CL,		// SubserviceFlags - Where is this used?
		0,						// AttachFlags
		CSSM_KEY_HIERARCHY_NONE,
		NULL,					// FunctionTable
		0,						// NumFuncTable
		NULL,					// reserved
		&clHand);
	if(crtn) {
		printError("CSSM_ModuleAttach(AppleCL)", crtn);
		return 0;
	}
	else {
		return clHand;
	}
}
CSSM_CSP_HANDLE
_hc_get_cdsa_csphandle(void)
{
    CSSM_PVC_MODE pvcPolicy = CSSM_PVC_NONE;
    CSSM_RETURN ret;

    if (cspHandle)
        return cspHandle;
        
    ret = CSSM_Init(&vers, CSSM_PRIVILEGE_SCOPE_NONE,
                    &guid, CSSM_KEY_HIERARCHY_NONE,
                    &pvcPolicy, NULL);
    if (ret != CSSM_OK) 
        abort();

    ret = CSSM_ModuleLoad(&gGuidAppleCSP, CSSM_KEY_HIERARCHY_NONE, NULL, NULL);
    if (ret)
        abort();

    ret = CSSM_ModuleAttach(&gGuidAppleCSP, &vers, &cssm_memory_funcs,
                            0, CSSM_SERVICE_CSP, 0,
                            CSSM_KEY_HIERARCHY_NONE,
                            NULL, 0, NULL, &cspHandle);
    if (ret)
        abort();

    return cspHandle;
}
CSSM_CSP_HANDLE CSPAttacher::getCspHand(bool bareCsp)
{
	const char 		*modName;
	CSSM_RETURN		crtn;
	const CSSM_GUID *guid;
	CSSM_VERSION 	vers = {2, 0};
	StLock<Mutex> 	_(mLock);
	CSSM_CSP_HANDLE	cspHand;
	
	if(bareCsp) {
		if(mCspHand != CSSM_INVALID_HANDLE) {
			/* already connected */
			return mCspHand;
		}	
		guid = &gGuidAppleCSP;
		modName = "AppleCSP";
	}
	else {
		if(mCspDlHand != CSSM_INVALID_HANDLE) {
			/* already connected */
			return mCspDlHand;
		}	
		guid = &gGuidAppleCSPDL;
		modName = "AppleCSPDL";
	}
	crtn = CSSM_ModuleLoad(guid,
		CSSM_KEY_HIERARCHY_NONE,
		NULL,			// eventHandler
		NULL);			// AppNotifyCallbackCtx
	if(crtn) {
		clErrorLog("AppleX509CLSession::cspAttach: error (%d) loading %s",
			(int)crtn, modName);
		CssmError::throwMe(crtn);
	}
	crtn = CSSM_ModuleAttach (guid,
		&vers,
		&CL_memFuncs,			// memFuncs
		0,						// SubserviceID
		CSSM_SERVICE_CSP,		// SubserviceFlags 
		0,						// AttachFlags
		CSSM_KEY_HIERARCHY_NONE,
		NULL,					// FunctionTable
		0,						// NumFuncTable
		NULL,					// reserved
		&cspHand);
	if(crtn) {
		clErrorLog("AppleX509CLSession::cspAttach: error (%d) attaching to %s",
			(int)crtn, modName);
		CssmError::throwMe(crtn);
	}
	if(bareCsp) {
		mCspHand = cspHand;
	}
	else {
		mCspDlHand = cspHand;
	}
	return cspHand;
}
CSSM_CSP_HANDLE cspDlDbStartup(
	CSSM_BOOL bareCsp,			// true ==> CSP, false ==> CSP/DL
	CSSM_DB_HANDLE *dbHandPtr)	// optional - TO BE DELETED
{
	CSSM_CSP_HANDLE cspHand;
	CSSM_RETURN		crtn;
	const CSSM_GUID *guid;
	char *modName;
	
	if(dbHandPtr) {
		*dbHandPtr = 0;
	}
	if(cssmStartup() == CSSM_FALSE) {
		return 0;
	}
	if(bareCsp) {
		guid = &gGuidAppleCSP;
		modName = (char*) "AppleCSP";
	}
	else {
		guid = &gGuidAppleCSPDL;
		modName = (char *) "AppleCSPDL";
	}
	crtn = CSSM_ModuleLoad(guid,
		CSSM_KEY_HIERARCHY_NONE,
		NULL,			// eventHandler
		NULL);			// AppNotifyCallbackCtx
	if(crtn) {
		char outStr[100];
		sprintf(outStr, "CSSM_ModuleLoad(%s)", modName);
		printError(outStr, crtn);
		return 0;
	}
	crtn = CSSM_ModuleAttach (guid,
		&vers,
		&memFuncs,			// memFuncs
		0,					// SubserviceID
		CSSM_SERVICE_CSP,	
		0,					// AttachFlags
		CSSM_KEY_HIERARCHY_NONE,
		NULL,				// FunctionTable
		0,					// NumFuncTable
		NULL,				// reserved
		&cspHand);
	if(crtn) {
		char outStr[100];
		sprintf(outStr, "CSSM_ModuleAttach(%s)", modName);
		printError(outStr, crtn);
		return 0;
	}
	return cspHand;
}
Exemple #10
0
CSSM_RETURN cdsaInit(void) {
	if (cdsaInitialized)
		return CSSM_OK;

	CSSM_RETURN crtn;
	CSSM_PVC_MODE pvcPolicy = CSSM_PVC_NONE;

	/* Initialize CSSM. */
	crtn = CSSM_Init(&cspversion,
					 CSSM_PRIVILEGE_SCOPE_PROCESS,
					 &guid,
					 CSSM_KEY_HIERARCHY_NONE,
					 &pvcPolicy,
					 /*reserved*/NULL);
	if (crtn)
		return crtn;

	/* Load the CSP bundle into this app's memory space */
	crtn = CSSM_ModuleLoad(&gGuidAppleCSP,
						   CSSM_KEY_HIERARCHY_NONE,
						   NULL,      // eventHandler
						   NULL);     // AppNotifyCallbackCtx
	if (crtn)
		return crtn;

	/* Obtain a handle which will be used to refer to the CSP */
	crtn = CSSM_ModuleAttach(&gGuidAppleCSP,
							 &cspversion,
							 &memFuncs,      // memFuncs
							 0,              // SubserviceID
							 CSSM_SERVICE_CSP,
							 0,              // AttachFlags
							 CSSM_KEY_HIERARCHY_NONE,
							 NULL,           // FunctionTable
							 0,              // NumFuncTable
							 NULL,           // reserved
							 &cspHandle);
	if (crtn)
		return crtn;

	cdsaInitialized = 1;

	return CSSM_OK;
}
/*
 * Attach to CSP. Returns zero on error.
 */
CSSM_CSP_HANDLE cuCspStartup(
	CSSM_BOOL bareCsp)		// true ==> CSP, false ==> CSP/DL
{
	CSSM_CSP_HANDLE cspHand;
	CSSM_RETURN		crtn;
	const CSSM_GUID *guid;
	
	/* common CSSM init */
	if(cuCssmStartup() == CSSM_FALSE) {
		return 0;
	}
	if(bareCsp) {
		guid = &gGuidAppleCSP;
	}
	else {
		guid = &gGuidAppleCSPDL;
	}
	crtn = CSSM_ModuleLoad(guid,
		CSSM_KEY_HIERARCHY_NONE,
		NULL,			// eventHandler
		NULL);			// AppNotifyCallbackCtx
	if(crtn) {
		cuPrintError("CSSM_ModuleLoad()", crtn);
		return 0;
	}
	crtn = CSSM_ModuleAttach (guid,
		&vers,
		&memFuncs,			// memFuncs
		0,					// SubserviceID
		CSSM_SERVICE_CSP,	
		0,					// AttachFlags
		CSSM_KEY_HIERARCHY_NONE,
		NULL,				// FunctionTable
		0,					// NumFuncTable
		NULL,				// reserved
		&cspHand);
	if(crtn) {
		cuPrintError("CSSM_ModuleAttach()", crtn);
		return 0;
	}
	return cspHand;
}
Exemple #12
0
/*
 * Initialize CDSA and attach to the CSP.
 */
CSSM_RETURN cdsaCspAttach(
	CSSM_CSP_HANDLE		*cspHandle)
{
	CSSM_CSP_HANDLE cspHand;
	CSSM_RETURN		crtn;
	
	/* initialize CDSA (this is reusable) */
	crtn = cssmStartup();
	if(crtn) {
		return crtn;
	}
	
	/* Load the CSP bundle into this app's memory space */
	crtn = CSSM_ModuleLoad(&gGuidAppleCSP,
		CSSM_KEY_HIERARCHY_NONE,
		NULL,			// eventHandler
		NULL);			// AppNotifyCallbackCtx
	if(crtn) {
		return crtn;
	}
	
	/* obtain a handle which will be used to refer to the CSP */ 
	crtn = CSSM_ModuleAttach (&gGuidAppleCSP,
		&vers,
		&memFuncs,			// memFuncs
		0,					// SubserviceID
		CSSM_SERVICE_CSP,	
		0,					// AttachFlags
		CSSM_KEY_HIERARCHY_NONE,
		NULL,				// FunctionTable
		0,					// NumFuncTable
		NULL,				// reserved
		&cspHand);
	if(crtn) {
		return crtn;
	}
	*cspHandle = cspHand;
	return CSSM_OK;
}
CryptoX_Result
CryptoMac_LoadPublicKey(const unsigned char* aCertData,
                        CryptoX_PublicKey* aPublicKey)
{
  if (!aCertData || !aPublicKey) {
    return CryptoX_Error;
  }
  *aPublicKey = NULL;

  if (!OnLionOrLater()) {
    if (!sCspHandle) {
      CSSM_RETURN rv;
      if (!sCssmInitialized) {
        CSSM_PVC_MODE pvcPolicy = CSSM_PVC_NONE;
        rv = CSSM_Init(&sCssmVersion,
                       CSSM_PRIVILEGE_SCOPE_PROCESS,
                       &sMozCssmGuid,
                       CSSM_KEY_HIERARCHY_NONE,
                       &pvcPolicy,
                       NULL);
        if (rv != CSSM_OK) {
          return CryptoX_Error;
        }
        sCssmInitialized = true;
      }

      rv = CSSM_ModuleLoad(&gGuidAppleCSP,
                           CSSM_KEY_HIERARCHY_NONE,
                           NULL,
                           NULL);
      if (rv != CSSM_OK) {
        return CryptoX_Error;
      }

      CSSM_CSP_HANDLE cspHandle;
      rv = CSSM_ModuleAttach(&gGuidAppleCSP,
                             &sCssmVersion,
                             &cssmMemFuncs,
                             0,
                             CSSM_SERVICE_CSP,
                             0,
                             CSSM_KEY_HIERARCHY_NONE,
                             NULL,
                             0,
                             NULL,
                             &cspHandle);
      if (rv != CSSM_OK) {
        return CryptoX_Error;
      }
      sCspHandle = cspHandle;
    }

    FILE* certFile = NULL;
    long certFileSize = 0;
    uint8* certBuffer = NULL;

    certFile = fopen((char*)aCertData, "rb");
    if (!certFile) {
      return CryptoX_Error;
    }
    if (fseek(certFile, 0, SEEK_END)) {
      fclose(certFile);
      return CryptoX_Error;
    }
    certFileSize = ftell(certFile);
    if (certFileSize < 0) {
      fclose(certFile);
      return CryptoX_Error;
    }
    certBuffer = (uint8*)malloc(certFileSize);
    if (fseek(certFile, 0, SEEK_SET)) {
      free(certBuffer);
      fclose(certFile);
      return CryptoX_Error;
    }
    uint readResult = fread(certBuffer, sizeof(uint8), certFileSize, certFile);
    if (readResult != certFileSize) {
      free(certBuffer);
      fclose(certFile);
      return CryptoX_Error;
    }
    fclose(certFile);

    CFDataRef certData = CFDataCreate(kCFAllocatorDefault,
                                      certBuffer,
                                      certFileSize);
    free(certBuffer);
    if (!certData) {
      return CryptoX_Error;
    }

    SecCertificateRef cert = SecCertificateCreateWithData(kCFAllocatorDefault,
                                                          certData);
    CFRelease(certData);
    if (!cert) {
      return CryptoX_Error;
    }

    SecKeyRef publicKey;
    OSStatus status = SecCertificateCopyPublicKey(cert, (SecKeyRef*)&publicKey);
    CFRelease(cert);
    if (status) {
      return CryptoX_Error;
    }

    *aPublicKey = (void*)publicKey;
    return CryptoX_Success;
  }

  CFURLRef url =
    CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault,
                                            aCertData,
                                            strlen((char*)aCertData),
                                            false);
  if (!url) {
    return CryptoX_Error;
  }

  CFReadStreamRef stream = CFReadStreamCreateWithFile(kCFAllocatorDefault, url);
  if (!stream) {
    CFRelease(url);
    return CryptoX_Error;
  }

  SecTransformRef readTransform =
    SecTransformCreateReadTransformWithReadStreamPtr(stream);
  if (!readTransform) {
    CFRelease(url);
    CFRelease(stream);
    return CryptoX_Error;
  }

  CFErrorRef error;
  CFDataRef tempCertData = (CFDataRef)SecTransformExecutePtr(readTransform,
                                                             &error);
  if (!tempCertData || error) {
    CFRelease(url);
    CFRelease(stream);
    CFRelease(readTransform);
    return CryptoX_Error;
  }

  SecCertificateRef cert = SecCertificateCreateWithData(kCFAllocatorDefault,
                                                        tempCertData);
  if (!cert) {
    CFRelease(url);
    CFRelease(stream);
    CFRelease(readTransform);
    CFRelease(tempCertData);
    return CryptoX_Error;
  }

  CryptoX_Result result = CryptoX_Error;
  OSStatus status = SecCertificateCopyPublicKey(cert,
                                                (SecKeyRef*)aPublicKey);
  if (status == 0) {
    result = CryptoX_Success;
  }

  CFRelease(url);
  CFRelease(stream);
  CFRelease(readTransform);
  CFRelease(tempCertData);
  CFRelease(cert);

  return result;
}
int main(int argc, char **argv)
{
	int					arg;
	char				*argp;
	CSSM_HANDLE 		modHand = 0;
	void 				*foo;
	CSSM_SERVICE_TYPE	svcType = CSSM_SERVICE_CSP;
	const CSSM_GUID		*guid = &gGuidAppleCSP;
	const char			*modName = "AppleCSP";
	CSSM_RETURN			crtn;
	CSSM_BOOL			doLoad = CSSM_FALSE;
	CSSM_BOOL			doUnload = CSSM_FALSE;
	
	/* force link against malloc */
	foo = malloc(1);
	for(arg=1; arg<argc; arg++) {
		argp = argv[arg];
		switch(argp[0]) {
			case 'l':
				doLoad = CSSM_TRUE;
				break;
			case 'u':
				doLoad = doUnload = CSSM_TRUE;
				break;
			case 'd':
				guid = &gGuidAppleCSPDL;
				modName = "AppleCSPDL";
				break;
			case 'c':
				guid = &gGuidAppleX509CL;
				svcType = CSSM_SERVICE_CL;
				modName = "AppleX509CL";
				break;
			case 't':
				guid = &gGuidAppleX509TP;
				svcType = CSSM_SERVICE_TP;
				modName = "AppleX509TP";
				break;
		    case 'h':
		    default:
				usage(argv);
		}
	}
	
	if(doPause("Top of test")) {
		goto done;
	}
	
	/* CSSM init, just once */
	if(!cssmStartup()) {
		printf("Oops, error starting up CSSM\n");
		exit(1);
	}
	if(doPause("CSSM initialized")) {
		goto done;
	}
		
	
	if(!doLoad) {
		/* load, just once */
		crtn = CSSM_ModuleLoad(guid,
			CSSM_KEY_HIERARCHY_NONE,
			NULL,			// eventHandler
			NULL);			// AppNotifyCallbackCtx
		if(crtn) {
			printf("Error loading %s\n", modName);
			printError("CSSM_ModuleLoad", crtn);
			return 0;
		}
		if(doPause("CSSM_ModuleLoad() complete")) {
			goto done;
		}
	}
	while(1) {
		if(doLoad) {
			/* load, each time */
			crtn = CSSM_ModuleLoad(guid,
				CSSM_KEY_HIERARCHY_NONE,
				NULL,			// eventHandler
				NULL);			// AppNotifyCallbackCtx
			if(crtn) {
				printf("Error loading %s\n", modName);
				printError("CSSM_ModuleLoad", crtn);
				return 0;
			}
			if(doPause("CSSM_ModuleLoad() complete")) {
				break;
			}
		}
		crtn = CSSM_ModuleAttach (guid,
			&vers,
			&memFuncs,				// memFuncs
			0,						// SubserviceID
			svcType,			
			0,						// AttachFlags
			CSSM_KEY_HIERARCHY_NONE,
			NULL,					// FunctionTable
			0,						// NumFuncTable
			NULL,					// reserved
			&modHand);
		if(crtn) {
			printf("Error attaching to %s\n", modName);
			printError("CSSM_ModuleAttach", crtn);
			return 0;
		}
		if(doPause("ModuleAttach() complete")) {
			break;
		}
		CSSM_ModuleDetach(modHand);
		modHand = 0;
		if(doPause("ModuleDetach() complete")) {
			break;
		}
		if(doUnload) {
			/* unload, each time */
			crtn = CSSM_ModuleUnload(guid, NULL, NULL);
			if(crtn) {
				printf("Error unloading %s\n", modName);
				printError("CSSM_ModuleUnload", crtn);
				return 0;
			}
			if(doPause("ModuleUnload() complete")) {
				break;
			}
		}	/* unloading */
	}		/* main loop */

done:
	fpurge(stdin);
	if(modHand) {
		CSSM_ModuleDetach(modHand);
		printf("Final detach complete; cr to exit: ");
	}
	else {
		printf("Test complete; cr to exit: ");
	}
	getchar();
	return 0;
}