opensc/src/libopensc/internal.h

249 lines
9.1 KiB
C
Raw Normal View History

/*
* internal.h: Internal definitions for libopensc
*
* Copyright (C) 2001, 2002 Juha Yrjölä <juha.yrjola@iki.fi>
* 2005 The OpenSC project
*
* 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,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* 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
*/
#ifndef _SC_INTERNAL_H
#define _SC_INTERNAL_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
#include "opensc.h"
#include "simclist.h"
#include "log.h"
#include "cards.h"
#include <assert.h>
Complete rewrite of OpenSC build system. 1. Build system now supports MinGW (Windows) compilation using msys and cross compilation. 2. Ability to explicitly disable and enable dependencies of the package. 3. openct, pcsc and nsplugins features are disabled by default. 4. Modified pcsc driver to use pcsc dynamically, no compile time dependency is required. 5. --enable-pcsc-lite configuration option renamed to --enable-pcsc. 6. Install opensc.conf file (as opensc.conf.new if opensc.conf exists). 7. Add--enable-doc configuration option, allow installing documentation into target. 8. Add --disable-man configuration option, allow msys mingw32 users to build from svn without extra dependencies. 9. Add export files to each library in order to export only required symbols. Windows native build may use these files instead of scanning objects' symbols. 10. Add opensc-tool --info to display some general information about the build. 11. Create compatibility library to be linked against library instread of recompiling the same source files in different places. 12. Add different win32 version resource to each class of outputs. 13. Make xsl-stylesheets location selectable. 14. Some win32 fixups. 15. Some warning fixups. 16. Many other autoconf/automake cleanups. Alon Bar-Lev svn diff -r 3315:3399 https://www.opensc-project.org/svn/opensc/branches/alonbl/mingw _M . D configure.in _M src _M src/openssh M src/openssh/Makefile.am _M src/tools M src/tools/rutoken-tool.c M src/tools/opensc-tool.c M src/tools/cardos-info.c M src/tools/pkcs15-crypt.c M src/tools/pkcs15-init.c M src/tools/piv-tool.c M src/tools/netkey-tool.c M src/tools/eidenv.c M src/tools/cryptoflex-tool.c M src/tools/util.c M src/tools/pkcs11-tool.c M src/tools/pkcs15-tool.c M src/tools/util.h M src/tools/opensc-explorer.c M src/tools/Makefile.am _M src/pkcs11 M src/pkcs11/pkcs11-global.c M src/pkcs11/framework-pkcs15.c M src/pkcs11/mechanism.c M src/pkcs11/pkcs11-display.c M src/pkcs11/pkcs11-object.c A src/pkcs11/opensc-pkcs11.exports M src/pkcs11/sc-pkcs11.h M src/pkcs11/pkcs11-spy.c M src/pkcs11/openssl.c M src/pkcs11/Makefile.am A src/pkcs11/pkcs11-spy.exports _M src/tests _M src/tests/regression M src/tests/regression/Makefile.am M src/tests/sc-test.c M src/tests/pintest.c M src/tests/Makefile.am _M src/include _M src/include/opensc M src/include/opensc/Makefile.am A src/include/opensc/svnignore M src/include/Makefile.am _M src/signer _M src/signer/npinclude M src/signer/npinclude/Makefile.am M src/signer/Makefile.am A src/signer/signer.exports _M src/common A src/common/compat_dummy.c D src/common/getopt.txt D src/common/strlcpy.c D src/common/LICENSE A src/common/compat_getopt.txt A src/common/compat_strlcpy.c A src/common/LICENSE.compat_getopt A src/common/compat_getopt.c D src/common/strlcpy.h D src/common/ChangeLog D src/common/getpass.c D src/common/my_getopt.c A src/common/compat_strlcpy.h A src/common/compat_getpass.c A src/common/compat_getopt.h A src/common/ChangeLog.compat_getopt D src/common/README.strlcpy D src/common/my_getopt.h A src/common/compat_getpass.h A src/common/README.compat_strlcpy D src/common/strlcpy.3 A src/common/README.compat_getopt D src/common/getopt.3 D src/common/README.my_getopt A src/common/compat_strlcpy.3 A src/common/compat_getopt.3 M src/common/Makefile.am M src/Makefile.am _M src/pkcs15init M src/pkcs15init/pkcs15-oberthur.c M src/pkcs15init/profile.c M src/pkcs15init/pkcs15-lib.c M src/pkcs15init/pkcs15-rutoken.c A src/pkcs15init/pkcs15init.exports M src/pkcs15init/pkcs15-gpk.c M src/pkcs15init/Makefile.am _M src/scconf M src/scconf/Makefile.am M src/scconf/parse.c A src/scconf/scconf.exports _M src/libopensc M src/libopensc/card-rutoken.c M src/libopensc/compression.c M src/libopensc/sc.c M src/libopensc/card-piv.c M src/libopensc/pkcs15-openpgp.c M src/libopensc/pkcs15-postecert.c M src/libopensc/pkcs15-tcos.c M src/libopensc/opensc-config.in M src/libopensc/reader-pcsc.c A src/libopensc/internal-winscard.h M src/libopensc/ctx.c A src/libopensc/libopensc.exports M src/libopensc/pkcs15-piv.c M src/libopensc/pkcs15-infocamere.c M src/libopensc/internal.h M src/libopensc/pkcs15-actalis.c M src/libopensc/pkcs15-starcert.c M src/libopensc/card-oberthur.c M src/libopensc/pkcs15-atrust-acos.c M src/libopensc/p15card-helper.c D src/libopensc/part10.h M src/libopensc/ui.c M src/libopensc/card-gpk.c M src/libopensc/pkcs15-wrap.c M src/libopensc/pkcs15-gemsafeGPK.c M src/libopensc/log.c M src/libopensc/pkcs15-esteid.c M src/libopensc/pkcs15-prkey-rutoken.c M src/libopensc/log.h M src/libopensc/Makefile.am M src/libopensc/reader-openct.c _M aclocal M aclocal/Makefile.am _M win32 M win32/Makefile.am A win32/versioninfo.rc.in A win32/ltrc.inc A configure.ac _M doc _M doc/tools M doc/tools/pkcs15-profile.xml D doc/changelog.sh D doc/export-wiki.xsl _M doc/api _M doc/api/file M doc/api/man.xsl _M doc/api/asn1 _M doc/api/apps _M doc/api/init _M doc/api/types _M doc/api/card M doc/api/html.xsl _M doc/api/misc _M doc/api/util M doc/Makefile.am D doc/export-wiki.sh AM doc/nonpersistent A doc/nonpersistent/export-wiki.xsl A doc/nonpersistent/Makefile.am A doc/nonpersistent/export-wiki.sh A doc/nonpersistent/svn2cl.xsl D doc/generate-man.sh D doc/svn2cl.xsl M Makefile.am A svnignore _M etc M etc/opensc.conf.in M etc/Makefile.am D man _M solaris M solaris/Makefile git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@3405 c6295689-39f2-0310-b995-f0e70906c6a9
2008-03-06 16:06:59 +00:00
#ifdef _WIN32
#include <windows.h>
#endif
#define SC_FILE_MAGIC 0x14426950
#define SC_CARD_MAGIC 0x27182818
#define SC_CTX_MAGIC 0x0A550335
#ifndef _WIN32
#define msleep(t) usleep((t) * 1000)
#else
#define msleep(t) Sleep(t)
#define sleep(t) Sleep((t) * 1000)
#endif
struct sc_atr_table {
/* The atr fields are required to
* be in aa:bb:cc hex format. */
char *atr;
/* The atrmask is logically AND'd with an
* card atr prior to comparison with the
* atr reference value above. */
char *atrmask;
char *name;
int type;
unsigned long flags;
/* Reference to card_atr configuration block,
* available to user configured card entries. */
scconf_block *card_atr;
};
/* Internal use only */
int _sc_add_reader(struct sc_context *ctx, struct sc_reader *reader);
int _sc_parse_atr(struct sc_reader *reader);
/* Add an ATR to the card driver's struct sc_atr_table */
int _sc_add_atr(struct sc_context *ctx, struct sc_card_driver *driver, struct sc_atr_table *src);
int _sc_free_atr(struct sc_context *ctx, struct sc_card_driver *driver);
/**
* Convert an unsigned long into 4 bytes in big endian order
* @param buf the byte array for the result, should be 4 bytes long
* @param x the value to be converted
*/
void ulong2bebytes(u8 *buf, unsigned long x);
/**
* Convert an unsigned long into 2 bytes in big endian order
* @param buf the byte array for the result, should be 2 bytes long
* @param x the value to be converted
*/
void ushort2bebytes(u8 *buf, unsigned short x);
/**
* Convert 4 bytes in big endian order into an unsigned long
* @param buf the byte array of 4 bytes
* @return the converted value
*/
unsigned long bebytes2ulong(const u8 *buf);
/**
* Convert 2 bytes in big endian order into an unsigned short
* @param buf the byte array of 2 bytes
* @return the converted value
*/
unsigned short bebytes2ushort(const u8 *buf);
/* Returns an scconf_block entry with matching ATR/ATRmask to the ATR specified,
* NULL otherwise. Additionally, if card driver is not specified, search through
* all card drivers user configured ATRs. */
scconf_block *_sc_match_atr_block(sc_context_t *ctx, struct sc_card_driver *driver, u8 *atr, size_t atr_len);
/* Returns an index number if a match was found, -1 otherwise. table has to
* be null terminated. */
int _sc_match_atr(struct sc_card *card, struct sc_atr_table *table, int *type_out);
int _sc_check_forced_protocol(struct sc_context *ctx, u8 *atr, size_t atr_len, unsigned int *protocol);
int _sc_card_add_algorithm(struct sc_card *card, const struct sc_algorithm_info *info);
int _sc_card_add_rsa_alg(struct sc_card *card, unsigned int key_length,
unsigned long flags, unsigned long exponent);
struct sc_algorithm_info * _sc_card_find_rsa_alg(struct sc_card *card,
unsigned int key_length);
int sc_asn1_read_tag(const u8 ** buf, size_t buflen, unsigned int *cla_out,
unsigned int *tag_out, size_t *taglen);
/********************************************************************/
/* pkcs1 padding/encoding functions */
/********************************************************************/
int sc_pkcs1_strip_01_padding(const u8 *in_dat, size_t in_len, u8 *out_dat,
size_t *out_len);
int sc_pkcs1_strip_02_padding(const u8 *data, size_t len, u8 *out_dat,
size_t *out_len);
int sc_pkcs1_strip_digest_info_prefix(unsigned int *algorithm,
const u8 *in_dat, size_t in_len, u8 *out_dat, size_t *out_len);
/**
* PKCS1 encodes the given data.
* @param ctx IN sc_context_t object
* @param flags IN the algorithm to use
* @param in IN input buffer
* @param inlen IN length of the input
* @param out OUT output buffer (in == out is allowed)
* @param outlen OUT length of the output buffer
* @param modlen IN length of the modulus in bytes
* @return SC_SUCCESS on success and an error code otherwise
*/
int sc_pkcs1_encode(sc_context_t *ctx, unsigned long flags,
const u8 *in, size_t inlen, u8 *out, size_t *outlen, size_t modlen);
/**
* Get the necessary padding and sec. env. flags.
* @param ctx IN sc_contex_t object
* @param iflags IN the desired algorithms flags
* @param caps IN the card / key capabilities
* @param pflags OUT the padding flags to use
* @param salg OUT the security env. algorithm flag to use
* @return SC_SUCCESS on success and an error code otherwise
*/
int sc_get_encoding_flags(sc_context_t *ctx,
unsigned long iflags, unsigned long caps,
unsigned long *pflags, unsigned long *salg);
/********************************************************************/
/* mutex functions */
/********************************************************************/
/**
* Creates a new sc_mutex object. Note: unless sc_mutex_set_mutex_funcs()
* this function does nothing and always returns SC_SUCCESS.
* @param ctx sc_context_t object with the thread context
* @param mutex pointer for the newly created mutex object
* @return SC_SUCCESS on success and an error code otherwise
*/
int sc_mutex_create(const sc_context_t *ctx, void **mutex);
/**
* Tries to acquire a lock for a sc_mutex object. Note: Unless
* sc_mutex_set_mutex_funcs() has been called before this
* function does nothing and always returns SUCCESS.
* @param ctx sc_context_t object with the thread context
* @param mutex mutex object to lock
* @return SC_SUCCESS on success and an error code otherwise
*/
int sc_mutex_lock(const sc_context_t *ctx, void *mutex);
/**
* Unlocks a sc_mutex object. Note: Unless sc_mutex_set_mutex_funcs()
* has been called before this function does nothing and always returns
* SC_SUCCESS.
* @param ctx sc_context_t object with the thread context
* @param mutex mutex object to unlock
* @return SC_SUCCESS on success and an error code otherwise
*/
int sc_mutex_unlock(const sc_context_t *ctx, void *mutex);
/**
* Destroys a sc_mutex object. Note: Unless sc_mutex_set_mutex_funcs()
* has been called before this function does nothing and always returns
* SC_SUCCESS.
* @param ctx sc_context_t object with the thread context
* @param mutex mutex object to be destroyed
* @return SC_SUCCESS on success and an error code otherwise
*/
int sc_mutex_destroy(const sc_context_t *ctx, void *mutex);
/**
* Returns a unique id for every thread.
* @param ctx sc_context_t object with the thread context
* @return unsigned long with the unique id or 0 if not supported
*/
unsigned long sc_thread_id(const sc_context_t *ctx);
/********************************************************************/
/* internal APDU handling functions */
/********************************************************************/
/**
* Returns the encoded APDU in newly created buffer.
* @param ctx sc_context_t object
* @param apdu sc_apdu_t object with the APDU to encode
* @param buf pointer to the newly allocated buffer
* @param len length of the encoded APDU
* @param proto protocol to be used
* @return SC_SUCCESS on success and an error code otherwise
*/
int sc_apdu_get_octets(sc_context_t *ctx, const sc_apdu_t *apdu, u8 **buf,
size_t *len, unsigned int proto);
/**
* Sets the status bytes and return data in the APDU
* @param ctx sc_context_t object
* @param apdu the apdu to which the data should be written
* @param buf returned data
* @param len length of the returned data
* @return SC_SUCCESS on success and an error code otherwise
*/
int sc_apdu_set_resp(sc_context_t *ctx, sc_apdu_t *apdu, const u8 *buf,
size_t len);
/**
* Logs APDU
* @param ctx sc_context_t object
* @param buf buffer with the APDU data
* @param len length of the APDU
* @param is_outgoing != 0 if the data is send to the card
*/
void sc_apdu_log(sc_context_t *ctx, const u8 *data, size_t len,
int is_outgoing);
extern struct sc_reader_driver *sc_get_pcsc_driver(void);
extern struct sc_reader_driver *sc_get_ctapi_driver(void);
extern struct sc_reader_driver *sc_get_openct_driver(void);
#ifdef __cplusplus
}
#endif
#endif