]> git.saurik.com Git - apple/security.git/blob - AppleCSP/RSA_DSA/RSA_DSA_csp.cpp
Security-179.tar.gz
[apple/security.git] / AppleCSP / RSA_DSA / RSA_DSA_csp.cpp
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_csp.cpp - Algorithm factory for RSA/DSA
21 */
22
23 #include "RSA_DSA_csp.h"
24 #include "RSA_DSA_signature.h" /* raw signer */
25 #include <MiscCSPAlgs/SHA1_MD5_Object.h> /* raw digest */
26 #include <AppleCSP/SignatureContext.h>
27 #include <Security/digestobject.h>
28 #include "RSA_DSA_keys.h"
29 #include "RSA_asymmetric.h"
30 #include <MiscCSPAlgs/MD2Object.h>
31 #include <Security/cssmapple.h>
32
33 #define OPENSSL_DSA_ENABLE 1
34
35 CssmAllocator *RSA_DSA_Factory::normAllocator;
36 CssmAllocator *RSA_DSA_Factory::privAllocator;
37
38 /* normally found in crypto.h, which has way too much useless cruft....move these to
39 * a local header.... */
40 extern "C" {
41 extern int CRYPTO_set_mem_functions(
42 void *(*m)(size_t),
43 void *(*r)(void *,size_t),
44 void (*f)(void *));
45 int CRYPTO_set_locked_mem_functions(
46 void *(*m)(size_t),
47 void (*free_func)(void *));
48 }
49
50 /*
51 * openssl-style memory allocator callbacks
52 */
53 static void *osMalloc(size_t size)
54 {
55 return RSA_DSA_Factory::privAllocator->malloc(size);
56 }
57 static void osFree(void *data)
58 {
59 RSA_DSA_Factory::privAllocator->free(data);
60 }
61 static void *osRealloc(void *oldPtr, size_t newSize)
62 {
63 return RSA_DSA_Factory::privAllocator->realloc(oldPtr, newSize);
64 }
65
66 RSA_DSA_Factory::RSA_DSA_Factory(CssmAllocator *normAlloc, CssmAllocator *privAlloc)
67 {
68 setNormAllocator(normAlloc);
69 setPrivAllocator(privAlloc);
70 /* once-per-address space */
71 CRYPTO_set_mem_functions(osMalloc, osRealloc, osFree);
72 CRYPTO_set_locked_mem_functions(osMalloc, osFree);
73 /* these should go in a lib somewhere */
74 ERR_load_RSA_strings();
75 ERR_load_BN_strings();
76 ERR_load_DSA_strings();
77 }
78
79 RSA_DSA_Factory::~RSA_DSA_Factory()
80 {
81 // TBD terminateCryptKit();
82 }
83
84 bool RSA_DSA_Factory::setup(
85 AppleCSPSession &session,
86 CSPFullPluginSession::CSPContext * &cspCtx,
87 const Context &context)
88 {
89 switch(context.type()) {
90 case CSSM_ALGCLASS_SIGNATURE:
91 switch(context.algorithm()) {
92 case CSSM_ALGID_SHA1WithRSA:
93 if(cspCtx == NULL) {
94 cspCtx = new SignatureContext(session,
95 *(new SHA1Object()),
96 *(new RSASigner(*privAllocator,
97 session,
98 CSSM_ALGID_SHA1)));
99 }
100 return true;
101 case CSSM_ALGID_MD5WithRSA:
102 if(cspCtx == NULL) {
103 cspCtx = new SignatureContext(session,
104 *(new MD5Object()),
105 *(new RSASigner(*privAllocator,
106 session,
107 CSSM_ALGID_MD5)));
108 }
109 return true;
110 case CSSM_ALGID_MD2WithRSA:
111 if(cspCtx == NULL) {
112 cspCtx = new SignatureContext(session,
113 *(new MD2Object()),
114 *(new RSASigner(*privAllocator,
115 session,
116 CSSM_ALGID_MD2)));
117 }
118 return true;
119 #if OPENSSL_DSA_ENABLE
120 case CSSM_ALGID_SHA1WithDSA:
121 if(cspCtx == NULL) {
122 cspCtx = new SignatureContext(session,
123 *(new SHA1Object()),
124 *(new DSASigner(*privAllocator,
125 session,
126 CSSM_ALGID_SHA1)));
127 }
128 return true;
129 case CSSM_ALGID_DSA:
130 if(cspCtx == NULL) {
131 cspCtx = new SignatureContext(session,
132 *(new NullDigest()),
133 *(new DSASigner(*privAllocator,
134 session,
135 // set later via setDigestAlgorithm but not used by DSA
136 CSSM_ALGID_NONE)));
137 }
138 return true;
139 #endif
140 case CSSM_ALGID_RSA:
141 if(cspCtx == NULL) {
142 cspCtx = new SignatureContext(session,
143 *(new NullDigest()),
144 *(new RSASigner(*privAllocator,
145 session,
146 // set later via setDigestAlgorithm
147 CSSM_ALGID_NONE)));
148 }
149 return true;
150 default:
151 break;
152 }
153 break;
154
155 case CSSM_ALGCLASS_KEYGEN:
156 switch(context.algorithm()) {
157 case CSSM_ALGID_RSA:
158 if(cspCtx == NULL) {
159 cspCtx = new RSAKeyPairGenContext(session, context);
160 }
161 return true;
162 #if OPENSSL_DSA_ENABLE
163 case CSSM_ALGID_DSA:
164 if(cspCtx == NULL) {
165 cspCtx = new DSAKeyPairGenContext(session, context);
166 }
167 return true;
168 #endif
169 default:
170 break;
171 }
172 break;
173
174 case CSSM_ALGCLASS_ASYMMETRIC:
175 switch(context.algorithm()) {
176 case CSSM_ALGID_RSA:
177 if(cspCtx == NULL) {
178 cspCtx = new RSA_CryptContext(session);
179 }
180 return true;
181 default:
182 break;
183 }
184 break;
185
186 /* more here - symmetric, etc. */
187 default:
188 break;
189 }
190 /* not implemented here */
191 return false;
192 }
193
194
195