-
Notifications
You must be signed in to change notification settings - Fork 51
/
init.cc
192 lines (166 loc) · 5.89 KB
/
init.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
// 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.4: General-purpose functions
// C_Initialize
// C_Finalize
// C_GetFunctionList
// C_GetInfo
#include "pkcs11test.h"
using namespace std; // So sue me
namespace pkcs11 {
namespace test {
// Explicitly test Initialize/Finalize.
TEST(Init, Simple) {
EXPECT_CKR_OK(g_fns->C_Initialize(NULL_PTR));
EXPECT_CKR_OK(g_fns->C_Finalize(NULL_PTR));
}
TEST(Init, Uninitialized) {
// Nothing should work if the library hasn't been initialized.
CK_FLAGS flags = CKF_SERIAL_SESSION;
CK_SESSION_HANDLE session;
EXPECT_CKR(CKR_CRYPTOKI_NOT_INITIALIZED,
g_fns->C_OpenSession(g_slot_id, flags, NULL_PTR, NULL_PTR, &session));
EXPECT_CKR(CKR_CRYPTOKI_NOT_INITIALIZED, g_fns->C_CloseSession(1));
CK_MECHANISM mechanism;
EXPECT_CKR(CKR_CRYPTOKI_NOT_INITIALIZED, g_fns->C_EncryptInit(1, &mechanism, 1));
CK_OBJECT_CLASS data_class = CKO_DATA;
CK_ATTRIBUTE attrs[] = {
{CKA_CLASS, &data_class, sizeof(data_class)},
};
CK_ULONG num_attrs = sizeof(attrs) / sizeof(attrs[0]);
CK_OBJECT_HANDLE object;
EXPECT_CKR(CKR_CRYPTOKI_NOT_INITIALIZED,
g_fns->C_CreateObject(1, attrs, num_attrs, &object));
EXPECT_CKR(CKR_CRYPTOKI_NOT_INITIALIZED,
g_fns->C_FindObjectsInit(1, attrs, num_attrs));
}
TEST(Init, DoubleFinalize) {
EXPECT_CKR_OK(g_fns->C_Initialize(NULL_PTR));
EXPECT_CKR_OK(g_fns->C_Finalize(NULL_PTR));
EXPECT_CKR(CKR_CRYPTOKI_NOT_INITIALIZED, g_fns->C_Finalize(NULL_PTR));
}
TEST(Init, UnexpectedFinalize) {
EXPECT_CKR(CKR_CRYPTOKI_NOT_INITIALIZED, g_fns->C_Finalize(NULL_PTR));
}
TEST(Init, InitArgsBadReserved) {
CK_C_INITIALIZE_ARGS init_args = {0};
// PKCS#11 s11.4: The value of pReserved thereby obtained must be NULL_PTR; it it's not, then C_Initialize should
// return with the value CKR_ARGUMENTS_BAD.
init_args.pReserved = (void*)1;
EXPECT_CKR(CKR_ARGUMENTS_BAD, g_fns->C_Initialize(&init_args));
}
TEST(Init, InitArgsNoNewThreads) {
CK_C_INITIALIZE_ARGS init_args = {0};
init_args.flags = CKF_LIBRARY_CANT_CREATE_OS_THREADS;
CK_RV rv = g_fns->C_Initialize(&init_args);
if (rv == CKR_OK) {
if (g_verbose) cout << "Library can cope without creating OS threads" << endl;
EXPECT_CKR_OK(g_fns->C_Finalize(NULL_PTR));
} else {
if (g_verbose) cout << "Library needs to be able to create OS threads" << endl;
EXPECT_CKR(CKR_NEED_TO_CREATE_THREADS, rv);
}
}
TEST(Init, InitArgsNoLock) {
CK_C_INITIALIZE_ARGS init_args = {0};
EXPECT_CKR_OK(g_fns->C_Initialize(&init_args));
EXPECT_CKR_OK(g_fns->C_Finalize(NULL_PTR));
}
TEST(Init, InitArgsInternalLocks) {
CK_C_INITIALIZE_ARGS init_args = {0};
init_args.flags = CKF_OS_LOCKING_OK;
// Expect the library to use OS threading primitives
EXPECT_CKR_OK(g_fns->C_Initialize(&init_args));
EXPECT_CKR_OK(g_fns->C_Finalize(NULL_PTR));
}
namespace {
CK_RV MutexCreate(CK_VOID_PTR_PTR ppMutex) {
return CKR_OK;
}
CK_RV MutexDestroy(CK_VOID_PTR pMutex) {
return CKR_OK;
}
CK_RV MutexLock(CK_VOID_PTR pMutex) {
return CKR_OK;
}
CK_RV MutexUnlock(CK_VOID_PTR pMutex) {
return CKR_OK;
}
} // namespace
TEST(Init, InitArgsMyLocks) {
CK_C_INITIALIZE_ARGS init_args = {0};
init_args.CreateMutex = MutexCreate;
init_args.DestroyMutex = MutexDestroy;
init_args.LockMutex = MutexLock;
init_args.UnlockMutex = MutexUnlock;
CK_RV rv = g_fns->C_Initialize(&init_args);
if (rv == CKR_CANT_LOCK) {
TEST_SKIPPED("Application-provided locking functions not supported");
return;
}
EXPECT_CKR_OK(rv);
EXPECT_CKR_OK(g_fns->C_Finalize(NULL_PTR));
}
TEST(Init, InitArgsMyOrInternalLocks) {
CK_C_INITIALIZE_ARGS init_args = {0};
init_args.CreateMutex = MutexCreate;
init_args.DestroyMutex = MutexDestroy;
init_args.LockMutex = MutexLock;
init_args.UnlockMutex = MutexUnlock;
init_args.flags = CKF_OS_LOCKING_OK;
CK_RV rv = g_fns->C_Initialize(&init_args);
if (rv == CKR_CANT_LOCK) {
TEST_SKIPPED("Application-provided locking functions not supported");
return;
}
EXPECT_CKR_OK(rv);
EXPECT_CKR_OK(g_fns->C_Finalize(NULL_PTR));
}
// From here on, wrap Initialize/Finalize in a fixture.
TEST_F(PKCS11Test, InitNestedFail) {
EXPECT_CKR(CKR_CRYPTOKI_ALREADY_INITIALIZED, g_fns->C_Initialize(NULL_PTR));
}
TEST_F(PKCS11Test, FailedTermination) {
EXPECT_CKR(CKR_ARGUMENTS_BAD, g_fns->C_Finalize((void *)1));
}
TEST_F(PKCS11Test, GetInfo) {
CK_INFO info;
memset(&info, 0, sizeof(info));
EXPECT_CKR_OK(g_fns->C_GetInfo(&info));
if (g_verbose) cout << info_description(&info) << endl;
EXPECT_TRUE(IS_SPACE_PADDED(info.manufacturerID));
EXPECT_TRUE(IS_SPACE_PADDED(info.libraryDescription));
EXPECT_LE(2, info.cryptokiVersion.major);
}
TEST_F(PKCS11Test, GetInfoFail) {
CK_RV rv = g_fns->C_GetInfo(nullptr);
EXPECT_TRUE(rv == CKR_ARGUMENTS_BAD || rv == CKR_FUNCTION_FAILED) << " rv=" << CK_RV_(rv);
}
TEST(Init, GetInfoNoInit) {
CK_INFO info;
memset(&info, 0, sizeof(info));
EXPECT_CKR(CKR_CRYPTOKI_NOT_INITIALIZED, g_fns->C_GetInfo(&info));
}
TEST_F(PKCS11Test, GetFunctionList) {
CK_FUNCTION_LIST_PTR fns;
EXPECT_CKR_OK(g_fns->C_GetFunctionList(&fns));
EXPECT_EQ(0, memcmp(g_fns, fns, sizeof(CK_FUNCTION_LIST)));
}
TEST_F(PKCS11Test, GetFunctionListFail) {
CK_RV rv = g_fns->C_GetFunctionList(nullptr) ;
EXPECT_TRUE(rv == CKR_ARGUMENTS_BAD || rv == CKR_FUNCTION_FAILED) << " rv=" << CK_RV_(rv);
}
} // namespace test
} // namespace pkcs11