1 | /*
|
---|
2 | apdu.c
|
---|
3 | ISO 7816-4 Application Layer PDU's handling
|
---|
4 |
|
---|
5 | This file is part of the Unix driver for Towitoko smartcard readers
|
---|
6 | Copyright (C) 2000 2001 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 "defines.h"
|
---|
26 | #include "apdu.h"
|
---|
27 | #include <stdlib.h>
|
---|
28 | #include <string.h>
|
---|
29 | #include "../globals.h"
|
---|
30 |
|
---|
31 | /*
|
---|
32 | * Not exported constants definiton
|
---|
33 | */
|
---|
34 |
|
---|
35 | #define APDU_MIN_CMD_SIZE 4 /* Min command size */
|
---|
36 | #define APDU_MIN_RSP_SIZE 2 /* Min response size */
|
---|
37 | #define APDU_CMD_HEADER_SIZE 4 /* Size of the header */
|
---|
38 |
|
---|
39 | /*
|
---|
40 | * Exported functions definition
|
---|
41 | */
|
---|
42 |
|
---|
43 | APDU_Cmd * APDU_Cmd_New (BYTE * data, unsigned long length)
|
---|
44 | {
|
---|
45 | APDU_Cmd *apdu;
|
---|
46 |
|
---|
47 | if ((length > APDU_MAX_CMD_SIZE))
|
---|
48 | return NULL;
|
---|
49 |
|
---|
50 | apdu = (APDU_Cmd *) malloc (sizeof (APDU_Cmd));
|
---|
51 |
|
---|
52 | if (apdu != NULL)
|
---|
53 | {
|
---|
54 | apdu->length = MAX (APDU_MIN_CMD_SIZE, length);
|
---|
55 | apdu->command = (BYTE *) calloc (apdu->length, sizeof (BYTE));
|
---|
56 |
|
---|
57 | if (apdu->command != NULL)
|
---|
58 | {
|
---|
59 | memcpy (apdu->command, data, length);
|
---|
60 | if (length < apdu->length)
|
---|
61 | memset (apdu->command + length, 0, apdu->length - length);
|
---|
62 | }
|
---|
63 | else
|
---|
64 | {
|
---|
65 | free (apdu);
|
---|
66 | apdu = NULL;
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | return apdu;
|
---|
71 | }
|
---|
72 |
|
---|
73 | void APDU_Cmd_Delete (APDU_Cmd * apdu)
|
---|
74 | {
|
---|
75 | free (apdu->command);
|
---|
76 | free (apdu);
|
---|
77 | }
|
---|
78 |
|
---|
79 | int APDU_Cmd_Case (APDU_Cmd * apdu)
|
---|
80 | {
|
---|
81 | BYTE B1;
|
---|
82 | ushort B2B3;
|
---|
83 | ulong L;
|
---|
84 | int res;
|
---|
85 |
|
---|
86 | /* Calculate length of body */
|
---|
87 | L = MAX(apdu->length - 4, 0);
|
---|
88 |
|
---|
89 | /* Case 1 */
|
---|
90 | if (L == 0)
|
---|
91 | {
|
---|
92 | res = APDU_CASE_1;
|
---|
93 | }
|
---|
94 | else
|
---|
95 | {
|
---|
96 | /* Get first byte of body */
|
---|
97 | B1 = apdu->command[4];
|
---|
98 |
|
---|
99 | if ((B1 != 0) && (L == (ulong)B1 + 1))
|
---|
100 | res = APDU_CASE_2S;
|
---|
101 | else if (L == 1)
|
---|
102 | res = APDU_CASE_3S;
|
---|
103 | else if ((B1 != 0) && (L == (ulong)B1 + 2))
|
---|
104 | res = APDU_CASE_4S;
|
---|
105 | else if ((B1 == 0) && (L>2))
|
---|
106 | {
|
---|
107 | /* Get second and third byte of body */
|
---|
108 | B2B3 = (((ushort)(apdu->command[5]) << 8) | apdu->command[6]);
|
---|
109 |
|
---|
110 | if ((B2B3 != 0) && (L == (ulong)B2B3 + 3))
|
---|
111 | res = APDU_CASE_2E;
|
---|
112 | else if (L == 3)
|
---|
113 | res = APDU_CASE_3E;
|
---|
114 | else if ((B2B3 != 0) && (L == (ulong)B2B3 + 5))
|
---|
115 | res = APDU_CASE_4E;
|
---|
116 | else
|
---|
117 | res = APDU_MALFORMED;
|
---|
118 | }
|
---|
119 | else
|
---|
120 | {
|
---|
121 | res = APDU_MALFORMED;
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | return res;
|
---|
126 | }
|
---|
127 |
|
---|
128 | BYTE APDU_Cmd_Cla (APDU_Cmd * apdu)
|
---|
129 | {
|
---|
130 | return apdu->command[0];
|
---|
131 | }
|
---|
132 |
|
---|
133 | BYTE APDU_Cmd_Ins (APDU_Cmd * apdu)
|
---|
134 | {
|
---|
135 | return apdu->command[1];
|
---|
136 | }
|
---|
137 |
|
---|
138 | BYTE APDU_Cmd_P1 (APDU_Cmd * apdu)
|
---|
139 | {
|
---|
140 | return apdu->command[2];
|
---|
141 | }
|
---|
142 |
|
---|
143 | BYTE APDU_Cmd_P2 (APDU_Cmd * apdu)
|
---|
144 | {
|
---|
145 | return apdu->command[3];
|
---|
146 | }
|
---|
147 |
|
---|
148 | unsigned long APDU_Cmd_Lc (APDU_Cmd * apdu)
|
---|
149 | {
|
---|
150 | int c;
|
---|
151 | unsigned long res;
|
---|
152 |
|
---|
153 | c = APDU_Cmd_Case (apdu);
|
---|
154 |
|
---|
155 | if ((c == APDU_CASE_1) || (c == APDU_CASE_3S) || (c == APDU_CASE_3E))
|
---|
156 | res = 0;
|
---|
157 | else if ((c == APDU_CASE_2S) || (c == APDU_CASE_4S))
|
---|
158 | res = apdu->command[4];
|
---|
159 | else if ((c == APDU_CASE_2E) || (c == APDU_CASE_4E))
|
---|
160 | res = (((unsigned long)(apdu->command[5]) << 8) | apdu->command[6]);
|
---|
161 | else
|
---|
162 | res = 0;
|
---|
163 |
|
---|
164 | return res;
|
---|
165 | }
|
---|
166 |
|
---|
167 | unsigned long APDU_Cmd_Le (APDU_Cmd * apdu)
|
---|
168 | {
|
---|
169 | int c;
|
---|
170 | unsigned long res;
|
---|
171 |
|
---|
172 | c = APDU_Cmd_Case (apdu);
|
---|
173 |
|
---|
174 | if ((c == APDU_CASE_1) || (c == APDU_CASE_2S) || (c == APDU_CASE_2E))
|
---|
175 | res = 0;
|
---|
176 | else if (c == APDU_CASE_3S)
|
---|
177 | // res = ((apdu->command[4] == 0) ? 256: apdu->command[4]);
|
---|
178 | res = apdu->command[4];
|
---|
179 | else if (c == APDU_CASE_4S)
|
---|
180 | res = ((apdu->command[apdu->length - 1] == 0) ? 256: apdu->command[apdu->length - 1]);
|
---|
181 | else if (c == APDU_CASE_3E)
|
---|
182 | res = ((((unsigned long)(apdu->command[5]) << 8) | apdu->command[6]) == 0 ? 65536 : (((unsigned long)(apdu->command[5]) << 8) | apdu->command[6]));
|
---|
183 | else if (c == APDU_CASE_4E)
|
---|
184 | res = ((((unsigned long)(apdu->command[apdu->length - 2]) << 8) | apdu->command[apdu->length - 1]) == 0 ? 65536 : (((unsigned long)(apdu->command[apdu->length - 2]) << 8) | apdu->command[apdu->length - 1]));
|
---|
185 | else
|
---|
186 | res = 0;
|
---|
187 |
|
---|
188 | return res;
|
---|
189 | }
|
---|
190 |
|
---|
191 | bool APDU_Cmd_Le_Available (APDU_Cmd * apdu)
|
---|
192 | {
|
---|
193 | int c;
|
---|
194 | bool res;
|
---|
195 |
|
---|
196 | c = APDU_Cmd_Case (apdu);
|
---|
197 |
|
---|
198 | if (c == APDU_CASE_3S)
|
---|
199 | res = (apdu->command[4] == 0);
|
---|
200 | else if (c == APDU_CASE_4S)
|
---|
201 | res = (apdu->command[apdu->length - 1] == 0);
|
---|
202 | else if (c == APDU_CASE_3E)
|
---|
203 | res = ((((unsigned long)(apdu->command[5]) << 8) | apdu->command[6]) == 0);
|
---|
204 | else if (c == APDU_CASE_4E)
|
---|
205 | res = ((((unsigned long)(apdu->command[apdu->length - 2]) << 8) | apdu->command[apdu->length - 1]) == 0);
|
---|
206 | else
|
---|
207 | res = FALSE;
|
---|
208 |
|
---|
209 | return res;
|
---|
210 | }
|
---|
211 |
|
---|
212 | BYTE * APDU_Cmd_Header (APDU_Cmd * apdu)
|
---|
213 | {
|
---|
214 | return apdu->command;
|
---|
215 | }
|
---|
216 |
|
---|
217 |
|
---|
218 | BYTE * APDU_Cmd_Data (APDU_Cmd * apdu)
|
---|
219 | {
|
---|
220 | int c;
|
---|
221 | BYTE * res;
|
---|
222 |
|
---|
223 | c = APDU_Cmd_Case (apdu);
|
---|
224 |
|
---|
225 | if ((c == APDU_CASE_1) || (c == APDU_CASE_3S) || (c == APDU_CASE_3E))
|
---|
226 | res = NULL;
|
---|
227 | else if ((c == APDU_CASE_2S) || (c == APDU_CASE_4S))
|
---|
228 | res = apdu->command + 5;
|
---|
229 | else if ((c == APDU_CASE_2E) || (c == APDU_CASE_4E))
|
---|
230 | res = apdu->command + 7;
|
---|
231 | else
|
---|
232 | res = NULL;
|
---|
233 |
|
---|
234 | return res;
|
---|
235 | }
|
---|
236 |
|
---|
237 | BYTE * APDU_Cmd_Raw (APDU_Cmd * apdu)
|
---|
238 | {
|
---|
239 | return apdu->command;
|
---|
240 | }
|
---|
241 |
|
---|
242 | unsigned long APDU_Cmd_RawLen (APDU_Cmd * apdu)
|
---|
243 | {
|
---|
244 | return apdu->length;
|
---|
245 | }
|
---|
246 |
|
---|
247 | APDU_Rsp * APDU_Rsp_New (BYTE * data, unsigned long length)
|
---|
248 | {
|
---|
249 | APDU_Rsp *apdu;
|
---|
250 |
|
---|
251 | if (length < APDU_MIN_RSP_SIZE)
|
---|
252 | return NULL;
|
---|
253 |
|
---|
254 | apdu = (APDU_Rsp *) malloc (sizeof (APDU_Rsp));
|
---|
255 |
|
---|
256 | if (apdu != NULL)
|
---|
257 | {
|
---|
258 | apdu->length = length;
|
---|
259 | apdu->response = (BYTE *) calloc (length, sizeof (BYTE));
|
---|
260 |
|
---|
261 | if (apdu->response != NULL)
|
---|
262 | {
|
---|
263 | memcpy (apdu->response, data, length);
|
---|
264 | }
|
---|
265 | else
|
---|
266 | {
|
---|
267 | free (apdu);
|
---|
268 | apdu = NULL;
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | return apdu;
|
---|
273 | }
|
---|
274 |
|
---|
275 | void APDU_Rsp_Delete (APDU_Rsp * apdu)
|
---|
276 | {
|
---|
277 | free (apdu->response);
|
---|
278 | free (apdu);
|
---|
279 | }
|
---|
280 |
|
---|
281 | BYTE APDU_Rsp_SW1 (APDU_Rsp * apdu)
|
---|
282 | {
|
---|
283 | return (apdu->response[(apdu->length) - 2]);
|
---|
284 | }
|
---|
285 |
|
---|
286 | BYTE APDU_Rsp_SW2 (APDU_Rsp * apdu)
|
---|
287 | {
|
---|
288 | return (apdu->response[(apdu->length) - 1]);
|
---|
289 | }
|
---|
290 |
|
---|
291 | unsigned long
|
---|
292 | APDU_Rsp_DataLen (APDU_Rsp * apdu)
|
---|
293 | {
|
---|
294 | return (apdu->length - 2);
|
---|
295 | }
|
---|
296 |
|
---|
297 | BYTE * APDU_Rsp_Data (APDU_Rsp * apdu)
|
---|
298 | {
|
---|
299 | return apdu->response;
|
---|
300 | }
|
---|
301 |
|
---|
302 | BYTE * APDU_Rsp_Raw (APDU_Rsp * apdu)
|
---|
303 | {
|
---|
304 | return apdu->response;
|
---|
305 | }
|
---|
306 |
|
---|
307 | unsigned long APDU_Rsp_RawLen (APDU_Rsp * apdu)
|
---|
308 | {
|
---|
309 | return apdu->length;
|
---|
310 | }
|
---|
311 |
|
---|
312 | void APDU_Rsp_TruncateData (APDU_Rsp * apdu, unsigned long length)
|
---|
313 | {
|
---|
314 | if ((length > 0) && ((signed long)length < (signed long)(apdu->length - 2)))
|
---|
315 | {
|
---|
316 | apdu->response[length] = APDU_Rsp_SW1 (apdu);
|
---|
317 | apdu->response[length + 1] = APDU_Rsp_SW2 (apdu);
|
---|
318 | apdu->length = length +2;
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | int APDU_Rsp_AppendData (APDU_Rsp * apdu1, APDU_Rsp * apdu2)
|
---|
323 | {
|
---|
324 | BYTE * response;
|
---|
325 | unsigned long length;
|
---|
326 | int ret;
|
---|
327 |
|
---|
328 | length = APDU_Rsp_DataLen(apdu1) + APDU_Rsp_RawLen(apdu2);
|
---|
329 |
|
---|
330 | if ((length > 2) && (length <= APDU_MAX_RSP_SIZE))
|
---|
331 | {
|
---|
332 | response = (BYTE *) realloc (apdu1->response, length);
|
---|
333 |
|
---|
334 | if (response != NULL)
|
---|
335 | {
|
---|
336 | memcpy (response + APDU_Rsp_DataLen (apdu1),
|
---|
337 | APDU_Rsp_Raw (apdu2), APDU_Rsp_RawLen(apdu2));
|
---|
338 |
|
---|
339 | apdu1->response = response;
|
---|
340 | apdu1->length = length;
|
---|
341 | ret = APDU_OK;
|
---|
342 | }
|
---|
343 | else
|
---|
344 | {
|
---|
345 | ret = APDU_MALFORMED;
|
---|
346 | }
|
---|
347 | }
|
---|
348 | else
|
---|
349 | {
|
---|
350 | ret = APDU_MALFORMED;
|
---|
351 | }
|
---|
352 |
|
---|
353 | return ret;
|
---|
354 | }
|
---|