]> git.saurik.com Git - apple/security.git/blob - AppleCSP/CryptKitCSP/cryptkitcsp.cpp
Security-179.tar.gz
[apple/security.git] / AppleCSP / CryptKitCSP / cryptkitcsp.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 // cryptkitcsp - top C++ implementation layer for CryptKit
21 //
22
23 #ifdef CRYPTKIT_CSP_ENABLE
24
25 #include "cryptkitcsp.h"
26 #include "FEESignatureObject.h" /* raw signer */
27 #include <AppleCSP/SignatureContext.h>
28 #include "FEEKeys.h"
29 #include "FEEAsymmetricContext.h"
30 #include <Security/cssmapple.h>
31 #include <CryptKit/falloc.h>
32 #include <CryptKit/feeFunctions.h>
33 #include <MiscCSPAlgs/SHA1_MD5_Object.h>
34 #include <Security/digestobject.h>
35
36 CssmAllocator *CryptKitFactory::normAllocator;
37 CssmAllocator *CryptKitFactory::privAllocator;
38
39 /*
40 * CryptKit-style memory allocator callbacks
41 */
42 static void *ckMalloc(unsigned size)
43 {
44 return CryptKitFactory::privAllocator->malloc(size);
45 }
46 static void ckFree(void *data)
47 {
48 CryptKitFactory::privAllocator->free(data);
49 }
50 static void *ckRealloc(void *oldPtr, unsigned newSize)
51 {
52 return CryptKitFactory::privAllocator->realloc(oldPtr, newSize);
53 }
54
55 //
56 // Manage the CryptKit algorithm factory
57 //
58
59 CryptKitFactory::CryptKitFactory(CssmAllocator *normAlloc, CssmAllocator *privAlloc)
60 {
61 setNormAllocator(normAlloc);
62 setPrivAllocator(privAlloc);
63 /* once-per-address space */
64 initCryptKit();
65 fallocRegister(ckMalloc, ckFree, ckRealloc);
66 }
67
68 CryptKitFactory::~CryptKitFactory()
69 {
70 terminateCryptKit();
71 }
72
73 bool CryptKitFactory::setup(
74 AppleCSPSession &session,
75 CSPFullPluginSession::CSPContext * &cspCtx,
76 const Context &context)
77 {
78 switch(context.type()) {
79 case CSSM_ALGCLASS_SIGNATURE:
80 switch(context.algorithm()) {
81 case CSSM_ALGID_FEE_MD5:
82 if(cspCtx == NULL) {
83 cspCtx = new SignatureContext(session,
84 *(new MD5Object()),
85 *(new FEERawSigner(feeRandCallback,
86 &session,
87 session,
88 *privAllocator)));
89 }
90 return true;
91 case CSSM_ALGID_FEE_SHA1:
92 if(cspCtx == NULL) {
93 cspCtx = new SignatureContext(session,
94 *(new SHA1Object()),
95 *(new FEERawSigner(feeRandCallback,
96 &session,
97 session,
98 *privAllocator)));
99 }
100 return true;
101 case CSSM_ALGID_SHA1WithECDSA:
102 if(cspCtx == NULL) {
103 cspCtx = new SignatureContext(session,
104 *(new SHA1Object()),
105 *(new FEEECDSASigner(feeRandCallback,
106 &session,
107 session,
108 *privAllocator)));
109 }
110 return true;
111 case CSSM_ALGID_FEE:
112 if(cspCtx == NULL) {
113 cspCtx = new SignatureContext(session,
114 *(new NullDigest()),
115 *(new FEERawSigner(feeRandCallback,
116 &session,
117 session,
118 *privAllocator)));
119 }
120 return true;
121 case CSSM_ALGID_ECDSA:
122 if(cspCtx == NULL) {
123 cspCtx = new SignatureContext(session,
124 *(new NullDigest()),
125 *(new FEEECDSASigner(feeRandCallback,
126 &session,
127 session,
128 *privAllocator)));
129 }
130 return true;
131 default:
132 break;
133 }
134 break;
135
136 case CSSM_ALGCLASS_KEYGEN:
137 switch(context.algorithm()) {
138 case CSSM_ALGID_FEE:
139 if(cspCtx == NULL) {
140 cspCtx = new CryptKit::FEEKeyPairGenContext(session, context);
141 }
142 return true;
143 default:
144 break;
145 }
146 break;
147
148 case CSSM_ALGCLASS_ASYMMETRIC:
149 switch(context.algorithm()) {
150 case CSSM_ALGID_FEEDEXP:
151 if(cspCtx == NULL) {
152 cspCtx = new CryptKit::FEEDExpContext(session);
153 }
154 return true;
155 case CSSM_ALGID_FEED:
156 if(cspCtx == NULL) {
157 cspCtx = new CryptKit::FEEDContext(session);
158 }
159 return true;
160 default:
161 break;
162 }
163 break;
164
165 /* more here - symmetric, etc. */
166 default:
167 break;
168 }
169 /* not implemented here */
170 return false;
171 }
172
173 #endif /* CRYPTKIT_CSP_ENABLE */
174
175