1 | /*
|
---|
2 | ctapi.c
|
---|
3 | CT-API interface implementation for multiple card-terminals
|
---|
4 |
|
---|
5 | This file is part of the Unix driver for Towitoko smartcard readers
|
---|
6 | Copyright (C) 1998 1999 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 "defines.h"
|
---|
26 | #include "ctapi.h"
|
---|
27 | #include "ctbcs.h"
|
---|
28 | #include "ct_list.h"
|
---|
29 | #include "cardterminal.h"
|
---|
30 | #include "ct_slot.h"
|
---|
31 | #include <stdio.h>
|
---|
32 | #include <string.h>
|
---|
33 | #ifdef HAVE_PTHREAD_H
|
---|
34 | #include <pthread.h>
|
---|
35 | #endif
|
---|
36 |
|
---|
37 | /*
|
---|
38 | * Not exported variables definition
|
---|
39 | */
|
---|
40 |
|
---|
41 | /* Linked list of card-terminals */
|
---|
42 | static CT_List *ct_list = NULL;
|
---|
43 |
|
---|
44 | #ifdef HAVE_PTHREAD_H
|
---|
45 | static pthread_mutex_t ct_list_mutex = PTHREAD_MUTEX_INITIALIZER;
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | /*
|
---|
49 | * Exported functions definition
|
---|
50 | */
|
---|
51 |
|
---|
52 | char CT_init (unsigned short ctn, unsigned short pn)
|
---|
53 | {
|
---|
54 | CardTerminal *ct;
|
---|
55 | char ret;
|
---|
56 | bool ct_list_empty;
|
---|
57 |
|
---|
58 | #ifdef HAVE_PTHREAD_H
|
---|
59 | pthread_mutex_lock (&ct_list_mutex);
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | /* Check that ctn is not in use */
|
---|
63 | if (CT_List_GetCardTerminal (ct_list, ctn) == NULL)
|
---|
64 | {
|
---|
65 | /* Create a new CardTerminal */
|
---|
66 | ct = CardTerminal_New ();
|
---|
67 |
|
---|
68 | if (ct != NULL)
|
---|
69 | {
|
---|
70 | /* Initialize CardTerminal */
|
---|
71 | ret = CardTerminal_Init (ct, pn);
|
---|
72 |
|
---|
73 | /* Add CardTerminal to list */
|
---|
74 | if (ret == OK)
|
---|
75 | {
|
---|
76 | /* See if list is initialised */
|
---|
77 | ct_list_empty = (ct_list == NULL);
|
---|
78 |
|
---|
79 | if (ct_list_empty)
|
---|
80 | ct_list = CT_List_New ();
|
---|
81 |
|
---|
82 | /* Add the CardTerminal to the list */
|
---|
83 | if (!CT_List_AddCardTerminal (ct_list, ct, ctn))
|
---|
84 | {
|
---|
85 | CardTerminal_Close (ct);
|
---|
86 | CardTerminal_Delete (ct);
|
---|
87 |
|
---|
88 | if (ct_list_empty)
|
---|
89 | {
|
---|
90 | CT_List_Delete (ct_list);
|
---|
91 | ct_list = NULL;
|
---|
92 | }
|
---|
93 |
|
---|
94 | ret = ERR_MEMORY;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | else
|
---|
98 | {
|
---|
99 | CardTerminal_Delete(ct);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | else
|
---|
103 | {
|
---|
104 | ret = ERR_MEMORY;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | else
|
---|
108 | {
|
---|
109 | ret = ERR_CT;
|
---|
110 | }
|
---|
111 |
|
---|
112 | #ifdef HAVE_PTHREAD_H
|
---|
113 | pthread_mutex_unlock (&ct_list_mutex);
|
---|
114 | #endif
|
---|
115 |
|
---|
116 | #ifdef DEBUG_CTAPI
|
---|
117 | printf ("CTAPI: CT_init(ctn=%u, pn=%u)=%d\n", ctn, pn, ret);
|
---|
118 | #endif
|
---|
119 |
|
---|
120 | return ret;
|
---|
121 | }
|
---|
122 |
|
---|
123 | char CT_close (unsigned short ctn)
|
---|
124 | {
|
---|
125 | CardTerminal *ct;
|
---|
126 | char ret;
|
---|
127 |
|
---|
128 | #ifdef HAVE_PTHREAD_H
|
---|
129 | pthread_mutex_lock (&ct_list_mutex);
|
---|
130 | #endif
|
---|
131 |
|
---|
132 | /* Get the card-terminal */
|
---|
133 | ct = CT_List_GetCardTerminal (ct_list, ctn);
|
---|
134 |
|
---|
135 | if (ct != NULL)
|
---|
136 | {
|
---|
137 | /* Close CardTerminal */
|
---|
138 | ret = CardTerminal_Close(ct);
|
---|
139 |
|
---|
140 | /* Remove card-terminal from list */
|
---|
141 | CT_List_RemoveCardTerminal (ct_list, ctn);
|
---|
142 |
|
---|
143 | /* Delete the list if there are no more card-terminals */
|
---|
144 | if (CT_List_GetNumberOfElements (ct_list) == 0)
|
---|
145 | {
|
---|
146 | CT_List_Delete (ct_list);
|
---|
147 | ct_list = NULL;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | else
|
---|
151 | {
|
---|
152 | ret = ERR_CT;
|
---|
153 | }
|
---|
154 |
|
---|
155 | #ifdef HAVE_PTHREAD_H
|
---|
156 | pthread_mutex_unlock (&ct_list_mutex);
|
---|
157 | #endif
|
---|
158 |
|
---|
159 | #ifdef DEBUG_CTAPI
|
---|
160 | printf ("CTAPI: CT_close(ctn=%d)=%u\n", ctn, ret);
|
---|
161 | #endif
|
---|
162 |
|
---|
163 | return ret;
|
---|
164 | }
|
---|
165 |
|
---|
166 | char CT_data (unsigned short ctn, unsigned char *dad, unsigned char *sad, unsigned short lc, unsigned char *cmd, unsigned short *lr, unsigned char *rsp)
|
---|
167 | {
|
---|
168 | CardTerminal *ct;
|
---|
169 | CT_Slot *slot;
|
---|
170 | APDU_Cmd *apdu_cmd;
|
---|
171 | APDU_Rsp *apdu_rsp = NULL;
|
---|
172 | int remain;
|
---|
173 | unsigned char aux;
|
---|
174 | char ret;
|
---|
175 |
|
---|
176 | #ifdef DEBUG_CTAPI
|
---|
177 | int i;
|
---|
178 |
|
---|
179 | printf ("CTAPI: CT_data(ctn=%u, *dad=0x%02X, *sad=0x%02X, lc=%u, *cmd={", ctn, *dad, *sad, lc);
|
---|
180 |
|
---|
181 | for (i=0; i<lc; i++)
|
---|
182 | printf ("%02X ", cmd[i]);
|
---|
183 |
|
---|
184 | printf ("}, *lr=%u, rsp=[])\n", *lr);
|
---|
185 | #endif
|
---|
186 |
|
---|
187 | #ifdef HAVE_PTHREAD_H
|
---|
188 | pthread_mutex_lock (&ct_list_mutex);
|
---|
189 | #endif
|
---|
190 |
|
---|
191 | /* Get card-terminal */
|
---|
192 | ct = CT_List_GetCardTerminal (ct_list, ctn);
|
---|
193 |
|
---|
194 | #ifdef HAVE_PTHREAD_H
|
---|
195 | pthread_mutex_unlock (&ct_list_mutex);
|
---|
196 | #endif
|
---|
197 |
|
---|
198 | if (ct != NULL)
|
---|
199 | {
|
---|
200 | /* Create a command APDU */
|
---|
201 | apdu_cmd = APDU_Cmd_New (cmd, lc);
|
---|
202 |
|
---|
203 | if (apdu_cmd != NULL)
|
---|
204 | {
|
---|
205 |
|
---|
206 | #ifdef HAVE_PTHREAD_H
|
---|
207 | pthread_mutex_lock (CardTerminal_GetMutex(ct));
|
---|
208 | #endif
|
---|
209 |
|
---|
210 | /* Command goes to the reader */
|
---|
211 | if ((*dad) == 1)
|
---|
212 | {
|
---|
213 | /* CT-BCS command */
|
---|
214 | ret = CardTerminal_Command (ct, apdu_cmd, &apdu_rsp);
|
---|
215 |
|
---|
216 | (*sad) = 1;
|
---|
217 | (*dad) = (*sad);
|
---|
218 | }
|
---|
219 | else /* Command goes to an ICC */
|
---|
220 | {
|
---|
221 | /* Get the slot */
|
---|
222 | slot = CardTerminal_GetSlot(ct, ((*dad)==0)? 0: (*dad)-1);
|
---|
223 |
|
---|
224 | if (slot != NULL)
|
---|
225 | {
|
---|
226 | /* ICC command */
|
---|
227 | ret = CT_Slot_Command (slot, apdu_cmd, &apdu_rsp);
|
---|
228 |
|
---|
229 | if (CT_Slot_GetICCType (slot) != CT_SLOT_NULL)
|
---|
230 | {
|
---|
231 | aux = (*sad);
|
---|
232 | (*sad) = (*dad);
|
---|
233 | (*dad) = aux;
|
---|
234 | }
|
---|
235 | else
|
---|
236 | {
|
---|
237 | (*dad) = (*sad);
|
---|
238 | (*sad) = 1;
|
---|
239 | }
|
---|
240 | }
|
---|
241 | else
|
---|
242 | {
|
---|
243 | /* Invalid DAD address */
|
---|
244 | (*dad) = (*sad);
|
---|
245 | (*sad) = 1;
|
---|
246 | apdu_rsp = NULL;
|
---|
247 |
|
---|
248 | ret = ERR_INVALID;
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | #ifdef HAVE_PTHREAD_H
|
---|
253 | pthread_mutex_unlock (CardTerminal_GetMutex(ct));
|
---|
254 | #endif
|
---|
255 |
|
---|
256 | if (apdu_rsp != NULL)
|
---|
257 | {
|
---|
258 | /* Copy APDU data to rsp */
|
---|
259 | remain = MAX ((short)APDU_Rsp_RawLen(apdu_rsp) - (*lr),0);
|
---|
260 |
|
---|
261 | if (remain > 0)
|
---|
262 | ret = ERR_MEMORY;
|
---|
263 |
|
---|
264 | (*lr) = MIN ((*lr), (short)APDU_Rsp_RawLen (apdu_rsp));
|
---|
265 |
|
---|
266 | memcpy (rsp, APDU_Rsp_Raw (apdu_rsp) + remain, (*lr));
|
---|
267 |
|
---|
268 | /* Delete response APDU */
|
---|
269 | APDU_Rsp_Delete (apdu_rsp);
|
---|
270 | }
|
---|
271 | else
|
---|
272 | {
|
---|
273 | (*lr) = 0;
|
---|
274 | }
|
---|
275 |
|
---|
276 | /* Delete command APDU */
|
---|
277 | APDU_Cmd_Delete (apdu_cmd);
|
---|
278 | }
|
---|
279 | else
|
---|
280 | {
|
---|
281 | ret = ERR_MEMORY;
|
---|
282 | }
|
---|
283 | }
|
---|
284 | else
|
---|
285 | {
|
---|
286 | ret = ERR_CT;
|
---|
287 | }
|
---|
288 |
|
---|
289 | #ifdef DEBUG_CTAPI
|
---|
290 | printf ("CTAPI: CT_data(ctn=%u, *dad=0x%02X, *sad=0x%02X, lc=%u, *cmd={}, *lr=%u, rsp={", ctn, *dad, *sad, lc, *lr);
|
---|
291 |
|
---|
292 | for (i=0; i<*lr; i++)
|
---|
293 | printf ("%02X ", rsp[i]);
|
---|
294 |
|
---|
295 | printf ("})=%d\n", ret);
|
---|
296 | #endif
|
---|
297 |
|
---|
298 | return ret;
|
---|
299 | }
|
---|