/src/opensc/src/pkcs15init/pkcs15-setcos.c
Line | Count | Source |
1 | | /* |
2 | | * SetOCS 4.4 specific operations for PKCS15 initialization |
3 | | * |
4 | | * Copyright (C) 2003, 2005 Zetes |
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/internal.h" |
30 | | #include "libopensc/log.h" |
31 | | #include "pkcs15-init.h" |
32 | | #include "profile.h" |
33 | | |
34 | 0 | #define SETCOS_MAX_PINS 7 |
35 | | |
36 | | static unsigned char SETCOS_DEFAULT_PUBKEY[] = {0x01, 0x00, 0x01}; |
37 | 0 | #define SETCOS_DEFAULT_PUBKEY_LEN sizeof(SETCOS_DEFAULT_PUBKEY) |
38 | | |
39 | | static int setcos_create_pin_internal(sc_profile_t *, sc_pkcs15_card_t *, |
40 | | int, sc_pkcs15_auth_info_t *, const u8 *, size_t, const u8 *, size_t); |
41 | | |
42 | | |
43 | | static int |
44 | | setcos_puk_retries(sc_profile_t *profile, int pin_ref) |
45 | 0 | { |
46 | 0 | sc_pkcs15_auth_info_t auth_info = {0}; |
47 | |
|
48 | 0 | auth_info.auth_type = SC_PKCS15_PIN_AUTH_TYPE_PIN; |
49 | 0 | auth_info.attrs.pin.reference = 1; /* Default SO PIN ref. */ |
50 | 0 | sc_profile_get_pin_info(profile, SC_PKCS15INIT_SO_PIN, &auth_info); |
51 | | |
52 | | /* If pin_ref is the SO PIN, get the SO PUK info, otherwise the User PUK info */ |
53 | 0 | sc_profile_get_pin_info(profile, |
54 | 0 | pin_ref == auth_info.attrs.pin.reference ? SC_PKCS15INIT_SO_PUK : SC_PKCS15INIT_USER_PUK, |
55 | 0 | &auth_info); |
56 | |
|
57 | 0 | if ((auth_info.tries_left < 0) || (auth_info.tries_left > 15)) |
58 | 0 | return 3; /* Little extra safety */ |
59 | 0 | return auth_info.tries_left; |
60 | 0 | } |
61 | | |
62 | | |
63 | | /* |
64 | | * Erase the card. |
65 | | */ |
66 | | static int setcos_erase_card(sc_profile_t *profile, sc_pkcs15_card_t *p15card) |
67 | 0 | { |
68 | 0 | sc_path_t path; |
69 | 0 | int r; |
70 | | |
71 | | /* Just delete the entire MF */ |
72 | | |
73 | | /* Select parent DF and verify PINs/key as necessary */ |
74 | 0 | r = sc_pkcs15init_authenticate(profile, p15card, profile->mf_info->file, SC_AC_OP_DELETE); |
75 | 0 | if (r < 0) |
76 | 0 | return r == SC_ERROR_FILE_NOT_FOUND ? 0 : r; |
77 | | |
78 | | /* Empty path -> we have to to delete the current DF (= the MF) */ |
79 | 0 | memset(&path, 0, sizeof(sc_path_t)); |
80 | 0 | r = sc_delete_file(p15card->card, &path) ; |
81 | 0 | if (r) |
82 | 0 | return r; |
83 | | |
84 | 0 | sc_free_apps(p15card->card); |
85 | 0 | return 0; |
86 | 0 | } |
87 | | |
88 | | |
89 | | /* |
90 | | * Create the MF and global pin file if they don't exist. |
91 | | */ |
92 | | static int |
93 | | setcos_init_card(sc_profile_t *profile, sc_pkcs15_card_t *p15card) |
94 | 0 | { |
95 | 0 | struct sc_context *ctx = p15card->card->ctx; |
96 | 0 | sc_file_t *mf = profile->mf_info->file; |
97 | 0 | sc_file_t *pinfile; |
98 | 0 | int r; |
99 | |
|
100 | 0 | SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_VERBOSE); |
101 | | |
102 | | /* Create the MF if it doesn't exist yet */ |
103 | 0 | r = sc_select_file(p15card->card, &mf->path, NULL); |
104 | 0 | if (r == SC_ERROR_FILE_NOT_FOUND) { |
105 | 0 | sc_log(ctx, "MF doesn't exist, creating now"); |
106 | | |
107 | | /* Fix up the file's ACLs */ |
108 | 0 | r = sc_pkcs15init_fixup_file(profile, p15card, mf); |
109 | 0 | LOG_TEST_RET(ctx, r, "MF fixup failed"); |
110 | | |
111 | 0 | mf->status = SC_FILE_STATUS_CREATION; |
112 | 0 | r = sc_create_file(p15card->card, mf); |
113 | 0 | LOG_TEST_RET(ctx, r, "MF creation failed"); |
114 | 0 | } |
115 | 0 | LOG_TEST_RET(ctx, r, "Cannot select MF"); |
116 | | |
117 | | /* Create the global pin file if it doesn't exist yet */ |
118 | 0 | r = sc_profile_get_file(profile, "pinfile", &pinfile); |
119 | 0 | LOG_TEST_RET(ctx, r, "Cannot get 'pinfile' from profile"); |
120 | | |
121 | 0 | r = sc_select_file(p15card->card, &pinfile->path, NULL); |
122 | 0 | if (r == SC_ERROR_FILE_NOT_FOUND) { |
123 | 0 | sc_log(ctx, "Global pin file doesn't exist, creating now"); |
124 | | |
125 | | /* Fix up the file's ACLs */ |
126 | 0 | r = sc_pkcs15init_fixup_file(profile, p15card, pinfile); |
127 | 0 | if (r < 0) |
128 | 0 | sc_file_free(pinfile); |
129 | 0 | LOG_TEST_RET(ctx, r, "Pinfile fixup failed"); |
130 | | |
131 | | /* Set life cycle state to SC_FILE_STATUS_CREATION, |
132 | | * which means that all ACs are ignored. */ |
133 | 0 | pinfile->status = SC_FILE_STATUS_CREATION; |
134 | 0 | r = sc_create_file(p15card->card, pinfile); |
135 | 0 | if (r < 0) |
136 | 0 | sc_file_free(pinfile); |
137 | 0 | LOG_TEST_RET(ctx, r, "Pinfile creation failed"); |
138 | 0 | } |
139 | 0 | sc_file_free(pinfile); |
140 | 0 | LOG_TEST_RET(ctx, r, "Select pinfile failed"); |
141 | | |
142 | 0 | LOG_FUNC_RETURN(ctx, r); |
143 | 0 | } |
144 | | |
145 | | /* |
146 | | * Create a DF |
147 | | */ |
148 | | static int |
149 | | setcos_create_dir(sc_profile_t *profile, sc_pkcs15_card_t *p15card, sc_file_t *df) |
150 | 0 | { |
151 | 0 | struct sc_context *ctx = p15card->card->ctx; |
152 | 0 | int r; |
153 | |
|
154 | 0 | SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_VERBOSE); |
155 | |
|
156 | 0 | r = sc_pkcs15init_fixup_file(profile, p15card, df); |
157 | 0 | LOG_TEST_RET(ctx, r, "SetCOS file ACL fixup failed"); |
158 | | |
159 | 0 | r = sc_create_file(p15card->card, df); |
160 | 0 | LOG_TEST_RET(ctx, r, "SetCOS create file failed"); |
161 | | |
162 | 0 | LOG_FUNC_RETURN(ctx, r); |
163 | 0 | } |
164 | | |
165 | | |
166 | | /* |
167 | | * Select the PIN reference |
168 | | */ |
169 | | static int |
170 | | setcos_select_pin_reference(sc_profile_t *profile, sc_pkcs15_card_t *p15card, |
171 | | sc_pkcs15_auth_info_t *auth_info) |
172 | 0 | { |
173 | 0 | sc_pkcs15_auth_info_t auth_info_prof = {0}; |
174 | |
|
175 | 0 | auth_info_prof.attrs.pin.reference = 1; /* Default SO PIN ref. */ |
176 | 0 | auth_info_prof.auth_type = SC_PKCS15_PIN_AUTH_TYPE_PIN; |
177 | 0 | sc_profile_get_pin_info(profile, SC_PKCS15INIT_SO_PIN, &auth_info_prof); |
178 | | |
179 | | /* For the SO pin, we take the first available pin reference = 1 */ |
180 | 0 | if (auth_info != NULL && auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_SO_PIN) |
181 | 0 | auth_info->attrs.pin.reference = auth_info_prof.attrs.pin.reference; |
182 | | /* sc_pkcs15init_create_pin() starts checking if -1 is an acceptable |
183 | | * pin reference, which isn't for the SetCOS cards. And since the |
184 | | * value 1 has been assigned to the SO pin, we'll jump to 2. */ |
185 | 0 | else if (auth_info != NULL && auth_info->attrs.pin.reference <= 0) { |
186 | 0 | if (auth_info_prof.attrs.pin.reference != 1) |
187 | 0 | return SC_ERROR_INVALID_PIN_REFERENCE; |
188 | 0 | auth_info->attrs.pin.reference = auth_info_prof.attrs.pin.reference + 1; |
189 | 0 | } |
190 | | |
191 | 0 | return SC_SUCCESS; |
192 | 0 | } |
193 | | |
194 | | /* |
195 | | * Create a new PIN |
196 | | */ |
197 | | static int |
198 | | setcos_create_pin(sc_profile_t *profile, sc_pkcs15_card_t *p15card, |
199 | | sc_file_t *df, |
200 | | sc_pkcs15_object_t *pin_obj, |
201 | | const u8 *pin, size_t pin_len, |
202 | | const u8 *puk, size_t puk_len) |
203 | 0 | { |
204 | 0 | struct sc_context *ctx = p15card->card->ctx; |
205 | 0 | sc_pkcs15_auth_info_t *auth_info; |
206 | 0 | sc_file_t *pinfile = NULL, *tmp_pinfile = NULL; |
207 | 0 | int r, ignore_ac = 0; |
208 | |
|
209 | 0 | SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_VERBOSE); |
210 | |
|
211 | 0 | if (!pin_obj || !pin_obj->data) |
212 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
213 | | |
214 | 0 | auth_info = (sc_pkcs15_auth_info_t *)pin_obj->data; |
215 | |
|
216 | 0 | if (auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN) |
217 | 0 | return SC_ERROR_OBJECT_NOT_VALID; |
218 | | |
219 | | /* Create the global pin file if it doesn't exist yet */ |
220 | 0 | r = sc_profile_get_file(profile, "pinfile", &tmp_pinfile); |
221 | 0 | LOG_TEST_RET(ctx, r, "No 'pinfile' template in profile"); |
222 | | |
223 | 0 | r = sc_select_file(p15card->card, &tmp_pinfile->path, &pinfile); |
224 | 0 | sc_file_free(tmp_pinfile); |
225 | 0 | LOG_TEST_RET(ctx, r, "Cannot select 'pinfile'"); |
226 | | |
227 | 0 | sc_log(ctx, "pinfile->status:%X", pinfile->status); |
228 | 0 | sc_log(ctx, "create PIN with reference:%X, flags:%X, path:%s", |
229 | 0 | auth_info->attrs.pin.reference, auth_info->attrs.pin.flags, sc_print_path(&auth_info->path)); |
230 | |
|
231 | 0 | if (pinfile->status == SC_FILE_STATUS_CREATION) |
232 | 0 | ignore_ac = 1; |
233 | |
|
234 | 0 | r = setcos_create_pin_internal(profile, p15card, ignore_ac, auth_info, |
235 | 0 | pin, pin_len, puk, puk_len); |
236 | | |
237 | | /* If pinfile is in 'Creation' state and SOPIN has been created, |
238 | | * change status of MF and 'pinfile' to 'Operational:Activated' |
239 | | */ |
240 | 0 | sc_file_free(pinfile); |
241 | 0 | if (ignore_ac && (auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_SO_PIN)) { |
242 | 0 | sc_file_t *mf = profile->mf_info->file; |
243 | |
|
244 | 0 | r = sc_card_ctl(p15card->card, SC_CARDCTL_SETCOS_ACTIVATE_FILE, NULL); |
245 | 0 | LOG_TEST_RET(ctx, r, "Cannot set 'pinfile' into the activated state"); |
246 | | |
247 | 0 | r = sc_select_file(p15card->card, &mf->path, NULL); |
248 | 0 | LOG_TEST_RET(ctx, r, "Cannot select MF"); |
249 | | |
250 | 0 | r = sc_card_ctl(p15card->card, SC_CARDCTL_SETCOS_ACTIVATE_FILE, NULL); |
251 | 0 | LOG_TEST_RET(ctx, r, "Cannot set MF into the activated state"); |
252 | 0 | } |
253 | | |
254 | 0 | LOG_FUNC_RETURN(ctx, r); |
255 | 0 | } |
256 | | |
257 | | /* |
258 | | * Setup file struct & path: get correct template from the profile, construct full path |
259 | | */ |
260 | | static int |
261 | | setcos_new_file(sc_profile_t *profile, sc_card_t *card, |
262 | | unsigned int type, |
263 | | unsigned int num, /* number of objects of this type already on the card */ |
264 | | sc_file_t **out) |
265 | 0 | { |
266 | 0 | sc_file_t *file = NULL; |
267 | 0 | sc_path_t *p; |
268 | 0 | char name[64]; |
269 | 0 | const char *tag; |
270 | 0 | int r; |
271 | |
|
272 | 0 | if (type == SC_PKCS15_TYPE_PRKEY_RSA) |
273 | 0 | tag = "private-key"; |
274 | 0 | else if (type == SC_PKCS15_TYPE_PUBKEY_RSA) |
275 | 0 | tag = "public-key"; |
276 | 0 | else if ((type & SC_PKCS15_TYPE_CLASS_MASK) == SC_PKCS15_TYPE_CERT) |
277 | 0 | tag = "certificate"; |
278 | 0 | else if ((type & SC_PKCS15_TYPE_CLASS_MASK) == SC_PKCS15_TYPE_DATA_OBJECT) |
279 | 0 | tag = "data"; |
280 | 0 | else { |
281 | 0 | sc_log(card->ctx, "Unsupported file type"); |
282 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
283 | 0 | } |
284 | | |
285 | | /* Get template from profile */ |
286 | 0 | snprintf(name, sizeof(name), "template-%s", tag); |
287 | 0 | if (sc_profile_get_file(profile, name, &file) < 0) { |
288 | 0 | sc_log(card->ctx, "Profile doesn't define %s", name); |
289 | 0 | return SC_ERROR_NOT_SUPPORTED; |
290 | 0 | } |
291 | | |
292 | | /* Auto-increment FID for next object */ |
293 | 0 | file->id += num; |
294 | 0 | p = &file->path; |
295 | 0 | *p = profile->df_info->file->path; |
296 | 0 | if (p->len + 2 > SC_MAX_PATH_SIZE) { |
297 | 0 | sc_file_free(file); |
298 | 0 | return SC_ERROR_INVALID_DATA; |
299 | 0 | } |
300 | 0 | p->value[p->len++] = (u8) (file->id / 256); |
301 | 0 | p->value[p->len++] = (u8) (file->id % 256); |
302 | | |
303 | | /* Increment FID until there's no file with such path */ |
304 | 0 | r = sc_select_file(card, p, NULL); |
305 | 0 | while(r == 0) { |
306 | 0 | file->id++; |
307 | 0 | p->value[p->len - 2] = (u8) (file->id / 256); |
308 | 0 | p->value[p->len - 1] = (u8) (file->id % 256); |
309 | 0 | r = sc_select_file(card, p, NULL); |
310 | 0 | } |
311 | |
|
312 | 0 | *out = file; |
313 | 0 | return 0; |
314 | 0 | } |
315 | | |
316 | | static int |
317 | | setcos_encode_private_key(sc_profile_t *profile, sc_card_t *card, |
318 | | struct sc_pkcs15_prkey_rsa *rsa, |
319 | | u8 *key, size_t *keysize, int key_ref) |
320 | 0 | { |
321 | 0 | return 0; |
322 | 0 | } |
323 | | |
324 | | static int |
325 | | setcos_encode_public_key(sc_profile_t *profile, sc_card_t *card, |
326 | | struct sc_pkcs15_prkey_rsa *rsa, |
327 | | u8 *key, size_t *keysize, int key_ref) |
328 | 0 | { |
329 | 0 | return 0; |
330 | 0 | } |
331 | | |
332 | | |
333 | | static int |
334 | | setcos_create_key(sc_profile_t *profile, sc_pkcs15_card_t *p15card, |
335 | | struct sc_pkcs15_object *object) |
336 | 0 | { |
337 | 0 | struct sc_context *ctx = p15card->card->ctx; |
338 | 0 | struct sc_pkcs15_prkey_info *key_info; |
339 | 0 | struct sc_file *file = NULL; |
340 | 0 | size_t keybits; |
341 | 0 | int r; |
342 | |
|
343 | 0 | SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_VERBOSE); |
344 | |
|
345 | 0 | if (!object || !object->data) |
346 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
347 | | |
348 | 0 | key_info = (struct sc_pkcs15_prkey_info *)object->data; |
349 | 0 | keybits = key_info->modulus_length; |
350 | |
|
351 | 0 | if (object->type != SC_PKCS15_TYPE_PRKEY_RSA) |
352 | 0 | LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "Create key failed: RSA only supported"); |
353 | | |
354 | | /* Parameter check */ |
355 | 0 | if ((keybits < 512) || (keybits > 1024) || (keybits & 0x7)) |
356 | 0 | LOG_TEST_RET(ctx, SC_ERROR_INVALID_ARGUMENTS, "Invalid key length"); |
357 | | |
358 | 0 | sc_log(ctx, "create private key ID:%s\n", sc_pkcs15_print_id(&key_info->id)); |
359 | | |
360 | | /* Get the private key file */ |
361 | 0 | r = setcos_new_file(profile, p15card->card, SC_PKCS15_TYPE_PRKEY_RSA, key_info->key_reference, &file); |
362 | 0 | LOG_TEST_RET(ctx, r, "Cannot get new private key file"); |
363 | | |
364 | | /* Take enough room for a 1024 bit key */ |
365 | 0 | if (file->size < 512) |
366 | 0 | file->size = 512; |
367 | | |
368 | | /* Replace the path of instantiated key template by the path from the object data. */ |
369 | 0 | memcpy(&file->path, &key_info->path, sizeof(file->path)); |
370 | 0 | if (file->path.len < 2) { |
371 | 0 | sc_file_free(file); |
372 | 0 | LOG_TEST_RET(ctx, SC_ERROR_INVALID_DATA, "Invalid path"); |
373 | 0 | } |
374 | 0 | file->id = file->path.value[file->path.len - 2] * 0x100 |
375 | 0 | + file->path.value[file->path.len - 1]; |
376 | |
|
377 | 0 | key_info->key_reference = file->path.value[file->path.len - 1] & 0xFF; |
378 | |
|
379 | 0 | sc_log(ctx, "Path of private key file to create %s\n", sc_print_path(&file->path)); |
380 | |
|
381 | 0 | r = sc_select_file(p15card->card, &file->path, NULL); |
382 | 0 | if (!r) { |
383 | 0 | r = sc_pkcs15init_delete_by_path(profile, p15card, &file->path); |
384 | 0 | if (r != SC_SUCCESS) |
385 | 0 | sc_file_free(file); |
386 | 0 | LOG_TEST_RET(ctx, r, "Failed to delete private key file"); |
387 | 0 | } else if (r != SC_ERROR_FILE_NOT_FOUND) { |
388 | 0 | sc_file_free(file); |
389 | 0 | file = NULL; |
390 | 0 | LOG_TEST_RET(ctx, r, "Select private key file error"); |
391 | 0 | } |
392 | | |
393 | | /* Now create the key file */ |
394 | 0 | r = sc_pkcs15init_create_file(profile, p15card, file); |
395 | 0 | sc_file_free(file); |
396 | 0 | LOG_TEST_RET(ctx, r, "Cannot create private key file"); |
397 | | |
398 | 0 | LOG_FUNC_RETURN(ctx, r); |
399 | 0 | } |
400 | | |
401 | | |
402 | | /* |
403 | | * Store a private key |
404 | | */ |
405 | | static int |
406 | | setcos_store_key(struct sc_profile *profile, struct sc_pkcs15_card *p15card, |
407 | | struct sc_pkcs15_object *object, |
408 | | struct sc_pkcs15_prkey *prkey) |
409 | 0 | { |
410 | 0 | struct sc_context *ctx = p15card->card->ctx; |
411 | 0 | struct sc_pkcs15_prkey_info *key_info; |
412 | 0 | struct sc_cardctl_setcos_gen_store_key_info args; |
413 | 0 | struct sc_file *file = NULL; |
414 | 0 | int r; |
415 | 0 | size_t keybits; |
416 | |
|
417 | 0 | SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_VERBOSE); |
418 | |
|
419 | 0 | if (!object || !object->data || !prkey) |
420 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
421 | | |
422 | 0 | if (object->type != SC_PKCS15_TYPE_PRKEY_RSA) |
423 | 0 | LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "Store key failed: RSA only supported"); |
424 | | |
425 | 0 | if (!prkey->u.rsa.exponent.data || prkey->u.rsa.exponent.len == 0 || !prkey->u.rsa.p.data || prkey->u.rsa.p.len == 0 || !prkey->u.rsa.q.data || prkey->u.rsa.q.len == 0) |
426 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
427 | | |
428 | 0 | key_info = (struct sc_pkcs15_prkey_info *)object->data; |
429 | 0 | keybits = key_info->modulus_length; |
430 | | |
431 | | /* Parameter check */ |
432 | 0 | if ( (keybits < 512) || (keybits > 1024) || (keybits & 0x7)) |
433 | 0 | LOG_TEST_RET(ctx, SC_ERROR_INVALID_ARGUMENTS, "Invalid key length"); |
434 | | |
435 | 0 | sc_log(ctx, "store key with ID:%s and path:%s\n", sc_pkcs15_print_id(&key_info->id), |
436 | 0 | sc_print_path(&key_info->path)); |
437 | |
|
438 | 0 | r = sc_select_file(p15card->card, &key_info->path, &file); |
439 | 0 | LOG_TEST_RET(ctx, r, "Cannot store key: select key file failed"); |
440 | | |
441 | 0 | r = sc_pkcs15init_authenticate(profile, p15card, file, SC_AC_OP_UPDATE); |
442 | 0 | sc_file_free(file); |
443 | 0 | LOG_TEST_RET(ctx, r, "No authorisation to store private key"); |
444 | | |
445 | | /* Fill in data structure */ |
446 | 0 | memset(&args, 0, sizeof(args)); |
447 | 0 | args.mod_len = keybits; |
448 | 0 | args.op_type = OP_TYPE_STORE; |
449 | 0 | args.pubexp_len = prkey->u.rsa.exponent.len * 8; |
450 | 0 | args.pubexp = prkey->u.rsa.exponent.data; |
451 | 0 | args.primep_len = prkey->u.rsa.p.len * 8; |
452 | 0 | args.primep = prkey->u.rsa.p.data; |
453 | 0 | args.primeq_len = prkey->u.rsa.q.len * 8; |
454 | 0 | args.primeq = prkey->u.rsa.q.data; |
455 | | |
456 | | /* Generate/store rsa key */ |
457 | 0 | r = sc_card_ctl(p15card->card, SC_CARDCTL_SETCOS_GENERATE_STORE_KEY, &args); |
458 | 0 | LOG_TEST_RET(ctx, r, "Card control 'GENERATE_STORE_KEY' failed"); |
459 | | |
460 | 0 | LOG_FUNC_RETURN(ctx, r); |
461 | 0 | } |
462 | | |
463 | | |
464 | | static int |
465 | | setcos_generate_key(struct sc_profile *profile, struct sc_pkcs15_card *p15card, |
466 | | struct sc_pkcs15_object *object, |
467 | | struct sc_pkcs15_pubkey *pubkey) |
468 | 0 | { |
469 | 0 | struct sc_context *ctx = p15card->card->ctx; |
470 | 0 | struct sc_cardctl_setcos_gen_store_key_info args; |
471 | 0 | struct sc_cardctl_setcos_data_obj data_obj; |
472 | 0 | struct sc_pkcs15_prkey_info *key_info; |
473 | 0 | int r; |
474 | 0 | size_t keybits; |
475 | 0 | unsigned char raw_pubkey[256]; |
476 | 0 | struct sc_file *file = NULL; |
477 | |
|
478 | 0 | SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_VERBOSE); |
479 | |
|
480 | 0 | if (!object || !object->data) |
481 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
482 | | |
483 | 0 | key_info = (struct sc_pkcs15_prkey_info *)object->data; |
484 | 0 | keybits = key_info->modulus_length; |
485 | |
|
486 | 0 | if (object->type != SC_PKCS15_TYPE_PRKEY_RSA) |
487 | 0 | LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "Generate key failed: RSA only supported"); |
488 | | |
489 | | /* Parameter check */ |
490 | 0 | if ( (keybits < 512) || (keybits > 1024) || (keybits & 0x7)) |
491 | 0 | LOG_TEST_RET(ctx, SC_ERROR_INVALID_ARGUMENTS, "Invalid key length"); |
492 | | |
493 | 0 | r = sc_select_file(p15card->card, &key_info->path, &file); |
494 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "Cannot store key: select key file failed"); |
495 | | |
496 | | /* Authenticate */ |
497 | 0 | r = sc_pkcs15init_authenticate(profile, p15card, file, SC_AC_OP_UPDATE); |
498 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "No authorisation to store private key"); |
499 | | |
500 | | /* Fill in data structure */ |
501 | 0 | memset(&args, 0, sizeof(args)); |
502 | 0 | args.mod_len = keybits; |
503 | 0 | args.op_type = OP_TYPE_GENERATE; |
504 | 0 | args.pubexp_len = SETCOS_DEFAULT_PUBKEY_LEN * 8; |
505 | 0 | args.pubexp = SETCOS_DEFAULT_PUBKEY; |
506 | | |
507 | | /* Generate/store rsa key */ |
508 | 0 | r = sc_card_ctl(p15card->card, SC_CARDCTL_SETCOS_GENERATE_STORE_KEY, &args); |
509 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "Card control 'GENERATE_STORE_KEY' failed"); |
510 | | |
511 | | /* Key pair generation -> collect public key info */ |
512 | 0 | if (pubkey != NULL) { |
513 | 0 | pubkey->algorithm = SC_ALGORITHM_RSA; |
514 | 0 | pubkey->u.rsa.modulus.len = BYTES4BITS(keybits); |
515 | 0 | pubkey->u.rsa.modulus.data = malloc(pubkey->u.rsa.modulus.len); |
516 | 0 | if (pubkey->u.rsa.modulus.data == NULL) { |
517 | 0 | r = SC_ERROR_OUT_OF_MEMORY; |
518 | 0 | goto err; |
519 | 0 | } |
520 | 0 | pubkey->u.rsa.exponent.len = SETCOS_DEFAULT_PUBKEY_LEN; |
521 | 0 | pubkey->u.rsa.exponent.data = malloc(SETCOS_DEFAULT_PUBKEY_LEN); |
522 | 0 | if (pubkey->u.rsa.exponent.data == NULL) { |
523 | 0 | r = SC_ERROR_OUT_OF_MEMORY; |
524 | 0 | goto err; |
525 | 0 | } |
526 | 0 | memcpy(pubkey->u.rsa.exponent.data, SETCOS_DEFAULT_PUBKEY, SETCOS_DEFAULT_PUBKEY_LEN); |
527 | | |
528 | | /* Get public key modulus */ |
529 | 0 | r = sc_select_file(p15card->card, &file->path, NULL); |
530 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "Cannot get key modulus: select key file failed"); |
531 | | |
532 | 0 | data_obj.P1 = 0x01; |
533 | 0 | data_obj.P2 = 0x01; |
534 | 0 | data_obj.Data = raw_pubkey; |
535 | 0 | data_obj.DataLen = sizeof(raw_pubkey); |
536 | |
|
537 | 0 | r = sc_card_ctl(p15card->card, SC_CARDCTL_SETCOS_GETDATA, &data_obj); |
538 | 0 | LOG_TEST_GOTO_ERR(ctx, r, "Cannot get key modulus: 'SETCOS_GETDATA' failed"); |
539 | | |
540 | 0 | if (data_obj.DataLen < pubkey->u.rsa.modulus.len + 2) { |
541 | 0 | sc_log(ctx, "Cannot get key modulus: wrong length of raw key"); |
542 | 0 | r = SC_ERROR_UNKNOWN_DATA_RECEIVED; |
543 | 0 | goto err; |
544 | 0 | } |
545 | | |
546 | 0 | keybits = ((raw_pubkey[0] * 256) + raw_pubkey[1]); /* modulus bit length */ |
547 | 0 | if (keybits != key_info->modulus_length) { |
548 | 0 | sc_log(ctx, "key-size from card[%zu] does not match[%zu]", |
549 | 0 | keybits, key_info->modulus_length); |
550 | 0 | r = SC_ERROR_PKCS15INIT; |
551 | 0 | goto err; |
552 | 0 | } |
553 | 0 | memcpy(pubkey->u.rsa.modulus.data, &raw_pubkey[2], pubkey->u.rsa.modulus.len); |
554 | 0 | } |
555 | 0 | sc_file_free(file); |
556 | |
|
557 | 0 | return r; |
558 | | |
559 | 0 | err: |
560 | 0 | sc_file_free(file); |
561 | 0 | if (pubkey != NULL) { |
562 | 0 | free(pubkey->u.rsa.modulus.data); |
563 | 0 | pubkey->u.rsa.modulus.data = NULL; |
564 | 0 | pubkey->u.rsa.modulus.len = 0; |
565 | 0 | free(pubkey->u.rsa.exponent.data); |
566 | 0 | pubkey->u.rsa.exponent.data = NULL; |
567 | 0 | pubkey->u.rsa.exponent.len = 0; |
568 | 0 | } |
569 | 0 | LOG_FUNC_RETURN(ctx, r); |
570 | 0 | } |
571 | | |
572 | | |
573 | | /* |
574 | | * Create a new PIN |
575 | | */ |
576 | | static int |
577 | | setcos_create_pin_internal(sc_profile_t *profile, sc_pkcs15_card_t *p15card, |
578 | | int ignore_ac, sc_pkcs15_auth_info_t *auth_info, |
579 | | const u8 *pin, size_t pin_len, |
580 | | const u8 *puk, size_t puk_len) |
581 | 0 | { |
582 | 0 | struct sc_context *ctx = p15card->card->ctx; |
583 | 0 | int r; |
584 | 0 | struct sc_cardctl_setcos_data_obj data_obj; |
585 | 0 | sc_file_t *pinfile = NULL; |
586 | 0 | u8 data[32] = {0}; |
587 | |
|
588 | 0 | SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_VERBOSE); |
589 | 0 | if (!auth_info || auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN) |
590 | 0 | return SC_ERROR_OBJECT_NOT_VALID; |
591 | | |
592 | 0 | if (auth_info->attrs.pin.reference >= SETCOS_MAX_PINS) |
593 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
594 | 0 | if (pin == NULL || puk == NULL || pin_len < 4 || pin_len > 8 || puk_len < 4 || puk_len > 8) |
595 | 0 | return SC_ERROR_INVALID_PIN_LENGTH; |
596 | | |
597 | | /* Verify required access rights if needed (i.e. if the |
598 | | * pin file isn't in the CREATE life cycle state). */ |
599 | 0 | if (!ignore_ac) { |
600 | 0 | r = sc_profile_get_file(profile, "pinfile", &pinfile); |
601 | 0 | if (r >= 0) |
602 | 0 | r = sc_pkcs15init_authenticate(profile, p15card, pinfile, SC_AC_OP_UPDATE); |
603 | 0 | sc_file_free(pinfile); |
604 | 0 | if (r < 0) |
605 | 0 | return r; |
606 | 0 | } |
607 | | |
608 | | /* Make command to add a pin-record */ |
609 | 0 | data_obj.P1 = 01; |
610 | 0 | data_obj.P2 = 01; |
611 | | |
612 | | /* setcos pin number */ |
613 | 0 | data[0] = auth_info->attrs.pin.reference; |
614 | |
|
615 | 0 | memset(&data[1], auth_info->attrs.pin.pad_char, 16); /* padding */ |
616 | 0 | memcpy(&data[1], (u8 *)pin, pin_len); /* copy pin*/ |
617 | 0 | memcpy(&data[9], (u8 *)puk, puk_len); /* copy puk */ |
618 | |
|
619 | 0 | data[17] = auth_info->tries_left & 0x0F; |
620 | 0 | data[18] = auth_info->tries_left & 0x0F; |
621 | | /* 0xF0: unlimited unblock tries */ |
622 | 0 | data[19] = 0xF0 | setcos_puk_retries(profile, auth_info->attrs.pin.reference); |
623 | | |
624 | | /* Allow an unlimited number of signatures after a pin verification. |
625 | | * If set to 1 or so, we would have a UserConsent PIN. */ |
626 | 0 | data[20] = 0x00; |
627 | |
|
628 | 0 | if (auth_info->attrs.pin.type == 0) |
629 | 0 | data[21] = 0x01; /* BCD */ |
630 | 0 | else |
631 | 0 | data[21] = 0x00; /* ASCII */ |
632 | 0 | if ((auth_info->attrs.pin.flags & 0x010) == 0) /* test for initial pin */ |
633 | 0 | data[21] |= 0x80; |
634 | |
|
635 | 0 | data[22] = 0x00; /* not used */ |
636 | 0 | data[23] = 0x00; /* not used */ |
637 | |
|
638 | 0 | data_obj.Data = data; |
639 | 0 | data_obj.DataLen = 24; |
640 | |
|
641 | 0 | r = sc_card_ctl(p15card->card, SC_CARDCTL_SETCOS_PUTDATA, &data_obj); |
642 | 0 | sc_mem_clear(data, sizeof(data)); |
643 | |
|
644 | 0 | SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_VERBOSE, r); |
645 | 0 | } |
646 | | |
647 | | |
648 | | static struct sc_pkcs15init_operations sc_pkcs15init_setcos_operations = { |
649 | | setcos_erase_card, /* erase_card */ |
650 | | setcos_init_card, /* init_card */ |
651 | | setcos_create_dir, /* create_dir */ |
652 | | NULL, /* create_domain */ |
653 | | setcos_select_pin_reference, /* select_pin_reference */ |
654 | | setcos_create_pin, /* create_pin */ |
655 | | NULL, /* select_key_reference */ |
656 | | setcos_create_key, /* create_key */ |
657 | | setcos_store_key, /* store_key */ |
658 | | setcos_generate_key, /* generate_key */ |
659 | | setcos_encode_private_key, /* encode_private_key */ |
660 | | setcos_encode_public_key, /* encode_public_key */ |
661 | | NULL, /* finalize_card */ |
662 | | NULL, /* delete_object */ |
663 | | NULL, /* emu_update_dir */ |
664 | | NULL, /* emu_update_any_df */ |
665 | | NULL, /* emu_update_tokeninfo */ |
666 | | NULL, /* emu_write_info */ |
667 | | NULL, /* emu_store_data */ |
668 | | NULL /* sanity_check */ |
669 | | }; |
670 | | |
671 | | struct sc_pkcs15init_operations * |
672 | | sc_pkcs15init_get_setcos_ops(void) |
673 | 0 | { |
674 | 0 | return &sc_pkcs15init_setcos_operations; |
675 | 0 | } |