Coverage Report

Created: 2026-11-30 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/pkcs15-itacns.c
Line
Count
Source
1
/*
2
 * PKCS15 emulation layer for Italian CNS.
3
 *
4
 * Copyright (C) 2008, Emanuele Pucciarelli <ep@acm.org>
5
 * Many snippets have been taken out from other PKCS15 emulation layer
6
 * modules in this directory; their copyright is their authors'.
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
/*
24
 * Specifications for the development of this driver come from:
25
 * http://www.servizidemografici.interno.it/sitoCNSD/documentazioneRicerca.do?metodo=contenutoDocumento&servizio=documentazione&ID_DOCUMENTO=1043
26
 */
27
28
29
#ifdef HAVE_CONFIG_H
30
#include <config.h>
31
#endif
32
33
#include <stdlib.h>
34
#include <string.h>
35
#include <stdio.h>
36
37
#include "internal.h"
38
#include "pkcs15.h"
39
#include "log.h"
40
#include "cards.h"
41
#include "itacns.h"
42
#include "common/compat_strlcpy.h"
43
#include "common/compat_strlcat.h"
44
45
#ifdef ENABLE_OPENSSL
46
#include <openssl/x509.h>
47
#include <openssl/x509v3.h>
48
#endif
49
50
static const char path_serial[] = "10001003";
51
52
/* Manufacturers */
53
54
const char * itacns_mask_manufacturers[] = {
55
  "Unknown",
56
  "Kaitech",
57
  "Gemplus",
58
  "Ghirlanda",
59
  "Giesecke & Devrient",
60
  "IDEMIA (Oberthur)",
61
  "Orga",
62
  "Axalto",
63
  "Siemens",
64
  "STIncard",
65
  "GEP",
66
  "EPS Corp",
67
  "Athena",
68
  "Gemalto",
69
};
70
71
const char * iso7816_ic_manufacturers[] = {
72
  "Unknown",
73
  "Motorola",
74
  "STMicroelectronics",
75
  "Hitachi",
76
  "NXP Semiconductors",
77
  "Infineon",
78
  "Cylinc",
79
  "Texas Instruments",
80
  "Fujitsu",
81
  "Matsushita",
82
  "NEC",
83
  "Oki",
84
  "Toshiba",
85
  "Mitsubishi",
86
  "Samsung",
87
  "Hynix",
88
  "LG",
89
  "Emosyn-EM",
90
  "INSIDE",
91
  "ORGA",
92
  "SHARP",
93
  "ATMEL",
94
  "EM Microelectronic-Marin",
95
  "KSW Microtec",
96
  "ZMD",
97
  "XICOR",
98
  "Sony",
99
  "Malaysia Microelectronic Solutions",
100
  "Emosyn",
101
  "Shanghai Fudan",
102
  "Magellan",
103
  "Melexis",
104
  "Renesas",
105
  "TAGSYS",
106
  "Transcore",
107
  "Shanghai belling",
108
  "Masktech",
109
  "Innovision",
110
  "Hitachi",
111
  "Cypak",
112
  "Ricoh",
113
  "ASK",
114
  "Unicore",
115
  "Dallas",
116
  "Impinj",
117
  "RightPlug Alliance",
118
  "Broadcom",
119
  "MStar",
120
  "BeeDar",
121
  "RFIDsec",
122
  "Schweizer Electronic",
123
  "AMIC Technology",
124
  "Mikron",
125
  "Fraunhofer",
126
  "IDS Microchip",
127
  "Kovio",
128
  "HMT Microelectronic",
129
  "Silicon Craft",
130
  "Advanced Film Device",
131
  "Nitecrest",
132
  "Verayo",
133
  "HID Gloval",
134
  "Productivity Engineering",
135
  "Austriamicrosystems",
136
  "Gemalto"
137
};
138
139
/* Data files */
140
141
static const struct {
142
  const char *label;
143
  const char *path;
144
  int cie_only;
145
} itacns_data_files[] = {
146
  { "EF_DatiProcessore", "3F0010001002", 0 },
147
  { "EF_IDCarta", "3F0010001003", 0 },
148
  { "EF_DatiSistema", "3F0010001004", 1 },
149
  { "EF_DatiPersonali", "3F0011001102", 0 },
150
  { "EF_DatiPersonali_Annotazioni", "3F0011001103", 1 },
151
  { "EF_Impronte", "3F0011001104", 1 },
152
  { "EF_Foto", "3F0011001104", 1 },
153
  { "EF_DatiPersonaliAggiuntivi", "3F0012001201", 0 },
154
  { "EF_MemoriaResidua", "3F0012001202", 0 },
155
  { "EF_ServiziInstallati", "3F0012001203", 0 },
156
  { "EF_INST_FILE", "3F0012004142", 0 },
157
  { "EF_CardStatus", "3F003F02", 0 },
158
  { "EF_GDO", "3F002F02", 0 },
159
  { "EF_RootInstFile", "3F000405", 0 }
160
};
161
162
163
/*
164
 * Utility functions
165
 */
166
167
static int loadFile(const sc_pkcs15_card_t *p15card, const sc_path_t *path,
168
  u8 *buf, const size_t buflen)
169
2.60k
{
170
2.60k
  int sc_res;
171
2.60k
  SC_FUNC_CALLED(p15card->card->ctx, SC_LOG_DEBUG_NORMAL);
172
173
2.60k
  sc_res = sc_select_file(p15card->card, path, NULL);
174
2.60k
  if(sc_res != SC_SUCCESS)
175
811
    return sc_res;
176
177
1.79k
  sc_res = sc_read_binary(p15card->card, 0, buf, buflen, 0);
178
1.79k
  return sc_res;
179
2.60k
}
180
181
/*
182
 * The following functions add objects to the card emulator.
183
 */
184
185
static int itacns_add_cert(sc_pkcs15_card_t *p15card,
186
  int type, int authority, const sc_path_t *path,
187
  const sc_pkcs15_id_t *id, const char *label, int obj_flags,
188
  int *ext_info_ok, int *key_usage, int *x_key_usage, size_t *modulus_len)
189
1.41k
{
190
1.41k
  int r;
191
  /* const char *label = "Certificate"; */
192
1.41k
  sc_pkcs15_cert_info_t info;
193
1.41k
  sc_pkcs15_object_t    obj;
194
1.41k
#ifdef ENABLE_OPENSSL
195
1.41k
  X509 *x509;
196
1.41k
  sc_pkcs15_cert_t *cert;
197
1.41k
  int private_obj;
198
1.41k
#endif
199
200
1.41k
  SC_FUNC_CALLED(p15card->card->ctx, SC_LOG_DEBUG_NORMAL);
201
202
1.41k
  if(type != SC_PKCS15_TYPE_CERT_X509) {
203
0
    sc_log(p15card->card->ctx,
204
0
      "Cannot add a certificate of a type other than X.509");
205
0
    return 1;
206
0
  }
207
208
1.41k
  *ext_info_ok = 0;
209
210
211
1.41k
  memset(&info, 0, sizeof(info));
212
1.41k
  memset(&obj,  0, sizeof(obj));
213
214
1.41k
  info.id                = *id;
215
1.41k
  info.authority         = authority;
216
1.41k
  if (path)
217
1.41k
    info.path = *path;
218
219
1.41k
  strlcpy(obj.label, label, sizeof(obj.label));
220
221
  /* If we have OpenSSL, read keyUsage */
222
1.41k
#ifdef ENABLE_OPENSSL
223
1.41k
  private_obj = obj_flags & SC_PKCS15_CO_FLAG_PRIVATE;
224
1.41k
  r = sc_pkcs15_read_certificate(p15card, &info, private_obj, &cert);
225
1.41k
  LOG_TEST_RET(p15card->card->ctx, r,
226
0
    "Could not read X.509 certificate");
227
228
0
  {
229
0
    const u8 *throwaway = cert->data.value;
230
0
    x509 = d2i_X509(NULL, &throwaway, cert->data.len);
231
0
  }
232
233
0
  if (cert->key && cert->key->algorithm == SC_ALGORITHM_RSA) {
234
0
    *modulus_len = cert->key->u.rsa.modulus.len * 8;
235
0
  }
236
237
0
  sc_pkcs15_free_certificate(cert);
238
0
  if (!x509) {
239
0
    sc_log_openssl(p15card->card->ctx);
240
0
    return SC_SUCCESS;
241
0
  }
242
0
  X509_check_purpose(x509, -1, 0);
243
244
0
  if(X509_get_extension_flags(x509) & EXFLAG_KUSAGE) {
245
0
    *ext_info_ok = 1;
246
0
    *key_usage = X509_get_key_usage(x509);
247
0
    *x_key_usage = X509_get_extended_key_usage(x509);
248
0
  }
249
0
  OPENSSL_free(x509);
250
251
0
#endif /* ENABLE_OPENSSL */
252
253
0
  obj.flags = obj_flags;
254
0
  r = sc_pkcs15emu_add_x509_cert(p15card, &obj, &info);
255
0
  LOG_TEST_RET(p15card->card->ctx, r,
256
0
    "Could not add X.509 certificate");
257
258
0
  return r;
259
0
}
260
261
static int itacns_add_pubkey(sc_pkcs15_card_t *p15card,
262
   const sc_path_t *path, const sc_pkcs15_id_t *id, const char *label,
263
  int usage, int ref, int obj_flags, size_t modulus_len)
264
0
{
265
0
  int r;
266
0
  sc_pkcs15_pubkey_info_t info;
267
0
  sc_pkcs15_object_t obj;
268
269
0
  SC_FUNC_CALLED(p15card->card->ctx, SC_LOG_DEBUG_NORMAL);
270
271
0
  memset(&info, 0, sizeof(info));
272
0
  memset(&obj,  0, sizeof(obj));
273
274
0
  info.id     = *id;
275
0
  if (path)
276
0
    info.path = *path;
277
0
  info.usage    = usage;
278
0
  info.key_reference  = ref;
279
0
  strlcpy(obj.label, label, sizeof(obj.label));
280
0
  obj.flags   = obj_flags;
281
282
0
  info.modulus_length = modulus_len;
283
284
0
  r = sc_pkcs15emu_add_rsa_pubkey(p15card, &obj, &info);
285
0
  LOG_TEST_RET(p15card->card->ctx, r,
286
0
    "Could not add pub key");
287
0
  return r;
288
0
}
289
290
static int itacns_add_prkey(sc_pkcs15_card_t *p15card,
291
                const sc_pkcs15_id_t *id,
292
                const char *label,
293
                int type, size_t modulus_length, int usage,
294
    const sc_path_t *path, int ref,
295
                const sc_pkcs15_id_t *auth_id, int obj_flags)
296
0
{
297
0
  sc_pkcs15_prkey_info_t info;
298
0
  sc_pkcs15_object_t obj;
299
300
0
  SC_FUNC_CALLED(p15card->card->ctx, SC_LOG_DEBUG_NORMAL);
301
302
0
  if(type != SC_PKCS15_TYPE_PRKEY_RSA) {
303
0
    sc_log(p15card->card->ctx,
304
0
      "Cannot add a private key of a type other than RSA");
305
0
    return 1;
306
0
  }
307
308
0
  memset(&info, 0, sizeof(info));
309
0
  memset(&obj,  0, sizeof(obj));
310
311
0
  info.id     = *id;
312
0
  info.modulus_length = modulus_length;
313
0
  info.usage    = usage;
314
0
  info.native   = 1;
315
0
  info.key_reference  = ref;
316
317
0
  if (path)
318
0
          info.path = *path;
319
320
0
  obj.flags = obj_flags;
321
0
  strlcpy(obj.label, label, sizeof(obj.label));
322
0
  if (auth_id != NULL)
323
0
    obj.auth_id = *auth_id;
324
325
0
  return sc_pkcs15emu_add_rsa_prkey(p15card, &obj, &info);
326
0
}
327
328
static int itacns_add_pin(sc_pkcs15_card_t *p15card,
329
  char *label,
330
  int id,
331
  int auth_id,
332
  int reference,
333
  sc_path_t *path,
334
  int flags)
335
0
{
336
0
  struct sc_pkcs15_auth_info pin_info;
337
0
  struct sc_pkcs15_object pin_obj;
338
339
0
  SC_FUNC_CALLED(p15card->card->ctx, SC_LOG_DEBUG_NORMAL);
340
341
0
  memset(&pin_info, 0, sizeof(pin_info));
342
0
  pin_info.auth_type = SC_PKCS15_PIN_AUTH_TYPE_PIN;
343
0
  pin_info.auth_id.len = 1;
344
0
  pin_info.auth_id.value[0] = id;
345
0
  pin_info.attrs.pin.reference = reference;
346
0
  pin_info.attrs.pin.flags = flags;
347
0
  pin_info.attrs.pin.type = SC_PKCS15_PIN_TYPE_ASCII_NUMERIC;
348
0
  pin_info.attrs.pin.min_length = 5;
349
0
  pin_info.attrs.pin.stored_length = 8;
350
0
  pin_info.attrs.pin.max_length = 8;
351
0
  pin_info.attrs.pin.pad_char = 0xff;
352
0
  pin_info.logged_in = SC_PIN_STATE_UNKNOWN;
353
0
  if (p15card->card->type == SC_CARD_TYPE_ITACNS_CNS_JSIGN4)
354
0
    pin_info.tries_left = -1;
355
0
  if(path)
356
0
        pin_info.path = *path;
357
358
0
  memset(&pin_obj, 0, sizeof(pin_obj));
359
0
  strlcpy(pin_obj.label, label, sizeof(pin_obj.label));
360
0
  pin_obj.flags = SC_PKCS15_CO_FLAG_PRIVATE |
361
0
    (auth_id ? SC_PKCS15_CO_FLAG_MODIFIABLE : 0);
362
0
  if (auth_id) {
363
0
    pin_obj.auth_id.len = 1;
364
0
    pin_obj.auth_id.value[0] = auth_id;
365
0
  } else
366
0
    pin_obj.auth_id.len = 0;
367
368
0
  return sc_pkcs15emu_add_pin_obj(p15card, &pin_obj, &pin_info);
369
0
}
370
371
static int hextoint(char *src, unsigned int len)
372
404
{
373
404
  char hex[16];
374
404
  char *end;
375
404
  int res;
376
377
404
  if(len >= sizeof(hex))
378
0
    return -1;
379
404
  strncpy(hex, src, len);
380
404
  hex[len] = '\0';
381
404
  res = (int)strtol(hex, &end, 0x10);
382
404
  if(end != (char*)&hex[len])
383
354
    return -1;
384
50
  return res;
385
404
}
386
387
static int get_name_from_EF_DatiPersonali(unsigned char *EFdata,
388
  size_t EFdata_len, char name[], int name_len)
389
384
{
390
384
  const unsigned int EF_personaldata_maxlen = 400;
391
384
  const unsigned int tlv_length_size = 6;
392
384
  char *file = NULL;
393
384
  int file_size;
394
395
  /*
396
   * Bytes 0-5 contain the ASCII encoding of the following TLV
397
   * structure's total size, in base 16.
398
   */
399
384
  if (EFdata_len < tlv_length_size) {
400
    /* We need at least 6 bytes for file length here */
401
14
    return -1;
402
14
  }
403
370
  file_size = hextoint((char*)EFdata, tlv_length_size);
404
370
  if (EFdata_len < (file_size + tlv_length_size)) {
405
    /* Inconsistent external file length and internal file length
406
     * suggests we are trying to process junk data.
407
     * If the internal data length is shorter, the data can be padded,
408
     * but we should be fine as we will not go behind the buffer limits */
409
4
    return -1;
410
4
  }
411
366
  file = (char*)&EFdata[tlv_length_size];
412
413
366
  enum {
414
366
    f_issuer_code = 0,
415
366
    f_issuing_date,
416
366
    f_expiry_date,
417
366
    f_last_name,
418
366
    f_first_name,
419
366
    f_birth_date,
420
366
    f_sex,
421
366
    f_height,
422
366
    f_codice_fiscale,
423
366
    f_citizenship_code,
424
366
    f_birth_township_code,
425
366
    f_birth_country,
426
366
    f_birth_certificate,
427
366
    f_residence_township_code,
428
366
    f_residence_address,
429
366
    f_expat_notes
430
366
  };
431
432
  /* Read the fields up to f_first_name */
433
366
  struct {
434
366
    int len;
435
366
    char value[256];
436
366
  } fields[f_first_name+1];
437
366
  int i=0; /* offset inside the file */
438
366
  int f; /* field number */
439
440
366
  if (file_size < 0)
441
347
    return -1;
442
443
  /*
444
   * This shouldn't happen, but let us be protected against wrong
445
   * or malicious cards
446
   */
447
19
  if(file_size > (int)EF_personaldata_maxlen - (int)tlv_length_size)
448
0
    file_size = EF_personaldata_maxlen - tlv_length_size;
449
450
451
19
  memset(fields, 0, sizeof(fields));
452
453
39
  for(f=0; f<f_first_name+1; f++) {
454
38
    int field_size;
455
456
    /* Don't read beyond the allocated buffer */
457
38
    if(i+2 > file_size)
458
4
      return -1;
459
34
    field_size = hextoint((char*) &file[i], 2);
460
34
    i += 2;
461
462
34
    if (field_size < 0
463
27
        || i + field_size > file_size
464
20
        || field_size >= (int)sizeof(fields[f].value))
465
14
      return -1;
466
467
20
    fields[f].len = field_size;
468
20
    strncpy(fields[f].value, &file[i], field_size);
469
20
    fields[f].value[field_size] = '\0';
470
20
    i += field_size;
471
20
  }
472
473
1
  if (fields[f_first_name].len + fields[f_last_name].len + 1 >= name_len)
474
0
    return -1;
475
476
  /* the lengths are already checked that they will fit in buffer */
477
1
  snprintf(name, name_len, "%.*s %.*s",
478
1
    fields[f_first_name].len, fields[f_first_name].value,
479
1
    fields[f_last_name].len, fields[f_last_name].value);
480
1
  return 0;
481
1
}
482
483
static int itacns_add_data_files(sc_pkcs15_card_t *p15card)
484
1.15k
{
485
1.15k
  const size_t array_size =
486
1.15k
    sizeof(itacns_data_files)/sizeof(itacns_data_files[0]);
487
1.15k
  unsigned int i;
488
1.15k
  int rv;
489
1.15k
  sc_pkcs15_data_t *p15_personaldata = NULL;
490
1.15k
  sc_pkcs15_data_info_t dinfo;
491
1.15k
  struct sc_pkcs15_object *objs[32];
492
1.15k
  struct sc_pkcs15_data_info *cinfo;
493
1.15k
  int private_obj;
494
495
17.2k
  for(i=0; i < array_size; i++) {
496
16.1k
    sc_path_t path;
497
16.1k
    sc_pkcs15_data_info_t data;
498
16.1k
    sc_pkcs15_object_t    obj;
499
500
16.1k
    if (itacns_data_files[i].cie_only &&
501
4.61k
      p15card->card->type != SC_CARD_TYPE_ITACNS_CIE_V2)
502
3.58k
      continue;
503
504
12.5k
    sc_format_path(itacns_data_files[i].path, &path);
505
506
12.5k
    memset(&data, 0, sizeof(data));
507
12.5k
    memset(&obj, 0, sizeof(obj));
508
12.5k
    strlcpy(data.app_label, itacns_data_files[i].label,
509
12.5k
      sizeof(data.app_label));
510
12.5k
    strlcpy(obj.label, itacns_data_files[i].label,
511
12.5k
      sizeof(obj.label));
512
12.5k
    data.path = path;
513
12.5k
    rv = sc_pkcs15emu_add_data_object(p15card, &obj, &data);
514
12.5k
    LOG_TEST_RET(p15card->card->ctx, rv,
515
12.5k
      "Could not add data file");
516
12.5k
  }
517
518
  /*
519
   * If we got this far, we can read the Personal Data file and glean
520
   * the user's full name. Thus we can use it to put together a
521
   * user-friendlier card name.
522
   */
523
1.15k
  memset(&dinfo, 0, sizeof(dinfo));
524
1.15k
  strlcpy(dinfo.app_label, "EF_DatiPersonali", sizeof(dinfo.app_label));
525
526
  /* Find EF_DatiPersonali */
527
528
1.15k
  rv = sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_DATA_OBJECT,
529
1.15k
    objs, 32);
530
1.15k
  if(rv < 0) {
531
0
    sc_log(p15card->card->ctx,
532
0
      "Data enumeration failed");
533
0
    return SC_SUCCESS;
534
0
  }
535
536
3.71k
  for(i=0; i<32; i++) {
537
3.71k
    cinfo = (struct sc_pkcs15_data_info *) objs[i]->data;
538
3.71k
    if(!strcmp("EF_DatiPersonali", objs[i]->label))
539
1.15k
      break;
540
3.71k
  }
541
542
1.15k
  if(i>=32) {
543
0
    sc_log(p15card->card->ctx,
544
0
      "Could not find EF_DatiPersonali: "
545
0
      "keeping generic card name");
546
0
    return SC_SUCCESS;
547
0
  }
548
549
1.15k
  private_obj = objs[i]->flags & SC_PKCS15_CO_FLAG_PRIVATE;
550
1.15k
  rv = sc_pkcs15_read_data_object(p15card, cinfo, private_obj, &p15_personaldata);
551
1.15k
  if (rv) {
552
275
    sc_log(p15card->card->ctx,
553
275
      "Could not read EF_DatiPersonali: "
554
275
      "keeping generic card name");
555
275
    return SC_SUCCESS;
556
275
  }
557
558
878
  if (p15_personaldata->data) {
559
384
    char fullname[160];
560
384
    if (get_name_from_EF_DatiPersonali(p15_personaldata->data,
561
384
      p15_personaldata->data_len, fullname, sizeof(fullname))) {
562
383
      sc_log(p15card->card->ctx,
563
383
        "Could not parse EF_DatiPersonali: "
564
383
        "keeping generic card name");
565
383
      sc_pkcs15_free_data_object(p15_personaldata);
566
383
      free(cinfo->data.value);
567
383
      cinfo->data.value = NULL;
568
383
      return SC_SUCCESS;
569
383
    }
570
1
    set_string(&p15card->tokeninfo->label, fullname);
571
1
  }
572
495
  free(cinfo->data.value);
573
495
  cinfo->data.value = NULL;
574
495
  sc_pkcs15_free_data_object(p15_personaldata);
575
495
  return SC_SUCCESS;
576
878
}
577
578
static int itacns_add_keyset(sc_pkcs15_card_t *p15card,
579
  const char *label, int sec_env, sc_pkcs15_id_t *cert_id,
580
  const char *pubkey_path, const char *prkey_path,
581
  unsigned int pubkey_usage_flags, unsigned int prkey_usage_flags,
582
  u8 pin_ref, size_t modulus_len)
583
0
{
584
0
  int r;
585
0
  sc_path_t path;
586
0
  sc_path_t *private_path = NULL;
587
0
  char pinlabel[16];
588
0
  int fake_puk_authid, pin_flags;
589
590
591
  /* Public key; not really needed */
592
  /* FIXME: set usage according to the certificate. */
593
0
  if (pubkey_path) {
594
0
    sc_format_path(pubkey_path, &path);
595
0
    r = itacns_add_pubkey(p15card, &path, cert_id, label,
596
0
      pubkey_usage_flags, sec_env, 0, modulus_len);
597
0
    LOG_TEST_RET(p15card->card->ctx, r,
598
0
      "Could not add public key");
599
0
  }
600
601
  /*
602
   * FIXME: usage should be inferred from the X.509 certificate, and not
603
   * from whether the key needs Secure Messaging.
604
   */
605
0
  if (prkey_path) {
606
0
    sc_format_path(prkey_path, &path);
607
0
    private_path = &path;
608
0
  }
609
0
  r = itacns_add_prkey(p15card, cert_id, label, SC_PKCS15_TYPE_PRKEY_RSA,
610
0
    modulus_len,
611
0
    prkey_usage_flags,
612
0
    private_path, sec_env, cert_id, SC_PKCS15_CO_FLAG_PRIVATE);
613
0
  LOG_TEST_RET(p15card->card->ctx, r,
614
0
    "Could not add private key");
615
616
  /* PIN and PUK */
617
0
  strlcpy(pinlabel, "PIN ", sizeof(pinlabel));
618
0
  strlcat(pinlabel, label, sizeof(pinlabel));
619
620
  /* We are making up ID 0x90+ to link the PIN and the PUK. */
621
0
  fake_puk_authid = 0x90 + pin_ref;
622
0
  pin_flags = SC_PKCS15_PIN_FLAG_CASE_SENSITIVE
623
0
    | SC_PKCS15_PIN_FLAG_INITIALIZED;
624
0
  r = itacns_add_pin(p15card, pinlabel, sec_env, fake_puk_authid, pin_ref,
625
0
      private_path, pin_flags);
626
0
  LOG_TEST_RET(p15card->card->ctx, r,
627
0
    "Could not add PIN");
628
629
0
  strlcpy(pinlabel, "PUK ", sizeof(pinlabel));
630
0
  strlcat(pinlabel, label, sizeof(pinlabel));
631
  /*
632
   * Looking at pkcs15-tcos.c and pkcs15-framework.c, it seems that the
633
   * right thing to do here is to define a PUK as a SO PIN. Can anybody
634
   * comment on this?
635
   */
636
0
  pin_flags |= SC_PKCS15_PIN_FLAG_UNBLOCKING_PIN
637
0
  | SC_PKCS15_PIN_FLAG_UNBLOCK_DISABLED;
638
0
  r = itacns_add_pin(p15card, pinlabel, fake_puk_authid, 0, pin_ref+1,
639
0
      private_path, pin_flags);
640
0
  LOG_TEST_RET(p15card->card->ctx, r,
641
0
    "Could not add PUK");
642
643
0
  return 0;
644
0
}
645
646
/*
647
 * itacns_check_and_add_keyset() checks for the existence and correctness
648
 * of an X.509 certificate. If it is all right, it adds the related keys;
649
 * otherwise it aborts.
650
 */
651
652
static int itacns_check_and_add_keyset(sc_pkcs15_card_t *p15card,
653
  const char *label, int sec_env, size_t cert_offset,
654
  const char *cert_path, const char *pubkey_path, const char *prkey_path,
655
  u8 pin_ref, int *found_certificates)
656
2.39k
{
657
2.39k
  int r;
658
2.39k
  sc_path_t path;
659
2.39k
  sc_pkcs15_id_t cert_id;
660
2.39k
  int ext_info_ok;
661
2.39k
  int ku = 0, xku = 0;
662
2.39k
  size_t modulus_len = 0;
663
2.39k
  int pubkey_usage_flags = 0, prkey_usage_flags = 0;
664
665
2.39k
  cert_id.len = 1;
666
2.39k
  cert_id.value[0] = sec_env;
667
2.39k
  *found_certificates = 0;
668
669
  /* Certificate */
670
2.39k
  if (!cert_path) {
671
0
    sc_log(p15card->card->ctx,
672
0
      "We cannot use keys without a matching certificate");
673
0
    return SC_ERROR_NOT_SUPPORTED;
674
0
  }
675
676
2.39k
  sc_format_path(cert_path, &path);
677
2.39k
  r = sc_select_file(p15card->card, &path, NULL);
678
2.39k
  if (r == SC_ERROR_FILE_NOT_FOUND)
679
181
    return 0;
680
2.21k
  if (r != SC_SUCCESS) {
681
685
    sc_log(p15card->card->ctx,
682
685
      "Could not find certificate for %s", label);
683
685
    return r;
684
685
  }
685
686
  /*
687
   * Infocamere 1204 (and others?) store a more complex structure. We
688
   * are going to read the first bytes to guess its length, and invoke
689
   * itacns_add_cert so that it only reads the certificate.
690
   */
691
1.52k
  if (cert_offset) {
692
639
    u8 certlen[3];
693
639
    memset(certlen, 0, sizeof certlen);
694
639
    r = loadFile(p15card, &path, certlen, sizeof(certlen));
695
639
    LOG_TEST_RET(p15card->card->ctx, r,
696
580
      "Could not read certificate file");
697
580
    if (r < 3)
698
31
      return SC_ERROR_INVALID_DATA;
699
549
    path.index = (int)cert_offset;
700
549
    path.count = (certlen[1] << 8) + certlen[2];
701
    /* If those bytes are 00, then we are probably dealing with an
702
     * empty file. */
703
549
    if (path.count == 0)
704
25
      return 0;
705
549
  }
706
707
1.41k
  r = itacns_add_cert(p15card, SC_PKCS15_TYPE_CERT_X509, 0,
708
1.41k
    &path, &cert_id, label, 0, &ext_info_ok, &ku, &xku, &modulus_len);
709
1.41k
  if (r == SC_ERROR_INVALID_ASN1_OBJECT)
710
1.09k
    return 0;
711
314
  LOG_TEST_RET(p15card->card->ctx, r,
712
0
    "Could not add certificate");
713
0
  (*found_certificates)++;
714
715
  /* Set usage flags */
716
0
  if(ext_info_ok) {
717
0
#ifdef ENABLE_OPENSSL
718
0
    if (ku & KU_DIGITAL_SIGNATURE) {
719
0
      pubkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_VERIFY;
720
0
      prkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_SIGN;
721
0
    }
722
0
    if (ku & KU_NON_REPUDIATION) {
723
0
      pubkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_VERIFY;
724
0
      prkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_NONREPUDIATION;
725
0
    }
726
0
    if (ku & KU_KEY_ENCIPHERMENT || ku & KU_KEY_AGREEMENT
727
0
      || xku & XKU_SSL_CLIENT) {
728
0
      pubkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_WRAP;
729
0
      prkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_UNWRAP;
730
0
    }
731
0
    if (ku & KU_DATA_ENCIPHERMENT || xku & XKU_SMIME) {
732
0
      pubkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_ENCRYPT;
733
0
      prkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_DECRYPT;
734
0
    }
735
#else /* ENABLE_OPENSSL */
736
    sc_log(p15card->card->ctx,
737
      "Extended certificate info retrieved without OpenSSL. "
738
      "How is this possible?");
739
    return SC_ERROR_INTERNAL;
740
#endif /* ENABLE_OPENSSL */
741
0
  } else {
742
    /* Certificate info not retrieved; fall back onto defaults */
743
0
    pubkey_usage_flags =
744
0
        SC_PKCS15_PRKEY_USAGE_VERIFY
745
0
      | SC_PKCS15_PRKEY_USAGE_WRAP;
746
0
    prkey_usage_flags =
747
0
        SC_PKCS15_PRKEY_USAGE_SIGN
748
0
      | SC_PKCS15_PRKEY_USAGE_UNWRAP;
749
0
  }
750
751
0
  r = itacns_add_keyset(p15card, label, sec_env, &cert_id,
752
0
    pubkey_path, prkey_path, pubkey_usage_flags, prkey_usage_flags,
753
0
    pin_ref, modulus_len);
754
0
  LOG_TEST_RET(p15card->card->ctx, r,
755
0
    "Could not add keys for this certificate");
756
757
0
  return r;
758
0
}
759
760
/* Initialization. */
761
762
static int itacns_init(sc_pkcs15_card_t *p15card)
763
1.97k
{
764
1.97k
  int r;
765
1.97k
  sc_path_t path;
766
1.97k
  int certificate_count = 0;
767
1.97k
  int found_certs;
768
1.97k
  int card_is_cie_v1, cns0_secenv;
769
770
1.97k
  SC_FUNC_CALLED(p15card->card->ctx, SC_LOG_DEBUG_NORMAL);
771
772
1.97k
  set_string(&p15card->tokeninfo->label, p15card->card->name);
773
1.97k
  if(p15card->card->drv_data) {
774
1.97k
    unsigned int mask_code, ic_code;
775
1.97k
    char buffer[256];
776
1.97k
    itacns_drv_data_t *data =
777
1.97k
      (itacns_drv_data_t*) p15card->card->drv_data;
778
1.97k
    mask_code = data->mask_manufacturer_code;
779
1.97k
    if (mask_code >= sizeof(itacns_mask_manufacturers)
780
1.97k
      /sizeof(itacns_mask_manufacturers[0]))
781
1.47k
      mask_code = 0;
782
1.97k
    ic_code = data->ic_manufacturer_code;
783
1.97k
    if (ic_code >= sizeof(iso7816_ic_manufacturers)
784
1.97k
      /sizeof(iso7816_ic_manufacturers[0]))
785
1.38k
      ic_code = 0;
786
1.97k
    snprintf(buffer, sizeof(buffer), "IC: %s; mask: %s",
787
1.97k
      iso7816_ic_manufacturers[ic_code],
788
1.97k
      itacns_mask_manufacturers[mask_code]);
789
1.97k
    set_string(&p15card->tokeninfo->manufacturer_id, buffer);
790
1.97k
  }
791
792
  /* Read and set serial */
793
1.97k
  {
794
1.97k
    u8 serial[17];
795
1.97k
    int bytes;
796
1.97k
    sc_format_path(path_serial, &path);
797
1.97k
    bytes = loadFile(p15card, &path, serial, 16);
798
1.97k
    if (bytes < 0) return bytes;
799
1.15k
    if (bytes > 16) return -1;
800
1.15k
    serial[bytes] = '\0';
801
1.15k
    set_string(&p15card->tokeninfo->serial_number, (char*)serial);
802
1.15k
  }
803
804
  /* Is the card a CIE v1? */
805
0
  card_is_cie_v1 =
806
1.15k
       (p15card->card->type == SC_CARD_TYPE_ITACNS_CIE_V1)
807
1.01k
    || (p15card->card->type == SC_CARD_TYPE_CARDOS_CIE_V1);
808
1.15k
  cns0_secenv = (card_is_cie_v1 ? 0x31 : 0x01);
809
810
  /* If it's a Siemens CIE v1 card, set algo flags accordingly. */
811
1.15k
  if (card_is_cie_v1) {
812
140
    int i;
813
280
    for (i = 0; i < p15card->card->algorithm_count; i++) {
814
140
      sc_algorithm_info_t *info =
815
140
        &p15card->card->algorithms[i];
816
817
140
      if (info->algorithm != SC_ALGORITHM_RSA)
818
0
        continue;
819
140
      info->flags &= ~(SC_ALGORITHM_RSA_RAW
820
140
        | SC_ALGORITHM_RSA_HASH_NONE);
821
140
      info->flags |= (SC_ALGORITHM_RSA_PAD_PKCS1
822
140
        | SC_ALGORITHM_RSA_HASHES);
823
140
    }
824
140
  }
825
826
  /* Data files */
827
1.15k
  r = itacns_add_data_files(p15card);
828
1.15k
  LOG_TEST_GOTO_ERR(p15card->card->ctx, r,
829
1.15k
    "Could not add data files");
830
831
  /*** Certificate and keys. ***/
832
  /* Standard CNS */
833
1.15k
  r = itacns_check_and_add_keyset(p15card, "CNS0", cns0_secenv,
834
1.15k
    0, "3F0011001101", "3F003F01", NULL,
835
1.15k
    0x10, &found_certs);
836
1.15k
  LOG_TEST_GOTO_ERR(p15card->card->ctx, r,
837
737
    "Could not add CNS0");
838
737
  certificate_count += found_certs;
839
840
  /* Infocamere 1204 */
841
737
  r = itacns_check_and_add_keyset(p15card, "CNS01", 0x21,
842
737
    5, "3F002FFF8228", NULL, "3F002FFF0000",
843
737
    0x10, &found_certs);
844
737
  LOG_TEST_GOTO_ERR(p15card->card->ctx, r,
845
364
    "Could not add CNS01");
846
364
  certificate_count += found_certs;
847
848
  /* Digital signature */
849
364
  r = itacns_check_and_add_keyset(p15card, "CNS1", 0x10,
850
364
    0, "3F0014009010", "3F00140081108010", "3F0014008110",
851
364
    0x1a, &found_certs);
852
364
  LOG_TEST_GOTO_ERR(p15card->card->ctx, r,
853
139
    "Could not add CNS1");
854
139
  certificate_count += found_certs;
855
856
  /* Idemia card */
857
139
  r = itacns_check_and_add_keyset(p15card, "CNS1", 0x02,
858
139
    0, "3F00140090012002", "3F0011001102", "3F0014009002",
859
139
    0x10, &found_certs);
860
139
  LOG_TEST_RET(p15card->card->ctx, r,
861
64
    "Could not add CNS1");
862
64
  certificate_count += found_certs;
863
864
  /* Did we find anything? */
865
64
  if (certificate_count == 0)
866
64
    sc_debug(p15card->card->ctx, SC_LOG_DEBUG_NORMAL,
867
64
      "Warning: no certificates found!");
868
869
  /* Back to Master File */
870
64
  sc_format_path("3F00", &path);
871
64
  r = sc_select_file(p15card->card, &path, NULL);
872
64
  LOG_TEST_GOTO_ERR(p15card->card->ctx, r,
873
45
    "Could not select master file again");
874
875
45
  LOG_FUNC_RETURN(p15card->card->ctx, r);
876
877
1.03k
err:
878
1.03k
  sc_pkcs15_card_clear(p15card);
879
1.03k
  LOG_FUNC_RETURN(p15card->card->ctx, r);
880
1.03k
}
881
882
int sc_pkcs15emu_itacns_init_ex(sc_pkcs15_card_t *p15card, struct sc_aid *aid)
883
77.7k
{
884
77.7k
  sc_card_t *card = p15card->card;
885
77.7k
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_NORMAL);
886
887
  /* Check card */
888
77.7k
  if (! (
889
77.7k
      (card->type > SC_CARD_TYPE_ITACNS_BASE &&
890
25.8k
      card->type < SC_CARD_TYPE_ITACNS_BASE + 1000)
891
75.7k
    || card->type == SC_CARD_TYPE_CARDOS_CIE_V1)
892
77.7k
    )
893
75.7k
    return SC_ERROR_WRONG_CARD;
894
895
  /* Init card */
896
1.97k
  return itacns_init(p15card);
897
77.7k
}