]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_apple_csp/lib/FEEKeys.cpp
7ce182480cbb06e076cfeb72b1ae713852292d95
[apple/security.git] / OSX / libsecurity_apple_csp / lib / FEEKeys.cpp
1 /*
2 * Copyright (c) 2000-2001,2011-2012,2014 Apple Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 /*
20 * FEEKeys.cpp - FEE-related asymmetric key pair classes.
21 *
22 */
23
24 #ifdef CRYPTKIT_CSP_ENABLE
25
26 #include "FEEKeys.h"
27 #include "FEECSPUtils.h"
28 #include "CryptKitSpace.h"
29 #include <security_cryptkit/feePublicKey.h>
30 #include <security_cryptkit/falloc.h>
31 #include <security_cdsa_utilities/cssmdata.h>
32 #include "AppleCSPSession.h"
33 #include "AppleCSPUtils.h"
34 #include <assert.h>
35 #include <security_utilities/debugging.h>
36
37 #define feeKeyDebug(args...) secinfo("feeKey", ## args)
38
39 /***
40 *** FEE-style BinaryKey
41 ***/
42
43 /* constructor with optional existing feePubKey */
44 CryptKit::FEEBinaryKey::FEEBinaryKey(feePubKey feeKey)
45 : mFeeKey(feeKey)
46 {
47 if(mFeeKey == NULL) {
48 mFeeKey = feePubKeyAlloc();
49 if(mFeeKey == NULL) {
50 CssmError::throwMe(CSSMERR_CSP_MEMORY_ERROR);
51 }
52 }
53 }
54
55 CryptKit::FEEBinaryKey::~FEEBinaryKey()
56 {
57 if(mFeeKey) {
58 feePubKeyFree(mFeeKey);
59 mFeeKey = NULL;
60 }
61 }
62
63 void CryptKit::FEEBinaryKey::generateKeyBlob(
64 Allocator &allocator,
65 CssmData &blob,
66 CSSM_KEYBLOB_FORMAT &format,
67 AppleCSPSession &session,
68 const CssmKey *paramKey, /* optional, unused here */
69 CSSM_KEYATTR_FLAGS &attrFlags) /* IN/OUT */
70 {
71 unsigned char *keyBlob;
72 unsigned len = 0;
73 feeReturn frtn = FR_Internal;
74 bool freeTheKey = false;
75 feePubKey keyToEncode = mFeeKey;
76
77 assert(mFeeKey != NULL);
78 if((format == CSSM_KEYBLOB_RAW_FORMAT_DIGEST) &&
79 (mKeyHeader.KeyClass == CSSM_KEYCLASS_PRIVATE_KEY)) {
80 /* key digest calculation; special case for private keys: cook
81 * up the associated public key and encode that */
82 keyToEncode = feePubKeyAlloc();
83 if(keyToEncode == NULL) {
84 CssmError::throwMe(CSSMERR_CSP_MEMORY_ERROR);
85 }
86 frtn = feePubKeyInitPubKeyFromPriv(mFeeKey, keyToEncode);
87 if(frtn) {
88 feePubKeyFree(keyToEncode);
89 throwCryptKit(frtn, "feePubKeyInitPubKeyFromPriv");
90 }
91 freeTheKey = true;
92 }
93
94 bool badFormat = false;
95 int isPrivate = feePubKeyIsPrivate(keyToEncode);
96
97 switch(mKeyHeader.AlgorithmId) {
98 case CSSM_ALGID_FEE:
99 if(isPrivate) {
100 /* FEE private key */
101 switch(format) {
102 case CSSM_KEYBLOB_RAW_FORMAT_DIGEST:
103 format = CSSM_KEYBLOB_RAW_FORMAT_NONE;
104 /* and drop thru */
105 case CSSM_KEYBLOB_RAW_FORMAT_NONE:
106 frtn = feePubKeyCreateDERPrivBlob(keyToEncode, &keyBlob, &len);
107 break;
108 case CSSM_KEYBLOB_RAW_FORMAT_OCTET_STRING:
109 frtn = feePubKeyCreatePrivBlob(keyToEncode, &keyBlob, &len);
110 break;
111 default:
112 badFormat = true;
113 break;
114 }
115 }
116 else {
117 /* FEE Public key */
118 switch(format) {
119 case CSSM_KEYBLOB_RAW_FORMAT_DIGEST:
120 format = CSSM_KEYBLOB_RAW_FORMAT_NONE;
121 /* and drop thru */
122 case CSSM_KEYBLOB_RAW_FORMAT_NONE:
123 frtn = feePubKeyCreateDERPubBlob(keyToEncode, &keyBlob, &len);
124 break;
125 case CSSM_KEYBLOB_RAW_FORMAT_OCTET_STRING:
126 frtn = feePubKeyCreatePubBlob(keyToEncode, &keyBlob, &len);
127 break;
128 default:
129 badFormat = true;
130 break;
131 }
132 }
133 /* end of base ALGID_FEE */
134 break;
135
136 case CSSM_ALGID_ECDSA:
137 if(isPrivate) {
138 /* ECDSA/ECDH private key */
139 switch(format) {
140 case CSSM_KEYBLOB_RAW_FORMAT_PKCS8:
141 /* ECDSA private key: PKCS8 */
142 frtn = feePubKeyCreatePKCS8Blob(keyToEncode, &keyBlob, &len);
143 break;
144 case CSSM_KEYBLOB_RAW_FORMAT_NONE:
145 /* set to default format, drop thru */
146 format = CSSM_KEYBLOB_RAW_FORMAT_OPENSSL;
147 case CSSM_KEYBLOB_RAW_FORMAT_OPENSSL:
148 /* ECDSA private key, SEC1/OpenSSL format */
149 frtn = feePubKeyCreateOpenSSLBlob(keyToEncode, &keyBlob, &len);
150 break;
151 case CSSM_KEYBLOB_RAW_FORMAT_DIGEST:
152 format = CSSM_KEYBLOB_RAW_FORMAT_OCTET_STRING;
153 /* and drop thru */
154 case CSSM_KEYBLOB_RAW_FORMAT_OCTET_STRING:
155 /* raw private key bytes */
156 frtn = feeCreateECDSAPrivBlob(keyToEncode, &keyBlob, &len);
157 break;
158 default:
159 badFormat = true;
160 break;
161 }
162 }
163 else {
164 /*
165 * ECDSA public key.
166 * Note there is no OpenSSL case here, that format is only generated for
167 * private keys.
168 */
169 switch(format) {
170 case CSSM_KEYBLOB_RAW_FORMAT_NONE:
171 /* set to default format, drop thru */
172 format = CSSM_KEYBLOB_RAW_FORMAT_X509;
173 case CSSM_KEYBLOB_RAW_FORMAT_X509:
174 /* ECDSA, public key, default: X509 */
175 frtn = feePubKeyCreateX509Blob(keyToEncode, &keyBlob, &len);
176 break;
177 case CSSM_KEYBLOB_RAW_FORMAT_DIGEST:
178 format = CSSM_KEYBLOB_RAW_FORMAT_OCTET_STRING;
179 /* and drop thru */
180 case CSSM_KEYBLOB_RAW_FORMAT_OCTET_STRING:
181 /* raw x|y string */
182 frtn = feeCreateECDSAPubBlob(keyToEncode, &keyBlob, &len);
183 break;
184 default:
185 badFormat = true;
186 break;
187 }
188 }
189 /* end of case CSSM_ALGID_ECDSA */
190 break;
191 default:
192 /* not reached */
193 break;
194 }
195
196 if(badFormat) {
197 CssmError::throwMe(isPrivate ?
198 CSSMERR_CSP_INVALID_ATTR_PRIVATE_KEY_FORMAT :
199 CSSMERR_CSP_INVALID_ATTR_PUBLIC_KEY_FORMAT);
200 }
201 if(frtn) {
202 throwCryptKit(frtn, "feePubKeyCreate*Blob");
203 }
204 setUpCssmData(blob, len, allocator);
205 memmove(blob.data(), keyBlob, len);
206 blob.length(len);
207 ffree(keyBlob);
208 if(freeTheKey) {
209 /* free the temp pub key we created here */
210 feePubKeyFree(keyToEncode);
211 }
212 }
213
214 /***
215 *** FEE-style AppleKeyPairGenContext
216 ***/
217
218 /*
219 * This one is specified in, and called from, CSPFullPluginSession. Our
220 * only job is to prepare two subclass-specific BinaryKeys and call up to
221 * AppleKeyPairGenContext.
222 */
223 void CryptKit::FEEKeyPairGenContext::generate(
224 const Context &context,
225 CssmKey &pubKey,
226 CssmKey &privKey)
227 {
228 FEEBinaryKey *pubBinKey = new FEEBinaryKey();
229 FEEBinaryKey *privBinKey = new FEEBinaryKey();
230
231 try {
232 AppleKeyPairGenContext::generate(context,
233 session(),
234 pubKey,
235 pubBinKey,
236 privKey,
237 privBinKey);
238 }
239 catch (...) {
240 delete pubBinKey;
241 delete privBinKey;
242 throw;
243 }
244
245 }
246
247 void CryptKit::FEEKeyPairGenContext::generate(const Context &context, uint32, CssmData &params, uint32 &attrCount, Context::Attr * &attrs) {
248 CssmError::throwMe(CSSM_ERRCODE_FUNCTION_NOT_IMPLEMENTED);
249 }
250
251 // this one is specified in, and called from, AppleKeyPairGenContext
252 void CryptKit::FEEKeyPairGenContext::generate(
253 const Context &context,
254 BinaryKey &pubBinKey,
255 BinaryKey &privBinKey,
256 uint32 &keyBits)
257 {
258 /*
259 * These casts throw exceptions if the keys are of the
260 * wrong classes, which would be a major bogon, since we created
261 * the keys in the above generate() function.
262 */
263 FEEBinaryKey &fPubBinKey =
264 dynamic_cast<FEEBinaryKey &>(pubBinKey);
265 FEEBinaryKey &fPrivBinKey =
266 dynamic_cast<FEEBinaryKey &>(privBinKey);
267
268 /*
269 * Two parameters from context. Key size in bits is required;
270 * seed is optional. If not present, we cook up random private data.
271 */
272 keyBits = context.getInt(CSSM_ATTRIBUTE_KEY_LENGTH,
273 CSSMERR_CSP_MISSING_ATTR_KEY_LENGTH);
274 CssmCryptoData *cseed = context.get<CssmCryptoData>(CSSM_ATTRIBUTE_SEED);
275 CssmData *seed;
276 bool haveSeed;
277 CssmAutoData aSeed(session()); // malloc on demand
278 if(cseed) {
279 /* caller specified seed */
280 haveSeed = true;
281 seed = &cseed->param();
282 }
283 else {
284 /* generate random seed */
285 haveSeed = false;
286 unsigned keyBytes = ((keyBits + 7) / 8) + 1;
287 aSeed.malloc(keyBytes);
288 session().getRandomBytes(keyBytes, aSeed);
289 seed = &aSeed.get();
290 }
291
292 CSSM_ALGORITHMS algId = context.algorithm();
293
294 /* Curve and prime types - optional */
295 feePrimeType primeType = FPT_Default;
296 uint32 uPrimeType = context.getInt(CSSM_ATTRIBUTE_FEE_PRIME_TYPE);
297 switch(uPrimeType) {
298 case CSSM_FEE_PRIME_TYPE_DEFAULT:
299 break;
300 case CSSM_FEE_PRIME_TYPE_MERSENNE:
301 primeType = FPT_Mersenne;
302 break;
303 case CSSM_FEE_PRIME_TYPE_FEE:
304 primeType = FPT_FEE;
305 break;
306 case CSSM_FEE_PRIME_TYPE_GENERAL:
307 primeType = FPT_General;
308 break;
309 default:
310 /* FIXME - maybe we should be more specific */
311 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_ALG_PARAMS);
312 }
313 feeCurveType curveType = FCT_Default;
314 switch(algId) {
315 case CSSM_ALGID_ECDSA:
316 /* no options */
317 curveType = FCT_ANSI;
318 break;
319 default:
320 {
321 uint32 uCurveType = context.getInt(CSSM_ATTRIBUTE_FEE_CURVE_TYPE);
322 switch(uCurveType) {
323 case CSSM_FEE_CURVE_TYPE_DEFAULT:
324 break;
325 case CSSM_FEE_CURVE_TYPE_MONTGOMERY:
326 curveType = FCT_Montgomery;
327 break;
328 case CSSM_FEE_CURVE_TYPE_WEIERSTRASS:
329 curveType = FCT_Weierstrass;
330 break;
331 case CSSM_FEE_CURVE_TYPE_ANSI_X9_62:
332 curveType = FCT_ANSI;
333 break;
334 default:
335 /* FIXME - maybe we should be more specific */
336 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_ALG_PARAMS);
337 }
338 break;
339 }
340 }
341 feeReturn frtn = feePubKeyInitFromPrivDataKeyBits(
342 fPrivBinKey.feeKey(),
343 (unsigned char *)seed->data(),
344 (unsigned int)seed->length(),
345 keyBits,
346 primeType,
347 curveType,
348 /*
349 * our random seed: trust it
350 * caller's seed: hash it
351 */
352 haveSeed ? 1 : 0);
353 if(frtn) {
354 throwCryptKit(frtn, "feePubKeyInitFromPrivDataKeyBits");
355 }
356 frtn = feePubKeyInitPubKeyFromPriv(fPrivBinKey.feeKey(),
357 fPubBinKey.feeKey());
358 if(frtn) {
359 throwCryptKit(frtn, "feePubKeyInitPubKeyFromPriv");
360 }
361 }
362
363
364 /***
365 *** FEE-style CSPKeyInfoProvider.
366 ***/
367 CryptKit::FEEKeyInfoProvider::FEEKeyInfoProvider(
368 const CssmKey &cssmKey,
369 AppleCSPSession &session) :
370 CSPKeyInfoProvider(cssmKey, session)
371 {
372 }
373 CSPKeyInfoProvider *FEEKeyInfoProvider::provider(
374 const CssmKey &cssmKey,
375 AppleCSPSession &session)
376 {
377 switch(cssmKey.algorithm()) {
378 case CSSM_ALGID_FEE:
379 case CSSM_ALGID_ECDSA:
380 break;
381 default:
382 return NULL;
383 }
384 switch(cssmKey.keyClass()) {
385 case CSSM_KEYCLASS_PUBLIC_KEY:
386 case CSSM_KEYCLASS_PRIVATE_KEY:
387 /* FIXME - verify proper CSSM_KEYBLOB_RAW_FORMAT_xx */
388 break;
389 default:
390 return NULL;
391 }
392 /* OK, we'll handle this one */
393 return new FEEKeyInfoProvider(cssmKey, session);
394 }
395
396 /* Given a raw key, cook up a Binary key */
397 void CryptKit::FEEKeyInfoProvider::CssmKeyToBinary(
398 CssmKey *paramKey, // optional, ignored
399 CSSM_KEYATTR_FLAGS &attrFlags, // IN/OUT
400 BinaryKey **binKey)
401 {
402 *binKey = NULL;
403 feePubKey feeKey = NULL;
404
405 /* first cook up a feePubKey, then drop that into a BinaryKey */
406 feeKey = rawCssmKeyToFee(mKey);
407 FEEBinaryKey *feeBinKey = new FEEBinaryKey(feeKey);
408 *binKey = feeBinKey;
409 }
410
411 /*
412 * Obtain key size in bits.
413 * Currently only raw public keys are dealt with (they're the ones
414 * which come from certs, the only current use for this function).
415 * Note that if we need to handle ref keys, we'll need a session ref...
416 */
417 void CryptKit::FEEKeyInfoProvider::QueryKeySizeInBits(
418 CSSM_KEY_SIZE &keySize)
419 {
420 feePubKey feeKey = NULL;
421
422 if(mKey.blobType() != CSSM_KEYBLOB_RAW) {
423 CssmError::throwMe(CSSMERR_CSP_INVALID_KEY_FORMAT);
424 }
425 feeKey = rawCssmKeyToFee(mKey);
426 keySize.LogicalKeySizeInBits = feePubKeyBitsize(feeKey);
427 keySize.EffectiveKeySizeInBits = keySize.LogicalKeySizeInBits;
428 feePubKeyFree(feeKey);
429 }
430
431 /*
432 * Obtain blob suitable for hashing in CSSM_APPLECSP_KEYDIGEST
433 * passthrough.
434 */
435 bool CryptKit::FEEKeyInfoProvider::getHashableBlob(
436 Allocator &allocator,
437 CssmData &blob) // blob to hash goes here
438 {
439 /*
440 * The optimized case, a raw key in the "proper" format already.
441 * Currently this is:
442 * FEE public key in default/NONE form (which happens to be DER)
443 */
444 assert(mKey.blobType() == CSSM_KEYBLOB_RAW);
445 if((mKey.algorithm() == CSSM_ALGID_FEE) &&
446 (mKey.blobFormat() == CSSM_KEYBLOB_RAW_FORMAT_NONE) &&
447 (mKey.keyClass() == CSSM_KEYCLASS_PUBLIC_KEY)) {
448 const CssmData &keyBlob = CssmData::overlay(mKey.KeyData);
449 copyCssmData(keyBlob, blob, allocator);
450 return true;
451 }
452
453 /* caller converts to binary and proceeds */
454 return false;
455 }
456
457 #endif /* CRYPTKIT_CSP_ENABLE */