diff options
author | Mounir IDRASSI <mounir.idrassi@idrix.fr> | 2023-06-29 00:06:20 +0200 |
---|---|---|
committer | Mounir IDRASSI <mounir.idrassi@idrix.fr> | 2023-06-29 00:06:20 +0200 |
commit | 034b64f4153550cbe5849bcbfc27e187377cc512 (patch) | |
tree | d831496163c3891031765010bf1934406b0c4a3c /src/Common/SCardManager.cpp | |
parent | 502ab9112a7624dbd7c1c90c2e12ed45512b8b3c (diff) | |
download | VeraCrypt-034b64f4153550cbe5849bcbfc27e187377cc512.tar.gz VeraCrypt-034b64f4153550cbe5849bcbfc27e187377cc512.zip |
EMV keyfile support: Overall code improvements and bug fixes
Diffstat (limited to 'src/Common/SCardManager.cpp')
-rw-r--r-- | src/Common/SCardManager.cpp | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/src/Common/SCardManager.cpp b/src/Common/SCardManager.cpp new file mode 100644 index 00000000..40578d39 --- /dev/null +++ b/src/Common/SCardManager.cpp @@ -0,0 +1,109 @@ +#include "SCardManager.h" +#include "PCSCException.h" + +namespace VeraCrypt +{ + shared_ptr<SCardLoader> SCardManager::loader = make_shared<SCardLoader>(); + + SCardManager::SCardManager() + { + loader->Initialize(); + } + + SCardManager::~SCardManager() + { + loader->Finalize(); + } + + vector<wstring> SCardManager::GetReaders() + { + vector<wstring> readers; + LPTSTR mszReaders = NULL; + LPTSTR ptr = NULL; + DWORD dwReaders = 0; + SCARDCONTEXT hScardContext = 0; + LONG lRet = SCARD_S_SUCCESS; + + hScardContext = loader->GetSCardContext(); + lRet = loader->SCardIsValidContext(hScardContext); + if (SCARD_S_SUCCESS != lRet) + { + loader->SCardReleaseContext(hScardContext); + lRet = loader->SCardEstablishContext(SCARD_SCOPE_USER, NULL, NULL, &hScardContext); + if (lRet != SCARD_S_SUCCESS) + throw PCSCException(lRet); + } + +#ifdef SCARD_AUTOALLOCATE + dwReaders = SCARD_AUTOALLOCATE; + lRet = loader->SCardListReaders(hScardContext, NULL, (LPTSTR)&mszReaders, &dwReaders); +#else + lRet = loader->SCardListReaders(hScardContext, NULL, NULL, &dwReaders); + if (lRet == SCARD_S_SUCCESS) + { + mszReaders = (LPTSTR)calloc(dwReaders, sizeof(char)); + lRet = loader->SCardListReaders(hScardContext, NULL, mszReaders, &dwReaders); + } +#endif + + if (lRet == SCARD_S_SUCCESS && !mszReaders) + { + lRet = SCARD_E_NO_READERS_AVAILABLE; + } + if (lRet == SCARD_E_NO_READERS_AVAILABLE) + { + readers.clear(); + lRet = SCARD_S_SUCCESS; + } + + if (lRet == SCARD_S_SUCCESS && mszReaders) + { + ptr = mszReaders; + while (*ptr) + { +#ifdef TC_WINDOWS + readers.push_back(ptr); +#else + readers.push_back(StringConverter::ToWide(ptr)); +#endif + ptr += +#ifdef TC_WINDOWS + wcslen(ptr) + 1; +#else + strlen(ptr) + 1; +#endif + } + +#ifdef SCARD_AUTOALLOCATE + loader->SCardFreeMemory(hScardContext, mszReaders); +#else + free(mszReaders); +#endif + } + + if (lRet != SCARD_S_SUCCESS) + throw PCSCException(lRet); + + return readers; + } + + shared_ptr<SCardReader> SCardManager::GetReader(size_t readerNumber) + { + vector<wstring> readers; + shared_ptr<SCardReader> smartCardReader; + + loader->Initialize(); + + readers = GetReaders(); + if (readerNumber < readers.size()) + { + smartCardReader = make_shared<SCardReader>(readers[readerNumber], loader); + if (smartCardReader) + { + return smartCardReader; + } + } + + throw InvalidEMVPath(); + } +}
\ No newline at end of file |