1/*
2 * Copyright (c) 2000-2001,2011,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 * FEECSPUtils.h - Misc. utility function for FEE/CryptKit CSP.
21 *
22 */
23
24#ifdef	CRYPTKIT_CSP_ENABLE
25
26#ifndef	_FEE_CSP_UTILS_H_
27#define _FEE_CSP_UTILS_H_
28
29#include "AppleCSPSession.h"
30#include <security_cryptkit/feeTypes.h>
31#include <security_cdsa_utilities/context.h>
32
33/*
34 * Default FEE keyblob format, indicating DER-encoding.
35 * FEE keys can optionally be generated by requesting
36 * CSSM_KEYBLOB_RAW_FORMAT_OCTET_STRING, indicating native FEE key blobs.
37 */
38#define FEE_KEYBLOB_DEFAULT_FORMAT	CSSM_KEYBLOB_RAW_FORMAT_NONE
39
40namespace CryptKit {
41
42/* Given a FEE error, throw appropriate CssmError */
43void throwCryptKit(
44	feeReturn 	frtn,
45	const char	*op);		/* optional */
46
47/*
48 * Given a Context:
49 * -- obtain CSSM key of specified CSSM_ATTRIBUTE_TYPE
50 * -- validate keyClass
51 * -- validate keyUsage
52 * -- convert to feePubKey, allocating the feePubKey if necessary
53 */
54feePubKey contextToFeeKey(
55	const Context 		&context,
56	AppleCSPSession	 	&session,
57	CSSM_ATTRIBUTE_TYPE	attrType,	  // CSSM_ATTRIBUTE_KEY, CSSM_ATTRIBUTE_PUBLIC_KEY
58	CSSM_KEYCLASS		keyClass,	  // CSSM_KEYCLASS_{PUBLIC,PRIVATE}_KEY
59	CSSM_KEYUSE			usage,		  // CSSM_KEYUSE_ENCRYPT, CSSM_KEYUSE_SIGN, etc.
60	bool				&mallocdKey); // RETURNED
61
62/*
63 * Convert a CssmKey to a feePubKey. May result in the creation of a new
64 * feePubKey (when cssmKey is a raw key); allocdKey is true in that case
65 * in which case the caller generally has to free the allocd key).
66 */
67feePubKey cssmKeyToFee(
68	const CssmKey	&cssmKey,
69	AppleCSPSession	&session,
70	bool			&allocdKey);	// RETURNED
71
72/*
73 * Convert a raw CssmKey to a newly alloc'd feePubKey.
74 */
75feePubKey rawCssmKeyToFee(
76	const CssmKey	&cssmKey);
77
78/*
79 * Glue function which allows C code to use AppleCSPSession
80 * as an RNG. A ptr to this function gets passed down to
81 * CryptKit C functions as a feeRandFcn.
82 */
83feeReturn feeRandCallback(
84	void 			*ref,		// actually an AppleCSPSession *
85	unsigned char 	*bytes,		// must be alloc'd by caller
86	unsigned 		numBytes);
87
88} /* namespace CryptKit */
89
90#endif	/* _FEE_CSP_UTILS_H_ */
91#endif	/* CRYPTKIT_CSP_ENABLE */
92