#include "KeyHandle.h"
namespace Tokend
{
KeyHandle::KeyHandle(const MetaRecord &metaRecord,
const RefPointer<Record> &record) :
RecordHandle(metaRecord, record)
{
}
KeyHandle::~KeyHandle()
{
}
void KeyHandle::wrapUsingKey(const Context &context,
const AccessCredentials *cred, KeyHandle *wrappingKeyHandle,
const CssmKey *wrappingKey, const CssmData *descriptiveData,
CssmKey &wrappedKey)
{
secdebug("crypto", "wrapKey alg: %u", context.algorithm());
IFDUMPING("crypto", context.dump("wrapKey context"));
if (wrappingKeyHandle)
{
secdebug("tokend",
"wrapKey of a reference key using a reference key not supported");
CssmError::throwMe(CSSM_ERRCODE_FUNCTION_NOT_IMPLEMENTED);
}
exportKey(context, cred, wrappedKey);
CssmError::throwMe(CSSM_ERRCODE_FUNCTION_NOT_IMPLEMENTED);
}
void KeyHandle::wrapKey(const Context &context, const CssmKey &subjectKey,
const CssmData *descriptiveData, CssmKey &wrappedKey)
{
secdebug("tokend", "wrapKey of a raw subject key not supported");
CssmError::throwMe(CSSM_ERRCODE_FUNCTION_NOT_IMPLEMENTED);
}
void KeyHandle::unwrapKey(const Context &context,
const AccessCredentials *cred, const AclEntryPrototype *access,
const CssmKey &wrappedKey, CSSM_KEYUSE usage,
CSSM_KEYATTR_FLAGS attributes, CssmData *descriptiveData,
CSSM_HANDLE &hUnwrappedKey, CssmKey &unwrappedKey)
{
secdebug("crypto", "unwrapKey alg: %u", context.algorithm());
IFDUMPING("crypto", context.dump("unwrapKey context"));
#if 0
if (keyClass() == CSSM_KEYCLASS_SESSION_KEY)
{
if (context.type() != CSSM_ALGCLASS_SYMMETRIC))
CssmError::throwMe(CSSMERR_CSP_INVALID_CONTEXT);
}
else
#endif
if (context.type() != CSSM_ALGCLASS_ASYMMETRIC)
CssmError::throwMe(CSSMERR_CSP_INVALID_CONTEXT);
if (wrappedKey.keyClass() != CSSM_KEYCLASS_SESSION_KEY)
CssmError::throwMe(CSSMERR_CSP_INVALID_KEY_CLASS);
if(wrappedKey.blobType() != CSSM_KEYBLOB_WRAPPED)
CssmError::throwMe(CSSMERR_CSP_KEY_BLOB_TYPE_INCORRECT);
if (!(attributes & CSSM_KEYATTR_RETURN_DATA)
|| (attributes & (CSSM_KEYATTR_RETURN_REF | CSSM_KEYATTR_RETURN_NONE
| CSSM_KEYATTR_PERMANENT | CSSM_KEYATTR_PRIVATE)) != 0)
CssmError::throwMe(CSSMERR_CSP_INVALID_KEYATTR_MASK);
CssmKey::Header &hdr = unwrappedKey.header();
hdr.clearPod();
hdr.HeaderVersion = CSSM_KEYHEADER_VERSION;
hdr.cspGuid(gGuidAppleSdCSPDL);
hdr.blobType(CSSM_KEYBLOB_RAW);
hdr.algorithm(wrappedKey.algorithm());
hdr.keyClass(wrappedKey.keyClass());
hdr.KeyUsage = usage;
hdr.KeyAttr = attributes & ~(CSSM_KEYATTR_RETURN_DATA
| CSSM_KEYATTR_RETURN_REF | CSSM_KEYATTR_RETURN_NONE);
hdr.StartDate = wrappedKey.header().StartDate;
hdr.EndDate = wrappedKey.header().EndDate;
unwrappedKey.KeyData.Data = NULL; unwrappedKey.KeyData.Length = 0;
if (wrappedKey.blobFormat() != CSSM_KEYBLOB_WRAPPED_FORMAT_PKCS7)
CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_WRAPPED_KEY_FORMAT);
if (descriptiveData)
{
descriptiveData->Data = NULL;
descriptiveData->Length = 0;
}
decrypt(context, wrappedKey.keyData(), unwrappedKey.keyData());
hdr.blobFormat(CSSM_KEYBLOB_RAW_FORMAT_OCTET_STRING);
hdr.LogicalKeySizeInBits = unwrappedKey.length() * 8;
}
KeyHandleFactory::~KeyHandleFactory()
{
}
}