pcsc-lite  1.8.13
prothandler.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
5  * David Corcoran <corcoran@musclecard.com>
6  * Copyright (C) 2004-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 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $Id: prothandler.c 7004 2014-10-02 09:26:36Z rousseau $
33  */
34 
40 #include "config.h"
41 #include <string.h>
42 
43 #include "misc.h"
44 #include "pcscd.h"
45 #include "debuglog.h"
46 #include "readerfactory.h"
47 #include "prothandler.h"
48 #include "atrhandler.h"
49 #include "ifdwrapper.h"
50 #include "eventhandler.h"
51 
62 DWORD PHSetProtocol(struct ReaderContext * rContext,
63  DWORD dwPreferred, UCHAR ucAvailable, UCHAR ucDefault)
64 {
65  DWORD protocol;
66  LONG rv;
67  UCHAR ucChosen;
68 
69  /* App has specified no protocol */
70  if (dwPreferred == 0)
71  return SET_PROTOCOL_WRONG_ARGUMENT;
72 
73  /* requested protocol is not available */
74  if (! (dwPreferred & ucAvailable))
75  {
76  /* Note:
77  * dwPreferred must be either SCARD_PROTOCOL_T0 or SCARD_PROTOCOL_T1
78  * if dwPreferred == SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1 the test
79  * (SCARD_PROTOCOL_T0 == dwPreferred) will not work as expected
80  * and the debug message will not be correct.
81  *
82  * This case may only occur if
83  * dwPreferred == SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1
84  * and ucAvailable == 0 since we have (dwPreferred & ucAvailable) == 0
85  * and the case ucAvailable == 0 should never occur (the card is at
86  * least T=0 or T=1)
87  */
88  Log2(PCSC_LOG_ERROR, "Protocol T=%d requested but unsupported by the card",
89  (SCARD_PROTOCOL_T0 == dwPreferred) ? 0 : 1);
90  return SET_PROTOCOL_WRONG_ARGUMENT;
91  }
92 
93  /* set default value */
94  protocol = ucDefault;
95 
96  /* keep only the available protocols */
97  dwPreferred &= ucAvailable;
98 
99  /* we try to use T=1 first */
100  if (dwPreferred & SCARD_PROTOCOL_T1)
101  ucChosen = SCARD_PROTOCOL_T1;
102  else
103  if (dwPreferred & SCARD_PROTOCOL_T0)
104  ucChosen = SCARD_PROTOCOL_T0;
105  else
106  /* App wants unsupported protocol */
107  return SET_PROTOCOL_WRONG_ARGUMENT;
108 
109  Log2(PCSC_LOG_INFO, "Attempting PTS to T=%d",
110  (SCARD_PROTOCOL_T0 == ucChosen ? 0 : 1));
111  rv = IFDSetPTS(rContext, ucChosen, 0x00, 0x00, 0x00, 0x00);
112 
113  if (IFD_SUCCESS == rv)
114  protocol = ucChosen;
115  else
116  if (IFD_NOT_SUPPORTED == rv)
117  Log2(PCSC_LOG_INFO, "PTS not supported by driver, using T=%d",
118  (SCARD_PROTOCOL_T0 == protocol) ? 0 : 1);
119  else
120  if (IFD_PROTOCOL_NOT_SUPPORTED == rv)
121  Log2(PCSC_LOG_INFO, "PTS protocol not supported, using T=%d",
122  (SCARD_PROTOCOL_T0 == protocol) ? 0 : 1);
123  else
124  {
125  Log3(PCSC_LOG_INFO, "PTS failed (%ld), using T=%d", rv,
126  (SCARD_PROTOCOL_T0 == protocol) ? 0 : 1);
127 
128  /* ISO 7816-3:1997 ch. 7.2 PPS protocol page 14
129  * - If the PPS exchange is unsuccessful, then the interface device
130  * shall either reset or reject the card.
131  */
132  return SET_PROTOCOL_PPS_FAILED;
133  }
134 
135  return protocol;
136 }
137 
LONG IFDSetPTS(READER_CONTEXT *rContext, DWORD dwProtocol, UCHAR ucFlags, UCHAR ucPTS1, UCHAR ucPTS2, UCHAR ucPTS3)
Set the protocol type selection (PTS).
Definition: ifdwrapper.c:69
#define IFD_NOT_SUPPORTED
request is not supported
Definition: ifdhandler.h:364
This handles protocol defaults, PTS, etc.
This wraps the dynamic ifdhandler functions.
#define SCARD_PROTOCOL_T1
T=1 active protocol.
Definition: pcsclite.h:176
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:175
This handles card insertion/removal events, updates ATR, protocol, and status information.
DWORD PHSetProtocol(struct ReaderContext *rContext, DWORD dwPreferred, UCHAR ucAvailable, UCHAR ucDefault)
Determine which protocol to use.
Definition: prothandler.c:62
This keeps a list of defines for pcsc-lite.
This keeps track of a list of currently available reader structures.
#define IFD_PROTOCOL_NOT_SUPPORTED
requested protocol not supported
Definition: ifdhandler.h:357
This handles debugging.
#define IFD_SUCCESS
no error
Definition: ifdhandler.h:351