Coverage Report

Created: 2026-07-24 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/pkcs15-esteid2018.c
Line
Count
Source
1
/*
2
 * PKCS15 emulation layer for EstEID card issued from December 2018.
3
 *
4
 * Copyright (C) 2019, Martin Paljak <martin@martinpaljak.net>
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#ifdef HAVE_CONFIG_H
22
#include "config.h"
23
#endif
24
25
#include <ctype.h>
26
#include <stdio.h>
27
#include <stdlib.h>
28
#include <string.h>
29
30
#include "common/compat_strlcpy.h"
31
32
#include "internal.h"
33
#include "opensc.h"
34
#include "pkcs15.h"
35
36
static int
37
is_latvian_eid(sc_pkcs15_card_t *p15card)
38
0
{
39
0
  return p15card->card->type == SC_CARD_TYPE_LATEID_2018 ||
40
0
         p15card->card->type == SC_CARD_TYPE_LATEID_2018_V2_2025;
41
0
}
42
43
0
static int sc_pkcs15emu_esteid2018_init(sc_pkcs15_card_t *p15card) {
44
0
  sc_card_t *card = p15card->card;
45
0
  u8 buff[11];
46
0
  int r, i, cert_slot = 0;
47
0
  size_t field_length = 0, taglen, buflen;
48
0
  sc_path_t tmppath;
49
50
  /* Read documber number to be used as serial */
51
0
  sc_format_path("3F00D003", &tmppath);
52
0
  LOG_TEST_RET(card->ctx, sc_select_file(card, &tmppath, NULL), "SELECT docnr");
53
0
  r = sc_read_binary(card, 0, buff, 11, 0);
54
0
  LOG_TEST_RET(card->ctx, r, "read document number failed");
55
0
  const u8 *tag = sc_asn1_find_tag(card->ctx, buff, (size_t)r, 0x04, &taglen);
56
0
  if (tag == NULL)
57
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
58
59
0
  for (size_t j = 0; j < taglen; j++)
60
0
    if (!isalnum(tag[j]))
61
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
62
0
  free(p15card->tokeninfo->serial_number);
63
0
  p15card->tokeninfo->serial_number = malloc(taglen + 1);
64
0
  if (!p15card->tokeninfo->serial_number)
65
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
66
67
0
  set_string(&p15card->tokeninfo->label, "ID-kaart");
68
0
  set_string(&p15card->tokeninfo->manufacturer_id, "IDEMIA");
69
0
  p15card->tokeninfo->serial_number = memcpy(p15card->tokeninfo->serial_number, tag, taglen);
70
0
  p15card->tokeninfo->serial_number[taglen] = '\0';
71
0
  p15card->tokeninfo->flags = SC_PKCS15_TOKEN_READONLY;
72
73
0
  if (is_latvian_eid(p15card)) {
74
0
    u8 *buf;
75
0
    const u8 *ptr;
76
0
    sc_pkcs15_object_t obj = {0};
77
0
    sc_pkcs15_cert_info_t *cert_info = NULL;
78
79
0
    if (!p15card->file_app) {
80
0
      p15card->file_app = sc_file_new();
81
0
    }
82
0
    if (!p15card->file_app) {
83
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
84
0
    }
85
86
0
    sc_format_path("3F00adf1", &p15card->file_app->path);
87
0
    sc_format_path("3F00adf17005", &tmppath);
88
0
    r = sc_pkcs15_read_file(p15card, &tmppath, &buf, &buflen, 0);
89
0
    LOG_TEST_GOTO_ERR(card->ctx, r, "Reading EF.CDF file failed");
90
0
    ptr = buf;
91
0
    r = sc_pkcs15_decode_cdf_entry(p15card, &obj, &ptr, &buflen);
92
0
    LOG_TEST_GOTO_ERR(card->ctx, r, "Decoding EF.CDF file failed");
93
94
0
    cert_info = (struct sc_pkcs15_cert_info *)obj.data;
95
0
    if (cert_info && cert_info->path.len > 0) {
96
0
      cert_slot = cert_info->path.value[cert_info->path.len - 1] - 1;
97
0
      if (cert_slot != 0 && cert_slot != 1) {
98
        /* unknown slot, see `cert_paths` below */
99
0
        r = SC_ERROR_UNKNOWN_DATA_RECEIVED;
100
0
        LOG_TEST_GOTO_ERR(card->ctx, r, "Unknown certificate path");
101
0
      }
102
0
    }
103
104
0
    sc_pkcs15_free_cert_info(cert_info);
105
0
    sc_file_free(p15card->file_app);
106
0
    p15card->file_app = NULL;
107
0
    free(buf);
108
0
  }
109
110
  /* add certificates */
111
0
  for (i = 0; i < 2; i++) {
112
0
    static const char *esteid_cert_names[2] = {"Isikutuvastus", "Allkirjastamine"};
113
0
    static const char *cert_paths[2][2] = {
114
0
        {"3f00:adf1:3401", "3f00:adf2:341f"},
115
0
        {"3f00:adf1:3402", "3f00:adf2:341e"}
116
0
        };
117
0
    static const u8 esteid_cert_ids[2] = {1, 2};
118
119
0
    struct sc_pkcs15_cert_info cert_info = {
120
0
        .id = {.len = 1, .value[0] = esteid_cert_ids[i]}
121
0
    };
122
0
    struct sc_pkcs15_object cert_obj = {0};
123
124
0
    strlcpy(cert_obj.label, esteid_cert_names[i], sizeof(cert_obj.label));
125
0
    sc_format_path(cert_paths[cert_slot][i], &cert_info.path);
126
0
    r = sc_pkcs15emu_add_x509_cert(p15card, &cert_obj, &cert_info);
127
0
    LOG_TEST_GOTO_ERR(card->ctx, r, "Could not add cert oebjct");
128
129
    // Read data from first cert
130
0
    if (i != 0)
131
0
      continue;
132
133
0
    sc_pkcs15_cert_t *cert = NULL;
134
0
    r = sc_pkcs15_read_certificate(p15card, &cert_info, 0, &cert);
135
0
    LOG_TEST_GOTO_ERR(card->ctx, r, "Could not read authentication certificate");
136
137
0
    if (cert->key->algorithm == SC_ALGORITHM_EC)
138
0
      field_length = cert->key->u.ec.params.field_length;
139
140
0
    static const struct sc_object_id cn_oid = {
141
0
        {2, 5, 4, 3, -1}
142
0
    };
143
0
    u8 *cn_name = NULL;
144
0
    size_t cn_len = 0;
145
0
    sc_pkcs15_get_name_from_dn(card->ctx, cert->subject, cert->subject_len, &cn_oid, &cn_name, &cn_len);
146
0
    sc_pkcs15_free_certificate(cert);
147
0
    if (cn_len > 0) {
148
0
      char *token_name = (char *)realloc(cn_name, cn_len + 1);
149
0
      if (token_name) {
150
0
        token_name[cn_len] = '\0';
151
0
        free(p15card->tokeninfo->label);
152
0
        p15card->tokeninfo->label = token_name;
153
0
      } else
154
0
        free(cn_name);
155
0
    }
156
0
  }
157
158
  /* add pins */
159
0
  static const u8 pin_authid[3] = {1, 2, 3};
160
0
  for (i = 0; i < 3; i++) {
161
0
    static const char *esteid_pin_names[3] = {"PIN1", "PIN2", "PUK"};
162
0
    static const size_t pin_min[2][3] = {
163
0
        {4, 5, 8}, // Estonian
164
0
        {4, 6, 8}, // Latvian
165
0
    };
166
0
    static const int esteid_pin_ref[3] = {0x01, 0x85, 0x02};
167
0
    static const char *esteid_pin_path[3] = {"3F00", "3F00ADF2", "3F00"};
168
169
0
    static const unsigned int esteid_pin_flags[3] = {
170
0
        SC_PKCS15_PIN_FLAG_NEEDS_PADDING | SC_PKCS15_PIN_TYPE_FLAGS_PIN_GLOBAL,
171
0
        SC_PKCS15_PIN_FLAG_NEEDS_PADDING | SC_PKCS15_PIN_TYPE_FLAGS_PIN_LOCAL,
172
0
        SC_PKCS15_PIN_FLAG_NEEDS_PADDING | SC_PKCS15_PIN_TYPE_FLAGS_PUK_GLOBAL};
173
174
0
    struct sc_pkcs15_auth_info pin_info = {
175
0
        .auth_id = {.len = 1, .value[0] = pin_authid[i]},
176
0
        .auth_type = SC_PKCS15_PIN_AUTH_TYPE_PIN,
177
0
        .attrs = {
178
0
            .pin = {
179
0
                .reference = esteid_pin_ref[i],
180
0
                .flags = esteid_pin_flags[i],
181
0
                .type = SC_PKCS15_PIN_TYPE_ASCII_NUMERIC,
182
0
                .min_length = pin_min[is_latvian_eid(p15card)][i],
183
0
                .stored_length = 12,
184
0
                .max_length = 12,
185
0
                .pad_char = 0xFF}},
186
0
        .tries_left = 3,
187
0
        .max_tries = 3
188
0
          };
189
0
    struct sc_pkcs15_object pin_obj = {
190
0
        .flags = esteid_pin_flags[i]};
191
192
0
    sc_format_path(esteid_pin_path[i], &pin_info.path);
193
0
    strlcpy(pin_obj.label, esteid_pin_names[i], sizeof(pin_obj.label));
194
195
    /* Link normal PINs with PUK */
196
0
    if (i < 2) {
197
0
      pin_obj.auth_id.len = 1;
198
0
      pin_obj.auth_id.value[0] = 3;
199
0
    }
200
201
0
    r = sc_pkcs15emu_add_pin_obj(p15card, &pin_obj, &pin_info);
202
0
    LOG_TEST_GOTO_ERR(card->ctx, r, "Could not add pin object");
203
0
  }
204
205
  // trigger PIN counter refresh via pin_cmd
206
0
  struct sc_pkcs15_object *objs[3];
207
0
  r = sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_AUTH, objs, 3);
208
0
  if (r != 3) {
209
0
    sc_log(card->ctx, "Can not get auth objects");
210
0
    goto err;
211
0
  }
212
0
  for (i = 0; i < r; i++) {
213
0
    r = sc_pkcs15_get_pin_info(p15card, objs[i]);
214
0
    LOG_TEST_GOTO_ERR(card->ctx, r, "Could not get pin object");
215
0
  }
216
217
  /* add private keys */
218
0
  for (i = 0; i < 2; i++) {
219
0
    static const u8 prkey_id[2] = {1, 2};
220
0
    static const char *prkey_name[2] = {"Isikutuvastus", "Allkirjastamine"};
221
0
    static const char *prkey_path[2] = {"3F00:ADF1", "3F00:ADF2"};
222
0
    static const unsigned int prkey_usage[2] = {SC_PKCS15_PRKEY_USAGE_SIGN | SC_PKCS15_PRKEY_USAGE_DERIVE,
223
0
        SC_PKCS15_PRKEY_USAGE_NONREPUDIATION};
224
0
    static const int prkey_consent[2] = {0, 1};
225
0
    static const u8 prkey_ref[2][2] = {
226
0
        {0x81, 0x9F}, // Slot 1
227
0
        {0x82, 0x9E}, // Slot 2
228
0
    };
229
230
0
    struct sc_pkcs15_prkey_info prkey_info = {
231
0
        .id = {.len = 1, .value[0] = prkey_id[i]},
232
0
        .native = 1,
233
0
        .key_reference = prkey_ref[is_latvian_eid(p15card)][i],
234
0
        .field_length = field_length,
235
0
        .usage = prkey_usage[i]
236
0
           };
237
0
    struct sc_pkcs15_object prkey_obj = {
238
0
        .auth_id = {.len = 1, .value[0] = pin_authid[i]},
239
0
        .user_consent = prkey_consent[i],
240
0
        .flags = SC_PKCS15_CO_FLAG_PRIVATE
241
0
      };
242
243
0
    sc_format_path(prkey_path[i], &prkey_info.path);
244
0
    strlcpy(prkey_obj.label, prkey_name[i], sizeof(prkey_obj.label));
245
246
0
    r = sc_pkcs15emu_add_ec_prkey(p15card, &prkey_obj, &prkey_info);
247
0
    LOG_TEST_GOTO_ERR(card->ctx, r, "Could not add private key object");
248
0
  }
249
250
0
  return SC_SUCCESS;
251
0
err:
252
0
  sc_pkcs15_card_clear(p15card);
253
0
  LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
254
0
}
255
256
0
int sc_pkcs15emu_esteid2018_init_ex(sc_pkcs15_card_t *p15card, struct sc_aid *aid) {
257
0
  if (p15card->card->type == SC_CARD_TYPE_ESTEID_2018 ||
258
0
      p15card->card->type == SC_CARD_TYPE_ESTEID_2018_V2_2025 ||
259
0
      is_latvian_eid(p15card))
260
0
    return sc_pkcs15emu_esteid2018_init(p15card);
261
0
  return SC_ERROR_WRONG_CARD;
262
0
}