]> git.saurik.com Git - apple/security.git/blob - AppleCSP/CryptKitCSP/cryptkitcsp.cpp
Security-30.1.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
35 CssmAllocator *CryptKitFactory::normAllocator;
36 CssmAllocator *CryptKitFactory::privAllocator;
37
38 /*
39 * CryptKit-style memory allocator callbacks
40 */
41 static void *ckMalloc(unsigned size)
42 {
43 return CryptKitFactory::privAllocator->malloc(size);
44 }
45 static void ckFree(void *data)
46 {
47 CryptKitFactory::privAllocator->free(data);
48 }
49 static void *ckRealloc(void *oldPtr, unsigned newSize)
50 {
51 return CryptKitFactory::privAllocator->realloc(oldPtr, newSize);
52 }
53
54 //
55 // Manage the CryptKit algorithm factory
56 //
57
58 CryptKitFactory::CryptKitFactory(CssmAllocator *normAlloc, CssmAllocator *privAlloc)
59 {
60 setNormAllocator(normAlloc);
61 setPrivAllocator(privAlloc);
62 /* once-per-address space */
63 initCryptKit();
64 fallocRegister(ckMalloc, ckFree, ckRealloc);
65 }
66
67 CryptKitFactory::~CryptKitFactory()
68 {
69 terminateCryptKit();
70 }
71
72 bool CryptKitFactory::setup(
73 AppleCSPSession &session,
74 CSPFullPluginSession::CSPContext * &cspCtx,
75 const Context &context)
76 {
77 switch(context.type()) {
78 case CSSM_ALGCLASS_SIGNATURE:
79 switch(context.algorithm()) {
80 case CSSM_ALGID_FEE_MD5:
81 if(cspCtx == NULL) {
82 cspCtx = new SignatureContext(session,
83 *(new MD5Object()),
84 *(new FEERawSigner(feeRandCallback,
85 &session,
86 session,
87 *privAllocator)));
88 }
89 return true;
90 case CSSM_ALGID_FEE_SHA1:
91 if(cspCtx == NULL) {
92 cspCtx = new SignatureContext(session,
93 *(new SHA1Object()),
94 *(new FEERawSigner(feeRandCallback,
95 &session,
96 session,
97 *privAllocator)));
98 }
99 return true;
100 case CSSM_ALGID_SHA1WithECDSA:
101 if(cspCtx == NULL) {
102 cspCtx = new SignatureContext(session,
103 *(new SHA1Object()),
104 *(new FEEECDSASigner(feeRandCallback,
105 &session,
106 session,
107 *privAllocator)));
108 }
109 return true;
110 default:
111 break;
112 }
113 break;
114
115 case CSSM_ALGCLASS_KEYGEN:
116 switch(context.algorithm()) {
117 case CSSM_ALGID_FEE:
118 if(cspCtx == NULL) {
119 cspCtx = new CryptKit::FEEKeyPairGenContext(session, context);
120 }
121 return true;
122 default:
123 break;
124 }
125 break;
126
127 case CSSM_ALGCLASS_ASYMMETRIC:
128 switch(context.algorithm()) {
129 case CSSM_ALGID_FEEDEXP:
130 if(cspCtx == NULL) {
131 cspCtx = new CryptKit::FEEDExpContext(session);
132 }
133 return true;
134 case CSSM_ALGID_FEED:
135 if(cspCtx == NULL) {
136 cspCtx = new CryptKit::FEEDContext(session);
137 }
138 return true;
139 default:
140 break;
141 }
142 break;
143
144 /* more here - symmetric, etc. */
145 default:
146 break;
147 }
148 /* not implemented here */
149 return false;
150 }
151
152 #endif /* CRYPTKIT_CSP_ENABLE */
153
154