Coverage Report

Created: 2025-12-07 06:37

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
    }
98
99
0
    sc_pkcs15_free_cert_info(cert_info);
100
0
    sc_file_free(p15card->file_app);
101
0
    p15card->file_app = NULL;
102
0
    free(buf);
103
0
  }
104
105
  /* add certificates */
106
0
  for (i = 0; i < 2; i++) {
107
0
    static const char *esteid_cert_names[2] = {"Isikutuvastus", "Allkirjastamine"};
108
0
    static const char *cert_paths[2][2] = {
109
0
        {"3f00:adf1:3401", "3f00:adf2:341f"},
110
0
        {"3f00:adf1:3402", "3f00:adf2:341e"}
111
0
        };
112
0
    static const u8 esteid_cert_ids[2] = {1, 2};
113
114
0
    struct sc_pkcs15_cert_info cert_info = {
115
0
        .id = {.len = 1, .value[0] = esteid_cert_ids[i]}
116
0
    };
117
0
    struct sc_pkcs15_object cert_obj = {0};
118
119
0
    strlcpy(cert_obj.label, esteid_cert_names[i], sizeof(cert_obj.label));
120
0
    sc_format_path(cert_paths[cert_slot][i], &cert_info.path);
121
0
    r = sc_pkcs15emu_add_x509_cert(p15card, &cert_obj, &cert_info);
122
0
    LOG_TEST_GOTO_ERR(card->ctx, r, "Could not add cert oebjct");
123
124
    // Read data from first cert
125
0
    if (i != 0)
126
0
      continue;
127
128
0
    sc_pkcs15_cert_t *cert = NULL;
129
0
    r = sc_pkcs15_read_certificate(p15card, &cert_info, 0, &cert);
130
0
    LOG_TEST_GOTO_ERR(card->ctx, r, "Could not read authentication certificate");
131
132
0
    if (cert->key->algorithm == SC_ALGORITHM_EC)
133
0
      field_length = cert->key->u.ec.params.field_length;
134
135
0
    static const struct sc_object_id cn_oid = {
136
0
        {2, 5, 4, 3, -1}
137
0
    };
138
0
    u8 *cn_name = NULL;
139
0
    size_t cn_len = 0;
140
0
    sc_pkcs15_get_name_from_dn(card->ctx, cert->subject, cert->subject_len, &cn_oid, &cn_name, &cn_len);
141
0
    sc_pkcs15_free_certificate(cert);
142
0
    if (cn_len > 0) {
143
0
      char *token_name = (char *)realloc(cn_name, cn_len + 1);
144
0
      if (token_name) {
145
0
        token_name[cn_len] = '\0';
146
0
        free(p15card->tokeninfo->label);
147
0
        p15card->tokeninfo->label = token_name;
148
0
      } else
149
0
        free(cn_name);
150
0
    }
151
0
  }
152
153
  /* add pins */
154
0
  static const u8 pin_authid[3] = {1, 2, 3};
155
0
  for (i = 0; i < 3; i++) {
156
0
    static const char *esteid_pin_names[3] = {"PIN1", "PIN2", "PUK"};
157
0
    static const size_t pin_min[2][3] = {
158
0
        {4, 5, 8}, // Estonian
159
0
        {4, 6, 8}, // Latvian
160
0
    };
161
0
    static const int esteid_pin_ref[3] = {0x01, 0x85, 0x02};
162
0
    static const char *esteid_pin_path[3] = {"3F00", "3F00ADF2", "3F00"};
163
164
0
    static const unsigned int esteid_pin_flags[3] = {
165
0
        SC_PKCS15_PIN_FLAG_NEEDS_PADDING | SC_PKCS15_PIN_TYPE_FLAGS_PIN_GLOBAL,
166
0
        SC_PKCS15_PIN_FLAG_NEEDS_PADDING | SC_PKCS15_PIN_TYPE_FLAGS_PIN_LOCAL,
167
0
        SC_PKCS15_PIN_FLAG_NEEDS_PADDING | SC_PKCS15_PIN_TYPE_FLAGS_PUK_GLOBAL};
168
169
0
    struct sc_pkcs15_auth_info pin_info = {
170
0
        .auth_id = {.len = 1, .value[0] = pin_authid[i]},
171
0
        .auth_type = SC_PKCS15_PIN_AUTH_TYPE_PIN,
172
0
        .attrs = {
173
0
            .pin = {
174
0
                .reference = esteid_pin_ref[i],
175
0
                .flags = esteid_pin_flags[i],
176
0
                .type = SC_PKCS15_PIN_TYPE_ASCII_NUMERIC,
177
0
                .min_length = pin_min[is_latvian_eid(p15card)][i],
178
0
                .stored_length = 12,
179
0
                .max_length = 12,
180
0
                .pad_char = 0xFF}},
181
0
        .tries_left = 3,
182
0
        .max_tries = 3
183
0
          };
184
0
    struct sc_pkcs15_object pin_obj = {
185
0
        .flags = esteid_pin_flags[i]};
186
187
0
    sc_format_path(esteid_pin_path[i], &pin_info.path);
188
0
    strlcpy(pin_obj.label, esteid_pin_names[i], sizeof(pin_obj.label));
189
190
    /* Link normal PINs with PUK */
191
0
    if (i < 2) {
192
0
      pin_obj.auth_id.len = 1;
193
0
      pin_obj.auth_id.value[0] = 3;
194
0
    }
195
196
0
    r = sc_pkcs15emu_add_pin_obj(p15card, &pin_obj, &pin_info);
197
0
    LOG_TEST_GOTO_ERR(card->ctx, r, "Could not add pin object");
198
0
  }
199
200
  // trigger PIN counter refresh via pin_cmd
201
0
  struct sc_pkcs15_object *objs[3];
202
0
  r = sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_AUTH, objs, 3);
203
0
  if (r != 3) {
204
0
    sc_log(card->ctx, "Can not get auth objects");
205
0
    goto err;
206
0
  }
207
0
  for (i = 0; i < r; i++) {
208
0
    r = sc_pkcs15_get_pin_info(p15card, objs[i]);
209
0
    LOG_TEST_GOTO_ERR(card->ctx, r, "Could not get pin object");
210
0
  }
211
212
  /* add private keys */
213
0
  for (i = 0; i < 2; i++) {
214
0
    static const u8 prkey_id[2] = {1, 2};
215
0
    static const char *prkey_name[2] = {"Isikutuvastus", "Allkirjastamine"};
216
0
    static const char *prkey_path[2] = {"3F00:ADF1", "3F00:ADF2"};
217
0
    static const unsigned int prkey_usage[2] = {SC_PKCS15_PRKEY_USAGE_SIGN | SC_PKCS15_PRKEY_USAGE_DERIVE,
218
0
        SC_PKCS15_PRKEY_USAGE_NONREPUDIATION};
219
0
    static const int prkey_consent[2] = {0, 1};
220
0
    static const u8 prkey_ref[2][2] = {
221
0
        {0x81, 0x9F}, // Slot 1
222
0
        {0x82, 0x9E}, // Slot 2
223
0
    };
224
225
0
    struct sc_pkcs15_prkey_info prkey_info = {
226
0
        .id = {.len = 1, .value[0] = prkey_id[i]},
227
0
        .native = 1,
228
0
        .key_reference = prkey_ref[is_latvian_eid(p15card)][i],
229
0
        .field_length = field_length,
230
0
        .usage = prkey_usage[i]
231
0
           };
232
0
    struct sc_pkcs15_object prkey_obj = {
233
0
        .auth_id = {.len = 1, .value[0] = pin_authid[i]},
234
0
        .user_consent = prkey_consent[i],
235
0
        .flags = SC_PKCS15_CO_FLAG_PRIVATE
236
0
      };
237
238
0
    sc_format_path(prkey_path[i], &prkey_info.path);
239
0
    strlcpy(prkey_obj.label, prkey_name[i], sizeof(prkey_obj.label));
240
241
0
    r = sc_pkcs15emu_add_ec_prkey(p15card, &prkey_obj, &prkey_info);
242
0
    LOG_TEST_GOTO_ERR(card->ctx, r, "Could not add private key object");
243
0
  }
244
245
0
  return SC_SUCCESS;
246
0
err:
247
0
  sc_pkcs15_card_clear(p15card);
248
0
  LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
249
0
}
250
251
0
int sc_pkcs15emu_esteid2018_init_ex(sc_pkcs15_card_t *p15card, struct sc_aid *aid) {
252
0
  if (p15card->card->type == SC_CARD_TYPE_ESTEID_2018 ||
253
0
      p15card->card->type == SC_CARD_TYPE_ESTEID_2018_V2_2025 ||
254
0
      is_latvian_eid(p15card))
255
0
    return sc_pkcs15emu_esteid2018_init(p15card);
256
0
  return SC_ERROR_WRONG_CARD;
257
0
}