#include <security_cdsa_client/genkey.h>
using namespace CssmClient;
GenerateKey::GenerateKey(const CSP &csp, CSSM_ALGORITHMS alg, uint32 size)
: Context(csp, alg), mKeySize(size), mSeed(NULL), mSalt(NULL), mParams(NULL)
{
}
void
GenerateKey::database(const Db &inDb)
{
mDb = inDb;
if (mDb && isActive())
set(CSSM_ATTRIBUTE_DL_DB_HANDLE, mDb->handle());
}
void GenerateKey::activate()
{
StLock<Mutex> _(mActivateMutex);
if (!mActive)
{
check(CSSM_CSP_CreateKeyGenContext(attachment()->handle(), mAlgorithm,
mKeySize, mSeed, mSalt, NULL, NULL, mParams, &mHandle));
mActive = true;
if (mDb)
set(CSSM_ATTRIBUTE_DL_DB_HANDLE, mDb->handle());
}
}
Key GenerateKey::operator () (const KeySpec &spec)
{
Key key;
check(CSSM_GenerateKey(handle(), spec.usage, spec.attributes, spec.label,
&compositeRcc(), key.makeNewKey(attachment())));
key->activate();
return key;
}
void GenerateKey::operator () (CssmKey &key, const KeySpec &spec)
{
check(CSSM_GenerateKey(handle(), spec.usage, spec.attributes, spec.label, &compositeRcc(), &key));
}
void GenerateKey::operator () (Key &publicKey, const KeySpec &pubSpec,
Key &privateKey, const KeySpec &privSpec)
{
check(CSSM_GenerateKeyPair(handle(),
pubSpec.usage, pubSpec.attributes,
pubSpec.label, publicKey.makeNewKey(attachment()),
privSpec.usage, privSpec.attributes,
privSpec.label, &compositeRcc(), privateKey.makeNewKey(attachment())));
publicKey->activate();
privateKey->activate();
}
void GenerateKey::operator () (CssmKey &publicKey, const KeySpec &pubSpec,
CssmKey &privateKey, const KeySpec &privSpec)
{
check(CSSM_GenerateKeyPair(handle(),
pubSpec.usage, pubSpec.attributes, pubSpec.label, &publicKey,
privSpec.usage, privSpec.attributes, privSpec.label, &compositeRcc(), &privateKey));
}