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