/* 
 * Obtain key size in bits.
 */
void RSAKeyInfoProvider::QueryKeySizeInBits(
	CSSM_KEY_SIZE &keySize)
{
	RSA *rsaKey = NULL;
	CSSM_DATA label = {0, NULL};
	
	if(mKey.blobType() != CSSM_KEYBLOB_RAW) {
		CssmError::throwMe(CSSMERR_CSP_INVALID_KEY_FORMAT);
	}
	rsaKey = rawCssmKeyToRsa(mKey, label);
	keySize.LogicalKeySizeInBits = RSA_size(rsaKey) * 8;
	keySize.EffectiveKeySizeInBits = keySize.LogicalKeySizeInBits;
	RSA_free(rsaKey);
	if(label.Data) {
		free(label.Data);
	}	
}
/* Given a raw key, cook up a Binary key */
void RSAKeyInfoProvider::CssmKeyToBinary(
	CssmKey				*paramKey,		// ignored
	CSSM_KEYATTR_FLAGS	&attrFlags,		// IN/OUT, unused here
	BinaryKey 			**binKey)
{
	*binKey = NULL;
	RSA *rsaKey = NULL;
	CSSM_DATA label = {0, NULL};
	
	/* first cook up an RSA key */
	rsaKey = rawCssmKeyToRsa(mKey, label);
	
	/* now drop that into a BinaryKey */
	RSABinaryKey *rsaBinKey = new RSABinaryKey(rsaKey);
	*binKey = rsaBinKey;
	if(label.Data) {
		rsaBinKey->setOaep(label);
		free(label.Data);
	}
}
Example #3
0
/*
 * Convert a CssmKey to an RSA * key. May result in the creation of a new
 * RSA (when cssmKey is a raw key); allocdKey is true in that case
 * in which case the caller generally has to free the allocd key).
 */
RSA *cssmKeyToRsa(
    const CssmKey	&cssmKey,
    AppleCSPSession	&session,
    bool			&allocdKey,		// RETURNED
    CSSM_DATA		&label)			// mallocd and RETURNED for OAEP
{
    RSA *rsaKey = NULL;
    allocdKey = false;

    const CSSM_KEYHEADER *hdr = &cssmKey.KeyHeader;
    if(hdr->AlgorithmId != CSSM_ALGID_RSA) {
        // someone else's key (should never happen)
        CssmError::throwMe(CSSMERR_CSP_INVALID_ALGORITHM);
    }
    switch(hdr->BlobType) {
    case CSSM_KEYBLOB_RAW:
        rsaKey = rawCssmKeyToRsa(cssmKey, label);
        allocdKey = true;
        break;
    case CSSM_KEYBLOB_REFERENCE:
    {
        BinaryKey &binKey = session.lookupRefKey(cssmKey);
        RSABinaryKey *rsaBinKey = dynamic_cast<RSABinaryKey *>(&binKey);
        /* this cast failing means that this is some other
         * kind of binary key */
        if(rsaBinKey == NULL) {
            rsaMiscDebug("cssmKeyToRsa: wrong BinaryKey subclass\n");
            CssmError::throwMe(CSSMERR_CSP_INVALID_KEY);
        }
        assert(rsaBinKey->mRsaKey != NULL);
        rsaKey = rsaBinKey->mRsaKey;
        break;
    }
    default:
        CssmError::throwMe(CSSMERR_CSP_KEY_BLOB_TYPE_INCORRECT);
    }
    return rsaKey;
}