]>
Commit | Line | Data |
---|---|---|
b1ab9ed8 A |
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 | #ifdef BSAFE_CSP_ENABLE | |
19 | ||
20 | ||
21 | // | |
22 | // algmaker - algorithm factory for BSafe 4 | |
23 | // | |
24 | #include "bsafecspi.h" | |
25 | #include "bsafecsp.h" | |
26 | #include "AppleCSPSession.h" | |
27 | ||
28 | // | |
29 | // BSafe's Chooser table. | |
30 | // These are references to all *BSafe algorithms* we use (and thus must link in) | |
31 | // | |
32 | const B_ALGORITHM_METHOD * const BSafe::bsChooser[] = { | |
33 | // digests | |
34 | &AM_SHA, | |
35 | &AM_MD5, | |
36 | &AM_MD2, | |
37 | ||
38 | // organizational | |
39 | &AM_CBC_ENCRYPT, | |
40 | &AM_CBC_DECRYPT, | |
41 | &AM_ECB_ENCRYPT, | |
42 | &AM_ECB_DECRYPT, | |
43 | &AM_OFB_ENCRYPT, | |
44 | &AM_OFB_DECRYPT, | |
45 | ||
46 | // DES & variants | |
47 | &AM_DES_ENCRYPT, | |
48 | &AM_DES_DECRYPT, | |
49 | &AM_DESX_ENCRYPT, | |
50 | &AM_DESX_DECRYPT, | |
51 | &AM_DES_EDE_ENCRYPT, | |
52 | &AM_DES_EDE_DECRYPT, | |
53 | ||
54 | // RCn stuff | |
55 | &AM_RC2_CBC_ENCRYPT, | |
56 | &AM_RC2_CBC_DECRYPT, | |
57 | &AM_RC2_ENCRYPT, | |
58 | &AM_RC2_DECRYPT, | |
59 | &AM_RC4_ENCRYPT, | |
60 | &AM_RC4_DECRYPT, | |
61 | &AM_RC5_ENCRYPT, | |
62 | &AM_RC5_DECRYPT, | |
63 | &AM_RC5_CBC_ENCRYPT, | |
64 | &AM_RC5_CBC_DECRYPT, | |
65 | ||
66 | // RSA | |
67 | &AM_RSA_STRONG_KEY_GEN, | |
68 | &AM_RSA_KEY_GEN, | |
69 | &AM_RSA_CRT_ENCRYPT_BLIND, | |
70 | &AM_RSA_CRT_DECRYPT_BLIND, | |
71 | &AM_RSA_ENCRYPT, | |
72 | &AM_RSA_DECRYPT, | |
73 | ||
74 | // DSA | |
75 | &AM_DSA_PARAM_GEN, | |
76 | &AM_DSA_KEY_GEN, | |
77 | ||
78 | // signatures | |
79 | &AM_DSA_SIGN, | |
80 | &AM_DSA_VERIFY, | |
81 | ||
82 | // random number generation | |
83 | &AM_MD5_RANDOM, | |
84 | &AM_SHA_RANDOM, | |
85 | ||
86 | // sentinel | |
87 | (B_ALGORITHM_METHOD *)NULL_PTR | |
88 | }; | |
89 | ||
90 | ||
91 | // | |
92 | // Makers | |
93 | // | |
94 | template <class Ctx> | |
95 | class Maker0 : public BSafe::MakerBase { | |
96 | public: | |
97 | Ctx *make(AppleCSPSession &session, const Context &context) const | |
98 | { return new Ctx(session, context); } | |
99 | }; | |
100 | ||
101 | template <class Ctx, class Arg> | |
102 | class Maker1 : public BSafe::MakerBase { | |
103 | Arg arg; | |
104 | public: | |
105 | Maker1(Arg a) : arg(a) { } | |
106 | Ctx *make(AppleCSPSession &session, const Context &context) const | |
107 | { return new Ctx(session, context, arg); } | |
108 | }; | |
109 | ||
110 | template <class Ctx, class Arg1, class Arg2> | |
111 | class Maker2 : public BSafe::MakerBase { | |
112 | Arg1 arg1; Arg2 arg2; | |
113 | public: | |
114 | Maker2(Arg1 a1, Arg2 a2) : arg1(a1), arg2(a2) { } | |
115 | Ctx *make(AppleCSPSession &session, const Context &context) const | |
116 | { return new Ctx(session, context, arg1, arg2); } | |
117 | }; | |
118 | ||
119 | template <class Ctx, class Arg1, class Arg2, class Arg3> | |
120 | class Maker3 : public BSafe::MakerBase { | |
121 | Arg1 arg1; Arg2 arg2; Arg3 arg3; | |
122 | public: | |
123 | Maker3(Arg1 a1, Arg2 a2, Arg3 a3) : | |
124 | arg1(a1), arg2(a2), arg3(a3) { } | |
125 | Ctx *make(AppleCSPSession &session, const Context &context) const | |
126 | { return new Ctx(session, context, arg1, arg2, arg3); } | |
127 | }; | |
128 | ||
129 | ||
130 | bug_const BSafe::MakerTable BSafe::algorithms[] = { | |
131 | // signing algorithms | |
132 | // constructor args: BSafe algorithm, signature size | |
133 | { | |
134 | CSSM_ALGID_SHA1WithDSA, | |
135 | CSSM_ALGCLASS_SIGNATURE, | |
136 | new Maker2<SigningContext, B_INFO_TYPE, size_t> | |
137 | (AI_DSAWithSHA1, 48) // max size of 48 bytes | |
138 | }, | |
139 | { | |
140 | CSSM_ALGID_SHA1WithRSA, | |
141 | CSSM_ALGCLASS_SIGNATURE, | |
142 | new Maker2<SigningContext, B_INFO_TYPE, size_t> | |
143 | (AI_SHA1WithRSAEncryption, 0) // size = RSA key size | |
144 | }, | |
145 | ||
146 | { | |
147 | CSSM_ALGID_MD5WithRSA, | |
148 | CSSM_ALGCLASS_SIGNATURE, | |
149 | new Maker2<SigningContext, B_INFO_TYPE, size_t> | |
150 | (AI_MD5WithRSAEncryption, 0) // size = RSA key size | |
151 | }, | |
152 | ||
153 | { | |
154 | CSSM_ALGID_MD2WithRSA, | |
155 | CSSM_ALGCLASS_SIGNATURE, | |
156 | new Maker2<SigningContext, B_INFO_TYPE, size_t> | |
157 | (AI_MD2WithRSAEncryption, 0) // size = RSA key size | |
158 | }, | |
159 | ||
160 | // MAC algorithms | |
161 | // constructor args: BSafe algorithm, signature size | |
162 | { | |
163 | CSSM_ALGID_SHA1HMAC, | |
164 | CSSM_ALGCLASS_MAC, | |
165 | new Maker2<MacContext, B_INFO_TYPE, size_t> | |
166 | (AI_SHA1, 20) | |
167 | }, | |
168 | ||
169 | // symmetric key generation | |
170 | // constructor args: min/max key size in bits, mustBeByteSized | |
171 | { | |
172 | CSSM_ALGID_RC2, | |
173 | CSSM_ALGCLASS_KEYGEN, | |
174 | new Maker3<SymmetricKeyGenContext, uint32, uint32, bool> | |
175 | (1*8, 128*8, true) | |
176 | }, | |
177 | { | |
178 | CSSM_ALGID_RC4, | |
179 | CSSM_ALGCLASS_KEYGEN, | |
180 | new Maker3<SymmetricKeyGenContext, uint32, uint32, bool> | |
181 | (1*8, 256*8, true) | |
182 | }, | |
183 | { | |
184 | CSSM_ALGID_RC5, | |
185 | CSSM_ALGCLASS_KEYGEN, | |
186 | new Maker3<SymmetricKeyGenContext, uint32, uint32, bool> | |
187 | (1*8, 255*8, true) | |
188 | }, | |
189 | { | |
190 | CSSM_ALGID_DES, | |
191 | CSSM_ALGCLASS_KEYGEN, | |
192 | new Maker3<SymmetricKeyGenContext, uint32, uint32, bool> | |
193 | (64, 64, true) | |
194 | }, | |
195 | { | |
196 | CSSM_ALGID_DESX, | |
197 | CSSM_ALGCLASS_KEYGEN, | |
198 | new Maker3<SymmetricKeyGenContext, uint32, uint32, bool> | |
199 | (192, 192, true) | |
200 | }, | |
201 | { | |
202 | CSSM_ALGID_3DES_3KEY, | |
203 | CSSM_ALGCLASS_KEYGEN, | |
204 | new Maker3<SymmetricKeyGenContext, uint32, uint32, bool> | |
205 | (192, 192, true) | |
206 | }, | |
207 | { | |
208 | CSSM_ALGID_SHA1HMAC, | |
209 | CSSM_ALGCLASS_KEYGEN, | |
210 | new Maker3<SymmetricKeyGenContext, uint32, uint32, bool> | |
211 | (160, 2048, true) | |
212 | }, | |
213 | ||
214 | // symmetric encryption algorithms | |
215 | // constructor arg: block size (1 ==> stream cipher) | |
216 | { | |
217 | CSSM_ALGID_DES, | |
218 | CSSM_ALGCLASS_SYMMETRIC, | |
219 | new Maker1<BlockCipherContext, size_t>(8) | |
220 | }, | |
221 | { | |
222 | CSSM_ALGID_DESX, | |
223 | CSSM_ALGCLASS_SYMMETRIC, | |
224 | new Maker1<BlockCipherContext, size_t>(8) | |
225 | }, | |
226 | { | |
227 | CSSM_ALGID_3DES_3KEY_EDE, | |
228 | CSSM_ALGCLASS_SYMMETRIC, | |
229 | new Maker1<BlockCipherContext, size_t>(8) | |
230 | }, | |
231 | { | |
232 | CSSM_ALGID_RC2, | |
233 | CSSM_ALGCLASS_SYMMETRIC, | |
234 | new Maker1<BlockCipherContext, size_t>(8) | |
235 | }, | |
236 | { | |
237 | CSSM_ALGID_RC4, | |
238 | CSSM_ALGCLASS_SYMMETRIC, | |
239 | new Maker1<BlockCipherContext, size_t>(1) | |
240 | }, | |
241 | { | |
242 | CSSM_ALGID_RC5, | |
243 | CSSM_ALGCLASS_SYMMETRIC, | |
244 | new Maker1<BlockCipherContext, size_t>(8) | |
245 | }, | |
246 | ||
247 | // asymmetric encryption algorithms | |
248 | { | |
249 | CSSM_ALGID_RSA, | |
250 | CSSM_ALGCLASS_ASYMMETRIC, | |
251 | new Maker0<PublicKeyCipherContext>() | |
252 | }, | |
253 | { | |
254 | CSSM_ALGID_DSA, | |
255 | CSSM_ALGCLASS_ASYMMETRIC, | |
256 | new Maker0<PublicKeyCipherContext>() | |
257 | }, | |
258 | ||
259 | // key pair generate algorithms | |
260 | { | |
261 | CSSM_ALGID_RSA, | |
262 | CSSM_ALGCLASS_KEYGEN, | |
263 | new Maker0<BSafeKeyPairGenContext>() | |
264 | }, | |
265 | { | |
266 | CSSM_ALGID_DSA, | |
267 | CSSM_ALGCLASS_KEYGEN, | |
268 | new Maker0<BSafeKeyPairGenContext>() | |
269 | }, | |
270 | ||
271 | // pseudo-random number generators | |
272 | { | |
273 | CSSM_ALGID_MD5Random, | |
274 | CSSM_ALGCLASS_RANDOMGEN, | |
275 | new Maker1<RandomContext, B_INFO_TYPE>(AI_MD5Random) | |
276 | }, | |
277 | { | |
278 | CSSM_ALGID_SHARandom, | |
279 | CSSM_ALGCLASS_RANDOMGEN, | |
280 | new Maker1<RandomContext, B_INFO_TYPE>(AI_SHA1Random) | |
281 | }, | |
282 | }; | |
283 | ||
284 | const unsigned int BSafe::algorithmCount = sizeof(algorithms) / sizeof(algorithms[0]); | |
285 | ||
286 | ||
287 | // | |
288 | // BSafeFactory hookup | |
289 | // | |
290 | void BSafeFactory::setNormAllocator(Allocator *alloc) | |
291 | { | |
292 | BSafe::setNormAllocator(alloc); | |
293 | } | |
294 | void BSafeFactory::setPrivAllocator(Allocator *alloc) | |
295 | { | |
296 | BSafe::setPrivAllocator(alloc); | |
297 | } | |
298 | ||
299 | bool BSafeFactory::setup( | |
300 | AppleCSPSession &session, | |
301 | CSPFullPluginSession::CSPContext * &cspCtx, | |
302 | const Context &context) | |
303 | { | |
304 | return BSafe::setup(session, cspCtx, context); | |
305 | } | |
306 | ||
307 | ||
308 | // | |
309 | // Algorithm setup | |
310 | // | |
311 | bool BSafe::setup( | |
312 | AppleCSPSession &session, | |
313 | CSPFullPluginSession::CSPContext * &cspCtx, | |
314 | const Context &context) | |
315 | { | |
316 | for (const BSafe::MakerTable *alg = algorithms; | |
317 | alg < algorithms + algorithmCount; | |
318 | alg++) { | |
319 | if ((alg->algorithmId == context.algorithm()) && | |
320 | (alg->algClass == context.type())) { | |
321 | if(cspCtx != NULL) { | |
322 | /* we allow reuse */ | |
323 | return true; | |
324 | } | |
325 | // make new context | |
326 | cspCtx = alg->maker->make(session, context); | |
327 | return true; | |
328 | } | |
329 | } | |
330 | /* not ours */ | |
331 | return false; | |
332 | } | |
333 | #endif /* BSAFE_CSP_ENABLE */ |