- Started to add configuration file support to

libopensc
- Added typedefs for some basic structs (e.g.
  struct sc_card --> sc_card_t)
- Added a second argument to sc_establish_context()
  to identify the calling application
- Renamed sc_destroy_context() to sc_release_context()


git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@378 c6295689-39f2-0310-b995-f0e70906c6a9
This commit is contained in:
jey 2002-03-24 14:12:38 +00:00
parent ffe0282a93
commit f21926af00
23 changed files with 153 additions and 50 deletions

View File

@ -935,6 +935,10 @@ AM_CONDITIONAL(HAVE_SCIDI, test "foo" = "bar")
scldap_etc_path=`eval echo ${sysconfdir}` ; scldap_etc_path=`eval echo ${scldap_etc_path}`
AC_DEFINE_UNQUOTED(SCLDAP_ETC_PATH_CUSTOM, "$scldap_etc_path", [etc path for scldap])
opensc_etc_path=`eval echo ${sysconfdir}` ; opensc_etc_path=`eval echo ${opensc_etc_path}`
AC_DEFINE_UNQUOTED(OPENSC_ETC_PATH, "$opensc_etc_path", [etc path for libopensc])
opensc_conf_path=`eval echo ${opensc_etc_path}/opensc.conf`
AC_DEFINE_UNQUOTED(OPENSC_CONF_PATH, "$opensc_conf_path", [default config file for libopensc])
AC_SUBST(CFLAGS_OPENSC)
CFLAGS_OPENSC="-I\${top_srcdir}/src/libopensc"

11
etc/opensc.conf.example Normal file
View File

@ -0,0 +1,11 @@
# Configuration file for OpenSC
# Example configuration file
opensc defaults {
debuglevel = 0
# debugfile = /tmp/opensc-debug.log
# errorfile = /tmp/opensc-errors.log
}
opensc pam_pkcs15 {
}

View File

@ -12,7 +12,7 @@ PCSC_FLAGS = @CFLAGS_PCSC@
PCSC_LIB = @LIBPCSC@
endif
INCLUDES = $(PCSC_FLAGS)
INCLUDES = $(PCSC_FLAGS) -I../scconf
bin_SCRIPTS = opensc-config
@ -24,7 +24,7 @@ libopensc_la_SOURCES = asn1.c base64.c sec.c log.c sc.c card.c iso7816.c \
card-setcos.c card-miocos.c card-flex.c card-gpk.c \
card-tcos.c card-emv.c card-default.c
libopensc_la_LDFLAGS = -version-info 0:6:0
libopensc_la_LIBADD = $(PCSC_LIB) $(SSL_LIB)
libopensc_la_LIBADD = $(PCSC_LIB) $(SSL_LIB) $(LIBSCCONF)
include_HEADERS = opensc.h opensc-pkcs15.h opensc-emv.h \
cardctl.h

View File

@ -26,9 +26,11 @@
#endif
#include "opensc.h"
#include <assert.h>
#define SC_FILE_MAGIC 0x14426950
#define SC_CARD_MAGIC 0x27182818
#define SC_CTX_MAGIC 0x0A550335
struct sc_atr_table {
const u8 *atr;

View File

@ -26,12 +26,14 @@
#ifndef _OPENSC_H
#define _OPENSC_H
#include <pthread.h>
#ifndef NDEBUG
#include <assert.h>
#endif
#include <stdio.h>
#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif
#include <scconf.h>
#ifdef __cplusplus
extern "C" {
#endif
@ -192,6 +194,7 @@ struct sc_acl_entry {
struct sc_acl_entry *next;
};
typedef struct sc_acl_entry sc_acl_entry_t;
struct sc_file {
struct sc_path path;
@ -213,6 +216,7 @@ struct sc_file {
size_t prop_attr_len;
unsigned int magic;
};
typedef struct sc_file sc_file_t;
#define SC_SEC_OPERATION_DECIPHER 0x0001
#define SC_SEC_OPERATION_SIGN 0x0002
@ -263,6 +267,7 @@ struct sc_security_env {
u8 key_ref[8];
size_t key_ref_len;
};
typedef struct sc_security_env sc_security_env_t;
struct sc_algorithm_id {
unsigned int algorithm;
@ -280,6 +285,7 @@ struct sc_algorithm_info {
} _rsa;
} u;
};
typedef struct sc_algorithm_info sc_algorithm_info_t;
struct sc_app_info {
u8 aid[SC_MAX_AID_SIZE];
@ -292,12 +298,12 @@ struct sc_app_info {
const char *desc; /* App description, if known */
int rec_nr; /* -1, if EF(DIR) is transparent */
};
typedef struct sc_app_info sc_app_info_t;
struct sc_card_cache {
struct sc_path current_path;
};
#define SC_PROTO_T0 0x00000001
#define SC_PROTO_T1 0x00000002
#define SC_PROTO_RAW 0x00001000
@ -320,6 +326,7 @@ struct sc_slot_info {
void *drv_data;
};
typedef struct sc_slot_info sc_slot_info_t;
struct sc_event_listener {
unsigned int event_mask;
@ -404,7 +411,6 @@ struct sc_card {
struct sc_algorithm_info *algorithms;
int algorithm_count;
pthread_mutex_t mutex;
int lock_count;
const struct sc_card_driver *driver;
@ -414,8 +420,12 @@ struct sc_card {
struct sc_card_cache cache;
int cache_valid;
#ifdef HAVE_PTHREAD
pthread_mutex_t mutex;
#endif
unsigned int magic;
};
typedef struct sc_card sc_card_t;
struct sc_card_operations {
/* Called in sc_connect_card(). Must return 1, if the current
@ -522,6 +532,8 @@ struct sc_card_driver {
};
struct sc_context {
scconf_context *conf;
char *app_name;
int debug;
FILE *debug_file, *error_file;
@ -535,8 +547,13 @@ struct sc_context {
const struct sc_card_driver *card_drivers[SC_MAX_CARD_DRIVERS+1];
const struct sc_card_driver *forced_driver;
#ifdef HAVE_PTHREAD
pthread_mutex_t mutex;
#endif
unsigned int magic;
};
typedef struct sc_context sc_context_t;
struct sc_apdu {
int cse; /* APDU case */
@ -550,6 +567,7 @@ struct sc_apdu {
unsigned int sw1, sw2; /* Status words returned in R-APDU */
};
typedef struct sc_apdu sc_apdu_t;
/* Base64 encoding/decoding functions */
int sc_base64_encode(const u8 *in, size_t inlen, u8 *out, size_t outlen,
@ -564,13 +582,16 @@ void sc_format_apdu(struct sc_card *card, struct sc_apdu *apdu, int cse, int ins
/**
* Establishes an OpenSC context
* @param ctx A pointer to a pointer that will receive the allocated context
* @param app_name A string that identifies the application, used primarily
* in finding application-specific configuration data. Can be NULL.
*/
int sc_establish_context(struct sc_context **ctx);
int sc_establish_context(struct sc_context **ctx, const char *app_name);
/**
* Destroys an established OpenSC context
* @param ctx A pointer to the context structure to be destroyed
* Releases an established OpenSC context
* @param ctx A pointer to the context structure to be released
*/
int sc_destroy_context(struct sc_context *ctx);
int sc_release_context(struct sc_context *ctx);
/**
* Forces the use of a specified card driver
* @param ctx OpenSC context

View File

@ -26,9 +26,11 @@
#endif
#include "opensc.h"
#include <assert.h>
#define SC_FILE_MAGIC 0x14426950
#define SC_CARD_MAGIC 0x27182818
#define SC_CTX_MAGIC 0x0A550335
struct sc_atr_table {
const u8 *atr;

View File

@ -142,20 +142,60 @@ int sc_wait_for_card(struct sc_context *ctx, int reader, int timeout)
}
#endif
int sc_establish_context(struct sc_context **ctx_out)
static void set_defaults(struct sc_context *ctx)
{
ctx->debug = 0;
if (ctx->debug_file)
fclose(ctx->debug_file);
ctx->debug_file = NULL;
ctx->log_errors = 1;
ctx->error_file = stderr;
}
static int load_parameters(struct sc_context *ctx, scconf_block *block)
{
const char *val;
val = scconf_find_value_first(block, "debuglevel");
sscanf(val, "%d", &ctx->debug);
val = scconf_find_value_first(block, "debugfile");
if (ctx->debug_file)
fclose(ctx->debug_file);
if (strcmp(val, "stdout") == 0)
ctx->debug_file = fopen(val, "a");
val = scconf_find_value_first(block, "errorfile");
if (ctx->error_file)
fclose(ctx->error_file);
if (strcmp(val, "stderr") != 0)
ctx->error_file = fopen(val, "a");
return 0;
}
int sc_establish_context(struct sc_context **ctx_out, const char *app_name)
{
struct sc_context *ctx;
int i;
int i, r;
assert(ctx_out != NULL);
ctx = malloc(sizeof(struct sc_context));
if (ctx == NULL)
return SC_ERROR_OUT_OF_MEMORY;
memset(ctx, 0, sizeof(struct sc_context));
ctx->log_errors = 1;
set_defaults(ctx);
ctx->app_name = strdup(app_name);
ctx->conf = scconf_init(OPENSC_CONF_PATH);
if (ctx->conf != NULL) {
r = scconf_parse(ctx->conf);
if (scconf_parse(ctx->conf) < 1) {
scconf_deinit(ctx->conf);
ctx->conf = NULL;
} else
load_parameters(ctx, ctx->conf->root);
}
#ifdef HAVE_PTHREAD
pthread_mutex_init(&ctx->mutex, NULL);
#endif
for (i = 0; i < SC_MAX_READER_DRIVERS+1; i++)
ctx->reader_drivers[i] = NULL;
i = 0;
@ -199,7 +239,7 @@ int sc_establish_context(struct sc_context **ctx_out)
return 0;
}
int sc_destroy_context(struct sc_context *ctx)
int sc_release_context(struct sc_context *ctx)
{
int i;
@ -220,6 +260,9 @@ int sc_destroy_context(struct sc_context *ctx)
drv->ops->finish(ctx->reader_drv_data[i]);
}
ctx->debug_file = ctx->error_file = NULL;
if (ctx->conf)
scconf_deinit(ctx->conf);
free(ctx->app_name);
free(ctx);
return 0;
}
@ -228,7 +271,9 @@ int sc_set_card_driver(struct sc_context *ctx, const char *short_name)
{
int i = 0, match = 0;
#ifdef HAVE_PTHREAD
pthread_mutex_lock(&ctx->mutex);
#endif
if (short_name == NULL) {
ctx->forced_driver = NULL;
match = 1;
@ -242,7 +287,9 @@ int sc_set_card_driver(struct sc_context *ctx, const char *short_name)
}
i++;
}
#ifdef HAVE_PTHREAD
pthread_mutex_unlock(&ctx->mutex);
#endif
if (match == 0)
return SC_ERROR_OBJECT_NOT_FOUND; /* FIXME: invent error */
return 0;

View File

@ -281,7 +281,7 @@ int main(int argc, char *const argv[])
}
if (action_count == 0)
print_usage_and_die();
r = sc_establish_context(&ctx);
r = sc_establish_context(&ctx, "opensc-ssh");
if (r) {
fprintf(stderr, "Failed to establish context: %s\n", sc_strerror(r));
return 1;
@ -329,6 +329,6 @@ end:
sc_disconnect_card(card, 0);
}
if (ctx)
sc_destroy_context(ctx);
sc_release_context(ctx);
return err;
}

View File

@ -80,7 +80,7 @@ CK_RV C_Finalize(CK_VOID_PTR pReserved)
for (i=0; i<context->reader_count; i++)
card_removed(i);
sc_destroy_context(context);
sc_release_context(context);
return CKR_OK;
}

View File

@ -122,7 +122,7 @@ int p15_eid_init(int argc, const char **argv)
if (ctx) {
return SCAM_FAILED;
}
r = sc_establish_context(&ctx);
r = sc_establish_context(&ctx, "scam");
if (r != SC_SUCCESS) {
scam_fw_p15_eid.printmsg("sc_establish_context: %s\n", sc_strerror(r));
return SCAM_FAILED;
@ -372,7 +372,7 @@ void p15_eid_deinit(void)
}
card = NULL;
if (ctx) {
sc_destroy_context(ctx);
sc_release_context(ctx);
}
ctx = NULL;
}

View File

@ -122,7 +122,7 @@ int p15_ldap_init(int argc, const char **argv)
if (ctx || lctx) {
return SCAM_FAILED;
}
r = sc_establish_context(&ctx);
r = sc_establish_context(&ctx, "scam");
if (r != SC_SUCCESS) {
scam_fw_p15_ldap.printmsg("sc_establish_context: %s\n", sc_strerror(r));
return SCAM_FAILED;
@ -312,7 +312,7 @@ void p15_ldap_deinit(void)
}
card = NULL;
if (ctx) {
sc_destroy_context(ctx);
sc_release_context(ctx);
}
ctx = NULL;
}

View File

@ -70,16 +70,16 @@ const char *scam_get_atr(unsigned int readernum)
return NULL;
}
if (readernum >= ctx->reader_count || readernum < 0) {
sc_destroy_context(ctx);
sc_release_context(ctx);
return NULL;
}
if (sc_detect_card_presence(ctx->reader[readernum], 0) != 1) {
sc_destroy_context(ctx);
sc_release_context(ctx);
return NULL;
}
r = sc_connect_card(ctx->reader[readernum], 0, &card);
if (r) {
sc_destroy_context(ctx);
sc_release_context(ctx);
return NULL;
}
for (i = 0; i < card->atr_len; i++) {
@ -103,7 +103,7 @@ const char *scam_get_atr(unsigned int readernum)
}
atr[c] = 0;
sc_disconnect_card(card, 0);
sc_destroy_context(ctx);
sc_release_context(ctx);
return &atr[0];
}

View File

@ -16,7 +16,7 @@ int sc_test_init(int *argc, char *argv[])
int i, c;
printf("Using libopensc version %s.\n", sc_version);
i = sc_establish_context(&ctx);
i = sc_establish_context(&ctx, "tests");
if (i < 0) {
printf("sc_establish_context() failed (%d)\n", i);
return i;
@ -70,5 +70,5 @@ int sc_test_init(int *argc, char *argv[])
void sc_test_cleanup(void)
{
sc_disconnect_card(card, 0);
sc_destroy_context(ctx);
sc_release_context(ctx);
}

View File

@ -29,6 +29,8 @@
#include <openssl/pem.h>
#include <openssl/err.h>
const char *app_name = "cryptoflex-tool";
int opt_reader = 0, opt_debug = 0;
int opt_key_num = 1, opt_pin_num = -1;
int quiet = 0;
@ -1162,8 +1164,8 @@ int main(int argc, char * const argv[])
}
}
if (action_count == 0)
print_usage_and_die("cryptoflex-tool");
r = sc_establish_context(&ctx);
print_usage_and_die();
r = sc_establish_context(&ctx, app_name);
if (r) {
fprintf(stderr, "Failed to establish context: %s\n", sc_strerror(r));
return 1;
@ -1236,6 +1238,6 @@ end:
sc_disconnect_card(card, 0);
}
if (ctx)
sc_destroy_context(ctx);
sc_release_context(ctx);
return err;
}

View File

@ -33,6 +33,8 @@
#define DIM(v) (sizeof(v)/sizeof((v)[0]))
const char *app_name = "opensc-explorer";
int opt_reader = 0, opt_debug = 0;
const char *opt_driver = NULL;
@ -81,7 +83,7 @@ void die(int ret)
sc_disconnect_card(card, 0);
}
if (ctx)
sc_destroy_context(ctx);
sc_release_context(ctx);
exit(ret);
}
@ -1154,7 +1156,7 @@ int main(int argc, char * const argv[])
if (c == -1)
break;
if (c == '?')
print_usage_and_die("opensc-explorer");
print_usage_and_die();
switch (c) {
case 'r':
opt_reader = atoi(optarg);
@ -1168,7 +1170,7 @@ int main(int argc, char * const argv[])
}
}
r = sc_establish_context(&ctx);
r = sc_establish_context(&ctx, app_name);
if (r) {
fprintf(stderr, "Failed to establish context: %s\n", sc_strerror(r));
return 1;

View File

@ -38,6 +38,8 @@
#define OPT_PIN_ID 0x103
#define OPT_NO_CACHE 0x104
const char *app_name = "opensc-tool";
int opt_reader = 0, opt_no_cache = 0, opt_debug = 0;
char * opt_apdus[8];
int opt_apdu_count = 0;
@ -356,7 +358,7 @@ int main(int argc, char * const argv[])
if (c == -1)
break;
if (c == '?')
print_usage_and_die("opensc-tool");
print_usage_and_die();
switch (c) {
case 'l':
do_list_readers = 1;
@ -400,8 +402,8 @@ int main(int argc, char * const argv[])
}
}
if (action_count == 0)
print_usage_and_die("opensc-tool");
r = sc_establish_context(&ctx);
print_usage_and_die();
r = sc_establish_context(&ctx, app_name);
if (r) {
fprintf(stderr, "Failed to establish context: %s\n", sc_strerror(r));
return 1;
@ -481,6 +483,6 @@ end:
sc_disconnect_card(card, 0);
}
if (ctx)
sc_destroy_context(ctx);
sc_release_context(ctx);
return err;
}

View File

@ -31,6 +31,8 @@
#include <opensc.h>
#include <opensc-pkcs15.h>
const char *app_name = "pkcs15-crypt";
int opt_reader = 0, quiet = 0;
int opt_debug = 0;
char * opt_pincode = NULL, * opt_key_id = NULL;
@ -256,7 +258,7 @@ int main(int argc, char * const argv[])
}
if (action_count == 0)
print_usage_and_die("pkcs15-crypt");
r = sc_establish_context(&ctx);
r = sc_establish_context(&ctx, app_name);
if (r) {
fprintf(stderr, "Failed to establish context: %s\n", sc_strerror(r));
return 1;
@ -362,6 +364,6 @@ end:
sc_disconnect_card(card, 0);
}
if (ctx)
sc_destroy_context(ctx);
sc_release_context(ctx);
return err;
}

View File

@ -23,6 +23,7 @@
#endif
#include <sys/types.h>
#include <string.h>
#include <assert.h>
#include <openssl/bn.h>
#include "opensc.h"
#include "cardctl.h"

View File

@ -35,6 +35,7 @@
#include <stdio.h>
#include <ctype.h>
#include <stdarg.h>
#include <assert.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/err.h>
@ -46,6 +47,8 @@
#include "profile.h"
#include "pkcs15-init.h"
const char *app_name = "pkcs15-init";
/* Handle encoding of PKCS15 on the card */
typedef int (*pkcs15_encoder)(struct sc_context *,
struct sc_pkcs15_card *, u8 **, size_t *);
@ -278,7 +281,7 @@ done: if (card) {
sc_unlock(card);
sc_disconnect_card(card, 0);
}
sc_destroy_context(ctx);
sc_release_context(ctx);
return r? 1 : 0;
}
@ -303,7 +306,7 @@ connect(int reader)
{
int r;
r = sc_establish_context(&ctx);
r = sc_establish_context(&ctx, app_name);
if (r) {
error("Failed to establish context: %s\n", sc_strerror(r));
return 0;

View File

@ -24,6 +24,8 @@
#include "util.h"
#include <opensc-pkcs15.h>
const char *app_name = "pkcs15-tool";
int opt_reader = 0, opt_debug = 0;
int opt_no_cache = 0;
char * opt_pin_id;
@ -696,7 +698,7 @@ int main(int argc, char * const argv[])
}
if (action_count == 0)
print_usage_and_die("pkcs15-tool");
r = sc_establish_context(&ctx);
r = sc_establish_context(&ctx, app_name);
if (r) {
fprintf(stderr, "Failed to establish context: %s\n", sc_strerror(r));
return 1;
@ -794,6 +796,6 @@ end:
sc_disconnect_card(card, 0);
}
if (ctx)
sc_destroy_context(ctx);
sc_release_context(ctx);
return err;
}

View File

@ -24,6 +24,7 @@
#include <stdio.h>
#include <ctype.h>
#include <stdarg.h>
#include <assert.h>
#include "util.h"
#include "profile.h"

View File

@ -63,10 +63,10 @@ void hex_dump_asc(FILE *f, const u8 *in, size_t count, int addr)
}
}
void print_usage_and_die(const char *pgmname)
void print_usage_and_die()
{
int i = 0;
printf("Usage: %s [OPTIONS]\nOptions:\n", pgmname);
printf("Usage: %s [OPTIONS]\nOptions:\n", app_name);
while (options[i].name) {
char buf[40], tmp[5];

View File

@ -18,11 +18,12 @@
extern const struct option options[];
extern const char *option_help[];
extern const char *app_name;
void print_binary(FILE *f, const u8 *buf, int count);
void hex_dump(FILE *f, const u8 *in, int len, const char *sep);
void hex_dump_asc(FILE *f, const u8 *in, size_t count, int addr);
void print_usage_and_die(const char *pgmname);
void print_usage_and_die();
const char * acl_to_str(const struct sc_acl_entry *e);
void warn(const char *fmt, ...);
void error(const char *fmt, ...);