summaryrefslogtreecommitdiff
path: root/passthru.c
blob: 828516d4ae30d12c86f9ecbda4b8d8747fd4dcb1 (plain)
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
/*
 * implement the applets for the CAC card.
 */
#ifdef USE_PASSTHRU
#include "vcard.h"
#include "vcard_emul.h"
#include "card_7816.h"
#include "vreader.h"
#include "mutex.h"
#include "vcard_emul.h"
#include "passthru.h"
#include <stdlib.h>
#include <string.h>
#include <pcsclite.h>

/*
 * Passthru applet private data
 */
struct VCardAppletPrivateStruct {
    char *reader_name;
    /* pcsc-lite parameters */
    SCARDHANDLE hCard;
    uint32_t hProtocol;
    SCARD_IO_REQUEST *send_io;
    unsigned char atr[MAX_ATR_SIZE];
    int atr_len;
};

static SCARDCONTEXT global_context;

#define MAX_RESPONSE_LENGTH 261 /*65537 */
/*
 * handle all the APDU's that are common to all CAC applets
 */
static VCardStatus
passthru_process_apdu(VCard *card, VCardAPDU *apdu, VCardResponse **response)
{
    LONG rv;
    unsigned char buf[MAX_RESPONSE_LENGTH];
    uint32_t len = MAX_RESPONSE_LENGTH;
    VCardAppletPrivate *applet_private = NULL;
    SCARD_IO_REQUEST receive_io;

    applet_private = vcard_get_current_applet_private(card, 0);
    if (applet_private == NULL) {
        *response = vcard_make_response(VCARD7816_STATUS_EXC_ERROR);
        return VCARD_DONE;
    }

    rv = SCardTransmit(applet_private->hCard, applet_private->send_io,
                       apdu->a_data, apdu->a_len, &receive_io, buf, &len);
    if (rv != SCARD_S_SUCCESS) {
        *response = vcard_make_response(VCARD7816_STATUS_EXC_ERROR);
        return VCARD_DONE;
    }

    *response = vcard_response_new_data(buf, len);
    if (*response == NULL) {
        *response =
            vcard_make_response(VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE);
    } else {
        (*response)->b_total_len = (*response)->b_len;
    }
    return VCARD_DONE;
}

static void
passthru_card_set_atr(VCard *card, unsigned char *atr, int atr_len)
{
    VCardAppletPrivate *applet_private = NULL;
    applet_private = vcard_get_current_applet_private(card, 0);
    if (applet_private == NULL) {
        return;
    }
    applet_private->atr_len = MIN(atr_len, sizeof(applet_private->atr));
    memcpy(applet_private->atr, atr, applet_private->atr_len);
}

static void passthru_card_get_atr(VCard *card, unsigned char *atr, int *atr_len)
{
    VCardAppletPrivate *applet_private = NULL;
    SCARD_READERSTATE *state;

    applet_private = vcard_get_current_applet_private(card, 0);
    if ((applet_private == NULL) || (applet_private->atr_len == 0)) {
        vcard_emul_get_atr(card, atr, atr_len);
        return;
    }
    *atr_len = MIN(applet_private->atr_len, *atr_len);
    memcpy(atr, applet_private->atr, *atr_len);
    return;
}

/*
 *  resest the inter call state between applet selects
 */
static VCardStatus
passthru_reset(VCard *card, int channel)
{
    return VCARD_DONE;
}

static VCardStatus
passthru_pcsc_lite_init()
{
    LONG rv;
    if (global_context != 0) {
        return VCARD_DONE;
    }
    rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &global_context);
    if (rv != SCARD_S_SUCCESS) {
        return VCARD_FAIL;
    }
    return VCARD_DONE;
}

/*
 *  match if s1 is completely contained in s2
 */
static int
string_match(const char *s1, const char *s2)
{
    int len = strlen(s1);
    const char *start;

    for (start = strchr(s2, *s1); start; start = strchr(start+1, *s1)) {
        if (strncmp(start, s1, len) == 0) {
            return 1;
        }
    }
    return 0;
}


/*
 *  Look for the reader that best matches the name for VReader
 */
static char *
passthru_get_reader_name(VReader *reader)
{
    const char *reader_name = vreader_get_name(reader);
    char *reader_list = NULL;
    char *reader_entry = NULL;
    char *reader_match = NULL;
    uint32_t reader_string_length;
    VCardStatus status;
    LONG rv;

    if (reader_name == NULL) {
        return NULL;
    }

    status = passthru_pcsc_lite_init();
    if (status != VCARD_DONE) {
        return NULL;
    }


    /* find the existing reader names */
    rv = SCardListReaders(global_context, NULL, NULL, &reader_string_length);
    if (rv !=  SCARD_S_SUCCESS) {
        return NULL;
    }
    reader_list = (char *)malloc(reader_string_length);
    rv = SCardListReaders(global_context, NULL, reader_list,
                          &reader_string_length);
    if (rv !=  SCARD_S_SUCCESS) {
        goto cleanup;
    }

    /* match that name */
    for (reader_entry = reader_list; *reader_entry;
                                   reader_entry += strlen(reader_entry)+1) {
        if (string_match(reader_entry, reader_name)) {
            reader_match = strdup(reader_entry);
            break;
        }
    }
cleanup:
    if (reader_list) {
        free(reader_list);
    }
    return reader_match;
}


/*
 * utilities for creating and destroying the private applet data
 */
static void
passthru_delete_applet_private(VCardAppletPrivate *applet_private)
{
    if (applet_private == NULL) {
        return;
    }
    if (applet_private->hCard) {
        SCardDisconnect(applet_private->hCard, SCARD_LEAVE_CARD);
    }
    if (applet_private->reader_name != NULL) {
        free(applet_private->reader_name);
    }
    free(applet_private);
}

static VCardAppletPrivate *
passthru_new_applet_private(VReader *reader)
{
    VCardAppletPrivate *applet_private = NULL;
    LONG rv;

    applet_private = (VCardAppletPrivate *)malloc(sizeof(VCardAppletPrivate));

    if (applet_private == NULL) {
        goto fail;
    }
    applet_private->hCard = 0;
    applet_private->reader_name = NULL;

    applet_private->reader_name = passthru_get_reader_name(reader);
    if (applet_private->reader_name == NULL) {
        goto fail;
    }

    rv = SCardConnect(global_context, applet_private->reader_name,
        SCARD_SHARE_DIRECT, SCARD_PROTOCOL_T0|SCARD_PROTOCOL_T1,
        &applet_private->hCard,
        &applet_private->hProtocol);
    if (rv !=  SCARD_S_SUCCESS) {
        goto fail;
    }

    if (applet_private->hProtocol == SCARD_PROTOCOL_T0) {
        applet_private->send_io = SCARD_PCI_T0;
    } else {
        applet_private->send_io = SCARD_PCI_T1;
    }
    applet_private->atr_len = 0;
    return applet_private;

fail:
    if (applet_private) {
        passthru_delete_applet_private(applet_private);
    }
    return NULL;
}


/*
 * create a new applet which links to our override function.
 */
static VCardApplet *
passthru_new_applet(VReader *reader)
{
    VCardAppletPrivate *applet_private = NULL;
    VCardApplet *applet = NULL;
    unsigned char passthru_aid[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    int passthru_aid_len = sizeof(passthru_aid);

    applet_private = passthru_new_applet_private(reader);
    if (applet_private == NULL) {
        goto failure;
    }
    applet = vcard_new_applet(passthru_process_apdu, passthru_reset,
                              passthru_aid, passthru_aid_len);
    if (applet == NULL) {
        goto failure;
    }
    vcard_set_applet_private(applet, applet_private,
                          passthru_delete_applet_private);
    applet_private = NULL;

    return applet;

failure:
    if (applet_private != NULL) {
        passthru_delete_applet_private(applet_private);
    }
    return NULL;
}



/*
 * Initialize the card. This is the only 'card type emulator' portion of this
 * the rest are connected through function pointers.
 */
VCardStatus
passthru_card_init(VReader *vreader, VCard *card,
                const char *flags,
                unsigned char * const *cert,
                int cert_len[],
                VCardKey *key[] /* adopt the keys*/,
                int cert_count)
{
    int i;
    VCardApplet *applet;

    /* Don't do soft emulation of the 7816, pass everything to the card */
    vcard_set_type(card, VCARD_DIRECT);

    applet = passthru_new_applet(vreader);
    if (applet == NULL) {
        goto failure;
    }

    vcard_add_applet(card, applet);

    /* we are adopting the keys, so free them now (since we don't use them) */
    for (i = 0; i < cert_count; i++) {
        vcard_emul_delete_key(key[i]);
    }

    return VCARD_DONE;

failure:
    return VCARD_FAIL;
}

/*
 *  Begin passthru_emul code. This emulator only works with the passthru card
 *  type.
 *
 */

/*
 *  Get the state entry that matches this reader. If none found, return NULL
 */
static SCARD_READERSTATE_A *
passthru_get_reader_state(SCARD_READERSTATE_A *reader_states,
                          int reader_count, char *name)
{
    int i;

    for (i = 0; i < reader_count; i++) {
        if (name == NULL && reader_states[i].szReader == NULL) {
            /* looking for a blank slot to return */
            return &reader_states[i];
        }
        if (name == NULL || reader_states[i].szReader == NULL) {
            continue;
        }
        if (strcmp(name, reader_states[i].szReader) == 0) {
            return &reader_states[i];
        }
    }
    return NULL;
}

/*
 * find a card slot that has been cleared out
 */
static SCARD_READERSTATE_A *
passthru_get_blank_reader(SCARD_READERSTATE_A *reader_states, int reader_count)
{
    return passthru_get_reader_state(reader_states,  reader_count, NULL);
}


/*
 *  This is the main work of the emulator, handling the thread that looks for
 *  changes in the readers and the cards.
 */
static void *
passthru_emul_event_thread(void *args)
{
    char *reader_list = NULL;
    int reader_list_len = 0;
    SCARD_READERSTATE_A *reader_states = NULL;
    int reader_count = 0;     /* number of active readers */
    int max_reader_count = 0; /* size of the reader_state array (including
                                 inactive readers) */
    LONG rv;
    int timeout = 1000;
    int i;

    do {
        /* variables to hold on to our new values until we are ready to replace
         * our old values */
        char *new_reader_list = NULL;
        int new_reader_list_len = 0;
        int new_reader_count = 0;

        /* other temps */
        char *reader_entry;
        VReader *reader;

        /*
         * First check to see if the reader list has changed
         */
        rv = SCardListReaders(global_context, NULL, NULL, &new_reader_list_len);
        if (rv !=  SCARD_S_SUCCESS) {
            goto next;
        }
        /*
         * If the names have changed, we need to update our list and states.
         * This is where we detect reader insertions and removals.
         */
        if (new_reader_list_len != reader_list_len) {
            /* update the list */
            new_reader_list = (char *)malloc(new_reader_list_len);
            if (new_reader_list == NULL) {
                goto next;
            }
            rv = SCardListReaders(global_context, NULL, new_reader_list,
                                  &new_reader_list_len);
            if (rv !=  SCARD_S_SUCCESS) {
                free(new_reader_list);
                goto next;
            }
            /* clear out our event state */
            for (i = 0; i < reader_count; i++) {
                    reader_states[i].dwEventState = 0;
            }
            /* count the readers and mark the ones that are still with us */
            for (reader_entry = new_reader_list; *reader_entry;
                 reader_entry += strlen(reader_entry)+1) {
                SCARD_READERSTATE_A *this_state;
                new_reader_count++;
                /* if the reader is still on the list, mark it present */
                this_state = passthru_get_reader_state(reader_states,
                                                       reader_count,
                                                       reader_entry);
                if (this_state) {
                    this_state->dwEventState = SCARD_STATE_PRESENT;
                }
            }
            /* eject any removed readers */
            for (i = 0; i < reader_count; i++) {
                if (reader_states[i].dwEventState == SCARD_STATE_PRESENT) {
                    reader_states[i].dwEventState = 0;
                    continue;
                }
                reader = vreader_get_reader_by_name(reader_states[i].szReader);
                vreader_remove_reader(reader);
                vreader_free(reader);
                reader_states[i].szReader = NULL;
            }
            /* handle the shrinking list */
            if (new_reader_count < reader_count) {
                /* fold all the valid entries at the end of our reader_states
                 * array up into those locations vacated by ejected readers. */
                for (i = reader_count - 1; i < (new_reader_count - 1); i--) {
                    if (reader_states[i].szReader) {
                        SCARD_READERSTATE_A *blank_reader;
                        blank_reader =
                            passthru_get_blank_reader(reader_states,
                                                      new_reader_count);
                        assert(blank_reader);
                        *blank_reader = reader_states[i];
                        reader_states[i].szReader = NULL;
                    }
                }
            }
            /* handle the growing list */
            if (new_reader_count >  max_reader_count) {
                SCARD_READERSTATE_A *new_reader_states;

                /* grow the list */
                new_reader_states =
                    (SCARD_READERSTATE_A *)realloc(reader_states,
                        sizeof(SCARD_READERSTATE_A)*new_reader_count);
                if (new_reader_states) {
                    /* successful, update our current state */
                    reader_states = new_reader_states;
                    max_reader_count = new_reader_count;
                } else {
                    new_reader_count = max_reader_count; /* couldn't get enough
                                                          * space to handle
                                                          * all the new readers
                                                          * */
                }
                /* mark our new entries as empty */
                for (i = reader_count; i > new_reader_count; i++) {
                    reader_states[i].szReader = NULL;
                }
            }
            /* now walk the reader list, updating the state */
            for (reader_entry = new_reader_list; *reader_entry;
                 reader_entry += strlen(reader_entry)+1) {
                SCARD_READERSTATE_A *this_state;
                this_state = passthru_get_reader_state(reader_states,
                                                       new_reader_count,
                                                       reader_entry);
                if (this_state) {
                    /* replace the old copy of the string with the new copy.
                     * This will allow us to free reader_list at the end */
                    reader_states->szReader = reader_entry;
                    continue;
                }
                /* this is a new reader, add it to the list */
                this_state =
                    passthru_get_blank_reader(reader_states, new_reader_count);
                if (!this_state) {
                    continue; /* this can happen of we couldn't get enough
                                 slots in the grow list */
                }
                this_state->szReader = reader_entry;
                this_state->dwCurrentState = SCARD_STATE_UNAWARE;
                reader = vreader_new(reader_entry, NULL, NULL);
                if (reader) {
                    vreader_add_reader(reader);
                }
                vreader_free(reader);
            }
            /* finally update our current variables */
            free(reader_list);
            reader_list = new_reader_list;
            reader_list_len = new_reader_list_len;
            reader_count = new_reader_count;
        }
next:
        rv = SCardGetStatusChange(global_context, timeout,
                                  reader_states, reader_count);
        if (rv == SCARD_E_TIMEOUT) {
            continue; /* check for new readers */
        }
        if (rv != SCARD_S_SUCCESS) {
            static int restarts;
            VCardStatus status;

            /* try resetting the pcsc_lite subsystem */
            SCardReleaseContext(global_context);
            global_context = 0; /* should close it */
            printf("***** SCard failure %x\n", rv);
            restarts++;
            if (restarts >= 3) {
                printf("***** SCard failed %d times\n", restarts);
                return; /* exit thread */
            }
            status = passthru_pcsc_lite_init();
            assert(status == CARD_DONE);
            sleep(1);
            continue;
        }
        /* deal with card insertion/removal */
        for (i = 0; i < reader_count ; i++) {
            if ((reader_states[i].dwEventState & SCARD_STATE_CHANGED) == 0) {
                continue;
            }
            reader_states[i].dwCurrentState = reader_states[i].dwEventState;
            reader = vreader_get_reader_by_name(reader_states[i].szReader);
            if (reader == NULL) {
                continue;
            }
            if (reader_states[i].dwEventState & SCARD_STATE_EMPTY) {
                if (vreader_card_is_present(reader) == VREADER_OK) {
                    vreader_insert_card(reader, NULL);
                }
            }
            if (reader_states[i].dwEventState & SCARD_STATE_PRESENT) {
                VCard *card;
                VCardStatus status = VCARD_FAIL;
                /* if there already was a card present, eject it before we
                 * insert the new one */
                if (vreader_card_is_present(reader) == VREADER_OK) {
                    vreader_insert_card(reader, NULL);
                }

                card = vcard_new(NULL, NULL);
                if (card != NULL) {
                    status = passthru_card_init(reader, card, "",
                                                NULL, NULL, NULL, 0);
                    passthru_card_set_atr(card, reader_states[i].rgbAtr,
                                  reader_states[i].cbAtr);
                    vcard_set_atr_func(card, passthru_card_get_atr);
                }
                if (status == VCARD_DONE) {
                    vreader_insert_card(reader, card);
                }
                vcard_free(card);
            }
            vreader_free(reader);
        }

    } while (1);
    return NULL;
}

/*
 *  Initializing the passthru emul is simply initializing pcsc-lite and
 *  launching the event thread.
 */
VCardStatus
passthru_emul_init(VCardEmulOptions *options)
{
    thread_t tid;
    thread_status_t tstatus;
    VCardStatus status;

    vreader_init();
    vevent_queue_init();

    status = passthru_pcsc_lite_init();
    if (status != VCARD_DONE) {
        return status;
    }

    /* launch reader thread */
    tstatus = THREAD_CREATE(tid, passthru_emul_event_thread, NULL);
    if (!THREAD_SUCCESS(tstatus)) {
        return VCARD_FAIL;
    }
    return VCARD_DONE;
}


VCardEmulOptions *
passthru_emul_options(const char *args)
{
    return NULL;
}
#endif