-
Notifications
You must be signed in to change notification settings - Fork 705
/
card-default.c
131 lines (118 loc) · 3.41 KB
/
card-default.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* card-default.c: Support for cards with no driver
*
* Copyright (C) 2001 Juha Yrjölä <juha.yrjola@iki.fi>
*
* 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
*/
#include "sc-internal.h"
#include "sc-log.h"
static struct sc_card_operations default_ops;
static const struct sc_card_driver default_drv = {
NULL,
"Default driver for unknown cards",
"default",
&default_ops
};
static int default_finish(struct sc_card *card)
{
return 0;
}
static int default_match_card(struct sc_card *card)
{
return 1; /* always match */
}
static int autodetect_class(struct sc_card *card)
{
int classes[] = { 0x00, 0xC0, 0xB0, 0xA0 };
int class_count = sizeof(classes)/sizeof(int);
u8 rbuf[SC_MAX_APDU_BUFFER_SIZE];
struct sc_apdu apdu;
int i, r;
if (card->ctx->debug >= 2)
debug(card->ctx, "autodetecting CLA byte\n");
for (i = 0; i < class_count; i++) {
if (card->ctx->debug >= 2)
debug(card->ctx, "trying with 0x%02X\n", classes[i]);
apdu.cla = classes[i];
apdu.cse = SC_APDU_CASE_1;
apdu.ins = 0xC0;
apdu.p1 = apdu.p2 = 0;
apdu.datalen = 0;
r = sc_transmit_apdu(card, &apdu);
SC_TEST_RET(card->ctx, r, "APDU transmit failed");
if (apdu.sw1 == 0x6E)
continue;
if (apdu.sw1 == 0x90 && apdu.sw2 == 0x00)
break;
if (apdu.sw1 == 0x61)
break;
if (card->ctx->debug >= 2)
debug(card->ctx, "got strange SWs: 0x%02X 0x%02X\n",
apdu.sw1, apdu.sw2);
break;
}
if (i == class_count)
return -1;
card->cla = classes[i];
if (card->ctx->debug >= 2)
debug(card->ctx, "detected CLA byte as 0x%02X\n", card->cla);
if (apdu.resplen < 2) {
if (card->ctx->debug >= 2)
debug(card->ctx, "SELECT FILE returned %d bytes\n",
apdu.resplen);
return 0;
}
if (rbuf[0] == 0x6F) {
if (card->ctx->debug >= 2)
debug(card->ctx, "SELECT FILE seems to behave according to ISO 7816-4\n");
return 0;
}
if (rbuf[0] == 0x00 && rbuf[1] == 0x00) {
const struct sc_card_driver *drv;
if (card->ctx->debug >= 2)
debug(card->ctx, "SELECT FILE seems to return Schlumberger 'flex stuff\n");
drv = sc_get_flex_driver();
card->ops->select_file = drv->ops->select_file;
return 0;
}
return 0;
}
static int default_init(struct sc_card *card)
{
int r;
card->ops_data = NULL;
r = autodetect_class(card);
if (r) {
error(card->ctx, "unable to determine the right class byte\n");
return SC_ERROR_INVALID_CARD;
}
return 0;
}
static const struct sc_card_driver * sc_get_driver(void)
{
const struct sc_card_driver *iso_drv = sc_get_iso7816_driver();
default_ops = *iso_drv->ops;
default_ops.match_card = default_match_card;
default_ops.init = default_init;
default_ops.finish = default_finish;
return &default_drv;
}
#if 1
const struct sc_card_driver * sc_get_default_driver(void)
{
return sc_get_driver();
}
#endif