pcsc-lite  1.8.11
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 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: prothandler.c 6851 2014-02-14 15:43:32Z rousseau $
36  */
37 
43 #include "config.h"
44 #include <string.h>
45 
46 #include "misc.h"
47 #include "pcscd.h"
48 #include "debuglog.h"
49 #include "readerfactory.h"
50 #include "prothandler.h"
51 #include "atrhandler.h"
52 #include "ifdwrapper.h"
53 #include "eventhandler.h"
54 
65 DWORD PHSetProtocol(struct ReaderContext * rContext,
66  DWORD dwPreferred, UCHAR ucAvailable, UCHAR ucDefault)
67 {
68  DWORD protocol;
69  LONG rv;
70  UCHAR ucChosen;
71 
72  /* App has specified no protocol */
73  if (dwPreferred == 0)
74  return SET_PROTOCOL_WRONG_ARGUMENT;
75 
76  /* requested protocol is not available */
77  if (! (dwPreferred & ucAvailable))
78  {
79  /* Note:
80  * dwPreferred must be either SCARD_PROTOCOL_T0 or SCARD_PROTOCOL_T1
81  * if dwPreferred == SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1 the test
82  * (SCARD_PROTOCOL_T0 == dwPreferred) will not work as expected
83  * and the debug message will not be correct.
84  *
85  * This case may only occur if
86  * dwPreferred == SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1
87  * and ucAvailable == 0 since we have (dwPreferred & ucAvailable) == 0
88  * and the case ucAvailable == 0 should never occur (the card is at
89  * least T=0 or T=1)
90  */
91  Log2(PCSC_LOG_ERROR, "Protocol T=%d requested but unsupported by the card",
92  (SCARD_PROTOCOL_T0 == dwPreferred) ? 0 : 1);
93  return SET_PROTOCOL_WRONG_ARGUMENT;
94  }
95 
96  /* set default value */
97  protocol = ucDefault;
98 
99  /* keep only the available protocols */
100  dwPreferred &= ucAvailable;
101 
102  /* we try to use T=1 first */
103  if (dwPreferred & SCARD_PROTOCOL_T1)
104  ucChosen = SCARD_PROTOCOL_T1;
105  else
106  if (dwPreferred & SCARD_PROTOCOL_T0)
107  ucChosen = SCARD_PROTOCOL_T0;
108  else
109  /* App wants unsupported protocol */
110  return SET_PROTOCOL_WRONG_ARGUMENT;
111 
112  Log2(PCSC_LOG_INFO, "Attempting PTS to T=%d",
113  (SCARD_PROTOCOL_T0 == ucChosen ? 0 : 1));
114  rv = IFDSetPTS(rContext, ucChosen, 0x00, 0x00, 0x00, 0x00);
115 
116  if (IFD_SUCCESS == rv)
117  protocol = ucChosen;
118  else
119  if (IFD_NOT_SUPPORTED == rv)
120  Log2(PCSC_LOG_INFO, "PTS not supported by driver, using T=%d",
121  (SCARD_PROTOCOL_T0 == protocol) ? 0 : 1);
122  else
123  if (IFD_PROTOCOL_NOT_SUPPORTED == rv)
124  Log2(PCSC_LOG_INFO, "PTS protocol not supported, using T=%d",
125  (SCARD_PROTOCOL_T0 == protocol) ? 0 : 1);
126  else
127  {
128  Log3(PCSC_LOG_INFO, "PTS failed (%ld), using T=%d", rv,
129  (SCARD_PROTOCOL_T0 == protocol) ? 0 : 1);
130 
131  /* ISO 7816-3:1997 ch. 7.2 PPS protocol page 14
132  * - If the PPS exchange is unsuccessful, then the interface device
133  * shall either reset or reject the card.
134  */
135  return SET_PROTOCOL_PPS_FAILED;
136  }
137 
138  return protocol;
139 }
140 
LONG IFDSetPTS(READER_CONTEXT *rContext, DWORD dwProtocol, UCHAR ucFlags, UCHAR ucPTS1, UCHAR ucPTS2, UCHAR ucPTS3)
Set the protocol type selection (PTS).
Definition: ifdwrapper.c:72
#define IFD_NOT_SUPPORTED
request is not supported
Definition: ifdhandler.h:367
This handles protocol defaults, PTS, etc.
This wraps the dynamic ifdhandler functions.
#define SCARD_PROTOCOL_T1
T=1 active protocol.
Definition: pcsclite.h:179
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
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:65
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:360
This handles debugging.
#define IFD_SUCCESS
no error
Definition: ifdhandler.h:354