| | 1 | /* |
| | 2 | * PKCS15 emulation layer for Italian CNS. |
| | 3 | * |
| | 4 | * Copyright (C) 2008, Emanuele Pucciarelli <ep@acm.org> |
| | 5 | * Many snippets have been taken out from other PKCS15 emulation layer |
| | 6 | * modules in this directory; their copyright is their authors'. |
| | 7 | * |
| | 8 | * This library is free software; you can redistribute it and/or |
| | 9 | * modify it under the terms of the GNU Lesser General Public |
| | 10 | * License as published by the Free Software Foundation; either |
| | 11 | * version 2.1 of the License, or (at your option) any later version. |
| | 12 | * |
| | 13 | * This library is distributed in the hope that it will be useful, |
| | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| | 16 | * Lesser General Public License for more details. |
| | 17 | * |
| | 18 | * You should have received a copy of the GNU Lesser General Public |
| | 19 | * License along with this library; if not, write to the Free Software |
| | 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| | 21 | */ |
| | 22 | |
| | 23 | /* |
| | 24 | * Specifications for the development of this driver come from: |
| | 25 | * http://www.servizidemografici.interno.it/sitoCNSD/documentazioneRicerca.do?metodo=contenutoDocumento&servizio=documentazione&ID_DOCUMENTO=1043 |
| | 26 | */ |
| | 27 | |
| | 28 | |
| | 29 | #ifdef HAVE_CONFIG_H |
| | 30 | #include <config.h> |
| | 31 | #endif |
| | 32 | |
| | 33 | #include "pkcs15.h" |
| | 34 | #include "log.h" |
| | 35 | #include "cards.h" |
| | 36 | #include "itacns.h" |
| | 37 | #include <stdlib.h> |
| | 38 | #include <string.h> |
| | 39 | #include <stdio.h> |
| | 40 | #include "common/compat_strlcpy.h" |
| | 41 | |
| | 42 | #ifdef ENABLE_OPENSSL |
| | 43 | #include <openssl/x509v3.h> |
| | 44 | #endif |
| | 45 | |
| | 46 | int sc_pkcs15emu_itacns_init_ex(sc_pkcs15_card_t *, |
| | 47 | sc_pkcs15emu_opt_t *); |
| | 48 | |
| | 49 | static const char path_serial[] = "10001003"; |
| | 50 | |
| | 51 | /* Manufacturers */ |
| | 52 | |
| | 53 | char * itacns_mask_manufacturers[] = { |
| | 54 | "Unknown", |
| | 55 | "Kaitech", |
| | 56 | "Gemplus", |
| | 57 | "Ghirlanda", |
| | 58 | "Giesecke & Devrient", |
| | 59 | "Oberthur Card Systems", |
| | 60 | "Orga", |
| | 61 | "Axalto", |
| | 62 | "Siemens", |
| | 63 | "STIncard", |
| | 64 | "GEP", |
| | 65 | "EPS Corp", |
| | 66 | "Athena" |
| | 67 | }; |
| | 68 | |
| | 69 | char * iso7816_ic_manufacturers[] = { |
| | 70 | "Unknown", |
| | 71 | "Motorola", |
| | 72 | "STMicroelectronics", |
| | 73 | "Hitachi", |
| | 74 | "Philips Semiconductors", |
| | 75 | "Siemens", |
| | 76 | "Cylinc", |
| | 77 | "Texas Instruments", |
| | 78 | "Fujitsu", |
| | 79 | "Matsushita", |
| | 80 | "NEC", |
| | 81 | "Oki", |
| | 82 | "Toshiba", |
| | 83 | "Mitsubishi", |
| | 84 | "Samsung", |
| | 85 | "Hyundai", |
| | 86 | "LG" |
| | 87 | }; |
| | 88 | |
| | 89 | /* Data files */ |
| | 90 | |
| | 91 | static const struct { |
| | 92 | char *label; |
| | 93 | char *path; |
| | 94 | int cie_only; |
| | 95 | } itacns_data_files[] = { |
| | 96 | { "EF_DatiProcessore", "3F0010001002", 0 }, |
| | 97 | { "EF_IDCarta", "3F0010001003", 0 }, |
| | 98 | { "EF_DatiSistema", "3F0010001004", 1 }, |
| | 99 | { "EF_DatiPersonali", "3F0011001102", 0 }, |
| | 100 | { "EF_DatiPersonali_Annotazioni", "3F0011001103", 1 }, |
| | 101 | { "EF_Impronte", "3F0011001104", 1 }, |
| | 102 | { "EF_Foto", "3F0011001104", 1 }, |
| | 103 | { "EF_DatiPersonaliAggiuntivi", "3F0012001201", 0 }, |
| | 104 | { "EF_MemoriaResidua", "3F0012001202", 0 }, |
| | 105 | { "EF_ServiziInstallati", "3F0012001203", 0 }, |
| | 106 | { "EF_INST_FILE", "3F0012004142", 0 }, |
| | 107 | { "EF_CardStatus", "3F003F02", 0 }, |
| | 108 | { "EF_GDO", "3F002F02", 0 }, |
| | 109 | { "EF_RootInstFile", "3F000405", 0 } |
| | 110 | }; |
| | 111 | |
| | 112 | |
| | 113 | /* |
| | 114 | * Utility functions |
| | 115 | */ |
| | 116 | |
| | 117 | static void set_string(char **strp, const char *value) |
| | 118 | { |
| | 119 | if (*strp) |
| | 120 | free(*strp); |
| | 121 | *strp = value ? strdup(value) : NULL; |
| | 122 | } |
| | 123 | |
| | 124 | static int loadFile(const sc_pkcs15_card_t *p15card, const sc_path_t *path, |
| | 125 | u8 *buf, const size_t buflen) |
| | 126 | { |
| | 127 | SC_FUNC_CALLED(p15card->card->ctx, 1); |
| | 128 | |
| | 129 | int sc_res; |
| | 130 | sc_res = sc_select_file(p15card->card, path, NULL); |
| | 131 | if(sc_res != SC_SUCCESS) |
| | 132 | return sc_res; |
| | 133 | |
| | 134 | sc_res = sc_read_binary(p15card->card, 0, buf, buflen, 0); |
| | 135 | return sc_res; |
| | 136 | } |
| | 137 | |
| | 138 | /* |
| | 139 | * The following functions add objects to the card emulator. |
| | 140 | */ |
| | 141 | |
| | 142 | static int itacns_add_cert(sc_pkcs15_card_t *p15card, |
| | 143 | int type, int authority, const sc_path_t *path, |
| | 144 | const sc_pkcs15_id_t *id, const char *label, int obj_flags, |
| | 145 | int *ext_info_ok, int *key_usage, int *x_key_usage) |
| | 146 | { |
| | 147 | SC_FUNC_CALLED(p15card->card->ctx, 1); |
| | 148 | int r; |
| | 149 | *ext_info_ok = 0; |
| | 150 | |
| | 151 | /* const char *label = "Certificate"; */ |
| | 152 | sc_pkcs15_cert_info_t info; |
| | 153 | sc_pkcs15_object_t obj; |
| | 154 | |
| | 155 | memset(&info, 0, sizeof(info)); |
| | 156 | memset(&obj, 0, sizeof(obj)); |
| | 157 | |
| | 158 | info.id = *id; |
| | 159 | info.authority = authority; |
| | 160 | if (path) |
| | 161 | info.path = *path; |
| | 162 | |
| | 163 | strlcpy(obj.label, label, sizeof(obj.label)); |
| | 164 | obj.flags = obj_flags; |
| | 165 | |
| | 166 | r = sc_pkcs15emu_add_x509_cert(p15card, &obj, &info); |
| | 167 | SC_TEST_RET(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, r, "Could not add X.509 certificate"); |
| | 168 | |
| | 169 | /* If we have OpenSSL, read keyUsage */ |
| | 170 | #ifdef ENABLE_OPENSSL |
| | 171 | |
| | 172 | X509 *x509; |
| | 173 | sc_pkcs15_cert_t *cert; |
| | 174 | |
| | 175 | r = sc_pkcs15_read_certificate(p15card, &info, &cert); |
| | 176 | SC_TEST_RET(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, r, "Could not read X.509 certificate"); |
| | 177 | |
| | 178 | const u8 *throwaway = cert->data; |
| | 179 | x509 = d2i_X509(NULL, &throwaway, cert->data_len); |
| | 180 | sc_pkcs15_free_certificate(cert); |
| | 181 | if (!x509) return SC_SUCCESS; |
| | 182 | X509_check_purpose(x509, -1, 0); |
| | 183 | if(x509->ex_flags & EXFLAG_KUSAGE) { |
| | 184 | *ext_info_ok = 1; |
| | 185 | *key_usage = x509->ex_kusage; |
| | 186 | *x_key_usage = x509->ex_xkusage; |
| | 187 | } |
| | 188 | OPENSSL_free(x509); |
| | 189 | |
| | 190 | return SC_SUCCESS; |
| | 191 | |
| | 192 | #else /* ENABLE_OPENSSL */ |
| | 193 | |
| | 194 | return SC_SUCCESS; |
| | 195 | |
| | 196 | #endif /* ENABLE_OPENSSL */ |
| | 197 | |
| | 198 | } |
| | 199 | |
| | 200 | static int itacns_add_pubkey(sc_pkcs15_card_t *p15card, |
| | 201 | const sc_path_t *path, const sc_pkcs15_id_t *id, const char *label, |
| | 202 | int usage, int ref, int obj_flags, int *modulus_len_out) |
| | 203 | { |
| | 204 | SC_FUNC_CALLED(p15card->card->ctx, 1); |
| | 205 | |
| | 206 | int r; |
| | 207 | sc_pkcs15_pubkey_info_t info; |
| | 208 | sc_pkcs15_object_t obj; |
| | 209 | |
| | 210 | memset(&info, 0, sizeof(info)); |
| | 211 | memset(&obj, 0, sizeof(obj)); |
| | 212 | |
| | 213 | info.id = *id; |
| | 214 | if (path) |
| | 215 | info.path = *path; |
| | 216 | info.usage = usage; |
| | 217 | info.key_reference = ref; |
| | 218 | strlcpy(obj.label, label, sizeof(obj.label)); |
| | 219 | obj.flags = obj_flags; |
| | 220 | |
| | 221 | /* This is hard-coded, unless weird versions of the CNS turn up sometime. */ |
| | 222 | info.modulus_length = 1024; |
| | 223 | |
| | 224 | *modulus_len_out = info.modulus_length; |
| | 225 | r = sc_pkcs15emu_add_rsa_pubkey(p15card, &obj, &info); |
| | 226 | SC_TEST_RET(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, r, "Could not add pub key"); |
| | 227 | return r; |
| | 228 | } |
| | 229 | |
| | 230 | static int itacns_add_prkey(sc_pkcs15_card_t *p15card, |
| | 231 | const sc_pkcs15_id_t *id, |
| | 232 | const char *label, |
| | 233 | int type, unsigned int modulus_length, int usage, |
| | 234 | int algo_flags, const sc_path_t *path, int ref, |
| | 235 | const sc_pkcs15_id_t *auth_id, int obj_flags) |
| | 236 | { |
| | 237 | SC_FUNC_CALLED(p15card->card->ctx, 1); |
| | 238 | |
| | 239 | sc_pkcs15_prkey_info_t info; |
| | 240 | sc_pkcs15_object_t obj; |
| | 241 | |
| | 242 | memset(&info, 0, sizeof(info)); |
| | 243 | memset(&obj, 0, sizeof(obj)); |
| | 244 | |
| | 245 | info.id = *id; |
| | 246 | info.modulus_length = modulus_length; |
| | 247 | info.usage = usage; |
| | 248 | info.native = 1; |
| | 249 | info.key_reference = ref; |
| | 250 | info.access_flags = |
| | 251 | SC_PKCS15_PRKEY_ACCESS_SENSITIVE |
| | 252 | | SC_PKCS15_PRKEY_ACCESS_ALWAYSSENSITIVE |
| | 253 | | SC_PKCS15_PRKEY_ACCESS_NEVEREXTRACTABLE |
| | 254 | | SC_PKCS15_PRKEY_ACCESS_LOCAL; |
| | 255 | |
| | 256 | if (path) |
| | 257 | info.path = *path; |
| | 258 | |
| | 259 | obj.flags = obj_flags; |
| | 260 | strlcpy(obj.label, label, sizeof(obj.label)); |
| | 261 | if (auth_id != NULL) |
| | 262 | obj.auth_id = *auth_id; |
| | 263 | |
| | 264 | return sc_pkcs15emu_add_rsa_prkey(p15card, &obj, &info); |
| | 265 | } |
| | 266 | |
| | 267 | static int itacns_add_pin(sc_pkcs15_card_t *p15card, |
| | 268 | char *label, |
| | 269 | int id, |
| | 270 | int auth_id, |
| | 271 | int reference, |
| | 272 | sc_path_t *path, |
| | 273 | int flags) |
| | 274 | { |
| | 275 | SC_FUNC_CALLED(p15card->card->ctx, 1); |
| | 276 | |
| | 277 | struct sc_pkcs15_pin_info pin_info; |
| | 278 | struct sc_pkcs15_object pin_obj; |
| | 279 | |
| | 280 | memset(&pin_info, 0, sizeof(pin_info)); |
| | 281 | pin_info.auth_id.len = 1; |
| | 282 | pin_info.auth_id.value[0] = id; |
| | 283 | pin_info.reference = reference; |
| | 284 | pin_info.flags = flags; |
| | 285 | pin_info.type = SC_PKCS15_PIN_TYPE_ASCII_NUMERIC; |
| | 286 | pin_info.min_length = 5; |
| | 287 | pin_info.stored_length = 8; |
| | 288 | pin_info.max_length = 8; |
| | 289 | pin_info.pad_char = 0xff; |
| | 290 | if(path) |
| | 291 | pin_info.path = *path; |
| | 292 | |
| | 293 | memset(&pin_obj, 0, sizeof(pin_obj)); |
| | 294 | strlcpy(pin_obj.label, label, sizeof(pin_obj.label)); |
| | 295 | pin_obj.flags = SC_PKCS15_CO_FLAG_PRIVATE | (auth_id ? SC_PKCS15_CO_FLAG_MODIFIABLE : 0); |
| | 296 | if (auth_id) { |
| | 297 | pin_obj.auth_id.len = 1; |
| | 298 | pin_obj.auth_id.value[0] = auth_id; |
| | 299 | } else |
| | 300 | pin_obj.auth_id.len = 0; |
| | 301 | |
| | 302 | return sc_pkcs15emu_add_pin_obj(p15card, &pin_obj, &pin_info); |
| | 303 | } |
| | 304 | |
| | 305 | static int hextoint(unsigned char *src, int len) |
| | 306 | { |
| | 307 | char hex[16]; |
| | 308 | if(len >= sizeof(hex)) |
| | 309 | return -1; |
| | 310 | strncpy(hex, src, len+1); |
| | 311 | hex[len] = '\0'; |
| | 312 | char *end; |
| | 313 | int res = strtol(hex, &end, 0x10); |
| | 314 | if(end != (char*)&hex[len]) |
| | 315 | return -1; |
| | 316 | return res; |
| | 317 | } |
| | 318 | |
| | 319 | static int get_name_from_EF_DatiPersonali(unsigned char *EFdata, |
| | 320 | char name[], int name_len) |
| | 321 | { |
| | 322 | /* |
| | 323 | * Bytes 0-5 contain the ASCII encoding of the TLV total size, in base 16. |
| | 324 | */ |
| | 325 | |
| | 326 | const unsigned int EF_personaldata_maxlen = 400; |
| | 327 | const tlv_length_size = 6; |
| | 328 | unsigned char *file = &EFdata[tlv_length_size]; |
| | 329 | int file_size = hextoint(EFdata, tlv_length_size); |
| | 330 | if(file_size < 0) |
| | 331 | return -1; |
| | 332 | |
| | 333 | /* |
| | 334 | * This shouldn't happen, but let us be protected against wrong |
| | 335 | * or malicious cards |
| | 336 | */ |
| | 337 | if(file_size > EF_personaldata_maxlen - tlv_length_size) |
| | 338 | file_size = EF_personaldata_maxlen - tlv_length_size; |
| | 339 | |
| | 340 | enum { |
| | 341 | f_issuer_code = 0, |
| | 342 | f_issuing_date, |
| | 343 | f_expiry_date, |
| | 344 | f_last_name, |
| | 345 | f_first_name, |
| | 346 | f_birth_date, |
| | 347 | f_sex, |
| | 348 | f_height, |
| | 349 | f_codice_fiscale, |
| | 350 | f_citizenship_code, |
| | 351 | f_birth_township_code, |
| | 352 | f_birth_country, |
| | 353 | f_birth_certificate, |
| | 354 | f_residence_township_code, |
| | 355 | f_residence_address, |
| | 356 | f_expat_notes |
| | 357 | }; |
| | 358 | |
| | 359 | struct { |
| | 360 | int len; |
| | 361 | char value[256]; |
| | 362 | } fields[f_first_name+1]; |
| | 363 | memset(fields, 0, sizeof(fields)); |
| | 364 | |
| | 365 | /* Read the fields up to f_first_name */ |
| | 366 | int i=0; /* offset inside the file */ |
| | 367 | int f; /* field number */ |
| | 368 | |
| | 369 | for(f=0; f<f_first_name+1; f++) { |
| | 370 | // Don't read beyond the allocated buffer |
| | 371 | if(i > file_size) |
| | 372 | return -1; |
| | 373 | |
| | 374 | int field_size = hextoint(&file[i], 2); |
| | 375 | if((field_size < 0) || (field_size+i > file_size)) |
| | 376 | return -1; |
| | 377 | |
| | 378 | i += 2; |
| | 379 | |
| | 380 | if(field_size >= sizeof(fields[f].value)) |
| | 381 | return -1; |
| | 382 | |
| | 383 | fields[f].len = field_size; |
| | 384 | strncpy(fields[f].value, &file[i], field_size); |
| | 385 | fields[f].value[field_size] = '\0'; |
| | 386 | i += field_size; |
| | 387 | } |
| | 388 | |
| | 389 | if (fields[f_first_name].len + fields[f_last_name].len + 1 >= name_len) |
| | 390 | return -1; |
| | 391 | |
| | 392 | snprintf(name, name_len, "%s %s", |
| | 393 | fields[f_first_name].value, fields[f_last_name].value); |
| | 394 | return 0; |
| | 395 | } |
| | 396 | |
| | 397 | static int itacns_add_data_files(sc_pkcs15_card_t *p15card) |
| | 398 | { |
| | 399 | const size_t list_size = sizeof(itacns_data_files)/sizeof(itacns_data_files[0]); |
| | 400 | int i; |
| | 401 | int r; |
| | 402 | for(i=0; i < list_size; i++) { |
| | 403 | if(itacns_data_files[i].cie_only && |
| | 404 | p15card->card->type != SC_CARD_TYPE_ITACNS_CIE_V2) |
| | 405 | continue; |
| | 406 | |
| | 407 | sc_path_t path; |
| | 408 | |
| | 409 | sc_format_path(itacns_data_files[i].path, &path); |
| | 410 | sc_pkcs15_data_info_t data; |
| | 411 | sc_pkcs15_object_t obj; |
| | 412 | |
| | 413 | memset(&data, 0, sizeof(data)); |
| | 414 | memset(&obj, 0, sizeof(obj)); |
| | 415 | strlcpy(data.app_label, itacns_data_files[i].label, |
| | 416 | sizeof(data.app_label)); |
| | 417 | strlcpy(obj.label, itacns_data_files[i].label, |
| | 418 | sizeof(obj.label)); |
| | 419 | data.path = path; |
| | 420 | r = sc_pkcs15emu_add_data_object(p15card, &obj, &data); |
| | 421 | } |
| | 422 | |
| | 423 | /* |
| | 424 | * If we got this far, we can read the Personal Data file and glean |
| | 425 | * the user's full name. Thus we can use it to put together a |
| | 426 | * user-friendlier card name. |
| | 427 | */ |
| | 428 | sc_pkcs15_data_t *p15_personaldata = NULL; |
| | 429 | |
| | 430 | sc_pkcs15_data_info_t dinfo; |
| | 431 | memset(&dinfo, 0, sizeof(dinfo)); |
| | 432 | strcpy(dinfo.app_label, "EF_DatiPersonali"); |
| | 433 | |
| | 434 | // Find EF_DatiPersonali |
| | 435 | |
| | 436 | struct sc_pkcs15_object *objs[32]; |
| | 437 | int rv = sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_DATA_OBJECT, |
| | 438 | objs, 32); |
| | 439 | if(rv < 0) { |
| | 440 | sc_debug(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, |
| | 441 | "Data enumeration failed"); |
| | 442 | return SC_SUCCESS; |
| | 443 | } |
| | 444 | |
| | 445 | struct sc_pkcs15_data_info *cinfo; |
| | 446 | for(i=0; i<32; i++) { |
| | 447 | cinfo = (struct sc_pkcs15_data_info *) objs[i]->data; |
| | 448 | if(!strcmp("EF_DatiPersonali", objs[i]->label)) |
| | 449 | break; |
| | 450 | } |
| | 451 | |
| | 452 | if(i>=32) { |
| | 453 | sc_debug(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, |
| | 454 | "Could not find EF_DatiPersonali: keeping generic card name"); |
| | 455 | return SC_SUCCESS; |
| | 456 | } |
| | 457 | |
| | 458 | rv = sc_pkcs15_read_data_object(p15card, cinfo, &p15_personaldata); |
| | 459 | if (rv) { |
| | 460 | sc_debug(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, |
| | 461 | "Could not read EF_DatiPersonali: keeping generic card name"); |
| | 462 | } |
| | 463 | |
| | 464 | |
| | 465 | char fullname[160]; |
| | 466 | |
| | 467 | if(get_name_from_EF_DatiPersonali(p15_personaldata->data, fullname, sizeof(fullname))) { |
| | 468 | sc_debug(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, |
| | 469 | "Could not parse EF_DatiPersonali: keeping generic card name"); |
| | 470 | return SC_SUCCESS; |
| | 471 | } |
| | 472 | set_string(&p15card->label, fullname); |
| | 473 | sc_pkcs15_free_data_object(p15_personaldata); |
| | 474 | return SC_SUCCESS; |
| | 475 | } |
| | 476 | |
| | 477 | static int itacns_add_keyset(sc_pkcs15_card_t *p15card, |
| | 478 | const char *label, int sec_env, sc_pkcs15_id_t *cert_id, |
| | 479 | const char *pubkey_path, const char *prkey_path, |
| | 480 | unsigned int pubkey_usage_flags, unsigned int prkey_usage_flags, |
| | 481 | u8 pin_ref, int needs_enc) |
| | 482 | { |
| | 483 | int r; |
| | 484 | sc_path_t path; |
| | 485 | |
| | 486 | /* This is hard-coded, for the time being. */ |
| | 487 | int modulus_length = 1024; |
| | 488 | |
| | 489 | /* Access flags; these depend on whether the private keys use PSO_ENC |
| | 490 | or PSO_CDS for signing. */ |
| | 491 | |
| | 492 | const int enc_algo_flags = SC_ALGORITHM_NEED_USAGE |
| | 493 | | SC_ALGORITHM_RSA_RAW |
| | 494 | | SC_ALGORITHM_RSA_HASH_NONE; |
| | 495 | const int cds_algo_flags = SC_ALGORITHM_NEED_USAGE |
| | 496 | | SC_ALGORITHM_RSA_PAD_PKCS1 |
| | 497 | | SC_ALGORITHM_RSA_HASH_NONE; |
| | 498 | |
| | 499 | /* Public key; not really needed */ |
| | 500 | /* FIXME: set usage according to the certificate. */ |
| | 501 | if (pubkey_path) { |
| | 502 | sc_format_path(pubkey_path, &path); |
| | 503 | r = itacns_add_pubkey(p15card, &path, cert_id, label, |
| | 504 | pubkey_usage_flags, sec_env, 0, &modulus_length); |
| | 505 | SC_TEST_RET(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, r, "Could not add public key"); |
| | 506 | } |
| | 507 | |
| | 508 | /* FIXME: usage should be inferred from the X.509 certificate, and not |
| | 509 | from whether the key needs Secure Messaging. */ |
| | 510 | sc_path_t *private_path = NULL; |
| | 511 | if (prkey_path) { |
| | 512 | sc_format_path(prkey_path, &path); |
| | 513 | private_path = &path; |
| | 514 | } |
| | 515 | r = itacns_add_prkey(p15card, cert_id, label, SC_PKCS15_TYPE_PRKEY_RSA, |
| | 516 | modulus_length, |
| | 517 | prkey_usage_flags, |
| | 518 | (needs_enc ? enc_algo_flags : cds_algo_flags), |
| | 519 | private_path, sec_env, cert_id, SC_PKCS15_CO_FLAG_PRIVATE); |
| | 520 | SC_TEST_RET(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, r, "Could not add private key"); |
| | 521 | |
| | 522 | /* PIN and PUK */ |
| | 523 | char pinlabel[16]; |
| | 524 | strncpy(pinlabel, "PIN ", sizeof(pinlabel)); |
| | 525 | strncat(pinlabel, label, sizeof(pinlabel)); |
| | 526 | /* We are making up ID 0x90+ to link the PIN and the PUK. */ |
| | 527 | int fake_puk_authid = 0x90 + pin_ref; |
| | 528 | int pin_flags = SC_PKCS15_PIN_FLAG_CASE_SENSITIVE |
| | 529 | | SC_PKCS15_PIN_FLAG_INITIALIZED; |
| | 530 | |
| | 531 | r = itacns_add_pin(p15card, pinlabel, sec_env, fake_puk_authid, pin_ref, |
| | 532 | private_path, pin_flags); |
| | 533 | SC_TEST_RET(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, r, "Could not add PIN"); |
| | 534 | |
| | 535 | strncpy(pinlabel, "PUK ", sizeof(pinlabel)); |
| | 536 | strncat(pinlabel, label, sizeof(pinlabel)); |
| | 537 | /* |
| | 538 | * Looking at pkcs15-tcos.c and pkcs15-framework.c, it seems that the |
| | 539 | * right thing to do here is to define a PUK as a SO PIN. Can anybody |
| | 540 | * comment on this? |
| | 541 | */ |
| | 542 | pin_flags |= SC_PKCS15_PIN_FLAG_UNBLOCKING_PIN |
| | 543 | | SC_PKCS15_PIN_FLAG_UNBLOCK_DISABLED; |
| | 544 | r = itacns_add_pin(p15card, pinlabel, fake_puk_authid, 0, pin_ref+1, |
| | 545 | private_path, pin_flags); |
| | 546 | SC_TEST_RET(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, r, "Could not add PUK"); |
| | 547 | |
| | 548 | return 0; |
| | 549 | } |
| | 550 | |
| | 551 | /* |
| | 552 | * itacns_check_and_add_keyset() checks for the existence and correctness |
| | 553 | * of an X.509 certificate. If it is all right, it adds the related keys; |
| | 554 | * otherwise it aborts. |
| | 555 | */ |
| | 556 | |
| | 557 | static int itacns_check_and_add_keyset(sc_pkcs15_card_t *p15card, |
| | 558 | const char *label, int sec_env, size_t cert_offset, |
| | 559 | const char *cert_path, const char *pubkey_path, const char *prkey_path, |
| | 560 | u8 pin_ref, int needs_enc, int *found_certificates) |
| | 561 | { |
| | 562 | int r; |
| | 563 | sc_path_t path; |
| | 564 | sc_pkcs15_id_t cert_id; |
| | 565 | |
| | 566 | cert_id.len = 1; |
| | 567 | cert_id.value[0] = sec_env; |
| | 568 | *found_certificates = 0; |
| | 569 | |
| | 570 | /* Usage flags */ |
| | 571 | const int auth_pubkey_usage_flags = SC_PKCS15_PRKEY_USAGE_ENCRYPT |
| | 572 | | SC_PKCS15_PRKEY_USAGE_DECRYPT |
| | 573 | | SC_PKCS15_PRKEY_USAGE_WRAP |
| | 574 | | SC_PKCS15_PRKEY_USAGE_VERIFY; |
| | 575 | const int auth_prkey_usage_flags = SC_PKCS15_PRKEY_USAGE_DECRYPT |
| | 576 | | SC_PKCS15_PRKEY_USAGE_UNWRAP |
| | 577 | | SC_PKCS15_PRKEY_USAGE_SIGN; |
| | 578 | const int sig_pubkey_usage_flags = SC_PKCS15_PRKEY_USAGE_VERIFY; |
| | 579 | const int sig_prkey_usage_flags = SC_PKCS15_PRKEY_USAGE_NONREPUDIATION; |
| | 580 | |
| | 581 | /* Certificate */ |
| | 582 | if (!cert_path) { |
| | 583 | sc_debug(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, |
| | 584 | "We cannot use keys without a matching certificate"); |
| | 585 | return SC_ERROR_NOT_SUPPORTED; |
| | 586 | } |
| | 587 | |
| | 588 | sc_format_path(cert_path, &path); |
| | 589 | r = sc_select_file(p15card->card, &path, NULL); |
| | 590 | if (r == SC_ERROR_FILE_NOT_FOUND) |
| | 591 | return 0; |
| | 592 | if (r != SC_SUCCESS) { |
| | 593 | sc_debug(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, |
| | 594 | "Could not find certificate for %s", label); |
| | 595 | return r; |
| | 596 | } |
| | 597 | |
| | 598 | /* |
| | 599 | * Infocamere 1204 (and others?) store a more complex structure. We |
| | 600 | * are going to read the first bytes to guess its length, and invoke |
| | 601 | * itacns_add_cert so that it only reads the certificate. |
| | 602 | */ |
| | 603 | if (cert_offset) { |
| | 604 | u8 certlen[3]; |
| | 605 | r = loadFile(p15card, &path, certlen, sizeof(certlen)); |
| | 606 | SC_TEST_RET(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, r, "Could not read certificate file"); |
| | 607 | path.index = cert_offset; |
| | 608 | path.count = (certlen[1] << 8) + certlen[2]; |
| | 609 | /* If those bytes are 00, then we are probably dealign with an |
| | 610 | * empty file. */ |
| | 611 | if (path.count == 0) |
| | 612 | return 0; |
| | 613 | } |
| | 614 | int ext_info_ok; |
| | 615 | int ku, xku; |
| | 616 | r = itacns_add_cert(p15card, SC_PKCS15_TYPE_CERT_X509, 0, |
| | 617 | &path, &cert_id, label, 0, &ext_info_ok, &ku, &xku); |
| | 618 | if (r == SC_ERROR_INVALID_ASN1_OBJECT) |
| | 619 | return 0; |
| | 620 | SC_TEST_RET(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, r, "Could not add certificate"); |
| | 621 | (*found_certificates)++; |
| | 622 | |
| | 623 | /* Set usage flags */ |
| | 624 | int pubkey_usage_flags = 0, prkey_usage_flags = 0; |
| | 625 | if(ext_info_ok) { |
| | 626 | #ifdef ENABLE_OPENSSL |
| | 627 | if (ku & KU_DIGITAL_SIGNATURE) { |
| | 628 | pubkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_VERIFY; |
| | 629 | prkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_SIGN; |
| | 630 | } |
| | 631 | if (ku & KU_NON_REPUDIATION) { |
| | 632 | pubkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_VERIFY; |
| | 633 | prkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_NONREPUDIATION; |
| | 634 | } |
| | 635 | if (ku & KU_KEY_ENCIPHERMENT || ku & KU_KEY_AGREEMENT |
| | 636 | || xku & XKU_SSL_CLIENT) { |
| | 637 | pubkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_WRAP; |
| | 638 | prkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_UNWRAP; |
| | 639 | } |
| | 640 | if (ku & KU_DATA_ENCIPHERMENT || xku & XKU_SMIME) { |
| | 641 | pubkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_ENCRYPT; |
| | 642 | prkey_usage_flags |= SC_PKCS15_PRKEY_USAGE_DECRYPT; |
| | 643 | } |
| | 644 | #else /* ENABLE_OPENSSL */ |
| | 645 | sc_debug(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, "Extended certificate " |
| | 646 | "info retrieved without OpenSSL. How is this possible?"); |
| | 647 | return SC_ERROR_INTERNAL; |
| | 648 | #endif /* ENABLE_OPENSSL */ |
| | 649 | } else { |
| | 650 | /* Certificate info not retrieved; fall back onto defaults */ |
| | 651 | pubkey_usage_flags = SC_PKCS15_PRKEY_USAGE_VERIFY | SC_PKCS15_PRKEY_USAGE_WRAP; |
| | 652 | prkey_usage_flags = SC_PKCS15_PRKEY_USAGE_SIGN | SC_PKCS15_PRKEY_USAGE_UNWRAP; |
| | 653 | } |
| | 654 | |
| | 655 | r = itacns_add_keyset(p15card, label, sec_env, &cert_id, |
| | 656 | pubkey_path, prkey_path, pubkey_usage_flags, prkey_usage_flags, |
| | 657 | pin_ref, needs_enc); |
| | 658 | SC_TEST_RET(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, r, "Could not add keys for" |
| | 659 | " this certificate"); |
| | 660 | |
| | 661 | return r; |
| | 662 | } |
| | 663 | |
| | 664 | /* Initialization. */ |
| | 665 | |
| | 666 | static int itacns_init(sc_pkcs15_card_t *p15card) |
| | 667 | { |
| | 668 | SC_FUNC_CALLED(p15card->card->ctx, 1); |
| | 669 | |
| | 670 | int r; |
| | 671 | sc_path_t path; |
| | 672 | int certificate_count = 0; |
| | 673 | int found_certs; |
| | 674 | |
| | 675 | set_string(&p15card->label, p15card->card->name); |
| | 676 | if(p15card->card->drv_data) { |
| | 677 | int mask_code, ic_code; |
| | 678 | char buffer[256]; |
| | 679 | itacns_drv_data_t *data = (itacns_drv_data_t*) p15card->card->drv_data; |
| | 680 | mask_code = data->mask_manufacturer_code; |
| | 681 | if (mask_code >= sizeof(itacns_mask_manufacturers)) |
| | 682 | mask_code = 0; |
| | 683 | ic_code = data->ic_manufacturer_code; |
| | 684 | if (ic_code >= sizeof(iso7816_ic_manufacturers)) |
| | 685 | ic_code = 0; |
| | 686 | snprintf(buffer, sizeof(buffer), "IC: %s; mask: %s", |
| | 687 | iso7816_ic_manufacturers[ic_code], |
| | 688 | itacns_mask_manufacturers[mask_code]); |
| | 689 | set_string(&p15card->manufacturer_id, buffer); |
| | 690 | p15card->version = (data->os_version_h << 8 | data->os_version_l); |
| | 691 | } |
| | 692 | |
| | 693 | /* Read and set serial */ |
| | 694 | u8 serial[17]; |
| | 695 | { |
| | 696 | int bytes; |
| | 697 | sc_format_path(path_serial, &path); |
| | 698 | bytes = loadFile(p15card, &path, serial, 16); |
| | 699 | if (bytes < 0) return bytes; |
| | 700 | if (bytes > 16) return -1; |
| | 701 | serial[bytes] = '\0'; |
| | 702 | set_string(&p15card->serial_number, (char*)serial); |
| | 703 | } |
| | 704 | |
| | 705 | /* Is the card a CIE v1? */ |
| | 706 | int card_is_cie_v1 = (p15card->card->type == SC_CARD_TYPE_ITACNS_CIE_V1) |
| | 707 | || (p15card->card->type == SC_CARD_TYPE_CARDOS_CIE_V1); |
| | 708 | int cns0_secenv = (card_is_cie_v1 ? 0x31 : 0x01); |
| | 709 | |
| | 710 | /* If it's a Siemens CIE v1 card, set algo flags accordingly. */ |
| | 711 | if (card_is_cie_v1) { |
| | 712 | int i; |
| | 713 | for (i = 0; i < p15card->card->algorithm_count; i++) { |
| | 714 | sc_algorithm_info_t *info = &p15card->card->algorithms[i]; |
| | 715 | |
| | 716 | if (info->algorithm != SC_ALGORITHM_RSA) |
| | 717 | continue; |
| | 718 | info->flags &= ~(SC_ALGORITHM_RSA_RAW |
| | 719 | | SC_ALGORITHM_RSA_HASH_NONE); |
| | 720 | info->flags |= (SC_ALGORITHM_RSA_PAD_PKCS1 |
| | 721 | | SC_ALGORITHM_RSA_HASHES); |
| | 722 | } |
| | 723 | } |
| | 724 | |
| | 725 | /* Data files */ |
| | 726 | r = itacns_add_data_files(p15card); |
| | 727 | SC_TEST_RET(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, r, "Could not add data files"); |
| | 728 | |
| | 729 | /*** Certificate and keys. ***/ |
| | 730 | /* Standard CNS */ |
| | 731 | r = itacns_check_and_add_keyset(p15card, "CNS0", cns0_secenv, |
| | 732 | 0, "3F0011001101", "3F003F01", NULL, |
| | 733 | 0x10, !card_is_cie_v1, &found_certs); |
| | 734 | SC_TEST_RET(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, r, "Could not add CNS0"); |
| | 735 | certificate_count += found_certs; |
| | 736 | |
| | 737 | /* Infocamere 1204 */ |
| | 738 | r = itacns_check_and_add_keyset(p15card, "CNS01", 0x21, |
| | 739 | 5, "3F002FFF8228", NULL, "3F002FFF0000", |
| | 740 | 0x10, 1, &found_certs); |
| | 741 | SC_TEST_RET(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, r, "Could not add CNS01"); |
| | 742 | certificate_count += found_certs; |
| | 743 | |
| | 744 | /* Digital signature */ |
| | 745 | r = itacns_check_and_add_keyset(p15card, "CNS1", 0x10, |
| | 746 | 0, "3F0014009010", "3F00140081108010", "3F0014008110", |
| | 747 | 0x1a, 0, &found_certs); |
| | 748 | SC_TEST_RET(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, r, "Could not add CNS1"); |
| | 749 | certificate_count += found_certs; |
| | 750 | |
| | 751 | /* Did we find anything? */ |
| | 752 | if (certificate_count == 0) |
| | 753 | sc_debug(p15card->card->ctx, SC_LOG_DEBUG_VERBOSE, "Warning: no certificates found!"); |
| | 754 | |
| | 755 | /* Back to Master File */ |
| | 756 | sc_format_path("3F00", &path); |
| | 757 | r = sc_select_file(p15card->card, &path, NULL); |
| | 758 | SC_TEST_RET(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, r, "Could not select master file again"); |
| | 759 | |
| | 760 | return r; |
| | 761 | } |
| | 762 | |
| | 763 | int sc_pkcs15emu_itacns_init_ex(sc_pkcs15_card_t *p15card, |
| | 764 | sc_pkcs15emu_opt_t *opts) |
| | 765 | { |
| | 766 | sc_card_t *card = p15card->card; |
| | 767 | SC_FUNC_CALLED(card->ctx, 1); |
| | 768 | |
| | 769 | /* Check card */ |
| | 770 | if (!(opts && opts->flags & SC_PKCS15EMU_FLAGS_NO_CHECK)) { |
| | 771 | if (! ( |
| | 772 | (card->type > SC_CARD_TYPE_ITACNS_BASE && |
| | 773 | card->type < SC_CARD_TYPE_ITACNS_BASE + 1000) |
| | 774 | || card->type == SC_CARD_TYPE_CARDOS_CIE_V1) |
| | 775 | ) |
| | 776 | return SC_ERROR_WRONG_CARD; |
| | 777 | } |
| | 778 | |
| | 779 | /* Init card */ |
| | 780 | return itacns_init(p15card); |
| | 781 | } |