/
card-idprime.c
1224 lines (1064 loc) · 36 KB
/
card-idprime.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-idprime.c: Support for Gemalto IDPrime smart cards
*
* Copyright (c) 2019 Red Hat, Inc.
*
* Author: Jakub Jelen <jjelen@redhat.com>
*
* 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 "internal.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "cardctl.h"
#include "pkcs15.h"
static const struct sc_card_operations *iso_ops = NULL;
static struct sc_card_operations idprime_ops;
static struct sc_card_driver idprime_drv = {
"Gemalto IDPrime",
"idprime",
&idprime_ops,
NULL, 0, NULL
};
/* This ATR says, there is no EF.DIR nor EF.ATR so ISO discovery mechanisms
* are not useful here */
static const struct sc_atr_table idprime_atrs[] = {
{ "3b:7f:96:00:00:80:31:80:65:b0:84:41:3d:f6:12:0f:fe:82:90:00",
"ff:ff:00:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:00:00:ff:ff:ff",
"Gemalto IDPrime 3810",
SC_CARD_TYPE_IDPRIME_3810, 0, NULL },
{ "3b:7f:96:00:00:80:31:80:65:b0:84:56:51:10:12:0f:fe:82:90:00",
"ff:ff:00:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:00:00:ff:ff:ff",
"Gemalto IDPrime 830",
SC_CARD_TYPE_IDPRIME_830, 0, NULL },
{ "3b:7f:96:00:00:80:31:80:65:b0:84:61:60:fb:12:0f:fe:82:90:00",
"ff:ff:00:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:00:00:ff:ff:ff",
"Gemalto IDPrime 930/3930",
SC_CARD_TYPE_IDPRIME_930, 0, NULL },
{ "3b:ff:96:00:00:81:31:fe:43:80:31:80:65:b0:84:65:66:fb:12:01:78:82:90:00:85",
"ff:ff:00:ff:ff:ff:ff:00:ff:ff:ff:ff:ff:ff:00:00:00:00:ff:ff:ff:ff:ff:ff:00",
"based Gemalto IDPrime 930",
SC_CARD_TYPE_IDPRIME_930, 0, NULL },
{ "3b:7f:96:00:00:80:31:80:65:b0:85:59:56:fb:12:0f:fe:82:90:00",
"ff:ff:00:ff:ff:ff:ff:ff:ff:ff:ff:00:00:00:ff:00:00:ff:ff:ff",
"Gemalto IDPrime 940",
SC_CARD_TYPE_IDPRIME_940, 0, NULL },
{ "3b:7f:96:00:00:80:31:80:65:b0:85:05:00:39:12:0f:fe:82:90:00",
"ff:ff:00:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:00:00:ff:ff:ff",
"Gemalto IDPrime 940C",
SC_CARD_TYPE_IDPRIME_940, 0, NULL },
{ "3b:7f:96:00:00:80:31:80:65:b0:85:03:00:ef:12:0f:fe:82:90:00",
"ff:ff:00:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:00:00:ff:ff:ff",
"Gemalto IDPrime 840",
SC_CARD_TYPE_IDPRIME_840, 0, NULL },
{ "3b:7f:96:00:00:80:31:80:65:b0:84:41:3d:f6:12:0f:fe:82:90:00",
"ff:ff:00:ff:ff:ff:ff:ff:ff:ff:00:00:00:00:ff:00:00:ff:ff:ff",
"Gemalto IDPrime MD 8840, 3840, 3810, 840, 830 and MD 940 Cards",
SC_CARD_TYPE_IDPRIME_GENERIC, 0, NULL },
{ "3b:ff:96:00:00:81:31:fe:43:80:31:80:65:b0:85:59:56:fb:12:0f:fe:82:90:00:00",
"ff:ff:00:ff:ff:ff:ff:00:ff:ff:ff:ff:ff:ff:00:00:00:00:ff:ff:ff:ff:ff:ff:00",
"Gemalto IDPrime MD 8840, 3840, 3810, 840 and 830 Cards",
SC_CARD_TYPE_IDPRIME_GENERIC, 0, NULL },
{ NULL, NULL, NULL, 0, 0, NULL }
};
static const sc_path_t idprime_path = {
"", 0,
0, 0, SC_PATH_TYPE_DF_NAME,
{ "\xA0\x00\x00\x00\x18\x80\x00\x00\x00\x06\x62", 11 }
};
/* data structures to store meta data about IDPrime objects */
typedef struct idprime_object {
int fd;
int key_reference;
int valid_key_ref;
u8 df[2];
unsigned short length;
int pin_index;
} idprime_object_t;
/*
* IDPrime Container structure
* Simplification of auxiliary data from aux-data.c
*/
#define MAX_CONTAINER_NAME_LEN 39
#define CONTAINER_OBJ_LEN 86
typedef struct idprime_container {
uint8_t index; /* Index of the container */
char guid[MAX_CONTAINER_NAME_LEN + 1]; /* Container name */
} idprime_container_t;
/*
* IDPrime key reference structure
*/
#define KEYREF_OBJ_LEN 8
typedef struct idprime_keyref {
uint8_t index; /* Index of the key reference */
uint8_t pin_index; /* Index of the auth pin used for accessing key */
int key_reference; /* Key reference used for accessing key */
} idprime_keyref_t;
/*
* IDPrime private data per card state
*/
typedef struct idprime_private_data {
u8 *cache_buf; /* cached version of the currently selected file */
size_t cache_buf_len; /* length of the cached selected file */
int cached; /* is the cached selected file valid */
size_t file_size; /* this is real file size since IDPrime is quite strict about lengths */
list_t pki_list; /* list of pki containers */
idprime_object_t *pki_current; /* current pki object _ctl function */
int tinfo_present; /* Token Info Label object is present*/
u8 tinfo_df[2]; /* DF of object with Token Info Label */
unsigned long current_op; /* current operation set by idprime_set_security_env */
list_t containers; /* list of private key containers */
list_t keyrefmap; /* list of key references for private keys */
} idprime_private_data_t;
/* For SimCList autocopy, we need to know the size of the data elements */
static size_t idprime_list_meter(const void *el) {
return sizeof(idprime_object_t);
}
static size_t idprime_container_list_meter(const void *el) {
return sizeof(idprime_container_t);
}
static size_t idprime_keyref_list_meter(const void *el) {
return sizeof(idprime_keyref_t);
}
static int idprime_add_container_to_list(list_t *list, const idprime_container_t *container)
{
if (list_append(list, container) < 0)
return SC_ERROR_INTERNAL;
return SC_SUCCESS;
}
static int idprime_container_list_seeker(const void *el, const void *key)
{
const idprime_container_t *container = (idprime_container_t *)el;
if ((el == NULL) || (key == NULL))
return 0;
if (container->index == *(uint8_t *)key)
return 1;
return 0;
}
static int idprime_add_keyref_to_list(list_t *list, const idprime_keyref_t *keyref)
{
if (list_append(list, keyref) < 0)
return SC_ERROR_INTERNAL;
return SC_SUCCESS;
}
static int idprime_keyref_list_seeker(const void *el, const void *key)
{
const idprime_keyref_t *keyref = (idprime_keyref_t *)el;
if ((el == NULL) || (key == NULL))
return 0;
if (keyref->index == *(uint8_t *)key)
return 1;
return 0;
}
void idprime_free_private_data(idprime_private_data_t *priv)
{
free(priv->cache_buf);
list_destroy(&priv->pki_list);
list_destroy(&priv->containers);
list_destroy(&priv->keyrefmap);
free(priv);
return;
}
idprime_private_data_t *idprime_new_private_data(void)
{
idprime_private_data_t *priv;
priv = calloc(1, sizeof(idprime_private_data_t));
if (priv == NULL)
return NULL;
/* Initialize PKI Applets list */
if (list_init(&priv->pki_list) != 0 ||
list_attributes_copy(&priv->pki_list, idprime_list_meter, 1) != 0) {
idprime_free_private_data(priv);
return NULL;
}
/* Initialize container list */
if (list_init(&priv->containers) != 0 ||
list_attributes_copy(&priv->containers, idprime_container_list_meter, 1) != 0 ||
list_attributes_seeker(&priv->containers, idprime_container_list_seeker) != 0) {
idprime_free_private_data(priv);
return NULL;
}
/* Initialize keyref list */
if (list_init(&priv->keyrefmap) != 0 ||
list_attributes_copy(&priv->keyrefmap, idprime_keyref_list_meter, 1) != 0 ||
list_attributes_seeker(&priv->keyrefmap, idprime_keyref_list_seeker) != 0) {
idprime_free_private_data(priv);
return NULL;
}
return priv;
}
int idprime_add_object_to_list(list_t *list, const idprime_object_t *object)
{
if (list_append(list, object) < 0)
return SC_ERROR_INTERNAL;
return SC_SUCCESS;
}
/* This selects main IDPrime AID which is used for communication with
* the card */
static int idprime_select_idprime(sc_card_t *card)
{
return iso_ops->select_file(card, &idprime_path, NULL);
}
/* Select file by string path */
static int idprime_select_file_by_path(sc_card_t *card, const char *str_path)
{
int r;
sc_file_t *file = NULL;
sc_path_t index_path;
/* First, we need to make sure the IDPrime AID is selected */
r = idprime_select_idprime(card);
if (r != SC_SUCCESS) {
LOG_FUNC_RETURN(card->ctx, r);
}
/* Returns FCI with expected length of data */
sc_format_path(str_path, &index_path);
r = iso_ops->select_file(card, &index_path, &file);
if (r != SC_SUCCESS) {
LOG_FUNC_RETURN(card->ctx, r);
}
/* Ignore too large files */
if (file->size > MAX_FILE_SIZE) {
r = SC_ERROR_INVALID_DATA;
} else {
r = (int)file->size;
}
sc_file_free(file);
LOG_FUNC_RETURN(card->ctx, r);
}
static int idprime_process_containermap(sc_card_t *card, idprime_private_data_t *priv, int length)
{
u8 *buf = NULL;
int r = SC_ERROR_OUT_OF_MEMORY;
int i;
uint8_t max_entries, container_index;
SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
buf = malloc(length);
if (buf == NULL) {
goto done;
}
r = 0;
do {
/* Read at most CONTAINER_OBJ_LEN bytes */
int read_length = length - r > CONTAINER_OBJ_LEN ? CONTAINER_OBJ_LEN : length - r;
if (length == r) {
r = SC_ERROR_NOT_ENOUGH_MEMORY;
goto done;
}
const int got = iso_ops->read_binary(card, r, buf + r, read_length, 0);
if (got < 1) {
r = SC_ERROR_WRONG_LENGTH;
goto done;
}
r += got;
/* Try to read chunks of container size and stop when last container looks empty */
container_index = r > CONTAINER_OBJ_LEN ? (r / CONTAINER_OBJ_LEN - 1) * CONTAINER_OBJ_LEN : 0;
} while(length - r > 0 && buf[container_index] != 0);
max_entries = r / CONTAINER_OBJ_LEN;
for (i = 0; i < max_entries; i++) {
u8 *start = &buf[i * CONTAINER_OBJ_LEN];
idprime_container_t new_container = {0};
if (start[0] == 0) /* Empty record */
break;
new_container.index = i;
/* Reading UNICODE characters but skipping second byte */
int j = 0;
for (j = 0; j < MAX_CONTAINER_NAME_LEN; j++) {
if (start[2 * j] == 0)
break;
new_container.guid[j] = start[2 * j];
}
sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Found container with index=%d, guid=%s", new_container.index, new_container.guid);
if ((r = idprime_add_container_to_list(&priv->containers, &new_container)) != SC_SUCCESS) {
goto done;
}
}
r = SC_SUCCESS;
done:
free(buf);
LOG_FUNC_RETURN(card->ctx, r);
}
static int idprime_process_keyrefmap(sc_card_t *card, idprime_private_data_t *priv, int length)
{
u8 *buf = NULL;
int r = SC_ERROR_OUT_OF_MEMORY;
int i, max_entries;
buf = malloc(length);
if (buf == NULL) {
goto done;
}
r = 0;
do {
if (length == r) {
r = SC_ERROR_NOT_ENOUGH_MEMORY;
goto done;
}
const int got = iso_ops->read_binary(card, r, buf + r, length - r, 0);
if (got < 1) {
r = SC_ERROR_WRONG_LENGTH;
goto done;
}
r += got;
} while(length - r > 0);
max_entries = r / KEYREF_OBJ_LEN;
for (i = 0; i < max_entries; i++) {
idprime_keyref_t new_keyref;
u8 *start = &buf[i * KEYREF_OBJ_LEN];
if (start[0] == 0) /* Empty key ref */
continue;
new_keyref.index = start[2];
new_keyref.key_reference = start[1];
new_keyref.pin_index = start[7];
sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Found key reference with index=%d, pin=%d, keyref=%d", new_keyref.index, new_keyref.pin_index, new_keyref.key_reference);
if ((r = idprime_add_keyref_to_list(&priv->keyrefmap, &new_keyref)) != SC_SUCCESS) {
goto done;
}
}
r = SC_SUCCESS;
done:
free(buf);
LOG_FUNC_RETURN(card->ctx, r);
}
static int idprime_process_index(sc_card_t *card, idprime_private_data_t *priv, int length)
{
u8 *buf = NULL;
int r = SC_ERROR_OUT_OF_MEMORY;
int i, num_entries;
idprime_object_t new_object;
SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
buf = malloc(length);
if (buf == NULL) {
goto done;
}
r = 0;
do {
if (length == r) {
r = SC_ERROR_NOT_ENOUGH_MEMORY;
goto done;
}
const int got = iso_ops->read_binary(card, r, buf + r, length - r, 0);
if (got < 1) {
r = SC_ERROR_WRONG_LENGTH;
goto done;
}
/* First byte shows the number of entries, each of them 21 bytes long */
num_entries = buf[0];
r += got;
} while(r < num_entries * 21 + 1);
new_object.fd = 0;
for (i = 0; i < num_entries; i++) {
u8 *start = &buf[i*21+1];
/* First two bytes specify the object DF */
new_object.df[0] = start[0];
new_object.df[1] = start[1];
/* Second two bytes refer to the object size */
new_object.length = bebytes2ushort(&start[2]);
sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "df=%s, len=%u",
sc_dump_hex(new_object.df, sizeof(new_object.df)), new_object.length);
/* in minidriver, mscp/kxcNN or kscNN lists certificates */
if (((memcmp(&start[4], "ksc", 3) == 0) || memcmp(&start[4], "kxc", 3) == 0)
&& (memcmp(&start[12], "mscp", 5) == 0)) {
uint8_t cert_id = 0;
idprime_container_t *container = NULL;
if (start[7] >= '0' && start[7] <= '9' && start[8] >= '0' && start[8] <= '9') {
cert_id = (start[7] - '0') * 10 + start[8] - '0';
}
new_object.fd++;
new_object.key_reference = -1;
new_object.valid_key_ref = 0;
new_object.pin_index = 1;
container = (idprime_container_t *) list_seek(&priv->containers, &cert_id);
if (!container) {
/* Object is added, but missing private key */
sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "No corresponding container with private key found for certificate with id=%d", cert_id);
sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Adding certificate with fd=%d", new_object.fd);
idprime_add_object_to_list(&priv->pki_list, &new_object);
continue;
}
switch (card->type) {
case SC_CARD_TYPE_IDPRIME_3810:
new_object.key_reference = 0x31 + cert_id;
break;
case SC_CARD_TYPE_IDPRIME_830:
new_object.key_reference = 0x41 + cert_id;
break;
case SC_CARD_TYPE_IDPRIME_930:
new_object.key_reference = 0x11 + cert_id * 2;
break;
case SC_CARD_TYPE_IDPRIME_940: {
idprime_keyref_t *keyref = (idprime_keyref_t *) list_seek(&priv->keyrefmap, &cert_id);
if (!keyref) {
sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "No corresponding key reference found for certificate with id=%d, skipping", cert_id);
continue;
}
new_object.key_reference = keyref->key_reference;
new_object.pin_index = keyref->pin_index;
break;
}
case SC_CARD_TYPE_IDPRIME_840:
new_object.key_reference = 0xf7 + cert_id;
break;
default:
new_object.key_reference = 0x56 + cert_id;
break;
}
new_object.valid_key_ref = 1;
sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Found certificate with fd=%d, key_ref=%d corresponding to container \"%s\"",
new_object.fd, new_object.key_reference, container->guid);
idprime_add_object_to_list(&priv->pki_list, &new_object);
/* This looks like non-standard extension listing pkcs11 token info label in my card */
} else if ((memcmp(&start[4], "tinfo", 6) == 0) && (memcmp(&start[12], "p11", 4) == 0)) {
memcpy(priv->tinfo_df, new_object.df, sizeof(priv->tinfo_df));
priv->tinfo_present = 1;
sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Found p11/tinfo object");
} else if ((memcmp(&start[4], "cmapfile", 8) == 0) && (memcmp(&start[12], "mscp", 4) == 0)) {
sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Found mscp/cmapfile object %s",
(start[0] == 02 && start[1] == 04 ? "(already processed)" : "(in non-standard path!)"));
} else if (memcmp(&start[4], "cardapps", 8) == 0) {
sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Found cardapps object");
} else if (memcmp(&start[4], "cardid", 6) == 0) {
sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Found cardid object");
} else if (memcmp(&start[4], "cardcf", 6) == 0) {
sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Found cardcf object");
}
}
r = SC_SUCCESS;
done:
free(buf);
LOG_FUNC_RETURN(card->ctx, r);
}
/* CPLC has 42 bytes, but we get it with 3B header */
#define CPLC_LENGTH 45
static int idprime_init(sc_card_t *card)
{
int r;
unsigned long flags, ext_flags;
idprime_private_data_t *priv = NULL;
struct sc_apdu apdu;
u8 rbuf[CPLC_LENGTH];
size_t rbuflen = sizeof(rbuf);
SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
/* We need to differentiate the OS version since they behave slightly differently */
sc_format_apdu(card, &apdu, SC_APDU_CASE_2, 0xCA, 0x9F, 0x7F);
apdu.resp = rbuf;
apdu.resplen = rbuflen;
apdu.le = rbuflen;
r = sc_transmit_apdu(card, &apdu);
if (r == SC_SUCCESS && apdu.resplen == CPLC_LENGTH) {
/* We are interested in the OS release level here */
switch (rbuf[11]) {
case 0x01:
sc_log(card->ctx, "Detected IDPrime applet version 1");
break;
case 0x02:
sc_log(card->ctx, "Detected IDPrime applet version 2");
break;
case 0x03:
sc_log(card->ctx, "Detected IDPrime applet version 3");
break;
case 0x04:
sc_log(card->ctx, "Detected IDPrime applet version 4");
break;
default:
sc_log(card->ctx, "Unknown OS version received: %d", rbuf[11]);
break;
}
} else {
sc_log(card->ctx, "Failed to get CPLC data or invalid length returned, "
"err=%d, len=%"SC_FORMAT_LEN_SIZE_T"u",
r, apdu.resplen);
}
/* Proprietary data -- Applet version */
sc_format_apdu(card, &apdu, SC_APDU_CASE_2, 0xCA, 0xDF, 0x30);
apdu.resp = rbuf;
apdu.resplen = rbuflen;
apdu.le = rbuflen;
r = sc_transmit_apdu(card, &apdu);
if (r == SC_SUCCESS && apdu.resplen >= 10) {
/* Ber-TLV encoded */
if (rbuf[0] == 0xDF && rbuf[1] == 0x30 && rbuf[2] == apdu.resplen - 3) {
sc_log(card->ctx, "IDPrime Java Applet version %.*s", (int)apdu.resplen - 3, rbuf + 3);
}
}
priv = idprime_new_private_data();
if (!priv) {
LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
}
/* Select and process container file */
r = idprime_select_file_by_path(card, "0204");;
if (r <= 0) {
idprime_free_private_data(priv);
if (r == 0)
r = SC_ERROR_INVALID_DATA;
LOG_FUNC_RETURN(card->ctx, r);
}
sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Index file found");
r = idprime_process_containermap(card, priv, r);
if (r != SC_SUCCESS) {
idprime_free_private_data(priv);
LOG_FUNC_RETURN(card->ctx, r);
}
if (card->type == SC_CARD_TYPE_IDPRIME_940) {
if ((r = idprime_select_file_by_path(card, "0005")) <= 0) {
idprime_free_private_data(priv);
if (r == 0)
r = SC_ERROR_INVALID_DATA;
LOG_FUNC_RETURN(card->ctx, r);
}
if ((r = idprime_process_keyrefmap(card, priv, r)) != SC_SUCCESS) {
idprime_free_private_data(priv);
LOG_FUNC_RETURN(card->ctx, r);
}
}
/* Select and process the index file */
r = idprime_select_file_by_path(card, "0101");
if (r <= 0) {
idprime_free_private_data(priv);
if (r == 0)
r = SC_ERROR_INVALID_DATA;
LOG_FUNC_RETURN(card->ctx, r);
}
sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Index file found");
r = idprime_process_index(card, priv, r);
if (r != SC_SUCCESS) {
idprime_free_private_data(priv);
LOG_FUNC_RETURN(card->ctx, r);
}
card->drv_data = priv;
switch (card->type) {
case SC_CARD_TYPE_IDPRIME_3810:
card->name = "Gemalto IDPrime 3810";
break;
case SC_CARD_TYPE_IDPRIME_830:
card->name = "Gemalto IDPrime MD 830";
break;
case SC_CARD_TYPE_IDPRIME_930:
card->name = "Gemalto IDPrime 930/3930";
break;
case SC_CARD_TYPE_IDPRIME_940:
card->name = "Gemalto IDPrime 940";
break;
case SC_CARD_TYPE_IDPRIME_840:
card->name = "Gemalto IDPrime MD 840";
break;
case SC_CARD_TYPE_IDPRIME_GENERIC:
default:
card->name = "Gemalto IDPrime (generic)";
break;
}
card->cla = 0x00;
/* Set up algorithm info for RSA. */
flags = SC_ALGORITHM_RSA_PAD_PKCS1
| SC_ALGORITHM_RSA_PAD_PSS
| SC_ALGORITHM_RSA_PAD_OAEP
/* SHA-1 mechanisms are not allowed in the card I have */
| (SC_ALGORITHM_RSA_HASH_SHA256 | SC_ALGORITHM_RSA_HASH_SHA384 | SC_ALGORITHM_RSA_HASH_SHA512)
| (SC_ALGORITHM_MGF1_SHA256 | SC_ALGORITHM_MGF1_SHA384 | SC_ALGORITHM_MGF1_SHA512)
;
_sc_card_add_rsa_alg(card, 1024, flags, 0);
_sc_card_add_rsa_alg(card, 2048, flags, 0);
if (card->type == SC_CARD_TYPE_IDPRIME_930
|| card->type == SC_CARD_TYPE_IDPRIME_940) {
_sc_card_add_rsa_alg(card, 4096, flags, 0);
/* Set up algorithm info for EC */
flags = SC_ALGORITHM_ECDSA_RAW | SC_ALGORITHM_ECDSA_HASH_NONE;
ext_flags = SC_ALGORITHM_EXT_EC_F_P
| SC_ALGORITHM_EXT_EC_ECPARAMETERS
| SC_ALGORITHM_EXT_EC_NAMEDCURVE
| SC_ALGORITHM_EXT_EC_UNCOMPRESES
;
_sc_card_add_ec_alg(card, 256, flags, ext_flags, NULL);
_sc_card_add_ec_alg(card, 384, flags, ext_flags, NULL);
_sc_card_add_ec_alg(card, 521, flags, ext_flags, NULL);
}
card->caps |= SC_CARD_CAP_ISO7816_PIN_INFO;
card->caps |= SC_CARD_CAP_RNG;
LOG_FUNC_RETURN(card->ctx, 0);
}
static int idprime_finish(sc_card_t *card)
{
idprime_private_data_t * priv = card->drv_data;
SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
if (priv) {
idprime_free_private_data(priv);
}
return SC_SUCCESS;
}
static int idprime_match_card(sc_card_t *card)
{
int i, r;
SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
i = _sc_match_atr(card, idprime_atrs, &card->type);
if (i < 0)
return 0;
r = idprime_select_file_by_path(card, "0101");
LOG_FUNC_RETURN(card->ctx, r > 0);
}
/* initialize getting a list and return the number of elements in the list */
static int idprime_get_init_and_get_count(list_t *list, idprime_object_t **entry, int *countp)
{
if (countp == NULL || entry == NULL) {
return SC_ERROR_INVALID_ARGUMENTS;
}
*countp = list_size(list);
list_iterator_start(list);
*entry = list_iterator_next(list);
return SC_SUCCESS;
}
/* finalize the list iterator */
static int idprime_final_iterator(list_t *list)
{
list_iterator_stop(list);
return SC_SUCCESS;
}
/* fill in the prkey_info for the current object on the list and advance to the next object */
static int idprime_fill_prkey_info(list_t *list, idprime_object_t **entry, sc_pkcs15_prkey_info_t *prkey_info)
{
memset(prkey_info, 0, sizeof(sc_pkcs15_prkey_info_t));
if (*entry == NULL) {
return SC_ERROR_FILE_END_REACHED;
}
prkey_info->path.len = sizeof((*entry)->df);
memcpy(prkey_info->path.value, (*entry)->df, sizeof((*entry)->df));
prkey_info->path.type = SC_PATH_TYPE_FILE_ID;
/* Do not specify the length -- it will be read from the FCI */
prkey_info->path.count = -1;
/* TODO figure out the IDs as the original driver? */
prkey_info->id.value[0] = ((*entry)->fd >> 8) & 0xff;
prkey_info->id.value[1] = (*entry)->fd & 0xff;
prkey_info->id.len = 2;
if ((*entry)->valid_key_ref)
prkey_info->key_reference = (*entry)->key_reference;
else
prkey_info->key_reference = -1;
*entry = list_iterator_next(list);
return SC_SUCCESS;
}
/* get PIN id of the current object on the list */
static int idprime_get_pin_id(list_t *list, idprime_object_t **entry, const char **pin_id)
{
if (pin_id == NULL || entry == NULL) {
return SC_ERROR_INVALID_ARGUMENTS;
}
*pin_id = "11"; // normal PIN id
if ((*entry)->pin_index != 1)
*pin_id = "83"; // signature PIN id
return SC_SUCCESS;
}
#define IDPRIME_CARDID_LEN 16
static int idprime_get_serial(sc_card_t* card, sc_serial_number_t* serial)
{
sc_path_t cardid_path;
sc_file_t *file = NULL;
u8 buf[IDPRIME_CARDID_LEN];
int r;
LOG_FUNC_CALLED(card->ctx);
/* XXX this is assumed to be cardid for windows. It can be read from the index file */
sc_format_path("0201", &cardid_path);
r = iso_ops->select_file(card, &cardid_path, &file);
if (r != SC_SUCCESS || file->size != IDPRIME_CARDID_LEN) { /* The cardid is always 16 B */
sc_file_free(file);
LOG_FUNC_RETURN(card->ctx, SC_ERROR_WRONG_LENGTH);
}
r = iso_ops->read_binary(card, 0, buf, file->size, 0);
sc_file_free(file);
if (r < 1) {
LOG_FUNC_RETURN(card->ctx, r);
} else if (r != IDPRIME_CARDID_LEN) {
LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_DATA);
}
serial->len = MIN(IDPRIME_CARDID_LEN, SC_MAX_SERIALNR);
memcpy(serial->value, buf, serial->len);
LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
}
static int idprime_get_token_name(sc_card_t* card, char** tname)
{
idprime_private_data_t * priv = card->drv_data;
sc_path_t tinfo_path = {"\x00\x00", 2, 0, 0, SC_PATH_TYPE_PATH, {"", 0}};
sc_file_t *file = NULL;
u8 buf[2];
char *name;
int r;
LOG_FUNC_CALLED(card->ctx);
if (tname == NULL) {
LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
}
if (!priv->tinfo_present) {
LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
}
memcpy(tinfo_path.value, priv->tinfo_df, 2);
r = iso_ops->select_file(card, &tinfo_path, &file);
if (r != SC_SUCCESS || file->size == 0) {
sc_file_free(file);
LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
}
/* First two bytes lists 0x01, the second indicates length */
r = iso_ops->read_binary(card, 0, buf, 2, 0);
if (r < 2 || buf[1] > file->size) { /* make sure we do not overrun */
sc_file_free(file);
LOG_FUNC_RETURN(card->ctx, r);
}
sc_file_free(file);
name = malloc(buf[1]);
if (name == NULL) {
LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
}
r = iso_ops->read_binary(card, 2, (unsigned char *)name, buf[1], 0);
if (r < 1) {
free(name);
LOG_FUNC_RETURN(card->ctx, r);
}
if (name[r-1] != '\0') {
name[r-1] = '\0';
}
*tname = name;
LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
}
static int idprime_card_ctl(sc_card_t *card, unsigned long cmd, void *ptr)
{
idprime_private_data_t * priv = card->drv_data;
LOG_FUNC_CALLED(card->ctx);
sc_log(card->ctx, "cmd=%ld ptr=%p", cmd, ptr);
if (priv == NULL) {
LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
}
switch (cmd) {
case SC_CARDCTL_GET_SERIALNR:
return idprime_get_serial(card, (sc_serial_number_t *) ptr);
case SC_CARDCTL_IDPRIME_GET_TOKEN_NAME:
return idprime_get_token_name(card, (char **) ptr);
case SC_CARDCTL_IDPRIME_INIT_GET_OBJECTS:
return idprime_get_init_and_get_count(&priv->pki_list, &priv->pki_current,
(int *)ptr);
case SC_CARDCTL_IDPRIME_GET_NEXT_OBJECT:
return idprime_fill_prkey_info(&priv->pki_list, &priv->pki_current,
(sc_pkcs15_prkey_info_t *)ptr);
case SC_CARDCTL_IDPRIME_FINAL_GET_OBJECTS:
return idprime_final_iterator(&priv->pki_list);
case SC_CARDCTL_IDPRIME_GET_PIN_ID:
return idprime_get_pin_id(&priv->pki_list, &priv->pki_current,
(const char **)ptr);
}
LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
}
#define HEADER_LEN 4
static int idprime_select_file(sc_card_t *card, const sc_path_t *in_path, sc_file_t **file_out)
{
int r;
idprime_private_data_t * priv = card->drv_data;
SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
/* forget any old cached values */
if (priv->cache_buf) {
free(priv->cache_buf);
priv->cache_buf = NULL;
}
priv->cache_buf_len = 0;
priv->cached = 0;
r = iso_ops->select_file(card, in_path, file_out);
if (r == SC_SUCCESS && file_out != NULL) {
/* Cache the real file size for the caching read_binary() */
priv->file_size = (*file_out)->size;
}
/* Return the exit code of the select command */
return r;
}
// used to read existing certificates
static int idprime_read_binary(sc_card_t *card, unsigned int offset,
unsigned char *buf, size_t count, unsigned long *flags)
{
struct idprime_private_data *priv = card->drv_data;
int r = 0;
int size;
size_t sz;
sc_log(card->ctx, "called; %"SC_FORMAT_LEN_SIZE_T"u bytes at offset %d",
count, offset);
if (!priv->cached && offset == 0) {
/* Read what was reported by FCI from select command */
size_t left = priv->file_size;
unsigned read = 0;
// this function is called to read and uncompress the certificate
u8 buffer[SC_MAX_EXT_APDU_BUFFER_SIZE];
u8 *data_buffer = buffer;
if (sizeof(buffer) < count || sizeof(buffer) < priv->file_size) {
LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
}
while (left > 0) {
r = iso_ops->read_binary(card, read, buffer + read, priv->file_size - read, flags);
if (r <= 0) {
LOG_FUNC_RETURN(card->ctx, r);
}
left -= r;
read += r;
}
if (read < 4 || read != priv->file_size) {
LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_DATA);
}
if (buffer[0] == 1 && buffer[1] == 0) {
/* Data will be decompressed later */
data_buffer += 4;
sz = priv->file_size - 4;
if (flags)
*flags |= SC_FILE_FLAG_COMPRESSED_AUTO;
} else {
sz = priv->file_size;
}
priv->cache_buf = malloc(sz);
if (priv->cache_buf == NULL) {
return SC_ERROR_OUT_OF_MEMORY;
}
memcpy(priv->cache_buf, data_buffer, sz);
priv->cache_buf_len = sz;
priv->cached = 1;
}
if (offset >= priv->cache_buf_len) {
return 0;
}
size = (int) MIN((priv->cache_buf_len - offset), count);
memcpy(buf, priv->cache_buf + offset, size);
return size;
}
static int
idprime_set_security_env(struct sc_card *card,
const struct sc_security_env *env, int se_num)
{
int r;
struct sc_security_env new_env;
idprime_private_data_t *priv = NULL;
if (card == NULL || env == NULL) {
return SC_ERROR_INVALID_ARGUMENTS;
}
SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
priv = card->drv_data;
/* The card requires algorithm reference here */
new_env = *env;
new_env.flags |= SC_SEC_ENV_ALG_REF_PRESENT;
/* SHA-1 mechanisms are not allowed in the card I have available */
switch (env->operation) {
case SC_SEC_OPERATION_DECIPHER:
if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_OAEP) {
if (env->algorithm_flags & SC_ALGORITHM_MGF1_SHA1) {
new_env.algorithm_ref = 0x1D;
} else if (env->algorithm_flags & SC_ALGORITHM_MGF1_SHA256) {
new_env.algorithm_ref = 0x4D;
} else if (env->algorithm_flags & SC_ALGORITHM_MGF1_SHA384) {
new_env.algorithm_ref = 0x5D;
} else if (env->algorithm_flags & SC_ALGORITHM_MGF1_SHA512) {
new_env.algorithm_ref = 0x6D;
}
} else { /* RSA-PKCS without hashing */
new_env.algorithm_ref = 0x1A;
}
break;
case SC_SEC_OPERATION_SIGN:
if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PSS) {
if (env->algorithm_flags & SC_ALGORITHM_MGF1_SHA256) {
new_env.algorithm_ref = 0x45;
} else if (env->algorithm_flags & SC_ALGORITHM_MGF1_SHA384) {
new_env.algorithm_ref = 0x55;
} else if (env->algorithm_flags & SC_ALGORITHM_MGF1_SHA512) {
new_env.algorithm_ref = 0x65;