Added a new Attribute to store TPM key handle
[aaf/sshsm.git] / SoftHSMv2 / src / lib / session_mgr / Session.cpp
1 /*
2  * Copyright (c) 2010 .SE (The Internet Infrastructure Foundation)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 /*****************************************************************************
28  Session.h
29
30  This class represents a single session
31  *****************************************************************************/
32
33 #include "CryptoFactory.h"
34 #include "Session.h"
35
36 // Constructor
37 Session::Session(Slot* inSlot, bool inIsReadWrite, CK_VOID_PTR inPApplication, CK_NOTIFY inNotify)
38 {
39         slot = inSlot;
40         token = slot->getToken();
41         isReadWrite = inIsReadWrite;
42         hSession = CK_INVALID_HANDLE;
43         pApplication = inPApplication;
44         notify = inNotify;
45         operation = SESSION_OP_NONE;
46         findOp = NULL;
47         digestOp = NULL;
48         hashAlgo = HashAlgo::Unknown;
49         macOp = NULL;
50         asymmetricCryptoOp = NULL;
51         symmetricCryptoOp = NULL;
52         mechanism = AsymMech::Unknown;
53         reAuthentication = false;
54         allowSinglePartOp = false;
55         allowMultiPartOp = false;
56         publicKey = NULL;
57         privateKey = NULL;
58         symmetricKey = NULL;
59         param = NULL;
60         paramLen = 0;
61
62         // Storing Key handle in session
63         hKey = CK_INVALID_HANDLE;
64         hwCryptoOpaque = NULL;
65 }
66
67 // Constructor
68 Session::Session()
69 {
70         slot = NULL;
71         token = NULL;
72         isReadWrite = false;
73         hSession = CK_INVALID_HANDLE;
74         pApplication = NULL;
75         notify = NULL;
76         operation = SESSION_OP_NONE;
77         findOp = NULL;
78         digestOp = NULL;
79         hashAlgo = HashAlgo::Unknown;
80         macOp = NULL;
81         asymmetricCryptoOp = NULL;
82         symmetricCryptoOp = NULL;
83         mechanism = AsymMech::Unknown;
84         reAuthentication = false;
85         allowSinglePartOp = false;
86         allowMultiPartOp = false;
87         publicKey = NULL;
88         privateKey = NULL;
89         symmetricKey = NULL;
90         param = NULL;
91         paramLen = 0;
92
93     // Storing Key handle in session
94     hKey = CK_INVALID_HANDLE;
95        hwCryptoOpaque = NULL;
96 }
97
98 // Destructor
99 Session::~Session()
100 {
101         resetOp();
102 }
103
104 void Session::setKeyHandle(CK_OBJECT_HANDLE inHKey)
105 {
106     //store the key hanldle for subsequent use
107     hKey = inHKey;
108 }
109
110
111 CK_OBJECT_HANDLE Session::getKeyHandle()
112 {
113     //return the Key handle for subsequent use
114     return hKey;
115 }
116
117 // Get session info
118 CK_RV Session::getInfo(CK_SESSION_INFO_PTR pInfo)
119 {
120         if (pInfo == NULL_PTR) return CKR_ARGUMENTS_BAD;
121
122         pInfo->slotID = slot->getSlotID();
123
124         pInfo->state = getState();
125         pInfo->flags = CKF_SERIAL_SESSION;
126         if (isRW())
127         {
128                 pInfo->flags |= CKF_RW_SESSION;
129         }
130         pInfo->ulDeviceError = 0;
131
132         return CKR_OK;
133 }
134
135 // Is a read and write session
136 bool Session::isRW()
137 {
138         return isReadWrite;
139 }
140
141 // Get session state
142 CK_STATE Session::getState()
143 {
144         if (token->isSOLoggedIn())
145         {
146                 return CKS_RW_SO_FUNCTIONS;
147         }
148
149         if (token->isUserLoggedIn())
150         {
151                 if (isRW())
152                 {
153                         return CKS_RW_USER_FUNCTIONS;
154                 }
155                 else
156                 {
157                         return CKS_RO_USER_FUNCTIONS;
158                 }
159         }
160
161         if (isRW())
162         {
163                 return CKS_RW_PUBLIC_SESSION;
164         }
165         else
166         {
167                 return CKS_RO_PUBLIC_SESSION;
168         }
169 }
170
171 void Session::setHandle(CK_SESSION_HANDLE inHSession)
172 {
173         hSession = inHSession;
174 }
175
176 CK_SESSION_HANDLE Session::getHandle()
177 {
178         return hSession;
179 }
180
181 // Return the slot that the session is connected to
182 Slot* Session::getSlot()
183 {
184         return slot;
185 }
186
187 // Return the token that the session is connected to
188 Token* Session::getToken()
189 {
190         return token;
191 }
192
193 // Set the operation type
194 void Session::setOpType(int inOperation)
195 {
196         operation = inOperation;
197 }
198
199 // Get the operation type
200 int Session::getOpType()
201 {
202         return operation;
203 }
204
205 // Reset the operations
206 void Session::resetOp()
207 {
208         if (param != NULL)
209         {
210                 free(param);
211                 param = NULL;
212                 paramLen = 0;
213         }
214
215         if (digestOp != NULL)
216         {
217                 CryptoFactory::i()->recycleHashAlgorithm(digestOp);
218                 digestOp = NULL;
219         }
220         else if (findOp != NULL)
221         {
222                 findOp->recycle();
223                 findOp = NULL;
224         }
225         else if (asymmetricCryptoOp != NULL)
226         {
227                 if (publicKey != NULL)
228                 {
229                         asymmetricCryptoOp->recyclePublicKey(publicKey);
230                         publicKey = NULL;
231                 }
232                 if (privateKey != NULL)
233                 {
234                         asymmetricCryptoOp->recyclePrivateKey(privateKey);
235                         privateKey = NULL;
236                 }
237                 CryptoFactory::i()->recycleAsymmetricAlgorithm(asymmetricCryptoOp);
238                 asymmetricCryptoOp = NULL;
239         }
240         else if (symmetricCryptoOp != NULL)
241         {
242                 if (symmetricKey != NULL)
243                 {
244                         symmetricCryptoOp->recycleKey(symmetricKey);
245                         symmetricKey = NULL;
246                 }
247                 CryptoFactory::i()->recycleSymmetricAlgorithm(symmetricCryptoOp);
248                 symmetricCryptoOp = NULL;
249         }
250         else if (macOp != NULL)
251         {
252                 if (symmetricKey != NULL)
253                 {
254                         macOp->recycleKey(symmetricKey);
255                         symmetricKey = NULL;
256                 }
257                 CryptoFactory::i()->recycleMacAlgorithm(macOp);
258                 macOp = NULL;
259         }
260
261         operation = SESSION_OP_NONE;
262         reAuthentication = false;
263 }
264
265 void Session::setFindOp(FindOperation *inFindOp)
266 {
267         if (findOp != NULL) {
268                 delete findOp;
269         }
270         findOp = inFindOp;
271 }
272
273 FindOperation *Session::getFindOp()
274 {
275         return findOp;
276 }
277
278 // Set the digesting operator
279 void Session::setDigestOp(HashAlgorithm* inDigestOp)
280 {
281         if (digestOp != NULL)
282         {
283                 CryptoFactory::i()->recycleHashAlgorithm(digestOp);
284         }
285
286         digestOp = inDigestOp;
287 }
288
289 // Get the digesting operator
290 HashAlgorithm* Session::getDigestOp()
291 {
292         return digestOp;
293 }
294
295 void Session::setHashAlgo(HashAlgo::Type inHashAlgo)
296 {
297         hashAlgo = inHashAlgo;
298 }
299
300 HashAlgo::Type Session::getHashAlgo()
301 {
302         return hashAlgo;
303 }
304
305 // Set the MACing operator
306 void Session::setMacOp(MacAlgorithm *inMacOp)
307 {
308         if (macOp != NULL)
309         {
310                 setSymmetricKey(NULL);
311                 CryptoFactory::i()->recycleMacAlgorithm(macOp);
312         }
313
314         macOp = inMacOp;
315 }
316
317 // Get the MACing operator
318 MacAlgorithm *Session::getMacOp()
319 {
320         return macOp;
321 }
322
323 void Session::setAsymmetricCryptoOp(AsymmetricAlgorithm *inAsymmetricCryptoOp)
324 {
325         if (asymmetricCryptoOp != NULL)
326         {
327                 setPublicKey(NULL);
328                 setPrivateKey(NULL);
329                 CryptoFactory::i()->recycleAsymmetricAlgorithm(asymmetricCryptoOp);
330         }
331
332         asymmetricCryptoOp = inAsymmetricCryptoOp;
333 }
334
335 AsymmetricAlgorithm *Session::getAsymmetricCryptoOp()
336 {
337         return asymmetricCryptoOp;
338 }
339
340 void Session::setSymmetricCryptoOp(SymmetricAlgorithm *inSymmetricCryptoOp)
341 {
342         if (symmetricCryptoOp != NULL)
343         {
344                 setSymmetricKey(NULL);
345                 CryptoFactory::i()->recycleSymmetricAlgorithm(symmetricCryptoOp);
346         }
347
348         symmetricCryptoOp = inSymmetricCryptoOp;
349 }
350
351 SymmetricAlgorithm *Session::getSymmetricCryptoOp()
352 {
353         return symmetricCryptoOp;
354 }
355
356 void Session::setMechanism(AsymMech::Type inMechanism)
357 {
358         mechanism = inMechanism;
359 }
360
361 AsymMech::Type Session::getMechanism()
362 {
363         return mechanism;
364 }
365
366 void Session::setParameters(void* inParam, size_t inParamLen)
367 {
368         if (inParam == NULL || inParamLen == 0) return;
369
370         if (param != NULL)
371         {
372                 free(param);
373                 paramLen = 0;
374         }
375
376         param = malloc(inParamLen);
377         if (param != NULL)
378         {
379                 memcpy(param, inParam, inParamLen);
380                 paramLen = inParamLen;
381         }
382 }
383
384 void* Session::getParameters(size_t& inParamLen)
385 {
386         inParamLen = paramLen;
387         return param;
388 }
389
390 void Session::setReAuthentication(bool inReAuthentication)
391 {
392         reAuthentication = inReAuthentication;
393 }
394
395 bool Session::getReAuthentication()
396 {
397         return reAuthentication;
398 }
399
400 void Session::setAllowMultiPartOp(bool inAllowMultiPartOp)
401 {
402         allowMultiPartOp = inAllowMultiPartOp;
403 }
404
405 bool Session::getAllowMultiPartOp()
406 {
407         return allowMultiPartOp;
408 }
409
410 void Session::setAllowSinglePartOp(bool inAllowSinglePartOp)
411 {
412         allowSinglePartOp = inAllowSinglePartOp;
413 }
414
415 bool Session::getAllowSinglePartOp()
416 {
417         return allowSinglePartOp;
418 }
419
420 void Session::setPublicKey(PublicKey* inPublicKey)
421 {
422         if (asymmetricCryptoOp == NULL)
423                 return;
424
425         if (publicKey != NULL)
426         {
427                 asymmetricCryptoOp->recyclePublicKey(publicKey);
428         }
429
430         publicKey = inPublicKey;
431 }
432
433 PublicKey* Session::getPublicKey()
434 {
435         return publicKey;
436 }
437
438 void Session::setPrivateKey(PrivateKey* inPrivateKey)
439 {
440         if (asymmetricCryptoOp == NULL)
441                 return;
442
443         if (privateKey != NULL)
444         {
445                 asymmetricCryptoOp->recyclePrivateKey(privateKey);
446         }
447
448         privateKey = inPrivateKey;
449 }
450
451 PrivateKey* Session::getPrivateKey()
452 {
453         return privateKey;
454 }
455
456 void Session::setSymmetricKey(SymmetricKey* inSymmetricKey)
457 {
458         if (symmetricKey != NULL)
459         {
460                 if (macOp) {
461                         macOp->recycleKey(symmetricKey);
462                 } else if (symmetricCryptoOp) {
463                         symmetricCryptoOp->recycleKey(symmetricKey);
464                 } else {
465                         return;
466                 }
467         }
468
469         symmetricKey = inSymmetricKey;
470 }
471
472 SymmetricKey* Session::getSymmetricKey()
473 {
474         return symmetricKey;
475 }
476
477 void Session::setHwCryptoOpaque(void *inHwCryptoOpaque )
478 {
479         hwCryptoOpaque = inHwCryptoOpaque;
480 }
481
482 void*  Session::getHwCryptoOpaque()
483 {
484         return hwCryptoOpaque;
485 }
486