]> git.saurik.com Git - apple/security.git/blob - AppleCSP/RSA_DSA/RSA_DSA_keys.h
Security-163.tar.gz
[apple/security.git] / AppleCSP / RSA_DSA / RSA_DSA_keys.h
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, 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_keys.h - key pair support for RSA/DSA
21 */
22
23 #ifndef _RSA_DSA_KEYS_H_
24 #define _RSA_DSA_KEYS_H_
25
26 #include <AppleCSP/AppleCSPContext.h>
27 #include <AppleCSP/AppleCSPSession.h>
28 #include <RSA_DSA/RSA_DSA_csp.h>
29 #include "AppleCSPKeys.h"
30 #include <opensslUtils/osKeyTemplates.h>
31 #include <openssl/rsa.h>
32 #include <openssl/dsa.h>
33 #include <Security/context.h>
34 #include <SecurityNssAsn1/SecNssCoder.h>
35
36 #define RSA_PUB_KEY_FORMAT CSSM_KEYBLOB_RAW_FORMAT_PKCS1
37 #define RSA_PRIV_KEY_FORMAT CSSM_KEYBLOB_RAW_FORMAT_PKCS8
38
39 #define DSA_PUB_KEY_FORMAT CSSM_KEYBLOB_RAW_FORMAT_X509
40 #define DSA_PRIV_KEY_FORMAT CSSM_KEYBLOB_RAW_FORMAT_FIPS186
41
42 #define DSA_MIN_KEY_SIZE 512
43 #define DSA_MAX_KEY_SIZE 1024
44 #define DSA_KEY_BITS_MASK (64 - 1) /* these bits must be zero */
45 /* i.e., aligned to 64 bits */
46 /*
47 * RSA version of a BinaryKey.
48 */
49 class RSABinaryKey : public BinaryKey {
50 public:
51 RSABinaryKey(RSA *rsaKey = NULL);
52 ~RSABinaryKey();
53 void generateKeyBlob(
54 CssmAllocator &allocator,
55 CssmData &blob,
56 CSSM_KEYBLOB_FORMAT &format,
57 AppleCSPSession &session,
58 const CssmKey *paramKey, /* optional, unused here */
59 CSSM_KEYATTR_FLAGS &attrFlags); /* IN/OUT */
60
61 RSA *mRsaKey;
62 };
63
64 class RSAKeyPairGenContext :
65 public AppleCSPContext, private AppleKeyPairGenContext {
66 public:
67 RSAKeyPairGenContext(
68 AppleCSPSession &session,
69 const Context &) :
70 AppleCSPContext(session) {}
71
72 ~RSAKeyPairGenContext() { }
73
74 /* no init functionality, but we need to implement it */
75 void init(
76 const Context &,
77 bool) { }
78
79 // this one is specified in, and called from, CSPFullPluginSession
80 void generate(
81 const Context &context,
82 CssmKey &pubKey,
83 CssmKey &privKey);
84
85 // this one is specified in, and called from, AppleKeyPairGenContext
86 void generate(
87 const Context &context,
88 BinaryKey &pubBinKey,
89 BinaryKey &privBinKey,
90 uint32 &keySize);
91
92 }; /* KeyPairGenContext */
93
94 /*
95 * CSPKeyInfoProvider for RSA keys
96 */
97 class RSAKeyInfoProvider : public CSPKeyInfoProvider
98 {
99 private:
100 RSAKeyInfoProvider(
101 const CssmKey &cssmKey,
102 AppleCSPSession &session);
103 public:
104 static CSPKeyInfoProvider *provider(
105 const CssmKey &cssmKey,
106 AppleCSPSession &session);
107
108 ~RSAKeyInfoProvider() { }
109 void CssmKeyToBinary(
110 CssmKey *paramKey, // optional
111 CSSM_KEYATTR_FLAGS &attrFlags, // IN/OUT
112 BinaryKey **binKey); // RETURNED
113 void QueryKeySizeInBits(
114 CSSM_KEY_SIZE &keySize); // RETURNED
115 bool getHashableBlob(
116 CssmAllocator &allocator,
117 CssmData &hashBlob);
118 };
119
120 /*
121 * DSA version of a BinaryKey.
122 */
123 class DSABinaryKey : public BinaryKey {
124 public:
125 DSABinaryKey(DSA *dsaKey = NULL);
126 ~DSABinaryKey();
127 void generateKeyBlob(
128 CssmAllocator &allocator,
129 CssmData &blob,
130 CSSM_KEYBLOB_FORMAT &format,
131 AppleCSPSession &session,
132 const CssmKey *paramKey, /* optional */
133 CSSM_KEYATTR_FLAGS &attrFlags); /* IN/OUT */
134
135 DSA *mDsaKey;
136 };
137
138 class DSAKeyPairGenContext :
139 public AppleCSPContext, private AppleKeyPairGenContext {
140 public:
141 DSAKeyPairGenContext(
142 AppleCSPSession &session,
143 const Context &) :
144 AppleCSPContext(session), mGenAttrs(NULL) {}
145
146 ~DSAKeyPairGenContext() { freeGenAttrs(); }
147
148 /* no init functionality, but we need to implement it */
149 void init(
150 const Context &,
151 bool) { }
152
153 // this one is specified in, and called from, CSPFullPluginSession
154 void generate(
155 const Context &context,
156 CssmKey &pubKey,
157 CssmKey &privKey);
158
159 // this one is specified in, and called from, AppleKeyPairGenContext
160 void generate(
161 const Context &context,
162 BinaryKey &pubBinKey,
163 BinaryKey &privBinKey,
164 uint32 &keySize);
165
166 // specified in, and called from, CSPFullPluginSessionÊ- generate parameters
167 void generate(
168 const Context &context,
169 uint32 bitSize,
170 CssmData &params,
171 uint32 &attrCount,
172 Context::Attr * &attrs);
173
174 /*
175 * Necessary to handle and deflect "context changed" notification which occurs
176 * after the strange return from "generate parameters", when the plugin adds
177 * the "returned" values to the Context.
178 */
179 bool changed(const Context &context) { return true; }
180
181 void dsaGenParams(
182 uint32 keySizeInBits,
183 const void *inSeed, // optional
184 unsigned inSeedLen,
185 NSS_DSAAlgParams &algParams,
186 SecNssCoder &coder);
187
188 private:
189 /* gross hack to store attributes "returned" from GenParams */
190 Context::Attr *mGenAttrs;
191 void freeGenAttrs();
192 }; /* KeyPairGenContext */
193
194 /*
195 * CSPKeyInfoProvider for DSA keys
196 */
197 class DSAKeyInfoProvider : public CSPKeyInfoProvider
198 {
199 private:
200 DSAKeyInfoProvider(
201 const CssmKey &cssmKey,
202 AppleCSPSession &session);
203 public:
204 static CSPKeyInfoProvider *provider(
205 const CssmKey &cssmKey,
206 AppleCSPSession &session);
207
208 ~DSAKeyInfoProvider() { }
209 void CssmKeyToBinary(
210 CssmKey *paramKey, // optional
211 CSSM_KEYATTR_FLAGS &attrFlags, // IN/OUT
212 BinaryKey **binKey); // RETURNED
213 void QueryKeySizeInBits(
214 CSSM_KEY_SIZE &keySize); // RETURNED
215 bool getHashableBlob(
216 CssmAllocator &allocator,
217 CssmData &hashBlob);
218 };
219
220 #endif /* _RSA_DSA_KEYS_H_ */