/src/opensc/src/libopensc/card-sc-hsm.c
Line | Count | Source (jump to first uncovered line) |
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 | 7.26k | { |
149 | 7.26k | int rv; |
150 | 7.26k | sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data; |
151 | 7.26k | sc_file_t *file = NULL; |
152 | 7.26k | sc_path_t cpath; |
153 | 7.26k | size_t card_max_recv_size = card->max_recv_size; |
154 | 7.26k | size_t reader_max_recv_size = card->reader->max_recv_size; |
155 | | |
156 | 7.26k | if (file_out == NULL) { // Versions before 0.16 of the SmartCard-HSM do not support P2='0C' |
157 | 465 | rv = sc_hsm_select_file_ex(card, in_path, forceselect, &file); |
158 | 465 | sc_file_free(file); |
159 | 465 | return rv; |
160 | 465 | } |
161 | | |
162 | 6.79k | 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 | 6.79k | if (in_path->type == SC_PATH_TYPE_DF_NAME |
180 | 6.79k | || (in_path->type == SC_PATH_TYPE_PATH |
181 | 615 | && in_path->len == sc_hsm_aid.len |
182 | 615 | && !memcmp(in_path->value, sc_hsm_aid.value, sc_hsm_aid.len)) |
183 | 6.79k | || (in_path->type == SC_PATH_TYPE_PATH |
184 | 615 | && in_path->len == 0 |
185 | 615 | && in_path->aid.len == sc_hsm_aid.len |
186 | 6.18k | && !memcmp(in_path->aid.value, sc_hsm_aid.value, sc_hsm_aid.len))) { |
187 | 6.18k | if (!priv || (priv->dffcp == NULL) || forceselect) { |
188 | | /* Force use of Le = 0x00 in iso7816_select_file as required by SC-HSM */ |
189 | 6.07k | card->max_recv_size = card->reader->max_recv_size = SC_READER_SHORT_APDU_MAX_RECV_SIZE; |
190 | 6.07k | rv = (*iso_ops->select_file)(card, in_path, file_out); |
191 | 6.07k | card->max_recv_size = card_max_recv_size; |
192 | 6.07k | card->reader->max_recv_size = reader_max_recv_size; |
193 | 6.07k | LOG_TEST_RET(card->ctx, rv, "Could not select SmartCard-HSM application"); |
194 | | |
195 | 446 | if (priv) { |
196 | 94 | sc_file_free(priv->dffcp); |
197 | | // Cache the FCP returned when selecting the applet |
198 | 94 | sc_file_dup(&priv->dffcp, *file_out); |
199 | 94 | } |
200 | 446 | } else { |
201 | 112 | sc_file_dup(file_out, priv->dffcp); |
202 | 112 | rv = SC_SUCCESS; |
203 | 112 | } |
204 | 558 | return rv; |
205 | 6.18k | } |
206 | | |
207 | 615 | 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 | 430 | if (in_path->len == 2) { |
210 | 0 | file = sc_file_new(); |
211 | 0 | if (file == NULL) |
212 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY); |
213 | 0 | file->path = *in_path; |
214 | 0 | file->id = 0x3F00; |
215 | 0 | file->type = SC_FILE_TYPE_DF; |
216 | 0 | file->magic = SC_FILE_MAGIC; |
217 | |
|
218 | 0 | *file_out = file; |
219 | 0 | return SC_SUCCESS; |
220 | 430 | } else { |
221 | | /* Force use of Le = 0x00 in iso7816_select_file as required by SC-HSM */ |
222 | 430 | card->max_recv_size = card->reader->max_recv_size = SC_READER_SHORT_APDU_MAX_RECV_SIZE; |
223 | 430 | sc_path_t truncated; |
224 | 430 | memcpy(&truncated, in_path, sizeof truncated); |
225 | 430 | truncated.len = in_path->len - 2; |
226 | 430 | memcpy(truncated.value, in_path->value+2, truncated.len); |
227 | 430 | rv = (*iso_ops->select_file)(card, &truncated, file_out); |
228 | 430 | card->max_recv_size = card_max_recv_size; |
229 | 430 | card->reader->max_recv_size = reader_max_recv_size; |
230 | 430 | return rv; |
231 | 430 | } |
232 | 430 | } |
233 | | /* Force use of Le = 0x00 in iso7816_select_file as required by SC-HSM */ |
234 | 185 | card->max_recv_size = card->reader->max_recv_size = SC_READER_SHORT_APDU_MAX_RECV_SIZE; |
235 | 185 | rv = (*iso_ops->select_file)(card, in_path, file_out); |
236 | 185 | card->max_recv_size = card_max_recv_size; |
237 | 185 | card->reader->max_recv_size = reader_max_recv_size; |
238 | 185 | return rv; |
239 | 615 | } |
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 | 6.64k | { |
247 | 6.64k | return sc_hsm_select_file_ex(card, in_path, 0, file_out); |
248 | 6.64k | } |
249 | | |
250 | | |
251 | | |
252 | | static int sc_hsm_get_challenge(struct sc_card *card, unsigned char *rnd, size_t len) |
253 | 0 | { |
254 | 0 | LOG_FUNC_CALLED(card->ctx); |
255 | |
|
256 | 0 | if (len > 1024) { |
257 | 0 | len = 1024; |
258 | 0 | } |
259 | |
|
260 | 0 | LOG_FUNC_RETURN(card->ctx, iso_ops->get_challenge(card, rnd, len)); |
261 | 0 | } |
262 | | |
263 | | |
264 | | |
265 | | static int sc_hsm_match_card(struct sc_card *card) |
266 | 5.80k | { |
267 | 5.80k | sc_path_t path; |
268 | 5.80k | int i, r, type = 0; |
269 | 5.80k | sc_file_t *file = NULL; |
270 | | |
271 | 5.80k | i = _sc_match_atr(card, sc_hsm_atrs, &type); |
272 | 5.80k | if (i >= 0 && type != SC_CARD_TYPE_SC_HSM_SOC) { |
273 | 149 | card->type = type; |
274 | 149 | return 1; |
275 | 149 | } |
276 | | |
277 | 5.65k | sc_path_set(&path, SC_PATH_TYPE_DF_NAME, sc_hsm_aid.value, sc_hsm_aid.len, 0, 0); |
278 | 5.65k | r = sc_hsm_select_file(card, &path, &file); |
279 | 5.65k | 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 | 352 | if (file != NULL) { |
283 | 352 | size_t sz = file->prop_attr_len; |
284 | 352 | sc_file_free(file); |
285 | 352 | if (sz < 2) { |
286 | 351 | return 0; |
287 | 351 | } |
288 | 352 | } |
289 | | |
290 | 1 | if (type == SC_CARD_TYPE_SC_HSM_SOC) { |
291 | 0 | card->type = SC_CARD_TYPE_SC_HSM_SOC; |
292 | 1 | } else { |
293 | 1 | card->type = SC_CARD_TYPE_SC_HSM; |
294 | 1 | } |
295 | | |
296 | 1 | return 1; |
297 | 352 | } |
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 | | { |
495 | | int r, protocol; |
496 | | sc_path_t path; |
497 | | u8 all_certs[1024]; |
498 | | EAC_CTX *ctx = NULL; |
499 | | size_t all_certs_len = sizeof all_certs, left, device_cert_len, issuer_cert_len; |
500 | | const unsigned char *cert = all_certs, *device_cert, *issuer_cert; |
501 | | BUF_MEM *comp_pub_key = NULL; |
502 | | sc_cvc_t cvc_device, cvc_issuer; |
503 | | /* this is only needed to call sc_pkcs15emu_sc_hsm_decode_cvc */ |
504 | | sc_pkcs15_card_t p15card; |
505 | | 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 | | p15card.card = card; |
509 | | |
510 | | memset(&cvc_device, 0, sizeof(cvc_device)); |
511 | | memset(&cvc_issuer, 0, sizeof(cvc_issuer)); |
512 | | |
513 | | |
514 | | if (priv->EF_C_DevAut && priv->EF_C_DevAut_len) { |
515 | | all_certs_len = priv->EF_C_DevAut_len; |
516 | | cert = priv->EF_C_DevAut; |
517 | | } else { |
518 | | /* get issuer and device certificate from the card */ |
519 | | r = sc_path_set(&path, SC_PATH_TYPE_FILE_ID, (u8 *) "\x2F\x02", 2, 0, 0); |
520 | | if (r < 0) |
521 | | goto err; |
522 | | r = sc_select_file(card, &path, NULL); |
523 | | if (r < 0) |
524 | | goto err; |
525 | | r = sc_read_binary(card, 0, all_certs, all_certs_len, 0); |
526 | | if (r < 0) |
527 | | goto err; |
528 | | if (r == 0) { |
529 | | r = SC_ERROR_FILE_NOT_FOUND; |
530 | | goto err; |
531 | | } |
532 | | |
533 | | all_certs_len = r; |
534 | | |
535 | | /* save EF_C_DevAut for further use */ |
536 | | cert = realloc(priv->EF_C_DevAut, all_certs_len); |
537 | | if (cert) { |
538 | | memcpy((unsigned char *) cert, all_certs, all_certs_len); |
539 | | priv->EF_C_DevAut = (unsigned char *) cert; |
540 | | priv->EF_C_DevAut_len = all_certs_len; |
541 | | } |
542 | | |
543 | | cert = all_certs; |
544 | | } |
545 | | left = all_certs_len; |
546 | | |
547 | | device_cert = cert; |
548 | | r = sc_pkcs15emu_sc_hsm_decode_cvc(&p15card, &cert, &left, &cvc_device); |
549 | | if (r < 0) |
550 | | goto err; |
551 | | device_cert_len = all_certs_len - left; |
552 | | |
553 | | issuer_cert = cert; |
554 | | r = sc_pkcs15emu_sc_hsm_decode_cvc(&p15card, &cert, &left, &cvc_issuer); |
555 | | if (r < 0) |
556 | | goto err; |
557 | | issuer_cert_len = all_certs_len - device_cert_len - left; |
558 | | |
559 | | ctx = EAC_CTX_new(); |
560 | | if (!ctx) { |
561 | | r = SC_ERROR_INTERNAL; |
562 | | goto err; |
563 | | } |
564 | | |
565 | | |
566 | | /* check all CVCs given of the document's pki */ |
567 | | if (!TA_STEP2_import_certificate(ctx, issuer_cert, issuer_cert_len) |
568 | | || !TA_STEP2_import_certificate(ctx, device_cert, device_cert_len)) { |
569 | | r = SC_ERROR_INTERNAL; |
570 | | goto err; |
571 | | } |
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 | | protocol = NID_id_CA_ECDH_AES_CBC_CMAC_128; |
578 | | |
579 | | /* initialize CA domain parameter with the document's public key */ |
580 | | if (!EAC_CTX_init_ca(ctx, protocol, 8)) { |
581 | | r = SC_ERROR_INTERNAL; |
582 | | goto err; |
583 | | } |
584 | | EVP_PKEY_free(ctx->ca_ctx->ka_ctx->key); |
585 | | if (!EVP_PKEY_up_ref(ctx->ta_ctx->pub_key)) { |
586 | | sc_log_openssl(card->ctx); |
587 | | r = SC_ERROR_INTERNAL; |
588 | | goto err; |
589 | | } |
590 | | ctx->ca_ctx->ka_ctx->key = ctx->ta_ctx->pub_key; |
591 | | |
592 | | /* generate keys for CA */ |
593 | | comp_pub_key = TA_STEP3_generate_ephemeral_key(ctx); |
594 | | r = perform_chip_authentication_ex(card, ctx, |
595 | | cvc_device.publicPoint, cvc_device.publicPointlen); |
596 | | |
597 | | err: |
598 | | if (r < 0) |
599 | | EAC_CTX_clear_free(ctx); |
600 | | if (comp_pub_key) |
601 | | BUF_MEM_free(comp_pub_key); |
602 | | sc_pkcs15emu_sc_hsm_free_cvc(&cvc_device); |
603 | | sc_pkcs15emu_sc_hsm_free_cvc(&cvc_issuer); |
604 | | |
605 | | return r; |
606 | | } |
607 | | |
608 | | #else |
609 | | |
610 | | static int sc_hsm_perform_chip_authentication(sc_card_t *card) |
611 | 0 | { |
612 | 0 | return SC_ERROR_NOT_SUPPORTED; |
613 | 0 | } |
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 | 13 | { |
621 | 13 | sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data; |
622 | 13 | sc_apdu_t apdu; |
623 | 13 | u8 cmdbuff[16]; |
624 | 13 | #ifdef ENABLE_SM |
625 | 13 | u8 rbuf[SC_MAX_APDU_BUFFER_SIZE]; |
626 | 13 | #endif |
627 | 13 | int r; |
628 | 13 | int cmd = data->cmd; |
629 | 13 | size_t pin2_len = data->pin2.len; |
630 | | |
631 | 13 | 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 | 13 | if ((card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH) |
640 | 13 | && (data->cmd == SC_PIN_CMD_CHANGE) |
641 | 13 | && (data->pin_reference == 0x81) |
642 | 13 | && (!data->pin1.data || data->pin1.len <= 0)) { |
643 | 0 | return sc_hsm_soc_change(card, data, tries_left); |
644 | 13 | } else if ((card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH) |
645 | 13 | && (data->cmd == SC_PIN_CMD_UNBLOCK) |
646 | 13 | && (data->pin_reference == 0x81) |
647 | 13 | && (!data->pin1.data || data->pin1.len <= 0)) { |
648 | 0 | return sc_hsm_soc_unblock(card, data, tries_left); |
649 | 0 | } |
650 | | |
651 | 13 | #ifdef ENABLE_SM |
652 | | /* For contactless cards always establish a secure channel before PIN |
653 | | * verification. Also, Session PIN generation requires SM. */ |
654 | 13 | if ((card->type == SC_CARD_TYPE_SC_HSM_SOC |
655 | 13 | || card->type == SC_CARD_TYPE_SC_HSM_GOID |
656 | 13 | || card->reader->uid.len || cmd == SC_PIN_CMD_GET_SESSION_PIN) |
657 | 13 | && (data->cmd != SC_PIN_CMD_GET_INFO)) { |
658 | 0 | struct sc_pin_cmd_data check_sm_pin_data; |
659 | 0 | memset(&check_sm_pin_data, 0, sizeof(check_sm_pin_data)); |
660 | 0 | check_sm_pin_data.cmd = SC_PIN_CMD_GET_INFO; |
661 | 0 | check_sm_pin_data.pin_type = data->pin_type; |
662 | 0 | check_sm_pin_data.pin_reference = data->pin_reference; |
663 | |
|
664 | 0 | r = SC_ERROR_NOT_ALLOWED; |
665 | 0 | 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 | 0 | if (r == SC_ERROR_ASN1_OBJECT_NOT_FOUND || r == SC_ERROR_NOT_ALLOWED) { |
670 | | /* need to establish a new SM channel */ |
671 | 0 | LOG_TEST_RET(card->ctx, |
672 | 0 | sc_hsm_perform_chip_authentication(card), |
673 | 0 | "Could not perform chip authentication"); |
674 | 0 | } |
675 | 0 | } |
676 | 13 | #endif |
677 | | |
678 | 13 | if ((card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH) |
679 | 13 | && (data->cmd == SC_PIN_CMD_VERIFY) |
680 | 13 | && (data->pin_reference == 0x81) |
681 | 13 | && (!data->pin1.data || data->pin1.len <= 0)) { |
682 | 0 | r = sc_hsm_soc_biomatch(card, data, tries_left); |
683 | 13 | } else { |
684 | 13 | 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 | 13 | 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 | 13 | #ifdef ENABLE_SM |
714 | 13 | if ((data->cmd == SC_PIN_CMD_GET_INFO) |
715 | 13 | && (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 | 13 | #endif |
723 | | |
724 | 13 | data->pin1.offset = 5; |
725 | 13 | data->pin2.offset = 5; |
726 | | |
727 | 13 | r = (*iso_ops->pin_cmd)(card, data, tries_left); |
728 | 13 | data->apdu = NULL; |
729 | 13 | } |
730 | 13 | LOG_TEST_RET(card->ctx, r, "Verification failed"); |
731 | | |
732 | 5 | 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 | 5 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
776 | 5 | } |
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 | 626 | { |
800 | 626 | sc_context_t *ctx = card->ctx; |
801 | 626 | sc_apdu_t apdu; |
802 | 626 | u8 cmdbuff[4]; |
803 | 626 | int r; |
804 | | |
805 | 626 | 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 | 626 | cmdbuff[0] = 0x54; |
811 | 626 | cmdbuff[1] = 0x02; |
812 | 626 | cmdbuff[2] = (idx >> 8) & 0xFF; |
813 | 626 | cmdbuff[3] = idx & 0xFF; |
814 | | |
815 | 626 | assert(count <= sc_get_max_recv_size(card)); |
816 | 626 | sc_format_apdu(card, &apdu, SC_APDU_CASE_4, 0xB1, 0x00, 0x00); |
817 | 626 | apdu.data = cmdbuff; |
818 | 626 | apdu.datalen = 4; |
819 | 626 | apdu.lc = 4; |
820 | 626 | apdu.le = count; |
821 | 626 | apdu.resplen = count; |
822 | 626 | apdu.resp = buf; |
823 | | |
824 | 626 | r = sc_transmit_apdu(card, &apdu); |
825 | 626 | LOG_TEST_RET(ctx, r, "APDU transmit failed"); |
826 | | |
827 | 620 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
828 | 620 | if (r != SC_ERROR_FILE_END_REACHED) { |
829 | 586 | LOG_TEST_RET(ctx, r, "Check SW error"); |
830 | 586 | } |
831 | | |
832 | 530 | LOG_FUNC_RETURN(ctx, (int)apdu.resplen); |
833 | 530 | } |
834 | | |
835 | | |
836 | | /* NOTE: idx is an offset into the card's file, not into buf */ |
837 | | static int sc_hsm_write_ef(sc_card_t *card, |
838 | | int fid, |
839 | | unsigned int idx, const u8 *buf, size_t count) |
840 | 0 | { |
841 | 0 | sc_context_t *ctx = card->ctx; |
842 | 0 | sc_apdu_t apdu; |
843 | 0 | u8 *cmdbuff, *p; |
844 | 0 | size_t len; |
845 | 0 | int r; |
846 | |
|
847 | 0 | if (idx > 0xffff) { |
848 | 0 | sc_log(ctx, "invalid EF offset: 0x%X > 0xFFFF", idx); |
849 | 0 | return SC_ERROR_OFFSET_TOO_LARGE; |
850 | 0 | } |
851 | | |
852 | 0 | cmdbuff = malloc(8 + count); |
853 | 0 | if (!cmdbuff) { |
854 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY); |
855 | 0 | } |
856 | | |
857 | 0 | size_t bytes_left = count; |
858 | | // 8 bytes are required for T54(4) and T53(4) |
859 | 0 | size_t blk_size = card->max_send_size - 8; |
860 | 0 | size_t to_send = 0; |
861 | 0 | size_t file_offset = (size_t) idx; |
862 | 0 | size_t offset = 0; |
863 | 0 | do { |
864 | 0 | to_send = bytes_left >= blk_size ? blk_size : bytes_left; |
865 | 0 | p = cmdbuff; |
866 | | // ASN1 0x54 offset |
867 | 0 | *p++ = 0x54; |
868 | 0 | *p++ = 0x02; |
869 | 0 | *p++ = (file_offset >> 8) & 0xFF; |
870 | 0 | *p++ = file_offset & 0xFF; |
871 | | // ASN1 0x53 to_send |
872 | 0 | *p++ = 0x53; |
873 | 0 | if (to_send < 128) { |
874 | 0 | *p++ = (u8)to_send; |
875 | 0 | len = 6; |
876 | 0 | } else if (to_send < 256) { |
877 | 0 | *p++ = 0x81; |
878 | 0 | *p++ = (u8)to_send; |
879 | 0 | len = 7; |
880 | 0 | } else { |
881 | 0 | *p++ = 0x82; |
882 | 0 | *p++ = (to_send >> 8) & 0xFF; |
883 | 0 | *p++ = to_send & 0xFF; |
884 | 0 | len = 8; |
885 | 0 | } |
886 | |
|
887 | 0 | if (buf != NULL) |
888 | 0 | memcpy(p, buf+offset, to_send); |
889 | 0 | len += to_send; |
890 | |
|
891 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_3, 0xD7, fid >> 8, fid & 0xFF); |
892 | 0 | apdu.data = cmdbuff; |
893 | 0 | apdu.datalen = len; |
894 | 0 | apdu.lc = len; |
895 | |
|
896 | 0 | r = sc_transmit_apdu(card, &apdu); |
897 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "APDU transmit failed"); |
898 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
899 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "Check SW error"); |
900 | | |
901 | 0 | bytes_left -= to_send; |
902 | 0 | offset += to_send; |
903 | 0 | file_offset += to_send; |
904 | 0 | } while (0 < bytes_left); |
905 | | |
906 | 0 | err: |
907 | 0 | free(cmdbuff); |
908 | |
|
909 | 0 | LOG_FUNC_RETURN(ctx, (int)count); |
910 | 0 | } |
911 | | |
912 | | |
913 | | static int sc_hsm_update_binary(sc_card_t *card, |
914 | | unsigned int idx, const u8 *buf, size_t count, |
915 | | unsigned long flags) |
916 | 0 | { |
917 | 0 | return sc_hsm_write_ef(card, 0, idx, buf, count); |
918 | 0 | } |
919 | | |
920 | | |
921 | | |
922 | | static int sc_hsm_list_files(sc_card_t *card, u8 * buf, size_t buflen) |
923 | 0 | { |
924 | 0 | sc_apdu_t apdu; |
925 | 0 | u8 recvbuf[MAX_EXT_APDU_LENGTH]; |
926 | 0 | sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data; |
927 | 0 | int r; |
928 | |
|
929 | 0 | if (priv->noExtLength) { |
930 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_2, 0x58, 0, 0); |
931 | 0 | } else { |
932 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_2_EXT, 0x58, 0, 0); |
933 | 0 | } |
934 | 0 | apdu.cla = 0x80; |
935 | 0 | apdu.resp = recvbuf; |
936 | 0 | apdu.resplen = sizeof(recvbuf); |
937 | 0 | apdu.le = 0; |
938 | 0 | r = sc_transmit_apdu(card, &apdu); |
939 | |
|
940 | 0 | if ((r == SC_ERROR_TRANSMIT_FAILED) && (!priv->noExtLength)) { |
941 | 0 | sc_log(card->ctx, "No extended length support ? Trying fall-back to short APDUs, probably breaking support for RSA 2048 operations"); |
942 | 0 | priv->noExtLength = 1; |
943 | 0 | card->max_send_size = 248; // 255 - 7 because of TLV in odd ins UPDATE BINARY |
944 | 0 | return sc_hsm_list_files(card, buf, buflen); |
945 | 0 | } |
946 | 0 | LOG_TEST_RET(card->ctx, r, "ENUMERATE OBJECTS APDU transmit failed"); |
947 | | |
948 | 0 | if (buflen < apdu.resplen) |
949 | 0 | memcpy(buf, recvbuf, buflen); |
950 | 0 | else |
951 | 0 | memcpy(buf, recvbuf, apdu.resplen); |
952 | |
|
953 | 0 | LOG_FUNC_RETURN(card->ctx, (int)apdu.resplen); |
954 | 0 | } |
955 | | |
956 | | |
957 | | |
958 | | static int sc_hsm_create_file(sc_card_t *card, sc_file_t *file) |
959 | 0 | { |
960 | 0 | int r; |
961 | |
|
962 | 0 | r = sc_hsm_write_ef(card, file->id, 0, NULL, 0); |
963 | 0 | LOG_TEST_RET(card->ctx, r, "Create file failed"); |
964 | | |
965 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
966 | 0 | } |
967 | | |
968 | | |
969 | | |
970 | | static int sc_hsm_delete_file(sc_card_t *card, const sc_path_t *path) |
971 | 0 | { |
972 | 0 | sc_context_t *ctx = card->ctx; |
973 | 0 | sc_apdu_t apdu; |
974 | 0 | u8 sbuf[2]; |
975 | 0 | int r; |
976 | |
|
977 | 0 | if ((path->type != SC_PATH_TYPE_FILE_ID) || (path->len != 2)) { |
978 | 0 | sc_log(card->ctx, "File type has to be SC_PATH_TYPE_FILE_ID"); |
979 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS); |
980 | 0 | } |
981 | | |
982 | 0 | sbuf[0] = path->value[0]; |
983 | 0 | sbuf[1] = path->value[1]; |
984 | |
|
985 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xE4, 0x02, 0x00); |
986 | 0 | apdu.data = sbuf; |
987 | 0 | apdu.datalen = sizeof(sbuf); |
988 | 0 | apdu.lc = sizeof(sbuf); |
989 | |
|
990 | 0 | r = sc_transmit_apdu(card, &apdu); |
991 | 0 | LOG_TEST_RET(ctx, r, "APDU transmit failed"); |
992 | | |
993 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
994 | 0 | LOG_TEST_RET(ctx, r, "Check SW error"); |
995 | | |
996 | 0 | LOG_FUNC_RETURN(ctx, SC_SUCCESS); |
997 | 0 | } |
998 | | |
999 | | |
1000 | | static int sc_hsm_set_security_env(sc_card_t *card, |
1001 | | const sc_security_env_t *env, |
1002 | | int se_num) |
1003 | 0 | { |
1004 | 0 | sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data; |
1005 | |
|
1006 | 0 | priv->env = env; |
1007 | |
|
1008 | 0 | switch(env->algorithm) { |
1009 | 0 | case SC_ALGORITHM_RSA: |
1010 | 0 | if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1) { |
1011 | 0 | if (env->algorithm_flags & SC_ALGORITHM_RSA_HASH_SHA1) { |
1012 | 0 | priv->algorithm = ALGO_RSA_PKCS1_SHA1; |
1013 | 0 | } else if (env->algorithm_flags & SC_ALGORITHM_RSA_HASH_SHA256) { |
1014 | 0 | priv->algorithm = ALGO_RSA_PKCS1_SHA256; |
1015 | 0 | } else if (env->algorithm_flags & SC_ALGORITHM_RSA_HASH_SHA384) { |
1016 | 0 | priv->algorithm = ALGO_RSA_PKCS1_SHA384; |
1017 | 0 | } else if (env->algorithm_flags & SC_ALGORITHM_RSA_HASH_SHA512) { |
1018 | 0 | priv->algorithm = ALGO_RSA_PKCS1_SHA512; |
1019 | 0 | } else { |
1020 | 0 | priv->algorithm = ALGO_RSA_PKCS1; |
1021 | 0 | } |
1022 | 0 | } else if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PSS) { |
1023 | 0 | if ((env->algorithm_flags & SC_ALGORITHM_RSA_HASHES) && |
1024 | 0 | (((env->algorithm_flags & SC_ALGORITHM_MGF1_HASHES) >> 8) != (env->algorithm_flags & SC_ALGORITHM_RSA_HASHES))) { |
1025 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS); |
1026 | 0 | } |
1027 | 0 | priv->algorithm = ALGO_RSA_PSS; |
1028 | 0 | } else { |
1029 | 0 | if (env->operation == SC_SEC_OPERATION_DECIPHER) { |
1030 | 0 | priv->algorithm = ALGO_RSA_DECRYPT; |
1031 | 0 | } else { |
1032 | 0 | priv->algorithm = ALGO_RSA_RAW; |
1033 | 0 | } |
1034 | 0 | } |
1035 | 0 | break; |
1036 | 0 | case SC_ALGORITHM_EC: |
1037 | 0 | if (env->operation == SC_SEC_OPERATION_DERIVE) { |
1038 | 0 | priv->algorithm = ALGO_EC_DH; |
1039 | 0 | } else if (env->algorithm_flags & SC_ALGORITHM_ECDSA_HASH_NONE) { |
1040 | 0 | priv->algorithm = ALGO_EC_RAW; |
1041 | 0 | } else if (env->algorithm_flags & SC_ALGORITHM_ECDSA_HASH_SHA1) { |
1042 | 0 | priv->algorithm = ALGO_EC_SHA1; |
1043 | 0 | } else if (env->algorithm_flags & SC_ALGORITHM_ECDSA_HASH_SHA224) { |
1044 | 0 | priv->algorithm = ALGO_EC_SHA224; |
1045 | 0 | } else if (env->algorithm_flags & SC_ALGORITHM_ECDSA_HASH_SHA256) { |
1046 | 0 | priv->algorithm = ALGO_EC_SHA256; |
1047 | 0 | } else if (env->algorithm_flags & SC_ALGORITHM_ECDSA_HASH_SHA384) { |
1048 | 0 | priv->algorithm = ALGO_EC_SHA384; |
1049 | 0 | } else if (env->algorithm_flags & SC_ALGORITHM_ECDSA_HASH_SHA512) { |
1050 | 0 | priv->algorithm = ALGO_EC_SHA512; |
1051 | 0 | } else if (env->algorithm_flags & SC_ALGORITHM_ECDSA_RAW) { |
1052 | 0 | priv->algorithm = ALGO_EC_RAW; |
1053 | 0 | } else { |
1054 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS); |
1055 | 0 | } |
1056 | 0 | break; |
1057 | 0 | default: |
1058 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS); |
1059 | 0 | break; |
1060 | 0 | } |
1061 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1062 | 0 | } |
1063 | | |
1064 | | |
1065 | | |
1066 | | static int sc_hsm_decode_ecdsa_signature(sc_card_t *card, |
1067 | | const u8 * data, size_t datalen, |
1068 | | u8 * out, size_t outlen, |
1069 | 0 | size_t key_size) { |
1070 | |
|
1071 | 0 | int r; |
1072 | 0 | size_t fieldsizebytes = (key_size + 7) >> 3; |
1073 | |
|
1074 | 0 | sc_log(card->ctx, |
1075 | 0 | "Field size %"SC_FORMAT_LEN_SIZE_T"u, signature buffer size %"SC_FORMAT_LEN_SIZE_T"u", |
1076 | 0 | fieldsizebytes, outlen); |
1077 | |
|
1078 | 0 | r = sc_asn1_decode_ecdsa_signature(card->ctx, data, datalen, fieldsizebytes, &out, outlen); |
1079 | 0 | LOG_FUNC_RETURN(card->ctx, r); |
1080 | 0 | } |
1081 | | |
1082 | | |
1083 | | |
1084 | | static int sc_hsm_compute_signature(sc_card_t *card, |
1085 | | const u8 * data, size_t datalen, |
1086 | | u8 * out, size_t outlen) |
1087 | 0 | { |
1088 | 0 | int r; |
1089 | 0 | sc_apdu_t apdu; |
1090 | 0 | u8 rbuf[514]; |
1091 | 0 | sc_hsm_private_data_t *priv; |
1092 | |
|
1093 | 0 | if (card == NULL || data == NULL || out == NULL) { |
1094 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
1095 | 0 | } |
1096 | 0 | priv = (sc_hsm_private_data_t *) card->drv_data; |
1097 | |
|
1098 | 0 | if (priv->env == NULL) { |
1099 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_OBJECT_NOT_FOUND); |
1100 | 0 | } |
1101 | | |
1102 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_4_EXT, 0x68, priv->env->key_ref[0], priv->algorithm); |
1103 | 0 | apdu.cla = 0x80; |
1104 | 0 | apdu.resp = rbuf; |
1105 | 0 | apdu.resplen = sizeof(rbuf); |
1106 | 0 | apdu.le = 512; |
1107 | |
|
1108 | 0 | apdu.data = data; |
1109 | 0 | apdu.lc = datalen; |
1110 | 0 | apdu.datalen = datalen; |
1111 | 0 | r = sc_transmit_apdu(card, &apdu); |
1112 | |
|
1113 | 0 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
1114 | 0 | if (apdu.sw1 == 0x90 && apdu.sw2 == 0x00) { |
1115 | 0 | int len; |
1116 | |
|
1117 | 0 | if ((priv->algorithm & 0xF0) == ALGO_EC_RAW) { |
1118 | 0 | len = sc_hsm_decode_ecdsa_signature(card, apdu.resp, apdu.resplen, out, outlen, priv->env->key_size_bits); |
1119 | 0 | if (len < 0) { |
1120 | 0 | LOG_FUNC_RETURN(card->ctx, len); |
1121 | 0 | } |
1122 | 0 | } else { |
1123 | 0 | len = (int)(apdu.resplen > outlen ? outlen : apdu.resplen); |
1124 | 0 | memcpy(out, apdu.resp, len); |
1125 | 0 | } |
1126 | 0 | LOG_FUNC_RETURN(card->ctx, len); |
1127 | 0 | } |
1128 | 0 | LOG_FUNC_RETURN(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2)); |
1129 | 0 | } |
1130 | | |
1131 | | |
1132 | | |
1133 | | static int sc_hsm_decipher(sc_card_t *card, const u8 * crgram, size_t crgram_len, u8 * out, size_t outlen) |
1134 | 0 | { |
1135 | 0 | int r; |
1136 | 0 | size_t len; |
1137 | 0 | sc_apdu_t apdu; |
1138 | 0 | u8 rbuf[514]; |
1139 | 0 | sc_hsm_private_data_t *priv; |
1140 | |
|
1141 | 0 | if (card == NULL || crgram == NULL || out == NULL) { |
1142 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
1143 | 0 | } |
1144 | 0 | LOG_FUNC_CALLED(card->ctx); |
1145 | 0 | priv = (sc_hsm_private_data_t *) card->drv_data; |
1146 | |
|
1147 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_4_EXT, 0x62, priv->env->key_ref[0], priv->algorithm); |
1148 | 0 | apdu.cla = 0x80; |
1149 | 0 | apdu.resp = rbuf; |
1150 | 0 | apdu.resplen = sizeof(rbuf); |
1151 | 0 | apdu.le = 512; |
1152 | |
|
1153 | 0 | apdu.data = (u8 *)crgram; |
1154 | 0 | apdu.lc = crgram_len; |
1155 | 0 | apdu.datalen = crgram_len; |
1156 | |
|
1157 | 0 | r = sc_transmit_apdu(card, &apdu); |
1158 | |
|
1159 | 0 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
1160 | 0 | if (apdu.sw1 == 0x90 && apdu.sw2 == 0x00) { |
1161 | 0 | if (priv->algorithm == ALGO_EC_DH) { |
1162 | | // |
1163 | | // The SmartCard-HSM returns the point result of the DH operation |
1164 | | // with a leading '04' |
1165 | 0 | assert(apdu.resplen > 0); |
1166 | 0 | len = apdu.resplen - 1 > outlen ? outlen : apdu.resplen - 1; |
1167 | 0 | memcpy(out, apdu.resp + 1, len); |
1168 | 0 | LOG_FUNC_RETURN(card->ctx, (int)len); |
1169 | 0 | } else { |
1170 | 0 | len = apdu.resplen > outlen ? outlen : apdu.resplen; |
1171 | 0 | memcpy(out, apdu.resp, len); |
1172 | 0 | LOG_FUNC_RETURN(card->ctx, (int)len); |
1173 | 0 | } |
1174 | 0 | } |
1175 | 0 | else |
1176 | 0 | LOG_FUNC_RETURN(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2)); |
1177 | 0 | } |
1178 | | |
1179 | | |
1180 | | |
1181 | | void sc_hsm_set_serialnr(sc_card_t *card, char *serial) |
1182 | 0 | { |
1183 | 0 | sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data; |
1184 | |
|
1185 | 0 | if (priv->serialno) { |
1186 | 0 | free(priv->serialno); |
1187 | 0 | } |
1188 | |
|
1189 | 0 | priv->serialno = strdup(serial); |
1190 | 0 | } |
1191 | | |
1192 | | |
1193 | | |
1194 | | static int sc_hsm_get_serialnr(sc_card_t *card, sc_serial_number_t *serial) |
1195 | 13 | { |
1196 | 13 | sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data; |
1197 | | |
1198 | 13 | LOG_FUNC_CALLED(card->ctx); |
1199 | | |
1200 | 13 | if (!priv->serialno && 0 == strcmp(card->ctx->app_name, "opensc-tool")) { |
1201 | | /* sc-hsm initializes the serial number via its PKCS#15 layer. |
1202 | | * Create and destroy a dummy card to get this initialized. Only do |
1203 | | * this for `opensc-tool --serial` to avoid unnecessary card commands |
1204 | | * in all other cases. */ |
1205 | 0 | sc_pkcs15_card_t *p15card = NULL; |
1206 | 0 | (void)sc_pkcs15_bind(card, NULL, &p15card); |
1207 | 0 | sc_pkcs15_unbind(p15card); |
1208 | 0 | } |
1209 | | |
1210 | 13 | if (!priv->serialno) { |
1211 | 13 | return SC_ERROR_OBJECT_NOT_FOUND; |
1212 | 13 | } |
1213 | | |
1214 | 0 | serial->len = strlen(priv->serialno); |
1215 | 0 | if (serial->len > sizeof(serial->value)) |
1216 | 0 | serial->len = sizeof(serial->value); |
1217 | |
|
1218 | 0 | memcpy(serial->value, priv->serialno, serial->len); |
1219 | |
|
1220 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1221 | 0 | } |
1222 | | |
1223 | | |
1224 | | |
1225 | | static int sc_hsm_initialize(sc_card_t *card, sc_cardctl_sc_hsm_init_param_t *params) |
1226 | 0 | { |
1227 | 0 | sc_context_t *ctx = card->ctx; |
1228 | 0 | sc_pkcs15_tokeninfo_t ti; |
1229 | 0 | struct sc_pin_cmd_data pincmd; |
1230 | 0 | int r; |
1231 | 0 | size_t tilen; |
1232 | 0 | sc_apdu_t apdu; |
1233 | 0 | u8 ibuff[68+0xFF], *p; |
1234 | |
|
1235 | 0 | LOG_FUNC_CALLED(card->ctx); |
1236 | |
|
1237 | 0 | p = ibuff; |
1238 | 0 | *p++ = 0x80; // Options |
1239 | 0 | *p++ = 0x02; |
1240 | 0 | memcpy(p, params->options, 2); |
1241 | 0 | p += 2; |
1242 | |
|
1243 | 0 | if (params->user_pin_len > 0xFF) { |
1244 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
1245 | 0 | } |
1246 | 0 | *p++ = 0x81; // User PIN |
1247 | 0 | *p++ = (u8)params->user_pin_len; |
1248 | 0 | memcpy(p, params->user_pin, params->user_pin_len); |
1249 | 0 | p += params->user_pin_len; |
1250 | |
|
1251 | 0 | *p++ = 0x82; // Initialization code |
1252 | 0 | *p++ = 0x08; |
1253 | 0 | memcpy(p, params->init_code, 8); |
1254 | 0 | p += 8; |
1255 | |
|
1256 | 0 | *p++ = 0x91; // User PIN retry counter |
1257 | 0 | *p++ = 0x01; |
1258 | 0 | *p++ = params->user_pin_retry_counter; |
1259 | |
|
1260 | 0 | if (params->dkek_shares >= 0) { |
1261 | 0 | *p++ = 0x92; // Number of DKEK shares |
1262 | 0 | *p++ = 0x01; |
1263 | 0 | *p++ = (u8)params->dkek_shares; |
1264 | 0 | } |
1265 | |
|
1266 | 0 | if (params->num_of_pub_keys > 0) { |
1267 | 0 | *p++ = 0x93; // Use public key authentication |
1268 | 0 | *p++ = 0x02; |
1269 | 0 | *p++ = params->num_of_pub_keys; // Total number of public keys used for public authentication |
1270 | 0 | *p++ = params->required_pub_keys; // Number of public keys required for authentication |
1271 | 0 | } |
1272 | |
|
1273 | 0 | if (params->bio1.len) { |
1274 | 0 | *p++ = 0x95; |
1275 | 0 | *p++ = params->bio1.len; |
1276 | 0 | memcpy(p, params->bio1.value, params->bio1.len); |
1277 | 0 | p += params->bio1.len; |
1278 | 0 | } |
1279 | 0 | if (params->bio2.len) { |
1280 | 0 | *p++ = 0x96; |
1281 | 0 | *p++ = params->bio2.len; |
1282 | 0 | memcpy(p, params->bio2.value, params->bio2.len); |
1283 | 0 | p += params->bio2.len; |
1284 | 0 | } |
1285 | |
|
1286 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x50, 0x00, 0x00); |
1287 | 0 | apdu.cla = 0x80; |
1288 | 0 | apdu.data = ibuff; |
1289 | 0 | apdu.datalen = p - ibuff; |
1290 | 0 | apdu.lc = apdu.datalen; |
1291 | |
|
1292 | 0 | r = sc_transmit_apdu(card, &apdu); |
1293 | 0 | LOG_TEST_RET(ctx, r, "APDU transmit failed"); |
1294 | | |
1295 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1296 | |
|
1297 | 0 | if (r == SC_ERROR_NOT_ALLOWED) { |
1298 | 0 | r = SC_ERROR_PIN_CODE_INCORRECT; |
1299 | 0 | } |
1300 | |
|
1301 | 0 | LOG_TEST_RET(ctx, r, "Check SW error"); |
1302 | | |
1303 | 0 | if (params->label) { |
1304 | 0 | memset(&ti, 0, sizeof(ti)); |
1305 | |
|
1306 | 0 | ti.label = params->label; |
1307 | 0 | ti.flags = SC_PKCS15_TOKEN_PRN_GENERATION; |
1308 | |
|
1309 | 0 | r = sc_pkcs15_encode_tokeninfo(ctx, &ti, &p, &tilen); |
1310 | 0 | LOG_TEST_RET(ctx, r, "Error encoding tokeninfo"); |
1311 | | |
1312 | 0 | memset(&pincmd, 0, sizeof(pincmd)); |
1313 | 0 | pincmd.cmd = SC_PIN_CMD_VERIFY; |
1314 | 0 | pincmd.pin_type = SC_AC_CHV; |
1315 | 0 | pincmd.pin_reference = 0x81; |
1316 | 0 | pincmd.pin1.data = params->user_pin; |
1317 | 0 | pincmd.pin1.len = params->user_pin_len; |
1318 | |
|
1319 | 0 | r = (*iso_ops->pin_cmd)(card, &pincmd, NULL); |
1320 | 0 | LOG_TEST_RET(ctx, r, "Could not verify PIN"); |
1321 | | |
1322 | 0 | r = sc_hsm_write_ef(card, 0x2F03, 0, p, tilen); |
1323 | 0 | LOG_TEST_RET(ctx, r, "Could not write EF.TokenInfo"); |
1324 | 0 | } |
1325 | | |
1326 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1327 | 0 | } |
1328 | | |
1329 | | |
1330 | | |
1331 | | static int sc_hsm_import_dkek_share(sc_card_t *card, sc_cardctl_sc_hsm_dkek_t *params) |
1332 | 0 | { |
1333 | 0 | sc_context_t *ctx = card->ctx; |
1334 | 0 | sc_apdu_t apdu; |
1335 | 0 | u8 status[SC_MAX_APDU_BUFFER_SIZE]; |
1336 | 0 | int r; |
1337 | |
|
1338 | 0 | LOG_FUNC_CALLED(card->ctx); |
1339 | |
|
1340 | 0 | if (params->importShare) { |
1341 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0x52, 0x00, 0x00); |
1342 | 0 | apdu.cla = 0x80; |
1343 | 0 | apdu.data = params->dkek_share; |
1344 | 0 | apdu.datalen = sizeof(params->dkek_share); |
1345 | 0 | apdu.lc = apdu.datalen; |
1346 | 0 | } else { |
1347 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0x52, 0x00, 0x00); |
1348 | 0 | } |
1349 | 0 | apdu.cla = 0x80; |
1350 | 0 | apdu.le = 0; |
1351 | 0 | apdu.resp = status; |
1352 | 0 | apdu.resplen = sizeof(status); |
1353 | |
|
1354 | 0 | r = sc_transmit_apdu(card, &apdu); |
1355 | 0 | LOG_TEST_RET(ctx, r, "APDU transmit failed"); |
1356 | | |
1357 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1358 | |
|
1359 | 0 | LOG_TEST_RET(ctx, r, "Check SW error"); |
1360 | | |
1361 | 0 | assert(apdu.resplen >= (sizeof(params->key_check_value) + 2)); |
1362 | | |
1363 | 0 | params->dkek_shares = status[0]; |
1364 | 0 | params->outstanding_shares = status[1]; |
1365 | 0 | memcpy(params->key_check_value, status + 2, sizeof(params->key_check_value)); |
1366 | |
|
1367 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1368 | 0 | } |
1369 | | |
1370 | | |
1371 | | |
1372 | | static int sc_hsm_wrap_key(sc_card_t *card, sc_cardctl_sc_hsm_wrapped_key_t *params) |
1373 | 0 | { |
1374 | 0 | sc_context_t *ctx = card->ctx; |
1375 | 0 | sc_apdu_t apdu; |
1376 | 0 | u8 data[1500]; |
1377 | 0 | int r; |
1378 | |
|
1379 | 0 | LOG_FUNC_CALLED(card->ctx); |
1380 | |
|
1381 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_2_EXT, 0x72, params->key_id, 0x92); |
1382 | 0 | apdu.cla = 0x80; |
1383 | 0 | apdu.le = 0; |
1384 | 0 | apdu.resp = data; |
1385 | 0 | apdu.resplen = sizeof(data); |
1386 | |
|
1387 | 0 | r = sc_transmit_apdu(card, &apdu); |
1388 | 0 | LOG_TEST_RET(ctx, r, "APDU transmit failed"); |
1389 | | |
1390 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1391 | |
|
1392 | 0 | LOG_TEST_RET(ctx, r, "Check SW error"); |
1393 | | |
1394 | 0 | if (params->wrapped_key == NULL) { |
1395 | 0 | params->wrapped_key_length = apdu.resplen; |
1396 | 0 | params->wrapped_key = malloc(apdu.resplen); |
1397 | 0 | if (params->wrapped_key == NULL) { |
1398 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY); |
1399 | 0 | } |
1400 | 0 | } else { |
1401 | 0 | if (apdu.resplen > params->wrapped_key_length) { |
1402 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_BUFFER_TOO_SMALL); |
1403 | 0 | } |
1404 | 0 | params->wrapped_key_length = apdu.resplen; |
1405 | 0 | } |
1406 | 0 | memcpy(params->wrapped_key, data, apdu.resplen); |
1407 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1408 | 0 | } |
1409 | | |
1410 | | |
1411 | | |
1412 | | static int sc_hsm_unwrap_key(sc_card_t *card, sc_cardctl_sc_hsm_wrapped_key_t *params) |
1413 | 0 | { |
1414 | 0 | sc_context_t *ctx = card->ctx; |
1415 | 0 | sc_apdu_t apdu; |
1416 | 0 | int r; |
1417 | |
|
1418 | 0 | LOG_FUNC_CALLED(card->ctx); |
1419 | |
|
1420 | 0 | r = sc_hsm_write_ef(card, 0x2F10, 0, params->wrapped_key, params->wrapped_key_length); |
1421 | 0 | LOG_TEST_RET(card->ctx, r, "Create EF failed"); |
1422 | | |
1423 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x74, params->key_id, 0x93); |
1424 | 0 | apdu.cla = 0x80; |
1425 | 0 | r = sc_transmit_apdu(card, &apdu); |
1426 | 0 | LOG_TEST_RET(ctx, r, "APDU transmit failed"); |
1427 | | |
1428 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1429 | 0 | LOG_TEST_RET(ctx, r, "Check SW error"); |
1430 | | |
1431 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1432 | 0 | } |
1433 | | |
1434 | | static int verify_certificate(sc_card_t *card, sc_cvc_t *cvc, |
1435 | | const u8 *cvc_buf, size_t cvc_buf_len) |
1436 | 0 | { |
1437 | 0 | u8 tag = SC_ASN1_TAG_CONTEXT | SC_ASN1_TAG_BIT_STRING; /* 0x83 */ |
1438 | 0 | size_t pukref_len; |
1439 | 0 | u8 pukref[BUFSIZ]; |
1440 | 0 | sc_apdu_t apdu; |
1441 | 0 | u8 *ptr; |
1442 | 0 | int r; |
1443 | |
|
1444 | 0 | LOG_FUNC_CALLED(card->ctx); |
1445 | | |
1446 | | /* check if public key is already known */ |
1447 | 0 | if ((r = sc_asn1_put_tag(tag, (u8 *)cvc->chr, cvc->chrLen, |
1448 | 0 | pukref, sizeof(pukref), &ptr)) < 0) { |
1449 | 0 | sc_log(card->ctx, "Error formatting ASN.1 sequence: %s\n", sc_strerror(r)); |
1450 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_UNKNOWN); |
1451 | 0 | } |
1452 | 0 | pukref_len = ptr - pukref; |
1453 | | |
1454 | | /* MANAGE SECURITY ENVIRONMENT to query public key by chr */ |
1455 | 0 | sc_format_apdu_ex(&apdu, 0x00, 0x22, 0x81, 0xB6, pukref, pukref_len, NULL, 0); |
1456 | |
|
1457 | 0 | r = sc_transmit_apdu(card, &apdu); |
1458 | 0 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
1459 | | |
1460 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1461 | 0 | if (!r) { |
1462 | | /* already known */ |
1463 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1464 | 0 | } |
1465 | 0 | if (apdu.sw1 != 0x6A && apdu.sw2 != 0x88) { |
1466 | 0 | LOG_TEST_RET(card->ctx, SC_ERROR_UNKNOWN, "Check SW error"); |
1467 | 0 | } |
1468 | | |
1469 | 0 | if ((r = sc_asn1_put_tag(tag, (u8 *)cvc->car, cvc->carLen, |
1470 | 0 | pukref, sizeof(pukref), &ptr)) < 0) { |
1471 | 0 | sc_log(card->ctx, "Error formatting ASN.1 sequence: %s\n", sc_strerror(r)); |
1472 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_UNKNOWN); |
1473 | 0 | } |
1474 | 0 | pukref_len = ptr - pukref; |
1475 | | |
1476 | | /* MANAGE SECURITY ENVIRONMENT to set the CAR public key */ |
1477 | 0 | sc_format_apdu_ex(&apdu, 0x00, 0x22, 0x81, 0xB6, pukref, pukref_len, NULL, 0); |
1478 | |
|
1479 | 0 | r = sc_transmit_apdu(card, &apdu); |
1480 | 0 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
1481 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1482 | 0 | LOG_TEST_RET(card->ctx, r, "Check SW error"); |
1483 | | |
1484 | | /* PERFORM SECURITY OPERATION -> VERIFY CERTIFICATE */ |
1485 | 0 | sc_format_apdu_ex(&apdu, 0x00, 0x2A, 0x00, 0xBE, cvc_buf, cvc_buf_len, NULL, 0); |
1486 | |
|
1487 | 0 | r = sc_transmit_apdu(card, &apdu); |
1488 | 0 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
1489 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1490 | 0 | LOG_TEST_RET(card->ctx, r, "Check SW error"); |
1491 | | |
1492 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1493 | 0 | } |
1494 | | |
1495 | | |
1496 | | |
1497 | | static int sc_hsm_register_public_key(sc_card_t *card, |
1498 | | sc_cardctl_sc_hsm_pka_register_t *pka_register) |
1499 | 0 | { |
1500 | 0 | u8 tag = SC_ASN1_TAG_CONTEXT | SC_ASN1_TAG_BIT_STRING; /* 0x83 */ |
1501 | 0 | u8 recvbuf[4]; |
1502 | 0 | sc_context_t *ctx = card->ctx; |
1503 | 0 | sc_apdu_t apdu; |
1504 | 0 | u8 *ptr; |
1505 | 0 | int r; |
1506 | 0 | sc_pkcs15_card_t p15card; |
1507 | 0 | const u8 *pka_buf; |
1508 | 0 | size_t pka_buf_len; |
1509 | 0 | sc_cvc_pka_t pka; |
1510 | | /* outer CAR in ASN.1 needs a byte for tag and a byte for length */ |
1511 | 0 | u8 asn1_outer_car[sizeof(pka.public_key_req.cvc.outer_car) + 2]; |
1512 | |
|
1513 | 0 | LOG_FUNC_CALLED(ctx); |
1514 | |
|
1515 | 0 | memset(&pka, 0, sizeof(pka)); |
1516 | 0 | memset(&p15card, 0, sizeof(p15card)); |
1517 | 0 | p15card.card = card; |
1518 | |
|
1519 | 0 | pka_buf = pka_register->buf; |
1520 | 0 | pka_buf_len = pka_register->buflen; |
1521 | 0 | r = sc_pkcs15emu_sc_hsm_decode_pka(&p15card, &pka_buf, &pka_buf_len, &pka); |
1522 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "sc_pkcs15emu_sc_hsm_decode_pka failed"); |
1523 | | |
1524 | | /* the DICA CVC must be verified first */ |
1525 | 0 | r = verify_certificate(card, &pka.dica.cvc, pka.dica.ptr, pka.dica.len); |
1526 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "Verify device issuer CA CVC failed"); |
1527 | | |
1528 | | /* the device CVC must be verified before registering the public key */ |
1529 | 0 | r = verify_certificate(card, &pka.device.cvc, pka.device.ptr, pka.device.len); |
1530 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "Verify device CVC failed"); |
1531 | | |
1532 | 0 | r = sc_asn1_put_tag(tag, |
1533 | 0 | (u8 *)pka.public_key_req.cvc.outer_car, |
1534 | 0 | pka.public_key_req.cvc.outerCARLen, |
1535 | 0 | asn1_outer_car, sizeof(asn1_outer_car), &ptr); |
1536 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "ASN.1 encode outer CAR failed"); |
1537 | | |
1538 | | /* MANAGE SECURITY ENVIRONMENT with the outer CAR of the public key */ |
1539 | 0 | sc_format_apdu_ex(&apdu, 0x00, 0x22, 0x81, 0xB6, |
1540 | 0 | asn1_outer_car, ptr - asn1_outer_car, NULL, 0); |
1541 | |
|
1542 | 0 | r = sc_transmit_apdu(card, &apdu); |
1543 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "APDU transmit failed"); |
1544 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1545 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "Check SW error"); |
1546 | | |
1547 | 0 | sc_format_apdu_ex(&apdu, 0x80, 0x54, 0x00, 0x00, |
1548 | 0 | pka.public_key_req.ptr, pka.public_key_req.len, |
1549 | 0 | recvbuf, sizeof(recvbuf)); |
1550 | |
|
1551 | 0 | r = sc_transmit_apdu(card, &apdu); |
1552 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "APDU transmit failed"); |
1553 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1554 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "Check SW error"); |
1555 | | |
1556 | 0 | pka_register->new_status.num_total = recvbuf[0]; |
1557 | 0 | pka_register->new_status.num_missing = recvbuf[1]; |
1558 | 0 | pka_register->new_status.num_required = recvbuf[2]; |
1559 | 0 | pka_register->new_status.num_authenticated = recvbuf[3]; |
1560 | |
|
1561 | 0 | r = 0; |
1562 | | /* fall-through */ |
1563 | |
|
1564 | 0 | err: |
1565 | 0 | sc_pkcs15emu_sc_hsm_free_cvc_pka(&pka); |
1566 | 0 | return r; |
1567 | 0 | } |
1568 | | |
1569 | | |
1570 | | |
1571 | | static int sc_hsm_public_key_auth_status(sc_card_t *card, |
1572 | | sc_cardctl_sc_hsm_pka_status_t *status) |
1573 | 0 | { |
1574 | 0 | u8 recvbuf[4]; |
1575 | 0 | sc_context_t *ctx = card->ctx; |
1576 | 0 | sc_apdu_t apdu; |
1577 | 0 | int r; |
1578 | |
|
1579 | 0 | LOG_FUNC_CALLED(card->ctx); |
1580 | | |
1581 | | /* get status */ |
1582 | 0 | sc_format_apdu_ex(&apdu, 0x00, 0x54, 0x00, 0x00, NULL, 0, recvbuf, sizeof recvbuf); |
1583 | 0 | apdu.cla = 0x80; |
1584 | |
|
1585 | 0 | r = sc_transmit_apdu(card, &apdu); |
1586 | 0 | LOG_TEST_RET(ctx, r, "APDU transmit failed"); |
1587 | | |
1588 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1589 | 0 | LOG_TEST_RET(ctx, r, "Check SW error"); |
1590 | | |
1591 | 0 | status->num_total = recvbuf[0]; |
1592 | 0 | status->num_missing = recvbuf[1]; |
1593 | 0 | status->num_required = recvbuf[2]; |
1594 | 0 | status->num_authenticated = recvbuf[3]; |
1595 | |
|
1596 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1597 | 0 | } |
1598 | | |
1599 | | |
1600 | | |
1601 | | static int sc_hsm_init_token(sc_card_t *card, sc_cardctl_pkcs11_init_token_t *params) |
1602 | 0 | { |
1603 | 0 | sc_context_t *ctx = card->ctx; |
1604 | 0 | sc_cardctl_sc_hsm_init_param_t ip; |
1605 | 0 | int r; |
1606 | 0 | char label[33],*cpo; |
1607 | |
|
1608 | 0 | LOG_FUNC_CALLED(ctx); |
1609 | |
|
1610 | 0 | if (params->so_pin_len != 16) { |
1611 | 0 | LOG_TEST_RET(ctx, SC_ERROR_INVALID_DATA, "SO PIN wrong length (!=16)"); |
1612 | 0 | } |
1613 | | |
1614 | 0 | memset(&ip, 0, sizeof(ip)); |
1615 | 0 | ip.dkek_shares = -1; |
1616 | 0 | ip.options[0] = 0x00; |
1617 | 0 | ip.options[1] = 0x01; |
1618 | |
|
1619 | 0 | r = sc_hsm_encode_sopin(params->so_pin, ip.init_code); |
1620 | 0 | LOG_TEST_RET(ctx, r, "SO PIN wrong format"); |
1621 | | |
1622 | 0 | ip.user_pin = ip.init_code; // Use the first 6 bytes of the SO-PIN as initial User-PIN value |
1623 | 0 | ip.user_pin_len = 6; |
1624 | 0 | ip.user_pin_retry_counter = 3; |
1625 | |
|
1626 | 0 | if (params->label) { |
1627 | | // Strip trailing spaces |
1628 | 0 | memcpy(label, params->label, 32); |
1629 | 0 | label[32] = 0; |
1630 | 0 | cpo = label + 31; |
1631 | 0 | while ((cpo >= label) && (*cpo == ' ')) { |
1632 | 0 | *cpo = 0; |
1633 | 0 | cpo--; |
1634 | 0 | } |
1635 | 0 | ip.label = label; |
1636 | 0 | } |
1637 | |
|
1638 | 0 | r = sc_hsm_initialize(card, &ip); |
1639 | 0 | LOG_TEST_RET(ctx, r, "Check SW error"); |
1640 | | |
1641 | 0 | LOG_FUNC_RETURN(ctx, SC_SUCCESS); |
1642 | 0 | } |
1643 | | |
1644 | | |
1645 | | |
1646 | | static int sc_hsm_init_pin(sc_card_t *card, sc_cardctl_pkcs11_init_pin_t *params) |
1647 | 0 | { |
1648 | 0 | sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data; |
1649 | 0 | sc_context_t *ctx = card->ctx; |
1650 | 0 | int r; |
1651 | 0 | sc_apdu_t apdu; |
1652 | 0 | u8 ibuff[50], *p; |
1653 | |
|
1654 | 0 | LOG_FUNC_CALLED(card->ctx); |
1655 | |
|
1656 | 0 | if (params->pin_len > 16) { |
1657 | 0 | LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_DATA, "User PIN too long"); |
1658 | 0 | } |
1659 | | |
1660 | 0 | p = ibuff; |
1661 | |
|
1662 | 0 | memcpy(p, priv->sopin, sizeof(priv->sopin)); |
1663 | 0 | p += sizeof(priv->sopin); |
1664 | |
|
1665 | 0 | memcpy(p, params->pin, params->pin_len); |
1666 | 0 | p += params->pin_len; |
1667 | |
|
1668 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x2C, 0x00, 0x81); |
1669 | 0 | apdu.data = ibuff; |
1670 | 0 | apdu.datalen = p - ibuff; |
1671 | 0 | apdu.lc = apdu.datalen; |
1672 | |
|
1673 | 0 | r = sc_transmit_apdu(card, &apdu); |
1674 | 0 | LOG_TEST_RET(ctx, r, "APDU transmit failed"); |
1675 | | |
1676 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1677 | | |
1678 | | // Cards before version 1.0 do not implement RESET_RETRY_COUNTER |
1679 | | // For those cards the CHANGE REFERENCE DATA command is used instead |
1680 | 0 | if (r == SC_ERROR_INS_NOT_SUPPORTED) { |
1681 | 0 | p = ibuff; |
1682 | 0 | memcpy(p, priv->sopin, 6); |
1683 | 0 | p += 6; |
1684 | |
|
1685 | 0 | memcpy(p, params->pin, params->pin_len); |
1686 | 0 | p += params->pin_len; |
1687 | |
|
1688 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x24, 0x00, 0x81); |
1689 | 0 | apdu.data = ibuff; |
1690 | 0 | apdu.datalen = p - ibuff; |
1691 | 0 | apdu.lc = apdu.datalen; |
1692 | |
|
1693 | 0 | r = sc_transmit_apdu(card, &apdu); |
1694 | 0 | LOG_TEST_RET(ctx, r, "APDU transmit failed"); |
1695 | | |
1696 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1697 | 0 | } |
1698 | | |
1699 | 0 | LOG_TEST_RET(ctx, r, "Check SW error"); |
1700 | | |
1701 | 0 | memset(priv->sopin, 0, sizeof(priv->sopin)); |
1702 | |
|
1703 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1704 | 0 | } |
1705 | | |
1706 | | |
1707 | | |
1708 | | static int sc_hsm_generate_keypair(sc_card_t *card, sc_cardctl_sc_hsm_keygen_info_t *keyinfo) |
1709 | 0 | { |
1710 | 0 | u8 rbuf[1200]; |
1711 | 0 | int r; |
1712 | 0 | sc_apdu_t apdu; |
1713 | |
|
1714 | 0 | LOG_FUNC_CALLED(card->ctx); |
1715 | |
|
1716 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_4_EXT, 0x46, keyinfo->key_id, keyinfo->auth_key_id); |
1717 | 0 | apdu.cla = 0x00; |
1718 | 0 | apdu.resp = rbuf; |
1719 | 0 | apdu.resplen = sizeof(rbuf); |
1720 | 0 | apdu.le = 0; |
1721 | |
|
1722 | 0 | apdu.data = keyinfo->gakprequest; |
1723 | 0 | apdu.lc = keyinfo->gakprequest_len; |
1724 | 0 | apdu.datalen = keyinfo->gakprequest_len; |
1725 | |
|
1726 | 0 | r = sc_transmit_apdu(card, &apdu); |
1727 | 0 | LOG_TEST_RET(card->ctx, r, "APDU transmit failed"); |
1728 | | |
1729 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
1730 | 0 | LOG_TEST_RET(card->ctx, r, "Check SW error"); |
1731 | | |
1732 | 0 | keyinfo->gakpresponse_len = apdu.resplen; |
1733 | 0 | keyinfo->gakpresponse = malloc(apdu.resplen); |
1734 | |
|
1735 | 0 | if (keyinfo->gakpresponse == NULL) { |
1736 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY); |
1737 | 0 | } |
1738 | | |
1739 | 0 | memcpy(keyinfo->gakpresponse, apdu.resp, apdu.resplen); |
1740 | |
|
1741 | 0 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
1742 | 0 | } |
1743 | | |
1744 | | |
1745 | | |
1746 | | static int sc_hsm_card_ctl(sc_card_t *card, unsigned long cmd, void *ptr) |
1747 | 13 | { |
1748 | 13 | switch (cmd) { |
1749 | 13 | case SC_CARDCTL_GET_SERIALNR: |
1750 | 13 | return sc_hsm_get_serialnr(card, (sc_serial_number_t *)ptr); |
1751 | 0 | case SC_CARDCTL_PKCS11_INIT_TOKEN: |
1752 | 0 | return sc_hsm_init_token(card, (sc_cardctl_pkcs11_init_token_t *)ptr); |
1753 | 0 | case SC_CARDCTL_PKCS11_INIT_PIN: |
1754 | 0 | return sc_hsm_init_pin(card, (sc_cardctl_pkcs11_init_pin_t *)ptr); |
1755 | 0 | case SC_CARDCTL_SC_HSM_GENERATE_KEY: |
1756 | 0 | return sc_hsm_generate_keypair(card, (sc_cardctl_sc_hsm_keygen_info_t *)ptr); |
1757 | 0 | case SC_CARDCTL_SC_HSM_INITIALIZE: |
1758 | 0 | return sc_hsm_initialize(card, (sc_cardctl_sc_hsm_init_param_t *)ptr); |
1759 | 0 | case SC_CARDCTL_SC_HSM_IMPORT_DKEK_SHARE: |
1760 | 0 | return sc_hsm_import_dkek_share(card, (sc_cardctl_sc_hsm_dkek_t *)ptr); |
1761 | 0 | case SC_CARDCTL_SC_HSM_WRAP_KEY: |
1762 | 0 | return sc_hsm_wrap_key(card, (sc_cardctl_sc_hsm_wrapped_key_t *)ptr); |
1763 | 0 | case SC_CARDCTL_SC_HSM_UNWRAP_KEY: |
1764 | 0 | return sc_hsm_unwrap_key(card, (sc_cardctl_sc_hsm_wrapped_key_t *)ptr); |
1765 | 0 | case SC_CARDCTL_SC_HSM_REGISTER_PUBLIC_KEY: |
1766 | 0 | return sc_hsm_register_public_key(card, ptr); |
1767 | 0 | case SC_CARDCTL_SC_HSM_PUBLIC_KEY_AUTH_STATUS: |
1768 | 0 | return sc_hsm_public_key_auth_status(card, ptr); |
1769 | 13 | } |
1770 | 0 | return SC_ERROR_NOT_SUPPORTED; |
1771 | 13 | } |
1772 | | |
1773 | | |
1774 | | |
1775 | | static int sc_hsm_init(struct sc_card *card) |
1776 | 150 | { |
1777 | | #if defined(ENABLE_SM) && defined(ENABLE_OPENPACE) && defined(_WIN32) |
1778 | | char expanded_val[PATH_MAX]; |
1779 | | size_t expanded_len = PATH_MAX; |
1780 | | #endif |
1781 | 150 | int flags,ext_flags; |
1782 | 150 | sc_file_t *file = NULL; |
1783 | 150 | sc_path_t path; |
1784 | 150 | sc_hsm_private_data_t *priv = NULL; |
1785 | | |
1786 | 150 | LOG_FUNC_CALLED(card->ctx); |
1787 | | |
1788 | 150 | flags = SC_ALGORITHM_RSA_RAW|SC_ALGORITHM_RSA_PAD_PSS|SC_ALGORITHM_ONBOARD_KEY_GEN |
1789 | 150 | |SC_ALGORITHM_RSA_HASH_SHA1|SC_ALGORITHM_RSA_HASH_SHA256|SC_ALGORITHM_RSA_HASH_SHA384|SC_ALGORITHM_RSA_HASH_SHA512 |
1790 | 150 | |SC_ALGORITHM_MGF1_SHA256|SC_ALGORITHM_MGF1_SHA384|SC_ALGORITHM_MGF1_SHA512; |
1791 | | |
1792 | 150 | _sc_card_add_rsa_alg(card, 1024, flags, 0); |
1793 | 150 | _sc_card_add_rsa_alg(card, 1536, flags, 0); |
1794 | 150 | _sc_card_add_rsa_alg(card, 2048, flags, 0); |
1795 | 150 | _sc_card_add_rsa_alg(card, 3072, flags, 0); |
1796 | 150 | _sc_card_add_rsa_alg(card, 4096, flags, 0); |
1797 | | |
1798 | 150 | flags = SC_ALGORITHM_ECDSA_RAW| |
1799 | 150 | SC_ALGORITHM_ECDH_CDH_RAW| |
1800 | 150 | SC_ALGORITHM_ECDSA_HASH_NONE| |
1801 | 150 | SC_ALGORITHM_ECDSA_HASH_SHA1| |
1802 | 150 | SC_ALGORITHM_ECDSA_HASH_SHA224| |
1803 | 150 | SC_ALGORITHM_ECDSA_HASH_SHA256| |
1804 | 150 | SC_ALGORITHM_ECDSA_HASH_SHA384| |
1805 | 150 | SC_ALGORITHM_ECDSA_HASH_SHA512| |
1806 | 150 | SC_ALGORITHM_ONBOARD_KEY_GEN; |
1807 | | |
1808 | 150 | ext_flags = SC_ALGORITHM_EXT_EC_F_P| |
1809 | 150 | SC_ALGORITHM_EXT_EC_ECPARAMETERS| |
1810 | 150 | SC_ALGORITHM_EXT_EC_NAMEDCURVE| |
1811 | 150 | SC_ALGORITHM_EXT_EC_UNCOMPRESES| |
1812 | 150 | SC_ALGORITHM_ONBOARD_KEY_GEN; |
1813 | 150 | _sc_card_add_ec_alg(card, 192, flags, ext_flags, NULL); |
1814 | 150 | _sc_card_add_ec_alg(card, 224, flags, ext_flags, NULL); |
1815 | 150 | _sc_card_add_ec_alg(card, 256, flags, ext_flags, NULL); |
1816 | 150 | _sc_card_add_ec_alg(card, 320, flags, ext_flags, NULL); |
1817 | 150 | _sc_card_add_ec_alg(card, 384, flags, ext_flags, NULL); |
1818 | 150 | _sc_card_add_ec_alg(card, 512, flags, ext_flags, NULL); |
1819 | 150 | _sc_card_add_ec_alg(card, 521, flags, ext_flags, NULL); |
1820 | | |
1821 | 150 | card->caps |= SC_CARD_CAP_RNG|SC_CARD_CAP_APDU_EXT|SC_CARD_CAP_ISO7816_PIN_INFO; |
1822 | | |
1823 | | // APDU Buffer limits |
1824 | | // JCOP 2.4.1r3 1462 |
1825 | | // JCOP 2.4.2r3 1454 |
1826 | | // JCOP 3 1232 |
1827 | | // MicroSD with JCOP 3 478 / 506 - handled in reader-pcsc.c |
1828 | | // Reiner SCT 1014 - handled in reader-pcsc.c |
1829 | | |
1830 | | // Use JCOP 3 card limits for sending |
1831 | 150 | card->max_send_size = 1232; |
1832 | | // Assume that card supports sending with extended length APDU and without limit |
1833 | 150 | card->max_recv_size = 0; |
1834 | | |
1835 | 150 | if (card->type == SC_CARD_TYPE_SC_HSM_SOC |
1836 | 150 | || card->type == SC_CARD_TYPE_SC_HSM_GOID) { |
1837 | 149 | card->max_recv_size = 0x0630; // SoC Proxy forces this limit |
1838 | 149 | } else { |
1839 | | // Adjust to the limits set by the reader |
1840 | 1 | if (card->reader->max_send_size < card->max_send_size) { |
1841 | 1 | if (18 >= card->reader->max_send_size) |
1842 | 1 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INCONSISTENT_CONFIGURATION); |
1843 | | |
1844 | | // 17 byte header and TLV because of odd ins in UPDATE BINARY |
1845 | 0 | card->max_send_size = card->reader->max_send_size - 17; |
1846 | 0 | } |
1847 | | |
1848 | 0 | if (0 < card->reader->max_recv_size) { |
1849 | 0 | if (3 >= card->reader->max_recv_size) |
1850 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INCONSISTENT_CONFIGURATION); |
1851 | 0 | card->max_recv_size = card->reader->max_recv_size - 2; |
1852 | 0 | } |
1853 | 0 | } |
1854 | | |
1855 | 149 | priv = card->drv_data; |
1856 | 149 | if (!priv) { |
1857 | 149 | priv = calloc(1, sizeof(sc_hsm_private_data_t)); |
1858 | 149 | if (!priv) |
1859 | 149 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY); |
1860 | 149 | card->drv_data = priv; |
1861 | 149 | } |
1862 | | |
1863 | 149 | sc_path_set(&path, SC_PATH_TYPE_DF_NAME, sc_hsm_aid.value, sc_hsm_aid.len, 0, 0); |
1864 | 149 | if (sc_hsm_select_file_ex(card, &path, 0, &file) == SC_SUCCESS |
1865 | 149 | && file && file->prop_attr && file->prop_attr_len >= 2) { |
1866 | 4 | static char card_name[SC_MAX_APDU_BUFFER_SIZE]; |
1867 | 4 | u8 type = 0xFF; |
1868 | 4 | u8 major = file->prop_attr[file->prop_attr_len - 2]; |
1869 | 4 | u8 minor = file->prop_attr[file->prop_attr_len - 1]; |
1870 | 4 | char p00[] = "SmartCard-HSM Applet for JCOP"; |
1871 | 4 | char p01[] = "SmartCard-HSM Demo Applet for JCOP"; |
1872 | 4 | char *p = "SmartCard-HSM"; |
1873 | 4 | if (file->prop_attr_len >= 3) { |
1874 | 2 | type = file->prop_attr[file->prop_attr_len - 3]; |
1875 | 2 | } |
1876 | 4 | switch (type) { |
1877 | 1 | case 0x00: |
1878 | 1 | p = p00; |
1879 | 1 | break; |
1880 | 1 | case 0x01: |
1881 | 1 | p = p01; |
1882 | 1 | break; |
1883 | 2 | default: |
1884 | 2 | break; |
1885 | 4 | } |
1886 | 4 | snprintf(card_name, sizeof card_name, "%s version %u.%u", p, major, minor); |
1887 | 4 | card->name = card_name; |
1888 | | |
1889 | 4 | if (file->prop_attr[1] & 0x04) { |
1890 | 2 | card->caps |= SC_CARD_CAP_SESSION_PIN; |
1891 | 2 | } |
1892 | 4 | } |
1893 | 149 | sc_file_free(file); |
1894 | | |
1895 | 149 | priv->EF_C_DevAut = NULL; |
1896 | 149 | priv->EF_C_DevAut_len = 0; |
1897 | | |
1898 | | #if defined(ENABLE_SM) && defined(ENABLE_OPENPACE) |
1899 | | EAC_init(); |
1900 | | #ifdef _WIN32 |
1901 | | expanded_len = ExpandEnvironmentStringsA(CVCDIR, expanded_val, sizeof expanded_val); |
1902 | | if (0 < expanded_len && expanded_len < sizeof expanded_val) |
1903 | | EAC_set_cvc_default_dir(expanded_val); |
1904 | | #else |
1905 | | EAC_set_cvc_default_dir(CVCDIR); |
1906 | | #endif |
1907 | | #endif |
1908 | | |
1909 | 149 | return 0; |
1910 | 149 | } |
1911 | | |
1912 | | |
1913 | | |
1914 | | static int sc_hsm_finish(sc_card_t * card) |
1915 | 149 | { |
1916 | 149 | sc_hsm_private_data_t *priv = (sc_hsm_private_data_t *) card->drv_data; |
1917 | 149 | #ifdef ENABLE_SM |
1918 | 149 | sc_sm_stop(card); |
1919 | 149 | #endif |
1920 | 149 | if (priv) { |
1921 | 149 | free(priv->serialno); |
1922 | 149 | sc_file_free(priv->dffcp); |
1923 | 149 | free(priv->EF_C_DevAut); |
1924 | 149 | } |
1925 | 149 | free(priv); |
1926 | | |
1927 | 149 | return SC_SUCCESS; |
1928 | 149 | } |
1929 | | |
1930 | | |
1931 | | |
1932 | | static struct sc_card_driver * sc_get_driver(void) |
1933 | 14.1k | { |
1934 | 14.1k | struct sc_card_driver *iso_drv = sc_get_iso7816_driver(); |
1935 | | |
1936 | 14.1k | if (iso_ops == NULL) |
1937 | 1 | iso_ops = iso_drv->ops; |
1938 | | |
1939 | 14.1k | sc_hsm_ops = *iso_drv->ops; |
1940 | 14.1k | sc_hsm_ops.match_card = sc_hsm_match_card; |
1941 | 14.1k | sc_hsm_ops.select_file = sc_hsm_select_file; |
1942 | 14.1k | sc_hsm_ops.get_challenge = sc_hsm_get_challenge; |
1943 | 14.1k | sc_hsm_ops.read_binary = sc_hsm_read_binary; |
1944 | 14.1k | sc_hsm_ops.update_binary = sc_hsm_update_binary; |
1945 | 14.1k | sc_hsm_ops.list_files = sc_hsm_list_files; |
1946 | 14.1k | sc_hsm_ops.create_file = sc_hsm_create_file; |
1947 | 14.1k | sc_hsm_ops.delete_file = sc_hsm_delete_file; |
1948 | 14.1k | sc_hsm_ops.set_security_env = sc_hsm_set_security_env; |
1949 | 14.1k | sc_hsm_ops.compute_signature = sc_hsm_compute_signature; |
1950 | 14.1k | sc_hsm_ops.decipher = sc_hsm_decipher; |
1951 | 14.1k | sc_hsm_ops.init = sc_hsm_init; |
1952 | 14.1k | sc_hsm_ops.finish = sc_hsm_finish; |
1953 | 14.1k | sc_hsm_ops.card_ctl = sc_hsm_card_ctl; |
1954 | 14.1k | sc_hsm_ops.pin_cmd = sc_hsm_pin_cmd; |
1955 | 14.1k | sc_hsm_ops.logout = sc_hsm_logout; |
1956 | | |
1957 | | /* no record oriented file services */ |
1958 | 14.1k | sc_hsm_ops.read_record = NULL; |
1959 | 14.1k | sc_hsm_ops.write_record = NULL; |
1960 | 14.1k | sc_hsm_ops.append_record = NULL; |
1961 | 14.1k | sc_hsm_ops.update_record = NULL; |
1962 | | |
1963 | 14.1k | return &sc_hsm_drv; |
1964 | 14.1k | } |
1965 | | |
1966 | | |
1967 | | |
1968 | | struct sc_card_driver * sc_get_sc_hsm_driver(void) |
1969 | 14.1k | { |
1970 | 14.1k | return sc_get_driver(); |
1971 | 14.1k | } |
1972 | | |