File: Pinpad.cpp

package info (click to toggle)
beid 3.5.2.dfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 147,240 kB
  • ctags: 34,507
  • sloc: cpp: 149,944; ansic: 41,577; java: 8,927; cs: 6,528; sh: 2,426; perl: 1,866; xml: 805; python: 463; makefile: 263; lex: 92
file content (428 lines) | stat: -rw-r--r-- 13,016 bytes parent folder | download
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/* ****************************************************************************

 * eID Middleware Project.
 * Copyright (C) 2008-2009 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software 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 software; if not, see
 * http://www.gnu.org/licenses/.

**************************************************************************** */
#include "Pinpad.h"
#include "Context.h"
#include "pinpad2.h"
#include "../common/Log.h"
#include "../common/Util.h"
#include "../common/Config.h"

// Make sure PP_DUMP_CMDS is not defined in a release!
#define PP_DUMP_CMDS
#undef PP_DUMP_CMDS

namespace eIDMW
{

CPinpad::CPinpad(void)
{
	m_bNewCard = true;
	m_ulLangCode = 0;
}

void CPinpad::Init(CContext *poContext, unsigned long hCard,
	const std::string & csReader, const std::string & csPinpadPrefix)
{
    m_poContext = poContext;
    m_hCard = hCard;
	m_csReader = csReader;

	// We only unload the pinpad lib that is currently
	// loaded if another type of card is inserted then
	// the card before the current one.
	if (csPinpadPrefix != m_csPinpadPrefix)
		UnloadPinpadLib();

	m_csPinpadPrefix = csPinpadPrefix;
}

bool CPinpad::UsePinpad(tPinOperation operation)
{
	if (m_bNewCard)
	{
		m_bUsePinpadLib = m_oPinpadLib.Load((unsigned long) m_poContext->m_oPCSC.m_hContext,
			m_hCard, m_csReader, m_csPinpadPrefix, GetLanguage());

		// The GemPC pinpad reader does a "Verify PIN" with empty buffer in an attempt
		// to get and display the remainings attempts. But the BE eID card takes this
		// empty buffer to be a bad PIN, so you quickly end up with a blocked card.
		// Therefore, we don't allow this reader to be used as a pinpad reader..
		if (!m_bUsePinpadLib && StartsWith(m_csReader.c_str(), "Gemplus GemPC Pinpad"))
			return false;

		GetFeatureList();
	}

	switch (operation)
	{
	case PIN_OP_VERIFY: return m_bCanVerifyUnlock;
	case PIN_OP_CHANGE: return m_bCanChangeUnlock;
	default: return false;
	}
}

// See par 4.1.11.3 bmFormatString description
unsigned char CPinpad::ToFormatString(const tPin & pin)
{
	switch(pin.encoding)
	{
	case PIN_ENC_ASCII:
		return 0x00 | 0x00 | 0x00 | 0x02;
	case PIN_ENC_BCD:
		return 0x00 | 0x00 | 0x00 | 0x01;
	case PIN_ENC_GP:
		// READER FIX:
		// The SPR532 reader wants this value to be as for BCD
		const char *csReader = m_csReader.c_str();
		if (strstr(csReader, "SPRx32 USB") != NULL)
			return 0x00 | 0x00 | 0x00 | 0x01;
		return 0x80 | 0x08 | 0x00 | 0x01;
	}
	return 0;
}

// See par 4.1.11.4 bmPINBlockString description
unsigned char CPinpad::ToPinBlockString(const tPin & pin)
{
	switch(pin.encoding)
	{
	case PIN_ENC_ASCII:
		return (unsigned char ) pin.ulStoredLen;
	case PIN_ENC_BCD:
		return (unsigned char ) pin.ulStoredLen;
	case PIN_ENC_GP:
		return 0x40 | (unsigned char ) (pin.ulStoredLen - 1);
	}
	return (unsigned char ) pin.ulStoredLen;
}

// See par 4.1.11.5 bmPINLengthFormat
unsigned char CPinpad::ToPinLengthFormat(const tPin & pin)
{
	switch(pin.encoding)
	{
	case PIN_ENC_ASCII:
		return 0x00 | 0x00;
	case PIN_ENC_BCD:
		return 0x00 | 0x00;
	case PIN_ENC_GP:
		return 0x00 | 0x04;
	}
	return 0x00;
}

unsigned char CPinpad::GetMaxPinLen(const tPin & pin)
{
	unsigned char ucRes = (unsigned char)
		(pin.ulMaxLen > 0 ? pin.ulMaxLen : pin.ulStoredLen);

	// READER FIX:
	// Limitation of the GemPC reader: at most 8 PIN digits
	const char *csReader = m_csReader.c_str();
	if (strstr(csReader, "Gemplus GemPC Pinpad") == csReader)
		ucRes = (ucRes > 8) ? 8 : ucRes;

	return ucRes;
}

CByteArray CPinpad::PinCmd(tPinOperation operation,
	const tPin & pin, unsigned char ucPinType,
    const CByteArray & oAPDU, unsigned long & ulRemaining)
{
	if (!UsePinpad(operation))
		throw CMWEXCEPTION(EIDMW_ERR_PIN_OPERATION);

	CByteArray oResp;

	if (operation == PIN_OP_VERIFY)
		oResp = PinCmd1(operation, pin, ucPinType, oAPDU, ulRemaining);
	else
		oResp = PinCmd2(operation, pin, ucPinType, oAPDU, ulRemaining);

	if (oResp.Size() != 2)
	{
	  MWLOG(LEV_ERROR, MOD_CAL, L"pinpad reader returned %ls\n", oResp.ToWString().c_str());
	  return EIDMW_ERR_UNKNOWN; // should never happen
	}

	const unsigned char *pucSW12 = oResp.GetBytes();
	if (pucSW12[0] == 0x64 && pucSW12[1] == 0x00)
		throw CMWEXCEPTION(EIDMW_ERR_TIMEOUT);
	if (pucSW12[0] == 0x64 && pucSW12[1] == 0x01)
		throw CMWEXCEPTION(EIDMW_ERR_PIN_CANCEL);
	if (pucSW12[0] == 0x64 && pucSW12[1] == 0x02)
		throw CMWEXCEPTION(EIDMW_NEW_PINS_DIFFER);
	if (pucSW12[0] == 0x64 && pucSW12[1] == 0x03)
		throw CMWEXCEPTION(EIDMW_WRONG_PIN_FORMAT);
	if (pucSW12[0] == 0x6B && pucSW12[1] == 0x80)
		throw CMWEXCEPTION(EIDMW_PINPAD_ERR);

	return oResp;
}

/** For operations involving 1 PIN */
CByteArray CPinpad::PinCmd1(tPinOperation operation,
	const tPin & pin, unsigned char ucPinType,
    const CByteArray & oAPDU, unsigned long & ulRemaining)
{
	EIDMW_PP_VERIFY_CCID xVerifyCmd;
	unsigned long ulVerifyCmdLen;

	memset(&xVerifyCmd, 0, sizeof(xVerifyCmd));
	xVerifyCmd.bTimerOut = 30;
	xVerifyCmd.bTimerOut2 = 30;
	xVerifyCmd.bmFormatString = ToFormatString(pin);
	xVerifyCmd.bmPINBlockString = ToPinBlockString(pin);
	xVerifyCmd.bmPINLengthFormat = ToPinLengthFormat(pin);
	xVerifyCmd.wPINMaxExtraDigit[0] = GetMaxPinLen(pin);
	xVerifyCmd.wPINMaxExtraDigit[1] = (unsigned char) pin.ulMinLen;
	xVerifyCmd.bEntryValidationCondition = 0x02;
	xVerifyCmd.bNumberMessage = 0x01;
	ToUchar2(m_ulLangCode, xVerifyCmd.wLangId),
	xVerifyCmd.bMsgIndex = 0;
	ToUchar4(oAPDU.Size(), xVerifyCmd.ulDataLength);
	memcpy(xVerifyCmd.abData, oAPDU.GetBytes(), oAPDU.Size());
	ulVerifyCmdLen = sizeof(xVerifyCmd) - PP_APDU_MAX_LEN + oAPDU.Size();

	CByteArray oCmd((unsigned char *) &xVerifyCmd, ulVerifyCmdLen);
	if (m_ioctlVerifyDirect)
	{
		return PinpadControl(m_ioctlVerifyDirect, oCmd, operation,
			ucPinType, pin.csLabel, true);
	}
	else
	{
		PinpadControl(m_ioctlVerifyStart, oCmd, operation,
			ucPinType, pin.csLabel, false);
		return PinpadControl(m_ioctlVerifyFinish, CByteArray(), operation,
			ucPinType, "", true);
	}
}

/** For operations involving 2 PINs */
CByteArray CPinpad::PinCmd2(tPinOperation operation,
	const tPin & pin, unsigned char ucPinType,
    const CByteArray & oAPDU, unsigned long & ulRemaining)
{
	EIDMW_PP_CHANGE_CCID xChangeCmd;
	unsigned long ulChangeCmdLen;

	memset(&xChangeCmd, 0, sizeof(xChangeCmd));
	xChangeCmd.bTimerOut = 30;
	xChangeCmd.bTimerOut2 = 30;
	xChangeCmd.bmFormatString = ToFormatString(pin);
	xChangeCmd.bmPINBlockString = ToPinBlockString(pin);
	xChangeCmd.bmPINLengthFormat = ToPinLengthFormat(pin);
	xChangeCmd.bInsertionOffsetOld = 0x00;
	xChangeCmd.bInsertionOffsetNew = (unsigned char) pin.ulStoredLen;
	xChangeCmd.wPINMaxExtraDigit[0] = GetMaxPinLen(pin);
	xChangeCmd.wPINMaxExtraDigit[1] = (unsigned char) pin.ulMinLen;
	xChangeCmd.bConfirmPIN = 0x03;
	xChangeCmd.bEntryValidationCondition = 0x02;
	xChangeCmd.bNumberMessage = 0x03;
	ToUchar2(m_ulLangCode, xChangeCmd.wLangId);
	xChangeCmd.bMsgIndex1 = 0x00;
	xChangeCmd.bMsgIndex2 = 0x01;
	xChangeCmd.bMsgIndex3 = 0x02;
	ToUchar4(oAPDU.Size(), xChangeCmd.ulDataLength);
	memcpy(xChangeCmd.abData, oAPDU.GetBytes(), oAPDU.Size());
	ulChangeCmdLen = sizeof(xChangeCmd) - PP_APDU_MAX_LEN + oAPDU.Size();

	CByteArray oCmd((unsigned char *) &xChangeCmd, ulChangeCmdLen);
	if (m_ioctlChangeDirect)
	{
		return PinpadControl(m_ioctlChangeDirect, oCmd, operation,
			ucPinType, pin.csLabel, true);
	}
	else
	{
		PinpadControl(m_ioctlChangeStart, oCmd, operation,
			ucPinType, pin.csLabel, false);
		return PinpadControl(m_ioctlChangeFinish, CByteArray(), operation,
			ucPinType, "", true);
	}
}

void CPinpad::UnloadPinpadLib()
{
	m_bNewCard = true;
	m_bCanVerifyUnlock = false;
	m_bCanChangeUnlock = false;
	m_bUsePinpadLib = false;
	m_oPinpadLib.Unload();
}

CByteArray CPinpad::PinpadControl(unsigned long ulControl, const CByteArray & oCmd,
	tPinOperation operation, unsigned char ucPintype,
	const std::string & csPinLabel,	bool bShowDlg)
{
	unsigned char pinpadOperation = PinOperation2Lib(operation);

#ifdef PP_DUMP_CMDS
	printf("PP ctrl (%svia pinpad lib): 0x%0x\n", (m_bUsePinpadLib ? "" : "not "), ulControl);
	printf("PP IN: %s\n", oCmd.ToString(true, false, 0, 0xFFFFFFFF).c_str());
#endif

	unsigned long ulDlgHandle;
	bool bCloseDlg = bShowDlg;
	if (bShowDlg)
		bCloseDlg = m_oPinpadLib.ShowDlg(pinpadOperation,
		ucPintype, csPinLabel, m_csReader, &ulDlgHandle);

	CByteArray oResp;
	try
	{
		if (!m_bUsePinpadLib)
		{
			oResp = m_poContext->m_oPCSC.Control(m_hCard, ulControl, oCmd);
		}
		else
		{
			oResp = m_oPinpadLib.PinCmd(m_hCard, ulControl, oCmd, ucPintype,
				pinpadOperation);
		}
	}
	catch (...)
	{
		if (bShowDlg)
			m_oPinpadLib.CloseDlg(ulDlgHandle);
		throw ;
	}
	if (bShowDlg)
		m_oPinpadLib.CloseDlg(ulDlgHandle);

#ifdef PP_DUMP_CMDS
	printf("PP OUT: %s\n", oResp.ToString(true, false, 0, 0xFFFFFFFF).c_str());
#endif

	return oResp;
}

#define CHECK_FEATURE(feature, featureID, iotcl) \
	if (feature[0] == featureID) \
		iotcl = 256 * (256 * ((256 * feature[2]) + feature[3]) + feature[4]) + feature[5];

void CPinpad::GetFeatureList()
{
	m_bCanVerifyUnlock = false;
	m_bCanChangeUnlock = false;
	m_ioctlVerifyStart = m_ioctlVerifyFinish = m_ioctlVerifyDirect = 0;
	m_ioctlChangeStart = m_ioctlChangeFinish = m_ioctlChangeDirect = 0;

	try {
		CByteArray oFeatures = PinpadControl(CCID_IOCTL_GET_FEATURE_REQUEST,
			CByteArray(), PIN_OP_VERIFY, 0, "", false);

		// Example of a feature list: 06 04 00 31 20 30 07 04 00 31 20 34
		// Which means:
		//  - IOCTL for CCID_VERIFY_DIRECT = 0x00312030
		//  - IOCTL for CCID_CHANGE_DIRECT = 0x00312034
		unsigned long ulFeatureLen = oFeatures.Size();
		if ((ulFeatureLen % 6) == 0)
		{
			const unsigned char *pucFeatures = oFeatures.GetBytes();
			ulFeatureLen /= 6;
			for (unsigned long i = 0; i < ulFeatureLen; i++)
			{
				CHECK_FEATURE(pucFeatures, CCID_VERIFY_START, m_ioctlVerifyStart)
				CHECK_FEATURE(pucFeatures, CCID_VERIFY_FINISH, m_ioctlVerifyFinish)
				CHECK_FEATURE(pucFeatures, CCID_VERIFY_DIRECT, m_ioctlVerifyDirect)
				CHECK_FEATURE(pucFeatures, CCID_CHANGE_START, m_ioctlChangeStart)
				CHECK_FEATURE(pucFeatures, CCID_CHANGE_FINISH, m_ioctlChangeFinish)
				CHECK_FEATURE(pucFeatures, CCID_CHANGE_DIRECT, m_ioctlChangeDirect)
				pucFeatures += 6;
			}

			m_bCanVerifyUnlock = (m_ioctlVerifyStart && m_ioctlVerifyFinish) || m_ioctlVerifyDirect;
			m_bCanChangeUnlock = (m_ioctlChangeStart && m_ioctlChangeFinish) || m_ioctlChangeDirect;

			if (m_bCanVerifyUnlock || m_bCanChangeUnlock)
				m_ulLangCode = GetLanguage();
		}
	}
	catch(const CMWException & e)
	{
		// very likely CCID_IOCTL_GET_FEATURE_REQUEST isn't supported
		// by this reader -> nothing to do
		e.GetError();
	}

	m_bNewCard = false;
}

unsigned char CPinpad::PinOperation2Lib(tPinOperation operation)
{
	switch(operation)
	{
	case PIN_OP_VERIFY: return EIDMW_PP_OP_VERIFY;
	case PIN_OP_CHANGE: return EIDMW_PP_OP_CHANGE;
	// Add others when needed
	default: throw CMWEXCEPTION(EIDMW_ERR_CHECK);
	}
}

unsigned long CPinpad::GetLanguage()
{
	if (m_ulLangCode == 0)
	{
		m_ulLangCode =  PP_LANG_EN;
		std::wstring csLang = CConfig::GetString(CConfig::EIDMW_CONFIG_PARAM_GENERAL_LANGUAGE);
		if (csLang == L"nl")
			m_ulLangCode = PP_LANG_NL;
		else if (csLang == L"fr")
			m_ulLangCode = PP_LANG_FR;
		else if (csLang == L"de")
			m_ulLangCode = PP_LANG_DE;
	}
	return m_ulLangCode;
}

}

/**************************** logs *******************

1. SPR532 (driver 2.14_2.11, firmware 5.05)

Verify:
	1E 1E 01 47 04 08 04 02 01 CD CD 00 00 00 00 0D
	00 00 00 00 20 00 01 08 20 FF FF FF FF FF FF FF

Change:
	1E 1E 01 47 04 00 08 0C 04 03 02 03 CD CD 00 00
	00 00 00 00 15 00 00 00 00 24 00 01 10 20 FF FF
	FF FF FF FF FF 20 FF FF FF FF FF FF FF

Note: bmFormatString must be 01 (instead of 89) !!!


2. GemPC pinpad

Verify:
	1E 1E 89 47 04 08 04 02 01 CD CD 00 00 00 00 0D
	00 00 00 00 20 00 01 08 20 FF FF FF FF FF FF FF

Change:
	1E 1E 89 47 04 00 08 08 04 03 02 03 CD CD 00 00
	00 00 00 00 15 00 00 00 00 24 00 01 10 20 FF FF
	FF FF FF FF FF 20 FF FF FF FF FF FF FF
00
Note: wPINMaxExtraDigit[0] must be at most 8 (instead of 12) !!

******************************************************/