]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_apple_csp/lib/RSA_DSA_keys.h
2 * Copyright (c) 2000-2001,2011,2013-2014 Apple 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 * RSA_DSA_keys.h - key pair support for RSA/DSA
23 #ifndef _RSA_DSA_KEYS_H_
24 #define _RSA_DSA_KEYS_H_
26 #include <AppleCSPContext.h>
27 #include <AppleCSPSession.h>
28 #include <RSA_DSA_csp.h>
29 #include "AppleCSPKeys.h"
30 #include <Security/osKeyTemplates.h>
31 #include <openssl/rsa.h>
32 #include <openssl/dsa.h>
33 #include <security_cdsa_utilities/context.h>
34 #include <security_asn1/SecNssCoder.h>
36 #define RSA_PUB_KEY_FORMAT CSSM_KEYBLOB_RAW_FORMAT_PKCS1
37 #define RSA_PRIV_KEY_FORMAT CSSM_KEYBLOB_RAW_FORMAT_PKCS8
39 #define DSA_PUB_KEY_FORMAT CSSM_KEYBLOB_RAW_FORMAT_X509
40 #define DSA_PRIV_KEY_FORMAT CSSM_KEYBLOB_RAW_FORMAT_FIPS186
42 #define DSA_MIN_KEY_SIZE 512
43 #define DSA_MAX_KEY_SIZE 4096
44 #define DSA_KEY_BITS_MASK (64 - 1) /* these bits must be zero */
45 /* i.e., aligned to 64 bits */
47 #define RSA_MAX_KEY_SIZE (8 * 1024)
48 #define RSA_MAX_PUB_EXPONENT_SIZE 64
50 /* Those max RSA sizes can be overridden with these system preferences */
51 #define kRSAKeySizePrefsDomain "com.apple.security"
52 #define kRSAMaxKeySizePref CFSTR("RSAMaxKeySize")
53 #define kRSAMaxPublicExponentPref CFSTR("RSAMaxPublicExponent")
56 * RSA version of a BinaryKey.
58 class RSABinaryKey
: public BinaryKey
{
60 RSABinaryKey(RSA
*rsaKey
= NULL
);
65 CSSM_KEYBLOB_FORMAT
&format
,
66 AppleCSPSession
&session
,
67 const CssmKey
*paramKey
, /* optional, unused here */
68 CSSM_KEYATTR_FLAGS
&attrFlags
); /* IN/OUT */
72 bool isOaep() { return mOaep
; }
73 const CSSM_DATA
&label() { return mLabel
; }
75 const CSSM_DATA
&label
);
78 * optional fields for OEAP keys
79 * (mKeyHeader.AlgorithmId == CSSM_ALGMODE_PKCS1_EME_OAEP)
85 class RSAKeyPairGenContext
:
86 public AppleCSPContext
, private AppleKeyPairGenContext
{
89 AppleCSPSession
&session
,
91 AppleCSPContext(session
) {}
93 ~RSAKeyPairGenContext() { }
95 /* no init functionality, but we need to implement it */
100 // this one is specified in, and called from, CSPFullPluginSession
102 const Context
&context
,
106 // declared in CSPFullPluginSession, but not implemented here
107 void generate(const Context
&context
, uint32
, CssmData
¶ms
, uint32
&attrCount
, Context::Attr
* &attrs
);
109 // this one is specified in, and called from, AppleKeyPairGenContext
111 const Context
&context
,
112 BinaryKey
&pubBinKey
,
113 BinaryKey
&privBinKey
,
116 }; /* KeyPairGenContext */
119 * CSPKeyInfoProvider for RSA keys
121 class RSAKeyInfoProvider
: public CSPKeyInfoProvider
125 const CssmKey
&cssmKey
,
126 AppleCSPSession
&session
);
128 static CSPKeyInfoProvider
*provider(
129 const CssmKey
&cssmKey
,
130 AppleCSPSession
&session
);
132 ~RSAKeyInfoProvider() { }
133 void CssmKeyToBinary(
134 CssmKey
*paramKey
, // optional
135 CSSM_KEYATTR_FLAGS
&attrFlags
, // IN/OUT
136 BinaryKey
**binKey
); // RETURNED
137 void QueryKeySizeInBits(
138 CSSM_KEY_SIZE
&keySize
); // RETURNED
139 bool getHashableBlob(
140 Allocator
&allocator
,
145 * DSA version of a BinaryKey.
147 class DSABinaryKey
: public BinaryKey
{
149 DSABinaryKey(DSA
*dsaKey
= NULL
);
151 void generateKeyBlob(
152 Allocator
&allocator
,
154 CSSM_KEYBLOB_FORMAT
&format
,
155 AppleCSPSession
&session
,
156 const CssmKey
*paramKey
, /* optional */
157 CSSM_KEYATTR_FLAGS
&attrFlags
); /* IN/OUT */
162 class DSAKeyPairGenContext
:
163 public AppleCSPContext
, private AppleKeyPairGenContext
{
165 DSAKeyPairGenContext(
166 AppleCSPSession
&session
,
168 AppleCSPContext(session
), mGenAttrs(NULL
) {}
170 ~DSAKeyPairGenContext() { freeGenAttrs(); }
172 /* no init functionality, but we need to implement it */
177 // this one is specified in, and called from, CSPFullPluginSession
179 const Context
&context
,
183 // this one is specified in, and called from, AppleKeyPairGenContext
185 const Context
&context
,
186 BinaryKey
&pubBinKey
,
187 BinaryKey
&privBinKey
,
190 // specified in, and called from, CSPFullPluginSessionÊ- generate parameters
192 const Context
&context
,
196 Context::Attr
* &attrs
);
199 * Necessary to handle and deflect "context changed" notification which occurs
200 * after the strange return from "generate parameters", when the plugin adds
201 * the "returned" values to the Context.
203 bool changed(const Context
&context
) { return true; }
206 uint32 keySizeInBits
,
207 const void *inSeed
, // optional
209 NSS_DSAAlgParams
&algParams
,
213 /* gross hack to store attributes "returned" from GenParams */
214 Context::Attr
*mGenAttrs
;
216 }; /* KeyPairGenContext */
219 * CSPKeyInfoProvider for DSA keys
221 class DSAKeyInfoProvider
: public CSPKeyInfoProvider
225 const CssmKey
&cssmKey
,
226 AppleCSPSession
&session
);
228 static CSPKeyInfoProvider
*provider(
229 const CssmKey
&cssmKey
,
230 AppleCSPSession
&session
);
232 ~DSAKeyInfoProvider() { }
233 void CssmKeyToBinary(
234 CssmKey
*paramKey
, // optional
235 CSSM_KEYATTR_FLAGS
&attrFlags
, // IN/OUT
236 BinaryKey
**binKey
); // RETURNED
237 void QueryKeySizeInBits(
238 CSSM_KEY_SIZE
&keySize
); // RETURNED
239 bool getHashableBlob(
240 Allocator
&allocator
,
244 #endif /* _RSA_DSA_KEYS_H_ */