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
|
|
|
|
*
|
2002-04-05 14:46:44 +00:00
|
|
|
|
* Copyright (C) 2001, 2002 Juha Yrj<EFBFBD>l<EFBFBD> <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
|
|
|
|
|
*/
|
|
|
|
|
|
2002-04-05 10:44:51 +00:00
|
|
|
|
#include "internal.h"
|
2004-01-29 09:36:48 +00:00
|
|
|
|
#include <string.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",
|
2001-12-25 20:45:48 +00:00
|
|
|
|
&default_ops
|
|
|
|
|
};
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
static int default_finish(sc_card_t *card)
|
2001-12-25 20:45:48 +00:00
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
if (card->ctx->debug >= 2)
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_debug(card->ctx, "autodetecting CLA byte\n");
|
2001-12-25 20:45:48 +00:00
|
|
|
|
for (i = 0; i < class_count; i++) {
|
|
|
|
|
if (card->ctx->debug >= 2)
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_debug(card->ctx, "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);
|
|
|
|
|
SC_TEST_RET(card->ctx, 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;
|
|
|
|
|
if (card->ctx->debug >= 2)
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_debug(card->ctx, "got strange SWs: 0x%02X 0x%02X\n",
|
2001-12-25 20:45:48 +00:00
|
|
|
|
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];
|
|
|
|
|
if (card->ctx->debug >= 2)
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_debug(card->ctx, "detected CLA byte as 0x%02X\n", card->cla);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
if (apdu.resplen < 2) {
|
|
|
|
|
if (card->ctx->debug >= 2)
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_debug(card->ctx, "SELECT FILE returned %d bytes\n",
|
2002-01-08 13:56:50 +00:00
|
|
|
|
apdu.resplen);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (rbuf[0] == 0x6F) {
|
|
|
|
|
if (card->ctx->debug >= 2)
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_debug(card->ctx, "SELECT FILE seems to behave according to ISO 7816-4\n");
|
2002-01-08 13:56:50 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (rbuf[0] == 0x00 && rbuf[1] == 0x00) {
|
2003-02-20 12:51:07 +00:00
|
|
|
|
struct sc_card_driver *drv;
|
2002-01-08 13:56:50 +00:00
|
|
|
|
if (card->ctx->debug >= 2)
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_debug(card->ctx, "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;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2001-12-25 20:45:48 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
2003-05-28 20:52:33 +00:00
|
|
|
|
card->name = "Unidentified 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) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(card->ctx, "unable to determine the right class byte\n");
|
2001-12-25 20:45:48 +00:00
|
|
|
|
return SC_ERROR_INVALID_CARD;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
default_ops.finish = default_finish;
|
2001-12-25 20:45:48 +00:00
|
|
|
|
|
2002-10-19 14:04:52 +00:00
|
|
|
|
return &default_drv;
|
2001-12-25 20:45:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if 1
|
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();
|
|
|
|
|
}
|
|
|
|
|
#endif
|