Skip to content
Open
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
2 changes: 1 addition & 1 deletion lib/libpcsc-cpp/include/pcsc-cpp/pcsc-cpp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ constexpr uint8_t PIN_PAD_PIN_ENTRY_TIMEOUT = 90; // 1 minute, 30 seconds
class SmartCard
{
public:
enum class Protocol { UNDEFINED, T0, T1 }; // AUTO = T0 | T1
enum class Protocol : uint8_t { UNDEFINED, T0, T1 }; // AUTO = T0 | T1

class Session
{
Expand Down
61 changes: 34 additions & 27 deletions lib/libpcsc-cpp/src/SmartCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,25 +218,7 @@ class CardImpl
responseBytes.resize(responseLength - 2);
PCSC_CPP_WARNING_POP

ResponseApdu response {sw1, sw2, std::move(responseBytes)};

// Let expected errors through for handling in upper layers or in if blocks below.
switch (response.sw1) {
using enum ResponseApdu::Status;
case OK:
case MORE_DATA_AVAILABLE:
case WRONG_LE_LENGTH:
case VERIFICATION_FAILED:
case VERIFICATION_CANCELLED:
case WRONG_LENGTH:
case COMMAND_NOT_ALLOWED:
case WRONG_PARAMETERS:
return response;
default:
THROW(Error,
"Error response: '" + response + "', protocol "
+ std::to_string(_protocol.dwProtocol));
}
return {sw1, sw2, std::move(responseBytes)};
}
};

Expand All @@ -254,18 +236,43 @@ try {

ResponseApdu SmartCard::Session::transmit(const CommandApdu& command) const
{
auto response = card.transmitBytes(command);
if (response.sw1 == ResponseApdu::WRONG_LE_LENGTH) {
response = card.transmitBytes(CommandApdu(command, response.sw2));
}
auto transmitOnce = [this](const CommandApdu& currentCommand) {
auto response = card.transmitBytes(currentCommand);
if (response.sw1 == ResponseApdu::WRONG_LE_LENGTH) {
response = card.transmitBytes(CommandApdu(currentCommand, response.sw2));
}
return response;
};

auto response = transmitOnce(command);
while (response.sw1 == ResponseApdu::MORE_DATA_AVAILABLE) {
auto newResponse = card.transmitBytes(CommandApdu::getResponse(response.sw2));
auto newResponse = transmitOnce(CommandApdu::getResponse(response.sw2));
if (constexpr size_t maxResponseSize = (std::numeric_limits<uint16_t>::max)();
newResponse.data.size() > maxResponseSize - response.data.size()) {
THROW(Error, "Command chaining and extended lenght not supported");
}
response.data.insert(response.data.end(), newResponse.data.cbegin(),
newResponse.data.cend());
response.sw1 = newResponse.sw1;
response.sw2 = newResponse.sw2;
response.data.insert(response.data.end(), newResponse.data.cbegin(),
newResponse.data.cend());
}
return response;

// Let expected errors through for handling in upper layers or in if blocks below.
switch (response.sw1) {
using enum ResponseApdu::Status;
case OK:
case WRONG_LE_LENGTH:
case VERIFICATION_FAILED:
case VERIFICATION_CANCELLED:
case WRONG_LENGTH:
case COMMAND_NOT_ALLOWED:
case WRONG_PARAMETERS:
return response;
default:
THROW(Error,
"Error response: '" + response + "', protocol "
+ std::to_string(uint8_t(card.protocol())));
}
}

ResponseApdu SmartCard::Session::transmitCTL(const CommandApdu& command, uint16_t lang,
Expand Down
34 changes: 34 additions & 0 deletions lib/libpcsc-cpp/tests/mock/test-connect-to-card-transmit-apdus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include <gtest/gtest.h>

#include <limits>

using namespace pcsc_cpp;

namespace
Expand Down Expand Up @@ -43,3 +45,35 @@ TEST(pcsc_cpp_test, transmitApduSuccess)

EXPECT_TRUE(response.isOK());
}

TEST(pcsc_cpp_test, transmitStopsMoreDataAtSizeLimit)
{
PcscMock::reset();

const auto getResponse = CommandApdu::getResponse();
byte_vector chunk(ResponseApdu::MAX_SIZE, 0x41);
chunk[ResponseApdu::MAX_DATA_SIZE] = ResponseApdu::MORE_DATA_AVAILABLE;
chunk[ResponseApdu::MAX_DATA_SIZE + 1] = 0x00;

// The 256th chunk brings the total to 65 536 bytes, exceeding the 65 535-byte limit.
const size_t chunkCount =
(std::numeric_limits<uint16_t>::max)() / ResponseApdu::MAX_DATA_SIZE + 1;
PcscMock::ApduScript script(chunkCount, {getResponse, chunk});

// Put a READ BINARY command next in the script to detect any extra GET RESPONSE.
// The size limit should stop the command chain before this point.
// An extra GET RESPONSE instead throws PcscMockError, so the EXPECT_THROW fails below.
const auto nextCommand = CommandApdu::readBinary(0, 1);
script.emplace_back(nextCommand, byte_vector {0x42, ResponseApdu::OK, 0x00});
PcscMock::setApduScript(script);

auto card = connectToCard();
auto session = card.beginSession();

EXPECT_THROW(session.transmit(getResponse), Error);
const auto response = session.transmit(nextCommand);
EXPECT_TRUE(response.isOK());
EXPECT_EQ(response.data, (byte_vector {0x42}));

PcscMock::reset();
}
Loading