-
Notifications
You must be signed in to change notification settings - Fork 705
/
card.c
1736 lines (1440 loc) · 44.6 KB
/
card.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.c: General smart card functions
*
* Copyright (C) 2001, 2002 Juha Yrjölä <juha.yrjola@iki.fi>
*
* 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 <assert.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <string.h>
#include <limits.h>
#include "reader-tr03119.h"
#include "internal.h"
#include "asn1.h"
#include "common/compat_strlcpy.h"
#ifdef ENABLE_SM
static int sc_card_sm_load(sc_card_t *card, const char *path, const char *module);
static int sc_card_sm_unload(sc_card_t *card);
static int sc_card_sm_check(sc_card_t *card);
#endif
int sc_check_sw(sc_card_t *card, unsigned int sw1, unsigned int sw2)
{
if (card == NULL)
return SC_ERROR_INVALID_ARGUMENTS;
if (card->ops->check_sw == NULL)
return SC_ERROR_NOT_SUPPORTED;
return card->ops->check_sw(card, sw1, sw2);
}
void sc_format_apdu(sc_card_t *card, sc_apdu_t *apdu,
int cse, int ins, int p1, int p2)
{
if (card == NULL || apdu == NULL) {
return;
}
memset(apdu, 0, sizeof(*apdu));
apdu->cla = (u8) card->cla;
apdu->cse = cse;
apdu->ins = (u8) ins;
apdu->p1 = (u8) p1;
apdu->p2 = (u8) p2;
}
void sc_format_apdu_cse_lc_le(struct sc_apdu *apdu)
{
/* TODO calculating the APDU case, Lc and Le should actually only be
* done in sc_apdu2bytes, but to gradually change OpenSC we start here. */
/* Let sc_detect_apdu_cse set short or extended and test for chaining */
if (!apdu)
return;
if (apdu->datalen > SC_MAX_APDU_DATA_SIZE
|| apdu->resplen > SC_MAX_APDU_RESP_SIZE) {
/* extended length or data chaining and/or get response */
if (apdu->datalen <= SC_MAX_EXT_APDU_DATA_SIZE)
apdu->lc = apdu->datalen;
if (apdu->resplen <= SC_MAX_EXT_APDU_RESP_SIZE)
apdu->le = apdu->resplen;
if (apdu->resplen && !apdu->datalen)
apdu->cse = SC_APDU_CASE_2;
if (!apdu->resplen && apdu->datalen)
apdu->cse = SC_APDU_CASE_3;
if (apdu->resplen && apdu->datalen)
apdu->cse = SC_APDU_CASE_4;
} else {
/* short length */
if (apdu->datalen <= SC_MAX_APDU_DATA_SIZE)
apdu->lc = apdu->datalen;
if (apdu->resplen <= SC_MAX_APDU_RESP_SIZE)
apdu->le = apdu->resplen;
if (!apdu->resplen && !apdu->datalen)
apdu->cse = SC_APDU_CASE_1;
if (apdu->resplen && !apdu->datalen)
apdu->cse = SC_APDU_CASE_2_SHORT;
if (!apdu->resplen && apdu->datalen)
apdu->cse = SC_APDU_CASE_3_SHORT;
if (apdu->resplen && apdu->datalen)
apdu->cse = SC_APDU_CASE_4_SHORT;
}
}
void sc_format_apdu_ex(struct sc_apdu *apdu,
u8 cla, u8 ins, u8 p1, u8 p2,
const u8 *data, size_t datalen,
u8 *resp, size_t resplen)
{
if (!apdu) {
return;
}
memset(apdu, 0, sizeof(*apdu));
apdu->cla = cla;
apdu->ins = ins;
apdu->p1 = p1;
apdu->p2 = p2;
apdu->resp = resp;
apdu->resplen = resplen;
apdu->data = data;
apdu->datalen = datalen;
sc_format_apdu_cse_lc_le(apdu);
}
static sc_card_t * sc_card_new(sc_context_t *ctx)
{
sc_card_t *card;
if (ctx == NULL)
return NULL;
card = calloc(1, sizeof(struct sc_card));
if (card == NULL)
return NULL;
card->ops = malloc(sizeof(struct sc_card_operations));
if (card->ops == NULL) {
free(card);
return NULL;
}
card->ctx = ctx;
if (sc_mutex_create(ctx, &card->mutex) != SC_SUCCESS) {
free(card->ops);
free(card);
return NULL;
}
card->type = -1;
card->app_count = -1;
return card;
}
static void sc_card_free(sc_card_t *card)
{
sc_free_apps(card);
sc_free_ef_atr(card);
free(card->ops);
if (card->algorithms != NULL) {
int i;
for (i=0; i<card->algorithm_count; i++) {
struct sc_algorithm_info *info = (card->algorithms + i);
if (info->algorithm == SC_ALGORITHM_EC) {
struct sc_ec_parameters ep = info->u._ec.params;
free(ep.named_curve);
free(ep.der.value);
}
}
free(card->algorithms);
card->algorithms = NULL;
card->algorithm_count = 0;
}
sc_file_free(card->cache.current_ef);
sc_file_free(card->cache.current_df);
if (card->mutex != NULL) {
int r = sc_mutex_destroy(card->ctx, card->mutex);
if (r != SC_SUCCESS)
sc_log(card->ctx, "unable to destroy mutex");
}
sc_mem_clear(card, sizeof(*card));
free(card);
}
size_t sc_get_max_recv_size(const sc_card_t *card)
{
size_t max_recv_size;
if (card == NULL || card->reader == NULL) {
return 0;
}
max_recv_size = card->max_recv_size;
/* initialize max_recv_size to a meaningful value */
if (card->caps & SC_CARD_CAP_APDU_EXT) {
if (!max_recv_size)
max_recv_size = 65536;
} else {
if (!max_recv_size)
max_recv_size = 256;
}
/* Override card limitations with reader limitations. */
if (card->reader->max_recv_size != 0
&& (card->reader->max_recv_size < card->max_recv_size))
max_recv_size = card->reader->max_recv_size;
return max_recv_size;
}
size_t sc_get_max_send_size(const sc_card_t *card)
{
size_t max_send_size;
if (card == NULL || card->reader == NULL) {
return 0;
}
max_send_size = card->max_send_size;
/* initialize max_send_size to a meaningful value */
if (card->caps & SC_CARD_CAP_APDU_EXT
&& card->reader->active_protocol != SC_PROTO_T0) {
if (!max_send_size)
max_send_size = 65535;
} else {
if (!max_send_size)
max_send_size = 255;
}
/* Override card limitations with reader limitations. */
if (card->reader->max_send_size != 0
&& (card->reader->max_send_size < card->max_send_size))
max_send_size = card->reader->max_send_size;
return max_send_size;
}
int sc_connect_card(sc_reader_t *reader, sc_card_t **card_out)
{
sc_card_t *card;
sc_context_t *ctx;
struct sc_card_driver *driver;
int i, r = 0, idx, connected = 0;
if (card_out == NULL || reader == NULL)
return SC_ERROR_INVALID_ARGUMENTS;
ctx = reader->ctx;
SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_VERBOSE);
if (reader->ops->connect == NULL)
LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_SUPPORTED);
card = sc_card_new(ctx);
if (card == NULL)
LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY);
r = reader->ops->connect(reader);
if (r)
goto err;
connected = 1;
card->reader = reader;
card->ctx = ctx;
if (reader->flags & SC_READER_ENABLE_ESCAPE)
sc_detect_escape_cmds(reader);
memcpy(&card->atr, &reader->atr, sizeof(card->atr));
memcpy(&card->uid, &reader->uid, sizeof(card->uid));
_sc_parse_atr(reader);
/* See if the ATR matches any ATR specified in the config file */
if ((driver = ctx->forced_driver) == NULL) {
sc_log(ctx, "matching configured ATRs");
for (i = 0; ctx->card_drivers[i] != NULL; i++) {
driver = ctx->card_drivers[i];
if (driver->atr_map == NULL ||
!strcmp(driver->short_name, "default")) {
driver = NULL;
continue;
}
sc_log(ctx, "trying driver '%s'", driver->short_name);
idx = _sc_match_atr(card, driver->atr_map, NULL);
if (idx >= 0) {
struct sc_atr_table *src = &driver->atr_map[idx];
sc_log(ctx, "matched driver '%s'", driver->name);
/* It's up to card driver to notice these correctly */
card->name = src->name;
card->type = src->type;
card->flags = src->flags;
break;
}
driver = NULL;
}
}
if (driver != NULL) {
/* Forced driver, or matched via ATR mapping from config file */
card->driver = driver;
memcpy(card->ops, card->driver->ops, sizeof(struct sc_card_operations));
if (card->ops->match_card != NULL)
if (card->ops->match_card(card) != 1)
sc_log(ctx, "driver '%s' match_card() failed: %s (will continue anyway)", card->driver->name, sc_strerror(r));
if (card->ops->init != NULL) {
r = card->ops->init(card);
if (r) {
sc_log(ctx, "driver '%s' init() failed: %s", card->driver->name, sc_strerror(r));
goto err;
}
}
}
else {
sc_card_t uninitialized = *card;
sc_log(ctx, "matching built-in ATRs");
for (i = 0; ctx->card_drivers[i] != NULL; i++) {
/* FIXME If we had a clean API description, we'd probably get a
* cleaner implementation of the driver's match_card and init,
* which should normally *not* modify the card object if
* unsuccessful. However, after years of relentless hacking, reality
* is different: The card object is changed in virtually every card
* driver so in order to prevent unwanted interaction, we reset the
* card object here and hope that the card driver at least doesn't
* allocate any internal resources that need to be freed. If we
* had more time, we should refactor the existing code to not
* modify sc_card_t until complete success (possibly by combining
* `match_card()` and `init()`) */
*card = uninitialized;
struct sc_card_driver *drv = ctx->card_drivers[i];
const struct sc_card_operations *ops = drv->ops;
sc_log(ctx, "trying driver '%s'", drv->short_name);
if (ops == NULL || ops->match_card == NULL) {
continue;
}
else if (!(ctx->flags & SC_CTX_FLAG_ENABLE_DEFAULT_DRIVER)
&& !strcmp("default", drv->short_name)) {
sc_log(ctx , "ignore 'default' card driver");
continue;
}
/* Needed if match_card() needs to talk with the card (e.g. card-muscle) */
*card->ops = *ops;
if (ops->match_card(card) != 1)
continue;
sc_log(ctx, "matched: %s", drv->name);
memcpy(card->ops, ops, sizeof(struct sc_card_operations));
card->driver = drv;
r = ops->init(card);
if (r) {
sc_log(ctx, "driver '%s' init() failed: %s", drv->name, sc_strerror(r));
if (r == SC_ERROR_INVALID_CARD) {
card->driver = NULL;
continue;
}
goto err;
}
break;
}
}
if (card->driver == NULL) {
sc_log(ctx, "unable to find driver for inserted card");
r = SC_ERROR_INVALID_CARD;
goto err;
}
if (card->name == NULL)
card->name = card->driver->name;
/* initialize max_send_size/max_recv_size to a meaningful value */
card->max_recv_size = sc_get_max_recv_size(card);
card->max_send_size = sc_get_max_send_size(card);
sc_log(ctx,
"card info name:'%s', type:%i, flags:0x%lX, max_send/recv_size:%"SC_FORMAT_LEN_SIZE_T"u/%"SC_FORMAT_LEN_SIZE_T"u",
card->name, card->type, card->flags, card->max_send_size,
card->max_recv_size);
#ifdef ENABLE_SM
/* Check, if secure messaging module present. */
r = sc_card_sm_check(card);
if (r) {
sc_log(ctx, "cannot load secure messaging module");
goto err;
}
#endif
*card_out = card;
LOG_FUNC_RETURN(ctx, SC_SUCCESS);
err:
if (connected)
reader->ops->disconnect(reader);
if (card != NULL)
sc_card_free(card);
LOG_FUNC_RETURN(ctx, r);
}
int sc_disconnect_card(sc_card_t *card)
{
sc_context_t *ctx;
if (!card)
return SC_ERROR_INVALID_ARGUMENTS;
ctx = card->ctx;
LOG_FUNC_CALLED(ctx);
if (card->ops->finish) {
int r = card->ops->finish(card);
if (r)
sc_log(ctx, "card driver finish() failed: %s", sc_strerror(r));
}
if (card->reader->ops->disconnect) {
int r = card->reader->ops->disconnect(card->reader);
if (r)
sc_log(ctx, "disconnect() failed: %s", sc_strerror(r));
}
#ifdef ENABLE_SM
/* release SM related resources */
sc_card_sm_unload(card);
#endif
sc_card_free(card);
LOG_FUNC_RETURN(ctx, SC_SUCCESS);
}
int sc_reset(sc_card_t *card, int do_cold_reset)
{
int r, r2;
if (card == NULL)
return SC_ERROR_INVALID_ARGUMENTS;
if (card->reader->ops->reset == NULL)
return SC_ERROR_NOT_SUPPORTED;
r = sc_mutex_lock(card->ctx, card->mutex);
if (r != SC_SUCCESS)
return r;
r = card->reader->ops->reset(card->reader, do_cold_reset);
sc_invalidate_cache(card);
r2 = sc_mutex_unlock(card->ctx, card->mutex);
if (r2 != SC_SUCCESS) {
sc_log(card->ctx, "unable to release lock");
r = r != SC_SUCCESS ? r : r2;
}
return r;
}
int sc_lock(sc_card_t *card)
{
int r = 0, r2 = 0;
int was_reset = 0;
int reader_lock_obtained = 0;
if (card == NULL)
return SC_ERROR_INVALID_ARGUMENTS;
LOG_FUNC_CALLED(card->ctx);
r = sc_mutex_lock(card->ctx, card->mutex);
if (r != SC_SUCCESS)
return r;
if (card->lock_count == 0) {
if (card->reader->ops->lock != NULL) {
r = card->reader->ops->lock(card->reader);
while (r == SC_ERROR_CARD_RESET || r == SC_ERROR_READER_REATTACHED) {
sc_invalidate_cache(card);
if (was_reset++ > 4) /* TODO retry a few times */
break;
r = card->reader->ops->lock(card->reader);
}
if (r == 0)
reader_lock_obtained = 1;
}
if (r == 0)
card->cache.valid = 1;
}
if (r == 0)
card->lock_count++;
r2 = sc_mutex_unlock(card->ctx, card->mutex);
if (r2 != SC_SUCCESS) {
sc_log(card->ctx, "unable to release card->mutex lock");
r = r != SC_SUCCESS ? r : r2;
}
if (r == 0 && was_reset > 0) {
#ifdef ENABLE_SM
if (card->sm_ctx.ops.open)
card->sm_ctx.ops.open(card);
#endif
}
/* give card driver a chance to do something when reader lock first obtained */
if (r == 0 && reader_lock_obtained == 1 && card->ops->card_reader_lock_obtained) {
if (SC_SUCCESS != card->ops->card_reader_lock_obtained(card, was_reset))
sc_log(card->ctx, "card_reader_lock_obtained failed");
}
LOG_FUNC_RETURN(card->ctx, r);
}
int sc_unlock(sc_card_t *card)
{
int r, r2;
if (!card)
return SC_ERROR_INVALID_ARGUMENTS;
LOG_FUNC_CALLED(card->ctx);
r = sc_mutex_lock(card->ctx, card->mutex);
if (r != SC_SUCCESS)
LOG_FUNC_RETURN(card->ctx, r);
if (card->lock_count < 1) {
LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
}
if (--card->lock_count == 0) {
if (card->flags & SC_CARD_FLAG_KEEP_ALIVE) {
/* Multiple processes accessing the card will most likely render
* the card cache useless. To not have a bad cache, we explicitly
* invalidate it. */
sc_invalidate_cache(card);
}
/* release reader lock */
if (card->reader->ops->unlock != NULL)
r = card->reader->ops->unlock(card->reader);
}
r2 = sc_mutex_unlock(card->ctx, card->mutex);
if (r2 != SC_SUCCESS) {
sc_log(card->ctx, "unable to release lock");
r = (r == SC_SUCCESS) ? r2 : r;
}
return r;
}
int sc_list_files(sc_card_t *card, u8 *buf, size_t buflen)
{
int r;
if (card == NULL) {
return SC_ERROR_INVALID_ARGUMENTS;
}
LOG_FUNC_CALLED(card->ctx);
if (card->ops->list_files == NULL)
LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
r = card->ops->list_files(card, buf, buflen);
LOG_FUNC_RETURN(card->ctx, r);
}
int sc_create_file(sc_card_t *card, sc_file_t *file)
{
int r;
char pbuf[SC_MAX_PATH_STRING_SIZE];
const sc_path_t *in_path;
if (card == NULL || file == NULL) {
return SC_ERROR_INVALID_ARGUMENTS;
}
in_path = &file->path;
r = sc_path_print(pbuf, sizeof(pbuf), in_path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_log(card->ctx,
"called; type=%d, path=%s, id=%04i, size=%"SC_FORMAT_LEN_SIZE_T"u",
in_path->type, pbuf, file->id, file->size);
/* ISO 7816-4: "Number of data bytes in the file, including structural information if any"
* can not be bigger than two bytes */
if (file->size > 0xFFFF)
LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
if (card->ops->create_file == NULL)
LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
r = card->ops->create_file(card, file);
LOG_FUNC_RETURN(card->ctx, r);
}
int sc_delete_file(sc_card_t *card, const sc_path_t *path)
{
int r;
char pbuf[SC_MAX_PATH_STRING_SIZE];
if (card == NULL) {
return SC_ERROR_INVALID_ARGUMENTS;
}
r = sc_path_print(pbuf, sizeof(pbuf), path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_log(card->ctx, "called; type=%d, path=%s", path->type, pbuf);
if (card->ops->delete_file == NULL)
LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
r = card->ops->delete_file(card, path);
LOG_FUNC_RETURN(card->ctx, r);
}
int sc_read_binary(sc_card_t *card, unsigned int idx,
unsigned char *buf, size_t count, unsigned long *flags)
{
size_t max_le = sc_get_max_recv_size(card);
size_t todo = count;
int r;
if (card == NULL || card->ops == NULL || buf == NULL) {
return SC_ERROR_INVALID_ARGUMENTS;
}
sc_log(card->ctx, "called; %"SC_FORMAT_LEN_SIZE_T"u bytes at index %d",
count, idx);
if (count == 0)
LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
#ifdef ENABLE_SM
if (card->sm_ctx.ops.read_binary) {
r = card->sm_ctx.ops.read_binary(card, idx, buf, count);
if (r)
LOG_FUNC_RETURN(card->ctx, r);
}
#endif
if (card->ops->read_binary == NULL)
LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
/* lock the card now to avoid deselection of the file */
r = sc_lock(card);
LOG_TEST_RET(card->ctx, r, "sc_lock() failed");
while (todo > 0) {
size_t chunk = MIN(todo, max_le);
r = card->ops->read_binary(card, idx, buf, chunk, flags);
if (r == 0 || r == SC_ERROR_FILE_END_REACHED)
break;
if (r < 0 && todo != count) {
/* the last command failed, but previous ones succeeded.
* Let's just return what we've successfully read. */
sc_log(card->ctx, "Subsequent read failed with %d, returning what was read successfully.", r);
break;
}
if (r < 0) {
sc_unlock(card);
LOG_FUNC_RETURN(card->ctx, r);
}
if ((idx > SIZE_MAX - (size_t) r) || (size_t) r > todo) {
/* `idx + r` or `todo - r` would overflow */
sc_unlock(card);
LOG_FUNC_RETURN(card->ctx, SC_ERROR_OFFSET_TOO_LARGE);
}
todo -= (size_t) r;
buf += (size_t) r;
idx += (size_t) r;
}
sc_unlock(card);
LOG_FUNC_RETURN(card->ctx, (int)(count - todo));
}
int sc_write_binary(sc_card_t *card, unsigned int idx,
const u8 *buf, size_t count, unsigned long flags)
{
size_t max_lc = sc_get_max_send_size(card);
size_t todo = count;
int r;
if (card == NULL || card->ops == NULL || buf == NULL) {
return SC_ERROR_INVALID_ARGUMENTS;
}
sc_log(card->ctx, "called; %"SC_FORMAT_LEN_SIZE_T"u bytes at index %d",
count, idx);
if (count == 0)
LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
if (card->ops->write_binary == NULL)
LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
/* lock the card now to avoid deselection of the file */
r = sc_lock(card);
LOG_TEST_RET(card->ctx, r, "sc_lock() failed");
while (todo > 0) {
size_t chunk = MIN(todo, max_lc);
r = card->ops->write_binary(card, idx, buf, chunk, flags);
if (r == 0 || r == SC_ERROR_FILE_END_REACHED)
break;
if (r < 0) {
sc_unlock(card);
LOG_FUNC_RETURN(card->ctx, r);
}
if ((idx > SIZE_MAX - (size_t) r) || (size_t) r > todo) {
/* `idx + r` or `todo - r` would overflow */
sc_unlock(card);
LOG_FUNC_RETURN(card->ctx, SC_ERROR_OFFSET_TOO_LARGE);
}
todo -= (size_t) r;
buf += (size_t) r;
idx += (size_t) r;
}
sc_unlock(card);
LOG_FUNC_RETURN(card->ctx, (int)(count - todo));
}
int sc_update_binary(sc_card_t *card, unsigned int idx,
const u8 *buf, size_t count, unsigned long flags)
{
size_t max_lc = sc_get_max_send_size(card);
size_t todo = count;
int r;
if (card == NULL || card->ops == NULL || buf == NULL) {
return SC_ERROR_INVALID_ARGUMENTS;
}
sc_log(card->ctx, "called; %"SC_FORMAT_LEN_SIZE_T"u bytes at index %d",
count, idx);
if (count == 0)
LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
#ifdef ENABLE_SM
if (card->sm_ctx.ops.update_binary) {
r = card->sm_ctx.ops.update_binary(card, idx, buf, count);
if (r)
LOG_FUNC_RETURN(card->ctx, r);
}
#endif
if (card->ops->update_binary == NULL)
LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
/* lock the card now to avoid deselection of the file */
r = sc_lock(card);
LOG_TEST_RET(card->ctx, r, "sc_lock() failed");
while (todo > 0) {
size_t chunk = MIN(todo, max_lc);
r = card->ops->update_binary(card, idx, buf, chunk, flags);
if (r == 0 || r == SC_ERROR_FILE_END_REACHED)
break;
if (r < 0) {
sc_unlock(card);
LOG_FUNC_RETURN(card->ctx, r);
}
if ((idx > SIZE_MAX - (size_t) r) || (size_t) r > todo) {
/* `idx + r` or `todo - r` would overflow */
sc_unlock(card);
LOG_FUNC_RETURN(card->ctx, SC_ERROR_OFFSET_TOO_LARGE);
}
todo -= (size_t) r;
buf += (size_t) r;
idx += (size_t) r;
}
sc_unlock(card);
LOG_FUNC_RETURN(card->ctx, (int)(count - todo));
}
int sc_erase_binary(struct sc_card *card, unsigned int idx, size_t count, unsigned long flags)
{
int r;
size_t todo = count;
if (card == NULL || card->ops == NULL) {
return SC_ERROR_INVALID_ARGUMENTS;
}
sc_log(card->ctx,
"called; erase %"SC_FORMAT_LEN_SIZE_T"u bytes from offset %d",
count, idx);
if (count == 0)
LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
if (card->ops->erase_binary == NULL)
LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
/* lock the card now to avoid deselection of the file */
r = sc_lock(card);
LOG_TEST_RET(card->ctx, r, "sc_lock() failed");
while (todo > 0) {
r = card->ops->erase_binary(card, idx, todo, flags);
if (r == 0 || r == SC_ERROR_FILE_END_REACHED)
break;
if (r < 0) {
sc_unlock(card);
LOG_FUNC_RETURN(card->ctx, r);
}
if ((idx > SIZE_MAX - (size_t) r) || (size_t) r > todo) {
/* `idx + r` or `todo - r` would overflow */
sc_unlock(card);
LOG_FUNC_RETURN(card->ctx, SC_ERROR_OFFSET_TOO_LARGE);
}
todo -= (size_t) r;
idx += (size_t) r;
}
sc_unlock(card);
LOG_FUNC_RETURN(card->ctx, (int)(count - todo));
}
int sc_select_file(sc_card_t *card, const sc_path_t *in_path, sc_file_t **file)
{
int r;
char pbuf[SC_MAX_PATH_STRING_SIZE];
if (card == NULL || in_path == NULL) {
return SC_ERROR_INVALID_ARGUMENTS;
}
r = sc_path_print(pbuf, sizeof(pbuf), in_path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
/* FIXME We should be a bit less strict and let the upper layers do
* the initialization (including reuse of existing file objects). We
* implemented this here because we are lazy. */
if (file != NULL)
*file = NULL;
sc_log(card->ctx, "called; type=%d, path=%s", in_path->type, pbuf);
if (in_path->len > SC_MAX_PATH_SIZE)
LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
if (in_path->type == SC_PATH_TYPE_PATH) {
/* Perform a sanity check */
size_t i;
if ((in_path->len & 1) != 0)
LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
for (i = 0; i < in_path->len/2; i++) {
u8 p1 = in_path->value[2*i],
p2 = in_path->value[2*i+1];
if ((p1 == 0x3F && p2 == 0x00) && i != 0)
LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
}
}
if (card->ops->select_file == NULL)
LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
r = card->ops->select_file(card, in_path, file);
LOG_TEST_RET(card->ctx, r, "'SELECT' error");
if (file) {
if (*file)
/* Remember file path */
(*file)->path = *in_path;
else
/* FIXME We should be a bit less strict and let the upper layers do
* the error checking. We implemented this here because we are
* lazy. */
r = SC_ERROR_INVALID_DATA;
}
LOG_FUNC_RETURN(card->ctx, r);
}
int sc_get_data(sc_card_t *card, unsigned int tag, u8 *buf, size_t len)
{
int r;
sc_log(card->ctx, "called, tag=%04x", tag);
if (card->ops->get_data == NULL)
LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
r = card->ops->get_data(card, tag, buf, len);
LOG_FUNC_RETURN(card->ctx, r);
}
int sc_put_data(sc_card_t *card, unsigned int tag, const u8 *buf, size_t len)
{
int r;
sc_log(card->ctx,"called, tag=%04x", tag);
if (card->ops->put_data == NULL)
LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
r = card->ops->put_data(card, tag, buf, len);
LOG_FUNC_RETURN(card->ctx, r);
}
int sc_get_challenge(sc_card_t *card, u8 *rnd, size_t len)
{
int r;
if (len == 0)
return SC_SUCCESS;
if (card == NULL || rnd == NULL) {
return SC_ERROR_INVALID_ARGUMENTS;
}
LOG_FUNC_CALLED(card->ctx);
if (card->ops == NULL || card->ops->get_challenge == NULL)
LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
r = sc_lock(card);
if (r != SC_SUCCESS)
LOG_FUNC_RETURN(card->ctx, r);
while (len > 0) {
r = card->ops->get_challenge(card, rnd, len);
if (r == 0)
r = SC_ERROR_INVALID_DATA;
if (r < 0) {
sc_unlock(card);
LOG_FUNC_RETURN(card->ctx, r);
}
rnd += (size_t) r;
len -= (size_t) r;
}
sc_unlock(card);
LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
}
int sc_read_record(sc_card_t *card, unsigned int rec_nr, unsigned int idx,
u8 *buf ,size_t count, unsigned long flags)
{
size_t max_le = sc_get_max_recv_size(card);
size_t todo = count;
int r;
if (card == NULL || card->ops == NULL || buf == NULL) {
return SC_ERROR_INVALID_ARGUMENTS;
}
LOG_FUNC_CALLED(card->ctx);
if (count == 0)
LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
if (card->ops->read_record == NULL)
LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
/* lock the card now to avoid deselection of the file */
r = sc_lock(card);
LOG_TEST_RET(card->ctx, r, "sc_lock() failed");
while (todo > 0) {
size_t chunk = MIN(todo, max_le);
r = card->ops->read_record(card, rec_nr, idx, buf, chunk, flags);
if (r == 0 || r == SC_ERROR_FILE_END_REACHED)
break;
if (r < 0 && todo != count) {
/* the last command failed, but previous ones succeeded.
* Let's just return what we've successfully read. */
sc_log(card->ctx, "Subsequent read failed with %d, returning what was read successfully.", r);
break;
}
if (r < 0) {
sc_unlock(card);
LOG_FUNC_RETURN(card->ctx, r);
}
if ((idx > SIZE_MAX - (size_t) r) || (size_t) r > todo) {
/* `idx + r` or `todo - r` would overflow */
sc_unlock(card);
LOG_FUNC_RETURN(card->ctx, SC_ERROR_OFFSET_TOO_LARGE);
}
todo -= (size_t) r;
buf += (size_t) r;
idx += (size_t) r;
}
sc_unlock(card);