2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 * FEEKeys.cpp - FEE-related asymmetric key pair classes.
22 * Created 2/21/2001 by dmitch.
25 #ifdef CRYPTKIT_CSP_ENABLE
28 #include "FEECSPUtils.h"
29 #include "CryptKitSpace.h"
30 #include <CryptKit/feePublicKey.h>
31 #include <CryptKit/falloc.h>
32 #include <Security/cssmdata.h>
33 #include "AppleCSPSession.h"
34 #include "AppleCSPUtils.h"
36 #include <Security/debugging.h>
38 #define feeKeyDebug(args...) secdebug("feeKey", ## args)
41 *** FEE-style BinaryKey
44 /* constructor with optional existing feePubKey */
45 CryptKit::FEEBinaryKey::FEEBinaryKey(feePubKey feeKey
)
49 mFeeKey
= feePubKeyAlloc();
51 CssmError::throwMe(CSSMERR_CSP_MEMORY_ERROR
);
56 CryptKit::FEEBinaryKey::~FEEBinaryKey()
59 feePubKeyFree(mFeeKey
);
64 void CryptKit::FEEBinaryKey::generateKeyBlob(
65 CssmAllocator
&allocator
,
67 CSSM_KEYBLOB_FORMAT
&format
,
68 AppleCSPSession
&session
,
69 const CssmKey
*paramKey
, /* optional, unused here */
70 CSSM_KEYATTR_FLAGS
&attrFlags
) /* IN/OUT */
72 unsigned char *keyBlob
;
76 bool freeTheKey
= false;
77 feePubKey keyToEncode
= mFeeKey
;
79 assert(mFeeKey
!= NULL
);
81 /* also case FEE_KEYBLOB_DEFAULT_FORMAT: */
82 case CSSM_KEYBLOB_RAW_FORMAT_NONE
:
85 case CSSM_KEYBLOB_RAW_FORMAT_DIGEST
:
87 /* key digest calculation; special case for private keys: cook
88 * up the associated public key and encode that */
89 if(mKeyHeader
.KeyClass
== CSSM_KEYCLASS_PRIVATE_KEY
) {
90 keyToEncode
= feePubKeyAlloc();
91 if(keyToEncode
== NULL
) {
92 CssmError::throwMe(CSSMERR_CSP_MEMORY_ERROR
);
94 frtn
= feePubKeyInitPubKeyFromPriv(mFeeKey
, keyToEncode
);
96 feePubKeyFree(keyToEncode
);
97 throwCryptKit(frtn
, "feePubKeyInitPubKeyFromPriv");
101 /* in any case, DER-encode a public key */
105 case CSSM_KEYBLOB_RAW_FORMAT_OCTET_STRING
:
106 /* native non-DER-encoded blob */
110 feeKeyDebug("FEEBinaryKey::generateKeyBlob: bad format (%ld)\n", format
);
111 CssmError::throwMe(feePubKeyIsPrivate(mFeeKey
) ?
112 CSSMERR_CSP_INVALID_ATTR_PRIVATE_KEY_FORMAT
:
113 CSSMERR_CSP_INVALID_ATTR_PUBLIC_KEY_FORMAT
);
115 if(feePubKeyIsPrivate(keyToEncode
)) {
117 frtn
= feePubKeyCreateDERPrivBlob(keyToEncode
, &keyBlob
, &len
);
120 frtn
= feePubKeyCreatePrivBlob(keyToEncode
, &keyBlob
, &len
);
125 frtn
= feePubKeyCreateDERPubBlob(keyToEncode
, &keyBlob
, &len
);
128 frtn
= feePubKeyCreatePubBlob(keyToEncode
, &keyBlob
, &len
);
132 throwCryptKit(frtn
, "feePubKeyCreate*Blob");
134 setUpCssmData(blob
, len
, allocator
);
135 memmove(blob
.data(), keyBlob
, len
);
138 format
= derBlob
? FEE_KEYBLOB_DEFAULT_FORMAT
:
139 CSSM_KEYBLOB_RAW_FORMAT_OCTET_STRING
;
141 /* free the temp pub key we created here */
142 feePubKeyFree(keyToEncode
);
147 *** FEE-style AppleKeyPairGenContext
151 * This one is specified in, and called from, CSPFullPluginSession. Our
152 * only job is to prepare two subclass-specific BinaryKeys and call up to
153 * AppleKeyPairGenContext.
155 void CryptKit::FEEKeyPairGenContext::generate(
156 const Context
&context
,
160 FEEBinaryKey
*pubBinKey
= new FEEBinaryKey();
161 FEEBinaryKey
*privBinKey
= new FEEBinaryKey();
164 AppleKeyPairGenContext::generate(context
,
179 // this one is specified in, and called from, AppleKeyPairGenContext
180 void CryptKit::FEEKeyPairGenContext::generate(
181 const Context
&context
,
182 BinaryKey
&pubBinKey
,
183 BinaryKey
&privBinKey
,
187 * These casts throw exceptions if the keys are of the
188 * wrong classes, which would be a major bogon, since we created
189 * the keys in the above generate() function.
191 FEEBinaryKey
&fPubBinKey
=
192 dynamic_cast<FEEBinaryKey
&>(pubBinKey
);
193 FEEBinaryKey
&fPrivBinKey
=
194 dynamic_cast<FEEBinaryKey
&>(privBinKey
);
197 * Two parameters from context. Key size in bits is required;
198 * seed is optional. If not present, we cook up random private data.
200 keyBits
= context
.getInt(CSSM_ATTRIBUTE_KEY_LENGTH
,
201 CSSMERR_CSP_MISSING_ATTR_KEY_LENGTH
);
202 CssmCryptoData
*cseed
= context
.get
<CssmCryptoData
>(CSSM_ATTRIBUTE_SEED
);
205 CssmAutoData
aSeed(session()); // malloc on demand
207 /* caller specified seed */
209 seed
= &cseed
->param();
212 /* generate random seed */
214 unsigned keyBytes
= ((keyBits
+ 7) / 8) + 1;
215 aSeed
.malloc(keyBytes
);
216 session().getRandomBytes(keyBytes
, aSeed
);
220 /* Curve and prime types - optional */
221 feePrimeType primeType
= FPT_Default
;
222 uint32 uPrimeType
= context
.getInt(CSSM_ATTRIBUTE_FEE_PRIME_TYPE
);
224 case CSSM_FEE_PRIME_TYPE_DEFAULT
:
226 case CSSM_FEE_PRIME_TYPE_MERSENNE
:
227 primeType
= FPT_Mersenne
;
229 case CSSM_FEE_PRIME_TYPE_FEE
:
232 case CSSM_FEE_PRIME_TYPE_GENERAL
:
233 primeType
= FPT_General
;
236 /* FIXME - maybe we should be more specific */
237 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_ALG_PARAMS
);
239 feeCurveType curveType
= FCT_Default
;
240 uint32 uCurveType
= context
.getInt(CSSM_ATTRIBUTE_FEE_CURVE_TYPE
);
242 case CSSM_FEE_CURVE_TYPE_DEFAULT
:
244 case CSSM_FEE_CURVE_TYPE_MONTGOMERY
:
245 curveType
= FCT_Montgomery
;
247 case CSSM_FEE_CURVE_TYPE_WEIERSTRASS
:
248 curveType
= FCT_Weierstrass
;
251 /* FIXME - maybe we should be more specific */
252 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_ALG_PARAMS
);
254 feeReturn frtn
= feePubKeyInitFromPrivDataKeyBits(
255 fPrivBinKey
.feeKey(),
256 (unsigned char *)seed
->data(),
262 * our random seed: trust it
263 * caller's seed: hash it
267 throwCryptKit(frtn
, "feePubKeyInitFromPrivDataKeyBits");
269 frtn
= feePubKeyInitPubKeyFromPriv(fPrivBinKey
.feeKey(),
270 fPubBinKey
.feeKey());
272 throwCryptKit(frtn
, "feePubKeyInitPubKeyFromPriv");
278 *** FEE-style CSPKeyInfoProvider.
280 CryptKit::FEEKeyInfoProvider::FEEKeyInfoProvider(
281 const CssmKey
&cssmKey
,
282 AppleCSPSession
&session
) :
283 CSPKeyInfoProvider(cssmKey
, session
)
286 CSPKeyInfoProvider
*FEEKeyInfoProvider::provider(
287 const CssmKey
&cssmKey
,
288 AppleCSPSession
&session
)
290 switch(cssmKey
.algorithm()) {
296 switch(cssmKey
.keyClass()) {
297 case CSSM_KEYCLASS_PUBLIC_KEY
:
298 case CSSM_KEYCLASS_PRIVATE_KEY
:
299 /* FIXME - verify proper CSSM_KEYBLOB_RAW_FORMAT_xx */
304 /* OK, we'll handle this one */
305 return new FEEKeyInfoProvider(cssmKey
, session
);
308 /* Given a raw key, cook up a Binary key */
309 void CryptKit::FEEKeyInfoProvider::CssmKeyToBinary(
310 CssmKey
*paramKey
, // optional, ignored
311 CSSM_KEYATTR_FLAGS
&attrFlags
, // IN/OUT
315 feePubKey feeKey
= NULL
;
317 /* first cook up a feePubKey, then drop that into a BinaryKey */
318 feeKey
= rawCssmKeyToFee(mKey
);
319 FEEBinaryKey
*feeBinKey
= new FEEBinaryKey(feeKey
);
324 * Obtain key size in bits.
325 * Currently only raw public keys are dealt with (they're the ones
326 * which come from certs, the only current use for this function).
327 * Note that if we need to handle ref keys, we'll need a session ref...
329 void CryptKit::FEEKeyInfoProvider::QueryKeySizeInBits(
330 CSSM_KEY_SIZE
&keySize
)
332 feePubKey feeKey
= NULL
;
334 if(mKey
.blobType() != CSSM_KEYBLOB_RAW
) {
335 CssmError::throwMe(CSSMERR_CSP_INVALID_KEY_FORMAT
);
337 feeKey
= rawCssmKeyToFee(mKey
);
338 keySize
.LogicalKeySizeInBits
= feePubKeyBitsize(feeKey
);
339 keySize
.EffectiveKeySizeInBits
= keySize
.LogicalKeySizeInBits
;
340 feePubKeyFree(feeKey
);
344 * Obtain blob suitable for hashing in CSSM_APPLECSP_KEYDIGEST
347 bool CryptKit::FEEKeyInfoProvider::getHashableBlob(
348 CssmAllocator
&allocator
,
349 CssmData
&blob
) // blob to hash goes here
352 * The optimized case, a raw key in the "proper" format already.
354 assert(mKey
.blobType() == CSSM_KEYBLOB_RAW
);
355 if((mKey
.blobFormat() == CSSM_KEYBLOB_RAW_FORMAT_NONE
) &&
356 (mKey
.keyClass() == CSSM_KEYCLASS_PUBLIC_KEY
)) {
357 const CssmData
&keyBlob
= CssmData::overlay(mKey
.KeyData
);
358 copyCssmData(keyBlob
, blob
, allocator
);
362 /* caller converts to binary and proceeds */
366 #endif /* CRYPTKIT_CSP_ENABLE */