Coverage Report

Created: 2026-08-31 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/card-dtrust.c
Line
Count
Source
1
/*
2
 * card-dtrust.c: Support for (CardOS based) D-Trust Signature Cards
3
 *
4
 * Copyright (C) 2023 Mario Haustein <mario.haustein@hrz.tu-chemnitz.de>
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 *
20
 * based on card-cardos.c
21
 */
22
23
/*
24
 * This are the support periods for the D-Trust cards. The end of life time is
25
 * set by the expiry of the underlying card operating system and sets the
26
 * validity limit of the issued certificates. After end of life, the code paths
27
 * for the affected products may be removed, as the cards are then not useful
28
 * anymore.
29
 *
30
 *        Start of Sales  End of Sales  End of life
31
 * D-Trust Card 4.1/4.4   n/a   Nov 2024  Sep 2026
32
 * D-Trust Card 5.1/5.4   Nov 2023  n/a   Oct 2028
33
 * D-Trust Card 6.1/6.4   Summer 2025 n/a   n/a
34
 */
35
36
#ifdef HAVE_CONFIG_H
37
#include "config.h"
38
#endif
39
40
#include <stdlib.h>
41
#include <string.h>
42
43
#include "libopensc/pace.h"
44
45
#include "asn1.h"
46
#include "card-cardos-common.h"
47
#include "internal.h"
48
#include "sm.h"
49
#include "sm/sm-eac.h"
50
51
#include "card-dtrust.h"
52
53
static const struct sc_card_operations *iso_ops = NULL;
54
55
static struct sc_card_operations dtrust_ops;
56
57
// clang-format off
58
static struct sc_card_driver dtrust_drv = {
59
  "D-Trust Signature Card",
60
  "dtrust",
61
  &dtrust_ops,
62
  NULL, 0, NULL
63
};
64
// clang-format on
65
66
struct dtrust_drv_data_t {
67
  /* track PACE state */
68
  unsigned int pace : 1;
69
  unsigned int can : 1;
70
  /* global CAN from configuration file */
71
  char *can_value;
72
  /* use CAN cache */
73
  unsigned int can_cache : 1;
74
  /* PKCS#15 context for CAN caching */
75
  struct sc_pkcs15_card *p15card;
76
  /* save the current security environment */
77
  const sc_security_env_t *env;
78
};
79
80
// clang-format off
81
static const struct sc_atr_table dtrust_atrs[] = {
82
  /* D-Trust Signature Card v4.1 and v4.4 - CardOS 5.4
83
   *
84
   * The ATR was intentionally omitted from minidriver_registration[] within win32/customactions.cpp
85
   * as it is identical to that of CardOS v5.4 and therefore already included.
86
   * Any new ATR may need an entry in minidriver_registration[]. */
87
  { "3b:d2:18:00:81:31:fe:58:c9:04:11", NULL, NULL, SC_CARD_TYPE_DTRUST_V4_1_STD, 0, NULL },
88
89
90
  /* D-Trust Signature Card v5.1 and v5.4 - CardOS 6.0
91
   *
92
   * These cards are dual interface cards. Thus they have separate ATRs. */
93
94
  /* contact based */
95
  { "3b:d2:18:00:81:31:fe:58:cb:01:16", NULL, NULL, SC_CARD_TYPE_DTRUST_V5_1_STD, 0, NULL },
96
97
  /* contactless */
98
  { "3b:82:80:01:cb:01:c9",             NULL, NULL, SC_CARD_TYPE_DTRUST_V5_1_STD, 0, NULL },
99
  { "07:78:77:74:03:cb:01:09",          NULL, NULL, SC_CARD_TYPE_DTRUST_V5_1_STD, 0, NULL },
100
101
  { NULL,                               NULL, NULL, 0,                            0, NULL }
102
};
103
// clang-format on
104
105
static struct sc_object_id oid_secp256r1 = {
106
    {1, 2, 840, 10045, 3, 1, 7, -1}
107
};
108
static struct sc_object_id oid_secp384r1 = {
109
    {1, 3, 132, 0, 34, -1}
110
};
111
112
static int
113
_dtrust_match_cardos(sc_card_t *card)
114
62
{
115
62
  int r;
116
62
  size_t prodlen;
117
62
  u8 buf[32];
118
119
  /* check OS version */
120
62
  r = sc_get_data(card, 0x0182, buf, 32);
121
62
  LOG_TEST_RET(card->ctx, r, "OS version check failed");
122
123
19
  if (card->type == SC_CARD_TYPE_DTRUST_V4_1_STD) {
124
10
    if (r != 2 || buf[0] != 0xc9 || buf[1] != 0x04)
125
9
      return SC_ERROR_WRONG_CARD;
126
10
  } else if (card->type == SC_CARD_TYPE_DTRUST_V5_1_STD) {
127
9
    if (r != 2 || buf[0] != 0xcb || buf[1] != 0x01)
128
8
      return SC_ERROR_WRONG_CARD;
129
9
  }
130
131
  /* check product name */
132
2
  r = sc_get_data(card, 0x0180, buf, 32);
133
2
  LOG_TEST_RET(card->ctx, r, "Product name check failed");
134
135
0
  prodlen = (size_t)r;
136
0
  if (card->type == SC_CARD_TYPE_DTRUST_V4_1_STD) {
137
0
    if (prodlen != strlen("CardOS V5.4     2019") + 1 || memcmp(buf, "CardOS V5.4     2019", prodlen))
138
0
      return SC_ERROR_WRONG_CARD;
139
0
  } else if (card->type == SC_CARD_TYPE_DTRUST_V5_1_STD) {
140
0
    if (prodlen != strlen("CardOS V6.0 2021") + 1 || memcmp(buf, "CardOS V6.0 2021", prodlen))
141
0
      return SC_ERROR_WRONG_CARD;
142
0
  }
143
144
0
  return SC_SUCCESS;
145
0
}
146
147
static int
148
_dtrust_match_profile(sc_card_t *card)
149
0
{
150
0
  sc_path_t cia_path;
151
0
  int r;
152
0
  u8 buf[SC_MAX_APDU_BUFFER_SIZE];
153
0
  size_t slen, plen;
154
0
  const u8 *sp, *pp;
155
0
  char *name;
156
157
0
  sc_format_path("5032", &cia_path);
158
0
  cia_path.aid.len = sizeof(cia_path.aid.value);
159
0
  r = sc_hex_to_bin("E8:28:BD:08:0F:A0:00:00:01:67:45:53:49:47:4E", (u8 *)&cia_path.aid.value, &cia_path.aid.len);
160
0
  LOG_TEST_RET(card->ctx, r, "Formatting AID failed");
161
162
0
  r = sc_select_file(card, &cia_path, NULL);
163
0
  LOG_TEST_RET(card->ctx, r, "Selecting CIA path failed");
164
165
0
  r = sc_read_binary(card, 0, buf, SC_MAX_APDU_BUFFER_SIZE, NULL);
166
0
  LOG_TEST_RET(card->ctx, r, "Reading CIA information failed");
167
168
0
  sp = sc_asn1_find_tag(card->ctx, buf, r, 0x30, &slen);
169
0
  if (sp == NULL)
170
0
    return SC_ERROR_WRONG_CARD;
171
172
  /* check vendor */
173
0
  pp = sc_asn1_find_tag(card->ctx, sp, slen, 0x0c, &plen);
174
0
  if (pp == NULL)
175
0
    return SC_ERROR_WRONG_CARD;
176
177
0
  if (plen != 16 || memcmp(pp, "D-TRUST GmbH (C)", 16))
178
0
    return SC_ERROR_WRONG_CARD;
179
180
  /* check profile */
181
0
  pp = sc_asn1_find_tag(card->ctx, sp, slen, 0x80, &plen);
182
0
  if (pp == NULL)
183
0
    return SC_ERROR_WRONG_CARD;
184
185
  /*
186
   * The profile string contains (two) additional characters. They depend
187
   * on the production process, but aren't relevant for determining the
188
   * card profile.
189
   */
190
0
  if (card->type == SC_CARD_TYPE_DTRUST_V4_1_STD) {
191
0
    if (plen >= 27 && !memcmp(pp, "D-TRUST Card 4.1 Std. RSA 2", 27))
192
0
      card->type = SC_CARD_TYPE_DTRUST_V4_1_STD;
193
0
    else if (plen >= 28 && !memcmp(pp, "D-TRUST Card 4.1 Multi ECC 2", 28))
194
0
      card->type = SC_CARD_TYPE_DTRUST_V4_1_MULTI;
195
0
    else if (plen >= 27 && !memcmp(pp, "D-TRUST Card 4.1 M100 ECC 2", 27))
196
0
      card->type = SC_CARD_TYPE_DTRUST_V4_1_M100;
197
0
    else if (plen >= 27 && !memcmp(pp, "D-TRUST Card 4.4 Std. RSA 2", 27))
198
0
      card->type = SC_CARD_TYPE_DTRUST_V4_4_STD;
199
0
    else if (plen >= 28 && !memcmp(pp, "D-TRUST Card 4.4 Multi ECC 2", 28))
200
0
      card->type = SC_CARD_TYPE_DTRUST_V4_4_MULTI;
201
0
    else
202
0
      return SC_ERROR_WRONG_CARD;
203
0
  } else if (card->type == SC_CARD_TYPE_DTRUST_V5_1_STD) {
204
0
    if (plen >= 27 && !memcmp(pp, "D-TRUST Card 5.1 Std. RSA 2", 27))
205
0
      card->type = SC_CARD_TYPE_DTRUST_V5_1_STD;
206
0
    else if (plen >= 28 && !memcmp(pp, "D-TRUST Card 5.1 Multi ECC 2", 28))
207
0
      card->type = SC_CARD_TYPE_DTRUST_V5_1_MULTI;
208
0
    else if (plen >= 27 && !memcmp(pp, "D-TRUST Card 5.1 M100 ECC 2", 27))
209
0
      card->type = SC_CARD_TYPE_DTRUST_V5_1_M100;
210
0
    else if (plen >= 27 && !memcmp(pp, "D-TRUST Card 5.4 Std. RSA 2", 27))
211
0
      card->type = SC_CARD_TYPE_DTRUST_V5_4_STD;
212
0
    else if (plen >= 28 && !memcmp(pp, "D-TRUST Card 5.4 Multi ECC 2", 28))
213
0
      card->type = SC_CARD_TYPE_DTRUST_V5_4_MULTI;
214
0
    else
215
0
      return SC_ERROR_WRONG_CARD;
216
0
  }
217
218
0
  name = malloc(plen + 1);
219
0
  if (name == NULL)
220
0
    return SC_ERROR_OUT_OF_MEMORY;
221
0
  memcpy(name, pp, plen);
222
0
  name[plen] = '\0';
223
0
  card->name = name;
224
225
0
  sc_log(card->ctx, "found %s", card->name);
226
227
0
  return SC_SUCCESS;
228
0
}
229
230
static int dtrust_finish(sc_card_t *card);
231
232
static int
233
dtrust_match_card(sc_card_t *card)
234
15.2k
{
235
15.2k
  if (_sc_match_atr(card, dtrust_atrs, &card->type) < 0)
236
15.1k
    return 0;
237
238
62
  if (_dtrust_match_cardos(card) != SC_SUCCESS)
239
62
    return 0;
240
241
0
  if (_dtrust_match_profile(card) != SC_SUCCESS)
242
0
    return 0;
243
244
0
  sc_log(card->ctx, "D-Trust Signature Card");
245
246
0
  return 1;
247
0
}
248
249
static int
250
_dtrust_get_serialnr(sc_card_t *card)
251
0
{
252
0
  int r;
253
254
0
  card->serialnr.len = SC_MAX_SERIALNR;
255
0
  r = sc_parse_ef_gdo(card, card->serialnr.value, &card->serialnr.len, NULL, 0);
256
0
  if (r < 0) {
257
0
    card->serialnr.len = 0;
258
0
    return r;
259
0
  }
260
261
0
  return SC_SUCCESS;
262
0
}
263
264
static int
265
dtrust_init(sc_card_t *card)
266
0
{
267
0
  struct dtrust_drv_data_t *drv_data;
268
0
  const char *can_env, *can_value;
269
0
  size_t i, j;
270
0
  scconf_block **found_blocks, *block;
271
0
  int r;
272
0
  const size_t data_field_length = 437;
273
0
  unsigned long flags, ext_flags;
274
275
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
276
277
0
  card->cla = 0x00;
278
279
0
  drv_data = calloc(1, sizeof(struct dtrust_drv_data_t));
280
0
  if (drv_data == NULL)
281
0
    return SC_ERROR_OUT_OF_MEMORY;
282
283
0
  drv_data->pace = 0;
284
0
  drv_data->can = 0;
285
0
  drv_data->can_value = NULL;
286
0
  drv_data->can_cache = 1;
287
0
  drv_data->p15card = NULL;
288
289
  /* read environment variable */
290
0
  can_env = getenv("DTRUST_CAN");
291
292
  /* read configuration */
293
0
  can_value = NULL;
294
0
  for (i = 0; card->ctx->conf_blocks[i]; i++) {
295
0
    found_blocks = scconf_find_blocks(card->ctx->conf, card->ctx->conf_blocks[i], "card_driver", "dtrust");
296
0
    if (!found_blocks)
297
0
      continue;
298
299
0
    for (j = 0, block = found_blocks[j]; block; j++, block = found_blocks[j]) {
300
0
      drv_data->can_cache = scconf_get_bool(block, "can_use_cache", drv_data->can_cache);
301
302
      /* Environment variable has precedence over configured CAN */
303
0
      if (can_env == NULL) {
304
0
        can_value = scconf_get_str(block, "can", can_value);
305
0
      }
306
0
    }
307
0
    free(found_blocks);
308
0
  }
309
310
0
  if (can_env != NULL) {
311
0
    sc_log(card->ctx, "Using CAN provided by environment variable.");
312
0
    can_value = can_env;
313
0
  } else if (can_value != NULL) {
314
0
    sc_log(card->ctx, "Using CAN provided by configuration file.");
315
0
  }
316
317
0
  if (can_value != NULL) {
318
0
    size_t can_len;
319
320
0
    can_len = strlen(can_value);
321
0
    drv_data->can_value = sc_mem_secure_alloc(can_len + 1);
322
0
    if (drv_data->can_value == NULL) {
323
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
324
0
    }
325
0
    memcpy(drv_data->can_value, can_value, can_len + 1);
326
0
  }
327
328
0
  card->drv_data = drv_data;
329
330
0
  r = _dtrust_get_serialnr(card);
331
0
  if (r != SC_SUCCESS) {
332
0
    dtrust_finish(card);
333
0
    LOG_TEST_RET(card->ctx, r, "Error reading serial number.");
334
0
  }
335
336
0
  card->caps |= SC_CARD_CAP_APDU_EXT | SC_CARD_CAP_ISO7816_PIN_INFO;
337
338
0
  card->max_send_size = data_field_length - 6;
339
#ifdef _WIN32
340
  /* see card-cardos.c */
341
  if (card->reader->max_send_size == 255 && card->reader->max_recv_size == 256) {
342
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "resetting reader to use data_field_length");
343
    card->reader->max_send_size = data_field_length - 6;
344
    card->reader->max_recv_size = data_field_length - 3;
345
  }
346
#endif
347
348
0
  card->max_send_size = sc_get_max_send_size(card); /* see card-cardos.c */
349
0
  card->max_recv_size = data_field_length - 2;
350
0
  card->max_recv_size = sc_get_max_recv_size(card);
351
352
0
  flags = 0;
353
354
0
  switch (card->type) {
355
0
  case SC_CARD_TYPE_DTRUST_V4_1_STD:
356
0
  case SC_CARD_TYPE_DTRUST_V4_4_STD:
357
0
  case SC_CARD_TYPE_DTRUST_V5_1_STD:
358
0
  case SC_CARD_TYPE_DTRUST_V5_4_STD:
359
0
    flags |= SC_ALGORITHM_RSA_PAD_PKCS1;
360
0
    flags |= SC_ALGORITHM_RSA_PAD_PSS;
361
0
    flags |= SC_ALGORITHM_RSA_PAD_OAEP;
362
0
    flags |= SC_ALGORITHM_RSA_HASH_SHA256;
363
0
    flags |= SC_ALGORITHM_RSA_HASH_SHA384;
364
0
    flags |= SC_ALGORITHM_RSA_HASH_SHA512;
365
0
    flags |= SC_ALGORITHM_MGF1_SHA256;
366
0
    flags |= SC_ALGORITHM_MGF1_SHA384;
367
0
    flags |= SC_ALGORITHM_MGF1_SHA512;
368
369
0
    _sc_card_add_rsa_alg(card, 3072, flags, 0);
370
0
    break;
371
372
0
  case SC_CARD_TYPE_DTRUST_V4_1_MULTI:
373
0
  case SC_CARD_TYPE_DTRUST_V4_1_M100:
374
0
  case SC_CARD_TYPE_DTRUST_V4_4_MULTI:
375
0
    flags |= SC_ALGORITHM_ECDSA_RAW;
376
0
    flags |= SC_ALGORITHM_ECDH_CDH_RAW;
377
0
    ext_flags = SC_ALGORITHM_EXT_EC_NAMEDCURVE;
378
379
0
    _sc_card_add_ec_alg(card, 256, flags, ext_flags, &oid_secp256r1);
380
0
    break;
381
382
0
  case SC_CARD_TYPE_DTRUST_V5_1_MULTI:
383
0
  case SC_CARD_TYPE_DTRUST_V5_1_M100:
384
0
  case SC_CARD_TYPE_DTRUST_V5_4_MULTI:
385
0
    flags |= SC_ALGORITHM_ECDSA_RAW;
386
0
    flags |= SC_ALGORITHM_ECDH_CDH_RAW;
387
0
    ext_flags = SC_ALGORITHM_EXT_EC_NAMEDCURVE;
388
389
0
    _sc_card_add_ec_alg(card, 384, flags, ext_flags, &oid_secp384r1);
390
0
    break;
391
392
0
  default:
393
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_WRONG_CARD);
394
0
  }
395
396
0
  switch (card->type) {
397
0
  case SC_CARD_TYPE_DTRUST_V5_1_STD:
398
0
  case SC_CARD_TYPE_DTRUST_V5_4_STD:
399
0
  case SC_CARD_TYPE_DTRUST_V5_1_MULTI:
400
0
  case SC_CARD_TYPE_DTRUST_V5_1_M100:
401
0
  case SC_CARD_TYPE_DTRUST_V5_4_MULTI:
402
0
    r = sc_pkcs15_bind(card, NULL, &drv_data->p15card);
403
0
    if (r != SC_SUCCESS) {
404
0
      dtrust_finish(card);
405
0
      LOG_TEST_RET(card->ctx, r, "Binding PKCS#15 context failed");
406
0
    }
407
0
    break;
408
0
  }
409
410
0
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
411
0
}
412
413
static int
414
dtrust_finish(sc_card_t *card)
415
0
{
416
0
  struct dtrust_drv_data_t *drv_data;
417
418
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
419
420
0
  drv_data = card->drv_data;
421
422
0
  if (drv_data->p15card != NULL) {
423
0
    sc_pkcs15_unbind(drv_data->p15card);
424
0
  }
425
426
0
  if (drv_data->can_value != NULL) {
427
0
    sc_mem_secure_free(drv_data->can_value, strlen(drv_data->can_value) + 1);
428
0
  }
429
430
0
  free((char *)card->name);
431
0
  free(card->drv_data);
432
433
0
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
434
0
}
435
436
static int
437
dtrust_select_app(struct sc_card *card, int ref)
438
0
{
439
0
  sc_path_t path;
440
0
  int r;
441
442
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
443
444
0
  switch (card->type) {
445
0
  case SC_CARD_TYPE_DTRUST_V5_1_STD:
446
0
  case SC_CARD_TYPE_DTRUST_V5_4_STD:
447
0
  case SC_CARD_TYPE_DTRUST_V5_1_MULTI:
448
0
  case SC_CARD_TYPE_DTRUST_V5_1_M100:
449
0
  case SC_CARD_TYPE_DTRUST_V5_4_MULTI:
450
0
    switch (ref) {
451
0
    case DTRUST5_PIN_ID_QES:
452
0
      sc_format_path("3F000101", &path);
453
0
      break;
454
455
0
    case DTRUST5_PIN_ID_AUT:
456
0
      sc_format_path("3F000102", &path);
457
0
      break;
458
459
0
    default:
460
0
      sc_format_path("3F00", &path);
461
0
      break;
462
0
    }
463
464
0
    r = sc_select_file(card, &path, NULL);
465
0
    LOG_TEST_RET(card->ctx, r, "Selecting master file failed");
466
0
    break;
467
0
  }
468
469
0
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
470
0
}
471
472
static int
473
dtrust_perform_pace(struct sc_card *card,
474
    int ref,
475
    const unsigned char *pin,
476
    size_t pinlen,
477
    int *tries_left)
478
0
{
479
0
  struct dtrust_drv_data_t *drv_data;
480
0
  sc_path_t can_path;
481
0
  u8 can_buffer[16];
482
0
  u8 *can_ptr = can_buffer;
483
0
  size_t can_len;
484
0
  int r;
485
0
  struct establish_pace_channel_input pace_input;
486
0
  struct establish_pace_channel_output pace_output;
487
488
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
489
490
0
  drv_data = card->drv_data;
491
492
  /* Dummy file path for the CAN cache */
493
0
  sc_format_path("CA4E", &can_path);
494
495
  /* Read CAN cache always. We need to know if the cache contains a CAN
496
   * value even if we use a CAN source with higher precedence. The CAN
497
   * cache must only be written if it was empty. Writing to a non-empty
498
   * cache file will append the data to be written instead of overwriting
499
   * the file. */
500
0
  can_len = sizeof(can_buffer) - 1;
501
0
  r = sc_pkcs15_read_cached_file(drv_data->p15card, &can_path, &can_ptr, &can_len);
502
0
  if (r == SC_SUCCESS && can_len > 0) {
503
0
    can_buffer[can_len] = '\0';
504
0
  } else {
505
0
    can_len = 0;
506
0
  }
507
508
  /* The PKCS#11 layer cannot provide a CAN. Instead we consider the
509
   * following sources for CAN input.
510
   *  1. A CAN provided by the caller
511
   *  2. A CAN provided in the environment variable DTRUST_CAN
512
   *  3. A CAN provided in the configuration file
513
   *  4. A cached CAN when the cache feature is enabled
514
   *  5. If the reader supports the PACE protocol, we let it query for a
515
   *     CAN on the pin pad.
516
   *  6. Querying the user interactively if possible */
517
0
  if (ref == PACE_PIN_ID_CAN) {
518
    /* Use CAN from environment variable or configuration file */
519
0
    if (pin == NULL) {
520
0
      pin = (const unsigned char *)drv_data->can_value;
521
0
      if (pin != NULL) {
522
0
        sc_log(card->ctx, "Using static CAN (environment variable/configuration file).");
523
0
        pinlen = strlen(drv_data->can_value);
524
0
      }
525
0
    }
526
527
    /* Use the CAN cache if no CAN is provided. */
528
0
    if (drv_data->can_cache && pin == NULL) {
529
0
      if (can_len > 0) {
530
0
        sc_log(card->ctx, "Using cached CAN.");
531
0
        pin = can_buffer;
532
0
        pinlen = can_len;
533
0
      } else {
534
0
        sc_log(card->ctx, "No cached CAN available.");
535
0
      }
536
0
    }
537
538
    /* Query the user interactively if no cached CAN is available. */
539
0
    if (pin == NULL) {
540
0
      if (card->reader->capabilities & SC_READER_CAP_PACE_GENERIC) {
541
        /* If no CAN is provided and the reader is
542
         * PACE-capable, we leave pin == NULL to request the
543
         * ready for querying the CAN on its pin pad. */
544
0
        sc_log(card->ctx, "Letting the reader prompt for the CAN on its pin pad.");
545
0
      } else {
546
        /* TODO: Request user input */
547
0
        sc_log(card->ctx, "Unable to query for the CAN. Aborting.");
548
0
        LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
549
0
      }
550
0
    }
551
0
  }
552
553
  /* Establish secure channel via PACE */
554
0
  memset(&pace_input, 0, sizeof pace_input);
555
0
  memset(&pace_output, 0, sizeof pace_output);
556
557
0
  pace_input.pin_id = ref;
558
0
  pace_input.pin = pin;
559
0
  pace_input.pin_length = pinlen;
560
561
  /* Select the right application for authentication. */
562
0
  r = dtrust_select_app(card, ref);
563
0
  LOG_TEST_RET(card->ctx, r, "Selecting application failed");
564
565
0
  r = perform_pace(card, pace_input, &pace_output, EAC_TR_VERSION_2_02);
566
567
  /* We need to track whether we established a PACE channel. Checking
568
   * against card->sm_ctx.sm_mode != SM_MODE_TRANSMIT is not sufficient
569
   * as PACE-capable card readers handle secure messaging transparently. */
570
0
  if (r == SC_SUCCESS) {
571
0
    drv_data->pace = 1;
572
0
  }
573
574
  /* We further need to track whether we authenticated against CAN as
575
   * only this PINs allows us to verify the QES or AUT-PIN. */
576
0
  if (ref == PACE_PIN_ID_CAN) {
577
0
    drv_data->can = r == SC_SUCCESS;
578
0
  }
579
580
0
  free(pace_output.ef_cardaccess);
581
0
  free(pace_output.recent_car);
582
0
  free(pace_output.previous_car);
583
0
  free(pace_output.id_icc);
584
0
  free(pace_output.id_pcd);
585
586
0
  if (tries_left != NULL) {
587
0
    if (r != SC_SUCCESS &&
588
0
        pace_output.mse_set_at_sw1 == 0x63 &&
589
0
        (pace_output.mse_set_at_sw2 & 0xc0) == 0xc0) {
590
0
      *tries_left = pace_output.mse_set_at_sw2 & 0x0f;
591
0
    } else {
592
0
      *tries_left = -1;
593
0
    }
594
0
  }
595
596
  /* Write CAN to the cache, if it is correct and the cache was initially empty. */
597
0
  if (ref == PACE_PIN_ID_CAN && pin != NULL && drv_data->can_cache &&
598
0
      r == SC_SUCCESS && can_len == 0) {
599
0
    sc_pkcs15_cache_file(drv_data->p15card, &can_path, pin, pinlen);
600
0
  }
601
602
0
  return r;
603
0
}
604
605
static int
606
dtrust_pin_cmd_get_info(struct sc_card *card,
607
    struct sc_pin_cmd_data *data)
608
0
{
609
0
  struct dtrust_drv_data_t *drv_data;
610
0
  int r;
611
612
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
613
614
0
  drv_data = card->drv_data;
615
616
0
  switch (data->pin_reference) {
617
0
  case PACE_PIN_ID_CAN:
618
    /* unlimited number of retries */
619
0
    data->pin1.max_tries = -1;
620
0
    data->pin1.tries_left = -1;
621
0
    r = SC_SUCCESS;
622
0
    break;
623
624
0
  case PACE_PIN_ID_PUK:
625
0
  case DTRUST5_PIN_ID_PIN_T:
626
0
  case DTRUST5_PIN_ID_PIN_T_AUT:
627
    /* Select the right application for authentication. */
628
0
    r = dtrust_select_app(card, data->pin_reference);
629
0
    LOG_TEST_RET(card->ctx, r, "Selecting application failed");
630
631
    /* FIXME: Doesn't work. Returns SW1=69 SW2=85 (Conditions of use not satisfied) instead. */
632
0
    data->pin1.max_tries = 3;
633
0
    r = eac_pace_get_tries_left(card, data->pin_reference, &data->pin1.tries_left);
634
0
    break;
635
636
0
  default:
637
    /* Check if CAN authentication is necessary */
638
0
    if (!drv_data->can) {
639
      /* Establish a secure channel with CAN to query PIN information. */
640
0
      r = dtrust_perform_pace(card, PACE_PIN_ID_CAN, NULL, 0, NULL);
641
0
      LOG_TEST_RET(card->ctx, r, "CAN authentication failed");
642
643
      /* Select the right application again. */
644
0
      r = dtrust_select_app(card, data->pin_reference);
645
0
      LOG_TEST_RET(card->ctx, r, "Selecting application failed");
646
0
    }
647
648
    /* Now query PIN information */
649
0
    r = iso_ops->pin_cmd(card, data);
650
0
    break;
651
0
  }
652
653
0
  LOG_FUNC_RETURN(card->ctx, r);
654
0
}
655
656
static int
657
dtrust_pin_cmd_verify(struct sc_card *card,
658
    struct sc_pin_cmd_data *data)
659
0
{
660
0
  struct dtrust_drv_data_t *drv_data;
661
0
  int r;
662
663
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
664
665
0
  drv_data = card->drv_data;
666
667
0
  switch (data->pin_reference) {
668
  /* When the retry counter reaches 1 PACE-PINs become suspended. Before
669
   * verifying a suspended PIN, the CAN has to verified. We go without
670
   * verifying the CAN here, as this only matters for the PUK and the
671
   * transport PIN. Neither PIN ist required during normal operation. The
672
   * user has to resume a suspended PIN using dtrust-tool which manages
673
   * CAN authentication. */
674
0
  case PACE_PIN_ID_CAN:
675
0
  case PACE_PIN_ID_PUK:
676
0
  case DTRUST5_PIN_ID_PIN_T:
677
0
  case DTRUST5_PIN_ID_PIN_T_AUT:
678
    /* Establish secure channel via PACE */
679
0
    r = dtrust_perform_pace(card, data->pin_reference, data->pin1.data, data->pin1.len, &data->pin1.tries_left);
680
0
    break;
681
682
0
  default:
683
    /* Check if CAN authentication is necessary */
684
0
    if (!drv_data->can) {
685
      /* Establish a secure channel with CAN to to verify the PINs. */
686
0
      r = dtrust_perform_pace(card, PACE_PIN_ID_CAN, NULL, 0, NULL);
687
0
      LOG_TEST_RET(card->ctx, r, "CAN authentication failed");
688
689
      /* Select the right application again. */
690
0
      r = dtrust_select_app(card, data->pin_reference);
691
0
      LOG_TEST_RET(card->ctx, r, "Selecting application failed");
692
0
    }
693
694
    /* Now verify the PIN */
695
0
    r = iso_ops->pin_cmd(card, data);
696
697
0
    break;
698
0
  }
699
700
0
  LOG_FUNC_RETURN(card->ctx, r);
701
0
}
702
703
static int
704
dtrust_pin_cmd(struct sc_card *card,
705
    struct sc_pin_cmd_data *data)
706
0
{
707
0
  struct dtrust_drv_data_t *drv_data;
708
0
  int r;
709
710
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
711
712
0
  drv_data = card->drv_data;
713
714
0
  if (!data)
715
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
716
717
  /* Upper layers may try to verify the PIN twice, first with PIN type
718
   * SC_AC_CHV and then with PIN type SC_AC_CONTEXT_SPECIFIC. For the
719
   * second attempt we first check by SC_PIN_CMD_GET_INFO whether a
720
   * second PIN authentication is still necessary. If not, we simply
721
   * return without a second verification attempt. Otherwise we perform
722
   * the verification as requested. This only matters for pin pad readers
723
   * to prevent the user from prompting the PIN twice. */
724
0
  if (data->cmd == SC_PIN_CMD_VERIFY && data->pin_type == SC_AC_CONTEXT_SPECIFIC) {
725
0
    struct sc_pin_cmd_data data2;
726
727
0
    sc_log(card->ctx, "Checking if verification of PIN 0x%02x is necessary.", data->pin_reference);
728
729
0
    memset(&data2, 0, sizeof(struct sc_pin_cmd_data));
730
0
    data2.pin_reference = data->pin_reference;
731
0
    data2.pin1 = data->pin1;
732
733
    /* Check verification state */
734
0
    data2.cmd = SC_PIN_CMD_GET_INFO;
735
0
    data2.pin_type = data->pin_type;
736
0
    r = dtrust_pin_cmd(card, &data2);
737
738
0
    if (data2.pin1.logged_in & SC_PIN_STATE_LOGGED_IN) {
739
      /* Return if we are already authenticated */
740
0
      sc_log(card->ctx, "PIN 0x%02x already verified. Skipping authentication.", data->pin_reference);
741
742
0
      data->pin1 = data2.pin1;
743
0
      LOG_FUNC_RETURN(card->ctx, r);
744
0
    }
745
746
0
    sc_log(card->ctx, "Additional verification of PIN 0x%02x is necessary.", data->pin_reference);
747
0
  }
748
749
  /* No special handling for D-Trust Card 4.1/4.4 */
750
0
  if (card->type >= SC_CARD_TYPE_DTRUST_V4_1_STD && card->type <= SC_CARD_TYPE_DTRUST_V4_4_MULTI) {
751
0
    r = iso_ops->pin_cmd(card, data);
752
0
    LOG_FUNC_RETURN(card->ctx, r);
753
0
  }
754
755
0
  switch (data->cmd) {
756
0
  case SC_PIN_CMD_GET_INFO:
757
0
    r = dtrust_pin_cmd_get_info(card, data);
758
0
    break;
759
760
0
  case SC_PIN_CMD_VERIFY:
761
0
    r = dtrust_pin_cmd_verify(card, data);
762
0
    break;
763
764
0
  case SC_PIN_CMD_CHANGE:
765
    /* The card requires a secure channel to change the PIN.
766
     * Although we could return the error code of the card, we
767
     * prevent to send the APDU in case no secure channel was
768
     * established. This prevents us from exposing our new PIN
769
     * inadvertently in plaintext over the contactless interface in
770
     * case of a software error in the upper layers. */
771
0
    if (!drv_data->pace) {
772
0
      sc_log(card->ctx, "Secure channel required for PIN change");
773
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_SECURITY_STATUS_NOT_SATISFIED);
774
0
    }
775
776
0
    if (data->pin1.len != 0 || !(data->flags & SC_PIN_CMD_IMPLICIT_CHANGE)) {
777
0
      sc_log(card->ctx, "Card supports implicit PIN change only");
778
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
779
0
    }
780
781
0
    if (data->pin2.len == 0 && !(data->flags & SC_PIN_CMD_USE_PINPAD)) {
782
0
      sc_log(card->ctx, "No value provided for the new PIN");
783
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
784
0
    }
785
786
0
    r = iso_ops->pin_cmd(card, data);
787
0
    break;
788
789
0
  case SC_PIN_CMD_UNBLOCK:
790
    /* The supports only to reset the retry counter to its default
791
     * value, but not to set verify or set a PIN. */
792
0
    if (data->pin1.len != 0 || data->pin2.len != 0 ||
793
0
        data->flags & SC_PIN_CMD_USE_PINPAD) {
794
0
      sc_log(card->ctx, "Card supports retry counter reset only");
795
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
796
0
    }
797
798
0
    r = iso_ops->pin_cmd(card, data);
799
0
    break;
800
801
0
  default:
802
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
803
0
  }
804
805
0
  LOG_FUNC_RETURN(card->ctx, r);
806
0
}
807
808
static int
809
dtrust_set_security_env(sc_card_t *card,
810
    const sc_security_env_t *env,
811
    int se_num)
812
0
{
813
0
  struct dtrust_drv_data_t *drv_data;
814
815
0
  if (card == NULL || env == NULL)
816
0
    return SC_ERROR_INVALID_ARGUMENTS;
817
818
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
819
820
0
  drv_data = card->drv_data;
821
0
  drv_data->env = env;
822
823
0
  if (!(env->flags & SC_SEC_ENV_KEY_REF_PRESENT) || env->key_ref_len != 1) {
824
0
    sc_log(card->ctx, "No or invalid key reference");
825
0
    return SC_ERROR_INVALID_ARGUMENTS;
826
0
  }
827
828
  /*
829
   * The card does not support to set a security environment. Instead a
830
   * predefined template has to be loaded via MSE RESTORE which depends
831
   * on the algorithm used.
832
   */
833
834
0
  switch (env->operation) {
835
0
  case SC_SEC_OPERATION_DECIPHER:
836
0
    if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_02) {
837
0
      se_num = 0x31;
838
0
    } else if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_OAEP) {
839
0
      switch (env->algorithm_flags & SC_ALGORITHM_MGF1_HASHES) {
840
0
      case SC_ALGORITHM_MGF1_SHA256:
841
0
        se_num = 0x32;
842
0
        break;
843
0
      case SC_ALGORITHM_MGF1_SHA384:
844
0
        se_num = 0x33;
845
0
        break;
846
0
      case SC_ALGORITHM_MGF1_SHA512:
847
0
        se_num = 0x34;
848
0
        break;
849
850
0
      default:
851
0
        return SC_ERROR_NOT_SUPPORTED;
852
0
      }
853
0
    } else {
854
0
      return SC_ERROR_NOT_SUPPORTED;
855
0
    }
856
0
    break;
857
858
0
  case SC_SEC_OPERATION_SIGN:
859
0
    if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01) {
860
0
      switch (env->algorithm_flags & SC_ALGORITHM_RSA_HASHES) {
861
0
      case SC_ALGORITHM_RSA_HASH_SHA256:
862
0
        se_num = 0x25;
863
0
        break;
864
0
      case SC_ALGORITHM_RSA_HASH_SHA384:
865
0
        se_num = 0x26;
866
0
        break;
867
0
      case SC_ALGORITHM_RSA_HASH_SHA512:
868
0
        se_num = 0x27;
869
0
        break;
870
871
0
      default:
872
0
        return SC_ERROR_NOT_SUPPORTED;
873
0
      }
874
0
    } else if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PSS) {
875
      /*
876
       * According to the specification the message digest has
877
       * to match the hash function used for the PSS scheme.
878
       * We don't enforce this constraint here as the output
879
       * is valid in all cases as long as the message digest
880
       * is calculated in software and not on the card.
881
       */
882
883
0
      switch (env->algorithm_flags & SC_ALGORITHM_MGF1_HASHES) {
884
0
      case SC_ALGORITHM_MGF1_SHA256:
885
0
        se_num = 0x19;
886
0
        break;
887
0
      case SC_ALGORITHM_MGF1_SHA384:
888
0
        se_num = 0x1A;
889
0
        break;
890
0
      case SC_ALGORITHM_MGF1_SHA512:
891
0
        se_num = 0x1B;
892
0
        break;
893
894
0
      default:
895
0
        return SC_ERROR_NOT_SUPPORTED;
896
0
      }
897
0
    } else if (env->algorithm_flags & SC_ALGORITHM_ECDSA_RAW) {
898
0
      switch (card->type) {
899
0
      case SC_CARD_TYPE_DTRUST_V4_1_MULTI:
900
0
      case SC_CARD_TYPE_DTRUST_V4_1_M100:
901
0
      case SC_CARD_TYPE_DTRUST_V4_4_MULTI:
902
        /* ECDSA on SHA-256 hashes. Other hashes will work though. */
903
0
        se_num = 0x21;
904
0
        break;
905
906
0
      case SC_CARD_TYPE_DTRUST_V5_1_MULTI:
907
0
      case SC_CARD_TYPE_DTRUST_V5_1_M100:
908
0
      case SC_CARD_TYPE_DTRUST_V5_4_MULTI:
909
        /* ECDSA on SHA-384 hashes. Other hashes will work though. */
910
0
        se_num = 0x22;
911
0
        break;
912
913
0
      default:
914
0
        return SC_ERROR_NOT_SUPPORTED;
915
0
      }
916
0
    } else {
917
0
      return SC_ERROR_NOT_SUPPORTED;
918
0
    }
919
0
    break;
920
921
0
  case SC_SEC_OPERATION_DERIVE:
922
0
    if (env->algorithm_flags & SC_ALGORITHM_ECDH_CDH_RAW) {
923
0
      se_num = 0x39;
924
0
    } else {
925
0
      return SC_ERROR_NOT_SUPPORTED;
926
0
    }
927
0
    break;
928
929
0
  default:
930
0
    return SC_ERROR_NOT_SUPPORTED;
931
0
  }
932
933
0
  return iso_ops->restore_security_env(card, se_num);
934
0
}
935
936
static int
937
dtrust_compute_signature(struct sc_card *card, const u8 *data,
938
    size_t data_len, u8 *out, size_t outlen)
939
0
{
940
0
  struct dtrust_drv_data_t *drv_data;
941
0
  unsigned long flags;
942
0
  size_t buflen = 0, tmplen;
943
0
  u8 *buf = NULL;
944
0
  int r;
945
946
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
947
948
0
  drv_data = card->drv_data;
949
0
  flags = drv_data->env->algorithm_flags;
950
951
  /*
952
   * PKCS#1 padded signatures require some special handling. When using
953
   * the PKCS#1 scheme, first a digest info OID is prepended to the
954
   * message digest. Afterward this resulting octet string is padded to
955
   * the length of the key modulus. The card performs padding, but
956
   * requires the digest info to be prepended in software.
957
   */
958
959
  /* Only PKCS#1 signature scheme requires special handling */
960
0
  if (!(flags & SC_ALGORITHM_RSA_PAD_PKCS1))
961
0
    return iso_ops->compute_signature(card, data, data_len, out, outlen);
962
963
  /*
964
   * We have to clear the padding flag, because padding is done in
965
   * hardware. We are keeping the hash algorithm flags, to ensure the
966
   * digest info is prepended before padding.
967
   */
968
0
  flags &= ~SC_ALGORITHM_RSA_PAD_PKCS1;
969
0
  flags |= SC_ALGORITHM_RSA_PAD_NONE;
970
971
  /* 32 Bytes should be enough to prepend the digest info */
972
0
  buflen = data_len + 32;
973
0
  buf = sc_mem_secure_alloc(buflen);
974
0
  if (buf == NULL)
975
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
976
977
0
  tmplen = buflen;
978
979
  /* Prepend digest info */
980
0
  r = sc_pkcs1_encode(card->ctx, flags, data, data_len, buf, &tmplen, 0, NULL);
981
0
  LOG_TEST_GOTO_ERR(card->ctx, r, "Prepending digest info failed");
982
983
  /* Do padding in hardware and compute signature */
984
0
  r = iso_ops->compute_signature(card, buf, tmplen, out, outlen);
985
986
0
err:
987
0
  sc_mem_secure_clear_free(buf, buflen);
988
989
0
  return r;
990
0
}
991
992
static int
993
dtrust_decipher(struct sc_card *card, const u8 *data,
994
    size_t data_len, u8 *out, size_t outlen)
995
0
{
996
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
997
998
0
  switch (card->type) {
999
  /* No special handling necessary for RSA cards. */
1000
0
  case SC_CARD_TYPE_DTRUST_V4_1_STD:
1001
0
  case SC_CARD_TYPE_DTRUST_V4_4_STD:
1002
0
  case SC_CARD_TYPE_DTRUST_V5_1_STD:
1003
0
  case SC_CARD_TYPE_DTRUST_V5_4_STD:
1004
0
    LOG_FUNC_RETURN(card->ctx, iso_ops->decipher(card, data, data_len, out, outlen));
1005
1006
  /* Elliptic Curve cards cannot use PSO:DECIPHER command and need to
1007
   * perform key agreement by a CardOS specific command. */
1008
0
  case SC_CARD_TYPE_DTRUST_V4_1_MULTI:
1009
0
  case SC_CARD_TYPE_DTRUST_V4_1_M100:
1010
0
  case SC_CARD_TYPE_DTRUST_V4_4_MULTI:
1011
0
  case SC_CARD_TYPE_DTRUST_V5_1_MULTI:
1012
0
  case SC_CARD_TYPE_DTRUST_V5_1_M100:
1013
0
  case SC_CARD_TYPE_DTRUST_V5_4_MULTI:
1014
0
    LOG_FUNC_RETURN(card->ctx, cardos_ec_compute_shared_value(card, data, data_len, out, outlen));
1015
1016
0
  default:
1017
0
    return SC_ERROR_NOT_SUPPORTED;
1018
0
  }
1019
0
}
1020
1021
static int
1022
dtrust_logout(sc_card_t *card)
1023
0
{
1024
0
  struct dtrust_drv_data_t *drv_data;
1025
0
  int r;
1026
1027
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1028
1029
0
  drv_data = card->drv_data;
1030
1031
0
  sc_sm_stop(card);
1032
0
  drv_data->pace = 0;
1033
0
  drv_data->can = 0;
1034
1035
  /* If PACE is done between reader and card, SM is transparent to us as
1036
   * it ends at the reader. With CLA=0x0C we provoke a SM error to
1037
   * disable SM on the reader. */
1038
0
  if (card->reader->capabilities & SC_READER_CAP_PACE_GENERIC) {
1039
0
    struct sc_apdu apdu;
1040
1041
0
    sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0xA4, 0x00, 0x00);
1042
0
    apdu.cla = 0x0C;
1043
1044
0
    r = sc_transmit_apdu(card, &apdu);
1045
0
    if (r != SC_SUCCESS)
1046
0
      sc_log(card->ctx, "Warning: Could not logout.");
1047
0
  }
1048
1049
0
  r = sc_select_file(card, sc_get_mf_path(), NULL);
1050
1051
0
  LOG_FUNC_RETURN(card->ctx, r);
1052
0
}
1053
1054
struct sc_card_driver *
1055
sc_get_dtrust_driver(void)
1056
15.7k
{
1057
15.7k
  if (iso_ops == NULL)
1058
1
    iso_ops = sc_get_iso7816_driver()->ops;
1059
1060
15.7k
  dtrust_ops = *iso_ops;
1061
15.7k
  dtrust_ops.match_card = dtrust_match_card;
1062
15.7k
  dtrust_ops.init = dtrust_init;
1063
15.7k
  dtrust_ops.finish = dtrust_finish;
1064
15.7k
  dtrust_ops.pin_cmd = dtrust_pin_cmd;
1065
15.7k
  dtrust_ops.set_security_env = dtrust_set_security_env;
1066
15.7k
  dtrust_ops.compute_signature = dtrust_compute_signature;
1067
15.7k
  dtrust_ops.decipher = dtrust_decipher;
1068
15.7k
  dtrust_ops.logout = dtrust_logout;
1069
1070
15.7k
  return &dtrust_drv;
1071
15.7k
}