#ifndef _RSA_DSA_KEYS_H_
#define _RSA_DSA_KEYS_H_
#include <AppleCSPContext.h>
#include <AppleCSPSession.h>
#include <RSA_DSA_csp.h>
#include "AppleCSPKeys.h"
#include <Security/osKeyTemplates.h>
#include <openssl/rsa_legacy.h>
#include <openssl/dsa_legacy.h>
#include <security_cdsa_utilities/context.h>
#include <security_asn1/SecNssCoder.h>
#define RSA_PUB_KEY_FORMAT CSSM_KEYBLOB_RAW_FORMAT_PKCS1
#define RSA_PRIV_KEY_FORMAT CSSM_KEYBLOB_RAW_FORMAT_PKCS8
#define DSA_PUB_KEY_FORMAT CSSM_KEYBLOB_RAW_FORMAT_X509
#define DSA_PRIV_KEY_FORMAT CSSM_KEYBLOB_RAW_FORMAT_FIPS186
#define DSA_MIN_KEY_SIZE 512
#define DSA_MAX_KEY_SIZE 4096
#define DSA_KEY_BITS_MASK (64 - 1)
#define RSA_MAX_KEY_SIZE (8 * 1024)
#define RSA_MAX_PUB_EXPONENT_SIZE 64
#define kRSAKeySizePrefsDomain "com.apple.security"
#define kRSAMaxKeySizePref CFSTR("RSAMaxKeySize")
#define kRSAMaxPublicExponentPref CFSTR("RSAMaxPublicExponent")
class RSABinaryKey : public BinaryKey {
public:
RSABinaryKey(RSA *rsaKey = NULL);
~RSABinaryKey();
void generateKeyBlob(
Allocator &allocator,
CssmData &blob,
CSSM_KEYBLOB_FORMAT &format,
AppleCSPSession &session,
const CssmKey *paramKey,
CSSM_KEYATTR_FLAGS &attrFlags);
RSA *mRsaKey;
bool isOaep() { return mOaep; }
const CSSM_DATA &label() { return mLabel; }
void setOaep(
const CSSM_DATA &label);
private:
bool mOaep;
CssmAutoData mLabel;
};
class RSAKeyPairGenContext :
public AppleCSPContext, private AppleKeyPairGenContext {
public:
RSAKeyPairGenContext(
AppleCSPSession &session,
const Context &) :
AppleCSPContext(session) {}
~RSAKeyPairGenContext() { }
void init(
const Context &,
bool) { }
void generate(
const Context &context,
CssmKey &pubKey,
CssmKey &privKey);
void generate(const Context &context, uint32, CssmData ¶ms, uint32 &attrCount, Context::Attr * &attrs);
void generate(
const Context &context,
BinaryKey &pubBinKey,
BinaryKey &privBinKey,
uint32 &keySize);
};
class RSAKeyInfoProvider : public CSPKeyInfoProvider
{
private:
RSAKeyInfoProvider(
const CssmKey &cssmKey,
AppleCSPSession &session);
public:
static CSPKeyInfoProvider *provider(
const CssmKey &cssmKey,
AppleCSPSession &session);
~RSAKeyInfoProvider() { }
void CssmKeyToBinary(
CssmKey *paramKey, CSSM_KEYATTR_FLAGS &attrFlags, BinaryKey **binKey); void QueryKeySizeInBits(
CSSM_KEY_SIZE &keySize); bool getHashableBlob(
Allocator &allocator,
CssmData &hashBlob);
};
class DSABinaryKey : public BinaryKey {
public:
DSABinaryKey(DSA *dsaKey = NULL);
~DSABinaryKey();
void generateKeyBlob(
Allocator &allocator,
CssmData &blob,
CSSM_KEYBLOB_FORMAT &format,
AppleCSPSession &session,
const CssmKey *paramKey,
CSSM_KEYATTR_FLAGS &attrFlags);
DSA *mDsaKey;
};
class DSAKeyPairGenContext :
public AppleCSPContext, private AppleKeyPairGenContext {
public:
DSAKeyPairGenContext(
AppleCSPSession &session,
const Context &) :
AppleCSPContext(session), mGenAttrs(NULL) {}
~DSAKeyPairGenContext() { freeGenAttrs(); }
void init(
const Context &,
bool) { }
void generate(
const Context &context,
CssmKey &pubKey,
CssmKey &privKey);
void generate(
const Context &context,
BinaryKey &pubBinKey,
BinaryKey &privBinKey,
uint32 &keySize);
void generate(
const Context &context,
uint32 bitSize,
CssmData ¶ms,
uint32 &attrCount,
Context::Attr * &attrs);
bool changed(const Context &context) { return true; }
void dsaGenParams(
uint32 keySizeInBits,
const void *inSeed, unsigned inSeedLen,
NSS_DSAAlgParams &algParams,
SecNssCoder &coder);
private:
Context::Attr *mGenAttrs;
void freeGenAttrs();
};
class DSAKeyInfoProvider : public CSPKeyInfoProvider
{
private:
DSAKeyInfoProvider(
const CssmKey &cssmKey,
AppleCSPSession &session);
public:
static CSPKeyInfoProvider *provider(
const CssmKey &cssmKey,
AppleCSPSession &session);
~DSAKeyInfoProvider() { }
void CssmKeyToBinary(
CssmKey *paramKey, CSSM_KEYATTR_FLAGS &attrFlags, BinaryKey **binKey); void QueryKeySizeInBits(
CSSM_KEY_SIZE &keySize); bool getHashableBlob(
Allocator &allocator,
CssmData &hashBlob);
};
#endif