Coverage Report

Created: 2026-11-30 06:17

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
369
{
170
369
  int sc_res;
171
369
  SC_FUNC_CALLED(p15card->card->ctx, SC_LOG_DEBUG_NORMAL);
172
173
369
  sc_res = sc_select_file(p15card->card, path, NULL);
174
369
  if(sc_res != SC_SUCCESS)
175
96
    return sc_res;
176
177
273
  sc_res = sc_read_binary(p15card->card, 0, buf, buflen, 0);
178
273
  return sc_res;
179
369
}
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
211
{
190
211
  int r;
191
  /* const char *label = "Certificate"; */
192
211
  sc_pkcs15_cert_info_t info;
193
211
  sc_pkcs15_object_t    obj;
194
211
#ifdef ENABLE_OPENSSL
195
211
  X509 *x509;
196
211
  sc_pkcs15_cert_t *cert;
197
211
  int private_obj;
198
211
#endif
199
200
211
  SC_FUNC_CALLED(p15card->card->ctx, SC_LOG_DEBUG_NORMAL);
201
202
211
  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
211
  *ext_info_ok = 0;
209
210
211
211
  memset(&info, 0, sizeof(info));
212
211
  memset(&obj,  0, sizeof(obj));
213
214
211
  info.id                = *id;
215
211
  info.authority         = authority;
216
211
  if (path)
217
211
    info.path = *path;
218
219
211
  strlcpy(obj.label, label, sizeof(obj.label));
220
221
  /* If we have OpenSSL, read keyUsage */
222
211
#ifdef ENABLE_OPENSSL
223
211
  private_obj = obj_flags & SC_PKCS15_CO_FLAG_PRIVATE;
224
211
  r = sc_pkcs15_read_certificate(p15card, &info, private_obj, &cert);
225
211
  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
28
{
373
28
  char hex[16];
374
28
  char *end;
375
28
  int res;
376
377
28
  if(len >= sizeof(hex))
378
0
    return -1;
379
28
  strncpy(hex, src, len);
380
28
  hex[len] = '\0';
381
28
  res = (int)strtol(hex, &end, 0x10);
382
28
  if(end != (char*)&hex[len])
383
3
    return -1;
384
25
  return res;
385
28
}
386
387
static int get_name_from_EF_DatiPersonali(unsigned char *EFdata,
388
  size_t EFdata_len, char name[], int name_len)
389
13
{
390
13
  const unsigned int EF_personaldata_maxlen = 400;
391
13
  const unsigned int tlv_length_size = 6;
392
13
  char *file = NULL;
393
13
  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
13
  if (EFdata_len < tlv_length_size) {
400
    /* We need at least 6 bytes for file length here */
401
4
    return -1;
402
4
  }
403
9
  file_size = hextoint((char*)EFdata, tlv_length_size);
404
9
  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
1
    return -1;
410
1
  }
411
8
  file = (char*)&EFdata[tlv_length_size];
412
413
8
  enum {
414
8
    f_issuer_code = 0,
415
8
    f_issuing_date,
416
8
    f_expiry_date,
417
8
    f_last_name,
418
8
    f_first_name,
419
8
    f_birth_date,
420
8
    f_sex,
421
8
    f_height,
422
8
    f_codice_fiscale,
423
8
    f_citizenship_code,
424
8
    f_birth_township_code,
425
8
    f_birth_country,
426
8
    f_birth_certificate,
427
8
    f_residence_township_code,
428
8
    f_residence_address,
429
8
    f_expat_notes
430
8
  };
431
432
  /* Read the fields up to f_first_name */
433
8
  struct {
434
8
    int len;
435
8
    char value[256];
436
8
  } fields[f_first_name+1];
437
8
  int i=0; /* offset inside the file */
438
8
  int f; /* field number */
439
440
8
  if (file_size < 0)
441
0
    return -1;
442
443
  /*
444
   * This shouldn't happen, but let us be protected against wrong
445
   * or malicious cards
446
   */
447
8
  if(file_size > (int)EF_personaldata_maxlen - (int)tlv_length_size)
448
0
    file_size = EF_personaldata_maxlen - tlv_length_size;
449
450
451
8
  memset(fields, 0, sizeof(fields));
452
453
21
  for(f=0; f<f_first_name+1; f++) {
454
20
    int field_size;
455
456
    /* Don't read beyond the allocated buffer */
457
20
    if(i+2 > file_size)
458
1
      return -1;
459
19
    field_size = hextoint((char*) &file[i], 2);
460
19
    i += 2;
461
462
19
    if (field_size < 0
463
16
        || i + field_size > file_size
464
13
        || field_size >= (int)sizeof(fields[f].value))
465
6
      return -1;
466
467
13
    fields[f].len = field_size;
468
13
    strncpy(fields[f].value, &file[i], field_size);
469
13
    fields[f].value[field_size] = '\0';
470
13
    i += field_size;
471
13
  }
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
178
{
485
178
  const size_t array_size =
486
178
    sizeof(itacns_data_files)/sizeof(itacns_data_files[0]);
487
178
  unsigned int i;
488
178
  int rv;
489
178
  sc_pkcs15_data_t *p15_personaldata = NULL;
490
178
  sc_pkcs15_data_info_t dinfo;
491
178
  struct sc_pkcs15_object *objs[32];
492
178
  struct sc_pkcs15_data_info *cinfo;
493
178
  int private_obj;
494
495
2.67k
  for(i=0; i < array_size; i++) {
496
2.49k
    sc_path_t path;
497
2.49k
    sc_pkcs15_data_info_t data;
498
2.49k
    sc_pkcs15_object_t    obj;
499
500
2.49k
    if (itacns_data_files[i].cie_only &&
501
712
      p15card->card->type != SC_CARD_TYPE_ITACNS_CIE_V2)
502
688
      continue;
503
504
1.80k
    sc_format_path(itacns_data_files[i].path, &path);
505
506
1.80k
    memset(&data, 0, sizeof(data));
507
1.80k
    memset(&obj, 0, sizeof(obj));
508
1.80k
    strlcpy(data.app_label, itacns_data_files[i].label,
509
1.80k
      sizeof(data.app_label));
510
1.80k
    strlcpy(obj.label, itacns_data_files[i].label,
511
1.80k
      sizeof(obj.label));
512
1.80k
    data.path = path;
513
1.80k
    rv = sc_pkcs15emu_add_data_object(p15card, &obj, &data);
514
1.80k
    LOG_TEST_RET(p15card->card->ctx, rv,
515
1.80k
      "Could not add data file");
516
1.80k
  }
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
178
  memset(&dinfo, 0, sizeof(dinfo));
524
178
  strlcpy(dinfo.app_label, "EF_DatiPersonali", sizeof(dinfo.app_label));
525
526
  /* Find EF_DatiPersonali */
527
528
178
  rv = sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_DATA_OBJECT,
529
178
    objs, 32);
530
178
  if(rv < 0) {
531
0
    sc_log(p15card->card->ctx,
532
0
      "Data enumeration failed");
533
0
    return SC_SUCCESS;
534
0
  }
535
536
540
  for(i=0; i<32; i++) {
537
540
    cinfo = (struct sc_pkcs15_data_info *) objs[i]->data;
538
540
    if(!strcmp("EF_DatiPersonali", objs[i]->label))
539
178
      break;
540
540
  }
541
542
178
  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
178
  private_obj = objs[i]->flags & SC_PKCS15_CO_FLAG_PRIVATE;
550
178
  rv = sc_pkcs15_read_data_object(p15card, cinfo, private_obj, &p15_personaldata);
551
178
  if (rv) {
552
38
    sc_log(p15card->card->ctx,
553
38
      "Could not read EF_DatiPersonali: "
554
38
      "keeping generic card name");
555
38
    return SC_SUCCESS;
556
38
  }
557
558
140
  if (p15_personaldata->data) {
559
13
    char fullname[160];
560
13
    if (get_name_from_EF_DatiPersonali(p15_personaldata->data,
561
13
      p15_personaldata->data_len, fullname, sizeof(fullname))) {
562
12
      sc_log(p15card->card->ctx,
563
12
        "Could not parse EF_DatiPersonali: "
564
12
        "keeping generic card name");
565
12
      sc_pkcs15_free_data_object(p15_personaldata);
566
12
      free(cinfo->data.value);
567
12
      cinfo->data.value = NULL;
568
12
      return SC_SUCCESS;
569
12
    }
570
1
    set_string(&p15card->tokeninfo->label, fullname);
571
1
  }
572
128
  free(cinfo->data.value);
573
128
  cinfo->data.value = NULL;
574
128
  sc_pkcs15_free_data_object(p15_personaldata);
575
128
  return SC_SUCCESS;
576
140
}
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
366
{
657
366
  int r;
658
366
  sc_path_t path;
659
366
  sc_pkcs15_id_t cert_id;
660
366
  int ext_info_ok;
661
366
  int ku = 0, xku = 0;
662
366
  size_t modulus_len = 0;
663
366
  int pubkey_usage_flags = 0, prkey_usage_flags = 0;
664
665
366
  cert_id.len = 1;
666
366
  cert_id.value[0] = sec_env;
667
366
  *found_certificates = 0;
668
669
  /* Certificate */
670
366
  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
366
  sc_format_path(cert_path, &path);
677
366
  r = sc_select_file(p15card->card, &path, NULL);
678
366
  if (r == SC_ERROR_FILE_NOT_FOUND)
679
20
    return 0;
680
346
  if (r != SC_SUCCESS) {
681
122
    sc_log(p15card->card->ctx,
682
122
      "Could not find certificate for %s", label);
683
122
    return r;
684
122
  }
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
224
  if (cert_offset) {
692
94
    u8 certlen[3];
693
94
    memset(certlen, 0, sizeof certlen);
694
94
    r = loadFile(p15card, &path, certlen, sizeof(certlen));
695
94
    LOG_TEST_RET(p15card->card->ctx, r,
696
89
      "Could not read certificate file");
697
89
    if (r < 3)
698
5
      return SC_ERROR_INVALID_DATA;
699
84
    path.index = (int)cert_offset;
700
84
    path.count = (certlen[1] << 8) + certlen[2];
701
    /* If those bytes are 00, then we are probably dealing with an
702
     * empty file. */
703
84
    if (path.count == 0)
704
3
      return 0;
705
84
  }
706
707
211
  r = itacns_add_cert(p15card, SC_PKCS15_TYPE_CERT_X509, 0,
708
211
    &path, &cert_id, label, 0, &ext_info_ok, &ku, &xku, &modulus_len);
709
211
  if (r == SC_ERROR_INVALID_ASN1_OBJECT)
710
169
    return 0;
711
42
  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
275
{
764
275
  int r;
765
275
  sc_path_t path;
766
275
  int certificate_count = 0;
767
275
  int found_certs;
768
275
  int card_is_cie_v1, cns0_secenv;
769
770
275
  SC_FUNC_CALLED(p15card->card->ctx, SC_LOG_DEBUG_NORMAL);
771
772
275
  set_string(&p15card->tokeninfo->label, p15card->card->name);
773
275
  if(p15card->card->drv_data) {
774
275
    unsigned int mask_code, ic_code;
775
275
    char buffer[256];
776
275
    itacns_drv_data_t *data =
777
275
      (itacns_drv_data_t*) p15card->card->drv_data;
778
275
    mask_code = data->mask_manufacturer_code;
779
275
    if (mask_code >= sizeof(itacns_mask_manufacturers)
780
275
      /sizeof(itacns_mask_manufacturers[0]))
781
241
      mask_code = 0;
782
275
    ic_code = data->ic_manufacturer_code;
783
275
    if (ic_code >= sizeof(iso7816_ic_manufacturers)
784
275
      /sizeof(iso7816_ic_manufacturers[0]))
785
235
      ic_code = 0;
786
275
    snprintf(buffer, sizeof(buffer), "IC: %s; mask: %s",
787
275
      iso7816_ic_manufacturers[ic_code],
788
275
      itacns_mask_manufacturers[mask_code]);
789
275
    set_string(&p15card->tokeninfo->manufacturer_id, buffer);
790
275
  }
791
792
  /* Read and set serial */
793
275
  {
794
275
    u8 serial[17];
795
275
    int bytes;
796
275
    sc_format_path(path_serial, &path);
797
275
    bytes = loadFile(p15card, &path, serial, 16);
798
275
    if (bytes < 0) return bytes;
799
178
    if (bytes > 16) return -1;
800
178
    serial[bytes] = '\0';
801
178
    set_string(&p15card->tokeninfo->serial_number, (char*)serial);
802
178
  }
803
804
  /* Is the card a CIE v1? */
805
0
  card_is_cie_v1 =
806
178
       (p15card->card->type == SC_CARD_TYPE_ITACNS_CIE_V1)
807
178
    || (p15card->card->type == SC_CARD_TYPE_CARDOS_CIE_V1);
808
178
  cns0_secenv = (card_is_cie_v1 ? 0x31 : 0x01);
809
810
  /* If it's a Siemens CIE v1 card, set algo flags accordingly. */
811
178
  if (card_is_cie_v1) {
812
0
    int i;
813
0
    for (i = 0; i < p15card->card->algorithm_count; i++) {
814
0
      sc_algorithm_info_t *info =
815
0
        &p15card->card->algorithms[i];
816
817
0
      if (info->algorithm != SC_ALGORITHM_RSA)
818
0
        continue;
819
0
      info->flags &= ~(SC_ALGORITHM_RSA_RAW
820
0
        | SC_ALGORITHM_RSA_HASH_NONE);
821
0
      info->flags |= (SC_ALGORITHM_RSA_PAD_PKCS1
822
0
        | SC_ALGORITHM_RSA_HASHES);
823
0
    }
824
0
  }
825
826
  /* Data files */
827
178
  r = itacns_add_data_files(p15card);
828
178
  LOG_TEST_GOTO_ERR(p15card->card->ctx, r,
829
178
    "Could not add data files");
830
831
  /*** Certificate and keys. ***/
832
  /* Standard CNS */
833
178
  r = itacns_check_and_add_keyset(p15card, "CNS0", cns0_secenv,
834
178
    0, "3F0011001101", "3F003F01", NULL,
835
178
    0x10, &found_certs);
836
178
  LOG_TEST_GOTO_ERR(p15card->card->ctx, r,
837
115
    "Could not add CNS0");
838
115
  certificate_count += found_certs;
839
840
  /* Infocamere 1204 */
841
115
  r = itacns_check_and_add_keyset(p15card, "CNS01", 0x21,
842
115
    5, "3F002FFF8228", NULL, "3F002FFF0000",
843
115
    0x10, &found_certs);
844
115
  LOG_TEST_GOTO_ERR(p15card->card->ctx, r,
845
56
    "Could not add CNS01");
846
56
  certificate_count += found_certs;
847
848
  /* Digital signature */
849
56
  r = itacns_check_and_add_keyset(p15card, "CNS1", 0x10,
850
56
    0, "3F0014009010", "3F00140081108010", "3F0014008110",
851
56
    0x1a, &found_certs);
852
56
  LOG_TEST_GOTO_ERR(p15card->card->ctx, r,
853
17
    "Could not add CNS1");
854
17
  certificate_count += found_certs;
855
856
  /* Idemia card */
857
17
  r = itacns_check_and_add_keyset(p15card, "CNS1", 0x02,
858
17
    0, "3F00140090012002", "3F0011001102", "3F0014009002",
859
17
    0x10, &found_certs);
860
17
  LOG_TEST_RET(p15card->card->ctx, r,
861
4
    "Could not add CNS1");
862
4
  certificate_count += found_certs;
863
864
  /* Did we find anything? */
865
4
  if (certificate_count == 0)
866
4
    sc_debug(p15card->card->ctx, SC_LOG_DEBUG_NORMAL,
867
4
      "Warning: no certificates found!");
868
869
  /* Back to Master File */
870
4
  sc_format_path("3F00", &path);
871
4
  r = sc_select_file(p15card->card, &path, NULL);
872
4
  LOG_TEST_GOTO_ERR(p15card->card->ctx, r,
873
3
    "Could not select master file again");
874
875
3
  LOG_FUNC_RETURN(p15card->card->ctx, r);
876
877
162
err:
878
162
  sc_pkcs15_card_clear(p15card);
879
162
  LOG_FUNC_RETURN(p15card->card->ctx, r);
880
162
}
881
882
int sc_pkcs15emu_itacns_init_ex(sc_pkcs15_card_t *p15card, struct sc_aid *aid)
883
10.8k
{
884
10.8k
  sc_card_t *card = p15card->card;
885
10.8k
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_NORMAL);
886
887
  /* Check card */
888
10.8k
  if (! (
889
10.8k
      (card->type > SC_CARD_TYPE_ITACNS_BASE &&
890
3.68k
      card->type < SC_CARD_TYPE_ITACNS_BASE + 1000)
891
10.6k
    || card->type == SC_CARD_TYPE_CARDOS_CIE_V1)
892
10.8k
    )
893
10.6k
    return SC_ERROR_WRONG_CARD;
894
895
  /* Init card */
896
275
  return itacns_init(p15card);
897
10.8k
}