/src/opensc/src/pkcs15init/pkcs15-cflex.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Cryptoflex specific operation for PKCS #15 initialization |
3 | | * |
4 | | * Copyright (C) 2002 Juha Yrjölä <juha.yrjola@iki.fi> |
5 | | * |
6 | | * This library is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * This library is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with this library; if not, write to the Free Software |
18 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | | */ |
20 | | |
21 | | #include "config.h" |
22 | | |
23 | | #include <stdlib.h> |
24 | | #include <string.h> |
25 | | #include <sys/types.h> |
26 | | |
27 | | #include "libopensc/opensc.h" |
28 | | #include "libopensc/cardctl.h" |
29 | | #include "libopensc/log.h" |
30 | | #include "pkcs15-init.h" |
31 | | #include "profile.h" |
32 | | |
33 | | static void invert_buf(u8 *dest, const u8 *src, size_t c); |
34 | | static int cflex_create_dummy_chvs(sc_profile_t *, sc_pkcs15_card_t *, |
35 | | sc_file_t *, int, |
36 | | sc_file_t **); |
37 | | static void cflex_delete_dummy_chvs(sc_profile_t *, sc_pkcs15_card_t *, |
38 | | int, sc_file_t **); |
39 | | static int cflex_create_pin_file(sc_profile_t *, sc_pkcs15_card_t *, |
40 | | sc_path_t *, int, |
41 | | const u8 *, size_t, int, |
42 | | const u8 *, size_t, int, |
43 | | sc_file_t **, int); |
44 | | static int cflex_create_empty_pin_file(sc_profile_t *, sc_pkcs15_card_t *, |
45 | | sc_path_t *, int, sc_file_t **); |
46 | | static int cflex_get_keyfiles(sc_profile_t *, sc_card_t *, |
47 | | const sc_path_t *, sc_file_t **, sc_file_t **); |
48 | | |
49 | | unsigned char dummy_pin_value[6] = {0x30, 0x30, 0x30, 0x30, 0x30, 0x30}; |
50 | | |
51 | | static int |
52 | | cflex_delete_file(sc_profile_t *profile, sc_pkcs15_card_t *p15card, sc_file_t *df) |
53 | 0 | { |
54 | 0 | sc_path_t path; |
55 | 0 | sc_file_t *parent; |
56 | 0 | int r = 0; |
57 | | /* Select the parent DF */ |
58 | 0 | path = df->path; |
59 | 0 | if (path.len < 2) { |
60 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
61 | 0 | } |
62 | 0 | path.len -= 2; |
63 | 0 | r = sc_select_file(p15card->card, &path, &parent); |
64 | 0 | if (r < 0) |
65 | 0 | return r; |
66 | | |
67 | 0 | r = sc_pkcs15init_authenticate(profile, p15card, parent, SC_AC_OP_DELETE); |
68 | 0 | sc_file_free(parent); |
69 | 0 | if (r < 0) |
70 | 0 | return r; |
71 | | |
72 | | /* cryptoflex has no ERASE AC */ |
73 | 0 | memset(&path, 0, sizeof(path)); |
74 | 0 | path.type = SC_PATH_TYPE_FILE_ID; |
75 | 0 | path.value[0] = df->id >> 8; |
76 | 0 | path.value[1] = df->id & 0xFF; |
77 | 0 | path.len = 2; |
78 | |
|
79 | 0 | r = sc_delete_file(p15card->card, &path); |
80 | 0 | return r; |
81 | 0 | } |
82 | | |
83 | | /* |
84 | | * Erase the card via rm |
85 | | */ |
86 | | static int cflex_erase_card(struct sc_profile *profile, sc_pkcs15_card_t *p15card) |
87 | 0 | { |
88 | 0 | struct sc_context *ctx = p15card->card->ctx; |
89 | 0 | sc_file_t *df = profile->df_info->file, *dir, *userpinfile = NULL; |
90 | 0 | int r; |
91 | |
|
92 | 0 | LOG_FUNC_CALLED(ctx); |
93 | | /* Delete EF(DIR). This may not be very nice |
94 | | * against other applications that use this file, but |
95 | | * extremely useful for testing :) |
96 | | * Note we need to delete if before the DF because we create |
97 | | * it *after* the DF. |
98 | | * */ |
99 | 0 | if (sc_profile_get_file(profile, "DIR", &dir) >= 0) { |
100 | 0 | r = cflex_delete_file(profile, p15card, dir); |
101 | 0 | sc_file_free(dir); |
102 | 0 | if (r < 0 && r != SC_ERROR_FILE_NOT_FOUND) |
103 | 0 | goto out; |
104 | 0 | } |
105 | | |
106 | 0 | r=cflex_delete_file(profile, p15card, df); |
107 | | |
108 | | /* If the user pin file isn't in a sub-DF of the pkcs15 DF, delete it */ |
109 | 0 | if (sc_profile_get_file(profile, "pinfile-1", &userpinfile) >= 0 && |
110 | 0 | userpinfile->path.len <= profile->df_info->file->path.len + 2 && |
111 | 0 | memcmp(userpinfile->path.value, profile->df_info->file->path.value, |
112 | 0 | userpinfile->path.len) != 0) { |
113 | 0 | r = cflex_delete_file(profile, p15card, userpinfile); |
114 | 0 | sc_file_free(userpinfile); |
115 | 0 | userpinfile=NULL; |
116 | 0 | } |
117 | | |
118 | |
|
119 | 0 | out: /* Forget all cached keys, the pin files on card are all gone. */ |
120 | 0 | sc_file_free(userpinfile); |
121 | |
|
122 | 0 | sc_free_apps(p15card->card); |
123 | 0 | if (r == SC_ERROR_FILE_NOT_FOUND) |
124 | 0 | r=0; |
125 | |
|
126 | 0 | SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_VERBOSE, r); |
127 | 0 | } |
128 | | |
129 | | /* |
130 | | * Card initialization. |
131 | | * For the cryptoflex, read the card's serial number from 3F00 0002 |
132 | | */ |
133 | | static int |
134 | | cryptoflex_init_card(sc_profile_t *profile, sc_pkcs15_card_t *p15card) |
135 | 0 | { |
136 | 0 | sc_path_t path; |
137 | 0 | sc_file_t *file; |
138 | 0 | u8 buf[32]; |
139 | 0 | char serial[128]; |
140 | 0 | size_t len; |
141 | 0 | int r; |
142 | |
|
143 | 0 | sc_format_path("3F000002", &path); |
144 | 0 | if ((r = sc_select_file(p15card->card, &path, &file)) < 0) { |
145 | 0 | if (r == SC_ERROR_FILE_NOT_FOUND) |
146 | 0 | return 0; |
147 | 0 | return r; |
148 | 0 | } |
149 | | |
150 | 0 | if ((len = file->size) > sizeof(buf)) |
151 | 0 | len = sizeof(buf); |
152 | 0 | sc_file_free(file); |
153 | 0 | if ((r = sc_read_binary(p15card->card, 0, buf, len, 0)) < 0) |
154 | 0 | return r; |
155 | 0 | len = r; |
156 | 0 | if (len == 0) |
157 | 0 | return 0; |
158 | | |
159 | 0 | if ((r = sc_bin_to_hex(buf, len, serial, sizeof(serial), '\0')) < 0) |
160 | 0 | return r; |
161 | 0 | sc_pkcs15init_set_serial(profile, serial); |
162 | 0 | return 0; |
163 | 0 | } |
164 | | |
165 | | /* |
166 | | * Create a DF |
167 | | */ |
168 | | static int |
169 | | cflex_create_dir(sc_profile_t *profile, sc_pkcs15_card_t *p15card, sc_file_t *df) |
170 | 0 | { |
171 | | /* Create the application DF */ |
172 | 0 | return sc_pkcs15init_create_file(profile, p15card, df); |
173 | 0 | } |
174 | | |
175 | | /* |
176 | | * Create a PIN domain (i.e. a sub-directory holding a user PIN) |
177 | | */ |
178 | | static int |
179 | | cflex_create_domain(sc_profile_t *profile, sc_pkcs15_card_t *p15card, |
180 | | const sc_pkcs15_id_t *id, sc_file_t **ret) |
181 | 0 | { |
182 | 0 | return sc_pkcs15_create_pin_domain(profile, p15card, id, ret); |
183 | 0 | } |
184 | | |
185 | | /* |
186 | | * Select the PIN reference |
187 | | */ |
188 | | static int |
189 | | cflex_select_pin_reference(sc_profile_t *profike, sc_pkcs15_card_t *p15card, |
190 | | sc_pkcs15_auth_info_t *auth_info) |
191 | 0 | { |
192 | 0 | int preferred; |
193 | |
|
194 | 0 | if (auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN) |
195 | 0 | return SC_ERROR_OBJECT_NOT_VALID; |
196 | | |
197 | 0 | if (auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_SO_PIN) { |
198 | 0 | preferred = 2; |
199 | 0 | } else { |
200 | 0 | preferred = 1; |
201 | 0 | } |
202 | 0 | if (auth_info->attrs.pin.reference <= preferred) { |
203 | 0 | auth_info->attrs.pin.reference = preferred; |
204 | 0 | return 0; |
205 | 0 | } |
206 | | |
207 | 0 | if (auth_info->attrs.pin.reference > 2) |
208 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
209 | | |
210 | | /* Caller, please select a different PIN reference */ |
211 | 0 | return SC_ERROR_INVALID_PIN_REFERENCE; |
212 | 0 | } |
213 | | |
214 | | |
215 | | /* |
216 | | * Create a new PIN inside a DF |
217 | | */ |
218 | | static int |
219 | | cflex_create_pin(sc_profile_t *profile, sc_pkcs15_card_t *p15card, sc_file_t *df, |
220 | | sc_pkcs15_object_t *pin_obj, |
221 | | const u8 *pin, size_t pin_len, |
222 | | const u8 *puk, size_t puk_len) |
223 | 0 | { |
224 | 0 | struct sc_context *ctx = p15card->card->ctx; |
225 | 0 | sc_pkcs15_auth_info_t *auth_info = (sc_pkcs15_auth_info_t *) pin_obj->data; |
226 | 0 | struct sc_pkcs15_pin_attributes *pin_attrs = &auth_info->attrs.pin; |
227 | 0 | sc_file_t *dummies[2]; |
228 | 0 | int ndummies, pin_type, puk_type, r; |
229 | 0 | sc_file_t *file = NULL; |
230 | |
|
231 | 0 | LOG_FUNC_CALLED(ctx); |
232 | |
|
233 | 0 | if (auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN) |
234 | 0 | return SC_ERROR_OBJECT_NOT_VALID; |
235 | | |
236 | | /* If the profile doesn't specify a reference for this PIN, guess */ |
237 | 0 | if (pin_attrs->flags & SC_PKCS15_PIN_FLAG_SO_PIN) { |
238 | 0 | pin_type = SC_PKCS15INIT_SO_PIN; |
239 | 0 | puk_type = SC_PKCS15INIT_SO_PUK; |
240 | 0 | if (pin_attrs->reference != 2) |
241 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
242 | 0 | } else { |
243 | 0 | pin_type = SC_PKCS15INIT_USER_PIN; |
244 | 0 | puk_type = SC_PKCS15INIT_USER_PUK; |
245 | 0 | if (pin_attrs->reference != 1) |
246 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
247 | 0 | } |
248 | | |
249 | | /* Get file definition from the profile */ |
250 | 0 | if (sc_profile_get_file(profile, (pin_attrs->reference == 1)? "CHV1" : "CHV2", &file) < 0 |
251 | 0 | && sc_profile_get_file(profile, "CHV", &file) < 0) |
252 | 0 | LOG_TEST_RET(ctx, SC_ERROR_FILE_NOT_FOUND, "profile does not define pin file ACLs"); |
253 | | |
254 | 0 | ndummies = cflex_create_dummy_chvs(profile, p15card, file, SC_AC_OP_CREATE, dummies); |
255 | 0 | sc_file_free(file); |
256 | 0 | LOG_TEST_RET(ctx, ndummies, "Unable to create dummy CHV file"); |
257 | | |
258 | 0 | r = cflex_create_pin_file(profile, p15card, &df->path, pin_attrs->reference, |
259 | 0 | pin, pin_len, sc_profile_get_pin_retries(profile, pin_type), |
260 | 0 | puk, puk_len, sc_profile_get_pin_retries(profile, puk_type), |
261 | 0 | NULL, 0); |
262 | |
|
263 | 0 | cflex_delete_dummy_chvs(profile, p15card, ndummies, dummies); |
264 | 0 | SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_VERBOSE, r); |
265 | 0 | } |
266 | | |
267 | | /* |
268 | | * Create a new key file |
269 | | */ |
270 | | static int |
271 | | cflex_create_key(sc_profile_t *profile, sc_pkcs15_card_t *p15card, sc_pkcs15_object_t *obj) |
272 | 0 | { |
273 | 0 | sc_pkcs15_prkey_info_t *key_info = (sc_pkcs15_prkey_info_t *) obj->data; |
274 | 0 | sc_file_t *prkf = NULL, *pukf = NULL; |
275 | 0 | size_t size; |
276 | 0 | int r; |
277 | |
|
278 | 0 | if (obj->type != SC_PKCS15_TYPE_PRKEY_RSA) { |
279 | 0 | sc_log(p15card->card->ctx, "Cryptoflex supports only RSA keys."); |
280 | 0 | return SC_ERROR_NOT_SUPPORTED; |
281 | 0 | } |
282 | | |
283 | | /* Get the public and private key file */ |
284 | 0 | r = cflex_get_keyfiles(profile, p15card->card, &key_info->path, &prkf, &pukf); |
285 | 0 | if (r < 0) |
286 | 0 | return r; |
287 | | |
288 | | /* Adjust the file sizes, if necessary */ |
289 | 0 | switch (key_info->modulus_length) { |
290 | 0 | case 512: size = 166; break; |
291 | 0 | case 768: size = 246; break; |
292 | 0 | case 1024: size = 326; break; |
293 | 0 | case 2048: size = 646; break; |
294 | 0 | default: |
295 | 0 | sc_log(p15card->card->ctx, |
296 | 0 | "Unsupported key size %"SC_FORMAT_LEN_SIZE_T"u\n", |
297 | 0 | key_info->modulus_length); |
298 | 0 | r = SC_ERROR_INVALID_ARGUMENTS; |
299 | 0 | goto out; |
300 | 0 | } |
301 | | |
302 | 0 | if (prkf && prkf->size < size) |
303 | 0 | prkf->size = size; |
304 | 0 | if (pukf && pukf->size < size + 4) |
305 | 0 | pukf->size = size + 4; |
306 | | |
307 | | /* Now create the files */ |
308 | 0 | if ((r = sc_pkcs15init_create_file(profile, p15card, prkf)) < 0 |
309 | 0 | || (r = sc_pkcs15init_create_file(profile, p15card, pukf)) < 0) |
310 | 0 | goto out; |
311 | | |
312 | 0 | key_info->key_reference = 0; |
313 | |
|
314 | 0 | out: sc_file_free(prkf); |
315 | 0 | sc_file_free(pukf); |
316 | 0 | return r; |
317 | 0 | } |
318 | | |
319 | | |
320 | | /* |
321 | | * Generate key |
322 | | */ |
323 | | static int |
324 | | cflex_generate_key(sc_profile_t *profile, sc_pkcs15_card_t *p15card, |
325 | | sc_pkcs15_object_t *obj, |
326 | | sc_pkcs15_pubkey_t *pubkey) |
327 | 0 | { |
328 | 0 | struct sc_cardctl_cryptoflex_genkey_info args; |
329 | 0 | sc_card_t *card = p15card->card; |
330 | 0 | sc_pkcs15_prkey_info_t *key_info = (sc_pkcs15_prkey_info_t *) obj->data; |
331 | 0 | size_t keybits; |
332 | 0 | unsigned char raw_pubkey[256]; |
333 | 0 | sc_file_t *prkf = NULL, *pukf = NULL; |
334 | 0 | int r; |
335 | |
|
336 | 0 | if (obj->type != SC_PKCS15_TYPE_PRKEY_RSA) { |
337 | 0 | sc_log(card->ctx, "Cryptoflex supports only RSA keys."); |
338 | 0 | return SC_ERROR_NOT_SUPPORTED; |
339 | 0 | } |
340 | | /* Get the public and private key file */ |
341 | 0 | r = cflex_get_keyfiles(profile, card, &key_info->path, &prkf, &pukf); |
342 | 0 | if (r < 0) |
343 | 0 | return r; |
344 | 0 | if (! prkf) |
345 | 0 | return SC_ERROR_NOT_SUPPORTED; |
346 | | |
347 | | /* Make sure we authenticate first */ |
348 | 0 | r = sc_pkcs15init_authenticate(profile, p15card, prkf, SC_AC_OP_CRYPTO); |
349 | 0 | if (r < 0) |
350 | 0 | goto out; |
351 | | |
352 | 0 | keybits = key_info->modulus_length; |
353 | | |
354 | | /* Perform key generation */ |
355 | 0 | memset(&args, 0, sizeof(args)); |
356 | 0 | args.exponent = 0x10001; |
357 | 0 | args.key_bits = keybits; |
358 | 0 | args.key_num = key_info->key_reference; |
359 | 0 | r = sc_card_ctl(card, SC_CARDCTL_CRYPTOFLEX_GENERATE_KEY, &args); |
360 | 0 | if (r < 0) |
361 | 0 | goto out; |
362 | | |
363 | | /* extract public key */ |
364 | 0 | pubkey->algorithm = SC_ALGORITHM_RSA; |
365 | 0 | pubkey->u.rsa.modulus.len = keybits / 8; |
366 | 0 | pubkey->u.rsa.modulus.data = malloc(keybits / 8); |
367 | 0 | pubkey->u.rsa.exponent.len = 3; |
368 | 0 | pubkey->u.rsa.exponent.data = malloc(3); |
369 | 0 | memcpy(pubkey->u.rsa.exponent.data, "\x01\x00\x01", 3); |
370 | 0 | if ((r = sc_select_file(card, &pukf->path, NULL)) < 0 |
371 | 0 | || (r = sc_read_binary(card, 3, raw_pubkey, keybits / 8, 0)) < 0) |
372 | 0 | goto out; |
373 | | |
374 | 0 | invert_buf(pubkey->u.rsa.modulus.data, raw_pubkey, pubkey->u.rsa.modulus.len); |
375 | |
|
376 | 0 | out: sc_file_free(pukf); |
377 | 0 | sc_file_free(prkf); |
378 | 0 | return r; |
379 | 0 | } |
380 | | |
381 | | /* |
382 | | * Store a private key |
383 | | */ |
384 | | static int |
385 | | cflex_store_key(sc_profile_t *profile, sc_pkcs15_card_t *p15card, |
386 | | sc_pkcs15_object_t *obj, |
387 | | sc_pkcs15_prkey_t *key) |
388 | 0 | { |
389 | 0 | sc_pkcs15_prkey_info_t *key_info = (sc_pkcs15_prkey_info_t *) obj->data; |
390 | 0 | sc_card_t *card = p15card->card; |
391 | 0 | sc_file_t *prkf = NULL, *pukf = NULL; |
392 | 0 | unsigned char keybuf[1024]; |
393 | 0 | size_t size; |
394 | 0 | int r; |
395 | |
|
396 | 0 | if (obj->type != SC_PKCS15_TYPE_PRKEY_RSA) { |
397 | 0 | sc_log(card->ctx, "Cryptoflex supports only RSA keys."); |
398 | 0 | return SC_ERROR_NOT_SUPPORTED; |
399 | 0 | } |
400 | | /* Get the public and private key file */ |
401 | 0 | r = cflex_get_keyfiles(profile, card, &key_info->path, &prkf, &pukf); |
402 | 0 | if (r < 0) |
403 | 0 | return r; |
404 | | |
405 | | /* Write the private key */ |
406 | 0 | size = sizeof(keybuf); |
407 | 0 | r = profile->ops->encode_private_key(profile, card, |
408 | 0 | &key->u.rsa, keybuf, &size, |
409 | 0 | key_info->key_reference); |
410 | 0 | if (r < 0) |
411 | 0 | goto out; |
412 | | |
413 | 0 | r = sc_pkcs15init_update_file(profile, p15card, prkf, keybuf, (unsigned)size); |
414 | 0 | if (r < 0) |
415 | 0 | goto out; |
416 | | |
417 | | /* Write the public key */ |
418 | 0 | size = sizeof(keybuf); |
419 | 0 | r = profile->ops->encode_public_key(profile, card, |
420 | 0 | &key->u.rsa, keybuf, &size, |
421 | 0 | key_info->key_reference); |
422 | 0 | if (r < 0) |
423 | 0 | goto out; |
424 | | |
425 | 0 | r = sc_pkcs15init_update_file(profile, p15card, pukf, keybuf, (unsigned)size); |
426 | |
|
427 | 0 | out: sc_file_free(prkf); |
428 | 0 | sc_file_free(pukf); |
429 | 0 | return r; |
430 | 0 | } |
431 | | |
432 | | /* |
433 | | * If an access condition references e.g. CHV1, but we don't have |
434 | | * a CHV1 file yet, create an unprotected dummy file in the MF. |
435 | | */ |
436 | | static int |
437 | | cflex_create_dummy_chvs(sc_profile_t *profile, sc_pkcs15_card_t *p15card, |
438 | | sc_file_t *file, int op, |
439 | | sc_file_t **dummies) |
440 | 0 | { |
441 | 0 | struct sc_context *ctx = p15card->card->ctx; |
442 | 0 | const sc_acl_entry_t *acl; |
443 | 0 | int r = 0, ndummies = 0; |
444 | |
|
445 | 0 | LOG_FUNC_CALLED(ctx); |
446 | | /* See if the DF is supposed to be PIN protected, and if |
447 | | * it is, whether that CHV file actually exists. If it doesn't, |
448 | | * create it. |
449 | | */ |
450 | 0 | acl = sc_file_get_acl_entry(file, op); |
451 | 0 | for (; acl; acl = acl->next) { |
452 | 0 | sc_path_t parent, ef; |
453 | |
|
454 | 0 | if (acl->method != SC_AC_CHV) |
455 | 0 | continue; |
456 | | |
457 | 0 | parent = file->path; |
458 | 0 | parent.len -= 2; |
459 | |
|
460 | 0 | r = SC_ERROR_FILE_NOT_FOUND; |
461 | 0 | while (parent.len >= 2 && r == SC_ERROR_FILE_NOT_FOUND) { |
462 | 0 | ef = parent; |
463 | 0 | ef.value[ef.len++] = acl->key_ref - 1; |
464 | 0 | ef.value[ef.len++] = 0; |
465 | 0 | parent.len -= 2; |
466 | |
|
467 | 0 | if (ef.len == parent.len |
468 | 0 | && !memcmp(ef.value, parent.value, ef.len)) |
469 | 0 | continue; |
470 | | |
471 | 0 | r = sc_select_file(p15card->card, &ef, NULL); |
472 | 0 | } |
473 | | |
474 | | /* If a valid EF(CHVx) was found, we're fine */ |
475 | 0 | if (r == 0) |
476 | 0 | continue; |
477 | 0 | if (r != SC_ERROR_FILE_NOT_FOUND) |
478 | 0 | break; |
479 | | |
480 | | /* Create a CHV file in the MF */ |
481 | 0 | parent = file->path; |
482 | 0 | parent.len = 2; |
483 | 0 | r = cflex_create_empty_pin_file(profile, p15card, &parent, |
484 | 0 | acl->key_ref, &dummies[ndummies]); |
485 | 0 | if (r < 0) |
486 | 0 | break; |
487 | 0 | ndummies++; |
488 | 0 | } |
489 | |
|
490 | 0 | if (r < 0) { |
491 | 0 | cflex_delete_dummy_chvs(profile, p15card, ndummies, dummies); |
492 | 0 | return r; |
493 | 0 | } |
494 | | |
495 | 0 | SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_VERBOSE, ndummies); |
496 | 0 | } |
497 | | |
498 | | static void |
499 | | cflex_delete_dummy_chvs(sc_profile_t *profile, sc_pkcs15_card_t *p15card, |
500 | | int ndummies, sc_file_t **dummies) |
501 | 0 | { |
502 | 0 | while (ndummies--) { |
503 | 0 | cflex_delete_file(profile, p15card, dummies[ndummies]); |
504 | 0 | sc_file_free(dummies[ndummies]); |
505 | 0 | } |
506 | 0 | } |
507 | | |
508 | | /* |
509 | | * Create a pin file |
510 | | */ |
511 | | static void put_pin(sc_profile_t *profile, unsigned char *buf, |
512 | | const u8 *pin, size_t len, int retry) |
513 | 0 | { |
514 | 0 | if (len > 8) |
515 | 0 | len = 8; |
516 | 0 | memset(buf, profile->pin_pad_char, 8); |
517 | 0 | memcpy(buf, pin, len); |
518 | 0 | buf[8] = retry; |
519 | 0 | buf[9] = retry; |
520 | 0 | } |
521 | | |
522 | | static int |
523 | | cflex_create_pin_file(sc_profile_t *profile, sc_pkcs15_card_t *p15card, |
524 | | sc_path_t *df_path, int ref, |
525 | | const u8 *pin, size_t pin_len, int pin_tries, |
526 | | const u8 *puk, size_t puk_len, int puk_tries, |
527 | | sc_file_t **file_ret, int unprotected) |
528 | 0 | { |
529 | 0 | struct sc_context *ctx = p15card->card->ctx; |
530 | 0 | struct sc_pkcs15_object *pin_obj = NULL; |
531 | 0 | unsigned char buffer[23]; |
532 | 0 | sc_path_t path; |
533 | 0 | sc_file_t *dummies[2], *file; |
534 | 0 | int r, ndummies; |
535 | |
|
536 | 0 | LOG_FUNC_CALLED(ctx); |
537 | 0 | if (file_ret) |
538 | 0 | *file_ret = NULL; |
539 | | |
540 | | /* Build the CHV path */ |
541 | 0 | path = *df_path; |
542 | 0 | path.value[path.len++] = ref - 1; |
543 | 0 | path.value[path.len++] = 0; |
544 | | |
545 | | /* See if the CHV already exists */ |
546 | 0 | r = sc_select_file(p15card->card, &path, NULL); |
547 | 0 | if (r >= 0) |
548 | 0 | return SC_ERROR_FILE_ALREADY_EXISTS; |
549 | | |
550 | | /* Get the file definition from the profile */ |
551 | 0 | if (sc_profile_get_file_by_path(profile, &path, &file) < 0 |
552 | 0 | && sc_profile_get_file(profile, (ref == 1)? "CHV1" : "CHV2", &file) < 0 |
553 | 0 | && sc_profile_get_file(profile, "CHV", &file) < 0) |
554 | 0 | LOG_TEST_RET(ctx, SC_ERROR_FILE_NOT_FOUND, "profile does not define pin file ACLs"); |
555 | | |
556 | 0 | file->path = path; |
557 | 0 | file->size = 23; |
558 | 0 | file->id = (ref == 1)? 0x0000 : 0x0100; |
559 | |
|
560 | 0 | if (unprotected) { |
561 | 0 | sc_file_add_acl_entry(file, SC_AC_OP_UPDATE, |
562 | 0 | SC_AC_NONE, SC_AC_KEY_REF_NONE); |
563 | 0 | } |
564 | | |
565 | | /* Build the contents of the file */ |
566 | 0 | buffer[0] = buffer[1] = buffer[2] = 0xFF; |
567 | 0 | put_pin(profile, buffer + 3, pin, pin_len, pin_tries); |
568 | 0 | put_pin(profile, buffer + 13, puk, puk_len, puk_tries); |
569 | | |
570 | | /* For updating the file, create a dummy CHV files if |
571 | | * necessary */ |
572 | 0 | ndummies = cflex_create_dummy_chvs(profile, p15card, |
573 | 0 | file, SC_AC_OP_UPDATE, dummies); |
574 | 0 | if (ndummies < 0) |
575 | 0 | sc_file_free(file); |
576 | 0 | LOG_TEST_RET(ctx, ndummies, "Unable to create dummy CHV file"); |
577 | | |
578 | 0 | if (!unprotected) { |
579 | 0 | struct sc_pin_cmd_data pin_cmd; |
580 | |
|
581 | 0 | memset(&pin_cmd, 0, sizeof(pin_cmd)); |
582 | 0 | pin_cmd.cmd = SC_PIN_CMD_VERIFY; |
583 | 0 | pin_cmd.pin_type = SC_AC_CHV; |
584 | 0 | pin_cmd.pin_reference = ref; |
585 | 0 | pin_cmd.pin1.data = dummy_pin_value; |
586 | 0 | pin_cmd.pin1.len = sizeof(dummy_pin_value); |
587 | |
|
588 | 0 | r = sc_pin_cmd(p15card->card, &pin_cmd, NULL); |
589 | 0 | if (r < 0) |
590 | 0 | sc_file_free(file); |
591 | 0 | LOG_TEST_RET(ctx, r, "Cannot verify dummy PIN"); |
592 | 0 | }; |
593 | |
|
594 | 0 | if (ref == 2) { |
595 | | /* Cache dummy SOPIN value */ |
596 | 0 | r = sc_pkcs15_find_pin_by_type_and_reference(p15card, NULL, SC_AC_CHV, ref, &pin_obj); |
597 | 0 | if (!r && pin_obj) |
598 | 0 | sc_pkcs15_pincache_add(p15card, pin_obj, dummy_pin_value, sizeof(dummy_pin_value)); |
599 | 0 | } |
600 | |
|
601 | 0 | r = sc_pkcs15init_create_file(profile, p15card, file); |
602 | 0 | if (r < 0) |
603 | 0 | sc_file_free(file); |
604 | 0 | LOG_TEST_RET(ctx, r, "Failed to create PIN file"); |
605 | | |
606 | 0 | r = sc_update_binary(p15card->card, 0, buffer, 23, 0); |
607 | 0 | if (r < 0 || file_ret == NULL) |
608 | 0 | sc_file_free(file); |
609 | 0 | else |
610 | 0 | *file_ret = file; |
611 | 0 | LOG_TEST_RET(ctx, r, "Failed to update PIN file"); |
612 | | |
613 | | /* Delete the dummy CHV files */ |
614 | 0 | cflex_delete_dummy_chvs(profile, p15card, ndummies, dummies); |
615 | |
|
616 | 0 | if (pin_obj) { |
617 | | /* Cache new SOPIN value */ |
618 | 0 | sc_pkcs15_pincache_add(p15card, pin_obj, pin, pin_len); |
619 | 0 | } |
620 | |
|
621 | 0 | SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_VERBOSE, r); |
622 | 0 | } |
623 | | |
624 | | /* |
625 | | * Create a faux pin file |
626 | | */ |
627 | | static int |
628 | | cflex_create_empty_pin_file(sc_profile_t *profile, sc_pkcs15_card_t *p15card, |
629 | | sc_path_t *path, int ref, sc_file_t **file_ret) |
630 | 0 | { |
631 | 0 | int r; |
632 | |
|
633 | 0 | LOG_FUNC_CALLED(p15card->card->ctx); |
634 | 0 | *file_ret = NULL; |
635 | 0 | r = cflex_create_pin_file(profile, p15card, path, ref, |
636 | 0 | dummy_pin_value, sizeof(dummy_pin_value), 8, |
637 | 0 | NULL, 0, 0, |
638 | 0 | file_ret, 1); |
639 | 0 | if (r == SC_ERROR_FILE_ALREADY_EXISTS) |
640 | 0 | SC_FUNC_RETURN(p15card->card->ctx, SC_LOG_DEBUG_VERBOSE, r); |
641 | | |
642 | 0 | SC_FUNC_RETURN(p15card->card->ctx, SC_LOG_DEBUG_VERBOSE, r); |
643 | 0 | } |
644 | | |
645 | | /* |
646 | | * Get private and public key file |
647 | | */ |
648 | | static int cflex_get_keyfiles(sc_profile_t *profile, sc_card_t *card, |
649 | | const sc_path_t *df_path, |
650 | | sc_file_t **prkf, sc_file_t **pukf) |
651 | 0 | { |
652 | 0 | sc_path_t path = *df_path; |
653 | 0 | int r; |
654 | | |
655 | | /* Get the private key file */ |
656 | 0 | r = sc_profile_get_file_by_path(profile, &path, prkf); |
657 | 0 | if (r < 0) { |
658 | 0 | char pbuf[SC_MAX_PATH_STRING_SIZE]; |
659 | |
|
660 | 0 | r = sc_path_print(pbuf, sizeof(pbuf), &path); |
661 | 0 | if (r != SC_SUCCESS) |
662 | 0 | pbuf[0] = '\0'; |
663 | |
|
664 | 0 | sc_log(card->ctx, "Cannot find private key file info " |
665 | 0 | "in profile (path=%s).", pbuf); |
666 | 0 | return r; |
667 | 0 | } |
668 | | |
669 | | /* Get the public key file */ |
670 | 0 | path.len -= 2; |
671 | 0 | sc_append_file_id(&path, 0x1012); |
672 | 0 | r = sc_profile_get_file_by_path(profile, &path, pukf); |
673 | 0 | if (r < 0) { |
674 | 0 | sc_log(card->ctx, "Cannot find public key file info in profile."); |
675 | 0 | sc_file_free(*prkf); |
676 | 0 | return r; |
677 | 0 | } |
678 | | |
679 | 0 | return 0; |
680 | 0 | } |
681 | | |
682 | | static void |
683 | | invert_buf(u8 *dest, const u8 *src, size_t c) |
684 | 0 | { |
685 | 0 | unsigned int i; |
686 | |
|
687 | 0 | for (i = 0; i < c; i++) |
688 | 0 | dest[i] = src[c-1-i]; |
689 | 0 | } |
690 | | |
691 | | static int |
692 | | bn2cf(sc_pkcs15_bignum_t *num, u8 *buf, size_t bufsize) |
693 | 0 | { |
694 | 0 | size_t len = num->len; |
695 | |
|
696 | 0 | if (len > bufsize) |
697 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
698 | | |
699 | 0 | invert_buf(buf, num->data, len); |
700 | 0 | while (len < bufsize) |
701 | 0 | buf[len++] = 0; |
702 | 0 | return 0; |
703 | 0 | } |
704 | | |
705 | | static int |
706 | | bn2cft(sc_pkcs15_bignum_t *num, u8 tag, u8 *buf, size_t bufsize) |
707 | 0 | { |
708 | 0 | size_t len = num->len; |
709 | |
|
710 | 0 | if (len + 3 > bufsize) |
711 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
712 | 0 | memset(buf, 0, bufsize); |
713 | 0 | buf[0] = tag; |
714 | 0 | buf[1] = len + 1; |
715 | 0 | memcpy(buf + 3, num->data, len); |
716 | 0 | return 0; |
717 | 0 | } |
718 | | |
719 | | /* |
720 | | * Cryptoflex key encoding |
721 | | */ |
722 | | static int |
723 | | cryptoflex_encode_private_key(sc_profile_t *profile, sc_card_t *card, |
724 | | struct sc_pkcs15_prkey_rsa *rsa, |
725 | | u8 *key, size_t *keysize, int key_ref) |
726 | 0 | { |
727 | 0 | size_t base = rsa->modulus.len / 2, key_blob_size; |
728 | 0 | int r, key_num = key_ref + 1; |
729 | |
|
730 | 0 | switch (rsa->modulus.len) { |
731 | 0 | case 512 / 8: |
732 | 0 | case 768 / 8: |
733 | 0 | case 1024 / 8: |
734 | 0 | case 2048 / 8: |
735 | 0 | break; |
736 | 0 | default: |
737 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
738 | 0 | } |
739 | | |
740 | 0 | key_blob_size = 5 * base + 3; |
741 | 0 | if (*keysize < key_blob_size + 3) |
742 | 0 | return SC_ERROR_BUFFER_TOO_SMALL; |
743 | 0 | *keysize = key_blob_size + 3; |
744 | |
|
745 | 0 | *key++ = key_blob_size >> 8; |
746 | 0 | *key++ = key_blob_size & 0xFF; |
747 | 0 | *key++ = key_num; |
748 | |
|
749 | 0 | if ((r = bn2cf(&rsa->p, key + 0 * base, base)) < 0 |
750 | 0 | || (r = bn2cf(&rsa->q, key + 1 * base, base)) < 0 |
751 | 0 | || (r = bn2cf(&rsa->iqmp, key + 2 * base, base)) < 0 |
752 | 0 | || (r = bn2cf(&rsa->dmp1, key + 3 * base, base)) < 0 |
753 | 0 | || (r = bn2cf(&rsa->dmq1, key + 4 * base, base)) < 0) |
754 | 0 | return r; |
755 | | |
756 | 0 | key += 5 * base; |
757 | 0 | *key++ = 0; |
758 | 0 | *key++ = 0; |
759 | 0 | *key = 0; |
760 | |
|
761 | 0 | return 0; |
762 | 0 | } |
763 | | |
764 | | static int |
765 | | cryptoflex_encode_public_key(sc_profile_t *profile, sc_card_t *card, |
766 | | struct sc_pkcs15_prkey_rsa *rsa, |
767 | | u8 *key, size_t *keysize, int key_ref) |
768 | 0 | { |
769 | 0 | size_t base; |
770 | 0 | int r, key_num = key_ref + 1; |
771 | |
|
772 | 0 | switch (rsa->modulus.len) { |
773 | 0 | case 512 / 8: |
774 | 0 | case 768 / 8: |
775 | 0 | case 1024 / 8: |
776 | 0 | case 2048 / 8: |
777 | 0 | break; |
778 | 0 | default: |
779 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
780 | 0 | } |
781 | | |
782 | 0 | base = rsa->modulus.len / 2; |
783 | 0 | if (*keysize < (5 * base + 10)) |
784 | 0 | return SC_ERROR_BUFFER_TOO_SMALL; |
785 | 0 | *keysize = 5 * base + 10; |
786 | |
|
787 | 0 | memset(key, 0, *keysize); |
788 | 0 | *key++ = (5 * base + 7) >> 8; |
789 | 0 | *key++ = (5 * base + 7) & 0xFF; |
790 | 0 | *key++ = key_num; |
791 | | |
792 | | /* Funny code - not sure why we do it this way: |
793 | | * |
794 | | * Specs say: We store: (Length) |
795 | | * modulus modulus (N bytes) |
796 | | * J0 Montgomery const 0 (N/2 bytes) |
797 | | * H Montgomery const 0 (N bytes) |
798 | | * exponent exponent 4 |
799 | | * |
800 | | * --okir */ |
801 | 0 | if ((r = bn2cf(&rsa->modulus, key + 0 * base, 2 * base)) < 0 |
802 | 0 | || (r = bn2cf(&rsa->exponent, key + 5 * base, 4)) < 0) |
803 | 0 | return r; |
804 | | |
805 | 0 | return 0; |
806 | 0 | } |
807 | | |
808 | | /* |
809 | | * Cyberflex key encoding |
810 | | */ |
811 | | static int |
812 | | cyberflex_encode_private_key(sc_profile_t *profile, sc_card_t *card, |
813 | | struct sc_pkcs15_prkey_rsa *rsa, |
814 | | u8 *key, size_t *keysize, int key_ref) |
815 | 0 | { |
816 | 0 | size_t base = rsa->modulus.len / 2, key_blob_size, bnlen; |
817 | 0 | int r, key_num = key_ref + 1, alg_id; |
818 | |
|
819 | 0 | switch (rsa->modulus.len) { |
820 | 0 | case 512 / 8: alg_id = 0xC4; break; |
821 | 0 | case 768 / 8: alg_id = 0xC6; break; |
822 | 0 | case 1024 / 8: alg_id = 0xC8; break; |
823 | 0 | default: |
824 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
825 | 0 | } |
826 | | |
827 | 0 | key_blob_size = 12 + 5 * (base + 3) + 4; |
828 | 0 | if (*keysize < key_blob_size) |
829 | 0 | return SC_ERROR_BUFFER_TOO_SMALL; |
830 | 0 | *keysize = key_blob_size; |
831 | |
|
832 | 0 | memset(key, 0, *keysize); |
833 | 0 | *key++ = key_blob_size >> 8; |
834 | 0 | *key++ = key_blob_size & 0xFF; |
835 | 0 | *key++ = key_num; |
836 | 0 | *key++ = alg_id; |
837 | | |
838 | | /* key blob header: |
839 | | * "C2:06:C1:08:13:00:00:05" |
840 | | */ |
841 | 0 | memcpy(key, "\xc2\x06\xc1\x08\x12\x00\x00\x05", 8); |
842 | 0 | key += 8; |
843 | | |
844 | | /* Each bignum is encoded with a 2 byte header and a |
845 | | * NULL pad byte */ |
846 | 0 | bnlen = base + 3; |
847 | |
|
848 | 0 | if ((r = bn2cft(&rsa->q, 0xC2, key + 0 * bnlen, bnlen)) < 0 |
849 | 0 | || (r = bn2cft(&rsa->p, 0xC2, key + 1 * bnlen, bnlen)) < 0 |
850 | 0 | || (r = bn2cft(&rsa->iqmp, 0xC2, key + 2 * bnlen, bnlen)) < 0 |
851 | 0 | || (r = bn2cft(&rsa->dmq1, 0xC2, key + 3 * bnlen, bnlen)) < 0 |
852 | 0 | || (r = bn2cft(&rsa->dmp1, 0xC2, key + 4 * bnlen, bnlen)) < 0) |
853 | 0 | return r; |
854 | | |
855 | 0 | key += 5 * bnlen; |
856 | 0 | key[0] = 0x0A; |
857 | 0 | key[1] = 0x0A; |
858 | 0 | key[2] = 0x00; |
859 | 0 | key[3] = 0x00; |
860 | |
|
861 | 0 | return 0; |
862 | 0 | } |
863 | | |
864 | | static int |
865 | | cyberflex_encode_public_key(sc_profile_t *profile, sc_card_t *card, |
866 | | struct sc_pkcs15_prkey_rsa *rsa, |
867 | | u8 *key, size_t *keysize, int key_ref) |
868 | 0 | { |
869 | 0 | size_t base = rsa->modulus.len, key_blob_size, bnlen; |
870 | 0 | int r, key_num = key_ref + 1, alg_id; |
871 | |
|
872 | 0 | switch (rsa->modulus.len) { |
873 | 0 | case 512 / 8: alg_id = 0xC5; break; |
874 | 0 | case 768 / 8: alg_id = 0xC7; break; |
875 | 0 | case 1024 / 8: alg_id = 0xC9; break; |
876 | 0 | default: |
877 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
878 | 0 | } |
879 | | |
880 | 0 | key_blob_size = 12 + 3 + base + 7 + 4; |
881 | 0 | if (*keysize < key_blob_size) |
882 | 0 | return SC_ERROR_BUFFER_TOO_SMALL; |
883 | 0 | *keysize = key_blob_size; |
884 | |
|
885 | 0 | memset(key, 0, *keysize); |
886 | 0 | *key++ = key_blob_size >> 8; |
887 | 0 | *key++ = key_blob_size & 0xFF; |
888 | 0 | *key++ = key_num; |
889 | 0 | *key++ = alg_id; |
890 | | |
891 | | /* Key blob header */ |
892 | 0 | memcpy(key, "\xC1\x06\xC0\x08\x13\x00\x00\x05", 8); |
893 | 0 | key += 8; |
894 | |
|
895 | 0 | bnlen = rsa->modulus.len + 3; |
896 | 0 | if ((r = bn2cft(&rsa->modulus, 0xC0, key, bnlen)) < 0 |
897 | 0 | || (r = bn2cft(&rsa->exponent, 0xC0, key + bnlen, 3 + 4)) < 0) |
898 | 0 | return r; |
899 | | |
900 | 0 | key += bnlen + 3 + 4; |
901 | 0 | key[0] = 0x0A; |
902 | 0 | key[1] = 0x0A; |
903 | 0 | key[2] = 0x00; |
904 | 0 | key[3] = 0x00; |
905 | 0 | return 0; |
906 | 0 | } |
907 | | |
908 | | static struct sc_pkcs15init_operations sc_pkcs15init_cryptoflex_operations = { |
909 | | cflex_erase_card, |
910 | | cryptoflex_init_card, |
911 | | cflex_create_dir, |
912 | | cflex_create_domain, |
913 | | cflex_select_pin_reference, |
914 | | cflex_create_pin, |
915 | | NULL, /* select_key_reference */ |
916 | | cflex_create_key, |
917 | | cflex_store_key, |
918 | | cflex_generate_key, |
919 | | cryptoflex_encode_private_key, |
920 | | cryptoflex_encode_public_key, |
921 | | NULL, /* finalize_card */ |
922 | | NULL, /* delete_object */ |
923 | | NULL, NULL, NULL, NULL, NULL, /* pkcs15init emulation */ |
924 | | NULL /* sanity_check */ |
925 | | }; |
926 | | |
927 | | static struct sc_pkcs15init_operations sc_pkcs15init_cyberflex_operations = { |
928 | | cflex_erase_card, |
929 | | NULL, /* init_card */ |
930 | | cflex_create_dir, |
931 | | cflex_create_domain, |
932 | | cflex_select_pin_reference, |
933 | | cflex_create_pin, |
934 | | NULL, /* select_key_reference */ |
935 | | cflex_create_key, |
936 | | cflex_store_key, |
937 | | cflex_generate_key, |
938 | | cyberflex_encode_private_key, |
939 | | cyberflex_encode_public_key, |
940 | | NULL, /* finalize_card */ |
941 | | NULL, /* delete_object */ |
942 | | NULL, NULL, NULL, NULL, NULL, /* pkcs15init emulation */ |
943 | | NULL /* sanity_check */ |
944 | | }; |
945 | | |
946 | | struct sc_pkcs15init_operations * |
947 | | sc_pkcs15init_get_cryptoflex_ops(void) |
948 | 0 | { |
949 | 0 | return &sc_pkcs15init_cryptoflex_operations; |
950 | 0 | } |
951 | | |
952 | | struct sc_pkcs15init_operations * |
953 | | sc_pkcs15init_get_cyberflex_ops(void) |
954 | 0 | { |
955 | 0 | return &sc_pkcs15init_cyberflex_operations; |
956 | 0 | } |