Coverage Report

Created: 2026-01-07 06:04

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