Added a new Attribute to store TPM key handle
[aaf/sshsm.git] / SoftHSMv2 / src / lib / session_mgr / Session.h
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         void setKeyHandle(CK_OBJECT_HANDLE inHKey);
128         CK_OBJECT_HANDLE getKeyHandle();
129
130         void setHwCryptoOpaque(void* inHwCryptoOpaque);
131         void *getHwCryptoOpaque();
132
133 private:
134         // Constructor
135         Session();
136
137         // Slot and token
138         Slot* slot;
139         Token* token;
140
141         // Application data (not in use)
142         CK_VOID_PTR pApplication;
143         CK_NOTIFY notify;
144
145         // Session properties
146         bool isReadWrite;
147         CK_SESSION_HANDLE hSession;
148
149         // Operations
150         int operation;
151
152         // Find
153         FindOperation *findOp;
154
155         // Digest
156         HashAlgorithm* digestOp;
157         HashAlgo::Type hashAlgo;
158
159         // Mac
160         MacAlgorithm* macOp;
161
162         // Asymmetric Crypto
163         AsymmetricAlgorithm* asymmetricCryptoOp;
164
165         // Symmetric Crypto
166         SymmetricAlgorithm* symmetricCryptoOp;
167
168         AsymMech::Type mechanism;
169         void* param;
170         size_t paramLen;
171         bool reAuthentication;
172         bool allowMultiPartOp;
173         bool allowSinglePartOp;
174         PublicKey* publicKey;
175         PrivateKey* privateKey;
176
177         // Symmetric Crypto
178         SymmetricKey* symmetricKey;
179
180         // hw plugin specific data
181         void *hwCryptoOpaque;
182
183     // Storing Key handle in session
184     CK_OBJECT_HANDLE hKey;
185 };
186
187 #endif // !_SOFTHSM_V2_SESSION_H