-
Notifications
You must be signed in to change notification settings - Fork 5
/
PKCS11Manager.cpp
189 lines (138 loc) · 5.9 KB
/
PKCS11Manager.cpp
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
/*
The MIT License (MIT)
Copyright (c) 2014 - Commonwealth of Australia (Represented by the Department of Defence)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "stdafx.h"
#include <Windows.h>
#include <strstream>
#include "Utility.h"
#include "PKCS11Manager.h"
#include "Log.h"
using namespace std;
// Pointer to the Singleton instance
PKCS11Manager * PKCS11Manager::m_Instance = NULL;
// Handle to the PKCS library
HINSTANCE PKCS11Manager::hPKCS11 = NULL;
// PKCS11 Function Pointer
CK_FUNCTION_LIST * PKCS11Manager::pPKCS11 = NULL;
// TODO: Create ERR valeus for all exceptions thrown
PKCS11Manager::PKCS11Manager(void)
{
}
PKCS11Manager::~PKCS11Manager(void)
{
this->Destroy();
}
PKCS11Manager* PKCS11Manager::Create(LPCTSTR libraryPath) {
// Has this object already been created? If so, just pass it back
if (m_Instance != NULL) {
return m_Instance;
}
// Attempt to load the library via the supplied module path
hPKCS11 = LoadLibrary(libraryPath);
if (NULL == hPKCS11) {
Log::error("PKCS11Manager::Create: Unable to load the supplied PKCS11 module.\n");
throw;
}
// Load the reference to C_GetFunctionList
CK_C_GetFunctionList pC_GetFunctionList = NULL;
pC_GetFunctionList = (CK_C_GetFunctionList) GetProcAddress(hPKCS11, "C_GetFunctionList");
if (pC_GetFunctionList == NULL)
{
Log::error("PKCS11Manager::Create: GetProcAddress on C_GetFunctionList failed.\n");
FreeLibrary(hPKCS11);
hPKCS11 = NULL;
throw;
}
// Call C_GetFunctionList
CK_RV result;
result = (*pC_GetFunctionList) (&pPKCS11);
if (result != CKR_OK)
{
FreeLibrary(hPKCS11);
pPKCS11 = NULL;
hPKCS11 = NULL;
// Force a throw
Utility::ThrowOnError(result, "PKCS11Manager::Create", "C_GetFunctionList");
}
// Call C_Initialize;
result = pPKCS11->C_Initialize(NULL_PTR);
if (result != CKR_OK) {
FreeLibrary(hPKCS11);
pPKCS11 = NULL;
hPKCS11 = NULL;
// Force a throw
Utility::ThrowOnError(result, "PKCS11Manager::Create", "C_Initialize");
}
// Call C_GetInfo
CK_INFO info;
result = pPKCS11->C_GetInfo(&info);
if (result != CKR_OK) {
FreeLibrary(hPKCS11);
pPKCS11 = NULL;
hPKCS11 = NULL;
// Force a throw
Utility::ThrowOnError(result, "PKCS11Manager::Create", "C_GetInfo");
}
string manufacturer = Utility::CK_UTF8CHARtoString(info.manufacturerID, 32);
string libraryDescription = Utility::CK_UTF8CHARtoString(info.libraryDescription, 32);
Log::debug("PKCS11:Create: CryptoKi Version %d.%d\n", info.cryptokiVersion.major, info.cryptokiVersion.minor);
Log::debug("PKCS11:Create: Manufacturer ID '%s'\n", manufacturer.c_str());
Log::debug("PKCS11:Create: Library Description '%s'\n", libraryDescription.c_str());
Log::debug("PKCS11:Create: Library Version %d.%d\n", info.libraryVersion.major, info.libraryVersion.minor);
// Create our new PKCS11Manager instance and return it;
m_Instance = new PKCS11Manager();
return m_Instance;
}
void PKCS11Manager::Destroy() {
if (NULL == hPKCS11) return;
pPKCS11->C_Finalize(NULL_PTR);
FreeLibrary(hPKCS11);
pPKCS11 = NULL;
hPKCS11 = NULL;
}
int PKCS11Manager::QuerySlots(bool tokenPresent, vector<PKCS11Slot> * slots) {
Log::debug("PKCS11Manager::QuerySlots: Called\n");
// Get slot count
CK_ULONG count;
pPKCS11->C_GetSlotList(tokenPresent ? CK_TRUE : CK_FALSE, NULL_PTR, &count);
// No point if there aren't any
if (count == 0) return count;
// List slots
CK_SLOT_ID * slotIds = new CK_SLOT_ID[count];
pPKCS11->C_GetSlotList(tokenPresent ? CK_TRUE : CK_FALSE, slotIds, &count);
// Retrieve the slot info
for (CK_ULONG i = 0; i < count; i++) {
CK_SLOT_INFO info;
PKCS11Slot slot(this->pPKCS11);
CK_RV result;
result = pPKCS11->C_GetSlotInfo(slotIds[i], &info);
Utility::ThrowOnError(result, "PKCS11Manager::QuerySlots", "C_GetSlotInfo");
slot.id = slotIds[i];
slot.manufacturer = Utility::CK_UTF8CHARtoString(info.manufacturerID, 32);
slot.description = Utility::CK_UTF8CHARtoString(info.slotDescription, 64);
slot.isTokenPresent = (bool)((info.flags & CKF_TOKEN_PRESENT) != 0);
slot.isTokenRemovable = (bool)((info.flags & CKF_REMOVABLE_DEVICE) != 0);
slot.isHardware = (bool)((info.flags & CKF_HW_SLOT) != 0);
Log::debug("PKCS11Manager::QuerySlots: ADDING SLOT %u\n", slot.id);
Log::debug("PKCS11Manager::QuerySlots: Manufacturer = %s\n", slot.manufacturer.c_str());
Log::debug("PKCS11Manager::QuerySlots: Description = %s\n", slot.description.c_str());
slots->push_back(slot);
}
return count;
}