#ifdef BSAFE_CSP_ENABLE
#include "bsafePKCS1.h"
#include "bsafecspi.h"
#include "cspdebugging.h"
#include "bsobjects.h"
#include <Security/pkcs1oids.h>
#include <Security/cdsaUtils.h>
#include <Security/cssmerrno.h>
static void BS_ItemToSnaccBigInt(
const ITEM &item,
BigIntegerStr &snaccInt)
{
snaccInt.Set(reinterpret_cast<const char *>(item.data), item.len);
}
static void BS_snaccBigIntToItem(
BigIntegerStr &snaccInt, ITEM &item)
{
char *cp = snaccInt;
item.data = reinterpret_cast<unsigned char *>(cp);
item.len = snaccInt.Len();
}
void BS_setKeyPkcs1(
const CssmData &pkcs1Blob,
B_KEY_OBJ bsKey)
{
RSAPublicKey snaccPubKey;
try {
SC_decodeAsnObj(pkcs1Blob, snaccPubKey);
}
catch(const CssmError &cerror) {
CSSM_RETURN crtn = cerror.cssmError();
errorLog1("BS_setKeyPkcs1: SC_decodeAsnObj returned %s\n",
cssmErrorString(crtn).c_str());
switch(crtn) {
case CSSMERR_CSSM_MEMORY_ERROR:
crtn = CSSMERR_CSP_MEMORY_ERROR;
break;
case CSSMERR_CSSM_INVALID_INPUT_POINTER:
crtn = CSSMERR_CSP_INVALID_KEY;
default:
break;
}
CssmError::throwMe(crtn);
}
A_RSA_KEY rsaKey;
BS_snaccBigIntToItem(snaccPubKey.modulus, rsaKey.modulus);
BS_snaccBigIntToItem(snaccPubKey.publicExponent, rsaKey.exponent);
BSafe::check(
B_SetKeyInfo(bsKey, KI_RSAPublic, POINTER(&rsaKey)), true);
}
void BS_GetKeyPkcs1(
const B_KEY_OBJ bsKey,
CssmOwnedData &pkcs1Blob)
{
A_RSA_KEY *rsaKey;
BSafe::check(
B_GetKeyInfo((POINTER *)&rsaKey, bsKey, KI_RSAPublic), true);
RSAPublicKey snaccPubKey;
BS_ItemToSnaccBigInt(rsaKey->modulus, snaccPubKey.modulus);
BS_ItemToSnaccBigInt(rsaKey->exponent, snaccPubKey.publicExponent);
size_t maxSize = 2 * (rsaKey->modulus.len + rsaKey->exponent.len);
try {
SC_encodeAsnObj(snaccPubKey, pkcs1Blob, maxSize);
}
catch(const CssmError &cerror) {
CSSM_RETURN crtn = cerror.cssmError();
errorLog1("BS_GetKeyPkcs1: SC_encodeAsnObj returned %s\n",
cssmErrorString(crtn).c_str());
switch(crtn) {
case CSSMERR_CSSM_MEMORY_ERROR:
crtn = CSSMERR_CSP_MEMORY_ERROR;
break;
default:
break;
}
CssmError::throwMe(crtn);
}
}
#endif