1 /* 2 * internal.h: Internal definitions for libopensc 3 * 4 * Copyright (C) 2001, 2002 Juha Yrjölä <juha.yrjola@iki.fi> 5 * 2005 The OpenSC project 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 22 #ifndef _SC_INTERNAL_H 23 #define _SC_INTERNAL_H 24 25 #ifdef HAVE_CONFIG_H 26 #include "config.h" 27 #endif 28 29 #ifdef __cplusplus 30 extern "C" { 31 #endif 32 33 #include <assert.h> 34 #ifdef _WIN32 35 #include <windows.h> 36 #endif 37 38 #include "common/simclist.h" 39 #include "libopensc/opensc.h" 40 #include "libopensc/log.h" 41 #include "libopensc/cards.h" 42 #include "scconf/scconf.h" 43 44 #ifdef ENABLE_OPENSSL 45 #include "libopensc/sc-ossl-compat.h" 46 #endif 47 48 #define SC_FILE_MAGIC 0x14426950 49 50 #ifndef _WIN32 51 #define msleep(t) usleep((t) * 1000) 52 #else 53 #define msleep(t) Sleep(t) 54 #define sleep(t) Sleep((t) * 1000) 55 #endif 56 57 #ifndef MAX 58 #define MAX(x, y) (((x) > (y)) ? (x) : (y)) 59 #endif 60 #ifndef MIN 61 #define MIN(x, y) (((x) < (y)) ? (x) : (y)) 62 #endif 63 64 struct sc_atr_table { 65 /* The atr fields are required to 66 * be in aa:bb:cc hex format. */ 67 const char *atr; 68 /* The atrmask is logically AND'd with an 69 * card atr prior to comparison with the 70 * atr reference value above. */ 71 const char *atrmask; 72 const char *name; 73 int type; 74 unsigned long flags; 75 /* Reference to card_atr configuration block, 76 * available to user configured card entries. */ 77 scconf_block *card_atr; 78 }; 79 80 /* Internal use only */ 81 int _sc_add_reader(struct sc_context *ctx, struct sc_reader *reader); 82 int _sc_parse_atr(struct sc_reader *reader); 83 84 /* Add an ATR to the card driver's struct sc_atr_table */ 85 int _sc_add_atr(struct sc_context *ctx, struct sc_card_driver *driver, struct sc_atr_table *src); 86 int _sc_free_atr(struct sc_context *ctx, struct sc_card_driver *driver); 87 88 /** 89 * Convert an unsigned long into 4 bytes in big endian order 90 * @param buf the byte array for the result, should be 4 bytes long 91 * @param x the value to be converted 92 * @return the buffer passed, containing the converted value 93 */ 94 u8 *ulong2bebytes(u8 *buf, unsigned long x); 95 /** 96 * Convert an unsigned long into 2 bytes in big endian order 97 * @param buf the byte array for the result, should be 2 bytes long 98 * @param x the value to be converted 99 * @return the buffer passed, containing the converted value 100 */ 101 u8 *ushort2bebytes(u8 *buf, unsigned short x); 102 /** 103 * Convert 4 bytes in big endian order into an unsigned long 104 * @param buf the byte array of 4 bytes 105 * @return the converted value 106 */ 107 unsigned long bebytes2ulong(const u8 *buf); 108 /** 109 * Convert 2 bytes in big endian order into an unsigned short 110 * @param buf the byte array of 2 bytes 111 * @return the converted value 112 */ 113 unsigned short bebytes2ushort(const u8 *buf); 114 115 /** 116 * Convert 2 bytes in little endian order into an unsigned short 117 * @param buf the byte array of 2 bytes 118 * @return the converted value 119 */ 120 unsigned short lebytes2ushort(const u8 *buf); 121 /** 122 * Convert 4 bytes in little endian order into an unsigned long 123 * @param buf the byte array of 4 bytes 124 * @return the converted value 125 */ 126 unsigned long lebytes2ulong(const u8 *buf); 127 128 /* Usable for setting string elements of token info, which 129 * are either initialized to NULL or we need to clean 130 * previous value. 131 * 132 * @param strp The pointer where to store string 133 * @param value The string to store (is strdupped) 134 */ 135 void set_string(char **strp, const char *value); 136 137 #define BYTES4BITS(num) (((num) + 7) / 8) /* number of bytes necessary to hold 'num' bits */ 138 139 /* Returns an scconf_block entry with matching ATR/ATRmask to the ATR specified, 140 * NULL otherwise. Additionally, if card driver is not specified, search through 141 * all card drivers user configured ATRs. */ 142 scconf_block *_sc_match_atr_block(sc_context_t *ctx, struct sc_card_driver *driver, struct sc_atr *atr); 143 144 /* Returns an index number if a match was found, -1 otherwise. table has to 145 * be null terminated. */ 146 int _sc_match_atr(struct sc_card *card, const struct sc_atr_table *table, int *type_out); 147 148 int _sc_card_add_algorithm(struct sc_card *card, const struct sc_algorithm_info *info); 149 int _sc_card_add_symmetric_alg(sc_card_t *card, unsigned int algorithm, 150 unsigned int key_length, unsigned long flags); 151 int _sc_card_add_rsa_alg(struct sc_card *card, unsigned int key_length, 152 unsigned long flags, unsigned long exponent); 153 int _sc_card_add_ec_alg(struct sc_card *card, unsigned int key_length, 154 unsigned long flags, unsigned long ext_flags, 155 struct sc_object_id *curve_oid); 156 int _sc_card_add_eddsa_alg(struct sc_card *card, unsigned int key_length, 157 unsigned long flags, unsigned long ext_flags, 158 struct sc_object_id *curve_oid); 159 int _sc_card_add_xeddsa_alg(struct sc_card *card, unsigned int key_length, 160 unsigned long flags, unsigned long ext_flags, 161 struct sc_object_id *curve_oid); 162 163 /********************************************************************/ 164 /* pkcs1 padding/encoding functions */ 165 /********************************************************************/ 166 167 int sc_pkcs1_strip_01_padding(struct sc_context *ctx, const u8 *in_dat, size_t in_len, 168 u8 *out_dat, size_t *out_len); 169 int sc_pkcs1_strip_02_padding(struct sc_context *ctx, const u8 *data, size_t len, 170 u8 *out_dat, size_t *out_len); 171 int sc_pkcs1_strip_digest_info_prefix(unsigned int *algorithm, 172 const u8 *in_dat, size_t in_len, u8 *out_dat, size_t *out_len); 173 174 /** 175 * PKCS1 encodes the given data. 176 * @param ctx IN sc_context_t object 177 * @param flags IN the algorithm to use 178 * @param in IN input buffer 179 * @param inlen IN length of the input 180 * @param out OUT output buffer (in == out is allowed) 181 * @param outlen OUT length of the output buffer 182 * @param mod_bits IN length of the modulus in bits 183 * @return SC_SUCCESS on success and an error code otherwise 184 */ 185 int sc_pkcs1_encode(sc_context_t *ctx, unsigned long flags, 186 const u8 *in, size_t inlen, u8 *out, size_t *outlen, size_t mod_bits); 187 /** 188 * Get the necessary padding and sec. env. flags. 189 * @param ctx IN sc_contex_t object 190 * @param iflags IN the desired algorithms flags 191 * @param caps IN the card / key capabilities 192 * @param pflags OUT the padding flags to use 193 * @param salg OUT the security env. algorithm flag to use 194 * @return SC_SUCCESS on success and an error code otherwise 195 */ 196 int sc_get_encoding_flags(sc_context_t *ctx, 197 unsigned long iflags, unsigned long caps, 198 unsigned long *pflags, unsigned long *salg); 199 200 /********************************************************************/ 201 /* mutex functions */ 202 /********************************************************************/ 203 204 /** 205 * Creates a new sc_mutex object. Note: unless sc_mutex_set_mutex_funcs() 206 * this function does nothing and always returns SC_SUCCESS. 207 * @param ctx sc_context_t object with the thread context 208 * @param mutex pointer for the newly created mutex object 209 * @return SC_SUCCESS on success and an error code otherwise 210 */ 211 int sc_mutex_create(const sc_context_t *ctx, void **mutex); 212 /** 213 * Tries to acquire a lock for a sc_mutex object. Note: Unless 214 * sc_mutex_set_mutex_funcs() has been called before this 215 * function does nothing and always returns SUCCESS. 216 * @param ctx sc_context_t object with the thread context 217 * @param mutex mutex object to lock 218 * @return SC_SUCCESS on success and an error code otherwise 219 */ 220 int sc_mutex_lock(const sc_context_t *ctx, void *mutex); 221 /** 222 * Unlocks a sc_mutex object. Note: Unless sc_mutex_set_mutex_funcs() 223 * has been called before this function does nothing and always returns 224 * SC_SUCCESS. 225 * @param ctx sc_context_t object with the thread context 226 * @param mutex mutex object to unlock 227 * @return SC_SUCCESS on success and an error code otherwise 228 */ 229 int sc_mutex_unlock(const sc_context_t *ctx, void *mutex); 230 /** 231 * Destroys a sc_mutex object. Note: Unless sc_mutex_set_mutex_funcs() 232 * has been called before this function does nothing and always returns 233 * SC_SUCCESS. 234 * @param ctx sc_context_t object with the thread context 235 * @param mutex mutex object to be destroyed 236 * @return SC_SUCCESS on success and an error code otherwise 237 */ 238 int sc_mutex_destroy(const sc_context_t *ctx, void *mutex); 239 /** 240 * Returns a unique id for every thread. 241 * @param ctx sc_context_t object with the thread context 242 * @return unsigned long with the unique id or 0 if not supported 243 */ 244 unsigned long sc_thread_id(const sc_context_t *ctx); 245 246 /********************************************************************/ 247 /* internal APDU handling functions */ 248 /********************************************************************/ 249 250 /** 251 * Returns the encoded APDU in newly created buffer. 252 * @param ctx sc_context_t object 253 * @param apdu sc_apdu_t object with the APDU to encode 254 * @param buf pointer to the newly allocated buffer 255 * @param len length of the encoded APDU 256 * @param proto protocol to be used 257 * @return SC_SUCCESS on success and an error code otherwise 258 */ 259 int sc_apdu_get_octets(sc_context_t *ctx, const sc_apdu_t *apdu, u8 **buf, 260 size_t *len, unsigned int proto); 261 /** 262 * Sets the status bytes and return data in the APDU 263 * @param ctx sc_context_t object 264 * @param apdu the apdu to which the data should be written 265 * @param buf returned data 266 * @param len length of the returned data 267 * @return SC_SUCCESS on success and an error code otherwise 268 */ 269 int sc_apdu_set_resp(sc_context_t *ctx, sc_apdu_t *apdu, const u8 *buf, 270 size_t len); 271 /** 272 * Logs APDU 273 * @param ctx sc_context_t object 274 * @param buf buffer with the APDU data 275 * @param len length of the APDU 276 * @param is_outgoing != 0 if the data is send to the card 277 */ 278 #define sc_apdu_log(ctx, data, len, is_outgoing) \ 279 sc_debug_hex(ctx, SC_LOG_DEBUG_NORMAL, is_outgoing != 0 ? "Outgoing APDU" : "Incoming APDU", data, len) 280 281 extern struct sc_reader_driver *sc_get_pcsc_driver(void); 282 extern struct sc_reader_driver *sc_get_ctapi_driver(void); 283 extern struct sc_reader_driver *sc_get_openct_driver(void); 284 extern struct sc_reader_driver *sc_get_cryptotokenkit_driver(void); 285 286 #ifdef __cplusplus 287 } 288 #endif 289 290 #endif 291