summaryrefslogtreecommitdiff
path: root/lib/libsectok/sc7816.c
blob: 113d2763c09ba5b98869e6f33d912e0237d9d6fd (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* $Id: sc7816.c,v 1.9 2003/04/02 22:57:51 deraadt Exp $ */

/*
copyright 2000
the regents of the university of michigan
all rights reserved

permission is granted to use, copy, create derivative works
and redistribute this software and such derivative works
for any purpose, so long as the name of the university of
michigan is not used in any advertising or publicity
pertaining to the use or distribution of this software
without specific, written prior authorization.  if the
above copyright notice or any other identification of the
university of michigan is included in any copy of any
portion of this software, then the disclaimer below must
also be included.

this software is provided as is, without representation
from the university of michigan as to its fitness for any
purpose, and without warranty by the university of
michigan of any kind, either express or implied, including
without limitation the implied warranties of
merchantability and fitness for a particular purpose. the
regents of the university of michigan shall not be liable
for any damages, including special, indirect, incidental, or
consequential damages, with respect to any claim arising
out of or in connection with the use of the software, even
if it has been or is hereafter advised of the possibility of
such damages.
*/

/*
 * sc7816 library for use with pc/sc ifd drivers
 *
 * Jim Rees
 * Mukesh Agrawal
 * University of Michigan CITI, August 2000
 */

#ifdef __palmos__
#include <Common.h>
#include <System/SysAll.h>
#include <System/Unix/unix_stdlib.h>
#include <System/Unix/unix_string.h>
#include <UI/UIAll.h>
#include "field.h"
#else
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#endif

#ifdef SCPERF
#define SCPERF_FIRST_APPEARANCE
#endif /* SCPERF */

#include "sectok.h"
#include "sc7816.h"

char *scerrtab[] = {
    "ok",
    "no such tty",
    "out of memory",
    "timeout",
    "slag!",
    "card type not supported",
    "no card in reader",
    "not implemented",
    "error loading driver",
    "communications error",
    "reader not open",
    "unknown error",
};

int
scxopen(int rn, int flags, int *ep, char *config_path, char *driver_path)
{
    int r, sw;

    flags ^= STONOWAIT;

    r = sectok_xopen(rn, flags, config_path, driver_path, &sw);

    if (ep) {
	if (sectok_r1(sw) == 0x06)
	    *ep = sectok_r2(sw);
	else
	    *ep = SCECOMM;
    }

    return r;
}

int
scopen(int rn, int flags, int *ep)
{
    return scxopen(rn, flags, ep, NULL, NULL);
}

int
scclose(int fd)
{
    return sectok_close(fd);
}

int
sccardpresent(int fd)
{
    return sectok_cardpresent(fd);
}

int
scxreset(int fd, int flags, unsigned char *atr, int *ep)
{
    int r, sw;

    r = sectok_reset(fd, flags, atr, &sw);

    if (ep) {
	if (sectok_swOK(sw))
	    *ep = SCEOK;
	else if (sectok_r1(sw) == 0x06)
	    *ep = sectok_r2(sw);
	else
	    *ep = SCESLAG;
    }

    return r;
}

int
screset(int fd, unsigned char *atr, int *ep)
{
    return scxreset(fd, 0, atr, ep);
}

int
scrw(int fd, int cla, int ins, int p1, int p2, int ilen, unsigned char *ibuf, int olen, unsigned char *obuf, int *sw1p, int *sw2p)
{
    int n, sw;

    n = sectok_apdu(fd, cla, ins, p1, p2, ilen, ibuf, olen, obuf, &sw);
    *sw1p = sectok_r1(sw);
    *sw2p = sectok_r2(sw);
    return n;
}

int
scwrite(int fd, int cla, int ins, int p1, int p2, int p3, unsigned char *buf, int *sw1p, int *sw2p)
{
    int rv;
#ifdef SCPERF
    char *scperf_buf;

    scperf_buf = malloc (64);

    snprintf (scperf_buf, 64, "scwrite (ins %02x, p3 %02x) start", ins, p3);
    SetTime(scperf_buf);
#endif /* SCPERF */
    rv = scrw(fd, cla, ins, p1, p2, p3, buf, 0, NULL, sw1p, sw2p);

#ifdef SCPERF
    SetTime("scwrite() end");
#endif /* SCPERF */
    return (rv >= 0) ? p3 : -1;
}

int
scread(int fd, int cla, int ins, int p1, int p2, int p3, unsigned char *buf, int *sw1p, int *sw2p)
{
    int rv;
#ifdef SCPERF
    char *scperf_buf;

    scperf_buf = malloc (64);

    snprintf (scperf_buf, 64, "scread (ins %02x, p3 %02x) start", ins, p3);
    SetTime(scperf_buf);
#endif /* SCPERF */
    rv = scrw(fd, cla, ins, p1, p2, 0, NULL, p3, buf, sw1p, sw2p);

#ifdef SCPERF
    SetTime("scread() end");
#endif /* SCPERF */
    return rv;
}

#ifndef __palmos__
int
parse_input(char *ibuf, unsigned char *obuf, int olen)
{
    return sectok_parse_input(ibuf, obuf, olen);
}

int
get_input(FILE *f, unsigned char *obuf, int omin, int olen)
{
    return sectok_get_input(f, obuf, omin, olen);
}
#endif

void
print_r1r2(int r1, int r2)
{
    printf("%s\n", get_r1r2s(r1, r2));
}

char *
get_r1r2s(int r1, int r2)
{
    return sectok_get_sw(sectok_mksw(r1, r2));
}

#ifndef __palmos__
int
fdump_reply(FILE *f, unsigned char *p, int n, int r1, int r2)
{
    return sectok_fdump_reply(f, p, n, sectok_mksw(r1, r2));
}

int
dump_reply(unsigned char *p, int n, int r1, int r2)
{
    return sectok_fdump_reply(stdout, p, n, sectok_mksw(r1, r2));
}
#endif