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 #ifndef _SOFTHSM_V2_SESSION_H
34 #define _SOFTHSM_V2_SESSION_H
35 
36 #include "Slot.h"
37 #include "FindOperation.h"
38 #include "HashAlgorithm.h"
39 #include "MacAlgorithm.h"
40 #include "AsymmetricAlgorithm.h"
41 #include "SymmetricAlgorithm.h"
42 #include "Token.h"
43 #include "cryptoki.h"
44 
45 #define SESSION_OP_NONE			0x0
46 #define SESSION_OP_FIND			0x1
47 #define SESSION_OP_ENCRYPT		0x2
48 #define SESSION_OP_DECRYPT		0x3
49 #define SESSION_OP_DIGEST		0x4
50 #define SESSION_OP_SIGN			0x5
51 #define SESSION_OP_VERIFY		0x6
52 #define SESSION_OP_DIGEST_ENCRYPT	0x7
53 #define SESSION_OP_DECRYPT_DIGEST	0x8
54 #define SESSION_OP_SIGN_ENCRYPT		0x9
55 #define SESSION_OP_DECRYPT_VERIFY	0x10
56 
57 class Session
58 {
59 public:
60 	Session(Slot* inSlot, bool inIsReadWrite, CK_VOID_PTR inPApplication, CK_NOTIFY inNotify);
61 
62 	// Destructor
63 	virtual ~Session();
64 
65 	// Slot and token
66 	Slot* getSlot();
67 	Token* getToken();
68 
69 	// Session properties
70 	CK_RV getInfo(CK_SESSION_INFO_PTR pInfo);
71 	bool isRW();
72 	CK_STATE getState();
73 	void setHandle(CK_SESSION_HANDLE inHSession);
74 	CK_SESSION_HANDLE getHandle();
75 
76 	// Operations
77 	int getOpType();
78 	void setOpType(int inOperation);
79 	void resetOp();
80 
81 	// Find
82 	void setFindOp(FindOperation *inFindOp);
83 	FindOperation *getFindOp();
84 
85 	// Digest
86 	void setDigestOp(HashAlgorithm* inDigestOp);
87 	HashAlgorithm* getDigestOp();
88 	void setHashAlgo(HashAlgo::Type inHashAlgo);
89 	HashAlgo::Type getHashAlgo();
90 
91 	// Mac
92 	void setMacOp(MacAlgorithm* inMacOp);
93 	MacAlgorithm* getMacOp();
94 
95 	// Asymmetric Crypto
96 	void setAsymmetricCryptoOp(AsymmetricAlgorithm* inAsymmetricCryptoOp);
97 	AsymmetricAlgorithm* getAsymmetricCryptoOp();
98 
99 	// Symmetric Crypto
100 	void setSymmetricCryptoOp(SymmetricAlgorithm* inSymmetricCryptoOp);
101 	SymmetricAlgorithm* getSymmetricCryptoOp();
102 
103 	void setMechanism(AsymMech::Type inMechanism);
104 	AsymMech::Type getMechanism();
105 
106 	void setParameters(void* inParam, size_t inParamLen);
107 	void* getParameters(size_t& inParamLen);
108 
109 	void setReAuthentication(bool inReAuthentication);
110 	bool getReAuthentication();
111 
112 	void setAllowMultiPartOp(bool inAllowMultiPartOp);
113 	bool getAllowMultiPartOp();
114 
115 	void setAllowSinglePartOp(bool inAllowSinglePartOp);
116 	bool getAllowSinglePartOp();
117 
118 	void setPublicKey(PublicKey* inPublicKey);
119 	PublicKey* getPublicKey();
120 
121 	void setPrivateKey(PrivateKey* inPrivateKey);
122 	PrivateKey* getPrivateKey();
123 
124 	void setSymmetricKey(SymmetricKey* inSymmetricKey);
125 	SymmetricKey* getSymmetricKey();
126 
127 private:
128 	// Constructor
129 	Session();
130 
131 	// Slot and token
132 	Slot* slot;
133 	Token* token;
134 
135 	// Application data (not in use)
136 	CK_VOID_PTR pApplication;
137 	CK_NOTIFY notify;
138 
139 	// Session properties
140 	bool isReadWrite;
141 	CK_SESSION_HANDLE hSession;
142 
143 	// Operations
144 	int operation;
145 
146 	// Find
147 	FindOperation *findOp;
148 
149 	// Digest
150 	HashAlgorithm* digestOp;
151 	HashAlgo::Type hashAlgo;
152 
153 	// Mac
154 	MacAlgorithm* macOp;
155 
156 	// Asymmetric Crypto
157 	AsymmetricAlgorithm* asymmetricCryptoOp;
158 
159 	// Symmetric Crypto
160 	SymmetricAlgorithm* symmetricCryptoOp;
161 
162 	AsymMech::Type mechanism;
163 	void* param;
164 	size_t paramLen;
165 	bool reAuthentication;
166 	bool allowMultiPartOp;
167 	bool allowSinglePartOp;
168 	PublicKey* publicKey;
169 	PrivateKey* privateKey;
170 
171 	// Symmetric Crypto
172 	SymmetricKey* symmetricKey;
173 };
174 
175 #endif // !_SOFTHSM_V2_SESSION_H
176