-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathyubihsm_pkcs11.c
More file actions
5806 lines (4778 loc) · 163 KB
/
Copy pathyubihsm_pkcs11.c
File metadata and controls
5806 lines (4778 loc) · 163 KB
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2015-2018 Yubico AB
*
* 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.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <cmdline.h>
#include <yubihsm.h>
#include <openssl/rsa.h>
#include "debug_p11.h"
#include "util_pkcs11.h"
#include "yubihsm_pkcs11.h"
#include "../common/insecure_memzero.h"
#include "../common/parsing.h"
#ifdef __WIN32
#include <winsock.h>
#else
#include <arpa/inet.h>
#endif
#ifdef _MSVC
#define strtok_r strtok_s
#endif
#define YUBIHSM_PKCS11_MANUFACTURER "Yubico (www.yubico.com)"
#define YUBIHSM_PKCS11_LIBDESC "YubiHSM PKCS#11 Library"
#define YUBIHSM_PKCS11_MIN_PIN_LEN 12 // key_id (4) + password (8)
#define YUBIHSM_PKCS11_MAX_PIN_LEN 68 // key_id (4) + password (64)
#define UNUSED(x) (void) (x) // TODO(adma): also in yubihsm-shell.h
#define GLOBAL_LOCK_OR_RETURN \
do { \
if (g_ctx.mutex != NULL) { \
CK_RV lock_rv = g_ctx.lock_mutex(g_ctx.mutex); \
if (lock_rv != CKR_OK) { \
DBG_ERR("Unable to acquire global lock"); \
return lock_rv; \
} \
} \
} while (0)
#define GLOBAL_UNLOCK_OR_RETURN \
do { \
if (g_ctx.mutex != NULL) { \
CK_RV lock_rv = g_ctx.unlock_mutex(g_ctx.mutex); \
if (lock_rv != CKR_OK) { \
DBG_ERR("Unable to release global lock"); \
return lock_rv; \
} \
} \
} while (0)
extern CK_FUNCTION_LIST function_list;
static bool g_yh_initialized = false;
static yubihsm_pkcs11_context g_ctx;
static void destroy_slot_mutex(void *data) {
yubihsm_pkcs11_slot *slot = (yubihsm_pkcs11_slot *) data;
if (slot->mutex != NULL) {
g_ctx.destroy_mutex(slot->mutex);
}
slot->mutex = NULL;
}
static bool compare_ecdh_keys(void *data, void *item) {
if (data == NULL || item == NULL) {
return false;
}
CK_OBJECT_HANDLE *a = data;
ListItem *itm = (ListItem *) item;
ecdh_session_key *key = (ecdh_session_key *) &itm->data;
CK_OBJECT_HANDLE b = key->id;
return *a == b;
}
/* General Purpose */
CK_DEFINE_FUNCTION(CK_RV, C_Initialize)(CK_VOID_PTR pInitArgs) {
DIN;
if (g_yh_initialized == true) {
return CKR_CRYPTOKI_ALREADY_INITIALIZED;
}
CK_C_INITIALIZE_ARGS_PTR init_args = pInitArgs;
yh_dbg_init(false, false, 0, "stderr");
if (pInitArgs != NULL) {
if ((init_args->flags & CKF_OS_LOCKING_OK) == 0 &&
init_args->CreateMutex == NULL && init_args->DestroyMutex == NULL &&
init_args->LockMutex == NULL && init_args->UnlockMutex == NULL) {
// NOTE(adma): no threading required
// all is good, do nothing
g_ctx.create_mutex = NULL;
g_ctx.destroy_mutex = NULL;
g_ctx.lock_mutex = NULL;
g_ctx.unlock_mutex = NULL;
} else if ((init_args->flags & CKF_OS_LOCKING_OK) != 0 &&
init_args->CreateMutex == NULL &&
init_args->DestroyMutex == NULL &&
init_args->LockMutex == NULL && init_args->UnlockMutex == NULL) {
// NOTE(adma): threading with native OS locks
set_native_locking(&g_ctx);
} else if ((init_args->flags & CKF_OS_LOCKING_OK) == 0 &&
init_args->CreateMutex != NULL &&
init_args->DestroyMutex != NULL &&
init_args->LockMutex != NULL && init_args->UnlockMutex != NULL) {
// NOTE(adma): threading with supplied functions
g_ctx.create_mutex = init_args->CreateMutex;
g_ctx.destroy_mutex = init_args->DestroyMutex;
g_ctx.lock_mutex = init_args->LockMutex;
g_ctx.unlock_mutex = init_args->UnlockMutex;
} else if ((init_args->flags & CKF_OS_LOCKING_OK) != 0 &&
init_args->CreateMutex != NULL &&
init_args->DestroyMutex != NULL &&
init_args->LockMutex != NULL && init_args->UnlockMutex != NULL) {
// NOTE(adma): threading with native or supplied functions
g_ctx.create_mutex = init_args->CreateMutex;
g_ctx.destroy_mutex = init_args->DestroyMutex;
g_ctx.lock_mutex = init_args->LockMutex;
g_ctx.unlock_mutex = init_args->UnlockMutex;
} else {
DBG_ERR("Invalid locking specified");
return CKR_ARGUMENTS_BAD;
}
}
CK_RV rv = CKR_OK;
if (g_ctx.create_mutex != NULL) {
rv = g_ctx.create_mutex(&g_ctx.mutex);
if (rv != CKR_OK) {
DBG_ERR("Unable to create global mutex");
return rv;
}
} else {
g_ctx.mutex = NULL;
}
struct cmdline_parser_params params = {0};
struct gengetopt_args_info args_info = {0};
cmdline_parser_params_init(¶ms);
params.initialize = 1;
params.check_required = 1;
char *tmp = "";
if (cmdline_parser(0, &tmp, &args_info) != 0) {
DBG_ERR("Unable to initialize ggo structure");
return CKR_GENERAL_ERROR;
}
params.initialize = 0;
params.override = 1;
char *args = NULL;
char *args_parsed = NULL;
yh_connector **connector_list = NULL;
if (init_args != NULL && init_args->pReserved != NULL) {
args = strdup(init_args->pReserved);
if (args == NULL) {
DBG_ERR("Failed copying reserved string");
return CKR_HOST_MEMORY;
}
char *str = args;
char *save = NULL;
char *part;
while ((part = strtok_r(str, " \r\n\t", &save))) {
str = NULL;
size_t len = args_parsed ? strlen(args_parsed) : 0;
char *new_args = realloc(args_parsed, len + strlen(part) + 4);
if (new_args) {
args_parsed = new_args;
sprintf(args_parsed + len, "--%s ", part);
} else {
DBG_ERR("Failed allocating memory for args");
rv = CKR_HOST_MEMORY;
goto c_i_failure;
}
}
DBG_INFO("Now parsing supplied init args as '%s'", args_parsed);
if (cmdline_parser_string_ext(args_parsed, &args_info,
"yubihsm_pkcs11 module", ¶ms) != 0) {
DBG_ERR("Parsing of the reserved init args '%s' failed", args);
rv = CKR_GENERAL_ERROR;
goto c_i_failure;
}
free(args);
args = NULL;
free(args_parsed);
args_parsed = NULL;
}
// NOTE(thorduri): #TOCTOU
char *config_file = args_info.config_file_arg;
struct stat sb = {0};
if (stat(config_file, &sb) == -1) {
config_file = getenv("YUBIHSM_PKCS11_CONF");
}
params.override = 0;
if (config_file != NULL &&
cmdline_parser_config_file(config_file, &args_info, ¶ms) != 0) {
DBG_ERR("Unable to parse configuration file");
rv = CKR_GENERAL_ERROR;
goto c_i_failure;
}
yh_dbg_init(args_info.debug_flag, args_info.dinout_flag,
args_info.libdebug_flag, args_info.debug_file_arg);
// NOTE(adma): it's better to set the argument optional and check its presence
// here
if (args_info.connector_given == 0) {
DBG_ERR("No connector defined");
rv = CKR_ARGUMENTS_BAD;
goto c_i_failure;
}
yh_rc yrc = yh_init();
if (yrc != YHR_SUCCESS) {
DBG_ERR("Unable to initialize libyubihsm: %s", yh_strerror(yrc));
rv = yrc_to_rv(yrc);
goto c_i_failure;
}
DBG_INFO("Found %u configured connector(s)", args_info.connector_given);
connector_list = calloc(args_info.connector_given, sizeof(yh_connector *));
if (connector_list == NULL) {
DBG_ERR("Failed allocating memory");
rv = CKR_HOST_MEMORY;
goto c_i_failure;
}
size_t n_connectors = 0;
for (unsigned int i = 0; i < args_info.connector_given; i++) {
yrc = yh_init_connector(args_info.connector_arg[i], &connector_list[i]);
if (yrc != YHR_SUCCESS) {
DBG_ERR("Failed to init connector: %s", yh_strerror(yrc));
rv = yrc_to_rv(yrc);
goto c_i_failure;
}
if (args_info.cacert_given) {
yrc = yh_set_connector_option(connector_list[i], YH_CONNECTOR_HTTPS_CA,
args_info.cacert_arg);
if (yrc != YHR_SUCCESS) {
DBG_ERR("Failed to set HTTPS CA option: %s", yh_strerror(yrc));
rv = yrc_to_rv(yrc);
goto c_i_failure;
}
}
if (args_info.cert_given) {
yrc = yh_set_connector_option(connector_list[i], YH_CONNECTOR_HTTPS_CERT,
args_info.cert_arg);
if (yrc != YHR_SUCCESS) {
DBG_ERR("Failed to set HTTPS cert option: %s", yh_strerror(yrc));
rv = yrc_to_rv(yrc);
goto c_i_failure;
}
}
if (args_info.key_given) {
yrc = yh_set_connector_option(connector_list[i], YH_CONNECTOR_HTTPS_KEY,
args_info.key_arg);
if (yrc != YHR_SUCCESS) {
DBG_ERR("Failed to set HTTPS key option: %s", yh_strerror(yrc));
rv = yrc_to_rv(yrc);
goto c_i_failure;
}
}
if (args_info.proxy_given) {
yrc =
yh_set_connector_option(connector_list[i], YH_CONNECTOR_PROXY_SERVER,
args_info.proxy_arg);
if (yrc != YHR_SUCCESS) {
DBG_ERR("Failed to set proxy server option: %s", yh_strerror(yrc));
rv = yrc_to_rv(yrc);
goto c_i_failure;
}
}
if (args_info.noproxy_given) {
yrc = yh_set_connector_option(connector_list[i], YH_CONNECTOR_NOPROXY,
args_info.noproxy_arg);
if (yrc != YHR_SUCCESS) {
DBG_ERR("Failed to set noproxy option: %s", yh_strerror(yrc));
rv = yrc_to_rv(yrc);
goto c_i_failure;
}
}
yrc = yh_connect(connector_list[i], args_info.timeout_arg);
if (yrc != YHR_SUCCESS) {
DBG_ERR("Failed to connect to '%s': %s", args_info.connector_arg[i],
yh_strerror(yrc));
continue;
} else {
n_connectors++;
}
}
rv = add_connectors(&g_ctx, args_info.connector_given,
args_info.connector_arg, connector_list);
if (rv != CKR_OK) {
DBG_ERR("Failed building connectors list");
goto c_i_failure;
}
list_create(&g_ctx.device_pubkeys, YH_EC_P256_PUBKEY_LEN, NULL);
for (unsigned int i = 0; i < args_info.device_pubkey_given; i++) {
uint8_t pk[80] = {0};
size_t pk_len = sizeof(pk);
if (hex_decode(args_info.device_pubkey_arg[i], pk, &pk_len) == false ||
pk_len != YH_EC_P256_PUBKEY_LEN) {
DBG_ERR("Invalid device public key configured");
rv = CKR_ARGUMENTS_BAD;
goto c_i_failure;
}
list_append(&g_ctx.device_pubkeys, pk);
}
cmdline_parser_free(&args_info);
free(connector_list);
DBG_INFO("Found %zu usable connector(s)", n_connectors);
DBG_INFO("Found %d configured device public key(s)",
g_ctx.device_pubkeys.length);
g_yh_initialized = true;
DOUT;
return CKR_OK;
c_i_failure:
free(args_parsed);
free(args);
list_iterate(&g_ctx.slots, destroy_slot_mutex);
list_destroy(&g_ctx.slots);
list_destroy(&g_ctx.device_pubkeys);
if (connector_list) {
for (unsigned int i = 0; i < args_info.connector_given; i++) {
yh_disconnect(connector_list[i]);
}
}
cmdline_parser_free(&args_info);
free(connector_list);
if (g_ctx.mutex != NULL) {
g_ctx.destroy_mutex(g_ctx.mutex);
g_ctx.mutex = NULL;
}
DOUT;
return rv;
}
CK_DEFINE_FUNCTION(CK_RV, C_Finalize)(CK_VOID_PTR pReserved) {
DIN;
if (pReserved != NULL) {
DBG_ERR("Finalized called with pReserved != NULL");
return CKR_ARGUMENTS_BAD;
}
if (g_yh_initialized == false) {
DBG_ERR("libyubihsm is not initialized or already finalized");
return CKR_CRYPTOKI_NOT_INITIALIZED;
}
list_iterate(&g_ctx.slots, destroy_slot_mutex);
list_destroy(&g_ctx.slots);
list_destroy(&g_ctx.device_pubkeys);
if (g_ctx.mutex != NULL) {
g_ctx.destroy_mutex(g_ctx.mutex);
g_ctx.mutex = NULL;
}
g_yh_initialized = false;
yh_exit();
DOUT;
if (_YHP11_OUTPUT != stdout && _YHP11_OUTPUT != stderr &&
_YHP11_OUTPUT != NULL) {
fclose(_YHP11_OUTPUT);
_YHP11_OUTPUT = stderr;
}
return CKR_OK;
}
CK_DEFINE_FUNCTION(CK_RV, C_GetInfo)(CK_INFO_PTR pInfo) {
DIN;
if (g_yh_initialized == false) {
DBG_ERR("libyubihsm is not initialized or already finalized");
return CKR_CRYPTOKI_NOT_INITIALIZED;
}
if (pInfo == NULL) {
return CKR_ARGUMENTS_BAD;
}
CK_VERSION ver = {VERSION_MAJOR, (VERSION_MINOR * 10) + VERSION_PATCH};
pInfo->cryptokiVersion = function_list.version;
memset(pInfo->manufacturerID, ' ', sizeof(pInfo->manufacturerID));
memcpy((char *) pInfo->manufacturerID, YUBIHSM_PKCS11_MANUFACTURER,
strlen(YUBIHSM_PKCS11_MANUFACTURER));
pInfo->flags = 0;
memset(pInfo->libraryDescription, ' ', sizeof(pInfo->libraryDescription));
memcpy((char *) pInfo->libraryDescription, YUBIHSM_PKCS11_LIBDESC,
strlen(YUBIHSM_PKCS11_LIBDESC));
pInfo->libraryVersion = ver;
DOUT;
return CKR_OK;
}
CK_DEFINE_FUNCTION(CK_RV, C_GetFunctionList)
(CK_FUNCTION_LIST_PTR_PTR ppFunctionList) {
yh_dbg_init(false, false, 0, "stderr");
DIN;
if (ppFunctionList == NULL) {
DBG_ERR("GetFunctionList called with ppFunctionList = NULL");
return CKR_ARGUMENTS_BAD;
}
*ppFunctionList = &function_list;
DOUT;
return CKR_OK;
}
/* Slot and token management */
CK_DEFINE_FUNCTION(CK_RV, C_GetSlotList)
(CK_BBOOL tokenPresent, CK_SLOT_ID_PTR pSlotList, CK_ULONG_PTR pulCount) {
DIN;
if (g_yh_initialized == false) {
DBG_ERR("libyubihsm is not initialized or already finalized");
return CKR_CRYPTOKI_NOT_INITIALIZED;
}
if (!pulCount) {
DBG_ERR("pulCount argument bad");
return CKR_ARGUMENTS_BAD;
}
GLOBAL_LOCK_OR_RETURN;
if (pSlotList == NULL) {
*pulCount = 0;
// NOTE(adma) just return the number of slots
if (tokenPresent == CK_TRUE) {
for (ListItem *item = g_ctx.slots.head; item != NULL; item = item->next) {
yubihsm_pkcs11_slot *slot = (yubihsm_pkcs11_slot *) item->data;
if (yh_connector_has_device(slot->connector) == true) {
*pulCount += 1;
}
}
DBG_INFO("Number of slots with a token is %lu", *pulCount);
} else {
*pulCount = g_ctx.slots.length;
DBG_INFO("Total number of slots is %lu", *pulCount);
}
// NOTE(adma): actually return the slot IDs
DBG_INFO("Can return %lu slot(s)", *pulCount);
GLOBAL_UNLOCK_OR_RETURN;
DOUT;
return CKR_OK;
}
uint16_t j = 0;
bool full = false;
bool overflow = false;
for (ListItem *item = g_ctx.slots.head; item != NULL; item = item->next) {
if (j == *pulCount) {
full = true;
}
yubihsm_pkcs11_slot *slot = (yubihsm_pkcs11_slot *) item->data;
if (tokenPresent == CK_TRUE) {
if (yh_connector_has_device(slot->connector) != true) {
continue;
}
}
if (full == false) {
pSlotList[j] = slot->id;
DBG_INFO("Returning slot %lu", pSlotList[j]);
} else {
overflow = true;
}
j += 1;
}
*pulCount = j;
if (overflow == true) {
GLOBAL_UNLOCK_OR_RETURN;
return CKR_BUFFER_TOO_SMALL;
}
GLOBAL_UNLOCK_OR_RETURN;
DOUT;
return CKR_OK;
}
CK_DEFINE_FUNCTION(CK_RV, C_GetSlotInfo)
(CK_SLOT_ID slotID, CK_SLOT_INFO_PTR pInfo) {
DIN;
if (g_yh_initialized == false) {
DBG_ERR("libyubihsm is not initialized or already finalized");
return CKR_CRYPTOKI_NOT_INITIALIZED;
}
if (!pInfo) {
DBG_ERR("Invalid pInfo");
return CKR_ARGUMENTS_BAD;
}
yubihsm_pkcs11_slot *slot = get_slot(&g_ctx, slotID);
if (slot == NULL) {
DBG_ERR("Invalid slot ID %lu", slotID);
return CKR_SLOT_ID_INVALID;
}
char *s = "YubiHSM Connector ";
size_t l = strlen(s);
memset(pInfo->slotDescription, ' ', sizeof(pInfo->slotDescription));
memcpy((char *) pInfo->slotDescription, s, l);
yh_get_connector_address(slot->connector, &s);
memcpy((char *) pInfo->slotDescription + l, s, strlen(s));
s = "Yubico";
l = strlen(s);
memset(pInfo->manufacturerID, ' ', sizeof(pInfo->manufacturerID));
memcpy((char *) pInfo->manufacturerID, s, l);
pInfo->flags = CKF_REMOVABLE_DEVICE | CKF_HW_SLOT;
if (yh_connector_has_device(slot->connector) == true) {
pInfo->flags |= CKF_TOKEN_PRESENT;
}
uint8_t major = 0;
uint8_t minor = 0;
uint8_t patch = 0;
yh_get_connector_version(slot->connector, &major, &minor, &patch);
pInfo->hardwareVersion.major = major;
pInfo->hardwareVersion.minor = (minor * 10) + patch;
pInfo->firmwareVersion.major = major;
pInfo->firmwareVersion.minor = (minor * 10) + patch;
release_slot(&g_ctx, slot);
DOUT;
return CKR_OK;
}
CK_DEFINE_FUNCTION(CK_RV, C_GetTokenInfo)
(CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo) {
DIN;
if (g_yh_initialized == false) {
DBG_ERR("libyubihsm is not initialized or already finalized");
return CKR_CRYPTOKI_NOT_INITIALIZED;
}
if (!pInfo) {
DBG_ERR("Invalid pInfo");
return CKR_ARGUMENTS_BAD;
}
yubihsm_pkcs11_slot *slot = get_slot(&g_ctx, slotID);
if (slot == NULL) {
DBG_ERR("Invalid slot ID %lu", slotID);
return CKR_SLOT_ID_INVALID;
}
CK_RV rv = CKR_OK;
if (yh_connector_has_device(slot->connector) == false) {
DBG_ERR("Slot %lu has no token inserted", slotID);
rv = CKR_TOKEN_NOT_PRESENT;
goto c_gt_out;
}
char *s = "YubiHSM";
size_t l = strlen(s);
memset(pInfo->label, ' ', sizeof(pInfo->label));
memcpy((char *) pInfo->label, s, l);
s = YUBIHSM_PKCS11_MANUFACTURER;
l = strlen(s);
memset(pInfo->manufacturerID, ' ', sizeof(pInfo->manufacturerID));
memcpy((char *) pInfo->manufacturerID, s, l);
uint8_t major = 0;
uint8_t minor = 0;
uint8_t patch = 0;
uint32_t serial = 0;
yh_rc yrc = yh_util_get_device_info(slot->connector, &major, &minor, &patch,
&serial, NULL, NULL, NULL, NULL);
if (yrc != YHR_SUCCESS) {
DBG_ERR("Unable to get device version and serial number: %s",
yh_strerror(yrc));
rv = yrc_to_rv(yrc);
goto c_gt_out;
}
s = "YubiHSM";
l = strlen(s);
memset(pInfo->model, ' ', sizeof(pInfo->model));
memcpy((char *) pInfo->model, s, l);
memset(pInfo->serialNumber, ' ', sizeof(pInfo->serialNumber));
l = sprintf((char *) pInfo->serialNumber, "%08u", serial);
pInfo->serialNumber[l] = ' ';
pInfo->flags = CKF_RNG | CKF_LOGIN_REQUIRED | CKF_USER_PIN_INITIALIZED |
CKF_TOKEN_INITIALIZED;
pInfo->ulMaxSessionCount =
CK_EFFECTIVELY_INFINITE; // maximum number of sessions that can be opened
// with the token at one time by a single
// application
pInfo->ulSessionCount =
CK_UNAVAILABLE_INFORMATION; // number of sessions that this application
// currently has open with the token
pInfo->ulMaxRwSessionCount =
CK_EFFECTIVELY_INFINITE; // maximum number of read/write sessions that can
// be opened with the token at one time by a single
// application
pInfo->ulRwSessionCount =
CK_UNAVAILABLE_INFORMATION; // number of read/write sessions that this
// application currently has open with the token
pInfo->ulMaxPinLen =
YUBIHSM_PKCS11_MAX_PIN_LEN; // maximum length in bytes of the PIN
pInfo->ulMinPinLen =
YUBIHSM_PKCS11_MIN_PIN_LEN; // minimum length in bytes of the PIN
pInfo->ulTotalPublicMemory = CK_UNAVAILABLE_INFORMATION;
pInfo->ulFreePublicMemory = CK_UNAVAILABLE_INFORMATION;
pInfo->ulTotalPrivateMemory = CK_UNAVAILABLE_INFORMATION;
pInfo->ulFreePrivateMemory = CK_UNAVAILABLE_INFORMATION;
CK_VERSION ver = {major, (minor * 10) + patch};
pInfo->hardwareVersion = ver;
pInfo->firmwareVersion = ver;
memset(pInfo->utcTime, ' ', sizeof(pInfo->utcTime));
DOUT;
c_gt_out:
release_slot(&g_ctx, slot);
return rv;
}
CK_DEFINE_FUNCTION(CK_RV, C_WaitForSlotEvent)
(CK_FLAGS flags, CK_SLOT_ID_PTR pSlot, CK_VOID_PTR pReserved) {
DIN;
UNUSED(flags);
UNUSED(pSlot);
UNUSED(pReserved);
DOUT;
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_DEFINE_FUNCTION(CK_RV, C_GetMechanismList)
(CK_SLOT_ID slotID, CK_MECHANISM_TYPE_PTR pMechanismList,
CK_ULONG_PTR pulCount) {
DIN;
if (g_yh_initialized == false) {
DBG_ERR("libyubihsm is not initialized or already finalized");
return CKR_CRYPTOKI_NOT_INITIALIZED;
}
if (pulCount == NULL) {
DBG_ERR("Wrong/missing parameter");
return CKR_ARGUMENTS_BAD;
}
yubihsm_pkcs11_slot *slot = get_slot(&g_ctx, slotID);
if (slot == NULL) {
DBG_ERR("Invalid slot ID %lu", slotID);
return CKR_SLOT_ID_INVALID;
}
CK_RV rv = get_mechanism_list(slot, pMechanismList, pulCount);
if (rv != CKR_OK) {
DBG_ERR("Failed getting device info");
goto c_gml_out;
}
DBG_INFO("Found %lu mechanisms", *pulCount);
DOUT;
c_gml_out:
release_slot(&g_ctx, slot);
return rv;
}
CK_DEFINE_FUNCTION(CK_RV, C_GetMechanismInfo)
(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type, CK_MECHANISM_INFO_PTR pInfo) {
DIN;
if (g_yh_initialized == false) {
DBG_ERR("libyubihsm is not initialized or already finalized");
return CKR_CRYPTOKI_NOT_INITIALIZED;
}
if (pInfo == NULL) {
DBG_ERR("Wrong/missing parameter");
return CKR_ARGUMENTS_BAD;
}
yubihsm_pkcs11_slot *slot = get_slot(&g_ctx, slotID);
if (slot == NULL) {
DBG_ERR("Invalid slot ID %lu", slotID);
return CKR_SLOT_ID_INVALID;
}
CK_RV rv = get_mechanism_info(slot, type, pInfo);
release_slot(&g_ctx, slot);
DOUT;
return rv;
}
CK_DEFINE_FUNCTION(CK_RV, C_InitToken)
(CK_SLOT_ID slotID, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen,
CK_UTF8CHAR_PTR pLabel) {
DIN;
UNUSED(slotID);
UNUSED(pPin);
UNUSED(ulPinLen);
UNUSED(pLabel);
DOUT;
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_DEFINE_FUNCTION(CK_RV, C_InitPIN)
(CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen) {
DIN;
UNUSED(hSession);
UNUSED(pPin);
UNUSED(ulPinLen);
DOUT;
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_DEFINE_FUNCTION(CK_RV, C_SetPIN)
(CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pOldPin, CK_ULONG ulOldLen,
CK_UTF8CHAR_PTR pNewPin, CK_ULONG ulNewLen) {
DIN;
UNUSED(hSession);
UNUSED(pOldPin);
UNUSED(ulOldLen);
UNUSED(pNewPin);
UNUSED(ulNewLen);
DOUT;
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_DEFINE_FUNCTION(CK_RV, C_OpenSession)
(CK_SLOT_ID slotID, CK_FLAGS flags, CK_VOID_PTR pApplication, CK_NOTIFY Notify,
CK_SESSION_HANDLE_PTR phSession) {
DIN; // TODO(adma): check pApplication and Notify
UNUSED(Notify);
UNUSED(pApplication);
if (g_yh_initialized == false) {
DBG_ERR("libyubihsm is not initialized or already finalized");
return CKR_CRYPTOKI_NOT_INITIALIZED;
}
if (phSession == NULL) {
DBG_ERR("Wrong/Missing parameter");
return CKR_ARGUMENTS_BAD;
}
if ((flags & CKF_SERIAL_SESSION) == 0) {
// NOTE(adma): required by specs
DBG_ERR("Open session called without CKF_SERIAL_SESSION set");
return CKR_SESSION_PARALLEL_NOT_SUPPORTED;
}
yubihsm_pkcs11_slot *slot = get_slot(&g_ctx, slotID);
if (slot == NULL) {
DBG_ERR("Invalid slot ID %lu", slotID);
return CKR_SLOT_ID_INVALID;
}
CK_RV rv = CKR_OK;
if (yh_connector_has_device(slot->connector) == false) {
DBG_ERR("Slot %lu has no token inserted", slotID);
rv = CKR_TOKEN_NOT_PRESENT;
goto c_os_out;
}
// NOTE(adma): we have already checked that the connector is
// connectable at this point. This function should only "allocate" a
// session pointer
if (create_session(slot, flags, phSession) == false) {
DBG_ERR("Connector %lu has too many open sessions", slotID);
rv = CKR_SESSION_COUNT;
goto c_os_out;
}
DBG_INFO("Allocated session %lu", *phSession);
DOUT;
c_os_out:
release_slot(&g_ctx, slot);
return rv;
}
CK_DEFINE_FUNCTION(CK_RV, C_CloseSession)(CK_SESSION_HANDLE hSession) {
DIN;
if (g_yh_initialized == false) {
DBG_ERR("libyubihsm is not initialized or already finalized");
return CKR_CRYPTOKI_NOT_INITIALIZED;
}
yubihsm_pkcs11_session *session = 0;
CK_RV rv = get_session(&g_ctx, hSession, &session, SESSION_AUTHENTICATED);
if (rv == CKR_OK) {
if (session->slot->pkcs11_sessions.length == 1) {
// NOTE: if this is the last session and is authenticated we need to
// de-auth
yh_rc yrc = yh_util_close_session(session->slot->device_session);
if (yrc != YHR_SUCCESS) {
DBG_ERR("Failed closing device session: %s, continuing",
yh_strerror(yrc));
}
yrc = yh_destroy_session(&session->slot->device_session);
if (yrc != YHR_SUCCESS) {
DBG_ERR("Failed destroying session: %s", yh_strerror(yrc));
// TODO: should we handle the error cases here better?
}
session->slot->device_session = NULL;
}
release_session(&g_ctx, session);
} else if (rv == CKR_SESSION_HANDLE_INVALID) {
// BUG(thorduri): piggybacking off of the validation in get_session()
// above, which might not hold forever.
DBG_ERR("Trying to close invalid session");
return CKR_SESSION_HANDLE_INVALID;
}
if (session) {
list_destroy(&session->ecdh_session_keys);
}
if (delete_session(&g_ctx, &hSession) == false) {
DBG_ERR("Trying to close invalid session");
return CKR_SESSION_HANDLE_INVALID;
}
DBG_INFO("Closing session %lu", hSession);
DOUT;
return CKR_OK;
}
CK_DEFINE_FUNCTION(CK_RV, C_CloseAllSessions)(CK_SLOT_ID slotID) {
DIN;
if (g_yh_initialized == false) {
DBG_ERR("libyubihsm is not initialized or already finalized");
return CKR_CRYPTOKI_NOT_INITIALIZED;
}
yubihsm_pkcs11_slot *slot = get_slot(&g_ctx, slotID);
if (slot == NULL) {
DBG_ERR("Invalid slot");
return CKR_SLOT_ID_INVALID;
}
DBG_INFO("Closing all sessions for slot %lu", slotID);
if (slot->device_session) {
yh_rc yrc = yh_util_close_session(slot->device_session);
if (yrc != YHR_SUCCESS) {
DBG_ERR("Failed closing device session: %s, continuing",
yh_strerror(yrc));
}
yrc = yh_destroy_session(&slot->device_session);
if (yrc != YHR_SUCCESS) {
// TODO: handle or ignore these errrors?
DBG_ERR("Failed destroying device session: %s", yh_strerror(yrc));
}
slot->device_session = NULL;
}
list_destroy(&slot->pkcs11_sessions);
list_create(&slot->pkcs11_sessions, sizeof(yubihsm_pkcs11_session), NULL);