pcsc-lite 2.3.3
prothandler.c
Go to the documentation of this file.
1/*
2 * MUSCLE SmartCard Development ( https://pcsclite.apdu.fr/ )
3 *
4 * Copyright (C) 1999
5 * David Corcoran <corcoran@musclecard.com>
6 * Copyright (C) 2004-2023
7 * Ludovic Rousseau <ludovic.rousseau@free.fr>
8 *
9Redistribution and use in source and binary forms, with or without
10modification, are permitted provided that the following conditions
11are met:
12
131. Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
152. 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.
183. 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
21THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
38#include "config.h"
39
40#include "misc.h"
41#include "debuglog.h"
42#include "prothandler.h"
43#include "ifdwrapper.h"
44
55DWORD PHSetProtocol(struct ReaderContext * rContext,
56 DWORD dwPreferred, UCHAR ucAvailable, UCHAR ucDefault)
57{
58 DWORD protocol;
59 LONG rv;
60 UCHAR ucChosen;
61
62 /* App has specified no protocol */
63 if (dwPreferred == SCARD_PROTOCOL_UNDEFINED)
64 return SET_PROTOCOL_WRONG_ARGUMENT;
65
66 /* requested protocol is not available */
67 if (! (dwPreferred & ucAvailable))
68 {
69 /* Note:
70 * dwPreferred must be either SCARD_PROTOCOL_T0 or SCARD_PROTOCOL_T1
71 * if dwPreferred == SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1 the test
72 * (SCARD_PROTOCOL_T0 == dwPreferred) will not work as expected
73 * and the debug message will not be correct.
74 *
75 * This case may only occur if
76 * dwPreferred == SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1
77 * and ucAvailable == 0 since we have (dwPreferred & ucAvailable) == 0
78 * and the case ucAvailable == 0 should never occur (the card is at
79 * least T=0 or T=1)
80 */
81 Log2(PCSC_LOG_ERROR, "Protocol T=%d requested but unsupported by the card",
82 (SCARD_PROTOCOL_T0 == dwPreferred) ? 0 : 1);
83 return SET_PROTOCOL_WRONG_ARGUMENT;
84 }
85
86 /* set default value */
87 protocol = ucDefault;
88
89 /* keep only the available protocols */
90 dwPreferred &= ucAvailable;
91
92 /* we try to use T=1 first */
93 if (dwPreferred & SCARD_PROTOCOL_T1)
94 ucChosen = SCARD_PROTOCOL_T1;
95 else
96 if (dwPreferred & SCARD_PROTOCOL_T0)
97 ucChosen = SCARD_PROTOCOL_T0;
98 else
99 /* App wants unsupported protocol */
100 return SET_PROTOCOL_WRONG_ARGUMENT;
101
102again:
103 Log2(PCSC_LOG_INFO, "Attempting PTS to T=%d",
104 (SCARD_PROTOCOL_T0 == ucChosen ? 0 : 1));
105 rv = IFDSetPTS(rContext, ucChosen, 0x00, 0x00, 0x00, 0x00);
106
107 switch(rv)
108 {
109 case IFD_SUCCESS:
110 protocol = ucChosen;
111 break;
112
114 case IFD_ERROR_NOT_SUPPORTED:
115 /* protocol not supported */
116 if (protocol != dwPreferred)
117 {
118 if (protocol & dwPreferred)
119 {
120 Log3(PCSC_LOG_INFO,
121 "Set PTS failed (%ld). Using T=%d", rv,
122 (SCARD_PROTOCOL_T0 == protocol) ? 0 : 1);
123
124 /* try again with the other protocol */
125 ucChosen = protocol;
126
127 /* but no other protocol should be tried after that */
128 dwPreferred = protocol;
129
130 goto again;
131 }
132 else
133 {
134 Log2(PCSC_LOG_INFO, "Set PTS failed (%ld)", rv);
135 protocol = SET_PROTOCOL_WRONG_ARGUMENT;
136 }
137 }
138 else
139 {
140 /* no other protocol to use */
141 Log2(PCSC_LOG_INFO, "PTS protocol failed (%ld)", rv);
142 protocol = SET_PROTOCOL_PPS_FAILED;
143 }
144 break;
145
147 /* command not supported */
148 Log3(PCSC_LOG_INFO, "Set PTS failed (%ld). Using T=%d", rv,
149 (SCARD_PROTOCOL_T0 == protocol) ? 0 : 1);
150 break;
151
152 default:
153 Log2(PCSC_LOG_INFO, "Set PTS failed (%ld)", rv);
154
155 /* ISO 7816-3:1997 ch. 7.2 PPS protocol page 14
156 * - If the PPS exchange is unsuccessful, then the interface
157 * device shall either reset or reject the card.
158 */
159 protocol = SET_PROTOCOL_PPS_FAILED;
160 }
161
162 return protocol;
163}
164
This handles debugging.
#define IFD_NOT_SUPPORTED
request is not supported
Definition ifdhandler.h:364
#define IFD_PROTOCOL_NOT_SUPPORTED
requested protocol not supported
Definition ifdhandler.h:357
#define IFD_SUCCESS
no error
Definition ifdhandler.h:351
RESPONSECODE IFDSetPTS(READER_CONTEXT *rContext, DWORD dwProtocol, UCHAR ucFlags, UCHAR ucPTS1, UCHAR ucPTS2, UCHAR ucPTS3)
Set the protocol type selection (PTS).
Definition ifdwrapper.c:67
This wraps the dynamic ifdhandler functions.
DWORD PHSetProtocol(struct ReaderContext *rContext, DWORD dwPreferred, UCHAR ucAvailable, UCHAR ucDefault)
Determine which protocol to use.
Definition prothandler.c:55
This handles protocol defaults, PTS, etc.
#define SCARD_PROTOCOL_UNDEFINED
protocol not set
#define SCARD_PROTOCOL_T1
T=1 active protocol.
#define SCARD_PROTOCOL_T0
T=0 active protocol.