Skip to content

Commit

Permalink
Use byte_type
Browse files Browse the repository at this point in the history
WE2-751

Signed-off-by: Mart Somermaa <[email protected]>
  • Loading branch information
mrts committed Jun 26, 2023
1 parent 43a6e94 commit 6aba432
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 62 deletions.
11 changes: 6 additions & 5 deletions include/electronic-id/electronic-id.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ class ElectronicID
using ptr = std::shared_ptr<ElectronicID>;
using PinMinMaxLength = std::pair<size_t, size_t>;
using PinRetriesRemainingAndMax = std::pair<uint8_t, int8_t>;
using Signature = std::pair<pcsc_cpp::byte_vector, SignatureAlgorithm>;
using byte_vector = pcsc_cpp::byte_vector;
using byte_type = pcsc_cpp::byte_type;
using Signature = std::pair<byte_vector, SignatureAlgorithm>;

enum Type {
EstEID,
Expand Down Expand Up @@ -67,8 +69,8 @@ class ElectronicID

virtual PinRetriesRemainingAndMax authPinRetriesLeft() const = 0;

virtual pcsc_cpp::byte_vector signWithAuthKey(const pcsc_cpp::byte_vector& pin,
const pcsc_cpp::byte_vector& hash) const = 0;
virtual pcsc_cpp::byte_vector signWithAuthKey(const byte_vector& pin,
const byte_vector& hash) const = 0;

// Functions related to signing.
virtual const std::set<SignatureAlgorithm>& supportedSigningAlgorithms() const = 0;
Expand All @@ -79,8 +81,7 @@ class ElectronicID

virtual PinRetriesRemainingAndMax signingPinRetriesLeft() const = 0;

virtual Signature signWithSigningKey(const pcsc_cpp::byte_vector& pin,
const pcsc_cpp::byte_vector& hash,
virtual Signature signWithSigningKey(const byte_vector& pin, const byte_vector& hash,
const HashAlgorithm hashAlgo) const = 0;

// General functions.
Expand Down
2 changes: 1 addition & 1 deletion lib/libpcsc-cpp
22 changes: 10 additions & 12 deletions src/electronic-ids/pcsc/EIDIDEMIA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ using namespace electronic_id;
namespace
{

const byte_vector::value_type PIN_PADDING_CHAR = 0xFF;
const byte_vector::value_type AUTH_PIN_REFERENCE = 0x01;
const byte_type PIN_PADDING_CHAR = 0xFF;
const byte_type AUTH_PIN_REFERENCE = 0x01;

} // namespace

Expand Down Expand Up @@ -126,17 +126,15 @@ const SelectCertificateCmds& EIDIDEMIA::selectCertificate() const
return isUpdated() ? selectCert2Cmds : selectCert1Cmds;
}

ElectronicID::PinRetriesRemainingAndMax
EIDIDEMIA::pinRetriesLeft(byte_vector::value_type pinReference) const
ElectronicID::PinRetriesRemainingAndMax EIDIDEMIA::pinRetriesLeft(byte_type pinReference) const
{
const pcsc_cpp::CommandApdu GET_DATA_ODD {0x00,
0xCB,
0x3F,
0xFF,
{0x4D, 0x08, 0x70, 0x06, 0xBF, 0x81,
byte_vector::value_type(pinReference & 0x0F), 0x02,
0xA0, 0x80},
0x00};
const pcsc_cpp::CommandApdu GET_DATA_ODD {
0x00,
0xCB,
0x3F,
0xFF,
{0x4D, 0x08, 0x70, 0x06, 0xBF, 0x81, byte_type(pinReference & 0x0F), 0x02, 0xA0, 0x80},
0x00};
const auto response = card->transmit(GET_DATA_ODD);
if (!response.isOK()) {
THROW(SmartCardError,
Expand Down
12 changes: 5 additions & 7 deletions src/electronic-ids/pcsc/EIDIDEMIA.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,23 @@ class EIDIDEMIA : public PcscElectronicID
explicit EIDIDEMIA(pcsc_cpp::SmartCard::ptr _card) : PcscElectronicID(std::move(_card)) {}

protected:
pcsc_cpp::byte_vector getCertificateImpl(const CertificateType type) const override;
byte_vector getCertificateImpl(const CertificateType type) const override;

PinRetriesRemainingAndMax authPinRetriesLeftImpl() const override;
pcsc_cpp::byte_vector signWithAuthKeyImpl(const pcsc_cpp::byte_vector& pin,
const pcsc_cpp::byte_vector& hash) const override;
byte_vector signWithAuthKeyImpl(const byte_vector& pin, const byte_vector& hash) const override;

PinRetriesRemainingAndMax signingPinRetriesLeftImpl() const override;
Signature signWithSigningKeyImpl(const pcsc_cpp::byte_vector& pin,
const pcsc_cpp::byte_vector& hash,
Signature signWithSigningKeyImpl(const byte_vector& pin, const byte_vector& hash,
const HashAlgorithm hashAlgo) const override;

virtual const SelectApplicationIDCmds& selectApplicationID() const;
virtual const SelectCertificateCmds& selectCertificate() const;
virtual const ManageSecurityEnvCmds& selectSecurityEnv() const = 0;

virtual size_t pinBlockLength() const { return authPinMinMaxLength().second; }
virtual pcsc_cpp::byte_vector::value_type signingPinReference() const { return 0x85; }
virtual byte_type signingPinReference() const { return 0x85; }
virtual SignatureAlgorithm signingSignatureAlgorithm() const = 0;
PinRetriesRemainingAndMax pinRetriesLeft(pcsc_cpp::byte_vector::value_type pinReference) const;
PinRetriesRemainingAndMax pinRetriesLeft(byte_type pinReference) const;

virtual bool useInternalAuthenticateAndRSAWithPKCS1PaddingDuringSigning() const
{
Expand Down
6 changes: 3 additions & 3 deletions src/electronic-ids/pcsc/EstEIDGemalto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ const std::vector<byte_vector> SELECT_EE_DIR_AND_SIGN_CERT_FILE {
{0x00, 0xa4, 0x02, 0x0c, 0x02, 0xdd, 0xce},
};

const byte_vector::value_type AUTH_PIN_REFERENCE = 0x01;
const byte_vector::value_type SIGNING_PIN_REFERENCE = 0x02;
const byte_type AUTH_PIN_REFERENCE = 0x01;
const byte_type SIGNING_PIN_REFERENCE = 0x02;

} // namespace

Expand Down Expand Up @@ -97,7 +97,7 @@ ElectronicID::PinRetriesRemainingAndMax EstEIDGemaltoV3_5_8::signingPinRetriesLe
}

ElectronicID::PinRetriesRemainingAndMax
EstEIDGemaltoV3_5_8::pinRetriesLeft(byte_vector::value_type pinReference) const
EstEIDGemaltoV3_5_8::pinRetriesLeft(byte_type pinReference) const
{
static const CommandApdu PINRETRY {0x00, 0xA4, 0x02, 0x0C, {0x00, 0x16}};
const CommandApdu READRECORD {0x00, 0xB2, pinReference, 0x04};
Expand Down
10 changes: 4 additions & 6 deletions src/electronic-ids/pcsc/EstEIDGemalto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class EstEIDGemaltoV3_5_8 : public PcscElectronicID
EstEIDGemaltoV3_5_8(pcsc_cpp::SmartCard::ptr _card) : PcscElectronicID(std::move(_card)) {}

private:
pcsc_cpp::byte_vector getCertificateImpl(const CertificateType type) const override;
byte_vector getCertificateImpl(const CertificateType type) const override;

JsonWebSignatureAlgorithm authSignatureAlgorithm() const override
{
Expand All @@ -49,14 +49,12 @@ class EstEIDGemaltoV3_5_8 : public PcscElectronicID
std::string name() const override { return "EstEID Gemalto v3.5.8"; }
Type type() const override { return EstEID; }

pcsc_cpp::byte_vector signWithAuthKeyImpl(const pcsc_cpp::byte_vector& pin,
const pcsc_cpp::byte_vector& hash) const override;
byte_vector signWithAuthKeyImpl(const byte_vector& pin, const byte_vector& hash) const override;

Signature signWithSigningKeyImpl(const pcsc_cpp::byte_vector& pin,
const pcsc_cpp::byte_vector& hash,
Signature signWithSigningKeyImpl(const byte_vector& pin, const byte_vector& hash,
const HashAlgorithm hashAlgo) const override;

PinRetriesRemainingAndMax pinRetriesLeft(pcsc_cpp::byte_vector::value_type pinReference) const;
PinRetriesRemainingAndMax pinRetriesLeft(byte_type pinReference) const;
};

} // namespace electronic_id
26 changes: 13 additions & 13 deletions src/electronic-ids/pcsc/FinEID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ const byte_vector SELECT_AUTH_CERT_FILE {0x00, 0xA4, 0x08, 0x0C, 0x02, 0x43, 0x3
const byte_vector SELECT_SIGN_CERT_FILE_V3 {0x00, 0xA4, 0x08, 0x0C, 0x04, 0x50, 0x16, 0x43, 0x35};
const byte_vector SELECT_SIGN_CERT_FILE_V4 {0x00, 0xA4, 0x08, 0x0C, 0x04, 0x50, 0x16, 0x43, 0x32};

constexpr byte_vector::value_type PIN_PADDING_CHAR = 0x00;
constexpr byte_vector::value_type AUTH_PIN_REFERENCE = 0x11;
constexpr byte_vector::value_type SIGNING_PIN_REFERENCE = 0x82;
constexpr byte_vector::value_type AUTH_KEY_REFERENCE = 0x01;
constexpr byte_vector::value_type SIGNING_KEY_REFERENCE_V3 = 0x03;
constexpr byte_vector::value_type SIGNING_KEY_REFERENCE_V4 = 0x02;
constexpr byte_vector::value_type ECDSA_ALGO = 0x04;
constexpr byte_vector::value_type RSA_PSS_ALGO = 0x05;
constexpr byte_type PIN_PADDING_CHAR = 0x00;
constexpr byte_type AUTH_PIN_REFERENCE = 0x11;
constexpr byte_type SIGNING_PIN_REFERENCE = 0x82;
constexpr byte_type AUTH_KEY_REFERENCE = 0x01;
constexpr byte_type SIGNING_KEY_REFERENCE_V3 = 0x03;
constexpr byte_type SIGNING_KEY_REFERENCE_V4 = 0x02;
constexpr byte_type ECDSA_ALGO = 0x04;
constexpr byte_type RSA_PSS_ALGO = 0x05;

} // namespace

Expand Down Expand Up @@ -98,9 +98,9 @@ ElectronicID::PinRetriesRemainingAndMax FinEIDv3::signingPinRetriesLeftImpl() co
}

byte_vector FinEIDv3::sign(const HashAlgorithm hashAlgo, const byte_vector& hash,
const byte_vector& pin, value_type pinReference,
PinMinMaxLength pinMinMaxLength, value_type keyReference,
value_type signatureAlgo, value_type LE) const
const byte_vector& pin, byte_type pinReference,
PinMinMaxLength pinMinMaxLength, byte_type keyReference,
byte_type signatureAlgo, byte_type LE) const
{
if (signatureAlgo != ECDSA_ALGO && hashAlgo.isSHA3()) {
THROW(ArgumentFatalError, "No OID for algorithm " + std::string(hashAlgo));
Expand Down Expand Up @@ -134,7 +134,7 @@ byte_vector FinEIDv3::sign(const HashAlgorithm hashAlgo, const byte_vector& hash
// Select security environment for COMPUTE SIGNATURE.
selectComputeSignatureEnv(*card, signatureAlgo, keyReference, name());

byte_vector tlv {0x90, value_type(hash.size())};
byte_vector tlv {0x90, byte_type(hash.size())};
tlv.insert(tlv.cend(), hash.cbegin(), hash.cend());

const CommandApdu computeSignature {{0x00, 0x2A, 0x90, 0xA0}, tlv};
Expand Down Expand Up @@ -166,7 +166,7 @@ byte_vector FinEIDv3::sign(const HashAlgorithm hashAlgo, const byte_vector& hash
return signature.data;
}

ElectronicID::PinRetriesRemainingAndMax FinEIDv3::pinRetriesLeft(value_type pinReference) const
ElectronicID::PinRetriesRemainingAndMax FinEIDv3::pinRetriesLeft(byte_type pinReference) const
{
const pcsc_cpp::CommandApdu GET_DATA {
0x00, 0xCB, 0x00, 0xFF, {0xA0, 0x03, 0x83, 0x01, pinReference}};
Expand Down
9 changes: 3 additions & 6 deletions src/electronic-ids/pcsc/FinEID.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ class FinEIDv3 : public PcscElectronicID
FinEIDv3(pcsc_cpp::SmartCard::ptr _card) : PcscElectronicID(std::move(_card)) {}

protected:
using byte_vector = pcsc_cpp::byte_vector;
using value_type = byte_vector::value_type;

byte_vector getCertificateImpl(const CertificateType type) const override;

JsonWebSignatureAlgorithm authSignatureAlgorithm() const override
Expand All @@ -58,10 +55,10 @@ class FinEIDv3 : public PcscElectronicID
const HashAlgorithm hashAlgo) const override;

byte_vector sign(const HashAlgorithm hashAlgo, const byte_vector& hash, const byte_vector& pin,
value_type pinReference, PinMinMaxLength pinMinMaxLength,
value_type keyReference, value_type signatureAlgo, value_type LE) const;
byte_type pinReference, PinMinMaxLength pinMinMaxLength,
byte_type keyReference, byte_type signatureAlgo, byte_type LE) const;

PinRetriesRemainingAndMax pinRetriesLeft(value_type pinReference) const;
PinRetriesRemainingAndMax pinRetriesLeft(byte_type pinReference) const;
};

class FinEIDv4 : public FinEIDv3
Expand Down
16 changes: 7 additions & 9 deletions src/electronic-ids/pcsc/pcsc-common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ getCertificate(pcsc_cpp::SmartCard& card,
}

inline pcsc_cpp::byte_vector addPaddingToPin(const pcsc_cpp::byte_vector& pin, size_t paddingLength,
pcsc_cpp::byte_vector::value_type paddingChar)
pcsc_cpp::byte_type paddingChar)
{
auto paddedPin = pin;
paddedPin.resize(std::max(pin.size(), paddingLength), paddingChar);
return paddedPin;
}

inline void verifyPin(pcsc_cpp::SmartCard& card, pcsc_cpp::byte_vector::value_type p2,
inline void verifyPin(pcsc_cpp::SmartCard& card, pcsc_cpp::byte_type p2,
const pcsc_cpp::byte_vector& pin, size_t pinMinLength, size_t paddingLength,
pcsc_cpp::byte_vector::value_type paddingChar)
pcsc_cpp::byte_type paddingChar)
{
const pcsc_cpp::CommandApdu VERIFY_PIN {0x00, 0x20, 0x00, p2};
pcsc_cpp::ResponseApdu response;
Expand Down Expand Up @@ -177,15 +177,13 @@ inline pcsc_cpp::byte_vector computeSignature(pcsc_cpp::SmartCard& card,
return response.data;
}

inline void selectComputeSignatureEnv(pcsc_cpp::SmartCard& card,
pcsc_cpp::byte_vector::value_type signatureAlgo,
pcsc_cpp::byte_vector::value_type keyReference,
const std::string& cardType)
inline void selectComputeSignatureEnv(pcsc_cpp::SmartCard& card, pcsc_cpp::byte_type signatureAlgo,
pcsc_cpp::byte_type keyReference, const std::string& cardType)
{
static const pcsc_cpp::CommandApdu SET_COMPUTE_SIGNATURE_ENV {0x00, 0x22, 0x41, 0xB6};

const auto response =
card.transmit({SET_COMPUTE_SIGNATURE_ENV, {0x80, 0x01, signatureAlgo, 0x84, 0x01, keyReference}});
const auto response = card.transmit(
{SET_COMPUTE_SIGNATURE_ENV, {0x80, 0x01, signatureAlgo, 0x84, 0x01, keyReference}});

if (!response.isOK()) {
THROW(SmartCardError,
Expand Down

0 comments on commit 6aba432

Please sign in to comment.