-
Notifications
You must be signed in to change notification settings - Fork 712
/
minidriver.c
7261 lines (6157 loc) · 221 KB
/
minidriver.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
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
/*
* minidriver.c: OpenSC minidriver
*
* Copyright (C) 2009,2010 [email protected]
* Copyright (C) 2015 [email protected]
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* This module requires "cardmod.h" from CNG SDK or platform SDK to build.
*/
#include "config.h"
#ifdef ENABLE_MINIDRIVER
#ifdef _MANAGED
#pragma managed(push, off)
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <windows.h>
#include <commctrl.h>
#ifdef __MINGW32__
#include <mmsystem.h>
#else
#include <timeapi.h>
#endif
#include "common/compat_strlcpy.h"
#include "libopensc/asn1.h"
#include "libopensc/cardctl.h"
#include "libopensc/opensc.h"
#include "libopensc/pkcs15.h"
#include "libopensc/log.h"
#include "libopensc/internal.h"
#include "libopensc/aux-data.h"
#include "libopensc/sc-ossl-compat.h"
#include "ui/notify.h"
#include "ui/strings.h"
#include "ui/wchar_from_char_str.h"
#include "pkcs15init/pkcs15-init.h"
#ifdef ENABLE_OPENSSL
#include <openssl/opensslv.h>
#include <openssl/pem.h>
#include <openssl/crypto.h>
#endif
#ifdef ENABLE_OPENPACE
#include <eac/eac.h>
#endif
#if defined(__MINGW32__)
#include "cardmod-mingw-compat.h"
#endif
#include "cardmod.h"
#define MD_FUNC_CALLED(pCardData, level) do { \
logprintf(pCardData, level, "MD_Function:%s:%d called\n",__FUNCTION__, __LINE__); \
} while(0)
#define MD_FUNC_CALLED_EXT(pCardData, level) do { \
logprintf(pCardData, level, "\nP:%lu T:%lu MD_Function:%s:%d called\n", \
(unsigned long)GetCurrentProcessId(),\
(unsigned long)GetCurrentThreadId(), \
__FUNCTION__, __LINE__); \
} while(0)
#define MD_FUNC_RETURN(pCardData, level, ...) do { \
DWORD _ret = __VA_ARGS__; \
logprintf(pCardData, level,\
"MD_Function:%s:%d returning with: 0x%08X\n", __FUNCTION__, __LINE__, (unsigned)_ret); \
return _ret; \
} while(0)
/* store the instance given at DllMain when attached to access internal resources */
HINSTANCE g_inst;
#define MD_MINIMUM_VERSION_SUPPORTED 4
#define MD_CURRENT_VERSION_SUPPORTED 7
#define NULLSTR(a) (a == NULL ? "<NULL>" : a)
#define NULLWSTR(a) (a == NULL ? L"<NULL>" : a)
#define MD_MAX_KEY_CONTAINERS 32
#define MD_CARDID_SIZE 16
#define MD_ROLE_USER_SIGN (ROLE_ADMIN + 1)
/*
* must be higher than MD_ROLE_USER_SIGN and
* less than or equal MAX_PINS
*/
#define MD_MAX_PINS MAX_PINS
#define MD_CARDCF_LENGTH (sizeof(CARD_CACHE_FILE_FORMAT))
#define MD_KEY_USAGE_KEYEXCHANGE \
SC_PKCS15INIT_X509_KEY_ENCIPHERMENT | \
SC_PKCS15INIT_X509_DATA_ENCIPHERMENT | \
SC_PKCS15INIT_X509_DIGITAL_SIGNATURE
#define MD_KEY_USAGE_KEYEXCHANGE_ECC \
SC_PKCS15INIT_X509_KEY_AGREEMENT| \
SC_PKCS15INIT_X509_DIGITAL_SIGNATURE
#define MD_KEY_USAGE_SIGNATURE \
SC_PKCS15INIT_X509_DIGITAL_SIGNATURE | \
SC_PKCS15INIT_X509_KEY_CERT_SIGN | \
SC_PKCS15INIT_X509_CRL_SIGN
#define MD_KEY_ACCESS \
SC_PKCS15_PRKEY_ACCESS_SENSITIVE | \
SC_PKCS15_PRKEY_ACCESS_ALWAYSSENSITIVE | \
SC_PKCS15_PRKEY_ACCESS_NEVEREXTRACTABLE | \
SC_PKCS15_PRKEY_ACCESS_LOCAL
/* copied from pkcs15-cardos.c */
#define USAGE_ANY_SIGN (SC_PKCS15_PRKEY_USAGE_SIGN | SC_PKCS15_PRKEY_USAGE_NONREPUDIATION)
#define USAGE_ANY_DECIPHER (SC_PKCS15_PRKEY_USAGE_DECRYPT | SC_PKCS15_PRKEY_USAGE_UNWRAP)
#define USAGE_ANY_AGREEMENT (SC_PKCS15_PRKEY_USAGE_DERIVE)
/* if use of internal-winscard.h */
#ifndef SCARD_E_INVALID_PARAMETER
#define SCARD_E_INVALID_PARAMETER 0x80100004L
#define SCARD_E_UNSUPPORTED_FEATURE 0x80100022L
#define SCARD_E_NO_MEMORY 0x80100006L
#define SCARD_W_WRONG_CHV 0x8010006BL
#define SCARD_E_FILE_NOT_FOUND 0x80100024L
#define SCARD_E_UNKNOWN_CARD 0x8010000DL
#define SCARD_F_UNKNOWN_ERROR 0x80100014L
#endif
/* defined twice: in versioninfo-minidriver.rc.in and in minidriver.c */
#define IDI_SMARTCARD 102
#define SUBKEY_ENABLE_CANCEL "Software\\OpenSC Project\\OpenSC\\md_pinpad_dlg_enable_cancel"
/* magic to determine previous pinpad authentication */
#define MAGIC_SESSION_PIN "opensc-minidriver"
#define TLS1_0_PROTOCOL_VERSION 0x0301
#define TLS1_1_PROTOCOL_VERSION 0x0302
#define TLS1_2_PROTOCOL_VERSION 0x0303
#define TLS_DERIVE_KEY_SIZE 48
struct md_directory {
unsigned char name[9];
CARD_DIRECTORY_ACCESS_CONDITION acl;
struct md_file *files;
struct md_directory *subdirs;
struct md_directory *next;
};
struct md_file {
unsigned char name[9];
CARD_FILE_ACCESS_CONDITION acl;
unsigned char *blob;
size_t size;
struct md_file *next;
};
struct md_pkcs15_container {
int index;
struct sc_pkcs15_id id;
char guid[MAX_CONTAINER_NAME_LEN + 1];
unsigned char flags;
size_t size_key_exchange, size_sign;
struct sc_pkcs15_object *cert_obj, *prkey_obj, *pubkey_obj;
// BOOL guid_overwrite;
};
struct md_dh_agreement {
DWORD dwSize;
PBYTE pbAgreement;
};
struct md_guid_conversion {
CHAR szOpenSCGuid[MAX_CONTAINER_NAME_LEN+1];
CHAR szWindowsGuid[MAX_CONTAINER_NAME_LEN+1];
};
#define MD_MAX_CONVERSIONS 50
struct md_guid_conversion md_static_conversions[MD_MAX_CONVERSIONS] = {0};
typedef struct _VENDOR_SPECIFIC
{
BOOL initialized;
struct sc_pkcs15_object *pin_objs[MD_MAX_PINS];
struct sc_context *ctx;
struct sc_reader *reader;
struct sc_card *card;
struct sc_pkcs15_card *p15card;
struct md_pkcs15_container p15_containers[MD_MAX_KEY_CONTAINERS];
struct md_directory root;
SCARDCONTEXT hSCardCtx;
SCARDHANDLE hScard;
/* These will be used in CardAuthenticateEx to display a dialog box when doing
* external PIN verification.
*/
HWND hwndParent;
LPWSTR wszPinContext;
/* these will be used to store intermediate dh agreements results */
struct md_dh_agreement* dh_agreements;
BYTE allocatedAgreements;
CRITICAL_SECTION hScard_lock;
} VENDOR_SPECIFIC;
static DWORD md_translate_OpenSC_to_Windows_error(int OpenSCerror,
DWORD dwDefaulCode);
static DWORD associate_card(PCARD_DATA pCardData);
static void disassociate_card(PCARD_DATA pCardData);
static DWORD md_pkcs15_delete_object(PCARD_DATA pCardData, struct sc_pkcs15_object *obj);
static DWORD md_fs_init(PCARD_DATA pCardData);
static void md_fs_finalize(PCARD_DATA pCardData);
#if defined(__GNUC__)
static void logprintf(PCARD_DATA pCardData, int level, const char* format, ...)
__attribute__ ((format (SC_PRINTF_FORMAT, 3, 4)));
#endif
static void logprintf(PCARD_DATA pCardData, int level, _Printf_format_string_ const char* format, ...)
{
va_list arg;
VENDOR_SPECIFIC *vs;
/* Use a simplified log to get all messages including messages
* before opensc is loaded. The file must be modifiable by all
* users as we maybe called under lsa or user. Note data from
* multiple process and threads may get intermingled.
* flush to get last message before any crash
* close so as the file is not left open during any wait.
*/
DWORD md_debug = 0;
size_t sz = sizeof(md_debug);
int rv;
rv = sc_ctx_win32_get_config_value("CARDMOD_LOW_LEVEL_DEBUG",
"MiniDriverDebug", "Software\\OpenSC Project\\OpenSC",
(char *)(&md_debug), &sz);
if (rv == SC_SUCCESS && md_debug != 0) {
FILE *lldebugfp = fopen("C:\\tmp\\md.log","a+");
if (lldebugfp) {
va_start(arg, format);
vfprintf(lldebugfp, format, arg);
va_end(arg);
fflush(lldebugfp);
fclose(lldebugfp);
}
}
va_start(arg, format);
if(pCardData != NULL) {
vs = (VENDOR_SPECIFIC*)(pCardData->pvVendorSpecific);
if(vs != NULL && vs->ctx != NULL)
sc_do_log_noframe(vs->ctx, level, format, arg);
}
va_end(arg);
}
static void loghex(PCARD_DATA pCardData, int level, PBYTE data, size_t len)
{
char line[74];
char *c;
unsigned int i, a;
unsigned char * p;
logprintf(pCardData, level, "--- %p:%"SC_FORMAT_LEN_SIZE_T"u\n",
data, len);
if (data == NULL || len <= 0) return;
p = data;
c = line;
i = 0;
a = 0;
memset(line, 0, sizeof(line));
while(i < len) {
snprintf(c, sizeof(line) - (size_t)(c - line), "%02X", *p);
line[sizeof(line) - 1] = 0;
p++;
c += 2;
i++;
if (i%32 == 0) {
logprintf(pCardData, level, " %04X %s\n", a, line);
a +=32;
memset(line, 0, sizeof(line));
c = line;
} else {
if (i%4 == 0) *(c++) = ' ';
if (i%16 == 0) *(c++) = ' ';
}
}
if (i%32 != 0)
logprintf(pCardData, level, " %04X %s\n", a, line);
}
static DWORD reinit_card(PCARD_DATA pCardData)
{
VENDOR_SPECIFIC *vs;
DWORD r;
if (!pCardData)
MD_FUNC_RETURN(pCardData, 1, SCARD_E_INVALID_PARAMETER);
vs = (VENDOR_SPECIFIC *)(pCardData->pvVendorSpecific);
if (!vs)
MD_FUNC_RETURN(pCardData, 1, SCARD_E_INVALID_PARAMETER);
if (vs->initialized) {
disassociate_card(pCardData);
md_fs_finalize(pCardData);
}
r = associate_card(pCardData);
if (r != SCARD_S_SUCCESS)
MD_FUNC_RETURN(pCardData, 1, r);
r = md_fs_init(pCardData);
if (r != SCARD_S_SUCCESS) {
logprintf(pCardData, 1,
"reinit_card md_fs_init failed, r = 0x%lX\n",
(unsigned long)r);
disassociate_card(pCardData);
MD_FUNC_RETURN(pCardData, 1, r);
}
MD_FUNC_RETURN(pCardData, 1, SCARD_S_SUCCESS);
}
static BOOL lock(PCARD_DATA pCardData)
{
if (pCardData) {
VENDOR_SPECIFIC *vs = (VENDOR_SPECIFIC*)(pCardData->pvVendorSpecific);
if (vs) {
EnterCriticalSection(&vs->hScard_lock);
return TRUE;
}
}
return FALSE;
}
static void unlock(PCARD_DATA pCardData)
{
if (pCardData) {
VENDOR_SPECIFIC *vs = (VENDOR_SPECIFIC*)(pCardData->pvVendorSpecific);
if (vs) {
LeaveCriticalSection(&vs->hScard_lock);
}
}
}
static DWORD reinit_card_for(PCARD_DATA pCardData, const char *name)
{
DWORD r;
r = reinit_card(pCardData);
if (r != SCARD_S_SUCCESS)
logprintf(pCardData, 1,
"%s was called, but unable to initialize card, r = %u\n",
name, (unsigned int)r);
MD_FUNC_RETURN(pCardData, 1, r);
}
static DWORD check_card_status(PCARD_DATA pCardData, const char *name)
{
VENDOR_SPECIFIC *vs;
MD_FUNC_CALLED(pCardData, 3);
if (!pCardData)
MD_FUNC_RETURN(pCardData, 3, SCARD_E_INVALID_PARAMETER);
vs = (VENDOR_SPECIFIC *)(pCardData->pvVendorSpecific);
if (!vs)
MD_FUNC_RETURN(pCardData, 3, SCARD_E_INVALID_PARAMETER);
if (vs->initialized)
MD_FUNC_RETURN(pCardData, 3, SCARD_S_SUCCESS);
MD_FUNC_RETURN(pCardData, 3, reinit_card_for(pCardData, name));
}
/*
* check if the card is OK, has been removed, or the
* caller has changed the handles.
* if so, then try to reinit card
* or if different handles but same reader, just use the handles
*/
static DWORD
check_card_reader_status(PCARD_DATA pCardData, const char *name)
{
VENDOR_SPECIFIC *vs = NULL;
DWORD dwRet;
int r;
logprintf(pCardData, 1, "check_reader_status for %s\n", name);
if(!pCardData)
MD_FUNC_RETURN(pCardData, 1, SCARD_E_INVALID_PARAMETER);
dwRet = check_card_status(pCardData, name);
if (dwRet != SCARD_S_SUCCESS)
MD_FUNC_RETURN(pCardData, 3, dwRet);
vs = (VENDOR_SPECIFIC*)(pCardData->pvVendorSpecific);
if(!vs)
MD_FUNC_RETURN(pCardData, 3, SCARD_E_INVALID_PARAMETER);
logprintf(pCardData, 7, "sizeof(size_t):%u sizeof(ULONG_PTR):%u sizeof(__int3264):%u sizeof pCardData->hSCardCtx:%u\n",
(unsigned)sizeof(size_t), (unsigned)sizeof(ULONG_PTR), (unsigned)sizeof(__int3264), (unsigned)sizeof(pCardData->hSCardCtx));
logprintf(pCardData, 1, "pCardData->hSCardCtx:0x%08"SC_FORMAT_LEN_SIZE_T"X hScard:0x%08"SC_FORMAT_LEN_SIZE_T"X\n",
(size_t)pCardData->hSCardCtx,
(size_t)pCardData->hScard);
if (pCardData->hSCardCtx != vs->hSCardCtx || pCardData->hScard != vs->hScard) {
logprintf(pCardData, 1, "HANDLES CHANGED from vs->hSCardCtx:0x%08"SC_FORMAT_LEN_SIZE_T"X vs->hScard:0x%08"SC_FORMAT_LEN_SIZE_T"X\n",
(size_t)vs->hSCardCtx,
(size_t)vs->hScard);
if (vs->ctx) {
if (1 == pcsc_check_reader_handles(vs->ctx, vs->reader, &pCardData->hSCardCtx, &pCardData->hScard)) {
_sc_delete_reader(vs->ctx, vs->reader);
MD_FUNC_RETURN(pCardData, 1, reinit_card_for(pCardData, name));
} else {
vs->hScard = pCardData->hScard;
vs->hSCardCtx = pCardData->hSCardCtx;
r = sc_ctx_use_reader(vs->ctx, &vs->hSCardCtx, &vs->hScard);
logprintf(pCardData, 1, "sc_ctx_use_reader returned %d\n", r);
if (r)
MD_FUNC_RETURN(pCardData, 1, SCARD_F_INTERNAL_ERROR);
}
}
}
/* This should always work, as BaseCSP should be checking for removal too */
r = sc_detect_card_presence(vs->reader);
logprintf(pCardData, 2,
"check_reader_status r=%d flags 0x%08X\n", r,
(unsigned int)vs->reader->flags);
if (r < 0)
MD_FUNC_RETURN(pCardData, 1,
md_translate_OpenSC_to_Windows_error(r, SCARD_F_INTERNAL_ERROR));
if (!(r & SC_READER_CARD_PRESENT)) {
/*
* if there is really no card present it may not make sense to
* try initializing the card but since it won't hurt let's try
* it anyway for completeness
*/
logprintf(pCardData, 1, "no card present? trying to reinit\n");
MD_FUNC_RETURN(pCardData, 1, reinit_card_for(pCardData, name));
}
MD_FUNC_RETURN(pCardData, 1, SCARD_S_SUCCESS);
}
static DWORD
get_pin_by_name(PCARD_DATA pCardData, struct sc_pkcs15_card *p15card, int role, struct sc_pkcs15_object **ret_obj)
{
scconf_block *conf_block = NULL;
scconf_block **blocks = NULL;
char *pin_type;
char str_path[SC_MAX_AID_STRING_SIZE];
char *pin = NULL;
struct sc_pkcs15_id id;
MD_FUNC_CALLED(pCardData, 1);
switch (role) {
case ROLE_USER:
pin_type = "user_pin";
break;
case MD_ROLE_USER_SIGN:
pin_type = "sign_pin";
break;
default:
MD_FUNC_RETURN(pCardData, 1, SCARD_E_INVALID_PARAMETER);
}
conf_block = sc_get_conf_block(p15card->card->ctx, "framework", "pkcs15", 1);
if (!conf_block)
MD_FUNC_RETURN(pCardData, 1, SCARD_F_INTERNAL_ERROR);
memset(str_path, 0, sizeof(str_path));
sc_bin_to_hex(p15card->app->path.value, p15card->app->path.len, str_path, sizeof(str_path), 0);
blocks = scconf_find_blocks(p15card->card->ctx->conf, conf_block, "application", str_path);
if (blocks) {
if (blocks[0]) {
pin = (char *)scconf_get_str(blocks[0], pin_type, NULL);
}
free(blocks);
}
if (!pin)
MD_FUNC_RETURN(pCardData, 1, SCARD_F_INTERNAL_ERROR);
strncpy((char*)id.value, pin, sizeof(id.value) - 1);
id.len = strlen(pin);
if (id.len > sizeof(id.value))
id.len = sizeof(id.value);
MD_FUNC_RETURN(pCardData, 1, sc_pkcs15_find_pin_by_auth_id(p15card, &id, ret_obj) ? SCARD_F_INTERNAL_ERROR : SCARD_S_SUCCESS);
}
static DWORD
md_get_pin_by_role(PCARD_DATA pCardData, PIN_ID role, struct sc_pkcs15_object **ret_obj)
{
VENDOR_SPECIFIC *vs;
int rv;
MD_FUNC_CALLED(pCardData, 1);
if (!pCardData)
MD_FUNC_RETURN(pCardData, 1, SCARD_E_INVALID_PARAMETER);
vs = (VENDOR_SPECIFIC*)(pCardData->pvVendorSpecific);
if (!ret_obj)
MD_FUNC_RETURN(pCardData, 1, SCARD_E_INVALID_PARAMETER);
rv = get_pin_by_name(pCardData, vs->p15card, role, ret_obj);
if (!rv)
goto out;
/* please keep me in sync with _get_auth_object_by_name() in pkcs11/framework-pkcs15.c */
if (role == ROLE_USER) {
/* Get 'global' User PIN; if no, get the 'local' one */
rv = sc_pkcs15_find_pin_by_flags(vs->p15card, SC_PKCS15_PIN_TYPE_FLAGS_PIN_GLOBAL,
SC_PKCS15_PIN_TYPE_FLAGS_MASK, NULL, ret_obj);
if (rv)
rv = sc_pkcs15_find_pin_by_flags(vs->p15card, SC_PKCS15_PIN_TYPE_FLAGS_PIN_LOCAL,
SC_PKCS15_PIN_TYPE_FLAGS_MASK, NULL, ret_obj);
}
else if (role == MD_ROLE_USER_SIGN) {
int idx = 0;
/* Get the 'global' user PIN */
rv = sc_pkcs15_find_pin_by_flags(vs->p15card, SC_PKCS15_PIN_TYPE_FLAGS_PIN_GLOBAL,
SC_PKCS15_PIN_TYPE_FLAGS_MASK, NULL, ret_obj);
if (!rv) {
/* Global (user) PIN exists, get the local one -- sign PIN */
rv = sc_pkcs15_find_pin_by_flags(vs->p15card, SC_PKCS15_PIN_TYPE_FLAGS_PIN_LOCAL,
SC_PKCS15_PIN_TYPE_FLAGS_MASK, NULL, ret_obj);
}
else {
/* No global PIN, try to get first local one -- user PIN */
rv = sc_pkcs15_find_pin_by_flags(vs->p15card, SC_PKCS15_PIN_TYPE_FLAGS_PIN_LOCAL,
SC_PKCS15_PIN_TYPE_FLAGS_MASK, &idx, ret_obj);
if (!rv) {
/* User PIN is local, try to get the second local -- sign PIN */
idx++;
rv = sc_pkcs15_find_pin_by_flags(vs->p15card, SC_PKCS15_PIN_TYPE_FLAGS_PIN_LOCAL,
SC_PKCS15_PIN_TYPE_FLAGS_MASK, &idx, ret_obj);
}
}
}
else if (role == ROLE_ADMIN) {
/* Get SO PIN; if no, get the 'global' PUK; if no get the 'local' one */
rv = sc_pkcs15_find_so_pin(vs->p15card, ret_obj);
if (rv)
rv = sc_pkcs15_find_pin_by_flags(vs->p15card, SC_PKCS15_PIN_TYPE_FLAGS_PUK_GLOBAL,
SC_PKCS15_PIN_TYPE_FLAGS_MASK, NULL, ret_obj);
if (rv)
rv = sc_pkcs15_find_pin_by_flags(vs->p15card, SC_PKCS15_PIN_TYPE_FLAGS_PUK_LOCAL,
SC_PKCS15_PIN_TYPE_FLAGS_MASK, NULL, ret_obj);
}
else {
logprintf(pCardData, 2,
"cannot get PIN object: unsupported role %u\n",
(unsigned int)role);
MD_FUNC_RETURN(pCardData, 1, SCARD_E_UNSUPPORTED_FEATURE);
}
out:
if (rv)
MD_FUNC_RETURN(pCardData, 1, SCARD_E_UNSUPPORTED_FEATURE);
if (*ret_obj)
logprintf(pCardData, 7, "Returning PIN '%.*s' for role %u\n",
(int) sizeof (*ret_obj)->label, (*ret_obj)->label,
(unsigned int)role);
MD_FUNC_RETURN(pCardData, 1, SCARD_S_SUCCESS);
}
static const char *
md_get_config_str(PCARD_DATA pCardData, enum ui_str id)
{
VENDOR_SPECIFIC *vs;
const char *ret = NULL;
if (!pCardData)
return ret;
vs = (VENDOR_SPECIFIC*) pCardData->pvVendorSpecific;
if (vs->ctx && vs->reader) {
struct sc_atr atr;
atr.len = pCardData->cbAtr;
memcpy(atr.value, pCardData->pbAtr, atr.len);
ret = ui_get_str(vs->ctx, &atr, vs->p15card, id);
}
return ret;
}
static HICON
md_get_config_icon(PCARD_DATA pCardData, char *flag_name, HICON ret_default)
{
VENDOR_SPECIFIC *vs;
HICON ret = ret_default;
if (!pCardData)
return ret;
logprintf(pCardData, 2, "Get '%s' option\n", flag_name);
vs = (VENDOR_SPECIFIC*) pCardData->pvVendorSpecific;
if (vs->ctx && vs->reader) {
struct sc_atr atr;
scconf_block *atrblock;
atr.len = pCardData->cbAtr;
memcpy(atr.value, pCardData->pbAtr, atr.len);
atrblock = _sc_match_atr_block(vs->ctx, NULL, &atr);
logprintf(pCardData, 2, "Match ATR:\n");
loghex(pCardData, 3, atr.value, atr.len);
if (atrblock) {
const char *filename = scconf_get_str(atrblock, flag_name, NULL);
if (filename) {
ret = (HICON) LoadImage(g_inst, filename, IMAGE_ICON, 0, 0,
LR_LOADFROMFILE|LR_DEFAULTSIZE|LR_SHARED);
}
if (!ret)
ret = ret_default;
}
}
return ret;
}
static HICON
md_get_pinpad_dlg_icon(PCARD_DATA pCardData)
{
return md_get_config_icon(pCardData, "md_pinpad_dlg_icon", NULL);
}
static int
md_get_config_int(PCARD_DATA pCardData, char *flag_name, int ret_default)
{
VENDOR_SPECIFIC *vs;
int ret = ret_default;
if (!pCardData)
return ret;
logprintf(pCardData, 2, "Get '%s' option\n", flag_name);
vs = (VENDOR_SPECIFIC*) pCardData->pvVendorSpecific;
if (vs->ctx && vs->reader) {
struct sc_atr atr;
scconf_block *atrblock;
atr.len = pCardData->cbAtr;
memcpy(atr.value, pCardData->pbAtr, atr.len);
atrblock = _sc_match_atr_block(vs->ctx, NULL, &atr);
logprintf(pCardData, 2, "Match ATR:\n");
loghex(pCardData, 3, atr.value, atr.len);
if (atrblock)
ret = scconf_get_int(atrblock, flag_name, ret_default);
}
return ret;
}
static int
md_get_pinpad_dlg_timeout(PCARD_DATA pCardData)
{
return md_get_config_int(pCardData, "md_pinpad_dlg_timeout", 30);
}
static BOOL
md_get_config_bool(PCARD_DATA pCardData, char *flag_name, BOOL ret_default)
{
VENDOR_SPECIFIC *vs;
BOOL ret = ret_default;
if (!pCardData)
return ret;
vs = (VENDOR_SPECIFIC*) pCardData->pvVendorSpecific;
if (!vs)
return ret;
if (vs->ctx && vs->reader) {
struct sc_atr atr;
scconf_block *atrblock;
atr.len = pCardData->cbAtr;
memcpy(atr.value, pCardData->pbAtr, atr.len);
atrblock = _sc_match_atr_block(vs->ctx, NULL, &atr);
logprintf(pCardData, 2, "Match ATR:\n");
loghex(pCardData, 3, atr.value, atr.len);
if (atrblock)
ret = scconf_get_bool(atrblock, flag_name, ret_default) ? TRUE : FALSE;
}
logprintf(pCardData, 2, "flag_name:%s:%s\n", flag_name, ret ? "TRUE": "FALSE");
return ret;
}
/* 'cancellation' mode can be enabled from the OpenSC configuration file*/
static BOOL
md_is_pinpad_dlg_enable_cancel(PCARD_DATA pCardData)
{
VENDOR_SPECIFIC *vs;
logprintf(pCardData, 2, "Is cancelling the PIN pad dialog enabled?\n");
vs = (VENDOR_SPECIFIC*) pCardData->pvVendorSpecific;
if (vs && vs->ctx && vs->ctx->exe_path) {
DWORD enable_cancel;
size_t sz = sizeof enable_cancel;
if (SC_SUCCESS == sc_ctx_win32_get_config_value(NULL, vs->ctx->exe_path,
SUBKEY_ENABLE_CANCEL,
(char *)(&enable_cancel), &sz)) {
switch (enable_cancel) {
case 0:
return FALSE;
case 1:
return TRUE;
}
}
}
return md_get_config_bool(pCardData, "md_pinpad_dlg_enable_cancel", FALSE);
}
/* 'Write' mode can be enabled from the OpenSC configuration file*/
static BOOL
md_is_read_only(PCARD_DATA pCardData)
{
BOOL ret = TRUE;
logprintf(pCardData, 2, "Is read-only?\n");
if (pCardData && pCardData->pvVendorSpecific) {
VENDOR_SPECIFIC *vs = (VENDOR_SPECIFIC*) pCardData->pvVendorSpecific;
if (vs->p15card && vs->p15card->tokeninfo) {
if (vs->p15card->tokeninfo->flags & SC_PKCS15_TOKEN_READONLY) {
ret = TRUE;
} else {
ret = FALSE;
}
}
}
return md_get_config_bool(pCardData, "read_only", ret);
}
/* 'Write' mode can be enabled from the OpenSC configuration file*/
static BOOL
md_is_supports_X509_enrollment(PCARD_DATA pCardData)
{
BOOL defaultvalue = !md_is_read_only(pCardData);
logprintf(pCardData, 2, "Is supports X509 enrollment?\n");
return md_get_config_bool(pCardData, "md_supports_X509_enrollment", defaultvalue);
}
/* Get know if the GUID has to used as ID of crypto objects */
static BOOL
md_is_guid_as_id(PCARD_DATA pCardData)
{
logprintf(pCardData, 2, "Is GUID has to be used as ID of crypto objects?\n");
return md_get_config_bool(pCardData, "md_guid_as_id", FALSE);
}
/* Get know if the GUID has to used as label of crypto objects */
static BOOL
md_is_guid_as_label(PCARD_DATA pCardData)
{
logprintf(pCardData, 2, "Is GUID has to be used as label of crypto objects?\n");
return md_get_config_bool(pCardData, "md_guid_as_label", FALSE);
}
/* Get know if disabled CARD_CREATE_CONTAINER_KEY_GEN mechanism */
static BOOL
md_is_supports_container_key_gen(PCARD_DATA pCardData)
{
logprintf(pCardData, 2, "Is supports 'key generation' create_container mechanism?\n");
return md_get_config_bool(pCardData, "md_supports_container_key_gen", TRUE);
}
/* Get know if disabled CARD_CREATE_CONTAINER_KEY_IMPORT mechanism */
static BOOL
md_is_supports_container_key_import(PCARD_DATA pCardData)
{
logprintf(pCardData, 2, "Is supports 'key import' create container mechanism?\n");
return md_get_config_bool(pCardData, "md_supports_container_key_import", TRUE);
}
/* generate unique key label (GUID)*/
static VOID md_generate_guid( __in_ecount(MAX_CONTAINER_NAME_LEN+1) PSTR szGuid) {
RPC_CSTR szRPCGuid = NULL;
GUID Label = {0};
UuidCreate(&Label);
if (UuidToStringA(&Label, &szRPCGuid) == RPC_S_OK && szRPCGuid) {
strlcpy(szGuid, (PSTR)szRPCGuid, MAX_CONTAINER_NAME_LEN + 1);
RpcStringFreeA(&szRPCGuid);
} else
szGuid[0] = 0;
}
static DWORD
md_contguid_get_guid_from_card(PCARD_DATA pCardData, struct sc_pkcs15_object *prkey, __in_ecount(MAX_CONTAINER_NAME_LEN+1) PSTR szGuid)
{
int rv;
VENDOR_SPECIFIC *vs;
size_t guid_len = MAX_CONTAINER_NAME_LEN+1;
vs = (VENDOR_SPECIFIC*) pCardData->pvVendorSpecific;
if (!vs)
return SCARD_E_INVALID_PARAMETER;
rv = sc_pkcs15_get_object_guid(vs->p15card, prkey, 1, (unsigned char*) szGuid, &guid_len);
if (rv) {
logprintf(pCardData, 2, "md_contguid_get_guid_from_card(): error %d\n", rv);
return SCARD_F_INTERNAL_ERROR;
}
return SCARD_S_SUCCESS;
}
/* add a new entry in the guid conversion table */
static DWORD
md_contguid_add_conversion(PCARD_DATA pCardData, struct sc_pkcs15_object *prkey,
__in_ecount(MAX_CONTAINER_NAME_LEN+1) PSTR szWindowsGuid)
{
DWORD ret;
int i;
CHAR szOpenSCGuid[MAX_CONTAINER_NAME_LEN+1] = "";
ret = md_contguid_get_guid_from_card(pCardData, prkey, szOpenSCGuid);
if (ret != SCARD_S_SUCCESS)
return ret;
if (strcmp(szOpenSCGuid, szWindowsGuid) == 0)
return ret;
for (i = 0; i < MD_MAX_CONVERSIONS; i++) {
if (md_static_conversions[i].szWindowsGuid[0] == 0) {
strlcpy(md_static_conversions[i].szWindowsGuid,
szWindowsGuid, MAX_CONTAINER_NAME_LEN + 1);
strlcpy(md_static_conversions[i].szOpenSCGuid,
szOpenSCGuid, MAX_CONTAINER_NAME_LEN + 1);
logprintf(pCardData, 0, "md_contguid_add_conversion(): Registering conversion '%s' '%s'\n", szWindowsGuid, szOpenSCGuid);
return SCARD_S_SUCCESS;;
}
}
logprintf(pCardData, 0, "md_contguid_add_conversion(): Unable to add a new conversion with guid %s.\n", szWindowsGuid);
return SCARD_F_INTERNAL_ERROR;;
}
/* remove an entry in the guid conversion table*/
static VOID
md_contguid_delete_conversion(PCARD_DATA pCardData, __in_ecount(MAX_CONTAINER_NAME_LEN+1) PSTR szWindowsGuid)
{
int i;
for (i = 0; i < MD_MAX_CONVERSIONS; i++) {
if (strcmp(md_static_conversions[i].szWindowsGuid,szWindowsGuid) == 0) {
memset(md_static_conversions + i, 0, sizeof(struct md_guid_conversion));
}
}
}
/* build key args from the minidriver guid */
static VOID
md_contguid_build_key_args_from_cont_guid(PCARD_DATA pCardData, __in_ecount(MAX_CONTAINER_NAME_LEN+1) PSTR szGuid,
struct sc_pkcs15init_prkeyargs *prkey_args)
{
/* strlen(szGuid) <= MAX_CONTAINER_NAME */
logprintf(pCardData, 3, "Using the guid '%s'\n", szGuid);
if (szGuid[0] != 0) {
prkey_args->guid = (unsigned char*) szGuid;
prkey_args->guid_len = strlen(szGuid);
}
if (md_is_guid_as_id(pCardData)) {
memcpy(prkey_args->id.value, szGuid, strlen(szGuid));
prkey_args->id.len = strlen(szGuid);
}
if (md_is_guid_as_label(pCardData)) {
prkey_args->label = szGuid;
}
}
/* build minidriver guid from the key */
static DWORD
md_contguid_build_cont_guid_from_key(PCARD_DATA pCardData, struct sc_pkcs15_object *key_obj, __in_ecount(MAX_CONTAINER_NAME_LEN+1) PSTR szGuid)
{
struct sc_pkcs15_prkey_info *prkey_info = (struct sc_pkcs15_prkey_info *)key_obj->data;
DWORD dwret = SCARD_S_SUCCESS;
szGuid[0] = '\0';
/* prioritize the use of the key id over the key label as a container name */
if (md_is_guid_as_id(pCardData) && prkey_info->id.len > 0 && prkey_info->id.len <= MAX_CONTAINER_NAME_LEN) {
memcpy(szGuid, prkey_info->id.value, prkey_info->id.len);
szGuid[prkey_info->id.len] = 0;
} else if (md_is_guid_as_label(pCardData) && key_obj->label[0] != 0) {
strlcpy(szGuid, key_obj->label, MAX_CONTAINER_NAME_LEN + 1);
} else {
dwret = md_contguid_get_guid_from_card(pCardData, key_obj, szGuid);
}
return dwret;
}
static DWORD
md_cont_flags_from_key(PCARD_DATA pCardData, struct sc_pkcs15_object *key_obj, unsigned char *cont_flags)
{
struct sc_pkcs15_prkey_info *prkey_info = NULL;
VENDOR_SPECIFIC *vs;
int rv;
vs = (VENDOR_SPECIFIC*) pCardData->pvVendorSpecific;
if (!vs)
return SCARD_E_INVALID_PARAMETER;
prkey_info = (struct sc_pkcs15_prkey_info *)key_obj->data;
*cont_flags = CONTAINER_MAP_VALID_CONTAINER;
if (prkey_info->aux_data) {
rv = sc_aux_data_get_md_flags(vs->ctx, prkey_info->aux_data, cont_flags);
if (rv != SC_ERROR_NOT_SUPPORTED && rv != SC_SUCCESS)
return SCARD_F_INTERNAL_ERROR;
}
return SCARD_S_SUCCESS;
}
/* Search directory by name and optionally by name of it's parent */
static DWORD
md_fs_find_directory(PCARD_DATA pCardData, struct md_directory *parent, char *name, struct md_directory **out)
{
VENDOR_SPECIFIC *vs;
struct md_directory *dir = NULL;
if (out)
*out = NULL;
if (!pCardData)
return SCARD_E_INVALID_PARAMETER;
vs = pCardData->pvVendorSpecific;
if (!vs)
return SCARD_E_INVALID_PARAMETER;