Skip to content

Commit 1c6a8fc

Browse files
authored
Take PIN ownership to minimze memory copy-s (#339)
followup WE2-479 Signed-off-by: Raul Metsma <[email protected]>
1 parent b8d6e7f commit 1c6a8fc

File tree

3 files changed

+16
-27
lines changed

3 files changed

+16
-27
lines changed

src/controller/command-handlers/authenticate.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ QVariantMap createAuthenticationToken(const QString& signatureAlgorithm,
5757
}
5858

5959
QByteArray createSignature(const QString& origin, const QString& challengeNonce,
60-
const ElectronicID& eid, const pcsc_cpp::byte_vector& pin)
60+
const ElectronicID& eid, pcsc_cpp::byte_vector&& pin)
6161
{
6262
static const std::map<JsonWebSignatureAlgorithm, QCryptographicHash::Algorithm>
6363
SIGNATURE_ALGO_TO_HASH {
@@ -86,7 +86,7 @@ QByteArray createSignature(const QString& origin, const QString& challengeNonce,
8686
const pcsc_cpp::byte_vector hashToBeSigned {hashToBeSignedQBytearray.cbegin(),
8787
hashToBeSignedQBytearray.cend()};
8888

89-
const auto signature = eid.signWithAuthKey(pin, hashToBeSigned);
89+
const auto signature = eid.signWithAuthKey(std::move(pin), hashToBeSigned);
9090

9191
return QByteArray::fromRawData(reinterpret_cast<const char*>(signature.data()),
9292
int(signature.size()))
@@ -120,20 +120,14 @@ Authenticate::Authenticate(const CommandWithArguments& cmd) : CertificateReader(
120120
QVariantMap Authenticate::onConfirm(WebEidUI* window,
121121
const CardCertificateAndPinInfo& cardCertAndPin)
122122
{
123-
const auto signatureAlgorithm =
124-
QString::fromStdString(cardCertAndPin.cardInfo->eid().authSignatureAlgorithm());
125-
126-
pcsc_cpp::byte_vector pin;
127-
getPin(pin, cardCertAndPin.cardInfo->eid(), window);
128-
auto pin_cleanup = qScopeGuard([&pin] {
129-
// Erase PIN memory.
130-
std::fill(pin.begin(), pin.end(), '\0');
131-
});
132-
133123
try {
124+
const auto signatureAlgorithm =
125+
QString::fromStdString(cardCertAndPin.cardInfo->eid().authSignatureAlgorithm());
126+
pcsc_cpp::byte_vector pin;
127+
pin.reserve(5 + 16); // Avoid realloc: apdu + pin padding
128+
getPin(pin, cardCertAndPin.cardInfo->eid(), window);
134129
const auto signature =
135-
createSignature(origin.url(), challengeNonce, cardCertAndPin.cardInfo->eid(), pin);
136-
130+
createSignature(origin.url(), challengeNonce, cardCertAndPin.cardInfo->eid(), std::move(pin));
137131
return createAuthenticationToken(signatureAlgorithm, cardCertAndPin.certificateBytesInDer,
138132
signature);
139133

src/controller/command-handlers/sign.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ using namespace electronic_id;
3232
namespace
3333
{
3434

35-
QPair<QString, QVariantMap> signHash(const ElectronicID& eid, const pcsc_cpp::byte_vector& pin,
35+
QPair<QString, QVariantMap> signHash(const ElectronicID& eid, pcsc_cpp::byte_vector&& pin,
3636
const QByteArray& docHash, const HashAlgorithm hashAlgo)
3737
{
3838
const auto hashBytes = pcsc_cpp::byte_vector {docHash.begin(), docHash.end()};
39-
const auto signature = eid.signWithSigningKey(pin, hashBytes, hashAlgo);
39+
const auto signature = eid.signWithSigningKey(std::move(pin), hashBytes, hashAlgo);
4040

4141
const auto signatureBase64 =
4242
QByteArray::fromRawData(reinterpret_cast<const char*>(signature.first.data()),
@@ -97,16 +97,11 @@ void Sign::emitCertificatesReady(const std::vector<CardCertificateAndPinInfo>& c
9797

9898
QVariantMap Sign::onConfirm(WebEidUI* window, const CardCertificateAndPinInfo& cardCertAndPin)
9999
{
100-
pcsc_cpp::byte_vector pin;
101-
getPin(pin, cardCertAndPin.cardInfo->eid(), window);
102-
auto pin_cleanup = qScopeGuard([&pin] {
103-
// Erase PIN memory.
104-
std::fill(pin.begin(), pin.end(), '\0');
105-
});
106-
107100
try {
108-
const auto signature = signHash(cardCertAndPin.cardInfo->eid(), pin, docHash, hashAlgo);
109-
101+
pcsc_cpp::byte_vector pin;
102+
pin.reserve(5 + 16); // Avoid realloc: apdu + pin padding
103+
getPin(pin, cardCertAndPin.cardInfo->eid(), window);
104+
const auto signature = signHash(cardCertAndPin.cardInfo->eid(), std::move(pin), docHash, hashAlgo);
110105
return {{QStringLiteral("signature"), signature.first},
111106
{QStringLiteral("signatureAlgorithm"), signature.second}};
112107

@@ -144,7 +139,7 @@ void Sign::validateAndStoreDocHashAndHashAlgo(const QVariantMap& args)
144139
docHash =
145140
QByteArray::fromBase64(validateAndGetArgument<QByteArray>(QStringLiteral("hash"), args));
146141

147-
QString hashAlgoInput = validateAndGetArgument<QString>(QStringLiteral("hashFunction"), args);
142+
auto hashAlgoInput = validateAndGetArgument<QString>(QStringLiteral("hashFunction"), args);
148143
if (hashAlgoInput.size() > 8) {
149144
THROW(CommandHandlerInputDataError, "hashFunction value is invalid");
150145
}

0 commit comments

Comments
 (0)