/
card-belpic.c
1522 lines (1340 loc) · 48.3 KB
/
card-belpic.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
/*
* card-belpic.c: Support for Belgium EID card
*
* Copyright (C) 2003, Zetes Belgium
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* About the Belpic (Belgian Personal Identity Card) card
*
* The Belpic card is a Cyberflex Java card, so you normaly communicate
* with an applet running on the card. In order to support a pkcs15 file
* structure, an applet (the Belpic applet) has been build that emulates
* this. So the card's behaviour is specific for this Belpic applet, that's
* why a separate driver has been made.
*
* The card contains the citizen's ID data (name, address, photo, ...) and
* her keys and certs. The ID data are in a seperate directory on the card and
* are not handled by this software. For the cryptographic data (keys and certs)
* a pkcs#15 structure has been chosen and they can be accessed and used
* by the OpenSC software.
*
* The current situation about the cryptographic data is: there is 1 PIN
* that protects 2 private keys and corresponding certs. Then there is a
* CA cert and the root cert. The first key (Auth Key) can be used for
* authentication, the second one (NonRep Key) for non repudation purposes
* (so it can be used as an alternative to manual signatures).
*
* There are some special things to note, which all have some consequences:
* (1) the SELECT FILE command doesn't return any FCI (file length, type, ...)
* (2) the NonRep key needs a VERIFY PIN before a signature can be done with it
* (3) pin pad readers had to be supported by a proprietory interface (as at
* that moment no other solution was known/avaiable/ready)
* The consequences are:
*
* For (1): we let the SELECT FILE command return that the file length is
* a fixed large number and that each file is a transparant working EF
* (except the root dir 3F 00). This way however, there is a problem with the
* sc_read_binary() function that will only stop reading untill it receivces
* a 0. Therefore, we use the 'next_idx' trick. Or, if that might fail
* and so a READ BINARY past the end of file is done, length 0 is returned
* instead of an error code.
*
* For (2), we decided that a GUI for asking the PIN would be the best
* thing to do (another option would be to make 2 virtual slots but that
* causes other problems and is less user-friendly). A GUI being popped up
* by the pkcs11 lib before each NonRep signature has another important
* security advantage: applications that cache the PIN can't silently do
* a NonRep signature because there will allways be the GUI.
*
* For (3), we link dynamically against a pin pad lib (DLL) that implements the
* proprietory API for a specific pin pad. For each pin pad reader (identified
* by it's PC/SC reader name), a pin pad lib correspondends. Some reader/lib
* name pairs are hardcoded, and others can be added in the config file.
* Note that there's also a GUI used in this case: if a signature with the
* NonRep key is done: a dialog box is shown that asks the user to enter
* her PIN on the pin pad reader in order to make a legally valid signature.
*
* Further the (current) Belpic card as quite some limitations:
* no key pair generation or update of data except after establishing a Secure
* Channel or CTV-authentication (which can only be done at the municipalities),
* no encryption. The result is that only a very limited amount of functions
* is/had to be implemented to get the pkcs11 library working.
*
* About the belpic_set_language: the RA-PC software (including the pkcs11 lib)
* in the Brussels' communities should be able to change the language of the GUI
* messages. So the language set by this function takes priority on all other
* language-selection functionality.
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include "internal.h"
#include "log.h"
#ifdef BELPIC_PIN_PAD
#ifndef HAVE_GUI
#define HAVE_GUI
#endif
#endif
#ifdef BELPIC_PIN_PAD
#include "winscard.h"
#include "scr.h"
#endif
/* These defines are disabled for OpenSC */
#if 0
#define GET_LANG_FROM_CARD
#define HAVE_ALLOW_SSO
#endif
#ifdef HAVE_GUI
#include "scgui.h"
#ifndef SCR_USAGE_SIGN
#define SCR_USAGE_SIGN 2 /* in scr.h */
#endif
#ifndef SCR_USAGE_AUTH
#define SCR_USAGE_AUTH 1
#endif
#endif
/* To be removed */
#include <time.h>
static long t1, t2, tot_read = 0, tot_dur = 0, dur;
#define BELPIC_VERSION "1.4"
/* Most of the #defines here are also present in the pkcs15 files, but
* because this driver has no access to them, it's hardcoded here. If
* other Belpic cards with other 'settings' appear, we'll have to move
* these #defines to the struct belpic_priv_data */
#define BELPIC_MAX_FILE_SIZE 65535
#define BELPIC_PIN_BUF_SIZE 8
#define BELPIC_MIN_USER_PIN_LEN 4
#define BELPIC_MAX_USER_PIN_LEN 12
#define BELPIC_PIN_ENCODING SC_PIN_ENCODING_GLP
#define BELPIC_PAD_CHAR 0xFF
#define BELPIC_KEY_REF_NONREP 0x83
/* Used for a trick in select file and read binary */
static size_t next_idx = (size_t)-1;
static struct sc_atr_table belpic_atrs[] = {
/* Applet V1.1 */
{ "3B:98:13:40:0A:A5:03:01:01:01:AD:13:11", NULL, NULL, SC_CARD_TYPE_BELPIC_EID, 0, NULL },
/* Applet V1.0 with new EMV-compatible ATR */
{ "3B:98:94:40:0A:A5:03:01:01:01:AD:13:10", NULL, NULL, SC_CARD_TYPE_BELPIC_EID, 0, NULL },
/* Applet beta 5 + V1.0 */
{ "3B:98:94:40:FF:A5:03:01:01:01:AD:13:10", NULL, NULL, SC_CARD_TYPE_BELPIC_EID, 0, NULL },
#if 0
/* Applet beta 3 + 4 */
{ "3B:98:11:40:FF:A5:03:01:01:01:AD:13:04", NULL, NULL, SC_CARD_TYPE_BELPIC_EID, 0, NULL },
/* Applet beta 2 */
{ "3B:68:00:00:29:05:01:02:01:AD:13:03", NULL, NULL, SC_CARD_TYPE_BELPIC_EID, 0, NULL },
#endif
{ NULL, NULL, NULL, 0, 0, NULL }
};
struct belpic_priv_data {
int lang;
int options;
#ifdef BELPIC_PIN_PAD
FARPROC scr_init;
FARPROC scr_verify_pin;
FARPROC scr_change_pin;
char szPinPadDll[64];
#endif
};
#define DRVDATA(card) ((struct belpic_priv_data *) ((card)->drv_data))
/* Single Sign On */
#ifdef HAVE_ALLOW_SSO
#define SSO_OK(drv) ((drv)->allow_sso)
#else
#define SSO_OK(drv) 0
#endif
static struct sc_card_operations belpic_ops;
static struct sc_card_driver belpic_drv = {
"Belpic cards",
"belpic",
&belpic_ops,
NULL, 0, NULL
};
static const struct sc_card_operations *iso_ops = NULL;
#define LNG_ENG 0
#define LNG_DUTCH 1
#define LNG_FRENCH 2
#define LNG_GERMAN 3
#define LNG_NONE 0xFFFF
#ifdef BELPIC_PIN_PAD
/* Option flags from the config file */
#define PP_MSG_AUTH_PIN 0x00000001
#define PP_MSG_WRONG_PIN 0x00000002
#define PP_MSG_CHANGEPIN_MISMATCH 0x00000004
#define PP_MSG_PIN_BLOCKED 0x00000008
/* Hardcoded pin pad reader names (PC/SC) and their pin pad lib */
static char *pp_reader_names[] = {
"Xiring X Pass Serial",
NULL
};
static char *pp_reader_libs[] = {
"xireid",
NULL
};
static BYTE aid_belpic[] = { 0xA0, 0x00, 0x00, 0x01, 0x77, 0x50, 0x4B, 0x43, 0x53, 0x2D, 0x31, 0x35 };
static SCR_Application scr_app_belpic = {
{aid_belpic, sizeof(aid_belpic)},
"ID",
NULL
};
static char *app_id_longstr[] = {
"Identity",
"Identiteit",
"Identité",
"Identität"
};
#endif /* BELPIC_PIN_PAD */
#if defined(HAVE_GUI) ||defined(BELPIC_PIN_PAD)
static char *pin_usg_sig[] = {
"Signature",
"Handtekening",
"Signature",
"Signatur"
};
static char *pin_usg_auth[] = {
"Authentication",
"Authentificatie",
"Authentification",
"Authentifizierung"
};
#endif /* defined(HAVE_GUI) ||defined(BELPIC_PIN_PAD) */
#ifdef BELPIC_PIN_PAD
static char *lang_codes[4] = {
"en",
"nl",
"fr",
"de"
};
static char *pp_msg_auth_sh[] = {
"Authentication",
"Authentificatie",
"Authentification",
"Kennzeichnung"
};
static char *pp_msg_auth[] = {
"Enter your PIN on the reader, in order to authenticate yourself",
"Geef uw PIN in op de lezer, om u te authentificeren",
"Entrez votre PIN sur le lecteur, pour vous authentifier",
"Bitte geben Sie Ihre PIN am Kartenlesegerät ein, um sich zu authentifizieren"
};
static char *pp_msg_sign_sh[] = {
"Signature",
"Handtekening",
"Signature",
"Signatur"
};
static char *pp_msg_sign[] = {
"Caution: You are about to make a legally binding electronic signature with your identity card.\nPlease enter your PIN on the card reader to continue or click the Cancel button.\n\nIf you only want to log on to a web site or server, do NOT enter your PIN and click the Cancel button.",
"Let op: u gaat een wettelijk bindende electronische handtekening plaatsen met uw identiteitskaart.\nGeef uw PIN in op de kaartlezer om verder te gaan of klik op Stoppen.\n\nAls u enkel wil aanloggen op een web site of een server, geef uw PIN NIET in en klik op Stoppen.",
"Attention: vous allez apposer une signature électronique juridiquement valide avec votre carte d'identité.\nVeuillez entrer votre PIN sur le lecteur externe pour continuer ou cliquez sur Annuler.\n\nSi vous désirez seulement vous connecter à un site ou un serveur, n'entrez PAS votre PIN et cliquez sur Annuler.",
"Achtung: Mit Ihrem Personalausweis werden Sie eine rechtlich bindende elektronische Signatur setzen.\nBitte geben Sie Ihre PIN am Kartenlesgerät ein zum weitergehen oder klicken Sie auf Abbrechen.\n\nWenn Sie nur auf das Internet gehen möchten, geben Sie bitte Ihre PIN NICHT ein, sondern klicken Sie auf Abbrechen."
};
static char *pp_msg_change_sh[] = {
"PIN change",
"PIN verandering",
"Modification du PIN ",
"PIN ändern"
};
static char *pp_msg_change[] = {
"Change your PIN on the reader",
"Verander uw PIN op de lezer",
"Modifiez votre PIN sur le lecteur",
"Bitte ändern Sie Ihre PIN am Kartenlesegerät"
};
static char *pp_msg_pin_mismatch[] = {
"The new PINs you entered were different.\n\nRetry or cancel?",
"De ingevoerde nieuwe PINs zijn verschillend.\n\nOpnieuw proberen of stoppen?",
"Les nouveaux PIN entrés sont différents.\n\nRéessayer ou annuler?",
"Die von Ihnen eingegebenen PINs unterscheiden sich.\n\nErneut versuchen oder abbrechen?"
};
#define PCSC_ERROR(ctx, desc, rv) sc_debug(ctx, SC_LOG_DEBUG_NORMAL, desc ": %lx\n", rv);
#endif /* BELPIC_PIN_PAD */
/* Language support for the GUI messages */
#ifdef HAVE_GUI
#ifdef WIN32
#define BTN_KEYB_SHORTCUT "&"
#else
#define BTN_KEYB_SHORTCUT "_"
#endif
static char *app_msg[] = {
"Identity",
"Identiteit",
#ifdef _WIN32
"Identité",
#else
"Identite",
#endif
#ifdef _WIN32
"Identität"
#else
"Identitat",
#endif
};
static char *btn_msg_retry[4] = {
BTN_KEYB_SHORTCUT"Try again",
BTN_KEYB_SHORTCUT"Opnieuw proberen",
BTN_KEYB_SHORTCUT"Réessayer",
BTN_KEYB_SHORTCUT"Erneut versuchen"
};
static char *btn_msg_cancel[4] = {
BTN_KEYB_SHORTCUT"Cancel",
BTN_KEYB_SHORTCUT"Stoppen",
BTN_KEYB_SHORTCUT"Annuler",
BTN_KEYB_SHORTCUT"Abbrechen"
};
static char *btn_msg_ok[4] = {
BTN_KEYB_SHORTCUT"OK",
BTN_KEYB_SHORTCUT"OK",
BTN_KEYB_SHORTCUT"OK",
BTN_KEYB_SHORTCUT"OK"
};
static char *btn_msg_close[4] = {
BTN_KEYB_SHORTCUT"Close",
BTN_KEYB_SHORTCUT"Sluiten",
BTN_KEYB_SHORTCUT"Fermer",
BTN_KEYB_SHORTCUT"Schliessen"
};
static char *enter_pin_msg_auth[] = {
"Enter your PIN, in order to authenticate yourself",
"Geef uw PIN in, om u te authentificeren",
"Entrez votre PIN, pour vous authentifier",
"Bitte geben Sie Ihre PIN ein, um sich zu authentifizieren"
};
static char *enter_pin_msg_sign[4] = {
#ifdef _WIN32
"Caution: You are about to make a legally binding electronic signature with your identity card.\nPlease enter your PIN to continue or click the Cancel button.\n\nWarning: if you only want to log on to a web site or server, do NOT enter your PIN and click the Cancel button.",
"Let op: u gaat een wettelijk bindende electronische handtekening plaatsen met uw identiteitskaart.\nGeef uw PIN in om verder te gaan of klik op Stoppen.\n\nWaarschuwing: als u enkel wil aanloggen op een web site of een server, geef uw PIN NIET in en klik op Stoppen.",
"Attention: vous allez apposer une signature électronique juridiquement valide avec votre carte d'identité.\nVeuillez entrer votre PIN pour continuer ou cliquez sur Annuler.\n\nPrécaution: si vous désirez seulement vous connecter à un site ou un serveur, n'entrez PAS votre PIN et cliquez sur Annuler.",
"Achtung: Mit Ihrem Personalausweis werden Sie eine rechtlich bindende elektronische Signatur setzen.\nBitte geben Sie Ihre PIN ein zum weitergehen oder klicken Sie auf Abbrechen.\n\nWarnung: Wenn Sie nur auf das Internet gehen möchten, geben Sie bitte Ihre PIN NICHT ein, sondern klicken Sie auf Abbrechen."
#else
#ifdef __APPLE__
"CAUTION: you are about to make a legally binding electronic signature with your identity card. Please enter your PIN to continue or press the Cancel button. If you only want to log on to a web site or a server, do NOT enter your PIN and press the Cancel button.",
"LET OP: u gaat een wettelijk bindende electronische handtekening plaatsen met uw identiteitskaart. Geef uw PIN in om verder te gaan of klik op Stoppen. Als u enkel wil aanloggen op een web site of een server, geef uw PIN NIET in en klik op Stoppen.",
"ATTENTION: vous allez apposer une signature electronique\njuridiquement valide avec votre carte d'identite.Veuillez entrer votre PIN pour continuer ou cliquez sur Annuler. Si vous desirez seulement vous connecter a un site ou un serveur, n' entrez PAS votre PIN et cliquez sur Annuler.",
"ACHTUNG: Mit Ihrem Personalausweis werden Sie eine rechtlich bindende elektronische Signatur setzen. Geben Sie Ihre PIN ein zum weitergehen oder klicken Sie auf Abbrechen. Warnung: Wenn Sie nur auf das Internet gehen mochten, geben Sie bitte Ihre PIN NICHT ein, sondern klicken Sie auf Abbrechen."
#else
"<u>Caution</u>: you are about to make a legally binding electronic\nsignature with your identity card.\nPlease enter your PIN to continue or press the Cancel button.\n\nIf you only want to log on to a web site or a server,\ndo <b>NOT</b> enter your PIN and press the Cancel button.",
"<u>Let op</u>: u gaat een wettelijk bindende electronische handtekening\nplaatsen met uw identiteitskaart.\nGeef uw PIN in om verder te gaan of klik op Stoppen.\n\nAls u enkel wil aanloggen op een web site\nof een server, geef uw PIN <b>NIET</b> in en klik op Stoppen.",
"<u>Attention</u>: vous allez apposer une signature electronique\njuridiquement valide avec votre carte d'identite.\nVeuillez entrer votre PIN pour continuer ou cliquez sur Annuler.\n\nSi vous desirez seulement vous connecter a un site\nou un serveur, n'entrez <b>PAS</b> votre PIN et cliquez sur Annuler.",
"<u>Achtung</u>: Mit Ihrem Personalausweis werden Sie eine rechtlich\r\nbindende elektronische Signatur setzen.\r\nGeben Sie Ihre PIN ein zum weitergehen oder klicken Sie auf Abbrechen.\r\n\r\nWarnung: Wenn Sie nur auf das Internet gehen mochten, geben\r\nSie bitte Ihre PIN <b>NICHT</b> ein, sondern klicken Sie auf Abbrechen."
#endif
#endif
};
static char *wrong_pin_len_msgs[4] = {
"Wrong PIN length",
"Foute PIN lengte",
"Longueur de PIN erroné",
"Falsche PIN-Länge"
};
static char *wrong_pin_msgs[4] = {
"Wrong PIN, %d tries left\n\nRetry or cancel?",
"Foute PIN, nog %d pogingen\n\nOpnieuw proberen of stoppen?",
"PIN erroné, %d essais restants\n\nRéessayer ou annuler?",
"Falsche PIN, %d verbleibende Versuche\n\nErneut versuchen oder abbrechen?"
};
static char *pin_blocked_msgs[4] = {
"PIN blocked",
"PIN geblokkeerd",
"PIN bloqué ",
"PIN gesperrt"
};
#endif /* HAVE_GUI */
/* To be removed */
#if 0
static void dumphex(char *msg, const u8 * buf, int len)
{
int i;
printf("%s", msg);
for (i = 0; i < len; i++)
printf("%02X ", buf[i]);
printf(" (%d bytes)\n", len);
}
#endif
#ifdef BELPIC_PIN_PAD
#define SCR_INIT_ID 100
#define SCR_VERIFY_ID 101
#define SCR_CHANGE_ID 102
#define SCR_CARD_HANDLE 999
struct tTLV {
unsigned char *base;
unsigned char *end;
unsigned char *current;
unsigned char *next;
};
static void TLVInit(struct tTLV *tlv, u8 * base, size_t size)
{
tlv->base = base;
tlv->end = base + size;
tlv->current = tlv->next = base;
}
static void TLVNext(struct tTLV *tlv, u8 tag)
{
assert(tlv->next + 2 < tlv->end);
tlv->current = tlv->next;
*(tlv->next++) = tag;
*(tlv->next++) = 0;
}
static void TLVAdd(struct tTLV *tlv, u8 val)
{
assert(tlv->next + 1 < tlv->end);
*(tlv->next++) = val;
tlv->current[1]++;
}
static void TLVAddBuffer(struct tTLV *tlv, u8 * val, size_t size)
{
assert(tlv->next + size < tlv->end);
memcpy(tlv->next, val, size);
tlv->current[1] = size;
tlv->next = tlv->next + size;
}
static size_t TLVLen(struct tTLV *tlv)
{
return tlv->next - tlv->base;
}
static LONG SCR_SCardInit(LPCTSTR szPinPadDll, LPCTSTR szReader, DWORD version,
SCR_SupportConstants * supported)
{
LONG rv;
unsigned char sendbuf[256];
unsigned char recvbuf[2];
char szTemp[32];
DWORD dwRecvLength;
struct tTLV tlv;
memset(szTemp, 0, sizeof(szTemp));
memset(sendbuf, 0, sizeof(sendbuf));
memset(recvbuf, 0, sizeof(recvbuf));
dwRecvLength = sizeof(recvbuf);
/* Make TLV buffer */
TLVInit(&tlv, sendbuf, sizeof(sendbuf));
TLVNext(&tlv, 0x01); /* Function ID */
sprintf(szTemp, "%ld", SCR_INIT_ID);
TLVAddBuffer(&tlv, (u8 *) szTemp, strlen(szTemp));
TLVNext(&tlv, 0x02); /* PinPad Dll */
TLVAddBuffer(&tlv, (u8 *) szPinPadDll, strlen(szPinPadDll));
TLVNext(&tlv, 0x03); /* Reader Name */
TLVAddBuffer(&tlv, (u8 *) szReader, strlen(szReader));
TLVNext(&tlv, 0x04); /* Version */
sprintf(szTemp, "%ld", version);
TLVAddBuffer(&tlv, (u8 *) szTemp, strlen(szTemp));
#ifdef HAVE_PCSC_OLD
rv = SCardControl(SCR_CARD_HANDLE, sendbuf, TLVLen(&tlv), recvbuf, &dwRecvLength);
#else
rv = SCardControl(SCR_CARD_HANDLE, 0, sendbuf, TLVLen(&tlv),
recvbuf, dwRecvLength, &dwRecvLength);
#endif
if (dwRecvLength > 0) {
*supported = recvbuf[0];
} else {
rv = SC_ERROR_UNKNOWN_DATA_RECEIVED;
}
return rv;
}
static LONG SCR_SCardPIN(long lAction, LPCTSTR szPinPadDll, const SCR_Card * pCard, BYTE pinID,
const SCR_PinUsage * pUsage, const SCR_Application * pApp,
BYTE * pCardStatus)
{
LONG rv;
unsigned char sendbuf[256];
unsigned char recvbuf[2];
char szTemp[32];
DWORD dwRecvLength;
struct tTLV tlv;
memset(szTemp, 0, sizeof(szTemp));
memset(recvbuf, 0, sizeof(recvbuf));
dwRecvLength = sizeof(recvbuf);
/* Make TLV buffer */
TLVInit(&tlv, sendbuf, sizeof(sendbuf));
TLVNext(&tlv, 0x01); /* Function ID */
sprintf(szTemp, "%ld", lAction);
TLVAddBuffer(&tlv, (u8 *) szTemp, strlen(szTemp));
TLVNext(&tlv, 0x02); /* PinPad Dll */
TLVAddBuffer(&tlv, (u8 *) szPinPadDll, strlen(szPinPadDll));
TLVNext(&tlv, 0x03); /* SCR_Card Handle */
sprintf(szTemp, "%ld", pCard->hCard);
TLVAddBuffer(&tlv, (u8 *) szTemp, strlen(szTemp));
if (pCard->language != NULL) {
TLVNext(&tlv, 0x04); /* SCR_Card language */
TLVAddBuffer(&tlv, (u8 *) pCard->language, strlen(pCard->language));
}
if (pCard->id.data != NULL) {
TLVNext(&tlv, 0x05); /* SCR_Card id */
TLVAddBuffer(&tlv, pCard->id.data, pCard->id.length);
}
TLVNext(&tlv, 0x06); /* PinID */
TLVAdd(&tlv, pinID);
if (pUsage != NULL) {
TLVNext(&tlv, 0x07); /* SCR_PinUsage code */
sprintf(szTemp, "%ld", pUsage->code);
TLVAddBuffer(&tlv, (u8 *) szTemp, strlen(szTemp));
if (pUsage->shortString != NULL) {
TLVNext(&tlv, 0x08); /* SCR_PinUsage shortstring */
TLVAddBuffer(&tlv, (u8 *) pUsage->shortString, strlen(pUsage->shortString));
}
if (pUsage->longString != NULL) {
TLVNext(&tlv, 0x09); /* SCR_PinUsage longstring */
TLVAddBuffer(&tlv, (u8 *) pUsage->longString, strlen(pUsage->longString));
}
}
if (pApp->id.data != NULL) {
TLVNext(&tlv, 0x0A); /* SCR_Application id */
TLVAddBuffer(&tlv, (u8 *) pApp->id.data, pApp->id.length);
}
if (pApp->shortString != NULL) {
TLVNext(&tlv, 0x0B); /* SCR_Application shortstring */
TLVAddBuffer(&tlv, (u8 *) pApp->shortString, strlen(pApp->shortString));
}
if (pApp->longString != NULL) {
TLVNext(&tlv, 0x0C); /* SCR_Application longstring */
TLVAddBuffer(&tlv, (u8 *) pApp->longString, strlen(pApp->longString));
}
#ifdef HAVE_PCSC_OLD
rv = SCardControl(SCR_CARD_HANDLE, sendbuf, TLVLen(&tlv), recvbuf, &dwRecvLength);
#else
rv = SCardControl(SCR_CARD_HANDLE, 0, sendbuf, TLVLen(&tlv),
recvbuf, dwRecvLength, &dwRecvLength);
#endif
if (dwRecvLength < 2) {
rv = SC_ERROR_UNKNOWN_DATA_RECEIVED;
} else {
memcpy(pCardStatus, recvbuf, 2);
}
return rv;
}
static LONG SCR_SCardVerifyPIN(LPCTSTR szPinPadDll, const SCR_Card * pCard, BYTE pinID,
const SCR_PinUsage * pUsage, const SCR_Application * pApp,
BYTE * pCardStatus)
{
return SCR_SCardPIN(SCR_VERIFY_ID, szPinPadDll, pCard, pinID, pUsage, pApp, pCardStatus);
}
static LONG SCR_SCardChangePIN(LPCTSTR szPinPadDll, const SCR_Card * pCard, BYTE pinID,
const SCR_Application * pApp, BYTE * pCardStatus)
{
return SCR_SCardPIN(SCR_CHANGE_ID, szPinPadDll, pCard, pinID, NULL, pApp, pCardStatus);
}
#endif /* BELPIC_PIN_PAD */
#if defined(HAVE_GUI) ||defined(BELPIC_PIN_PAD)
static int belpic_calculate_lang(sc_card_t *card)
{
struct belpic_priv_data *priv = DRVDATA(card);
int lang = priv->lang;
return lang;
}
#endif /* defined(HAVE_GUI) ||defined(BELPIC_PIN_PAD) */
static int str2lang(sc_context_t *ctx, char *lang)
{
if (memcmp(lang, "en", 2) == 0)
return LNG_ENG;
else if (memcmp(lang, "nl", 2) == 0)
return LNG_DUTCH;
else if (memcmp(lang, "fr", 2) == 0)
return LNG_FRENCH;
else if (memcmp(lang, "de", 2) == 0)
return LNG_GERMAN;
sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "Unknown/unsupported language code: %c%c\n", lang[0], lang[1]);
return -1;
}
#ifdef GET_LANG_FROM_CARD
/* str is in lower case, the case of buf can be both, and buf is large enough */
static int match_string(const char *str, const char *buf)
{
int i = 0;
while (str[i] != '\0') {
if (str[i] != ((buf[i] >= 'A' && buf[i] <= 'Z') ? buf[i] + 32 : buf[i]))
return 0;
i++;
}
return 1; /* match */
}
static int get_pref(const char *prefs, int prefs_len, const char *title, const char *key, int *len)
{
int i = 0;
int title_len = strlen(title);
int key_len = strlen(key);
while (prefs[i] != '\0' && i < prefs_len)
i++;
prefs_len = i;
i = 0;
while (i < prefs_len) {
while (i < prefs_len && prefs[i] != '[')
i++;
if (i + title_len >= prefs_len)
return -1;
if (!match_string(title, prefs + i)) {
i++;
continue;
}
i += title_len;
while (i < prefs_len) {
while (i < prefs_len && (prefs[i] == '\r' || prefs[i] == '\n'))
i++;
if (i < prefs_len && prefs[i] == '[')
break;
if (i + key_len + 1 >= prefs_len)
return -2;
if (!match_string(key, prefs + i)) {
i++;
continue;
}
i += key_len;
if (prefs[i] != '=')
return -3;
*len = ++i;
while (*len < prefs_len && prefs[*len] != '\r' && prefs[*len] != '\n')
(*len)++;
*len -= i;
return i;
}
}
return -1;
}
static int get_language(sc_card_t *card)
{
sc_apdu_t apdu;
u8 prefs[240], *lg_value;
u8 path[] = { 0x3F, 0x00, 0xDF, 0x01, 0x40, 0x39 };
int r, i, len;
/* Get the language from the card's preferences file */
assert(card != NULL);
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xA4, 0x08, 0x0C);
apdu.lc = sizeof(path);
apdu.data = path;
apdu.datalen = sizeof(path);
apdu.resplen = 0;
apdu.le = 0;
r = sc_lock(card);
if (r < 0)
goto prefs_error;
r = sc_transmit_apdu(card, &apdu);
if (r < 0) {
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "Select_File[prefs_file] command failed: %d\n", r);
sc_unlock(card);
goto prefs_error;
}
r = sc_check_sw(card, apdu.sw1, apdu.sw2);
if (r < 0) {
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "Select_File[prefs_file]: card returned %d\n", r);
sc_unlock(card);
goto prefs_error;
}
r = iso_ops->read_binary(card, 0, prefs, sizeof(prefs), 0);
sc_unlock(card);
if (r <= 0) {
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "Read_Binary[prefs_file] returned %d\n", r);
goto prefs_error;
}
#if 0
dumphex("Prefs: ", prefs, r);
#endif
i = get_pref(prefs, r, "[gen]", "lg", &len);
if (i <= 0 || len < 2) {
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "Couldn't find language in prefs file: %d\n", i);
goto prefs_error;
}
lg_value = prefs + i; /* language code(s) found, starts here */
i = 0;
while (1) {
while (i <= len - 2 && (lg_value[i] == ' ' || lg_value[i] == '|'))
i++;
if (i > len - 2)
goto prefs_error;
r = str2lang(card->ctx, lg_value + i);
if (r >= 0)
return r;
i += 2;
}
prefs_error:
/* If troubles with the card's prefs file, get the language from the OS */
#ifdef _WIN32
switch (GetUserDefaultLangID() & 0x00FF) {
case 0x13:
return LNG_DUTCH;
case 0x0C:
return LNG_FRENCH;
case 0x07:
return LNG_GERMAN;
default:
return LNG_ENG;
}
#endif
return LNG_ENG; /* default */
}
#endif /* GET_LANG_FROM_CARD */
static scconf_block *get_belpic_conf(sc_context_t *ctx, const char *name)
{
scconf_block *conf_block = NULL, **blocks;
int i;
for (i = 0; ctx->conf_blocks[i] != NULL; i++) {
blocks = scconf_find_blocks(ctx->conf, ctx->conf_blocks[i], name, NULL);
if (!blocks)
return NULL;
conf_block = blocks[0];
free(blocks);
if (conf_block != NULL)
break;
}
return conf_block;
}
#ifdef BELPIC_PIN_PAD
static void load_pin_pad_err(const char *reader_name, const char *pp_reader_lib, char *msg)
{
char buf[300];
void *hDlg;
if (strlen(reader_name) + strlen(pp_reader_lib) > 200)
return;
sprintf(buf, "Error while loading library \"%s\" for pin pad reader \"%s\": %s\n",
pp_reader_lib, reader_name, msg);
scgui_ask_message(app_msg[0], "Pin pad library error", buf, btn_msg_close[0], NULL,
reader_name);
}
static int belpic_load_pin_pad_lib(sc_card_t *card, struct belpic_priv_data *priv_data,
const char *reader_name, const char *pp_reader_lib)
{
LONG r;
DWORD supported;
memset(priv_data->szPinPadDll, 0, sizeof(priv_data->szPinPadDll));
strcpy(priv_data->szPinPadDll, pp_reader_lib);
priv_data->scr_init = (FARPROC) SCR_SCardInit;
priv_data->scr_verify_pin = (FARPROC) SCR_SCardVerifyPIN;
priv_data->scr_change_pin = (FARPROC) SCR_SCardChangePIN;
if (priv_data->scr_init == NULL || priv_data->scr_verify_pin == NULL) {
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "Function not found in \"%s\" err = 0x%0x\n",
pp_reader_lib, GetLastError());
load_pin_pad_err(reader_name, pp_reader_lib,
"unsufficient functionality found in library");
return SC_ERROR_READER;
}
r = priv_data->scr_init(pp_reader_lib, reader_name, 1, &supported);
if (r != SCARD_S_SUCCESS) {
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "SCR_Init() returned 0x%0x\n", r);
load_pin_pad_err(reader_name, pp_reader_lib, "Initialization of library failed");
return SC_ERROR_READER;
}
if (supported) {
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "SCR_init() returned not supported code 0x%0x\n", supported);
load_pin_pad_err(reader_name, pp_reader_lib,
"Initialization of library returned UNSUPPORTED");
return SC_ERROR_READER;
}
#if 0
HINSTANCE dll = LoadLibrary(pp_reader_lib);
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "Pin pad reader \"%s\" found, now loading corresponding lib \"%s\"\n",
reader_name, pp_reader_lib);
if (dll == NULL) {
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "Unable to load library \"%s\", err = 0x%0x\n",
pp_reader_lib, GetLastError());
load_pin_pad_err(reader_name, pp_reader_lib,
"library not found or unable to load it");
return SC_ERROR_READER;
}
priv_data->scr_init = GetProcAddress(dll, "SCR_Init");
priv_data->scr_verify_pin = GetProcAddress(dll, "SCR_VerifyPIN");
priv_data->scr_change_pin = GetProcAddress(dll, "SCR_ChangePIN");
if (priv_data->scr_init == NULL || priv_data->scr_verify_pin == NULL) {
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "Function not found in \"%s\" err = 0x%0x\n",
pp_reader_lib, GetLastError());
load_pin_pad_err(reader_name, pp_reader_lib,
"unsufficient functionality found in library");
return SC_ERROR_READER;
}
r = priv_data->scr_init(reader_name, 1, &supported);
if (r != SCARD_S_SUCCESS) {
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "SCR_Init() returned 0x%0x\n", r);
load_pin_pad_err(reader_name, pp_reader_lib, "Initialization of library failed");
return SC_ERROR_READER;
}
if (supported) {
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "SCR_init() returned not supported code 0x%0x\n", supported);
load_pin_pad_err(reader_name, pp_reader_lib,
"Initialization of library returned UNSUPPORTED");
return SC_ERROR_READER;
}
#endif
return 1;
}
static int belpic_detect_pin_pad(sc_card_t *card, struct belpic_priv_data *priv_data)
{
int i = 0;
char *reader_name = card->reader->name, *conf_reader, *conf_lib;
scconf_block *conf_block = NULL;
char *reader_i = "reader ", *lib_i = "lib ";
int rn_len = strlen(reader_name);
/* Hardcoded readers */
for (i = 0; pp_reader_names[i] != NULL; i++) {
int pp_rn_len = strlen(pp_reader_names[i]);
if (rn_len >= pp_rn_len && strncmp(reader_name, pp_reader_names[i], pp_rn_len) == 0) {
return belpic_load_pin_pad_lib(card, priv_data,
reader_name, pp_reader_libs[i]);
}
}
/* From the config file */
conf_block = get_belpic_conf(card->ctx, "belpic_pin_pad");
if (conf_block == NULL)
return 0;
for (i = 0; i < 10; i++) {
reader_i[6] = (char) (0x30 + i);
conf_reader = (char *) scconf_get_str(conf_block, reader_i, NULL);
if (conf_reader != NULL && rn_len >= strlen(conf_reader) &&
strncmp(reader_name, conf_reader, strlen(conf_reader)) == 0) {
lib_i[3] = (char) (0x30 + i);
conf_lib = (char *) scconf_get_str(conf_block, lib_i, NULL);
if (conf_lib != NULL)
return belpic_load_pin_pad_lib(card, priv_data,
reader_name, conf_lib);
}
}
return 0;
}
#endif /* BELPIC_PIN_PAD */
static int belpic_finish(sc_card_t *card)
{
free(DRVDATA(card));
return 0;
}
static int belpic_match_card(sc_card_t *card)
{
int i;
i = _sc_match_atr(card, belpic_atrs, &card->type);
if (i < 0)
return 0;
return 1;
}
static int belpic_init(sc_card_t *card)
{
struct belpic_priv_data *priv = NULL;
scconf_block *conf_block;
#ifdef BELPIC_PIN_PAD
int r;
#endif
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "Belpic V%s", BELPIC_VERSION);
#ifdef HAVE_GUI
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, " with GUI support");
#endif
#ifdef BELPIC_PIN_PAD
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, " with support for pin pad reader libs");
#endif
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "\n");
if (card->type < 0)
card->type = SC_CARD_TYPE_BELPIC_EID; /* Unknown card: assume it's the Belpic Card */
priv = calloc(1, sizeof(struct belpic_priv_data));
if (priv == NULL)
return SC_ERROR_OUT_OF_MEMORY;
card->drv_data = priv;
card->cla = 0x00;
if (card->type == SC_CARD_TYPE_BELPIC_EID) {
_sc_card_add_rsa_alg(card, 1024,
SC_ALGORITHM_RSA_PAD_PKCS1 | SC_ALGORITHM_RSA_HASH_NONE, 0);
}
/* State that we have an RNG */
card->caps |= SC_CARD_CAP_RNG;
/* Language prefences */
priv->lang = -1;
conf_block = get_belpic_conf(card->ctx, "belpic_general");
if (conf_block != NULL) {
char *lang = (char *) scconf_get_str(conf_block, "force_language", NULL);
if (lang != NULL && strlen(lang) == 2)
priv->lang = str2lang(card->ctx, lang);
}
#ifdef GET_LANG_FROM_CARD
if (priv->lang == -1)
priv->lang = get_language(card);
#endif
card->max_pin_len = BELPIC_MAX_USER_PIN_LEN;
#ifdef HAVE_GUI
r = scgui_init();
if (r != 0)
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "scgui_init() returned error %d\n", i);
#endif
#ifdef BELPIC_PIN_PAD
r = belpic_detect_pin_pad(card, priv);
if (r == 1)
card->reader->capabilities |= SC_READER_CAP_PIN_PAD;
else if (r < 0)
return r; /* error loading/initing pin pad lib */
conf_block = get_belpic_conf(card->ctx, "belpic_pin_pad");
if (conf_block != NULL) {
if (scconf_get_bool(conf_block, "msg_auth_pin", 1))
priv->options |= PP_MSG_AUTH_PIN;
if (scconf_get_bool(conf_block, "msg_wrong_pin", 1))
priv->options |= PP_MSG_WRONG_PIN;
if (scconf_get_bool(conf_block, "msg_changepin_mismatch", 1))
priv->options |= PP_MSG_CHANGEPIN_MISMATCH;
if (scconf_get_bool(conf_block, "msg_pin_blocked", 1))
priv->options |= PP_MSG_PIN_BLOCKED;
}
#endif
return 0;
}
static int belpic_select_file(sc_card_t *card,
const sc_path_t *in_path, sc_file_t **file_out)
{
sc_context_t *ctx;
sc_apdu_t apdu;
u8 pathbuf[SC_MAX_PATH_SIZE], *path = pathbuf;
int r, pathlen;
sc_file_t *file = NULL;
assert(card != NULL && in_path != NULL);
ctx = card->ctx;
memcpy(path, in_path->value, in_path->len);
pathlen = in_path->len;
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xA4, 0x08, 0x0C);
apdu.lc = pathlen;
apdu.data = path;
apdu.datalen = pathlen;