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