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 <security_cryptkit/feePublicKey.h>
31 #include <security_cryptkit/falloc.h>
32 #include <security_cdsa_utilities/cssmdata.h>
33 #include "AppleCSPSession.h"
34 #include "AppleCSPUtils.h"
36 #include <security_utilities/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(
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
;
74 feeReturn frtn
= FR_Internal
;
75 bool freeTheKey
= false;
76 feePubKey keyToEncode
= mFeeKey
;
78 assert(mFeeKey
!= NULL
);
79 if((format
== CSSM_KEYBLOB_RAW_FORMAT_DIGEST
) &&
80 (mKeyHeader
.KeyClass
== CSSM_KEYCLASS_PRIVATE_KEY
)) {
81 /* key digest calculation; special case for private keys: cook
82 * up the associated public key and encode that */
83 keyToEncode
= feePubKeyAlloc();
84 if(keyToEncode
== NULL
) {
85 CssmError::throwMe(CSSMERR_CSP_MEMORY_ERROR
);
87 frtn
= feePubKeyInitPubKeyFromPriv(mFeeKey
, keyToEncode
);
89 feePubKeyFree(keyToEncode
);
90 throwCryptKit(frtn
, "feePubKeyInitPubKeyFromPriv");
95 bool badFormat
= false;
96 int isPrivate
= feePubKeyIsPrivate(keyToEncode
);
98 switch(mKeyHeader
.AlgorithmId
) {
101 /* FEE private key */
103 case CSSM_KEYBLOB_RAW_FORMAT_DIGEST
:
104 format
= CSSM_KEYBLOB_RAW_FORMAT_NONE
;
106 case CSSM_KEYBLOB_RAW_FORMAT_NONE
:
107 frtn
= feePubKeyCreateDERPrivBlob(keyToEncode
, &keyBlob
, &len
);
109 case CSSM_KEYBLOB_RAW_FORMAT_OCTET_STRING
:
110 frtn
= feePubKeyCreatePrivBlob(keyToEncode
, &keyBlob
, &len
);
120 case CSSM_KEYBLOB_RAW_FORMAT_DIGEST
:
121 format
= CSSM_KEYBLOB_RAW_FORMAT_NONE
;
123 case CSSM_KEYBLOB_RAW_FORMAT_NONE
:
124 frtn
= feePubKeyCreateDERPubBlob(keyToEncode
, &keyBlob
, &len
);
126 case CSSM_KEYBLOB_RAW_FORMAT_OCTET_STRING
:
127 frtn
= feePubKeyCreatePubBlob(keyToEncode
, &keyBlob
, &len
);
134 /* end of base ALGID_FEE */
137 case CSSM_ALGID_ECDSA
:
139 /* ECDSA/ECDH private key */
141 case CSSM_KEYBLOB_RAW_FORMAT_PKCS8
:
142 /* ECDSA private key: PKCS8 */
143 frtn
= feePubKeyCreatePKCS8Blob(keyToEncode
, &keyBlob
, &len
);
145 case CSSM_KEYBLOB_RAW_FORMAT_NONE
:
146 /* set to default format, drop thru */
147 format
= CSSM_KEYBLOB_RAW_FORMAT_OPENSSL
;
148 case CSSM_KEYBLOB_RAW_FORMAT_OPENSSL
:
149 /* ECDSA private key, SEC1/OpenSSL format */
150 frtn
= feePubKeyCreateOpenSSLBlob(keyToEncode
, &keyBlob
, &len
);
152 case CSSM_KEYBLOB_RAW_FORMAT_DIGEST
:
153 format
= CSSM_KEYBLOB_RAW_FORMAT_OCTET_STRING
;
155 case CSSM_KEYBLOB_RAW_FORMAT_OCTET_STRING
:
156 /* raw private key bytes */
157 frtn
= feeCreateECDSAPrivBlob(keyToEncode
, &keyBlob
, &len
);
167 * Note there is no OpenSSL case here, that format is only generated for
171 case CSSM_KEYBLOB_RAW_FORMAT_NONE
:
172 /* set to default format, drop thru */
173 format
= CSSM_KEYBLOB_RAW_FORMAT_X509
;
174 case CSSM_KEYBLOB_RAW_FORMAT_X509
:
175 /* ECDSA, public key, default: X509 */
176 frtn
= feePubKeyCreateX509Blob(keyToEncode
, &keyBlob
, &len
);
178 case CSSM_KEYBLOB_RAW_FORMAT_DIGEST
:
179 format
= CSSM_KEYBLOB_RAW_FORMAT_OCTET_STRING
;
181 case CSSM_KEYBLOB_RAW_FORMAT_OCTET_STRING
:
183 frtn
= feeCreateECDSAPubBlob(keyToEncode
, &keyBlob
, &len
);
190 /* end of case CSSM_ALGID_ECDSA */
198 CssmError::throwMe(isPrivate
?
199 CSSMERR_CSP_INVALID_ATTR_PRIVATE_KEY_FORMAT
:
200 CSSMERR_CSP_INVALID_ATTR_PUBLIC_KEY_FORMAT
);
203 throwCryptKit(frtn
, "feePubKeyCreate*Blob");
205 setUpCssmData(blob
, len
, allocator
);
206 memmove(blob
.data(), keyBlob
, len
);
210 /* free the temp pub key we created here */
211 feePubKeyFree(keyToEncode
);
216 *** FEE-style AppleKeyPairGenContext
220 * This one is specified in, and called from, CSPFullPluginSession. Our
221 * only job is to prepare two subclass-specific BinaryKeys and call up to
222 * AppleKeyPairGenContext.
224 void CryptKit::FEEKeyPairGenContext::generate(
225 const Context
&context
,
229 FEEBinaryKey
*pubBinKey
= new FEEBinaryKey();
230 FEEBinaryKey
*privBinKey
= new FEEBinaryKey();
233 AppleKeyPairGenContext::generate(context
,
248 // this one is specified in, and called from, AppleKeyPairGenContext
249 void CryptKit::FEEKeyPairGenContext::generate(
250 const Context
&context
,
251 BinaryKey
&pubBinKey
,
252 BinaryKey
&privBinKey
,
256 * These casts throw exceptions if the keys are of the
257 * wrong classes, which would be a major bogon, since we created
258 * the keys in the above generate() function.
260 FEEBinaryKey
&fPubBinKey
=
261 dynamic_cast<FEEBinaryKey
&>(pubBinKey
);
262 FEEBinaryKey
&fPrivBinKey
=
263 dynamic_cast<FEEBinaryKey
&>(privBinKey
);
266 * Two parameters from context. Key size in bits is required;
267 * seed is optional. If not present, we cook up random private data.
269 keyBits
= context
.getInt(CSSM_ATTRIBUTE_KEY_LENGTH
,
270 CSSMERR_CSP_MISSING_ATTR_KEY_LENGTH
);
271 CssmCryptoData
*cseed
= context
.get
<CssmCryptoData
>(CSSM_ATTRIBUTE_SEED
);
274 CssmAutoData
aSeed(session()); // malloc on demand
276 /* caller specified seed */
278 seed
= &cseed
->param();
281 /* generate random seed */
283 unsigned keyBytes
= ((keyBits
+ 7) / 8) + 1;
284 aSeed
.malloc(keyBytes
);
285 session().getRandomBytes(keyBytes
, aSeed
);
289 CSSM_ALGORITHMS algId
= context
.algorithm();
291 /* Curve and prime types - optional */
292 feePrimeType primeType
= FPT_Default
;
293 uint32 uPrimeType
= context
.getInt(CSSM_ATTRIBUTE_FEE_PRIME_TYPE
);
295 case CSSM_FEE_PRIME_TYPE_DEFAULT
:
297 case CSSM_FEE_PRIME_TYPE_MERSENNE
:
298 primeType
= FPT_Mersenne
;
300 case CSSM_FEE_PRIME_TYPE_FEE
:
303 case CSSM_FEE_PRIME_TYPE_GENERAL
:
304 primeType
= FPT_General
;
307 /* FIXME - maybe we should be more specific */
308 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_ALG_PARAMS
);
310 feeCurveType curveType
= FCT_Default
;
312 case CSSM_ALGID_ECDSA
:
314 curveType
= FCT_ANSI
;
318 uint32 uCurveType
= context
.getInt(CSSM_ATTRIBUTE_FEE_CURVE_TYPE
);
320 case CSSM_FEE_CURVE_TYPE_DEFAULT
:
322 case CSSM_FEE_CURVE_TYPE_MONTGOMERY
:
323 curveType
= FCT_Montgomery
;
325 case CSSM_FEE_CURVE_TYPE_WEIERSTRASS
:
326 curveType
= FCT_Weierstrass
;
328 case CSSM_FEE_CURVE_TYPE_ANSI_X9_62
:
329 curveType
= FCT_ANSI
;
332 /* FIXME - maybe we should be more specific */
333 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_ALG_PARAMS
);
338 feeReturn frtn
= feePubKeyInitFromPrivDataKeyBits(
339 fPrivBinKey
.feeKey(),
340 (unsigned char *)seed
->data(),
341 (unsigned int)seed
->length(),
346 * our random seed: trust it
347 * caller's seed: hash it
351 throwCryptKit(frtn
, "feePubKeyInitFromPrivDataKeyBits");
353 frtn
= feePubKeyInitPubKeyFromPriv(fPrivBinKey
.feeKey(),
354 fPubBinKey
.feeKey());
356 throwCryptKit(frtn
, "feePubKeyInitPubKeyFromPriv");
362 *** FEE-style CSPKeyInfoProvider.
364 CryptKit::FEEKeyInfoProvider::FEEKeyInfoProvider(
365 const CssmKey
&cssmKey
,
366 AppleCSPSession
&session
) :
367 CSPKeyInfoProvider(cssmKey
, session
)
370 CSPKeyInfoProvider
*FEEKeyInfoProvider::provider(
371 const CssmKey
&cssmKey
,
372 AppleCSPSession
&session
)
374 switch(cssmKey
.algorithm()) {
376 case CSSM_ALGID_ECDSA
:
381 switch(cssmKey
.keyClass()) {
382 case CSSM_KEYCLASS_PUBLIC_KEY
:
383 case CSSM_KEYCLASS_PRIVATE_KEY
:
384 /* FIXME - verify proper CSSM_KEYBLOB_RAW_FORMAT_xx */
389 /* OK, we'll handle this one */
390 return new FEEKeyInfoProvider(cssmKey
, session
);
393 /* Given a raw key, cook up a Binary key */
394 void CryptKit::FEEKeyInfoProvider::CssmKeyToBinary(
395 CssmKey
*paramKey
, // optional, ignored
396 CSSM_KEYATTR_FLAGS
&attrFlags
, // IN/OUT
400 feePubKey feeKey
= NULL
;
402 /* first cook up a feePubKey, then drop that into a BinaryKey */
403 feeKey
= rawCssmKeyToFee(mKey
);
404 FEEBinaryKey
*feeBinKey
= new FEEBinaryKey(feeKey
);
409 * Obtain key size in bits.
410 * Currently only raw public keys are dealt with (they're the ones
411 * which come from certs, the only current use for this function).
412 * Note that if we need to handle ref keys, we'll need a session ref...
414 void CryptKit::FEEKeyInfoProvider::QueryKeySizeInBits(
415 CSSM_KEY_SIZE
&keySize
)
417 feePubKey feeKey
= NULL
;
419 if(mKey
.blobType() != CSSM_KEYBLOB_RAW
) {
420 CssmError::throwMe(CSSMERR_CSP_INVALID_KEY_FORMAT
);
422 feeKey
= rawCssmKeyToFee(mKey
);
423 keySize
.LogicalKeySizeInBits
= feePubKeyBitsize(feeKey
);
424 keySize
.EffectiveKeySizeInBits
= keySize
.LogicalKeySizeInBits
;
425 feePubKeyFree(feeKey
);
429 * Obtain blob suitable for hashing in CSSM_APPLECSP_KEYDIGEST
432 bool CryptKit::FEEKeyInfoProvider::getHashableBlob(
433 Allocator
&allocator
,
434 CssmData
&blob
) // blob to hash goes here
437 * The optimized case, a raw key in the "proper" format already.
439 * FEE public key in default/NONE form (which happens to be DER)
441 assert(mKey
.blobType() == CSSM_KEYBLOB_RAW
);
442 if((mKey
.algorithm() == CSSM_ALGID_FEE
) &&
443 (mKey
.blobFormat() == CSSM_KEYBLOB_RAW_FORMAT_NONE
) &&
444 (mKey
.keyClass() == CSSM_KEYCLASS_PUBLIC_KEY
)) {
445 const CssmData
&keyBlob
= CssmData::overlay(mKey
.KeyData
);
446 copyCssmData(keyBlob
, blob
, allocator
);
450 /* caller converts to binary and proceeds */
454 #endif /* CRYPTKIT_CSP_ENABLE */