2 * Copyright (c) 2000-2001,2011,2014 Apple Inc. All Rights Reserved.
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
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.
20 * RSA_DSA_csp.cpp - Algorithm factory for RSA/DSA
23 #include "RSA_DSA_csp.h"
24 #include "RSA_DSA_signature.h" /* raw signer */
25 #include <SHA1_MD5_Object.h> /* raw digest */
26 #include <SignatureContext.h>
27 #include <security_cdsa_utilities/digestobject.h>
28 #include "RSA_DSA_keys.h"
29 #include "RSA_asymmetric.h"
30 #include <MD2Object.h>
31 #include <SHA2_Object.h>
32 #include <Security/cssmapple.h>
34 #define OPENSSL_DSA_ENABLE 1
36 Allocator
*RSA_DSA_Factory::normAllocator
;
37 Allocator
*RSA_DSA_Factory::privAllocator
;
39 /* normally found in crypto_legacy.h, which has way too much useless cruft....move these to
40 * a local header.... */
42 extern int CRYPTO_set_mem_functions(
44 void *(*r
)(void *,size_t),
46 int CRYPTO_set_locked_mem_functions(
48 void (*free_func
)(void *));
52 * openssl-style memory allocator callbacks
54 static void *osMalloc(size_t size
)
56 return RSA_DSA_Factory::privAllocator
->malloc(size
);
58 static void osFree(void *data
)
60 RSA_DSA_Factory::privAllocator
->free(data
);
62 static void *osRealloc(void *oldPtr
, size_t newSize
)
64 return RSA_DSA_Factory::privAllocator
->realloc(oldPtr
, newSize
);
67 RSA_DSA_Factory::RSA_DSA_Factory(Allocator
*normAlloc
, Allocator
*privAlloc
)
69 setNormAllocator(normAlloc
);
70 setPrivAllocator(privAlloc
);
71 /* once-per-address space */
72 CRYPTO_set_mem_functions(osMalloc
, osRealloc
, osFree
);
73 CRYPTO_set_locked_mem_functions(osMalloc
, osFree
);
74 /* these should go in a lib somewhere */
75 ERR_load_RSA_strings();
76 ERR_load_BN_strings();
77 ERR_load_DSA_strings();
80 RSA_DSA_Factory::~RSA_DSA_Factory()
82 // TBD terminateCryptKit();
85 bool RSA_DSA_Factory::setup(
86 AppleCSPSession
&session
,
87 CSPFullPluginSession::CSPContext
* &cspCtx
,
88 const Context
&context
)
90 switch(context
.type()) {
91 case CSSM_ALGCLASS_SIGNATURE
:
92 switch(context
.algorithm()) {
93 case CSSM_ALGID_SHA1WithRSA
:
95 cspCtx
= new SignatureContext(session
,
97 *(new RSASigner(*privAllocator
,
102 case CSSM_ALGID_MD5WithRSA
:
104 cspCtx
= new SignatureContext(session
,
106 *(new RSASigner(*privAllocator
,
111 case CSSM_ALGID_MD2WithRSA
:
113 cspCtx
= new SignatureContext(session
,
115 *(new RSASigner(*privAllocator
,
120 #if OPENSSL_DSA_ENABLE
121 case CSSM_ALGID_SHA1WithDSA
:
123 cspCtx
= new SignatureContext(session
,
125 *(new DSASigner(*privAllocator
,
132 cspCtx
= new SignatureContext(session
,
134 *(new DSASigner(*privAllocator
,
136 // set later via setDigestAlgorithm but not used by DSA
143 cspCtx
= new SignatureContext(session
,
145 *(new RSASigner(*privAllocator
,
147 // set later via setDigestAlgorithm
151 case CSSM_ALGID_SHA256WithRSA
:
153 cspCtx
= new SignatureContext(session
,
154 *(new SHA256Object()),
155 *(new RSASigner(*privAllocator
,
157 CSSM_ALGID_SHA256
)));
160 case CSSM_ALGID_SHA224WithRSA
:
162 cspCtx
= new SignatureContext(session
,
163 *(new SHA224Object()),
164 *(new RSASigner(*privAllocator
,
166 CSSM_ALGID_SHA224
)));
169 case CSSM_ALGID_SHA384WithRSA
:
171 cspCtx
= new SignatureContext(session
,
172 *(new SHA384Object()),
173 *(new RSASigner(*privAllocator
,
175 CSSM_ALGID_SHA384
)));
178 case CSSM_ALGID_SHA512WithRSA
:
180 cspCtx
= new SignatureContext(session
,
181 *(new SHA512Object()),
182 *(new RSASigner(*privAllocator
,
184 CSSM_ALGID_SHA512
)));
192 case CSSM_ALGCLASS_KEYGEN
:
193 switch(context
.algorithm()) {
195 case CSSM_ALGMODE_PKCS1_EME_OAEP
:
197 cspCtx
= new RSAKeyPairGenContext(session
, context
);
200 #if OPENSSL_DSA_ENABLE
203 cspCtx
= new DSAKeyPairGenContext(session
, context
);
212 case CSSM_ALGCLASS_ASYMMETRIC
:
213 switch(context
.algorithm()) {
215 case CSSM_ALGMODE_PKCS1_EME_OAEP
:
217 cspCtx
= new RSA_CryptContext(session
);
225 /* more here - symmetric, etc. */
229 /* not implemented here */