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 * RSA_DSA_utils.h
21 */
22#ifndef	_RSA_DSA_UTILS_H_
23#define _RSA_DSA_UTILS_H_
24
25#include <openssl/rsa.h>
26#include <openssl/dsa.h>
27#include <AppleCSPSession.h>
28#include <security_cdsa_utilities/context.h>
29
30#ifdef	__cplusplus
31extern "C" {
32#endif
33
34uint32 rsaMaxKeySize();
35uint32 rsaMaxPubExponentSize();
36
37/*
38 * Given a Context:
39 * -- obtain CSSM key (there must only be one)
40 * -- validate keyClass
41 * -- validate keyUsage
42 * -- convert to RSA *, allocating the RSA key if necessary
43 */
44RSA *contextToRsaKey(
45	const Context 		&context,
46	AppleCSPSession	 	&session,
47	CSSM_KEYCLASS		keyClass,	  // CSSM_KEYCLASS_{PUBLIC,PRIVATE}_KEY
48	CSSM_KEYUSE			usage,		  // CSSM_KEYUSE_ENCRYPT, CSSM_KEYUSE_SIGN, etc.
49	bool				&mallocdKey,  // RETURNED
50	CSSM_DATA			&label);	  // mallocd and RETURNED for OAEP
51
52/*
53 * Convert a CssmKey to an RSA * key. May result in the creation of a new
54 * RSA (when cssmKey is a raw key); allocdKey is true in that case
55 * in which case the caller generally has to free the allocd key).
56 */
57RSA *cssmKeyToRsa(
58	const CssmKey	&cssmKey,
59	AppleCSPSession	&session,
60	bool			&allocdKey,		// RETURNED
61	CSSM_DATA		&label);		// mallocd and RETURNED for OAEP
62
63/*
64 * Convert a raw CssmKey to a newly alloc'd RSA *.
65 */
66RSA *rawCssmKeyToRsa(
67	const CssmKey	&cssmKey,
68	CSSM_DATA		&label);		// mallocd and RETURNED for OAEP keys
69
70/*
71 * Given a partially formed DSA public key (with no p, q, or g) and a
72 * CssmKey representing a supposedly fully-formed DSA key, populate
73 * the public key's p, g, and q with values from the fully formed key.
74 */
75CSSM_RETURN dsaGetParamsFromKey(
76	DSA 			*partialKey,
77	const CssmKey	&paramKey,
78	AppleCSPSession	&session);
79
80/*
81 * Given a Context:
82 * -- obtain CSSM key (there must only be one)
83 * -- validate keyClass
84 * -- validate keyUsage
85 * -- convert to DSA *, allocating the DSA key if necessary
86 */
87DSA *contextToDsaKey(
88	const Context 		&context,
89	AppleCSPSession	 	&session,
90	CSSM_KEYCLASS		keyClass,	  // CSSM_KEYCLASS_{PUBLIC,PRIVATE}_KEY
91	CSSM_KEYUSE			usage,		  // CSSM_KEYUSE_ENCRYPT, CSSM_KEYUSE_SIGN, etc.
92	bool				&mallocdKey); // RETURNED
93
94/*
95 * Convert a CssmKey to an DSA * key. May result in the creation of a new
96 * DSA (when cssmKey is a raw key); allocdKey is true in that case
97 * in which case the caller generally has to free the allocd key).
98 */
99DSA *cssmKeyToDsa(
100	const CssmKey	&cssmKey,
101	AppleCSPSession	&session,
102	bool			&allocdKey);	// RETURNED
103
104/*
105 * Convert a raw CssmKey to a newly alloc'd DSA *.
106 */
107DSA *rawCssmKeyToDsa(
108	const CssmKey	&cssmKey,
109	AppleCSPSession	&session,
110	const CssmKey	*paramKey);		// optional
111
112/*
113 * Given a DSA private key, calculate its public component if it
114 * doesn't already exist. Used for calculating the key digest of
115 * an incoming raw private key.
116 */
117void dsaKeyPrivToPub(
118	DSA *dsaKey);
119
120#ifdef	__cplusplus
121}
122#endif
123
124#endif	/*_RSA_DSA_UTILS_H_ */
125