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
22 changes: 7 additions & 15 deletions lib/libpcsc-cpp/include/pcsc-cpp/pcsc-cpp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ constexpr uint16_t toSW(byte_type sw1, byte_type sw2) noexcept
return uint16_t(sw1 << 8) | sw2;
}

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

/** Struct that wraps response APDUs. */
struct ResponseApdu
{
Expand Down Expand Up @@ -120,22 +123,14 @@ struct ResponseApdu
return {sw1, sw2, std::move(data)};
}

byte_vector toBytes() const
{
// makes a copy, valid both if data is empty or full
auto bytes = data;

bytes.push_back(sw1);
bytes.push_back(sw2);

return bytes;
}

constexpr uint16_t toSW() const noexcept { return pcsc_cpp::toSW(sw1, sw2); }

constexpr bool isOK() const noexcept { return sw1 == OK && sw2 == 0x00; }

// TODO: friend function toString() in utilities.hpp
friend std::string operator+(std::string&& lhs, const ResponseApdu& rhs)
{
return lhs + pcsc_cpp::bytes2hexstr(rhs.data) + pcsc_cpp::bytes2hexstr({rhs.sw1, rhs.sw2});
}
};

/** Struct that wraps command APDUs. */
Expand Down Expand Up @@ -270,9 +265,6 @@ std::vector<Reader> listReaders();

// Utility functions.

/** 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);

Expand Down
3 changes: 1 addition & 2 deletions lib/libpcsc-cpp/src/SmartCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,7 @@ class CardImpl
break;
default:
THROW(Error,
"Error response: '" + bytes2hexstr({response.sw1, response.sw2}) + "', protocol "
+ std::to_string(protocol()));
"Error response: '" + response + "', protocol " + std::to_string(protocol()));
}

if (response.sw1 == ResponseApdu::WRONG_LE_LENGTH) {
Expand Down
6 changes: 3 additions & 3 deletions lib/libpcsc-cpp/src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ class UnexpectedResponseError : public Error
const char* file, const int line,
const char* callerFunctionName) :
Error("transmitApduWithExpectedResponse(): Unexpected response to command '"s
+ bytes2hexstr(command) + "' - expected '9000', got '"s
+ bytes2hexstr(response.toBytes()) + "' in " + removeAbsolutePathPrefix(file) + ':'
+ std::to_string(line) + ':' + callerFunctionName)
+ bytes2hexstr(command) + "' - expected '9000', got '"s + response + "' in "
+ removeAbsolutePathPrefix(file) + ':' + std::to_string(line) + ':'
+ callerFunctionName)
{
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/electronic-id.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ AutoSelectFailed::AutoSelectFailed(Reason r) :
VerifyPinFailed::VerifyPinFailed(const Status s, const observer_ptr<pcsc_cpp::ResponseApdu> ra,
const int8_t r) :
Error(std::string("Verify PIN failed, status: ") + std::string(magic_enum::enum_name(s))
+ (ra ? ", response: " + pcsc_cpp::bytes2hexstr(ra->toBytes()) : "")),
+ (ra ? ", response: " + *ra : "")),
_status(s), _retries(r)
{
}
Expand Down
4 changes: 1 addition & 3 deletions src/electronic-ids/pcsc/EIDIDEMIA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ ElectronicID::PinRetriesRemainingAndMax EIDIDEMIA::pinRetriesLeft(byte_type pinR
0x00};
const auto response = card->transmit(GET_DATA_ODD);
if (!response.isOK()) {
THROW(SmartCardError,
"Command GET DATA ODD failed with error "
+ pcsc_cpp::bytes2hexstr(response.toBytes()));
THROW(SmartCardError, "Command GET DATA ODD failed with error " + response);
}
if (response.data.size() < 14) {
THROW(SmartCardError,
Expand Down
16 changes: 5 additions & 11 deletions src/electronic-ids/pcsc/FinEID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,25 +148,20 @@ byte_vector FinEIDv3::sign(const HashAlgorithm hashAlgo, const byte_vector& hash

if (response.sw1 == ResponseApdu::WRONG_LENGTH) {
THROW(SmartCardError,
"Wrong data length in command COMPUTE SIGNATURE argument: "
+ bytes2hexstr(response.toBytes()));
"Wrong data length in command COMPUTE SIGNATURE argument: " + response);
}
if (!response.isOK()) {
THROW(SmartCardError,
"Command COMPUTE SIGNATURE failed with error " + bytes2hexstr(response.toBytes()));
THROW(SmartCardError, "Command COMPUTE SIGNATURE failed with error " + response);
}

const CommandApdu getSignature {0x00, 0x2A, 0x9E, 0x9A, LE};
const auto signature = card->transmit(getSignature);

if (signature.sw1 == ResponseApdu::WRONG_LENGTH) {
THROW(SmartCardError,
"Wrong data length in command GET SIGNATURE argument: "
+ bytes2hexstr(response.toBytes()));
THROW(SmartCardError, "Wrong data length in command GET SIGNATURE argument: " + response);
}
if (!signature.isOK()) {
THROW(SmartCardError,
"Command GET SIGNATURE failed with error " + bytes2hexstr(signature.toBytes()));
THROW(SmartCardError, "Command GET SIGNATURE failed with error " + signature);
}

return signature.data;
Expand All @@ -178,8 +173,7 @@ ElectronicID::PinRetriesRemainingAndMax FinEIDv3::pinRetriesLeft(byte_type pinRe
0x00, 0xCB, 0x00, 0xFF, {0xA0, 0x03, 0x83, 0x01, pinReference}};
const auto response = card->transmit(GET_DATA);
if (!response.isOK()) {
THROW(SmartCardError,
"Command GET DATA failed with error " + pcsc_cpp::bytes2hexstr(response.toBytes()));
THROW(SmartCardError, "Command GET DATA failed with error " + response);
}
if (response.data.size() < 21) {
THROW(SmartCardError,
Expand Down
17 changes: 6 additions & 11 deletions src/electronic-ids/pcsc/pcsc-common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,12 @@ inline pcsc_cpp::byte_vector internalAuthenticate(pcsc_cpp::SmartCard& card,

if (response.sw1 == pcsc_cpp::ResponseApdu::WRONG_LENGTH) {
THROW(SmartCardError,
cardType + ": Wrong data length in command INTERNAL AUTHENTICATE argument: "
+ pcsc_cpp::bytes2hexstr(response.toBytes()));
cardType
+ ": Wrong data length in command INTERNAL AUTHENTICATE argument: " + response);
}
if (!response.isOK()) {
THROW(SmartCardError,
cardType + ": Command INTERNAL AUTHENTICATE failed with error "
+ pcsc_cpp::bytes2hexstr(response.toBytes()));
cardType + ": Command INTERNAL AUTHENTICATE failed with error " + response);
}

return response.data;
Expand All @@ -144,13 +143,11 @@ inline pcsc_cpp::byte_vector computeSignature(pcsc_cpp::SmartCard& card,

if (response.sw1 == pcsc_cpp::ResponseApdu::WRONG_LENGTH) {
THROW(SmartCardError,
cardType + ": Wrong data length in command COMPUTE SIGNATURE argument: "
+ pcsc_cpp::bytes2hexstr(response.toBytes()));
cardType + ": Wrong data length in command COMPUTE SIGNATURE argument: " + response);
}
if (!response.isOK()) {
THROW(SmartCardError,
cardType + ": Command COMPUTE SIGNATURE failed with error "
+ pcsc_cpp::bytes2hexstr(response.toBytes()));
cardType + ": Command COMPUTE SIGNATURE failed with error " + response);
}

return response.data;
Expand All @@ -165,9 +162,7 @@ inline pcsc_cpp::byte_type selectSecurityEnv(pcsc_cpp::SmartCard& card, pcsc_cpp
{0x00, 0x22, 0x41, env, {0x80, 0x01, signatureAlgo, 0x84, 0x01, keyReference}});

if (!response.isOK()) {
THROW(SmartCardError,
cardType + ": Command SET ENV failed with error "
+ pcsc_cpp::bytes2hexstr(response.toBytes()));
THROW(SmartCardError, cardType + ": Command SET ENV failed with error " + response);
}
return signatureAlgo;
}
Expand Down
Loading