cac.c 76.6 KB
Newer Older
1 2 3
/*
 * implement the applets for the CAC card.
 *
4 5 6
 * Adaptation to GSC-IS 2.1:
 * https://nvlpubs.nist.gov/nistpubs/Legacy/IR/nistir6887e2003.pdf
 *
7 8 9 10 11 12 13 14 15
 * and NIST SP 800-73-4
 * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-73-4.pdf
 *
 * Copyright 2010 - 2018 Red Hat, Inc.
 *
 * Authors: Robert Relyea <rrelyea@redhat.com>
 *          Alon Levy <alevy@redhat.com>
 *          Jakub Jelen <jjelen@redhat.com>
 *
16
 * This code is licensed under the GNU LGPL, version 2.1 or later.
Marc-André Lureau's avatar
Marc-André Lureau committed
17
 * See the COPYING file in the top-level directory.
18 19
 */

20 21 22 23
#include "glib-compat.h"

#include <string.h>
#include <stdbool.h>
24 25

#include "cac.h"
Jakub Jelen's avatar
Jakub Jelen committed
26
#include "cac-aca.h"
27 28 29
#include "vcard.h"
#include "vcard_emul.h"
#include "card_7816.h"
Jakub Jelen's avatar
Jakub Jelen committed
30 31
#include "simpletlv.h"
#include "common.h"
32

33
static const unsigned char cac_aca_aid[] = {
Jakub Jelen's avatar
Jakub Jelen committed
34
    0xa0, 0x00, 0x00, 0x00, 0x79, 0x03, 0x00 };
35
static const unsigned char cac_ccc_aid[] = {
Jakub Jelen's avatar
Jakub Jelen committed
36
    0xa0, 0x00, 0x00, 0x01, 0x16, 0xDB, 0x00 };
37
static const unsigned char cac_02fb_aid[] = {
38
    0xa0, 0x00, 0x00, 0x00, 0x79, 0x02, 0xFB };
39
static const unsigned char cac_1201_aid[] = {
40
    0xa0, 0x00, 0x00, 0x00, 0x79, 0x12, 0x01 };
41
static const unsigned char cac_1202_aid[] = {
42
    0xa0, 0x00, 0x00, 0x00, 0x79, 0x12, 0x02 };
43
static const unsigned char cac_02f0_aid[] = {
44
    0xa0, 0x00, 0x00, 0x00, 0x79, 0x02, 0xF0 };
45
static const unsigned char cac_02f1_aid[] = {
46
    0xa0, 0x00, 0x00, 0x00, 0x79, 0x02, 0xF1 };
47
static const unsigned char cac_02f2_aid[] = {
48
    0xa0, 0x00, 0x00, 0x00, 0x79, 0x02, 0xF2 };
49
static const unsigned char cac_access_control_aid[] = {
50
    0xa0, 0x00, 0x00, 0x01, 0x16, 0x30, 0x00 };
51
static const unsigned char cac_pki_certificate_aid[] = {
52
    0xa0, 0x00, 0x00, 0x00, 0x79, 0x02, 0xFE };
53
static const unsigned char cac_pki_credential_aid[] = {
54
    0xa0, 0x00, 0x00, 0x00, 0x79, 0x02, 0xFD };
55
static const unsigned char cac_person_instance_aid[] = {
56
    0xa0, 0x00, 0x00, 0x00, 0x79, 0x02, 0x00 };
57
static const unsigned char cac_personnel_aid[] = {
58
    0xa0, 0x00, 0x00, 0x00, 0x79, 0x02, 0x01 };
Jakub Jelen's avatar
Jakub Jelen committed
59 60


61 62 63 64 65 66 67
/* private data for PKI applets */
typedef struct CACPKIAppletDataStruct {
    unsigned char *sign_buffer;
    int sign_buffer_len;
    VCardKey *key;
} CACPKIAppletData;

Jakub Jelen's avatar
Jakub Jelen committed
68 69 70 71
/* private data for CCC container */
typedef struct CACCCCAppletDataStruct {
} CACCCCAppletData;

Jakub Jelen's avatar
Jakub Jelen committed
72 73
/* private data for ACA container */
typedef struct CACACAAppletDataStruct {
74
    unsigned int pki_applets;
Jakub Jelen's avatar
Jakub Jelen committed
75 76 77
    /* At the moment mostly in cac-aca.c */
} CACACAAppletData;

78 79 80 81 82
/* private data for passthrough applets */
typedef struct CACPTAppletDataStruct {
    char *label;
} CACPTAppletData;

83 84 85 86
struct coid {
    unsigned char v[2];
};

87 88 89 90
/*
 * CAC applet private data
 */
struct VCardAppletPrivateStruct {
Jakub Jelen's avatar
Jakub Jelen committed
91 92 93 94 95 96 97
    /* common attributes */
    unsigned char *tag_buffer;
    int tag_buffer_len;
    unsigned char *val_buffer;
    int val_buffer_len;
    struct simpletlv_member *properties;
    unsigned int properties_len;
98
    unsigned int long_properties_len;
99 100 101 102 103 104
    /* TODO we should also keep a state, which OID is selected,
     * but it does not matter now, because we do not have anything different
     * in either buffer
     */
    struct coid *coids;
    unsigned int coids_len;
Jakub Jelen's avatar
Jakub Jelen committed
105
    /* applet-specific */
106 107
    union {
        CACPKIAppletData pki_data;
Jakub Jelen's avatar
Jakub Jelen committed
108
        CACCCCAppletData ccc_data;
Jakub Jelen's avatar
Jakub Jelen committed
109
        CACACAAppletData aca_data;
110
        CACPTAppletData pt_data;
111 112 113 114
        void *reserved;
    } u;
};

Jakub Jelen's avatar
Jakub Jelen committed
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
/*
 * Encode SimpleTLV structures to file expected to be returned by the card.
 * This means, data in SimpleTLV prefixed with 2B encoding the length of
 * the whole buffer.
 */
static int
cac_create_file(struct simpletlv_member *tlv, size_t tlv_len,
                unsigned char **out, int type)
{
    int len, length;
    unsigned char *buffer = NULL, *start;

    len = simpletlv_get_length(tlv, tlv_len, type);
    if (len < 0)
        goto failure;

    buffer = g_malloc(2 /*2B length*/ + len);

    start = buffer + 2;
    if (type == SIMPLETLV_TL)
        length = simpletlv_encode_tl(tlv, tlv_len, &start, len, NULL);
    else if (type == SIMPLETLV_VALUE)
        length = simpletlv_encode_val(tlv, tlv_len, &start, len, NULL);
    else
        goto failure;

    if (length <= 0)
        goto failure;

    ushort2lebytes(buffer, length);

    *out = buffer;
    return len + 2;

failure:
    *out = NULL;
    g_free(buffer);
    return 0;
}

static inline int
cac_create_tl_file(struct simpletlv_member *tlv, size_t tlv_len,
                   unsigned char **out)
{
    return cac_create_file(tlv, tlv_len, out, SIMPLETLV_TL);
}

static inline int
cac_create_val_file(struct simpletlv_member *tlv, size_t tlv_len,
                    unsigned char **out)
{
    return cac_create_file(tlv, tlv_len, out, SIMPLETLV_VALUE);
}

169 170 171 172 173 174 175 176 177 178
static inline int
cac_create_empty_file(unsigned char **out)
{
    *out = g_malloc_n(2, sizeof(unsigned char));

    (*out)[0] = 0x00;
    (*out)[1] = 0x00;
    return 2;
}

Jakub Jelen's avatar
Jakub Jelen committed
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
/*
 * This function returns properties of an applet encoded as SimpleTLV.
 * If the tags argument is provided, only the tags in the passed list
 * with respective values are returned.
 * Otherwise, all the tags are returned.
 */
static VCardResponse *
get_properties(VCard *card,
               struct simpletlv_member *properties, unsigned int properties_len,
               unsigned char *tags, unsigned int tags_len,
               unsigned int a_Le)
{
    VCardResponse *r = NULL;
    struct simpletlv_member *cp = NULL;
    unsigned int cp_len = 0;
    unsigned char *properties_buffer = NULL;
    unsigned int properties_buffer_len = 0;

    if (tags_len > 0 && tags) {
        unsigned int i, j, k = 0;

        cp = g_malloc_n(tags_len, sizeof(struct simpletlv_member));

        /* show only matching */
        for (j = 0; j < tags_len; j++) {
            int match = 0;
            for (i = 0; i < properties_len; i++) {
                if (properties[i].tag == tags[j]) {
                    memcpy(&cp[k], &properties[i],
                        sizeof(struct simpletlv_member));
                    match++;
                    k++;
                    break; // XXX do not allow more tags of the same ID
                }
            }
            /* if this tag was not found, return */
            if (!match) {
                r = vcard_make_response(VCARD7816_STATUS_ERROR_DATA_NOT_FOUND);
                goto cleanup;
            }
        }
        cp_len = tags_len;
    } else {
        cp = properties;
        cp_len = properties_len;
    }

    /* Encode the SimpleTLV structure */
    properties_buffer_len = simpletlv_encode(cp, cp_len,
        &properties_buffer, 0, NULL);
    if (properties_buffer_len <= 0) {
        g_debug("%s: Failed to encode properties buffer", __func__);
        goto cleanup;
    }

    if (a_Le > properties_buffer_len) {
        r = vcard_response_new_status_bytes(
            VCARD7816_SW1_LE_ERROR, properties_buffer_len);
        goto cleanup;
    }
    r = vcard_response_new(card, properties_buffer, properties_buffer_len,
        a_Le, VCARD7816_STATUS_SUCCESS);

cleanup:
    g_free(properties_buffer);
    if (tags_len > 0 && tags)
        g_free(cp);
    if (r == NULL)
       r = vcard_make_response(VCARD7816_STATUS_ERROR_GENERAL);
    return r;
}

251 252 253 254 255 256
/*
 * handle all the APDU's that are common to all CAC applets
 */
static VCardStatus
cac_common_process_apdu(VCard *card, VCardAPDU *apdu, VCardResponse **response)
{
Jakub Jelen's avatar
Jakub Jelen committed
257
    VCardAppletPrivate *applet_private;
258
    VCardStatus ret = VCARD_FAIL;
259 260
    int found = 0;
    unsigned int i;
261

Jakub Jelen's avatar
Jakub Jelen committed
262 263
    applet_private = vcard_get_current_applet_private(card, apdu->a_channel);

264
    switch (apdu->a_ins) {
Jakub Jelen's avatar
Jakub Jelen committed
265 266
    case CAC_GET_PROPERTIES:
        /* 5.3.3.4: Get Properties APDU */
267
        g_assert(applet_private);
Jakub Jelen's avatar
Jakub Jelen committed
268 269 270 271 272 273 274 275

        if (apdu->a_p2 != 0x00) {
            /* P2 needs to be 0x00 */
            *response = vcard_make_response(
                        VCARD7816_STATUS_ERROR_P1_P2_INCORRECT);
            ret = VCARD_DONE;
            break;
        }
276

Jakub Jelen's avatar
Jakub Jelen committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
        switch (apdu->a_p1) {
        case 0x00:
            /* Get a GSC-IS v2.0 compatible properties response message. */
            /* If P1 = 0x00 cannot be supported by the smart card, SW1 = 0x6A and SW2 = 86. */
            *response = vcard_make_response(
                        VCARD7816_STATUS_ERROR_P1_P2_INCORRECT);
            break;
        case 0x01:
            /* Get all the properties. */
            if (apdu->a_Lc != 0) {
                *response = vcard_make_response(
                            VCARD7816_STATUS_ERROR_DATA_INVALID);
                ret = VCARD_DONE;
                break;
            }
292 293
            /* TODO the properties buffer should be shorter for P1 = 0x01 */

Jakub Jelen's avatar
Jakub Jelen committed
294 295
            *response = get_properties(card, applet_private->properties,
                applet_private->properties_len, NULL, 0, apdu->a_Le);
296

Jakub Jelen's avatar
Jakub Jelen committed
297 298 299 300 301 302 303 304 305 306 307 308 309
            break;
        case 0x02:
            /* Get the properties of the tags provided in list of tags in
             * the command data field. */
            if (apdu->a_Lc == 0) {
                *response = vcard_make_response(
                            VCARD7816_STATUS_ERROR_DATA_INVALID);
                ret = VCARD_DONE;
                break;
            }
            *response = get_properties(card, applet_private->properties,
                applet_private->properties_len, apdu->a_body, apdu->a_Lc, apdu->a_Le);
            break;
310 311 312 313 314 315 316
        case 0x40:
            /* XXX This is undocumented P1 argument, which returns properties
             * extended with some more values of unknown meaning.
             */
            *response = get_properties(card, applet_private->properties,
                applet_private->long_properties_len, NULL, 0, apdu->a_Le);
            break;
Jakub Jelen's avatar
Jakub Jelen committed
317 318 319 320 321 322
        default:
            /* unknown params returns (SW1=0x6A, SW2=0x86) */
            *response = vcard_make_response(
                        VCARD7816_STATUS_ERROR_P1_P2_INCORRECT);
            break;
        }
323

Jakub Jelen's avatar
Jakub Jelen committed
324 325
        ret = VCARD_DONE;
        break;
326 327 328
    case VCARD7816_INS_SELECT_FILE:
        if (apdu->a_p1 != 0x02) {
            /* let the 7816 code handle applet switches */
329 330
            ret = VCARD_NEXT;
            break;
331
        }
Jakub Jelen's avatar
Jakub Jelen committed
332

333
        g_assert(applet_private);
Jakub Jelen's avatar
Jakub Jelen committed
334

335 336 337 338
        /* handle file id setting */
        if (apdu->a_Lc != 2) {
            *response = vcard_make_response(
                VCARD7816_STATUS_ERROR_DATA_INVALID);
339 340
            ret = VCARD_DONE;
            break;
341
        }
342 343 344 345 346 347 348 349 350
        /* CAC 2 Card Object ID needs to match one of the COID defined
         * in the applet
         */
        for (i = 0; i < applet_private->coids_len; i++) {
            if (memcmp(apdu->a_body, applet_private->coids[i].v, 2) == 0) {
                found = 1;
            }
        }
        if (!found) {
351 352
            *response = vcard_make_response(
                VCARD7816_STATUS_ERROR_FILE_NOT_FOUND);
353 354
            ret = VCARD_DONE;
            break;
355 356
        }
        *response = vcard_make_response(VCARD7816_STATUS_SUCCESS);
357 358
        ret = VCARD_DONE;
        break;
359 360 361
    case VCARD7816_INS_GET_RESPONSE:
    case VCARD7816_INS_VERIFY:
        /* let the 7816 code handle these */
362 363 364 365
        ret = VCARD_NEXT;
        break;
    default:
        *response = vcard_make_response(
366
            VCARD7816_STATUS_ERROR_INS_CODE_INVALID);
367 368
        ret = VCARD_DONE;
        break;
369
    }
370
    return ret;
371 372
}

Jakub Jelen's avatar
Jakub Jelen committed
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
/*
 * Handle READ BUFFER APDU and other common APDUs for CAC applets
 */
static VCardStatus
cac_common_process_apdu_read(VCard *card, VCardAPDU *apdu,
                             VCardResponse **response)
{
    VCardAppletPrivate *applet_private;
    VCardStatus ret = VCARD_FAIL;
    int size, offset;

    applet_private = vcard_get_current_applet_private(card, apdu->a_channel);

    switch (apdu->a_ins) {
    case CAC_READ_BUFFER:
        /* Body contains exactly two bytes */
        if (!apdu->a_body || apdu->a_Lc != 2) {
            *response = vcard_make_response(
                VCARD7816_STATUS_ERROR_DATA_INVALID);
            ret = VCARD_DONE;
            break;
        }

        /* Second byte defines how many bytes should be read */
        size = apdu->a_body[1];

        /* P1 | P2 defines offset to read from */
        offset = (apdu->a_p1 << 8) | apdu->a_p2;
Christophe Fergeau's avatar
Christophe Fergeau committed
401
        g_debug("%s: Requested offset: %d bytes", __func__, offset);
Jakub Jelen's avatar
Jakub Jelen committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

        /* First byte selects TAG+LEN or VALUE buffer */
        switch (apdu->a_body[0]) {
        case CAC_FILE_VALUE:
            size = MIN(size, applet_private->val_buffer_len - offset);
            if (size < 0) { /* Overrun returns (SW1=0x6A, SW2=0x86) */
                *response = vcard_make_response(
                    VCARD7816_STATUS_ERROR_P1_P2_INCORRECT);
                break;
            }
            *response = vcard_response_new_bytes(
                        card, applet_private->val_buffer + offset, size,
                        apdu->a_Le, VCARD7816_SW1_SUCCESS, 0);
            break;
        case CAC_FILE_TAG:
Christophe Fergeau's avatar
Christophe Fergeau committed
417
            g_debug("%s: Requested: %d bytes", __func__, size);
Jakub Jelen's avatar
Jakub Jelen committed
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
            size = MIN(size, applet_private->tag_buffer_len - offset);
            if (size < 0) { /* Overrun returns (SW1=0x6A, SW2=0x86) */
                *response = vcard_make_response(
                    VCARD7816_STATUS_ERROR_P1_P2_INCORRECT);
                break;
            }
            g_debug("%s: Returning: %d bytes (have %d)", __func__, size,
                applet_private->tag_buffer_len);
            *response = vcard_response_new_bytes(
                        card, applet_private->tag_buffer + offset, size,
                        apdu->a_Le, VCARD7816_SW1_SUCCESS, 0);
            break;
        default:
            *response = vcard_make_response(
                VCARD7816_STATUS_ERROR_DATA_INVALID);
            break;
        }
        if (*response == NULL) {
            *response = vcard_make_response(
                            VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE);
        }
        ret = VCARD_DONE;
        break;
    case CAC_UPDATE_BUFFER:
        *response = vcard_make_response(
                        VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED);
        ret = VCARD_DONE;
        break;
    default:
        ret = cac_common_process_apdu(card, apdu, response);
        break;
    }
    return ret;
}


454 455 456 457 458 459
/*
 *  reset the inter call state between applet selects
 */
static VCardStatus
cac_applet_pki_reset(VCard *card, int channel)
{
460 461
    VCardAppletPrivate *applet_private;
    CACPKIAppletData *pki_applet;
462
    applet_private = vcard_get_current_applet_private(card, channel);
463
    g_assert(applet_private);
464 465
    pki_applet = &(applet_private->u.pki_data);

466 467
    g_free(pki_applet->sign_buffer);
    pki_applet->sign_buffer = NULL;
468 469 470 471
    pki_applet->sign_buffer_len = 0;
    return VCARD_DONE;
}

472 473 474 475 476
static VCardStatus
cac_applet_passthrough_reset(VCard *card, int channel)
{
    VCardAppletPrivate *applet_private;
    applet_private = vcard_get_current_applet_private(card, channel);
477
    g_assert(applet_private);
478 479 480 481 482 483 484 485 486 487

    g_free(applet_private->tag_buffer);
    applet_private->tag_buffer = NULL;
    applet_private->tag_buffer_len = 0;
    g_free(applet_private->val_buffer);
    applet_private->val_buffer = NULL;
    applet_private->val_buffer_len = 0;
    return VCARD_DONE;
}

488 489 490 491
static VCardStatus
cac_applet_pki_process_apdu(VCard *card, VCardAPDU *apdu,
                            VCardResponse **response)
{
492 493
    CACPKIAppletData *pki_applet;
    VCardAppletPrivate *applet_private;
Jakub Jelen's avatar
Jakub Jelen committed
494
    int size;
495
    unsigned char *sign_buffer;
496
    bool retain_sign_buffer = FALSE;
497
    vcard_7816_status_t status;
498
    VCardStatus ret = VCARD_FAIL;
499 500

    applet_private = vcard_get_current_applet_private(card, apdu->a_channel);
501
    g_assert(applet_private);
502 503 504 505 506 507
    pki_applet = &(applet_private->u.pki_data);

    switch (apdu->a_ins) {
    case CAC_UPDATE_BUFFER:
        *response = vcard_make_response(
            VCARD7816_STATUS_ERROR_CONDITION_NOT_SATISFIED);
508 509
        ret = VCARD_DONE;
        break;
510 511 512 513
    case CAC_SIGN_DECRYPT:
        if (apdu->a_p2 != 0) {
            *response = vcard_make_response(
                             VCARD7816_STATUS_ERROR_P1_P2_INCORRECT);
514
            ret = VCARD_DONE;
515 516 517 518
            break;
        }
        size = apdu->a_Lc;

519 520
        sign_buffer = g_realloc(pki_applet->sign_buffer,
                                pki_applet->sign_buffer_len + size);
521 522 523 524 525 526 527 528 529
        memcpy(sign_buffer+pki_applet->sign_buffer_len, apdu->a_body, size);
        size += pki_applet->sign_buffer_len;
        switch (apdu->a_p1) {
        case  0x80:
            /* p1 == 0x80 means we haven't yet sent the whole buffer, wait for
             * the rest */
            pki_applet->sign_buffer = sign_buffer;
            pki_applet->sign_buffer_len = size;
            *response = vcard_make_response(VCARD7816_STATUS_SUCCESS);
530
            retain_sign_buffer = TRUE;
531
            break;
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
        case 0x00:
            /* we now have the whole buffer, do the operation, result will be
             * in the sign_buffer */
            status = vcard_emul_rsa_op(card, pki_applet->key,
                                       sign_buffer, size);
            if (status != VCARD7816_STATUS_SUCCESS) {
                *response = vcard_make_response(status);
                break;
            }
            *response = vcard_response_new(card, sign_buffer, size, apdu->a_Le,
                                                     VCARD7816_STATUS_SUCCESS);
            if (*response == NULL) {
                *response = vcard_make_response(
                                VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE);
            }
            break;
        default:
           *response = vcard_make_response(
                                VCARD7816_STATUS_ERROR_P1_P2_INCORRECT);
            break;
        }
553 554 555 556 557
        if (!retain_sign_buffer) {
            g_free(sign_buffer);
            pki_applet->sign_buffer = NULL;
            pki_applet->sign_buffer_len = 0;
        }
558 559 560
        ret = VCARD_DONE;
        break;
    default:
Jakub Jelen's avatar
Jakub Jelen committed
561
        ret = cac_common_process_apdu_read(card, apdu, response);
562
        break;
563
    }
564
    return ret;
565 566
}

Jakub Jelen's avatar
Jakub Jelen committed
567 568 569 570 571
static VCardStatus
cac_applet_aca_process_apdu(VCard *card, VCardAPDU *apdu,
                            VCardResponse **response)
{
    VCardStatus ret = VCARD_FAIL;
572
    CACACAAppletData *aca_applet;
Jakub Jelen's avatar
Jakub Jelen committed
573
    VCardAppletPrivate *applet_private;
574
    int format;
Jakub Jelen's avatar
Jakub Jelen committed
575 576

    applet_private = vcard_get_current_applet_private(card, apdu->a_channel);
577
    g_assert(applet_private);
578
    aca_applet = &(applet_private->u.aca_data);
Jakub Jelen's avatar
Jakub Jelen committed
579 580 581 582 583 584 585 586 587 588 589 590 591

    switch (apdu->a_ins) {
    case CAC_GET_ACR:
        /* generate some ACRs Chapter 5.3.3.5
         * Works only on the ACA container, not the others!
         */
        if (apdu->a_p2 != 0x00) {
            /* P2 needs to be 0x00 */
            *response = vcard_make_response(
                        VCARD7816_STATUS_ERROR_P1_P2_INCORRECT);
            ret = VCARD_DONE;
            break;
        }
592 593 594 595 596

        format = ((apdu->a_p1 & 0x40)
            ? CAC_FORMAT_EXTENDED
            : CAC_FORMAT_SIMPLETLV);

Jakub Jelen's avatar
Jakub Jelen committed
597 598
        switch (apdu->a_p1) {
        case 0x00:
599 600
        case 0x40:
        case 0x41: /* This one returns the same as 0x40 for some reason */
Jakub Jelen's avatar
Jakub Jelen committed
601 602 603 604 605 606
            /* All ACR table entries are to be extracted */
            if (apdu->a_Lc != 0) {
                *response = vcard_make_response(
                            VCARD7816_STATUS_ERROR_DATA_INVALID);
                break;
            }
607 608
            *response = cac_aca_get_acr_response(card, apdu->a_Le, NULL,
                format);
Jakub Jelen's avatar
Jakub Jelen committed
609 610 611 612 613 614 615 616 617
            break;

        case 0x01:
            /* Only one entry of the ACR table is extracted based on ACRID */
            if (apdu->a_Lc != 1) { /* ACRID is one byte */
                *response = vcard_make_response(
                            VCARD7816_STATUS_ERROR_DATA_INVALID);
                break;
            }
618 619
            *response = cac_aca_get_acr_response(card, apdu->a_Le,
                apdu->a_body, format);
Jakub Jelen's avatar
Jakub Jelen committed
620
            break;
621

Jakub Jelen's avatar
Jakub Jelen committed
622
        case 0x10:
623 624 625
        case 0x50:
        case 0x51: /* returns the same as 0x50 for some reason */
        case 0x52: /* returns the same as 0x50 for some reason */
Jakub Jelen's avatar
Jakub Jelen committed
626 627 628 629 630 631 632
            /* All Applet/Object ACR table entries are to be extracted */
            if (apdu->a_Lc != 0) {
                *response = vcard_make_response(
                            VCARD7816_STATUS_ERROR_DATA_INVALID);
                break;
            }
            *response = cac_aca_get_applet_acr_response(card, apdu->a_Le,
633
                aca_applet->pki_applets, NULL, 0, NULL, format);
Jakub Jelen's avatar
Jakub Jelen committed
634 635 636 637 638 639 640 641 642 643 644
            break;

        case 0x11:
            /* Only the entries of the Applet/Object ACR table for
             * one applet are extracted based on applet AID */
            if (apdu->a_Lc != 7) {
                *response = vcard_make_response(
                            VCARD7816_STATUS_ERROR_DATA_INVALID);
                break;
            }
            *response = cac_aca_get_applet_acr_response(card, apdu->a_Le,
645 646
                aca_applet->pki_applets, apdu->a_body, apdu->a_Lc, NULL,
                format);
Jakub Jelen's avatar
Jakub Jelen committed
647 648 649 650 651 652 653 654 655 656 657
            break;

        case 0x12:
            /* Only one entry of the Applet/Object ACR table for
             * an object is extracted based on object ID */
            if (apdu->a_Lc != 2) {
                *response = vcard_make_response(
                            VCARD7816_STATUS_ERROR_DATA_INVALID);
                break;
            }
            *response = cac_aca_get_applet_acr_response(card, apdu->a_Le,
658
                aca_applet->pki_applets, NULL, 0, apdu->a_body, format);
Jakub Jelen's avatar
Jakub Jelen committed
659 660 661
            break;

        case 0x20:
662
        case 0x60:
Jakub Jelen's avatar
Jakub Jelen committed
663 664 665 666 667 668
            /* The Access Method Provider table is extracted. */
            if (apdu->a_Lc != 0) {
                *response = vcard_make_response(
                            VCARD7816_STATUS_ERROR_DATA_INVALID);
                break;
            }
669
            *response = cac_aca_get_amp_response(card, apdu->a_Le, format);
Jakub Jelen's avatar
Jakub Jelen committed
670
            break;
671

Jakub Jelen's avatar
Jakub Jelen committed
672
        case 0x21:
673
        case 0x61:
Jakub Jelen's avatar
Jakub Jelen committed
674 675 676 677 678 679
            /* The Service Applet table is extracted. */
            if (apdu->a_Lc != 0) {
                *response = vcard_make_response(
                            VCARD7816_STATUS_ERROR_DATA_INVALID);
                break;
            }
680
            *response = cac_aca_get_service_response(card, apdu->a_Le,
681
                aca_applet->pki_applets, format);
Jakub Jelen's avatar
Jakub Jelen committed
682
            break;
683

Jakub Jelen's avatar
Jakub Jelen committed
684 685 686 687 688 689 690
        default:
            *response = vcard_make_response(
                VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED);
            break;
        }
        ret = VCARD_DONE;
        break;
691 692 693 694 695 696 697 698 699
    /* XXX unknown APDU from ActivClient after PIN verification */
    case 0x5A:
        /* ???
         * 80 5A 00 00 00
         * HW response: 90 00
         */
        *response = vcard_make_response(VCARD7816_STATUS_SUCCESS);
        ret = VCARD_DONE;
        break;
Jakub Jelen's avatar
Jakub Jelen committed
700 701 702 703 704 705 706
    default:
        ret = cac_common_process_apdu(card, apdu, response);
        break;
    }
    return ret;
}

707 708 709 710 711 712 713 714 715
static VCardStatus
cac_passthrough_container_process_apdu(VCard *card, VCardAPDU *apdu,
                                       VCardResponse **response)
{
    VCardStatus ret = VCARD_FAIL;
    CACPTAppletData *pt_applet;
    VCardAppletPrivate *applet_private;

    applet_private = vcard_get_current_applet_private(card, apdu->a_channel);
716
    g_assert(applet_private);
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
    pt_applet = &(applet_private->u.pt_data);

    switch (apdu->a_ins) {
    case CAC_READ_BUFFER:
        /* The data were not yet retrieved from the card -- do it now */
        if (applet_private->tag_buffer == NULL || applet_private->val_buffer == NULL) {
            unsigned char *data;
            unsigned int data_len;
            size_t tlv_len;
            struct simpletlv_member *tlv;

            data = vcard_emul_read_object(card, pt_applet->label, &data_len);
            if (data) {
                tlv = simpletlv_parse(data, data_len, &tlv_len);
                g_free(data);

                /* break the data buffer to TL and V buffers */
                applet_private->tag_buffer_len = cac_create_tl_file(tlv, tlv_len,
                    &applet_private->tag_buffer);
                applet_private->val_buffer_len = cac_create_val_file(tlv, tlv_len,
                    &applet_private->val_buffer);

                simpletlv_free(tlv, tlv_len);
            } else {
                /* there is not a CAC card in a slot ? */
                /* Return an empty buffer so far */
                /* TODO try to emulate the expected structures here */
                applet_private->tag_buffer_len = cac_create_empty_file(
                    &applet_private->tag_buffer);
                applet_private->val_buffer_len = cac_create_empty_file(
                    &applet_private->val_buffer);
            }
        }
        /* fallthrough */
    default:
        ret = cac_common_process_apdu_read(card, apdu, response);
        break;
    }
    return ret;
}

758 759 760 761 762 763
/*
 * utilities for creating and destroying the private applet data
 */
static void
cac_delete_pki_applet_private(VCardAppletPrivate *applet_private)
{
764
    CACPKIAppletData *pki_applet_data;
765 766

    if (applet_private == NULL) {
767 768 769
        return;
    }
    pki_applet_data = &(applet_private->u.pki_data);
770
    g_free(pki_applet_data->sign_buffer);
Jakub Jelen's avatar
Jakub Jelen committed
771 772
    g_free(applet_private->tag_buffer);
    g_free(applet_private->val_buffer);
773
    g_free(applet_private->coids);
Jakub Jelen's avatar
Jakub Jelen committed
774
    /* this one is cloned so needs to be freed */
775
    simpletlv_free(applet_private->properties, applet_private->long_properties_len);
776 777 778
    if (pki_applet_data->key != NULL) {
        vcard_emul_delete_key(pki_applet_data->key);
    }
779
    g_free(applet_private);
780 781
}

Jakub Jelen's avatar
Jakub Jelen committed
782 783 784 785 786 787 788 789
static void
cac_delete_ccc_applet_private(VCardAppletPrivate *applet_private)
{
    if (applet_private == NULL) {
        return;
    }
    g_free(applet_private->tag_buffer);
    g_free(applet_private->val_buffer);
790
    g_free(applet_private->coids);
Jakub Jelen's avatar
Jakub Jelen committed
791 792 793
    g_free(applet_private);
}

Jakub Jelen's avatar
Jakub Jelen committed
794 795 796 797 798 799
static void
cac_delete_aca_applet_private(VCardAppletPrivate *applet_private)
{
    if (applet_private == NULL) {
        return;
    }
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
    g_free(applet_private->coids);
    g_free(applet_private);
}

static void
cac_delete_empty_applet_private(VCardAppletPrivate *applet_private)
{
    if (applet_private == NULL) {
        return;
    }
    g_free(applet_private->coids);
    g_free(applet_private->tag_buffer);
    g_free(applet_private->val_buffer);
    /* this one is cloned so needs to be freed */
    simpletlv_free(applet_private->properties, applet_private->properties_len);
Jakub Jelen's avatar
Jakub Jelen committed
815 816 817
    g_free(applet_private);
}

818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
static void
cac_delete_passthrough_applet_private(VCardAppletPrivate *applet_private)
{
    CACPTAppletData *pt_applet_data;

    if (applet_private == NULL) {
        return;
    }
    pt_applet_data = &(applet_private->u.pt_data);
    g_free(pt_applet_data->label);
    g_free(applet_private->tag_buffer);
    g_free(applet_private->val_buffer);
    g_free(applet_private->coids);
    /* this one is cloned so needs to be freed */
    simpletlv_free(applet_private->properties, applet_private->properties_len);
    g_free(applet_private);
}

836
static VCardAppletPrivate *
Jakub Jelen's avatar
Jakub Jelen committed
837
cac_new_pki_applet_private(int i, const unsigned char *cert,
838 839
                           int cert_len, VCardKey *key)
{
840 841
    CACPKIAppletData *pki_applet_data;
    VCardAppletPrivate *applet_private;
Jakub Jelen's avatar
Jakub Jelen committed
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
    int bits;

    /* PKI applet Properies ex.:
     * 01  Tag: Applet Information
     * 05  Length
     *    10  Applet family
     *    02 06 02 03  Applet version
     * 40  Tag: Number of objects managed by this instance
     * 01  Length
     *    01  One
     * 51  Tag: First PKI object
     * 11  Length
     *    41  Tag: ObjectID
     *    02  Length
     *       01 01
     *    42  Buffer properties
     *    05  Length
     *       00  Type of tag supported
     *       1E 00  T-Buffer length (LSB, MSB)
     *       54 05  V-Buffer length (LSB, MSB)
     *    43  Tag: PKI properties
     *    04  Length
     *       06  Algorithm ID                       Table 5-6 in GSC-IS 2.1
     *       10  Key length bytes /8
     *       01  Private key initialized
     *       01  Public key initialized
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
     *
     * Long Properties:
     * PKI applet:
     * 01 05        Applet Information
     *  10 02 06 02 03
     * 40 01        Number of objects
     *  01
     * 51 14        First PKI Object
     *  41 02       Object ID
     *   01 02
     *  42 05       Buffer properties
     *   00 1E 00 57 05
     *  43 04       PKI Properties
     *   06 10 01 00
     * 26 01        ???
     *  01
     * 39 01
     *  00
     * 3A 07        ACA ???
     *  A0 00 00 00 79 03 00
Jakub Jelen's avatar
Jakub Jelen committed
888 889 890 891
     */
    unsigned char object_id[] = "\x01\x00";
    unsigned char buffer_properties[] = "\x00\x00\x00\x00\x00";
    unsigned char pki_properties[] = "\x06\x10\x01\x01";
892 893
    unsigned char buffer_26[] = "\x01";
    static struct simpletlv_member pki_object[] = {
Jakub Jelen's avatar
Jakub Jelen committed
894 895 896 897 898 899
      {CAC_PROPERTIES_OBJECT_ID, 2, {/*.value = object_id*/},
          SIMPLETLV_TYPE_LEAF},
      {CAC_PROPERTIES_BUFFER_PROPERTIES, 5, {/*.value = buffer_properties*/},
          SIMPLETLV_TYPE_LEAF},
      {CAC_PROPERTIES_PKI_PROPERTIES, 4, {/*.value = pki_properties*/},
          SIMPLETLV_TYPE_LEAF},
900
      {0x26, 0x01, {/*.child = buffer_26*/}, SIMPLETLV_TYPE_LEAF},
Jakub Jelen's avatar
Jakub Jelen committed
901 902 903
    };
    unsigned char applet_information[] = "\x10\x02\x06\x02\x03";
    unsigned char number_objects[] = "\x01";
904 905
    unsigned char buffer_39[] = "\x00";
    unsigned char aca_aid[] = "\xA0\x00\x00\x00\x79\x03\x00";
Jakub Jelen's avatar
Jakub Jelen committed
906 907 908 909 910
    static struct simpletlv_member properties[] = {
      {CAC_PROPERTIES_APPLET_INFORMATION, 5, {/*.value = applet_information*/},
          SIMPLETLV_TYPE_LEAF},
      {CAC_PROPERTIES_NUMBER_OBJECTS, 1, {/*.value = number_objects */},
          SIMPLETLV_TYPE_LEAF},
911
      {CAC_PROPERTIES_PKI_OBJECT, 4, {/*.child = pki_object*/},
Jakub Jelen's avatar
Jakub Jelen committed
912
          SIMPLETLV_TYPE_COMPOUND},
913 914
      {0x39, 0x01, {/*.child = buffer_39*/}, SIMPLETLV_TYPE_LEAF},
      {0x3A, 0x07, {/*.child = aca_aid*/}, SIMPLETLV_TYPE_LEAF},
Jakub Jelen's avatar
Jakub Jelen committed
915 916 917 918 919
    };
    size_t properties_len = sizeof(properties)/sizeof(struct simpletlv_member);
    /* if this would be 1, the certificate would be compressed */
    unsigned char certinfo[] = "\x00";
    struct simpletlv_member buffer[] = {
920 921 922 923 924 925 926
        {CAC_PKI_TAG_CERTINFO, 1, {/*.value = certinfo*/},
            SIMPLETLV_TYPE_LEAF},
        {CAC_PKI_TAG_CERTIFICATE, cert_len, {/*.value = cert*/},
            SIMPLETLV_TYPE_LEAF},
        {CAC_PKI_TAG_MSCUID, 0, {/*.value = NULL*/}, SIMPLETLV_TYPE_LEAF},
        {CAC_PKI_TAG_ERROR_DETECTION_CODE, 0, {/*.value = NULL*/},
            SIMPLETLV_TYPE_LEAF},
Jakub Jelen's avatar
Jakub Jelen committed
927 928
    };
    size_t buffer_len = sizeof(buffer)/sizeof(struct simpletlv_member);
929

930
    applet_private = g_new0(VCardAppletPrivate, 1);
931 932 933
    pki_applet_data = &(applet_private->u.pki_data);
    /*
     * if we want to support compression, then we simply change the 0 to a 1
Jakub Jelen's avatar
Jakub Jelen committed
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
     * in certinfo and compress the cert data with libz
     */

    /* prepare the buffers to when READ_BUFFER will be called.
     * Assuming VM card with (LSB first if > 255)
     * separate Tag+Length, Value buffers as described in 8.4:
     *    2 B       1 B     1-3 B     1 B    1-3 B
     * [ T-Len ] [ Tag1 ] [ Len1 ] [ Tag2] [ Len2 ] [...]
     *
     *    2 B       Len1 B      Len2 B
     * [ V-Len ] [ Value 1 ] [ Value 2 ] [...]
     * */

    /* Tag+Len buffer */
    buffer[0].value.value = certinfo;
    buffer[1].value.value = (unsigned char *)cert;
950 951
    buffer[2].value.value = NULL;
    buffer[3].value.value = NULL;
Jakub Jelen's avatar
Jakub Jelen committed
952 953 954 955 956 957 958 959
    /* Ex:
     * 0A 00     Length of whole buffer
     * 71        Tag: CertInfo
     * 01        Length: 1B
     * 70        Tag: Certificate
     * FF B2 03  Length: (\x03 << 8) || \xB2
     * 72        Tag: MSCUID
     * 26        Length
960
     */
Jakub Jelen's avatar
Jakub Jelen committed
961 962 963 964 965 966
    applet_private->tag_buffer_len = cac_create_tl_file(buffer, buffer_len,
        &applet_private->tag_buffer);
    if (applet_private->tag_buffer_len == 0) {
        goto failure;
    }
    g_debug("%s: applet_private->tag_buffer = %s", __func__,
967
        hex_dump(applet_private->tag_buffer, applet_private->tag_buffer_len));
968

Jakub Jelen's avatar
Jakub Jelen committed
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
    /* Value buffer */
    /* Ex:
     * DA 03      Length of complete buffer
     * 01         Value of CertInfo
     * 78 [..] 6C Cert Value
     * 7B 63 37 35 62 62 61 64 61 2D 35 32 39 38 2D 31
     * 37 35 62 2D 39 32 64 63 2D 39 38 35 30 36 62 65
     * 30 30 30 30 30 7D          MSCUID Value
     */
    applet_private->val_buffer_len = cac_create_val_file(buffer, buffer_len,
        &applet_private->val_buffer);
    if (applet_private->val_buffer_len == 0) {
        goto failure;
    }
    g_debug("%s: applet_private->val_buffer = %s", __func__,
984
        hex_dump(applet_private->val_buffer, applet_private->val_buffer_len));
Jakub Jelen's avatar
Jakub Jelen committed
985 986 987 988 989

    /* Inject Object ID */
    object_id[1] = i;
    pki_object[0].value.value = object_id;

990 991 992 993 994
    /* Create Object ID list */
    applet_private->coids = g_malloc(sizeof(struct coid));
    memcpy(applet_private->coids[0].v, object_id, 2);
    applet_private->coids_len = 1;

Jakub Jelen's avatar
Jakub Jelen committed
995 996 997 998 999 1000 1001 1002 1003 1004 1005
    /* Inject T-Buffer and V-Buffer lengths in the properties buffer */
    ushort2lebytes(&buffer_properties[1], applet_private->tag_buffer_len);
    ushort2lebytes(&buffer_properties[3], applet_private->val_buffer_len);
    pki_object[1].value.value = buffer_properties;

    /* PKI properties needs adjustments based on the key sizes */
    bits = vcard_emul_rsa_bits(key);
    g_debug("RSA bits = %d", bits);
    if (bits > 0)
        pki_properties[1] = 0xff & (bits / 8 / 8);
    pki_object[2].value.value = pki_properties;
1006
    pki_object[3].value.value = buffer_26;
Jakub Jelen's avatar
Jakub Jelen committed
1007 1008 1009 1010 1011

    /* Inject Applet Version */
    properties[0].value.value = applet_information;
    properties[1].value.value = number_objects;
    properties[2].value.child = pki_object;
1012 1013
    properties[3].value.value = buffer_39;
    properties[4].value.value = aca_aid;
Jakub Jelen's avatar
Jakub Jelen committed
1014 1015

    /* Clone the properties */
1016 1017 1018 1019
    applet_private->properties_len = 3;
    applet_private->long_properties_len = properties_len;
    applet_private->properties = simpletlv_clone(properties,
        applet_private->long_properties_len);
Jakub Jelen's avatar
Jakub Jelen committed
1020 1021 1022
    if (applet_private->properties == NULL) {
        goto failure;
    }
1023 1024
    pki_applet_data->key = key;
    return applet_private;
Jakub Jelen's avatar
Jakub Jelen committed
1025 1026 1027 1028 1029

failure:
    if (applet_private)
        cac_delete_pki_applet_private(applet_private);
    return NULL;
1030 1031
}

1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
/*
 * Other applets breakdown:
 *
 * A00000007902FB: ??? (generic container ??)
 * $ opensc-tool -s 00A4040007A00000007902FB -s 8056010000
 * PROPERTIES:
 * 01 05
 *  10 02 06 01 00
 * 40 01
 *  01
 * 50 0B
 *  41 02
 *   02 FB
 *  42 05
 *   01  <-- Inidicates that this is not SimpleTLV ?
 *   62 00 60 01
 * $ opensc-tool -s 00A4040007A00000007902FB -s 8052000002010202
 * $ opensc-tool -s 00A4040007A00000007902FB -s 8052000002020202
 * TAG, VALUE BUFFER:
 * empty
 *
 * A00000007902FE: PKI Certificate
 * $ opensc-tool -s 00A4040007A00000007902FE -s 8056010000
 * PROPERTIES:
 * 01 05
 *  10 02 06 02 03
 * 40 01
 *  01
 * 50 0B
 *  41 02
 *   02 FE
 *  42 05
 *   01  <-- Indicates that this is not SimpleTLV, but what?
 *   B2 00 30 02
 * $ opensc-tool -s 00A4040007A00000007902FE -s 8052000002010202
 * $ opensc-tool -s 00A4040007A00000007902FE -s 8052000002020202
 * TAG BUFFER:
 * 36 00   <-- length
 * 00 00 14 01 DC 0B 00 00 14 01 DD 03 00 00 14 01 72 29
 * 01 00 14 01 DC 0B 01 00 14 01 DD 03 01 00 14 01 72 29
 * 02 00 14 01 DC 0B 02 00 14 01 DD 03 02 00 14 01 72 29
 * [ID?][same]       [ID?][same]       [ID?][same]
 *
 * VALUE BUFFER:
 * A5 00
 * 00 C8 C8 CE          <-- same
 * A0 00 00 00 79 01 00 <-- PKI Applet AID
 * 00 01 00             <-- OID ???
 * 00 01 01             <-- same
 * 7B 62 30 37 39 32 32 64 61 2D 35 30 30 30 2D 31
 * 30 37 39 2D 39 32 64 39 2D 39 38 35 30 36 62 65
 * 30 30 30 30 30 7D    <-- MSCUID ???
 *
 * 00 C8 C8 CE
 * A0 00 00 00 79 01 01
 * 00 01 01
 * 00 01 01
 * 7B 63 35 36 33 65 35 31 38 2D 34 32 63 31 2D 31
 * 35 36 33 2D 39 32 64 62 2D 39 38 35 30 36 62 65
 * 30 30 30 30 30 7D
 *
 * 00 C8 C8 CE
 * A0 00 00 00 79 01 02
 * 00 01 02
 * 00 01 01
 * 7B 64 37 62 33 63 38 63 38 2D 31 32 63 38 2D 31
 * 37 62 33 2D 39 32 64 64 2D 39 38 35 30 36 62 65
 * 30 30 30 30 30 7D
 *
 * Read from OpenSC PKCS#11:
 * XXX TODO OpenSC criples the data while attempting to parse it as a SimpleTLV buffers
 * 1401 << TL
 * 00 << V
 * DC0B << TL
 * C8 << V
 * C8CE << TL
 * A000000079010000140101DD03000001
 * 14010172297B62303739323264612D35
 * 3030302D313037392D393264392D3938
 * 353036626530303030307D00C8C80100
 * 1401CEDC0BA000000079010100010100
 * 0100140101DD03017B63010014013572
 * 293633653531382D343263312D313536
 * 332D393264622D393835303662653030
 * 3030307D00C8C8CEA0000200140100DC
 * 0B007901020001020001017B02001401
 * 64DD0337623302001401630000000000
 * 00000000000000000000000000000000
 * 00000000000000000000000000000000
 * 000000 << V
 *
 *
 * A00000007902FD: PKI Credential
 * $ opensc-tool -s 00A4040007A00000007902FD -s 8056010000
 * PROPERTIES:
 * 01 05
 *  10 02 06 02 03
 * 40 01
 *  01
 * 50 0B
 *  41 02
 *   02 FD
 *  42 05
 *   01  <-- Inidicates that this is not SimpleTLV ?
 *   B2 00 58 07
 * $ opensc-tool -s 00A4040007A00000007902FD -s 8052000002010202
 * $ opensc-tool -s 00A4040007A00000007902FD -s00200000083737373737373737 -s 8052000002020202
 * TAG BUFFER:
 * 12 00
 * 00 00 14 01 64 A5
 * 01 00 14 01 64 A5
 * 02 00 14 01 64 B9
 *
 * VALUE BUFFER (after login):
 * 03 02
 * 00 02 01 00 00 14 00 00 00 CF 12 87 F7 D0 6B 10
 * 09 EF D5 A7 01 50 48 0D 44 60 7A 2E EC 20 01 00
 * 00 80 00 00 00 A9 A3 31 FA 76 D2 19 03 89 B8 6A
 * 7F 3C 06 EE DC 50 44 18 79 F1 F6 8B B1 A3 29 E8
 * 71 7A 39 4D 25 F6 0D 97 15 C6 D9 A3 34 AC E3 6B
 * 36 F6 F9 50 0C A9 ED 99 8B A9 0A EF EC 47 0B 46
 * 17 FE 4E 71 08 57 88 5D 76 F1 42 8F B9 77 43 2F
 * C7 80 AC F6 57 58 CB 1F 6F 3A 72 A3 8C 8A E2 8E
 * 63 5B C6 65 5F 37 9A 75 18 39 3B 32 35 A0 19 6E
 * 95 1D 86 A8 C4 AF E9 FC 0F 89 5D B0 98 EF C7 57
 * A5 8E A6 A7 39
 * 00 02 01 00 00 14 00 00 00 17 61 A8 36 F7 6D C8
 * 91 2D 8B 45 95 02 D7 8F E0 5C 5B A3 2A 20 01 00
 * 00 80 00 00 00 BF 8C CB 0B D0 9E 6A 70 18 45 1B
 * A8 2D B5 09 17 8B 1F AF 73 75 23 7B 33 A0 6D 5A
 * 8D 50 38 E8 1E 7B 5D 27 BD 72 A7 9A 60 BD F6 07
 * 19 C2 92 7A 64 F1 EF A6 AC AD 1A 12 6F 46 94 DA
 * D9 C4 BC B0 23 31 BC 29 88 19 3C E3 8F DB F2 64
 * 41 F2 B0 79 7B CC B1 AC E3 26 1D E3 7C 8C 3C 29
 * DD B2 41 58 F6 35 47 46 AE A8 D0 F2 AA 5F 7B 89
 * B5 D8 53 1C 5F FC EE 41 C8 5B B2 C6 64 33 63 30
 * F1 70 FA D4 C9
 * 00 02 01 00 00 28 00 00 00 37 36 30 33 31 37 43
 * 37 32 37 38 41 33 33 32 36 31 45 41 42 45 44 31
 * 34 44 39 37 31 42 37 34 46 32 31 46 45 43 45 45
 * 37 20 01 00 00 80 00 00 00 C6 18 34 B8 64 DF BF
 * 07 1D 1A 3C A2 81 2D 6E 63 FA 60 C2 D6 9F 28 8B
 * D8 FD 2B B4 E7 03 E2 75 D3 81 DB 24 A7 D5 14 D5
 * B6 C2 65 9F 14 40 A5 78 DB 15 79 A4 69 22 6F 9A
 * 83 A8 FC BF 39 D0 89 B5 21 1B 11 D7 31 2E 4C E9
 * B2 03 F8 74 3B EC B1 E1 6E 89 7D C1 32 82 16 B0
 * 53 36 90 B0 79 BF 4C F5 3E E1 9D 54 A9 AD 8B F5
 * 01 F1 39 55 D8 F3 AC 47 9A 7F 73 3D 3E C0 AE 65
 * F3 2B 60 F3 07 06 3D 61 5B
 *
 *
 * A0000000790200: Person Instance (D.1 CAC Data Model Specific)
 * $ opensc-tool -s 00A4040007A0000000790200 -s 8056010000
 * PROPERTIES:
 * 01 05
 *  10 02 06 02 03
 * 40 01
 *  01
 * 50 0B
 *  41 02
 *   02 00
 *  42 05
 *   00 42 00 42 01
 * $ opensc-tool -s 00A4040007A0000000790200 -s 8052000002010202
 * $ opensc-tool -s 00A4040007A0000000790200 -s 8052000002020202
 * TAG BUFFER:
 * 20 00
 * 01 05
 * 02 00
 * 03 06
 * 04 00
 * 05 09
 * 06 08
 * 07 01
 * 08 01
 * 11 01
 * 17 0A
 * 18 01
 * 62 08
 * 65 08
 * 63 08
 * 66 08
 * 67 01
 * VALUE BUFFER:
 * 4B 00
 * 4D 61 72 69 65               Person First Name
 * 54 75 72 6E 65 72            Person Last Name
 * 38 37 36 30 32 30 30 33 35   Person Identifier
 * 31 39 37 30 30 34 30 32      Date of Birth
 * 46                           Sex Category Code
 * 54                           Person Identifier Type Code
 * 39                           Blood Type Code
 * 31 30 30 31 32 37 37 33 39 38  DoD EDI Person Identifier
 * 5A                           Organ Donor
 * 32 30 30 39 30 34 30 32      Identification Card Issue Date
 * 32 30 30 39 30 34 30 32      Date Demographic Data was Loaded on
 * 32 30 31 32 30 34 30 31      Identification Card Expiration Date
 * 32 30 31 32 30 34 30 31      Date Demographic Data on Chip Expires
 * 55                           Card Instance Identifier
 *
 * For real cards, we could try to proxy this from original card,
 * OpenSC exposes this as a data object as SimpleLTV merged in one buffer
 * $ pkcs11-tool --login --read-object --application-label 'Person Instance' --type data
 * 01 05
 *  4d 61 72 69 65
 * 02 00
 * 03 06
 *  54 75 72 6e 65 72
 * 04 00
 * 05 09
 *  38 37 36 30 32 30 30 33 35
 * 06 08
 *  31 39 37 30 30 34 30 32
 * 07 01
 *  46
 * 08 01
 *  54
 * 11 01
 *  39
 * 17 0a
 *  31 30 30 31 32 37 37 33 39 38
 * 18 01
 *  5a
 * 62 08
 *  32 30 30 39 30 34 30 32
 * 65 08
 *  32 30 30 39 30 34 30 32
 * 63 08
 *  32 30 31 32 30 34 30 31
 * 66 08
 *  32 30 31 32 30 34 30 31
 * 2e 31 30 <-- this is also broken in OpenSC
 * should be
 * 67 01 55
 *
 *
 * A0000000790201: Personnel (D.1 CAC Data Model Specific)
 * $ opensc-tool -s 00A4040007A0000000790201 -s 8056010000
 * PROPERTIES:
 * 01 05
 *  10 02 06 02 03
 * 40 01
 *  01
 * 50 0B
 *  41 02
 *   02 01
 *  42 05
 *   00 22 00 62 00
 *
 * $ opensc-tool -s 00A4040007A0000000790201 -s 8052000002010202
 * $ opensc-tool -s 00A4040007A0000000790201 -s 8052000002020202
 * TAG BUFFER:
 * 16 00
 * 19 00
 * 20 00
 * 35 00
 * 24 01
 * 25 02
 * 26 04
 * 34 01
 * 36 02
 * D3 02
 * D4 00
 * D5 00
 * VALUE BUFFER:
 * 0C 00
 * 4E           Branch of Service Code
 * 30 31        Pay Grade Code
 * 57 4F 2D 31  Rank Code
 * 41           Personnel Category Code
 * 4D 57        Pay Plan Code
 * 30 30        Personnel Entitlement Condition Code
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
 *
 * pkcs11-tool --pin 77777777 --read-object --application-label 'Personnel' --type data
 * OpenSC exposes this as a data object as SimpleLTV merged in one buffer:
 * 19 00
 * 20 00
 * 35 00
 * 24 01
 *  4e
 * 25 02
 *  30 31
 * 26 04
 *  57 4f 2d 31
 * 34 01
 *  41
 * 36 02
 *  4d 57
 * d3 02
 *  30 30
 * 00 00 fc bf <<< OpenSC bug (junk in the end instead of last encoded elements)
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
 *
 *
 *
 *
 * A0000000791201: Empty
 * $ opensc-tool -s 00A4040007A0000000791201 -s 8056010000
 * PROPERTIES:
 * 01 05
 *  10 02 06 02 03
 * 40 01
 *  01
 * 50 0B
 *  41 02
 *   12 01
 *  42 05
 *   00 42 02 C0 05
 * $ opensc-tool -s 00A4040007A0000000791201 -s 8052000002010202
 * $ opensc-tool -s 00A4040007A0000000791201 -s 8052000002020202
 * TAG, VALUE BUFFERS:
 * empty
 *
 * A0000000791202: Empty
 * $ opensc-tool -s 00A4040007A0000000791202 -s 8056010000
 * PROPERTIES:
 * 01 05
 *  10 02 06 02 03
 * 40 01
 *  01
 * 50 0B
 *  41 02
 *   12 02
 *  42 05
 *   00 42 01 40 06
 * $ opensc-tool -s 00A4040007A0000000791202 -s 8052000002010202
 * $ opensc-tool -s 00A4040007A0000000791202 -s 8052000002020202
 * TAG, VALUE BUFFERS:
 * empty
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
 *
 * A00000007902F0: Empty (no buffers)
 * $ opensc-tool -s 00A4040007A00000007902F0 -s 8056010000
 * PROPERTIES:
 * 01 05
 *  10 02 06 02 03
 * 40 01
 *  00
 *
 * A00000007902F1: Empty (no buffers)
 * $ opensc-tool -s 00A4040007A00000007902F1 -s 8056010000
 * PROPERTIES:
 * 01 05
 *  10 02 06 02 03
 * 40 01
 *  00
 *
 * A00000007902F2: Empty (no buffers)
 * $ opensc-tool -s 00A4040007A00000007902F2 -s 8056010000
 * PROPERTIES:
 * 01 05
 *  10 02 06 02 03
 * 40 01
 *  00
 *
 *
 * Access Control File
 * A0000001163000
 * PROPERTIES: shared among the OIDs
 * 01 05
 * 10 02 06 02 03
 * 40 01        Number of objects
 *  04
 * 50 0B        TV Buffer
 *  41 02       Object ID
 *   30 00
 *  42 05
 *   00         <- These are SimpleTLV
 *   1A 00 D2 07
 * 50 0B        TV Buffer
 *  41 02       Object ID
 *   60 10
 *  42 05
 *   00
 *   0E 00 BA 0B
 * 50 0B        TV buffer
 *  41 02       Object ID
 *   60 30
 *  42 05
 *   00
 *   0A 00 EE 2C
 * 50 0B        TV Buffer
 *  41 02       Object ID
 *   90 00
 *  42 05
 *   00
 *   0E 00 4E 04
 *
1418 1419
 * OID buffers (from NIST SP 800-73-4)
 * :3000: Card Holder Unique Identifier
1420 1421
 * Tag buffer:
 * 0C 00
1422 1423 1424 1425
 * 30 19        FASC
 * 34 10        GUID
 * 35 08        Expiration Date
 * 3E FF 1A 06  Issuer Asymmetric Signature
1426 1427 1428 1429 1430
 * FE 00        Error detection code
 * Value buffer:
 * D4 F8 10 DA 08 26 6C 10 A2 04 E5 83 60 DA 01 0C
 * 11 CE 66 62 84 38 10 93 E1  <-- SEIWG data
 *
1431
 * :6010: Cardholder Fingerprints
1432 1433
 * Tag buffer:
 * 06 00
1434
 * BC FF CF 04  Fingerprint I & II
1435 1436
 * FE 00        Error Detection Code
 *
1437
 * :6030: Cardholder Facial Image
1438 1439
 * Tag buffer:
 * 06 00
1440
 * BC FF A9 29  Image for Visual Verification
1441 1442
 * FE 00        Error Detection Code
 *
1443
 * :9000: Security Object
1444
 * 08 00
1445 1446
 * BB FF 38 02  Security Object
 * BA 30        Mapping of DG to ContainerID
1447
 * FE 00        Error Detectionc Code
1448 1449
 *
 * we should do this as passthrough too
1450 1451
 */

1452

Jakub Jelen's avatar
Jakub Jelen committed
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
static VCardAppletPrivate *
cac_new_ccc_applet_private(int cert_count)
{
    VCardAppletPrivate *applet_private;

    /* CCC applet Properties ex.:
     * 01  Tag: Applet Information
     * 05  Length
     *    10  Applet family
     *    02 06 02 03  Applet version
     * 40  Tag: Number of objects managed by this instance
     * 01  Length
     *    01  One
     * 50  Tag: First TV-Buffer Object
     * 0B  Length
     *    41  Tag: ObjectID
     *    02  Length
     *       DB 00
     *    42  Tag: Buffer Properties
     *    05  Length
     *       00  Type of Tag Supported
     *       F6 00 T-Buffer length (LSB, MSB)
     *       04 02 V-Buffer length (LSB, MSB)
     */
    static unsigned char object_id[] = "\xDB\x00";
    static unsigned char buffer_properties[] = "\x00\x00\x00\x00\x00";
1479 1480
    static unsigned char buffer_26[] = "\x01";
    static struct simpletlv_member tv_object[3] = {
Jakub Jelen's avatar
Jakub Jelen committed
1481 1482 1483 1484
      {CAC_PROPERTIES_OBJECT_ID, 2, {/*.value = object_id*/},
          SIMPLETLV_TYPE_LEAF},
      {CAC_PROPERTIES_BUFFER_PROPERTIES, 5, {/*.value = buffer_properties*/},
          SIMPLETLV_TYPE_LEAF},
1485
      {0x26, 0x01, {/*.value = buffer_26*/}, SIMPLETLV_TYPE_LEAF},
Jakub Jelen's avatar
Jakub Jelen committed
1486 1487 1488
    };
    static unsigned char applet_information[] = "\x10\x02\x06\x02\x03";
    static unsigned char number_objects[] = "\x01";
1489 1490
    static unsigned char buffer_39[] = "\x00";
    static unsigned char aca_aid[] = "\xA0\x00\x00\x00\x79\x03\x00";
Jakub Jelen's avatar
Jakub Jelen committed
1491 1492 1493 1494 1495 1496 1497
    static struct simpletlv_member properties[] = {
      {CAC_PROPERTIES_APPLET_INFORMATION, 5, {/*.value = applet_information*/},
          SIMPLETLV_TYPE_LEAF},
      {CAC_PROPERTIES_NUMBER_OBJECTS, 1, {/*.value = number_objects */},
          SIMPLETLV_TYPE_LEAF},
      {CAC_PROPERTIES_TV_OBJECT, 2, {/*.child = tv_object*/},
          SIMPLETLV_TYPE_COMPOUND},
1498 1499
      {0x39, 0x01, {/*.child = buffer_39*/}, SIMPLETLV_TYPE_LEAF},
      {0x3A, 0x07, {/*.child = aca_aid*/}, SIMPLETLV_TYPE_LEAF},
Jakub Jelen's avatar
Jakub Jelen committed
1500 1501 1502 1503 1504 1505 1506
    };
    size_t properties_len = sizeof(properties)/sizeof(struct simpletlv_member);

    unsigned char card_identifier[] = "\xA0\x00\x00\x00\x79\x03\x02\x40\x70\x50"
        "\x72\x36\x0E\x00\x00\x58\xBD\x00\x2C\x19\xB5";
    unsigned char cc_version[] = "\x21";
    unsigned char cg_version[] = "\x21";
1507 1508
    unsigned char cardurl[21][16] = {
        /* common CardURLs */
Jakub Jelen's avatar
Jakub Jelen committed
1509 1510 1511 1512 1513
        "\xA0\x00\x00\x00\x79\x01\x02\xFB\x02\xFB\x00\x00\x00\x00\x00\x00", /* ??? */
        "\xA0\x00\x00\x00\x79\x01\x02\xFE\x02\xFE\x00\x00\x00\x00\x00\x00", /* PKI Certificate */
        "\xA0\x00\x00\x00\x79\x01\x02\xFD\x02\xFD\x00\x00\x00\x00\x00\x00", /* PKI Credential */
        "\xA0\x00\x00\x00\x79\x01\x02\x00\x02\x00\x00\x00\x00\x00\x00\x00", /* Person Instance */
        "\xA0\x00\x00\x00\x79\x01\x02\x01\x02\x01\x00\x00\x00\x00\x00\x00", /* Personel */
1514 1515 1516 1517 1518 1519 1520
        "\xA0\x00\x00\x01\x16\x01\x30\x00\x30\x00\x00\x00\x00\x00\x00\x00", /* Access Control F. */
        "\xA0\x00\x00\x01\x16\x01\x60\x10\x30\x00\x00\x00\x00\x00\x00\x00", /* -//- */
        "\xA0\x00\x00\x01\x16\x01\x60\x30\x30\x00\x00\x00\x00\x00\x00\x00", /* -//- */
        "\xA0\x00\x00\x01\x16\x01\x90\x00\x30\x00\x00\x00\x00\x00\x00\x00", /* -//- */
        "\xA0\x00\x00\x00\x79\x01\x12\x01\x12\x01\x00\x00\x00\x00\x00\x00", /* ?? */
        "\xA0\x00\x00\x00\x79\x01\x12\x02\x12\x02\x00\x00\x00\x00\x00\x00", /* ?? */
        /* dynamic list of all possible PKI objects CardURLs */
Jakub Jelen's avatar
Jakub Jelen committed
1521 1522 1523
        "\xA0\x00\x00\x00\x79\x04\x01\x00\x01\x00\x00\x00\x00\x00\x00\x00", /* PKI */
        "\xA0\x00\x00\x00\x79\x04\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00", /* PKI */
        "\xA0\x00\x00\x00\x79\x04\x01\x02\x01\x02\x00\x00\x00\x00\x00\x00", /* PKI */
1524 1525 1526 1527 1528 1529 1530
        "\xA0\x00\x00\x00\x79\x04\x01\x03\x01\x03\x00\x00\x00\x00\x00\x00", /* PKI */
        "\xA0\x00\x00\x00\x79\x04\x01\x04\x01\x04\x00\x00\x00\x00\x00\x00", /* PKI */
        "\xA0\x00\x00\x00\x79\x04\x01\x05\x01\x05\x00\x00\x00\x00\x00\x00", /* PKI */
        "\xA0\x00\x00\x00\x79\x04\x01\x06\x01\x06\x00\x00\x00\x00\x00\x00", /* PKI */
        "\xA0\x00\x00\x00\x79\x04\x01\x07\x01\x07\x00\x00\x00\x00\x00\x00", /* PKI */
        "\xA0\x00\x00\x00\x79\x04\x01\x08\x01\x08\x00\x00\x00\x00\x00\x00", /* PKI */
        "\xA0\x00\x00\x00\x79\x04\x01\x09\x01\x09\x00\x00\x00\x00\x00\x00", /* PKI */
Jakub Jelen's avatar
Jakub Jelen committed
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
        /*
         *                                       [ Empty for VM cards!  ]
         * [ RID 5B         ][T ][  OID ][ AID ] [ P][AccessKeyInfo ][ K]
         * CardApplicationType-^                ^  ^- Pin ID           ^
         * AccessProfile is empty --------------'                      |
         * Key Crypto Algorithm ---------------------------------------'
         *
         * AID -- the "address" of the container
         * Object ID = object type
         *
         * 7.3 The Applications CardURL
         */
    };
    unsigned char pkcs15[] = "\x00";
1545 1546
    /* NIST SP 800-73-4: The data model of the PIV Card Application shall
     * be identified by data model number 0x10 */
Jakub Jelen's avatar
Jakub Jelen committed
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
    unsigned char reg_data_model[] = "\x10";
    unsigned char acr_table[] = "\x07\xA0\x00\x00\x00\x79\x03\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00";
    static struct simpletlv_member buffer[] = {
      {CAC_CCC_CARD_IDENTIFIER, 0x15, {/*.value = card_identifier*/},
          SIMPLETLV_TYPE_LEAF},
      {CAC_CCC_CAPABILITY_CONTAINER_VERSION, 1, {/*.value = cc_version*/},
          SIMPLETLV_TYPE_LEAF},
      {CAC_CCC_CAPABILITY_GRAMMAR_VERSION, 1, {/*.value = cg_version*/},
          SIMPLETLV_TYPE_LEAF},
      {CAC_CCC_APPLICATION_CARDURL, 16, {/*.value = cardurl[0]*/},
          SIMPLETLV_TYPE_LEAF},
      {CAC_CCC_APPLICATION_CARDURL, 16, {/*.value = cardurl[1]*/},
1560
          SIMPLETLV_TYPE_LEAF},
Jakub Jelen's avatar
Jakub Jelen committed
1561
      {CAC_CCC_APPLICATION_CARDURL, 16, {/*.value = cardurl[2]*/},
1562
          SIMPLETLV_TYPE_LEAF},
Jakub Jelen's avatar
Jakub Jelen committed
1563
      {CAC_CCC_APPLICATION_CARDURL, 16, {/*.value = cardurl[3]*/},
1564
          SIMPLETLV_TYPE_LEAF},
Jakub Jelen's avatar
Jakub Jelen committed
1565
      {CAC_CCC_APPLICATION_CARDURL, 16, {/*.value = cardurl[4]*/},
1566
          SIMPLETLV_TYPE_LEAF},
Jakub Jelen's avatar
Jakub Jelen committed
1567
      {CAC_CCC_APPLICATION_CARDURL, 16, {/*.value = cardurl[5]*/},
1568
          SIMPLETLV_TYPE_LEAF},
Jakub Jelen's avatar
Jakub Jelen committed
1569
      {CAC_CCC_APPLICATION_CARDURL, 16, {/*.value = cardurl[6]*/},
1570
          SIMPLETLV_TYPE_LEAF},
Jakub Jelen's avatar
Jakub Jelen committed
1571
      {CAC_CCC_APPLICATION_CARDURL, 16, {/*.value = cardurl[7]*/},
1572
          SIMPLETLV_TYPE_LEAF},
Jakub Jelen's avatar
Jakub Jelen committed
1573
      {CAC_CCC_APPLICATION_CARDURL, 16, {/*.value = cardurl[8]*/},
1574
          SIMPLETLV_TYPE_LEAF},
Jakub Jelen's avatar
Jakub Jelen committed
1575
      {CAC_CCC_APPLICATION_CARDURL, 16, {/*.value = cardurl[9]*/},
1576
          SIMPLETLV_TYPE_LEAF},
Jakub Jelen's avatar
Jakub Jelen committed
1577
      {CAC_CCC_APPLICATION_CARDURL, 16, {/*.value = cardurl[10]*/},
1578
          SIMPLETLV_TYPE_LEAF},
Jakub Jelen's avatar
Jakub Jelen committed
1579 1580 1581 1582 1583 1584
      {CAC_CCC_APPLICATION_CARDURL, 16, {/*.value = cardurl[11]*/},
          SIMPLETLV_TYPE_NONE},
      {CAC_CCC_APPLICATION_CARDURL, 16, {/*.value = cardurl[12]*/},
          SIMPLETLV_TYPE_NONE},
      {CAC_CCC_APPLICATION_CARDURL, 16, {/*.value = cardurl[13]*/},
          SIMPLETLV_TYPE_NONE},
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
      {CAC_CCC_APPLICATION_CARDURL, 16, {/*.value = cardurl[14]*/},
          SIMPLETLV_TYPE_NONE},
      {CAC_CCC_APPLICATION_CARDURL, 16, {/*.value = cardurl[15]*/},
          SIMPLETLV_TYPE_NONE},
      {CAC_CCC_APPLICATION_CARDURL, 16, {/*.value = cardurl[16]*/},
          SIMPLETLV_TYPE_NONE},
      {CAC_CCC_APPLICATION_CARDURL, 16, {/*.value = cardurl[17]*/},
          SIMPLETLV_TYPE_NONE},
      {CAC_CCC_APPLICATION_CARDURL, 16, {/*.value = cardurl[18]*/},
          SIMPLETLV_TYPE_NONE},
      {CAC_CCC_APPLICATION_CARDURL, 16, {/*.value = cardurl[19]*/},
          SIMPLETLV_TYPE_NONE},
      {CAC_CCC_APPLICATION_CARDURL, 16, {/*.value = cardurl[20]*/},
          SIMPLETLV_TYPE_NONE},
Jakub Jelen's avatar
Jakub Jelen committed
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
      {CAC_CCC_PKCS15, 1, {/*.value = pkcs15 */},
          SIMPLETLV_TYPE_LEAF},
      {CAC_CCC_REGISTERED_DATA_MODEL_NUMBER, 1, {/*.value = reg_data_model */},
          SIMPLETLV_TYPE_LEAF},
      {CAC_CCC_ACCESS_CONTROL_RULE_TABLE, 17, {/*.value = acr_table */},
          SIMPLETLV_TYPE_LEAF},
      {CAC_CCC_CARD_APDUS, 0, {}, SIMPLETLV_TYPE_LEAF},
      {CAC_CCC_REDIRECTION_TAG, 0, {}, SIMPLETLV_TYPE_LEAF},
      {CAC_CCC_CAPABILITY_TUPLES, 0, {}, SIMPLETLV_TYPE_LEAF},
      {CAC_CCC_STATUS_TUPLES, 0, {}, SIMPLETLV_TYPE_LEAF},
      {CAC_CCC_NEXT_CCC, 0, {}, SIMPLETLV_TYPE_LEAF},
      {CAC_CCC_ERROR_DETECTION_CODE, 0, {}, SIMPLETLV_TYPE_LEAF},
    };
    size_t buffer_len = sizeof(buffer)/sizeof(struct simpletlv_member);
    int i;

    applet_private = g_new0(VCardAppletPrivate, 1);

    /* prepare the buffers to when READ_BUFFER will be called.
     * Assuming VM card with (LSB first if > 255)
     * separate Tag+Length, Value buffers as described in 8.4:
     *    2 B       1 B     1-3 B     1 B    1-3 B
     * [ T-Len ] [ Tag1 ] [ Len1 ] [ Tag2] [ Len2 ] [...]
     *
     *    2 B       Len1 B      Len2 B
     * [ V-Len ] [ Value 1 ] [ Value 2 ] [...]
     * */

    buffer[0].value.value = card_identifier;
    buffer[1].value.value = cc_version;
    buffer[2].value.value = cg_version;
1630 1631 1632 1633
    /* common CardURLs */
    for (i = 0; i < 11; i++) {
        buffer[3+i].value.value = cardurl[i];
    }
Jakub Jelen's avatar
Jakub Jelen committed
1634

1635
    if (cert_count > 10) {
Jakub Jelen's avatar
Jakub Jelen committed
1636 1637 1638 1639 1640 1641
        // XXX too many objects for now
        g_debug("Too many PKI objects");
        return NULL;
    }
    /* Generate card URLs for PKI applets */
    for (i = 0; i < cert_count; i++) {
1642 1643
        buffer[i+14].value.value = cardurl[i+11];
        buffer[i+14].type = SIMPLETLV_TYPE_LEAF;
Jakub Jelen's avatar
Jakub Jelen committed
1644 1645
    }

1646 1647 1648
    buffer[24].value.value = pkcs15;
    buffer[25].value.value = reg_data_model;
    buffer[26].value.value = acr_table;
Jakub Jelen's avatar
Jakub Jelen committed
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
    /* CCC Tag+Len buffer */
    /* Ex:
     * 34 00      Length of complete buffer
     * F0 15      Card Identifier
     * F1 01      Capability Container version number
     * F2 01      Capability Grammar version number
     * F3 10      Applications CardURL
     * F3 10      Applications CardURL
     * F3 10      Applications CardURL
     * F3 10      Applications CardURL
     * F3 10      Applications CardURL
     * F3 10      Applications CardURL
     * F3 10      Applications CardURL
     * F3 10      Applications CardURL
     * F3 10      Applications CardURL
     * F3 10      Applications CardURL
     * F3 10      Applications CardURL
     * F3 10      Applications CardURL
     * F3 10      Applications CardURL
     * F3 10      Applications CardURL
     * F4 01      PKCS#15
     * F5 01      Registered Data Model number
     * F6 11      Access Control Rule Table
     * F7 00      CARD APDUs
     * FA 00      Redirection Tag
     * FB 00      Capability Tuples  (CTs)
     * FC 00      Status Tuples (STs)
     * FD 00      Next CCC
     * FE 00      Error Detection Code
     */
    applet_private->tag_buffer_len = cac_create_tl_file(buffer, buffer_len,
        &applet_private->tag_buffer);
    if (applet_private->tag_buffer_len == 0)
        goto failure;
    g_debug("%s: applet_private->tag_buffer = %s", __func__,
1684
        hex_dump(applet_private->tag_buffer, applet_private->tag_buffer_len));
Jakub Jelen's avatar
Jakub Jelen committed
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725

    /* Value buffer */
    /* Ex:
     * 0A 01       Length of complete buffer
     * A0 00 00 00 79 03 02 40 70 50 72 36 0E 00 00 58 BD 00 2C 19 B5
     * [ GSC-RID    ] [] [] [ Card ID                               ]
     * Manufacturer ID-'  '- Card Type = javaCard
     *             Card Identifier
     * 21          CC version
     * 21          Capability Grammar version
     * A0 00 00 00 79 01 02 FB 02 FB 00 00 00 00 00 00
     * A0 00 00 00 79 01 02 FE 02 FE 00 00 00 00 00 00
     * A0 00 00 00 79 01 02 FD 02 FD 00 00 00 00 00 00
     * A0 00 00 00 79 01 02 00 02 00 00 00 00 00 00 00
     * A0 00 00 00 79 01 02 01 02 01 00 00 00 00 00 00
     * A0 00 00 00 79 04 01 00 01 00 00 00 00 00 00 00
     * A0 00 00 00 79 04 01 01 01 01 00 00 00 00 00 00
     * A0 00 00 00 79 04 01 02 01 02 00 00 00 00 00 00
     * A0 00 00 01 16 01 30 00 30 00 00 00 00 00 00 00
     * A0 00 00 01 16 01 60 10 30 00 00 00 00 00 00 00
     * A0 00 00 01 16 01 60 30 30 00 00 00 00 00 00 00
     * A0 00 00 01 16 01 90 00 30 00 00 00 00 00 00 00
     * A0 00 00 00 79 01 12 01 12 01 00 00 00 00 00 00
     * A0 00 00 00 79 01 12 02 12 02 00 00 00 00 00 00
     * [     RID    ] [] [OID] [AID] [ unused in VM? ]
     * Appl. Type    -'
     *  0x01 generic
     *  0x02 ski
     *  0x04 pki
     *             CardURLs
     * 00          PKCS#15
     * 10          Reg. data model number
     * 07 A0 00 00 00 79 03 00 00 00 00 00 00 00 00 00 00
     * [] [    ACA AID       ] [            ????        ]
     *             Access Control Rule table
     */
    applet_private->val_buffer_len = cac_create_val_file(buffer, buffer_len,
        &applet_private->val_buffer);
    if (applet_private->val_buffer_len == 0)
        goto failure;
    g_debug("%s: applet_private->val_buffer = %s", __func__,
1726
        hex_dump(applet_private->val_buffer, applet_private->val_buffer_len));
Jakub Jelen's avatar
Jakub Jelen committed
1727 1728 1729 1730

    /* Inject Object ID */
    tv_object[0].value.value = object_id;

1731 1732 1733 1734 1735
    /* Create Object ID list */
    applet_private->coids = g_malloc(sizeof(struct coid));
    memcpy(applet_private->coids[0].v, object_id, 2);
    applet_private->coids_len = 1;

Jakub Jelen's avatar
Jakub Jelen committed
1736 1737 1738 1739
    /* Inject T-Buffer and V-Buffer lengths in the properties buffer */
    ushort2lebytes(&buffer_properties[1], applet_private->tag_buffer_len);
    ushort2lebytes(&buffer_properties[3], applet_private->val_buffer_len);
    tv_object[1].value.value = buffer_properties;
1740
    tv_object[2].value.value = buffer_26;
Jakub Jelen's avatar
Jakub Jelen committed
1741 1742 1743 1744 1745

    /* Inject Applet Version */
    properties[0].value.value = applet_information;
    properties[1].value.value = number_objects;
    properties[2].value.child = tv_object;
1746 1747
    properties[3].value.value = buffer_39;
    properties[4].value.value = aca_aid;
Jakub Jelen's avatar
Jakub Jelen committed
1748 1749 1750

    /* Link the properties */
    applet_private->properties = properties;
1751 1752
    applet_private->properties_len = 3;
    applet_private->long_properties_len = properties_len;
Jakub Jelen's avatar
Jakub Jelen committed
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794

    return applet_private;

failure:
    if (applet_private) {
        cac_delete_ccc_applet_private(applet_private);
    }
    return NULL;
}


/*
 * create a new CCC applet
 */
static VCardApplet *
cac_new_ccc_applet(int cert_count)
{
    VCardAppletPrivate *applet_private;
    VCardApplet *applet;

    applet_private = cac_new_ccc_applet_private(cert_count);
    if (applet_private == NULL) {
        goto failure;
    }
    applet = vcard_new_applet(cac_common_process_apdu_read, NULL,
                              cac_ccc_aid, sizeof(cac_ccc_aid));
    if (applet == NULL) {
        goto failure;
    }
    vcard_set_applet_private(applet, applet_private,
                             cac_delete_ccc_applet_private);
    applet_private = NULL;

    return applet;

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

Jakub Jelen's avatar
Jakub Jelen committed
1795
static VCardAppletPrivate *
1796
cac_new_aca_applet_private(int cert_count)
Jakub Jelen's avatar
Jakub Jelen committed
1797
{
1798
    CACACAAppletData *aca_applet_data;
Jakub Jelen's avatar
Jakub Jelen committed
1799 1800 1801 1802 1803 1804 1805 1806
    VCardAppletPrivate *applet_private;

    /* ACA applet Properties ex.:
     * 01  Tag: Applet Information
     * 05  Length
     *    10  Applet family
     *    02 06 02 02  Applet version
     */
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838

    /* 0x40 Long properties (?):
     * 01 05        Applet Information
     *  10 02 06 02 02
     * 23 0C
     *  20 11 00 00 00 00 00 00 00 00 00 58
     * 2A 02
     *  02 00
     * 2B 05        RID ???
     *  A0 00 00 00 79
     * 22 10
     *  00 00 00 DA 01 B4 03 30 00 00 00 70 00 51 02 9D
     * 2C 01
     *  00
     * 20 02
     *  08 01
     * 21 01
     *  0F
     * 32 04
     *  03 03 03 03
     * 30 01
     *  08
     * 31 04
     *  06 08 00 00
     * 24 01
     *  FF
     * 25 02
     *  01 00
     * 26 01
     *  01
     */

Jakub Jelen's avatar
Jakub Jelen committed
1839
    static unsigned char applet_information[] = "\x10\x02\x06\x02\x02";
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
    static unsigned char buffer_23[] = "\x20\x11\x00\x00\x00\x00\x00\x00\x00"
                                       "\x00\x00\x58";
    static unsigned char buffer_2A[] = "\x02\x00";
    static unsigned char rid[] = "\xA0\x00\x00\x00\x79";
    static unsigned char buffer_22[] = "\x00\x00\x00\xDA\x01\xB4\x03\x30\x00"
                                       "\x00\x00\x70\x00\x51\x02\x9D";
    static unsigned char buffer_2C[] = "\x00";
    static unsigned char buffer_20[] = "\x08\x01";
    static unsigned char buffer_21[] = "\x0F";
    static unsigned char buffer_32[] = "\x03\x03\x03\x03";
    static unsigned char buffer_30[] = "\x08";
    static unsigned char buffer_31[] = "\x06\x08\x00\x00";
    static unsigned char buffer_24[] = "\xFF";
    static unsigned char buffer_25[] = "\x01\x00";
    static unsigned char buffer_26[] = "\x01";
    static struct simpletlv_member properties[] = {
Jakub Jelen's avatar
Jakub Jelen committed
1856 1857
      {CAC_PROPERTIES_APPLET_INFORMATION, 5, {/*.value = applet_information*/},
          SIMPLETLV_TYPE_LEAF},
1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
      {0x23, 0x0C, {/*.value = buffer_23*/}, SIMPLETLV_TYPE_LEAF},
      {0x2A, 0x02, {/*.value = buffer_2A*/}, SIMPLETLV_TYPE_LEAF},
      {0x2B, 0x05, {/*.value = rid*/}, SIMPLETLV_TYPE_LEAF},
      {0x22, 0x10, {/*.value = buffer_22*/}, SIMPLETLV_TYPE_LEAF},
      {0x2C, 0x01, {/*.value = buffer_2C*/}, SIMPLETLV_TYPE_LEAF},
      {0x20, 0x02, {/*.value = buffer_20*/}, SIMPLETLV_TYPE_LEAF},
      {0x21, 0x01, {/*.value = buffer_21*/}, SIMPLETLV_TYPE_LEAF},
      {0x32, 0x04, {/*.value = buffer_32*/}, SIMPLETLV_TYPE_LEAF},
      {0x30, 0x01, {/*.value = buffer_30*/}, SIMPLETLV_TYPE_LEAF},
      {0x31, 0x04, {/*.value = buffer_31*/}, SIMPLETLV_TYPE_LEAF},
      {0x24, 0x01, {/*.value = buffer_24*/}, SIMPLETLV_TYPE_LEAF},
      {0x25, 0x02, {/*.value = buffer_25*/}, SIMPLETLV_TYPE_LEAF},
      {0x26, 0x01, {/*.value = buffer_26*/}, SIMPLETLV_TYPE_LEAF},
Jakub Jelen's avatar
Jakub Jelen committed
1871 1872 1873 1874
    };

    /* Inject Applet Version into Applet information */
    properties[0].value.value = applet_information;
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887
    properties[1].value.value = buffer_23;
    properties[2].value.value = buffer_2A;
    properties[3].value.value = rid;
    properties[4].value.value = buffer_22;
    properties[5].value.value = buffer_2C;
    properties[6].value.value = buffer_20;
    properties[7].value.value = buffer_21;
    properties[8].value.value = buffer_32;
    properties[9].value.value = buffer_30;
    properties[10].value.value = buffer_31;
    properties[11].value.value = buffer_24;
    properties[12].value.value = buffer_25;
    properties[13].value.value = buffer_26;
Jakub Jelen's avatar
Jakub Jelen committed
1888 1889 1890

    /* Create the private data structure */
    applet_private = g_new0(VCardAppletPrivate, 1);
1891
    aca_applet_data = &(applet_private->u.aca_data);
Jakub Jelen's avatar
Jakub Jelen committed
1892 1893 1894
    if (applet_private == NULL)
        goto failure;

1895 1896 1897 1898 1899 1900
    /* store the applet OID */
    applet_private->coids = g_malloc(sizeof(struct coid));
    applet_private->coids[0].v[0] = 0x03;
    applet_private->coids[0].v[1] = 0x00;
    applet_private->coids_len = 1;

Jakub Jelen's avatar
Jakub Jelen committed
1901 1902 1903
    /* Link the properties */
    applet_private->properties = properties;
    applet_private->properties_len = 1;
1904
    applet_private->long_properties_len = 14;
Jakub Jelen's avatar
Jakub Jelen committed
1905

1906 1907
    aca_applet_data->pki_applets = cert_count;

Jakub Jelen's avatar
Jakub Jelen committed
1908 1909 1910 1911 1912 1913 1914 1915 1916
    return applet_private;

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

1917 1918 1919 1920 1921 1922 1923
static VCardAppletPrivate *
cac_new_empty_applet_private(unsigned char objects[][2], unsigned int objects_len)
{
    VCardAppletPrivate *applet_private = NULL;

    unsigned char object_id[] = "\x00\x00";
    unsigned char buffer_properties[] = "\x00\x00\x00\x00\x00";
1924
    static unsigned char buffer_26[] = "\x01";
1925 1926 1927 1928 1929
    static struct simpletlv_member tv_buffer[] = {
      {CAC_PROPERTIES_OBJECT_ID, 2, {/*.value = object_id*/},
          SIMPLETLV_TYPE_LEAF},
      {CAC_PROPERTIES_BUFFER_PROPERTIES, 5, {/*.value = buffer_properties*/},
          SIMPLETLV_TYPE_LEAF},
1930
      {0x26, 0x01, {/*.value = buffer_26*/}, SIMPLETLV_TYPE_LEAF},
1931 1932 1933 1934 1935 1936 1937 1938
    };
    unsigned char applet_information[] = "\x10\x02\x06\x02\x03";
    unsigned char number_objects = 0;
    static struct simpletlv_member properties[7] = {
      {CAC_PROPERTIES_APPLET_INFORMATION, 5, {/*.value = applet_information*/},
          SIMPLETLV_TYPE_LEAF},
      {CAC_PROPERTIES_NUMBER_OBJECTS, 1, {/*.value = number_objects*/},
          SIMPLETLV_TYPE_LEAF},
1939
      {CAC_PROPERTIES_TV_OBJECT, 3, {.child = NULL},
1940
          SIMPLETLV_TYPE_COMPOUND},
1941
      {CAC_PROPERTIES_TV_OBJECT, 3, {.child = NULL},
1942
          SIMPLETLV_TYPE_COMPOUND},
1943
      {CAC_PROPERTIES_TV_OBJECT, 3, {.child = NULL},
1944
          SIMPLETLV_TYPE_COMPOUND},
1945
      {CAC_PROPERTIES_TV_OBJECT, 3, {.child = NULL},
1946
          SIMPLETLV_TYPE_COMPOUND},
1947
      {CAC_PROPERTIES_TV_OBJECT, 3, {.child = NULL},
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959