2001-12-25 20:45:48 +00:00
|
|
|
/*
|
2002-01-08 13:56:50 +00:00
|
|
|
* card-default.c: Support for cards with no driver
|
2001-12-25 20:45:48 +00:00
|
|
|
*
|
2006-12-19 21:32:31 +00:00
|
|
|
* Copyright (C) 2001, 2002 Juha Yrjölä <juha.yrjola@iki.fi>
|
2001-12-25 20:45:48 +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
|
|
|
|
*/
|
|
|
|
|
2010-03-04 08:14:36 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2004-01-29 09:36:48 +00:00
|
|
|
#include <string.h>
|
2001-12-25 20:45:48 +00:00
|
|
|
|
2010-03-04 08:14:36 +00:00
|
|
|
#include "internal.h"
|
|
|
|
|
2001-12-25 20:45:48 +00:00
|
|
|
static struct sc_card_operations default_ops;
|
2003-02-20 12:51:07 +00:00
|
|
|
static struct sc_card_driver default_drv = {
|
2001-12-25 20:45:48 +00:00
|
|
|
"Default driver for unknown cards",
|
2002-01-08 13:56:50 +00:00
|
|
|
"default",
|
2005-09-07 08:33:55 +00:00
|
|
|
&default_ops,
|
|
|
|
NULL, 0, NULL
|
2001-12-25 20:45:48 +00:00
|
|
|
};
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
static int default_match_card(sc_card_t *card)
|
2001-12-25 20:45:48 +00:00
|
|
|
{
|
|
|
|
return 1; /* always match */
|
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
static int autodetect_class(sc_card_t *card)
|
2001-12-25 20:45:48 +00:00
|
|
|
{
|
|
|
|
int classes[] = { 0x00, 0xC0, 0xB0, 0xA0 };
|
|
|
|
int class_count = sizeof(classes)/sizeof(int);
|
2002-01-08 13:56:50 +00:00
|
|
|
u8 rbuf[SC_MAX_APDU_BUFFER_SIZE];
|
2005-03-08 20:59:35 +00:00
|
|
|
sc_apdu_t apdu;
|
2001-12-25 20:45:48 +00:00
|
|
|
int i, r;
|
|
|
|
|
2010-03-15 12:17:13 +00:00
|
|
|
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "autodetecting CLA byte\n");
|
2001-12-25 20:45:48 +00:00
|
|
|
for (i = 0; i < class_count; i++) {
|
2010-03-15 12:17:13 +00:00
|
|
|
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "trying with 0x%02X\n", classes[i]);
|
2004-01-29 09:21:30 +00:00
|
|
|
memset(&apdu, 0, sizeof(apdu));
|
2001-12-25 20:45:48 +00:00
|
|
|
apdu.cla = classes[i];
|
2002-05-09 10:22:42 +00:00
|
|
|
apdu.cse = SC_APDU_CASE_2_SHORT;
|
2002-02-11 15:55:34 +00:00
|
|
|
apdu.ins = 0xC0;
|
2001-12-25 20:45:48 +00:00
|
|
|
apdu.p1 = apdu.p2 = 0;
|
2002-02-11 15:55:34 +00:00
|
|
|
apdu.datalen = 0;
|
2002-05-09 10:22:42 +00:00
|
|
|
apdu.lc = 0;
|
|
|
|
apdu.le = 256;
|
2002-10-19 14:04:52 +00:00
|
|
|
apdu.resp = rbuf;
|
|
|
|
apdu.resplen = sizeof(rbuf);
|
2001-12-25 20:45:48 +00:00
|
|
|
r = sc_transmit_apdu(card, &apdu);
|
2010-03-15 12:17:13 +00:00
|
|
|
SC_TEST_RET(card->ctx, SC_LOG_DEBUG_NORMAL, r, "APDU transmit failed");
|
2002-02-11 15:55:34 +00:00
|
|
|
if (apdu.sw1 == 0x6E)
|
|
|
|
continue;
|
2001-12-25 20:45:48 +00:00
|
|
|
if (apdu.sw1 == 0x90 && apdu.sw2 == 0x00)
|
|
|
|
break;
|
|
|
|
if (apdu.sw1 == 0x61)
|
|
|
|
break;
|
2010-03-15 12:17:13 +00:00
|
|
|
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL,
|
|
|
|
"got strange SWs: 0x%02X 0x%02X\n", apdu.sw1, apdu.sw2);
|
2002-02-11 15:55:34 +00:00
|
|
|
break;
|
2001-12-25 20:45:48 +00:00
|
|
|
}
|
|
|
|
if (i == class_count)
|
|
|
|
return -1;
|
|
|
|
card->cla = classes[i];
|
2010-03-15 12:17:13 +00:00
|
|
|
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL,
|
|
|
|
"detected CLA byte as 0x%02X\n", card->cla);
|
2002-01-08 13:56:50 +00:00
|
|
|
if (apdu.resplen < 2) {
|
2010-03-15 12:17:13 +00:00
|
|
|
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL,
|
|
|
|
"SELECT FILE returned %d bytes\n", apdu.resplen);
|
2009-09-15 07:51:50 +00:00
|
|
|
return SC_SUCCESS;
|
2002-01-08 13:56:50 +00:00
|
|
|
}
|
|
|
|
if (rbuf[0] == 0x6F) {
|
2010-03-15 12:17:13 +00:00
|
|
|
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL,
|
|
|
|
"SELECT FILE seems to behave according to ISO 7816-4\n");
|
2009-09-15 07:51:50 +00:00
|
|
|
return SC_SUCCESS;
|
2002-01-08 13:56:50 +00:00
|
|
|
}
|
|
|
|
if (rbuf[0] == 0x00 && rbuf[1] == 0x00) {
|
2003-02-20 12:51:07 +00:00
|
|
|
struct sc_card_driver *drv;
|
2010-03-15 12:17:13 +00:00
|
|
|
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL,
|
|
|
|
"SELECT FILE seems to return Schlumberger 'flex stuff\n");
|
2003-12-08 10:54:55 +00:00
|
|
|
drv = sc_get_cryptoflex_driver();
|
2002-01-08 13:56:50 +00:00
|
|
|
card->ops->select_file = drv->ops->select_file;
|
2009-09-15 07:51:50 +00:00
|
|
|
return SC_SUCCESS;
|
2002-01-08 13:56:50 +00:00
|
|
|
}
|
2009-09-15 07:51:50 +00:00
|
|
|
return SC_SUCCESS;
|
2001-12-25 20:45:48 +00:00
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
static int default_init(sc_card_t *card)
|
2001-12-25 20:45:48 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
2009-11-13 19:01:21 +00:00
|
|
|
card->name = "Unsupported card";
|
2002-02-24 19:32:14 +00:00
|
|
|
card->drv_data = NULL;
|
2001-12-25 20:45:48 +00:00
|
|
|
r = autodetect_class(card);
|
|
|
|
if (r) {
|
2010-03-15 12:17:13 +00:00
|
|
|
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "unable to determine the right class byte\n");
|
2001-12-25 20:45:48 +00:00
|
|
|
return SC_ERROR_INVALID_CARD;
|
|
|
|
}
|
|
|
|
|
2009-09-15 07:51:50 +00:00
|
|
|
return SC_SUCCESS;
|
2001-12-25 20:45:48 +00:00
|
|
|
}
|
|
|
|
|
2003-02-20 12:51:07 +00:00
|
|
|
static struct sc_card_driver * sc_get_driver(void)
|
2001-12-25 20:45:48 +00:00
|
|
|
{
|
2003-02-20 12:51:07 +00:00
|
|
|
struct sc_card_driver *iso_drv = sc_get_iso7816_driver();
|
2001-12-25 20:45:48 +00:00
|
|
|
|
|
|
|
default_ops = *iso_drv->ops;
|
|
|
|
default_ops.match_card = default_match_card;
|
|
|
|
default_ops.init = default_init;
|
|
|
|
|
2002-10-19 14:04:52 +00:00
|
|
|
return &default_drv;
|
2001-12-25 20:45:48 +00:00
|
|
|
}
|
|
|
|
|
2003-02-20 12:51:07 +00:00
|
|
|
struct sc_card_driver * sc_get_default_driver(void)
|
2001-12-25 20:45:48 +00:00
|
|
|
{
|
|
|
|
return sc_get_driver();
|
|
|
|
}
|