2001-12-22 20:43:09 +00:00
|
|
|
/*
|
2005-06-16 19:35:31 +00:00
|
|
|
* card.c: General smart card functions
|
2001-12-22 20:43:09 +00:00
|
|
|
*
|
2006-12-19 21:32:31 +00:00
|
|
|
* Copyright (C) 2001, 2002 Juha Yrjölä <juha.yrjola@iki.fi>
|
2001-12-22 20:43:09 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2002-04-05 10:44:51 +00:00
|
|
|
#include "internal.h"
|
|
|
|
#include "asn1.h"
|
2001-12-22 20:43:09 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
2005-02-04 15:57:38 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2002-03-21 09:36:38 +00:00
|
|
|
#include <string.h>
|
2001-12-22 20:43:09 +00:00
|
|
|
|
2005-08-05 17:18:10 +00:00
|
|
|
int sc_check_sw(sc_card_t *card, unsigned int sw1, unsigned int sw2)
|
2001-12-22 20:43:09 +00:00
|
|
|
{
|
2005-08-05 17:18:10 +00:00
|
|
|
if (card == NULL)
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
if (card->ops->check_sw == NULL)
|
|
|
|
return SC_ERROR_NOT_SUPPORTED;
|
2002-02-15 23:17:58 +00:00
|
|
|
return card->ops->check_sw(card, sw1, sw2);
|
2001-12-22 20:43:09 +00:00
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
void sc_format_apdu(sc_card_t *card, sc_apdu_t *apdu,
|
2002-01-10 23:02:48 +00:00
|
|
|
int cse, int ins, int p1, int p2)
|
2001-12-22 20:43:09 +00:00
|
|
|
{
|
|
|
|
assert(card != NULL && apdu != NULL);
|
|
|
|
memset(apdu, 0, sizeof(*apdu));
|
|
|
|
apdu->cla = (u8) card->cla;
|
|
|
|
apdu->cse = cse;
|
|
|
|
apdu->ins = (u8) ins;
|
|
|
|
apdu->p1 = (u8) p1;
|
|
|
|
apdu->p2 = (u8) p2;
|
|
|
|
}
|
|
|
|
|
2006-02-01 22:59:42 +00:00
|
|
|
static sc_card_t * sc_card_new(sc_context_t *ctx)
|
2002-02-20 09:56:47 +00:00
|
|
|
{
|
2005-03-08 20:59:35 +00:00
|
|
|
sc_card_t *card;
|
2005-02-06 19:40:40 +00:00
|
|
|
|
2006-02-01 22:59:42 +00:00
|
|
|
if (ctx == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
card = (sc_card_t *) calloc(1, sizeof(struct sc_card));
|
2002-02-20 09:56:47 +00:00
|
|
|
if (card == NULL)
|
|
|
|
return NULL;
|
2002-04-19 14:23:31 +00:00
|
|
|
card->ops = (struct sc_card_operations *) malloc(sizeof(struct sc_card_operations));
|
2002-02-20 09:56:47 +00:00
|
|
|
if (card->ops == NULL) {
|
|
|
|
free(card);
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-02-01 22:59:42 +00:00
|
|
|
|
|
|
|
card->ctx = ctx;
|
|
|
|
if (sc_mutex_create(ctx, &card->mutex) != SC_SUCCESS) {
|
|
|
|
free(card->ops);
|
|
|
|
free(card);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-02-06 19:40:40 +00:00
|
|
|
card->type = -1;
|
2002-02-20 09:56:47 +00:00
|
|
|
card->app_count = -1;
|
2003-04-28 16:34:38 +00:00
|
|
|
card->magic = SC_CARD_MAGIC;
|
2002-02-20 09:56:47 +00:00
|
|
|
|
|
|
|
return card;
|
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
static void sc_card_free(sc_card_t *card)
|
2002-02-20 09:56:47 +00:00
|
|
|
{
|
|
|
|
assert(sc_card_valid(card));
|
2003-05-15 13:32:17 +00:00
|
|
|
sc_free_apps(card);
|
2002-03-19 10:04:11 +00:00
|
|
|
if (card->ef_dir != NULL)
|
|
|
|
sc_file_free(card->ef_dir);
|
2002-02-20 09:56:47 +00:00
|
|
|
free(card->ops);
|
2002-03-08 05:59:57 +00:00
|
|
|
if (card->algorithms != NULL)
|
|
|
|
free(card->algorithms);
|
2006-02-05 19:35:55 +00:00
|
|
|
if (card->mutex != NULL) {
|
|
|
|
int r = sc_mutex_destroy(card->ctx, card->mutex);
|
|
|
|
if (r != SC_SUCCESS)
|
2009-09-14 08:46:59 +00:00
|
|
|
sc_debug(card->ctx, "unable to destroy mutex\n");
|
2006-02-05 19:35:55 +00:00
|
|
|
}
|
2005-09-17 10:44:45 +00:00
|
|
|
sc_mem_clear(card, sizeof(*card));
|
2002-02-20 09:56:47 +00:00
|
|
|
free(card);
|
|
|
|
}
|
|
|
|
|
2006-02-01 22:59:42 +00:00
|
|
|
int sc_connect_card(sc_reader_t *reader, int slot_id, sc_card_t **card_out)
|
2001-12-22 20:43:09 +00:00
|
|
|
{
|
2005-03-08 20:59:35 +00:00
|
|
|
sc_card_t *card;
|
2006-02-01 22:59:42 +00:00
|
|
|
sc_context_t *ctx;
|
2005-03-08 20:59:35 +00:00
|
|
|
sc_slot_info_t *slot = _sc_get_slot_info(reader, slot_id);
|
2003-02-20 12:51:07 +00:00
|
|
|
struct sc_card_driver *driver;
|
2006-01-23 18:09:37 +00:00
|
|
|
int i, r = 0, idx, connected = 0;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
2006-02-01 22:59:42 +00:00
|
|
|
if (card_out == NULL || reader == NULL)
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
ctx = reader->ctx;
|
2001-12-22 20:43:09 +00:00
|
|
|
SC_FUNC_CALLED(ctx, 1);
|
2002-02-24 19:32:14 +00:00
|
|
|
if (reader->ops->connect == NULL)
|
|
|
|
SC_FUNC_RETURN(ctx, 0, SC_ERROR_NOT_SUPPORTED);
|
|
|
|
if (slot == NULL)
|
|
|
|
SC_FUNC_RETURN(ctx, 0, SC_ERROR_SLOT_NOT_FOUND);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
2006-02-01 22:59:42 +00:00
|
|
|
card = sc_card_new(ctx);
|
2001-12-22 20:43:09 +00:00
|
|
|
if (card == NULL)
|
|
|
|
SC_FUNC_RETURN(ctx, 1, SC_ERROR_OUT_OF_MEMORY);
|
2002-02-24 19:32:14 +00:00
|
|
|
r = reader->ops->connect(reader, slot);
|
|
|
|
if (r)
|
2002-01-08 13:56:50 +00:00
|
|
|
goto err;
|
2002-02-24 19:32:14 +00:00
|
|
|
|
2006-01-23 18:09:37 +00:00
|
|
|
connected = 1;
|
2001-12-22 20:43:09 +00:00
|
|
|
card->reader = reader;
|
2002-02-24 19:32:14 +00:00
|
|
|
card->slot = slot;
|
2001-12-22 20:43:09 +00:00
|
|
|
card->ctx = ctx;
|
2002-02-24 19:32:14 +00:00
|
|
|
|
2003-12-18 16:35:28 +00:00
|
|
|
/* These can be overridden by the card driver */
|
|
|
|
card->max_send_size = reader->driver->max_send_size;
|
|
|
|
card->max_recv_size = reader->driver->max_recv_size;
|
|
|
|
|
2002-02-24 19:32:14 +00:00
|
|
|
memcpy(card->atr, slot->atr, slot->atr_len);
|
|
|
|
card->atr_len = slot->atr_len;
|
|
|
|
|
2002-06-03 15:05:58 +00:00
|
|
|
_sc_parse_atr(reader->ctx, slot);
|
|
|
|
|
2002-12-03 15:40:40 +00:00
|
|
|
/* See if the ATR matches any ATR specified in the config file */
|
|
|
|
if ((driver = ctx->forced_driver) == NULL) {
|
2005-02-14 09:12:44 +00:00
|
|
|
if (ctx->debug >= 3)
|
|
|
|
sc_debug(ctx, "matching configured ATRs\n");
|
2002-12-03 15:40:40 +00:00
|
|
|
for (i = 0; ctx->card_drivers[i] != NULL; i++) {
|
|
|
|
driver = ctx->card_drivers[i];
|
2005-02-14 09:12:44 +00:00
|
|
|
|
2005-02-22 07:59:42 +00:00
|
|
|
if (driver->atr_map == NULL ||
|
|
|
|
!strcmp(driver->short_name, "default")) {
|
2005-02-14 09:12:44 +00:00
|
|
|
driver = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ctx->debug >= 3)
|
|
|
|
sc_debug(ctx, "trying driver: %s\n", driver->short_name);
|
|
|
|
idx = _sc_match_atr(card, driver->atr_map, NULL);
|
|
|
|
if (idx >= 0) {
|
|
|
|
struct sc_atr_table *src = &driver->atr_map[idx];
|
|
|
|
|
|
|
|
if (ctx->debug >= 3)
|
|
|
|
sc_debug(ctx, "matched: %s\n", driver->name);
|
|
|
|
/* It's up to card driver to notice these correctly */
|
|
|
|
card->name = src->name;
|
|
|
|
card->type = src->type;
|
|
|
|
card->flags = src->flags;
|
2002-12-03 15:40:40 +00:00
|
|
|
break;
|
2005-02-14 09:12:44 +00:00
|
|
|
}
|
2002-12-03 15:40:40 +00:00
|
|
|
driver = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (driver != NULL) {
|
|
|
|
/* Forced driver, or matched via ATR mapping from
|
|
|
|
* config file */
|
|
|
|
card->driver = driver;
|
2002-01-08 13:56:50 +00:00
|
|
|
memcpy(card->ops, card->driver->ops, sizeof(struct sc_card_operations));
|
|
|
|
if (card->ops->init != NULL) {
|
|
|
|
r = card->ops->init(card);
|
|
|
|
if (r) {
|
2009-09-14 08:46:59 +00:00
|
|
|
sc_debug(ctx, "driver '%s' init() failed: %s\n", card->driver->name,
|
2002-01-08 13:56:50 +00:00
|
|
|
sc_strerror(r));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2005-02-14 09:12:44 +00:00
|
|
|
} else {
|
2001-12-25 20:45:48 +00:00
|
|
|
if (ctx->debug >= 3)
|
2005-02-14 09:12:44 +00:00
|
|
|
sc_debug(ctx, "matching built-in ATRs\n");
|
|
|
|
for (i = 0; ctx->card_drivers[i] != NULL; i++) {
|
|
|
|
struct sc_card_driver *drv = ctx->card_drivers[i];
|
|
|
|
const struct sc_card_operations *ops = drv->ops;
|
|
|
|
|
|
|
|
if (ctx->debug >= 3)
|
|
|
|
sc_debug(ctx, "trying driver: %s\n", drv->short_name);
|
|
|
|
if (ops == NULL || ops->match_card == NULL)
|
|
|
|
continue;
|
2006-06-07 08:33:37 +00:00
|
|
|
/* Needed if match_card() needs to talk with the card (e.g. card-muscle) */
|
|
|
|
*card->ops = *ops;
|
2005-02-14 09:12:44 +00:00
|
|
|
if (ops->match_card(card) != 1)
|
2001-12-25 20:45:48 +00:00
|
|
|
continue;
|
2005-02-14 09:12:44 +00:00
|
|
|
if (ctx->debug >= 3)
|
|
|
|
sc_debug(ctx, "matched: %s\n", drv->name);
|
|
|
|
memcpy(card->ops, ops, sizeof(struct sc_card_operations));
|
|
|
|
card->driver = drv;
|
|
|
|
r = ops->init(card);
|
|
|
|
if (r) {
|
2009-09-14 08:46:59 +00:00
|
|
|
sc_debug(ctx, "driver '%s' init() failed: %s\n", drv->name,
|
2005-02-14 09:12:44 +00:00
|
|
|
sc_strerror(r));
|
|
|
|
if (r == SC_ERROR_INVALID_CARD) {
|
|
|
|
card->driver = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
goto err;
|
2001-12-29 02:07:32 +00:00
|
|
|
}
|
2005-02-14 09:12:44 +00:00
|
|
|
break;
|
2001-12-22 20:43:09 +00:00
|
|
|
}
|
|
|
|
}
|
2002-01-08 13:56:50 +00:00
|
|
|
if (card->driver == NULL) {
|
2009-09-14 08:46:59 +00:00
|
|
|
sc_debug(ctx, "unable to find driver for inserted card\n");
|
2002-01-08 13:56:50 +00:00
|
|
|
r = SC_ERROR_INVALID_CARD;
|
|
|
|
goto err;
|
2001-12-22 20:43:09 +00:00
|
|
|
}
|
2003-05-28 20:52:33 +00:00
|
|
|
if (card->name == NULL)
|
|
|
|
card->name = card->driver->name;
|
2001-12-22 20:43:09 +00:00
|
|
|
*card_out = card;
|
|
|
|
|
2005-02-14 09:12:44 +00:00
|
|
|
sc_debug(ctx, "card info: %s, %i, 0x%X\n", card->name, card->type, card->flags);
|
2001-12-22 20:43:09 +00:00
|
|
|
SC_FUNC_RETURN(ctx, 1, 0);
|
2002-01-08 13:56:50 +00:00
|
|
|
err:
|
2006-01-23 18:09:37 +00:00
|
|
|
if (connected)
|
2006-03-01 09:45:09 +00:00
|
|
|
reader->ops->disconnect(reader, slot);
|
2002-02-20 09:56:47 +00:00
|
|
|
if (card != NULL)
|
|
|
|
sc_card_free(card);
|
2002-01-08 13:56:50 +00:00
|
|
|
SC_FUNC_RETURN(ctx, 1, r);
|
2001-12-22 20:43:09 +00:00
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int sc_disconnect_card(sc_card_t *card, int action)
|
2001-12-22 20:43:09 +00:00
|
|
|
{
|
2005-03-08 20:59:35 +00:00
|
|
|
sc_context_t *ctx;
|
2001-12-25 20:45:48 +00:00
|
|
|
assert(sc_card_valid(card));
|
|
|
|
ctx = card->ctx;
|
2001-12-22 20:43:09 +00:00
|
|
|
SC_FUNC_CALLED(ctx, 1);
|
2001-12-25 20:45:48 +00:00
|
|
|
assert(card->lock_count == 0);
|
|
|
|
if (card->ops->finish) {
|
|
|
|
int r = card->ops->finish(card);
|
|
|
|
if (r)
|
2009-09-14 08:46:59 +00:00
|
|
|
sc_debug(card->ctx, "card driver finish() failed: %s\n",
|
2005-02-06 19:40:40 +00:00
|
|
|
sc_strerror(r));
|
2002-02-24 19:32:14 +00:00
|
|
|
}
|
|
|
|
if (card->reader->ops->disconnect) {
|
2006-03-01 09:45:09 +00:00
|
|
|
int r = card->reader->ops->disconnect(card->reader, card->slot);
|
2002-02-24 19:32:14 +00:00
|
|
|
if (r)
|
2009-09-14 08:46:59 +00:00
|
|
|
sc_debug(card->ctx, "disconnect() failed: %s\n",
|
2001-12-25 20:45:48 +00:00
|
|
|
sc_strerror(r));
|
|
|
|
}
|
2002-02-20 09:56:47 +00:00
|
|
|
sc_card_free(card);
|
2001-12-22 20:43:09 +00:00
|
|
|
SC_FUNC_RETURN(ctx, 1, 0);
|
|
|
|
}
|
|
|
|
|
2006-03-22 21:44:09 +00:00
|
|
|
int sc_reset(sc_card_t *card)
|
|
|
|
{
|
|
|
|
int r, r2;
|
|
|
|
|
|
|
|
if (card == NULL)
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
if (card->reader->ops->reset == NULL)
|
|
|
|
return SC_ERROR_NOT_SUPPORTED;
|
|
|
|
|
|
|
|
r = sc_mutex_lock(card->ctx, card->mutex);
|
|
|
|
if (r != SC_SUCCESS)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = card->reader->ops->reset(card->reader, card->slot);
|
|
|
|
/* invalidate cache */
|
|
|
|
memset(&card->cache, 0, sizeof(card->cache));
|
|
|
|
card->cache_valid = 0;
|
|
|
|
|
|
|
|
r2 = sc_mutex_unlock(card->ctx, card->mutex);
|
|
|
|
if (r2 != SC_SUCCESS) {
|
2009-09-14 08:46:59 +00:00
|
|
|
sc_debug(card->ctx, "unable to release lock\n");
|
2006-03-22 21:44:09 +00:00
|
|
|
r = r != SC_SUCCESS ? r : r2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int sc_lock(sc_card_t *card)
|
2001-12-22 20:43:09 +00:00
|
|
|
{
|
2006-03-24 08:06:19 +00:00
|
|
|
int r = 0, r2 = 0;
|
2005-02-06 19:40:40 +00:00
|
|
|
|
2006-11-28 11:54:51 +00:00
|
|
|
SC_FUNC_CALLED(card->ctx, 3);
|
|
|
|
|
2006-02-01 22:59:42 +00:00
|
|
|
if (card == NULL)
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
r = sc_mutex_lock(card->ctx, card->mutex);
|
|
|
|
if (r != SC_SUCCESS)
|
|
|
|
return r;
|
2003-12-16 14:31:12 +00:00
|
|
|
if (card->lock_count == 0) {
|
2002-02-24 19:32:14 +00:00
|
|
|
if (card->reader->ops->lock != NULL)
|
|
|
|
r = card->reader->ops->lock(card->reader, card->slot);
|
|
|
|
if (r == 0)
|
|
|
|
card->cache_valid = 1;
|
|
|
|
}
|
2001-12-25 20:45:48 +00:00
|
|
|
if (r == 0)
|
|
|
|
card->lock_count++;
|
2006-03-24 08:06:19 +00:00
|
|
|
r2 = sc_mutex_unlock(card->ctx, card->mutex);
|
|
|
|
if (r2 != SC_SUCCESS) {
|
2009-09-14 08:46:59 +00:00
|
|
|
sc_debug(card->ctx, "unable to release lock\n");
|
2006-03-24 08:06:19 +00:00
|
|
|
r = r != SC_SUCCESS ? r : r2;
|
|
|
|
}
|
2003-12-16 14:31:12 +00:00
|
|
|
return r;
|
2001-12-22 20:43:09 +00:00
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int sc_unlock(sc_card_t *card)
|
2001-12-22 20:43:09 +00:00
|
|
|
{
|
2006-06-17 12:24:04 +00:00
|
|
|
int r, r2;
|
|
|
|
|
|
|
|
SC_FUNC_CALLED(card->ctx, 3);
|
2001-12-25 20:45:48 +00:00
|
|
|
|
2006-02-01 22:59:42 +00:00
|
|
|
if (card == NULL)
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
r = sc_mutex_lock(card->ctx, card->mutex);
|
|
|
|
if (r != SC_SUCCESS)
|
|
|
|
return r;
|
2003-05-20 08:30:46 +00:00
|
|
|
assert(card->lock_count >= 1);
|
2006-06-17 12:24:04 +00:00
|
|
|
if (--card->lock_count == 0) {
|
|
|
|
/* invalidate cache */
|
2003-05-22 13:59:25 +00:00
|
|
|
memset(&card->cache, 0, sizeof(card->cache));
|
|
|
|
card->cache_valid = 0;
|
2006-06-17 12:24:04 +00:00
|
|
|
/* release reader lock */
|
2002-02-24 19:32:14 +00:00
|
|
|
if (card->reader->ops->unlock != NULL)
|
|
|
|
r = card->reader->ops->unlock(card->reader, card->slot);
|
2005-02-11 20:09:34 +00:00
|
|
|
}
|
2006-06-17 12:24:04 +00:00
|
|
|
r2 = sc_mutex_unlock(card->ctx, card->mutex);
|
|
|
|
if (r2 != SC_SUCCESS) {
|
2009-09-14 08:46:59 +00:00
|
|
|
sc_debug(card->ctx, "unable to release lock\n");
|
2006-06-17 12:24:04 +00:00
|
|
|
r = (r == SC_SUCCESS) ? r2 : r;
|
|
|
|
}
|
2003-12-16 14:31:12 +00:00
|
|
|
return r;
|
2001-12-22 20:43:09 +00:00
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int sc_list_files(sc_card_t *card, u8 *buf, size_t buflen)
|
2001-12-22 20:43:09 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
2002-01-09 01:03:10 +00:00
|
|
|
assert(card != NULL);
|
|
|
|
SC_FUNC_CALLED(card->ctx, 1);
|
2005-02-11 20:09:34 +00:00
|
|
|
if (card->ops->list_files == NULL)
|
2002-01-09 01:03:10 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 1, SC_ERROR_NOT_SUPPORTED);
|
|
|
|
r = card->ops->list_files(card, buf, buflen);
|
2005-02-11 20:09:34 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 1, r);
|
2001-12-22 20:43:09 +00:00
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int sc_create_file(sc_card_t *card, sc_file_t *file)
|
2001-12-22 20:43:09 +00:00
|
|
|
{
|
2002-01-08 13:56:50 +00:00
|
|
|
int r;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
2002-01-08 13:56:50 +00:00
|
|
|
assert(card != NULL);
|
2003-08-29 12:55:17 +00:00
|
|
|
if (card->ctx->debug >= 1) {
|
2006-01-11 23:41:17 +00:00
|
|
|
char pbuf[SC_MAX_PATH_STRING_SIZE];
|
2003-08-29 12:55:17 +00:00
|
|
|
const sc_path_t *in_path = &file->path;
|
|
|
|
|
2006-01-11 23:41:17 +00:00
|
|
|
r = sc_path_print(pbuf, sizeof(pbuf), in_path);
|
|
|
|
if (r != SC_SUCCESS)
|
|
|
|
pbuf[0] = '\0';
|
|
|
|
|
2003-10-13 14:34:38 +00:00
|
|
|
sc_debug(card->ctx, "called; type=%d, path=%s, size=%u\n",
|
2006-01-11 23:41:17 +00:00
|
|
|
in_path->type, pbuf, file->size);
|
2003-08-29 12:55:17 +00:00
|
|
|
}
|
2005-02-11 20:09:34 +00:00
|
|
|
if (card->ops->create_file == NULL)
|
2002-01-08 13:56:50 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 1, SC_ERROR_NOT_SUPPORTED);
|
|
|
|
r = card->ops->create_file(card, file);
|
2005-02-11 20:09:34 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 1, r);
|
2001-12-22 20:43:09 +00:00
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int sc_delete_file(sc_card_t *card, const sc_path_t *path)
|
2001-12-22 20:43:09 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
2002-01-08 13:56:50 +00:00
|
|
|
assert(card != NULL);
|
2003-08-29 12:55:17 +00:00
|
|
|
if (card->ctx->debug >= 1) {
|
2006-01-11 23:41:17 +00:00
|
|
|
char pbuf[SC_MAX_PATH_STRING_SIZE];
|
|
|
|
|
|
|
|
r = sc_path_print(pbuf, sizeof(pbuf), path);
|
|
|
|
if (r != SC_SUCCESS)
|
|
|
|
pbuf[0] = '\0';
|
|
|
|
|
2003-08-29 12:55:17 +00:00
|
|
|
sc_debug(card->ctx, "called; type=%d, path=%s\n",
|
2006-01-11 23:41:17 +00:00
|
|
|
path->type, pbuf);
|
2003-08-29 12:55:17 +00:00
|
|
|
}
|
2005-02-11 20:09:34 +00:00
|
|
|
if (card->ops->delete_file == NULL)
|
2002-01-08 13:56:50 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 1, SC_ERROR_NOT_SUPPORTED);
|
|
|
|
r = card->ops->delete_file(card, path);
|
2005-02-11 20:09:34 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 1, r);
|
2001-12-22 20:43:09 +00:00
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int sc_read_binary(sc_card_t *card, unsigned int idx,
|
2001-12-29 02:07:32 +00:00
|
|
|
unsigned char *buf, size_t count, unsigned long flags)
|
2001-12-22 20:43:09 +00:00
|
|
|
{
|
2003-12-18 16:35:28 +00:00
|
|
|
size_t max_le = card->max_recv_size;
|
2001-12-22 20:43:09 +00:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(card != NULL && card->ops != NULL && buf != NULL);
|
|
|
|
if (card->ctx->debug >= 2)
|
2003-10-13 14:34:38 +00:00
|
|
|
sc_debug(card->ctx, "called; %d bytes at index %d\n", count, idx);
|
2003-04-16 10:19:20 +00:00
|
|
|
if (count == 0)
|
|
|
|
return 0;
|
2002-01-08 13:56:50 +00:00
|
|
|
if (card->ops->read_binary == NULL)
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_NOT_SUPPORTED);
|
2003-12-18 16:35:28 +00:00
|
|
|
if (count > max_le) {
|
2001-12-22 20:43:09 +00:00
|
|
|
int bytes_read = 0;
|
|
|
|
unsigned char *p = buf;
|
|
|
|
|
2001-12-25 20:45:48 +00:00
|
|
|
r = sc_lock(card);
|
|
|
|
SC_TEST_RET(card->ctx, r, "sc_lock() failed");
|
2001-12-22 20:43:09 +00:00
|
|
|
while (count > 0) {
|
2004-10-13 18:54:06 +00:00
|
|
|
size_t n = count > max_le ? max_le : count;
|
2001-12-29 02:07:32 +00:00
|
|
|
r = sc_read_binary(card, idx, p, n, flags);
|
2001-12-25 20:45:48 +00:00
|
|
|
if (r < 0) {
|
|
|
|
sc_unlock(card);
|
|
|
|
SC_TEST_RET(card->ctx, r, "sc_read_binary() failed");
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
p += r;
|
|
|
|
idx += r;
|
|
|
|
bytes_read += r;
|
|
|
|
count -= r;
|
2001-12-25 20:45:48 +00:00
|
|
|
if (r == 0) {
|
|
|
|
sc_unlock(card);
|
2001-12-22 20:43:09 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 2, bytes_read);
|
2001-12-25 20:45:48 +00:00
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
}
|
2001-12-25 20:45:48 +00:00
|
|
|
sc_unlock(card);
|
2001-12-22 20:43:09 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 2, bytes_read);
|
|
|
|
}
|
2001-12-29 02:07:32 +00:00
|
|
|
r = card->ops->read_binary(card, idx, buf, count, flags);
|
2005-02-11 20:09:34 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 2, r);
|
2002-01-08 13:56:50 +00:00
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int sc_write_binary(sc_card_t *card, unsigned int idx,
|
2002-01-08 13:56:50 +00:00
|
|
|
const u8 *buf, size_t count, unsigned long flags)
|
|
|
|
{
|
2003-12-18 16:35:28 +00:00
|
|
|
size_t max_lc = card->max_send_size;
|
2002-01-08 13:56:50 +00:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(card != NULL && card->ops != NULL && buf != NULL);
|
|
|
|
if (card->ctx->debug >= 2)
|
2003-10-13 14:34:38 +00:00
|
|
|
sc_debug(card->ctx, "called; %d bytes at index %d\n", count, idx);
|
2003-04-16 10:19:20 +00:00
|
|
|
if (count == 0)
|
|
|
|
return 0;
|
2002-01-08 13:56:50 +00:00
|
|
|
if (card->ops->write_binary == NULL)
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_NOT_SUPPORTED);
|
2003-12-18 16:35:28 +00:00
|
|
|
if (count > max_lc) {
|
2002-01-08 13:56:50 +00:00
|
|
|
int bytes_written = 0;
|
|
|
|
const u8 *p = buf;
|
|
|
|
|
|
|
|
r = sc_lock(card);
|
|
|
|
SC_TEST_RET(card->ctx, r, "sc_lock() failed");
|
|
|
|
while (count > 0) {
|
2004-10-13 18:54:06 +00:00
|
|
|
size_t n = count > max_lc? max_lc : count;
|
2002-01-08 13:56:50 +00:00
|
|
|
r = sc_write_binary(card, idx, p, n, flags);
|
|
|
|
if (r < 0) {
|
|
|
|
sc_unlock(card);
|
2007-09-04 05:39:15 +00:00
|
|
|
SC_TEST_RET(card->ctx, r, "sc_write_binary() failed");
|
2002-01-08 13:56:50 +00:00
|
|
|
}
|
|
|
|
p += r;
|
|
|
|
idx += r;
|
|
|
|
bytes_written += r;
|
|
|
|
count -= r;
|
|
|
|
if (r == 0) {
|
|
|
|
sc_unlock(card);
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, bytes_written);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sc_unlock(card);
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, bytes_written);
|
|
|
|
}
|
|
|
|
r = card->ops->write_binary(card, idx, buf, count, flags);
|
2005-02-11 20:09:34 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 2, r);
|
2001-12-22 20:43:09 +00:00
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int sc_update_binary(sc_card_t *card, unsigned int idx,
|
2002-01-13 23:56:13 +00:00
|
|
|
const u8 *buf, size_t count, unsigned long flags)
|
|
|
|
{
|
2003-12-18 16:35:28 +00:00
|
|
|
size_t max_lc = card->max_send_size;
|
2002-01-13 23:56:13 +00:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(card != NULL && card->ops != NULL && buf != NULL);
|
|
|
|
if (card->ctx->debug >= 2)
|
2003-10-13 14:34:38 +00:00
|
|
|
sc_debug(card->ctx, "called; %d bytes at index %d\n", count, idx);
|
2003-04-16 10:19:20 +00:00
|
|
|
if (count == 0)
|
|
|
|
return 0;
|
2002-01-13 23:56:13 +00:00
|
|
|
if (card->ops->update_binary == NULL)
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_NOT_SUPPORTED);
|
2003-12-18 16:35:28 +00:00
|
|
|
if (count > max_lc) {
|
2002-01-13 23:56:13 +00:00
|
|
|
int bytes_written = 0;
|
|
|
|
const u8 *p = buf;
|
|
|
|
|
|
|
|
r = sc_lock(card);
|
|
|
|
SC_TEST_RET(card->ctx, r, "sc_lock() failed");
|
|
|
|
while (count > 0) {
|
2004-10-13 18:54:06 +00:00
|
|
|
size_t n = count > max_lc? max_lc : count;
|
2002-01-13 23:56:13 +00:00
|
|
|
r = sc_update_binary(card, idx, p, n, flags);
|
|
|
|
if (r < 0) {
|
|
|
|
sc_unlock(card);
|
2003-07-31 19:06:43 +00:00
|
|
|
SC_TEST_RET(card->ctx, r, "sc_update_binary() failed");
|
2002-01-13 23:56:13 +00:00
|
|
|
}
|
|
|
|
p += r;
|
|
|
|
idx += r;
|
|
|
|
bytes_written += r;
|
|
|
|
count -= r;
|
|
|
|
if (r == 0) {
|
|
|
|
sc_unlock(card);
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, bytes_written);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sc_unlock(card);
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, bytes_written);
|
|
|
|
}
|
|
|
|
r = card->ops->update_binary(card, idx, buf, count, flags);
|
2005-02-11 20:09:34 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 2, r);
|
2002-01-13 23:56:13 +00:00
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int sc_select_file(sc_card_t *card,
|
|
|
|
const sc_path_t *in_path,
|
|
|
|
sc_file_t **file)
|
2001-12-22 20:43:09 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(card != NULL && in_path != NULL);
|
2003-08-29 12:55:17 +00:00
|
|
|
if (card->ctx->debug >= 1) {
|
2006-01-11 23:41:17 +00:00
|
|
|
char pbuf[SC_MAX_PATH_STRING_SIZE];
|
|
|
|
|
|
|
|
r = sc_path_print(pbuf, sizeof(pbuf), in_path);
|
|
|
|
if (r != SC_SUCCESS)
|
|
|
|
pbuf[0] = '\0';
|
|
|
|
|
2003-08-29 12:55:17 +00:00
|
|
|
sc_debug(card->ctx, "called; type=%d, path=%s\n",
|
2006-01-11 23:41:17 +00:00
|
|
|
in_path->type, pbuf);
|
2003-08-29 12:55:17 +00:00
|
|
|
}
|
2002-01-21 09:05:22 +00:00
|
|
|
if (in_path->len > SC_MAX_PATH_SIZE)
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_INVALID_ARGUMENTS);
|
|
|
|
if (in_path->type == SC_PATH_TYPE_PATH) {
|
|
|
|
/* Perform a sanity check */
|
2003-05-30 08:54:42 +00:00
|
|
|
size_t i;
|
2002-01-21 09:05:22 +00:00
|
|
|
if ((in_path->len & 1) != 0)
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_INVALID_ARGUMENTS);
|
|
|
|
for (i = 0; i < in_path->len/2; i++) {
|
|
|
|
u8 p1 = in_path->value[2*i],
|
|
|
|
p2 = in_path->value[2*i+1];
|
2003-05-30 08:54:42 +00:00
|
|
|
if ((p1 == 0x3F && p2 == 0x00) && i != 0)
|
2002-01-21 09:05:22 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_INVALID_ARGUMENTS);
|
|
|
|
}
|
|
|
|
}
|
2006-02-23 11:02:24 +00:00
|
|
|
if (card->ops->select_file == NULL)
|
2001-12-22 20:43:09 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_NOT_SUPPORTED);
|
2001-12-22 23:51:12 +00:00
|
|
|
r = card->ops->select_file(card, in_path, file);
|
2002-03-06 13:22:08 +00:00
|
|
|
/* Remember file path */
|
|
|
|
if (r == 0 && file && *file)
|
|
|
|
(*file)->path = *in_path;
|
2005-02-11 20:09:34 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 1, r);
|
2001-12-22 20:43:09 +00:00
|
|
|
}
|
|
|
|
|
2003-10-30 17:03:41 +00:00
|
|
|
int sc_get_data(sc_card_t *card, unsigned int tag, u8 *buf, size_t len)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
sc_debug(card->ctx, "called, tag=%04x\n", tag);
|
2005-02-11 20:09:34 +00:00
|
|
|
if (card->ops->get_data == NULL)
|
2003-10-30 17:03:41 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_NOT_SUPPORTED);
|
|
|
|
r = card->ops->get_data(card, tag, buf, len);
|
2005-02-11 20:09:34 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 1, r);
|
2003-10-30 17:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int sc_put_data(sc_card_t *card, unsigned int tag, const u8 *buf, size_t len)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
sc_debug(card->ctx, "called, tag=%04x\n", tag);
|
2005-02-11 20:09:34 +00:00
|
|
|
if (card->ops->put_data == NULL)
|
2003-10-30 17:03:41 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_NOT_SUPPORTED);
|
|
|
|
r = card->ops->put_data(card, tag, buf, len);
|
2005-02-11 20:09:34 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 1, r);
|
2003-10-30 17:03:41 +00:00
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int sc_get_challenge(sc_card_t *card, u8 *rnd, size_t len)
|
2001-12-22 20:43:09 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(card != NULL);
|
|
|
|
SC_FUNC_CALLED(card->ctx, 2);
|
2005-02-11 20:09:34 +00:00
|
|
|
if (card->ops->get_challenge == NULL)
|
2001-12-22 20:43:09 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_NOT_SUPPORTED);
|
|
|
|
r = card->ops->get_challenge(card, rnd, len);
|
2005-02-11 20:09:34 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 2, r);
|
2001-12-22 20:43:09 +00:00
|
|
|
}
|
2001-12-25 20:45:48 +00:00
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int sc_read_record(sc_card_t *card, unsigned int rec_nr, u8 *buf,
|
2001-12-29 02:07:32 +00:00
|
|
|
size_t count, unsigned long flags)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(card != NULL);
|
|
|
|
SC_FUNC_CALLED(card->ctx, 2);
|
2005-02-11 20:09:34 +00:00
|
|
|
if (card->ops->read_record == NULL)
|
2001-12-29 02:07:32 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_NOT_SUPPORTED);
|
|
|
|
r = card->ops->read_record(card, rec_nr, buf, count, flags);
|
2005-02-11 20:09:34 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 2, r);
|
2001-12-29 02:07:32 +00:00
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int sc_write_record(sc_card_t *card, unsigned int rec_nr, const u8 * buf,
|
2002-02-21 19:23:21 +00:00
|
|
|
size_t count, unsigned long flags)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(card != NULL);
|
|
|
|
SC_FUNC_CALLED(card->ctx, 2);
|
2005-02-11 20:09:34 +00:00
|
|
|
if (card->ops->write_record == NULL)
|
2002-02-21 19:23:21 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_NOT_SUPPORTED);
|
|
|
|
r = card->ops->write_record(card, rec_nr, buf, count, flags);
|
2005-02-11 20:09:34 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 2, r);
|
2002-02-21 19:23:21 +00:00
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int sc_append_record(sc_card_t *card, const u8 * buf, size_t count,
|
2002-02-21 19:23:21 +00:00
|
|
|
unsigned long flags)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(card != NULL);
|
|
|
|
SC_FUNC_CALLED(card->ctx, 2);
|
2005-02-11 20:09:34 +00:00
|
|
|
if (card->ops->append_record == NULL)
|
2002-02-21 19:23:21 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_NOT_SUPPORTED);
|
|
|
|
r = card->ops->append_record(card, buf, count, flags);
|
2005-02-11 20:09:34 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 2, r);
|
2002-02-21 19:23:21 +00:00
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int sc_update_record(sc_card_t *card, unsigned int rec_nr, const u8 * buf,
|
2002-02-21 19:23:21 +00:00
|
|
|
size_t count, unsigned long flags)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(card != NULL);
|
|
|
|
SC_FUNC_CALLED(card->ctx, 2);
|
2005-02-11 20:09:34 +00:00
|
|
|
if (card->ops->update_record == NULL)
|
2002-02-21 19:23:21 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_NOT_SUPPORTED);
|
|
|
|
r = card->ops->update_record(card, rec_nr, buf, count, flags);
|
2005-02-11 20:09:34 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 2, r);
|
2002-02-21 19:23:21 +00:00
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int sc_delete_record(sc_card_t *card, unsigned int rec_nr)
|
2004-01-06 13:33:32 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(card != NULL);
|
|
|
|
SC_FUNC_CALLED(card->ctx, 2);
|
|
|
|
if (card->ops->delete_record == NULL)
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_NOT_SUPPORTED);
|
|
|
|
r = card->ops->delete_record(card, rec_nr);
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, r);
|
|
|
|
}
|
|
|
|
|
2007-06-21 10:07:01 +00:00
|
|
|
int sc_card_valid(const sc_card_t *card) {
|
2001-12-25 20:45:48 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
assert(card != NULL);
|
|
|
|
#endif
|
|
|
|
return card->magic == SC_CARD_MAGIC;
|
|
|
|
}
|
2002-02-21 18:53:23 +00:00
|
|
|
|
|
|
|
int
|
2005-03-08 20:59:35 +00:00
|
|
|
sc_card_ctl(sc_card_t *card, unsigned long cmd, void *args)
|
2002-02-21 18:53:23 +00:00
|
|
|
{
|
2003-04-14 08:17:53 +00:00
|
|
|
int r = SC_ERROR_NOT_SUPPORTED;
|
2002-02-21 18:53:23 +00:00
|
|
|
|
|
|
|
assert(card != NULL);
|
|
|
|
SC_FUNC_CALLED(card->ctx, 2);
|
2005-02-11 20:09:34 +00:00
|
|
|
if (card->ops->card_ctl != NULL)
|
2003-04-14 08:17:53 +00:00
|
|
|
r = card->ops->card_ctl(card, cmd, args);
|
|
|
|
|
|
|
|
/* suppress "not supported" error messages */
|
|
|
|
if (r == SC_ERROR_NOT_SUPPORTED) {
|
2006-05-01 10:02:50 +00:00
|
|
|
sc_debug(card->ctx, "card_ctl(%lu) not supported\n",
|
|
|
|
(unsigned long) cmd);
|
2003-04-14 08:17:53 +00:00
|
|
|
return r;
|
|
|
|
}
|
2005-02-11 20:09:34 +00:00
|
|
|
SC_FUNC_RETURN(card->ctx, 2, r);
|
2002-02-21 18:53:23 +00:00
|
|
|
}
|
2002-03-08 05:59:57 +00:00
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int _sc_card_add_algorithm(sc_card_t *card, const sc_algorithm_info_t *info)
|
2002-03-08 05:59:57 +00:00
|
|
|
{
|
2005-03-08 20:59:35 +00:00
|
|
|
sc_algorithm_info_t *p;
|
2005-02-06 19:40:40 +00:00
|
|
|
|
2002-03-08 05:59:57 +00:00
|
|
|
assert(sc_card_valid(card) && info != NULL);
|
2005-03-08 20:59:35 +00:00
|
|
|
p = (sc_algorithm_info_t *) realloc(card->algorithms, (card->algorithm_count + 1) * sizeof(*info));
|
2004-04-21 18:10:58 +00:00
|
|
|
if (!p) {
|
|
|
|
if (card->algorithms)
|
|
|
|
free(card->algorithms);
|
|
|
|
card->algorithms = NULL;
|
2002-03-08 05:59:57 +00:00
|
|
|
card->algorithm_count = 0;
|
|
|
|
return SC_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2004-04-21 18:10:58 +00:00
|
|
|
card->algorithms = p;
|
|
|
|
p += card->algorithm_count;
|
2002-03-08 05:59:57 +00:00
|
|
|
card->algorithm_count++;
|
|
|
|
*p = *info;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int _sc_card_add_rsa_alg(sc_card_t *card, unsigned int key_length,
|
2002-03-10 11:48:57 +00:00
|
|
|
unsigned long flags, unsigned long exponent)
|
2002-03-08 05:59:57 +00:00
|
|
|
{
|
2005-03-08 20:59:35 +00:00
|
|
|
sc_algorithm_info_t info;
|
2002-03-08 05:59:57 +00:00
|
|
|
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
info.algorithm = SC_ALGORITHM_RSA;
|
|
|
|
info.key_length = key_length;
|
|
|
|
info.flags = flags;
|
|
|
|
info.u._rsa.exponent = exponent;
|
2005-02-06 19:40:40 +00:00
|
|
|
|
2002-03-08 05:59:57 +00:00
|
|
|
return _sc_card_add_algorithm(card, &info);
|
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
sc_algorithm_info_t * _sc_card_find_rsa_alg(sc_card_t *card,
|
2002-03-08 05:59:57 +00:00
|
|
|
unsigned int key_length)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < card->algorithm_count; i++) {
|
2005-03-08 20:59:35 +00:00
|
|
|
sc_algorithm_info_t *info = &card->algorithms[i];
|
2005-02-06 19:40:40 +00:00
|
|
|
|
2002-03-08 05:59:57 +00:00
|
|
|
if (info->algorithm != SC_ALGORITHM_RSA)
|
|
|
|
continue;
|
|
|
|
if (info->key_length != key_length)
|
|
|
|
continue;
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
static int match_atr_table(sc_context_t *ctx, struct sc_atr_table *table, u8 *atr, size_t atr_len)
|
2002-03-08 05:59:57 +00:00
|
|
|
{
|
2005-02-22 07:59:42 +00:00
|
|
|
u8 *card_atr_bin = atr;
|
|
|
|
size_t card_atr_bin_len = atr_len;
|
|
|
|
char card_atr_hex[3 * SC_MAX_ATR_SIZE];
|
|
|
|
size_t card_atr_hex_len;
|
2005-02-06 19:40:40 +00:00
|
|
|
unsigned int i = 0;
|
2002-12-03 15:40:40 +00:00
|
|
|
|
2005-02-22 07:59:42 +00:00
|
|
|
if (ctx == NULL || table == NULL || atr == NULL)
|
2005-02-06 19:40:40 +00:00
|
|
|
return -1;
|
2005-02-22 07:59:42 +00:00
|
|
|
sc_bin_to_hex(card_atr_bin, card_atr_bin_len, card_atr_hex, sizeof(card_atr_hex), ':');
|
|
|
|
card_atr_hex_len = strlen(card_atr_hex);
|
2005-02-06 19:40:40 +00:00
|
|
|
|
|
|
|
if (ctx->debug >= 4)
|
2005-02-22 07:59:42 +00:00
|
|
|
sc_debug(ctx, "ATR : %s\n", card_atr_hex);
|
2005-02-04 15:57:38 +00:00
|
|
|
|
|
|
|
for (i = 0; table[i].atr != NULL; i++) {
|
2005-02-06 19:40:40 +00:00
|
|
|
const char *tatr = table[i].atr;
|
2005-02-09 14:07:36 +00:00
|
|
|
const char *matr = table[i].atrmask;
|
|
|
|
size_t tatr_len = strlen(tatr);
|
2008-02-14 17:02:02 +00:00
|
|
|
u8 mbin[SC_MAX_ATR_SIZE], tbin[SC_MAX_ATR_SIZE];
|
|
|
|
size_t mbin_len, tbin_len, s, matr_len;
|
|
|
|
size_t fix_hex_len = card_atr_hex_len;
|
|
|
|
size_t fix_bin_len = card_atr_bin_len;
|
2005-02-04 15:57:38 +00:00
|
|
|
|
2005-02-06 19:40:40 +00:00
|
|
|
if (ctx->debug >= 4)
|
2005-02-09 14:07:36 +00:00
|
|
|
sc_debug(ctx, "ATR try : %s\n", tatr);
|
2005-02-06 19:40:40 +00:00
|
|
|
|
2008-02-14 17:02:02 +00:00
|
|
|
if (tatr_len != fix_hex_len) {
|
2007-01-09 07:22:58 +00:00
|
|
|
if (ctx->debug >= 5)
|
|
|
|
sc_debug(ctx, "ignored - wrong length\n", tatr);
|
2005-02-04 15:57:38 +00:00
|
|
|
continue;
|
2007-01-09 07:22:58 +00:00
|
|
|
}
|
2005-02-09 14:07:36 +00:00
|
|
|
if (matr != NULL) {
|
|
|
|
if (ctx->debug >= 4)
|
|
|
|
sc_debug(ctx, "ATR mask: %s\n", matr);
|
|
|
|
|
|
|
|
matr_len = strlen(matr);
|
|
|
|
if (tatr_len != matr_len)
|
|
|
|
continue;
|
2007-01-09 07:22:58 +00:00
|
|
|
tbin_len = sizeof(tbin);
|
|
|
|
sc_hex_to_bin(tatr, tbin, &tbin_len);
|
2005-02-09 14:07:36 +00:00
|
|
|
mbin_len = sizeof(mbin);
|
|
|
|
sc_hex_to_bin(matr, mbin, &mbin_len);
|
2008-02-14 17:02:02 +00:00
|
|
|
if (mbin_len != fix_bin_len) {
|
2009-09-14 08:46:59 +00:00
|
|
|
sc_debug(ctx,"length of atr and atr mask do not match - ignored: %s - %s", tatr, matr);
|
2005-02-09 14:07:36 +00:00
|
|
|
continue;
|
2007-01-09 07:22:58 +00:00
|
|
|
}
|
|
|
|
for (s = 0; s < tbin_len; s++) {
|
|
|
|
/* reduce tatr with mask */
|
|
|
|
tbin[s] = (tbin[s] & mbin[s]);
|
|
|
|
/* create copy of card_atr_bin masked) */
|
2005-02-22 07:59:42 +00:00
|
|
|
mbin[s] = (card_atr_bin[s] & mbin[s]);
|
2007-01-09 07:22:58 +00:00
|
|
|
}
|
2005-02-09 14:07:36 +00:00
|
|
|
if (memcmp(tbin, mbin, tbin_len) != 0)
|
|
|
|
continue;
|
|
|
|
} else {
|
2005-02-22 07:59:42 +00:00
|
|
|
if (strncasecmp(tatr, card_atr_hex, tatr_len) != 0)
|
2005-02-09 14:07:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
2005-02-04 15:57:38 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int _sc_match_atr(sc_card_t *card, struct sc_atr_table *table, int *type_out)
|
2005-02-22 07:59:42 +00:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (card == NULL)
|
|
|
|
return -1;
|
|
|
|
res = match_atr_table(card->ctx, table, card->atr, card->atr_len);
|
|
|
|
if (res < 0)
|
|
|
|
return res;
|
|
|
|
if (type_out != NULL)
|
|
|
|
*type_out = table[res].type;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
scconf_block *_sc_match_atr_block(sc_context_t *ctx, struct sc_card_driver *driver, u8 *atr, size_t atr_len)
|
|
|
|
{
|
|
|
|
struct sc_card_driver *drv;
|
|
|
|
struct sc_atr_table *table;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (ctx == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (driver) {
|
|
|
|
drv = driver;
|
|
|
|
table = drv->atr_map;
|
|
|
|
res = match_atr_table(ctx, table, atr, atr_len);
|
|
|
|
if (res < 0)
|
|
|
|
return NULL;
|
|
|
|
return table[res].card_atr;
|
|
|
|
} else {
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; ctx->card_drivers[i] != NULL; i++) {
|
|
|
|
drv = ctx->card_drivers[i];
|
|
|
|
table = drv->atr_map;
|
|
|
|
res = match_atr_table(ctx, table, atr, atr_len);
|
|
|
|
if (res < 0)
|
|
|
|
continue;
|
|
|
|
return table[res].card_atr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int _sc_add_atr(sc_context_t *ctx, struct sc_card_driver *driver, struct sc_atr_table *src)
|
2002-12-03 15:40:40 +00:00
|
|
|
{
|
2005-02-06 19:40:40 +00:00
|
|
|
struct sc_atr_table *map, *dst;
|
2002-12-03 15:40:40 +00:00
|
|
|
|
|
|
|
map = (struct sc_atr_table *) realloc(driver->atr_map,
|
|
|
|
(driver->natrs + 2) * sizeof(struct sc_atr_table));
|
|
|
|
if (!map)
|
|
|
|
return SC_ERROR_OUT_OF_MEMORY;
|
|
|
|
driver->atr_map = map;
|
2005-02-06 19:40:40 +00:00
|
|
|
dst = &driver->atr_map[driver->natrs++];
|
|
|
|
memset(dst, 0, sizeof(*dst));
|
2005-05-02 18:48:59 +00:00
|
|
|
memset(&driver->atr_map[driver->natrs], 0, sizeof(struct sc_atr_table));
|
|
|
|
dst->atr = strdup(src->atr);
|
2005-02-06 19:40:40 +00:00
|
|
|
if (!dst->atr)
|
2002-12-03 15:40:40 +00:00
|
|
|
return SC_ERROR_OUT_OF_MEMORY;
|
2005-02-09 14:07:36 +00:00
|
|
|
if (src->atrmask) {
|
|
|
|
dst->atrmask = strdup(src->atrmask);
|
|
|
|
if (!dst->atrmask)
|
|
|
|
return SC_ERROR_OUT_OF_MEMORY;
|
|
|
|
} else {
|
|
|
|
dst->atrmask = NULL;
|
|
|
|
}
|
2005-02-06 19:40:40 +00:00
|
|
|
if (src->name) {
|
|
|
|
dst->name = strdup(src->name);
|
|
|
|
if (!dst->name)
|
|
|
|
return SC_ERROR_OUT_OF_MEMORY;
|
|
|
|
} else {
|
|
|
|
dst->name = NULL;
|
|
|
|
}
|
2005-02-10 10:07:13 +00:00
|
|
|
dst->type = src->type;
|
2005-02-06 19:40:40 +00:00
|
|
|
dst->flags = src->flags;
|
2005-02-22 07:59:42 +00:00
|
|
|
dst->card_atr = src->card_atr;
|
2005-02-14 09:12:44 +00:00
|
|
|
return SC_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int _sc_free_atr(sc_context_t *ctx, struct sc_card_driver *driver)
|
2005-02-14 09:12:44 +00:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < driver->natrs; i++) {
|
|
|
|
struct sc_atr_table *src = &driver->atr_map[i];
|
|
|
|
|
|
|
|
if (src->atr)
|
|
|
|
free(src->atr);
|
|
|
|
if (src->atrmask)
|
|
|
|
free(src->atrmask);
|
|
|
|
if (src->name)
|
|
|
|
free(src->name);
|
2005-02-22 07:59:42 +00:00
|
|
|
src->card_atr = NULL;
|
2005-02-14 09:12:44 +00:00
|
|
|
src = NULL;
|
|
|
|
}
|
|
|
|
if (driver->atr_map)
|
|
|
|
free(driver->atr_map);
|
|
|
|
driver->atr_map = NULL;
|
|
|
|
driver->natrs = 0;
|
|
|
|
|
|
|
|
return SC_SUCCESS;
|
2002-12-03 15:40:40 +00:00
|
|
|
}
|
2005-02-22 07:59:42 +00:00
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int _sc_check_forced_protocol(sc_context_t *ctx, u8 *atr, size_t atr_len, unsigned int *protocol)
|
2005-02-22 07:59:42 +00:00
|
|
|
{
|
|
|
|
scconf_block *atrblock = NULL;
|
|
|
|
int ok = 0;
|
|
|
|
|
|
|
|
if (!protocol)
|
|
|
|
return 0;
|
|
|
|
atrblock = _sc_match_atr_block(ctx, NULL, atr, atr_len);
|
|
|
|
if (atrblock != NULL) {
|
|
|
|
const char *forcestr;
|
|
|
|
|
|
|
|
forcestr = scconf_get_str(atrblock, "force_protocol", "unknown");
|
|
|
|
if (!strcmp(forcestr, "t0")) {
|
|
|
|
*protocol = SC_PROTO_T0;
|
|
|
|
ok = 1;
|
|
|
|
} else if (!strcmp(forcestr, "t1")) {
|
|
|
|
*protocol = SC_PROTO_T1;
|
|
|
|
ok = 1;
|
|
|
|
} else if (!strcmp(forcestr, "raw")) {
|
|
|
|
*protocol = SC_PROTO_RAW;
|
|
|
|
ok = 1;
|
|
|
|
}
|
|
|
|
if (ok)
|
|
|
|
sc_debug(ctx, "force_protocol: %s\n", forcestr);
|
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
2005-03-09 11:14:41 +00:00
|
|
|
|
2006-01-31 15:53:44 +00:00
|
|
|
scconf_block *sc_get_conf_block(sc_context_t *ctx, const char *name1, const char *name2, int priority)
|
2005-03-09 11:14:41 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
scconf_block *conf_block = NULL;
|
|
|
|
|
|
|
|
for (i = 0; ctx->conf_blocks[i] != NULL; i++) {
|
|
|
|
scconf_block **blocks;
|
|
|
|
|
|
|
|
blocks = scconf_find_blocks(ctx->conf, ctx->conf_blocks[i], name1, name2);
|
2006-02-23 11:02:24 +00:00
|
|
|
if (blocks != NULL) {
|
|
|
|
conf_block = blocks[0];
|
|
|
|
free(blocks);
|
|
|
|
}
|
2005-08-21 18:44:07 +00:00
|
|
|
if (conf_block != NULL && priority)
|
2005-03-09 11:14:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return conf_block;
|
|
|
|
}
|
|
|
|
|