-
Notifications
You must be signed in to change notification settings - Fork 51
/
session.cc
313 lines (269 loc) · 13.4 KB
/
session.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright 2013-2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// PKCS#11 s11.6: Session management functions
// C_OpenSession
// C_CloseSession
// C_CloseAllSessions
// C_GetSessionInfo
// C_GetOperationState
// C_SetOperationState
// PKCS#11 s11.16: Parallel function management functions
// C_GetFunctionStatus
// C_CancelFunction
#include <cstdlib>
#include "pkcs11test.h"
using namespace std; // So sue me
namespace pkcs11 {
namespace test {
TEST_F(PKCS11Test, OpenSessionUnsupportedNonSerial) {
// No CKF_SERIAL_SESSION => not supported
CK_SESSION_HANDLE session = 0;
EXPECT_CKR(CKR_SESSION_PARALLEL_NOT_SUPPORTED, g_fns->C_OpenSession(g_slot_id, 0, NULL_PTR, NULL_PTR, &session));
}
TEST_F(PKCS11Test, OpenSessionInvalidSlot) {
CK_FLAGS flags = CKF_SERIAL_SESSION;
CK_SESSION_HANDLE session;
EXPECT_CKR(CKR_SLOT_ID_INVALID, g_fns->C_OpenSession(INVALID_SLOT_ID, flags, NULL_PTR, NULL_PTR, &session));
}
TEST_F(PKCS11Test, OpenSessionInvalid) {
CK_FLAGS flags = CKF_SERIAL_SESSION;
CK_RV rv = g_fns->C_OpenSession(g_slot_id, flags, NULL_PTR, NULL_PTR, NULL_PTR);
EXPECT_TRUE(rv == CKR_ARGUMENTS_BAD || rv == CKR_FUNCTION_FAILED) << " rv=" << CK_RV_(rv);
}
TEST_F(PKCS11Test, ClosedSessionErrors) {
CK_FLAGS flags = CKF_SERIAL_SESSION;
CK_SESSION_HANDLE session;
EXPECT_CKR_OK(g_fns->C_OpenSession(g_slot_id, flags, NULL_PTR, NULL_PTR, &session));
EXPECT_CKR_OK(g_fns->C_CloseSession(session));
CK_SESSION_INFO session_info;
EXPECT_CKR(CKR_SESSION_HANDLE_INVALID, g_fns->C_GetSessionInfo(session, &session_info));
EXPECT_CKR(CKR_SESSION_HANDLE_INVALID, g_fns->C_CloseSession(session));
}
TEST_F(PKCS11Test, ParallelSessions) {
CK_FLAGS ro_flags = CKF_SERIAL_SESSION;
CK_FLAGS rw_flags = CKF_SERIAL_SESSION | CKF_RW_SESSION;
CK_SESSION_HANDLE session1;
CK_SESSION_HANDLE session2;
CK_SESSION_HANDLE session3;
EXPECT_CKR_OK(g_fns->C_OpenSession(g_slot_id, ro_flags, NULL_PTR, NULL_PTR, &session1));
EXPECT_CKR_OK(g_fns->C_OpenSession(g_slot_id, ro_flags, NULL_PTR, NULL_PTR, &session2));
EXPECT_CKR_OK(g_fns->C_OpenSession(g_slot_id, rw_flags, NULL_PTR, NULL_PTR, &session3));
CK_SESSION_INFO session1_info;
CK_SESSION_INFO session2_info;
CK_SESSION_INFO session3_info;
EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session1, &session1_info));
EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session2, &session2_info));
EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session3, &session3_info));
CK_STATE original_state1 = session1_info.state;
CK_STATE original_state2 = session2_info.state;
CK_STATE original_state3 = session3_info.state;
if (!(g_token_flags & CKF_PROTECTED_AUTHENTICATION_PATH)) {
// PKCS#11 s6.7.1: When the session is initially opened, it is in [..] the "R/O Public Session" if the application
// has no previously open sessions that are logged in
EXPECT_EQ(CKS_RO_PUBLIC_SESSION, session1_info.state);
EXPECT_EQ(CKS_RO_PUBLIC_SESSION, session2_info.state);
EXPECT_EQ(CKS_RW_PUBLIC_SESSION, session3_info.state);
} else {
// PKCS#11 s9.2: Token has a "protected authentication path", whereby a user can log into the token without passing
// a PIN through the Cryptoki library, so initial state might be logged in.
}
EXPECT_EQ(ro_flags, session1_info.flags);
EXPECT_EQ(ro_flags, session2_info.flags);
EXPECT_EQ(rw_flags, session3_info.flags);
EXPECT_EQ(g_slot_id, session1_info.slotID);
EXPECT_EQ(g_slot_id, session2_info.slotID);
EXPECT_EQ(g_slot_id, session3_info.slotID);
// Login relative to one session changes all session states.
EXPECT_CKR_OK(g_fns->C_Login(session1, CKU_USER, (CK_UTF8CHAR_PTR)g_user_pin, strlen(g_user_pin)));
EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session1, &session1_info));
EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session2, &session2_info));
EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session3, &session3_info));
EXPECT_EQ(CKS_RO_USER_FUNCTIONS, session1_info.state);
EXPECT_EQ(CKS_RO_USER_FUNCTIONS, session2_info.state);
EXPECT_EQ(CKS_RW_USER_FUNCTIONS, session3_info.state);
// Logout relative to one session changes all session states.
EXPECT_CKR_OK(g_fns->C_Logout(session3));
EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session1, &session1_info));
EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session2, &session2_info));
EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session3, &session3_info));
EXPECT_EQ(original_state1, session1_info.state);
EXPECT_EQ(original_state2, session2_info.state);
EXPECT_EQ(original_state3, session3_info.state);
// Close one session leaves the others intact.
EXPECT_CKR_OK(g_fns->C_CloseSession(session2));
EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session1, &session1_info));
EXPECT_CKR(CKR_SESSION_HANDLE_INVALID, g_fns->C_GetSessionInfo(session2, &session2_info));
EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session3, &session3_info));
EXPECT_CKR(CKR_SLOT_ID_INVALID, g_fns->C_CloseAllSessions(INVALID_SLOT_ID));
EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session1, &session1_info));
EXPECT_CKR(CKR_SESSION_HANDLE_INVALID, g_fns->C_GetSessionInfo(session2, &session2_info));
EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session3, &session3_info));
EXPECT_CKR_OK(g_fns->C_CloseAllSessions(g_slot_id));
EXPECT_CKR(CKR_SESSION_HANDLE_INVALID, g_fns->C_GetSessionInfo(session1, &session1_info));
EXPECT_CKR(CKR_SESSION_HANDLE_INVALID, g_fns->C_GetSessionInfo(session2, &session2_info));
EXPECT_CKR(CKR_SESSION_HANDLE_INVALID, g_fns->C_GetSessionInfo(session3, &session3_info));
EXPECT_CKR(CKR_SESSION_HANDLE_INVALID, g_fns->C_CloseSession(session1));
EXPECT_CKR(CKR_SESSION_HANDLE_INVALID, g_fns->C_CloseSession(session2));
EXPECT_CKR(CKR_SESSION_HANDLE_INVALID, g_fns->C_CloseSession(session3));
EXPECT_CKR_OK(g_fns->C_CloseAllSessions(g_slot_id));
}
TEST_F(ReadOnlySessionTest, InvalidSessionInfo) {
EXPECT_CKR(CKR_ARGUMENTS_BAD, g_fns->C_GetSessionInfo(session_, NULL_PTR));
CK_SESSION_INFO session_info;
EXPECT_CKR(CKR_SESSION_HANDLE_INVALID, g_fns->C_GetSessionInfo(INVALID_SESSION_HANDLE, &session_info));
}
TEST_F(ReadWriteSessionTest, InvalidSessionInfo) {
EXPECT_CKR(CKR_ARGUMENTS_BAD, g_fns->C_GetSessionInfo(session_, NULL_PTR));
CK_SESSION_INFO session_info;
EXPECT_CKR(CKR_SESSION_HANDLE_INVALID, g_fns->C_GetSessionInfo(INVALID_SESSION_HANDLE, &session_info));
}
TEST_F(ReadOnlySessionTest, CloseSessionInvalid) {
// PKCS#11 s11.16: Legacy function which should simply return the value CKR_FUNCTION_NOT_PARALLEL.
EXPECT_CKR(CKR_FUNCTION_NOT_PARALLEL, g_fns->C_GetFunctionStatus(session_));
EXPECT_CKR(CKR_FUNCTION_NOT_PARALLEL, g_fns->C_CancelFunction(session_));
EXPECT_EQ(CKR_SESSION_HANDLE_INVALID, g_fns->C_CloseSession(INVALID_SESSION_HANDLE));
}
TEST_F(ReadWriteSessionTest, CloseSessionInvalid) {
// PKCS#11 s11.16: Legacy function which should simply return the value CKR_FUNCTION_NOT_PARALLEL.
EXPECT_CKR(CKR_FUNCTION_NOT_PARALLEL, g_fns->C_GetFunctionStatus(session_));
EXPECT_CKR(CKR_FUNCTION_NOT_PARALLEL, g_fns->C_CancelFunction(session_));
EXPECT_EQ(CKR_SESSION_HANDLE_INVALID, g_fns->C_CloseSession(INVALID_SESSION_HANDLE));
}
TEST_F(ReadOnlySessionTest, SessionInfo) {
CK_SESSION_INFO session_info;
EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session_, &session_info));
if (g_verbose) cout << session_info_description(&session_info) << endl;
EXPECT_EQ(g_slot_id, session_info.slotID);
EXPECT_EQ(CKF_SERIAL_SESSION, session_info.flags);
CK_STATE original_state = session_info.state;
if (!(g_token_flags & CKF_PROTECTED_AUTHENTICATION_PATH)) {
// PKCS#11 s6.7.1: When the session is initially opened, it is in [..] the "R/O Public Session" if the application
// has no previously open sessions that are logged in
EXPECT_EQ(CKS_RO_PUBLIC_SESSION, session_info.state);
} else {
// PKCS#11 s9.2: Token has a "protected authentication path", whereby a user can log into the token without passing
// a PIN through the Cryptoki library, so initial state might be logged in.
}
// Logging in changes the state.
EXPECT_CKR_OK(g_fns->C_Login(session_, CKU_USER, (CK_UTF8CHAR_PTR)g_user_pin, strlen(g_user_pin)));
EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session_, &session_info));
EXPECT_EQ(CKS_RO_USER_FUNCTIONS, session_info.state);
// Log out again
EXPECT_CKR_OK(g_fns->C_Logout(session_));
EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session_, &session_info));
EXPECT_EQ(original_state, session_info.state);
}
TEST_F(ReadWriteSessionTest, SessionInfo) {
CK_SESSION_INFO session_info;
EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session_, &session_info));
if (g_verbose) cout << session_info_description(&session_info) << endl;
EXPECT_EQ(g_slot_id, session_info.slotID);
EXPECT_EQ(CKF_SERIAL_SESSION|CKF_RW_SESSION, session_info.flags);
CK_STATE original_state = session_info.state;
if (!(g_token_flags & CKF_PROTECTED_AUTHENTICATION_PATH)) {
// PKCS#11 s6.7.2: When the session is initially opened, it is in [..] the "R/W Public Session" if the application
// has no previously open sessions that are logged in
EXPECT_EQ(CKS_RW_PUBLIC_SESSION, session_info.state);
} else {
// PKCS#11 s9.2: Token has a "protected authentication path", whereby a user can log into the token without passing
// a PIN through the Cryptoki library, so initial state might be logged in.
}
// Logging in changes the state.
EXPECT_CKR_OK(g_fns->C_Login(session_, CKU_USER, (CK_UTF8CHAR_PTR)g_user_pin, strlen(g_user_pin)));
EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session_, &session_info));
EXPECT_EQ(CKS_RW_USER_FUNCTIONS, session_info.state);
// Log out again
EXPECT_CKR_OK(g_fns->C_Logout(session_));
EXPECT_CKR_OK(g_fns->C_GetSessionInfo(session_, &session_info));
EXPECT_EQ(original_state, session_info.state);
}
TEST_F(ReadWriteSessionTest, GetSetOperationStateInvalid) {
CK_ULONG len;
CK_RV rv = g_fns->C_GetOperationState(session_, NULL_PTR, &len);
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
TEST_SKIPPED("GetOperationState not supported");
return;
}
EXPECT_CKR(CKR_SESSION_HANDLE_INVALID,
g_fns->C_GetOperationState(INVALID_SESSION_HANDLE, NULL_PTR, &len));
EXPECT_CKR(CKR_ARGUMENTS_BAD,
g_fns->C_GetOperationState(session_, NULL_PTR, NULL_PTR));
CK_BYTE data[1024];
len = sizeof(data);
EXPECT_CKR(CKR_SESSION_HANDLE_INVALID,
g_fns->C_SetOperationState(INVALID_SESSION_HANDLE, data, len, 0, 0));
EXPECT_CKR(CKR_ARGUMENTS_BAD,
g_fns->C_SetOperationState(session_, NULL_PTR, NULL_PTR, 0, 0));
}
TEST_F(ReadWriteSessionTest, GetSetOperationState) {
CK_ULONG len;
CK_RV rv = g_fns->C_GetOperationState(session_, NULL_PTR, &len);
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
TEST_SKIPPED("GetOperationState not supported");
return;
}
// No state => OPERATION_NOT_INITIALIZED
EXPECT_CKR(CKR_OPERATION_NOT_INITIALIZED, rv) << " state len=" << len;
// Create some state
vector<CK_ATTRIBUTE_TYPE> attrs({CKA_ENCRYPT, CKA_DECRYPT});
SecretKey key(session_, attrs);
CK_MECHANISM mechanism = {CKM_DES_ECB, NULL_PTR, 0};
rv = g_fns->C_EncryptInit(session_, &mechanism, key.handle());
ASSERT_CKR_OK(rv);
// Encrypt one block.
unique_ptr<CK_BYTE, freer> plaintext(randmalloc(16));
CK_BYTE ciphertext[16];
CK_BYTE_PTR output = ciphertext;
CK_ULONG output_len = sizeof(ciphertext);
EXPECT_CKR_OK(g_fns->C_EncryptUpdate(session_, plaintext.get(), 8, output, &output_len));
EXPECT_EQ(8, output_len);
output += output_len;
output_len = sizeof(ciphertext) - (output - ciphertext);
rv = g_fns->C_GetOperationState(session_, NULL_PTR, &len);
if (rv == CKR_STATE_UNSAVEABLE) {
TEST_SKIPPED("GetOperationState reports state unsaveable");
return;
}
EXPECT_CKR(CKR_OK, rv);
unique_ptr<CK_BYTE, freer> state((CK_BYTE*)malloc(len));
rv = g_fns->C_GetOperationState(session_, state.get(), &len);
EXPECT_CKR_OK(rv);
if (rv == CKR_OK) {
// Set the state on a different session.
CK_FLAGS flags = CKF_SERIAL_SESSION | CKF_RW_SESSION;
CK_SESSION_HANDLE session2;
EXPECT_CKR_OK(g_fns->C_OpenSession(g_slot_id, flags, NULL_PTR, NULL_PTR, &session2));
rv = g_fns->C_SetOperationState(session2, state.get(), len, 0, 0);
if (rv == CKR_KEY_NEEDED) {
rv = g_fns->C_SetOperationState(session2, state.get(), len, key.handle(), 0);
}
EXPECT_CKR_OK(rv);
// Encrypt second block.
EXPECT_CKR_OK(g_fns->C_EncryptUpdate(session2, plaintext.get() + 8, 8, output, &output_len));
EXPECT_EQ(8, output_len);
output += output_len;
output_len = sizeof(ciphertext) - (output - ciphertext);
EXPECT_CKR_OK(g_fns->C_EncryptFinal(session2, output, &output_len));
// Check the result is the same as a one-shot encryption.
CK_BYTE ciphertext2[16];
output_len = sizeof(ciphertext2);
EXPECT_CKR_OK(g_fns->C_EncryptInit(session2, &mechanism, key.handle()));
EXPECT_CKR_OK(g_fns->C_Encrypt(session2, plaintext.get(), 16, ciphertext2, &output_len));
EXPECT_EQ(hex_data(ciphertext2, 16), hex_data(ciphertext, 16));
EXPECT_CKR_OK(g_fns->C_CloseSession(session2));
}
}
} // namespace test
} // namespace pkcs11