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;
72 hSession = CK_INVALID_HANDLE;
75 operation = SESSION_OP_NONE;
78 hashAlgo = HashAlgo::Unknown;
80 asymmetricCryptoOp = NULL;
81 symmetricCryptoOp = NULL;
82 mechanism = AsymMech::Unknown;
83 reAuthentication = false;
84 allowSinglePartOp = false;
85 allowMultiPartOp = false;
92 // Storing Key handle in session
93 hKey = CK_INVALID_HANDLE;
102 void Session::setKeyHandle(CK_OBJECT_HANDLE inHKey)
104 //store the key hanldle for subsequent use
109 CK_OBJECT_HANDLE Session::getKeyHandle()
111 //return the Key handle for subsequent use
116 CK_RV Session::getInfo(CK_SESSION_INFO_PTR pInfo)
118 if (pInfo == NULL_PTR) return CKR_ARGUMENTS_BAD;
120 pInfo->slotID = slot->getSlotID();
122 pInfo->state = getState();
123 pInfo->flags = CKF_SERIAL_SESSION;
126 pInfo->flags |= CKF_RW_SESSION;
128 pInfo->ulDeviceError = 0;
133 // Is a read and write session
140 CK_STATE Session::getState()
142 if (token->isSOLoggedIn())
144 return CKS_RW_SO_FUNCTIONS;
147 if (token->isUserLoggedIn())
151 return CKS_RW_USER_FUNCTIONS;
155 return CKS_RO_USER_FUNCTIONS;
161 return CKS_RW_PUBLIC_SESSION;
165 return CKS_RO_PUBLIC_SESSION;
169 void Session::setHandle(CK_SESSION_HANDLE inHSession)
171 hSession = inHSession;
174 CK_SESSION_HANDLE Session::getHandle()
179 // Return the slot that the session is connected to
180 Slot* Session::getSlot()
185 // Return the token that the session is connected to
186 Token* Session::getToken()
191 // Set the operation type
192 void Session::setOpType(int inOperation)
194 operation = inOperation;
197 // Get the operation type
198 int Session::getOpType()
203 // Reset the operations
204 void Session::resetOp()
213 if (digestOp != NULL)
215 CryptoFactory::i()->recycleHashAlgorithm(digestOp);
218 else if (findOp != NULL)
223 else if (asymmetricCryptoOp != NULL)
225 if (publicKey != NULL)
227 asymmetricCryptoOp->recyclePublicKey(publicKey);
230 if (privateKey != NULL)
232 asymmetricCryptoOp->recyclePrivateKey(privateKey);
235 CryptoFactory::i()->recycleAsymmetricAlgorithm(asymmetricCryptoOp);
236 asymmetricCryptoOp = NULL;
238 else if (symmetricCryptoOp != NULL)
240 if (symmetricKey != NULL)
242 symmetricCryptoOp->recycleKey(symmetricKey);
245 CryptoFactory::i()->recycleSymmetricAlgorithm(symmetricCryptoOp);
246 symmetricCryptoOp = NULL;
248 else if (macOp != NULL)
250 if (symmetricKey != NULL)
252 macOp->recycleKey(symmetricKey);
255 CryptoFactory::i()->recycleMacAlgorithm(macOp);
259 operation = SESSION_OP_NONE;
260 reAuthentication = false;
263 void Session::setFindOp(FindOperation *inFindOp)
265 if (findOp != NULL) {
271 FindOperation *Session::getFindOp()
276 // Set the digesting operator
277 void Session::setDigestOp(HashAlgorithm* inDigestOp)
279 if (digestOp != NULL)
281 CryptoFactory::i()->recycleHashAlgorithm(digestOp);
284 digestOp = inDigestOp;
287 // Get the digesting operator
288 HashAlgorithm* Session::getDigestOp()
293 void Session::setHashAlgo(HashAlgo::Type inHashAlgo)
295 hashAlgo = inHashAlgo;
298 HashAlgo::Type Session::getHashAlgo()
303 // Set the MACing operator
304 void Session::setMacOp(MacAlgorithm *inMacOp)
308 setSymmetricKey(NULL);
309 CryptoFactory::i()->recycleMacAlgorithm(macOp);
315 // Get the MACing operator
316 MacAlgorithm *Session::getMacOp()
321 void Session::setAsymmetricCryptoOp(AsymmetricAlgorithm *inAsymmetricCryptoOp)
323 if (asymmetricCryptoOp != NULL)
327 CryptoFactory::i()->recycleAsymmetricAlgorithm(asymmetricCryptoOp);
330 asymmetricCryptoOp = inAsymmetricCryptoOp;
333 AsymmetricAlgorithm *Session::getAsymmetricCryptoOp()
335 return asymmetricCryptoOp;
338 void Session::setSymmetricCryptoOp(SymmetricAlgorithm *inSymmetricCryptoOp)
340 if (symmetricCryptoOp != NULL)
342 setSymmetricKey(NULL);
343 CryptoFactory::i()->recycleSymmetricAlgorithm(symmetricCryptoOp);
346 symmetricCryptoOp = inSymmetricCryptoOp;
349 SymmetricAlgorithm *Session::getSymmetricCryptoOp()
351 return symmetricCryptoOp;
354 void Session::setMechanism(AsymMech::Type inMechanism)
356 mechanism = inMechanism;
359 AsymMech::Type Session::getMechanism()
364 void Session::setParameters(void* inParam, size_t inParamLen)
366 if (inParam == NULL || inParamLen == 0) return;
374 param = malloc(inParamLen);
377 memcpy(param, inParam, inParamLen);
378 paramLen = inParamLen;
382 void* Session::getParameters(size_t& inParamLen)
384 inParamLen = paramLen;
388 void Session::setReAuthentication(bool inReAuthentication)
390 reAuthentication = inReAuthentication;
393 bool Session::getReAuthentication()
395 return reAuthentication;
398 void Session::setAllowMultiPartOp(bool inAllowMultiPartOp)
400 allowMultiPartOp = inAllowMultiPartOp;
403 bool Session::getAllowMultiPartOp()
405 return allowMultiPartOp;
408 void Session::setAllowSinglePartOp(bool inAllowSinglePartOp)
410 allowSinglePartOp = inAllowSinglePartOp;
413 bool Session::getAllowSinglePartOp()
415 return allowSinglePartOp;
418 void Session::setPublicKey(PublicKey* inPublicKey)
420 if (asymmetricCryptoOp == NULL)
423 if (publicKey != NULL)
425 asymmetricCryptoOp->recyclePublicKey(publicKey);
428 publicKey = inPublicKey;
431 PublicKey* Session::getPublicKey()
436 void Session::setPrivateKey(PrivateKey* inPrivateKey)
438 if (asymmetricCryptoOp == NULL)
441 if (privateKey != NULL)
443 asymmetricCryptoOp->recyclePrivateKey(privateKey);
446 privateKey = inPrivateKey;
449 PrivateKey* Session::getPrivateKey()
454 void Session::setSymmetricKey(SymmetricKey* inSymmetricKey)
456 if (symmetricKey != NULL)
459 macOp->recycleKey(symmetricKey);
460 } else if (symmetricCryptoOp) {
461 symmetricCryptoOp->recycleKey(symmetricKey);
467 symmetricKey = inSymmetricKey;
470 SymmetricKey* Session::getSymmetricKey()