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
|
/* $Id: scT1.c,v 1.5 2001/07/20 15:51:45 rees Exp $ */
/*
copyright 1997, 1999, 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.
*/
/*
* T=1 protocol engine
*
* Jim Rees, University of Michigan, October 1997
*/
#ifdef __palmos__
#include <Common.h>
#include <System/SysAll.h>
#include <System/MemoryMgr.h>
#include <System/Unix/unix_stdlib.h>
#include <System/Unix/sys_socket.h>
#else
#include <stdio.h>
#endif
#include <string.h>
#ifdef TIMING
#include <sys/time.h>
#endif
#include "sectok.h"
#include "sc7816.h"
#ifdef __palmos__
#undef printf
#undef sprintf
#define printf palmprintf
#endif
int
scioT1(int ttyn, int cla, int ins, int p1, int p2, int ilen, unsigned char *ibuf, int olen, unsigned char *obuf, int *sw1p, int *sw2p)
{
int i, len, n;
unsigned char *bp, *obp, tbuf[256];
/* Send 3 bytes prologue (7816-3 sec 9.4.1 fig 16) */
bp = tbuf;
len = 5 + ilen;
/* Send APDU */
*bp++ = cla;
*bp++ = ins;
*bp++ = p1;
*bp++ = p2;
*bp++ = ilen;
/* Send data */
for (i = 0; i < ilen; i++)
*bp++ = ibuf[i];
#ifndef HUH
if (ilen) {
/* I don't understand this, but Visa card wants it */
*bp++ = 0;
len++;
}
#endif
obp = obuf ? obuf : tbuf;
n = scioT1Iblk(ttyn, len, tbuf, obp);
if (n >= 2) {
*sw1p = obp[n-2];
*sw2p = obp[n-1];
n -= 2;
}
return n;
}
int
scioT1Iblk(int ttyn, int ilen, unsigned char *ibuf, unsigned char *obuf)
{
int n;
unsigned char tbuf[256];
static unsigned char ssn;
tbuf[0] = 0;
tbuf[1] = ssn;
ssn ^= 0x40;
tbuf[2] = ilen;
memcpy(&tbuf[3], ibuf, ilen);
n = scioT1pkt(ttyn, tbuf, tbuf);
if (n < 0)
return n;
memcpy(obuf, &tbuf[3], tbuf[2]);
return tbuf[2];
}
int
scioT1pkt(int ttyn, unsigned char *ibuf, unsigned char *obuf)
{
int i, len;
unsigned char edc, *bp;
len = ibuf[2] + 3;
/* Calculate checksum */
for (i = 0, edc = 0; i < len; i++)
edc ^= ibuf[i];
ibuf[len++] = edc;
/* Wait BGT = 22 etu */
scsleep(scparam[ttyn].etu * 22 / 1000 + 1);
/* Send the packet */
scputblk(ttyn, ibuf, len);
/* Read return packet */
bp = obuf;
/* Read three byte header */
for (i = 0; i < 3; i++) {
if (scgetc(ttyn, bp++, (i == 0) ? scparam[ttyn].bwt : scparam[ttyn].cwt) != SCEOK) {
#ifdef DEBUG
printf("T=1 header read timeout\n");
#endif
return -1;
}
}
len = obuf[2];
/* Read data and edc */
for (i = 0; i < len + 1; i++) {
if (scgetc(ttyn, bp++, scparam[ttyn].cwt) != SCEOK) {
#ifdef DEBUG
printf("T=1 data read timeout\n");
#endif
return -1;
}
}
/* Check edc */
for (i = 0, edc = 0; i < len + 3; i++)
edc ^= obuf[i];
#ifdef DEBUG
if (edc != obuf[len + 3])
printf("edc mismatch, %02x != %02x\n", edc, obuf[len + 3]);
#endif
return len;
}
|