| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | #ifndef _OPENSC_H |
|---|
| 28 | #define _OPENSC_H |
|---|
| 29 | |
|---|
| 30 | #include <stdio.h> |
|---|
| 31 | #ifdef HAVE_UNISTD_H |
|---|
| 32 | #include <unistd.h> |
|---|
| 33 | #endif |
|---|
| 34 | |
|---|
| 35 | #ifdef __cplusplus |
|---|
| 36 | extern "C" { |
|---|
| 37 | #endif |
|---|
| 38 | |
|---|
| 39 | #include <opensc/scconf.h> |
|---|
| 40 | #include <opensc/errors.h> |
|---|
| 41 | #include <opensc/types.h> |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | #define SC_APDU_CASE_NONE 0x00 |
|---|
| 45 | #define SC_APDU_CASE_1 0x01 |
|---|
| 46 | #define SC_APDU_CASE_2_SHORT 0x02 |
|---|
| 47 | #define SC_APDU_CASE_3_SHORT 0x03 |
|---|
| 48 | #define SC_APDU_CASE_4_SHORT 0x04 |
|---|
| 49 | #define SC_APDU_SHORT_MASK 0x0f |
|---|
| 50 | #define SC_APDU_EXT 0x10 |
|---|
| 51 | #define SC_APDU_CASE_2_EXT SC_APDU_CASE_2_SHORT | SC_APDU_EXT |
|---|
| 52 | #define SC_APDU_CASE_3_EXT SC_APDU_CASE_3_SHORT | SC_APDU_EXT |
|---|
| 53 | #define SC_APDU_CASE_4_EXT SC_APDU_CASE_4_SHORT | SC_APDU_EXT |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | #define SC_APDU_CASE_2 0x22 |
|---|
| 57 | #define SC_APDU_CASE_3 0x23 |
|---|
| 58 | #define SC_APDU_CASE_4 0x24 |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | #define SC_FILE_TYPE_DF 0x04 |
|---|
| 62 | #define SC_FILE_TYPE_INTERNAL_EF 0x03 |
|---|
| 63 | #define SC_FILE_TYPE_WORKING_EF 0x01 |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | #define SC_FILE_EF_UNKNOWN 0x00 |
|---|
| 67 | #define SC_FILE_EF_TRANSPARENT 0x01 |
|---|
| 68 | #define SC_FILE_EF_LINEAR_FIXED 0x02 |
|---|
| 69 | #define SC_FILE_EF_LINEAR_FIXED_TLV 0x03 |
|---|
| 70 | #define SC_FILE_EF_LINEAR_VARIABLE 0x04 |
|---|
| 71 | #define SC_FILE_EF_LINEAR_VARIABLE_TLV 0x05 |
|---|
| 72 | #define SC_FILE_EF_CYCLIC 0x06 |
|---|
| 73 | #define SC_FILE_EF_CYCLIC_TLV 0x07 |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | #define SC_FILE_STATUS_ACTIVATED 0x00 |
|---|
| 77 | #define SC_FILE_STATUS_INVALIDATED 0x01 |
|---|
| 78 | #define SC_FILE_STATUS_CREATION 0x02 |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | #define SC_AC_NONE 0x00000000 |
|---|
| 83 | #define SC_AC_CHV 0x00000001 |
|---|
| 84 | #define SC_AC_TERM 0x00000002 |
|---|
| 85 | #define SC_AC_PRO 0x00000004 |
|---|
| 86 | #define SC_AC_AUT 0x00000008 |
|---|
| 87 | |
|---|
| 88 | #define SC_AC_SYMBOLIC 0x00000010 |
|---|
| 89 | #define SC_AC_UNKNOWN 0xFFFFFFFE |
|---|
| 90 | #define SC_AC_NEVER 0xFFFFFFFF |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | #define SC_AC_OP_SELECT 0 |
|---|
| 94 | #define SC_AC_OP_LOCK 1 |
|---|
| 95 | #define SC_AC_OP_DELETE 2 |
|---|
| 96 | #define SC_AC_OP_CREATE 3 |
|---|
| 97 | #define SC_AC_OP_REHABILITATE 4 |
|---|
| 98 | #define SC_AC_OP_INVALIDATE 5 |
|---|
| 99 | #define SC_AC_OP_LIST_FILES 6 |
|---|
| 100 | #define SC_AC_OP_CRYPTO 7 |
|---|
| 101 | #define SC_AC_OP_DELETE_SELF 8 |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | #define SC_AC_OP_READ 0 |
|---|
| 107 | #define SC_AC_OP_UPDATE 1 |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | #define SC_AC_OP_ERASE SC_AC_OP_DELETE |
|---|
| 111 | #define SC_AC_OP_WRITE 3 |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | #define SC_MAX_READER_DRIVERS 6 |
|---|
| 116 | #define SC_MAX_READERS 16 |
|---|
| 117 | #define SC_MAX_CARD_DRIVERS 32 |
|---|
| 118 | #define SC_MAX_CARD_DRIVER_SNAME_SIZE 16 |
|---|
| 119 | #define SC_MAX_SLOTS 4 |
|---|
| 120 | #define SC_MAX_CARD_APPS 8 |
|---|
| 121 | #define SC_MAX_APDU_BUFFER_SIZE 258 |
|---|
| 122 | #define SC_MAX_EXT_APDU_BUFFER_SIZE 65538 |
|---|
| 123 | #define SC_MAX_PIN_SIZE 256 |
|---|
| 124 | #define SC_MAX_ATR_SIZE 33 |
|---|
| 125 | #define SC_MAX_AID_SIZE 16 |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | #define SC_DEFAULT_MAX_SEND_SIZE 255 |
|---|
| 130 | #define SC_DEFAULT_MAX_RECV_SIZE 256 |
|---|
| 131 | |
|---|
| 132 | #define SC_AC_KEY_REF_NONE 0xFFFFFFFF |
|---|
| 133 | |
|---|
| 134 | #define SC_SEC_OPERATION_DECIPHER 0x0001 |
|---|
| 135 | #define SC_SEC_OPERATION_SIGN 0x0002 |
|---|
| 136 | #define SC_SEC_OPERATION_AUTHENTICATE 0x0003 |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | #define SC_SEC_ENV_ALG_REF_PRESENT 0x0001 |
|---|
| 140 | #define SC_SEC_ENV_FILE_REF_PRESENT 0x0002 |
|---|
| 141 | #define SC_SEC_ENV_KEY_REF_PRESENT 0x0004 |
|---|
| 142 | |
|---|
| 143 | #define SC_SEC_ENV_KEY_REF_ASYMMETRIC 0x0008 |
|---|
| 144 | #define SC_SEC_ENV_ALG_PRESENT 0x0010 |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | #define SC_ALGORITHM_RSA 0 |
|---|
| 148 | #define SC_ALGORITHM_DSA 1 |
|---|
| 149 | #define SC_ALGORITHM_EC 2 |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | #define SC_ALGORITHM_DES 64 |
|---|
| 153 | #define SC_ALGORITHM_3DES 65 |
|---|
| 154 | #define SC_ALGORITHM_GOST 66 |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | #define SC_ALGORITHM_MD5 128 |
|---|
| 158 | #define SC_ALGORITHM_SHA1 129 |
|---|
| 159 | #define SC_ALGORITHM_GOSTHASH 130 |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | #define SC_ALGORITHM_PBKDF2 192 |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | #define SC_ALGORITHM_PBES2 256 |
|---|
| 166 | |
|---|
| 167 | #define SC_ALGORITHM_ONBOARD_KEY_GEN 0x80000000 |
|---|
| 168 | #define SC_ALGORITHM_NEED_USAGE 0x40000000 |
|---|
| 169 | #define SC_ALGORITHM_SPECIFIC_FLAGS 0x0000FFFF |
|---|
| 170 | |
|---|
| 171 | #define SC_ALGORITHM_RSA_RAW 0x00000001 |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | #define SC_ALGORITHM_RSA_PADS 0x0000000E |
|---|
| 175 | #define SC_ALGORITHM_RSA_PAD_NONE 0x00000000 |
|---|
| 176 | #define SC_ALGORITHM_RSA_PAD_PKCS1 0x00000002 |
|---|
| 177 | #define SC_ALGORITHM_RSA_PAD_ANSI 0x00000004 |
|---|
| 178 | #define SC_ALGORITHM_RSA_PAD_ISO9796 0x00000008 |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | #define SC_ALGORITHM_RSA_HASH_NONE 0x00000010 |
|---|
| 183 | #define SC_ALGORITHM_RSA_HASH_SHA1 0x00000020 |
|---|
| 184 | #define SC_ALGORITHM_RSA_HASH_MD5 0x00000040 |
|---|
| 185 | #define SC_ALGORITHM_RSA_HASH_MD5_SHA1 0x00000080 |
|---|
| 186 | #define SC_ALGORITHM_RSA_HASH_RIPEMD160 0x00000100 |
|---|
| 187 | #define SC_ALGORITHM_RSA_HASH_SHA256 0x00000200 |
|---|
| 188 | #define SC_ALGORITHM_RSA_HASH_SHA384 0x00000400 |
|---|
| 189 | #define SC_ALGORITHM_RSA_HASH_SHA512 0x00000800 |
|---|
| 190 | #define SC_ALGORITHM_RSA_HASH_SHA224 0x00001000 |
|---|
| 191 | #define SC_ALGORITHM_RSA_HASHES 0x00001FE0 |
|---|
| 192 | |
|---|
| 193 | #define SC_ALGORITHM_GOST_CRYPT_PZ 0x0 |
|---|
| 194 | #define SC_ALGORITHM_GOST_CRYPT_GAMM 0x1 |
|---|
| 195 | #define SC_ALGORITHM_GOST_CRYPT_GAMMOS 0x2 |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | #define SC_EVENT_CARD_INSERTED 0x0001 |
|---|
| 199 | #define SC_EVENT_CARD_REMOVED 0x0002 |
|---|
| 200 | |
|---|
| 201 | typedef struct sc_security_env { |
|---|
| 202 | unsigned long flags; |
|---|
| 203 | int operation; |
|---|
| 204 | unsigned int algorithm, algorithm_flags; |
|---|
| 205 | |
|---|
| 206 | unsigned int algorithm_ref; |
|---|
| 207 | struct sc_path file_ref; |
|---|
| 208 | u8 key_ref[8]; |
|---|
| 209 | size_t key_ref_len; |
|---|
| 210 | } sc_security_env_t; |
|---|
| 211 | |
|---|
| 212 | struct sc_algorithm_id { |
|---|
| 213 | unsigned int algorithm; |
|---|
| 214 | struct sc_object_id obj_id; |
|---|
| 215 | void *params; |
|---|
| 216 | }; |
|---|
| 217 | |
|---|
| 218 | struct sc_pbkdf2_params { |
|---|
| 219 | u8 salt[16]; |
|---|
| 220 | size_t salt_len; |
|---|
| 221 | int iterations; |
|---|
| 222 | size_t key_length; |
|---|
| 223 | struct sc_algorithm_id hash_alg; |
|---|
| 224 | }; |
|---|
| 225 | |
|---|
| 226 | struct sc_pbes2_params { |
|---|
| 227 | struct sc_algorithm_id derivation_alg; |
|---|
| 228 | struct sc_algorithm_id key_encr_alg; |
|---|
| 229 | }; |
|---|
| 230 | |
|---|
| 231 | typedef struct sc_algorithm_info { |
|---|
| 232 | unsigned int algorithm; |
|---|
| 233 | unsigned int key_length; |
|---|
| 234 | unsigned int flags; |
|---|
| 235 | |
|---|
| 236 | union { |
|---|
| 237 | struct sc_rsa_info { |
|---|
| 238 | unsigned long exponent; |
|---|
| 239 | } _rsa; |
|---|
| 240 | } u; |
|---|
| 241 | } sc_algorithm_info_t; |
|---|
| 242 | |
|---|
| 243 | typedef struct sc_app_info { |
|---|
| 244 | u8 aid[SC_MAX_AID_SIZE]; |
|---|
| 245 | size_t aid_len; |
|---|
| 246 | char *label; |
|---|
| 247 | struct sc_path path; |
|---|
| 248 | u8 *ddo; |
|---|
| 249 | size_t ddo_len; |
|---|
| 250 | |
|---|
| 251 | const char *desc; |
|---|
| 252 | int rec_nr; |
|---|
| 253 | } sc_app_info_t; |
|---|
| 254 | |
|---|
| 255 | struct sc_card_cache { |
|---|
| 256 | struct sc_path current_path; |
|---|
| 257 | }; |
|---|
| 258 | |
|---|
| 259 | #define SC_PROTO_T0 0x00000001 |
|---|
| 260 | #define SC_PROTO_T1 0x00000002 |
|---|
| 261 | #define SC_PROTO_RAW 0x00001000 |
|---|
| 262 | #define SC_PROTO_ANY 0xFFFFFFFF |
|---|
| 263 | |
|---|
| 264 | struct sc_reader_driver { |
|---|
| 265 | const char *name; |
|---|
| 266 | const char *short_name; |
|---|
| 267 | struct sc_reader_operations *ops; |
|---|
| 268 | |
|---|
| 269 | size_t max_send_size, max_recv_size; |
|---|
| 270 | void *dll; |
|---|
| 271 | }; |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | #define SC_SLOT_CARD_PRESENT 0x00000001 |
|---|
| 275 | #define SC_SLOT_CARD_CHANGED 0x00000002 |
|---|
| 276 | |
|---|
| 277 | #define SC_SLOT_CAP_DISPLAY 0x00000001 |
|---|
| 278 | #define SC_SLOT_CAP_PIN_PAD 0x00000002 |
|---|
| 279 | |
|---|
| 280 | typedef struct sc_slot_info { |
|---|
| 281 | int id; |
|---|
| 282 | unsigned long flags, capabilities; |
|---|
| 283 | unsigned int supported_protocols, active_protocol; |
|---|
| 284 | u8 atr[SC_MAX_ATR_SIZE]; |
|---|
| 285 | size_t atr_len; |
|---|
| 286 | |
|---|
| 287 | struct _atr_info { |
|---|
| 288 | u8 *hist_bytes; |
|---|
| 289 | size_t hist_bytes_len; |
|---|
| 290 | int Fi, f, Di, N; |
|---|
| 291 | u8 FI, DI; |
|---|
| 292 | } atr_info; |
|---|
| 293 | |
|---|
| 294 | void *drv_data; |
|---|
| 295 | } sc_slot_info_t; |
|---|
| 296 | |
|---|
| 297 | struct sc_event_listener { |
|---|
| 298 | unsigned int event_mask; |
|---|
| 299 | void (*func)(void *, const struct sc_slot_info *, unsigned int event); |
|---|
| 300 | }; |
|---|
| 301 | |
|---|
| 302 | typedef struct sc_reader { |
|---|
| 303 | struct sc_context *ctx; |
|---|
| 304 | const struct sc_reader_driver *driver; |
|---|
| 305 | const struct sc_reader_operations *ops; |
|---|
| 306 | void *drv_data; |
|---|
| 307 | char *name; |
|---|
| 308 | |
|---|
| 309 | struct sc_slot_info slot[SC_MAX_SLOTS]; |
|---|
| 310 | int slot_count; |
|---|
| 311 | } sc_reader_t; |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | #define SC_PIN_CMD_VERIFY 0 |
|---|
| 318 | #define SC_PIN_CMD_CHANGE 1 |
|---|
| 319 | #define SC_PIN_CMD_UNBLOCK 2 |
|---|
| 320 | |
|---|
| 321 | #define SC_PIN_CMD_USE_PINPAD 0x0001 |
|---|
| 322 | #define SC_PIN_CMD_NEED_PADDING 0x0002 |
|---|
| 323 | |
|---|
| 324 | #define SC_PIN_ENCODING_ASCII 0 |
|---|
| 325 | #define SC_PIN_ENCODING_BCD 1 |
|---|
| 326 | #define SC_PIN_ENCODING_GLP 2 |
|---|
| 327 | |
|---|
| 328 | struct sc_pin_cmd_pin { |
|---|
| 329 | const char *prompt; |
|---|
| 330 | |
|---|
| 331 | const u8 *data; |
|---|
| 332 | int len; |
|---|
| 333 | |
|---|
| 334 | size_t min_length; |
|---|
| 335 | size_t max_length; |
|---|
| 336 | unsigned int encoding; |
|---|
| 337 | size_t pad_length; |
|---|
| 338 | u8 pad_char; |
|---|
| 339 | size_t offset; |
|---|
| 340 | size_t length_offset; |
|---|
| 341 | }; |
|---|
| 342 | |
|---|
| 343 | struct sc_pin_cmd_data { |
|---|
| 344 | unsigned int cmd; |
|---|
| 345 | unsigned int flags; |
|---|
| 346 | |
|---|
| 347 | unsigned int pin_type; |
|---|
| 348 | int pin_reference; |
|---|
| 349 | |
|---|
| 350 | struct sc_pin_cmd_pin pin1, pin2; |
|---|
| 351 | |
|---|
| 352 | struct sc_apdu *apdu; |
|---|
| 353 | }; |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | #define SC_MAX_SERIALNR 32 |
|---|
| 357 | |
|---|
| 358 | typedef struct sc_serial_number { |
|---|
| 359 | u8 value[SC_MAX_SERIALNR]; |
|---|
| 360 | size_t len; |
|---|
| 361 | } sc_serial_number_t; |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | #define SC_DISCONNECT 0 |
|---|
| 365 | #define SC_DISCONNECT_AND_RESET 1 |
|---|
| 366 | #define SC_DISCONNECT_AND_UNPOWER 2 |
|---|
| 367 | #define SC_DISCONNECT_AND_EJECT 3 |
|---|
| 368 | |
|---|
| 369 | struct sc_reader_operations { |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | int (*init)(struct sc_context *ctx, void **priv_data); |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | int (*finish)(struct sc_context *ctx, void *priv_data); |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | int (*detect_readers)(struct sc_context *ctx, void *priv_data); |
|---|
| 379 | |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | int (*release)(struct sc_reader *reader); |
|---|
| 383 | |
|---|
| 384 | int (*detect_card_presence)(struct sc_reader *reader, |
|---|
| 385 | struct sc_slot_info *slot); |
|---|
| 386 | int (*connect)(struct sc_reader *reader, struct sc_slot_info *slot); |
|---|
| 387 | int (*disconnect)(struct sc_reader *reader, struct sc_slot_info *slot); |
|---|
| 388 | int (*transmit)(struct sc_reader *reader, struct sc_slot_info *slot, |
|---|
| 389 | sc_apdu_t *apdu); |
|---|
| 390 | int (*lock)(struct sc_reader *reader, struct sc_slot_info *slot); |
|---|
| 391 | int (*unlock)(struct sc_reader *reader, struct sc_slot_info *slot); |
|---|
| 392 | int (*set_protocol)(struct sc_reader *reader, struct sc_slot_info *slot, |
|---|
| 393 | unsigned int proto); |
|---|
| 394 | |
|---|
| 395 | int (*display_message)(struct sc_reader *, struct sc_slot_info *, |
|---|
| 396 | const char *); |
|---|
| 397 | int (*perform_verify)(struct sc_reader *, struct sc_slot_info *, |
|---|
| 398 | struct sc_pin_cmd_data *); |
|---|
| 399 | |
|---|
| 400 | |
|---|
| 401 | int (*wait_for_event)(struct sc_reader **readers, |
|---|
| 402 | struct sc_slot_info **slots, |
|---|
| 403 | size_t nslots, |
|---|
| 404 | unsigned int event_mask, |
|---|
| 405 | int *reader_index, |
|---|
| 406 | unsigned int *event, |
|---|
| 407 | int timeout); |
|---|
| 408 | int (*reset)(struct sc_reader *, struct sc_slot_info *); |
|---|
| 409 | }; |
|---|
| 410 | |
|---|
| 411 | |
|---|
| 412 | |
|---|
| 413 | |
|---|
| 414 | |
|---|
| 415 | |
|---|
| 416 | |
|---|
| 417 | |
|---|
| 418 | |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | |
|---|
| 422 | |
|---|
| 423 | |
|---|
| 424 | #define SC_CARD_FLAG_VENDOR_MASK 0xFFFF0000 |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | #define SC_CARD_FLAG_ONBOARD_KEY_GEN 0x00000001 |
|---|
| 428 | |
|---|
| 429 | #define SC_CARD_FLAG_RNG 0x00000002 |
|---|
| 430 | |
|---|
| 431 | |
|---|
| 432 | |
|---|
| 433 | |
|---|
| 434 | |
|---|
| 435 | |
|---|
| 436 | |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | #define SC_CARD_CAP_APDU_EXT 0x00000001 |
|---|
| 440 | |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | #define SC_CARD_CAP_EMV 0x00000002 |
|---|
| 444 | |
|---|
| 445 | |
|---|
| 446 | #define SC_CARD_CAP_RNG 0x00000004 |
|---|
| 447 | |
|---|
| 448 | |
|---|
| 449 | #define SC_CARD_CAP_NO_FCI 0x00000008 |
|---|
| 450 | |
|---|
| 451 | |
|---|
| 452 | |
|---|
| 453 | #define SC_CARD_CAP_USE_FCI_AC 0x00000010 |
|---|
| 454 | |
|---|
| 455 | |
|---|
| 456 | #define SC_CARD_CAP_RSA_2048 0x00000020 |
|---|
| 457 | |
|---|
| 458 | |
|---|
| 459 | #define SC_CARD_CAP_ONLY_RAW_HASH 0x00000040 |
|---|
| 460 | #define SC_CARD_CAP_ONLY_RAW_HASH_STRIPPED 0x00000080 |
|---|
| 461 | |
|---|
| 462 | typedef struct sc_card { |
|---|
| 463 | struct sc_context *ctx; |
|---|
| 464 | struct sc_reader *reader; |
|---|
| 465 | struct sc_slot_info *slot; |
|---|
| 466 | |
|---|
| 467 | int type; |
|---|
| 468 | unsigned long caps, flags; |
|---|
| 469 | unsigned int wait_resend_apdu; |
|---|
| 470 | int cla; |
|---|
| 471 | u8 atr[SC_MAX_ATR_SIZE]; |
|---|
| 472 | size_t atr_len; |
|---|
| 473 | size_t max_send_size; |
|---|
| 474 | size_t max_recv_size; |
|---|
| 475 | |
|---|
| 476 | struct sc_app_info *app[SC_MAX_CARD_APPS]; |
|---|
| 477 | int app_count; |
|---|
| 478 | struct sc_file *ef_dir; |
|---|
| 479 | |
|---|
| 480 | struct sc_algorithm_info *algorithms; |
|---|
| 481 | int algorithm_count; |
|---|
| 482 | |
|---|
| 483 | int lock_count; |
|---|
| 484 | |
|---|
| 485 | struct sc_card_driver *driver; |
|---|
| 486 | struct sc_card_operations *ops; |
|---|
| 487 | const char *name; |
|---|
| 488 | void *drv_data; |
|---|
| 489 | int max_pin_len; |
|---|
| 490 | |
|---|
| 491 | struct sc_card_cache cache; |
|---|
| 492 | int cache_valid; |
|---|
| 493 | |
|---|
| 494 | sc_serial_number_t serialnr; |
|---|
| 495 | |
|---|
| 496 | void *mutex; |
|---|
| 497 | |
|---|
| 498 | unsigned int magic; |
|---|
| 499 | } sc_card_t; |
|---|
| 500 | |
|---|
| 501 | struct sc_card_operations { |
|---|
| 502 | |
|---|
| 503 | |
|---|
| 504 | |
|---|
| 505 | |
|---|
| 506 | int (*match_card)(struct sc_card *card); |
|---|
| 507 | |
|---|
| 508 | |
|---|
| 509 | |
|---|
| 510 | |
|---|
| 511 | int (*init)(struct sc_card *card); |
|---|
| 512 | |
|---|
| 513 | |
|---|
| 514 | int (*finish)(struct sc_card *card); |
|---|
| 515 | |
|---|
| 516 | |
|---|
| 517 | |
|---|
| 518 | int (*read_binary)(struct sc_card *card, unsigned int idx, |
|---|
| 519 | u8 * buf, size_t count, unsigned long flags); |
|---|
| 520 | int (*write_binary)(struct sc_card *card, unsigned int idx, |
|---|
| 521 | const u8 * buf, size_t count, unsigned long flags); |
|---|
| 522 | int (*update_binary)(struct sc_card *card, unsigned int idx, |
|---|
| 523 | const u8 * buf, size_t count, unsigned long flags); |
|---|
| 524 | int (*erase_binary)(struct sc_card *card, unsigned int idx, |
|---|
| 525 | size_t count, unsigned long flags); |
|---|
| 526 | |
|---|
| 527 | int (*read_record)(struct sc_card *card, unsigned int rec_nr, |
|---|
| 528 | u8 * buf, size_t count, unsigned long flags); |
|---|
| 529 | int (*write_record)(struct sc_card *card, unsigned int rec_nr, |
|---|
| 530 | const u8 * buf, size_t count, unsigned long flags); |
|---|
| 531 | int (*append_record)(struct sc_card *card, const u8 * buf, |
|---|
| 532 | size_t count, unsigned long flags); |
|---|
| 533 | int (*update_record)(struct sc_card *card, unsigned int rec_nr, |
|---|
| 534 | const u8 * buf, size_t count, unsigned long flags); |
|---|
| 535 | |
|---|
| 536 | |
|---|
| 537 | |
|---|
| 538 | |
|---|
| 539 | int (*select_file)(struct sc_card *card, const struct sc_path *path, |
|---|
| 540 | struct sc_file **file_out); |
|---|
| 541 | int (*get_response)(struct sc_card *card, size_t *count, u8 *buf); |
|---|
| 542 | int (*get_challenge)(struct sc_card *card, u8 * buf, size_t count); |
|---|
| 543 | |
|---|
| 544 | |
|---|
| 545 | |
|---|
| 546 | |
|---|
| 547 | |
|---|
| 548 | |
|---|
| 549 | |
|---|
| 550 | |
|---|
| 551 | |
|---|
| 552 | int (*verify)(struct sc_card *card, unsigned int type, |
|---|
| 553 | int ref_qualifier, const u8 *data, size_t data_len, |
|---|
| 554 | int *tries_left); |
|---|
| 555 | |
|---|
| 556 | |
|---|
| 557 | int (*logout)(struct sc_card *card); |
|---|
| 558 | |
|---|
| 559 | |
|---|
| 560 | |
|---|
| 561 | |
|---|
| 562 | int (*restore_security_env)(struct sc_card *card, int se_num); |
|---|
| 563 | |
|---|
| 564 | |
|---|
| 565 | |
|---|
| 566 | |
|---|
| 567 | int (*set_security_env)(struct sc_card *card, |
|---|
| 568 | const struct sc_security_env *env, int se_num); |
|---|
| 569 | |
|---|
| 570 | |
|---|
| 571 | |
|---|
| 572 | int (*decipher)(struct sc_card *card, const u8 * crgram, |
|---|
| 573 | size_t crgram_len, u8 * out, size_t outlen); |
|---|
| 574 | |
|---|
| 575 | |
|---|
| 576 | |
|---|
| 577 | int (*compute_signature)(struct sc_card *card, const u8 * data, |
|---|
| 578 | size_t data_len, u8 * out, size_t outlen); |
|---|
| 579 | int (*change_reference_data)(struct sc_card *card, unsigned int type, |
|---|
| 580 | int ref_qualifier, |
|---|
| 581 | const u8 *old, size_t oldlen, |
|---|
| 582 | const u8 *newref, size_t newlen, |
|---|
| 583 | int *tries_left); |
|---|
| 584 | int (*reset_retry_counter)(struct sc_card *card, unsigned int type, |
|---|
| 585 | int ref_qualifier, |
|---|
| 586 | const u8 *puk, size_t puklen, |
|---|
| 587 | const u8 *newref, size_t newlen); |
|---|
| 588 | |
|---|
| 589 | |
|---|
| 590 | |
|---|
| 591 | int (*create_file)(struct sc_card *card, struct sc_file *file); |
|---|
| 592 | int (*delete_file)(struct sc_card *card, const struct sc_path *path); |
|---|
| 593 | |
|---|
| 594 | |
|---|
| 595 | |
|---|
| 596 | int (*list_files)(struct sc_card *card, u8 *buf, size_t buflen); |
|---|
| 597 | |
|---|
| 598 | int (*check_sw)(struct sc_card *card,unsigned int sw1,unsigned int sw2); |
|---|
| 599 | int (*card_ctl)(struct sc_card *card, unsigned long request, |
|---|
| 600 | void *data); |
|---|
| 601 | int (*process_fci)(struct sc_card *card, struct sc_file *file, |
|---|
| 602 | const u8 *buf, size_t buflen); |
|---|
| 603 | int (*construct_fci)(struct sc_card *card, const struct sc_file *file, |
|---|
| 604 | u8 *out, size_t *outlen); |
|---|
| 605 | |
|---|
| 606 | |
|---|
| 607 | |
|---|
| 608 | |
|---|
| 609 | int (*pin_cmd)(struct sc_card *, struct sc_pin_cmd_data *, |
|---|
| 610 | int *tries_left); |
|---|
| 611 | |
|---|
| 612 | int (*get_data)(sc_card_t *, unsigned int, u8 *, size_t); |
|---|
| 613 | int (*put_data)(sc_card_t *, unsigned int, const u8 *, size_t); |
|---|
| 614 | |
|---|
| 615 | int (*delete_record)(sc_card_t *card, unsigned int rec_nr); |
|---|
| 616 | }; |
|---|
| 617 | |
|---|
| 618 | typedef struct sc_card_driver { |
|---|
| 619 | const char *name; |
|---|
| 620 | const char *short_name; |
|---|
| 621 | struct sc_card_operations *ops; |
|---|
| 622 | struct sc_atr_table *atr_map; |
|---|
| 623 | unsigned int natrs; |
|---|
| 624 | void *dll; |
|---|
| 625 | } sc_card_driver_t; |
|---|
| 626 | |
|---|
| 627 | |
|---|
| 628 | |
|---|
| 629 | |
|---|
| 630 | |
|---|
| 631 | |
|---|
| 632 | typedef struct { |
|---|
| 633 | |
|---|
| 634 | unsigned int ver; |
|---|
| 635 | |
|---|
| 636 | int (*create_mutex)(void **); |
|---|
| 637 | |
|---|
| 638 | int (*lock_mutex)(void *); |
|---|
| 639 | |
|---|
| 640 | int (*unlock_mutex)(void *); |
|---|
| 641 | |
|---|
| 642 | int (*destroy_mutex)(void *); |
|---|
| 643 | |
|---|
| 644 | unsigned long (*thread_id)(void); |
|---|
| 645 | } sc_thread_context_t; |
|---|
| 646 | |
|---|
| 647 | typedef struct sc_context { |
|---|
| 648 | scconf_context *conf; |
|---|
| 649 | scconf_block *conf_blocks[3]; |
|---|
| 650 | char *app_name; |
|---|
| 651 | int debug; |
|---|
| 652 | |
|---|
| 653 | int suppress_errors; |
|---|
| 654 | FILE *debug_file, *error_file; |
|---|
| 655 | char *preferred_language; |
|---|
| 656 | |
|---|
| 657 | const struct sc_reader_driver *reader_drivers[SC_MAX_READER_DRIVERS]; |
|---|
| 658 | void *reader_drv_data[SC_MAX_READER_DRIVERS]; |
|---|
| 659 | |
|---|
| 660 | struct sc_reader *reader[SC_MAX_READERS]; |
|---|
| 661 | int reader_count; |
|---|
| 662 | |
|---|
| 663 | struct sc_card_driver *card_drivers[SC_MAX_CARD_DRIVERS]; |
|---|
| 664 | struct sc_card_driver *forced_driver; |
|---|
| 665 | |
|---|
| 666 | sc_thread_context_t *thread_ctx; |
|---|
| 667 | void *mutex; |
|---|
| 668 | |
|---|
| 669 | unsigned int magic; |
|---|
| 670 | } sc_context_t; |
|---|
| 671 | |
|---|
| 672 | |
|---|
| 673 | |
|---|
| 674 | |
|---|
| 675 | |
|---|
| 676 | |
|---|
| 677 | |
|---|
| 678 | |
|---|
| 679 | int sc_transmit_apdu(sc_card_t *card, sc_apdu_t *apdu); |
|---|
| 680 | |
|---|
| 681 | void sc_format_apdu(sc_card_t *card, sc_apdu_t *apdu, int cse, int ins, |
|---|
| 682 | int p1, int p2); |
|---|
| 683 | |
|---|
| 684 | |
|---|
| 685 | |
|---|
| 686 | |
|---|
| 687 | |
|---|
| 688 | |
|---|
| 689 | |
|---|
| 690 | |
|---|
| 691 | |
|---|
| 692 | |
|---|
| 693 | |
|---|
| 694 | |
|---|
| 695 | |
|---|
| 696 | int sc_establish_context(sc_context_t **ctx, const char *app_name); |
|---|
| 697 | |
|---|
| 698 | |
|---|
| 699 | |
|---|
| 700 | |
|---|
| 701 | |
|---|
| 702 | |
|---|
| 703 | typedef struct { |
|---|
| 704 | |
|---|
| 705 | unsigned int ver; |
|---|
| 706 | |
|---|
| 707 | |
|---|
| 708 | |
|---|
| 709 | const char *app_name; |
|---|
| 710 | |
|---|
| 711 | unsigned long flags; |
|---|
| 712 | |
|---|
| 713 | sc_thread_context_t *thread_ctx; |
|---|
| 714 | } sc_context_param_t; |
|---|
| 715 | |
|---|
| 716 | |
|---|
| 717 | |
|---|
| 718 | |
|---|
| 719 | |
|---|
| 720 | |
|---|
| 721 | |
|---|
| 722 | |
|---|
| 723 | |
|---|
| 724 | int sc_context_create(sc_context_t **ctx, const sc_context_param_t *parm); |
|---|
| 725 | |
|---|
| 726 | |
|---|
| 727 | |
|---|
| 728 | |
|---|
| 729 | |
|---|
| 730 | int sc_release_context(sc_context_t *ctx); |
|---|
| 731 | |
|---|
| 732 | |
|---|
| 733 | |
|---|
| 734 | |
|---|
| 735 | |
|---|
| 736 | |
|---|
| 737 | int sc_ctx_detect_readers(sc_context_t *ctx); |
|---|
| 738 | |
|---|
| 739 | |
|---|
| 740 | |
|---|
| 741 | |
|---|
| 742 | |
|---|
| 743 | |
|---|
| 744 | |
|---|
| 745 | |
|---|
| 746 | sc_reader_t *sc_ctx_get_reader(sc_context_t *ctx, unsigned int i); |
|---|
| 747 | |
|---|
| 748 | |
|---|
| 749 | |
|---|
| 750 | |
|---|
| 751 | |
|---|
| 752 | |
|---|
| 753 | unsigned int sc_ctx_get_reader_count(sc_context_t *ctx); |
|---|
| 754 | |
|---|
| 755 | |
|---|
| 756 | |
|---|
| 757 | |
|---|
| 758 | |
|---|
| 759 | void sc_ctx_suppress_errors_on(sc_context_t *ctx); |
|---|
| 760 | |
|---|
| 761 | |
|---|
| 762 | |
|---|
| 763 | |
|---|
| 764 | |
|---|
| 765 | void sc_ctx_suppress_errors_off(sc_context_t *ctx); |
|---|
| 766 | |
|---|
| 767 | |
|---|
| 768 | |
|---|
| 769 | |
|---|
| 770 | |
|---|
| 771 | |
|---|
| 772 | int sc_set_card_driver(sc_context_t *ctx, const char *short_name); |
|---|
| 773 | |
|---|
| 774 | |
|---|
| 775 | |
|---|
| 776 | |
|---|
| 777 | |
|---|
| 778 | |
|---|
| 779 | int sc_connect_card(sc_reader_t *reader, int slot_id, sc_card_t **card); |
|---|
| 780 | |
|---|
| 781 | |
|---|
| 782 | |
|---|
| 783 | |
|---|
| 784 | |
|---|
| 785 | |
|---|
| 786 | |
|---|
| 787 | |
|---|
| 788 | int sc_disconnect_card(sc_card_t *card, int flag); |
|---|
| 789 | |
|---|
| 790 | |
|---|
| 791 | |
|---|
| 792 | |
|---|
| 793 | |
|---|
| 794 | int sc_card_valid(const sc_card_t *card); |
|---|
| 795 | |
|---|
| 796 | |
|---|
| 797 | |
|---|
| 798 | |
|---|
| 799 | |
|---|
| 800 | |
|---|
| 801 | |
|---|
| 802 | |
|---|
| 803 | |
|---|
| 804 | |
|---|
| 805 | |
|---|
| 806 | |
|---|
| 807 | int sc_detect_card_presence(sc_reader_t *reader, int slot_id); |
|---|
| 808 | |
|---|
| 809 | |
|---|
| 810 | |
|---|
| 811 | |
|---|
| 812 | |
|---|
| 813 | |
|---|
| 814 | |
|---|
| 815 | |
|---|
| 816 | |
|---|
| 817 | |
|---|
| 818 | |
|---|
| 819 | |
|---|
| 820 | |
|---|
| 821 | |
|---|
| 822 | |
|---|
| 823 | |
|---|
| 824 | |
|---|
| 825 | |
|---|
| 826 | |
|---|
| 827 | int sc_wait_for_event(sc_reader_t **readers, int *slots, size_t nslots, |
|---|
| 828 | unsigned int event_mask, |
|---|
| 829 | int *reader, unsigned int *event, int timeout); |
|---|
| 830 | |
|---|
| 831 | |
|---|
| 832 | |
|---|
| 833 | |
|---|
| 834 | |
|---|
| 835 | |
|---|
| 836 | |
|---|
| 837 | int sc_reset(sc_card_t *card); |
|---|
|