]> git.saurik.com Git - apple/security.git/blob - AppleCSPDL/SSContext.cpp
Security-28.tar.gz
[apple/security.git] / AppleCSPDL / SSContext.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 // SSContext - cryptographic contexts for the security server
21 //
22 #include "SSContext.h"
23
24 #include "SSCSPSession.h"
25 #include "SSKey.h"
26
27 using namespace SecurityServer;
28
29 //
30 // SSContext
31 //
32 SSContext::SSContext(SSCSPSession &session)
33 : mSession(session), mContext(NULL)
34 {
35 }
36
37 void
38 SSContext::init(const Context &context,
39 bool /* encoding */) // @@@ should be removed from API since it's already in mDirection
40 {
41 mContext = &context;
42 }
43
44 SecurityServer::ClientSession &
45 SSContext::clientSession()
46 {
47 return mSession.clientSession();
48 }
49
50
51 //
52 // SSRandomContext -- Context for GenerateRandom operations
53 //
54 SSRandomContext::SSRandomContext(SSCSPSession &session) : SSContext(session) {}
55
56 void
57 SSRandomContext::init(const Context &context, bool encoding)
58 {
59 SSContext::init(context, encoding);
60
61 // set/freeze output size
62 mOutSize = context.getInt(CSSM_ATTRIBUTE_OUTPUT_SIZE, CSSMERR_CSP_MISSING_ATTR_OUTPUT_SIZE);
63
64 #if 0
65 // seed the PRNG (if specified)
66 if (const CssmCryptoData *seed = context.get<CssmCryptoData>(CSSM_ATTRIBUTE_SEED)) {
67 const CssmData &seedValue = (*seed)();
68 clientSession().seedRandom(seedValue);
69 }
70 #endif
71 }
72
73 size_t
74 SSRandomContext::outputSize(bool final, size_t inSize)
75 {
76 return mOutSize;
77 }
78
79 void
80 SSRandomContext::final(CssmData &out)
81 {
82 clientSession().generateRandom(out);
83 }
84
85
86 //
87 // SSSignContext -- Context for signing and GenerateMac operations
88 //
89 SSSignContext::SSSignContext(SSCSPSession &session) : SSContext(session) {}
90
91 void
92 SSSignContext::update(const CssmData &data)
93 {
94 }
95
96 size_t
97 SSSignContext::outputSize(bool final, size_t inSize)
98 {
99 return 0;
100 }
101
102 void
103 SSSignContext::final(CssmData &out)
104 {
105 }
106
107
108 //
109 // SSVerifyContext -- Context for Verify and VerifyMac operations
110 //
111 SSVerifyContext::SSVerifyContext(SSCSPSession &session) : SSContext(session) {}
112
113 void
114 SSVerifyContext::update(const CssmData &data)
115 {
116 }
117
118 void
119 SSVerifyContext::final(const CssmData &in)
120 {
121 }
122
123
124 //
125 // SSCryptContext -- Context for Encrypt and Decrypt operations
126 //
127 SSCryptContext::SSCryptContext(SSCSPSession &session)
128 : SSContext(session), mKeyHandle(noKey), mCurrent(0), mCapacity(0),
129 mBuffer(NULL)
130 {
131 }
132
133
134 SSCryptContext::~SSCryptContext()
135 {
136 freeBuffer();
137 }
138
139 void
140 SSCryptContext::freeBuffer()
141 {
142 // @@@ We should probably use CssmAllocator::standard(sensitive) instead of malloc/realloc/free here
143 if (mBuffer)
144 {
145 // Zero out buffer (only on decrypt?)
146 if (mCapacity /* && !encoding() */)
147 {
148 memset(mBuffer, 0, mCapacity);
149 }
150
151 free(mBuffer);
152 mBuffer = NULL;
153 mCapacity = 0;
154 }
155 }
156
157 void
158 SSCryptContext::init(const Context &context, bool encoding)
159 {
160 SSContext::init(context, encoding);
161 freeBuffer();
162
163 mCurrent = 0;
164 mCapacity = 0;
165
166 const CssmKey &keyInContext =
167 context.get<const CssmKey>(CSSM_ATTRIBUTE_KEY,
168 CSSMERR_CSP_MISSING_ATTR_KEY);
169
170 // @@@ Should return SSKey.
171 mKeyHandle = mSession.lookupKey(keyInContext).keyHandle();
172 }
173
174 size_t
175 SSCryptContext::inputSize(size_t outSize)
176 {
177 return UINT_MAX;
178 }
179
180 size_t
181 SSCryptContext::outputSize(bool final, size_t inSize)
182 {
183 if (!final)
184 {
185 mCapacity = mCurrent + inSize;
186 mBuffer = realloc(mBuffer, mCapacity);
187 return 0;
188 }
189
190 // There should not be any remaining input data left when final is true;
191 assert(!inSize);
192
193 // Do the actual operation.
194 const CssmData in(mBuffer, mCurrent);
195 CssmData out;
196 if (encoding())
197 clientSession().encrypt(*mContext, mKeyHandle, in, out);
198 else
199 clientSession().decrypt(*mContext, mKeyHandle, in, out);
200
201 freeBuffer();
202 mBuffer = out.Data;
203 mCapacity = out.Length;
204 mCurrent = 0;
205 return mCapacity;
206 }
207
208 void
209 SSCryptContext::minimumProgress(size_t &in, size_t &out)
210 {
211 // This should never be called.
212 assert(false);
213 }
214
215 void
216 SSCryptContext::update(void *inp, size_t &inSize, void *outp, size_t &outSize)
217 {
218 outSize = 0;
219 assert(inSize);
220 assert(mCurrent + inSize <= mCapacity);
221 memcpy(&reinterpret_cast<uint8 *>(mBuffer)[mCurrent], inp, inSize);
222 mCurrent += inSize;
223 }
224
225 void
226 SSCryptContext::final(CssmData &out)
227 {
228 if(!out.Length) return;
229 assert(out.Data && out.Length);
230 uint32 todo = min(out.Length, mCapacity - mCurrent);
231 memcpy(out.Data, &reinterpret_cast<uint8 *>(mBuffer)[mCurrent], todo);
232 mCurrent += todo;
233 out.Length = todo;
234
235 freeBuffer();
236 }
237
238
239 #if 0
240 //
241 // SSKeyPairGenContext -- Context for key pair generation
242 //
243 SSKeyPairGenContext::SSKeyPairGenContext(SSCSPSession &session)
244 : SSContext(session) {}
245
246 void
247 SSKeyPairGenContext::generate(const Context &context,
248 CssmKey &pubKey,
249 SSKey *pubBinKey,
250 CssmKey &privKey,
251 SSKey *privBinKey)
252 {
253 }
254
255 void
256 SSKeyPairGenContext::generate(const Context &context,
257 SSKey &pubBinKey,
258 SSKey &privBinKey,
259 uint32 &keySize)
260 {
261 }
262
263
264 //
265 // SSSymmKeyGenContext -- Context for symmetric key generation
266 //
267 SSSymmKeyGenContext::SSSymmKeyGenContext(SSCSPSession &session,
268 uint32 minSize,
269 uint32 maxSize,
270 bool byteSized)
271 : SSContext(session),
272 minSizeInBits(minSize),
273 maxSizeInBits(maxSize),
274 mustBeByteSized(byteSized)
275 {
276 }
277
278 void
279 SSSymmKeyGenContext::generateSymKey(const Context &context, CssmKey &cssmKey)
280 {
281 }
282 #endif