-
Notifications
You must be signed in to change notification settings - Fork 0
/
hsm.c
437 lines (341 loc) · 14.1 KB
/
hsm.c
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dlfcn.h>
#include "hsm.h"
#define ARRAY_LEN(x) (sizeof(x)/sizeof(x[0]))
#define GLOBAL_SESSION 0
CK_FUNCTION_LIST *funcs = NULL;
typedef unsigned long (*C_GetFunctionList_t) (CK_FUNCTION_LIST_PTR_PTR);
void *hHSMLib = NULL;
CK_SLOT_ID C_HSMGetSlotID(const char *tokenName);
CK_SESSION_HANDLE C_HSMOpenSession(CK_SLOT_ID slot, const char *pin);
int C_HSMCloseSession(CK_SESSION_HANDLE *hSession) ;
CK_OBJECT_HANDLE C_HSMFindObjectFromName(CK_SESSION_HANDLE hSession, const char *label) ;
CK_RV C_HSMEncrypt(CK_SESSION_HANDLE hSession, const char *key_label,
unsigned char *plainBuf, size_t plainBufLen,
unsigned char **cipherBuf, size_t *cipherBufLen);
CK_RV C_HSMDecrypt(CK_SESSION_HANDLE hSession, const char *key_label,
unsigned char *cipherBuf, size_t cipherBufLen,
unsigned char **plainBuf, size_t *plainBufLen) ;
CK_BYTE IV[16] = {
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25
};
size_t pkcs7_padding_add(unsigned char *buf, size_t bufLen, unsigned int blockSize, unsigned char **outBuf) {
unsigned char pad_size = (unsigned char)(( blockSize - (bufLen % blockSize) ) & 0xFF );
if (pad_size == blockSize) pad_size = 0 ;
size_t newBuf_size = bufLen + pad_size;
*outBuf = (unsigned char *) calloc(newBuf_size, sizeof(unsigned char)) ;
memcpy(*outBuf, buf, bufLen) ;
memset(&(*outBuf)[bufLen], pad_size,(size_t)pad_size) ;
return newBuf_size ;
}
size_t pkcs7_padding_remove(unsigned char *buf, size_t bufLen, unsigned int blockSize) {
unsigned char pad_size = (unsigned char)( buf[bufLen -1] & 0xFF );
if (pad_size > 0 && pad_size < blockSize) {
unsigned char *tmp = (unsigned char *)calloc(pad_size, sizeof(unsigned char));
memset(tmp, pad_size, pad_size) ;
if (memcmp(&buf[bufLen - pad_size], tmp, pad_size) == 0 ) {
memset(&buf[bufLen - pad_size], 0x0, pad_size ) ;
bufLen -= pad_size;
}
free(tmp);
}
return bufLen ;
}
void HSMClose() {
if (funcs)
funcs->C_Finalize(NULL);
funcs = NULL ;
if (hHSMLib)
dlclose(hHSMLib) ;
hHSMLib = NULL ;
}
CK_RV HSMOpen(const char *libname) {
CK_RV rc = CKR_GENERAL_ERROR ;
C_GetFunctionList_t fn_C_GetFunctionList ;
hHSMLib = dlopen(libname, RTLD_NOW);
if ( hHSMLib ) {
fn_C_GetFunctionList = (C_GetFunctionList_t) dlsym(hHSMLib,"C_GetFunctionList");
if (fn_C_GetFunctionList) {
rc = fn_C_GetFunctionList(&funcs) ;
if (rc == CKR_OK)
rc = funcs->C_Initialize(NULL);
}
}
if (rc != CKR_OK)
HSMClose() ;
return rc ;
}
char *HSMGetManifactureID() {
char *msg = NULL;
if (funcs) {
CK_INFO info ;
memset(&info, 0x0, sizeof(CK_INFO));
CK_RV rc = funcs->C_GetInfo(&info) ;
if (rc == CKR_OK)
msg = strdup( (char *)info.manufacturerID );
}
return msg ;
}
CK_SESSION_HANDLE C_HSMOpenSession(CK_SLOT_ID slot, const char *pin) {
if (! funcs)
return CK_INVALID_HANDLE;
CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE;
CK_RV rv = funcs->C_OpenSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &hSession);
if (rv == CKR_OK) {
rv = funcs->C_Login(hSession,CKU_USER, (unsigned char *)pin, strlen(pin));
if (rv != CKR_OK)
C_HSMCloseSession(&hSession) ;
}
return hSession ;
}
int C_HSMCloseSession(CK_SESSION_HANDLE *hSession) {
if (!funcs)
return -1 ;
if (*hSession != CK_INVALID_HANDLE) {
funcs->C_Logout(*hSession) ;
funcs->C_CloseSession(*hSession) ;
*hSession = CK_INVALID_HANDLE ;
}
return 0 ;
}
CK_SLOT_ID C_HSMGetSlotID(const char *tokenName) {
CK_SLOT_ID slotID = CK_UNAVAILABLE_INFORMATION ;
if (!funcs)
return slotID ;
CK_ULONG ulCount = 0 ;
CK_RV rv = funcs->C_GetSlotList(CK_FALSE, NULL_PTR, &ulCount);
if (rv != CKR_OK || ulCount == 0)
return slotID ;
char name[33] = {0};
memset(name, 0x20, 32) ;
memcpy(name, tokenName, strlen(tokenName) ) ;
CK_SLOT_ID_PTR pSlotList = (CK_SLOT_ID_PTR)calloc(ulCount, sizeof(CK_SLOT_ID)) ;
rv = funcs->C_GetSlotList(CK_FALSE, pSlotList, &ulCount);
if (rv == CKR_OK) {
CK_TOKEN_INFO tokenInfo;
for(unsigned long i = 0 ; i < ulCount ; i++) {
memset(&tokenInfo, 0x0 , sizeof(tokenInfo));
rv = funcs->C_GetTokenInfo(pSlotList[i] , &tokenInfo) ;
if (rv == CKR_OK && strncmp((char *) tokenInfo.label , name, 32) == 0 ) {
slotID = pSlotList[i] ;
break ;
}
}
}
if(pSlotList)
free(pSlotList);
return slotID ;
}
unsigned long HSMGetSlotID(const char *slotName) {
return C_HSMGetSlotID(slotName);
}
CK_OBJECT_HANDLE C_HSMFindObjectFromName(CK_SESSION_HANDLE hSession, const char *label) {
/*
CK_OBJECT_CLASS key_class = CKO_SECRET_KEY;
CK_KEY_TYPE key_type = CKK_AES;
CK_ATTRIBUTE tmpl[] = {
{CKA_CLASS, &key_class, sizeof(key_class)},
{CKA_KEY_TYPE, &key_type, sizeof(key_type)},
{CKA_LABEL, (void *)label, strlen(label)}
};
*/
CK_ATTRIBUTE tmpl[] = { {CKA_LABEL, (void *)label, strlen(label)} };
if (!funcs ) return CK_INVALID_HANDLE ;
if (hSession == CK_INVALID_HANDLE) return CK_INVALID_HANDLE ;
CK_RV rc = funcs->C_FindObjectsInit(hSession, tmpl,1) ;
if (rc != CKR_OK)
return CK_INVALID_HANDLE ;
CK_OBJECT_HANDLE hKey = CK_INVALID_HANDLE ;
unsigned long objCount = 0 ;
rc = funcs->C_FindObjects(hSession, &hKey, 1, &objCount) ;
if (rc != CKR_OK || objCount != 1)
return CK_INVALID_HANDLE ;
funcs->C_FindObjectsFinal(hSession) ;
return hKey ;
}
CK_RV C_HSMEncrypt(CK_SESSION_HANDLE hSession, const char *key_label,
unsigned char *plainBuf, size_t plainBufLen,
unsigned char **cipherBuf, size_t *cipherBufLen) {
if (!funcs ) return CKR_GENERAL_ERROR ;
if (hSession == CK_INVALID_HANDLE) return CKR_GENERAL_ERROR ;
CK_OBJECT_HANDLE hKey = C_HSMFindObjectFromName(hSession, key_label) ;
if (hKey == CK_INVALID_HANDLE)
return CKR_GENERAL_ERROR ;
CK_LONG objSize = 0 ;
CK_LONG keyType = 0 ;
CK_ATTRIBUTE template[] = {
{CKA_KEY_TYPE, &keyType, sizeof(CK_LONG)},
{CKA_VALUE_LEN, &objSize, sizeof(CK_LONG)}
};
CK_RV rv = funcs->C_GetAttributeValue(hSession, hKey, template, ARRAY_LEN(template)) ;
if (rv != CKR_OK)
return rv ;
unsigned char *bufWithPadding = NULL ;
size_t len = pkcs7_padding_add(plainBuf, plainBufLen, objSize, &bufWithPadding) ;
CK_MECHANISM mechanism ;
switch (keyType)
{
case CKK_AES:
mechanism.mechanism = CKM_AES_CBC ;
break;
case CKK_DES:
mechanism.mechanism = CKM_DES_CBC ;
break;
case CKK_DES3:
mechanism.mechanism = CKM_DES3_CBC ;
break;
default:
mechanism.mechanism = 0 ;
break;
}
mechanism.pParameter = IV ;
mechanism.ulParameterLen = sizeof(IV);
rv = funcs->C_EncryptInit(hSession, &mechanism, hKey);
if (rv != CKR_OK)
return rv ;
*cipherBufLen = len + objSize ;
*cipherBuf = (unsigned char *) calloc(*cipherBufLen , sizeof(unsigned char)) ;
rv = funcs->C_Encrypt(hSession, bufWithPadding, len, *cipherBuf, cipherBufLen);
*cipherBuf = realloc(*cipherBuf, *cipherBufLen) ;
if (bufWithPadding)
free(bufWithPadding);
return rv ;
}
CK_RV C_HSMDecrypt(CK_SESSION_HANDLE hSession, const char *key_label,
unsigned char *cipherBuf, size_t cipherBufLen, unsigned char **plainBuf, size_t *plainBufLen)
{
if (!funcs ) return CKR_GENERAL_ERROR ;
if (hSession == CK_INVALID_HANDLE) return CKR_GENERAL_ERROR ;
CK_OBJECT_HANDLE hKey = C_HSMFindObjectFromName(hSession, key_label) ;
if (hKey == CK_INVALID_HANDLE)
return CKR_GENERAL_ERROR ;
CK_LONG objSize = 0 ;
CK_LONG keyType = 0 ;
CK_ATTRIBUTE template[] = {
{CKA_KEY_TYPE, &keyType, sizeof(CK_LONG)},
{CKA_VALUE_LEN, &objSize, sizeof(CK_LONG)}
};
CK_RV rv = funcs->C_GetAttributeValue(hSession, hKey, template, ARRAY_LEN(template)) ;
if (rv != CKR_OK)
return rv ;
CK_MECHANISM mechanism ;
switch (keyType)
{
case CKK_AES:
mechanism.mechanism = CKM_AES_CBC ;
break;
case CKK_DES:
mechanism.mechanism = CKM_DES_CBC ;
break;
case CKK_DES3:
mechanism.mechanism = CKM_DES3_CBC ;
break;
default:
mechanism.mechanism = 0 ;
break;
}
mechanism.pParameter = IV ;
mechanism.ulParameterLen = sizeof(IV);
rv = funcs->C_DecryptInit(hSession, &mechanism, hKey);
if (rv != CKR_OK)
return rv ;
*plainBufLen = cipherBufLen + objSize ;
*plainBuf = (unsigned char *) calloc(*plainBufLen , sizeof(unsigned char)) ;
rv = funcs->C_Decrypt(hSession, cipherBuf, cipherBufLen, *plainBuf, plainBufLen);
*plainBufLen = pkcs7_padding_remove(*plainBuf, *plainBufLen, objSize) ;
*plainBuf = (unsigned char *)realloc(*plainBuf, *plainBufLen);
return rv ;
}
CK_RV C_HSMSign(CK_SESSION_HANDLE hSession, const char *key_label, unsigned char *inBuf, size_t inBufLen,
unsigned char **outBuf, size_t *outBufLen)
{
if (!funcs ) return CKR_GENERAL_ERROR ;
if (hSession == CK_INVALID_HANDLE) return CKR_GENERAL_ERROR ;
CK_OBJECT_HANDLE hKey = C_HSMFindObjectFromName(hSession, key_label) ;
if (hKey == CK_INVALID_HANDLE)
return CKR_GENERAL_ERROR ;
CK_MECHANISM mechanism = {
CKM_RSA_PKCS, NULL_PTR, 0
} ;
unsigned char digest_header[19] = {
0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 };
unsigned char *digest_info = (unsigned char *)calloc(sizeof(digest_header) + inBufLen, sizeof(unsigned char));
memcpy(digest_info, digest_header, sizeof(digest_header)) ;
memcpy(&digest_info[sizeof(digest_header)], inBuf, inBufLen) ;
CK_RV rv = funcs->C_SignInit(hSession, &mechanism, hKey);
if (rv != CKR_OK) return rv ;
*outBufLen = 512 ;
*outBuf = (unsigned char *)calloc(*outBufLen, sizeof(unsigned char));
rv = funcs->C_Sign(hSession, (CK_BYTE_PTR)digest_info, sizeof(digest_header) + inBufLen, *outBuf, outBufLen);
*outBuf = (unsigned char *)realloc(*outBuf, *outBufLen);
free(digest_info);
return rv ;
}
CK_RV C_HSMVerify(CK_SESSION_HANDLE hSession, const char *key_label, unsigned char *inBuf, size_t inBufLen,
unsigned char *signBuf, size_t signBufLen)
{
if (!funcs ) return CKR_GENERAL_ERROR ;
if (hSession == CK_INVALID_HANDLE) return CKR_GENERAL_ERROR ;
CK_OBJECT_HANDLE hKey = C_HSMFindObjectFromName(hSession, key_label) ;
if (hKey == CK_INVALID_HANDLE)
return CKR_GENERAL_ERROR ;
CK_MECHANISM mechanism = {
CKM_RSA_PKCS, NULL_PTR, 0
} ;
CK_RV rv = funcs->C_VerifyInit(hSession, &mechanism, hKey);
if (rv != CKR_OK) return rv ;
unsigned char digest_header[19] = {
0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 };
unsigned char *digest_info = (unsigned char *)calloc(sizeof(digest_header) + inBufLen, sizeof(unsigned char));
memcpy(digest_info, digest_header, sizeof(digest_header)) ;
memcpy(&digest_info[sizeof(digest_header)], inBuf, inBufLen) ;
rv = funcs->C_Verify(hSession, (CK_BYTE_PTR)digest_info, sizeof(digest_header) + inBufLen, signBuf, signBufLen);
free(digest_info);
return rv ;
}
CK_RV HSMEncrypt(unsigned long slotID, const char *user, const char *pass,
const char *key_label, unsigned char *plainBuf, size_t plainBufLen, unsigned char **cipherBuf, size_t *cipherBufLen)
{
if (slotID == CK_UNAVAILABLE_INFORMATION || !key_label || !plainBuf || !plainBufLen) return CKR_GENERAL_ERROR;
CK_SESSION_HANDLE hSession = C_HSMOpenSession(slotID, pass ) ;
if (hSession == CK_INVALID_HANDLE) return CKR_GENERAL_ERROR ;
CK_RV rv= C_HSMEncrypt(hSession, key_label, plainBuf, plainBufLen, cipherBuf, cipherBufLen) ;
C_HSMCloseSession(&hSession);
return rv ;
}
CK_RV HSMDecrypt(unsigned long slotID, const char *user, const char *pass,
const char *key_label, unsigned char *cipherBuf, size_t cipherBufLen, unsigned char **plainBuf, size_t *plainBufLen)
{
if (slotID == CK_UNAVAILABLE_INFORMATION || !key_label || !cipherBuf || !cipherBufLen) return CKR_GENERAL_ERROR;
CK_SESSION_HANDLE hSession = C_HSMOpenSession(slotID, pass ) ;
if (hSession == CK_INVALID_HANDLE) return CKR_GENERAL_ERROR ;
CK_RV rv= C_HSMDecrypt(hSession, key_label, cipherBuf, cipherBufLen, plainBuf, plainBufLen) ;
C_HSMCloseSession(&hSession);
return rv ;
}
CK_RV HSMVerify(unsigned long slotID, const char *user, const char *pass,
const char *key_label, unsigned char *inBuf, size_t inBufLen, unsigned char *signBuf, size_t signBufLen)
{
if (slotID == CK_UNAVAILABLE_INFORMATION || !key_label || !inBuf || !inBufLen || !signBuf || !signBufLen) return CKR_GENERAL_ERROR;
CK_SESSION_HANDLE hSession = C_HSMOpenSession(slotID, pass ) ;
if (hSession == CK_INVALID_HANDLE) return CKR_GENERAL_ERROR ;
CK_RV rv= C_HSMVerify(hSession, key_label, inBuf, inBufLen, signBuf, signBufLen) ;
C_HSMCloseSession(&hSession);
return rv ;
}
CK_RV HSMSign(unsigned long slotID, const char *user, const char *pass,
const char *key_label, unsigned char *inBuf, size_t inBufLen, unsigned char **outBuf, size_t *outBufLen)
{
if (slotID == CK_UNAVAILABLE_INFORMATION || !key_label || !inBuf || !inBufLen ) return CKR_GENERAL_ERROR;
CK_SESSION_HANDLE hSession = C_HSMOpenSession(slotID, pass ) ;
if (hSession == CK_INVALID_HANDLE) return CKR_GENERAL_ERROR ;
CK_RV rv= C_HSMSign(hSession, key_label, inBuf, inBufLen, outBuf, outBufLen) ;
C_HSMCloseSession(&hSession);
return rv ;
}