pcsc-lite  1.8.11
atrhandler.c
Go to the documentation of this file.
1 /*
2  * MUSCLE SmartCard Development ( http://pcsclite.alioth.debian.org/pcsclite.html )
3  *
4  * Copyright (C) 1999-2002
5  * David Corcoran <corcoran@musclecard.com>
6  * Copyright (C) 2002-2011
7  * Ludovic Rousseau <ludovic.rousseau@free.fr>
8  *
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions
11 are met:
12 
13 1. Redistributions of source code must retain the above copyright
14  notice, this list of conditions and the following disclaimer.
15 2. Redistributions in binary form must reproduce the above copyright
16  notice, this list of conditions and the following disclaimer in the
17  documentation and/or other materials provided with the distribution.
18 3. The name of the author may not be used to endorse or promote products
19  derived from this software without specific prior written permission.
20 
21 Changes to this license can be made only by the copyright author with
22 explicit written consent.
23 
24 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  * $Id: atrhandler.c 6851 2014-02-14 15:43:32Z rousseau $
36  */
37 
48 #include "config.h"
49 #include <string.h>
50 
51 #include "misc.h"
52 #include "pcsclite.h"
53 #include "debuglog.h"
54 #include "atrhandler.h"
55 
56 /*
57  * Uncomment the following for ATR debugging
58  * or use ./configure --enable-debugatr
59  */
60 /* #define ATR_DEBUG */
61 
71 short ATRDecodeAtr(int *availableProtocols, int *currentProtocol,
72  PUCHAR pucAtr, DWORD dwLength)
73 {
74  USHORT p;
75  UCHAR Y1i, T; /* MSN/LSN of TDi */
76  int i = 1; /* value of the index in TAi, TBi, etc. */
77 
78 #ifdef ATR_DEBUG
79  if (dwLength > 0)
80  LogXxd(PCSC_LOG_DEBUG, "ATR: ", pucAtr, dwLength);
81 #endif
82 
83  if (dwLength < 2)
84  return 0;
86  /*
87  * Zero out the bitmasks
88  */
89  *availableProtocols = SCARD_PROTOCOL_UNDEFINED;
90  *currentProtocol = SCARD_PROTOCOL_UNDEFINED;
91 
92  /*
93  * Decode the TS byte
94  */
95  if ((pucAtr[0] != 0x3F) && (pucAtr[0] != 0x3B))
96  return 0;
98  /*
99  * Here comes the platform dependant stuff
100  */
101 
102  /*
103  * Decode the T0 byte
104  */
105  Y1i = pucAtr[1] >> 4; /* Get the MSN in Y1 */
106 
107  p = 2;
108 
109  /*
110  * Examine Y1
111  */
112  do
113  {
114  short TAi, TBi, TCi, TDi; /* Interface characters */
115 
116  TAi = (Y1i & 0x01) ? pucAtr[p++] : -1;
117  TBi = (Y1i & 0x02) ? pucAtr[p++] : -1;
118  TCi = (Y1i & 0x04) ? pucAtr[p++] : -1;
119  TDi = (Y1i & 0x08) ? pucAtr[p++] : -1;
120 
121  /* We don't use TBi and TCi but we must calculate them because
122  * of the p++ in the formulae */
123  (void)TBi;
124  (void)TCi;
125 
126 #ifdef ATR_DEBUG
127  Log9(PCSC_LOG_DEBUG,
128  "TA%d: %02X, TB%d: %02X, TC%d: %02X, TD%d: %02X",
129  i, TAi, i, TBi, i, TCi, i, TDi);
130 #endif
131 
132  /*
133  * Examine TDi to determine protocol and more
134  */
135  if (TDi >= 0)
136  {
137  Y1i = TDi >> 4; /* Get the MSN in Y1 */
138  T = TDi & 0x0F; /* Get the LSN in K */
139 
140  /*
141  * Set the current protocol TD1 (first TD only)
142  */
143  if (*currentProtocol == SCARD_PROTOCOL_UNDEFINED)
144  {
145  switch (T)
146  {
147  case 0:
148  *currentProtocol = SCARD_PROTOCOL_T0;
149  break;
150  case 1:
151  *currentProtocol = SCARD_PROTOCOL_T1;
152  break;
153  default:
154  return 0;
155  }
156  }
157 
158 #ifdef ATR_DEBUG
159  Log2(PCSC_LOG_DEBUG, "T=%d Protocol Found", T);
160 #endif
161  if (0 == T)
162  {
163  *availableProtocols |= SCARD_PROTOCOL_T0;
164  }
165  else
166  if (1 == T)
167  {
168  *availableProtocols |= SCARD_PROTOCOL_T1;
169  }
170  else
171  if (15 == T)
172  {
173  *availableProtocols |= SCARD_PROTOCOL_T15;
174  }
175  else
176  {
177  /*
178  * Do nothing for now since other protocols are not
179  * supported at this time
180  */
181  }
182  }
183  else
184  Y1i = 0;
185 
186  /* test presence of TA2 */
187  if ((2 == i) && (TAi >= 0))
188  {
189  T = TAi & 0x0F;
190 #ifdef ATR_DEBUG
191  Log2(PCSC_LOG_DEBUG, "Specific mode: T=%d", T);
192 #endif
193  switch (T)
194  {
195  case 0:
196  *currentProtocol = *availableProtocols = SCARD_PROTOCOL_T0;
197  break;
198 
199  case 1:
200  *currentProtocol = *availableProtocols = SCARD_PROTOCOL_T1;
201  break;
202 
203  default:
204  return 0;
205  }
206  }
207 
208  if (p > MAX_ATR_SIZE)
209  return 0;
211  /* next interface characters index */
212  i++;
213  }
214  while (Y1i != 0);
215 
216  /*
217  * If TDx is not set then the current must be T0
218  */
219  if (*currentProtocol == SCARD_PROTOCOL_UNDEFINED)
220  {
221  *currentProtocol = SCARD_PROTOCOL_T0;
222  *availableProtocols |= SCARD_PROTOCOL_T0;
223  }
224 
225 #ifdef ATR_DEBUG
226  Log3(PCSC_LOG_DEBUG, "CurrentProtocol: %d, AvailableProtocols: %d",
227  *currentProtocol, *availableProtocols);
228 #endif
229 
230  return 1;
231 }
#define SCARD_PROTOCOL_T1
T=1 active protocol.
Definition: pcsclite.h:179
#define SCARD_PROTOCOL_T15
T=15 protocol.
Definition: pcsclite.h:181
This keeps track of smart card protocols, timing issues and Answer to Reset ATR handling.
#define SCARD_PROTOCOL_T0
T=0 active protocol.
Definition: pcsclite.h:178
short ATRDecodeAtr(int *availableProtocols, int *currentProtocol, PUCHAR pucAtr, DWORD dwLength)
parse an ATR
Definition: atrhandler.c:71
This keeps a list of defines for pcsc-lite.
#define SCARD_PROTOCOL_UNDEFINED
protocol not set
Definition: pcsclite.h:176
#define MAX_ATR_SIZE
Maximum ATR size.
Definition: pcsclite.h:64
This handles debugging.