#include "RSA_DSA_csp.h"
#include "RSA_DSA_signature.h"
#include <MiscCSPAlgs/SHA1_MD5_Object.h>
#include <AppleCSP/SignatureContext.h>
#include <Security/digestobject.h>
#include "RSA_DSA_keys.h"
#include "RSA_asymmetric.h"
#include <MiscCSPAlgs/MD2Object.h>
#include <Security/cssmapple.h>
#define OPENSSL_DSA_ENABLE 1
CssmAllocator *RSA_DSA_Factory::normAllocator;
CssmAllocator *RSA_DSA_Factory::privAllocator;
extern "C" {
extern int CRYPTO_set_mem_functions(
void *(*m)(size_t),
void *(*r)(void *,size_t),
void (*f)(void *));
int CRYPTO_set_locked_mem_functions(
void *(*m)(size_t),
void (*free_func)(void *));
}
static void *osMalloc(size_t size)
{
return RSA_DSA_Factory::privAllocator->malloc(size);
}
static void osFree(void *data)
{
RSA_DSA_Factory::privAllocator->free(data);
}
static void *osRealloc(void *oldPtr, size_t newSize)
{
return RSA_DSA_Factory::privAllocator->realloc(oldPtr, newSize);
}
RSA_DSA_Factory::RSA_DSA_Factory(CssmAllocator *normAlloc, CssmAllocator *privAlloc)
{
setNormAllocator(normAlloc);
setPrivAllocator(privAlloc);
CRYPTO_set_mem_functions(osMalloc, osRealloc, osFree);
CRYPTO_set_locked_mem_functions(osMalloc, osFree);
ERR_load_RSA_strings();
ERR_load_BN_strings();
ERR_load_DSA_strings();
}
RSA_DSA_Factory::~RSA_DSA_Factory()
{
}
bool RSA_DSA_Factory::setup(
AppleCSPSession &session,
CSPFullPluginSession::CSPContext * &cspCtx,
const Context &context)
{
switch(context.type()) {
case CSSM_ALGCLASS_SIGNATURE:
switch(context.algorithm()) {
case CSSM_ALGID_SHA1WithRSA:
if(cspCtx == NULL) {
cspCtx = new SignatureContext(session,
*(new SHA1Object()),
*(new RSASigner(*privAllocator,
session,
CSSM_ALGID_SHA1)));
}
return true;
case CSSM_ALGID_MD5WithRSA:
if(cspCtx == NULL) {
cspCtx = new SignatureContext(session,
*(new MD5Object()),
*(new RSASigner(*privAllocator,
session,
CSSM_ALGID_MD5)));
}
return true;
case CSSM_ALGID_MD2WithRSA:
if(cspCtx == NULL) {
cspCtx = new SignatureContext(session,
*(new MD2Object()),
*(new RSASigner(*privAllocator,
session,
CSSM_ALGID_MD2)));
}
return true;
#if OPENSSL_DSA_ENABLE
case CSSM_ALGID_SHA1WithDSA:
if(cspCtx == NULL) {
cspCtx = new SignatureContext(session,
*(new SHA1Object()),
*(new DSASigner(*privAllocator,
session,
CSSM_ALGID_SHA1)));
}
return true;
case CSSM_ALGID_DSA:
if(cspCtx == NULL) {
cspCtx = new SignatureContext(session,
*(new NullDigest()),
*(new DSASigner(*privAllocator,
session,
CSSM_ALGID_NONE)));
}
return true;
#endif
case CSSM_ALGID_RSA:
if(cspCtx == NULL) {
cspCtx = new SignatureContext(session,
*(new NullDigest()),
*(new RSASigner(*privAllocator,
session,
CSSM_ALGID_NONE)));
}
return true;
default:
break;
}
break;
case CSSM_ALGCLASS_KEYGEN:
switch(context.algorithm()) {
case CSSM_ALGID_RSA:
if(cspCtx == NULL) {
cspCtx = new RSAKeyPairGenContext(session, context);
}
return true;
#if OPENSSL_DSA_ENABLE
case CSSM_ALGID_DSA:
if(cspCtx == NULL) {
cspCtx = new DSAKeyPairGenContext(session, context);
}
return true;
#endif
default:
break;
}
break;
case CSSM_ALGCLASS_ASYMMETRIC:
switch(context.algorithm()) {
case CSSM_ALGID_RSA:
if(cspCtx == NULL) {
cspCtx = new RSA_CryptContext(session);
}
return true;
default:
break;
}
break;
default:
break;
}
return false;
}