mirror of
https://github.com/Proxmark/proxmark3.git
synced 2025-08-20 21:33:19 -07:00
Add support for standard USB Smartcard Readers (#765)
* add PCSC interface (pcsc.c and pcsc.h) * new command 'sc select' to choose an USB Smartcard Reader * updated CI/.travis.yml accordingly * remove TCK CRC check in i2c.c It is done in PrintATR() anyway * Fix TCK CRC check in PrintATR() * Add PCSC reader support to 'sc info'
This commit is contained in:
parent
1511ea28a8
commit
a9104f7e31
14 changed files with 382 additions and 93 deletions
177
client/pcsc.c
Normal file
177
client/pcsc.c
Normal file
|
@ -0,0 +1,177 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Copyright (C) 2019 piwi
|
||||
//
|
||||
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
|
||||
// at your option, any later version. See the LICENSE.txt file for the text of
|
||||
// the license.
|
||||
//-----------------------------------------------------------------------------
|
||||
// PCSC functions to use alternative Smartcard Readers
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include "pcsc.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined (__APPLE__)
|
||||
#include <PCSC/winscard.h>
|
||||
#include <PCSC/wintypes.h>
|
||||
#define SCARD_ATTR_VALUE(Class, Tag) ((((ULONG)(Class)) << 16) | ((ULONG)(Tag)))
|
||||
#define SCARD_CLASS_ICC_STATE 9
|
||||
#define SCARD_ATTR_ATR_STRING SCARD_ATTR_VALUE(SCARD_CLASS_ICC_STATE, 0x0303)
|
||||
#elif defined (_WIN32)
|
||||
#include <winscard.h>
|
||||
#else
|
||||
#include <winscard.h>
|
||||
#include <reader.h>
|
||||
#endif
|
||||
|
||||
#include "ui.h"
|
||||
#include "util.h"
|
||||
#include "cmdhw.h"
|
||||
|
||||
static SCARDCONTEXT SC_Context;
|
||||
static SCARDHANDLE SC_Card;
|
||||
static DWORD SC_Protocol;
|
||||
static char* AlternativeSmartcardReader = NULL;
|
||||
|
||||
|
||||
char *getAlternativeSmartcardReader(void)
|
||||
{
|
||||
return AlternativeSmartcardReader;
|
||||
}
|
||||
|
||||
|
||||
bool pcscCheckForCardReaders(void)
|
||||
{
|
||||
LONG res = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &SC_Context);
|
||||
if (res != SCARD_S_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
DWORD pcchReaders;
|
||||
res = SCardListReaders(SC_Context, NULL, NULL, &pcchReaders);
|
||||
if (res != SCARD_S_SUCCESS) {
|
||||
SCardReleaseContext(SC_Context);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (res == SCARD_E_NO_READERS_AVAILABLE || res == SCARD_E_NO_SERVICE) {
|
||||
SCardReleaseContext(SC_Context);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
static char *pickReader(LPTSTR readerlist)
|
||||
{
|
||||
PrintAndLogEx(NORMAL, "Please select one of these:");
|
||||
PrintAndLogEx(NORMAL, " [0] PM3 RDV40 Smartcard Slot %s", PM3hasSmartcardSlot() ? "(default)" : "(default, not available)");
|
||||
|
||||
int num = 1;
|
||||
for (LPTSTR p = readerlist; *p != '\0'; ) {
|
||||
PrintAndLogEx(NORMAL, " [%1d] %s", num++, p);
|
||||
while (*p++ != '\0') ; // advance to next entry
|
||||
}
|
||||
|
||||
num--;
|
||||
|
||||
if (num == 1) {
|
||||
printf("Your choice (0 or 1)?");
|
||||
} else {
|
||||
printf("Your choice (0...%d)? ", num);
|
||||
}
|
||||
int selection = getch() - '0';
|
||||
printf("\n");
|
||||
|
||||
if (selection == 0) {
|
||||
PrintAndLogEx(INFO, "Selected RDV40 Smartcard Slot");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (selection >= 1 && selection <= num) {
|
||||
LPTSTR p = readerlist;
|
||||
for (int i = 1; i < selection; i++) {
|
||||
while (*p++ != '\0') ; // advance to next entry
|
||||
}
|
||||
PrintAndLogEx(INFO, "Selected %s", p);
|
||||
return p;
|
||||
}
|
||||
|
||||
PrintAndLogEx(INFO, "Invalid selection. Using RDV40 Smartcard Slot");
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
|
||||
char *matchString(LPTSTR readerlist, const char *readername)
|
||||
{
|
||||
return pickReader(readerlist);
|
||||
}
|
||||
|
||||
|
||||
bool pcscSelectAlternativeCardReader(const char *readername)
|
||||
{
|
||||
DWORD readerlist_len;
|
||||
LONG res = SCardListReaders(SC_Context, NULL, NULL, &readerlist_len);
|
||||
if (res != SCARD_S_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
LPTSTR readerlist = calloc(readerlist_len, sizeof(char));
|
||||
res = SCardListReaders(SC_Context, NULL, readerlist, &readerlist_len);
|
||||
if (res != SCARD_S_SUCCESS) {
|
||||
free(readerlist);
|
||||
return false;
|
||||
}
|
||||
|
||||
char *selected_readername = NULL;
|
||||
if (readername) {
|
||||
selected_readername = matchString(readerlist, readername);
|
||||
} else {
|
||||
selected_readername = pickReader(readerlist);
|
||||
}
|
||||
|
||||
if (selected_readername == NULL) {
|
||||
free(readerlist);
|
||||
return false;
|
||||
}
|
||||
|
||||
free(AlternativeSmartcardReader);
|
||||
AlternativeSmartcardReader = malloc((strlen(selected_readername) + 1) * sizeof(char));
|
||||
strcpy(AlternativeSmartcardReader, selected_readername);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool pcscGetATR(smart_card_atr_t *card)
|
||||
{
|
||||
if (!card) {
|
||||
return false;
|
||||
}
|
||||
|
||||
card->atr_len = 0;
|
||||
memset(card->atr, 0, sizeof(card->atr));
|
||||
|
||||
LONG res = SCardConnect(SC_Context, AlternativeSmartcardReader, SCARD_SHARE_SHARED,
|
||||
SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &SC_Card, &SC_Protocol);
|
||||
if (res != SCARD_S_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
DWORD atr_len = sizeof(card->atr);
|
||||
res = SCardGetAttrib(SC_Card, SCARD_ATTR_ATR_STRING, card->atr, &atr_len);
|
||||
if (res != SCARD_S_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
card->atr_len = atr_len;
|
||||
|
||||
// TODO: LogTrace without device
|
||||
|
||||
return true;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue