Coverage Report

Created: 2026-06-06 06:51

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
257
{
170
257
  int sc_res;
171
257
  SC_FUNC_CALLED(p15card->card->ctx, SC_LOG_DEBUG_NORMAL);
172
173
257
  sc_res = sc_select_file(p15card->card, path, NULL);
174
257
  if(sc_res != SC_SUCCESS)
175
63
    return sc_res;
176
177
194
  sc_res = sc_read_binary(p15card->card, 0, buf, buflen, 0);
178
194
  return sc_res;
179
257
}
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
172
{
190
172
  int r;
191
  /* const char *label = "Certificate"; */
192
172
  sc_pkcs15_cert_info_t info;
193
172
  sc_pkcs15_object_t    obj;
194
172
#ifdef ENABLE_OPENSSL
195
172
  X509 *x509;
196
172
  sc_pkcs15_cert_t *cert;
197
172
  int private_obj;
198
172
#endif
199
200
172
  SC_FUNC_CALLED(p15card->card->ctx, SC_LOG_DEBUG_NORMAL);
201
202
172
  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
172
  *ext_info_ok = 0;
209
210
211
172
  memset(&info, 0, sizeof(info));
212
172
  memset(&obj,  0, sizeof(obj));
213
214
172
  info.id                = *id;
215
172
  info.authority         = authority;
216
172
  if (path)
217
172
    info.path = *path;
218
219
172
  strlcpy(obj.label, label, sizeof(obj.label));
220
221
  /* If we have OpenSSL, read keyUsage */
222
172
#ifdef ENABLE_OPENSSL
223
172
  private_obj = obj_flags & SC_PKCS15_CO_FLAG_PRIVATE;
224
172
  r = sc_pkcs15_read_certificate(p15card, &info, private_obj, &cert);
225
172
  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(path)
354
0
        pin_info.path = *path;
355
356
0
  memset(&pin_obj, 0, sizeof(pin_obj));
357
0
  strlcpy(pin_obj.label, label, sizeof(pin_obj.label));
358
0
  pin_obj.flags = SC_PKCS15_CO_FLAG_PRIVATE |
359
0
    (auth_id ? SC_PKCS15_CO_FLAG_MODIFIABLE : 0);
360
0
  if (auth_id) {
361
0
    pin_obj.auth_id.len = 1;
362
0
    pin_obj.auth_id.value[0] = auth_id;
363
0
  } else
364
0
    pin_obj.auth_id.len = 0;
365
366
0
  return sc_pkcs15emu_add_pin_obj(p15card, &pin_obj, &pin_info);
367
0
}
368
369
static int hextoint(char *src, unsigned int len)
370
113
{
371
113
  char hex[16];
372
113
  char *end;
373
113
  int res;
374
375
113
  if(len >= sizeof(hex))
376
0
    return -1;
377
113
  strncpy(hex, src, len);
378
113
  hex[len] = '\0';
379
113
  res = (int)strtol(hex, &end, 0x10);
380
113
  if(end != (char*)&hex[len])
381
76
    return -1;
382
37
  return res;
383
113
}
384
385
static int get_name_from_EF_DatiPersonali(unsigned char *EFdata,
386
  size_t EFdata_len, char name[], int name_len)
387
99
{
388
99
  const unsigned int EF_personaldata_maxlen = 400;
389
99
  const unsigned int tlv_length_size = 6;
390
99
  char *file = NULL;
391
99
  int file_size;
392
393
  /*
394
   * Bytes 0-5 contain the ASCII encoding of the following TLV
395
   * structure's total size, in base 16.
396
   */
397
99
  if (EFdata_len < tlv_length_size) {
398
    /* We need at least 6 bytes for file length here */
399
7
    return -1;
400
7
  }
401
92
  file_size = hextoint((char*)EFdata, tlv_length_size);
402
92
  if (EFdata_len < (file_size + tlv_length_size)) {
403
    /* Inconsistent external file length and internal file length
404
     * suggests we are trying to process junk data.
405
     * If the internal data length is shorter, the data can be padded,
406
     * but we should be fine as we will not go behind the buffer limits */
407
9
    return -1;
408
9
  }
409
83
  file = (char*)&EFdata[tlv_length_size];
410
411
83
  enum {
412
83
    f_issuer_code = 0,
413
83
    f_issuing_date,
414
83
    f_expiry_date,
415
83
    f_last_name,
416
83
    f_first_name,
417
83
    f_birth_date,
418
83
    f_sex,
419
83
    f_height,
420
83
    f_codice_fiscale,
421
83
    f_citizenship_code,
422
83
    f_birth_township_code,
423
83
    f_birth_country,
424
83
    f_birth_certificate,
425
83
    f_residence_township_code,
426
83
    f_residence_address,
427
83
    f_expat_notes
428
83
  };
429
430
  /* Read the fields up to f_first_name */
431
83
  struct {
432
83
    int len;
433
83
    char value[256];
434
83
  } fields[f_first_name+1];
435
83
  int i=0; /* offset inside the file */
436
83
  int f; /* field number */
437
438
83
  if (file_size < 0)
439
75
    return -1;
440
441
  /*
442
   * This shouldn't happen, but let us be protected against wrong
443
   * or malicious cards
444
   */
445
8
  if(file_size > (int)EF_personaldata_maxlen - (int)tlv_length_size)
446
1
    file_size = EF_personaldata_maxlen - tlv_length_size;
447
448
449
8
  memset(fields, 0, sizeof(fields));
450
451
24
  for(f=0; f<f_first_name+1; f++) {
452
22
    int field_size;
453
454
    /* Don't read beyond the allocated buffer */
455
22
    if(i+2 > file_size)
456
1
      return -1;
457
21
    field_size = hextoint((char*) &file[i], 2);
458
21
    i += 2;
459
460
21
    if (field_size < 0
461
20
        || i + field_size > file_size
462
16
        || field_size >= (int)sizeof(fields[f].value))
463
5
      return -1;
464
465
16
    fields[f].len = field_size;
466
16
    strncpy(fields[f].value, &file[i], field_size);
467
16
    fields[f].value[field_size] = '\0';
468
16
    i += field_size;
469
16
  }
470
471
2
  if (fields[f_first_name].len + fields[f_last_name].len + 1 >= name_len)
472
1
    return -1;
473
474
  /* the lengths are already checked that they will fit in buffer */
475
1
  snprintf(name, name_len, "%.*s %.*s",
476
1
    fields[f_first_name].len, fields[f_first_name].value,
477
1
    fields[f_last_name].len, fields[f_last_name].value);
478
1
  return 0;
479
2
}
480
481
static int itacns_add_data_files(sc_pkcs15_card_t *p15card)
482
125
{
483
125
  const size_t array_size =
484
125
    sizeof(itacns_data_files)/sizeof(itacns_data_files[0]);
485
125
  unsigned int i;
486
125
  int rv;
487
125
  sc_pkcs15_data_t *p15_personaldata = NULL;
488
125
  sc_pkcs15_data_info_t dinfo;
489
125
  struct sc_pkcs15_object *objs[32];
490
125
  struct sc_pkcs15_data_info *cinfo;
491
125
  int private_obj;
492
493
1.87k
  for(i=0; i < array_size; i++) {
494
1.75k
    sc_path_t path;
495
1.75k
    sc_pkcs15_data_info_t data;
496
1.75k
    sc_pkcs15_object_t    obj;
497
498
1.75k
    if (itacns_data_files[i].cie_only &&
499
500
      p15card->card->type != SC_CARD_TYPE_ITACNS_CIE_V2)
500
268
      continue;
501
502
1.48k
    sc_format_path(itacns_data_files[i].path, &path);
503
504
1.48k
    memset(&data, 0, sizeof(data));
505
1.48k
    memset(&obj, 0, sizeof(obj));
506
1.48k
    strlcpy(data.app_label, itacns_data_files[i].label,
507
1.48k
      sizeof(data.app_label));
508
1.48k
    strlcpy(obj.label, itacns_data_files[i].label,
509
1.48k
      sizeof(obj.label));
510
1.48k
    data.path = path;
511
1.48k
    rv = sc_pkcs15emu_add_data_object(p15card, &obj, &data);
512
1.48k
    LOG_TEST_RET(p15card->card->ctx, rv,
513
1.48k
      "Could not add data file");
514
1.48k
  }
515
516
  /*
517
   * If we got this far, we can read the Personal Data file and glean
518
   * the user's full name. Thus we can use it to put together a
519
   * user-friendlier card name.
520
   */
521
125
  memset(&dinfo, 0, sizeof(dinfo));
522
125
  strlcpy(dinfo.app_label, "EF_DatiPersonali", sizeof(dinfo.app_label));
523
524
  /* Find EF_DatiPersonali */
525
526
125
  rv = sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_DATA_OBJECT,
527
125
    objs, 32);
528
125
  if(rv < 0) {
529
0
    sc_log(p15card->card->ctx,
530
0
      "Data enumeration failed");
531
0
    return SC_SUCCESS;
532
0
  }
533
534
433
  for(i=0; i<32; i++) {
535
433
    cinfo = (struct sc_pkcs15_data_info *) objs[i]->data;
536
433
    if(!strcmp("EF_DatiPersonali", objs[i]->label))
537
125
      break;
538
433
  }
539
540
125
  if(i>=32) {
541
0
    sc_log(p15card->card->ctx,
542
0
      "Could not find EF_DatiPersonali: "
543
0
      "keeping generic card name");
544
0
    return SC_SUCCESS;
545
0
  }
546
547
125
  private_obj = objs[i]->flags & SC_PKCS15_CO_FLAG_PRIVATE;
548
125
  rv = sc_pkcs15_read_data_object(p15card, cinfo, private_obj, &p15_personaldata);
549
125
  if (rv) {
550
25
    sc_log(p15card->card->ctx,
551
25
      "Could not read EF_DatiPersonali: "
552
25
      "keeping generic card name");
553
25
    return SC_SUCCESS;
554
25
  }
555
556
100
  if (p15_personaldata->data) {
557
99
    char fullname[160];
558
99
    if (get_name_from_EF_DatiPersonali(p15_personaldata->data,
559
99
      p15_personaldata->data_len, fullname, sizeof(fullname))) {
560
98
      sc_log(p15card->card->ctx,
561
98
        "Could not parse EF_DatiPersonali: "
562
98
        "keeping generic card name");
563
98
      sc_pkcs15_free_data_object(p15_personaldata);
564
98
      free(cinfo->data.value);
565
98
      cinfo->data.value = NULL;
566
98
      return SC_SUCCESS;
567
98
    }
568
1
    set_string(&p15card->tokeninfo->label, fullname);
569
1
  }
570
2
  free(cinfo->data.value);
571
2
  cinfo->data.value = NULL;
572
2
  sc_pkcs15_free_data_object(p15_personaldata);
573
2
  return SC_SUCCESS;
574
100
}
575
576
static int itacns_add_keyset(sc_pkcs15_card_t *p15card,
577
  const char *label, int sec_env, sc_pkcs15_id_t *cert_id,
578
  const char *pubkey_path, const char *prkey_path,
579
  unsigned int pubkey_usage_flags, unsigned int prkey_usage_flags,
580
  u8 pin_ref, size_t modulus_len)
581
0
{
582
0
  int r;
583
0
  sc_path_t path;
584
0
  sc_path_t *private_path = NULL;
585
0
  char pinlabel[16];
586
0
  int fake_puk_authid, pin_flags;
587
588
589
  /* Public key; not really needed */
590
  /* FIXME: set usage according to the certificate. */
591
0
  if (pubkey_path) {
592
0
    sc_format_path(pubkey_path, &path);
593
0
    r = itacns_add_pubkey(p15card, &path, cert_id, label,
594
0
      pubkey_usage_flags, sec_env, 0, modulus_len);
595
0
    LOG_TEST_RET(p15card->card->ctx, r,
596
0
      "Could not add public key");
597
0
  }
598
599
  /*
600
   * FIXME: usage should be inferred from the X.509 certificate, and not
601
   * from whether the key needs Secure Messaging.
602
   */
603
0
  if (prkey_path) {
604
0
    sc_format_path(prkey_path, &path);
605
0
    private_path = &path;
606
0
  }
607
0
  r = itacns_add_prkey(p15card, cert_id, label, SC_PKCS15_TYPE_PRKEY_RSA,
608
0
    modulus_len,
609
0
    prkey_usage_flags,
610
0
    private_path, sec_env, cert_id, SC_PKCS15_CO_FLAG_PRIVATE);
611
0
  LOG_TEST_RET(p15card->card->ctx, r,
612
0
    "Could not add private key");
613
614
  /* PIN and PUK */
615
0
  strlcpy(pinlabel, "PIN ", sizeof(pinlabel));
616
0
  strlcat(pinlabel, label, sizeof(pinlabel));
617
618
  /* We are making up ID 0x90+ to link the PIN and the PUK. */
619
0
  fake_puk_authid = 0x90 + pin_ref;
620
0
  pin_flags = SC_PKCS15_PIN_FLAG_CASE_SENSITIVE
621
0
    | SC_PKCS15_PIN_FLAG_INITIALIZED;
622
0
  r = itacns_add_pin(p15card, pinlabel, sec_env, fake_puk_authid, pin_ref,
623
0
      private_path, pin_flags);
624
0
  LOG_TEST_RET(p15card->card->ctx, r,
625
0
    "Could not add PIN");
626
627
0
  strlcpy(pinlabel, "PUK ", sizeof(pinlabel));
628
0
  strlcat(pinlabel, label, sizeof(pinlabel));
629
  /*
630
   * Looking at pkcs15-tcos.c and pkcs15-framework.c, it seems that the
631
   * right thing to do here is to define a PUK as a SO PIN. Can anybody
632
   * comment on this?
633
   */
634
0
  pin_flags |= SC_PKCS15_PIN_FLAG_UNBLOCKING_PIN
635
0
  | SC_PKCS15_PIN_FLAG_UNBLOCK_DISABLED;
636
0
  r = itacns_add_pin(p15card, pinlabel, fake_puk_authid, 0, pin_ref+1,
637
0
      private_path, pin_flags);
638
0
  LOG_TEST_RET(p15card->card->ctx, r,
639
0
    "Could not add PUK");
640
641
0
  return 0;
642
0
}
643
644
/*
645
 * itacns_check_and_add_keyset() checks for the existence and correctness
646
 * of an X.509 certificate. If it is all right, it adds the related keys;
647
 * otherwise it aborts.
648
 */
649
650
static int itacns_check_and_add_keyset(sc_pkcs15_card_t *p15card,
651
  const char *label, int sec_env, size_t cert_offset,
652
  const char *cert_path, const char *pubkey_path, const char *prkey_path,
653
  u8 pin_ref, int *found_certificates)
654
271
{
655
271
  int r;
656
271
  sc_path_t path;
657
271
  sc_pkcs15_id_t cert_id;
658
271
  int ext_info_ok;
659
271
  int ku = 0, xku = 0;
660
271
  size_t modulus_len = 0;
661
271
  int pubkey_usage_flags = 0, prkey_usage_flags = 0;
662
663
271
  cert_id.len = 1;
664
271
  cert_id.value[0] = sec_env;
665
271
  *found_certificates = 0;
666
667
  /* Certificate */
668
271
  if (!cert_path) {
669
0
    sc_log(p15card->card->ctx,
670
0
      "We cannot use keys without a matching certificate");
671
0
    return SC_ERROR_NOT_SUPPORTED;
672
0
  }
673
674
271
  sc_format_path(cert_path, &path);
675
271
  r = sc_select_file(p15card->card, &path, NULL);
676
271
  if (r == SC_ERROR_FILE_NOT_FOUND)
677
12
    return 0;
678
259
  if (r != SC_SUCCESS) {
679
78
    sc_log(p15card->card->ctx,
680
78
      "Could not find certificate for %s", label);
681
78
    return r;
682
78
  }
683
684
  /*
685
   * Infocamere 1204 (and others?) store a more complex structure. We
686
   * are going to read the first bytes to guess its length, and invoke
687
   * itacns_add_cert so that it only reads the certificate.
688
   */
689
181
  if (cert_offset) {
690
69
    u8 certlen[3];
691
69
    memset(certlen, 0, sizeof certlen);
692
69
    r = loadFile(p15card, &path, certlen, sizeof(certlen));
693
69
    LOG_TEST_RET(p15card->card->ctx, r,
694
65
      "Could not read certificate file");
695
65
    if (r < 3)
696
4
      return SC_ERROR_INVALID_DATA;
697
61
    path.index = (int)cert_offset;
698
61
    path.count = (certlen[1] << 8) + certlen[2];
699
    /* If those bytes are 00, then we are probably dealing with an
700
     * empty file. */
701
61
    if (path.count == 0)
702
1
      return 0;
703
61
  }
704
705
172
  r = itacns_add_cert(p15card, SC_PKCS15_TYPE_CERT_X509, 0,
706
172
    &path, &cert_id, label, 0, &ext_info_ok, &ku, &xku, &modulus_len);
707
172
  if (r == SC_ERROR_INVALID_ASN1_OBJECT)
708
143
    return 0;
709
29
  LOG_TEST_RET(p15card->card->ctx, r,
710
0
    "Could not add certificate");
711
0
  (*found_certificates)++;
712
713
  /* Set usage flags */
714
0
  if(ext_info_ok) {
715
0
#ifdef ENABLE_OPENSSL
716
0
    if (ku & KU_DIGITAL_SIGNATURE) {
717
0
      pubkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_VERIFY;
718
0
      prkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_SIGN;
719
0
    }
720
0
    if (ku & KU_NON_REPUDIATION) {
721
0
      pubkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_VERIFY;
722
0
      prkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_NONREPUDIATION;
723
0
    }
724
0
    if (ku & KU_KEY_ENCIPHERMENT || ku & KU_KEY_AGREEMENT
725
0
      || xku & XKU_SSL_CLIENT) {
726
0
      pubkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_WRAP;
727
0
      prkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_UNWRAP;
728
0
    }
729
0
    if (ku & KU_DATA_ENCIPHERMENT || xku & XKU_SMIME) {
730
0
      pubkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_ENCRYPT;
731
0
      prkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_DECRYPT;
732
0
    }
733
#else /* ENABLE_OPENSSL */
734
    sc_log(p15card->card->ctx,
735
      "Extended certificate info retrieved without OpenSSL. "
736
      "How is this possible?");
737
    return SC_ERROR_INTERNAL;
738
#endif /* ENABLE_OPENSSL */
739
0
  } else {
740
    /* Certificate info not retrieved; fall back onto defaults */
741
0
    pubkey_usage_flags =
742
0
        SC_PKCS15_PRKEY_USAGE_VERIFY
743
0
      | SC_PKCS15_PRKEY_USAGE_WRAP;
744
0
    prkey_usage_flags =
745
0
        SC_PKCS15_PRKEY_USAGE_SIGN
746
0
      | SC_PKCS15_PRKEY_USAGE_UNWRAP;
747
0
  }
748
749
0
  r = itacns_add_keyset(p15card, label, sec_env, &cert_id,
750
0
    pubkey_path, prkey_path, pubkey_usage_flags, prkey_usage_flags,
751
0
    pin_ref, modulus_len);
752
0
  LOG_TEST_RET(p15card->card->ctx, r,
753
0
    "Could not add keys for this certificate");
754
755
0
  return r;
756
0
}
757
758
/* Initialization. */
759
760
static int itacns_init(sc_pkcs15_card_t *p15card)
761
188
{
762
188
  int r;
763
188
  sc_path_t path;
764
188
  int certificate_count = 0;
765
188
  int found_certs;
766
188
  int card_is_cie_v1, cns0_secenv;
767
768
188
  SC_FUNC_CALLED(p15card->card->ctx, SC_LOG_DEBUG_NORMAL);
769
770
188
  set_string(&p15card->tokeninfo->label, p15card->card->name);
771
188
  if(p15card->card->drv_data) {
772
188
    unsigned int mask_code, ic_code;
773
188
    char buffer[256];
774
188
    itacns_drv_data_t *data =
775
188
      (itacns_drv_data_t*) p15card->card->drv_data;
776
188
    mask_code = data->mask_manufacturer_code;
777
188
    if (mask_code >= sizeof(itacns_mask_manufacturers)
778
188
      /sizeof(itacns_mask_manufacturers[0]))
779
154
      mask_code = 0;
780
188
    ic_code = data->ic_manufacturer_code;
781
188
    if (ic_code >= sizeof(iso7816_ic_manufacturers)
782
188
      /sizeof(iso7816_ic_manufacturers[0]))
783
164
      ic_code = 0;
784
188
    snprintf(buffer, sizeof(buffer), "IC: %s; mask: %s",
785
188
      iso7816_ic_manufacturers[ic_code],
786
188
      itacns_mask_manufacturers[mask_code]);
787
188
    set_string(&p15card->tokeninfo->manufacturer_id, buffer);
788
188
  }
789
790
  /* Read and set serial */
791
188
  {
792
188
    u8 serial[17];
793
188
    int bytes;
794
188
    sc_format_path(path_serial, &path);
795
188
    bytes = loadFile(p15card, &path, serial, 16);
796
188
    if (bytes < 0) return bytes;
797
125
    if (bytes > 16) return -1;
798
125
    serial[bytes] = '\0';
799
125
    set_string(&p15card->tokeninfo->serial_number, (char*)serial);
800
125
  }
801
802
  /* Is the card a CIE v1? */
803
0
  card_is_cie_v1 =
804
125
       (p15card->card->type == SC_CARD_TYPE_ITACNS_CIE_V1)
805
124
    || (p15card->card->type == SC_CARD_TYPE_CARDOS_CIE_V1);
806
125
  cns0_secenv = (card_is_cie_v1 ? 0x31 : 0x01);
807
808
  /* If it's a Siemens CIE v1 card, set algo flags accordingly. */
809
125
  if (card_is_cie_v1) {
810
1
    int i;
811
2
    for (i = 0; i < p15card->card->algorithm_count; i++) {
812
1
      sc_algorithm_info_t *info =
813
1
        &p15card->card->algorithms[i];
814
815
1
      if (info->algorithm != SC_ALGORITHM_RSA)
816
0
        continue;
817
1
      info->flags &= ~(SC_ALGORITHM_RSA_RAW
818
1
        | SC_ALGORITHM_RSA_HASH_NONE);
819
1
      info->flags |= (SC_ALGORITHM_RSA_PAD_PKCS1
820
1
        | SC_ALGORITHM_RSA_HASHES);
821
1
    }
822
1
  }
823
824
  /* Data files */
825
125
  r = itacns_add_data_files(p15card);
826
125
  LOG_TEST_GOTO_ERR(p15card->card->ctx, r,
827
125
    "Could not add data files");
828
829
  /*** Certificate and keys. ***/
830
  /* Standard CNS */
831
125
  r = itacns_check_and_add_keyset(p15card, "CNS0", cns0_secenv,
832
125
    0, "3F0011001101", "3F003F01", NULL,
833
125
    0x10, &found_certs);
834
125
  LOG_TEST_GOTO_ERR(p15card->card->ctx, r,
835
81
    "Could not add CNS0");
836
81
  certificate_count += found_certs;
837
838
  /* Infocamere 1204 */
839
81
  r = itacns_check_and_add_keyset(p15card, "CNS01", 0x21,
840
81
    5, "3F002FFF8228", NULL, "3F002FFF0000",
841
81
    0x10, &found_certs);
842
81
  LOG_TEST_GOTO_ERR(p15card->card->ctx, r,
843
49
    "Could not add CNS01");
844
49
  certificate_count += found_certs;
845
846
  /* Digital signature */
847
49
  r = itacns_check_and_add_keyset(p15card, "CNS1", 0x10,
848
49
    0, "3F0014009010", "3F00140081108010", "3F0014008110",
849
49
    0x1a, &found_certs);
850
49
  LOG_TEST_GOTO_ERR(p15card->card->ctx, r,
851
16
    "Could not add CNS1");
852
16
  certificate_count += found_certs;
853
854
  /* Idemia card */
855
16
  r = itacns_check_and_add_keyset(p15card, "CNS1", 0x02,
856
16
    0, "3F00140090012002", "3F0011001102", "3F0014009002",
857
16
    0x10, &found_certs);
858
16
  LOG_TEST_RET(p15card->card->ctx, r,
859
10
    "Could not add CNS1");
860
10
  certificate_count += found_certs;
861
862
  /* Did we find anything? */
863
10
  if (certificate_count == 0)
864
10
    sc_debug(p15card->card->ctx, SC_LOG_DEBUG_NORMAL,
865
10
      "Warning: no certificates found!");
866
867
  /* Back to Master File */
868
10
  sc_format_path("3F00", &path);
869
10
  r = sc_select_file(p15card->card, &path, NULL);
870
10
  LOG_TEST_GOTO_ERR(p15card->card->ctx, r,
871
4
    "Could not select master file again");
872
873
4
  LOG_FUNC_RETURN(p15card->card->ctx, r);
874
875
115
err:
876
115
  sc_pkcs15_card_clear(p15card);
877
115
  LOG_FUNC_RETURN(p15card->card->ctx, r);
878
115
}
879
880
int sc_pkcs15emu_itacns_init_ex(sc_pkcs15_card_t *p15card, struct sc_aid *aid)
881
10.5k
{
882
10.5k
  sc_card_t *card = p15card->card;
883
10.5k
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_NORMAL);
884
885
  /* Check card */
886
10.5k
  if (! (
887
10.5k
      (card->type > SC_CARD_TYPE_ITACNS_BASE &&
888
3.57k
      card->type < SC_CARD_TYPE_ITACNS_BASE + 1000)
889
10.3k
    || card->type == SC_CARD_TYPE_CARDOS_CIE_V1)
890
10.5k
    )
891
10.3k
    return SC_ERROR_WRONG_CARD;
892
893
  /* Init card */
894
188
  return itacns_init(p15card);
895
10.5k
}