Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions lib/libpcsc-cpp/include/pcsc-cpp/pcsc-cpp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
#define PCSC_CPP_WARNING_DISABLE_MSVC(text)
#endif

#ifdef __cpp_lib_constexpr_vector
#define PCSC_CPP_CONSTEXPR_VECTOR constexpr
#else
#define PCSC_CPP_CONSTEXPR_VECTOR
#endif

namespace pcsc_cpp
{

Expand All @@ -78,7 +84,7 @@ constexpr uint16_t toSW(byte_type sw1, byte_type sw2) noexcept
/** Struct that wraps response APDUs. */
struct ResponseApdu
{
enum Status {
enum Status: byte_type {
OK = 0x90,
MORE_DATA_AVAILABLE = 0x61,
VERIFICATION_FAILED = 0x63,
Expand All @@ -92,19 +98,12 @@ struct ResponseApdu
byte_type sw1 {};
byte_type sw2 {};

byte_vector data;
byte_vector data {};

static constexpr size_t MAX_DATA_SIZE = 256;
static constexpr size_t MAX_SIZE = MAX_DATA_SIZE + 2; // + sw1 and sw2

ResponseApdu(byte_type s1, byte_type s2, byte_vector d = {}) :
sw1(s1), sw2(s2), data(std::move(d))
{
}

ResponseApdu() = default;

static ResponseApdu fromBytes(byte_vector data)
PCSC_CPP_CONSTEXPR_VECTOR static ResponseApdu fromBytes(byte_vector data)
{
if (data.size() < 2) {
throw std::invalid_argument("Need at least 2 bytes for creating ResponseApdu");
Expand Down Expand Up @@ -282,14 +281,11 @@ std::vector<Reader> listReaders();

// Utility functions.

extern const byte_vector APDU_RESPONSE_OK;

/** Convert bytes to hex string. */
std::string bytes2hexstr(const byte_vector& bytes);

/** Transmit APDU command and verify that expected response is received. */
void transmitApduWithExpectedResponse(const SmartCard& card, const CommandApdu& command,
const byte_vector& expectedResponseBytes = APDU_RESPONSE_OK);
void transmitApduWithExpectedResponse(const SmartCard& card, const CommandApdu& command);

/** Read data length from currently selected file header, file must be ASN.1-encoded. */
size_t readDataLengthFromAsn1(const SmartCard& card);
Expand Down
2 changes: 1 addition & 1 deletion lib/libpcsc-cpp/src/SmartCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class CardImpl
{
byte_vector getResponseCommand {0x00, 0xc0, 0x00, 0x00, 0x00};

auto newResponse = ResponseApdu(response.sw1, response.sw2);
ResponseApdu newResponse {response.sw1, response.sw2};

while (newResponse.sw1 == ResponseApdu::MORE_DATA_AVAILABLE) {
getResponseCommand[4] = newResponse.sw2;
Expand Down
22 changes: 8 additions & 14 deletions lib/libpcsc-cpp/src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,13 @@ const byte_type DER_TWO_BYTE_LENGTH = 0x82;
class UnexpectedResponseError : public Error
{
public:
explicit UnexpectedResponseError(const CommandApdu& command,
const byte_vector& expectedResponseBytes,
const ResponseApdu& response, const char* file, const int line,
explicit UnexpectedResponseError(const CommandApdu& command, const ResponseApdu& response,
const char* file, const int line,
const char* callerFunctionName) :
Error("transmitApduWithExpectedResponse(): Unexpected response to command '"s
+ bytes2hexstr(command.toBytes()) + "' - expected '"s
+ bytes2hexstr(expectedResponseBytes) + "', got '"s + bytes2hexstr(response.toBytes())
+ " in " + removeAbsolutePathPrefix(file) + ':' + std::to_string(line) + ':'
+ callerFunctionName)
+ bytes2hexstr(command.toBytes()) + "' - expected '9000', got '"s
+ bytes2hexstr(response.toBytes()) + " in " + removeAbsolutePathPrefix(file) + ':'
+ std::to_string(line) + ':' + callerFunctionName)
{
}
};
Expand All @@ -72,8 +70,6 @@ class UnexpectedResponseError : public Error
namespace pcsc_cpp
{

const byte_vector APDU_RESPONSE_OK {ResponseApdu::OK, 0x00};

std::string bytes2hexstr(const byte_vector& bytes)
{
std::ostringstream hexStringBuilder;
Expand All @@ -86,13 +82,11 @@ std::string bytes2hexstr(const byte_vector& bytes)
return hexStringBuilder.str();
}

void transmitApduWithExpectedResponse(const SmartCard& card, const CommandApdu& command,
const byte_vector& expectedResponseBytes)
void transmitApduWithExpectedResponse(const SmartCard& card, const CommandApdu& command)
{
const auto response = card.transmit(command);
if (response.toBytes() != expectedResponseBytes) {
throw UnexpectedResponseError(command, expectedResponseBytes, response, __FILE__, __LINE__,
__func__);
if (!response.isOK()) {
throw UnexpectedResponseError(command, response, __FILE__, __LINE__, __func__);
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/libpcsc-cpp/tests/lib/libpcsc-mock/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.8.0)
cmake_minimum_required(VERSION 3.10.0)
if(POLICY CMP0092)
cmake_policy(SET CMP0092 NEW)
endif()
Expand Down
Empty file modified lib/libpcsc-cpp/tests/lib/libpcsc-mock/scripts/clang-format.sh
100644 → 100755
Empty file.
14 changes: 7 additions & 7 deletions lib/libpcsc-cpp/tests/lib/libpcsc-mock/src/pcsc-mock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,19 @@ const PcscMock::string_t PcscMock::DEFAULT_READER_NAME {"PcscMock-reader"s};
#endif

const PcscMock::byte_vector PcscMock::DEFAULT_COMMAND_APDU {0x2, 0x1, 0x3, 0x4};
const PcscMock::byte_vector PcscMock::DEFAULT_RESPONSE_APDU {0x90, 0x3};
const PcscMock::byte_vector PcscMock::DEFAULT_RESPONSE_APDU {0x90, 0x00};

const PcscMock::ApduScript PcscMock::DEFAULT_SCRIPT {{DEFAULT_COMMAND_APDU, DEFAULT_RESPONSE_APDU}};

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4273)
__declspec(dllexport) const SCARD_IO_REQUEST g_rgSCardT0Pci = {SCARD_PROTOCOL_T0,
sizeof(SCARD_IO_REQUEST)};
__declspec(dllexport) const SCARD_IO_REQUEST g_rgSCardT1Pci = {SCARD_PROTOCOL_T1,
sizeof(SCARD_IO_REQUEST)};
__declspec(dllexport) const SCARD_IO_REQUEST g_rgSCardRawPci = {SCARD_PROTOCOL_RAW,
sizeof(SCARD_IO_REQUEST)};
__declspec(dllexport)
const SCARD_IO_REQUEST g_rgSCardT0Pci = {SCARD_PROTOCOL_T0, sizeof(SCARD_IO_REQUEST)};
__declspec(dllexport)
const SCARD_IO_REQUEST g_rgSCardT1Pci = {SCARD_PROTOCOL_T1, sizeof(SCARD_IO_REQUEST)};
__declspec(dllexport)
const SCARD_IO_REQUEST g_rgSCardRawPci = {SCARD_PROTOCOL_RAW, sizeof(SCARD_IO_REQUEST)};
#pragma warning(pop)
#else
MOCK_CONST SCARD_IO_REQUEST g_rgSCardT0Pci = {SCARD_PROTOCOL_T0, sizeof(SCARD_IO_REQUEST)};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ TEST(pcsc_cpp_test, transmitApduSuccess)
CommandApdu command {PcscMock::DEFAULT_COMMAND_APDU[0], PcscMock::DEFAULT_COMMAND_APDU[1],
PcscMock::DEFAULT_COMMAND_APDU[2], PcscMock::DEFAULT_COMMAND_APDU[3]};

auto expectedResponse = ResponseApdu::fromBytes(PcscMock::DEFAULT_RESPONSE_APDU);

auto transactionGuard = card->beginTransaction();
auto response = card->transmit(command);

EXPECT_EQ(response.toBytes(), expectedResponse.toBytes());
EXPECT_TRUE(response.isOK());
}
Loading