2 * Copyright (c) 2010 .SE (The Internet Infrastructure Foundation)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 /*****************************************************************************
30 This class represents a single session
31 *****************************************************************************/
33 #include "CryptoFactory.h"
37 Session::Session(Slot* inSlot, bool inIsReadWrite, CK_VOID_PTR inPApplication, CK_NOTIFY inNotify)
40 token = slot->getToken();
41 isReadWrite = inIsReadWrite;
42 hSession = CK_INVALID_HANDLE;
43 pApplication = inPApplication;
45 operation = SESSION_OP_NONE;
48 hashAlgo = HashAlgo::Unknown;
50 asymmetricCryptoOp = NULL;
51 symmetricCryptoOp = NULL;
52 mechanism = AsymMech::Unknown;
53 reAuthentication = false;
54 allowSinglePartOp = false;
55 allowMultiPartOp = false;
62 // Storing Key handle in session
63 hKey = CK_INVALID_HANDLE;
64 hwCryptoOpaque = NULL;
73 hSession = CK_INVALID_HANDLE;
76 operation = SESSION_OP_NONE;
79 hashAlgo = HashAlgo::Unknown;
81 asymmetricCryptoOp = NULL;
82 symmetricCryptoOp = NULL;
83 mechanism = AsymMech::Unknown;
84 reAuthentication = false;
85 allowSinglePartOp = false;
86 allowMultiPartOp = false;
93 // Storing Key handle in session
94 hKey = CK_INVALID_HANDLE;
95 hwCryptoOpaque = NULL;
104 void Session::setKeyHandle(CK_OBJECT_HANDLE inHKey)
106 //store the key hanldle for subsequent use
111 CK_OBJECT_HANDLE Session::getKeyHandle()
113 //return the Key handle for subsequent use
118 CK_RV Session::getInfo(CK_SESSION_INFO_PTR pInfo)
120 if (pInfo == NULL_PTR) return CKR_ARGUMENTS_BAD;
122 pInfo->slotID = slot->getSlotID();
124 pInfo->state = getState();
125 pInfo->flags = CKF_SERIAL_SESSION;
128 pInfo->flags |= CKF_RW_SESSION;
130 pInfo->ulDeviceError = 0;
135 // Is a read and write session
142 CK_STATE Session::getState()
144 if (token->isSOLoggedIn())
146 return CKS_RW_SO_FUNCTIONS;
149 if (token->isUserLoggedIn())
153 return CKS_RW_USER_FUNCTIONS;
157 return CKS_RO_USER_FUNCTIONS;
163 return CKS_RW_PUBLIC_SESSION;
167 return CKS_RO_PUBLIC_SESSION;
171 void Session::setHandle(CK_SESSION_HANDLE inHSession)
173 hSession = inHSession;
176 CK_SESSION_HANDLE Session::getHandle()
181 // Return the slot that the session is connected to
182 Slot* Session::getSlot()
187 // Return the token that the session is connected to
188 Token* Session::getToken()
193 // Set the operation type
194 void Session::setOpType(int inOperation)
196 operation = inOperation;
199 // Get the operation type
200 int Session::getOpType()
205 // Reset the operations
206 void Session::resetOp()
215 if (digestOp != NULL)
217 CryptoFactory::i()->recycleHashAlgorithm(digestOp);
220 else if (findOp != NULL)
225 else if (asymmetricCryptoOp != NULL)
227 if (publicKey != NULL)
229 asymmetricCryptoOp->recyclePublicKey(publicKey);
232 if (privateKey != NULL)
234 asymmetricCryptoOp->recyclePrivateKey(privateKey);
237 CryptoFactory::i()->recycleAsymmetricAlgorithm(asymmetricCryptoOp);
238 asymmetricCryptoOp = NULL;
240 else if (symmetricCryptoOp != NULL)
242 if (symmetricKey != NULL)
244 symmetricCryptoOp->recycleKey(symmetricKey);
247 CryptoFactory::i()->recycleSymmetricAlgorithm(symmetricCryptoOp);
248 symmetricCryptoOp = NULL;
250 else if (macOp != NULL)
252 if (symmetricKey != NULL)
254 macOp->recycleKey(symmetricKey);
257 CryptoFactory::i()->recycleMacAlgorithm(macOp);
261 operation = SESSION_OP_NONE;
262 reAuthentication = false;
265 void Session::setFindOp(FindOperation *inFindOp)
267 if (findOp != NULL) {
273 FindOperation *Session::getFindOp()
278 // Set the digesting operator
279 void Session::setDigestOp(HashAlgorithm* inDigestOp)
281 if (digestOp != NULL)
283 CryptoFactory::i()->recycleHashAlgorithm(digestOp);
286 digestOp = inDigestOp;
289 // Get the digesting operator
290 HashAlgorithm* Session::getDigestOp()
295 void Session::setHashAlgo(HashAlgo::Type inHashAlgo)
297 hashAlgo = inHashAlgo;
300 HashAlgo::Type Session::getHashAlgo()
305 // Set the MACing operator
306 void Session::setMacOp(MacAlgorithm *inMacOp)
310 setSymmetricKey(NULL);
311 CryptoFactory::i()->recycleMacAlgorithm(macOp);
317 // Get the MACing operator
318 MacAlgorithm *Session::getMacOp()
323 void Session::setAsymmetricCryptoOp(AsymmetricAlgorithm *inAsymmetricCryptoOp)
325 if (asymmetricCryptoOp != NULL)
329 CryptoFactory::i()->recycleAsymmetricAlgorithm(asymmetricCryptoOp);
332 asymmetricCryptoOp = inAsymmetricCryptoOp;
335 AsymmetricAlgorithm *Session::getAsymmetricCryptoOp()
337 return asymmetricCryptoOp;
340 void Session::setSymmetricCryptoOp(SymmetricAlgorithm *inSymmetricCryptoOp)
342 if (symmetricCryptoOp != NULL)
344 setSymmetricKey(NULL);
345 CryptoFactory::i()->recycleSymmetricAlgorithm(symmetricCryptoOp);
348 symmetricCryptoOp = inSymmetricCryptoOp;
351 SymmetricAlgorithm *Session::getSymmetricCryptoOp()
353 return symmetricCryptoOp;
356 void Session::setMechanism(AsymMech::Type inMechanism)
358 mechanism = inMechanism;
361 AsymMech::Type Session::getMechanism()
366 void Session::setParameters(void* inParam, size_t inParamLen)
368 if (inParam == NULL || inParamLen == 0) return;
376 param = malloc(inParamLen);
379 memcpy(param, inParam, inParamLen);
380 paramLen = inParamLen;
384 void* Session::getParameters(size_t& inParamLen)
386 inParamLen = paramLen;
390 void Session::setReAuthentication(bool inReAuthentication)
392 reAuthentication = inReAuthentication;
395 bool Session::getReAuthentication()
397 return reAuthentication;
400 void Session::setAllowMultiPartOp(bool inAllowMultiPartOp)
402 allowMultiPartOp = inAllowMultiPartOp;
405 bool Session::getAllowMultiPartOp()
407 return allowMultiPartOp;
410 void Session::setAllowSinglePartOp(bool inAllowSinglePartOp)
412 allowSinglePartOp = inAllowSinglePartOp;
415 bool Session::getAllowSinglePartOp()
417 return allowSinglePartOp;
420 void Session::setPublicKey(PublicKey* inPublicKey)
422 if (asymmetricCryptoOp == NULL)
425 if (publicKey != NULL)
427 asymmetricCryptoOp->recyclePublicKey(publicKey);
430 publicKey = inPublicKey;
433 PublicKey* Session::getPublicKey()
438 void Session::setPrivateKey(PrivateKey* inPrivateKey)
440 if (asymmetricCryptoOp == NULL)
443 if (privateKey != NULL)
445 asymmetricCryptoOp->recyclePrivateKey(privateKey);
448 privateKey = inPrivateKey;
451 PrivateKey* Session::getPrivateKey()
456 void Session::setSymmetricKey(SymmetricKey* inSymmetricKey)
458 if (symmetricKey != NULL)
461 macOp->recycleKey(symmetricKey);
462 } else if (symmetricCryptoOp) {
463 symmetricCryptoOp->recycleKey(symmetricKey);
469 symmetricKey = inSymmetricKey;
472 SymmetricKey* Session::getSymmetricKey()
477 void Session::setHwCryptoOpaque(void *inHwCryptoOpaque )
479 hwCryptoOpaque = inHwCryptoOpaque;
482 void* Session::getHwCryptoOpaque()
484 return hwCryptoOpaque;