Coverage Report

Created: 2026-08-13 06:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/card-esteid2018.c
Line
Count
Source
1
/*
2
 * Driver 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 <stdlib.h>
27
#include <string.h>
28
29
#include "asn1.h"
30
#include "gp.h"
31
#include "internal.h"
32
33
/* Helping defines */
34
0
#define SIGNATURE_PAYLOAD_SIZE 0x30
35
0
#define SIGN_REF         0x10
36
0
#define PIN1_REF         0x01
37
0
#define PIN2_REF         0x85
38
0
#define PUK_REF          0x02
39
40
/*
41
 * EstEID: https://www.id.ee/wp-content/uploads/2025/10/id1developerguide2025.pdf
42
 */
43
static const struct sc_atr_table esteid_atrs[] = {
44
    {"3b:db:96:00:80:b1:fe:45:1f:83:00:12:23:3f:53:65:49:44:0f:90:00:f1",    NULL, "EstEID 2018",    SC_CARD_TYPE_ESTEID_2018,       0, NULL},
45
    {"3b:db:96:00:80:b1:fe:45:1f:83:00:12:42:8f:53:65:49:44:0f:90:00:20",    NULL, "Latvian eID 2018",  SC_CARD_TYPE_LATEID_2018,   0, NULL},
46
    {"3b:dc:96:00:80:b1:fe:45:1f:83:00:12:23:3f:54:65:49:44:32:0f:90:00:c3", NULL, "EstEID 2018 v2",   SC_CARD_TYPE_ESTEID_2018_V2_2025, 0, NULL},
47
    {"3b:dc:96:00:80:b1:fe:45:1f:83:00:12:42:8f:54:65:49:44:32:0f:90:00:12", NULL, "Latvian eID 2018 v2", SC_CARD_TYPE_LATEID_2018_V2_2025, 0, NULL},
48
    {NULL,                   NULL, NULL,       0,             0, NULL}
49
};
50
51
static const struct sc_aid IASECC_AID = {
52
    {0xA0, 0x00, 0x00, 0x00, 0x77, 0x01, 0x08, 0x00, 0x07, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x01, 0x00},
53
    16
54
};
55
56
static const struct sc_path MF = {
57
    {0x3f, 0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
58
    2, 0, 0, SC_PATH_TYPE_PATH, {{0}, 0}
59
};
60
61
static const struct sc_path adf2 = {
62
    {0x3f, 0x00, 0xAD, 0xF2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
63
    4, 0, 0, SC_PATH_TYPE_PATH, {{0}, 0}
64
};
65
66
static const struct sc_card_operations *iso_ops = NULL;
67
static struct sc_card_operations esteid_ops;
68
69
static struct sc_card_driver esteid2018_driver = {"EstEID 2018", "esteid2018", &esteid_ops, NULL, 0, NULL};
70
71
struct esteid_priv_data {
72
  sc_security_env_t sec_env; /* current security environment */
73
};
74
75
61
#define DRVDATA(card) ((struct esteid_priv_data *)((card)->drv_data))
76
77
#define SC_TRANSMIT_TEST_RET(card, apdu, text) \
78
220
  do { \
79
220
    LOG_TEST_RET(card->ctx, sc_transmit_apdu(card, &apdu), "APDU transmit failed"); \
80
220
    LOG_TEST_RET(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2), text); \
81
208
  } while (0)
82
83
7.33k
static int esteid_match_card(sc_card_t *card) {
84
7.33k
  int i = _sc_match_atr(card, esteid_atrs, &card->type);
85
86
7.33k
  if (i >= 0 && gp_select_aid(card, &IASECC_AID) == SC_SUCCESS) {
87
61
    card->name = esteid_atrs[i].name;
88
61
    return 1;
89
61
  }
90
7.27k
  return 0;
91
7.33k
}
92
93
343
static int esteid_select_file(struct sc_card *card, const struct sc_path *in_path, struct sc_file **file_out) {
94
343
  const u8 *path = in_path->value;
95
343
  u8 resp[SC_MAX_APDU_RESP_SIZE];
96
343
  size_t resplen = sizeof(resp);
97
343
  int r;
98
343
  struct sc_file *file = NULL;
99
343
  struct sc_apdu apdu;
100
101
343
  LOG_FUNC_CALLED(card->ctx);
102
103
  // Only support full paths
104
343
  if (in_path->type != SC_PATH_TYPE_PATH) {
105
123
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
106
123
  }
107
220
  if (in_path->len % 2 != 0) {
108
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
109
0
  }
110
111
222
  for (size_t pathlen = in_path->len; pathlen >= 2; pathlen -= 2, path += 2) {
112
220
    if (pathlen == 2 && memcmp(path, "\x3F\x00", 2) == 0) {
113
5
      sc_format_apdu_ex(&apdu, card->cla, 0xA4, 0x00, 0x0C, path, 0, NULL, 0);
114
5
      SC_TRANSMIT_TEST_RET(card, apdu, "MF select failed");
115
215
    } else if (pathlen == 2 && path[0] == 0xAD) {
116
4
      sc_format_apdu_ex(&apdu, card->cla, 0xA4, 0x01, 0x0C, path, 2, NULL, 0);
117
4
      SC_TRANSMIT_TEST_RET(card, apdu, "DF select failed");
118
211
    } else {
119
211
      sc_format_apdu_ex(&apdu, card->cla, 0xA4, 0x09, 0x04, path, pathlen, resp, resplen);
120
211
      SC_TRANSMIT_TEST_RET(card, apdu, "EF select failed");
121
122
78
      if (file_out != NULL) {
123
47
        file = sc_file_new();
124
47
        if (file == NULL)
125
47
          LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
126
47
        r = iso_ops->process_fci(card, file, resp, apdu.resplen);
127
47
        if (r != SC_SUCCESS) {
128
0
          sc_file_free(file);
129
47
        } else {
130
47
          *file_out = file;
131
47
        }
132
47
        LOG_TEST_RET(card->ctx, r, "Process fci failed");
133
47
      }
134
78
      break;
135
78
    }
136
220
  }
137
80
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
138
80
}
139
140
0
static int esteid_set_security_env(sc_card_t *card, const sc_security_env_t *env, int se_num) {
141
0
  struct esteid_priv_data *priv;
142
0
  sc_security_env_t new_env = {0};
143
144
0
  LOG_FUNC_CALLED(card->ctx);
145
146
0
  if (env == NULL || env->key_ref_len != 1)
147
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
148
149
0
  sc_log(card->ctx, "algo: %lu operation: %d keyref: %d", env->algorithm, env->operation, env->key_ref[0]);
150
151
0
  if (env->algorithm != SC_ALGORITHM_EC) {
152
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
153
0
  }
154
0
  new_env = *env;
155
0
  switch (env->operation) {
156
0
  case SC_SEC_OPERATION_SIGN:
157
0
    if (env->key_ref[0] & SIGN_REF) {
158
0
      new_env.algorithm_ref = 0x54;
159
0
    } else {
160
0
      new_env.operation = SC_SEC_OPERATION_AUTHENTICATE;
161
0
      new_env.algorithm_ref = 0x04;
162
0
    }
163
0
    break;
164
0
  case SC_SEC_OPERATION_DERIVE:
165
0
    new_env.algorithm_ref = 0x0B;
166
0
    break;
167
0
  default:
168
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
169
0
  }
170
0
  new_env.flags |= SC_SEC_ENV_ALG_REF_PRESENT;
171
0
  new_env.flags &= ~SC_SEC_ENV_FILE_REF_PRESENT;
172
0
  LOG_TEST_RET(card->ctx, iso_ops->set_security_env(card, &new_env, se_num), "Failed to set security environment");
173
174
0
  priv = DRVDATA(card);
175
0
  priv->sec_env = *env;
176
0
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
177
0
}
178
179
0
static int esteid_compute_signature(sc_card_t *card, const u8 *data, size_t datalen, u8 *out, size_t outlen) {
180
0
  struct esteid_priv_data *priv = DRVDATA(card);
181
0
  struct sc_security_env *env = NULL;
182
0
  struct sc_apdu apdu;
183
0
  u8 sbuf[SIGNATURE_PAYLOAD_SIZE] = {0};
184
0
  size_t le = MIN(SC_MAX_APDU_RESP_SIZE, MIN(SIGNATURE_PAYLOAD_SIZE * 2, outlen));
185
186
0
  LOG_FUNC_CALLED(card->ctx);
187
0
  if (data == NULL || out == NULL || datalen > SIGNATURE_PAYLOAD_SIZE)
188
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
189
190
0
  env = &priv->sec_env;
191
  // left-pad if necessary
192
0
  memcpy(&sbuf[SIGNATURE_PAYLOAD_SIZE - datalen], data, MIN(datalen, SIGNATURE_PAYLOAD_SIZE));
193
0
  datalen = SIGNATURE_PAYLOAD_SIZE;
194
195
0
  if ((env->key_ref[0] & SIGN_REF) > 0) {
196
0
    sc_format_apdu_ex(&apdu, 0x00, 0x2A, 0x9E, 0x9A, sbuf, datalen, out, le);
197
0
  } else {
198
0
    sc_format_apdu_ex(&apdu, 0x00, 0x88, 0, 0, sbuf, datalen, out, le);
199
0
  }
200
201
0
  SC_TRANSMIT_TEST_RET(card, apdu, "PSO CDS/INTERNAL AUTHENTICATE failed");
202
203
0
  LOG_FUNC_RETURN(card->ctx, (int)apdu.resplen);
204
0
}
205
206
0
static int esteid_get_pin_remaining_tries(sc_card_t *card, int pin_reference) {
207
0
  const u8 get_pin_info[] = {0x4D, 0x08, 0x70, 0x06, 0xBF, 0x81, pin_reference & 0x0F, 0x02, 0xA0, 0x80}; // mask out local/global
208
0
  struct sc_apdu apdu;
209
0
  u8 apdu_resp[SC_MAX_APDU_RESP_SIZE];
210
0
  LOG_FUNC_CALLED(card->ctx);
211
212
  // We don't get the file information here, so we need to be ugly
213
0
  if (pin_reference == PIN1_REF || pin_reference == PUK_REF) {
214
0
    LOG_TEST_RET(card->ctx, esteid_select_file(card, &MF, NULL), "Cannot select MF");
215
0
  } else if (pin_reference == PIN2_REF) {
216
0
    LOG_TEST_RET(card->ctx, esteid_select_file(card, &adf2, NULL), "Cannot select QSCD AID");
217
0
  } else {
218
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
219
0
  }
220
221
0
  sc_format_apdu_ex(&apdu, 0x00, 0xCB, 0x3F, 0xFF, get_pin_info, sizeof(get_pin_info), apdu_resp, sizeof(apdu_resp));
222
0
  SC_TRANSMIT_TEST_RET(card, apdu, "GET DATA(pin info) failed");
223
0
  if (apdu.resplen < 32) {
224
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
225
0
  }
226
227
  // XXX: sc_asn1_find_tag with the following payload (to get to tag 0x9B):
228
  // https://lapo.it/asn1js/#cB6_gQEaoBiaAQObAQOhEIwG8wAAc0MAnAbzAABzQwA
229
0
  return (int)apdu_resp[13];
230
0
}
231
232
0
static int esteid_pin_cmd(sc_card_t *card, struct sc_pin_cmd_data *data) {
233
0
  int r;
234
0
  struct sc_pin_cmd_data tmp;
235
0
  LOG_FUNC_CALLED(card->ctx);
236
0
  sc_log(card->ctx, "PIN CMD is %d", data->cmd);
237
0
  if (data->cmd == SC_PIN_CMD_GET_INFO) {
238
0
    sc_log(card->ctx, "SC_PIN_CMD_GET_INFO for %d", data->pin_reference);
239
0
    r = esteid_get_pin_remaining_tries(card, data->pin_reference);
240
0
    LOG_TEST_RET(card->ctx, r, "GET DATA(pin info) failed");
241
242
0
    data->pin1.tries_left = r;
243
0
    data->pin1.max_tries = -1; // "no support, which means the one set in PKCS#15 emulation sticks
244
0
    data->pin1.logged_in = SC_PIN_STATE_UNKNOWN;
245
0
    LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
246
0
  } else if (data->cmd == SC_PIN_CMD_UNBLOCK) {
247
    // Verify PUK, then issue UNBLOCK
248
    // VERIFY
249
0
    tmp = *data;
250
0
    tmp.cmd = SC_PIN_CMD_VERIFY;
251
0
    tmp.pin_reference = PUK_REF;
252
0
    tmp.pin2.len = 0;
253
0
    r = iso_ops->pin_cmd(card, &tmp);
254
0
    LOG_TEST_RET(card->ctx, r, "VERIFY during unblock failed");
255
256
0
    if (data->pin_reference == PIN2_REF) {
257
0
      LOG_TEST_RET(card->ctx, esteid_select_file(card, &adf2, NULL), "Cannot select QSCD AID");
258
0
    }
259
    // UNBLOCK
260
0
    tmp = *data;
261
0
    tmp.cmd = SC_PIN_CMD_UNBLOCK;
262
0
    tmp.pin1.len = 0;
263
0
    r = iso_ops->pin_cmd(card, &tmp);
264
0
    sc_mem_clear(&tmp, sizeof(tmp));
265
0
    LOG_FUNC_RETURN(card->ctx, r);
266
0
  }
267
268
0
  LOG_FUNC_RETURN(card->ctx, iso_ops->pin_cmd(card, data));
269
0
}
270
271
61
static int esteid_init(sc_card_t *card) {
272
61
  unsigned long flags, ext_flags;
273
61
  struct esteid_priv_data *priv;
274
275
61
  priv = calloc(1, sizeof *priv);
276
61
  if (!priv)
277
61
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
278
61
  card->drv_data = priv;
279
61
  card->max_recv_size = 233; // XXX: empirical, not documented
280
  // Workaround for the 2018 v2 card, with reader Alcor Micro AU9540
281
61
  if (card->type == SC_CARD_TYPE_ESTEID_2018_V2_2025) {
282
0
    card->max_recv_size = 0xC0;
283
0
  }
284
285
61
  flags = SC_ALGORITHM_ECDSA_RAW | SC_ALGORITHM_ECDH_CDH_RAW | SC_ALGORITHM_ECDSA_HASH_NONE;
286
61
  ext_flags = SC_ALGORITHM_EXT_EC_NAMEDCURVE | SC_ALGORITHM_EXT_EC_UNCOMPRESES;
287
288
61
  _sc_card_add_ec_alg(card, 384, flags, ext_flags, NULL);
289
290
61
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
291
61
}
292
293
61
static int esteid_finish(sc_card_t *card) {
294
61
  if (card != NULL)
295
61
    free(DRVDATA(card));
296
61
  return 0;
297
61
}
298
299
0
static int esteid_logout(sc_card_t *card) {
300
0
  return gp_select_aid(card, &IASECC_AID);
301
0
}
302
303
14.4k
struct sc_card_driver *sc_get_esteid2018_driver(void) {
304
14.4k
  struct sc_card_driver *iso_drv = sc_get_iso7816_driver();
305
306
14.4k
  if (iso_ops == NULL)
307
1
    iso_ops = iso_drv->ops;
308
309
14.4k
  esteid_ops = *iso_drv->ops;
310
14.4k
  esteid_ops.match_card = esteid_match_card;
311
14.4k
  esteid_ops.init = esteid_init;
312
14.4k
  esteid_ops.finish = esteid_finish;
313
314
14.4k
  esteid_ops.select_file = esteid_select_file;
315
316
14.4k
  esteid_ops.set_security_env = esteid_set_security_env;
317
14.4k
  esteid_ops.compute_signature = esteid_compute_signature;
318
14.4k
  esteid_ops.pin_cmd = esteid_pin_cmd;
319
14.4k
  esteid_ops.logout = esteid_logout;
320
321
14.4k
  return &esteid2018_driver;
322
14.4k
}