-
Notifications
You must be signed in to change notification settings - Fork 713
/
card-sc-hsm.c
1980 lines (1611 loc) · 54.9 KB
/
card-sc-hsm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
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-sc-hsm.c
*
* Driver for the SmartCard-HSM, a light-weight hardware security module
*
* Copyright (C) 2012 Andreas Schwier, CardContact, Minden, Germany, and others
* Copyright (C) 2018-2019 GSMK - Gesellschaft für Sichere Mobile Kommunikation mbH
*
* 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
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "internal.h"
#include "asn1.h"
#include "cardctl.h"
#include "types.h"
#include "card-sc-hsm.h"
#if defined(ENABLE_SM) && defined(ENABLE_OPENPACE)
#include "sm/sm-eac.h"
#include <eac/cv_cert.h>
#include <eac/eac.h>
#include <eac/ta.h>
#include <openssl/evp.h>
#endif
/* Static reference to ISO driver */
static const struct sc_card_operations *iso_ops = NULL;
/* Our operations */
static struct sc_card_operations sc_hsm_ops;
/* Our driver description */
static struct sc_card_driver sc_hsm_drv = {
"SmartCard-HSM",
"sc-hsm",
&sc_hsm_ops,
NULL,
0,
NULL
};
/* Our AID */
struct sc_aid sc_hsm_aid = { { 0xE8,0x2B,0x06,0x01,0x04,0x01,0x81,0xC3,0x1F,0x02,0x01 }, 11 };
/* Known ATRs for SmartCard-HSMs */
const struct sc_atr_table sc_hsm_atrs[] = {
/* standard version */
{"3B:FE:18:00:00:81:31:FE:45:80:31:81:54:48:53:4D:31:73:80:21:40:81:07:FA", NULL, NULL, SC_CARD_TYPE_SC_HSM, 0, NULL},
{"3B:8E:80:01:80:31:81:54:48:53:4D:31:73:80:21:40:81:07:18", NULL, NULL, SC_CARD_TYPE_SC_HSM, 0, NULL},
{"3B:DE:18:FF:81:91:FE:1F:C3:80:31:81:54:48:53:4D:31:73:80:21:40:81:07:1C", NULL, NULL, SC_CARD_TYPE_SC_HSM, 0, NULL},
{"3B:DE:96:FF:81:91:FE:1F:C3:80:31:81:54:48:53:4D:31:73:80:21:40:81:07:92", NULL, NULL, SC_CARD_TYPE_SC_HSM, 0, NULL},
{"3B:80:80:01:01", NULL, NULL, SC_CARD_TYPE_SC_HSM_SOC, 0, NULL}, // SoC Sample Card
{
"3B:84:80:01:47:6f:49:44:00",
"FF:FF:FF:FF:FF:FF:FF:FF:00",
"GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL
},
{
"3B:85:80:01:47:6f:49:44:00:00",
"FF:FF:FF:FF:FF:FF:FF:FF:00:00",
"GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL
},
{
"3B:86:80:01:47:6f:49:44:00:00:00",
"FF:FF:FF:FF:FF:FF:FF:FF:00:00:00",
"GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL
},
{
"3B:87:80:01:47:6f:49:44:00:00:00:00",
"FF:FF:FF:FF:FF:FF:FF:FF:00:00:00:00",
"GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL
},
{
"3B:88:80:01:47:6f:49:44:00:00:00:00:00",
"FF:FF:FF:FF:FF:FF:FF:FF:00:00:00:00:00",
"GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL
},
{
"3B:89:80:01:47:6f:49:44:00:00:00:00:00:00",
"FF:FF:FF:FF:FF:FF:FF:FF:00:00:00:00:00:00",
"GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL
},
{
"3B:8a:80:01:47:6f:49:44:00:00:00:00:00:00:00",
"FF:FF:FF:FF:FF:FF:FF:FF:00:00:00:00:00:00:00",
"GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL
},
{
"3B:8b:80:01:47:6f:49:44:00:00:00:00:00:00:00:00",
"FF:FF:FF:FF:FF:FF:FF:FF:00:00:00:00:00:00:00:00",
"GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL
},
{
"3B:8c:80:01:47:6f:49:44:00:00:00:00:00:00:00:00:00",
"FF:FF:FF:FF:FF:FF:FF:FF:00:00:00:00:00:00:00:00:00",
"GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL
},
{
"3B:8d:80:01:47:6f:49:44:00:00:00:00:00:00:00:00:00:00",
"FF:FF:FF:FF:FF:FF:FF:FF:00:00:00:00:00:00:00:00:00:00",
"GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL
},
{
"3B:8e:80:01:47:6f:49:44:00:00:00:00:00:00:00:00:00:00:00",
"FF:FF:FF:FF:FF:FF:FF:FF:00:00:00:00:00:00:00:00:00:00:00",
"GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL
},
{
"3B:8f:80:01:47:6f:49:44:00:00:00:00:00:00:00:00:00:00:00:00",
"FF:FF:FF:FF:FF:FF:FF:FF:00:00:00:00:00:00:00:00:00:00:00:00",
"GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL
},
{NULL, NULL, NULL, 0, 0, NULL}
};
static int sc_hsm_select_file_ex(sc_card_t *card,
const sc_path_t *in_path, int forceselect,
sc_file_t **file_out)
{
int rv;
sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data;
sc_file_t *file = NULL;
sc_path_t cpath;
size_t card_max_recv_size = card->max_recv_size;
size_t reader_max_recv_size = card->reader->max_recv_size;
if (file_out == NULL) { // Versions before 0.16 of the SmartCard-HSM do not support P2='0C'
rv = sc_hsm_select_file_ex(card, in_path, forceselect, &file);
sc_file_free(file);
return rv;
}
if ((in_path->type == SC_PATH_TYPE_FILE_ID) && in_path->aid.len) {
// Split applet selection and file selection into two separate calls
cpath = *in_path;
cpath.len = 0;
cpath.type = SC_PATH_TYPE_DF_NAME;
rv = sc_hsm_select_file_ex(card, &cpath, forceselect, NULL);
LOG_TEST_RET(card->ctx, rv, "Could not select SmartCard-HSM application");
if (in_path->len) {
cpath = *in_path;
cpath.aid.len = 0;
rv = sc_hsm_select_file_ex(card, &cpath, forceselect, file_out);
}
return rv;
}
// Prevent selection of applet unless this is the first time, selection is forced or the device is not authenticated
if (in_path->type == SC_PATH_TYPE_DF_NAME
|| (in_path->type == SC_PATH_TYPE_PATH
&& in_path->len == sc_hsm_aid.len
&& !memcmp(in_path->value, sc_hsm_aid.value, sc_hsm_aid.len))
|| (in_path->type == SC_PATH_TYPE_PATH
&& in_path->len == 0
&& in_path->aid.len == sc_hsm_aid.len
&& !memcmp(in_path->aid.value, sc_hsm_aid.value, sc_hsm_aid.len))) {
if (!priv || (priv->dffcp == NULL) || forceselect) {
/* Force use of Le = 0x00 in iso7816_select_file as required by SC-HSM */
card->max_recv_size = card->reader->max_recv_size = SC_READER_SHORT_APDU_MAX_RECV_SIZE;
rv = (*iso_ops->select_file)(card, in_path, file_out);
card->max_recv_size = card_max_recv_size;
card->reader->max_recv_size = reader_max_recv_size;
LOG_TEST_RET(card->ctx, rv, "Could not select SmartCard-HSM application");
if (priv) {
sc_file_free(priv->dffcp);
// Cache the FCP returned when selecting the applet
sc_file_dup(&priv->dffcp, *file_out);
}
} else {
sc_file_dup(file_out, priv->dffcp);
rv = SC_SUCCESS;
}
return rv;
}
if ((in_path->len >= 2) && (in_path->value[0] == 0x3F) && (in_path->value[1] == 0x00)) {
// The SmartCard-HSM is an applet that is not default selected. Simulate selection of the MF
if (in_path->len == 2) {
file = sc_file_new();
if (file == NULL)
LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
file->path = *in_path;
file->id = 0x3F00;
file->type = SC_FILE_TYPE_DF;
file->magic = SC_FILE_MAGIC;
*file_out = file;
return SC_SUCCESS;
} else {
/* Force use of Le = 0x00 in iso7816_select_file as required by SC-HSM */
card->max_recv_size = card->reader->max_recv_size = SC_READER_SHORT_APDU_MAX_RECV_SIZE;
sc_path_t truncated;
memcpy(&truncated, in_path, sizeof truncated);
truncated.len = in_path->len - 2;
memcpy(truncated.value, in_path->value+2, truncated.len);
rv = (*iso_ops->select_file)(card, &truncated, file_out);
card->max_recv_size = card_max_recv_size;
card->reader->max_recv_size = reader_max_recv_size;
return rv;
}
}
/* Force use of Le = 0x00 in iso7816_select_file as required by SC-HSM */
card->max_recv_size = card->reader->max_recv_size = SC_READER_SHORT_APDU_MAX_RECV_SIZE;
rv = (*iso_ops->select_file)(card, in_path, file_out);
card->max_recv_size = card_max_recv_size;
card->reader->max_recv_size = reader_max_recv_size;
return rv;
}
static int sc_hsm_select_file(sc_card_t *card,
const sc_path_t *in_path,
sc_file_t **file_out)
{
return sc_hsm_select_file_ex(card, in_path, 0, file_out);
}
static int sc_hsm_get_challenge(struct sc_card *card, unsigned char *rnd, size_t len)
{
LOG_FUNC_CALLED(card->ctx);
if (len > 1024) {
len = 1024;
}
LOG_FUNC_RETURN(card->ctx, iso_ops->get_challenge(card, rnd, len));
}
static int sc_hsm_match_card(struct sc_card *card)
{
sc_path_t path;
int i, r, type = 0;
sc_file_t *file = NULL;
i = _sc_match_atr(card, sc_hsm_atrs, &type);
if (i >= 0 && type != SC_CARD_TYPE_SC_HSM_SOC) {
card->type = type;
return 1;
}
sc_path_set(&path, SC_PATH_TYPE_DF_NAME, sc_hsm_aid.value, sc_hsm_aid.len, 0, 0);
r = sc_hsm_select_file(card, &path, &file);
LOG_TEST_RET(card->ctx, r, "Could not select SmartCard-HSM application");
// Validate that card returns a FCP with a proprietary tag 85 with value longer than 2 byte (Fixes #1377)
if (file != NULL) {
size_t sz = file->prop_attr_len;
sc_file_free(file);
if (sz < 2) {
return 0;
}
}
if (type == SC_CARD_TYPE_SC_HSM_SOC) {
card->type = SC_CARD_TYPE_SC_HSM_SOC;
} else {
card->type = SC_CARD_TYPE_SC_HSM;
}
return 1;
}
/*
* Encode 16 hexadecimals of SO-PIN into binary form
* Caller must check length of sopin and provide an 8 byte buffer
*/
static int sc_hsm_encode_sopin(const u8 *sopin, u8 *sopinbin)
{
int i;
unsigned char digit;
memset(sopinbin, 0, 8);
for (i = 0; i < 16; i++) {
*sopinbin <<= 4;
digit = *sopin++;
if (!isxdigit(digit))
return SC_ERROR_PIN_CODE_INCORRECT;
digit = toupper(digit);
if (digit >= 'A')
digit = digit - 'A' + 10;
else
digit = digit & 0xF;
*sopinbin |= digit & 0xf;
if (i & 1)
sopinbin++;
}
return SC_SUCCESS;
}
static int sc_hsm_soc_select_minbioclient(sc_card_t *card)
{
sc_apdu_t apdu;
struct sc_aid minBioClient_aid = {
{ 0xFF,'m','i','n','B','i','o','C','l','i','e','n','t',0x01 }, 14
};
/* Select MinBioClient */
#ifdef ENABLE_SM
sc_sm_stop(card);
#endif
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xA4, 0x04, 0x0C);
apdu.data = minBioClient_aid.value;
apdu.datalen = minBioClient_aid.len;
apdu.lc = minBioClient_aid.len;
LOG_TEST_RET(card->ctx,
sc_transmit_apdu(card, &apdu),
"APDU transmit failed");
return sc_check_sw(card, apdu.sw1, apdu.sw2);
}
static int sc_hsm_soc_change(sc_card_t *card, struct sc_pin_cmd_data *data,
int *tries_left)
{
sc_apdu_t apdu;
sc_path_t path;
int r;
if (card->type == SC_CARD_TYPE_SC_HSM_SOC) {
/* Select MinBioClient */
r = sc_hsm_soc_select_minbioclient(card);
LOG_TEST_RET(card->ctx, r, "Could not select MinBioClient application");
/* verify PIN */
sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x20, 0x00, 0x80);
r = sc_transmit_apdu(card, &apdu);
LOG_TEST_GOTO_ERR(card->ctx, r, "APDU transmit failed");
r = sc_check_sw(card, apdu.sw1, apdu.sw2);
LOG_TEST_GOTO_ERR(card->ctx, r, "Could not verify PIN");
/* change PIN */
sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x24, 0x01, 0x80);
r = sc_transmit_apdu(card, &apdu);
LOG_TEST_GOTO_ERR(card->ctx, r, "APDU transmit failed");
r = sc_check_sw(card, apdu.sw1, apdu.sw2);
LOG_TEST_GOTO_ERR(card->ctx, r, "Could not change PIN");
} else {
#ifdef ENABLE_SM
unsigned sm_mode = card->sm_ctx.sm_mode;
#endif
/* verify PIN */
sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x20, 0x00, 0x85);
apdu.cla = 0x80;
r = sc_transmit_apdu(card, &apdu);
LOG_TEST_GOTO_ERR(card->ctx, r, "APDU transmit failed");
#ifdef ENABLE_SM
/* temporary disable SM, change reference data does not reach the applet */
card->sm_ctx.sm_mode = SM_MODE_NONE;
#endif
/* change PIN */
sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x24, 0x01, 0x85);
apdu.cla = 0x80;
r = sc_transmit_apdu(card, &apdu);
#ifdef ENABLE_SM
/* restore SM if possible */
card->sm_ctx.sm_mode = sm_mode;
#endif
LOG_TEST_GOTO_ERR(card->ctx, r, "APDU transmit failed");
r = sc_check_sw(card, apdu.sw1, apdu.sw2);
LOG_TEST_GOTO_ERR(card->ctx, r, "Could not change PIN");
}
err:
if (card->type == SC_CARD_TYPE_SC_HSM_SOC) {
/* Select SC-HSM */
sc_path_set(&path, SC_PATH_TYPE_DF_NAME,
sc_hsm_aid.value, sc_hsm_aid.len, 0, 0);
LOG_TEST_RET(card->ctx,
sc_hsm_select_file_ex(card, &path, 1, NULL),
"Could not select SmartCard-HSM application");
}
return r;
}
static int sc_hsm_soc_unblock(sc_card_t *card, struct sc_pin_cmd_data *data,
int *tries_left)
{
sc_apdu_t apdu;
sc_path_t path;
int r;
if (card->type == SC_CARD_TYPE_SC_HSM_GOID) {
return SC_ERROR_NOT_SUPPORTED;
}
/* Select MinBioClient */
r = sc_hsm_soc_select_minbioclient(card);
LOG_TEST_RET(card->ctx, r, "Could not select MinBioClient application");
/* verify PUK */
sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x20, 0x00, 0x81);
r = sc_transmit_apdu(card, &apdu);
LOG_TEST_GOTO_ERR(card->ctx, r, "APDU transmit failed");
r = sc_check_sw(card, apdu.sw1, apdu.sw2);
LOG_TEST_GOTO_ERR(card->ctx, r, "Could not verify PUK");
/* reset retry counter */
sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x2c, 0x03, 0x00);
r = sc_transmit_apdu(card, &apdu);
LOG_TEST_GOTO_ERR(card->ctx, r, "APDU transmit failed");
r = sc_check_sw(card, apdu.sw1, apdu.sw2);
LOG_TEST_GOTO_ERR(card->ctx, r, "Could not unblock PIN");
err:
/* Select SC-HSM */
sc_path_set(&path, SC_PATH_TYPE_DF_NAME, sc_hsm_aid.value, sc_hsm_aid.len, 0, 0);
LOG_TEST_RET(card->ctx,
sc_hsm_select_file_ex(card, &path, 1, NULL),
"Could not select SmartCard-HSM application");
return r;
}
static int sc_hsm_soc_biomatch(sc_card_t *card, struct sc_pin_cmd_data *data,
int *tries_left)
{
sc_apdu_t apdu;
u8 rbuf[SC_MAX_APDU_BUFFER_SIZE];
int r;
if (card->type == SC_CARD_TYPE_SC_HSM_SOC) {
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x20, 0x00, 0x85);
apdu.cla = 0x80;
apdu.data = (unsigned char*)"\x7F\x24\x00";
apdu.datalen = 3;
apdu.lc = 3;
apdu.resplen = 0;
r = sc_transmit_apdu(card, &apdu);
LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
/* ignore the actual status bytes */
}
/* JCOP's SM accelerator is incapable of using case 1 APDU in SM */
sc_format_apdu(card, &apdu, SC_APDU_CASE_2, 0x20, 0x00, 0x81);
if (card->type == SC_CARD_TYPE_SC_HSM_GOID) {
apdu.cla = 0x80;
}
apdu.resp = rbuf;
apdu.resplen = sizeof rbuf;
r = sc_transmit_apdu(card, &apdu);
LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
/* now check the status bytes */
r = sc_check_sw(card, apdu.sw1, apdu.sw2);
if (r == SC_SUCCESS) {
LOG_FUNC_RETURN(card->ctx, r);
}
LOG_FUNC_RETURN(card->ctx, SC_ERROR_PIN_CODE_INCORRECT);
}
#if defined(ENABLE_SM) && defined(ENABLE_OPENPACE)
static int sc_hsm_perform_chip_authentication(sc_card_t *card)
{
int r, protocol;
sc_path_t path;
u8 all_certs[1024];
EAC_CTX *ctx = NULL;
size_t all_certs_len = sizeof all_certs, left, device_cert_len, issuer_cert_len;
const unsigned char *cert = all_certs, *device_cert, *issuer_cert;
BUF_MEM *comp_pub_key = NULL;
sc_cvc_t cvc_device, cvc_issuer;
/* this is only needed to call sc_pkcs15emu_sc_hsm_decode_cvc */
sc_pkcs15_card_t p15card;
sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data;
/* we know that sc_pkcs15emu_sc_hsm_decode_cvc does not require anything
* else to be initialized than p15card->card */
p15card.card = card;
memset(&cvc_device, 0, sizeof(cvc_device));
memset(&cvc_issuer, 0, sizeof(cvc_issuer));
if (priv->EF_C_DevAut && priv->EF_C_DevAut_len) {
all_certs_len = priv->EF_C_DevAut_len;
cert = priv->EF_C_DevAut;
} else {
/* get issuer and device certificate from the card */
r = sc_path_set(&path, SC_PATH_TYPE_FILE_ID, (u8 *) "\x2F\x02", 2, 0, 0);
if (r < 0)
goto err;
r = sc_select_file(card, &path, NULL);
if (r < 0)
goto err;
r = sc_read_binary(card, 0, all_certs, all_certs_len, 0);
if (r < 0)
goto err;
if (r == 0) {
r = SC_ERROR_FILE_NOT_FOUND;
goto err;
}
all_certs_len = r;
/* save EF_C_DevAut for further use */
cert = realloc(priv->EF_C_DevAut, all_certs_len);
if (cert) {
memcpy((unsigned char *) cert, all_certs, all_certs_len);
priv->EF_C_DevAut = (unsigned char *) cert;
priv->EF_C_DevAut_len = all_certs_len;
}
cert = all_certs;
}
left = all_certs_len;
device_cert = cert;
r = sc_pkcs15emu_sc_hsm_decode_cvc(&p15card, &cert, &left, &cvc_device);
if (r < 0)
goto err;
device_cert_len = all_certs_len - left;
issuer_cert = cert;
r = sc_pkcs15emu_sc_hsm_decode_cvc(&p15card, &cert, &left, &cvc_issuer);
if (r < 0)
goto err;
issuer_cert_len = all_certs_len - device_cert_len - left;
ctx = EAC_CTX_new();
if (!ctx) {
r = SC_ERROR_INTERNAL;
goto err;
}
/* check all CVCs given of the document's pki */
if (!TA_STEP2_import_certificate(ctx, issuer_cert, issuer_cert_len)
|| !TA_STEP2_import_certificate(ctx, device_cert, device_cert_len)) {
r = SC_ERROR_INTERNAL;
goto err;
}
/* XXX on older JCOPs only NID_id_CA_ECDH_3DES_CBC_CBC may be
* supported. The card does not export its capabilities. We hardcode
* NID_id_CA_ECDH_AES_CBC_CMAC_128 here, because we don't have the older
* cards in production. */
protocol = NID_id_CA_ECDH_AES_CBC_CMAC_128;
/* initialize CA domain parameter with the document's public key */
if (!EAC_CTX_init_ca(ctx, protocol, 8)) {
r = SC_ERROR_INTERNAL;
goto err;
}
EVP_PKEY_free(ctx->ca_ctx->ka_ctx->key);
if (!EVP_PKEY_up_ref(ctx->ta_ctx->pub_key)) {
sc_log_openssl(card->ctx);
r = SC_ERROR_INTERNAL;
goto err;
}
ctx->ca_ctx->ka_ctx->key = ctx->ta_ctx->pub_key;
/* generate keys for CA */
comp_pub_key = TA_STEP3_generate_ephemeral_key(ctx);
r = perform_chip_authentication_ex(card, ctx,
cvc_device.publicPoint, cvc_device.publicPointlen);
err:
if (r < 0)
EAC_CTX_clear_free(ctx);
if (comp_pub_key)
BUF_MEM_free(comp_pub_key);
sc_pkcs15emu_sc_hsm_free_cvc(&cvc_device);
sc_pkcs15emu_sc_hsm_free_cvc(&cvc_issuer);
return r;
}
#else
static int sc_hsm_perform_chip_authentication(sc_card_t *card)
{
return SC_ERROR_NOT_SUPPORTED;
}
#endif
static int sc_hsm_pin_cmd(sc_card_t *card, struct sc_pin_cmd_data *data,
int *tries_left)
{
sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data;
sc_apdu_t apdu;
u8 cmdbuff[16];
#ifdef ENABLE_SM
u8 rbuf[SC_MAX_APDU_BUFFER_SIZE];
#endif
int r;
int cmd = data->cmd;
size_t pin2_len = data->pin2.len;
if (cmd == SC_PIN_CMD_GET_SESSION_PIN) {
/* First, perform a standard VERIFY */
data->cmd = SC_PIN_CMD_VERIFY;
/* we assign pin2.len to 0 early on so that in case of an error we are
* not exiting with an undefined session PIN */
data->pin2.len = 0;
}
if ((card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH)
&& (data->cmd == SC_PIN_CMD_CHANGE)
&& (data->pin_reference == 0x81)
&& (!data->pin1.data || data->pin1.len <= 0)) {
return sc_hsm_soc_change(card, data, tries_left);
} else if ((card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH)
&& (data->cmd == SC_PIN_CMD_UNBLOCK)
&& (data->pin_reference == 0x81)
&& (!data->pin1.data || data->pin1.len <= 0)) {
return sc_hsm_soc_unblock(card, data, tries_left);
}
#ifdef ENABLE_SM
/* For contactless cards always establish a secure channel before PIN
* verification. Also, Session PIN generation requires SM. */
if ((card->type == SC_CARD_TYPE_SC_HSM_SOC
|| card->type == SC_CARD_TYPE_SC_HSM_GOID
|| card->reader->uid.len || cmd == SC_PIN_CMD_GET_SESSION_PIN)
&& (data->cmd != SC_PIN_CMD_GET_INFO)) {
struct sc_pin_cmd_data check_sm_pin_data;
memset(&check_sm_pin_data, 0, sizeof(check_sm_pin_data));
check_sm_pin_data.cmd = SC_PIN_CMD_GET_INFO;
check_sm_pin_data.pin_type = data->pin_type;
check_sm_pin_data.pin_reference = data->pin_reference;
r = SC_ERROR_NOT_ALLOWED;
if (card->sm_ctx.sm_mode == SM_MODE_TRANSMIT) {
/* check if the existing SM channel is still valid */
r = sc_pin_cmd(card, &check_sm_pin_data, NULL);
}
if (r == SC_ERROR_ASN1_OBJECT_NOT_FOUND || r == SC_ERROR_NOT_ALLOWED) {
/* need to establish a new SM channel */
LOG_TEST_RET(card->ctx,
sc_hsm_perform_chip_authentication(card),
"Could not perform chip authentication");
}
}
#endif
if ((card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH)
&& (data->cmd == SC_PIN_CMD_VERIFY)
&& (data->pin_reference == 0x81)
&& (!data->pin1.data || data->pin1.len <= 0)) {
r = sc_hsm_soc_biomatch(card, data, tries_left);
} else {
if ((data->cmd == SC_PIN_CMD_VERIFY) && (data->pin_reference == 0x88)) {
if (data->pin1.len != 16)
return SC_ERROR_INVALID_PIN_LENGTH;
// Save SO PIN for later use in sc_hsm_init_pin()
r = sc_hsm_encode_sopin(data->pin1.data, priv->sopin);
LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
}
if ((data->cmd == SC_PIN_CMD_CHANGE) && (data->pin_reference == 0x88)) {
if ((data->pin1.len != 16) || (data->pin2.len != 16))
return SC_ERROR_INVALID_PIN_LENGTH;
r = sc_hsm_encode_sopin(data->pin1.data, cmdbuff);
LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
r = sc_hsm_encode_sopin(data->pin2.data, cmdbuff + 8);
LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x24, 0x00, data->pin_reference);
apdu.data = cmdbuff;
apdu.datalen = sizeof(cmdbuff);
apdu.lc = 16;
apdu.resplen = 0;
data->apdu = &apdu;
}
#ifdef ENABLE_SM
if ((data->cmd == SC_PIN_CMD_GET_INFO)
&& (card->sm_ctx.sm_mode == SM_MODE_TRANSMIT)) {
/* JCOP's SM accelerator is incapable of using case 1 APDU in SM */
sc_format_apdu(card, &apdu, SC_APDU_CASE_2, 0x20, 0x00, data->pin_reference);
apdu.resp = rbuf;
apdu.resplen = sizeof rbuf;
data->apdu = &apdu;
}
#endif
data->pin1.offset = 5;
data->pin2.offset = 5;
r = (*iso_ops->pin_cmd)(card, data, tries_left);
data->apdu = NULL;
}
LOG_TEST_RET(card->ctx, r, "Verification failed");
if (cmd == SC_PIN_CMD_GET_SESSION_PIN) {
/* reset data->cmd to its original value */
data->cmd = SC_PIN_CMD_GET_SESSION_PIN;
if (data->pin_reference == 0x81) {
u8 recvbuf[SC_MAX_APDU_BUFFER_SIZE];
#ifdef ENABLE_SM
if (card->sm_ctx.sm_mode != SM_MODE_TRANSMIT) {
sc_log(card->ctx,
"Session PIN generation only supported in SM");
LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
}
#else
sc_log(card->ctx,
"Session PIN generation only supported in SM");
LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
#endif
sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0x5A, 0x01, data->pin_reference);
apdu.cla = 0x80;
apdu.resp = recvbuf;
apdu.resplen = sizeof recvbuf;
apdu.le = 0;
if (sc_transmit_apdu(card, &apdu) != SC_SUCCESS
|| sc_check_sw(card, apdu.sw1, apdu.sw2) != SC_SUCCESS) {
sc_log(card->ctx,
"Generating session PIN failed");
LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
}
if (data->pin2.data && pin2_len > 0) {
if (pin2_len >= apdu.resplen) {
memcpy((unsigned char *) data->pin2.data, apdu.resp,
apdu.resplen);
data->pin2.len = apdu.resplen;
} else {
sc_log(card->ctx,
"Buffer too small for session PIN");
}
}
} else {
sc_log(card->ctx,
"Session PIN not supported for this PIN (0x%02X)",
data->pin_reference);
}
}
LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
}
static int sc_hsm_logout(sc_card_t * card)
{
sc_path_t path;
sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data;
memset(priv->sopin, 0, sizeof(priv->sopin));
#ifdef ENABLE_SM
sc_sm_stop(card);
#endif
sc_path_set(&path, SC_PATH_TYPE_DF_NAME, sc_hsm_aid.value, sc_hsm_aid.len, 0, 0);
return sc_hsm_select_file_ex(card, &path, 1, NULL);
}
/* NOTE: idx is an offset into the card's file, not into buf */
static int sc_hsm_read_binary(sc_card_t *card,
unsigned int idx, u8 *buf, size_t count,
unsigned long *flags)
{
sc_context_t *ctx = card->ctx;
sc_apdu_t apdu;
u8 cmdbuff[4];
int r;
if (idx > 0xffff) {
sc_log(ctx, "invalid EF offset: 0x%X > 0xFFFF", idx);
return SC_ERROR_OFFSET_TOO_LARGE;
}
cmdbuff[0] = 0x54;
cmdbuff[1] = 0x02;
cmdbuff[2] = (idx >> 8) & 0xFF;
cmdbuff[3] = idx & 0xFF;
assert(count <= sc_get_max_recv_size(card));
sc_format_apdu(card, &apdu, SC_APDU_CASE_4, 0xB1, 0x00, 0x00);
apdu.data = cmdbuff;
apdu.datalen = 4;
apdu.lc = 4;
apdu.le = count;
apdu.resplen = count;
apdu.resp = buf;
r = sc_transmit_apdu(card, &apdu);
LOG_TEST_RET(ctx, r, "APDU transmit failed");
r = sc_check_sw(card, apdu.sw1, apdu.sw2);
if (r != SC_ERROR_FILE_END_REACHED) {
LOG_TEST_RET(ctx, r, "Check SW error");
}
LOG_FUNC_RETURN(ctx, (int)apdu.resplen);
}
/* NOTE: idx is an offset into the card's file, not into buf */
static int sc_hsm_write_ef(sc_card_t *card,
int fid,
unsigned int idx, const u8 *buf, size_t count)
{
sc_context_t *ctx = card->ctx;
sc_apdu_t apdu;
u8 *cmdbuff, *p;
size_t len;
int r;
if (idx > 0xffff) {
sc_log(ctx, "invalid EF offset: 0x%X > 0xFFFF", idx);
return SC_ERROR_OFFSET_TOO_LARGE;
}
cmdbuff = malloc(8 + count);
if (!cmdbuff) {
LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
}
size_t bytes_left = count;
// 8 bytes are required for T54(4) and T53(4)
size_t blk_size = card->max_send_size - 8;
size_t to_send = 0;
size_t file_offset = (size_t) idx;
size_t offset = 0;
do {
to_send = bytes_left >= blk_size ? blk_size : bytes_left;
p = cmdbuff;
// ASN1 0x54 offset
*p++ = 0x54;
*p++ = 0x02;
*p++ = (file_offset >> 8) & 0xFF;
*p++ = file_offset & 0xFF;
// ASN1 0x53 to_send
*p++ = 0x53;
if (to_send < 128) {
*p++ = (u8)to_send;
len = 6;
} else if (to_send < 256) {
*p++ = 0x81;
*p++ = (u8)to_send;
len = 7;
} else {
*p++ = 0x82;
*p++ = (to_send >> 8) & 0xFF;
*p++ = to_send & 0xFF;
len = 8;
}
if (buf != NULL)
memcpy(p, buf+offset, to_send);
len += to_send;
sc_format_apdu(card, &apdu, SC_APDU_CASE_3, 0xD7, fid >> 8, fid & 0xFF);
apdu.data = cmdbuff;
apdu.datalen = len;
apdu.lc = len;
r = sc_transmit_apdu(card, &apdu);
LOG_TEST_GOTO_ERR(ctx, r, "APDU transmit failed");
r = sc_check_sw(card, apdu.sw1, apdu.sw2);
LOG_TEST_GOTO_ERR(ctx, r, "Check SW error");
bytes_left -= to_send;
offset += to_send;
file_offset += to_send;
} while (0 < bytes_left);
err:
free(cmdbuff);
LOG_FUNC_RETURN(ctx, (int)count);
}
static int sc_hsm_update_binary(sc_card_t *card,
unsigned int idx, const u8 *buf, size_t count,
unsigned long flags)
{
return sc_hsm_write_ef(card, 0, idx, buf, count);
}
static int sc_hsm_list_files(sc_card_t *card, u8 * buf, size_t buflen)
{
sc_apdu_t apdu;
u8 recvbuf[MAX_EXT_APDU_LENGTH];
sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data;
int r;
if (priv->noExtLength) {
sc_format_apdu(card, &apdu, SC_APDU_CASE_2, 0x58, 0, 0);
} else {
sc_format_apdu(card, &apdu, SC_APDU_CASE_2_EXT, 0x58, 0, 0);
}
apdu.cla = 0x80;
apdu.resp = recvbuf;
apdu.resplen = sizeof(recvbuf);
apdu.le = 0;
r = sc_transmit_apdu(card, &apdu);
if ((r == SC_ERROR_TRANSMIT_FAILED) && (!priv->noExtLength)) {
sc_log(card->ctx, "No extended length support ? Trying fall-back to short APDUs, probably breaking support for RSA 2048 operations");
priv->noExtLength = 1;
card->max_send_size = 248; // 255 - 7 because of TLV in odd ins UPDATE BINARY
return sc_hsm_list_files(card, buf, buflen);
}
LOG_TEST_RET(card->ctx, r, "ENUMERATE OBJECTS APDU transmit failed");
if (buflen < apdu.resplen)
memcpy(buf, recvbuf, buflen);
else
memcpy(buf, recvbuf, apdu.resplen);
LOG_FUNC_RETURN(card->ctx, (int)apdu.resplen);
}
static int sc_hsm_create_file(sc_card_t *card, sc_file_t *file)
{
int r;
r = sc_hsm_write_ef(card, file->id, 0, NULL, 0);
LOG_TEST_RET(card->ctx, r, "Create file failed");
LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
}
static int sc_hsm_delete_file(sc_card_t *card, const sc_path_t *path)
{
sc_context_t *ctx = card->ctx;
sc_apdu_t apdu;
u8 sbuf[2];
int r;
if ((path->type != SC_PATH_TYPE_FILE_ID) || (path->len != 2)) {
sc_log(card->ctx, "File type has to be SC_PATH_TYPE_FILE_ID");
LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
}
sbuf[0] = path->value[0];
sbuf[1] = path->value[1];
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xE4, 0x02, 0x00);
apdu.data = sbuf;
apdu.datalen = sizeof(sbuf);
apdu.lc = sizeof(sbuf);
r = sc_transmit_apdu(card, &apdu);
LOG_TEST_RET(ctx, r, "APDU transmit failed");