Skip to content

Commit 2ca94da

Browse files
committed
Remove CardInfo
Signed-off-by: Raul Metsma <[email protected]>
1 parent f47fc15 commit 2ca94da

File tree

12 files changed

+76
-115
lines changed

12 files changed

+76
-115
lines changed

include/electronic-id/electronic-id.hpp

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -114,27 +114,9 @@ bool isCardSupported(const pcsc_cpp::byte_vector& atr);
114114

115115
ElectronicID::ptr getElectronicID(const pcsc_cpp::Reader& reader);
116116

117-
/** Aggregates reader and electronic ID objects for communicating with and inspecting the eID card.
118-
*/
119-
class CardInfo
120-
{
121-
public:
122-
using ptr = std::shared_ptr<CardInfo>;
123-
124-
CardInfo(pcsc_cpp::Reader r, ElectronicID::ptr e) : _reader(std::move(r)), _eid(std::move(e)) {}
125-
126-
const pcsc_cpp::Reader& reader() const { return _reader; }
127-
const ElectronicID& eid() const { return *_eid; }
128-
const ElectronicID::ptr eidPtr() const { return _eid; }
129-
130-
private:
131-
pcsc_cpp::Reader _reader;
132-
ElectronicID::ptr _eid;
133-
};
134-
135117
/** Automatic card selection that either returns a vector of card info pointers with available
136118
* supported cards or throws AutoSelectFailed. */
137-
std::vector<CardInfo::ptr> availableSupportedCards();
119+
std::vector<ElectronicID::ptr> availableSupportedCards();
138120

139121
/** Base class for fatal errors in parameters or environment conditions that do not allow retrying.
140122
*/

lib/libpcsc-cpp/include/pcsc-cpp/pcsc-cpp.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class SmartCard
232232
bool& inProgress;
233233
};
234234

235-
SmartCard(const ContextPtr& context, const string_t& readerName, byte_vector atr);
235+
SmartCard(ContextPtr context, string_t readerName, byte_vector atr);
236236
SmartCard(); // Null object constructor.
237237
~SmartCard();
238238
PCSC_CPP_DISABLE_COPY_MOVE(SmartCard);
@@ -244,9 +244,12 @@ class SmartCard
244244

245245
Protocol protocol() const { return _protocol; }
246246
const byte_vector& atr() const { return _atr; }
247+
const string_t& name() const { return _name; }
247248

248249
private:
250+
ContextPtr ctx;
249251
CardImplPtr card;
252+
string_t _name;
250253
byte_vector _atr;
251254
Protocol _protocol = Protocol::UNDEFINED;
252255
bool transactionInProgress = false;

lib/libpcsc-cpp/src/SmartCard.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,10 @@ SmartCard::TransactionGuard::~TransactionGuard()
288288
}
289289
}
290290

291-
SmartCard::SmartCard(const ContextPtr& contex, const string_t& readerName, byte_vector atr) :
292-
card(std::make_unique<CardImpl>(connectToCard(contex->handle(), readerName))),
293-
_atr(std::move(atr)), _protocol(convertToSmartCardProtocol(card->protocol()))
291+
SmartCard::SmartCard(ContextPtr contex, string_t readerName, byte_vector atr) :
292+
ctx(contex), card(std::make_unique<CardImpl>(connectToCard(contex->handle(), readerName))),
293+
_name(std::move(readerName)), _atr(std::move(atr)),
294+
_protocol(convertToSmartCardProtocol(card->protocol()))
294295
{
295296
// TODO: debug("Card ATR -> " + bytes2hexstr(atr))
296297
}

src/availableSupportedCards.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,15 @@
2626
#include "electronic-ids/ms-cryptoapi/listMsCryptoApiElectronicIDs.hpp"
2727
#endif
2828

29-
namespace
30-
{
31-
32-
using namespace electronic_id;
33-
34-
inline CardInfo::ptr connectToCard(const pcsc_cpp::Reader& reader)
35-
{
36-
auto eid = getElectronicID(reader);
37-
return std::make_shared<CardInfo>(reader, eid);
38-
}
39-
40-
} // namespace
41-
4229
namespace electronic_id
4330
{
4431

45-
std::vector<CardInfo::ptr> availableSupportedCards()
32+
std::vector<ElectronicID::ptr> availableSupportedCards()
4633
{
4734
std::vector<pcsc_cpp::Reader> readers;
4835
try {
4936
readers = pcsc_cpp::listReaders();
50-
std::vector<CardInfo::ptr> cards;
37+
std::vector<ElectronicID::ptr> cards;
5138

5239
auto seenCard = false;
5340
// The list may be empty, but we cannot throw yet due to the listMsCryptoApiElectronicIDs()
@@ -58,7 +45,7 @@ std::vector<CardInfo::ptr> availableSupportedCards()
5845
}
5946
seenCard = true;
6047
if (isCardSupported(reader.cardAtr)) {
61-
cards.push_back(connectToCard(reader));
48+
cards.push_back(getElectronicID(reader));
6249
}
6350
}
6451

src/electronic-ids/ms-cryptoapi/listMsCryptoApiElectronicIDs.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace electronic_id
3636

3737
// Enumerates all certificates and converts the valid hardware-based ones to MsCryptoApiElectronicID
3838
// objects.
39-
std::vector<CardInfo::ptr> listMsCryptoApiElectronicIDs()
39+
std::vector<ElectronicID::ptr> listMsCryptoApiElectronicIDs()
4040
{
4141
HCERTSTORE sys =
4242
CertOpenStore(CERT_STORE_PROV_SYSTEM, X509_ASN_ENCODING, 0,
@@ -46,13 +46,7 @@ std::vector<CardInfo::ptr> listMsCryptoApiElectronicIDs()
4646
}
4747
auto closeCertStore = stdext::make_scope_exit([=]() { CertCloseStore(sys, 0); });
4848

49-
std::vector<CardInfo::ptr> msCryptoApiElectronicIDs;
50-
pcsc_cpp::Reader dummyReader {
51-
nullptr,
52-
L"Dummy reader for MS CryptoAPI tokens"s,
53-
{},
54-
flag_set<pcsc_cpp::Reader::Status> {pcsc_cpp::Reader::Status::PRESENT},
55-
};
49+
std::vector<ElectronicID::ptr> msCryptoApiElectronicIDs;
5650

5751
PCCERT_CONTEXT cert = nullptr;
5852
while ((cert = CertEnumCertificatesInStore(sys, cert)) != nullptr) {
@@ -121,8 +115,7 @@ std::vector<CardInfo::ptr> listMsCryptoApiElectronicIDs()
121115
continue; // TODO: log.
122116
}
123117
algo.resize(size / 2 - 1);
124-
// TODO: use algo.starts_with(L"EC") when migrating to C++20.
125-
if (algo != L"RSA" && algo.rfind(L"EC", 0) != 0) {
118+
if (algo != L"RSA" && !algo.starts_with(L"EC")) {
126119
// We only support RSA and ECC algorithms.
127120
continue; // TODO: log.
128121
}
@@ -147,7 +140,7 @@ std::vector<CardInfo::ptr> listMsCryptoApiElectronicIDs()
147140
std::move(certData), certType,
148141
algo == L"RSA", key, freeKey);
149142

150-
msCryptoApiElectronicIDs.push_back(std::make_shared<CardInfo>(dummyReader, std::move(eid)));
143+
msCryptoApiElectronicIDs.push_back(std::move(eid));
151144
}
152145

153146
// CertEnumCertificatesInStore() function frees the CERT_CONTEXT referenced by non-NULL values

src/electronic-ids/ms-cryptoapi/listMsCryptoApiElectronicIDs.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
namespace electronic_id
2828
{
2929

30-
std::vector<CardInfo::ptr> listMsCryptoApiElectronicIDs();
30+
std::vector<ElectronicID::ptr> listMsCryptoApiElectronicIDs();
3131

3232
}

tests/common/selectcard.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include <stdexcept>
66

7-
inline electronic_id::CardInfo::ptr autoSelectSupportedCard() {
7+
inline electronic_id::ElectronicID::ptr autoSelectSupportedCard() {
88
using namespace electronic_id;
99

1010
auto cardList = availableSupportedCards();

tests/integration/test-authenticate.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ TEST(electronic_id_test, authenticate)
3636

3737
EXPECT_TRUE(cardInfo);
3838

39-
std::cout << "Selected card: " << cardInfo->eid().name() << '\n';
39+
std::cout << "Selected card: " << cardInfo->name() << '\n';
4040

41-
byte_vector cert = cardInfo->eid().getCertificate(CertificateType::AUTHENTICATION);
41+
byte_vector cert = cardInfo->getCertificate(CertificateType::AUTHENTICATION);
4242

4343
std::cout << "Does the reader have a PIN-pad? "
44-
<< (cardInfo->eid().smartcard().readerHasPinPad() ? "yes" : "no") << '\n';
44+
<< (cardInfo->smartcard().readerHasPinPad() ? "yes" : "no") << '\n';
4545

46-
switch (cardInfo->eid().authSignatureAlgorithm()) {
46+
switch (cardInfo->authSignatureAlgorithm()) {
4747
case JsonWebSignatureAlgorithm::ES384:
4848
case JsonWebSignatureAlgorithm::RS256:
4949
case JsonWebSignatureAlgorithm::PS256:
@@ -55,7 +55,7 @@ TEST(electronic_id_test, authenticate)
5555
"currently supported");
5656
}
5757

58-
GTEST_ASSERT_GE(cardInfo->eid().authPinRetriesLeft().first, 0U);
58+
GTEST_ASSERT_GE(cardInfo->authPinRetriesLeft().first, 0U);
5959

6060
byte_vector pin {'1', '2', '3', '4'};
6161
pin.reserve(64);
@@ -64,9 +64,9 @@ TEST(electronic_id_test, authenticate)
6464
<< std::string_view(reinterpret_cast<const char*>(pin.data()), pin.size()) << '\n';
6565

6666
const byte_vector dataToSign {'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!'};
67-
const JsonWebSignatureAlgorithm hashAlgo = cardInfo->eid().authSignatureAlgorithm();
67+
const JsonWebSignatureAlgorithm hashAlgo = cardInfo->authSignatureAlgorithm();
6868
const byte_vector hash = calculateDigest(hashAlgo.hashAlgorithm(), dataToSign);
69-
auto signature = cardInfo->eid().signWithAuthKey(std::move(pin), hash);
69+
auto signature = cardInfo->signWithAuthKey(std::move(pin), hash);
7070

7171
std::cout << "Authentication signature: " << signature << '\n';
7272

tests/integration/test-get-certificate.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ TEST(electronic_id_test, getCertificate)
3636

3737
EXPECT_TRUE(cardInfo);
3838

39-
std::cout << "Selected card: " << cardInfo->eid().name() << '\n';
39+
std::cout << "Selected card: " << cardInfo->name() << '\n';
4040

41-
auto certificate = cardInfo->eid().getCertificate(CertificateType::AUTHENTICATION);
41+
auto certificate = cardInfo->getCertificate(CertificateType::AUTHENTICATION);
4242

4343
std::cout << "Authentication certificate: " << certificate << '\n';
4444

45-
certificate = cardInfo->eid().getCertificate(CertificateType::SIGNING);
45+
certificate = cardInfo->getCertificate(CertificateType::SIGNING);
4646

4747
std::cout << "Signing certificate: " << certificate << '\n';
4848
}

tests/integration/test-signing.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,24 @@ static void signing(HashAlgorithm hashAlgo)
3737

3838
EXPECT_TRUE(cardInfo);
3939

40-
std::cout << "Selected card: " << cardInfo->eid().name() << '\n';
40+
std::cout << "Selected card: " << cardInfo->name() << '\n';
4141

42-
if (!cardInfo->eid().isSupportedSigningHashAlgorithm(hashAlgo)) {
42+
if (!cardInfo->isSupportedSigningHashAlgorithm(hashAlgo)) {
4343
std::string skip = "Card does not support hashing algorithm: " + std::string(hashAlgo);
4444
GTEST_SUCCESS_(skip.c_str());
4545
return;
4646
}
4747

48-
byte_vector cert = cardInfo->eid().getCertificate(CertificateType::SIGNING);
48+
byte_vector cert = cardInfo->getCertificate(CertificateType::SIGNING);
4949

50-
GTEST_ASSERT_GE(cardInfo->eid().signingPinRetriesLeft().first, 0U);
50+
GTEST_ASSERT_GE(cardInfo->signingPinRetriesLeft().first, 0U);
5151

5252
byte_vector pin;
53-
if (cardInfo->eid().name() == "EstEID IDEMIA v1")
53+
if (cardInfo->name() == "EstEID IDEMIA v1")
5454
pin = {'1', '2', '3', '4', '5'}; // EstIDEMIA test card default PIN2
55-
else if (cardInfo->eid().name() == "LatEID IDEMIA v1"
56-
|| cardInfo->eid().name() == "LatEID IDEMIA v2")
55+
else if (cardInfo->name() == "LatEID IDEMIA v1" || cardInfo->name() == "LatEID IDEMIA v2")
5756
pin = {'1', '2', '3', '4', '5', '6'}; // LatIDEMIA test card default PIN2
58-
else if (cardInfo->eid().name() == "FinEID v3" || cardInfo->eid().name() == "FinEID v4")
57+
else if (cardInfo->name() == "FinEID v3" || cardInfo->name() == "FinEID v4")
5958
pin = {'1', '2', '3', '4', '5', '6'}; // FinEID custom PIN
6059
else
6160
throw std::runtime_error("TEST signing: Unknown card");
@@ -66,7 +65,7 @@ static void signing(HashAlgorithm hashAlgo)
6665
const byte_vector dataToSign {'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!'};
6766
const byte_vector hash = calculateDigest(hashAlgo, dataToSign);
6867

69-
auto signature = cardInfo->eid().signWithSigningKey(std::move(pin), hash, hashAlgo);
68+
auto signature = cardInfo->signWithSigningKey(std::move(pin), hash, hashAlgo);
7069

7170
std::cout << "Signing signature: " << signature.first << '\n';
7271

0 commit comments

Comments
 (0)