Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash with FinEID cards #54

Merged
merged 1 commit into from
Jul 11, 2023
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
2 changes: 1 addition & 1 deletion lib/libpcsc-cpp
Submodule libpcsc-cpp updated 1 files
+6 −8 src/SmartCard.cpp
6 changes: 6 additions & 0 deletions src/electronic-ids/pcsc/EIDIDEMIA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,11 @@ ElectronicID::PinRetriesRemainingAndMax EIDIDEMIA::pinRetriesLeft(byte_type pinR
"Command GET DATA ODD failed with error "
+ pcsc_cpp::bytes2hexstr(response.toBytes()));
}
if (response.data.size() < 14) {
THROW(SmartCardError,
"Command GET DATA ODD failed: received data size "
+ std::to_string(response.data.size())
+ " is less than the expected size of the PIN remaining retries offset 14");
}
return {uint8_t(response.data[13]), uint8_t(response.data[10])};
}
8 changes: 7 additions & 1 deletion src/electronic-ids/pcsc/EstEIDGemalto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,16 @@ EstEIDGemaltoV3_5_8::pinRetriesLeft(byte_type pinReference) const
transmitApduWithExpectedResponse(*card, MASTER_FILE);
transmitApduWithExpectedResponse(*card, PINRETRY);
const auto response = card->transmit(READRECORD);
if (!response.isOK() || response.data.size() < 6) {
if (!response.isOK()) {
THROW(SmartCardError,
"Command READRECORD failed with error " + pcsc_cpp::bytes2hexstr(response.toBytes()));
}
if (response.data.size() < 6) {
THROW(SmartCardError,
"Command READRECORD failed: received data size "
+ std::to_string(response.data.size())
+ " is less than the expected size of the PIN remaining retries offset 6");
}
return {uint8_t(response.data[5]), int8_t(3)};
}

Expand Down
20 changes: 15 additions & 5 deletions src/electronic-ids/pcsc/FinEID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@
#include "pcsc-common.hpp"

// FINEID specification:
// App 3.0: https://dvv.fi/documents/16079645/17324923/S1v30.pdf/0bad6ff1-1617-1b1f-ab49-56a2f36ecd38/S1v30.pdf
// Imp 3.0: https://dvv.fi/documents/16079645/17324923/S4-1v30%20(1).pdf/9ed19b95-098d-ec6b-6f31-8147d1f87663/S4-1v30%20(1).pdf
// Imp 3.1: https://dvv.fi/documents/16079645/17324992/S4-1v31.pdf/ca3e699e-fae8-aea2-9ce3-28846d2ae95a/S4-1v31.pdf
// App 4.0: https://dvv.fi/documents/16079645/17324992/S1v40+(1).pdf/56a167fe-9f26-1fda-7d76-cfbbb29d184e/S1v40+(1).pdf
// Imp 4.0: https://dvv.fi/documents/16079645/17324992/S4-1v40.pdf/55bddc08-6893-b4b4-73fa-24dced600198/S4-1v40.pdf
// App 3.0:
// https://dvv.fi/documents/16079645/17324923/S1v30.pdf/0bad6ff1-1617-1b1f-ab49-56a2f36ecd38/S1v30.pdf
// Imp 3.0:
// https://dvv.fi/documents/16079645/17324923/S4-1v30%20(1).pdf/9ed19b95-098d-ec6b-6f31-8147d1f87663/S4-1v30%20(1).pdf
// Imp 3.1:
// https://dvv.fi/documents/16079645/17324992/S4-1v31.pdf/ca3e699e-fae8-aea2-9ce3-28846d2ae95a/S4-1v31.pdf
// App 4.0:
// https://dvv.fi/documents/16079645/17324992/S1v40+(1).pdf/56a167fe-9f26-1fda-7d76-cfbbb29d184e/S1v40+(1).pdf
// Imp 4.0:
// https://dvv.fi/documents/16079645/17324992/S4-1v40.pdf/55bddc08-6893-b4b4-73fa-24dced600198/S4-1v40.pdf

using namespace pcsc_cpp;

Expand Down Expand Up @@ -175,6 +180,11 @@ ElectronicID::PinRetriesRemainingAndMax FinEIDv3::pinRetriesLeft(byte_type pinRe
THROW(SmartCardError,
"Command GET DATA failed with error " + pcsc_cpp::bytes2hexstr(response.toBytes()));
}
if (response.data.size() < 21) {
THROW(SmartCardError,
"Command GET DATA failed: received data size " + std::to_string(response.data.size())
+ " is less than the expected size of the PIN remaining retries offset 21");
}
return {uint8_t(response.data[20]), int8_t(5)};
}

Expand Down
6 changes: 3 additions & 3 deletions src/electronic-ids/pcsc/pcsc-common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ inline pcsc_cpp::byte_vector computeSignature(pcsc_cpp::SmartCard& card,
{
static const pcsc_cpp::CommandApdu COMPUTE_SIGNATURE {0x00, 0x2A, 0x9E, 0x9A};

pcsc_cpp::CommandApdu internalAuth {COMPUTE_SIGNATURE, hash};
pcsc_cpp::CommandApdu computeSignature {COMPUTE_SIGNATURE, hash};
// LE is needed in case of protocol T1.
// TODO: Implement this in libpcsc-cpp.
if (card.protocol() == pcsc_cpp::SmartCard::Protocol::T1) {
internalAuth.le = 0;
computeSignature.le = 0;
}

const auto response = card.transmit(internalAuth);
const auto response = card.transmit(computeSignature);

if (response.sw1 == pcsc_cpp::ResponseApdu::WRONG_LENGTH) {
THROW(SmartCardError,
Expand Down