2001-12-22 20:43:09 +00:00
|
|
|
|
/*
|
2002-01-08 13:56:50 +00:00
|
|
|
|
* card.c: General SmartCard functions
|
2001-12-22 20:43:09 +00:00
|
|
|
|
*
|
2002-04-05 14:46:44 +00:00
|
|
|
|
* Copyright (C) 2001, 2002 Juha Yrj<EFBFBD>l<EFBFBD> <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
|
|
|
|
|
2002-02-15 23:17:58 +00:00
|
|
|
|
int sc_check_sw(struct sc_card *card, int sw1, int sw2)
|
2001-12-22 20:43:09 +00:00
|
|
|
|
{
|
2002-02-15 23:17:58 +00:00
|
|
|
|
assert(card->ops->check_sw != NULL);
|
|
|
|
|
return card->ops->check_sw(card, sw1, sw2);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int sc_check_apdu(struct sc_context *ctx, const struct sc_apdu *apdu)
|
|
|
|
|
{
|
2002-02-11 15:55:34 +00:00
|
|
|
|
if (apdu->le > 256) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(ctx, "Value of Le too big (maximum 256 bytes)\n");
|
2002-02-11 15:55:34 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, 4, SC_ERROR_INVALID_ARGUMENTS);
|
|
|
|
|
}
|
2002-05-09 10:22:42 +00:00
|
|
|
|
if (apdu->lc > 255) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(ctx, "Value of Lc too big (maximum 255 bytes)\n");
|
2002-02-11 15:55:34 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, 4, SC_ERROR_INVALID_ARGUMENTS);
|
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
switch (apdu->cse) {
|
|
|
|
|
case SC_APDU_CASE_1:
|
2002-01-08 13:56:50 +00:00
|
|
|
|
if (apdu->datalen > 0) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(ctx, "Case 1 APDU with data supplied\n");
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, 4, SC_ERROR_INVALID_ARGUMENTS);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
break;
|
|
|
|
|
case SC_APDU_CASE_2_SHORT:
|
2002-01-08 13:56:50 +00:00
|
|
|
|
if (apdu->datalen > 0) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(ctx, "Case 2 APDU with data supplied\n");
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, 4, SC_ERROR_INVALID_ARGUMENTS);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
}
|
2002-02-11 15:55:34 +00:00
|
|
|
|
if (apdu->le == 0) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(ctx, "Case 2 APDU with no response expected\n");
|
2002-02-11 15:55:34 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, 4, SC_ERROR_INVALID_ARGUMENTS);
|
|
|
|
|
}
|
2002-01-08 13:56:50 +00:00
|
|
|
|
if (apdu->resplen < apdu->le) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(ctx, "Response buffer size < Le\n");
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, 4, SC_ERROR_INVALID_ARGUMENTS);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
break;
|
|
|
|
|
case SC_APDU_CASE_3_SHORT:
|
2002-01-08 13:56:50 +00:00
|
|
|
|
if (apdu->datalen == 0 || apdu->data == NULL) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(ctx, "Case 3 APDU with no data supplied\n");
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, 4, SC_ERROR_INVALID_ARGUMENTS);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
break;
|
|
|
|
|
case SC_APDU_CASE_4_SHORT:
|
2002-01-08 13:56:50 +00:00
|
|
|
|
if (apdu->datalen == 0 || apdu->data == NULL) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(ctx, "Case 4 APDU with no data supplied\n");
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, 4, SC_ERROR_INVALID_ARGUMENTS);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
}
|
2002-02-11 15:55:34 +00:00
|
|
|
|
if (apdu->le == 0) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(ctx, "Case 4 APDU with no response expected\n");
|
2002-02-11 15:55:34 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, 4, SC_ERROR_INVALID_ARGUMENTS);
|
|
|
|
|
}
|
2002-01-08 13:56:50 +00:00
|
|
|
|
if (apdu->resplen < apdu->le) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(ctx, "Le > response buffer size\n");
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, 4, SC_ERROR_INVALID_ARGUMENTS);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
break;
|
|
|
|
|
case SC_APDU_CASE_2_EXT:
|
|
|
|
|
case SC_APDU_CASE_3_EXT:
|
|
|
|
|
case SC_APDU_CASE_4_EXT:
|
2003-10-14 09:56:50 +00:00
|
|
|
|
sc_error(ctx, "Invalid APDU case %d\n", apdu->cse);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, 4, SC_ERROR_INVALID_ARGUMENTS);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2003-12-18 16:35:28 +00:00
|
|
|
|
/*
|
|
|
|
|
* Handle APDU masquerading
|
|
|
|
|
*/
|
|
|
|
|
static int
|
|
|
|
|
sc_masquerade_apdu(sc_card_t *card, sc_apdu_t *apdu)
|
|
|
|
|
{
|
|
|
|
|
sc_context_t *ctx = card->reader->ctx;
|
|
|
|
|
int masq = card->reader->driver->apdu_masquerade;
|
|
|
|
|
int is_t0;
|
2005-02-06 19:40:40 +00:00
|
|
|
|
|
2003-12-18 16:35:28 +00:00
|
|
|
|
is_t0 = (card->slot->active_protocol == SC_PROTO_T0);
|
|
|
|
|
|
|
|
|
|
if (apdu->cse == SC_APDU_CASE_4_SHORT
|
|
|
|
|
&& is_t0 && (masq & SC_APDU_MASQUERADE_4AS3)) {
|
|
|
|
|
if (ctx->debug >= 5)
|
|
|
|
|
sc_debug(ctx, "Masquerading case 4 APDU as case 3");
|
|
|
|
|
apdu->cse = SC_APDU_CASE_3_SHORT;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (apdu->cse == SC_APDU_CASE_1
|
|
|
|
|
&& ((is_t0 && (masq & SC_APDU_MASQUERADE_1AS2))
|
|
|
|
|
|| ((masq & SC_APDU_MASQUERADE_1AS2_ALWAYS)))) {
|
|
|
|
|
if (ctx->debug >= 5)
|
|
|
|
|
sc_debug(ctx, "Masquerading case 1 APDU as case 2");
|
|
|
|
|
apdu->cse = SC_APDU_CASE_2_SHORT;
|
|
|
|
|
apdu->le = 0;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2002-06-16 21:19:37 +00:00
|
|
|
|
static int sc_transceive(struct sc_card *card, struct sc_apdu *apdu)
|
2001-12-22 20:43:09 +00:00
|
|
|
|
{
|
2002-02-24 19:32:14 +00:00
|
|
|
|
u8 sbuf[SC_MAX_APDU_BUFFER_SIZE];
|
|
|
|
|
u8 rbuf[SC_MAX_APDU_BUFFER_SIZE];
|
|
|
|
|
size_t sendsize, recvsize;
|
|
|
|
|
u8 *data = sbuf;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
size_t data_bytes = apdu->lc;
|
2002-02-24 19:32:14 +00:00
|
|
|
|
int r;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
|
2003-12-18 16:35:28 +00:00
|
|
|
|
#if 0
|
|
|
|
|
if (card->ctx->debug >= 6)
|
|
|
|
|
sc_debug(card->ctx, "masq=%x, max_send=%u, max_recv=%u",
|
|
|
|
|
card->reader->driver->apdu_masquerade,
|
|
|
|
|
card->max_recv_size,
|
|
|
|
|
card->max_send_size);
|
|
|
|
|
#endif
|
|
|
|
|
|
2002-02-24 19:32:14 +00:00
|
|
|
|
if (card->reader->ops->transmit == NULL)
|
|
|
|
|
return SC_ERROR_NOT_SUPPORTED;
|
2003-12-18 16:35:28 +00:00
|
|
|
|
|
|
|
|
|
/* APDU masquerading */
|
|
|
|
|
if (card->reader->driver->apdu_masquerade)
|
|
|
|
|
sc_masquerade_apdu(card, apdu);
|
|
|
|
|
|
2001-12-22 20:43:09 +00:00
|
|
|
|
if (data_bytes == 0)
|
|
|
|
|
data_bytes = 256;
|
|
|
|
|
*data++ = apdu->cla;
|
|
|
|
|
*data++ = apdu->ins;
|
|
|
|
|
*data++ = apdu->p1;
|
|
|
|
|
*data++ = apdu->p2;
|
|
|
|
|
switch (apdu->cse) {
|
|
|
|
|
case SC_APDU_CASE_1:
|
|
|
|
|
break;
|
|
|
|
|
case SC_APDU_CASE_2_SHORT:
|
|
|
|
|
*data++ = (u8) apdu->le;
|
|
|
|
|
break;
|
|
|
|
|
case SC_APDU_CASE_2_EXT:
|
|
|
|
|
*data++ = (u8) 0;
|
|
|
|
|
*data++ = (u8) (apdu->le >> 8);
|
|
|
|
|
*data++ = (u8) (apdu->le & 0xFF);
|
|
|
|
|
break;
|
|
|
|
|
case SC_APDU_CASE_3_SHORT:
|
|
|
|
|
*data++ = (u8) apdu->lc;
|
|
|
|
|
if (apdu->datalen != data_bytes)
|
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
|
memcpy(data, apdu->data, data_bytes);
|
|
|
|
|
data += data_bytes;
|
|
|
|
|
break;
|
|
|
|
|
case SC_APDU_CASE_4_SHORT:
|
|
|
|
|
*data++ = (u8) apdu->lc;
|
|
|
|
|
if (apdu->datalen != data_bytes)
|
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
|
memcpy(data, apdu->data, data_bytes);
|
|
|
|
|
data += data_bytes;
|
2002-02-11 15:55:34 +00:00
|
|
|
|
if (apdu->le == 256)
|
|
|
|
|
*data++ = 0x00;
|
|
|
|
|
else
|
|
|
|
|
*data++ = (u8) apdu->le;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2002-02-24 19:32:14 +00:00
|
|
|
|
sendsize = data - sbuf;
|
2005-02-07 22:43:51 +00:00
|
|
|
|
#if 0
|
2002-02-24 19:32:14 +00:00
|
|
|
|
recvsize = apdu->resplen + 2; /* space for the SW's */
|
2005-02-07 22:43:51 +00:00
|
|
|
|
#else
|
|
|
|
|
/* workaround for a broken cashmouse driver, should be harmless
|
|
|
|
|
* for other drivers -- Nils */
|
|
|
|
|
recvsize = sizeof(rbuf);
|
|
|
|
|
#endif
|
2001-12-22 20:43:09 +00:00
|
|
|
|
if (card->ctx->debug >= 5) {
|
|
|
|
|
char buf[2048];
|
2002-04-06 12:02:28 +00:00
|
|
|
|
|
2005-01-24 17:19:08 +00:00
|
|
|
|
#ifndef OPENSC_DONT_LOG_SENSITIVE
|
2002-04-06 12:02:28 +00:00
|
|
|
|
if (!apdu->sensitive || card->ctx->debug >= 6)
|
2005-01-24 17:19:08 +00:00
|
|
|
|
#else
|
|
|
|
|
if (!apdu->sensitive)
|
|
|
|
|
#endif
|
2002-04-06 12:02:28 +00:00
|
|
|
|
sc_hex_dump(card->ctx, sbuf, sendsize, buf, sizeof(buf));
|
2005-01-24 17:19:08 +00:00
|
|
|
|
else
|
|
|
|
|
/* sensitive information: just print the command
|
|
|
|
|
* header and no data */
|
2005-01-24 18:30:55 +00:00
|
|
|
|
snprintf(buf, sizeof(buf), "%02x %02x %02x %02x [sensitive data]",
|
2005-01-24 17:19:08 +00:00
|
|
|
|
apdu->cla, apdu->ins, apdu->p1, apdu->p2);
|
|
|
|
|
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_debug(card->ctx, "Sending %d bytes (resp. %d bytes%s):\n%s",
|
2002-04-06 12:02:28 +00:00
|
|
|
|
sendsize, recvsize,
|
|
|
|
|
apdu->sensitive ? ", sensitive" : "", buf);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
}
|
2002-02-24 19:32:14 +00:00
|
|
|
|
r = card->reader->ops->transmit(card->reader, card->slot, sbuf,
|
2002-12-23 18:47:27 +00:00
|
|
|
|
sendsize, rbuf, &recvsize,
|
|
|
|
|
apdu->control);
|
2002-04-06 12:02:28 +00:00
|
|
|
|
if (apdu->sensitive)
|
|
|
|
|
memset(sbuf, 0, sendsize);
|
2002-02-24 19:32:14 +00:00
|
|
|
|
SC_TEST_RET(card->ctx, r, "Unable to transmit");
|
|
|
|
|
|
|
|
|
|
assert(recvsize >= 2);
|
|
|
|
|
apdu->sw1 = (unsigned int) rbuf[recvsize-2];
|
|
|
|
|
apdu->sw2 = (unsigned int) rbuf[recvsize-1];
|
2002-04-06 12:02:28 +00:00
|
|
|
|
if (apdu->sensitive)
|
|
|
|
|
rbuf[recvsize-2] = rbuf[recvsize-1] = 0;
|
2002-02-24 19:32:14 +00:00
|
|
|
|
recvsize -= 2;
|
|
|
|
|
if (recvsize > apdu->resplen)
|
2002-04-06 12:02:28 +00:00
|
|
|
|
data_bytes = apdu->resplen;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
else
|
2002-04-06 12:02:28 +00:00
|
|
|
|
data_bytes = apdu->resplen = recvsize;
|
|
|
|
|
if (recvsize > 0) {
|
2002-12-20 14:55:17 +00:00
|
|
|
|
memcpy(apdu->resp, rbuf, data_bytes);
|
2002-04-06 12:02:28 +00:00
|
|
|
|
if (apdu->sensitive)
|
|
|
|
|
memset(rbuf, 0, recvsize);
|
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_transmit_apdu(struct sc_card *card, struct sc_apdu *apdu)
|
|
|
|
|
{
|
|
|
|
|
int r;
|
2001-12-29 02:07:32 +00:00
|
|
|
|
size_t orig_resplen;
|
2005-02-06 19:40:40 +00:00
|
|
|
|
|
2001-12-29 02:07:32 +00:00
|
|
|
|
assert(card != NULL && apdu != NULL);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_CALLED(card->ctx, 4);
|
2001-12-29 02:07:32 +00:00
|
|
|
|
orig_resplen = apdu->resplen;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
r = sc_check_apdu(card->ctx, apdu);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "APDU sanity check failed");
|
2001-12-25 20:45:48 +00:00
|
|
|
|
r = sc_lock(card);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "sc_lock() failed");
|
2002-06-16 21:19:37 +00:00
|
|
|
|
r = sc_transceive(card, apdu);
|
2001-12-25 20:45:48 +00:00
|
|
|
|
if (r != 0) {
|
|
|
|
|
sc_unlock(card);
|
2002-06-16 21:19:37 +00:00
|
|
|
|
SC_TEST_RET(card->ctx, r, "transceive() failed");
|
2001-12-25 20:45:48 +00:00
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
if (card->ctx->debug >= 5) {
|
|
|
|
|
char buf[2048];
|
|
|
|
|
|
|
|
|
|
buf[0] = '\0';
|
|
|
|
|
if (apdu->resplen > 0) {
|
|
|
|
|
sc_hex_dump(card->ctx, apdu->resp, apdu->resplen,
|
|
|
|
|
buf, sizeof(buf));
|
|
|
|
|
}
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_debug(card->ctx, "Received %d bytes (SW1=%02X SW2=%02X)\n%s",
|
2001-12-22 20:43:09 +00:00
|
|
|
|
apdu->resplen, apdu->sw1, apdu->sw2, buf);
|
|
|
|
|
}
|
2001-12-29 02:07:32 +00:00
|
|
|
|
if (apdu->sw1 == 0x6C && apdu->resplen == 0) {
|
|
|
|
|
apdu->resplen = orig_resplen;
|
|
|
|
|
apdu->le = apdu->sw2;
|
2005-01-30 19:20:38 +00:00
|
|
|
|
/* Fix for cards (e.g. belpic) that need a delay on fast readers */
|
|
|
|
|
if (card->wait_resend_apdu != 0)
|
|
|
|
|
msleep(card->wait_resend_apdu);
|
2002-06-16 21:19:37 +00:00
|
|
|
|
r = sc_transceive(card, apdu);
|
2001-12-29 02:07:32 +00:00
|
|
|
|
if (r != 0) {
|
|
|
|
|
sc_unlock(card);
|
2002-06-16 21:19:37 +00:00
|
|
|
|
SC_TEST_RET(card->ctx, r, "transceive() failed");
|
2001-12-29 02:07:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
if (apdu->sw1 == 0x61 && apdu->resplen == 0) {
|
2004-10-29 20:08:54 +00:00
|
|
|
|
size_t le;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
|
2001-12-29 02:07:32 +00:00
|
|
|
|
if (orig_resplen == 0) {
|
|
|
|
|
apdu->sw1 = 0x90; /* FIXME: should we do this? */
|
2001-12-25 20:45:48 +00:00
|
|
|
|
apdu->sw2 = 0;
|
|
|
|
|
sc_unlock(card);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
return 0;
|
2001-12-25 20:45:48 +00:00
|
|
|
|
}
|
2003-04-03 18:19:22 +00:00
|
|
|
|
|
|
|
|
|
le = apdu->sw2? (size_t) apdu->sw2 : 256;
|
|
|
|
|
|
2003-07-10 11:38:02 +00:00
|
|
|
|
if (card->ops->get_response == NULL) {
|
2001-12-25 20:45:48 +00:00
|
|
|
|
sc_unlock(card);
|
2003-07-10 11:38:02 +00:00
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_NOT_SUPPORTED);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
}
|
2003-07-10 11:38:02 +00:00
|
|
|
|
r = card->ops->get_response(card, apdu, le);
|
2003-07-11 09:40:49 +00:00
|
|
|
|
if (r < 0) {
|
2005-01-24 22:10:55 +00:00
|
|
|
|
sc_unlock(card);
|
2003-07-11 09:40:49 +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
|
|
|
|
sc_unlock(card);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sc_format_apdu(struct sc_card *card, struct sc_apdu *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;
|
2002-01-10 23:02:48 +00:00
|
|
|
|
|
2001-12-22 20:43:09 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-13 18:54:06 +00:00
|
|
|
|
static struct sc_card * sc_card_new(void)
|
2002-02-20 09:56:47 +00:00
|
|
|
|
{
|
|
|
|
|
struct sc_card *card;
|
2005-02-06 19:40:40 +00:00
|
|
|
|
|
2005-01-03 17:20:17 +00:00
|
|
|
|
card = (struct sc_card *) 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;
|
|
|
|
|
}
|
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;
|
2003-01-14 11:22:33 +00:00
|
|
|
|
card->mutex = sc_mutex_new();
|
2002-02-20 09:56:47 +00:00
|
|
|
|
|
|
|
|
|
return card;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void sc_card_free(struct sc_card *card)
|
|
|
|
|
{
|
|
|
|
|
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);
|
2003-01-14 11:22:33 +00:00
|
|
|
|
sc_mutex_free(card->mutex);
|
|
|
|
|
memset(card, 0, sizeof(*card));
|
2002-02-20 09:56:47 +00:00
|
|
|
|
free(card);
|
|
|
|
|
}
|
|
|
|
|
|
2002-02-24 19:32:14 +00:00
|
|
|
|
int sc_connect_card(struct sc_reader *reader, int slot_id,
|
|
|
|
|
struct sc_card **card_out)
|
2001-12-22 20:43:09 +00:00
|
|
|
|
{
|
|
|
|
|
struct sc_card *card;
|
2002-02-24 19:32:14 +00:00
|
|
|
|
struct sc_context *ctx = reader->ctx;
|
|
|
|
|
struct sc_slot_info *slot = _sc_get_slot_info(reader, slot_id);
|
2003-02-20 12:51:07 +00:00
|
|
|
|
struct sc_card_driver *driver;
|
2005-02-14 09:12:44 +00:00
|
|
|
|
int i, r = 0, idx;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
|
|
|
|
|
assert(card_out != NULL);
|
|
|
|
|
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
|
|
|
|
|
2002-02-20 09:56:47 +00:00
|
|
|
|
card = sc_card_new();
|
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
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
if (driver->atr_map == NULL) {
|
|
|
|
|
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) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(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;
|
|
|
|
|
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) {
|
|
|
|
|
sc_error(ctx, "driver '%s' init() failed: %s\n", drv->name,
|
|
|
|
|
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) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(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:
|
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
|
|
|
|
}
|
|
|
|
|
|
2002-02-24 19:32:14 +00:00
|
|
|
|
int sc_disconnect_card(struct sc_card *card, int action)
|
2001-12-22 20:43:09 +00:00
|
|
|
|
{
|
2001-12-25 20:45:48 +00:00
|
|
|
|
struct sc_context *ctx;
|
|
|
|
|
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)
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(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) {
|
|
|
|
|
int r = card->reader->ops->disconnect(card->reader, card->slot, action);
|
|
|
|
|
if (r)
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_lock(struct sc_card *card)
|
|
|
|
|
{
|
2001-12-25 20:45:48 +00:00
|
|
|
|
int r = 0;
|
2005-02-06 19:40:40 +00:00
|
|
|
|
|
2001-12-25 20:45:48 +00:00
|
|
|
|
assert(card != NULL);
|
2003-01-14 11:22:33 +00:00
|
|
|
|
sc_mutex_lock(card->mutex);
|
2003-12-16 14:31:12 +00:00
|
|
|
|
if (card->lock_count == 0) {
|
|
|
|
|
SC_FUNC_CALLED(card->ctx, 3);
|
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++;
|
2003-01-14 11:22:33 +00:00
|
|
|
|
sc_mutex_unlock(card->mutex);
|
2003-12-16 14:31:12 +00:00
|
|
|
|
return r;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_unlock(struct sc_card *card)
|
|
|
|
|
{
|
2001-12-25 20:45:48 +00:00
|
|
|
|
int r = 0;
|
|
|
|
|
|
|
|
|
|
assert(card != NULL);
|
2003-01-14 11:22:33 +00:00
|
|
|
|
sc_mutex_lock(card->mutex);
|
2003-05-20 08:30:46 +00:00
|
|
|
|
assert(card->lock_count >= 1);
|
|
|
|
|
if (card->lock_count == 1) {
|
2003-12-16 14:31:12 +00:00
|
|
|
|
SC_FUNC_CALLED(card->ctx, 3);
|
2003-05-22 13:59:25 +00:00
|
|
|
|
memset(&card->cache, 0, sizeof(card->cache));
|
|
|
|
|
card->cache_valid = 0;
|
|
|
|
|
if (card->ops->logout != NULL) {
|
|
|
|
|
sc_mutex_unlock(card->mutex);
|
2003-10-30 17:03:41 +00:00
|
|
|
|
sc_debug(card->ctx, "Calling card logout function\n");
|
2003-05-20 08:30:46 +00:00
|
|
|
|
card->ops->logout(card);
|
2003-05-22 13:59:25 +00:00
|
|
|
|
sc_mutex_lock(card->mutex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Check again, lock count may have changed
|
|
|
|
|
* while we were in logout() */
|
|
|
|
|
if (card->lock_count == 1) {
|
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
|
|
|
|
}
|
|
|
|
|
card->lock_count--;
|
|
|
|
|
sc_mutex_unlock(card->mutex);
|
2003-12-16 14:31:12 +00:00
|
|
|
|
return r;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-01-09 01:03:10 +00:00
|
|
|
|
int sc_list_files(struct sc_card *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
|
|
|
|
}
|
|
|
|
|
|
2002-01-17 11:44:27 +00:00
|
|
|
|
int sc_create_file(struct sc_card *card, struct sc_file *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) {
|
|
|
|
|
const sc_path_t *in_path = &file->path;
|
|
|
|
|
|
2003-10-13 14:34:38 +00:00
|
|
|
|
sc_debug(card->ctx, "called; type=%d, path=%s, size=%u\n",
|
2003-08-29 12:55:17 +00:00
|
|
|
|
in_path->type,
|
2003-10-13 14:34:38 +00:00
|
|
|
|
sc_print_path(in_path), 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
|
|
|
|
}
|
|
|
|
|
|
2002-01-08 13:56:50 +00:00
|
|
|
|
int sc_delete_file(struct sc_card *card, const struct sc_path *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) {
|
|
|
|
|
sc_debug(card->ctx, "called; type=%d, path=%s\n",
|
|
|
|
|
path->type,
|
|
|
|
|
sc_print_path(path));
|
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_read_binary(struct sc_card *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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_write_binary(struct sc_card *card, unsigned int idx,
|
|
|
|
|
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);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "sc_read_binary() failed");
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2002-01-13 23:56:13 +00:00
|
|
|
|
int sc_update_binary(struct sc_card *card, unsigned int idx,
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2001-12-22 20:43:09 +00:00
|
|
|
|
int sc_select_file(struct sc_card *card,
|
2001-12-22 23:51:12 +00:00
|
|
|
|
const struct sc_path *in_path,
|
2002-02-20 09:56:47 +00:00
|
|
|
|
struct sc_file **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) {
|
|
|
|
|
sc_debug(card->ctx, "called; type=%d, path=%s\n",
|
|
|
|
|
in_path->type,
|
|
|
|
|
sc_print_path(in_path));
|
|
|
|
|
}
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
if (card->ops->select_file == NULL)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2001-12-22 20:43:09 +00:00
|
|
|
|
int sc_get_challenge(struct sc_card *card, u8 *rnd, size_t len)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
2001-12-29 02:07:32 +00:00
|
|
|
|
int sc_read_record(struct sc_card *card, unsigned int rec_nr, u8 *buf,
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2002-02-21 19:23:21 +00:00
|
|
|
|
int sc_write_record(struct sc_card *card, unsigned int rec_nr, const u8 * buf,
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_append_record(struct sc_card *card, const u8 * buf, 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->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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_update_record(struct sc_card *card, unsigned int rec_nr, const u8 * buf,
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2004-01-06 13:33:32 +00:00
|
|
|
|
int sc_delete_record(struct sc_card *card, unsigned int rec_nr)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-25 20:45:48 +00:00
|
|
|
|
inline int sc_card_valid(const struct sc_card *card) {
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
|
assert(card != NULL);
|
|
|
|
|
#endif
|
|
|
|
|
return card->magic == SC_CARD_MAGIC;
|
|
|
|
|
}
|
2002-02-21 18:53:23 +00:00
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
sc_card_ctl(struct sc_card *card, unsigned long cmd, void *args)
|
|
|
|
|
{
|
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) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_debug(card->ctx, "card_ctl(%lu) not supported\n", 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
|
|
|
|
|
|
|
|
|
int _sc_card_add_algorithm(struct sc_card *card, const struct sc_algorithm_info *info)
|
|
|
|
|
{
|
|
|
|
|
struct sc_algorithm_info *p;
|
2005-02-06 19:40:40 +00:00
|
|
|
|
|
2002-03-08 05:59:57 +00:00
|
|
|
|
assert(sc_card_valid(card) && info != NULL);
|
2004-04-21 18:10:58 +00:00
|
|
|
|
p = (struct sc_algorithm_info *) realloc(card->algorithms, (card->algorithm_count + 1) * sizeof(*info));
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int _sc_card_add_rsa_alg(struct sc_card *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
|
|
|
|
{
|
|
|
|
|
struct sc_algorithm_info info;
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct sc_algorithm_info * _sc_card_find_rsa_alg(struct sc_card *card,
|
|
|
|
|
unsigned int key_length)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < card->algorithm_count; i++) {
|
|
|
|
|
struct sc_algorithm_info *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-02-10 10:07:13 +00:00
|
|
|
|
int _sc_match_atr(struct sc_card *card, struct sc_atr_table *table, int *type_out)
|
2002-03-08 05:59:57 +00:00
|
|
|
|
{
|
2005-02-06 19:40:40 +00:00
|
|
|
|
struct sc_context *ctx = card->ctx;
|
2005-02-09 14:07:36 +00:00
|
|
|
|
char card_atr[3 * SC_MAX_ATR_SIZE];
|
|
|
|
|
size_t card_atr_len;
|
2005-02-06 19:40:40 +00:00
|
|
|
|
unsigned int i = 0;
|
2002-12-03 15:40:40 +00:00
|
|
|
|
|
2005-02-06 19:40:40 +00:00
|
|
|
|
if (table == NULL || card == NULL)
|
|
|
|
|
return -1;
|
2005-02-04 15:57:38 +00:00
|
|
|
|
|
2005-02-09 14:07:36 +00:00
|
|
|
|
sc_bin_to_hex(card->atr, card->atr_len, card_atr, sizeof(card_atr), ':');
|
|
|
|
|
card_atr_len = strlen(card_atr);
|
2005-02-06 19:40:40 +00:00
|
|
|
|
|
|
|
|
|
if (ctx->debug >= 4)
|
2005-02-09 14:07:36 +00:00
|
|
|
|
sc_debug(ctx, "ATR : %s\n", card_atr);
|
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);
|
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
|
|
|
|
|
2005-02-09 14:07:36 +00:00
|
|
|
|
if (tatr_len != card_atr_len)
|
2005-02-04 15:57:38 +00:00
|
|
|
|
continue;
|
2005-02-09 14:07:36 +00:00
|
|
|
|
if (matr != NULL) {
|
|
|
|
|
u8 mbin[SC_MAX_ATR_SIZE], tbin[SC_MAX_ATR_SIZE];
|
|
|
|
|
size_t mbin_len, tbin_len, s, matr_len;
|
|
|
|
|
|
|
|
|
|
if (ctx->debug >= 4)
|
|
|
|
|
sc_debug(ctx, "ATR mask: %s\n", matr);
|
|
|
|
|
|
|
|
|
|
matr_len = strlen(matr);
|
|
|
|
|
if (tatr_len != matr_len)
|
|
|
|
|
continue;
|
|
|
|
|
mbin_len = sizeof(mbin);
|
|
|
|
|
sc_hex_to_bin(matr, mbin, &mbin_len);
|
|
|
|
|
if (mbin_len != card->atr_len)
|
|
|
|
|
continue;
|
|
|
|
|
for (s = 0; s < mbin_len; s++)
|
|
|
|
|
mbin[s] = (card->atr[s] & mbin[s]);
|
|
|
|
|
tbin_len = sizeof(tbin);
|
|
|
|
|
sc_hex_to_bin(tatr, tbin, &tbin_len);
|
|
|
|
|
if (memcmp(tbin, mbin, tbin_len) != 0)
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
if (strncasecmp(tatr, card_atr, tatr_len) != 0)
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2005-02-10 10:07:13 +00:00
|
|
|
|
if (type_out != NULL)
|
|
|
|
|
*type_out = table[i].type;
|
2005-02-04 15:57:38 +00:00
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2005-02-09 14:09:27 +00:00
|
|
|
|
int _sc_add_atr(struct sc_context *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));
|
|
|
|
|
dst->atr = strdup(src->atr);
|
|
|
|
|
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-14 09:12:44 +00:00
|
|
|
|
return SC_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int _sc_free_atr(struct sc_context *ctx, struct sc_card_driver *driver)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
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
|
|
|
|
}
|