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