54c0ff782f110f70d5fb079e46b812bd1ec55386
[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 }
65
66 // Constructor
67 Session::Session()
68 {
69         slot = NULL;
70         token = NULL;
71         isReadWrite = false;
72         hSession = CK_INVALID_HANDLE;
73         pApplication = NULL;
74         notify = NULL;
75         operation = SESSION_OP_NONE;
76         findOp = NULL;
77         digestOp = NULL;
78         hashAlgo = HashAlgo::Unknown;
79         macOp = NULL;
80         asymmetricCryptoOp = NULL;
81         symmetricCryptoOp = NULL;
82         mechanism = AsymMech::Unknown;
83         reAuthentication = false;
84         allowSinglePartOp = false;
85         allowMultiPartOp = false;
86         publicKey = NULL;
87         privateKey = NULL;
88         symmetricKey = NULL;
89         param = NULL;
90         paramLen = 0;
91
92     // Storing Key handle in session
93     hKey = CK_INVALID_HANDLE;
94 }
95
96 // Destructor
97 Session::~Session()
98 {
99         resetOp();
100 }
101
102 void Session::setKeyHandle(CK_OBJECT_HANDLE inHKey)
103 {
104     //store the key hanldle for subsequent use
105     hKey = inHKey;
106 }
107
108
109 CK_OBJECT_HANDLE Session::getKeyHandle()
110 {
111     //return the Key handle for subsequent use
112     return hKey;
113 }
114
115 // Get session info
116 CK_RV Session::getInfo(CK_SESSION_INFO_PTR pInfo)
117 {
118         if (pInfo == NULL_PTR) return CKR_ARGUMENTS_BAD;
119
120         pInfo->slotID = slot->getSlotID();
121
122         pInfo->state = getState();
123         pInfo->flags = CKF_SERIAL_SESSION;
124         if (isRW())
125         {
126                 pInfo->flags |= CKF_RW_SESSION;
127         }
128         pInfo->ulDeviceError = 0;
129
130         return CKR_OK;
131 }
132
133 // Is a read and write session
134 bool Session::isRW()
135 {
136         return isReadWrite;
137 }
138
139 // Get session state
140 CK_STATE Session::getState()
141 {
142         if (token->isSOLoggedIn())
143         {
144                 return CKS_RW_SO_FUNCTIONS;
145         }
146
147         if (token->isUserLoggedIn())
148         {
149                 if (isRW())
150                 {
151                         return CKS_RW_USER_FUNCTIONS;
152                 }
153                 else
154                 {
155                         return CKS_RO_USER_FUNCTIONS;
156                 }
157         }
158
159         if (isRW())
160         {
161                 return CKS_RW_PUBLIC_SESSION;
162         }
163         else
164         {
165                 return CKS_RO_PUBLIC_SESSION;
166         }
167 }
168
169 void Session::setHandle(CK_SESSION_HANDLE inHSession)
170 {
171         hSession = inHSession;
172 }
173
174 CK_SESSION_HANDLE Session::getHandle()
175 {
176         return hSession;
177 }
178
179 // Return the slot that the session is connected to
180 Slot* Session::getSlot()
181 {
182         return slot;
183 }
184
185 // Return the token that the session is connected to
186 Token* Session::getToken()
187 {
188         return token;
189 }
190
191 // Set the operation type
192 void Session::setOpType(int inOperation)
193 {
194         operation = inOperation;
195 }
196
197 // Get the operation type
198 int Session::getOpType()
199 {
200         return operation;
201 }
202
203 // Reset the operations
204 void Session::resetOp()
205 {
206         if (param != NULL)
207         {
208                 free(param);
209                 param = NULL;
210                 paramLen = 0;
211         }
212
213         if (digestOp != NULL)
214         {
215                 CryptoFactory::i()->recycleHashAlgorithm(digestOp);
216                 digestOp = NULL;
217         }
218         else if (findOp != NULL)
219         {
220                 findOp->recycle();
221                 findOp = NULL;
222         }
223         else if (asymmetricCryptoOp != NULL)
224         {
225                 if (publicKey != NULL)
226                 {
227                         asymmetricCryptoOp->recyclePublicKey(publicKey);
228                         publicKey = NULL;
229                 }
230                 if (privateKey != NULL)
231                 {
232                         asymmetricCryptoOp->recyclePrivateKey(privateKey);
233                         privateKey = NULL;
234                 }
235                 CryptoFactory::i()->recycleAsymmetricAlgorithm(asymmetricCryptoOp);
236                 asymmetricCryptoOp = NULL;
237         }
238         else if (symmetricCryptoOp != NULL)
239         {
240                 if (symmetricKey != NULL)
241                 {
242                         symmetricCryptoOp->recycleKey(symmetricKey);
243                         symmetricKey = NULL;
244                 }
245                 CryptoFactory::i()->recycleSymmetricAlgorithm(symmetricCryptoOp);
246                 symmetricCryptoOp = NULL;
247         }
248         else if (macOp != NULL)
249         {
250                 if (symmetricKey != NULL)
251                 {
252                         macOp->recycleKey(symmetricKey);
253                         symmetricKey = NULL;
254                 }
255                 CryptoFactory::i()->recycleMacAlgorithm(macOp);
256                 macOp = NULL;
257         }
258
259         operation = SESSION_OP_NONE;
260         reAuthentication = false;
261 }
262
263 void Session::setFindOp(FindOperation *inFindOp)
264 {
265         if (findOp != NULL) {
266                 delete findOp;
267         }
268         findOp = inFindOp;
269 }
270
271 FindOperation *Session::getFindOp()
272 {
273         return findOp;
274 }
275
276 // Set the digesting operator
277 void Session::setDigestOp(HashAlgorithm* inDigestOp)
278 {
279         if (digestOp != NULL)
280         {
281                 CryptoFactory::i()->recycleHashAlgorithm(digestOp);
282         }
283
284         digestOp = inDigestOp;
285 }
286
287 // Get the digesting operator
288 HashAlgorithm* Session::getDigestOp()
289 {
290         return digestOp;
291 }
292
293 void Session::setHashAlgo(HashAlgo::Type inHashAlgo)
294 {
295         hashAlgo = inHashAlgo;
296 }
297
298 HashAlgo::Type Session::getHashAlgo()
299 {
300         return hashAlgo;
301 }
302
303 // Set the MACing operator
304 void Session::setMacOp(MacAlgorithm *inMacOp)
305 {
306         if (macOp != NULL)
307         {
308                 setSymmetricKey(NULL);
309                 CryptoFactory::i()->recycleMacAlgorithm(macOp);
310         }
311
312         macOp = inMacOp;
313 }
314
315 // Get the MACing operator
316 MacAlgorithm *Session::getMacOp()
317 {
318         return macOp;
319 }
320
321 void Session::setAsymmetricCryptoOp(AsymmetricAlgorithm *inAsymmetricCryptoOp)
322 {
323         if (asymmetricCryptoOp != NULL)
324         {
325                 setPublicKey(NULL);
326                 setPrivateKey(NULL);
327                 CryptoFactory::i()->recycleAsymmetricAlgorithm(asymmetricCryptoOp);
328         }
329
330         asymmetricCryptoOp = inAsymmetricCryptoOp;
331 }
332
333 AsymmetricAlgorithm *Session::getAsymmetricCryptoOp()
334 {
335         return asymmetricCryptoOp;
336 }
337
338 void Session::setSymmetricCryptoOp(SymmetricAlgorithm *inSymmetricCryptoOp)
339 {
340         if (symmetricCryptoOp != NULL)
341         {
342                 setSymmetricKey(NULL);
343                 CryptoFactory::i()->recycleSymmetricAlgorithm(symmetricCryptoOp);
344         }
345
346         symmetricCryptoOp = inSymmetricCryptoOp;
347 }
348
349 SymmetricAlgorithm *Session::getSymmetricCryptoOp()
350 {
351         return symmetricCryptoOp;
352 }
353
354 void Session::setMechanism(AsymMech::Type inMechanism)
355 {
356         mechanism = inMechanism;
357 }
358
359 AsymMech::Type Session::getMechanism()
360 {
361         return mechanism;
362 }
363
364 void Session::setParameters(void* inParam, size_t inParamLen)
365 {
366         if (inParam == NULL || inParamLen == 0) return;
367
368         if (param != NULL)
369         {
370                 free(param);
371                 paramLen = 0;
372         }
373
374         param = malloc(inParamLen);
375         if (param != NULL)
376         {
377                 memcpy(param, inParam, inParamLen);
378                 paramLen = inParamLen;
379         }
380 }
381
382 void* Session::getParameters(size_t& inParamLen)
383 {
384         inParamLen = paramLen;
385         return param;
386 }
387
388 void Session::setReAuthentication(bool inReAuthentication)
389 {
390         reAuthentication = inReAuthentication;
391 }
392
393 bool Session::getReAuthentication()
394 {
395         return reAuthentication;
396 }
397
398 void Session::setAllowMultiPartOp(bool inAllowMultiPartOp)
399 {
400         allowMultiPartOp = inAllowMultiPartOp;
401 }
402
403 bool Session::getAllowMultiPartOp()
404 {
405         return allowMultiPartOp;
406 }
407
408 void Session::setAllowSinglePartOp(bool inAllowSinglePartOp)
409 {
410         allowSinglePartOp = inAllowSinglePartOp;
411 }
412
413 bool Session::getAllowSinglePartOp()
414 {
415         return allowSinglePartOp;
416 }
417
418 void Session::setPublicKey(PublicKey* inPublicKey)
419 {
420         if (asymmetricCryptoOp == NULL)
421                 return;
422
423         if (publicKey != NULL)
424         {
425                 asymmetricCryptoOp->recyclePublicKey(publicKey);
426         }
427
428         publicKey = inPublicKey;
429 }
430
431 PublicKey* Session::getPublicKey()
432 {
433         return publicKey;
434 }
435
436 void Session::setPrivateKey(PrivateKey* inPrivateKey)
437 {
438         if (asymmetricCryptoOp == NULL)
439                 return;
440
441         if (privateKey != NULL)
442         {
443                 asymmetricCryptoOp->recyclePrivateKey(privateKey);
444         }
445
446         privateKey = inPrivateKey;
447 }
448
449 PrivateKey* Session::getPrivateKey()
450 {
451         return privateKey;
452 }
453
454 void Session::setSymmetricKey(SymmetricKey* inSymmetricKey)
455 {
456         if (symmetricKey != NULL)
457         {
458                 if (macOp) {
459                         macOp->recycleKey(symmetricKey);
460                 } else if (symmetricCryptoOp) {
461                         symmetricCryptoOp->recycleKey(symmetricKey);
462                 } else {
463                         return;
464                 }
465         }
466
467         symmetricKey = inSymmetricKey;
468 }
469
470 SymmetricKey* Session::getSymmetricKey()
471 {
472         return symmetricKey;
473 }