Coverage Report

Created: 2026-11-30 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/card-authentic.c
Line
Count
Source
1
/*
2
 * card-authentic.c: Support for the Oberthur smart cards
3
 *  with PKI applet AuthentIC v3.2
4
 *
5
 * Copyright (C) 2010  Viktor Tarasov <vtarasov@opentrust.com>
6
 *      OpenTrust <www.opentrust.com>
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
#ifdef HAVE_CONFIG_H
24
#include <config.h>
25
#endif
26
27
#ifdef ENABLE_OPENSSL   /* empty file without openssl */
28
29
#include <string.h>
30
#include <stdlib.h>
31
32
#include "internal.h"
33
#include "asn1.h"
34
#include "cardctl.h"
35
#include "opensc.h"
36
#include "pkcs15.h"
37
#include "iso7816.h"
38
/* #include "hash-strings.h" */
39
#include "authentic.h"
40
41
#include <openssl/sha.h>
42
43
0
#define AUTHENTIC_CARD_DEFAULT_FLAGS ( 0   \
44
0
    | SC_ALGORITHM_ONBOARD_KEY_GEN    \
45
0
    | SC_ALGORITHM_RSA_PAD_ISO9796    \
46
0
    | SC_ALGORITHM_RSA_PAD_PKCS1    \
47
0
    | SC_ALGORITHM_RSA_HASH_NONE    \
48
0
    | SC_ALGORITHM_RSA_HASH_SHA1    \
49
0
    | SC_ALGORITHM_RSA_HASH_SHA256)
50
51
#define AUTHENTIC_READ_BINARY_LENGTH_MAX 0xE7
52
53
/* generic iso 7816 operations table */
54
static const struct sc_card_operations *iso_ops = NULL;
55
56
/* our operations table with overrides */
57
static struct sc_card_operations authentic_ops;
58
59
static struct sc_card_driver authentic_drv = {
60
  "Oberthur AuthentIC v3.1", "authentic", &authentic_ops,
61
  NULL, 0, NULL
62
};
63
64
0
#define AUTHENTIC_N_PINS 8
65
/*
66
 * FIXME: use dynamic allocation for the PIN data to reduce memory usage
67
 * actually size of 'authentic_private_data' 140kb
68
 */
69
struct authentic_private_data {
70
  struct sc_pin_cmd_data pins[AUTHENTIC_N_PINS];
71
  unsigned char pins_sha1[AUTHENTIC_N_PINS][SHA_DIGEST_LENGTH];
72
73
  struct sc_cplc cplc;
74
};
75
76
static const struct sc_atr_table authentic_known_atrs[] = {
77
  { "3B:DD:18:00:81:31:FE:45:80:F9:A0:00:00:00:77:01:00:70:0A:90:00:8B", NULL,
78
    "Oberthur AuthentIC 3.2.2", SC_CARD_TYPE_OBERTHUR_AUTHENTIC_3_2,  0, NULL },
79
  { NULL, NULL, NULL, 0, 0, NULL }
80
};
81
82
unsigned char aid_AuthentIC_3_2[] = {
83
  0xA0,0x00,0x00,0x00,0x77,0x01,0x00,0x70,0x0A,0x10,0x00,0xF1,0x00,0x00,0x01,0x00
84
};
85
86
static int authentic_select_file(struct sc_card *card, const struct sc_path *path, struct sc_file **file_out);
87
static int authentic_process_fci(struct sc_card *card, struct sc_file *file, const unsigned char *buf, size_t buflen);
88
static int authentic_get_serialnr(struct sc_card *card, struct sc_serial_number *serial);
89
static int authentic_pin_get_policy (struct sc_card *card, struct sc_pin_cmd_data *data, struct sc_acl_entry *acls);
90
static int authentic_pin_is_verified(struct sc_card *card, struct sc_pin_cmd_data *pin_cmd);
91
static int authentic_select_mf(struct sc_card *card, struct sc_file **file_out);
92
static int authentic_card_ctl(struct sc_card *card, unsigned long cmd, void *ptr);
93
94
#ifdef ENABLE_SM
95
static int authentic_sm_open(struct sc_card *card);
96
static int authentic_sm_get_wrapped_apdu(struct sc_card *card, struct sc_apdu *apdu, struct sc_apdu **sm_apdu);
97
static int authentic_sm_free_wrapped_apdu(struct sc_card *card, struct sc_apdu *apdu, struct sc_apdu **sm_apdu);
98
#endif
99
100
static int
101
authentic_update_blob(struct sc_context *ctx, unsigned tag, unsigned char *data, size_t data_len,
102
    unsigned char **blob, size_t *blob_size)
103
0
{
104
0
  unsigned char *pp = NULL;
105
0
  int offs = 0;
106
0
  size_t sz;
107
108
0
  if (data_len == 0)
109
0
    return SC_SUCCESS;
110
111
0
  sz = data_len + 2;
112
113
0
  if (tag > 0xFF)
114
0
    sz++;
115
116
0
  if (data_len > 0x7F && data_len < 0x100)
117
0
    sz++;
118
0
  else if (data_len >= 0x100)
119
0
    sz += 2;
120
121
0
  pp = realloc(*blob, *blob_size + sz);
122
0
  if (!pp)
123
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY);
124
125
0
  if (tag > 0xFF)
126
0
    *(pp + *blob_size + offs++) = (tag >> 8) & 0xFF;
127
0
  *(pp + *blob_size + offs++) = tag & 0xFF;
128
129
0
  if (data_len >= 0x100) {
130
0
    *(pp + *blob_size + offs++) = 0x82;
131
0
    *(pp + *blob_size + offs++) = (data_len >> 8) & 0xFF;
132
0
  }
133
0
  else if (data_len > 0x7F)   {
134
0
    *(pp + *blob_size + offs++) = 0x81;
135
0
  }
136
0
  *(pp + *blob_size + offs++) = data_len & 0xFF;
137
138
0
  memcpy(pp + *blob_size + offs, data, data_len);
139
140
0
  *blob_size += sz;
141
0
  *blob = pp;
142
143
0
  return SC_SUCCESS;
144
0
}
145
146
147
static int
148
authentic_parse_size(unsigned char *in, size_t in_len, size_t *out)
149
0
{
150
0
  if (!in || !out || in_len < 1)
151
0
    return SC_ERROR_INVALID_ARGUMENTS;
152
153
0
  if (*in < 0x80)   {
154
0
    *out = *in;
155
0
    return 1;
156
0
  }
157
0
  else if (*in == 0x81)   {
158
0
    if (in_len < 2)
159
0
      return SC_ERROR_INVALID_DATA;
160
0
    *out = *(in + 1);
161
0
    return 2;
162
0
  }
163
0
  else if (*in == 0x82)   {
164
0
    if (in_len < 3)
165
0
      return SC_ERROR_INVALID_DATA;
166
0
    *out = *(in + 1) * 0x100 + *(in + 2);
167
0
    return 3;
168
0
  }
169
170
0
  return SC_ERROR_INVALID_DATA;
171
0
}
172
173
174
static int
175
authentic_get_tagged_data(struct sc_context *ctx, unsigned char *in, size_t in_len,
176
    unsigned in_tag, unsigned char **out, size_t *out_len)
177
0
{
178
0
  size_t size_len, tag_len, offs, size;
179
0
  int rv;
180
0
  unsigned tag;
181
182
0
  if (!out || !out_len)
183
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
184
185
0
  for (offs = 0; offs < in_len; )   {
186
0
    if ((*(in + offs) == 0x7F) || (*(in + offs) == 0x5F))   {
187
0
      if (offs + 1 >= in_len)
188
0
        LOG_TEST_RET(ctx, SC_ERROR_INTERNAL, "parse error: invalid data");
189
0
      tag = *(in + offs) * 0x100 + *(in + offs + 1);
190
0
      tag_len = 2;
191
0
    }
192
0
    else   {
193
0
      tag = *(in + offs);
194
0
      tag_len = 1;
195
0
    }
196
197
0
    if (offs + tag_len >= in_len)
198
0
      LOG_TEST_RET(ctx, SC_ERROR_INTERNAL, "parse error: invalid data");
199
200
0
    rv = authentic_parse_size(in + offs + tag_len, in_len - (offs + tag_len), &size);
201
0
    LOG_TEST_RET(ctx, rv, "parse error: invalid size data");
202
0
    size_len = rv;
203
204
0
    if (tag == in_tag)   {
205
0
      if (in_len - (offs + tag_len + size_len) < size)
206
0
        LOG_TEST_RET(ctx, SC_ERROR_INTERNAL, "parse error: invalid data");
207
208
0
      *out = in + offs + tag_len + size_len;
209
0
      *out_len = size;
210
211
0
      return SC_SUCCESS;
212
0
    }
213
214
0
    offs += tag_len + size_len + size;
215
0
  }
216
217
0
  return SC_ERROR_ASN1_OBJECT_NOT_FOUND;
218
0
}
219
220
221
static int
222
authentic_decode_pubkey_rsa(struct sc_context *ctx, unsigned char *blob, size_t blob_len,
223
    struct sc_pkcs15_prkey **out_key)
224
0
{
225
0
  struct sc_pkcs15_prkey_rsa *key;
226
0
  unsigned char *data;
227
0
  size_t data_len;
228
0
  int rv;
229
230
0
  LOG_FUNC_CALLED(ctx);
231
232
0
  if (!out_key)
233
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
234
235
0
  if (!(*out_key))   {
236
0
    *out_key = calloc(1, sizeof(struct sc_pkcs15_prkey));
237
238
0
    if (!(*out_key))
239
0
      LOG_TEST_RET(ctx, SC_ERROR_OUT_OF_MEMORY, "Cannot callocate pkcs15 private key");
240
241
0
    (*out_key)->algorithm = SC_ALGORITHM_RSA;
242
0
  }
243
0
  else if (*out_key && (*out_key)->algorithm != SC_ALGORITHM_RSA)   {
244
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_DATA);
245
0
  }
246
247
0
  key = &(*out_key)->u.rsa;
248
249
0
  rv = authentic_get_tagged_data(ctx, blob, blob_len, AUTHENTIC_TAG_RSA_PUBLIC, &data, &data_len);
250
0
  LOG_TEST_RET(ctx, rv, "cannot get public key SDO data");
251
252
0
  blob = data;
253
0
  blob_len = data_len;
254
255
  /* Get RSA public modulus */
256
0
  rv = authentic_get_tagged_data(ctx, blob, blob_len, AUTHENTIC_TAG_RSA_PUBLIC_MODULUS, &data, &data_len);
257
0
  LOG_TEST_RET(ctx, rv, "cannot get public key SDO data");
258
259
0
  if (key->modulus.data)
260
0
    free(key->modulus.data);
261
0
  key->modulus.data = calloc(1, data_len);
262
0
  if (!key->modulus.data)
263
0
    LOG_TEST_RET(ctx, SC_ERROR_OUT_OF_MEMORY, "Cannot callocate modulus BN");
264
0
  memcpy(key->modulus.data, data, data_len);
265
0
  key->modulus.len = data_len;
266
267
  /* Get RSA public exponent */
268
0
  rv = authentic_get_tagged_data(ctx, blob, blob_len, AUTHENTIC_TAG_RSA_PUBLIC_EXPONENT, &data, &data_len);
269
0
  LOG_TEST_RET(ctx, rv, "cannot get public key SDO data");
270
271
0
  if (key->exponent.data)
272
0
    free(key->exponent.data);
273
0
  key->exponent.data = calloc(1, data_len);
274
0
  if (!key->exponent.data)
275
0
    LOG_TEST_RET(ctx, SC_ERROR_OUT_OF_MEMORY, "Cannot callocate modulus BN");
276
0
  memcpy(key->exponent.data, data, data_len);
277
0
  key->exponent.len = data_len;
278
279
0
  LOG_FUNC_RETURN(ctx, rv);
280
0
}
281
282
283
static int
284
authentic_parse_credential_data(struct sc_context *ctx, struct sc_pin_cmd_data *pin_cmd,
285
    struct sc_acl_entry *acls, unsigned char *blob, size_t blob_len)
286
0
{
287
0
  unsigned char *data;
288
0
  size_t data_len;
289
0
  int rv, ii;
290
0
  unsigned tag = AUTHENTIC_TAG_CREDENTIAL | pin_cmd->pin_reference;
291
292
0
  rv = authentic_get_tagged_data(ctx, blob, blob_len, tag, &blob, &blob_len);
293
0
  LOG_TEST_RET(ctx, rv, "cannot get credential data");
294
295
0
  rv = authentic_get_tagged_data(ctx, blob, blob_len, AUTHENTIC_TAG_CREDENTIAL_TRYLIMIT, &data, &data_len);
296
0
  LOG_TEST_RET(ctx, rv, "cannot get try limit");
297
0
  if (data_len < 1)
298
0
    LOG_TEST_RET(ctx, SC_ERROR_INVALID_DATA, "invalid try limit");
299
0
  pin_cmd->pin1.max_tries = *data;
300
301
0
  rv = authentic_get_tagged_data(ctx, blob, blob_len, AUTHENTIC_TAG_DOCP_MECH, &data, &data_len);
302
0
  LOG_TEST_RET(ctx, rv, "cannot get PIN type");
303
0
  if (data_len > 0 && *data == 0)
304
0
    pin_cmd->pin_type = SC_AC_CHV;
305
0
  else if (data_len > 0 && *data >= 2 && *data <= 7)
306
0
    pin_cmd->pin_type = SC_AC_AUT;
307
0
  else
308
0
    LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "unsupported Credential type");
309
310
  /* Parse optional ACLs when requested */
311
0
  if (acls) {
312
0
    rv = authentic_get_tagged_data(ctx, blob, blob_len, AUTHENTIC_TAG_DOCP_ACLS, &data, &data_len);
313
0
    LOG_TEST_RET(ctx, rv, "failed to get ACLs");
314
0
    sc_log(ctx, "data_len:%zu", data_len);
315
0
    if (data_len == 10)   {
316
0
      for (ii=0; ii<5; ii++)   {
317
0
        unsigned char acl = *(data + ii*2);
318
0
        unsigned char cred_id = *(data + ii*2 + 1);
319
0
        unsigned sc = acl * 0x100 + cred_id;
320
321
0
        sc_log(ctx, "%i: SC:%X", ii, sc);
322
0
        if (!sc)
323
0
          continue;
324
325
0
        if (acl & AUTHENTIC_AC_SM_MASK)   {
326
0
          acls[ii].method = SC_AC_SCB;
327
0
          acls[ii].key_ref = sc;
328
0
        }
329
0
        else if (acl!=0xFF && cred_id)   {
330
0
          sc_log(ctx, "%i: ACL(method:SC_AC_CHV,id:%i)", ii, cred_id);
331
0
          acls[ii].method = SC_AC_CHV;
332
0
          acls[ii].key_ref = cred_id;
333
0
        }
334
0
        else   {
335
0
          acls[ii].method = SC_AC_NEVER;
336
0
          acls[ii].key_ref = 0;
337
0
        }
338
0
      }
339
0
    }
340
0
  }
341
342
0
  rv = authentic_get_tagged_data(ctx, blob, blob_len, AUTHENTIC_TAG_CREDENTIAL_PINPOLICY, &data, &data_len);
343
0
  if (!rv)   {
344
0
    blob = data;
345
0
    blob_len = data_len;
346
347
0
    rv = authentic_get_tagged_data(ctx, blob, blob_len, AUTHENTIC_TAG_CREDENTIAL_PINPOLICY_MAXLENGTH, &data, &data_len);
348
0
    LOG_TEST_RET(ctx, rv, "failed to get PIN max.length value");
349
0
    if (data_len < 1)
350
0
      LOG_TEST_RET(ctx, SC_ERROR_INVALID_DATA, "invalid PIN max.length value");
351
0
    pin_cmd->pin1.max_length = *data;
352
353
0
    rv = authentic_get_tagged_data(ctx, blob, blob_len, AUTHENTIC_TAG_CREDENTIAL_PINPOLICY_MINLENGTH, &data, &data_len);
354
0
    LOG_TEST_RET(ctx, rv, "failed to get PIN min.length value");
355
0
    if (data_len < 1)
356
0
      LOG_TEST_RET(ctx, SC_ERROR_INVALID_DATA, "invalid PIN min.length value");
357
0
    pin_cmd->pin1.min_length = *data;
358
0
  }
359
360
0
  return SC_SUCCESS;
361
0
}
362
363
364
static int
365
authentic_get_cplc(struct sc_card *card)
366
0
{
367
0
  struct authentic_private_data *prv_data = (struct authentic_private_data *) card->drv_data;
368
0
  struct sc_apdu apdu;
369
0
  int rv, ii;
370
0
  unsigned char p1, p2;
371
372
0
  p1 = (SC_CPLC_TAG >> 8) & 0xFF;
373
0
  p2 = SC_CPLC_TAG & 0xFF;
374
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xCA, p1, p2);
375
0
  for (ii=0;ii<2;ii++)   {
376
0
    apdu.le = SC_CPLC_DER_SIZE;
377
0
    apdu.resplen = sizeof(prv_data->cplc.value);
378
0
    apdu.resp = prv_data->cplc.value;
379
380
0
    rv = sc_transmit_apdu(card, &apdu);
381
0
    LOG_TEST_RET(card->ctx, rv, "APDU transmit failed");
382
0
    rv = sc_check_sw(card, apdu.sw1, apdu.sw2);
383
0
    if (rv != SC_ERROR_CLASS_NOT_SUPPORTED)
384
0
      break;
385
386
0
    apdu.cla = 0x80;
387
0
  }
388
0
        LOG_TEST_RET(card->ctx, rv, "'GET CPLC' error");
389
390
0
  prv_data->cplc.len = SC_CPLC_DER_SIZE;
391
0
  return SC_SUCCESS;
392
0
}
393
394
static int
395
authentic_match_card(struct sc_card *card)
396
10.8k
{
397
10.8k
  struct sc_context *ctx = card->ctx;
398
10.8k
  int i;
399
400
10.8k
  sc_log_hex(ctx, "try to match card with ATR", card->atr.value, card->atr.len);
401
10.8k
  i = _sc_match_atr(card, authentic_known_atrs, &card->type);
402
10.8k
  if (i < 0)   {
403
10.8k
    sc_log(ctx, "card not matched");
404
10.8k
    return 0;
405
10.8k
  }
406
407
0
  sc_log(ctx, "'%s' card matched", authentic_known_atrs[i].name);
408
0
  return 1;
409
10.8k
}
410
411
412
static int
413
authentic_init_oberthur_authentic_3_2(struct sc_card *card)
414
0
{
415
0
  struct sc_context *ctx = card->ctx;
416
0
  unsigned int flags;
417
0
  int rv = 0;
418
419
0
  LOG_FUNC_CALLED(ctx);
420
421
0
  flags = AUTHENTIC_CARD_DEFAULT_FLAGS;
422
423
0
  card->caps = SC_CARD_CAP_RNG;
424
0
  card->caps |= SC_CARD_CAP_APDU_EXT;
425
0
  card->caps |= SC_CARD_CAP_USE_FCI_AC;
426
427
0
#ifdef ENABLE_SM
428
0
  card->sm_ctx.ops.open = authentic_sm_open;
429
0
  card->sm_ctx.ops.get_sm_apdu = authentic_sm_get_wrapped_apdu;
430
0
  card->sm_ctx.ops.free_sm_apdu = authentic_sm_free_wrapped_apdu;
431
0
#endif
432
433
0
  rv = iso7816_select_aid(card, aid_AuthentIC_3_2, sizeof(aid_AuthentIC_3_2), NULL, NULL);
434
0
  LOG_TEST_RET(ctx, rv, "AuthentIC application select error");
435
436
0
  rv = authentic_select_mf(card, NULL);
437
0
  LOG_TEST_RET(ctx, rv, "MF selection error");
438
439
0
  _sc_card_add_rsa_alg(card, 1024, flags, 0x10001);
440
0
  _sc_card_add_rsa_alg(card, 2048, flags, 0x10001);
441
442
0
  LOG_FUNC_RETURN(ctx, rv);
443
0
}
444
445
446
static int
447
authentic_init(struct sc_card *card)
448
0
{
449
0
  struct sc_context *ctx = card->ctx;
450
0
  int ii, rv = SC_ERROR_INVALID_CARD;
451
452
0
  LOG_FUNC_CALLED(ctx);
453
0
  for(ii=0;authentic_known_atrs[ii].atr;ii++)   {
454
0
    if (card->type == authentic_known_atrs[ii].type)   {
455
0
      card->name = authentic_known_atrs[ii].name;
456
0
      card->flags = authentic_known_atrs[ii].flags;
457
0
      break;
458
0
    }
459
0
  }
460
461
0
  if (!authentic_known_atrs[ii].atr)
462
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_CARD);
463
464
0
  card->cla  = 0x00;
465
0
  card->drv_data = (struct authentic_private_data *) calloc(1, sizeof(struct authentic_private_data));
466
0
  if (!card->drv_data)
467
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY);
468
469
0
  if (card->type == SC_CARD_TYPE_OBERTHUR_AUTHENTIC_3_2)
470
0
    rv = authentic_init_oberthur_authentic_3_2(card);
471
472
0
  if (rv != SC_SUCCESS)
473
0
    rv = authentic_get_serialnr(card, NULL);
474
475
0
  if (rv != SC_SUCCESS)
476
0
    rv = SC_ERROR_INVALID_CARD;
477
478
  /* Free private data on error */
479
0
  if (rv != SC_SUCCESS) {
480
0
    free(card->drv_data);
481
0
    card->drv_data = NULL;
482
0
  }
483
0
  LOG_FUNC_RETURN(ctx, rv);
484
0
}
485
486
static int
487
authentic_erase_binary(struct sc_card *card, unsigned int offs, size_t count, unsigned long flags)
488
0
{
489
0
  struct sc_context *ctx = card->ctx;
490
0
  int rv;
491
0
  unsigned char *buf_zero = NULL;
492
493
0
  LOG_FUNC_CALLED(ctx);
494
0
  if (!count)
495
0
    LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "'ERASE BINARY' with ZERO count not supported");
496
497
0
  buf_zero = calloc(1, count);
498
0
  if (!buf_zero)
499
0
    LOG_TEST_RET(ctx, SC_ERROR_OUT_OF_MEMORY, "cannot allocate buff 'zero'");
500
501
0
  rv = sc_update_binary(card, offs, buf_zero, count, flags);
502
0
  free(buf_zero);
503
504
0
  LOG_FUNC_RETURN(ctx, rv);
505
0
}
506
507
508
static int
509
authentic_set_current_files(struct sc_card *card, struct sc_path *path,
510
    unsigned char *resp, size_t resplen, struct sc_file **file_out)
511
0
{
512
0
  struct sc_context *ctx = card->ctx;
513
0
  struct sc_file *file = NULL;
514
0
  int rv;
515
516
0
  LOG_FUNC_CALLED(ctx);
517
0
  if (resplen)   {
518
0
    switch (resp[0]) {
519
0
    case 0x62:
520
0
    case 0x6F:
521
0
      file = sc_file_new();
522
0
      if (file == NULL)
523
0
        LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY);
524
0
      if (path)
525
0
        file->path = *path;
526
527
0
      rv = authentic_process_fci(card, file, resp, resplen);
528
0
      if (rv != SC_SUCCESS) {
529
0
        sc_file_free(file);
530
0
        LOG_TEST_RET(ctx, rv, "cannot set 'current file': FCI process error");
531
0
      }
532
533
0
      break;
534
0
    default:
535
0
      LOG_FUNC_RETURN(ctx, SC_ERROR_UNKNOWN_DATA_RECEIVED);
536
0
    }
537
538
0
    if (file_out)
539
0
      *file_out = file;
540
0
    else
541
0
      sc_file_free(file);
542
0
  }
543
544
0
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
545
0
}
546
547
548
static int
549
authentic_select_mf(struct sc_card *card, struct sc_file **file_out)
550
0
{
551
0
  struct sc_context *ctx = card->ctx;
552
0
  struct sc_path mfpath;
553
0
  int rv;
554
555
0
  struct sc_apdu apdu;
556
0
  unsigned char rbuf[SC_MAX_APDU_BUFFER_SIZE];
557
558
0
  LOG_FUNC_CALLED(ctx);
559
560
0
  sc_format_path("3F00", &mfpath);
561
0
  mfpath.type = SC_PATH_TYPE_PATH;
562
563
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xA4, 0x00, 0x00);
564
565
0
  apdu.resp = rbuf;
566
0
  apdu.resplen = sizeof(rbuf);
567
568
0
  rv = sc_transmit_apdu(card, &apdu);
569
0
  LOG_TEST_RET(ctx, rv, "APDU transmit failed");
570
0
  rv = sc_check_sw(card, apdu.sw1, apdu.sw2);
571
0
  LOG_TEST_RET(ctx, rv, "authentic_select_file() check SW failed");
572
573
0
  rv = authentic_set_current_files(card, &mfpath, apdu.resp, apdu.resplen, file_out);
574
0
  LOG_TEST_RET(ctx, rv, "authentic_select_file() cannot set 'current_file'");
575
576
0
  LOG_FUNC_RETURN(ctx, rv);
577
0
}
578
579
static int
580
authentic_select_file(struct sc_card *card, const struct sc_path *path,
581
     struct sc_file **file_out)
582
0
{
583
0
  struct sc_context *ctx = card->ctx;
584
0
  struct sc_apdu apdu;
585
0
  struct sc_path lpath;
586
0
  unsigned char rbuf[SC_MAX_APDU_BUFFER_SIZE];
587
0
  size_t pathlen;
588
0
  int rv;
589
590
0
  LOG_FUNC_CALLED(ctx);
591
592
0
  memcpy(&lpath, path, sizeof(struct sc_path));
593
594
0
  if (lpath.type == SC_PATH_TYPE_PATH && (lpath.len == 2))
595
0
    lpath.type = SC_PATH_TYPE_FILE_ID;
596
597
0
  pathlen = lpath.len;
598
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0xA4, 0x00, 0x00);
599
600
0
  if (card->type != SC_CARD_TYPE_OBERTHUR_AUTHENTIC_3_2)
601
0
    LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "Unsupported card");
602
603
0
  if (lpath.type == SC_PATH_TYPE_FILE_ID)   {
604
0
    apdu.p1 = 0x00;
605
0
  }
606
0
  else if (lpath.type == SC_PATH_TYPE_PATH)  {
607
0
    apdu.p1 = 0x08;
608
0
  }
609
0
  else if (lpath.type == SC_PATH_TYPE_FROM_CURRENT)  {
610
0
    apdu.p1 = 0x09;
611
0
  }
612
0
  else if (lpath.type == SC_PATH_TYPE_DF_NAME)   {
613
0
    apdu.p1 = 4;
614
0
  }
615
0
  else if (lpath.type == SC_PATH_TYPE_PARENT)   {
616
0
    apdu.p1 = 0x03;
617
0
    pathlen = 0;
618
0
    apdu.cse = SC_APDU_CASE_2_SHORT;
619
0
  }
620
0
  else   {
621
0
    sc_log(ctx, "Invalid PATH type: 0x%X", lpath.type);
622
0
    LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "authentic_select_file() invalid PATH type");
623
0
  }
624
625
0
  apdu.lc = pathlen;
626
0
  apdu.data = lpath.value;
627
0
  apdu.datalen = pathlen;
628
629
0
  if (apdu.cse == SC_APDU_CASE_4_SHORT || apdu.cse == SC_APDU_CASE_2_SHORT)   {
630
0
    apdu.resp = rbuf;
631
0
    apdu.resplen = sizeof(rbuf);
632
0
    apdu.le = 256;
633
0
  }
634
635
0
  rv = sc_transmit_apdu(card, &apdu);
636
0
  LOG_TEST_RET(ctx, rv, "APDU transmit failed");
637
0
  rv = sc_check_sw(card, apdu.sw1, apdu.sw2);
638
0
  LOG_TEST_RET(ctx, rv, "authentic_select_file() check SW failed");
639
640
0
  rv = authentic_set_current_files(card, &lpath, apdu.resp, apdu.resplen, file_out);
641
0
  LOG_TEST_RET(ctx, rv, "authentic_select_file() cannot set 'current_file'");
642
643
0
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
644
0
}
645
646
static int
647
authentic_read_binary(struct sc_card *card, unsigned int idx,
648
    unsigned char *buf, size_t count, unsigned long *flags)
649
0
{
650
0
  struct sc_context *ctx = card->ctx;
651
0
  struct sc_apdu apdu;
652
0
  size_t sz, rest, ret_count = 0;
653
0
  int rv = SC_ERROR_INVALID_ARGUMENTS;
654
655
0
  LOG_FUNC_CALLED(ctx);
656
0
  sc_log(ctx, "offs:%i,count:%zu,max_recv_size:%zu", idx, count, card->max_recv_size);
657
658
0
  rest = count;
659
0
  while(rest)   {
660
0
    sz = rest > 256 ? 256 : rest;
661
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xB0, (idx >> 8) & 0x7F, idx & 0xFF);
662
0
    apdu.le = sz;
663
0
    apdu.resplen = sz;
664
0
    apdu.resp = (buf + ret_count);
665
666
0
    rv = sc_transmit_apdu(card, &apdu);
667
0
    if(!rv)
668
0
      ret_count += apdu.resplen;
669
0
    else
670
0
      break;
671
672
0
    idx += sz;
673
0
    rest -= sz;
674
0
  }
675
676
0
  if (rv)   {
677
0
    LOG_TEST_RET(ctx, SC_ERROR_INTERNAL, "authentic_read_binary() failed");
678
0
    LOG_FUNC_RETURN(ctx, (int)count);
679
0
  }
680
681
0
  rv = sc_check_sw(card, apdu.sw1, apdu.sw2);
682
0
  if (!rv)
683
0
    count = ret_count;
684
685
0
  LOG_TEST_RET(ctx, rv, "authentic_read_binary() failed");
686
0
  LOG_FUNC_RETURN(ctx, (int)count);
687
0
}
688
689
690
static int
691
authentic_write_binary(struct sc_card *card, unsigned int idx,
692
    const unsigned char *buf, size_t count, unsigned long flags)
693
0
{
694
0
  struct sc_context *ctx = card->ctx;
695
0
  struct sc_apdu apdu;
696
0
  size_t sz, rest;
697
0
  int rv = SC_ERROR_INVALID_ARGUMENTS;
698
699
0
  LOG_FUNC_CALLED(ctx);
700
0
  sc_log(ctx, "offs:%i,count:%zu,max_send_size:%zu", idx, count, card->max_send_size);
701
702
0
  rest = count;
703
0
  while(rest)   {
704
0
    sz = rest > 255 ? 255 : rest;
705
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xD0, (idx >> 8) & 0x7F, idx & 0xFF);
706
0
    apdu.lc = sz;
707
0
    apdu.datalen = sz;
708
0
    apdu.data = buf + count - rest;
709
710
0
    rv = sc_transmit_apdu(card, &apdu);
711
0
    if(rv)
712
0
      break;
713
714
0
    idx += sz;
715
0
    rest -= sz;
716
0
  }
717
718
0
  if (rv)
719
0
  {
720
0
    LOG_TEST_RET(ctx, SC_ERROR_INTERNAL, "authentic_write_binary() failed");
721
0
    LOG_FUNC_RETURN(ctx, (int)count);
722
0
  }
723
724
0
  rv = sc_check_sw(card, apdu.sw1, apdu.sw2);
725
726
0
  LOG_TEST_RET(ctx, rv, "authentic_write_binary() failed");
727
0
  LOG_FUNC_RETURN(ctx, (int)count);
728
0
}
729
730
731
static int
732
authentic_update_binary(struct sc_card *card, unsigned int idx,
733
    const unsigned char *buf, size_t count, unsigned long flags)
734
0
{
735
0
  struct sc_context *ctx = card->ctx;
736
0
  struct sc_apdu apdu;
737
0
  size_t sz, rest;
738
0
  int rv = SC_ERROR_INVALID_ARGUMENTS;
739
740
0
  LOG_FUNC_CALLED(ctx);
741
0
  sc_log(ctx, "offs:%i,count:%zu,max_send_size:%zu", idx, count, card->max_send_size);
742
743
0
  rest = count;
744
0
  while(rest)   {
745
0
    sz = rest > 255 ? 255 : rest;
746
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xD6, (idx >> 8) & 0x7F, idx & 0xFF);
747
0
    apdu.lc = sz;
748
0
    apdu.datalen = sz;
749
0
    apdu.data = buf + count - rest;
750
751
0
    rv = sc_transmit_apdu(card, &apdu);
752
0
    if(rv)
753
0
      break;
754
755
0
    idx += sz;
756
0
    rest -= sz;
757
0
  }
758
759
0
  if (rv)
760
0
  {
761
0
    LOG_TEST_RET(ctx, SC_ERROR_INTERNAL, "authentic_update_binary() failed");
762
0
    LOG_FUNC_RETURN(ctx, (int)count);
763
0
  }
764
765
0
  rv = sc_check_sw(card, apdu.sw1, apdu.sw2);
766
767
0
  LOG_TEST_RET(ctx, rv, "authentic_update_binary() failed");
768
0
  LOG_FUNC_RETURN(ctx, (int)count);
769
0
}
770
771
772
static int
773
authentic_process_fci(struct sc_card *card, struct sc_file *file,
774
     const unsigned char *buf, size_t buflen)
775
0
{
776
0
  struct sc_context *ctx = card->ctx;
777
0
  size_t taglen;
778
0
  int rv;
779
0
  unsigned ii;
780
0
  const unsigned char *tag = NULL;
781
0
  unsigned char ops_DF[8] = {
782
0
    SC_AC_OP_CREATE, SC_AC_OP_DELETE, SC_AC_OP_CRYPTO, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
783
0
  };
784
0
  unsigned char ops_EF[8] = {
785
0
    SC_AC_OP_READ, SC_AC_OP_DELETE, SC_AC_OP_UPDATE, SC_AC_OP_RESIZE, 0xFF, 0xFF, 0xFF, 0xFF
786
0
  };
787
788
0
  LOG_FUNC_CALLED(ctx);
789
790
0
  tag = sc_asn1_find_tag(card->ctx,  buf, buflen, 0x6F, &taglen);
791
0
  if (tag != NULL) {
792
0
    sc_log(ctx, "  FCP length %zu", taglen);
793
0
    buf = tag;
794
0
    buflen = taglen;
795
0
  }
796
797
0
  tag = sc_asn1_find_tag(card->ctx,  buf, buflen, 0x62, &taglen);
798
0
  if (tag != NULL) {
799
0
    sc_log(ctx, "  FCP length %zu", taglen);
800
0
    buf = tag;
801
0
    buflen = taglen;
802
0
  }
803
804
0
  rv = iso_ops->process_fci(card, file, buf, buflen);
805
0
  LOG_TEST_RET(ctx, rv, "ISO parse FCI failed");
806
807
0
  if (!file->sec_attr_len)   {
808
0
    sc_log_hex(ctx, "ACLs not found in data", buf, buflen);
809
0
    sc_log(ctx, "Path:%s; Type:%X; PathType:%X", sc_print_path(&file->path), file->type, file->path.type);
810
0
    if (file->path.type == SC_PATH_TYPE_DF_NAME || file->type == SC_FILE_TYPE_DF)   {
811
0
      file->type = SC_FILE_TYPE_DF;
812
0
    }
813
0
    else   {
814
0
      LOG_TEST_RET(ctx, SC_ERROR_OBJECT_NOT_FOUND, "ACLs tag missing");
815
0
    }
816
0
  }
817
818
0
  sc_log_hex(ctx, "ACL data", file->sec_attr, file->sec_attr_len);
819
0
  for (ii = 0; ii < file->sec_attr_len / 2 && ii < sizeof ops_DF; ii++)  {
820
0
    unsigned char op = file->type == SC_FILE_TYPE_DF ? ops_DF[ii] : ops_EF[ii];
821
0
    unsigned char acl = *(file->sec_attr + ii*2);
822
0
    unsigned char cred_id = *(file->sec_attr + ii*2 + 1);
823
0
    unsigned sc = acl * 0x100 + cred_id;
824
825
0
    sc_log(ctx, "ACL(%i) op 0x%X, acl %X:%X", ii, op, acl, cred_id);
826
0
    if (op == 0xFF)
827
0
      ;
828
0
    else if (!acl && !cred_id)
829
0
      sc_file_add_acl_entry(file, op, SC_AC_NONE, 0);
830
0
    else if (acl == 0xFF)
831
0
      sc_file_add_acl_entry(file, op, SC_AC_NEVER, 0);
832
0
    else if (acl & AUTHENTIC_AC_SM_MASK)
833
0
      sc_file_add_acl_entry(file, op, SC_AC_SCB, sc);
834
0
    else if (cred_id)
835
0
      sc_file_add_acl_entry(file, op, SC_AC_CHV, cred_id);
836
0
    else
837
0
      sc_file_add_acl_entry(file, op, SC_AC_NEVER, 0);
838
0
  }
839
840
0
  LOG_FUNC_RETURN(ctx, 0);
841
0
}
842
843
844
static int
845
authentic_fcp_encode(struct sc_card *card, struct sc_file *file, unsigned char *out, size_t out_len)
846
0
{
847
0
  struct sc_context *ctx = card->ctx;
848
0
  unsigned char buf[0x80];
849
0
  size_t ii, offs;
850
0
  unsigned char ops_ef[4] = { SC_AC_OP_READ, SC_AC_OP_DELETE, SC_AC_OP_UPDATE, SC_AC_OP_RESIZE };
851
0
  unsigned char ops_df[3] = { SC_AC_OP_CREATE, SC_AC_OP_DELETE, SC_AC_OP_CRYPTO };
852
0
  unsigned char *ops = file->type == SC_FILE_TYPE_DF ? ops_df : ops_ef;
853
0
  size_t ops_len = file->type == SC_FILE_TYPE_DF ? 3 : 4;
854
855
0
  LOG_FUNC_CALLED(ctx);
856
857
0
  offs = 0;
858
0
  buf[offs++] = ISO7816_TAG_FCP_SIZE;
859
0
  buf[offs++] = 2;
860
0
  buf[offs++] = (file->size >> 8) & 0xFF;
861
0
  buf[offs++] = file->size & 0xFF;
862
863
0
  buf[offs++] = ISO7816_TAG_FCP_TYPE;
864
0
  buf[offs++] = 1;
865
0
  buf[offs++] = file->type == SC_FILE_TYPE_DF ? ISO7816_FILE_TYPE_DF : ISO7816_FILE_TYPE_TRANSPARENT_EF;
866
867
0
  buf[offs++] = ISO7816_TAG_FCP_FID;
868
0
  buf[offs++] = 2;
869
0
  buf[offs++] = (file->id >> 8) & 0xFF;
870
0
  buf[offs++] = file->id & 0xFF;
871
872
0
  buf[offs++] = ISO7816_TAG_FCP_ACLS;
873
0
  buf[offs++] = ops_len * 2;
874
0
  for (ii=0; ii < ops_len; ii++) {
875
0
    const struct sc_acl_entry *entry;
876
877
0
    entry = sc_file_get_acl_entry(file, ops[ii]);
878
0
    sc_log(ctx, "acl entry(method:%X,ref:%X)", entry->method, entry->key_ref);
879
880
0
    if (entry->method == SC_AC_NEVER)   {
881
      /* TODO: After development change for 0xFF */
882
0
      buf[offs++] = 0x00;
883
0
      buf[offs++] = 0x00;
884
0
    }
885
0
    else if (entry->method == SC_AC_NONE)   {
886
0
      buf[offs++] = 0x00;
887
0
      buf[offs++] = 0x00;
888
0
    }
889
0
    else if (entry->method == SC_AC_CHV)   {
890
0
      if (!(entry->key_ref & AUTHENTIC_V3_CREDENTIAL_ID_MASK)
891
0
          || (entry->key_ref & ~AUTHENTIC_V3_CREDENTIAL_ID_MASK))
892
0
        LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "Non supported Credential Reference");
893
0
      buf[offs++] = 0x00;
894
0
      buf[offs++] = 0x01 << (entry->key_ref - 1);
895
0
    }
896
0
    else
897
0
      LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "Non supported AC method");
898
0
  }
899
900
0
  if (out)   {
901
0
    if (out_len < offs)
902
0
      LOG_TEST_RET(ctx, SC_ERROR_BUFFER_TOO_SMALL, "Buffer too small to encode FCP");
903
0
    memcpy(out, buf, offs);
904
0
  }
905
906
0
  LOG_FUNC_RETURN(ctx, (int)offs);
907
0
}
908
909
910
static int
911
authentic_create_file(struct sc_card *card, struct sc_file *file)
912
0
{
913
0
  struct sc_context *ctx = card->ctx;
914
0
  struct sc_apdu apdu;
915
0
  unsigned char sbuf[0x100];
916
0
  size_t sbuf_len;
917
0
  struct sc_path path;
918
0
  int rv;
919
920
0
  LOG_FUNC_CALLED(ctx);
921
922
0
  if (file->type != SC_FILE_TYPE_WORKING_EF)
923
0
    LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "Creation of the file with of this type is not supported");
924
925
0
  if (file->path.len < 2)
926
0
    LOG_TEST_RET(ctx, SC_ERROR_INVALID_ARGUMENTS, "Invalid path length");
927
928
0
  rv = authentic_fcp_encode(card, file, sbuf + 2, sizeof(sbuf)-2);
929
0
  LOG_TEST_RET(ctx, rv, "FCP encode error");
930
0
  sbuf_len = rv;
931
932
0
  sbuf[0] = ISO7816_TAG_FCP;
933
0
  sbuf[1] = sbuf_len;
934
935
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xE0, 0, 0);
936
0
  apdu.data = sbuf;
937
0
  apdu.datalen = sbuf_len + 2;
938
0
  apdu.lc = sbuf_len + 2;
939
940
0
  rv = sc_transmit_apdu(card, &apdu);
941
0
  LOG_TEST_RET(ctx, rv, "APDU transmit failed");
942
0
  rv = sc_check_sw(card, apdu.sw1, apdu.sw2);
943
0
  LOG_TEST_RET(ctx, rv, "authentic_create_file() create file error");
944
945
0
  path = file->path;
946
0
  memcpy(path.value, path.value + path.len - 2, 2);
947
0
  path.len = 2;
948
0
  rv = authentic_set_current_files(card, &path, sbuf, sbuf_len + 2, NULL);
949
0
  LOG_TEST_RET(ctx, rv, "authentic_select_file() cannot set 'current_file'");
950
951
0
  LOG_FUNC_RETURN(ctx, rv);
952
0
}
953
954
955
static int
956
authentic_delete_file(struct sc_card *card, const struct sc_path *path)
957
0
{
958
0
  struct sc_context *ctx = card->ctx;
959
0
  struct sc_apdu apdu;
960
0
  unsigned char p1;
961
0
  int rv, ii;
962
963
0
  LOG_FUNC_CALLED(ctx);
964
965
0
  if (!path || path->len < 2)
966
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
967
968
0
  for (ii=0, p1 = 0x02; ii<2; ii++, p1 = 0x01)   {
969
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xE4, p1, 0x00);
970
0
    apdu.data = path->value + path->len - 2;
971
0
    apdu.datalen = 2;
972
0
    apdu.lc = 2;
973
974
0
    rv = sc_transmit_apdu(card, &apdu);
975
0
    LOG_TEST_RET(ctx, rv, "APDU transmit failed");
976
0
    rv = sc_check_sw(card, apdu.sw1, apdu.sw2);
977
0
    if (rv != SC_ERROR_FILE_NOT_FOUND || p1 != 0x02)
978
0
      break;
979
0
  }
980
0
  LOG_TEST_RET(ctx, rv, "Delete file failed");
981
982
0
  LOG_FUNC_RETURN(ctx, rv);
983
0
}
984
985
986
static int
987
authentic_chv_verify_pinpad(struct sc_card *card, struct sc_pin_cmd_data *pin_cmd)
988
0
{
989
0
  struct sc_context *ctx = card->ctx;
990
0
  unsigned char buffer[0x100];
991
0
  struct sc_pin_cmd_pin *pin1 = &pin_cmd->pin1;
992
0
  int rv;
993
994
0
  LOG_FUNC_CALLED(ctx);
995
0
  sc_log(ctx, "Verify PIN(ref:%i) with pin-pad", pin_cmd->pin_reference);
996
997
0
  rv = authentic_pin_is_verified(card, pin_cmd);
998
0
  if (!rv)
999
0
    LOG_FUNC_RETURN(ctx, rv);
1000
1001
0
  if (!card->reader || !card->reader->ops || !card->reader->ops->perform_verify)   {
1002
0
    sc_log(ctx, "Reader not ready for PIN PAD");
1003
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_READER);
1004
0
  }
1005
1006
0
  pin1->len = pin1->min_length;
1007
0
  pin1->max_length = 8;
1008
1009
0
  memset(buffer, pin1->pad_char, sizeof(buffer));
1010
0
  pin1->data = buffer;
1011
1012
0
  pin_cmd->cmd = SC_PIN_CMD_VERIFY;
1013
0
  pin_cmd->flags |= SC_PIN_CMD_USE_PINPAD;
1014
1015
0
  rv = iso_ops->pin_cmd(card, pin_cmd);
1016
1017
0
  LOG_FUNC_RETURN(ctx, rv);
1018
0
}
1019
1020
1021
static int
1022
authentic_chv_verify(struct sc_card *card, struct sc_pin_cmd_data *pin_cmd)
1023
0
{
1024
0
  struct sc_context *ctx = card->ctx;
1025
0
  struct sc_apdu apdu;
1026
0
  struct sc_pin_cmd_pin *pin1 = &pin_cmd->pin1;
1027
0
  unsigned char pin_buff[SC_MAX_APDU_BUFFER_SIZE];
1028
0
  size_t pin_len;
1029
0
  int rv;
1030
1031
0
  LOG_FUNC_CALLED(ctx);
1032
0
  sc_log(ctx, "CHV PIN reference %i, pin1(%p,len:%zu)", pin_cmd->pin_reference, pin1->data, pin1->len);
1033
1034
0
  if (pin1->data && !pin1->len)   {
1035
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x20, 0, pin_cmd->pin_reference);
1036
0
  }
1037
0
  else if (pin1->data && pin1->len)   {
1038
0
    if (pin1->len > sizeof(pin_buff)) {
1039
0
      LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_PIN_LENGTH);
1040
0
    }
1041
0
    memcpy(pin_buff, pin1->data, pin1->len);
1042
0
    pin_len = pin1->len;
1043
1044
0
    if (pin1->pad_length && pin_cmd->flags & SC_PIN_CMD_NEED_PADDING)   {
1045
0
      if (pin1->len > pin1->pad_length || pin1->pad_length > sizeof(pin_buff)) {
1046
0
        LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_PIN_LENGTH);
1047
0
      }
1048
0
      if (pin1->len < sizeof(pin_buff)) {
1049
0
        memset(pin_buff + pin1->len, pin1->pad_char,
1050
0
            pin1->pad_length - pin1->len);
1051
0
      }
1052
0
      pin_len = pin1->pad_length;
1053
0
    }
1054
1055
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x20, 0, pin_cmd->pin_reference);
1056
0
    apdu.data = pin_buff;
1057
0
    apdu.datalen = pin_len;
1058
0
    apdu.lc = pin_len;
1059
0
  }
1060
0
  else if ((card->reader->capabilities & SC_READER_CAP_PIN_PAD) && !pin1->data && !pin1->len)   {
1061
0
    rv = authentic_chv_verify_pinpad(card, pin_cmd);
1062
0
    sc_log(ctx, "authentic_chv_verify() authentic_chv_verify_pinpad returned %i", rv);
1063
0
    LOG_FUNC_RETURN(ctx, rv);
1064
0
  }
1065
0
  else   {
1066
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_SUPPORTED);
1067
0
  }
1068
1069
0
  rv = sc_transmit_apdu(card, &apdu);
1070
0
  LOG_TEST_RET(ctx, rv, "APDU transmit failed");
1071
1072
0
  if (apdu.sw1 == 0x63 && (apdu.sw2 & 0xF0) == 0xC0)   {
1073
0
    pin1->tries_left = apdu.sw2 & 0x0F;
1074
0
  }
1075
1076
0
  rv = sc_check_sw(card, apdu.sw1, apdu.sw2);
1077
1078
0
  LOG_FUNC_RETURN(ctx, rv);
1079
0
}
1080
1081
1082
static int
1083
authentic_pin_is_verified(struct sc_card *card, struct sc_pin_cmd_data *pin_cmd_data)
1084
0
{
1085
0
  struct sc_context *ctx = card->ctx;
1086
0
  struct sc_pin_cmd_data pin_cmd;
1087
0
  int rv;
1088
1089
0
  LOG_FUNC_CALLED(ctx);
1090
1091
0
  if (pin_cmd_data->pin_type != SC_AC_CHV)
1092
0
    LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "PIN type is not supported for the verification");
1093
1094
0
  pin_cmd = *pin_cmd_data;
1095
0
  pin_cmd.pin1.data = (unsigned char *)"";
1096
0
  pin_cmd.pin1.len = 0;
1097
1098
0
  rv = authentic_chv_verify(card, &pin_cmd);
1099
1100
0
  LOG_FUNC_RETURN(ctx, rv);
1101
0
}
1102
1103
1104
static int
1105
authentic_pin_verify(struct sc_card *card, struct sc_pin_cmd_data *pin_cmd)
1106
0
{
1107
0
  struct sc_context *ctx = card->ctx;
1108
0
  struct authentic_private_data *prv_data = (struct authentic_private_data *) card->drv_data;
1109
0
  unsigned char pin_sha1[SHA_DIGEST_LENGTH];
1110
0
  int rv;
1111
1112
0
  LOG_FUNC_CALLED(ctx);
1113
0
  sc_log(ctx, "PIN(type:%X,reference:%X,data:%p,length:%zu)",
1114
0
      pin_cmd->pin_type, pin_cmd->pin_reference, pin_cmd->pin1.data, pin_cmd->pin1.len);
1115
1116
0
  if (pin_cmd->pin_reference < 0 || pin_cmd->pin_reference >= AUTHENTIC_N_PINS) {
1117
0
    LOG_TEST_RET(ctx, SC_ERROR_INVALID_ARGUMENTS, "PIN reference out of bounds");
1118
0
  }
1119
1120
0
  if (pin_cmd->pin1.data && !pin_cmd->pin1.len)   {
1121
0
    pin_cmd->pin1.tries_left = -1;
1122
0
    rv = authentic_pin_is_verified(card, pin_cmd);
1123
0
    LOG_FUNC_RETURN(ctx, rv);
1124
0
  }
1125
1126
0
  if (pin_cmd->pin1.data)
1127
0
    SHA1(pin_cmd->pin1.data, pin_cmd->pin1.len, pin_sha1);
1128
0
  else
1129
0
    SHA1((unsigned char *)"", 0, pin_sha1);
1130
1131
0
  if (!memcmp(pin_sha1, prv_data->pins_sha1[pin_cmd->pin_reference], SHA_DIGEST_LENGTH))   {
1132
0
    sc_log(ctx, "Already verified");
1133
0
    LOG_FUNC_RETURN(ctx, SC_SUCCESS);
1134
0
  }
1135
1136
0
  memset(prv_data->pins_sha1[pin_cmd->pin_reference], 0, sizeof(prv_data->pins_sha1[0]));
1137
1138
0
  rv = authentic_pin_get_policy(card, pin_cmd, NULL);
1139
0
  LOG_TEST_RET(ctx, rv, "Get 'PIN policy' error");
1140
1141
0
  if (pin_cmd->pin1.len > pin_cmd->pin1.max_length)
1142
0
    LOG_TEST_RET(ctx, SC_ERROR_INVALID_PIN_LENGTH, "PIN policy check failed");
1143
1144
0
  pin_cmd->pin1.tries_left = -1;
1145
0
  rv = authentic_chv_verify(card, pin_cmd);
1146
0
  LOG_TEST_RET(ctx, rv, "PIN CHV verification error");
1147
1148
0
  memcpy(prv_data->pins_sha1[pin_cmd->pin_reference], pin_sha1, SHA_DIGEST_LENGTH);
1149
0
  LOG_FUNC_RETURN(ctx, rv);
1150
0
}
1151
1152
1153
static int
1154
authentic_pin_change_pinpad(struct sc_card *card, unsigned reference, int *tries_left)
1155
0
{
1156
0
  struct sc_context *ctx = card->ctx;
1157
0
  struct sc_pin_cmd_data pin_cmd;
1158
0
  unsigned char pin1_data[SC_MAX_APDU_BUFFER_SIZE], pin2_data[SC_MAX_APDU_BUFFER_SIZE];
1159
0
  int rv;
1160
1161
0
  LOG_FUNC_CALLED(ctx);
1162
0
  sc_log(ctx, "CHV PINPAD PIN reference %i", reference);
1163
1164
0
  if (!card->reader || !card->reader->ops || !card->reader->ops->perform_verify)   {
1165
0
    sc_log(ctx, "Reader not ready for PIN PAD");
1166
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_READER);
1167
0
  }
1168
1169
0
  memset(&pin_cmd, 0, sizeof(pin_cmd));
1170
0
  pin_cmd.pin_type = SC_AC_CHV;
1171
0
  pin_cmd.pin_reference = reference;
1172
0
  pin_cmd.cmd = SC_PIN_CMD_CHANGE;
1173
0
  pin_cmd.flags |= SC_PIN_CMD_USE_PINPAD | SC_PIN_CMD_NEED_PADDING;
1174
1175
0
  rv = authentic_pin_get_policy(card, &pin_cmd, NULL);
1176
0
  LOG_TEST_RET(ctx, rv, "Get 'PIN policy' error");
1177
1178
0
  memset(pin1_data, pin_cmd.pin1.pad_char, sizeof(pin1_data));
1179
0
  pin_cmd.pin1.data = pin1_data;
1180
1181
0
  pin_cmd.pin1.len = pin_cmd.pin1.min_length;
1182
0
  pin_cmd.pin1.max_length = 8;
1183
1184
0
  memcpy(&pin_cmd.pin2, &pin_cmd.pin1, sizeof(pin_cmd.pin1));
1185
0
  memset(pin2_data, pin_cmd.pin2.pad_char, sizeof(pin2_data));
1186
0
  pin_cmd.pin2.data = pin2_data;
1187
1188
0
  sc_log(ctx, "PIN1 lengths max/min/pad: %zu/%zu/%zu",
1189
0
      pin_cmd.pin1.max_length, pin_cmd.pin1.min_length, pin_cmd.pin1.pad_length);
1190
0
  sc_log(ctx, "PIN2 lengths max/min/pad: %zu/%zu/%zu",
1191
0
      pin_cmd.pin2.max_length, pin_cmd.pin2.min_length, pin_cmd.pin2.pad_length);
1192
1193
0
  rv = iso_ops->pin_cmd(card, &pin_cmd);
1194
1195
0
  LOG_FUNC_RETURN(ctx, rv);
1196
0
}
1197
1198
1199
static int
1200
authentic_pin_change(struct sc_card *card, struct sc_pin_cmd_data *data)
1201
0
{
1202
0
  struct sc_context *ctx = card->ctx;
1203
0
  struct authentic_private_data *prv_data = (struct authentic_private_data *) card->drv_data;
1204
0
  struct sc_apdu apdu;
1205
0
  unsigned char pin_data[SC_MAX_APDU_BUFFER_SIZE];
1206
0
  size_t offs;
1207
0
  int rv;
1208
1209
0
  rv = authentic_pin_get_policy(card, data, NULL);
1210
0
  LOG_TEST_RET(ctx, rv, "Get 'PIN policy' error");
1211
1212
0
  if (data->pin_reference < 0 || data->pin_reference >= AUTHENTIC_N_PINS) {
1213
0
    LOG_TEST_RET(ctx, SC_ERROR_INVALID_ARGUMENTS, "PIN reference out of bounds");
1214
0
  }
1215
1216
0
  memset(prv_data->pins_sha1[data->pin_reference], 0, sizeof(prv_data->pins_sha1[0]));
1217
1218
0
  if (!data->pin1.data && !data->pin1.len && !data->pin2.data && !data->pin2.len)   {
1219
0
    if (!(card->reader->capabilities & SC_READER_CAP_PIN_PAD))
1220
0
      LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "PIN pad not supported");
1221
0
    rv = authentic_pin_change_pinpad(card, data->pin_reference, &data->pin1.tries_left);
1222
0
    sc_log(ctx, "authentic_pin_cmd(SC_PIN_CMD_CHANGE) chv_change_pinpad returned %i", rv);
1223
0
    LOG_FUNC_RETURN(ctx, rv);
1224
0
  }
1225
1226
0
  if (card->max_send_size && (data->pin1.len + data->pin2.len > card->max_send_size))
1227
0
    LOG_TEST_RET(ctx, SC_ERROR_INVALID_PIN_LENGTH, "APDU transmit failed");
1228
1229
0
  memset(pin_data, data->pin1.pad_char, sizeof(pin_data));
1230
0
  offs = 0;
1231
0
  if (data->pin1.data && data->pin1.len)   {
1232
0
    if (data->pin1.pad_length > sizeof(pin_data) || data->pin1.len > sizeof(pin_data)) {
1233
0
      LOG_TEST_RET(ctx, SC_ERROR_INVALID_ARGUMENTS, "PIN length exceeds buffer");
1234
0
    }
1235
0
    memcpy(pin_data, data->pin1.data, data->pin1.len);
1236
0
    offs += data->pin1.pad_length;
1237
0
  }
1238
0
  if (data->pin2.data && data->pin2.len) {
1239
0
    if (data->pin2.len + offs > sizeof(pin_data)) {
1240
0
      LOG_TEST_RET(ctx, SC_ERROR_INVALID_ARGUMENTS, "PIN2 length exceeds buffer");
1241
0
    }
1242
0
    memcpy(pin_data + offs, data->pin2.data, data->pin2.len);
1243
0
  }
1244
1245
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x24, offs ? 0x00 : 0x01, data->pin_reference);
1246
0
  apdu.data = pin_data;
1247
0
  apdu.datalen = offs + data->pin1.pad_length;
1248
0
  apdu.lc = offs + data->pin1.pad_length;
1249
1250
0
  rv = sc_transmit_apdu(card, &apdu);
1251
0
  LOG_TEST_RET(ctx, rv, "APDU transmit failed");
1252
0
  rv = sc_check_sw(card, apdu.sw1, apdu.sw2);
1253
1254
0
  LOG_FUNC_RETURN(ctx, rv);
1255
0
}
1256
1257
1258
static int
1259
authentic_chv_set_pinpad(struct sc_card *card, unsigned char reference)
1260
0
{
1261
0
  struct sc_context *ctx = card->ctx;
1262
0
  struct sc_pin_cmd_data pin_cmd;
1263
0
  unsigned char pin_data[0x100];
1264
0
  int rv;
1265
1266
0
  LOG_FUNC_CALLED(ctx);
1267
0
  sc_log(ctx, "Set CHV PINPAD PIN reference %i", reference);
1268
1269
0
  if (!card->reader || !card->reader->ops || !card->reader->ops->perform_verify)   {
1270
0
    sc_log(ctx, "Reader not ready for PIN PAD");
1271
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_READER);
1272
0
  }
1273
1274
0
  memset(&pin_cmd, 0, sizeof(pin_cmd));
1275
0
  pin_cmd.pin_type = SC_AC_CHV;
1276
0
  pin_cmd.pin_reference = reference;
1277
0
  pin_cmd.cmd = SC_PIN_CMD_UNBLOCK;
1278
0
  pin_cmd.flags |= SC_PIN_CMD_USE_PINPAD | SC_PIN_CMD_NEED_PADDING;
1279
1280
0
  rv = authentic_pin_get_policy(card, &pin_cmd, NULL);
1281
0
  LOG_TEST_RET(ctx, rv, "Get 'PIN policy' error");
1282
1283
0
  memset(pin_data, pin_cmd.pin1.pad_char, sizeof(pin_data));
1284
0
  pin_cmd.pin1.data = pin_data;
1285
1286
0
  pin_cmd.pin1.len = pin_cmd.pin1.min_length;
1287
0
        pin_cmd.pin1.max_length = 8;
1288
1289
0
  memcpy(&pin_cmd.pin2, &pin_cmd.pin1, sizeof(pin_cmd.pin1));
1290
0
  memset(&pin_cmd.pin1, 0, sizeof(pin_cmd.pin1));
1291
1292
0
  sc_log(ctx, "PIN2 max/min/pad %zu/%zu/%zu",
1293
0
      pin_cmd.pin2.max_length, pin_cmd.pin2.min_length, pin_cmd.pin2.pad_length);
1294
0
  rv = iso_ops->pin_cmd(card, &pin_cmd);
1295
1296
0
  LOG_FUNC_RETURN(ctx, rv);
1297
0
}
1298
1299
1300
static int
1301
authentic_pin_get_policy (struct sc_card *card, struct sc_pin_cmd_data *data, struct sc_acl_entry *acls)
1302
0
{
1303
0
  struct sc_context *ctx = card->ctx;
1304
0
  struct sc_apdu apdu;
1305
0
  unsigned char rbuf[0x100];
1306
0
  int ii, rv;
1307
1308
0
  LOG_FUNC_CALLED(ctx);
1309
0
  sc_log(ctx, "get PIN(type:%X,ref:%X,tries-left:%i)", data->pin_type, data->pin_reference, data->pin1.tries_left);
1310
1311
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xCA, 0x5F, data->pin_reference);
1312
0
  for (ii=0;ii<2;ii++)   {
1313
0
    apdu.le = sizeof(rbuf);
1314
0
    apdu.resp = rbuf;
1315
0
    apdu.resplen = sizeof(rbuf);
1316
1317
0
    rv = sc_transmit_apdu(card, &apdu);
1318
0
    LOG_TEST_RET(ctx, rv, "APDU transmit failed");
1319
0
    rv = sc_check_sw(card, apdu.sw1, apdu.sw2);
1320
1321
0
    if (rv != SC_ERROR_CLASS_NOT_SUPPORTED)
1322
0
      break;
1323
1324
0
    apdu.cla = 0x80;
1325
0
  }
1326
0
        LOG_TEST_RET(ctx, rv, "'GET DATA' error");
1327
1328
0
  data->pin1.tries_left = -1;
1329
1330
0
  rv = authentic_parse_credential_data(ctx, data, acls, apdu.resp, apdu.resplen);
1331
0
        LOG_TEST_RET(ctx, rv, "Cannot parse credential data");
1332
1333
0
  data->pin1.encoding = SC_PIN_ENCODING_ASCII;
1334
0
  data->pin1.offset = 5;
1335
0
  data->pin1.pad_char = 0xFF;
1336
0
  data->pin1.pad_length = data->pin1.max_length;
1337
0
  data->pin1.logged_in = SC_PIN_STATE_UNKNOWN;
1338
1339
0
  data->flags |= SC_PIN_CMD_NEED_PADDING;
1340
1341
0
  sc_log(ctx, "PIN policy: size max/min/pad %zu/%zu/%zu, tries max/left %i/%i",
1342
0
      data->pin1.max_length, data->pin1.min_length, data->pin1.pad_length,
1343
0
      data->pin1.max_tries, data->pin1.tries_left);
1344
1345
0
  LOG_FUNC_RETURN(ctx, rv);
1346
0
}
1347
1348
1349
static int
1350
authentic_pin_reset(struct sc_card *card, struct sc_pin_cmd_data *data)
1351
0
{
1352
0
  struct sc_context *ctx = card->ctx;
1353
0
  struct authentic_private_data *prv_data = (struct authentic_private_data *) card->drv_data;
1354
0
  struct sc_pin_cmd_data pin_cmd, puk_cmd;
1355
0
  struct sc_acl_entry acls[SC_MAX_SDO_ACLS];
1356
0
  struct sc_apdu apdu;
1357
0
  unsigned reference;
1358
0
  int rv, ii;
1359
1360
0
  LOG_FUNC_CALLED(ctx);
1361
0
  sc_log(ctx, "reset PIN (ref:%i,lengths %zu/%zu)", data->pin_reference, data->pin1.len, data->pin2.len);
1362
1363
0
  if (data->pin_reference < 0 || data->pin_reference >= AUTHENTIC_N_PINS) {
1364
0
    LOG_TEST_RET(ctx, SC_ERROR_INVALID_ARGUMENTS, "PIN reference out of bounds");
1365
0
  }
1366
1367
0
  memset(prv_data->pins_sha1[data->pin_reference], 0, sizeof(prv_data->pins_sha1[0]));
1368
1369
0
  memset(&pin_cmd, 0, sizeof(pin_cmd));
1370
0
  pin_cmd.pin_reference = data->pin_reference;
1371
0
  pin_cmd.pin_type = data->pin_type;
1372
0
  pin_cmd.pin1.tries_left = -1;
1373
1374
0
  memset(&acls, 0, sizeof(acls));
1375
0
  rv = authentic_pin_get_policy(card, &pin_cmd, acls);
1376
0
  LOG_TEST_RET(ctx, rv, "Get 'PIN policy' error");
1377
1378
0
  if (acls[AUTHENTIC_ACL_NUM_PIN_RESET].method == SC_AC_CHV)   {
1379
0
    for (ii=0;ii<8;ii++)   {
1380
0
      unsigned char mask = 0x01 << ii;
1381
0
      if (acls[AUTHENTIC_ACL_NUM_PIN_RESET].key_ref & mask)   {
1382
0
        memset(&puk_cmd, 0, sizeof(puk_cmd));
1383
0
        puk_cmd.pin_reference = ii + 1;
1384
1385
0
        rv = authentic_pin_get_policy(card, &puk_cmd, NULL);
1386
0
        LOG_TEST_RET(ctx, rv, "Get 'PIN policy' error");
1387
1388
0
        if (puk_cmd.pin_type == SC_AC_CHV)
1389
0
          break;
1390
0
      }
1391
0
    }
1392
0
    if (ii < 8)   {
1393
0
      puk_cmd.pin1.data = data->pin1.data;
1394
0
      puk_cmd.pin1.len = data->pin1.len;
1395
1396
0
      rv = authentic_pin_verify(card, &puk_cmd);
1397
1398
0
      LOG_TEST_RET(ctx, rv, "Cannot verify PUK");
1399
0
    }
1400
0
  }
1401
1402
0
  reference = data->pin_reference;
1403
0
  if (data->pin2.len)   {
1404
0
    unsigned char pin_data[SC_MAX_APDU_BUFFER_SIZE];
1405
1406
0
    if (data->pin2.len > sizeof(pin_data) || data->pin2.len > pin_cmd.pin1.pad_length)
1407
0
      LOG_TEST_RET(ctx, SC_ERROR_INVALID_PIN_LENGTH, "PIN data too long");
1408
1409
0
    memset(pin_data, pin_cmd.pin1.pad_char, sizeof(pin_data));
1410
0
    memcpy(pin_data, data->pin2.data, data->pin2.len);
1411
1412
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x2C, 0x02, reference);
1413
0
    apdu.data = pin_data;
1414
0
    apdu.datalen = pin_cmd.pin1.pad_length;
1415
0
    apdu.lc = pin_cmd.pin1.pad_length;
1416
1417
0
    rv = sc_transmit_apdu(card, &apdu);
1418
0
    LOG_TEST_RET(ctx, rv, "APDU transmit failed");
1419
0
    rv = sc_check_sw(card, apdu.sw1, apdu.sw2);
1420
0
    LOG_TEST_RET(ctx, rv, "PIN cmd failed");
1421
0
  }
1422
0
  else if (data->pin2.data) {
1423
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x2C, 3, reference);
1424
1425
0
    rv = sc_transmit_apdu(card, &apdu);
1426
0
    LOG_TEST_RET(ctx, rv, "APDU transmit failed");
1427
0
    rv = sc_check_sw(card, apdu.sw1, apdu.sw2);
1428
0
    LOG_TEST_RET(ctx, rv, "PIN cmd failed");
1429
0
  }
1430
0
  else   {
1431
0
    rv = authentic_chv_set_pinpad(card, reference);
1432
0
    LOG_TEST_RET(ctx, rv, "Failed to set PIN with pin-pad");
1433
0
  }
1434
1435
0
  LOG_FUNC_RETURN(ctx, rv);
1436
0
}
1437
1438
1439
static int
1440
authentic_pin_cmd(struct sc_card *card, struct sc_pin_cmd_data *data)
1441
0
{
1442
0
  struct sc_context *ctx = card->ctx;
1443
0
  int rv = SC_ERROR_INTERNAL;
1444
1445
0
  LOG_FUNC_CALLED(ctx);
1446
0
  sc_log(ctx, "PIN-CMD:%X,PIN(type:%X,ret:%i)", data->cmd, data->pin_type, data->pin_reference);
1447
0
  sc_log(ctx, "PIN1(%p,len:%zu,tries-left:%i)", data->pin1.data, data->pin1.len, data->pin1.tries_left);
1448
0
  sc_log(ctx, "PIN2(%p,len:%zu)", data->pin2.data, data->pin2.len);
1449
1450
0
  switch (data->cmd)   {
1451
0
  case SC_PIN_CMD_VERIFY:
1452
0
    rv = authentic_pin_verify(card, data);
1453
0
    break;
1454
0
  case SC_PIN_CMD_CHANGE:
1455
0
    rv = authentic_pin_change(card, data);
1456
0
    break;
1457
0
  case SC_PIN_CMD_UNBLOCK:
1458
0
    rv = authentic_pin_reset(card, data);
1459
0
    break;
1460
0
  case SC_PIN_CMD_GET_INFO:
1461
0
    rv = authentic_pin_get_policy(card, data, NULL);
1462
0
    break;
1463
0
  default:
1464
0
    LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "Unsupported PIN command");
1465
0
  }
1466
1467
0
  LOG_FUNC_RETURN(ctx, rv);
1468
0
}
1469
1470
1471
static int
1472
authentic_get_serialnr(struct sc_card *card, struct sc_serial_number *serial)
1473
0
{
1474
0
  struct sc_context *ctx = card->ctx;
1475
0
  struct authentic_private_data *prv_data = (struct authentic_private_data *) card->drv_data;
1476
0
  int rv;
1477
1478
0
  LOG_FUNC_CALLED(ctx);
1479
0
  if (!card->serialnr.len)   {
1480
0
    rv = authentic_get_cplc(card);
1481
0
    LOG_TEST_RET(ctx, rv, "get CPLC data error");
1482
1483
0
    card->serialnr.len = 4;
1484
0
    memcpy(card->serialnr.value, prv_data->cplc.value + 15, 4);
1485
1486
0
    sc_log(ctx, "serial %02X%02X%02X%02X",
1487
0
        card->serialnr.value[0], card->serialnr.value[1],
1488
0
        card->serialnr.value[2], card->serialnr.value[3]);
1489
0
  }
1490
1491
0
  if (serial)
1492
0
    memcpy(serial, &card->serialnr, sizeof(*serial));
1493
1494
0
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
1495
0
}
1496
1497
1498
static int
1499
authentic_get_challenge(struct sc_card *card, unsigned char *rnd, size_t len)
1500
0
{
1501
  /* 'GET CHALLENGE' returns always 24 bytes */
1502
0
  unsigned char rbuf[0x18];
1503
0
  size_t out_len;
1504
0
  int r;
1505
1506
0
  LOG_FUNC_CALLED(card->ctx);
1507
1508
0
  r = iso_ops->get_challenge(card, rbuf, sizeof rbuf);
1509
0
  LOG_TEST_RET(card->ctx, r, "GET CHALLENGE cmd failed");
1510
1511
0
  if (len < (size_t) r) {
1512
0
    out_len = len;
1513
0
  } else {
1514
0
    out_len = (size_t) r;
1515
0
  }
1516
0
  memcpy(rnd, rbuf, out_len);
1517
1518
0
  LOG_FUNC_RETURN(card->ctx, (int)out_len);
1519
0
}
1520
1521
1522
static int
1523
authentic_manage_sdo_encode_prvkey(struct sc_card *card, struct sc_pkcs15_prkey *prvkey,
1524
      unsigned char **out, size_t *out_len)
1525
0
{
1526
0
  struct sc_context *ctx = card->ctx;
1527
0
  struct sc_pkcs15_prkey_rsa rsa;
1528
0
  unsigned char *blob = NULL, *blob01 = NULL;
1529
0
  size_t blob_len = 0, blob01_len = 0;
1530
0
  int rv;
1531
1532
0
  if (!prvkey || !out || !out_len)
1533
0
    LOG_TEST_RET(ctx, SC_ERROR_INVALID_ARGUMENTS, "Invalid arguments");
1534
0
  if (prvkey->algorithm != SC_ALGORITHM_RSA)
1535
0
    LOG_TEST_RET(ctx, SC_ERROR_INVALID_DATA, "Invalid SDO operation");
1536
1537
0
  rsa = prvkey->u.rsa;
1538
0
  if (!rsa.p.len || !rsa.p.data || !rsa.q.len || !rsa.q.data ||
1539
0
      !rsa.iqmp.len || !rsa.iqmp.data || !rsa.dmp1.len || !rsa.dmp1.data ||
1540
0
      !rsa.dmq1.len || !rsa.dmq1.data || !rsa.modulus.len || !rsa.modulus.data ||
1541
0
      !rsa.exponent.len || !rsa.exponent.data)
1542
0
    LOG_TEST_RET(ctx, SC_ERROR_INVALID_ARGUMENTS, "Incomplete RSA key components");
1543
1544
  /* Encode private RSA key part */
1545
0
  rv = authentic_update_blob(ctx, AUTHENTIC_TAG_RSA_PRIVATE_P, rsa.p.data, rsa.p.len, &blob, &blob_len);
1546
0
  LOG_TEST_GOTO_ERR(ctx, rv, "SDO RSA P encode error");
1547
1548
0
  rv = authentic_update_blob(ctx, AUTHENTIC_TAG_RSA_PRIVATE_Q, rsa.q.data, rsa.q.len, &blob, &blob_len);
1549
0
  LOG_TEST_GOTO_ERR(ctx, rv, "SDO RSA Q encode error");
1550
1551
0
  rv = authentic_update_blob(ctx, AUTHENTIC_TAG_RSA_PRIVATE_PQ, rsa.iqmp.data, rsa.iqmp.len, &blob, &blob_len);
1552
0
  LOG_TEST_GOTO_ERR(ctx, rv, "SDO RSA PQ encode error");
1553
1554
0
  rv = authentic_update_blob(ctx, AUTHENTIC_TAG_RSA_PRIVATE_DP1, rsa.dmp1.data, rsa.dmp1.len, &blob, &blob_len);
1555
0
  LOG_TEST_GOTO_ERR(ctx, rv, "SDO RSA DP1 encode error");
1556
1557
0
  rv = authentic_update_blob(ctx, AUTHENTIC_TAG_RSA_PRIVATE_DQ1, rsa.dmq1.data, rsa.dmq1.len, &blob, &blob_len);
1558
0
  LOG_TEST_GOTO_ERR(ctx, rv, "SDO RSA DQ1 encode error");
1559
1560
0
  rv = authentic_update_blob(ctx, AUTHENTIC_TAG_RSA_PRIVATE, blob, blob_len, &blob01, &blob01_len);
1561
0
  LOG_TEST_GOTO_ERR(ctx, rv, "SDO RSA Private encode error");
1562
1563
0
  if (blob) {
1564
0
    sc_mem_clear(blob, blob_len);
1565
0
    free(blob);
1566
0
    blob = NULL;
1567
0
    blob_len = 0;
1568
0
  }
1569
1570
  /* Encode public RSA key part */
1571
0
  sc_log(ctx, "modulus.len:%zu blob_len:%zu", rsa.modulus.len, blob_len);
1572
0
  rv = authentic_update_blob(ctx, AUTHENTIC_TAG_RSA_PUBLIC_MODULUS, rsa.modulus.data, rsa.modulus.len, &blob, &blob_len);
1573
0
  LOG_TEST_GOTO_ERR(ctx, rv, "SDO RSA Modulus encode error");
1574
1575
0
  sc_log(ctx, "exponent.len:%zu blob_len:%zu", rsa.exponent.len, blob_len);
1576
0
  rv = authentic_update_blob(ctx, AUTHENTIC_TAG_RSA_PUBLIC_EXPONENT, rsa.exponent.data, rsa.exponent.len, &blob, &blob_len);
1577
0
  LOG_TEST_GOTO_ERR(ctx, rv, "SDO RSA Exponent encode error");
1578
1579
0
  rv = authentic_update_blob(ctx, AUTHENTIC_TAG_RSA_PUBLIC, blob, blob_len, &blob01, &blob01_len);
1580
0
  LOG_TEST_GOTO_ERR(ctx, rv, "SDO RSA Private encode error");
1581
1582
0
  if (blob) {
1583
0
    sc_mem_clear(blob, blob_len);
1584
0
    free(blob);
1585
0
    blob = NULL;
1586
0
    blob_len = 0;
1587
0
  }
1588
1589
0
  rv = authentic_update_blob(ctx, AUTHENTIC_TAG_RSA, blob01, blob01_len, out, out_len);
1590
0
  LOG_TEST_GOTO_ERR(ctx, rv, "SDO RSA encode error");
1591
1592
0
err:
1593
0
  if (blob01) {
1594
0
    sc_mem_clear(blob01, blob01_len);
1595
0
    free(blob01);
1596
0
  }
1597
0
  if (blob) {
1598
0
    sc_mem_clear(blob, blob_len);
1599
0
    free(blob);
1600
0
  }
1601
0
  sc_mem_clear(&rsa, sizeof(rsa));
1602
0
  LOG_FUNC_RETURN(ctx, rv);
1603
0
}
1604
1605
1606
static int
1607
authentic_manage_sdo_encode(struct sc_card *card, struct sc_authentic_sdo *sdo, unsigned long cmd,
1608
      unsigned char **out, size_t *out_len)
1609
0
{
1610
0
  struct sc_context *ctx = card->ctx;
1611
0
  unsigned char *data = NULL;
1612
0
  size_t data_len = 0;
1613
0
  unsigned char data_tag = AUTHENTIC_TAG_DOCP;
1614
0
  int rv;
1615
1616
0
  LOG_FUNC_CALLED(ctx);
1617
0
  sc_log(ctx, "encode SDO operation (cmd:%lX,mech:%X,id:%X)", cmd, sdo->docp.mech, sdo->docp.id);
1618
1619
0
  if (!out || !out_len)
1620
0
    LOG_TEST_RET(ctx, SC_ERROR_INVALID_ARGUMENTS, "Invalid arguments");
1621
1622
0
  rv = authentic_update_blob(ctx, AUTHENTIC_TAG_DOCP_MECH, &sdo->docp.mech, sizeof(sdo->docp.mech),
1623
0
      &data, &data_len);
1624
0
  LOG_TEST_GOTO_ERR(ctx, rv, "DOCP MECH encode error");
1625
1626
0
  rv = authentic_update_blob(ctx, AUTHENTIC_TAG_DOCP_ID, &sdo->docp.id, sizeof(sdo->docp.id),
1627
0
      &data, &data_len);
1628
0
  LOG_TEST_GOTO_ERR(ctx, rv, "DOCP ID encode error");
1629
1630
0
  if (cmd == SC_CARDCTL_AUTHENTIC_SDO_CREATE)   {
1631
0
    rv = authentic_update_blob(ctx, AUTHENTIC_TAG_DOCP_ACLS, sdo->docp.acl_data, sdo->docp.acl_data_len,
1632
0
        &data, &data_len);
1633
0
    LOG_TEST_GOTO_ERR(ctx, rv, "DOCP ACLs encode error");
1634
1635
0
    if (sdo->docp.security_parameter)  {
1636
0
      rv = authentic_update_blob(ctx, AUTHENTIC_TAG_DOCP_SCP,
1637
0
          &sdo->docp.security_parameter, sizeof(sdo->docp.security_parameter),
1638
0
          &data, &data_len);
1639
0
      LOG_TEST_GOTO_ERR(ctx, rv, "DOCP ACLs encode error");
1640
0
    }
1641
0
    if (sdo->docp.usage_counter[0] || sdo->docp.usage_counter[1])  {
1642
0
      rv = authentic_update_blob(ctx, AUTHENTIC_TAG_DOCP_USAGE_COUNTER,
1643
0
          sdo->docp.usage_counter, sizeof(sdo->docp.usage_counter),
1644
0
          &data, &data_len);
1645
0
      LOG_TEST_GOTO_ERR(ctx, rv, "DOCP ACLs encode error");
1646
0
    }
1647
0
  }
1648
0
  else if (cmd == SC_CARDCTL_AUTHENTIC_SDO_STORE)   {
1649
0
    if (sdo->docp.mech == AUTHENTIC_MECH_CRYPTO_RSA1024
1650
0
        || sdo->docp.mech == AUTHENTIC_MECH_CRYPTO_RSA1280
1651
0
        || sdo->docp.mech == AUTHENTIC_MECH_CRYPTO_RSA1536
1652
0
        || sdo->docp.mech == AUTHENTIC_MECH_CRYPTO_RSA1792
1653
0
        || sdo->docp.mech == AUTHENTIC_MECH_CRYPTO_RSA2048)   {
1654
0
      rv = authentic_manage_sdo_encode_prvkey(card, sdo->data.prvkey, &data, &data_len);
1655
0
      LOG_TEST_GOTO_ERR(ctx, rv, "SDO RSA encode error");
1656
0
    }
1657
0
    else  {
1658
0
      rv = SC_ERROR_NOT_SUPPORTED;
1659
0
      LOG_TEST_GOTO_ERR(ctx, rv, "Cryptographic object unsupported for encoding");
1660
0
    }
1661
0
  }
1662
0
  else if (cmd == SC_CARDCTL_AUTHENTIC_SDO_GENERATE)   {
1663
0
    if (sdo->data.prvkey)   {
1664
0
            rv = authentic_update_blob(ctx, AUTHENTIC_TAG_RSA_PUBLIC_EXPONENT,
1665
0
          sdo->data.prvkey->u.rsa.exponent.data, sdo->data.prvkey->u.rsa.exponent.len,
1666
0
          &data, &data_len);
1667
0
            LOG_TEST_GOTO_ERR(ctx, rv, "SDO RSA Exponent encode error");
1668
0
    }
1669
1670
0
    data_tag = AUTHENTIC_TAG_RSA_GENERATE_DATA;
1671
0
  }
1672
0
  else if (cmd != SC_CARDCTL_AUTHENTIC_SDO_DELETE)   {
1673
0
    rv = SC_ERROR_INVALID_DATA;
1674
0
    LOG_TEST_GOTO_ERR(ctx, rv, "Invalid SDO operation");
1675
0
  }
1676
1677
0
  rv = authentic_update_blob(ctx, data_tag, data, data_len, out, out_len);
1678
0
  LOG_TEST_GOTO_ERR(ctx, rv, "SDO DOCP encode error");
1679
1680
0
  sc_log_hex(ctx, "encoded SDO operation data", *out, *out_len);
1681
1682
0
err:
1683
0
  sc_mem_clear(data, data_len);
1684
0
  free(data);
1685
1686
0
  LOG_FUNC_RETURN(ctx, rv);
1687
0
}
1688
1689
1690
static int
1691
authentic_manage_sdo_generate(struct sc_card *card, struct sc_authentic_sdo *sdo)
1692
0
{
1693
0
  struct sc_context *ctx = card->ctx;
1694
0
  struct sc_apdu apdu;
1695
0
  unsigned char rbuf[0x400];
1696
0
  unsigned char *data = NULL;
1697
0
  size_t data_len = 0;
1698
0
  int rv;
1699
1700
0
  LOG_FUNC_CALLED(ctx);
1701
0
  sc_log(ctx, "Generate SDO(mech:%X,id:%X)",  sdo->docp.mech, sdo->docp.id);
1702
1703
0
  rv = authentic_manage_sdo_encode(card, sdo, SC_CARDCTL_AUTHENTIC_SDO_GENERATE, &data, &data_len);
1704
0
  LOG_TEST_RET(ctx, rv, "Cannot encode SDO data");
1705
0
  sc_log(ctx, "encoded SDO length %zu", data_len);
1706
1707
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0x47, 0x00, 0x00);
1708
0
  apdu.data = data;
1709
0
  apdu.datalen = data_len;
1710
0
  apdu.lc = data_len;
1711
0
  apdu.resp = rbuf;
1712
0
        apdu.resplen = sizeof(rbuf);
1713
0
  apdu.le = 0x100;
1714
1715
0
  rv = sc_transmit_apdu(card, &apdu);
1716
0
  LOG_TEST_GOTO_ERR(ctx, rv, "APDU transmit failed");
1717
0
  rv = sc_check_sw(card, apdu.sw1, apdu.sw2);
1718
0
  LOG_TEST_GOTO_ERR(ctx, rv, "authentic_sdo_create() SDO put data error");
1719
1720
0
  rv = authentic_decode_pubkey_rsa(ctx, apdu.resp, apdu.resplen, &sdo->data.prvkey);
1721
0
  LOG_TEST_GOTO_ERR(card->ctx, rv, "cannot decode public key");
1722
1723
0
err:
1724
0
  free(data);
1725
0
  LOG_FUNC_RETURN(ctx, rv);
1726
0
}
1727
1728
1729
static int
1730
authentic_manage_sdo(struct sc_card *card, struct sc_authentic_sdo *sdo, unsigned long cmd)
1731
0
{
1732
0
  struct sc_context *ctx = card->ctx;
1733
0
  struct sc_apdu apdu;
1734
0
  unsigned char *data = NULL;
1735
0
  size_t data_len = 0, save_max_send = card->max_send_size;
1736
0
  int rv;
1737
1738
0
  LOG_FUNC_CALLED(ctx);
1739
0
  sc_log(ctx, "SDO(cmd:%lX,mech:%X,id:%X)", cmd, sdo->docp.mech, sdo->docp.id);
1740
1741
0
  rv = authentic_manage_sdo_encode(card, sdo, cmd, &data, &data_len);
1742
0
  LOG_TEST_GOTO_ERR(ctx, rv, "Cannot encode SDO data");
1743
0
  sc_log(ctx, "encoded SDO length %zu", data_len);
1744
1745
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xDB, 0x3F, 0xFF);
1746
0
  apdu.data = data;
1747
0
  apdu.datalen = data_len;
1748
0
  apdu.lc = data_len;
1749
0
  apdu.flags |= SC_APDU_FLAGS_CHAINING;
1750
1751
0
  if (card->max_send_size > 255)
1752
0
    card->max_send_size = 255;
1753
1754
0
  rv = sc_transmit_apdu(card, &apdu);
1755
0
  card->max_send_size = save_max_send;
1756
0
  if (rv != SC_SUCCESS) {
1757
0
    LOG_TEST_GOTO_ERR(ctx, rv, "APDU transmit failed");
1758
0
  }
1759
1760
0
  rv = sc_check_sw(card, apdu.sw1, apdu.sw2);
1761
0
  LOG_TEST_GOTO_ERR(ctx, rv, "authentic_sdo_create() SDO put data error");
1762
1763
0
err:
1764
0
  sc_mem_clear(data, data_len);
1765
0
  free(data);
1766
0
  LOG_FUNC_RETURN(ctx, rv);
1767
0
}
1768
1769
1770
static int
1771
authentic_card_ctl(struct sc_card *card, unsigned long cmd, void *ptr)
1772
0
{
1773
0
  struct sc_context *ctx = card->ctx;
1774
0
  struct sc_authentic_sdo *sdo = (struct sc_authentic_sdo *) ptr;
1775
1776
0
  switch (cmd) {
1777
0
  case SC_CARDCTL_GET_SERIALNR:
1778
0
    return authentic_get_serialnr(card, (struct sc_serial_number *)ptr);
1779
0
  case SC_CARDCTL_AUTHENTIC_SDO_CREATE:
1780
0
    sc_log(ctx, "CARDCTL SDO_CREATE: sdo(mech:%X,id:%X)", sdo->docp.mech, sdo->docp.id);
1781
0
    return authentic_manage_sdo(card, (struct sc_authentic_sdo *) ptr, cmd);
1782
0
  case SC_CARDCTL_AUTHENTIC_SDO_DELETE:
1783
0
    sc_log(ctx, "CARDCTL SDO_DELETE: sdo(mech:%X,id:%X)", sdo->docp.mech, sdo->docp.id);
1784
0
    return authentic_manage_sdo(card, (struct sc_authentic_sdo *) ptr, cmd);
1785
0
  case SC_CARDCTL_AUTHENTIC_SDO_STORE:
1786
0
    sc_log(ctx, "CARDCTL SDO_STORE: sdo(mech:%X,id:%X)", sdo->docp.mech, sdo->docp.id);
1787
0
    return authentic_manage_sdo(card, (struct sc_authentic_sdo *) ptr, cmd);
1788
0
  case SC_CARDCTL_AUTHENTIC_SDO_GENERATE:
1789
0
    sc_log(ctx, "CARDCTL SDO_GENERATE: sdo(mech:%X,id:%X)", sdo->docp.mech, sdo->docp.id);
1790
0
    return authentic_manage_sdo_generate(card, (struct sc_authentic_sdo *) ptr);
1791
0
  }
1792
0
  return SC_ERROR_NOT_SUPPORTED;
1793
0
}
1794
1795
1796
static int
1797
authentic_set_security_env(struct sc_card *card,
1798
    const struct sc_security_env *env, int se_num)
1799
0
{
1800
0
  struct sc_context *ctx = card->ctx;
1801
0
  struct sc_apdu apdu;
1802
0
  unsigned char cse_crt_dst[] = {
1803
0
    0x80, 0x01, AUTHENTIC_ALGORITHM_RSA_PKCS1,
1804
0
    0x83, 0x01, env->key_ref[0] & ~AUTHENTIC_OBJECT_REF_FLAG_LOCAL,
1805
0
  };
1806
0
  unsigned char cse_crt_ct[] = {
1807
0
    0x80, 0x01, AUTHENTIC_ALGORITHM_RSA_PKCS1,
1808
0
    0x83, 0x01, env->key_ref[0] & ~AUTHENTIC_OBJECT_REF_FLAG_LOCAL,
1809
0
  };
1810
0
  int rv;
1811
1812
0
  LOG_FUNC_CALLED(ctx);
1813
0
  sc_log(ctx, "set SE#%i(op:0x%X,algo:0x%lX,algo_ref:0x%lX,flags:0x%lX), key_ref:0x%X",
1814
0
      se_num, env->operation, env->algorithm, env->algorithm_ref, env->algorithm_flags, env->key_ref[0]);
1815
0
  switch (env->operation)  {
1816
0
  case SC_SEC_OPERATION_SIGN:
1817
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x22, 0x41, AUTHENTIC_TAG_CRT_DST);
1818
0
    apdu.data = cse_crt_dst;
1819
0
    apdu.datalen = sizeof(cse_crt_dst);
1820
0
    apdu.lc = sizeof(cse_crt_dst);
1821
0
    break;
1822
0
  case SC_SEC_OPERATION_DECIPHER:
1823
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x22, 0x41, AUTHENTIC_TAG_CRT_CT);
1824
0
    apdu.data = cse_crt_ct;
1825
0
    apdu.datalen = sizeof(cse_crt_ct);
1826
0
    apdu.lc = sizeof(cse_crt_ct);
1827
0
    break;
1828
0
  default:
1829
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_SUPPORTED);
1830
0
  }
1831
1832
0
  rv = sc_transmit_apdu(card, &apdu);
1833
0
  LOG_TEST_RET(ctx, rv, "APDU transmit failed");
1834
0
  rv = sc_check_sw(card, apdu.sw1, apdu.sw2);
1835
0
  LOG_TEST_RET(ctx, rv, "MSE restore error");
1836
1837
0
  LOG_FUNC_RETURN(ctx, rv);
1838
0
}
1839
1840
1841
static int
1842
authentic_decipher(struct sc_card *card, const unsigned char *in, size_t in_len,
1843
    unsigned char *out, size_t out_len)
1844
0
{
1845
0
  struct sc_context *ctx = card->ctx;
1846
0
  struct sc_apdu apdu;
1847
0
  unsigned char resp[SC_MAX_APDU_BUFFER_SIZE];
1848
0
  int rv;
1849
1850
0
  LOG_FUNC_CALLED(ctx);
1851
0
  sc_log(ctx, "crgram_len %zu;  outlen %zu", in_len, out_len);
1852
0
  if (!out || !out_len || in_len > SC_MAX_APDU_BUFFER_SIZE)
1853
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
1854
1855
0
  sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0x2A, 0x80, 0x86);
1856
0
  apdu.flags |= SC_APDU_FLAGS_CHAINING;
1857
0
  apdu.data = in;
1858
0
  apdu.datalen = in_len;
1859
0
  apdu.lc = in_len;
1860
0
  apdu.resp = resp;
1861
0
  apdu.resplen = sizeof(resp);
1862
0
  apdu.le = 256;
1863
1864
0
  rv = sc_transmit_apdu(card, &apdu);
1865
0
  LOG_TEST_RET(ctx, rv, "APDU transmit failed");
1866
0
  rv = sc_check_sw(card, apdu.sw1, apdu.sw2);
1867
0
  LOG_TEST_RET(ctx, rv, "Card returned error");
1868
1869
0
  if (out_len > apdu.resplen)
1870
0
    out_len = apdu.resplen;
1871
1872
0
  memcpy(out, apdu.resp, out_len);
1873
0
  rv = (int)out_len;
1874
1875
0
  LOG_FUNC_RETURN(ctx, rv);
1876
0
}
1877
1878
1879
static int
1880
authentic_finish(struct sc_card *card)
1881
0
{
1882
0
  struct sc_context *ctx = card->ctx;
1883
1884
0
  LOG_FUNC_CALLED(ctx);
1885
1886
0
#ifdef ENABLE_SM
1887
0
  if (card->sm_ctx.ops.close)
1888
0
    card->sm_ctx.ops.close(card);
1889
0
#endif
1890
1891
0
  if (card->drv_data)
1892
0
    free(card->drv_data);
1893
0
  card->drv_data = NULL;
1894
0
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
1895
0
}
1896
1897
1898
static int authentic_card_reader_lock_obtained(sc_card_t *card, int was_reset)
1899
0
{
1900
0
  int r = SC_SUCCESS;
1901
1902
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1903
1904
0
  if (was_reset > 0
1905
0
      && card->type == SC_CARD_TYPE_OBERTHUR_AUTHENTIC_3_2) {
1906
0
    r = iso7816_select_aid(card, aid_AuthentIC_3_2, sizeof(aid_AuthentIC_3_2), NULL, NULL);
1907
0
  }
1908
1909
0
  LOG_FUNC_RETURN(card->ctx, r);
1910
0
}
1911
1912
1913
/* SM related */
1914
#ifdef ENABLE_SM
1915
static int
1916
authentic_sm_acl_init (struct sc_card *card, struct sm_info *sm_info, int cmd,
1917
    unsigned char *resp, size_t *resp_len)
1918
0
{
1919
0
  struct sc_context *ctx;
1920
0
  struct sm_type_params_gp *params_gp;
1921
0
  struct sc_remote_data rdata;
1922
0
  int rv;
1923
1924
0
  if (!card || !sm_info || !resp || !resp_len)
1925
0
    return SC_ERROR_INVALID_ARGUMENTS;
1926
1927
0
  ctx = card->ctx;
1928
0
  params_gp = &sm_info->session.gp.params;
1929
1930
0
  if (!card->sm_ctx.module.ops.initialize || !card->sm_ctx.module.ops.get_apdus)
1931
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_SUPPORTED);
1932
1933
0
  if (*resp_len < 28)
1934
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
1935
1936
0
  sm_info->cmd = cmd;
1937
0
  sm_info->sm_type = SM_TYPE_GP_SCP01;
1938
0
  sm_info->card_type = card->type;
1939
0
  params_gp->index = 0; /* logical channel */
1940
0
  params_gp->version = 1;
1941
0
  params_gp->level = 3; /* Only supported SM level 'ENC & MAC' */
1942
1943
0
  sm_info->serialnr = card->serialnr;
1944
1945
0
  sc_remote_data_init(&rdata);
1946
1947
0
  rv = card->sm_ctx.module.ops.initialize(ctx, sm_info, &rdata);
1948
0
  LOG_TEST_RET(ctx, rv, "SM: INITIALIZE failed");
1949
0
  if (!rdata.length)
1950
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_INTERNAL);
1951
1952
0
  rv = sc_transmit_apdu(card, &rdata.data->apdu);
1953
0
  LOG_TEST_RET(ctx, rv, "transmit APDU failed");
1954
0
  rv = sc_check_sw(card, rdata.data->apdu.sw1, rdata.data->apdu.sw2);
1955
0
  LOG_TEST_RET(ctx, rv, "Card returned error");
1956
1957
0
  if (rdata.data->apdu.resplen != 28 || *resp_len < 28)
1958
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_INTERNAL);
1959
1960
0
  memcpy(resp, rdata.data->apdu.resp, 28);
1961
0
  *resp_len = 28;
1962
1963
0
  rdata.free(&rdata);
1964
0
  LOG_FUNC_RETURN(ctx, rv);
1965
0
}
1966
1967
1968
static int
1969
authentic_sm_execute (struct sc_card *card, struct sm_info *sm_info,
1970
    unsigned char *data, size_t data_len, unsigned char *out, size_t len)
1971
0
{
1972
0
  struct sc_context *ctx = card->ctx;
1973
0
  struct sc_remote_data rdata;
1974
0
  int rv, ii;
1975
1976
0
  if (!card->sm_ctx.module.ops.get_apdus)
1977
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_SUPPORTED);
1978
1979
0
  sc_remote_data_init(&rdata);
1980
0
  rv = card->sm_ctx.module.ops.get_apdus(ctx, sm_info, data, data_len, &rdata);
1981
0
  LOG_TEST_RET(ctx, rv, "SM: GET_APDUS failed");
1982
0
  if (!rdata.length)
1983
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_INTERNAL);
1984
1985
0
  sc_log(ctx, "GET_APDUS: rv %i; rdata length %i", rv, rdata.length);
1986
1987
0
  for (ii=0; ii < rdata.length; ii++)   {
1988
0
    struct sc_apdu *apdu = &((rdata.data + ii)->apdu);
1989
1990
0
    if (!apdu->ins)
1991
0
      break;
1992
0
    rv = sc_transmit_apdu(card, apdu);
1993
0
    if (rv < 0)
1994
0
      break;
1995
1996
0
    rv = sc_check_sw(card, apdu->sw1, apdu->sw2);
1997
0
    if (rv < 0)
1998
0
      break;
1999
0
  }
2000
2001
0
  rdata.free(&rdata);
2002
0
  LOG_FUNC_RETURN(ctx, rv);
2003
0
}
2004
2005
2006
static int
2007
authentic_sm_open(struct sc_card *card)
2008
0
{
2009
0
  struct sc_context *ctx = card->ctx;
2010
0
  unsigned char init_data[SC_MAX_APDU_BUFFER_SIZE];
2011
0
  size_t init_data_len = sizeof(init_data);
2012
0
  int rv;
2013
2014
0
  LOG_FUNC_CALLED(ctx);
2015
2016
0
  memset(&card->sm_ctx.info, 0, sizeof(card->sm_ctx.info));
2017
0
  memcpy(card->sm_ctx.info.config_section, card->sm_ctx.config_section, sizeof(card->sm_ctx.info.config_section));
2018
0
  sc_log(ctx, "SM context config '%s'; SM mode 0x%X", card->sm_ctx.info.config_section, card->sm_ctx.sm_mode);
2019
2020
0
  if (card->sm_ctx.sm_mode == SM_MODE_TRANSMIT && card->max_send_size == 0)
2021
0
    card->max_send_size = 239;
2022
2023
0
  rv = authentic_sm_acl_init (card, &card->sm_ctx.info, SM_CMD_INITIALIZE, init_data, &init_data_len);
2024
0
  LOG_TEST_RET(ctx, rv, "authentIC: cannot open SM");
2025
2026
0
  rv = authentic_sm_execute (card, &card->sm_ctx.info, init_data, init_data_len, NULL, 0);
2027
0
  LOG_TEST_RET(ctx, rv, "SM: execute failed");
2028
2029
0
  card->sm_ctx.info.cmd = SM_CMD_APDU_TRANSMIT;
2030
0
  LOG_FUNC_RETURN(ctx, rv);
2031
0
}
2032
2033
2034
static int
2035
authentic_sm_free_wrapped_apdu(struct sc_card *card, struct sc_apdu *plain, struct sc_apdu **sm_apdu)
2036
0
{
2037
0
  struct sc_context *ctx = card->ctx;
2038
2039
0
  LOG_FUNC_CALLED(ctx);
2040
0
  if (!sm_apdu)
2041
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
2042
0
        if (!(*sm_apdu))
2043
0
    LOG_FUNC_RETURN(ctx, SC_SUCCESS);
2044
2045
0
        if (plain)   {
2046
0
    if (plain->resplen < (*sm_apdu)->resplen)
2047
0
      LOG_TEST_RET(ctx, SC_ERROR_BUFFER_TOO_SMALL, "Insufficient plain APDU response size");
2048
0
    memcpy(plain->resp, (*sm_apdu)->resp, (*sm_apdu)->resplen);
2049
0
    plain->resplen = (*sm_apdu)->resplen;
2050
0
    plain->sw1 = (*sm_apdu)->sw1;
2051
0
    plain->sw2 = (*sm_apdu)->sw2;
2052
0
  }
2053
2054
0
  if ((*sm_apdu)->data)
2055
0
    free((unsigned char *) (*sm_apdu)->data);
2056
0
  if ((*sm_apdu)->resp)
2057
0
    free((unsigned char *) (*sm_apdu)->resp);
2058
2059
0
  free(*sm_apdu);
2060
0
  *sm_apdu = NULL;
2061
0
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
2062
0
}
2063
2064
static int
2065
authentic_sm_get_wrapped_apdu(struct sc_card *card, struct sc_apdu *plain, struct sc_apdu **sm_apdu)
2066
0
{
2067
0
  struct sc_context *ctx = card->ctx;
2068
0
  struct sc_apdu *apdu = NULL;
2069
0
  int rv  = 0;
2070
2071
0
  LOG_FUNC_CALLED(ctx);
2072
2073
0
  if (!plain || !sm_apdu)
2074
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
2075
0
  sc_log(ctx, "called; CLA:%X, INS:%X, P1:%X, P2:%X, data(%zu) %p",
2076
0
      plain->cla, plain->ins, plain->p1, plain->p2, plain->datalen, plain->data);
2077
0
  *sm_apdu = NULL;
2078
2079
0
  if ((plain->cla & 0x04)
2080
0
      || (plain->cla==0x00 && plain->ins==0x22)
2081
0
      || (plain->cla==0x00 && plain->ins==0x2A)
2082
0
      || (plain->cla==0x00 && plain->ins==0x84)
2083
0
      || (plain->cla==0x00 && plain->ins==0x88)
2084
0
      || (plain->cla==0x00 && plain->ins==0xA4)
2085
0
      || (plain->cla==0x00 && plain->ins==0xC0)
2086
0
      || (plain->cla==0x00 && plain->ins==0xCA)
2087
0
      || (plain->cla==0x80 && plain->ins==0x50)
2088
0
     )   {
2089
0
    sc_log(ctx, "SM wrap is not applied for this APDU");
2090
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_SM_NOT_APPLIED);
2091
0
  }
2092
2093
0
  if (card->sm_ctx.sm_mode != SM_MODE_TRANSMIT)
2094
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_SM_NOT_INITIALIZED);
2095
2096
0
  if (!card->sm_ctx.module.ops.get_apdus)
2097
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_SUPPORTED);
2098
2099
0
  apdu = calloc(1, sizeof(struct sc_apdu));
2100
0
  if (!apdu)
2101
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY);
2102
0
  memcpy((void *)apdu, (void *)plain, sizeof(struct sc_apdu));
2103
2104
0
  apdu->data = calloc (1, plain->datalen + 24);
2105
0
  if (!apdu->data) {
2106
0
    free(apdu);
2107
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY);
2108
0
  }
2109
0
    if (plain->data && plain->datalen)
2110
0
    memcpy((unsigned char *) apdu->data, plain->data, plain->datalen);
2111
2112
0
  apdu->resp = calloc (1, plain->resplen + 32);
2113
0
  if (!apdu->resp) {
2114
0
    free((unsigned char *) apdu->data);
2115
0
    free(apdu);
2116
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY);
2117
0
  }
2118
2119
0
  card->sm_ctx.info.cmd = SM_CMD_APDU_TRANSMIT;
2120
0
  card->sm_ctx.info.cmd_data = (void *)apdu;
2121
2122
0
  rv = card->sm_ctx.module.ops.get_apdus(ctx, &card->sm_ctx.info, NULL, 0, NULL);
2123
0
  if (rv < 0) {
2124
0
    free(apdu->resp);
2125
0
    free((unsigned char *) apdu->data);
2126
0
    free(apdu);
2127
0
  }
2128
0
  LOG_TEST_RET(ctx, rv, "SM: GET_APDUS failed");
2129
2130
0
  *sm_apdu = apdu;
2131
0
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
2132
0
}
2133
#endif
2134
2135
int authentic_logout(sc_card_t *card)
2136
0
{
2137
0
  int r = SC_ERROR_NOT_SUPPORTED;
2138
2139
0
  if (card->type == SC_CARD_TYPE_OBERTHUR_AUTHENTIC_3_2) {
2140
0
    r = iso7816_select_aid(card, aid_AuthentIC_3_2, sizeof(aid_AuthentIC_3_2), NULL, NULL);
2141
0
  }
2142
2143
0
  return r;
2144
0
}
2145
2146
static struct sc_card_driver *
2147
sc_get_driver(void)
2148
12.7k
{
2149
12.7k
  struct sc_card_driver *iso_drv = sc_get_iso7816_driver();
2150
2151
12.7k
  if (!iso_ops)
2152
1
    iso_ops = iso_drv->ops;
2153
2154
12.7k
  authentic_ops = *iso_ops;
2155
2156
12.7k
  authentic_ops.match_card = authentic_match_card;
2157
12.7k
  authentic_ops.init = authentic_init;
2158
12.7k
  authentic_ops.finish = authentic_finish;
2159
12.7k
  authentic_ops.read_binary = authentic_read_binary;
2160
12.7k
  authentic_ops.write_binary = authentic_write_binary;
2161
12.7k
  authentic_ops.update_binary = authentic_update_binary;
2162
12.7k
  authentic_ops.erase_binary = authentic_erase_binary;
2163
  /* authentic_ops.resize_file = authentic_resize_file; */
2164
12.7k
  authentic_ops.select_file = authentic_select_file;
2165
  /* get_response: Untested */
2166
12.7k
  authentic_ops.get_challenge = authentic_get_challenge;
2167
12.7k
  authentic_ops.set_security_env = authentic_set_security_env;
2168
  /* decipher: Untested */
2169
12.7k
  authentic_ops.decipher = authentic_decipher;
2170
  /* authentic_ops.compute_signature = authentic_compute_signature; */
2171
12.7k
  authentic_ops.create_file = authentic_create_file;
2172
12.7k
  authentic_ops.delete_file = authentic_delete_file;
2173
12.7k
  authentic_ops.card_ctl = authentic_card_ctl;
2174
12.7k
  authentic_ops.process_fci = authentic_process_fci;
2175
12.7k
  authentic_ops.pin_cmd = authentic_pin_cmd;
2176
12.7k
  authentic_ops.card_reader_lock_obtained = authentic_card_reader_lock_obtained;
2177
2178
12.7k
  return &authentic_drv;
2179
12.7k
}
2180
2181
struct sc_card_driver *
2182
sc_get_authentic_driver(void)
2183
12.7k
{
2184
12.7k
  return sc_get_driver();
2185
12.7k
}
2186
2187
#endif /* ENABLE_OPENSSL */