#ifdef BSAFE_CSP_ENABLE
#include "bsafecspi.h"
#include "bsafecsp.h"
#include "AppleCSPSession.h"
const B_ALGORITHM_METHOD * const BSafe::bsChooser[] = {
&AM_SHA,
&AM_MD5,
&AM_MD2,
&AM_CBC_ENCRYPT,
&AM_CBC_DECRYPT,
&AM_ECB_ENCRYPT,
&AM_ECB_DECRYPT,
&AM_OFB_ENCRYPT,
&AM_OFB_DECRYPT,
&AM_DES_ENCRYPT,
&AM_DES_DECRYPT,
&AM_DESX_ENCRYPT,
&AM_DESX_DECRYPT,
&AM_DES_EDE_ENCRYPT,
&AM_DES_EDE_DECRYPT,
&AM_RC2_CBC_ENCRYPT,
&AM_RC2_CBC_DECRYPT,
&AM_RC2_ENCRYPT,
&AM_RC2_DECRYPT,
&AM_RC4_ENCRYPT,
&AM_RC4_DECRYPT,
&AM_RC5_ENCRYPT,
&AM_RC5_DECRYPT,
&AM_RC5_CBC_ENCRYPT,
&AM_RC5_CBC_DECRYPT,
&AM_RSA_STRONG_KEY_GEN,
&AM_RSA_KEY_GEN,
&AM_RSA_CRT_ENCRYPT_BLIND,
&AM_RSA_CRT_DECRYPT_BLIND,
&AM_RSA_ENCRYPT,
&AM_RSA_DECRYPT,
&AM_DSA_PARAM_GEN,
&AM_DSA_KEY_GEN,
&AM_DSA_SIGN,
&AM_DSA_VERIFY,
&AM_MD5_RANDOM,
&AM_SHA_RANDOM,
(B_ALGORITHM_METHOD *)NULL_PTR
};
template <class Ctx>
class Maker0 : public BSafe::MakerBase {
public:
Ctx *make(AppleCSPSession &session, const Context &context) const
{ return new Ctx(session, context); }
};
template <class Ctx, class Arg>
class Maker1 : public BSafe::MakerBase {
Arg arg;
public:
Maker1(Arg a) : arg(a) { }
Ctx *make(AppleCSPSession &session, const Context &context) const
{ return new Ctx(session, context, arg); }
};
template <class Ctx, class Arg1, class Arg2>
class Maker2 : public BSafe::MakerBase {
Arg1 arg1; Arg2 arg2;
public:
Maker2(Arg1 a1, Arg2 a2) : arg1(a1), arg2(a2) { }
Ctx *make(AppleCSPSession &session, const Context &context) const
{ return new Ctx(session, context, arg1, arg2); }
};
template <class Ctx, class Arg1, class Arg2, class Arg3>
class Maker3 : public BSafe::MakerBase {
Arg1 arg1; Arg2 arg2; Arg3 arg3;
public:
Maker3(Arg1 a1, Arg2 a2, Arg3 a3) :
arg1(a1), arg2(a2), arg3(a3) { }
Ctx *make(AppleCSPSession &session, const Context &context) const
{ return new Ctx(session, context, arg1, arg2, arg3); }
};
bug_const BSafe::MakerTable BSafe::algorithms[] = {
{
CSSM_ALGID_SHA1WithDSA,
CSSM_ALGCLASS_SIGNATURE,
new Maker2<SigningContext, B_INFO_TYPE, size_t>
(AI_DSAWithSHA1, 48) },
{
CSSM_ALGID_SHA1WithRSA,
CSSM_ALGCLASS_SIGNATURE,
new Maker2<SigningContext, B_INFO_TYPE, size_t>
(AI_SHA1WithRSAEncryption, 0) },
{
CSSM_ALGID_MD5WithRSA,
CSSM_ALGCLASS_SIGNATURE,
new Maker2<SigningContext, B_INFO_TYPE, size_t>
(AI_MD5WithRSAEncryption, 0) },
{
CSSM_ALGID_MD2WithRSA,
CSSM_ALGCLASS_SIGNATURE,
new Maker2<SigningContext, B_INFO_TYPE, size_t>
(AI_MD2WithRSAEncryption, 0) },
{
CSSM_ALGID_SHA1HMAC,
CSSM_ALGCLASS_MAC,
new Maker2<MacContext, B_INFO_TYPE, size_t>
(AI_SHA1, 20)
},
{
CSSM_ALGID_RC2,
CSSM_ALGCLASS_KEYGEN,
new Maker3<SymmetricKeyGenContext, uint32, uint32, bool>
(1*8, 128*8, true)
},
{
CSSM_ALGID_RC4,
CSSM_ALGCLASS_KEYGEN,
new Maker3<SymmetricKeyGenContext, uint32, uint32, bool>
(1*8, 256*8, true)
},
{
CSSM_ALGID_RC5,
CSSM_ALGCLASS_KEYGEN,
new Maker3<SymmetricKeyGenContext, uint32, uint32, bool>
(1*8, 255*8, true)
},
{
CSSM_ALGID_DES,
CSSM_ALGCLASS_KEYGEN,
new Maker3<SymmetricKeyGenContext, uint32, uint32, bool>
(64, 64, true)
},
{
CSSM_ALGID_DESX,
CSSM_ALGCLASS_KEYGEN,
new Maker3<SymmetricKeyGenContext, uint32, uint32, bool>
(192, 192, true)
},
{
CSSM_ALGID_3DES_3KEY,
CSSM_ALGCLASS_KEYGEN,
new Maker3<SymmetricKeyGenContext, uint32, uint32, bool>
(192, 192, true)
},
{
CSSM_ALGID_SHA1HMAC,
CSSM_ALGCLASS_KEYGEN,
new Maker3<SymmetricKeyGenContext, uint32, uint32, bool>
(160, 2048, true)
},
{
CSSM_ALGID_DES,
CSSM_ALGCLASS_SYMMETRIC,
new Maker1<BlockCipherContext, size_t>(8)
},
{
CSSM_ALGID_DESX,
CSSM_ALGCLASS_SYMMETRIC,
new Maker1<BlockCipherContext, size_t>(8)
},
{
CSSM_ALGID_3DES_3KEY_EDE,
CSSM_ALGCLASS_SYMMETRIC,
new Maker1<BlockCipherContext, size_t>(8)
},
{
CSSM_ALGID_RC2,
CSSM_ALGCLASS_SYMMETRIC,
new Maker1<BlockCipherContext, size_t>(8)
},
{
CSSM_ALGID_RC4,
CSSM_ALGCLASS_SYMMETRIC,
new Maker1<BlockCipherContext, size_t>(1)
},
{
CSSM_ALGID_RC5,
CSSM_ALGCLASS_SYMMETRIC,
new Maker1<BlockCipherContext, size_t>(8)
},
{
CSSM_ALGID_RSA,
CSSM_ALGCLASS_ASYMMETRIC,
new Maker0<PublicKeyCipherContext>()
},
{
CSSM_ALGID_DSA,
CSSM_ALGCLASS_ASYMMETRIC,
new Maker0<PublicKeyCipherContext>()
},
{
CSSM_ALGID_RSA,
CSSM_ALGCLASS_KEYGEN,
new Maker0<BSafeKeyPairGenContext>()
},
{
CSSM_ALGID_DSA,
CSSM_ALGCLASS_KEYGEN,
new Maker0<BSafeKeyPairGenContext>()
},
{
CSSM_ALGID_MD5Random,
CSSM_ALGCLASS_RANDOMGEN,
new Maker1<RandomContext, B_INFO_TYPE>(AI_MD5Random)
},
{
CSSM_ALGID_SHARandom,
CSSM_ALGCLASS_RANDOMGEN,
new Maker1<RandomContext, B_INFO_TYPE>(AI_SHA1Random)
},
};
const unsigned int BSafe::algorithmCount = sizeof(algorithms) / sizeof(algorithms[0]);
void BSafeFactory::setNormAllocator(Allocator *alloc)
{
BSafe::setNormAllocator(alloc);
}
void BSafeFactory::setPrivAllocator(Allocator *alloc)
{
BSafe::setPrivAllocator(alloc);
}
bool BSafeFactory::setup(
AppleCSPSession &session,
CSPFullPluginSession::CSPContext * &cspCtx,
const Context &context)
{
return BSafe::setup(session, cspCtx, context);
}
bool BSafe::setup(
AppleCSPSession &session,
CSPFullPluginSession::CSPContext * &cspCtx,
const Context &context)
{
for (const BSafe::MakerTable *alg = algorithms;
alg < algorithms + algorithmCount;
alg++) {
if ((alg->algorithmId == context.algorithm()) &&
(alg->algClass == context.type())) {
if(cspCtx != NULL) {
return true;
}
cspCtx = alg->maker->make(session, context);
return true;
}
}
return false;
}
#endif