1 | /*
|
---|
2 | protocol_t1.c
|
---|
3 | Handling of ISO 7816 T=1 protocol
|
---|
4 |
|
---|
5 | This file is part of the Unix driver for Towitoko smartcard readers
|
---|
6 | Copyright (C) 2000 Carlos Prados <cprados@yahoo.com>
|
---|
7 |
|
---|
8 | This version is modified by doz21 to work in a special manner ;)
|
---|
9 |
|
---|
10 | This library is free software; you can redistribute it and/or
|
---|
11 | modify it under the terms of the GNU Lesser General Public
|
---|
12 | License as published by the Free Software Foundation; either
|
---|
13 | version 2 of the License, or (at your option) any later version.
|
---|
14 |
|
---|
15 | This library is distributed in the hope that it will be useful,
|
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
18 | Lesser General Public License for more details.
|
---|
19 |
|
---|
20 | You should have received a copy of the GNU Lesser General Public
|
---|
21 | License along with this library; if not, write to the Free Software
|
---|
22 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include <stdio.h>
|
---|
26 | #include <stdlib.h>
|
---|
27 | #include <string.h>
|
---|
28 | #include "../globals.h"
|
---|
29 | #include "defines.h"
|
---|
30 | #include "protocol_t1.h"
|
---|
31 | #include "t1_block.h"
|
---|
32 | #include "icc_async.h"
|
---|
33 |
|
---|
34 | /*
|
---|
35 | * Not exported functions declaration
|
---|
36 | */
|
---|
37 |
|
---|
38 | static int
|
---|
39 | Protocol_T1_SendBlock (T1_Block * block);
|
---|
40 |
|
---|
41 | static int
|
---|
42 | Protocol_T1_ReceiveBlock (T1_Block ** block);
|
---|
43 |
|
---|
44 | static int
|
---|
45 | Protocol_T1_UpdateBWT (unsigned short BWT);
|
---|
46 |
|
---|
47 | /*
|
---|
48 | * Exproted funtions definition
|
---|
49 | */
|
---|
50 |
|
---|
51 | int
|
---|
52 | Protocol_T1_Command (APDU_Cmd * cmd, APDU_Rsp ** rsp)
|
---|
53 | {
|
---|
54 | T1_Block *block;
|
---|
55 | BYTE *buffer, rsp_type, bytes, nr, wtx;
|
---|
56 | unsigned short counter;
|
---|
57 | int ret;
|
---|
58 | bool more;
|
---|
59 | if (APDU_Cmd_Ins(cmd) == T1_BLOCK_S_IFS_REQ)
|
---|
60 | {
|
---|
61 | BYTE inf = APDU_Cmd_P2(cmd);
|
---|
62 |
|
---|
63 | /* Create an IFS request S-Block */
|
---|
64 | block = T1_Block_NewSBlock (T1_BLOCK_S_IFS_REQ, 1, &inf);
|
---|
65 |
|
---|
66 | //#ifdef DEBUG_PROTOCOL
|
---|
67 | cs_debug ("Protocol: Sending block S(IFS request, %d)\n", inf);
|
---|
68 | //#endif
|
---|
69 | /* Send IFSD request */
|
---|
70 | ret = Protocol_T1_SendBlock (block);
|
---|
71 |
|
---|
72 | /* Delete block */
|
---|
73 | T1_Block_Delete (block);
|
---|
74 |
|
---|
75 | /* Receive a block */
|
---|
76 | ret = Protocol_T1_ReceiveBlock (&block);
|
---|
77 |
|
---|
78 | if (ret == PROTOCOL_T1_OK)
|
---|
79 | {
|
---|
80 | rsp_type = T1_Block_GetType (block);
|
---|
81 |
|
---|
82 | /* Positive IFS Response S-Block received */
|
---|
83 | if (rsp_type == T1_BLOCK_S_IFS_RES)
|
---|
84 | {
|
---|
85 | /* Update IFSD value */
|
---|
86 | inf = (*T1_Block_GetInf (block));
|
---|
87 | //#ifdef DEBUG_PROTOCOL
|
---|
88 | cs_debug ("Protocol: Received block S(IFS response, %d)\n", inf);
|
---|
89 | //#endif
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | return ret;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /* Calculate the number of bytes to send */
|
---|
97 | counter = 0;
|
---|
98 | bytes = MIN (APDU_Cmd_RawLen (cmd), ifsc);
|
---|
99 |
|
---|
100 | /* See if chaining is needed */
|
---|
101 | more = (APDU_Cmd_RawLen (cmd) > ifsc);
|
---|
102 |
|
---|
103 | /* Increment ns */
|
---|
104 | ns = (ns + 1) %2;
|
---|
105 |
|
---|
106 | /* Create an I-Block */
|
---|
107 | block = T1_Block_NewIBlock (bytes, APDU_Cmd_Raw (cmd), ns, more);
|
---|
108 |
|
---|
109 | #ifdef DEBUG_PROTOCOL
|
---|
110 | printf ("Sending block I(%d,%d)\n", ns, more);
|
---|
111 | #endif
|
---|
112 |
|
---|
113 | /* Send a block */
|
---|
114 | ret = Protocol_T1_SendBlock (block);
|
---|
115 |
|
---|
116 | /* Delete I-block */
|
---|
117 | T1_Block_Delete (block);
|
---|
118 |
|
---|
119 | while ((ret == PROTOCOL_T1_OK) && more)
|
---|
120 | {
|
---|
121 | /* Receive a block */
|
---|
122 | ret = Protocol_T1_ReceiveBlock (&block);
|
---|
123 |
|
---|
124 | if (ret == PROTOCOL_T1_OK)
|
---|
125 | {
|
---|
126 | rsp_type = T1_Block_GetType (block);
|
---|
127 |
|
---|
128 | /* Positive ACK R-Block received */
|
---|
129 | if (rsp_type == T1_BLOCK_R_OK)
|
---|
130 | {
|
---|
131 | #ifdef DEBUG_PROTOCOL
|
---|
132 | printf ("Protocol: Received block R(%d)\n", T1_Block_GetNR (block));
|
---|
133 | #endif
|
---|
134 | /* Delete block */
|
---|
135 | T1_Block_Delete (block);
|
---|
136 |
|
---|
137 | /* Increment ns */
|
---|
138 | ns = (ns + 1) % 2;
|
---|
139 |
|
---|
140 | /* Calculate the number of bytes to send */
|
---|
141 | counter += bytes;
|
---|
142 | bytes = MIN (APDU_Cmd_RawLen (cmd) - counter, ifsc);
|
---|
143 |
|
---|
144 | /* See if chaining is needed */
|
---|
145 | more = (APDU_Cmd_RawLen (cmd) - counter > ifsc);
|
---|
146 |
|
---|
147 | /* Create an I-Block */
|
---|
148 | block =
|
---|
149 | T1_Block_NewIBlock (bytes, APDU_Cmd_Raw (cmd) + counter,
|
---|
150 | ns, more);
|
---|
151 | #ifdef DEBUG_PROTOCOL
|
---|
152 | printf ("Protocol: Sending block I(%d,%d)\n", ns, more);
|
---|
153 | #endif
|
---|
154 | /* Send a block */
|
---|
155 | ret = Protocol_T1_SendBlock (block);
|
---|
156 |
|
---|
157 | /* Delete I-block */
|
---|
158 | T1_Block_Delete (block);
|
---|
159 | }
|
---|
160 |
|
---|
161 | else
|
---|
162 | {
|
---|
163 | /* Delete block */
|
---|
164 | T1_Block_Delete (block);
|
---|
165 |
|
---|
166 | ret = PROTOCOL_T1_NOT_IMPLEMENTED;
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | else
|
---|
171 | {
|
---|
172 | ret = PROTOCOL_T1_NOT_IMPLEMENTED;
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | /* Reset counter */
|
---|
177 | buffer = NULL;
|
---|
178 | counter = 0;
|
---|
179 | more = TRUE;
|
---|
180 | wtx = 0;
|
---|
181 |
|
---|
182 | while ((ret == PROTOCOL_T1_OK) && more)
|
---|
183 | {
|
---|
184 | if (wtx > 1)
|
---|
185 | Protocol_T1_UpdateBWT (wtx * BWT);
|
---|
186 |
|
---|
187 | /* Receive a block */
|
---|
188 | ret = Protocol_T1_ReceiveBlock (&block);
|
---|
189 |
|
---|
190 | if (wtx > 1)
|
---|
191 | {
|
---|
192 | Protocol_T1_UpdateBWT (BWT);
|
---|
193 | wtx = 0;
|
---|
194 | }
|
---|
195 |
|
---|
196 | if (ret == PROTOCOL_T1_OK)
|
---|
197 | {
|
---|
198 | rsp_type = T1_Block_GetType (block);
|
---|
199 |
|
---|
200 | if (rsp_type == T1_BLOCK_I)
|
---|
201 | {
|
---|
202 | #ifdef DEBUG_PROTOCOL
|
---|
203 | printf ("Protocol: Received block I(%d,%d)\n",
|
---|
204 | T1_Block_GetNS(block), T1_Block_GetMore (block));
|
---|
205 | #endif
|
---|
206 | /* Calculate nr */
|
---|
207 | nr = (T1_Block_GetNS (block) + 1) % 2;
|
---|
208 |
|
---|
209 | /* Save inf field */
|
---|
210 | bytes = T1_Block_GetLen (block);
|
---|
211 | buffer = (BYTE *) realloc(buffer, counter + bytes);
|
---|
212 | memcpy (buffer + counter, T1_Block_GetInf (block), bytes);
|
---|
213 | counter += bytes;
|
---|
214 |
|
---|
215 | /* See if chaining is requested */
|
---|
216 | more = T1_Block_GetMore (block);
|
---|
217 |
|
---|
218 | /* Delete block */
|
---|
219 | T1_Block_Delete (block);
|
---|
220 |
|
---|
221 | if (more)
|
---|
222 | {
|
---|
223 | /* Create an R-Block */
|
---|
224 | block = T1_Block_NewRBlock (T1_BLOCK_R_OK, nr);
|
---|
225 | #ifdef DEBUG_PROTOCOL
|
---|
226 | printf ("Protocol: Sending block R(%d)\n", nr);
|
---|
227 | #endif
|
---|
228 | /* Send R-Block */
|
---|
229 | ret = Protocol_T1_SendBlock (block);
|
---|
230 |
|
---|
231 | /* Delete I-block */
|
---|
232 | T1_Block_Delete (block);
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | /* WTX Request S-Block received */
|
---|
237 | else if (rsp_type == T1_BLOCK_S_WTX_REQ)
|
---|
238 | {
|
---|
239 | /* Get wtx multiplier */
|
---|
240 | wtx = (*T1_Block_GetInf (block));
|
---|
241 | #ifdef DEBUG_PROTOCOL
|
---|
242 | printf ("Protocol: Received block S(WTX request, %d)\n", wtx);
|
---|
243 | #endif
|
---|
244 | /* Delete block */
|
---|
245 | T1_Block_Delete (block);
|
---|
246 |
|
---|
247 | /* Create an WTX response S-Block */
|
---|
248 | block = T1_Block_NewSBlock (T1_BLOCK_S_WTX_RES, 1, &wtx);
|
---|
249 | #ifdef DEBUG_PROTOCOL
|
---|
250 | printf ("Protocol: Sending block S(WTX response, %d)\n", wtx);
|
---|
251 | #endif
|
---|
252 | /* Send WTX response */
|
---|
253 | ret = Protocol_T1_SendBlock (block);
|
---|
254 |
|
---|
255 | /* Delete block */
|
---|
256 | T1_Block_Delete (block);
|
---|
257 | }
|
---|
258 |
|
---|
259 | else
|
---|
260 | {
|
---|
261 | ret = PROTOCOL_T1_NOT_IMPLEMENTED;
|
---|
262 | }
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | if (ret == PROTOCOL_T1_OK)
|
---|
267 | (*rsp) = APDU_Rsp_New (buffer, counter);
|
---|
268 |
|
---|
269 | if (buffer != NULL)
|
---|
270 | free (buffer);
|
---|
271 |
|
---|
272 | return ret;
|
---|
273 | }
|
---|
274 |
|
---|
275 | /*
|
---|
276 | * Not exported functions definition
|
---|
277 | */
|
---|
278 |
|
---|
279 | static int
|
---|
280 | Protocol_T1_SendBlock (T1_Block * block)
|
---|
281 | {
|
---|
282 | BYTE *buffer;
|
---|
283 | int length, ret;
|
---|
284 |
|
---|
285 | {
|
---|
286 | /* Send T=1 block */
|
---|
287 | buffer = T1_Block_Raw (block);
|
---|
288 | length = T1_Block_RawLen (block);
|
---|
289 |
|
---|
290 | if (ICC_Async_Transmit (length, buffer))
|
---|
291 | {
|
---|
292 | ret = PROTOCOL_T1_ICC_ERROR;
|
---|
293 | }
|
---|
294 |
|
---|
295 | else
|
---|
296 | ret = PROTOCOL_T1_OK;
|
---|
297 | }
|
---|
298 |
|
---|
299 | return ret;
|
---|
300 | }
|
---|
301 |
|
---|
302 | static int
|
---|
303 | Protocol_T1_ReceiveBlock (T1_Block ** block)
|
---|
304 | {
|
---|
305 | BYTE buffer[T1_BLOCK_MAX_SIZE];
|
---|
306 | int ret;
|
---|
307 |
|
---|
308 | /* Receive four mandatory bytes */
|
---|
309 | if (ICC_Async_Receive (4, buffer))
|
---|
310 | {
|
---|
311 | ret = PROTOCOL_T1_ICC_ERROR;
|
---|
312 | (*block) = NULL;
|
---|
313 | }
|
---|
314 |
|
---|
315 | else
|
---|
316 | {
|
---|
317 | if (buffer[2] != 0x00)
|
---|
318 | {
|
---|
319 | /* Set timings to read the remaining block */
|
---|
320 | Protocol_T1_UpdateBWT (CWT);
|
---|
321 |
|
---|
322 | /* Receive remaining bytes */
|
---|
323 | if (ICC_Async_Receive (buffer[2], buffer + 4))
|
---|
324 | {
|
---|
325 | (*block) = NULL;
|
---|
326 | ret = PROTOCOL_T1_ICC_ERROR;
|
---|
327 | }
|
---|
328 |
|
---|
329 | else
|
---|
330 | {
|
---|
331 | (*block) = T1_Block_New (buffer, buffer[2] + 4);
|
---|
332 | ret = PROTOCOL_T1_OK;
|
---|
333 | }
|
---|
334 |
|
---|
335 | /* Restore timings */
|
---|
336 | Protocol_T1_UpdateBWT (BWT);
|
---|
337 | }
|
---|
338 | else
|
---|
339 | {
|
---|
340 | ret = PROTOCOL_T1_OK;
|
---|
341 | (*block) = T1_Block_New (buffer, 4);
|
---|
342 | }
|
---|
343 | }
|
---|
344 |
|
---|
345 | return ret;
|
---|
346 | }
|
---|
347 |
|
---|
348 | static int
|
---|
349 | Protocol_T1_UpdateBWT (unsigned short bwt)
|
---|
350 | {
|
---|
351 | if (ICC_Async_SetTimings (bwt))
|
---|
352 | return PROTOCOL_T1_ICC_ERROR;
|
---|
353 |
|
---|
354 | return PROTOCOL_T1_OK;
|
---|
355 | }
|
---|