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

Upgrade to C++20 #81

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
58 changes: 28 additions & 30 deletions include/electronic-id/enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace electronic_id
class CertificateType
{
public:
enum CertificateTypeEnum { AUTHENTICATION, SIGNING, NONE = -1 };
enum CertificateTypeEnum: int8_t { AUTHENTICATION, SIGNING, NONE = -1 };

CertificateType() = default;
constexpr CertificateType(const CertificateTypeEnum _value) : value(_value) {}
Expand All @@ -53,7 +53,7 @@ class CertificateType
class HashAlgorithm
{
public:
enum HashAlgorithmEnum {
enum HashAlgorithmEnum: int16_t {
SHA224 = 224, // SHA2
SHA256 = 256,
SHA384 = 384,
Expand Down Expand Up @@ -93,8 +93,6 @@ class HashAlgorithm
return value >= HashAlgorithm::SHA3_224 && value <= HashAlgorithm::SHA3_512;
}

constexpr bool isSHA2orSHA3() const { return isSHA2() || isSHA3(); }

static std::string allSupportedAlgorithmNames();
static pcsc_cpp::byte_vector rsaOID(const HashAlgorithmEnum hash);

Expand All @@ -109,40 +107,40 @@ class SignatureAlgorithm
enum SignatureAlgorithmEnum {
// ECDSA
ES = 1 << 13,
ES224 = ES | HashAlgorithm::SHA224,
ES256 = ES | HashAlgorithm::SHA256,
ES384 = ES | HashAlgorithm::SHA384,
ES512 = ES | HashAlgorithm::SHA512,
ES3_224 = ES | HashAlgorithm::SHA3_224,
ES3_256 = ES | HashAlgorithm::SHA3_256,
ES3_384 = ES | HashAlgorithm::SHA3_384,
ES3_512 = ES | HashAlgorithm::SHA3_512,
ES224 = ES | int16_t(HashAlgorithm::SHA224),
ES256 = ES | int16_t(HashAlgorithm::SHA256),
ES384 = ES | int16_t(HashAlgorithm::SHA384),
ES512 = ES | int16_t(HashAlgorithm::SHA512),
ES3_224 = ES | int16_t(HashAlgorithm::SHA3_224),
ES3_256 = ES | int16_t(HashAlgorithm::SHA3_256),
ES3_384 = ES | int16_t(HashAlgorithm::SHA3_384),
ES3_512 = ES | int16_t(HashAlgorithm::SHA3_512),
// RSASSA-PSS
PS = 1 << 14,
PS224 = PS | HashAlgorithm::SHA224,
PS256 = PS | HashAlgorithm::SHA256,
PS384 = PS | HashAlgorithm::SHA384,
PS512 = PS | HashAlgorithm::SHA512,
PS3_224 = PS | HashAlgorithm::SHA3_224,
PS3_256 = PS | HashAlgorithm::SHA3_256,
PS3_384 = PS | HashAlgorithm::SHA3_384,
PS3_512 = PS | HashAlgorithm::SHA3_512,
PS224 = PS | int16_t(HashAlgorithm::SHA224),
PS256 = PS | int16_t(HashAlgorithm::SHA256),
PS384 = PS | int16_t(HashAlgorithm::SHA384),
PS512 = PS | int16_t(HashAlgorithm::SHA512),
PS3_224 = PS | int16_t(HashAlgorithm::SHA3_224),
PS3_256 = PS | int16_t(HashAlgorithm::SHA3_256),
PS3_384 = PS | int16_t(HashAlgorithm::SHA3_384),
PS3_512 = PS | int16_t(HashAlgorithm::SHA3_512),
// RSASSA-PKCS1-v1_5
RS = 1 << 15,
RS224 = RS | HashAlgorithm::SHA224,
RS256 = RS | HashAlgorithm::SHA256,
RS384 = RS | HashAlgorithm::SHA384,
RS512 = RS | HashAlgorithm::SHA512,
RS3_224 = RS | HashAlgorithm::SHA3_224,
RS3_256 = RS | HashAlgorithm::SHA3_256,
RS3_384 = RS | HashAlgorithm::SHA3_384,
RS3_512 = RS | HashAlgorithm::SHA3_512,
RS224 = RS | int16_t(HashAlgorithm::SHA224),
RS256 = RS | int16_t(HashAlgorithm::SHA256),
RS384 = RS | int16_t(HashAlgorithm::SHA384),
RS512 = RS | int16_t(HashAlgorithm::SHA512),
RS3_224 = RS | int16_t(HashAlgorithm::SHA3_224),
RS3_256 = RS | int16_t(HashAlgorithm::SHA3_256),
RS3_384 = RS | int16_t(HashAlgorithm::SHA3_384),
RS3_512 = RS | int16_t(HashAlgorithm::SHA3_512),
NONE = -1
};

constexpr SignatureAlgorithm(const SignatureAlgorithmEnum _value) : value(_value) {}
constexpr SignatureAlgorithm(const SignatureAlgorithmEnum key, const HashAlgorithm hash) :
value(SignatureAlgorithmEnum(key | hash))
value(SignatureAlgorithmEnum(key | int16_t(hash)))
{
}

Expand Down Expand Up @@ -174,7 +172,7 @@ class SignatureAlgorithm
class JsonWebSignatureAlgorithm
{
public:
enum JsonWebSignatureAlgorithmEnum {
enum JsonWebSignatureAlgorithmEnum: int8_t {
ES256, // ECDSA
ES384,
ES512,
Expand Down
2 changes: 1 addition & 1 deletion lib/libpcsc-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ target_include_directories(${PROJECT_NAME}

target_compile_features(${PROJECT_NAME}
PUBLIC
cxx_std_17
cxx_std_20
)

target_compile_options(${PROJECT_NAME} PUBLIC
Expand Down
14 changes: 7 additions & 7 deletions lib/libpcsc-cpp/src/SmartCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ std::pair<SCARDHANDLE, DWORD> connectToCard(const SCARDCONTEXT ctx, const string
SCard(Connect, ctx, readerName.c_str(), DWORD(SCARD_SHARE_SHARED), requestedProtocol,
&cardHandle, &protocolOut);

return std::pair<SCARDHANDLE, DWORD> {cardHandle, protocolOut};
return {cardHandle, protocolOut};
}

template <class K, class V = uint32_t, class D, size_t dsize, typename Func>
Expand Down Expand Up @@ -97,7 +97,7 @@ class CardImpl
{
public:
explicit CardImpl(std::pair<SCARDHANDLE, DWORD> cardParams) :
cardHandle(cardParams.first), _protocol({cardParams.second, sizeof(SCARD_IO_REQUEST)})
cardHandle(cardParams.first), _protocol {cardParams.second, sizeof(SCARD_IO_REQUEST)}
{
// TODO: debug("Protocol: " + to_string(protocol()))
try {
Expand Down Expand Up @@ -145,8 +145,8 @@ class CardImpl
return false;
if (getenv("SMARTCARDPP_NOPINPAD"))
return false;
return features.find(FEATURE_VERIFY_PIN_START) != features.cend()
|| features.find(FEATURE_VERIFY_PIN_DIRECT) != features.cend();
return features.contains(FEATURE_VERIFY_PIN_START)
|| features.contains(FEATURE_VERIFY_PIN_DIRECT);
}

ResponseApdu transmitBytes(const byte_vector& commandBytes) const
Expand Down Expand Up @@ -189,9 +189,9 @@ class CardImpl
data->ulDataLength = uint32_t(commandBytes.size());
cmd.insert(cmd.cend(), commandBytes.cbegin(), commandBytes.cend());

DWORD ioctl = features.at(features.find(FEATURE_VERIFY_PIN_START) != features.cend()
? FEATURE_VERIFY_PIN_START
: FEATURE_VERIFY_PIN_DIRECT);
DWORD ioctl =
features.at(features.contains(FEATURE_VERIFY_PIN_START) ? FEATURE_VERIFY_PIN_START
: FEATURE_VERIFY_PIN_DIRECT);
byte_vector responseBytes(ResponseApdu::MAX_SIZE, 0);
auto responseLength = DWORD(responseBytes.size());
SCard(Control, cardHandle, ioctl, cmd.data(), DWORD(cmd.size()),
Expand Down
2 changes: 1 addition & 1 deletion lib/libpcsc-cpp/tests/lib/libpcsc-mock/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ target_include_directories(${PROJECT_NAME}

target_compile_features(${PROJECT_NAME}
PUBLIC
cxx_std_17
cxx_std_20
)

# PC/SC API dependencies.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <map>
#include <vector>
#include <stdexcept>
#include <stdint.h>
#include <cstdint>

#ifdef _WIN32
using MOCK_LONG = int32_t;
Expand All @@ -44,9 +44,9 @@ class PcscMockError : public std::runtime_error
class PcscMock
{
public:
typedef std::vector<unsigned char> byte_vector;
using byte_vector = std::vector<unsigned char>;
// An APDU script is a list of request-response APDU pairs.
typedef std::vector<std::pair<byte_vector, byte_vector>> ApduScript;
using ApduScript = std::vector<std::pair<byte_vector, byte_vector>>;
// Define local string type so that we can use wstring in Windows if needed.
#ifdef _WIN32
using string_t = std::wstring;
Expand All @@ -61,7 +61,7 @@ class PcscMock

static bool wasScardFunctionCalled(const std::string& scardFunction)
{
return instance()._recordedCalls.find(scardFunction) != instance()._recordedCalls.end();
return instance()._recordedCalls.contains(scardFunction);
}

static void addReturnValueForScardFunctionCall(const std::string& scardFunctionName,
Expand Down
4 changes: 2 additions & 2 deletions src/electronic-id.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ namespace electronic_id

bool isCardSupported(const pcsc_cpp::byte_vector& atr)
{
return SUPPORTED_ATRS.count(atr);
return SUPPORTED_ATRS.contains(atr);
}

ElectronicID::ptr getElectronicID(const pcsc_cpp::Reader& reader)
Expand Down Expand Up @@ -177,7 +177,7 @@ VerifyPinFailed::VerifyPinFailed(const Status s, const observer_ptr<pcsc_cpp::Re

HashAlgorithm::HashAlgorithm(const std::string& algoName)
{
if (!SUPPORTED_ALGORITHMS.count(algoName)) {
if (!SUPPORTED_ALGORITHMS.contains(algoName)) {
THROW(ArgumentFatalError,
"Hash algorithm is not valid, supported algorithms are "
+ allSupportedAlgorithmNames());
Expand Down
Loading