-
Notifications
You must be signed in to change notification settings - Fork 705
/
card-setcos.c
1161 lines (1034 loc) · 33.3 KB
/
card-setcos.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-setcos.c: Support for PKI cards by Setec
*
* Copyright (C) 2001, 2002 Juha Yrjölä <juha.yrjola@iki.fi>
* Copyright (C) 2005 Antti Tapaninen <aet@cc.hut.fi>
* Copyright (C) 2005 Zetes
*
* 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 <stdlib.h>
#include <string.h>
#include "internal.h"
#include "asn1.h"
#include "cardctl.h"
#define _FINEID_BROKEN_SELECT_FLAG 1
static const struct sc_atr_table setcos_atrs[] = {
/* some Nokia branded SC */
{ "3B:1F:11:00:67:80:42:46:49:53:45:10:52:66:FF:81:90:00", NULL, NULL, SC_CARD_TYPE_SETCOS_GENERIC, 0, NULL },
/* RSA SecurID 3100 */
{ "3B:9F:94:40:1E:00:67:16:43:46:49:53:45:10:52:66:FF:81:90:00", NULL, NULL, SC_CARD_TYPE_SETCOS_PKI, 0, NULL },
/* FINEID 1016 (SetCOS 4.3.1B3/PKCS#15, VRK) */
{ "3b:9f:94:40:1e:00:67:00:43:46:49:53:45:10:52:66:ff:81:90:00", "ff:ff:ff:ff:ff:ff:ff:00:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff", NULL, SC_CARD_TYPE_SETCOS_FINEID, SC_CARD_FLAG_RNG, NULL },
/* FINEID 2032 (EIDApplet/7816-15, VRK test) */
{ "3b:6b:00:ff:80:62:00:a2:56:46:69:6e:45:49:44", "ff:ff:00:ff:ff:ff:00:ff:ff:ff:ff:ff:ff:ff:ff", NULL, SC_CARD_TYPE_SETCOS_FINEID_V2, 0, NULL },
/* FINEID 2132 (EIDApplet/7816-15, 3rdparty test) */
{ "3b:64:00:ff:80:62:00:a2", "ff:ff:00:ff:ff:ff:00:ff", NULL, SC_CARD_TYPE_SETCOS_FINEID_V2, 0, NULL },
/* FINEID 2064 (EIDApplet/7816-15, VRK) */
{ "3b:7b:00:00:00:80:62:00:51:56:46:69:6e:45:49:44", "ff:ff:00:ff:ff:ff:ff:f0:ff:ff:ff:ff:ff:ff:ff:ff", NULL, SC_CARD_TYPE_SETCOS_FINEID_V2, 0, NULL },
/* FINEID 2164 (EIDApplet/7816-15, 3rdparty) */
{ "3b:64:00:00:80:62:00:51", "ff:ff:ff:ff:ff:ff:f0:ff", NULL, SC_CARD_TYPE_SETCOS_FINEID_V2, 0, NULL },
/* FINEID 2264 (EIDApplet/7816-15, OPK/EMV/AVANT) */
{ "3b:6e:00:00:00:62:00:00:57:41:56:41:4e:54:10:81:90:00", NULL, NULL, SC_CARD_TYPE_SETCOS_FINEID_V2, 0, NULL },
{ "3b:7b:94:00:00:80:62:11:51:56:46:69:6e:45:49:44", NULL, NULL, SC_CARD_TYPE_SETCOS_FINEID_V2, 0, NULL },
/* FINEID cards 1.3.2011 with Samsung chips (round connector) that supports 2048 bit keys. */
{ "3b:7b:94:00:00:80:62:12:51:56:46:69:6e:45:49:44", NULL, NULL, SC_CARD_TYPE_SETCOS_FINEID_V2_2048, 0, NULL },
/* FINEID card for organisations, chip unknown. */
{ "3b:7b:18:00:00:80:62:01:54:56:46:69:6e:45:49:44", NULL, NULL, SC_CARD_TYPE_SETCOS_FINEID_V2, _FINEID_BROKEN_SELECT_FLAG, NULL },
/* Swedish NIDEL card */
{ "3b:9f:94:80:1f:c3:00:68:10:44:05:01:46:49:53:45:31:c8:07:90:00:18", NULL, NULL, SC_CARD_TYPE_SETCOS_NIDEL, 0, NULL },
/* Setcos 4.4.1 */
{ "3b:9f:94:80:1f:c3:00:68:11:44:05:01:46:49:53:45:31:c8:00:00:00:00", "ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:00:00:00:00", NULL, SC_CARD_TYPE_SETCOS_44, 0, NULL },
{ NULL, NULL, NULL, 0, 0, NULL }
};
#define SETCOS_IS_EID_APPLET(card) ((card)->type == SC_CARD_TYPE_SETCOS_EID_V2_0 || (card)->type == SC_CARD_TYPE_SETCOS_EID_V2_1)
/* Setcos 4.4 Life Cycle Status Integer */
#define SETEC_LCSI_CREATE 0x01
#define SETEC_LCSI_INIT 0x03
#define SETEC_LCSI_ACTIVATED 0x07
#define SETEC_LCSI_DEACTIVATE 0x06
#define SETEC_LCSI_TEMINATE 0x0F /* MF only */
static struct sc_card_operations setcos_ops;
static struct sc_card_driver setcos_drv = {
"Setec cards",
"setcos",
&setcos_ops,
NULL, 0, NULL
};
static int match_hist_bytes(sc_card_t *card, const char *str, size_t len)
{
const char *src = (const char *) card->reader->atr_info.hist_bytes;
size_t srclen = card->reader->atr_info.hist_bytes_len;
size_t offset = 0;
if (len == 0)
len = strlen(str);
if (srclen < len)
return 0;
while (srclen - offset > len) {
if (memcmp(src + offset, str, len) == 0) {
return 1;
}
offset++;
}
return 0;
}
static int setcos_match_card(sc_card_t *card)
{
sc_apdu_t apdu;
u8 buf[6];
int i;
i = _sc_match_atr(card, setcos_atrs, &card->type);
if (i < 0) {
/* Unknown card, but has the FinEID application for sure */
if (match_hist_bytes(card, "FinEID", 0)) {
card->type = SC_CARD_TYPE_SETCOS_FINEID_V2_2048;
return 1;
}
if (match_hist_bytes(card, "FISE", 0)) {
card->type = SC_CARD_TYPE_SETCOS_GENERIC;
return 1;
}
/* Check if it's a EID2.x applet by reading the version info */
sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xCA, 0xDF, 0x30);
apdu.cla = 0x00;
apdu.resp = buf;
apdu.resplen = 5;
apdu.le = 5;
i = sc_transmit_apdu(card, &apdu);
if (i == 0 && apdu.sw1 == 0x90 && apdu.sw2 == 0x00 && apdu.resplen == 5) {
if (memcmp(buf, "v2.0", 4) == 0)
card->type = SC_CARD_TYPE_SETCOS_EID_V2_0;
else if (memcmp(buf, "v2.1", 4) == 0)
card->type = SC_CARD_TYPE_SETCOS_EID_V2_1;
else {
buf[sizeof(buf) - 1] = '\0';
sc_log(card->ctx, "SetCOS EID applet %s is not supported", (char *) buf);
return 0;
}
return 1;
}
return 0;
}
card->flags = setcos_atrs[i].flags;
return 1;
}
static int select_pkcs15_app(sc_card_t * card)
{
sc_path_t app;
int r;
/* Regular PKCS#15 AID */
sc_format_path("A000000063504B43532D3135", &app);
app.type = SC_PATH_TYPE_DF_NAME;
r = sc_select_file(card, &app, NULL);
return r;
}
static int setcos_init(sc_card_t *card)
{
card->name = "SetCOS";
/* Handle unknown or forced cards */
if (card->type < 0) {
card->type = SC_CARD_TYPE_SETCOS_GENERIC;
}
switch (card->type) {
case SC_CARD_TYPE_SETCOS_FINEID:
case SC_CARD_TYPE_SETCOS_FINEID_V2_2048:
case SC_CARD_TYPE_SETCOS_NIDEL:
card->cla = 0x00;
select_pkcs15_app(card);
if (card->flags & SC_CARD_FLAG_RNG)
card->caps |= SC_CARD_CAP_RNG;
break;
case SC_CARD_TYPE_SETCOS_44:
case SC_CARD_TYPE_SETCOS_EID_V2_0:
case SC_CARD_TYPE_SETCOS_EID_V2_1:
card->cla = 0x00;
card->caps |= SC_CARD_CAP_USE_FCI_AC;
card->caps |= SC_CARD_CAP_RNG;
card->caps |= SC_CARD_CAP_APDU_EXT;
break;
default:
/* XXX: Get SetCOS version */
card->cla = 0x80; /* SetCOS 4.3.x */
/* State that we have an RNG */
card->caps |= SC_CARD_CAP_RNG;
break;
}
switch (card->type) {
case SC_CARD_TYPE_SETCOS_PKI:
case SC_CARD_TYPE_SETCOS_FINEID_V2_2048:
{
unsigned long flags;
flags = SC_ALGORITHM_RSA_RAW | SC_ALGORITHM_RSA_PAD_PKCS1;
flags |= SC_ALGORITHM_RSA_HASH_NONE | SC_ALGORITHM_RSA_HASH_SHA1;
_sc_card_add_rsa_alg(card, 1024, flags, 0);
_sc_card_add_rsa_alg(card, 2048, flags, 0);
}
break;
case SC_CARD_TYPE_SETCOS_44:
case SC_CARD_TYPE_SETCOS_NIDEL:
case SC_CARD_TYPE_SETCOS_EID_V2_0:
case SC_CARD_TYPE_SETCOS_EID_V2_1:
{
unsigned long flags;
flags = SC_ALGORITHM_RSA_RAW | SC_ALGORITHM_RSA_PAD_PKCS1;
flags |= SC_ALGORITHM_RSA_HASH_NONE | SC_ALGORITHM_RSA_HASH_SHA1;
flags |= SC_ALGORITHM_ONBOARD_KEY_GEN;
_sc_card_add_rsa_alg(card, 512, flags, 0);
_sc_card_add_rsa_alg(card, 768, flags, 0);
_sc_card_add_rsa_alg(card, 1024, flags, 0);
_sc_card_add_rsa_alg(card, 2048, flags, 0);
}
break;
}
return 0;
}
static const struct sc_card_operations *iso_ops = NULL;
static int setcos_construct_fci_44(sc_card_t *card, const sc_file_t *file, u8 *out, size_t *outlen)
{
u8 *p = out;
u8 buf[64];
const u8 *pin_key_info;
int len;
/* Command */
*p++ = 0x6F;
p++;
/* Size (set to 0 for keys/PINs on a Java card) */
if (SETCOS_IS_EID_APPLET(card) &&
(file->type == SC_FILE_TYPE_INTERNAL_EF ||
(file->type == SC_FILE_TYPE_WORKING_EF && file->ef_structure == 0x22)))
buf[0] = buf[1] = 0x00;
else {
buf[0] = (file->size >> 8) & 0xFF;
buf[1] = file->size & 0xFF;
}
sc_asn1_put_tag(0x81, buf, 2, p, *outlen - (p - out), &p);
/* Type */
if (file->type_attr_len) {
memcpy(buf, file->type_attr, file->type_attr_len);
sc_asn1_put_tag(0x82, buf, file->type_attr_len, p, *outlen - (p - out), &p);
} else {
u8 bLen = 1;
buf[0] = file->shareable ? 0x40 : 0;
switch (file->type) {
case SC_FILE_TYPE_INTERNAL_EF: /* RSA keyfile */
buf[0] = 0x11;
break;
case SC_FILE_TYPE_WORKING_EF:
if (file->ef_structure == 0x22) { /* pin-file */
buf[0] = 0x0A; /* EF linear fixed EF for ISF keys */
if (SETCOS_IS_EID_APPLET(card))
bLen = 1;
else {
/* Setcos V4.4 */
bLen = 5;
buf[1] = 0x41; /* fixed */
buf[2] = file->record_length >> 8; /* 2 byte record length */
buf[3] = file->record_length & 0xFF;
buf[4] = file->size / file->record_length; /* record count */
}
} else {
buf[0] |= file->ef_structure & 7; /* set file-type, only for EF, not for DF objects */
}
break;
case SC_FILE_TYPE_DF:
buf[0] = 0x38;
break;
default:
return SC_ERROR_NOT_SUPPORTED;
}
sc_asn1_put_tag(0x82, buf, bLen, p, *outlen - (p - out), &p);
}
/* File ID */
buf[0] = (file->id >> 8) & 0xFF;
buf[1] = file->id & 0xFF;
sc_asn1_put_tag(0x83, buf, 2, p, *outlen - (p - out), &p);
/* DF name */
if (file->type == SC_FILE_TYPE_DF) {
if (file->name[0] != 0)
sc_asn1_put_tag(0x84, (u8 *) file->name, file->namelen, p, *outlen - (p - out), &p);
else { /* Name required -> take the FID if not specified */
buf[0] = (file->id >> 8) & 0xFF;
buf[1] = file->id & 0xFF;
sc_asn1_put_tag(0x84, buf, 2, p, *outlen - (p - out), &p);
}
}
/* Security Attributes */
memcpy(buf, file->sec_attr, file->sec_attr_len);
sc_asn1_put_tag(0x86, buf, file->sec_attr_len, p, *outlen - (p - out), &p);
/* Life cycle status */
if (file->prop_attr_len) {
memcpy(buf, file->prop_attr, file->prop_attr_len);
sc_asn1_put_tag(0x8A, buf, file->prop_attr_len, p, *outlen - (p - out), &p);
}
/* PIN definitions */
if (file->type == SC_FILE_TYPE_DF) {
if (card->type == SC_CARD_TYPE_SETCOS_EID_V2_1) {
pin_key_info = (const u8*)"\xC1\x04\x81\x82\x83\x84";
len = 6;
}
else if (card->type == SC_CARD_TYPE_SETCOS_EID_V2_0) {
pin_key_info = (const u8*)"\xC1\x04\x81\x82"; /* Max 2 PINs supported */
len = 4;
}
else {
/* Pin/key info: define 4 pins, no keys */
if(file->path.len == 2)
pin_key_info = (const u8*)"\xC1\x04\x81\x82\x83\x84\xC2\x00"; /* root-MF: use local pin-file */
else
pin_key_info = (const u8 *)"\xC1\x04\x01\x02\x03\x04\xC2\x00"; /* sub-DF: use parent pin-file in root-MF */
len = 8;
}
sc_asn1_put_tag(0xA5, pin_key_info, len, p, *outlen - (p - out), &p);
}
/* Length */
out[1] = p - out - 2;
*outlen = p - out;
return 0;
}
static int setcos_construct_fci(sc_card_t *card, const sc_file_t *file, u8 *out, size_t *outlen)
{
if (card->type == SC_CARD_TYPE_SETCOS_44 ||
card->type == SC_CARD_TYPE_SETCOS_NIDEL ||
SETCOS_IS_EID_APPLET(card))
return setcos_construct_fci_44(card, file, out, outlen);
else
return iso_ops->construct_fci(card, file, out, outlen);
}
static u8 acl_to_byte(const sc_acl_entry_t *e)
{
switch (e->method) {
case SC_AC_NONE:
return 0x00;
case SC_AC_CHV:
switch (e->key_ref) {
case 1:
return 0x01;
break;
case 2:
return 0x02;
break;
default:
return 0x00;
}
break;
case SC_AC_TERM:
return 0x04;
case SC_AC_NEVER:
return 0x0F;
}
return 0x00;
}
static unsigned int acl_to_byte_44(const struct sc_acl_entry *e, u8* p_bNumber)
{
/* Handle special fixed values */
if (e == (sc_acl_entry_t *) 1) /* SC_AC_NEVER */
return SC_AC_NEVER;
else if ((e == (sc_acl_entry_t *) 2) || /* SC_AC_NONE */
(e == (sc_acl_entry_t *) 3) || /* SC_AC_UNKNOWN */
(e == (sc_acl_entry_t *) 0))
return SC_AC_NONE;
/* Handle standard values */
*p_bNumber = e->key_ref;
return(e->method);
}
/* If pin is present in the pins list, return it's index.
* If it's not yet present, add it to the list and return the index. */
static int setcos_pin_index_44(int *pins, int len, int pin)
{
int i;
for (i = 0; i < len; i++) {
if (pins[i] == pin)
return i;
if (pins[i] == -1) {
pins[i] = pin;
return i;
}
}
assert(i != len); /* Too much PINs, shouldn't happen */
return 0;
}
/* The ACs are always for the SETEC_LCSI_ACTIVATED state, even if
* we have to create the file in the SC_FILE_STATUS_INITIALISATION state. */
static int setcos_create_file_44(sc_card_t *card, sc_file_t *file)
{
const u8 bFileStatus = file->status == SC_FILE_STATUS_CREATION ?
SETEC_LCSI_CREATE : SETEC_LCSI_ACTIVATED;
u8 bCommands_always = 0;
int pins[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
u8 bCommands_pin[sizeof(pins)/sizeof(pins[0])]; /* both 7 entries big */
u8 bCommands_key = 0;
u8 bNumber = 0;
u8 bKeyNumber = 0;
unsigned int bMethod = 0;
/* -1 means RFU */
const int df_idx[8] = { /* byte 1 = OpenSC type of AC Bit0, byte 2 = OpenSC type of AC Bit1 ...*/
SC_AC_OP_DELETE, SC_AC_OP_CREATE, SC_AC_OP_CREATE,
SC_AC_OP_INVALIDATE, SC_AC_OP_REHABILITATE,
SC_AC_OP_LOCK, SC_AC_OP_DELETE, -1};
const int ef_idx[8] = { /* note: SC_AC_OP_SELECT to be ignored, actually RFU */
SC_AC_OP_READ, SC_AC_OP_UPDATE, SC_AC_OP_WRITE,
SC_AC_OP_INVALIDATE, SC_AC_OP_REHABILITATE,
-1, SC_AC_OP_ERASE, -1};
const int efi_idx[8] = { /* internal EF used for RSA keys */
SC_AC_OP_READ, SC_AC_OP_ERASE, SC_AC_OP_UPDATE,
SC_AC_OP_INVALIDATE, SC_AC_OP_REHABILITATE,
-1, SC_AC_OP_ERASE, -1};
/* Set file creation status */
sc_file_set_prop_attr(file, &bFileStatus, 1);
/* Build ACL from local structure = get AC for each operation group */
if (file->sec_attr_len == 0) {
const int* p_idx;
int i;
int len = 0;
u8 bBuf[64];
/* Get specific operation groups for specified file-type */
switch (file->type){
case SC_FILE_TYPE_DF: /* DF */
p_idx = df_idx;
break;
case SC_FILE_TYPE_INTERNAL_EF: /* EF for RSA keys */
p_idx = efi_idx;
break;
default: /* SC_FILE_TYPE_WORKING_EF */
p_idx = ef_idx;
break;
}
/* Get enabled commands + required Keys/Pins */
memset(bCommands_pin, 0, sizeof(bCommands_pin));
for (i = 7; i >= 0; i--) { /* for each AC Setcos operation */
bCommands_always <<= 1;
bCommands_key <<= 1;
if (p_idx[i] == -1) /* -1 means that bit is RFU -> set to 0 */
continue;
bMethod = acl_to_byte_44(file->acl[ p_idx[i] ], &bNumber);
/* Convert to OpenSc-index, convert to pin/key number */
switch(bMethod){
case SC_AC_NONE: /* always allowed */
bCommands_always |= 1;
break;
case SC_AC_CHV: /* pin */
if ((bNumber & 0x7F) == 0 || (bNumber & 0x7F) > 7) {
sc_log(card->ctx, "SetCOS 4.4 PIN refs can only be 1..7\n");
return SC_ERROR_INVALID_ARGUMENTS;
}
bCommands_pin[setcos_pin_index_44(pins, sizeof(pins)/sizeof(pins[0]), (int) bNumber)] |= 1 << i;
break;
case SC_AC_TERM: /* key */
bKeyNumber = bNumber; /* There should be only 1 key */
bCommands_key |= 1;
break;
}
}
/* Add the commands that are always allowed */
if (bCommands_always) {
bBuf[len++] = 1;
bBuf[len++] = bCommands_always;
}
/* Add commands that require pins */
for (i = 0; i < (int)sizeof(bCommands_pin) && pins[i] != -1; i++) {
bBuf[len++] = 2;
bBuf[len++] = bCommands_pin[i];
if (SETCOS_IS_EID_APPLET(card))
bBuf[len++] = pins[i]; /* pin ref */
else
bBuf[len++] = pins[i] & 0x07; /* pin ref */
}
/* Add commands that require the key */
if (bCommands_key) {
bBuf[len++] = 2 | 0x20; /* indicate keyNumber present */
bBuf[len++] = bCommands_key;
bBuf[len++] = bKeyNumber;
}
/* RSA signing/decryption requires AC adaptive coding, can't be put
in AC simple coding. Only implemented for pins, not for a key. */
if ( (file->type == SC_FILE_TYPE_INTERNAL_EF) &&
(acl_to_byte_44(file->acl[SC_AC_OP_CRYPTO], &bNumber) == SC_AC_CHV) ) {
bBuf[len++] = 0x83;
bBuf[len++] = 0x01;
bBuf[len++] = 0x2A; /* INS byte for the sign/decrypt APDU */
bBuf[len++] = bNumber & 0x07; /* pin ref */
}
sc_file_set_sec_attr(file, bBuf, len);
}
return iso_ops->create_file(card, file);
}
static int setcos_create_file(sc_card_t *card, sc_file_t *file)
{
if (card->type == SC_CARD_TYPE_SETCOS_44 || SETCOS_IS_EID_APPLET(card))
return setcos_create_file_44(card, file);
if (file->prop_attr_len == 0)
sc_file_set_prop_attr(file, (const u8 *) "\x03\x00\x00", 3);
if (file->sec_attr_len == 0) {
int idx[6], i;
u8 buf[6];
if (file->type == SC_FILE_TYPE_DF) {
const int df_idx[6] = {
SC_AC_OP_SELECT, SC_AC_OP_LOCK, SC_AC_OP_DELETE,
SC_AC_OP_CREATE, SC_AC_OP_REHABILITATE,
SC_AC_OP_INVALIDATE
};
for (i = 0; i < 6; i++)
idx[i] = df_idx[i];
} else {
const int ef_idx[6] = {
SC_AC_OP_READ, SC_AC_OP_UPDATE, SC_AC_OP_WRITE,
SC_AC_OP_ERASE, SC_AC_OP_REHABILITATE,
SC_AC_OP_INVALIDATE
};
for (i = 0; i < 6; i++)
idx[i] = ef_idx[i];
}
for (i = 0; i < 6; i++) {
const struct sc_acl_entry *entry;
entry = sc_file_get_acl_entry(file, idx[i]);
buf[i] = acl_to_byte(entry);
}
sc_file_set_sec_attr(file, buf, 6);
}
return iso_ops->create_file(card, file);
}
static int setcos_set_security_env2(sc_card_t *card,
const sc_security_env_t *env, int se_num)
{
sc_apdu_t apdu;
u8 sbuf[SC_MAX_APDU_BUFFER_SIZE];
u8 *p;
int r, locked = 0;
assert(card != NULL && env != NULL);
if (card->type == SC_CARD_TYPE_SETCOS_44 ||
card->type == SC_CARD_TYPE_SETCOS_NIDEL ||
SETCOS_IS_EID_APPLET(card)) {
if (env->flags & SC_SEC_ENV_KEY_REF_SYMMETRIC) {
sc_log(card->ctx, "symmetric keyref not supported.\n");
return SC_ERROR_NOT_SUPPORTED;
}
if (se_num > 0) {
sc_log(card->ctx, "restore security environment not supported.\n");
return SC_ERROR_NOT_SUPPORTED;
}
}
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x22, 0, 0);
switch (env->operation) {
case SC_SEC_OPERATION_DECIPHER:
/* Should be 0x81 */
apdu.p1 = 0x41;
apdu.p2 = 0xB8;
break;
case SC_SEC_OPERATION_SIGN:
/* Should be 0x41 */
apdu.p1 = ((card->type == SC_CARD_TYPE_SETCOS_FINEID_V2) ||
(card->type == SC_CARD_TYPE_SETCOS_FINEID_V2_2048) ||
(card->type == SC_CARD_TYPE_SETCOS_44) ||
(card->type == SC_CARD_TYPE_SETCOS_NIDEL) ||
SETCOS_IS_EID_APPLET(card)) ? 0x41 : 0x81;
apdu.p2 = 0xB6;
break;
default:
return SC_ERROR_INVALID_ARGUMENTS;
}
apdu.le = 0;
p = sbuf;
if (env->flags & SC_SEC_ENV_ALG_REF_PRESENT) {
*p++ = 0x80; /* algorithm reference */
*p++ = 0x01;
*p++ = env->algorithm_ref & 0xFF;
}
if (env->flags & SC_SEC_ENV_FILE_REF_PRESENT) {
*p++ = 0x81;
*p++ = env->file_ref.len;
memcpy(p, env->file_ref.value, env->file_ref.len);
p += env->file_ref.len;
}
if (env->flags & SC_SEC_ENV_KEY_REF_PRESENT &&
!(card->type == SC_CARD_TYPE_SETCOS_NIDEL ||
card->type == SC_CARD_TYPE_SETCOS_FINEID_V2_2048)) {
if (env->flags & SC_SEC_ENV_KEY_REF_SYMMETRIC)
*p++ = 0x83;
else
*p++ = 0x84;
*p++ = env->key_ref_len;
memcpy(p, env->key_ref, env->key_ref_len);
p += env->key_ref_len;
}
r = (int)(p - sbuf);
apdu.lc = r;
apdu.datalen = r;
apdu.data = sbuf;
apdu.resplen = 0;
if (se_num > 0) {
r = sc_lock(card);
LOG_TEST_RET(card->ctx, r, "sc_lock() failed");
locked = 1;
}
if (apdu.datalen != 0) {
r = sc_transmit_apdu(card, &apdu);
if (r) {
sc_log(card->ctx,
"%s: APDU transmit failed", sc_strerror(r));
goto err;
}
r = sc_check_sw(card, apdu.sw1, apdu.sw2);
if (r) {
sc_log(card->ctx,
"%s: Card returned error", sc_strerror(r));
goto err;
}
}
if (se_num <= 0)
return 0;
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x22, 0xF2, se_num);
r = sc_transmit_apdu(card, &apdu);
sc_unlock(card);
LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
return sc_check_sw(card, apdu.sw1, apdu.sw2);
err:
if (locked)
sc_unlock(card);
return r;
}
static int setcos_set_security_env(sc_card_t *card,
const sc_security_env_t *env, int se_num)
{
if (env->flags & SC_SEC_ENV_ALG_PRESENT) {
sc_security_env_t tmp;
tmp = *env;
tmp.flags &= ~SC_SEC_ENV_ALG_PRESENT;
tmp.flags |= SC_SEC_ENV_ALG_REF_PRESENT;
if (tmp.algorithm != SC_ALGORITHM_RSA) {
sc_log(card->ctx, "Only RSA algorithm supported.\n");
return SC_ERROR_NOT_SUPPORTED;
}
switch (card->type) {
case SC_CARD_TYPE_SETCOS_PKI:
case SC_CARD_TYPE_SETCOS_FINEID:
case SC_CARD_TYPE_SETCOS_FINEID_V2_2048:
case SC_CARD_TYPE_SETCOS_NIDEL:
case SC_CARD_TYPE_SETCOS_44:
case SC_CARD_TYPE_SETCOS_EID_V2_0:
case SC_CARD_TYPE_SETCOS_EID_V2_1:
break;
default:
sc_log(card->ctx, "Card does not support RSA.\n");
return SC_ERROR_NOT_SUPPORTED;
break;
}
tmp.algorithm_ref = 0x00;
/* potential FIXME: return an error, if an unsupported
* pad or hash was requested, although this shouldn't happen.
*/
if ((env->operation == SC_SEC_OPERATION_SIGN && env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01) ||
(env->operation == SC_SEC_OPERATION_DECIPHER && env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_02))
tmp.algorithm_ref = 0x02;
if (tmp.algorithm_flags & SC_ALGORITHM_RSA_HASH_SHA1)
tmp.algorithm_ref |= 0x10;
return setcos_set_security_env2(card, &tmp, se_num);
}
return setcos_set_security_env2(card, env, se_num);
}
static void add_acl_entry(sc_file_t *file, int op, u8 byte)
{
unsigned int method, key_ref = SC_AC_KEY_REF_NONE;
switch (byte >> 4) {
case 0:
method = SC_AC_NONE;
break;
case 1:
method = SC_AC_CHV;
key_ref = 1;
break;
case 2:
method = SC_AC_CHV;
key_ref = 2;
break;
case 4:
method = SC_AC_TERM;
break;
case 15:
method = SC_AC_NEVER;
break;
default:
method = SC_AC_UNKNOWN;
break;
}
sc_file_add_acl_entry(file, op, method, key_ref);
}
static void parse_sec_attr(sc_file_t *file, const u8 * buf, size_t len)
{
int i;
int idx[6];
if (len < 6)
return;
if (file->type == SC_FILE_TYPE_DF) {
const int df_idx[6] = {
SC_AC_OP_SELECT, SC_AC_OP_LOCK, SC_AC_OP_DELETE,
SC_AC_OP_CREATE, SC_AC_OP_REHABILITATE,
SC_AC_OP_INVALIDATE
};
for (i = 0; i < 6; i++)
idx[i] = df_idx[i];
} else {
const int ef_idx[6] = {
SC_AC_OP_READ, SC_AC_OP_UPDATE, SC_AC_OP_WRITE,
SC_AC_OP_ERASE, SC_AC_OP_REHABILITATE,
SC_AC_OP_INVALIDATE
};
for (i = 0; i < 6; i++)
idx[i] = ef_idx[i];
}
for (i = 0; i < 6; i++)
add_acl_entry(file, idx[i], buf[i]);
}
static void parse_sec_attr_44(sc_file_t *file, const u8 *buf, size_t len)
{
/* OpenSc Operation values for each command operation-type */
const int df_idx[8] = { /* byte 1 = OpenSC type of AC Bit0, byte 2 = OpenSC type of AC Bit1 ...*/
SC_AC_OP_DELETE, SC_AC_OP_CREATE, SC_AC_OP_CREATE,
SC_AC_OP_INVALIDATE, SC_AC_OP_REHABILITATE,
SC_AC_OP_LOCK, SC_AC_OP_DELETE, -1};
const int ef_idx[8] = {
SC_AC_OP_READ, SC_AC_OP_UPDATE, SC_AC_OP_WRITE,
SC_AC_OP_INVALIDATE, SC_AC_OP_REHABILITATE,
-1, SC_AC_OP_ERASE, -1};
const int efi_idx[8] = { /* internal EF used for RSA keys */
SC_AC_OP_READ, SC_AC_OP_ERASE, SC_AC_OP_UPDATE,
SC_AC_OP_INVALIDATE, SC_AC_OP_REHABILITATE,
-1, SC_AC_OP_ERASE, -1};
u8 bValue;
int i;
int iKeyRef = 0;
int iMethod;
int iPinCount;
int iOffset = 0;
int iOperation;
const int* p_idx;
/* Check all sub-AC definitions within the total AC */
while (len > 1 && (size_t)iOffset < len) { /* minimum length = 2 */
size_t iACLen = buf[iOffset] & 0x0F;
if (iACLen >= len)
break;
iMethod = SC_AC_NONE; /* default no authentication required */
if (buf[iOffset] & 0X80) { /* AC in adaptive coding */
/* Evaluates only the command-byte, not the optional P1/P2/Option bytes */
size_t iParmLen = 1; /* command-byte is always present */
size_t iKeyLen = 0; /* Encryption key is optional */
if (buf[iOffset] & 0x20) iKeyLen++;
if (buf[iOffset+1] & 0x40) iParmLen++;
if (buf[iOffset+1] & 0x20) iParmLen++;
if (buf[iOffset+1] & 0x10) iParmLen++;
if (buf[iOffset+1] & 0x08) iParmLen++;
/* Get KeyNumber if available */
if(iKeyLen) {
int iSC;
if (len < 1 + iACLen)
break;
iSC = buf[iOffset+iACLen];
switch( (iSC>>5) & 0x03 ){
case 0:
iMethod = SC_AC_TERM; /* key authentication */
break;
case 1:
iMethod = SC_AC_AUT; /* key authentication */
break;
case 2:
case 3:
iMethod = SC_AC_PRO; /* secure messaging */
break;
}
iKeyRef = iSC & 0x1F; /* get key number */
}
/* Get PinNumber if available */
if (iACLen > (1+iParmLen+iKeyLen)) { /* check via total length if pin is present */
if (len < 1+1+1+(size_t)iParmLen)
break;
iKeyRef = buf[iOffset+1+1+iParmLen]; /* PTL + AM-header + parameter-bytes */
iMethod = SC_AC_CHV;
}
/* Convert SETCOS command to OpenSC command group */
if (len < 1+2)
break;
switch(buf[iOffset+2]){
case 0x2A: /* crypto operation */
iOperation = SC_AC_OP_CRYPTO;
break;
case 0x46: /* key-generation operation */
iOperation = SC_AC_OP_UPDATE;
break;
default:
iOperation = SC_AC_OP_SELECT;
break;
}
sc_file_add_acl_entry(file, iOperation, iMethod, iKeyRef);
}
else { /* AC in simple coding */
/* Initial AC is treated as an operational AC */
/* Get specific Cmd groups for specified file-type */
switch (file->type) {
case SC_FILE_TYPE_DF: /* DF */
p_idx = df_idx;
break;
case SC_FILE_TYPE_INTERNAL_EF: /* EF for RSA keys */
p_idx = efi_idx;
break;
default: /* EF */
p_idx = ef_idx;
break;
}
/* Encryption key present ? */
iPinCount = iACLen > 0 ? (int)iACLen - 1 : 0;
if (buf[iOffset] & 0x20) {
int iSC;
if (len < 1 + (size_t)iACLen)
break;
iSC = buf[iOffset + iACLen];
switch( (iSC>>5) & 0x03 ) {
case 0:
iMethod = SC_AC_TERM; /* key authentication */
break;
case 1:
iMethod = SC_AC_AUT; /* key authentication */
break;
case 2:
case 3:
iMethod = SC_AC_PRO; /* secure messaging */
break;
}
iKeyRef = iSC & 0x1F; /* get key number */
iPinCount--; /* one byte used for keyReference */
}
/* Pin present ? */
if ( iPinCount > 0 ) {
if (len < 1 + 2)
break;
iKeyRef = buf[iOffset + 2]; /* pin ref */
iMethod = SC_AC_CHV;
}
/* Add AC for each command-operationType into OpenSc structure */
bValue = buf[iOffset + 1];
for (i = 0; i < 8; i++) {
if((bValue & 1) && (p_idx[i] >= 0))
sc_file_add_acl_entry(file, p_idx[i], iMethod, iKeyRef);
bValue >>= 1;
}
}
/* Current field treated, get next AC sub-field */
iOffset += iACLen +1; /* AC + PTL-byte */
len -= iACLen +1;
}
}
static int setcos_select_file(sc_card_t *card,
const sc_path_t *in_path, sc_file_t **file)
{
int r;
r = iso_ops->select_file(card, in_path, file);
/* Certain FINeID cards for organisations return 6A88 instead of 6A82 for missing files */
if (card->flags & _FINEID_BROKEN_SELECT_FLAG && r == SC_ERROR_DATA_OBJECT_NOT_FOUND)
return SC_ERROR_FILE_NOT_FOUND;
if (r)
return r;
if (file != NULL) {
if (card->type == SC_CARD_TYPE_SETCOS_44 ||
card->type == SC_CARD_TYPE_SETCOS_NIDEL ||
SETCOS_IS_EID_APPLET(card))
parse_sec_attr_44(*file, (*file)->sec_attr, (*file)->sec_attr_len);
else
parse_sec_attr(*file, (*file)->sec_attr, (*file)->sec_attr_len);
}
return 0;
}
static int setcos_list_files(sc_card_t *card, u8 * buf, size_t buflen)
{
sc_apdu_t apdu;
int r;
sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xAA, 0, 0);
if (card->type == SC_CARD_TYPE_SETCOS_44 ||
card->type == SC_CARD_TYPE_SETCOS_NIDEL ||
SETCOS_IS_EID_APPLET(card))
apdu.cla = 0x80;
apdu.resp = buf;
apdu.resplen = buflen;
apdu.le = buflen > 256 ? 256 : buflen;
r = sc_transmit_apdu(card, &apdu);
LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
if (card->type == SC_CARD_TYPE_SETCOS_44 && apdu.sw1 == 0x6A && apdu.sw2 == 0x82)
return 0; /* no files found */
if (apdu.resplen == 0)
return sc_check_sw(card, apdu.sw1, apdu.sw2);
return (int)apdu.resplen;
}
static int setcos_process_fci(sc_card_t *card, sc_file_t *file,
const u8 *buf, size_t buflen)
{
int r = iso_ops->process_fci(card, file, buf, buflen);
/* SetCOS 4.4: RSA key file is an internal EF but it's
* file descriptor doesn't seem to follow ISO7816. */
if (r >= 0 && (card->type == SC_CARD_TYPE_SETCOS_44 ||
SETCOS_IS_EID_APPLET(card))) {
const u8 *tag;
size_t taglen = 1;
tag = (u8 *) sc_asn1_find_tag(card->ctx, buf, buflen, 0x82, &taglen);
if (tag != NULL && taglen == 1 && *tag == 0x11)
file->type = SC_FILE_TYPE_INTERNAL_EF;
}
return r;
}
/* Write internal data, e.g. add default pin-records to pin-file */
static int setcos_putdata(struct sc_card *card, struct sc_cardctl_setcos_data_obj* data_obj)
{
int r;
struct sc_apdu apdu;
SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
memset(&apdu, 0, sizeof(apdu));
apdu.cse = SC_APDU_CASE_3_SHORT;
apdu.cla = 0x00;
apdu.ins = 0xDA;
apdu.p1 = data_obj->P1;
apdu.p2 = data_obj->P2;
apdu.lc = data_obj->DataLen;
apdu.datalen = data_obj->DataLen;
apdu.data = data_obj->Data;
r = sc_transmit_apdu(card, &apdu);