forked from arnaudmorin/libgtop11dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmartCardReader.cpp
283 lines (204 loc) · 9.06 KB
/
SmartCardReader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* PKCS#11 library for .Net smart cards
* Copyright (C) 2007-2009 Gemalto <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef __APPLE__
#include <PCSC/winscard.h>
#include <PCSC/wintypes.h>
#else
#include <winscard.h>
#endif
#include "SmartCardReader.hpp"
#include "SmartCardReaderException.hpp"
#include "CardModuleService.hpp"
#ifndef WIN32
#define SCARD_CTL_CODE(code) (0x42000000 + (code))
#endif
#include "PCSCMissing.h"
#pragma pack(push, mdnet, 1)
typedef struct PIN_VERIFY_STRUCTURE
{
BYTE bTimerOut; /* timeout is seconds (00 means use default timeout) */
BYTE bTimerOut2; /* timeout in seconds after first key stroke */
BYTE bmFormatString; /* formatting options */
BYTE bmPINBlockString; /* bits 7-4 bit size of PIN length in APDU,
* bits 3-0 PIN block size in bytes after
* justification and formatting */
BYTE bmPINLengthFormat; /* bits 7-5 RFU,
* bit 4 set if system units are bytes, clear if
* system units are bits,
* bits 3-0 PIN length position in system units
*/
BYTE bPINMaxExtraDigit1; /* Max PIN size*/
BYTE bPINMaxExtraDigit2; /* Min PIN size*/
BYTE bEntryValidationCondition; /* Conditions under which PIN entry should
* be considered complete */
BYTE bNumberMessage; /* Number of messages to display for PIN
verification */
USHORT wLangId; /* Language for messages */
BYTE bMsgIndex; /* Message index (should be 00) */
BYTE bTeoPrologue[3]; /* T=1 block prologue field to use (fill with 00) */
ULONG ulDataLength; /* length of Data to be sent to the ICC */
BYTE abData[13]; /* Data to send to the ICC */
} PIN_VERIFY_STRUCTURE;
#pragma pack(pop, mdnet)
#define CM_IOCTL_GET_FEATURE_REQUEST SCARD_CTL_CODE(3400)
#define FEATURE_VERIFY_PIN_DIRECT 0x06
/*
*/
SmartCardReader::SmartCardReader( const std::string& a_stReaderName ) {
m_dwIoctlVerifyPIN = 0;
m_stReaderName = a_stReaderName;
m_CardHandle = 0;
m_bIsSecuredVerifyPIN = boost::logic::indeterminate;
}
/*
*/
bool SmartCardReader::isVerifyPinSecured( void ) {
if( boost::logic::indeterminate( m_bIsSecuredVerifyPIN ) ) {
// Get Reader Features
BYTE outBuffer[ 256 ];
//memset( outBuffer, 0, sizeof( outBuffer ) );
DWORD dwLen = 0;
LONG hResult = SCARD_F_INTERNAL_ERROR;
unsigned char ucRetry = 0;
do {
memset( outBuffer, 0, sizeof( outBuffer ) );
dwLen = 0;
hResult = SCardControl( m_CardHandle, CM_IOCTL_GET_FEATURE_REQUEST, NULL, 0, outBuffer, sizeof( outBuffer ), &dwLen );
//Log::log( "=============================================================================================== SmartCardReader::isVerifyPinSecured - retry <%d> - result <%#02x>", ucRetry, hResult );
ucRetry++;
} while( ( SCARD_S_SUCCESS != hResult ) && ( ucRetry < 5 ) );
if ( ( SCARD_S_SUCCESS == hResult ) && ( dwLen > 0 ) ) {
m_bIsSecuredVerifyPIN = false;
int i = 0;
// Search IOCTL of Verify PIN feature
while( ( i + 6 ) <= (int)dwLen ) {
// Search Verify PIN feature Tag
if( ( outBuffer[ i ] == FEATURE_VERIFY_PIN_DIRECT ) && ( outBuffer[ i + 1 ] == 4 ) ) {
m_dwIoctlVerifyPIN += ( outBuffer[ i + 2 ] << 24 );
m_dwIoctlVerifyPIN += ( outBuffer[ i + 3 ] << 16 );
m_dwIoctlVerifyPIN += ( outBuffer[ i + 4 ] << 8 );
m_dwIoctlVerifyPIN += outBuffer[ i + 5 ];
m_bIsSecuredVerifyPIN = true;
break;
} else {
i += (outBuffer[ i + 1 ] + 2 );
}
}
} else {
// The ScardControl failed. Return false and read the flag next time
return false;
}
}
return m_bIsSecuredVerifyPIN;
}
/*
*/
void SmartCardReader::verifyPinSecured( const unsigned char& a_ucRole ) {
if( true != m_bIsSecuredVerifyPIN ) {
throw SmartCardReaderException( SCARD_E_READER_UNSUPPORTED );
}
//DWORD PinId = a_ucRole;
LONG lRet;
BYTE offset;
DWORD dwSendLen;
PIN_VERIFY_STRUCTURE pin_verify;
BYTE inBuffer[256];
DWORD dwInLen = 0;
BYTE outBuffer[256];
DWORD dwOutLen = 0;
// Time out between key stroke = max(bTimerOut, bTimerOut2). Must be between 15 and 40 sec.
pin_verify.bTimerOut = 30;
pin_verify.bTimerOut2 = 00;
// Padding V2=0x82
pin_verify.bmFormatString = 0x82;
pin_verify.bmPINBlockString = 0x06;
pin_verify.bmPINLengthFormat = 0x00;
// Max PIN length
pin_verify.bPINMaxExtraDigit1 = 0x08;
// Min PIN length
pin_verify.bPINMaxExtraDigit2 = 0x04;
// Validation when key pressed
pin_verify.bEntryValidationCondition = 0x02;
pin_verify.bNumberMessage = 0x01;
pin_verify.wLangId = 0x0904;
pin_verify.bMsgIndex = 0x00;
pin_verify.bTeoPrologue[0] = 0x00;
pin_verify.bTeoPrologue[1] = 0x00;
// pin_verify.ulDataLength = 0x00; we don't know the size yet
pin_verify.bTeoPrologue[2] = 0x00;
offset = 0;
// Class
pin_verify.abData[offset++] = 0x00;
// Instruction Verify
pin_verify.abData[offset++] = 0x20;
// P1 always 0
pin_verify.abData[offset++] = 0x00;
// P2 Pin reference
pin_verify.abData[offset++] = a_ucRole;
// Lc 8 data bytes
pin_verify.abData[offset++] = 0x08;
pin_verify.abData[offset++] = 0xFF;
pin_verify.abData[offset++] = 0xFF;
pin_verify.abData[offset++] = 0xFF;
pin_verify.abData[offset++] = 0xFF;
pin_verify.abData[offset++] = 0xFF;
pin_verify.abData[offset++] = 0xFF;
pin_verify.abData[offset++] = 0xFF;
pin_verify.abData[offset++] = 0xFF;
// APDU size
pin_verify.ulDataLength = offset;
dwSendLen = sizeof(PIN_VERIFY_STRUCTURE);
// Select MSCM Application
inBuffer[0] = 0x00; //CLA
inBuffer[1] = 0xA4; //INS
inBuffer[2] = 0x04; //P1
inBuffer[3] = 0x00; //P2
inBuffer[4] = 0x04; //Li
char pCardModuleServiceName[ ] = "MSCM";
memcpy( &inBuffer[ 5 ], pCardModuleServiceName, 4 ); // ??? TO DO ??? Manage the size dynamically
dwInLen = 5 + inBuffer[ 4 ];
dwOutLen = sizeof(outBuffer);
memset(outBuffer, 0x00, sizeof(outBuffer));
lRet = SCardTransmit( m_CardHandle, SCARD_PCI_T0, inBuffer, dwInLen, NULL, outBuffer, &dwOutLen );
// Send Verify command to the reader
dwOutLen = 0;
memset(outBuffer, 0x00, sizeof(outBuffer));
lRet = SCardControl( m_CardHandle, m_dwIoctlVerifyPIN, (BYTE *)&pin_verify, dwSendLen, outBuffer, sizeof(outBuffer), &dwOutLen );
if( ( SCARD_W_REMOVED_CARD == lRet ) || ( SCARD_W_RESET_CARD == lRet ) ) {
DWORD dwActiveProtocol = 0;
lRet = SCardReconnect( m_CardHandle, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, SCARD_LEAVE_CARD, &dwActiveProtocol );
lRet = SCardControl( m_CardHandle, m_dwIoctlVerifyPIN, (BYTE *)&pin_verify, dwSendLen, outBuffer, sizeof(outBuffer), &dwOutLen );
}
//Log::log( "Token::verifyPinWithPinPad - sw <%#02x %#02x>", outBuffer[ 0 ], outBuffer[ 1 ] );
if( ( 0x90 == outBuffer[ 0 ] ) && ( 0x00 == outBuffer[ 1 ] ) ) {
// The PIN is verified
} else if( ( 0x63 == outBuffer[ 0 ] ) && ( 0x00 == outBuffer[ 1 ] ) ) {
throw SmartCardReaderException( SCARD_W_WRONG_CHV );
} else if( ( 0x64 == outBuffer[ 0 ] ) && ( 0x01 == outBuffer[ 1 ] ) ) {
// operation was cancelled by the Cancel button
throw SmartCardReaderException( SCARD_W_CANCELLED_BY_USER );
} else if( ( 0x64 == outBuffer[ 0 ] ) && ( 0x00 == outBuffer[ 1 ] ) ) {
// operation timed out
throw SmartCardReaderException( SCARD_E_TIMEOUT );
} else if( ( 0x64 == outBuffer[ 0 ] ) && ( 0x03 == outBuffer[ 1 ] ) ) {
// operation timed out
throw SmartCardReaderException( SCARD_E_TIMEOUT );
}
}