28 |
{ |
{ |
29 |
DWORD dwReturn; |
DWORD dwReturn; |
30 |
PBYTE pbResponse = NULL; |
PBYTE pbResponse = NULL; |
31 |
DWORD dwMinPinSize = 0, dwMaxPinSize; |
DWORD dwMinPinSize = 0, dwMaxPinSize, dwSize; |
32 |
__try |
__try |
33 |
{ |
{ |
34 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter PinId=%d",PinId); |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter PinId=%d",PinId); |
41 |
case ROLE_CONFIDENTIALITY: |
case ROLE_CONFIDENTIALITY: |
42 |
dwMinPinSize = 6; |
dwMinPinSize = 6; |
43 |
case ROLE_PUK: |
case ROLE_PUK: |
44 |
|
dwMinPinSize = 1; |
45 |
break; |
break; |
46 |
case ROLE_ADMIN: |
case ROLE_ADMIN: |
47 |
dwMinPinSize = 8; |
dwMinPinSize = 8; |
57 |
__leave; |
__leave; |
58 |
} |
} |
59 |
// check in status DO |
// check in status DO |
60 |
dwReturn = SCardReadFile(pCardData, szOpenPGPDir, szOpenPGPStatus, &pbResponse, NULL); |
dwReturn = OCardReadFile(pCardData, szOpenPGPDir, szOpenPGPStatus, &pbResponse, &dwSize); |
61 |
|
if (dwReturn) |
62 |
|
{ |
63 |
|
__leave; |
64 |
|
} |
65 |
switch(PinId) |
switch(PinId) |
66 |
{ |
{ |
67 |
case ROLE_SIGNATURE: |
case ROLE_SIGNATURE: |
97 |
|
|
98 |
DWORD GetRemainingPin(__in PCARD_DATA pCardData, __in PIN_ID PinId, __out PDWORD pdwCounter) |
DWORD GetRemainingPin(__in PCARD_DATA pCardData, __in PIN_ID PinId, __out PDWORD pdwCounter) |
99 |
{ |
{ |
100 |
DWORD dwReturn; |
DWORD dwReturn, dwSize; |
101 |
PBYTE pbResponse = NULL; |
PBYTE pbResponse = NULL; |
102 |
__try |
__try |
103 |
{ |
{ |
104 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter PinId=%d",PinId); |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter PinId=%d",PinId); |
105 |
dwReturn = SCardReadFile(pCardData, szOpenPGPDir, szOpenPGPStatus, &pbResponse, NULL); |
dwReturn = OCardReadFile(pCardData, szOpenPGPDir, szOpenPGPStatus, &pbResponse, &dwSize); |
106 |
switch(PinId) |
switch(PinId) |
107 |
{ |
{ |
108 |
case ROLE_SIGNATURE: |
case ROLE_SIGNATURE: |
133 |
__in_bcount(cbPin) PBYTE pbPin, __in DWORD cbPin) |
__in_bcount(cbPin) PBYTE pbPin, __in DWORD cbPin) |
134 |
{ |
{ |
135 |
DWORD dwReturn; |
DWORD dwReturn; |
136 |
|
POPENPGP_CONTEXT pContext = (POPENPGP_CONTEXT) pCardData->pvVendorSpecific; |
137 |
// 256 because the size of the PIN must fit in a Byte |
// 256 because the size of the PIN must fit in a Byte |
138 |
BYTE pbCmd[256 + 5] = {0x00, |
BYTE pbCmd[256 + 5] = {0x00, |
139 |
0x20, |
0x20, |
165 |
} |
} |
166 |
pbCmd[4] = (BYTE) cbPin; |
pbCmd[4] = (BYTE) cbPin; |
167 |
memcpy(pbCmd + 5, pbPin, cbPin); |
memcpy(pbCmd + 5, pbPin, cbPin); |
168 |
dwReturn = SCardSendCommand(pCardData, pbCmd, 5 + cbPin); |
dwReturn = OCardSendCommand(pCardData, pbCmd, 5 + cbPin); |
169 |
if (dwReturn) |
if (dwReturn) |
170 |
{ |
{ |
171 |
|
Trace(WINEVENT_LEVEL_VERBOSE, L"Authentication failed"); |
172 |
__leave; |
__leave; |
173 |
} |
} |
174 |
/*switch(PinId) |
Trace(WINEVENT_LEVEL_VERBOSE, L"Authentication successfull"); |
175 |
|
if (PinId == ROLE_ADMIN) |
176 |
{ |
{ |
177 |
case ROLE_AUTHENTICATION: |
pContext->fDoesTheAdminHasBeenAuthenticatedAtLeastOnce = TRUE; |
178 |
case ROLE_CONFIDENTIALITY: |
} |
|
dwReturn = VerifyPIN(pCardData, ROLE_SIGNATURE, pbPin, cbPin); |
|
|
break; |
|
|
}*/ |
|
|
|
|
179 |
} |
} |
180 |
__finally |
__finally |
181 |
{ |
{ |
201 |
__try |
__try |
202 |
{ |
{ |
203 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter PinId=%d",PinId); |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter PinId=%d",PinId); |
204 |
|
dwReturn = CheckPinLength(pCardData, PinId, cbNewPin); |
205 |
|
if (dwReturn) |
206 |
|
{ |
207 |
|
Trace(WINEVENT_LEVEL_ERROR, L"Invalid len %d",cbNewPin); |
208 |
|
dwReturn = SCARD_E_INVALID_PARAMETER; |
209 |
|
__leave; |
210 |
|
} |
211 |
dwReturn = CheckPinLength(pCardData, PinId, cbOldPin); |
dwReturn = CheckPinLength(pCardData, PinId, cbOldPin); |
212 |
if (dwReturn) |
if (dwReturn) |
213 |
{ |
{ |
233 |
pbCmd[4] = (BYTE) (cbOldPin + cbNewPin); |
pbCmd[4] = (BYTE) (cbOldPin + cbNewPin); |
234 |
memcpy(pbCmd + 5, pbOldPin, cbOldPin); |
memcpy(pbCmd + 5, pbOldPin, cbOldPin); |
235 |
memcpy(pbCmd + 5 + cbOldPin, pbNewPin, cbNewPin); |
memcpy(pbCmd + 5 + cbOldPin, pbNewPin, cbNewPin); |
236 |
dwReturn = SCardSendCommand(pCardData, pbCmd, 5 + cbOldPin + cbNewPin); |
dwReturn = OCardSendCommand(pCardData, pbCmd, 5 + cbOldPin + cbNewPin); |
237 |
} |
} |
238 |
__finally |
__finally |
239 |
{ |
{ |
260 |
__try |
__try |
261 |
{ |
{ |
262 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter PinId=%d",PinId); |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter PinId=%d",PinId); |
263 |
|
dwReturn = CheckPinLength(pCardData, ROLE_USER, cbNewPin); |
264 |
|
if (dwReturn) |
265 |
|
{ |
266 |
|
Trace(WINEVENT_LEVEL_ERROR, L"Invalid len %d",cbNewPin); |
267 |
|
dwReturn = SCARD_E_INVALID_PARAMETER; |
268 |
|
__leave; |
269 |
|
} |
270 |
switch(PinId) |
switch(PinId) |
271 |
{ |
{ |
272 |
case ROLE_ADMIN: |
case ROLE_ADMIN: |
273 |
// authenticate the admin |
// authenticate the admin |
274 |
dwReturn = VerifyPIN(pCardData, PinId, pbAuthenticator, cbAuthenticator); |
dwReturn = VerifyPIN(pCardData, PinId, pbAuthenticator, cbAuthenticator); |
275 |
if (!dwReturn) |
if (dwReturn) |
276 |
{ |
{ |
277 |
return dwReturn; |
__leave; |
278 |
} |
} |
279 |
pbCmd[4] = (BYTE) cbAuthenticator; |
pbCmd[4] = (BYTE) cbNewPin; |
280 |
memcpy(pbCmd + 5, pbNewPin, cbNewPin); |
memcpy(pbCmd + 5, pbNewPin, cbNewPin); |
281 |
dwCmdSize = 5 + cbNewPin; |
dwCmdSize = 5 + cbNewPin; |
282 |
break; |
break; |
285 |
if (dwReturn) |
if (dwReturn) |
286 |
{ |
{ |
287 |
__leave; |
__leave; |
288 |
} |
} |
289 |
pbCmd[2] = 0x00; |
pbCmd[2] = 0x00; |
290 |
pbCmd[4] = (BYTE) (cbAuthenticator + cbNewPin); |
pbCmd[4] = (BYTE) (cbAuthenticator + cbNewPin); |
291 |
memcpy(pbCmd + 5, pbAuthenticator, cbAuthenticator); |
memcpy(pbCmd + 5, pbAuthenticator, cbAuthenticator); |
296 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
dwReturn = SCARD_E_INVALID_PARAMETER; |
297 |
__leave; |
__leave; |
298 |
} |
} |
299 |
dwReturn = SCardSendCommand(pCardData, pbCmd, dwCmdSize); |
dwReturn = OCardSendCommand(pCardData, pbCmd, dwCmdSize); |
300 |
} |
} |
301 |
__finally |
__finally |
302 |
{ |
{ |
305 |
return dwReturn; |
return dwReturn; |
306 |
} |
} |
307 |
|
|
308 |
|
DWORD SetPUK(__in PCARD_DATA pCardData, |
309 |
|
__in_bcount(cbPin) PBYTE pbAdminPin, __in DWORD cbAdminPin, |
310 |
|
__in_bcount(cbPin) PBYTE pbPuk, __in DWORD cbPuk |
311 |
|
) |
312 |
|
{ |
313 |
|
DWORD dwReturn; |
314 |
|
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter"); |
315 |
|
__try |
316 |
|
{ |
317 |
|
dwReturn = CheckPinLength(pCardData, ROLE_PUK, cbPuk); |
318 |
|
if (dwReturn) |
319 |
|
{ |
320 |
|
Trace(WINEVENT_LEVEL_ERROR, L"Invalid len %d",cbPuk); |
321 |
|
dwReturn = SCARD_E_INVALID_PARAMETER; |
322 |
|
__leave; |
323 |
|
} |
324 |
|
dwReturn = VerifyPIN(pCardData, ROLE_ADMIN, pbAdminPin, cbAdminPin); |
325 |
|
if (dwReturn) |
326 |
|
{ |
327 |
|
__leave; |
328 |
|
} |
329 |
|
dwReturn = OCardWriteFile(pCardData, szOpenPGPDir, szOpenPGPPUK, pbPuk, cbPuk); |
330 |
|
} |
331 |
|
__finally |
332 |
|
{ |
333 |
|
} |
334 |
|
return dwReturn; |
335 |
|
} |
336 |
|
|
337 |
DWORD Deauthenticate(__in PCARD_DATA pCardData) |
DWORD Deauthenticate(__in PCARD_DATA pCardData) |
338 |
{ |
{ |
339 |
DWORD dwAP; |
/*DWORD dwCode, dwSize; |
340 |
DWORD dwReturn; |
DWORD dwReturn; |
341 |
__try |
__try |
342 |
{ |
{ |
343 |
// reset the card |
// reset the card |
344 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter"); |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter"); |
345 |
dwReturn = SCardReconnect(pCardData->hScard, |
dwCode = SCARD_COLD_RESET; |
346 |
SCARD_SHARE_SHARED, |
dwReturn = SCardControl(pCardData->hScard, IOCTL_SMARTCARD_POWER,&dwCode,4,NULL,0,&dwSize); |
347 |
SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, |
if (dwReturn && dwReturn != SCARD_W_RESET_CARD) |
|
SCARD_LEAVE_CARD, |
|
|
&dwAP ); |
|
|
if (dwReturn) |
|
348 |
{ |
{ |
349 |
Trace(WINEVENT_LEVEL_ERROR, L"SCardControl 0x%08X", dwReturn); |
Trace(WINEVENT_LEVEL_ERROR, L"SCardControl 0x%08X", dwReturn); |
350 |
__leave; |
__leave; |
351 |
} |
} |
352 |
// regain the ownership of a transaction |
Sleep(200); |
353 |
dwReturn = SCardBeginTransaction(pCardData->hScard); |
dwReturn = SelectOpenPGPApplication(pCardData); |
354 |
if (dwReturn) |
} |
355 |
|
__finally |
356 |
|
{ |
357 |
|
} |
358 |
|
return dwReturn;*/ |
359 |
|
return SCARD_E_UNSUPPORTED_FEATURE; |
360 |
|
} |
361 |
|
|
362 |
|
|
363 |
|
DWORD GetPinInfo(DWORD __in dwPinIndex, __inout PPIN_INFO pPinInfo) |
364 |
|
{ |
365 |
|
DWORD dwReturn=0, dwVersion; |
366 |
|
__try |
367 |
|
{ |
368 |
|
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter dwPinIndex=%d",dwPinIndex); |
369 |
|
dwVersion = (pPinInfo->dwVersion == 0) ? PIN_INFO_CURRENT_VERSION : pPinInfo->dwVersion; |
370 |
|
if ( dwVersion != PIN_INFO_CURRENT_VERSION ) |
371 |
{ |
{ |
372 |
Trace(WINEVENT_LEVEL_ERROR, L"SCardBeginTransaction 0x%08X", dwReturn); |
Trace(WINEVENT_LEVEL_ERROR, L"dwVersion %d", dwVersion); |
373 |
|
dwReturn = ERROR_REVISION_MISMATCH; |
374 |
|
__leave; |
375 |
|
} |
376 |
|
pPinInfo->dwVersion = dwVersion; |
377 |
|
switch(dwPinIndex) |
378 |
|
{ |
379 |
|
case ROLE_SIGNATURE: |
380 |
|
pPinInfo->PinType = AlphaNumericPinType; |
381 |
|
pPinInfo->PinPurpose = DigitalSignaturePin; |
382 |
|
pPinInfo->dwChangePermission = CREATE_PIN_SET(ROLE_SIGNATURE); |
383 |
|
SET_PIN(pPinInfo->dwChangePermission, ROLE_AUTHENTICATION); |
384 |
|
SET_PIN(pPinInfo->dwChangePermission, ROLE_CONFIDENTIALITY); |
385 |
|
pPinInfo->dwUnblockPermission = CREATE_PIN_SET(ROLE_ADMIN); |
386 |
|
SET_PIN(pPinInfo->dwUnblockPermission, ROLE_PUK); |
387 |
|
pPinInfo->PinCachePolicy.dwVersion = PIN_CACHE_POLICY_CURRENT_VERSION; |
388 |
|
pPinInfo->PinCachePolicy.PinCachePolicyType = PinCacheNormal; |
389 |
|
pPinInfo->dwFlags = 0; |
390 |
|
break; |
391 |
|
case ROLE_AUTHENTICATION: |
392 |
|
pPinInfo->PinType = AlphaNumericPinType; |
393 |
|
pPinInfo->PinPurpose = AuthenticationPin; |
394 |
|
pPinInfo->dwChangePermission = CREATE_PIN_SET(ROLE_SIGNATURE); |
395 |
|
SET_PIN(pPinInfo->dwChangePermission, ROLE_AUTHENTICATION); |
396 |
|
SET_PIN(pPinInfo->dwChangePermission, ROLE_CONFIDENTIALITY); |
397 |
|
pPinInfo->dwUnblockPermission = CREATE_PIN_SET(ROLE_ADMIN); |
398 |
|
SET_PIN(pPinInfo->dwUnblockPermission, ROLE_PUK); |
399 |
|
pPinInfo->PinCachePolicy.dwVersion = PIN_CACHE_POLICY_CURRENT_VERSION; |
400 |
|
pPinInfo->PinCachePolicy.PinCachePolicyType = PinCacheNormal; |
401 |
|
pPinInfo->dwFlags = 0; |
402 |
|
break; |
403 |
|
case ROLE_CONFIDENTIALITY: |
404 |
|
pPinInfo->PinType = AlphaNumericPinType; |
405 |
|
pPinInfo->PinPurpose = EncryptionPin; |
406 |
|
pPinInfo->dwChangePermission = CREATE_PIN_SET(ROLE_SIGNATURE); |
407 |
|
SET_PIN(pPinInfo->dwChangePermission, ROLE_AUTHENTICATION); |
408 |
|
SET_PIN(pPinInfo->dwChangePermission, ROLE_CONFIDENTIALITY); |
409 |
|
pPinInfo->dwUnblockPermission = CREATE_PIN_SET(ROLE_ADMIN); |
410 |
|
SET_PIN(pPinInfo->dwUnblockPermission, ROLE_PUK); |
411 |
|
pPinInfo->PinCachePolicy.dwVersion = PIN_CACHE_POLICY_CURRENT_VERSION; |
412 |
|
pPinInfo->PinCachePolicy.PinCachePolicyType = PinCacheNormal; |
413 |
|
pPinInfo->dwFlags = 0; |
414 |
|
break; |
415 |
|
case ROLE_ADMIN: |
416 |
|
pPinInfo->PinType = AlphaNumericPinType; |
417 |
|
pPinInfo->PinPurpose = AdministratorPin; |
418 |
|
pPinInfo->dwChangePermission = CREATE_PIN_SET(ROLE_ADMIN); |
419 |
|
pPinInfo->dwUnblockPermission = 0; |
420 |
|
pPinInfo->PinCachePolicy.dwVersion = PIN_CACHE_POLICY_CURRENT_VERSION; |
421 |
|
pPinInfo->PinCachePolicy.PinCachePolicyType = PinCacheNormal; |
422 |
|
pPinInfo->dwFlags = 0; |
423 |
|
break; |
424 |
|
case ROLE_PUK: |
425 |
|
pPinInfo->PinType = AlphaNumericPinType; |
426 |
|
pPinInfo->PinPurpose = UnblockOnlyPin; |
427 |
|
pPinInfo->dwChangePermission = CREATE_PIN_SET(ROLE_ADMIN); |
428 |
|
pPinInfo->dwUnblockPermission = 0; |
429 |
|
pPinInfo->PinCachePolicy.dwVersion = PIN_CACHE_POLICY_CURRENT_VERSION; |
430 |
|
pPinInfo->PinCachePolicy.PinCachePolicyType = PinCacheNormal; |
431 |
|
pPinInfo->dwFlags = 0; |
432 |
|
break; |
433 |
|
default: |
434 |
|
Trace(WINEVENT_LEVEL_ERROR, L"dwPinIndex == %d", dwPinIndex); |
435 |
|
dwReturn = SCARD_E_INVALID_PARAMETER ; |
436 |
__leave; |
__leave; |
437 |
} |
} |
|
dwReturn = SelectOpenPGPApplication(pCardData); |
|
438 |
} |
} |
439 |
__finally |
__finally |
440 |
{ |
{ |
441 |
} |
} |
442 |
|
Trace(WINEVENT_LEVEL_VERBOSE, L"dwReturn = 0x%08X",dwReturn); |
443 |
return dwReturn; |
return dwReturn; |
444 |
} |
} |