/src/opensc/src/libopensc/card-sc-hsm.c
Line | Count | Source |
1 | | /* |
2 | | * card-sc-hsm.c |
3 | | * |
4 | | * Driver for the SmartCard-HSM, a light-weight hardware security module |
5 | | * |
6 | | * Copyright (C) 2012 Andreas Schwier, CardContact, Minden, Germany, and others |
7 | | * Copyright (C) 2018-2019 GSMK - Gesellschaft für Sichere Mobile Kommunikation mbH |
8 | | * |
9 | | * This library is free software; you can redistribute it and/or |
10 | | * modify it under the terms of the GNU Lesser General Public |
11 | | * License as published by the Free Software Foundation; either |
12 | | * version 2.1 of the License, or (at your option) any later version. |
13 | | * |
14 | | * This library is distributed in the hope that it will be useful, |
15 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | | * Lesser General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public |
20 | | * License along with this library; if not, write to the Free Software |
21 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | | */ |
23 | | |
24 | | #ifdef HAVE_CONFIG_H |
25 | | #include "config.h" |
26 | | #endif |
27 | | |
28 | | #include <string.h> |
29 | | #include <stdlib.h> |
30 | | #include <ctype.h> |
31 | | |
32 | | #include "internal.h" |
33 | | #include "asn1.h" |
34 | | #include "cardctl.h" |
35 | | #include "types.h" |
36 | | |
37 | | #include "card-sc-hsm.h" |
38 | | |
39 | | #if defined(ENABLE_SM) && defined(ENABLE_OPENPACE) |
40 | | #include "sm/sm-eac.h" |
41 | | #include <eac/cv_cert.h> |
42 | | #include <eac/eac.h> |
43 | | #include <eac/ta.h> |
44 | | #include <openssl/evp.h> |
45 | | #endif |
46 | | |
47 | | |
48 | | /* Static reference to ISO driver */ |
49 | | static const struct sc_card_operations *iso_ops = NULL; |
50 | | |
51 | | /* Our operations */ |
52 | | static struct sc_card_operations sc_hsm_ops; |
53 | | |
54 | | /* Our driver description */ |
55 | | static struct sc_card_driver sc_hsm_drv = { |
56 | | "SmartCard-HSM", |
57 | | "sc-hsm", |
58 | | &sc_hsm_ops, |
59 | | NULL, |
60 | | 0, |
61 | | NULL |
62 | | }; |
63 | | |
64 | | |
65 | | |
66 | | /* Our AID */ |
67 | | struct sc_aid sc_hsm_aid = { { 0xE8,0x2B,0x06,0x01,0x04,0x01,0x81,0xC3,0x1F,0x02,0x01 }, 11 }; |
68 | | |
69 | | |
70 | | |
71 | | /* Known ATRs for SmartCard-HSMs */ |
72 | | const struct sc_atr_table sc_hsm_atrs[] = { |
73 | | /* standard version */ |
74 | | {"3B:FE:18:00:00:81:31:FE:45:80:31:81:54:48:53:4D:31:73:80:21:40:81:07:FA", NULL, NULL, SC_CARD_TYPE_SC_HSM, 0, NULL}, |
75 | | {"3B:8E:80:01:80:31:81:54:48:53:4D:31:73:80:21:40:81:07:18", NULL, NULL, SC_CARD_TYPE_SC_HSM, 0, NULL}, |
76 | | {"3B:DE:18:FF:81:91:FE:1F:C3:80:31:81:54:48:53:4D:31:73:80:21:40:81:07:1C", NULL, NULL, SC_CARD_TYPE_SC_HSM, 0, NULL}, |
77 | | {"3B:DE:96:FF:81:91:FE:1F:C3:80:31:81:54:48:53:4D:31:73:80:21:40:81:07:92", NULL, NULL, SC_CARD_TYPE_SC_HSM, 0, NULL}, |
78 | | |
79 | | {"3B:80:80:01:01", NULL, NULL, SC_CARD_TYPE_SC_HSM_SOC, 0, NULL}, // SoC Sample Card |
80 | | { |
81 | | "3B:84:80:01:47:6f:49:44:00", |
82 | | "FF:FF:FF:FF:FF:FF:FF:FF:00", |
83 | | "GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL |
84 | | }, |
85 | | { |
86 | | "3B:85:80:01:47:6f:49:44:00:00", |
87 | | "FF:FF:FF:FF:FF:FF:FF:FF:00:00", |
88 | | "GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL |
89 | | }, |
90 | | { |
91 | | "3B:86:80:01:47:6f:49:44:00:00:00", |
92 | | "FF:FF:FF:FF:FF:FF:FF:FF:00:00:00", |
93 | | "GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL |
94 | | }, |
95 | | { |
96 | | "3B:87:80:01:47:6f:49:44:00:00:00:00", |
97 | | "FF:FF:FF:FF:FF:FF:FF:FF:00:00:00:00", |
98 | | "GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL |
99 | | }, |
100 | | { |
101 | | "3B:88:80:01:47:6f:49:44:00:00:00:00:00", |
102 | | "FF:FF:FF:FF:FF:FF:FF:FF:00:00:00:00:00", |
103 | | "GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL |
104 | | }, |
105 | | { |
106 | | "3B:89:80:01:47:6f:49:44:00:00:00:00:00:00", |
107 | | "FF:FF:FF:FF:FF:FF:FF:FF:00:00:00:00:00:00", |
108 | | "GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL |
109 | | }, |
110 | | { |
111 | | "3B:8a:80:01:47:6f:49:44:00:00:00:00:00:00:00", |
112 | | "FF:FF:FF:FF:FF:FF:FF:FF:00:00:00:00:00:00:00", |
113 | | "GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL |
114 | | }, |
115 | | { |
116 | | "3B:8b:80:01:47:6f:49:44:00:00:00:00:00:00:00:00", |
117 | | "FF:FF:FF:FF:FF:FF:FF:FF:00:00:00:00:00:00:00:00", |
118 | | "GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL |
119 | | }, |
120 | | { |
121 | | "3B:8c:80:01:47:6f:49:44:00:00:00:00:00:00:00:00:00", |
122 | | "FF:FF:FF:FF:FF:FF:FF:FF:00:00:00:00:00:00:00:00:00", |
123 | | "GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL |
124 | | }, |
125 | | { |
126 | | "3B:8d:80:01:47:6f:49:44:00:00:00:00:00:00:00:00:00:00", |
127 | | "FF:FF:FF:FF:FF:FF:FF:FF:00:00:00:00:00:00:00:00:00:00", |
128 | | "GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL |
129 | | }, |
130 | | { |
131 | | "3B:8e:80:01:47:6f:49:44:00:00:00:00:00:00:00:00:00:00:00", |
132 | | "FF:FF:FF:FF:FF:FF:FF:FF:00:00:00:00:00:00:00:00:00:00:00", |
133 | | "GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL |
134 | | }, |
135 | | { |
136 | | "3B:8f:80:01:47:6f:49:44:00:00:00:00:00:00:00:00:00:00:00:00", |
137 | | "FF:FF:FF:FF:FF:FF:FF:FF:00:00:00:00:00:00:00:00:00:00:00:00", |
138 | | "GoID", SC_CARD_TYPE_SC_HSM_GOID, 0, NULL |
139 | | }, |
140 | | {NULL, NULL, NULL, 0, 0, NULL} |
141 | | }; |
142 | | |
143 | | |
144 | | |
145 | | static int sc_hsm_select_file_ex(sc_card_t *card, |
146 | | const sc_path_t *in_path, int forceselect, |
147 | | sc_file_t **file_out) |
148 | 103k | { |
149 | 103k | int rv; |
150 | 103k | sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data; |
151 | 103k | sc_file_t *file = NULL; |
152 | 103k | sc_path_t cpath; |
153 | 103k | size_t card_max_recv_size = card->max_recv_size; |
154 | 103k | size_t reader_max_recv_size = card->reader->max_recv_size; |
155 | | |
156 | 103k | if (file_out == NULL) { // Versions before 0.16 of the SmartCard-HSM do not support P2='0C' |
157 | 5.48k | rv = sc_hsm_select_file_ex(card, in_path, forceselect, &file); |
158 | 5.48k | sc_file_free(file); |
159 | 5.48k | return rv; |
160 | 5.48k | } |
161 | | |
162 | 97.9k | if ((in_path->type == SC_PATH_TYPE_FILE_ID) && in_path->aid.len) { |
163 | | // Split applet selection and file selection into two separate calls |
164 | 0 | cpath = *in_path; |
165 | 0 | cpath.len = 0; |
166 | 0 | cpath.type = SC_PATH_TYPE_DF_NAME; |
167 | 0 | rv = sc_hsm_select_file_ex(card, &cpath, forceselect, NULL); |
168 | 0 | LOG_TEST_RET(card->ctx, rv, "Could not select SmartCard-HSM application"); |
169 | | |
170 | 0 | if (in_path->len) { |
171 | 0 | cpath = *in_path; |
172 | 0 | cpath.aid.len = 0; |
173 | 0 | rv = sc_hsm_select_file_ex(card, &cpath, forceselect, file_out); |
174 | 0 | } |
175 | 0 | return rv; |
176 | 0 | } |
177 | | |
178 | | // Prevent selection of applet unless this is the first time, selection is forced or the device is not authenticated |
179 | 97.9k | if (in_path->type == SC_PATH_TYPE_DF_NAME |
180 | 57.6k | || (in_path->type == SC_PATH_TYPE_PATH |
181 | 55.1k | && in_path->len == sc_hsm_aid.len |
182 | 0 | && !memcmp(in_path->value, sc_hsm_aid.value, sc_hsm_aid.len)) |
183 | 57.6k | || (in_path->type == SC_PATH_TYPE_PATH |
184 | 55.1k | && in_path->len == 0 |
185 | 12.6k | && in_path->aid.len == sc_hsm_aid.len |
186 | 40.3k | && !memcmp(in_path->aid.value, sc_hsm_aid.value, sc_hsm_aid.len))) { |
187 | 40.3k | if (!priv || (priv->dffcp == NULL) || forceselect) { |
188 | | /* Force use of Le = 0x00 in iso7816_select_file as required by SC-HSM */ |
189 | 39.0k | card->max_recv_size = card->reader->max_recv_size = SC_READER_SHORT_APDU_MAX_RECV_SIZE; |
190 | 39.0k | rv = (*iso_ops->select_file)(card, in_path, file_out); |
191 | 39.0k | card->max_recv_size = card_max_recv_size; |
192 | 39.0k | card->reader->max_recv_size = reader_max_recv_size; |
193 | 39.0k | LOG_TEST_RET(card->ctx, rv, "Could not select SmartCard-HSM application"); |
194 | | |
195 | 2.50k | if (priv) { |
196 | 791 | sc_file_free(priv->dffcp); |
197 | | // Cache the FCP returned when selecting the applet |
198 | 791 | sc_file_dup(&priv->dffcp, *file_out); |
199 | 791 | } |
200 | 2.50k | } else { |
201 | 1.23k | sc_file_dup(file_out, priv->dffcp); |
202 | 1.23k | rv = SC_SUCCESS; |
203 | 1.23k | } |
204 | 3.73k | return rv; |
205 | 40.3k | } |
206 | | |
207 | 57.6k | if ((in_path->len >= 2) && (in_path->value[0] == 0x3F) && (in_path->value[1] == 0x00)) { |
208 | | // The SmartCard-HSM is an applet that is not default selected. Simulate selection of the MF |
209 | 42.8k | if (in_path->len == 2) { |
210 | 1.61k | file = sc_file_new(); |
211 | 1.61k | if (file == NULL) |
212 | 1.61k | LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY); |
213 | 1.61k | file->path = *in_path; |
214 | 1.61k | file->id = 0x3F00; |
215 | 1.61k | file->type = SC_FILE_TYPE_DF; |
216 | 1.61k | file->magic = SC_FILE_MAGIC; |
217 | | |
218 | 1.61k | *file_out = file; |
219 | 1.61k | return SC_SUCCESS; |
220 | 41.1k | } else { |
221 | | /* Force use of Le = 0x00 in iso7816_select_file as required by SC-HSM */ |
222 | 41.1k | card->max_recv_size = card->reader->max_recv_size = SC_READER_SHORT_APDU_MAX_RECV_SIZE; |
223 | 41.1k | sc_path_t truncated; |
224 | 41.1k | memcpy(&truncated, in_path, sizeof truncated); |
225 | 41.1k | truncated.len = in_path->len - 2; |
226 | 41.1k | memcpy(truncated.value, in_path->value+2, truncated.len); |
227 | 41.1k | rv = (*iso_ops->select_file)(card, &truncated, file_out); |
228 | 41.1k | card->max_recv_size = card_max_recv_size; |
229 | 41.1k | card->reader->max_recv_size = reader_max_recv_size; |
230 | 41.1k | return rv; |
231 | 41.1k | } |
232 | 42.8k | } |
233 | | /* Force use of Le = 0x00 in iso7816_select_file as required by SC-HSM */ |
234 | 14.8k | card->max_recv_size = card->reader->max_recv_size = SC_READER_SHORT_APDU_MAX_RECV_SIZE; |
235 | 14.8k | rv = (*iso_ops->select_file)(card, in_path, file_out); |
236 | 14.8k | card->max_recv_size = card_max_recv_size; |
237 | 14.8k | card->reader->max_recv_size = reader_max_recv_size; |
238 | 14.8k | return rv; |
239 | 57.6k | } |
240 | | |
241 | | |
242 | | |
243 | | static int sc_hsm_select_file(sc_card_t *card, |
244 | | const sc_path_t *in_path, |
245 | | sc_file_t **file_out) |
246 | 96.2k | { |
247 | 96.2k | return sc_hsm_select_file_ex(card, in_path, 0, file_out); |
248 | 96.2k | } |
249 | | |
250 | | |
251 | | |
252 | | static int sc_hsm_get_challenge(struct sc_card *card, unsigned char *rnd, size_t len) |
253 | 521 | { |
254 | 521 | LOG_FUNC_CALLED(card->ctx); |
255 | | |
256 | 521 | if (len > 1024) { |
257 | 0 | len = 1024; |
258 | 0 | } |
259 | | |
260 | 521 | LOG_FUNC_RETURN(card->ctx, iso_ops->get_challenge(card, rnd, len)); |
261 | 521 | } |
262 | | |
263 | | |
264 | | |
265 | | static int sc_hsm_match_card(struct sc_card *card) |
266 | 36.3k | { |
267 | 36.3k | sc_path_t path; |
268 | 36.3k | int i, r, type = 0; |
269 | 36.3k | sc_file_t *file = NULL; |
270 | | |
271 | 36.3k | i = _sc_match_atr(card, sc_hsm_atrs, &type); |
272 | 36.3k | if (i >= 0 && type != SC_CARD_TYPE_SC_HSM_SOC) { |
273 | 1.67k | card->type = type; |
274 | 1.67k | return 1; |
275 | 1.67k | } |
276 | | |
277 | 34.7k | sc_path_set(&path, SC_PATH_TYPE_DF_NAME, sc_hsm_aid.value, sc_hsm_aid.len, 0, 0); |
278 | 34.7k | r = sc_hsm_select_file(card, &path, &file); |
279 | 34.7k | LOG_TEST_RET(card->ctx, r, "Could not select SmartCard-HSM application"); |
280 | | |
281 | | // Validate that card returns a FCP with a proprietary tag 85 with value longer than 2 byte (Fixes #1377) |
282 | 1.71k | if (file != NULL) { |
283 | 1.71k | size_t sz = file->prop_attr_len; |
284 | 1.71k | sc_file_free(file); |
285 | 1.71k | if (sz < 2) { |
286 | 1.68k | return 0; |
287 | 1.68k | } |
288 | 1.71k | } |
289 | | |
290 | 29 | if (type == SC_CARD_TYPE_SC_HSM_SOC) { |
291 | 1 | card->type = SC_CARD_TYPE_SC_HSM_SOC; |
292 | 28 | } else { |
293 | 28 | card->type = SC_CARD_TYPE_SC_HSM; |
294 | 28 | } |
295 | | |
296 | 29 | return 1; |
297 | 1.71k | } |
298 | | |
299 | | |
300 | | |
301 | | /* |
302 | | * Encode 16 hexadecimals of SO-PIN into binary form |
303 | | * Caller must check length of sopin and provide an 8 byte buffer |
304 | | */ |
305 | | static int sc_hsm_encode_sopin(const u8 *sopin, u8 *sopinbin) |
306 | 0 | { |
307 | 0 | int i; |
308 | 0 | unsigned char digit; |
309 | |
|
310 | 0 | memset(sopinbin, 0, 8); |
311 | 0 | for (i = 0; i < 16; i++) { |
312 | 0 | *sopinbin <<= 4; |
313 | 0 | digit = *sopin++; |
314 | |
|
315 | 0 | if (!isxdigit(digit)) |
316 | 0 | return SC_ERROR_PIN_CODE_INCORRECT; |
317 | 0 | digit = toupper(digit); |
318 | |
|
319 | 0 | if (digit >= 'A') |
320 | 0 | digit = digit - 'A' + 10; |
321 | 0 | else |
322 | 0 | digit = digit & 0xF; |
323 | |
|
324 | 0 | *sopinbin |= digit & 0xf; |
325 | 0 | if (i & 1) |
326 | 0 | sopinbin++; |
327 | 0 | } |
328 | 0 | return SC_SUCCESS; |
329 | 0 | } |
330 | | |
331 | | |
332 | | static int sc_hsm_soc_select_minbioclient(sc_card_t *card) |
333 | 0 | { |
334 | 0 | static const struct sc_aid minBioClient_aid = { |
335 | 0 | {0xFF, 'm', 'i', 'n', 'B', 'i', 'o', 'C', 'l', 'i', 'e', 'n', 't', 0x01}, |
336 | 0 | 14 |
337 | 0 | }; |
338 | | |
339 | | /* Select MinBioClient */ |
340 | 0 | #ifdef ENABLE_SM |
341 | 0 | sc_sm_stop(card); |
342 | 0 | #endif |
343 | 0 | return iso7816_select_aid(card, minBioClient_aid.value, minBioClient_aid.len, NULL, NULL); |
344 | 0 | } |
345 | | |
346 | | static int sc_hsm_soc_change(sc_card_t *card, struct sc_pin_cmd_data *data, |
347 | | int *tries_left) |
348 | 0 | { |
349 | 0 | sc_apdu_t apdu; |
350 | 0 | sc_path_t path; |
351 | 0 | int r; |
352 | |
|
353 | 0 | if (card->type == SC_CARD_TYPE_SC_HSM_SOC) { |
354 | | /* Select MinBioClient */ |
355 | 0 | r = sc_hsm_soc_select_minbioclient(card); |
356 | 0 | LOG_TEST_RET(card->ctx, r, "Could not select MinBioClient application"); |
357 | | |
358 | | /* verify PIN */ |
359 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x20, 0x00, 0x80); |
360 | 0 | r = sc_transmit_apdu(card, &apdu); |
361 | 0 | LOG_TEST_GOTO_ERR(card->ctx, r, "APDU transmit failed"); |
362 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
363 | 0 | LOG_TEST_GOTO_ERR(card->ctx, r, "Could not verify PIN"); |
364 | | |
365 | | /* change PIN */ |
366 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x24, 0x01, 0x80); |
367 | 0 | r = sc_transmit_apdu(card, &apdu); |
368 | 0 | LOG_TEST_GOTO_ERR(card->ctx, r, "APDU transmit failed"); |
369 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
370 | 0 | LOG_TEST_GOTO_ERR(card->ctx, r, "Could not change PIN"); |
371 | 0 | } else { |
372 | 0 | #ifdef ENABLE_SM |
373 | 0 | unsigned sm_mode = card->sm_ctx.sm_mode; |
374 | 0 | #endif |
375 | | |
376 | | /* verify PIN */ |
377 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x20, 0x00, 0x85); |
378 | 0 | apdu.cla = 0x80; |
379 | 0 | r = sc_transmit_apdu(card, &apdu); |
380 | 0 | LOG_TEST_GOTO_ERR(card->ctx, r, "APDU transmit failed"); |
381 | | |
382 | 0 | #ifdef ENABLE_SM |
383 | | /* temporary disable SM, change reference data does not reach the applet */ |
384 | 0 | card->sm_ctx.sm_mode = SM_MODE_NONE; |
385 | 0 | #endif |
386 | | |
387 | | /* change PIN */ |
388 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x24, 0x01, 0x85); |
389 | 0 | apdu.cla = 0x80; |
390 | 0 | r = sc_transmit_apdu(card, &apdu); |
391 | 0 | #ifdef ENABLE_SM |
392 | | /* restore SM if possible */ |
393 | 0 | card->sm_ctx.sm_mode = sm_mode; |
394 | 0 | #endif |
395 | 0 | LOG_TEST_GOTO_ERR(card->ctx, r, "APDU transmit failed"); |
396 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
397 | 0 | LOG_TEST_GOTO_ERR(card->ctx, r, "Could not change PIN"); |
398 | 0 | } |
399 | | |
400 | 0 | err: |
401 | 0 | if (card->type == SC_CARD_TYPE_SC_HSM_SOC) { |
402 | | /* Select SC-HSM */ |
403 | 0 | sc_path_set(&path, SC_PATH_TYPE_DF_NAME, |
404 | 0 | sc_hsm_aid.value, sc_hsm_aid.len, 0, 0); |
405 | 0 | LOG_TEST_RET(card->ctx, |
406 | 0 | sc_hsm_select_file_ex(card, &path, 1, NULL), |
407 | 0 | "Could not select SmartCard-HSM application"); |
408 | 0 | } |
409 | | |
410 | 0 | return r; |
411 | 0 | } |
412 | | |
413 | | static int sc_hsm_soc_unblock(sc_card_t *card, struct sc_pin_cmd_data *data, |
414 | | int *tries_left) |
415 | 0 | { |
416 | 0 | sc_apdu_t apdu; |
417 | 0 | sc_path_t path; |
418 | 0 | int r; |
419 | |
|
420 | 0 | if (card->type == SC_CARD_TYPE_SC_HSM_GOID) { |
421 | 0 | return SC_ERROR_NOT_SUPPORTED; |
422 | 0 | } |
423 | | |
424 | | /* Select MinBioClient */ |
425 | 0 | r = sc_hsm_soc_select_minbioclient(card); |
426 | 0 | LOG_TEST_RET(card->ctx, r, "Could not select MinBioClient application"); |
427 | | |
428 | | /* verify PUK */ |
429 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x20, 0x00, 0x81); |
430 | 0 | r = sc_transmit_apdu(card, &apdu); |
431 | 0 | LOG_TEST_GOTO_ERR(card->ctx, r, "APDU transmit failed"); |
432 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
433 | 0 | LOG_TEST_GOTO_ERR(card->ctx, r, "Could not verify PUK"); |
434 | | |
435 | | /* reset retry counter */ |
436 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x2c, 0x03, 0x00); |
437 | 0 | r = sc_transmit_apdu(card, &apdu); |
438 | 0 | LOG_TEST_GOTO_ERR(card->ctx, r, "APDU transmit failed"); |
439 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
440 | 0 | LOG_TEST_GOTO_ERR(card->ctx, r, "Could not unblock PIN"); |
441 | | |
442 | 0 | err: |
443 | | /* Select SC-HSM */ |
444 | 0 | sc_path_set(&path, SC_PATH_TYPE_DF_NAME, sc_hsm_aid.value, sc_hsm_aid.len, 0, 0); |
445 | 0 | LOG_TEST_RET(card->ctx, |
446 | 0 | sc_hsm_select_file_ex(card, &path, 1, NULL), |
447 | 0 | "Could not select SmartCard-HSM application"); |
448 | | |
449 | 0 | return r; |
450 | 0 | } |
451 | | |
452 | | static int sc_hsm_soc_biomatch(sc_card_t *card, struct sc_pin_cmd_data *data, |
453 | | int *tries_left) |
454 | 0 | { |
455 | 0 | sc_apdu_t apdu; |
456 | 0 | u8 rbuf[SC_MAX_APDU_BUFFER_SIZE]; |
457 | 0 | int r; |
458 | |
|
459 | 0 | if (card->type == SC_CARD_TYPE_SC_HSM_SOC) { |
460 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x20, 0x00, 0x85); |
461 | 0 | apdu.cla = 0x80; |
462 | 0 | apdu.data = (unsigned char*)"\x7F\x24\x00"; |
463 | 0 | apdu.datalen = 3; |
464 | 0 | apdu.lc = 3; |
465 | 0 | apdu.resplen = 0; |
466 | |
|
467 | 0 | r = sc_transmit_apdu(card, &apdu); |
468 | 0 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
469 | | /* ignore the actual status bytes */ |
470 | 0 | } |
471 | | |
472 | | /* JCOP's SM accelerator is incapable of using case 1 APDU in SM */ |
473 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_2, 0x20, 0x00, 0x81); |
474 | 0 | if (card->type == SC_CARD_TYPE_SC_HSM_GOID) { |
475 | 0 | apdu.cla = 0x80; |
476 | 0 | } |
477 | 0 | apdu.resp = rbuf; |
478 | 0 | apdu.resplen = sizeof rbuf; |
479 | 0 | r = sc_transmit_apdu(card, &apdu); |
480 | 0 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
481 | | |
482 | | /* now check the status bytes */ |
483 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
484 | 0 | if (r == SC_SUCCESS) { |
485 | 0 | LOG_FUNC_RETURN(card->ctx, r); |
486 | 0 | } |
487 | | |
488 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_PIN_CODE_INCORRECT); |
489 | 0 | } |
490 | | |
491 | | #if defined(ENABLE_SM) && defined(ENABLE_OPENPACE) |
492 | | |
493 | | static int sc_hsm_perform_chip_authentication(sc_card_t *card) |
494 | 43 | { |
495 | 43 | int r, protocol; |
496 | 43 | sc_path_t path; |
497 | 43 | u8 all_certs[1024]; |
498 | 43 | EAC_CTX *ctx = NULL; |
499 | 43 | size_t all_certs_len = sizeof all_certs, left, device_cert_len, issuer_cert_len; |
500 | 43 | const unsigned char *cert = all_certs, *device_cert, *issuer_cert; |
501 | 43 | BUF_MEM *comp_pub_key = NULL; |
502 | 43 | sc_cvc_t cvc_device, cvc_issuer; |
503 | | /* this is only needed to call sc_pkcs15emu_sc_hsm_decode_cvc */ |
504 | 43 | sc_pkcs15_card_t p15card; |
505 | 43 | sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data; |
506 | | /* we know that sc_pkcs15emu_sc_hsm_decode_cvc does not require anything |
507 | | * else to be initialized than p15card->card */ |
508 | 43 | p15card.card = card; |
509 | | |
510 | 43 | memset(&cvc_device, 0, sizeof(cvc_device)); |
511 | 43 | memset(&cvc_issuer, 0, sizeof(cvc_issuer)); |
512 | | |
513 | | |
514 | 43 | if (priv->EF_C_DevAut && priv->EF_C_DevAut_len) { |
515 | 5 | all_certs_len = priv->EF_C_DevAut_len; |
516 | 5 | cert = priv->EF_C_DevAut; |
517 | 38 | } else { |
518 | | /* get issuer and device certificate from the card */ |
519 | 38 | r = sc_path_set(&path, SC_PATH_TYPE_FILE_ID, (u8 *) "\x2F\x02", 2, 0, 0); |
520 | 38 | if (r < 0) |
521 | 0 | goto err; |
522 | 38 | r = sc_select_file(card, &path, NULL); |
523 | 38 | if (r < 0) |
524 | 20 | goto err; |
525 | 18 | r = sc_read_binary(card, 0, all_certs, all_certs_len, 0); |
526 | 18 | if (r < 0) |
527 | 4 | goto err; |
528 | 14 | if (r == 0) { |
529 | 5 | r = SC_ERROR_FILE_NOT_FOUND; |
530 | 5 | goto err; |
531 | 5 | } |
532 | | |
533 | 9 | all_certs_len = r; |
534 | | |
535 | | /* save EF_C_DevAut for further use */ |
536 | 9 | cert = realloc(priv->EF_C_DevAut, all_certs_len); |
537 | 9 | if (cert) { |
538 | 9 | memcpy((unsigned char *) cert, all_certs, all_certs_len); |
539 | 9 | priv->EF_C_DevAut = (unsigned char *) cert; |
540 | 9 | priv->EF_C_DevAut_len = all_certs_len; |
541 | 9 | } |
542 | | |
543 | 9 | cert = all_certs; |
544 | 9 | } |
545 | 14 | left = all_certs_len; |
546 | | |
547 | 14 | device_cert = cert; |
548 | 14 | r = sc_pkcs15emu_sc_hsm_decode_cvc(&p15card, &cert, &left, &cvc_device); |
549 | 14 | if (r < 0) |
550 | 14 | goto err; |
551 | 0 | device_cert_len = all_certs_len - left; |
552 | |
|
553 | 0 | issuer_cert = cert; |
554 | 0 | r = sc_pkcs15emu_sc_hsm_decode_cvc(&p15card, &cert, &left, &cvc_issuer); |
555 | 0 | if (r < 0) |
556 | 0 | goto err; |
557 | 0 | issuer_cert_len = all_certs_len - device_cert_len - left; |
558 | |
|
559 | 0 | ctx = EAC_CTX_new(); |
560 | 0 | if (!ctx) { |
561 | 0 | r = SC_ERROR_INTERNAL; |
562 | 0 | goto err; |
563 | 0 | } |
564 | | |
565 | | |
566 | | /* check all CVCs given of the document's pki */ |
567 | 0 | if (!TA_STEP2_import_certificate(ctx, issuer_cert, issuer_cert_len) |
568 | 0 | || !TA_STEP2_import_certificate(ctx, device_cert, device_cert_len)) { |
569 | 0 | r = SC_ERROR_INTERNAL; |
570 | 0 | goto err; |
571 | 0 | } |
572 | | |
573 | | /* XXX on older JCOPs only NID_id_CA_ECDH_3DES_CBC_CBC may be |
574 | | * supported. The card does not export its capabilities. We hardcode |
575 | | * NID_id_CA_ECDH_AES_CBC_CMAC_128 here, because we don't have the older |
576 | | * cards in production. */ |
577 | 0 | protocol = NID_id_CA_ECDH_AES_CBC_CMAC_128; |
578 | | |
579 | | /* initialize CA domain parameter with the document's public key */ |
580 | 0 | if (!EAC_CTX_init_ca(ctx, protocol, 8)) { |
581 | 0 | r = SC_ERROR_INTERNAL; |
582 | 0 | goto err; |
583 | 0 | } |
584 | 0 | EVP_PKEY_free(ctx->ca_ctx->ka_ctx->key); |
585 | 0 | if (!EVP_PKEY_up_ref(ctx->ta_ctx->pub_key)) { |
586 | 0 | sc_log_openssl(card->ctx); |
587 | 0 | r = SC_ERROR_INTERNAL; |
588 | 0 | goto err; |
589 | 0 | } |
590 | 0 | ctx->ca_ctx->ka_ctx->key = ctx->ta_ctx->pub_key; |
591 | | |
592 | | /* generate keys for CA */ |
593 | 0 | comp_pub_key = TA_STEP3_generate_ephemeral_key(ctx); |
594 | 0 | r = perform_chip_authentication_ex(card, ctx, |
595 | 0 | cvc_device.publicPoint, cvc_device.publicPointlen); |
596 | |
|
597 | 43 | err: |
598 | 43 | if (r < 0) |
599 | 43 | EAC_CTX_clear_free(ctx); |
600 | 43 | if (comp_pub_key) |
601 | 0 | BUF_MEM_free(comp_pub_key); |
602 | 43 | sc_pkcs15emu_sc_hsm_free_cvc(&cvc_device); |
603 | 43 | sc_pkcs15emu_sc_hsm_free_cvc(&cvc_issuer); |
604 | | |
605 | 43 | return r; |
606 | 0 | } |
607 | | |
608 | | #else |
609 | | |
610 | | static int sc_hsm_perform_chip_authentication(sc_card_t *card) |
611 | | { |
612 | | return SC_ERROR_NOT_SUPPORTED; |
613 | | } |
614 | | #endif |
615 | | |
616 | | |
617 | | |
618 | | static int sc_hsm_pin_cmd(sc_card_t *card, struct sc_pin_cmd_data *data, |
619 | | int *tries_left) |
620 | 692 | { |
621 | 692 | sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data; |
622 | 692 | sc_apdu_t apdu; |
623 | 692 | u8 cmdbuff[16]; |
624 | 692 | #ifdef ENABLE_SM |
625 | 692 | u8 rbuf[SC_MAX_APDU_BUFFER_SIZE]; |
626 | 692 | #endif |
627 | 692 | int r; |
628 | 692 | int cmd = data->cmd; |
629 | 692 | size_t pin2_len = data->pin2.len; |
630 | | |
631 | 692 | if (cmd == SC_PIN_CMD_GET_SESSION_PIN) { |
632 | | /* First, perform a standard VERIFY */ |
633 | 0 | data->cmd = SC_PIN_CMD_VERIFY; |
634 | | /* we assign pin2.len to 0 early on so that in case of an error we are |
635 | | * not exiting with an undefined session PIN */ |
636 | 0 | data->pin2.len = 0; |
637 | 0 | } |
638 | | |
639 | 692 | if ((card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH) |
640 | 0 | && (data->cmd == SC_PIN_CMD_CHANGE) |
641 | 0 | && (data->pin_reference == 0x81) |
642 | 0 | && (!data->pin1.data || data->pin1.len <= 0)) { |
643 | 0 | return sc_hsm_soc_change(card, data, tries_left); |
644 | 692 | } else if ((card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH) |
645 | 0 | && (data->cmd == SC_PIN_CMD_UNBLOCK) |
646 | 0 | && (data->pin_reference == 0x81) |
647 | 0 | && (!data->pin1.data || data->pin1.len <= 0)) { |
648 | 0 | return sc_hsm_soc_unblock(card, data, tries_left); |
649 | 0 | } |
650 | | |
651 | 692 | #ifdef ENABLE_SM |
652 | | /* For contactless cards always establish a secure channel before PIN |
653 | | * verification. Also, Session PIN generation requires SM. */ |
654 | 692 | if ((card->type == SC_CARD_TYPE_SC_HSM_SOC |
655 | 692 | || card->type == SC_CARD_TYPE_SC_HSM_GOID |
656 | 0 | || card->reader->uid.len || cmd == SC_PIN_CMD_GET_SESSION_PIN) |
657 | 692 | && (data->cmd != SC_PIN_CMD_GET_INFO)) { |
658 | 43 | struct sc_pin_cmd_data check_sm_pin_data; |
659 | 43 | memset(&check_sm_pin_data, 0, sizeof(check_sm_pin_data)); |
660 | 43 | check_sm_pin_data.cmd = SC_PIN_CMD_GET_INFO; |
661 | 43 | check_sm_pin_data.pin_type = data->pin_type; |
662 | 43 | check_sm_pin_data.pin_reference = data->pin_reference; |
663 | | |
664 | 43 | r = SC_ERROR_NOT_ALLOWED; |
665 | 43 | if (card->sm_ctx.sm_mode == SM_MODE_TRANSMIT) { |
666 | | /* check if the existing SM channel is still valid */ |
667 | 0 | r = sc_pin_cmd(card, &check_sm_pin_data, NULL); |
668 | 0 | } |
669 | 43 | if (r == SC_ERROR_ASN1_OBJECT_NOT_FOUND || r == SC_ERROR_NOT_ALLOWED) { |
670 | | /* need to establish a new SM channel */ |
671 | 43 | LOG_TEST_RET(card->ctx, |
672 | 43 | sc_hsm_perform_chip_authentication(card), |
673 | 43 | "Could not perform chip authentication"); |
674 | 43 | } |
675 | 43 | } |
676 | 649 | #endif |
677 | | |
678 | 649 | if ((card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH) |
679 | 0 | && (data->cmd == SC_PIN_CMD_VERIFY) |
680 | 0 | && (data->pin_reference == 0x81) |
681 | 0 | && (!data->pin1.data || data->pin1.len <= 0)) { |
682 | 0 | r = sc_hsm_soc_biomatch(card, data, tries_left); |
683 | 649 | } else { |
684 | 649 | if ((data->cmd == SC_PIN_CMD_VERIFY) && (data->pin_reference == 0x88)) { |
685 | 0 | if (data->pin1.len != 16) |
686 | 0 | return SC_ERROR_INVALID_PIN_LENGTH; |
687 | | |
688 | | // Save SO PIN for later use in sc_hsm_init_pin() |
689 | 0 | r = sc_hsm_encode_sopin(data->pin1.data, priv->sopin); |
690 | 0 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
691 | | |
692 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
693 | 0 | } |
694 | | |
695 | 649 | if ((data->cmd == SC_PIN_CMD_CHANGE) && (data->pin_reference == 0x88)) { |
696 | 0 | if ((data->pin1.len != 16) || (data->pin2.len != 16)) |
697 | 0 | return SC_ERROR_INVALID_PIN_LENGTH; |
698 | | |
699 | 0 | r = sc_hsm_encode_sopin(data->pin1.data, cmdbuff); |
700 | 0 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
701 | | |
702 | 0 | r = sc_hsm_encode_sopin(data->pin2.data, cmdbuff + 8); |
703 | 0 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
704 | | |
705 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x24, 0x00, data->pin_reference); |
706 | 0 | apdu.data = cmdbuff; |
707 | 0 | apdu.datalen = sizeof(cmdbuff); |
708 | 0 | apdu.lc = 16; |
709 | 0 | apdu.resplen = 0; |
710 | 0 | data->apdu = &apdu; |
711 | 0 | } |
712 | | |
713 | 649 | #ifdef ENABLE_SM |
714 | 649 | if ((data->cmd == SC_PIN_CMD_GET_INFO) |
715 | 649 | && (card->sm_ctx.sm_mode == SM_MODE_TRANSMIT)) { |
716 | | /* JCOP's SM accelerator is incapable of using case 1 APDU in SM */ |
717 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_2, 0x20, 0x00, data->pin_reference); |
718 | 0 | apdu.resp = rbuf; |
719 | 0 | apdu.resplen = sizeof rbuf; |
720 | 0 | data->apdu = &apdu; |
721 | 0 | } |
722 | 649 | #endif |
723 | | |
724 | 649 | data->pin1.offset = 5; |
725 | 649 | data->pin2.offset = 5; |
726 | | |
727 | 649 | r = (*iso_ops->pin_cmd)(card, data, tries_left); |
728 | 649 | data->apdu = NULL; |
729 | 649 | } |
730 | 649 | LOG_TEST_RET(card->ctx, r, "Verification failed"); |
731 | | |
732 | 271 | if (cmd == SC_PIN_CMD_GET_SESSION_PIN) { |
733 | | /* reset data->cmd to its original value */ |
734 | 0 | data->cmd = SC_PIN_CMD_GET_SESSION_PIN; |
735 | 0 | if (data->pin_reference == 0x81) { |
736 | 0 | u8 recvbuf[SC_MAX_APDU_BUFFER_SIZE]; |
737 | 0 | #ifdef ENABLE_SM |
738 | 0 | if (card->sm_ctx.sm_mode != SM_MODE_TRANSMIT) { |
739 | 0 | sc_log(card->ctx, |
740 | 0 | "Session PIN generation only supported in SM"); |
741 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
742 | 0 | } |
743 | | #else |
744 | | sc_log(card->ctx, |
745 | | "Session PIN generation only supported in SM"); |
746 | | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
747 | | #endif |
748 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0x5A, 0x01, data->pin_reference); |
749 | 0 | apdu.cla = 0x80; |
750 | 0 | apdu.resp = recvbuf; |
751 | 0 | apdu.resplen = sizeof recvbuf; |
752 | 0 | apdu.le = 0; |
753 | 0 | if (sc_transmit_apdu(card, &apdu) != SC_SUCCESS |
754 | 0 | || sc_check_sw(card, apdu.sw1, apdu.sw2) != SC_SUCCESS) { |
755 | 0 | sc_log(card->ctx, |
756 | 0 | "Generating session PIN failed"); |
757 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
758 | 0 | } |
759 | 0 | if (data->pin2.data && pin2_len > 0) { |
760 | 0 | if (pin2_len >= apdu.resplen) { |
761 | 0 | memcpy((unsigned char *) data->pin2.data, apdu.resp, |
762 | 0 | apdu.resplen); |
763 | 0 | data->pin2.len = apdu.resplen; |
764 | 0 | } else { |
765 | 0 | sc_log(card->ctx, |
766 | 0 | "Buffer too small for session PIN"); |
767 | 0 | } |
768 | 0 | } |
769 | 0 | } else { |
770 | 0 | sc_log(card->ctx, |
771 | 0 | "Session PIN not supported for this PIN (0x%02X)", |
772 | 0 | data->pin_reference); |
773 | 0 | } |
774 | 0 | } |
775 | 271 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
776 | 271 | } |
777 | | |
778 | | |
779 | | |
780 | | static int sc_hsm_logout(sc_card_t * card) |
781 | 0 | { |
782 | 0 | sc_path_t path; |
783 | 0 | sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data; |
784 | 0 | memset(priv->sopin, 0, sizeof(priv->sopin)); |
785 | 0 | #ifdef ENABLE_SM |
786 | 0 | sc_sm_stop(card); |
787 | 0 | #endif |
788 | |
|
789 | 0 | sc_path_set(&path, SC_PATH_TYPE_DF_NAME, sc_hsm_aid.value, sc_hsm_aid.len, 0, 0); |
790 | |
|
791 | 0 | return sc_hsm_select_file_ex(card, &path, 1, NULL); |
792 | 0 | } |
793 | | |
794 | | |
795 | | /* NOTE: idx is an offset into the card's file, not into buf */ |
796 | | static int sc_hsm_read_binary(sc_card_t *card, |
797 | | unsigned int idx, u8 *buf, size_t count, |
798 | | unsigned long *flags) |
799 | 5.64k | { |
800 | 5.64k | sc_context_t *ctx = card->ctx; |
801 | 5.64k | sc_apdu_t apdu; |
802 | 5.64k | u8 cmdbuff[4]; |
803 | 5.64k | int r; |
804 | | |
805 | 5.64k | if (idx > 0xffff) { |
806 | 0 | sc_log(ctx, "invalid EF offset: 0x%X > 0xFFFF", idx); |
807 | 0 | return SC_ERROR_OFFSET_TOO_LARGE; |
808 | 0 | } |
809 | | |
810 | 5.64k | cmdbuff[0] = 0x54; |
811 | 5.64k | cmdbuff[1] = 0x02; |
812 | 5.64k | cmdbuff[2] = (idx >> 8) & 0xFF; |
813 | 5.64k | cmdbuff[3] = idx & 0xFF; |
814 | | |
815 | 5.64k | if (count > sc_get_max_recv_size(card)) |
816 | 0 | return SC_ERROR_INTERNAL; |
817 | 5.64k | sc_format_apdu(card, &apdu, SC_APDU_CASE_4, 0xB1, 0x00, 0x00); |
818 | 5.64k | apdu.data = cmdbuff; |
819 | 5.64k | apdu.datalen = 4; |
820 | 5.64k | apdu.lc = 4; |
821 | 5.64k | apdu.le = count; |
822 | 5.64k | apdu.resplen = count; |
823 | 5.64k | apdu.resp = buf; |
824 | | |
825 | 5.64k | r = sc_transmit_apdu(card, &apdu); |
826 | 5.64k | LOG_TEST_RET(ctx, r, "APDU transmit failed"); |
827 | | |
828 | 5.51k | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
829 | 5.51k | if (r != SC_ERROR_FILE_END_REACHED) { |
830 | 4.92k | LOG_TEST_RET(ctx, r, "Check SW error"); |
831 | 4.92k | } |
832 | | |
833 | 3.18k | LOG_FUNC_RETURN(ctx, (int)apdu.resplen); |
834 | 3.18k | } |
835 | | |
836 | | |
837 | | /* NOTE: idx is an offset into the card's file, not into buf */ |
838 | | static int sc_hsm_write_ef(sc_card_t *card, |
839 | | int fid, |
840 | | unsigned int idx, const u8 *buf, size_t count) |
841 | 846 | { |
842 | 846 | sc_context_t *ctx = card->ctx; |
843 | 846 | sc_apdu_t apdu; |
844 | 846 | u8 *cmdbuff, *p; |
845 | 846 | size_t len; |
846 | 846 | int r; |
847 | | |
848 | 846 | if (idx > 0xffff) { |
849 | 0 | sc_log(ctx, "invalid EF offset: 0x%X > 0xFFFF", idx); |
850 | 0 | return SC_ERROR_OFFSET_TOO_LARGE; |
851 | 0 | } |
852 | | |
853 | 846 | cmdbuff = malloc(8 + count); |
854 | 846 | if (!cmdbuff) { |
855 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY); |
856 | 0 | } |
857 | | |
858 | 846 | size_t bytes_left = count; |
859 | | // 8 bytes are required for T54(4) and T53(4) |
860 | 846 | size_t blk_size = card->max_send_size - 8; |
861 | 846 | size_t to_send = 0; |
862 | 846 | size_t file_offset = (size_t) idx; |
863 | 846 | size_t offset = 0; |
864 | 957 | do { |
865 | 957 | to_send = bytes_left >= blk_size ? blk_size : bytes_left; |
866 | 957 | p = cmdbuff; |
867 | | // ASN1 0x54 offset |
868 | 957 | *p++ = 0x54; |
869 | 957 | *p++ = 0x02; |
870 | 957 | *p++ = (file_offset >> 8) & 0xFF; |
871 | 957 | *p++ = file_offset & 0xFF; |
872 | | // ASN1 0x53 to_send |
873 | 957 | *p++ = 0x53; |
874 | 957 | if (to_send < 128) { |
875 | 233 | *p++ = (u8)to_send; |
876 | 233 | len = 6; |
877 | 724 | } else if (to_send < 256) { |
878 | 48 | *p++ = 0x81; |
879 | 48 | *p++ = (u8)to_send; |
880 | 48 | len = 7; |
881 | 676 | } else { |
882 | 676 | *p++ = 0x82; |
883 | 676 | *p++ = (to_send >> 8) & 0xFF; |
884 | 676 | *p++ = to_send & 0xFF; |
885 | 676 | len = 8; |
886 | 676 | } |
887 | | |
888 | 957 | if (buf != NULL) |
889 | 949 | memcpy(p, buf+offset, to_send); |
890 | 957 | len += to_send; |
891 | | |
892 | 957 | sc_format_apdu(card, &apdu, SC_APDU_CASE_3, 0xD7, fid >> 8, fid & 0xFF); |
893 | 957 | apdu.data = cmdbuff; |
894 | 957 | apdu.datalen = len; |
895 | 957 | apdu.lc = len; |
896 | | |
897 | 957 | r = sc_transmit_apdu(card, &apdu); |
898 | 957 | LOG_TEST_GOTO_ERR(ctx, r, "APDU transmit failed"); |
899 | 910 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
900 | 910 | LOG_TEST_GOTO_ERR(ctx, r, "Check SW error"); |
901 | | |
902 | 238 | bytes_left -= to_send; |
903 | 238 | offset += to_send; |
904 | 238 | file_offset += to_send; |
905 | 238 | } while (0 < bytes_left); |
906 | | |
907 | 846 | err: |
908 | 846 | free(cmdbuff); |
909 | | |
910 | 846 | LOG_FUNC_RETURN(ctx, (int)count); |
911 | 846 | } |
912 | | |
913 | | |
914 | | static int sc_hsm_update_binary(sc_card_t *card, |
915 | | unsigned int idx, const u8 *buf, size_t count, |
916 | | unsigned long flags) |
917 | 838 | { |
918 | 838 | return sc_hsm_write_ef(card, 0, idx, buf, count); |
919 | 838 | } |
920 | | |
921 | | |
922 | | |
923 | | static int sc_hsm_list_files(sc_card_t *card, u8 * buf, size_t buflen) |
924 | 914 | { |
925 | 914 | sc_apdu_t apdu; |
926 | 914 | u8 recvbuf[MAX_EXT_APDU_LENGTH]; |
927 | 914 | sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data; |
928 | 914 | int r; |
929 | | |
930 | 914 | if (priv->noExtLength) { |
931 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_2, 0x58, 0, 0); |
932 | 914 | } else { |
933 | 914 | sc_format_apdu(card, &apdu, SC_APDU_CASE_2_EXT, 0x58, 0, 0); |
934 | 914 | } |
935 | 914 | apdu.cla = 0x80; |
936 | 914 | apdu.resp = recvbuf; |
937 | 914 | apdu.resplen = sizeof(recvbuf); |
938 | 914 | apdu.le = 0; |
939 | 914 | r = sc_transmit_apdu(card, &apdu); |
940 | | |
941 | 914 | if ((r == SC_ERROR_TRANSMIT_FAILED) && (!priv->noExtLength)) { |
942 | 0 | sc_log(card->ctx, "No extended length support ? Trying fall-back to short APDUs, probably breaking support for RSA 2048 operations"); |
943 | 0 | priv->noExtLength = 1; |
944 | 0 | card->max_send_size = 248; // 255 - 7 because of TLV in odd ins UPDATE BINARY |
945 | 0 | return sc_hsm_list_files(card, buf, buflen); |
946 | 0 | } |
947 | 914 | LOG_TEST_RET(card->ctx, r, "ENUMERATE OBJECTS APDU transmit failed"); |
948 | | |
949 | 901 | if (buflen < apdu.resplen) |
950 | 0 | memcpy(buf, recvbuf, buflen); |
951 | 901 | else |
952 | 901 | memcpy(buf, recvbuf, apdu.resplen); |
953 | | |
954 | 901 | LOG_FUNC_RETURN(card->ctx, (int)apdu.resplen); |
955 | 901 | } |
956 | | |
957 | | |
958 | | |
959 | | static int sc_hsm_create_file(sc_card_t *card, sc_file_t *file) |
960 | 8 | { |
961 | 8 | int r; |
962 | | |
963 | 8 | r = sc_hsm_write_ef(card, file->id, 0, NULL, 0); |
964 | 8 | LOG_TEST_RET(card->ctx, r, "Create file failed"); |
965 | | |
966 | 8 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
967 | 8 | } |
968 | | |
969 | | |
970 | | |
971 | | static int sc_hsm_delete_file(sc_card_t *card, const sc_path_t *path) |
972 | 13 | { |
973 | 13 | sc_context_t *ctx = card->ctx; |
974 | 13 | sc_apdu_t apdu; |
975 | 13 | u8 sbuf[2]; |
976 | 13 | int r; |
977 | | |
978 | 13 | if ((path->type != SC_PATH_TYPE_FILE_ID) || (path->len != 2)) { |
979 | 0 | sc_log(card->ctx, "File type has to be SC_PATH_TYPE_FILE_ID"); |
980 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS); |
981 | 0 | } |
982 | | |
983 | 13 | sbuf[0] = path->value[0]; |
984 | 13 | sbuf[1] = path->value[1]; |
985 | | |
986 | 13 | sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xE4, 0x02, 0x00); |
987 | 13 | apdu.data = sbuf; |
988 | 13 | apdu.datalen = sizeof(sbuf); |
989 | 13 | apdu.lc = sizeof(sbuf); |
990 | | |
991 | 13 | r = sc_transmit_apdu(card, &apdu); |
992 | 13 | LOG_TEST_RET(ctx, r, "APDU transmit failed"); |
993 | | |
994 | 12 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
995 | 12 | LOG_TEST_RET(ctx, r, "Check SW error"); |
996 | | |
997 | 5 | LOG_FUNC_RETURN(ctx, SC_SUCCESS); |
998 | 5 | } |
999 | | |
1000 | | |
1001 | | static int sc_hsm_set_security_env(sc_card_t *card, |
1002 | | const sc_security_env_t *env, |
1003 | | int se_num) |
1004 | 1.76k | { |
1005 | 1.76k | sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data; |
1006 | | |
1007 | 1.76k | priv->env = env; |
1008 | | |
1009 | 1.76k | switch(env->algorithm) { |
1010 | 1.73k | case SC_ALGORITHM_RSA: |
1011 | 1.73k | if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1) { |
1012 | 0 | if (env->algorithm_flags & SC_ALGORITHM_RSA_HASH_SHA1) { |
1013 | 0 | priv->algorithm = ALGO_RSA_PKCS1_SHA1; |
1014 | 0 | } else if (env->algorithm_flags & SC_ALGORITHM_RSA_HASH_SHA256) { |
1015 | 0 | priv->algorithm = ALGO_RSA_PKCS1_SHA256; |
1016 | 0 | } else if (env->algorithm_flags & SC_ALGORITHM_RSA_HASH_SHA384) { |
1017 | 0 | priv->algorithm = ALGO_RSA_PKCS1_SHA384; |
1018 | 0 | } else if (env->algorithm_flags & SC_ALGORITHM_RSA_HASH_SHA512) { |
1019 | 0 | priv->algorithm = ALGO_RSA_PKCS1_SHA512; |
1020 | 0 | } else { |
1021 | 0 | priv->algorithm = ALGO_RSA_PKCS1; |
1022 | 0 | } |
1023 | 1.73k | } else if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PSS) { |
1024 | 710 | if ((env->algorithm_flags & SC_ALGORITHM_RSA_HASHES) && |
1025 | 0 | (((env->algorithm_flags & SC_ALGORITHM_MGF1_HASHES) >> 8) != (env->algorithm_flags & SC_ALGORITHM_RSA_HASHES))) { |
1026 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS); |
1027 | 0 | } |
1028 | 710 | priv->algorithm = ALGO_RSA_PSS; |
1029 | 1.02k | } else { |
1030 | 1.02k | if (env->operation == SC_SEC_OPERATION_DECIPHER) { |
1031 | 302 | priv->algorithm = ALGO_RSA_DECRYPT; |
1032 | 724 | } else { |
1033 | 724 | priv->algorithm = ALGO_RSA_RAW; |
1034 | 724 | } |
1035 | 1.02k | } |
1036 | 1.73k | break; |
1037 | 1.73k | case SC_ALGORITHM_EC: |
1038 | 32 | if (env->operation == SC_SEC_OPERATION_DERIVE) { |
1039 | 0 | priv->algorithm = ALGO_EC_DH; |
1040 | 32 | } else if (env->algorithm_flags & SC_ALGORITHM_ECDSA_HASH_NONE) { |
1041 | 3 | priv->algorithm = ALGO_EC_RAW; |
1042 | 29 | } else if (env->algorithm_flags & SC_ALGORITHM_ECDSA_HASH_SHA1) { |
1043 | 2 | priv->algorithm = ALGO_EC_SHA1; |
1044 | 27 | } else if (env->algorithm_flags & SC_ALGORITHM_ECDSA_HASH_SHA224) { |
1045 | 10 | priv->algorithm = ALGO_EC_SHA224; |
1046 | 17 | } else if (env->algorithm_flags & SC_ALGORITHM_ECDSA_HASH_SHA256) { |
1047 | 1 | priv->algorithm = ALGO_EC_SHA256; |
1048 | 16 | } else if (env->algorithm_flags & SC_ALGORITHM_ECDSA_HASH_SHA384) { |
1049 | 6 | priv->algorithm = ALGO_EC_SHA384; |
1050 | 10 | } else if (env->algorithm_flags & SC_ALGORITHM_ECDSA_HASH_SHA512) { |
1051 | 10 | priv->algorithm = ALGO_EC_SHA512; |
1052 | 10 | } else if (env->algorithm_flags & SC_ALGORITHM_ECDSA_RAW) { |
1053 | 0 | priv->algorithm = ALGO_EC_RAW; |
1054 | 0 | } else { |
1055 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS); |
1056 | 0 | } |
1057 | 32 | break; |
1058 | 32 | default: |
1059 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS); |
1060 | 0 | break; |
1061 | 1.76k | } |
1062 | 1.76k | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1063 | 1.76k | } |
1064 | | |
1065 | | |
1066 | | |
1067 | | static int sc_hsm_decode_ecdsa_signature(sc_card_t *card, |
1068 | | const u8 * data, size_t datalen, |
1069 | | u8 * out, size_t outlen, |
1070 | 14 | size_t key_size) { |
1071 | | |
1072 | 14 | int r; |
1073 | 14 | size_t fieldsizebytes = (key_size + 7) >> 3; |
1074 | | |
1075 | 14 | sc_log(card->ctx, |
1076 | 14 | "Field size %"SC_FORMAT_LEN_SIZE_T"u, signature buffer size %"SC_FORMAT_LEN_SIZE_T"u", |
1077 | 14 | fieldsizebytes, outlen); |
1078 | | |
1079 | 14 | r = sc_asn1_decode_ecdsa_signature(card->ctx, data, datalen, fieldsizebytes, &out, outlen); |
1080 | 14 | LOG_FUNC_RETURN(card->ctx, r); |
1081 | 14 | } |
1082 | | |
1083 | | |
1084 | | |
1085 | | static int sc_hsm_compute_signature(sc_card_t *card, |
1086 | | const u8 * data, size_t datalen, |
1087 | | u8 * out, size_t outlen) |
1088 | 1.45k | { |
1089 | 1.45k | int r; |
1090 | 1.45k | sc_apdu_t apdu; |
1091 | 1.45k | u8 rbuf[514]; |
1092 | 1.45k | sc_hsm_private_data_t *priv; |
1093 | | |
1094 | 1.45k | if (card == NULL || data == NULL || out == NULL) { |
1095 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
1096 | 0 | } |
1097 | 1.45k | priv = (sc_hsm_private_data_t *) card->drv_data; |
1098 | | |
1099 | 1.45k | if (priv->env == NULL) { |
1100 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_OBJECT_NOT_FOUND); |
1101 | 0 | } |
1102 | | |
1103 | 1.45k | sc_format_apdu(card, &apdu, SC_APDU_CASE_4_EXT, 0x68, priv->env->key_ref[0], priv->algorithm); |
1104 | 1.45k | apdu.cla = 0x80; |
1105 | 1.45k | apdu.resp = rbuf; |
1106 | 1.45k | apdu.resplen = sizeof(rbuf); |
1107 | 1.45k | apdu.le = 512; |
1108 | | |
1109 | 1.45k | apdu.data = data; |
1110 | 1.45k | apdu.lc = datalen; |
1111 | 1.45k | apdu.datalen = datalen; |
1112 | 1.45k | r = sc_transmit_apdu(card, &apdu); |
1113 | | |
1114 | 1.45k | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
1115 | 1.29k | if (apdu.sw1 == 0x90 && apdu.sw2 == 0x00) { |
1116 | 142 | int len; |
1117 | | |
1118 | 142 | if ((priv->algorithm & 0xF0) == ALGO_EC_RAW) { |
1119 | 14 | len = sc_hsm_decode_ecdsa_signature(card, apdu.resp, apdu.resplen, out, outlen, priv->env->key_size_bits); |
1120 | 14 | if (len < 0) { |
1121 | 11 | LOG_FUNC_RETURN(card->ctx, len); |
1122 | 11 | } |
1123 | 128 | } else { |
1124 | 128 | len = (int)(apdu.resplen > outlen ? outlen : apdu.resplen); |
1125 | 128 | memcpy(out, apdu.resp, len); |
1126 | 128 | } |
1127 | 131 | LOG_FUNC_RETURN(card->ctx, len); |
1128 | 131 | } |
1129 | 1.15k | LOG_FUNC_RETURN(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2)); |
1130 | 1.15k | } |
1131 | | |
1132 | | |
1133 | | |
1134 | | static int sc_hsm_decipher(sc_card_t *card, const u8 * crgram, size_t crgram_len, u8 * out, size_t outlen) |
1135 | 305 | { |
1136 | 305 | int r; |
1137 | 305 | size_t len; |
1138 | 305 | sc_apdu_t apdu; |
1139 | 305 | u8 rbuf[514]; |
1140 | 305 | sc_hsm_private_data_t *priv; |
1141 | | |
1142 | 305 | if (card == NULL || crgram == NULL || out == NULL) { |
1143 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
1144 | 0 | } |
1145 | 305 | LOG_FUNC_CALLED(card->ctx); |
1146 | 305 | priv = (sc_hsm_private_data_t *) card->drv_data; |
1147 | | |
1148 | 305 | sc_format_apdu(card, &apdu, SC_APDU_CASE_4_EXT, 0x62, priv->env->key_ref[0], priv->algorithm); |
1149 | 305 | apdu.cla = 0x80; |
1150 | 305 | apdu.resp = rbuf; |
1151 | 305 | apdu.resplen = sizeof(rbuf); |
1152 | 305 | apdu.le = 512; |
1153 | | |
1154 | 305 | apdu.data = (u8 *)crgram; |
1155 | 305 | apdu.lc = crgram_len; |
1156 | 305 | apdu.datalen = crgram_len; |
1157 | | |
1158 | 305 | r = sc_transmit_apdu(card, &apdu); |
1159 | | |
1160 | 305 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
1161 | 286 | if (apdu.sw1 == 0x90 && apdu.sw2 == 0x00) { |
1162 | 96 | if (priv->algorithm == ALGO_EC_DH) { |
1163 | | // |
1164 | | // The SmartCard-HSM returns the point result of the DH operation |
1165 | | // with a leading '04' |
1166 | 0 | if (apdu.resplen <= 0) |
1167 | 0 | return SC_ERROR_INTERNAL; |
1168 | 0 | len = apdu.resplen - 1 > outlen ? outlen : apdu.resplen - 1; |
1169 | 0 | memcpy(out, apdu.resp + 1, len); |
1170 | 0 | LOG_FUNC_RETURN(card->ctx, (int)len); |
1171 | 96 | } else { |
1172 | 96 | len = apdu.resplen > outlen ? outlen : apdu.resplen; |
1173 | 96 | memcpy(out, apdu.resp, len); |
1174 | 96 | LOG_FUNC_RETURN(card->ctx, (int)len); |
1175 | 96 | } |
1176 | 96 | } |
1177 | 190 | else |
1178 | 286 | LOG_FUNC_RETURN(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2)); |
1179 | 286 | } |
1180 | | |
1181 | | |
1182 | | |
1183 | | void sc_hsm_set_serialnr(sc_card_t *card, char *serial) |
1184 | 0 | { |
1185 | 0 | sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data; |
1186 | |
|
1187 | 0 | if (priv->serialno) { |
1188 | 0 | free(priv->serialno); |
1189 | 0 | } |
1190 | |
|
1191 | 0 | priv->serialno = strdup(serial); |
1192 | 0 | } |
1193 | | |
1194 | | |
1195 | | |
1196 | | static int sc_hsm_get_serialnr(sc_card_t *card, sc_serial_number_t *serial) |
1197 | 336 | { |
1198 | 336 | sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data; |
1199 | | |
1200 | 336 | LOG_FUNC_CALLED(card->ctx); |
1201 | | |
1202 | 336 | if (!priv->serialno && 0 == strcmp(card->ctx->app_name, "opensc-tool")) { |
1203 | | /* sc-hsm initializes the serial number via its PKCS#15 layer. |
1204 | | * Create and destroy a dummy card to get this initialized. Only do |
1205 | | * this for `opensc-tool --serial` to avoid unnecessary card commands |
1206 | | * in all other cases. */ |
1207 | 0 | sc_pkcs15_card_t *p15card = NULL; |
1208 | 0 | (void)sc_pkcs15_bind(card, NULL, &p15card); |
1209 | 0 | sc_pkcs15_unbind(p15card); |
1210 | 0 | } |
1211 | | |
1212 | 336 | if (!priv->serialno) { |
1213 | 336 | return SC_ERROR_OBJECT_NOT_FOUND; |
1214 | 336 | } |
1215 | | |
1216 | 0 | serial->len = strlen(priv->serialno); |
1217 | 0 | if (serial->len > sizeof(serial->value)) |
1218 | 0 | serial->len = sizeof(serial->value); |
1219 | |
|
1220 | 0 | memcpy(serial->value, priv->serialno, serial->len); |
1221 | |
|
1222 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1223 | 0 | } |
1224 | | |
1225 | | |
1226 | | |
1227 | | static int sc_hsm_initialize(sc_card_t *card, sc_cardctl_sc_hsm_init_param_t *params) |
1228 | 0 | { |
1229 | 0 | sc_context_t *ctx = card->ctx; |
1230 | 0 | sc_pkcs15_tokeninfo_t ti; |
1231 | 0 | struct sc_pin_cmd_data pincmd; |
1232 | 0 | int r; |
1233 | 0 | size_t tilen; |
1234 | 0 | sc_apdu_t apdu; |
1235 | 0 | u8 ibuff[68+0xFF], *p; |
1236 | |
|
1237 | 0 | LOG_FUNC_CALLED(card->ctx); |
1238 | |
|
1239 | 0 | p = ibuff; |
1240 | 0 | *p++ = 0x80; // Options |
1241 | 0 | *p++ = 0x02; |
1242 | 0 | memcpy(p, params->options, 2); |
1243 | 0 | p += 2; |
1244 | |
|
1245 | 0 | if (params->user_pin_len > 0xFF) { |
1246 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
1247 | 0 | } |
1248 | 0 | *p++ = 0x81; // User PIN |
1249 | 0 | *p++ = (u8)params->user_pin_len; |
1250 | 0 | memcpy(p, params->user_pin, params->user_pin_len); |
1251 | 0 | p += params->user_pin_len; |
1252 | |
|
1253 | 0 | *p++ = 0x82; // Initialization code |
1254 | 0 | *p++ = 0x08; |
1255 | 0 | memcpy(p, params->init_code, 8); |
1256 | 0 | p += 8; |
1257 | |
|
1258 | 0 | *p++ = 0x91; // User PIN retry counter |
1259 | 0 | *p++ = 0x01; |
1260 | 0 | *p++ = params->user_pin_retry_counter; |
1261 | |
|
1262 | 0 | if (params->dkek_shares >= 0) { |
1263 | 0 | *p++ = 0x92; // Number of DKEK shares |
1264 | 0 | *p++ = 0x01; |
1265 | 0 | *p++ = (u8)params->dkek_shares; |
1266 | 0 | } |
1267 | |
|
1268 | 0 | if (params->num_of_pub_keys > 0) { |
1269 | 0 | *p++ = 0x93; // Use public key authentication |
1270 | 0 | *p++ = 0x02; |
1271 | 0 | *p++ = params->num_of_pub_keys; // Total number of public keys used for public authentication |
1272 | 0 | *p++ = params->required_pub_keys; // Number of public keys required for authentication |
1273 | 0 | } |
1274 | |
|
1275 | 0 | if (params->bio1.len) { |
1276 | 0 | *p++ = 0x95; |
1277 | 0 | *p++ = params->bio1.len; |
1278 | 0 | memcpy(p, params->bio1.value, params->bio1.len); |
1279 | 0 | p += params->bio1.len; |
1280 | 0 | } |
1281 | 0 | if (params->bio2.len) { |
1282 | 0 | *p++ = 0x96; |
1283 | 0 | *p++ = params->bio2.len; |
1284 | 0 | memcpy(p, params->bio2.value, params->bio2.len); |
1285 | 0 | p += params->bio2.len; |
1286 | 0 | } |
1287 | |
|
1288 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x50, 0x00, 0x00); |
1289 | 0 | apdu.cla = 0x80; |
1290 | 0 | apdu.data = ibuff; |
1291 | 0 | apdu.datalen = p - ibuff; |
1292 | 0 | apdu.lc = apdu.datalen; |
1293 | |
|
1294 | 0 | r = sc_transmit_apdu(card, &apdu); |
1295 | 0 | LOG_TEST_RET(ctx, r, "APDU transmit failed"); |
1296 | | |
1297 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1298 | |
|
1299 | 0 | if (r == SC_ERROR_NOT_ALLOWED) { |
1300 | 0 | r = SC_ERROR_PIN_CODE_INCORRECT; |
1301 | 0 | } |
1302 | |
|
1303 | 0 | LOG_TEST_RET(ctx, r, "Check SW error"); |
1304 | | |
1305 | 0 | if (params->label) { |
1306 | 0 | memset(&ti, 0, sizeof(ti)); |
1307 | |
|
1308 | 0 | ti.label = params->label; |
1309 | 0 | ti.flags = SC_PKCS15_TOKEN_PRN_GENERATION; |
1310 | |
|
1311 | 0 | r = sc_pkcs15_encode_tokeninfo(ctx, &ti, &p, &tilen); |
1312 | 0 | LOG_TEST_RET(ctx, r, "Error encoding tokeninfo"); |
1313 | | |
1314 | 0 | memset(&pincmd, 0, sizeof(pincmd)); |
1315 | 0 | pincmd.cmd = SC_PIN_CMD_VERIFY; |
1316 | 0 | pincmd.pin_type = SC_AC_CHV; |
1317 | 0 | pincmd.pin_reference = 0x81; |
1318 | 0 | pincmd.pin1.data = params->user_pin; |
1319 | 0 | pincmd.pin1.len = params->user_pin_len; |
1320 | |
|
1321 | 0 | r = (*iso_ops->pin_cmd)(card, &pincmd, NULL); |
1322 | 0 | LOG_TEST_RET(ctx, r, "Could not verify PIN"); |
1323 | | |
1324 | 0 | r = sc_hsm_write_ef(card, 0x2F03, 0, p, tilen); |
1325 | 0 | LOG_TEST_RET(ctx, r, "Could not write EF.TokenInfo"); |
1326 | 0 | } |
1327 | | |
1328 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1329 | 0 | } |
1330 | | |
1331 | | |
1332 | | |
1333 | | static int sc_hsm_import_dkek_share(sc_card_t *card, sc_cardctl_sc_hsm_dkek_t *params) |
1334 | 0 | { |
1335 | 0 | sc_context_t *ctx = card->ctx; |
1336 | 0 | sc_apdu_t apdu; |
1337 | 0 | u8 status[SC_MAX_APDU_BUFFER_SIZE]; |
1338 | 0 | int r; |
1339 | |
|
1340 | 0 | LOG_FUNC_CALLED(card->ctx); |
1341 | |
|
1342 | 0 | if (params->importShare) { |
1343 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0x52, 0x00, 0x00); |
1344 | 0 | apdu.cla = 0x80; |
1345 | 0 | apdu.data = params->dkek_share; |
1346 | 0 | apdu.datalen = sizeof(params->dkek_share); |
1347 | 0 | apdu.lc = apdu.datalen; |
1348 | 0 | } else { |
1349 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0x52, 0x00, 0x00); |
1350 | 0 | } |
1351 | 0 | apdu.cla = 0x80; |
1352 | 0 | apdu.le = 0; |
1353 | 0 | apdu.resp = status; |
1354 | 0 | apdu.resplen = sizeof(status); |
1355 | |
|
1356 | 0 | r = sc_transmit_apdu(card, &apdu); |
1357 | 0 | LOG_TEST_RET(ctx, r, "APDU transmit failed"); |
1358 | | |
1359 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1360 | |
|
1361 | 0 | LOG_TEST_RET(ctx, r, "Check SW error"); |
1362 | | |
1363 | 0 | if (apdu.resplen < (sizeof(params->key_check_value) + 2)) |
1364 | 0 | return SC_ERROR_INTERNAL; |
1365 | | |
1366 | 0 | params->dkek_shares = status[0]; |
1367 | 0 | params->outstanding_shares = status[1]; |
1368 | 0 | memcpy(params->key_check_value, status + 2, sizeof(params->key_check_value)); |
1369 | |
|
1370 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1371 | 0 | } |
1372 | | |
1373 | | |
1374 | | |
1375 | | static int sc_hsm_wrap_key(sc_card_t *card, sc_cardctl_sc_hsm_wrapped_key_t *params) |
1376 | 0 | { |
1377 | 0 | sc_context_t *ctx = card->ctx; |
1378 | 0 | sc_apdu_t apdu; |
1379 | 0 | u8 data[1500]; |
1380 | 0 | int r; |
1381 | |
|
1382 | 0 | LOG_FUNC_CALLED(card->ctx); |
1383 | |
|
1384 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_2_EXT, 0x72, params->key_id, 0x92); |
1385 | 0 | apdu.cla = 0x80; |
1386 | 0 | apdu.le = 0; |
1387 | 0 | apdu.resp = data; |
1388 | 0 | apdu.resplen = sizeof(data); |
1389 | |
|
1390 | 0 | r = sc_transmit_apdu(card, &apdu); |
1391 | 0 | LOG_TEST_RET(ctx, r, "APDU transmit failed"); |
1392 | | |
1393 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1394 | |
|
1395 | 0 | LOG_TEST_RET(ctx, r, "Check SW error"); |
1396 | | |
1397 | 0 | if (params->wrapped_key == NULL) { |
1398 | 0 | params->wrapped_key_length = apdu.resplen; |
1399 | 0 | params->wrapped_key = malloc(apdu.resplen); |
1400 | 0 | if (params->wrapped_key == NULL) { |
1401 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY); |
1402 | 0 | } |
1403 | 0 | } else { |
1404 | 0 | if (apdu.resplen > params->wrapped_key_length) { |
1405 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_BUFFER_TOO_SMALL); |
1406 | 0 | } |
1407 | 0 | params->wrapped_key_length = apdu.resplen; |
1408 | 0 | } |
1409 | 0 | memcpy(params->wrapped_key, data, apdu.resplen); |
1410 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1411 | 0 | } |
1412 | | |
1413 | | |
1414 | | |
1415 | | static int sc_hsm_unwrap_key(sc_card_t *card, sc_cardctl_sc_hsm_wrapped_key_t *params) |
1416 | 0 | { |
1417 | 0 | sc_context_t *ctx = card->ctx; |
1418 | 0 | sc_apdu_t apdu; |
1419 | 0 | int r; |
1420 | |
|
1421 | 0 | LOG_FUNC_CALLED(card->ctx); |
1422 | |
|
1423 | 0 | r = sc_hsm_write_ef(card, 0x2F10, 0, params->wrapped_key, params->wrapped_key_length); |
1424 | 0 | LOG_TEST_RET(card->ctx, r, "Create EF failed"); |
1425 | | |
1426 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x74, params->key_id, 0x93); |
1427 | 0 | apdu.cla = 0x80; |
1428 | 0 | r = sc_transmit_apdu(card, &apdu); |
1429 | 0 | LOG_TEST_RET(ctx, r, "APDU transmit failed"); |
1430 | | |
1431 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1432 | 0 | LOG_TEST_RET(ctx, r, "Check SW error"); |
1433 | | |
1434 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1435 | 0 | } |
1436 | | |
1437 | | static int verify_certificate(sc_card_t *card, sc_cvc_t *cvc, |
1438 | | const u8 *cvc_buf, size_t cvc_buf_len) |
1439 | 0 | { |
1440 | 0 | u8 tag = SC_ASN1_TAG_CONTEXT | SC_ASN1_TAG_BIT_STRING; /* 0x83 */ |
1441 | 0 | size_t pukref_len; |
1442 | 0 | u8 pukref[BUFSIZ]; |
1443 | 0 | sc_apdu_t apdu; |
1444 | 0 | u8 *ptr; |
1445 | 0 | int r; |
1446 | |
|
1447 | 0 | LOG_FUNC_CALLED(card->ctx); |
1448 | | |
1449 | | /* check if public key is already known */ |
1450 | 0 | if ((r = sc_asn1_put_tag(tag, (u8 *)cvc->chr, cvc->chrLen, |
1451 | 0 | pukref, sizeof(pukref), &ptr)) < 0) { |
1452 | 0 | sc_log(card->ctx, "Error formatting ASN.1 sequence: %s\n", sc_strerror(r)); |
1453 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_UNKNOWN); |
1454 | 0 | } |
1455 | 0 | pukref_len = ptr - pukref; |
1456 | | |
1457 | | /* MANAGE SECURITY ENVIRONMENT to query public key by chr */ |
1458 | 0 | sc_format_apdu_ex(&apdu, 0x00, 0x22, 0x81, 0xB6, pukref, pukref_len, NULL, 0); |
1459 | |
|
1460 | 0 | r = sc_transmit_apdu(card, &apdu); |
1461 | 0 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
1462 | | |
1463 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1464 | 0 | if (!r) { |
1465 | | /* already known */ |
1466 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1467 | 0 | } |
1468 | 0 | if (apdu.sw1 != 0x6A && apdu.sw2 != 0x88) { |
1469 | 0 | LOG_TEST_RET(card->ctx, SC_ERROR_UNKNOWN, "Check SW error"); |
1470 | 0 | } |
1471 | | |
1472 | 0 | if ((r = sc_asn1_put_tag(tag, (u8 *)cvc->car, cvc->carLen, |
1473 | 0 | pukref, sizeof(pukref), &ptr)) < 0) { |
1474 | 0 | sc_log(card->ctx, "Error formatting ASN.1 sequence: %s\n", sc_strerror(r)); |
1475 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_UNKNOWN); |
1476 | 0 | } |
1477 | 0 | pukref_len = ptr - pukref; |
1478 | | |
1479 | | /* MANAGE SECURITY ENVIRONMENT to set the CAR public key */ |
1480 | 0 | sc_format_apdu_ex(&apdu, 0x00, 0x22, 0x81, 0xB6, pukref, pukref_len, NULL, 0); |
1481 | |
|
1482 | 0 | r = sc_transmit_apdu(card, &apdu); |
1483 | 0 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
1484 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1485 | 0 | LOG_TEST_RET(card->ctx, r, "Check SW error"); |
1486 | | |
1487 | | /* PERFORM SECURITY OPERATION -> VERIFY CERTIFICATE */ |
1488 | 0 | sc_format_apdu_ex(&apdu, 0x00, 0x2A, 0x00, 0xBE, cvc_buf, cvc_buf_len, NULL, 0); |
1489 | |
|
1490 | 0 | r = sc_transmit_apdu(card, &apdu); |
1491 | 0 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
1492 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1493 | 0 | LOG_TEST_RET(card->ctx, r, "Check SW error"); |
1494 | | |
1495 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1496 | 0 | } |
1497 | | |
1498 | | |
1499 | | |
1500 | | static int sc_hsm_register_public_key(sc_card_t *card, |
1501 | | sc_cardctl_sc_hsm_pka_register_t *pka_register) |
1502 | 0 | { |
1503 | 0 | u8 tag = SC_ASN1_TAG_CONTEXT | SC_ASN1_TAG_BIT_STRING; /* 0x83 */ |
1504 | 0 | u8 recvbuf[4]; |
1505 | 0 | sc_context_t *ctx = card->ctx; |
1506 | 0 | sc_apdu_t apdu; |
1507 | 0 | u8 *ptr; |
1508 | 0 | int r; |
1509 | 0 | sc_pkcs15_card_t p15card; |
1510 | 0 | const u8 *pka_buf; |
1511 | 0 | size_t pka_buf_len; |
1512 | 0 | sc_cvc_pka_t pka; |
1513 | | /* outer CAR in ASN.1 needs a byte for tag and a byte for length */ |
1514 | 0 | u8 asn1_outer_car[sizeof(pka.public_key_req.cvc.outer_car) + 2]; |
1515 | |
|
1516 | 0 | LOG_FUNC_CALLED(ctx); |
1517 | |
|
1518 | 0 | memset(&pka, 0, sizeof(pka)); |
1519 | 0 | memset(&p15card, 0, sizeof(p15card)); |
1520 | 0 | p15card.card = card; |
1521 | |
|
1522 | 0 | pka_buf = pka_register->buf; |
1523 | 0 | pka_buf_len = pka_register->buflen; |
1524 | 0 | r = sc_pkcs15emu_sc_hsm_decode_pka(&p15card, &pka_buf, &pka_buf_len, &pka); |
1525 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "sc_pkcs15emu_sc_hsm_decode_pka failed"); |
1526 | | |
1527 | | /* the DICA CVC must be verified first */ |
1528 | 0 | r = verify_certificate(card, &pka.dica.cvc, pka.dica.ptr, pka.dica.len); |
1529 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "Verify device issuer CA CVC failed"); |
1530 | | |
1531 | | /* the device CVC must be verified before registering the public key */ |
1532 | 0 | r = verify_certificate(card, &pka.device.cvc, pka.device.ptr, pka.device.len); |
1533 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "Verify device CVC failed"); |
1534 | | |
1535 | 0 | r = sc_asn1_put_tag(tag, |
1536 | 0 | (u8 *)pka.public_key_req.cvc.outer_car, |
1537 | 0 | pka.public_key_req.cvc.outerCARLen, |
1538 | 0 | asn1_outer_car, sizeof(asn1_outer_car), &ptr); |
1539 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "ASN.1 encode outer CAR failed"); |
1540 | | |
1541 | | /* MANAGE SECURITY ENVIRONMENT with the outer CAR of the public key */ |
1542 | 0 | sc_format_apdu_ex(&apdu, 0x00, 0x22, 0x81, 0xB6, |
1543 | 0 | asn1_outer_car, ptr - asn1_outer_car, NULL, 0); |
1544 | |
|
1545 | 0 | r = sc_transmit_apdu(card, &apdu); |
1546 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "APDU transmit failed"); |
1547 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1548 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "Check SW error"); |
1549 | | |
1550 | 0 | sc_format_apdu_ex(&apdu, 0x80, 0x54, 0x00, 0x00, |
1551 | 0 | pka.public_key_req.ptr, pka.public_key_req.len, |
1552 | 0 | recvbuf, sizeof(recvbuf)); |
1553 | |
|
1554 | 0 | r = sc_transmit_apdu(card, &apdu); |
1555 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "APDU transmit failed"); |
1556 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1557 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "Check SW error"); |
1558 | | |
1559 | 0 | pka_register->new_status.num_total = recvbuf[0]; |
1560 | 0 | pka_register->new_status.num_missing = recvbuf[1]; |
1561 | 0 | pka_register->new_status.num_required = recvbuf[2]; |
1562 | 0 | pka_register->new_status.num_authenticated = recvbuf[3]; |
1563 | |
|
1564 | 0 | r = 0; |
1565 | | /* fall-through */ |
1566 | |
|
1567 | 0 | err: |
1568 | 0 | sc_pkcs15emu_sc_hsm_free_cvc_pka(&pka); |
1569 | 0 | return r; |
1570 | 0 | } |
1571 | | |
1572 | | |
1573 | | |
1574 | | static int sc_hsm_public_key_auth_status(sc_card_t *card, |
1575 | | sc_cardctl_sc_hsm_pka_status_t *status) |
1576 | 0 | { |
1577 | 0 | u8 recvbuf[4]; |
1578 | 0 | sc_context_t *ctx = card->ctx; |
1579 | 0 | sc_apdu_t apdu; |
1580 | 0 | int r; |
1581 | |
|
1582 | 0 | LOG_FUNC_CALLED(card->ctx); |
1583 | | |
1584 | | /* get status */ |
1585 | 0 | sc_format_apdu_ex(&apdu, 0x00, 0x54, 0x00, 0x00, NULL, 0, recvbuf, sizeof recvbuf); |
1586 | 0 | apdu.cla = 0x80; |
1587 | |
|
1588 | 0 | r = sc_transmit_apdu(card, &apdu); |
1589 | 0 | LOG_TEST_RET(ctx, r, "APDU transmit failed"); |
1590 | | |
1591 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1592 | 0 | LOG_TEST_RET(ctx, r, "Check SW error"); |
1593 | | |
1594 | 0 | status->num_total = recvbuf[0]; |
1595 | 0 | status->num_missing = recvbuf[1]; |
1596 | 0 | status->num_required = recvbuf[2]; |
1597 | 0 | status->num_authenticated = recvbuf[3]; |
1598 | |
|
1599 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1600 | 0 | } |
1601 | | |
1602 | | |
1603 | | |
1604 | | static int sc_hsm_init_token(sc_card_t *card, sc_cardctl_pkcs11_init_token_t *params) |
1605 | 0 | { |
1606 | 0 | sc_context_t *ctx = card->ctx; |
1607 | 0 | sc_cardctl_sc_hsm_init_param_t ip; |
1608 | 0 | int r; |
1609 | 0 | char label[33],*cpo; |
1610 | |
|
1611 | 0 | LOG_FUNC_CALLED(ctx); |
1612 | |
|
1613 | 0 | if (params->so_pin_len != 16) { |
1614 | 0 | LOG_TEST_RET(ctx, SC_ERROR_INVALID_DATA, "SO PIN wrong length (!=16)"); |
1615 | 0 | } |
1616 | | |
1617 | 0 | memset(&ip, 0, sizeof(ip)); |
1618 | 0 | ip.dkek_shares = -1; |
1619 | 0 | ip.options[0] = 0x00; |
1620 | 0 | ip.options[1] = 0x01; |
1621 | |
|
1622 | 0 | r = sc_hsm_encode_sopin(params->so_pin, ip.init_code); |
1623 | 0 | LOG_TEST_RET(ctx, r, "SO PIN wrong format"); |
1624 | | |
1625 | 0 | ip.user_pin = ip.init_code; // Use the first 6 bytes of the SO-PIN as initial User-PIN value |
1626 | 0 | ip.user_pin_len = 6; |
1627 | 0 | ip.user_pin_retry_counter = 3; |
1628 | |
|
1629 | 0 | if (params->label) { |
1630 | | // Strip trailing spaces |
1631 | 0 | memcpy(label, params->label, 32); |
1632 | 0 | label[32] = 0; |
1633 | 0 | cpo = label + 31; |
1634 | 0 | while ((cpo >= label) && (*cpo == ' ')) { |
1635 | 0 | *cpo = 0; |
1636 | 0 | cpo--; |
1637 | 0 | } |
1638 | 0 | ip.label = label; |
1639 | 0 | } |
1640 | |
|
1641 | 0 | r = sc_hsm_initialize(card, &ip); |
1642 | 0 | LOG_TEST_RET(ctx, r, "Check SW error"); |
1643 | | |
1644 | 0 | LOG_FUNC_RETURN(ctx, SC_SUCCESS); |
1645 | 0 | } |
1646 | | |
1647 | | |
1648 | | |
1649 | | static int sc_hsm_init_pin(sc_card_t *card, sc_cardctl_pkcs11_init_pin_t *params) |
1650 | 13 | { |
1651 | 13 | sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data; |
1652 | 13 | sc_context_t *ctx = card->ctx; |
1653 | 13 | int r; |
1654 | 13 | sc_apdu_t apdu; |
1655 | 13 | u8 ibuff[50], *p; |
1656 | | |
1657 | 13 | LOG_FUNC_CALLED(card->ctx); |
1658 | | |
1659 | 13 | if (params->pin_len > 16) { |
1660 | 2 | LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_DATA, "User PIN too long"); |
1661 | 2 | } |
1662 | | |
1663 | 11 | p = ibuff; |
1664 | | |
1665 | 11 | memcpy(p, priv->sopin, sizeof(priv->sopin)); |
1666 | 11 | p += sizeof(priv->sopin); |
1667 | | |
1668 | 11 | memcpy(p, params->pin, params->pin_len); |
1669 | 11 | p += params->pin_len; |
1670 | | |
1671 | 11 | sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x2C, 0x00, 0x81); |
1672 | 11 | apdu.data = ibuff; |
1673 | 11 | apdu.datalen = p - ibuff; |
1674 | 11 | apdu.lc = apdu.datalen; |
1675 | | |
1676 | 11 | r = sc_transmit_apdu(card, &apdu); |
1677 | 11 | LOG_TEST_RET(ctx, r, "APDU transmit failed"); |
1678 | | |
1679 | 10 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1680 | | |
1681 | | // Cards before version 1.0 do not implement RESET_RETRY_COUNTER |
1682 | | // For those cards the CHANGE REFERENCE DATA command is used instead |
1683 | 10 | if (r == SC_ERROR_INS_NOT_SUPPORTED) { |
1684 | 6 | p = ibuff; |
1685 | 6 | memcpy(p, priv->sopin, 6); |
1686 | 6 | p += 6; |
1687 | | |
1688 | 6 | memcpy(p, params->pin, params->pin_len); |
1689 | 6 | p += params->pin_len; |
1690 | | |
1691 | 6 | sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x24, 0x00, 0x81); |
1692 | 6 | apdu.data = ibuff; |
1693 | 6 | apdu.datalen = p - ibuff; |
1694 | 6 | apdu.lc = apdu.datalen; |
1695 | | |
1696 | 6 | r = sc_transmit_apdu(card, &apdu); |
1697 | 6 | LOG_TEST_RET(ctx, r, "APDU transmit failed"); |
1698 | | |
1699 | 5 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1700 | 5 | } |
1701 | | |
1702 | 9 | LOG_TEST_RET(ctx, r, "Check SW error"); |
1703 | | |
1704 | 1 | memset(priv->sopin, 0, sizeof(priv->sopin)); |
1705 | | |
1706 | 1 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1707 | 1 | } |
1708 | | |
1709 | | |
1710 | | |
1711 | | static int sc_hsm_generate_keypair(sc_card_t *card, sc_cardctl_sc_hsm_keygen_info_t *keyinfo) |
1712 | 530 | { |
1713 | 530 | u8 rbuf[1200]; |
1714 | 530 | int r; |
1715 | 530 | sc_apdu_t apdu; |
1716 | | |
1717 | 530 | LOG_FUNC_CALLED(card->ctx); |
1718 | | |
1719 | 530 | sc_format_apdu(card, &apdu, SC_APDU_CASE_4_EXT, 0x46, keyinfo->key_id, keyinfo->auth_key_id); |
1720 | 530 | apdu.cla = 0x00; |
1721 | 530 | apdu.resp = rbuf; |
1722 | 530 | apdu.resplen = sizeof(rbuf); |
1723 | 530 | apdu.le = 0; |
1724 | | |
1725 | 530 | apdu.data = keyinfo->gakprequest; |
1726 | 530 | apdu.lc = keyinfo->gakprequest_len; |
1727 | 530 | apdu.datalen = keyinfo->gakprequest_len; |
1728 | | |
1729 | 530 | r = sc_transmit_apdu(card, &apdu); |
1730 | 530 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
1731 | | |
1732 | 527 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1733 | 527 | LOG_TEST_RET(card->ctx, r, "Check SW error"); |
1734 | | |
1735 | 37 | keyinfo->gakpresponse_len = apdu.resplen; |
1736 | 37 | keyinfo->gakpresponse = malloc(apdu.resplen); |
1737 | | |
1738 | 37 | if (keyinfo->gakpresponse == NULL) { |
1739 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY); |
1740 | 0 | } |
1741 | | |
1742 | 37 | memcpy(keyinfo->gakpresponse, apdu.resp, apdu.resplen); |
1743 | | |
1744 | 37 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1745 | 37 | } |
1746 | | |
1747 | | |
1748 | | |
1749 | | static int sc_hsm_card_ctl(sc_card_t *card, unsigned long cmd, void *ptr) |
1750 | 1.66k | { |
1751 | 1.66k | switch (cmd) { |
1752 | 336 | case SC_CARDCTL_GET_SERIALNR: |
1753 | 336 | return sc_hsm_get_serialnr(card, (sc_serial_number_t *)ptr); |
1754 | 0 | case SC_CARDCTL_PKCS11_INIT_TOKEN: |
1755 | 0 | return sc_hsm_init_token(card, (sc_cardctl_pkcs11_init_token_t *)ptr); |
1756 | 13 | case SC_CARDCTL_PKCS11_INIT_PIN: |
1757 | 13 | return sc_hsm_init_pin(card, (sc_cardctl_pkcs11_init_pin_t *)ptr); |
1758 | 530 | case SC_CARDCTL_SC_HSM_GENERATE_KEY: |
1759 | 530 | return sc_hsm_generate_keypair(card, (sc_cardctl_sc_hsm_keygen_info_t *)ptr); |
1760 | 0 | case SC_CARDCTL_SC_HSM_INITIALIZE: |
1761 | 0 | return sc_hsm_initialize(card, (sc_cardctl_sc_hsm_init_param_t *)ptr); |
1762 | 0 | case SC_CARDCTL_SC_HSM_IMPORT_DKEK_SHARE: |
1763 | 0 | return sc_hsm_import_dkek_share(card, (sc_cardctl_sc_hsm_dkek_t *)ptr); |
1764 | 0 | case SC_CARDCTL_SC_HSM_WRAP_KEY: |
1765 | 0 | return sc_hsm_wrap_key(card, (sc_cardctl_sc_hsm_wrapped_key_t *)ptr); |
1766 | 0 | case SC_CARDCTL_SC_HSM_UNWRAP_KEY: |
1767 | 0 | return sc_hsm_unwrap_key(card, (sc_cardctl_sc_hsm_wrapped_key_t *)ptr); |
1768 | 0 | case SC_CARDCTL_SC_HSM_REGISTER_PUBLIC_KEY: |
1769 | 0 | return sc_hsm_register_public_key(card, ptr); |
1770 | 0 | case SC_CARDCTL_SC_HSM_PUBLIC_KEY_AUTH_STATUS: |
1771 | 0 | return sc_hsm_public_key_auth_status(card, ptr); |
1772 | 1.66k | } |
1773 | 783 | return SC_ERROR_NOT_SUPPORTED; |
1774 | 1.66k | } |
1775 | | |
1776 | | |
1777 | | |
1778 | | static int sc_hsm_init(struct sc_card *card) |
1779 | 1.70k | { |
1780 | | #if defined(ENABLE_SM) && defined(ENABLE_OPENPACE) && defined(_WIN32) |
1781 | | char expanded_val[PATH_MAX]; |
1782 | | size_t expanded_len = PATH_MAX; |
1783 | | #endif |
1784 | 1.70k | int flags,ext_flags; |
1785 | 1.70k | sc_file_t *file = NULL; |
1786 | 1.70k | sc_path_t path; |
1787 | 1.70k | sc_hsm_private_data_t *priv = NULL; |
1788 | | |
1789 | 1.70k | LOG_FUNC_CALLED(card->ctx); |
1790 | | |
1791 | 1.70k | flags = SC_ALGORITHM_RSA_RAW|SC_ALGORITHM_RSA_PAD_PSS|SC_ALGORITHM_ONBOARD_KEY_GEN |
1792 | 1.70k | |SC_ALGORITHM_RSA_HASH_SHA1|SC_ALGORITHM_RSA_HASH_SHA256|SC_ALGORITHM_RSA_HASH_SHA384|SC_ALGORITHM_RSA_HASH_SHA512 |
1793 | 1.70k | |SC_ALGORITHM_MGF1_SHA256|SC_ALGORITHM_MGF1_SHA384|SC_ALGORITHM_MGF1_SHA512; |
1794 | | |
1795 | 1.70k | _sc_card_add_rsa_alg(card, 1024, flags, 0); |
1796 | 1.70k | _sc_card_add_rsa_alg(card, 1536, flags, 0); |
1797 | 1.70k | _sc_card_add_rsa_alg(card, 2048, flags, 0); |
1798 | 1.70k | _sc_card_add_rsa_alg(card, 3072, flags, 0); |
1799 | 1.70k | _sc_card_add_rsa_alg(card, 4096, flags, 0); |
1800 | | |
1801 | 1.70k | flags = SC_ALGORITHM_ECDSA_RAW| |
1802 | 1.70k | SC_ALGORITHM_ECDH_CDH_RAW| |
1803 | 1.70k | SC_ALGORITHM_ECDSA_HASH_NONE| |
1804 | 1.70k | SC_ALGORITHM_ECDSA_HASH_SHA1| |
1805 | 1.70k | SC_ALGORITHM_ECDSA_HASH_SHA224| |
1806 | 1.70k | SC_ALGORITHM_ECDSA_HASH_SHA256| |
1807 | 1.70k | SC_ALGORITHM_ECDSA_HASH_SHA384| |
1808 | 1.70k | SC_ALGORITHM_ECDSA_HASH_SHA512| |
1809 | 1.70k | SC_ALGORITHM_ONBOARD_KEY_GEN; |
1810 | | |
1811 | 1.70k | ext_flags = SC_ALGORITHM_EXT_EC_F_P| |
1812 | 1.70k | SC_ALGORITHM_EXT_EC_ECPARAMETERS| |
1813 | 1.70k | SC_ALGORITHM_EXT_EC_NAMEDCURVE| |
1814 | 1.70k | SC_ALGORITHM_EXT_EC_UNCOMPRESES| |
1815 | 1.70k | SC_ALGORITHM_ONBOARD_KEY_GEN; |
1816 | 1.70k | _sc_card_add_ec_alg(card, 192, flags, ext_flags, NULL); |
1817 | 1.70k | _sc_card_add_ec_alg(card, 224, flags, ext_flags, NULL); |
1818 | 1.70k | _sc_card_add_ec_alg(card, 256, flags, ext_flags, NULL); |
1819 | 1.70k | _sc_card_add_ec_alg(card, 320, flags, ext_flags, NULL); |
1820 | 1.70k | _sc_card_add_ec_alg(card, 384, flags, ext_flags, NULL); |
1821 | 1.70k | _sc_card_add_ec_alg(card, 512, flags, ext_flags, NULL); |
1822 | 1.70k | _sc_card_add_ec_alg(card, 521, flags, ext_flags, NULL); |
1823 | | |
1824 | 1.70k | card->caps |= SC_CARD_CAP_RNG|SC_CARD_CAP_APDU_EXT|SC_CARD_CAP_ISO7816_PIN_INFO; |
1825 | | |
1826 | | // APDU Size limits |
1827 | | // JCOP 2.4.1r3 1462 |
1828 | | // JCOP 2.4.2r3 1454 |
1829 | | // JCOP 3 1232 |
1830 | | // JCOP 4 1454 |
1831 | | // MicroSD with JCOP 3 478 / 506 - handled in reader-pcsc.c |
1832 | | // Reiner SCT 1014 - handled in reader-pcsc.c |
1833 | | // |
1834 | | // Note, that these are limits for the whole APDU, but the semantics of max_send_size |
1835 | | // is the size of the APDU send buffer so to get the right value from them, we need |
1836 | | // to subtract APDU headers (CLA, INS, P1, P2, Lc (3B)), 7 bytes altogether for |
1837 | | // class 3 APDU |
1838 | | // (or 9 bytes for case 4 when we pass in >255 B data and expect return of more than 255 B) |
1839 | | |
1840 | | // Use JCOP 3 (smallest unhandled by reader limitation) card limits for sending |
1841 | | // And make it 9 smaller to make sure we fit the rest of the APDU. |
1842 | 1.70k | card->max_send_size = 1232 - 9; |
1843 | | // Assume that card supports sending with extended length APDU and without limit |
1844 | 1.70k | card->max_recv_size = 0; |
1845 | | |
1846 | 1.70k | if (card->type == SC_CARD_TYPE_SC_HSM_SOC |
1847 | 1.70k | || card->type == SC_CARD_TYPE_SC_HSM_GOID) { |
1848 | 1.67k | card->max_recv_size = 0x0630; // SoC Proxy forces this limit |
1849 | 1.67k | } else { |
1850 | | // Adjust to the limits set by the reader |
1851 | 28 | if (card->reader->max_send_size < card->max_send_size) { |
1852 | 28 | if (18 >= card->reader->max_send_size) |
1853 | 28 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INCONSISTENT_CONFIGURATION); |
1854 | | |
1855 | | // 17 byte header and TLV because of odd ins in UPDATE BINARY |
1856 | 0 | card->max_send_size = card->reader->max_send_size - 17; |
1857 | 0 | } |
1858 | | |
1859 | 0 | if (0 < card->reader->max_recv_size) { |
1860 | 0 | if (3 >= card->reader->max_recv_size) |
1861 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INCONSISTENT_CONFIGURATION); |
1862 | 0 | card->max_recv_size = card->reader->max_recv_size - 2; |
1863 | 0 | } |
1864 | 0 | } |
1865 | | |
1866 | 1.67k | priv = card->drv_data; |
1867 | 1.67k | if (!priv) { |
1868 | 1.67k | priv = calloc(1, sizeof(sc_hsm_private_data_t)); |
1869 | 1.67k | if (!priv) |
1870 | 1.67k | LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY); |
1871 | 1.67k | card->drv_data = priv; |
1872 | 1.67k | } |
1873 | | |
1874 | 1.67k | sc_path_set(&path, SC_PATH_TYPE_DF_NAME, sc_hsm_aid.value, sc_hsm_aid.len, 0, 0); |
1875 | 1.67k | if (sc_hsm_select_file_ex(card, &path, 0, &file) == SC_SUCCESS |
1876 | 289 | && file && file->prop_attr && file->prop_attr_len >= 2) { |
1877 | 25 | static char card_name[SC_MAX_APDU_BUFFER_SIZE]; |
1878 | 25 | u8 type = 0xFF; |
1879 | 25 | u8 major = file->prop_attr[file->prop_attr_len - 2]; |
1880 | 25 | u8 minor = file->prop_attr[file->prop_attr_len - 1]; |
1881 | 25 | char p00[] = "SmartCard-HSM Applet for JCOP"; |
1882 | 25 | char p01[] = "SmartCard-HSM Demo Applet for JCOP"; |
1883 | 25 | char *p = "SmartCard-HSM"; |
1884 | 25 | if (file->prop_attr_len >= 3) { |
1885 | 16 | type = file->prop_attr[file->prop_attr_len - 3]; |
1886 | 16 | } |
1887 | 25 | switch (type) { |
1888 | 7 | case 0x00: |
1889 | 7 | p = p00; |
1890 | 7 | break; |
1891 | 7 | case 0x01: |
1892 | 7 | p = p01; |
1893 | 7 | break; |
1894 | 11 | default: |
1895 | 11 | break; |
1896 | 25 | } |
1897 | 25 | snprintf(card_name, sizeof card_name, "%s version %u.%u", p, major, minor); |
1898 | 25 | card->name = card_name; |
1899 | | |
1900 | 25 | if (file->prop_attr[1] & 0x04) { |
1901 | 11 | card->caps |= SC_CARD_CAP_SESSION_PIN; |
1902 | 11 | } |
1903 | 25 | } |
1904 | 1.67k | sc_file_free(file); |
1905 | | |
1906 | 1.67k | priv->EF_C_DevAut = NULL; |
1907 | 1.67k | priv->EF_C_DevAut_len = 0; |
1908 | | |
1909 | 1.67k | #if defined(ENABLE_SM) && defined(ENABLE_OPENPACE) |
1910 | 1.67k | EAC_init(); |
1911 | | #ifdef _WIN32 |
1912 | | expanded_len = ExpandEnvironmentStringsA(CVCDIR, expanded_val, sizeof expanded_val); |
1913 | | if (0 < expanded_len && expanded_len < sizeof expanded_val) |
1914 | | EAC_set_cvc_default_dir(expanded_val); |
1915 | | #else |
1916 | 1.67k | EAC_set_cvc_default_dir(CVCDIR); |
1917 | 1.67k | #endif |
1918 | 1.67k | #endif |
1919 | | |
1920 | 1.67k | return 0; |
1921 | 1.67k | } |
1922 | | |
1923 | | |
1924 | | |
1925 | | static int sc_hsm_finish(sc_card_t * card) |
1926 | 1.67k | { |
1927 | 1.67k | sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data; |
1928 | 1.67k | #ifdef ENABLE_SM |
1929 | 1.67k | sc_sm_stop(card); |
1930 | 1.67k | #endif |
1931 | 1.67k | if (priv) { |
1932 | 1.67k | free(priv->serialno); |
1933 | 1.67k | sc_file_free(priv->dffcp); |
1934 | 1.67k | free(priv->EF_C_DevAut); |
1935 | 1.67k | } |
1936 | 1.67k | free(priv); |
1937 | | |
1938 | 1.67k | return SC_SUCCESS; |
1939 | 1.67k | } |
1940 | | |
1941 | | |
1942 | | |
1943 | | static struct sc_card_driver * sc_get_driver(void) |
1944 | 99.2k | { |
1945 | 99.2k | struct sc_card_driver *iso_drv = sc_get_iso7816_driver(); |
1946 | | |
1947 | 99.2k | if (iso_ops == NULL) |
1948 | 9 | iso_ops = iso_drv->ops; |
1949 | | |
1950 | 99.2k | sc_hsm_ops = *iso_drv->ops; |
1951 | 99.2k | sc_hsm_ops.match_card = sc_hsm_match_card; |
1952 | 99.2k | sc_hsm_ops.select_file = sc_hsm_select_file; |
1953 | 99.2k | sc_hsm_ops.get_challenge = sc_hsm_get_challenge; |
1954 | 99.2k | sc_hsm_ops.read_binary = sc_hsm_read_binary; |
1955 | 99.2k | sc_hsm_ops.update_binary = sc_hsm_update_binary; |
1956 | 99.2k | sc_hsm_ops.list_files = sc_hsm_list_files; |
1957 | 99.2k | sc_hsm_ops.create_file = sc_hsm_create_file; |
1958 | 99.2k | sc_hsm_ops.delete_file = sc_hsm_delete_file; |
1959 | 99.2k | sc_hsm_ops.set_security_env = sc_hsm_set_security_env; |
1960 | 99.2k | sc_hsm_ops.compute_signature = sc_hsm_compute_signature; |
1961 | 99.2k | sc_hsm_ops.decipher = sc_hsm_decipher; |
1962 | 99.2k | sc_hsm_ops.init = sc_hsm_init; |
1963 | 99.2k | sc_hsm_ops.finish = sc_hsm_finish; |
1964 | 99.2k | sc_hsm_ops.card_ctl = sc_hsm_card_ctl; |
1965 | 99.2k | sc_hsm_ops.pin_cmd = sc_hsm_pin_cmd; |
1966 | 99.2k | sc_hsm_ops.logout = sc_hsm_logout; |
1967 | | |
1968 | | /* no record oriented file services */ |
1969 | 99.2k | sc_hsm_ops.read_record = NULL; |
1970 | 99.2k | sc_hsm_ops.write_record = NULL; |
1971 | 99.2k | sc_hsm_ops.append_record = NULL; |
1972 | 99.2k | sc_hsm_ops.update_record = NULL; |
1973 | | |
1974 | 99.2k | return &sc_hsm_drv; |
1975 | 99.2k | } |
1976 | | |
1977 | | |
1978 | | |
1979 | | struct sc_card_driver * sc_get_sc_hsm_driver(void) |
1980 | 99.2k | { |
1981 | 99.2k | return sc_get_driver(); |
1982 | 99.2k | } |
1983 | | |