-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmartcard.c
570 lines (523 loc) · 14.1 KB
/
smartcard.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
#include "libc/stdio.h"
#include "libc/nostd.h"
#include "libiso7816.h"
/* TODO: add ISO14443 layer header here when it is implemented */
#include "api/libsmartcard.h"
/* Handling the debug output */
#include "smartcard_print.h"
/*** The following two functions help APDU fragmentation across multiple
* units (envelopes in T=0 and blocks in T=1, blocks for ISO14443 card, ...).
*/
/* Prepare the buffer to send */
unsigned int SC_APDU_get_encapsulated_apdu_size(SC_APDU_cmd *apdu, unsigned int *out_apdu_lc_size, unsigned int *out_apdu_le_size){
unsigned int apdu_size, apdu_lc_size, apdu_le_size;
if(apdu == NULL){
return 0;
}
/* Compute the APDU size */
if(apdu->send_le != 2){
/* Short APDU case for Lc */
apdu_lc_size = ((apdu->lc <= SHORT_APDU_LC_MAX) ? ((apdu->lc == 0) ? 0 : 1) : 3);
}
else{
/* Extended APDU case for Lc (forced whatever Lc size is) */
apdu_lc_size = (apdu->lc == 0) ? 0 : 3;
}
if(apdu->send_le){
/* apdu->send_le = 1 means short APDU encoding except if the size exceeds 256.
* apdu->send_le = 2 means extended APDU encoding for Le.
*/
if((apdu->le <= SHORT_APDU_LE_MAX) && (apdu->send_le == 1)){
apdu_le_size = 1;
}
else{
if(apdu_lc_size != 0){
apdu_le_size = 2;
}
else{
apdu_le_size = 3;
}
}
}
else{
apdu_le_size = 0;
}
apdu_size = 4 + apdu_lc_size + apdu->lc + apdu_le_size;
if(out_apdu_lc_size != NULL){
*out_apdu_lc_size = apdu_lc_size;
}
if(out_apdu_le_size != NULL){
*out_apdu_le_size = apdu_le_size;
}
return apdu_size;
}
uint8_t SC_APDU_prepare_buffer(SC_APDU_cmd *apdu, uint8_t *buffer, unsigned int i, uint8_t block_size, int *ret){
unsigned int apdu_size, apdu_lc_size, apdu_le_size;
unsigned int to_push, offset;
unsigned int size = 0;
*ret = 0;
/* Sanity checks on the lengths */
if(apdu->le > ((uint32_t)0x1 << 16)){
/* Absolute limits for Lc and Le (Lc is uint16, no need to check) */
*ret = -1;
return 0;
}
if((apdu->lc > APDU_MAX_BUFF_LEN) || (apdu->le > APDU_MAX_BUFF_LEN)){
*ret = -1;
return 0;
}
/* Compute the APDU size */
apdu_size = SC_APDU_get_encapsulated_apdu_size(apdu, &apdu_lc_size, &apdu_le_size);
/* Sanity checks */
if(apdu_size < (i * block_size)){
*ret = -1;
return 0;
}
if(apdu_size > (4 + APDU_MAX_BUFF_LEN + (2 * 3))){
*ret = -1;
return 0;
}
/* Size to push */
to_push = ((apdu_size - (i * block_size)) > block_size) ? block_size : (apdu_size - (i * block_size));
/* Sanity check on the pushed size.
* Not necessary with the previous formula, but better safe than sorry.
*/
if(to_push > block_size){
/* Overlow in buffer */
*ret = -1;
return 0;
}
/* Put as much data as we can in the buffer */
offset = i * block_size; /* offset where we begin */
size = 0;
while(size < to_push){
if((size >= block_size) || (size >= 256)){
/* Sanity check: our buffer should not exceed 256 bytes long anyways ... */
*ret = -1;
return 0;
}
/* Do we have to push CLA, IN, P1, P2? */
if(offset == 0){
if(size >= block_size){
/* Overflow ... this is an error */
*ret = -1;
return 0;
}
buffer[size++] = apdu->cla;
offset++;
continue;
}
if(offset == 1){
if(size >= block_size){
/* Overflow ... this is an error */
*ret = -1;
return 0;
}
buffer[size++] = apdu->ins;
offset++;
continue;
}
if(offset == 2){
if(size >= block_size){
/* Overflow ... this is an error */
*ret = -1;
return 0;
}
buffer[size++] = apdu->p1;
offset++;
continue;
}
if(offset == 3){
if(size >= block_size){
/* Overflow ... this is an error */
*ret = -1;
return 0;
}
buffer[size++] = apdu->p2;
offset++;
continue;
}
/* Handle Lc */
if((offset >= 4) && (offset < (apdu_size - apdu_le_size))){
if(apdu->lc != 0){
if((apdu->lc <= SHORT_APDU_LC_MAX) && (apdu->send_le != 2)){
/* Sanity check: Lc is encoded on 1 byte */
if(apdu_lc_size != 1){
*ret = -1;
return 0;
}
if(offset == 4){
if(size >= block_size){
/* Overflow ... this is an error */
*ret = -1;
return 0;
}
buffer[size++] = apdu->lc;
offset++;
continue;
}
if(offset > 4){
if((offset-5) >= APDU_MAX_BUFF_LEN){
/* Overflow ... this is an error */
*ret = -1;
return 0;
}
if(size >= block_size){
/* Overflow ... this is an error */
*ret = -1;
return 0;
}
buffer[size++] = apdu->data[offset-5];
offset++;
continue;
}
}
else{
/* Sanity check: Lc is encoded on 3 bytes */
if(apdu_lc_size != 3){
*ret = -1;
return 0;
}
if(offset == 4){
if(size >= block_size){
/* Overflow ... this is an error */
*ret = -1;
return 0;
}
buffer[size++] = 0;
offset++;
continue;
}
if(offset == 5){
if(size >= block_size){
/* Overflow ... this is an error */
*ret = -1;
return 0;
}
buffer[size++] = (apdu->lc >> 8) & 0xff;
offset++;
continue;
}
if(offset == 6){
if(size >= block_size){
/* Overflow ... this is an error */
*ret = -1;
return 0;
}
buffer[size++] = apdu->lc & 0xff;
offset++;
continue;
}
if(offset > 6){
if((offset-7) >= APDU_MAX_BUFF_LEN){
/* Overflow ... this is an error */
*ret = -1;
return 0;
}
if(size >= block_size){
/* Overflow ... this is an error */
*ret = -1;
return 0;
}
buffer[size++] = apdu->data[offset-7];
offset++;
continue;
}
}
}
}
/* Handle Le */
if(apdu->send_le){
if(offset >= (apdu_size - apdu_le_size)){
if(apdu_le_size == 1){
if(offset == (apdu_size-1)){
if(size >= block_size){
/* Overflow ... this is an error */
*ret = -1;
return 0;
}
buffer[size++] = apdu->le;
offset++;
continue;
}
}
if(apdu_le_size == 2){
if(offset == (apdu_size-2)){
if(size >= block_size){
/* Overflow ... this is an error */
*ret = -1;
return 0;
}
buffer[size++] = (apdu->le >> 8) & 0xff;
offset++;
continue;
}
if(offset == (apdu_size-1)){
if(size >= block_size){
/* Overflow ... this is an error */
*ret = -1;
return 0;
}
buffer[size++] = apdu->le & 0xff;
offset++;
continue;
}
}
if(apdu_le_size == 3){
if(offset == (apdu_size-3)){
if(size >= block_size){
/* Overflow ... this is an error */
*ret = -1;
return 0;
}
buffer[size++] = 0x00;
offset++;
continue;
}
if(offset == (apdu_size-2)){
if(size >= block_size){
/* Overflow ... this is an error */
*ret = -1;
return 0;
}
buffer[size++] = (apdu->le >> 8) & 0xff;
offset++;
continue;
}
if(offset == (apdu_size-1)){
if(size >= block_size){
/* Overflow ... this is an error */
*ret = -1;
return 0;
}
buffer[size++] = apdu->le & 0xff;
offset++;
continue;
}
}
}
}
}
return to_push;
}
/* Print an APDU on the console */
void SC_print_APDU(SC_APDU_cmd *apdu)
{
if(apdu == NULL){
return;
}
/* Sanity check on length */
if(apdu->lc > APDU_MAX_BUFF_LEN){
log_printf("[Smartcard] SC_print_APDU error: length %d overflow\n", apdu->lc);
return;
}
log_printf("===== APDU ============\n");
log_printf("CLA = %x, INS = %x, P1 = %x, P2 = %x\n", apdu->cla, apdu->ins, apdu->p1, apdu->p2);
if(apdu->lc != 0){
log_printf(", Lc = %x\n", apdu->lc);
if(apdu->lc > 255){
log_printf(" (extended)");
}
}
else{
log_printf(", No Lc\n");
}
if(apdu->send_le != 0){
log_printf(", Le = %x\n", apdu->le);
if((apdu->le > 256) || (apdu->send_le == 2)){
log_printf(" (extended)");
}
}
else{
log_printf(", No Le\n");
}
log_printf("\n");
hexdump(apdu->data, apdu->lc);
return;
}
/* Print a RESP on the console */
void SC_print_RESP(SC_APDU_resp *resp)
{
if(resp == NULL){
return;
}
/* Sanity check on length */
if(resp->le > APDU_MAX_BUFF_LEN){
log_printf("[Smartcard] SC_print_RESP error: length %d overflow\n", resp->le);
return;
}
log_printf("===== RESP ============\n");
log_printf("SW1 = %x, SW2 = %x, Le = %x\n", resp->sw1, resp->sw2, resp->le);
hexdump(resp->data, resp->le);
return;
}
/* Print the Card information on the console */
void SC_print_Card(SC_Card *card)
{
if(card == NULL){
goto err;
}
switch(card->type){
case SMARTCARD_CONTACT:
log_printf("===== Contact Card ============\n");
log_printf("Protocol is T = %x\n", card->T_protocol);
SC_iso7816_print_ATR(&(card->info.atr));
break;
case SMARTCARD_NFC:
case SMARTCARD_UNKNOWN:
default:
log_printf("[Smartcard] Print Cards information: Unsupported asked smartcard type %d\n", card->type);
goto err;
}
err:
return;
}
/* Abstract Send APDU/Receive response function */
int SC_send_APDU(SC_APDU_cmd *apdu, SC_APDU_resp *resp, SC_Card *card){
if((apdu == NULL) || (resp == NULL) || (card == NULL)){
goto err;
}
switch(card->type){
case SMARTCARD_CONTACT:
return SC_iso7816_send_APDU(apdu, resp, &(card->info.atr), card->T_protocol);
break;
case SMARTCARD_NFC:
case SMARTCARD_UNKNOWN:
default:
log_printf("[Smartcard] Send APDU: Unsupported asked smartcard type %d\n", card->type);
goto err;
}
return 0;
err:
return -1;
}
static volatile bool map_voluntary;
int SC_fsm_early_init(sc_map_mode_t map_mode)
{
switch (map_mode) {
case SC_MAP_AUTO:
map_voluntary = false;
/* Early init what is necessary */
if(SC_iso7816_fsm_early_init(SC_7816_MAP_AUTO)){
goto err;
}
break;
case SC_MAP_VOLUNTARY:
map_voluntary = true;
/* Early init what is necessary */
if(SC_iso7816_fsm_early_init(SC_7816_MAP_VOLUNTARY)){
goto err;
}
break;
default:
log_printf("invalid map mode\n");
goto err;
}
return 0;
err:
return -1;
}
int SC_fsm_map(void)
{
if (map_voluntary) {
return SC_iso7816_fsm_map();
}
return 0;
}
int SC_fsm_unmap(void)
{
if (map_voluntary) {
return SC_iso7816_fsm_unmap();
}
return 0;
}
int SC_fsm_init(SC_Card *card, uint8_t do_negiotiate_pts, uint8_t do_change_baud_rate, uint8_t do_force_protocol, uint32_t do_force_etu){
if(card == NULL){
return -1;
}
/* Try to init in all the modes we have, contact and then contactless */
if(SC_iso7816_fsm_init(&(card->info.atr), &(card->T_protocol), do_negiotiate_pts, do_change_baud_rate, do_force_protocol, do_force_etu) == 0){
card->type = SMARTCARD_CONTACT;
goto end;
}
else{
goto err;
}
end:
return 0;
err:
return -1;
}
void SC_smartcard_lost(SC_Card *card){
if(card == NULL){
goto err;
}
switch(card->type){
case SMARTCARD_CONTACT:
SC_iso7816_smartcard_lost();
break;
case SMARTCARD_NFC:
case SMARTCARD_UNKNOWN:
default:
log_printf("[Smartcard] Print Cards information: Unsupported asked smartcard type %d\n", card->type);
goto err;
}
err:
return;
}
uint8_t SC_is_smartcard_inserted(SC_Card *card){
int ret = 0;
if(card == NULL){
goto err;
}
switch(card->type){
case SMARTCARD_CONTACT:
ret = SC_iso7816_is_smartcard_inserted();
break;
case SMARTCARD_NFC:
case SMARTCARD_UNKNOWN:
default:
log_printf("[Smartcard] Print Cards information: Unsupported asked smartcard type %d\n", card->type);
goto err;
}
return ret;
err:
return 0;
}
int SC_wait_card_timeout(SC_Card *card){
int ret = 0;
if(card == NULL){
goto err;
}
switch(card->type){
case SMARTCARD_CONTACT:
ret = SC_iso7816_wait_card_timeout(&(card->info.atr), card->T_protocol);
break;
case SMARTCARD_NFC:
case SMARTCARD_UNKNOWN:
default:
log_printf("[Smartcard] Print Cards information: Unsupported asked smartcard type %d\n", card->type);
goto err;
}
return ret;
err:
return -1;
}
int SC_register_user_handler_action(SC_Card *card, void (*action)(void)){
if(card == NULL){
goto err;
}
switch(card->type){
case SMARTCARD_CONTACT:
SC_iso7816_register_user_handler_action(action);
break;
case SMARTCARD_NFC:
case SMARTCARD_UNKNOWN:
default:
log_printf("[Smartcard] Print Cards information: Unsupported asked smartcard type %d\n", card->type);
goto err;
}
return 0;
err:
return -1;
}