Coverage Report

Created: 2025-07-18 06:10

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