]> git.saurik.com Git - apple/security.git/blob - AppleCSP/RSA_DSA/RSA_DSA_keys.h
Security-30.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 public:
96 RSAKeyInfoProvider(
97 const CssmKey &cssmKey);
98 ~RSAKeyInfoProvider() { }
99 void CssmKeyToBinary(
100 BinaryKey **binKey); // RETURNED
101 void QueryKeySizeInBits(
102 CSSM_KEY_SIZE &keySize); // RETURNED
103 };
104
105 /*
106 * DSA version of a BinaryKey.
107 */
108 class DSABinaryKey : public BinaryKey {
109 public:
110 DSABinaryKey(DSA *dsaKey = NULL);
111 ~DSABinaryKey();
112 void generateKeyBlob(
113 CssmAllocator &allocator,
114 CssmData &blob,
115 CSSM_KEYBLOB_FORMAT &format);
116
117 DSA *mDsaKey;
118 };
119
120 class DSAKeyPairGenContext :
121 public AppleCSPContext, private AppleKeyPairGenContext {
122 public:
123 DSAKeyPairGenContext(
124 AppleCSPSession &session,
125 const Context &) :
126 AppleCSPContext(session), mGenAttrs(NULL) {}
127
128 ~DSAKeyPairGenContext() { freeGenAttrs(); }
129
130 /* no init functionality, but we need to implement it */
131 void init(
132 const Context &,
133 bool) { }
134
135 // this one is specified in, and called from, CSPFullPluginSession
136 void generate(
137 const Context &context,
138 CssmKey &pubKey,
139 CssmKey &privKey);
140
141 // this one is specified in, and called from, AppleKeyPairGenContext
142 void generate(
143 const Context &context,
144 BinaryKey &pubBinKey,
145 BinaryKey &privBinKey,
146 uint32 &keySize);
147
148 // specified in, and called from, CSPFullPluginSessionÊ- generate parameters
149 void generate(
150 const Context &context,
151 uint32 bitSize,
152 CssmData &params,
153 uint32 &attrCount,
154 Context::Attr * &attrs);
155
156 /*
157 * Necessary to handle and deflect "context changed" notification which occurs
158 * after the strange return from "generate parameters", when the plugin adds
159 * the "returned" values to the Context.
160 */
161 bool changed(const Context &context) { return true; }
162
163 void dsaGenParams(
164 uint32 keySizeInBits,
165 const void *inSeed, // optional
166 unsigned inSeedLen,
167 DSAAlgParams &algParams);
168
169 private:
170 /* gross hack to store attributes "returned" from GenParams */
171 Context::Attr *mGenAttrs;
172 void freeGenAttrs();
173 }; /* KeyPairGenContext */
174
175 /*
176 * CSPKeyInfoProvider for DSA keys
177 */
178 class DSAKeyInfoProvider : public CSPKeyInfoProvider
179 {
180 public:
181 DSAKeyInfoProvider(
182 const CssmKey &cssmKey);
183 ~DSAKeyInfoProvider() { }
184 void CssmKeyToBinary(
185 BinaryKey **binKey); // RETURNED
186 void QueryKeySizeInBits(
187 CSSM_KEY_SIZE &keySize); // RETURNED
188 };
189
190 #endif /* _RSA_DSA_KEYS_H_ */