- paving way for dynamic card modules

- fixed a few memory leaks


git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@86 c6295689-39f2-0310-b995-f0e70906c6a9
This commit is contained in:
jey 2001-12-20 13:57:58 +00:00
parent ff560ba239
commit 41416437e5
8 changed files with 75 additions and 16 deletions

View File

@ -1,5 +1,6 @@
## Makefile.am -- Process this file with automake to produce Makefile.in
LD = ccmalloc gcc
lib_LTLIBRARIES = libopensc.la
libopensc_la_SOURCES = sc-asn1.c sc-base64.c sc-defaults.c \

View File

@ -630,9 +630,9 @@ static int asn1_parse(struct sc_context *ctx, struct sc_asn1_struct *asn1,
if (ctx->debug > 2)
debug(ctx, "called, depth %d%s\n", depth, choice ? ", choice" : "");
if (left < 2)
SC_FUNC_RETURN(ctx, SC_ERROR_ASN1_END_OF_CONTENTS);
return SC_ERROR_ASN1_END_OF_CONTENTS;
if (p[0] == 0 && p[1] == 0)
SC_FUNC_RETURN(ctx, SC_ERROR_ASN1_END_OF_CONTENTS);
return SC_ERROR_ASN1_END_OF_CONTENTS;
for (idx = 0; asn1[idx].name != NULL; idx++) {
entry = &asn1[idx];
r = 0;

View File

@ -106,6 +106,7 @@ extern "C" {
#define SC_ASN1_MAX_OBJECT_ID_OCTETS 16
typedef unsigned char u8;
typedef unsigned int uint32;
struct sc_object_id {
int value[SC_ASN1_MAX_OBJECT_ID_OCTETS];
@ -131,6 +132,59 @@ struct sc_file {
unsigned int magic;
};
struct sc_security_env {
int algorithm_ref;
struct sc_path key_file_id;
/* operation=1 ==> digital signing, signature=0 ==> decipher */
int operation;
int key_ref;
};
struct sc_card;
struct sc_card_operations {
int (*init)(struct sc_card *card);
int (*finish)(struct sc_card *card);
int (*read_binary)(struct sc_card *card, uint32 idx,
u8 * buf, size_t count);
int (*write_binary)(struct sc_card *card, uint32 idx,
const u8 * buf, size_t count);
int (*update_binary)(struct sc_card *card, uint32 idx,
const u8 * buf, size_t count);
int (*erase_binary)(struct sc_card *card, uint32 idx,
size_t count);
int (*read_binary_large)(struct sc_card *card, uint32 idx,
u8 * buf, size_t count);
int (*write_binary_large)(struct sc_card *card, uint32 idx,
const u8 * buf, size_t count);
int (*update_binary_large)(struct sc_card *card, uint32 idx,
const u8 * buf, size_t count);
/* possibly TODO: record handling */
int (*select_file)(struct sc_card *card, struct sc_file *file,
const struct sc_path *path, int selection_type);
int (*get_response)(struct sc_card *card, u8 * buf, size_t count);
int (*get_challenge)(struct sc_card *card, u8 * buf, size_t count);
/* ISO 7816-8 */
int (*verify)(struct sc_card *card, int ref_qualifier,
const u8 *data, size_t data_len, int *tries_left);
int (*restore_security_env)(struct sc_card *card, int se_num);
int (*set_security_env)(struct sc_card *card,
const struct sc_security_env *env);
int (*decipher)(struct sc_card *card, const u8 * crgram,
size_t crgram_len, u8 * out, size_t outlen);
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);
};
struct sc_card {
int cla;
struct sc_context *ctx;
@ -141,6 +195,7 @@ struct sc_card {
int atr_len;
pthread_mutex_t mutex;
struct sc_card_operations *ops;
};
struct sc_context {
@ -166,13 +221,6 @@ struct sc_apdu {
int sw1, sw2;
};
struct sc_security_env {
int algorithm_ref;
struct sc_path key_file_id;
/* signature=1 ==> digital signing, signature=0 ==> authentication */
int signature;
int key_ref;
};
struct sc_defaults {
const char *atr;

View File

@ -367,6 +367,7 @@ void sc_pkcs15_free_certificate(struct sc_pkcs15_cert *cert)
assert(cert != NULL);
free(cert->key.data);
free(cert->key.modulus);
free(cert->data);
free(cert);
}

View File

@ -37,13 +37,16 @@ int sc_pkcs15_decipher(struct sc_pkcs15_card *p15card,
senv.algorithm_ref = 0x02;
senv.key_file_id = prkey->file_id;
senv.signature = 0;
senv.operation = 0;
senv.key_ref = prkey->key_reference;
SC_FUNC_CALLED(ctx);
r = sc_select_file(p15card->card, &p15card->file_app,
&p15card->file_app.path, SC_SELECT_FILE_BY_PATH);
SC_TEST_RET(ctx, r, "sc_select_file() failed");
r = sc_select_file(p15card->card, &p15card->file_app,
&p15card->file_app.path, SC_SELECT_FILE_BY_PATH);
SC_TEST_RET(ctx, r, "sc_select_file() failed");
r = sc_restore_security_env(p15card->card, 0); /* empty SE */
SC_TEST_RET(ctx, r, "sc_restore_security_env() failed");
r = sc_set_security_env(p15card->card, &senv);
@ -72,7 +75,7 @@ int sc_pkcs15_compute_signature(struct sc_pkcs15_card *p15card,
break;
}
senv.key_file_id = prkey->file_id;
senv.signature = 1;
senv.operation = 1;
senv.key_ref = prkey->key_reference;
SC_FUNC_CALLED(ctx);

View File

@ -95,10 +95,12 @@ void parse_tokeninfo(struct sc_pkcs15_card *card, const u8 * buf, int buflen)
sprintf(byte, "%02X", serial[i]);
strcat(card->serial_number, byte);
}
if (asn1_tokeninfo[2].flags & SC_ASN1_PRESENT)
card->manufacturer_id = strdup(mnfid);
else
card->manufacturer_id = strdup("(unknown)");
if (card->manufacturer_id == NULL) {
if (asn1_tokeninfo[2].flags & SC_ASN1_PRESENT)
card->manufacturer_id = strdup(mnfid);
else
card->manufacturer_id = strdup("(unknown)");
}
return;
err:
if (card->serial_number == NULL)
@ -325,6 +327,8 @@ error:
int sc_pkcs15_destroy(struct sc_pkcs15_card *p15card)
{
free(p15card->label);
free(p15card->serial_number);
free(p15card->manufacturer_id);
free(p15card);
return 0;
}

View File

@ -514,6 +514,8 @@ int sc_disconnect_card(struct sc_card *card)
assert(card != NULL);
SCardDisconnect(card->pcsc_card, SCARD_LEAVE_CARD);
pthread_mutex_destroy(&card->mutex);
free(card);
return 0;
}

View File

@ -35,7 +35,7 @@ int sc_set_security_env(struct sc_card *card,
assert(card != NULL && env != NULL);
SC_FUNC_CALLED(card->ctx);
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x22, 0, 0);
if (env->signature) {
if (env->operation == 1) {
apdu.p1 = 0x81;
apdu.p2 = 0xB6;
} else {