Coverage Report

Created: 2025-11-16 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/pkcs15-pteid.c
Line
Count
Source
1
/*
2
 * PKCS15 emulation layer for Portugal eID card.
3
 *
4
 * Copyright (C) 2016-2017, Nuno Goncalves <nunojpg@gmail.com>
5
 * Copyright (C) 2009, Joao Poupino <joao.poupino@ist.utl.pt>
6
 * Copyright (C) 2004, Martin Paljak <martin@martinpaljak.net>
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
 * Based on the PKCS#15 emulation layer for EstEID card by Martin Paljak
23
 *
24
 */
25
26
/*
27
 * The card has a valid PKCS#15 file system. However, the private keys
28
 * are missing the SC_PKCS15_CO_FLAG_PRIVATE flag and this causes problems
29
 * with some applications (i.e. they don't work).
30
 *
31
 * The three main objectives of the emulation layer are:
32
 *
33
 * 1. Add the necessary SC_PKCS15_CO_FLAG_PRIVATE flag to private keys.
34
 * 2. Hide "superfluous" PKCS#15 objects, e.g. PUKs (the user can't use them).
35
 * 3. Improve usability by providing more descriptive names for the PINs, Keys, etc.
36
 *
37
 */
38
39
#ifdef HAVE_CONFIG_H
40
#include "config.h"
41
#endif
42
43
#include <stdlib.h>
44
#include <string.h>
45
#include <stdio.h>
46
47
#include "common/compat_strlcpy.h"
48
#include "internal.h"
49
#include "pkcs15.h"
50
51
static int pteid_detect_card(struct sc_card *card);
52
53
static
54
int dump_ef(sc_card_t * card, const char *path, u8 * buf, size_t * buf_len)
55
0
{
56
0
  int rv;
57
0
  sc_file_t *file = NULL;
58
0
  sc_path_t scpath;
59
0
  sc_format_path(path, &scpath);
60
0
  rv = sc_select_file(card, &scpath, &file);
61
0
  if (rv < 0) {
62
0
    sc_file_free(file);
63
0
    return rv;
64
0
  }
65
0
  if (file->size > *buf_len) {
66
0
    sc_file_free(file);
67
0
    return SC_ERROR_BUFFER_TOO_SMALL;
68
0
  }
69
0
  rv = sc_read_binary(card, 0, buf, file->size, 0);
70
0
  sc_file_free(file);
71
0
  if (rv < 0)
72
0
    return rv;
73
0
  *buf_len = rv;
74
75
0
  return SC_SUCCESS;
76
0
}
77
78
static const struct sc_asn1_entry c_asn1_odf[] = {
79
  {"privateKeys", SC_ASN1_STRUCT, SC_ASN1_CTX | 0 | SC_ASN1_CONS, 0, NULL, NULL},
80
  {"publicKeys", SC_ASN1_STRUCT, SC_ASN1_CTX | 1 | SC_ASN1_CONS, 0, NULL,  NULL},
81
  {"trustedPublicKeys", SC_ASN1_STRUCT, SC_ASN1_CTX | 2 | SC_ASN1_CONS, 0, NULL, NULL},
82
  {"secretKeys", SC_ASN1_STRUCT, SC_ASN1_CTX | 3 | SC_ASN1_CONS, 0, NULL,  NULL},
83
  {"certificates", SC_ASN1_STRUCT, SC_ASN1_CTX | 4 | SC_ASN1_CONS, 0,  NULL, NULL},
84
  {"trustedCertificates", SC_ASN1_STRUCT, SC_ASN1_CTX | 5 | SC_ASN1_CONS,  0, NULL, NULL},
85
  {"usefulCertificates", SC_ASN1_STRUCT, SC_ASN1_CTX | 6 | SC_ASN1_CONS,   0, NULL, NULL},
86
  {"dataObjects", SC_ASN1_STRUCT, SC_ASN1_CTX | 7 | SC_ASN1_CONS, 0, NULL,   NULL},
87
  {"authObjects", SC_ASN1_STRUCT, SC_ASN1_CTX | 8 | SC_ASN1_CONS, 0, NULL,   NULL},
88
  {NULL, 0, 0, 0, NULL, NULL}
89
};
90
91
static const unsigned int odf_indexes[] = {
92
  SC_PKCS15_PRKDF,    //0
93
  SC_PKCS15_PUKDF,    //1
94
  SC_PKCS15_PUKDF_TRUSTED,  //2
95
  SC_PKCS15_SKDF,     //3
96
  SC_PKCS15_CDF,      //4
97
  SC_PKCS15_CDF_TRUSTED,    //5
98
  SC_PKCS15_CDF_USEFUL,   //6
99
  SC_PKCS15_DODF,     //7
100
  SC_PKCS15_AODF,     //8
101
};
102
103
static
104
int parse_odf(const u8 * buf, size_t buflen, struct sc_pkcs15_card *p15card)
105
0
{
106
0
  const u8 *p = buf;
107
0
  size_t left = buflen;
108
0
  int r, i, type;
109
0
  sc_path_t path;
110
0
  struct sc_asn1_entry asn1_obj_or_path[] = {
111
0
    {"path", SC_ASN1_PATH, SC_ASN1_CONS | SC_ASN1_SEQUENCE, 0,
112
0
     &path, NULL},
113
0
    {NULL, 0, 0, 0, NULL, NULL}
114
0
  };
115
0
  struct sc_asn1_entry asn1_odf[10];
116
117
0
  sc_path_t path_prefix;
118
119
0
  sc_format_path("3F004F00", &path_prefix);
120
121
0
  sc_copy_asn1_entry(c_asn1_odf, asn1_odf);
122
0
  for (i = 0; asn1_odf[i].name != NULL; i++)
123
0
    sc_format_asn1_entry(asn1_odf + i, asn1_obj_or_path, NULL, 0);
124
0
  while (left > 0) {
125
0
    r = sc_asn1_decode_choice(p15card->card->ctx, asn1_odf, p, left,
126
0
            &p, &left);
127
0
    if (r == SC_ERROR_ASN1_END_OF_CONTENTS)
128
0
      break;
129
0
    if (r < 0)
130
0
      return r;
131
0
    type = r;
132
0
    r = sc_pkcs15_make_absolute_path(&path_prefix, &path);
133
0
    if (r < 0)
134
0
      return r;
135
0
    r = sc_pkcs15_add_df(p15card, odf_indexes[type], &path);
136
0
    if (r)
137
0
      return r;
138
0
  }
139
0
  return 0;
140
0
}
141
142
static int sc_pkcs15emu_pteid_init(sc_pkcs15_card_t * p15card)
143
0
{
144
0
  u8 buf[1024];
145
0
  sc_pkcs15_df_t *df;
146
0
  sc_pkcs15_object_t *p15_obj;
147
0
  sc_path_t path;
148
0
  struct sc_file *file = NULL;
149
0
  size_t len;
150
0
  int rv;
151
0
  int i;
152
153
0
  sc_context_t *ctx = p15card->card->ctx;
154
0
  LOG_FUNC_CALLED(ctx);
155
156
  /* Check for correct card atr */
157
0
  if (pteid_detect_card(p15card->card) != SC_SUCCESS)
158
0
    return SC_ERROR_WRONG_CARD;
159
160
0
  sc_log(p15card->card->ctx, "Selecting application DF");
161
0
  sc_format_path("4F00", &path);
162
0
  rv = sc_select_file(p15card->card, &path, &file);
163
0
  if (rv != SC_SUCCESS || !file)
164
0
    return SC_ERROR_INTERNAL;
165
  /* set the application DF */
166
0
  sc_file_free(p15card->file_app);
167
0
  p15card->file_app = file;
168
169
  /* Load TokenInfo */
170
0
  len = sizeof(buf);
171
0
  rv = dump_ef(p15card->card, "4F005032", buf, &len);
172
0
  if (rv != SC_SUCCESS) {
173
0
    sc_log(ctx, "Reading of EF.TOKENINFO failed: %d", rv);
174
0
    LOG_FUNC_RETURN(ctx, rv);
175
0
  }
176
0
  rv = sc_pkcs15_parse_tokeninfo(p15card->card->ctx, p15card->tokeninfo,
177
0
               buf, len);
178
0
  if (rv != SC_SUCCESS) {
179
0
    sc_log(ctx, "Decoding of EF.TOKENINFO failed: %d", rv);
180
0
    LOG_FUNC_RETURN(ctx, rv);
181
0
  }
182
183
0
  p15card->tokeninfo->flags |= SC_PKCS15_TOKEN_PRN_GENERATION
184
0
          | SC_PKCS15_TOKEN_EID_COMPLIANT
185
0
          | SC_PKCS15_TOKEN_READONLY;
186
187
  /* Load ODF */
188
0
  len = sizeof(buf);
189
0
  rv = dump_ef(p15card->card, "4F005031", buf, &len);
190
0
  if (rv != SC_SUCCESS) {
191
0
    sc_log(ctx, "Reading of ODF failed: %d", rv);
192
0
    LOG_FUNC_RETURN(ctx, rv);
193
0
  }
194
0
  rv = parse_odf(buf, len, p15card);
195
0
  if (rv != SC_SUCCESS) {
196
0
    sc_log(ctx, "Decoding of ODF failed: %d", rv);
197
0
    sc_pkcs15_card_clear(p15card);
198
0
    LOG_FUNC_RETURN(ctx, rv);
199
0
  }
200
201
  /* Decode EF.PrKDF, EF.PuKDF, EF.CDF and EF.AODF */
202
0
  for (df = p15card->df_list; df != NULL; df = df->next) {
203
0
    if (df->type == SC_PKCS15_PRKDF) {
204
0
      rv = sc_pkcs15_parse_df(p15card, df);
205
0
      if (rv != SC_SUCCESS) {
206
0
        sc_log(ctx,
207
0
               "Decoding of EF.PrKDF (%s) failed: %d",
208
0
               sc_print_path(&df->path), rv);
209
0
      }
210
0
    }
211
0
    if (df->type == SC_PKCS15_PUKDF) {
212
0
      rv = sc_pkcs15_parse_df(p15card, df);
213
0
      if (rv != SC_SUCCESS) {
214
0
        sc_log(ctx,
215
0
               "Decoding of EF.PuKDF (%s) failed: %d",
216
0
               sc_print_path(&df->path), rv);
217
0
      }
218
0
    }
219
0
    if (df->type == SC_PKCS15_CDF) {
220
0
      rv = sc_pkcs15_parse_df(p15card, df);
221
0
      if (rv != SC_SUCCESS) {
222
0
        sc_log(ctx,
223
0
               "Decoding of EF.CDF (%s) failed: %d",
224
0
               sc_print_path(&df->path), rv);
225
0
      }
226
0
    }
227
0
    if (df->type == SC_PKCS15_AODF) {
228
0
      rv = sc_pkcs15_parse_df(p15card, df);
229
0
      if (rv != SC_SUCCESS) {
230
0
        sc_log(ctx,
231
0
               "Decoding of EF.AODF (%s) failed: %d",
232
0
               sc_print_path(&df->path), rv);
233
0
      }
234
0
    }
235
0
  }
236
237
0
  p15_obj = p15card->obj_list;
238
0
  while (p15_obj != NULL) {
239
0
    if ( p15_obj->df && (p15_obj->df->type == SC_PKCS15_PRKDF) ) {
240
0
      struct sc_pkcs15_prkey_info *prkey_info = (sc_pkcs15_prkey_info_t *) p15_obj->data;
241
0
      prkey_info->access_flags = SC_PKCS15_PRKEY_ACCESS_SENSITIVE
242
0
          | SC_PKCS15_PRKEY_ACCESS_ALWAYSSENSITIVE
243
0
          | SC_PKCS15_PRKEY_ACCESS_NEVEREXTRACTABLE
244
0
          | SC_PKCS15_PRKEY_ACCESS_LOCAL;
245
0
      p15_obj->flags = SC_PKCS15_CO_FLAG_PRIVATE;
246
0
    }
247
248
249
0
    if ( p15_obj->df && (p15_obj->df->type == SC_PKCS15_AODF) ) {
250
0
      static const char *pteid_pin_names[3] = {
251
0
          "Auth PIN",
252
0
          "Sign PIN",
253
0
          "Address PIN"
254
0
      };
255
256
0
      struct sc_pin_cmd_data pin_cmd_data;
257
0
      struct sc_pkcs15_auth_info *pin_info = (sc_pkcs15_auth_info_t *) p15_obj->data;
258
259
0
      if (pin_info->auth_id.value[0] < 1 || pin_info->auth_id.value[0] > 3) {
260
0
        sc_log(ctx, "Invalid auth_id for PIN: Value %d out of range. Skipping.",
261
0
            pin_info->auth_id.value[0]);
262
0
        p15_obj = p15_obj->next;
263
0
        continue;
264
0
      }
265
266
0
      strlcpy(p15_obj->label, pteid_pin_names[pin_info->auth_id.value[0]-1], sizeof(p15_obj->label));
267
268
0
      pin_info->attrs.pin.flags |= SC_PKCS15_PIN_FLAG_NEEDS_PADDING;
269
0
      pin_info->tries_left = -1;
270
0
      pin_info->max_tries = 3;
271
0
      pin_info->auth_method = SC_AC_CHV;
272
273
0
      memset(&pin_cmd_data, 0, sizeof(pin_cmd_data));
274
0
      pin_cmd_data.cmd = SC_PIN_CMD_GET_INFO;
275
0
      pin_cmd_data.pin_type = pin_info->attrs.pin.type;
276
0
      pin_cmd_data.pin_reference = pin_info->attrs.pin.reference;
277
0
      rv = sc_pin_cmd(p15card->card, &pin_cmd_data, NULL);
278
0
      if (rv == SC_SUCCESS) {
279
0
        pin_info->tries_left = pin_cmd_data.pin1.tries_left;
280
0
        pin_info->logged_in = pin_cmd_data.pin1.logged_in;
281
0
      }
282
0
    }
283
    /* Remove found public keys as cannot be read_binary()'d */
284
0
    if ( p15_obj->df && (p15_obj->df->type == SC_PKCS15_PUKDF) ) {
285
0
      sc_pkcs15_object_t *puk = p15_obj;
286
0
      p15_obj = p15_obj->next;
287
0
      sc_pkcs15_remove_object(p15card, puk);
288
0
      sc_pkcs15_free_object(puk);
289
0
    } else {
290
0
      p15_obj = p15_obj->next;
291
0
    }
292
0
  }
293
294
  /* Add data objects */
295
0
  for (i = 0; i < 5; i++) {
296
0
    static const char *object_labels[5] = {
297
0
      "Trace",
298
0
      "Citizen Data",
299
0
      "Citizen Address Data",
300
0
      "SOd",
301
0
      "Citizen Notepad",
302
0
    };
303
0
    static const char *object_authids[5] = {NULL, NULL, "3", NULL, NULL};
304
0
    static const char *object_paths[5] = {
305
0
      "3f000003",
306
0
      "3f005f00ef02",
307
0
      "3f005f00ef05",
308
0
      "3f005f00ef06",
309
0
      "3f005f00ef07",
310
0
    };
311
0
    static const int object_flags[5] = {
312
0
      0,
313
0
      0,
314
0
      SC_PKCS15_CO_FLAG_PRIVATE,
315
0
      0,
316
0
      0,
317
0
    };
318
0
    struct sc_pkcs15_data_info obj_info;
319
0
    struct sc_pkcs15_object obj_obj;
320
321
0
    memset(&obj_info, 0, sizeof(obj_info));
322
0
    memset(&obj_obj, 0, sizeof(obj_obj));
323
324
0
    sc_format_path(object_paths[i], &obj_info.path);
325
0
    strlcpy(obj_info.app_label, object_labels[i], SC_PKCS15_MAX_LABEL_SIZE);
326
0
    if (object_authids[i] != NULL)
327
0
      sc_pkcs15_format_id(object_authids[i], &obj_obj.auth_id);
328
0
    strlcpy(obj_obj.label, object_labels[i], SC_PKCS15_MAX_LABEL_SIZE);
329
0
    obj_obj.flags = object_flags[i];
330
331
0
    rv = sc_pkcs15emu_object_add(p15card, SC_PKCS15_TYPE_DATA_OBJECT, &obj_obj, &obj_info);
332
0
    if (rv != SC_SUCCESS){
333
0
      sc_log(ctx, "Object add failed: %d", rv);
334
0
      break;
335
0
    }
336
0
  }
337
338
0
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
339
0
}
340
341
static int pteid_detect_card(struct sc_card *card)
342
0
{
343
0
  if (card->type == SC_CARD_TYPE_GEMSAFEV1_PTEID)
344
0
    return SC_SUCCESS;
345
0
  return SC_ERROR_WRONG_CARD;
346
0
}
347
348
int sc_pkcs15emu_pteid_init_ex(sc_pkcs15_card_t *p15card, struct sc_aid *aid)
349
0
{
350
0
  int r=SC_SUCCESS;
351
0
  sc_context_t *ctx = p15card->card->ctx;
352
0
  LOG_FUNC_CALLED(ctx);
353
354
  /* check for proper card */
355
0
  r = pteid_detect_card(p15card->card);
356
0
  if (r == SC_ERROR_WRONG_CARD)
357
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_WRONG_CARD);
358
  /* ok: initialize and return */
359
0
  LOG_FUNC_RETURN(ctx, sc_pkcs15emu_pteid_init(p15card));
360
0
}