root/trunk/src/libopensc/internal.h
| Revision 3405, 9.1 KB (checked in by alonbl, 9 months ago) | |
|---|---|
|
|
| Line | |
|---|---|
| 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 "opensc.h" |
| 34 | #include "log.h" |
| 35 | #include "ui.h" |
| 36 | #include "cards.h" |
| 37 | #include <assert.h> |
| 38 | #ifdef _WIN32 |
| 39 | #include <windows.h> |
| 40 | #endif |
| 41 | |
| 42 | #define SC_FILE_MAGIC 0x14426950 |
| 43 | #define SC_CARD_MAGIC 0x27182818 |
| 44 | #define SC_CTX_MAGIC 0x0A550335 |
| 45 | |
| 46 | #ifndef _WIN32 |
| 47 | #define msleep(t) usleep((t) * 1000) |
| 48 | #else |
| 49 | #define msleep(t) Sleep(t) |
| 50 | #define sleep(t) Sleep((t) * 1000) |
| 51 | #endif |
| 52 | |
| 53 | struct sc_atr_table { |
| 54 | /* The atr fields are required to |
| 55 | * be in aa:bb:cc hex format. */ |
| 56 | char *atr; |
| 57 | /* The atrmask is logically AND'd with an |
| 58 | * card atr prior to comparison with the |
| 59 | * atr reference value above. */ |
| 60 | char *atrmask; |
| 61 | char *name; |
| 62 | int type; |
| 63 | unsigned long flags; |
| 64 | /* Reference to card_atr configuration block, |
| 65 | * available to user configured card entries. */ |
| 66 | scconf_block *card_atr; |
| 67 | }; |
| 68 | |
| 69 | /* Internal use only */ |
| 70 | int sc_check_sw(struct sc_card *card, unsigned int sw1, unsigned int sw2); |
| 71 | |
| 72 | int _sc_add_reader(struct sc_context *ctx, struct sc_reader *reader); |
| 73 | int _sc_parse_atr(struct sc_context *ctx, struct sc_slot_info *slot); |
| 74 | struct sc_slot_info *_sc_get_slot_info(struct sc_reader *reader, int slot_id); |
| 75 | |
| 76 | /* Add an ATR to the card driver's struct sc_atr_table */ |
| 77 | int _sc_add_atr(struct sc_context *ctx, struct sc_card_driver *driver, struct sc_atr_table *src); |
| 78 | int _sc_free_atr(struct sc_context *ctx, struct sc_card_driver *driver); |
| 79 | |
| 80 | /** |
| 81 | * Convert an unsigned long into 4 bytes in big endian order |
| 82 | * @param buf the byte array for the result, should be 4 bytes long |
| 83 | * @param x the value to be converted |
| 84 | */ |
| 85 | void ulong2bebytes(u8 *buf, unsigned long x); |
| 86 | /** |
| 87 | * Convert an unsigned long into 2 bytes in big endian order |
| 88 | * @param buf the byte array for the result, should be 2 bytes long |
| 89 | * @param x the value to be converted |
| 90 | */ |
| 91 | void ushort2bebytes(u8 *buf, unsigned short x); |
| 92 | /** |
| 93 | * Convert 4 bytes in big endian order into an unsigned long |
| 94 | * @param buf the byte array of 4 bytes |
| 95 | * @return the converted value |
| 96 | */ |
| 97 | unsigned long bebytes2ulong(const u8 *buf); |
| 98 | /** |
| 99 | * Convert 2 bytes in big endian order into an unsigned short |
| 100 | * @param buf the byte array of 2 bytes |
| 101 | * @return the converted value |
| 102 | */ |
| 103 | unsigned short bebytes2ushort(const u8 *buf); |
| 104 | |
| 105 | /* Returns an scconf_block entry with matching ATR/ATRmask to the ATR specified, |
| 106 | * NULL otherwise. Additionally, if card driver is not specified, search through |
| 107 | * all card drivers user configured ATRs. */ |
| 108 | scconf_block *_sc_match_atr_block(sc_context_t *ctx, struct sc_card_driver *driver, u8 *atr, size_t atr_len); |
| 109 | |
| 110 | /* Returns an index number if a match was found, -1 otherwise. table has to |
| 111 | * be null terminated. */ |
| 112 | int _sc_match_atr(struct sc_card *card, struct sc_atr_table *table, int *type_out); |
| 113 | |
| 114 | int _sc_check_forced_protocol(struct sc_context *ctx, u8 *atr, size_t atr_len, unsigned int *protocol); |
| 115 | |
| 116 | int _sc_card_add_algorithm(struct sc_card *card, const struct sc_algorithm_info *info); |
| 117 | int _sc_card_add_rsa_alg(struct sc_card *card, unsigned int key_length, |
| 118 | unsigned long flags, unsigned long exponent); |
| 119 | struct sc_algorithm_info * _sc_card_find_rsa_alg(struct sc_card *card, |
| 120 | unsigned int key_length); |
| 121 | |
| 122 | int sc_asn1_read_tag(const u8 ** buf, size_t buflen, unsigned int *cla_out, |
| 123 | unsigned int *tag_out, size_t *taglen); |
| 124 | |
| 125 | /********************************************************************/ |
| 126 | /* pkcs1 padding/encoding functions */ |
| 127 | /********************************************************************/ |
| 128 | |
| 129 | int sc_pkcs1_strip_01_padding(const u8 *in_dat, size_t in_len, u8 *out_dat, |
| 130 | size_t *out_len); |
| 131 | int sc_pkcs1_strip_02_padding(const u8 *data, size_t len, u8 *out_dat, |
| 132 | size_t *out_len); |
| 133 | int sc_pkcs1_strip_digest_info_prefix(unsigned int *algorithm, |
| 134 | const u8 *in_dat, size_t in_len, u8 *out_dat, size_t *out_len); |
| 135 | |
| 136 | /** |
| 137 | * PKCS1 encodes the given data. |
| 138 | * @param ctx IN sc_context_t object |
| 139 | * @param flags IN the algorithm to use |
| 140 | * @param in IN input buffer |
| 141 | * @param inlen IN length of the input |
| 142 | * @param out OUT output buffer (in == out is allowed) |
| 143 | * @param outlen OUT length of the output buffer |
| 144 | * @param modlen IN length of the modulus in bytes |
| 145 | * @return SC_SUCCESS on success and an error code otherwise |
| 146 | */ |
| 147 | int sc_pkcs1_encode(sc_context_t *ctx, unsigned long flags, |
| 148 | const u8 *in, size_t inlen, u8 *out, size_t *outlen, size_t modlen); |
| 149 | /** |
| 150 | * Get the necessary padding and sec. env. flags. |
| 151 | * @param ctx IN sc_contex_t object |
| 152 | * @param iflags IN the desired algorithms flags |
| 153 | * @param caps IN the card / key capabilities |
| 154 | * @param pflags OUT the padding flags to use |
| 155 | * @param salg OUT the security env. algorithm flag to use |
| 156 | * @return SC_SUCCESS on success and an error code otherwise |
| 157 | */ |
| 158 | int sc_get_encoding_flags(sc_context_t *ctx, |
| 159 | unsigned long iflags, unsigned long caps, |
| 160 | unsigned long *pflags, unsigned long *salg); |
| 161 | |
| 162 | /********************************************************************/ |
| 163 | /* mutex functions */ |
| 164 | /********************************************************************/ |
| 165 | |
| 166 | /** |
| 167 | * Creates a new sc_mutex object. Note: unless sc_mutex_set_mutex_funcs() |
| 168 | * this function does nothing and always returns SC_SUCCESS. |
| 169 | * @param ctx sc_context_t object with the thread context |
| 170 | * @param mutex pointer for the newly created mutex object |
| 171 | * @return SC_SUCCESS on success and an error code otherwise |
| 172 | */ |
| 173 | int sc_mutex_create(const sc_context_t *ctx, void **mutex); |
| 174 | /** |
| 175 | * Tries to acquire a lock for a sc_mutex object. Note: Unless |
| 176 | * sc_mutex_set_mutex_funcs() has been called before this |
| 177 | * function does nothing and always returns SUCCESS. |
| 178 | * @param ctx sc_context_t object with the thread context |
| 179 | * @param mutex mutex object to lock |
| 180 | * @return SC_SUCCESS on success and an error code otherwise |
| 181 | */ |
| 182 | int sc_mutex_lock(const sc_context_t *ctx, void *mutex); |
| 183 | /** |
| 184 | * Unlocks a sc_mutex object. Note: Unless sc_mutex_set_mutex_funcs() |
| 185 | * has been called before this function does nothing and always returns |
| 186 | * SC_SUCCESS. |
| 187 | * @param ctx sc_context_t object with the thread context |
| 188 | * @param mutex mutex object to unlock |
| 189 | * @return SC_SUCCESS on success and an error code otherwise |
| 190 | */ |
| 191 | int sc_mutex_unlock(const sc_context_t *ctx, void *mutex); |
| 192 | /** |
| 193 | * Destroys a sc_mutex object. Note: Unless sc_mutex_set_mutex_funcs() |
| 194 | * has been called before this function does nothing and always returns |
| 195 | * SC_SUCCESS. |
| 196 | * @param ctx sc_context_t object with the thread context |
| 197 | * @param mutex mutex object to be destroyed |
| 198 | * @return SC_SUCCESS on success and an error code otherwise |
| 199 | */ |
| 200 | int sc_mutex_destroy(const sc_context_t *ctx, void *mutex); |
| 201 | /** |
| 202 | * Returns a unique id for every thread. |
| 203 | * @param ctx sc_context_t object with the thread context |
| 204 | * @return unsigned long with the unique id or 0 if not supported |
| 205 | */ |
| 206 | unsigned long sc_thread_id(const sc_context_t *ctx); |
| 207 | |
| 208 | /********************************************************************/ |
| 209 | /* internal APDU handling functions */ |
| 210 | /********************************************************************/ |
| 211 | |
| 212 | /** |
| 213 | * Returns the encoded APDU in newly created buffer. |
| 214 | * @param ctx sc_context_t object |
| 215 | * @param apdu sc_apdu_t object with the APDU to encode |
| 216 | * @param buf pointer to the newly allocated buffer |
| 217 | * @param len length of the encoded APDU |
| 218 | * @param proto protocol to be used |
| 219 | * @return SC_SUCCESS on success and an error code otherwise |
| 220 | */ |
| 221 | int sc_apdu_get_octets(sc_context_t *ctx, const sc_apdu_t *apdu, u8 **buf, |
| 222 | size_t *len, unsigned int proto); |
| 223 | /** |
| 224 | * Sets the status bytes and return data in the APDU |
| 225 | * @param ctx sc_context_t object |
| 226 | * @param apdu the apdu to which the data should be written |
| 227 | * @param buf returned data |
| 228 | * @param len length of the returned data |
| 229 | * @return SC_SUCCESS on success and an error code otherwise |
| 230 | */ |
| 231 | int sc_apdu_set_resp(sc_context_t *ctx, sc_apdu_t *apdu, const u8 *buf, |
| 232 | size_t len); |
| 233 | /** |
| 234 | * Logs APDU |
| 235 | * @param ctx sc_context_t object |
| 236 | * @param buf buffer with the APDU data |
| 237 | * @param len length of the APDU |
| 238 | * @param is_outgoing != 0 if the data is send to the card |
| 239 | */ |
| 240 | void sc_apdu_log(sc_context_t *ctx, const u8 *data, size_t len, |
| 241 | int is_outgoing); |
| 242 | #ifdef __cplusplus |
| 243 | } |
| 244 | #endif |
| 245 | |
| 246 | #endif |
Note: See TracBrowser
for help on using the browser.
