2001-12-29 02:07:32 +00:00
|
|
|
|
/*
|
2001-11-24 13:32:52 +00:00
|
|
|
|
* opensc.h: OpenSC library header file
|
2001-11-01 15:43:20 +00:00
|
|
|
|
*
|
2001-11-06 18:34:19 +00:00
|
|
|
|
* Copyright (C) 2001 Juha Yrj<EFBFBD>l<EFBFBD> <juha.yrjola@iki.fi>
|
2001-11-01 15:43:20 +00:00
|
|
|
|
*
|
2001-11-06 18:34:19 +00:00
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
2001-11-01 15:43:20 +00:00
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2001-11-06 18:34:19 +00:00
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
2001-11-01 15:43:20 +00:00
|
|
|
|
*
|
2001-11-06 18:34:19 +00:00
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2001-11-01 15:43:20 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2002-01-10 23:02:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* @file opensc.h
|
|
|
|
|
* @brief OpenSC library core header file
|
|
|
|
|
*/
|
|
|
|
|
|
2001-12-13 21:19:11 +00:00
|
|
|
|
#ifndef _OPENSC_H
|
|
|
|
|
#define _OPENSC_H
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
2001-11-20 22:21:58 +00:00
|
|
|
|
#include <pthread.h>
|
2001-11-01 15:43:20 +00:00
|
|
|
|
#include <winscard.h>
|
2001-12-25 20:45:48 +00:00
|
|
|
|
#ifndef NDEBUG
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#endif
|
2001-11-22 15:40:51 +00:00
|
|
|
|
#include <stdio.h>
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
2001-12-08 15:27:40 +00:00
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
2001-12-30 21:17:34 +00:00
|
|
|
|
#ifndef __GNUC__
|
|
|
|
|
#undef inline
|
|
|
|
|
#define inline
|
|
|
|
|
#endif
|
|
|
|
|
|
2002-01-10 23:02:48 +00:00
|
|
|
|
#define SC_SUCCESS 0
|
|
|
|
|
|
2001-11-01 15:43:20 +00:00
|
|
|
|
#define SC_ERROR_MIN -1000
|
|
|
|
|
#define SC_ERROR_UNKNOWN -1000
|
|
|
|
|
#define SC_ERROR_CMD_TOO_SHORT -1001
|
|
|
|
|
#define SC_ERROR_CMD_TOO_LONG -1002
|
|
|
|
|
#define SC_ERROR_NOT_SUPPORTED -1003
|
|
|
|
|
#define SC_ERROR_TRANSMIT_FAILED -1004
|
|
|
|
|
#define SC_ERROR_FILE_NOT_FOUND -1005
|
|
|
|
|
#define SC_ERROR_INVALID_ARGUMENTS -1006
|
2002-01-10 12:33:56 +00:00
|
|
|
|
#define SC_ERROR_PKCS15_APP_NOT_FOUND -1007
|
2001-11-01 15:43:20 +00:00
|
|
|
|
#define SC_ERROR_REQUIRED_PARAMETER_NOT_FOUND -1008
|
|
|
|
|
#define SC_ERROR_OUT_OF_MEMORY -1009
|
|
|
|
|
#define SC_ERROR_NO_READERS_FOUND -1010
|
|
|
|
|
#define SC_ERROR_OBJECT_NOT_VALID -1011
|
2001-12-21 23:34:47 +00:00
|
|
|
|
#define SC_ERROR_ILLEGAL_RESPONSE -1012
|
2001-11-01 15:43:20 +00:00
|
|
|
|
#define SC_ERROR_PIN_CODE_INCORRECT -1013
|
|
|
|
|
#define SC_ERROR_SECURITY_STATUS_NOT_SATISFIED -1014
|
|
|
|
|
#define SC_ERROR_CONNECTING_TO_RES_MGR -1015
|
|
|
|
|
#define SC_ERROR_INVALID_ASN1_OBJECT -1016
|
2001-11-04 13:57:04 +00:00
|
|
|
|
#define SC_ERROR_BUFFER_TOO_SMALL -1017
|
2001-11-07 13:45:41 +00:00
|
|
|
|
#define SC_ERROR_CARD_NOT_PRESENT -1018
|
|
|
|
|
#define SC_ERROR_RESOURCE_MANAGER -1019
|
2001-11-17 15:48:10 +00:00
|
|
|
|
#define SC_ERROR_CARD_REMOVED -1020
|
|
|
|
|
#define SC_ERROR_INVALID_PIN_LENGTH -1021
|
2001-11-20 22:21:58 +00:00
|
|
|
|
#define SC_ERROR_UNKNOWN_SMARTCARD -1022
|
|
|
|
|
#define SC_ERROR_UNKNOWN_REPLY -1023
|
|
|
|
|
#define SC_ERROR_OBJECT_NOT_FOUND -1024
|
2001-12-08 15:27:40 +00:00
|
|
|
|
#define SC_ERROR_CARD_RESET -1025
|
2001-12-16 18:46:32 +00:00
|
|
|
|
#define SC_ERROR_ASN1_OBJECT_NOT_FOUND -1026
|
|
|
|
|
#define SC_ERROR_ASN1_END_OF_CONTENTS -1027
|
|
|
|
|
#define SC_ERROR_TOO_MANY_OBJECTS -1028
|
2001-12-22 20:43:09 +00:00
|
|
|
|
#define SC_ERROR_INVALID_CARD -1029
|
2001-12-29 02:07:32 +00:00
|
|
|
|
#define SC_ERROR_WRONG_LENGTH -1030
|
|
|
|
|
#define SC_ERROR_RECORD_NOT_FOUND -1031
|
2002-01-20 21:20:09 +00:00
|
|
|
|
#define SC_ERROR_INTERNAL -1032
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
2001-12-29 02:07:32 +00:00
|
|
|
|
/* Different APDU cases */
|
2001-11-01 15:43:20 +00:00
|
|
|
|
#define SC_APDU_CASE_NONE 0
|
|
|
|
|
#define SC_APDU_CASE_1 1
|
|
|
|
|
#define SC_APDU_CASE_2_SHORT 2
|
|
|
|
|
#define SC_APDU_CASE_3_SHORT 3
|
|
|
|
|
#define SC_APDU_CASE_4_SHORT 4
|
|
|
|
|
#define SC_APDU_CASE_2_EXT 5
|
|
|
|
|
#define SC_APDU_CASE_3_EXT 6
|
|
|
|
|
#define SC_APDU_CASE_4_EXT 7
|
|
|
|
|
|
2001-12-29 02:07:32 +00:00
|
|
|
|
/* File types */
|
|
|
|
|
#define SC_FILE_TYPE_DF 0x04
|
|
|
|
|
#define SC_FILE_TYPE_INTERNAL_EF 0x03
|
|
|
|
|
#define SC_FILE_TYPE_WORKING_EF 0x01
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
2001-12-29 02:07:32 +00:00
|
|
|
|
/* EF structures */
|
|
|
|
|
#define SC_FILE_EF_UNKNOWN 0x00
|
2001-11-17 00:11:29 +00:00
|
|
|
|
#define SC_FILE_EF_TRANSPARENT 0x01
|
|
|
|
|
#define SC_FILE_EF_LINEAR_FIXED 0x02
|
|
|
|
|
#define SC_FILE_EF_LINEAR_FIXED_TLV 0x03
|
2001-12-25 20:45:48 +00:00
|
|
|
|
#define SC_FILE_EF_LINEAR_VARIABLE 0x04
|
2001-12-29 02:07:32 +00:00
|
|
|
|
#define SC_FILE_EF_LINEAR_VARIABLE_TLV 0x05
|
2001-12-25 20:45:48 +00:00
|
|
|
|
#define SC_FILE_EF_CYCLIC 0x06
|
2001-12-29 02:07:32 +00:00
|
|
|
|
#define SC_FILE_EF_CYCLIC_TLV 0x07
|
2001-12-25 20:45:48 +00:00
|
|
|
|
|
2001-12-29 02:07:32 +00:00
|
|
|
|
/* File status flags */
|
2001-12-25 20:45:48 +00:00
|
|
|
|
#define SC_FILE_STATUS_ACTIVATED 0x00
|
|
|
|
|
#define SC_FILE_STATUS_INVALIDATED 0x01
|
2001-11-17 00:11:29 +00:00
|
|
|
|
|
2001-12-29 02:07:32 +00:00
|
|
|
|
/* Access Control flags */
|
|
|
|
|
#define SC_AC_NONE 0x00000000
|
|
|
|
|
#define SC_AC_CHV1 0x00000001 /* Card Holder Verif. */
|
|
|
|
|
#define SC_AC_CHV2 0x00000002
|
2002-01-08 13:56:50 +00:00
|
|
|
|
#define SC_AC_TERM 0x00000004 /* Terminal auth. */
|
|
|
|
|
#define SC_AC_PRO 0x00000008 /* Secure Messaging */
|
|
|
|
|
#define SC_AC_AUT 0x00000010 /* Key auth. */
|
2002-01-20 21:20:09 +00:00
|
|
|
|
|
|
|
|
|
#define SC_AC_KEY_NUM_0 0x00000000
|
|
|
|
|
#define SC_AC_KEY_NUM_1 0x10000000
|
|
|
|
|
#define SC_AC_KEY_NUM_2 0x20000000
|
|
|
|
|
#define SC_AC_KEY_NUM_3 0x30000000
|
|
|
|
|
#define SC_AC_KEY_NUM_MASK 0xF0000000
|
|
|
|
|
|
2001-12-29 02:07:32 +00:00
|
|
|
|
#define SC_AC_NEVER 0xFFFFFFFE
|
|
|
|
|
#define SC_AC_UNKNOWN 0xFFFFFFFF
|
|
|
|
|
|
|
|
|
|
/* Operations relating to access control (in case of DF) */
|
|
|
|
|
#define SC_AC_OP_SELECT 0
|
|
|
|
|
#define SC_AC_OP_LOCK 1
|
|
|
|
|
#define SC_AC_OP_DELETE 2
|
|
|
|
|
#define SC_AC_OP_CREATE 3
|
|
|
|
|
#define SC_AC_OP_REHABILITATE 4
|
|
|
|
|
#define SC_AC_OP_INVALIDATE 5
|
2002-01-08 13:56:50 +00:00
|
|
|
|
#define SC_AC_OP_LIST_FILES 6
|
2001-12-29 02:07:32 +00:00
|
|
|
|
|
|
|
|
|
/* Operations relating to access control (in case of EF) */
|
|
|
|
|
#define SC_AC_OP_READ 0
|
|
|
|
|
#define SC_AC_OP_UPDATE 1
|
|
|
|
|
#define SC_AC_OP_WRITE 2
|
|
|
|
|
#define SC_AC_OP_ERASE 3
|
|
|
|
|
/* rehab and invalidate are the same as in DF case */
|
|
|
|
|
|
2002-01-08 13:56:50 +00:00
|
|
|
|
#define SC_MAX_AC_OPS 7
|
2001-12-29 02:07:32 +00:00
|
|
|
|
|
2002-01-08 13:56:50 +00:00
|
|
|
|
/* sc_read_record() flags */
|
2001-12-29 02:07:32 +00:00
|
|
|
|
#define SC_READ_RECORD_EF_ID_MASK 0x0001F
|
|
|
|
|
#define SC_READ_RECORD_BY_REC_ID 0x00000
|
|
|
|
|
#define SC_READ_RECORD_BY_REC_NR 0x00100
|
|
|
|
|
|
|
|
|
|
/* various maximum values */
|
2001-12-22 20:43:09 +00:00
|
|
|
|
#define SC_MAX_CARD_DRIVERS 16
|
2002-01-08 13:56:50 +00:00
|
|
|
|
#define SC_MAX_CARD_DRIVER_SNAME_SIZE 16
|
2001-11-01 15:43:20 +00:00
|
|
|
|
#define SC_MAX_READERS 4
|
2001-12-15 01:29:51 +00:00
|
|
|
|
#define SC_MAX_APDU_BUFFER_SIZE 255
|
2001-11-01 15:43:20 +00:00
|
|
|
|
#define SC_MAX_PATH_SIZE 16
|
|
|
|
|
#define SC_MAX_PIN_SIZE 16
|
2001-11-07 13:45:41 +00:00
|
|
|
|
#define SC_MAX_ATR_SIZE 33
|
2001-11-17 00:11:29 +00:00
|
|
|
|
#define SC_MAX_SEC_ATTR_SIZE 16
|
|
|
|
|
#define SC_MAX_PROP_ATTR_SIZE 16
|
2002-01-08 13:56:50 +00:00
|
|
|
|
#define SC_MAX_OBJECT_ID_OCTETS 16
|
|
|
|
|
#define SC_APDU_CHOP_SIZE 250
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
|
|
|
|
typedef unsigned char u8;
|
|
|
|
|
|
|
|
|
|
struct sc_object_id {
|
2001-12-29 02:07:32 +00:00
|
|
|
|
int value[SC_MAX_OBJECT_ID_OCTETS];
|
2001-11-01 15:43:20 +00:00
|
|
|
|
};
|
|
|
|
|
|
2001-12-22 20:43:09 +00:00
|
|
|
|
#define SC_PATH_TYPE_FILE_ID 0
|
|
|
|
|
#define SC_PATH_TYPE_DF_NAME 1
|
|
|
|
|
#define SC_PATH_TYPE_PATH 2
|
|
|
|
|
|
2001-11-01 15:43:20 +00:00
|
|
|
|
struct sc_path {
|
|
|
|
|
u8 value[SC_MAX_PATH_SIZE];
|
2001-12-22 13:38:25 +00:00
|
|
|
|
size_t len;
|
2001-12-16 18:46:32 +00:00
|
|
|
|
int index;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
|
|
|
|
|
int type;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct sc_file {
|
|
|
|
|
struct sc_path path;
|
2001-12-29 02:07:32 +00:00
|
|
|
|
u8 name[16]; /* DF name */
|
|
|
|
|
size_t namelen; /* length of DF name */
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
2001-11-05 19:39:18 +00:00
|
|
|
|
int type, shareable, ef_structure;
|
2001-12-29 02:07:32 +00:00
|
|
|
|
size_t size; /* Size of file (in bytes) */
|
|
|
|
|
int id; /* Short file id (2 bytes) */
|
|
|
|
|
int status; /* Status flags */
|
|
|
|
|
unsigned int acl[SC_MAX_AC_OPS]; /* Access Control List */
|
|
|
|
|
|
2002-01-13 23:56:13 +00:00
|
|
|
|
int record_length; /* In case of fixed-length or cyclic EF */
|
|
|
|
|
int record_count; /* Valid, if not transparent EF or DF */
|
|
|
|
|
|
2001-11-17 00:11:29 +00:00
|
|
|
|
u8 sec_attr[SC_MAX_SEC_ATTR_SIZE];
|
2001-12-22 13:38:25 +00:00
|
|
|
|
size_t sec_attr_len;
|
2001-12-25 20:45:48 +00:00
|
|
|
|
u8 prop_attr[SC_MAX_PROP_ATTR_SIZE];
|
2001-12-22 13:38:25 +00:00
|
|
|
|
size_t prop_attr_len;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
unsigned int magic;
|
|
|
|
|
};
|
|
|
|
|
|
2001-12-21 23:34:47 +00:00
|
|
|
|
#define SC_SEC_OPERATION_DECIPHER 0
|
|
|
|
|
#define SC_SEC_OPERATION_SIGN 1
|
|
|
|
|
|
2002-01-16 23:59:18 +00:00
|
|
|
|
/* sc_security_env flags */
|
2002-01-20 21:20:09 +00:00
|
|
|
|
#define SC_SEC_ENV_ALG_REF_PRESENT 0x0001
|
|
|
|
|
#define SC_SEC_ENV_FILE_REF_PRESENT 0x0002
|
|
|
|
|
#define SC_SEC_ENV_KEY_REF_PRESENT 0x0004
|
|
|
|
|
/* FIXME: the flag below is misleading */
|
|
|
|
|
#define SC_SEC_ENV_KEY_REF_ASYMMETRIC 0x0008
|
|
|
|
|
#define SC_SEC_ENV_ALG_PRESENT 0x0010
|
|
|
|
|
|
|
|
|
|
#define SC_ALGORITHM_RSA 0
|
|
|
|
|
#define SC_ALGORITHM_DSA 1
|
|
|
|
|
#define SC_ALGORITHM_EC 2
|
|
|
|
|
|
|
|
|
|
#define SC_ALGORITHM_RSA_PKCS1_PAD 0x01
|
|
|
|
|
#define SC_ALGORITHM_RSA_HASH_SHA1 0x02
|
2002-01-07 18:23:34 +00:00
|
|
|
|
|
2001-12-20 13:57:58 +00:00
|
|
|
|
struct sc_security_env {
|
2002-01-20 21:20:09 +00:00
|
|
|
|
int flags;
|
2001-12-20 13:57:58 +00:00
|
|
|
|
int operation;
|
2002-01-20 21:20:09 +00:00
|
|
|
|
unsigned int algorithm, algorithm_flags;
|
2002-01-16 23:59:18 +00:00
|
|
|
|
|
2002-01-20 21:20:09 +00:00
|
|
|
|
unsigned int algorithm_ref;
|
|
|
|
|
struct sc_path file_ref;
|
|
|
|
|
u8 key_ref[8];
|
|
|
|
|
size_t key_ref_len;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct sc_card_cache {
|
|
|
|
|
struct sc_path current_path;
|
2001-12-20 13:57:58 +00:00
|
|
|
|
};
|
|
|
|
|
|
2002-01-07 18:23:34 +00:00
|
|
|
|
/*
|
|
|
|
|
* Card capabilities
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* SC_CARD_APDU_EXT: Card can handle large (> 256 bytes) buffers in
|
|
|
|
|
* calls to read_binary, write_binary and update_binary; if not,
|
|
|
|
|
* several successive calls to the corresponding function is made. */
|
|
|
|
|
#define SC_CARD_CAP_APDU_EXT 0x00000001
|
|
|
|
|
|
|
|
|
|
/* SC_CARD_CAP_EMV: Card can handle operations specified in the
|
|
|
|
|
* EMV 4.0 standard. */
|
|
|
|
|
#define SC_CARD_CAP_EMV 0x00000002
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Card flags
|
|
|
|
|
*/
|
|
|
|
|
/* none yet */
|
|
|
|
|
|
2001-12-22 20:43:09 +00:00
|
|
|
|
struct sc_card {
|
|
|
|
|
struct sc_context *ctx;
|
|
|
|
|
|
|
|
|
|
SCARDHANDLE pcsc_card;
|
|
|
|
|
int reader;
|
2002-01-07 18:23:34 +00:00
|
|
|
|
unsigned long caps, flags;
|
|
|
|
|
int cla;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
u8 atr[SC_MAX_ATR_SIZE];
|
|
|
|
|
size_t atr_len;
|
|
|
|
|
|
|
|
|
|
pthread_mutex_t mutex;
|
2001-12-25 20:45:48 +00:00
|
|
|
|
int lock_count;
|
2001-12-29 02:07:32 +00:00
|
|
|
|
const struct sc_card_driver *driver;
|
2002-01-08 13:56:50 +00:00
|
|
|
|
struct sc_card_operations *ops;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
void *ops_data;
|
2002-01-20 21:20:09 +00:00
|
|
|
|
|
|
|
|
|
struct sc_card_cache cache;
|
|
|
|
|
int cache_valid;
|
|
|
|
|
|
2001-12-25 20:45:48 +00:00
|
|
|
|
unsigned int magic;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
};
|
2001-12-20 13:57:58 +00:00
|
|
|
|
|
2002-01-20 21:20:09 +00:00
|
|
|
|
|
2001-12-20 13:57:58 +00:00
|
|
|
|
struct sc_card_operations {
|
2001-12-22 20:43:09 +00:00
|
|
|
|
/* Called in sc_connect_card(). Must return 1, if the current
|
|
|
|
|
* card can be handled with this driver, or 0 otherwise. ATR
|
|
|
|
|
* field of the sc_card struct is filled in before calling
|
|
|
|
|
* this function. */
|
|
|
|
|
int (*match_card)(struct sc_card *card);
|
|
|
|
|
|
|
|
|
|
/* Called when ATR of the inserted card matches an entry in ATR
|
|
|
|
|
* table. May return SC_ERROR_INVALID_CARD to indicate that
|
|
|
|
|
* the card cannot be handled with this driver. */
|
2001-12-20 13:57:58 +00:00
|
|
|
|
int (*init)(struct sc_card *card);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
/* Called when the card object is being freed. finish() has to
|
|
|
|
|
* deallocate all possible private data. */
|
2001-12-20 13:57:58 +00:00
|
|
|
|
int (*finish)(struct sc_card *card);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
|
|
|
|
|
/* ISO 7816-4 functions */
|
2001-12-20 13:57:58 +00:00
|
|
|
|
|
2001-12-21 23:34:47 +00:00
|
|
|
|
int (*read_binary)(struct sc_card *card, unsigned int idx,
|
2001-12-29 02:07:32 +00:00
|
|
|
|
u8 * buf, size_t count, unsigned long flags);
|
2001-12-21 23:34:47 +00:00
|
|
|
|
int (*write_binary)(struct sc_card *card, unsigned int idx,
|
2001-12-29 02:07:32 +00:00
|
|
|
|
const u8 * buf, size_t count, unsigned long flags);
|
2001-12-21 23:34:47 +00:00
|
|
|
|
int (*update_binary)(struct sc_card *card, unsigned int idx,
|
2001-12-29 02:07:32 +00:00
|
|
|
|
const u8 * buf, size_t count, unsigned long flags);
|
2001-12-21 23:34:47 +00:00
|
|
|
|
int (*erase_binary)(struct sc_card *card, unsigned int idx,
|
2001-12-29 02:07:32 +00:00
|
|
|
|
size_t count, unsigned long flags);
|
|
|
|
|
int (*read_record)(struct sc_card *card, unsigned int rec_nr,
|
|
|
|
|
u8 * buf, size_t count, unsigned long flags);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
|
|
|
|
|
/* select_file: Does the equivalent of SELECT FILE command specified
|
|
|
|
|
* in ISO7816-4. Stores information about the selected file to
|
2002-01-01 17:25:10 +00:00
|
|
|
|
* <file>, if not NULL. */
|
2001-12-22 23:51:12 +00:00
|
|
|
|
int (*select_file)(struct sc_card *card, const struct sc_path *path,
|
|
|
|
|
struct sc_file *file_out);
|
2001-12-20 13:57:58 +00:00
|
|
|
|
int (*get_response)(struct sc_card *card, u8 * buf, size_t count);
|
|
|
|
|
int (*get_challenge)(struct sc_card *card, u8 * buf, size_t count);
|
|
|
|
|
|
2002-01-07 18:23:34 +00:00
|
|
|
|
/*
|
|
|
|
|
* ISO 7816-8 functions
|
|
|
|
|
*/
|
|
|
|
|
|
2002-01-10 23:02:48 +00:00
|
|
|
|
/* verify: Verifies reference data of type <acl>, identified by
|
|
|
|
|
* <ref_qualifier>. If <tries_left> is not NULL, number of verifying
|
|
|
|
|
* tries left is saved in case of verification failure, if the
|
|
|
|
|
* information is available. */
|
|
|
|
|
int (*verify)(struct sc_card *card, unsigned int type,
|
|
|
|
|
int ref_qualifier, const u8 *data, size_t data_len,
|
|
|
|
|
int *tries_left);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
|
|
|
|
|
/* restore_security_env: Restores a previously saved security
|
|
|
|
|
* environment, and stores information about the environment to
|
|
|
|
|
* <env_out>, if not NULL. */
|
2002-01-16 23:59:18 +00:00
|
|
|
|
int (*restore_security_env)(struct sc_card *card, int se_num);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
|
|
|
|
|
/* set_security_env: Initializes the security environment on card
|
|
|
|
|
* according to <env>, and stores the environment as <se_num> on the
|
|
|
|
|
* card. If se_num <= 0, the environment will not be stored. */
|
2001-12-20 13:57:58 +00:00
|
|
|
|
int (*set_security_env)(struct sc_card *card,
|
2001-12-22 20:43:09 +00:00
|
|
|
|
const struct sc_security_env *env, int se_num);
|
2002-01-07 18:23:34 +00:00
|
|
|
|
/* decipher: Engages the deciphering operation. Card will use the
|
|
|
|
|
* security environment set in a call to set_security_env or
|
|
|
|
|
* restore_security_env. */
|
2001-12-20 13:57:58 +00:00
|
|
|
|
int (*decipher)(struct sc_card *card, const u8 * crgram,
|
|
|
|
|
size_t crgram_len, u8 * out, size_t outlen);
|
2002-01-07 18:23:34 +00:00
|
|
|
|
/* compute_signature: Generates a digital signature on the card. Similiar
|
2002-01-09 01:03:10 +00:00
|
|
|
|
* to the function decipher. */
|
2001-12-20 13:57:58 +00:00
|
|
|
|
int (*compute_signature)(struct sc_card *card, const u8 * data,
|
|
|
|
|
size_t data_len, u8 * out, size_t outlen);
|
|
|
|
|
int (*change_reference_data)(struct sc_card *card, int ref_qualifier,
|
|
|
|
|
const u8 *old, size_t oldlen,
|
|
|
|
|
const u8 *newref, size_t newlen,
|
|
|
|
|
int *tries_left);
|
|
|
|
|
int (*reset_retry_counter)(struct sc_card *card, int ref_qualifier,
|
|
|
|
|
const u8 *puk, size_t puklen,
|
|
|
|
|
const u8 *newref, size_t newlen);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
/*
|
|
|
|
|
* ISO 7816-9 functions
|
|
|
|
|
*/
|
2002-01-17 11:44:27 +00:00
|
|
|
|
int (*create_file)(struct sc_card *card, struct sc_file *file);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
int (*delete_file)(struct sc_card *card, const struct sc_path *path);
|
2002-01-09 01:03:10 +00:00
|
|
|
|
/* list_files: Enumerates all the files in the current DF, and
|
|
|
|
|
* writes the corresponding file identifiers to <buf>. Returns
|
|
|
|
|
* the number of bytes stored. */
|
|
|
|
|
int (*list_files)(struct sc_card *card, u8 *buf, size_t buflen);
|
2001-12-20 13:57:58 +00:00
|
|
|
|
};
|
|
|
|
|
|
2001-12-22 20:43:09 +00:00
|
|
|
|
struct sc_card_driver {
|
|
|
|
|
char *libpath; /* NULL, if compiled in */
|
2002-01-08 13:56:50 +00:00
|
|
|
|
const char *name;
|
|
|
|
|
const char *short_name;
|
2001-12-20 13:57:58 +00:00
|
|
|
|
struct sc_card_operations *ops;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct sc_context {
|
|
|
|
|
SCARDCONTEXT pcsc_ctx;
|
|
|
|
|
char *readers[SC_MAX_READERS];
|
|
|
|
|
int reader_count;
|
2001-12-13 21:19:11 +00:00
|
|
|
|
|
2001-12-19 21:58:04 +00:00
|
|
|
|
int debug;
|
|
|
|
|
|
2001-12-13 21:19:11 +00:00
|
|
|
|
int use_std_output, use_cache;
|
2002-01-07 18:23:34 +00:00
|
|
|
|
const struct sc_card_driver *card_drivers[SC_MAX_CARD_DRIVERS+1];
|
2002-01-10 12:33:56 +00:00
|
|
|
|
const struct sc_card_driver *forced_driver;
|
2002-01-07 18:23:34 +00:00
|
|
|
|
pthread_mutex_t mutex;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct sc_apdu {
|
|
|
|
|
int cse; /* APDU case */
|
2001-12-29 02:07:32 +00:00
|
|
|
|
u8 cla, ins, p1, p2; /* CLA, INS, P1 and P2 bytes */
|
|
|
|
|
size_t lc, le; /* Lc and Le bytes */
|
|
|
|
|
const u8 *data; /* C-APDU data */
|
|
|
|
|
size_t datalen; /* length of data in C-APDU */
|
|
|
|
|
u8 *resp; /* R-APDU data buffer */
|
|
|
|
|
size_t resplen; /* in: size of R-APDU buffer,
|
|
|
|
|
* out: length of data returned in R-APDU */
|
|
|
|
|
|
|
|
|
|
unsigned int sw1, sw2; /* Status words returned in R-APDU */
|
2001-11-01 15:43:20 +00:00
|
|
|
|
};
|
|
|
|
|
|
2001-11-05 19:39:18 +00:00
|
|
|
|
/* Base64 encoding/decoding functions */
|
2001-12-22 13:38:25 +00:00
|
|
|
|
int sc_base64_encode(const u8 *in, size_t inlen, u8 *out, size_t outlen,
|
|
|
|
|
size_t linelength);
|
|
|
|
|
int sc_base64_decode(const char *in, u8 *out, size_t outlen);
|
2001-11-04 13:57:04 +00:00
|
|
|
|
|
2001-11-05 19:39:18 +00:00
|
|
|
|
/* APDU handling functions */
|
2001-11-01 15:43:20 +00:00
|
|
|
|
int sc_transmit_apdu(struct sc_card *card, struct sc_apdu *apdu);
|
2001-12-22 13:38:25 +00:00
|
|
|
|
void sc_format_apdu(struct sc_card *card, struct sc_apdu *apdu, int cse, int ins,
|
|
|
|
|
int p1, int p2);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
2002-01-10 23:02:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* Establishes an OpenSC context
|
|
|
|
|
* @param ctx A pointer to a pointer that will receive the allocated context
|
|
|
|
|
*/
|
2001-11-01 15:43:20 +00:00
|
|
|
|
int sc_establish_context(struct sc_context **ctx);
|
2002-01-10 23:02:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* Destroys an established OpenSC context
|
|
|
|
|
* @param ctx A pointer to the context structure to be destroyed
|
|
|
|
|
*/
|
2001-11-01 15:43:20 +00:00
|
|
|
|
int sc_destroy_context(struct sc_context *ctx);
|
2002-01-10 23:02:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* Forces the use of a specified card driver
|
|
|
|
|
* @param ctx OpenSC context
|
|
|
|
|
* @param short_name The short name of the driver to use (e.g. 'emv')
|
|
|
|
|
*/
|
2002-01-10 12:33:56 +00:00
|
|
|
|
int sc_set_card_driver(struct sc_context *ctx, const char *short_name);
|
2002-01-10 23:02:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* Connects to a card in a reader and auto-detects the card driver.
|
|
|
|
|
* The ATR (Answer to Reset) string of the card is also retrieved.
|
|
|
|
|
* @param ctx OpenSC context
|
|
|
|
|
* @param reader Reader number (this is prone to change)
|
|
|
|
|
* @param card The allocated card object will go here */
|
2001-11-01 15:43:20 +00:00
|
|
|
|
int sc_connect_card(struct sc_context *ctx,
|
|
|
|
|
int reader, struct sc_card **card);
|
2002-01-10 23:02:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* Disconnects from a card, and frees the card structure. Any locks
|
|
|
|
|
* made by the application must be released before calling this function.
|
|
|
|
|
* NOTE: The card is not reset nor powered down after the operation.
|
|
|
|
|
* @param card The card to disconnect
|
|
|
|
|
*/
|
2001-11-01 15:43:20 +00:00
|
|
|
|
int sc_disconnect_card(struct sc_card *card);
|
2002-01-10 23:02:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns 1 if the magic value of the card object is correct. Mostly
|
|
|
|
|
* used internally by the library.
|
|
|
|
|
* @param card The card object to check
|
|
|
|
|
*/
|
2001-12-29 02:07:32 +00:00
|
|
|
|
inline int sc_card_valid(const struct sc_card *card);
|
2001-11-17 15:48:10 +00:00
|
|
|
|
|
2002-01-10 23:02:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* Checks if a card is present in a reader
|
|
|
|
|
* @param ctx OpenSC context
|
|
|
|
|
* @param reader Reader number to use (this will change)
|
|
|
|
|
* @retval 1 if a card is present
|
|
|
|
|
* @retval 0 card absent
|
|
|
|
|
* @retval < 0 if an error occured
|
|
|
|
|
*/
|
2001-11-01 15:43:20 +00:00
|
|
|
|
int sc_detect_card(struct sc_context *ctx, int reader);
|
2001-11-17 15:48:10 +00:00
|
|
|
|
|
2002-01-10 23:02:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* Waits for card insertion to a reader
|
|
|
|
|
* @param ctx OpenSC context
|
|
|
|
|
* @param reader Reader number; -1 implies to listen on all readers
|
|
|
|
|
* @param timeout Amount of millisecs to wait; -1 means forever
|
|
|
|
|
* @retval 1 if a card was inserted
|
|
|
|
|
* @retval 0 if operation timed out
|
|
|
|
|
* @retval < 0 if an error occured
|
|
|
|
|
*/
|
2001-11-01 15:43:20 +00:00
|
|
|
|
int sc_wait_for_card(struct sc_context *ctx, int reader, int timeout);
|
|
|
|
|
|
2002-01-10 23:02:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* Locks the card against modification from other threads.
|
|
|
|
|
* After the initial call to sc_lock, the card is protected from
|
|
|
|
|
* access from other processes. The function may be called several times.
|
|
|
|
|
* @param card The card to lock
|
|
|
|
|
* @retval SC_SUCCESS on success
|
|
|
|
|
*/
|
2001-11-01 15:43:20 +00:00
|
|
|
|
int sc_lock(struct sc_card *card);
|
2002-01-10 23:02:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* Unlocks a previously locked card. After the lock count drops to zero,
|
|
|
|
|
* the card is again placed in shared mode, where other processes
|
|
|
|
|
* may access or lock it.
|
|
|
|
|
* @param card The card to unlock
|
|
|
|
|
* @retval SC_SUCCESS on success
|
|
|
|
|
*/
|
2001-11-01 15:43:20 +00:00
|
|
|
|
int sc_unlock(struct sc_card *card);
|
|
|
|
|
|
2001-11-04 14:08:38 +00:00
|
|
|
|
/* ISO 7816-4 related functions */
|
2002-01-01 17:25:10 +00:00
|
|
|
|
|
2002-01-10 23:02:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* Does the equivalent of ISO 7816-4 command SELECT FILE.
|
|
|
|
|
* @param card The card on which to issue the command
|
|
|
|
|
* @param path The path, file id or name of the desired file
|
|
|
|
|
* @param file If not NULL, will contain information about the selected file
|
|
|
|
|
* @retval SC_SUCCESS on success
|
|
|
|
|
*/
|
2001-12-22 23:51:12 +00:00
|
|
|
|
int sc_select_file(struct sc_card *card, const struct sc_path *path,
|
|
|
|
|
struct sc_file *file);
|
2002-01-10 23:02:48 +00:00
|
|
|
|
/* TODO: finish writing API docs */
|
2001-12-29 02:07:32 +00:00
|
|
|
|
int sc_read_binary(struct sc_card *card, unsigned int idx, u8 * buf,
|
|
|
|
|
size_t count, unsigned long flags);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
int sc_write_binary(struct sc_card *card, unsigned int idx, const u8 * buf,
|
|
|
|
|
size_t count, unsigned long flags);
|
2002-01-13 23:56:13 +00:00
|
|
|
|
int sc_update_binary(struct sc_card *card, unsigned int idx, const u8 * buf,
|
|
|
|
|
size_t count, unsigned long flags);
|
2001-12-29 02:07:32 +00:00
|
|
|
|
int sc_read_record(struct sc_card *card, unsigned int rec_nr, u8 * buf,
|
|
|
|
|
size_t count, unsigned long flags);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
int sc_get_challenge(struct sc_card *card, u8 * rndout, size_t len);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
2001-11-06 18:34:19 +00:00
|
|
|
|
/* ISO 7816-8 related functions */
|
2001-11-01 15:43:20 +00:00
|
|
|
|
int sc_restore_security_env(struct sc_card *card, int se_num);
|
|
|
|
|
int sc_set_security_env(struct sc_card *card,
|
2002-01-16 23:59:18 +00:00
|
|
|
|
const struct sc_security_env *env, int se_num);
|
2001-12-22 13:38:25 +00:00
|
|
|
|
int sc_decipher(struct sc_card *card, const u8 * crgram, size_t crgram_len,
|
|
|
|
|
u8 * out, size_t outlen);
|
2001-11-05 19:39:18 +00:00
|
|
|
|
int sc_compute_signature(struct sc_card *card, const u8 * data,
|
2001-12-22 13:38:25 +00:00
|
|
|
|
size_t data_len, u8 * out, size_t outlen);
|
2002-01-10 23:02:48 +00:00
|
|
|
|
int sc_verify(struct sc_card *card, unsigned int type, int ref, const u8 *buf,
|
|
|
|
|
size_t buflen, int *tries_left);
|
2001-11-20 22:21:58 +00:00
|
|
|
|
int sc_change_reference_data(struct sc_card *card, int ref, const u8 *old,
|
2001-12-22 13:38:25 +00:00
|
|
|
|
size_t oldlen, const u8 *newref, size_t newlen,
|
2001-12-02 19:21:46 +00:00
|
|
|
|
int *tries_left);
|
2001-11-20 22:21:58 +00:00
|
|
|
|
int sc_reset_retry_counter(struct sc_card *card, int ref, const u8 *puk,
|
2001-12-22 13:38:25 +00:00
|
|
|
|
size_t puklen, const u8 *newref, size_t newlen);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
2001-11-17 00:11:29 +00:00
|
|
|
|
/* ISO 7816-9 */
|
2002-01-17 11:44:27 +00:00
|
|
|
|
int sc_create_file(struct sc_card *card, struct sc_file *file);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
int sc_delete_file(struct sc_card *card, const struct sc_path *path);
|
2001-11-17 00:11:29 +00:00
|
|
|
|
|
2001-12-29 02:07:32 +00:00
|
|
|
|
inline int sc_file_valid(const struct sc_file *file);
|
|
|
|
|
void sc_format_path(const char *path_in, struct sc_path *path_out);
|
|
|
|
|
int sc_hex_to_bin(const char *in, u8 *out, size_t *outlen);
|
|
|
|
|
|
2002-01-07 18:23:34 +00:00
|
|
|
|
/* Possibly only valid on Setec cards */
|
2002-01-09 01:03:10 +00:00
|
|
|
|
int sc_list_files(struct sc_card *card, u8 * buf, size_t buflen);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
2002-01-17 12:04:14 +00:00
|
|
|
|
const char *sc_strerror(int sc_errno);
|
2001-11-20 22:21:58 +00:00
|
|
|
|
|
2001-11-18 01:52:32 +00:00
|
|
|
|
extern const char *sc_version;
|
2001-11-04 14:08:38 +00:00
|
|
|
|
|
2001-12-22 20:43:09 +00:00
|
|
|
|
extern const struct sc_card_driver *sc_get_iso7816_driver(void);
|
2001-12-27 17:25:10 +00:00
|
|
|
|
extern const struct sc_card_driver *sc_get_emv_driver(void);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
extern const struct sc_card_driver *sc_get_setec_driver(void);
|
2002-01-10 12:33:56 +00:00
|
|
|
|
extern const struct sc_card_driver *sc_get_flex_driver(void);
|
2001-12-25 20:45:48 +00:00
|
|
|
|
extern const struct sc_card_driver *sc_get_default_driver(void);
|
2001-11-17 14:55:41 +00:00
|
|
|
|
|
2001-12-08 15:27:40 +00:00
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2001-11-01 15:43:20 +00:00
|
|
|
|
#endif
|