/* * Obtain key size in bits. */ void DSAKeyInfoProvider::QueryKeySizeInBits( CSSM_KEY_SIZE &keySize) { DSA *dsaKey = NULL; if(mKey.blobType() != CSSM_KEYBLOB_RAW) { CssmError::throwMe(CSSMERR_CSP_INVALID_KEY_FORMAT); } dsaKey = rawCssmKeyToDsa(mKey, mSession, NULL); // no param key allowed here if(dsaKey->p != NULL) { /* normal fully-formed key */ keySize.LogicalKeySizeInBits = BN_num_bits(dsaKey->p); keySize.EffectiveKeySizeInBits = keySize.LogicalKeySizeInBits; DSA_free(dsaKey); } else { /* partial key, get an approximation from pub_key */ keySize.LogicalKeySizeInBits = BN_num_bits(dsaKey->pub_key); DSA_free(dsaKey); /* and indicate this anomaly like so */ CssmError::throwMe(CSSMERR_CSP_APPLE_PUBLIC_KEY_INCOMPLETE); } }
/* Given a raw key, cook up a Binary key */ void DSAKeyInfoProvider::CssmKeyToBinary( CssmKey *paramKey, // optional CSSM_KEYATTR_FLAGS &attrFlags, // IN/OUT BinaryKey **binKey) { *binKey = NULL; DSA *dsaKey = NULL; /* first cook up an DSA key, then drop that into a BinaryKey */ dsaKey = rawCssmKeyToDsa(mKey, mSession, paramKey); if(dsaKey->p == NULL) { attrFlags |= CSSM_KEYATTR_PARTIAL; } else { attrFlags &= ~CSSM_KEYATTR_PARTIAL; } DSABinaryKey *dsaBinKey = new DSABinaryKey(dsaKey); *binKey = dsaBinKey; }
/* * Convert a CssmKey to an DSA * key. May result in the creation of a new * DSA (when cssmKey is a raw key); allocdKey is true in that case * in which case the caller generally has to free the allocd key). */ DSA *cssmKeyToDsa( const CssmKey &cssmKey, AppleCSPSession &session, bool &allocdKey) // RETURNED { DSA *dsaKey = NULL; allocdKey = false; const CSSM_KEYHEADER *hdr = &cssmKey.KeyHeader; if(hdr->AlgorithmId != CSSM_ALGID_DSA) { // someone else's key (should never happen) CssmError::throwMe(CSSMERR_CSP_INVALID_ALGORITHM); } switch(hdr->BlobType) { case CSSM_KEYBLOB_RAW: dsaKey = rawCssmKeyToDsa(cssmKey, session, NULL); allocdKey = true; break; case CSSM_KEYBLOB_REFERENCE: { BinaryKey &binKey = session.lookupRefKey(cssmKey); DSABinaryKey *dsaBinKey = dynamic_cast<DSABinaryKey *>(&binKey); /* this cast failing means that this is some other * kind of binary key */ if(dsaBinKey == NULL) { rsaMiscDebug("cssmKeyToDsa: wrong BinaryKey subclass\n"); CssmError::throwMe(CSSMERR_CSP_INVALID_KEY); } assert(dsaBinKey->mDsaKey != NULL); dsaKey = dsaBinKey->mDsaKey; break; } default: CssmError::throwMe(CSSMERR_CSP_KEY_BLOB_TYPE_INCORRECT); } return dsaKey; }