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

Select revert back to main AWP #34

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
26 changes: 26 additions & 0 deletions src/electronic-ids/pcsc/EIDIDEMIA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,31 @@ namespace
const byte_vector::value_type PIN_PADDING_CHAR = 0xFF;
const byte_vector::value_type AUTH_PIN_REFERENCE = 0x01;

template <typename F>
struct ScopeExit
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have a scope guard implementation here: https://github.com/web-eid/libelectronic-id/blob/main/src/electronic-ids/pkcs11/PKCS11CardManager.hpp#L52-L54. Can we find a DRY solution that can be used in both places?

{
explicit ScopeExit(F f) : f(std::move(f)) {}
ScopeExit(const ScopeExit&) = delete;
ScopeExit(ScopeExit&&) = delete;
~ScopeExit() { f(); }
ScopeExit& operator=(const ScopeExit&) = delete;
ScopeExit& operator=(ScopeExit&&) = delete;
F f;
};

} // namespace

namespace electronic_id
{

byte_vector EIDIDEMIA::getCertificateImpl(const CertificateType type) const
{
ScopeExit f([=] {
// Revert to AWP application.
if (!type.isAuthentication()) {
transmitApduWithExpectedResponse(*card, selectApplicationID().AUTH_AID);
}
});
const std::vector<byte_vector> SELECT_AID_AND_CERT_FILE = {
selectApplicationID().MAIN_AID,
type.isAuthentication() ? selectApplicationID().AUTH_AID : selectApplicationID().SIGN_AID,
Expand Down Expand Up @@ -74,6 +92,10 @@ ElectronicID::Signature EIDIDEMIA::signWithSigningKeyImpl(const byte_vector& pin
const byte_vector& hash,
const HashAlgorithm hashAlgo) const
{
ScopeExit f([=] {
// Revert to AWP application.
transmitApduWithExpectedResponse(*card, selectApplicationID().AUTH_AID);
});
// Select signing application and signing security environment.
transmitApduWithExpectedResponse(*card, selectApplicationID().SIGN_AID);
transmitApduWithExpectedResponse(*card, selectSecurityEnv().SIGN_ENV);
Expand All @@ -89,6 +111,10 @@ ElectronicID::Signature EIDIDEMIA::signWithSigningKeyImpl(const byte_vector& pin

ElectronicID::PinRetriesRemainingAndMax EIDIDEMIA::signingPinRetriesLeftImpl() const
{
ScopeExit f([=] {
// Revert to AWP application.
transmitApduWithExpectedResponse(*card, selectApplicationID().AUTH_AID);
});
transmitApduWithExpectedResponse(*card, selectApplicationID().SIGN_AID);
return pinRetriesLeft(signingPinReference());
}
Expand Down