]>
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 // this one is specified in, and called from, AppleKeyPairGenContext
108 const Context
&context
,
109 BinaryKey
&pubBinKey
,
110 BinaryKey
&privBinKey
,
113 }; /* KeyPairGenContext */
116 * CSPKeyInfoProvider for RSA keys
118 class RSAKeyInfoProvider
: public CSPKeyInfoProvider
122 const CssmKey
&cssmKey
,
123 AppleCSPSession
&session
);
125 static CSPKeyInfoProvider
*provider(
126 const CssmKey
&cssmKey
,
127 AppleCSPSession
&session
);
129 ~RSAKeyInfoProvider() { }
130 void CssmKeyToBinary(
131 CssmKey
*paramKey
, // optional
132 CSSM_KEYATTR_FLAGS
&attrFlags
, // IN/OUT
133 BinaryKey
**binKey
); // RETURNED
134 void QueryKeySizeInBits(
135 CSSM_KEY_SIZE
&keySize
); // RETURNED
136 bool getHashableBlob(
137 Allocator
&allocator
,
142 * DSA version of a BinaryKey.
144 class DSABinaryKey
: public BinaryKey
{
146 DSABinaryKey(DSA
*dsaKey
= NULL
);
148 void generateKeyBlob(
149 Allocator
&allocator
,
151 CSSM_KEYBLOB_FORMAT
&format
,
152 AppleCSPSession
&session
,
153 const CssmKey
*paramKey
, /* optional */
154 CSSM_KEYATTR_FLAGS
&attrFlags
); /* IN/OUT */
159 class DSAKeyPairGenContext
:
160 public AppleCSPContext
, private AppleKeyPairGenContext
{
162 DSAKeyPairGenContext(
163 AppleCSPSession
&session
,
165 AppleCSPContext(session
), mGenAttrs(NULL
) {}
167 ~DSAKeyPairGenContext() { freeGenAttrs(); }
169 /* no init functionality, but we need to implement it */
174 // this one is specified in, and called from, CSPFullPluginSession
176 const Context
&context
,
180 // this one is specified in, and called from, AppleKeyPairGenContext
182 const Context
&context
,
183 BinaryKey
&pubBinKey
,
184 BinaryKey
&privBinKey
,
187 // specified in, and called from, CSPFullPluginSessionÊ- generate parameters
189 const Context
&context
,
193 Context::Attr
* &attrs
);
196 * Necessary to handle and deflect "context changed" notification which occurs
197 * after the strange return from "generate parameters", when the plugin adds
198 * the "returned" values to the Context.
200 bool changed(const Context
&context
) { return true; }
203 uint32 keySizeInBits
,
204 const void *inSeed
, // optional
206 NSS_DSAAlgParams
&algParams
,
210 /* gross hack to store attributes "returned" from GenParams */
211 Context::Attr
*mGenAttrs
;
213 }; /* KeyPairGenContext */
216 * CSPKeyInfoProvider for DSA keys
218 class DSAKeyInfoProvider
: public CSPKeyInfoProvider
222 const CssmKey
&cssmKey
,
223 AppleCSPSession
&session
);
225 static CSPKeyInfoProvider
*provider(
226 const CssmKey
&cssmKey
,
227 AppleCSPSession
&session
);
229 ~DSAKeyInfoProvider() { }
230 void CssmKeyToBinary(
231 CssmKey
*paramKey
, // optional
232 CSSM_KEYATTR_FLAGS
&attrFlags
, // IN/OUT
233 BinaryKey
**binKey
); // RETURNED
234 void QueryKeySizeInBits(
235 CSSM_KEY_SIZE
&keySize
); // RETURNED
236 bool getHashableBlob(
237 Allocator
&allocator
,
241 #endif /* _RSA_DSA_KEYS_H_ */