From 4acb07d16df40945e1670f2da40d49aeb65190ee Mon Sep 17 00:00:00 2001 From: Raul Metsma Date: Mon, 5 Aug 2024 23:07:49 +0300 Subject: [PATCH] Take PIN ovnership to minimze memory copy-s followup WE2-479 Signed-off-by: Raul Metsma --- lib/libelectronic-id | 2 +- src/controller/command-handlers/authenticate.cpp | 15 ++++----------- src/controller/command-handlers/sign.cpp | 15 ++++----------- 3 files changed, 9 insertions(+), 23 deletions(-) diff --git a/lib/libelectronic-id b/lib/libelectronic-id index 7991e0ec..14493a48 160000 --- a/lib/libelectronic-id +++ b/lib/libelectronic-id @@ -1 +1 @@ -Subproject commit 7991e0ecc5839a12d076166f663de5f3c7dcb8c0 +Subproject commit 14493a48c1ae8786df2b5de2fad6bf616dfce319 diff --git a/src/controller/command-handlers/authenticate.cpp b/src/controller/command-handlers/authenticate.cpp index a5697386..52b1261b 100644 --- a/src/controller/command-handlers/authenticate.cpp +++ b/src/controller/command-handlers/authenticate.cpp @@ -56,7 +56,7 @@ QVariantMap createAuthenticationToken(const QString& signatureAlgorithm, } QByteArray createSignature(const QString& origin, const QString& challengeNonce, - const ElectronicID& eid, const pcsc_cpp::byte_vector& pin) + const ElectronicID& eid, pcsc_cpp::byte_vector&& pin) { static const std::map SIGNATURE_ALGO_TO_HASH { @@ -85,7 +85,7 @@ QByteArray createSignature(const QString& origin, const QString& challengeNonce, const pcsc_cpp::byte_vector hashToBeSigned {hashToBeSignedQBytearray.cbegin(), hashToBeSignedQBytearray.cend()}; - const auto signature = eid.signWithAuthKey(pin, hashToBeSigned); + const auto signature = eid.signWithAuthKey(std::move(pin), hashToBeSigned); return QByteArray::fromRawData(reinterpret_cast(signature.data()), int(signature.size())) @@ -122,17 +122,10 @@ QVariantMap Authenticate::onConfirm(WebEidUI* window, const auto signatureAlgorithm = QString::fromStdString(cardCertAndPin.cardInfo->eid().authSignatureAlgorithm()); - auto pin = getPin(cardCertAndPin.cardInfo->eid(), window); - try { + auto pin = getPin(cardCertAndPin.cardInfo->eid(), window); const auto signature = - createSignature(origin.url(), challengeNonce, cardCertAndPin.cardInfo->eid(), pin); - - // Erase the PIN memory. - // TODO: Use a scope guard. Verify that the buffers are actually zeroed and no copies - // remain. - std::fill(pin.begin(), pin.end(), '\0'); - + createSignature(origin.url(), challengeNonce, cardCertAndPin.cardInfo->eid(), std::move(pin)); return createAuthenticationToken(signatureAlgorithm, cardCertAndPin.certificateBytesInDer, signature); diff --git a/src/controller/command-handlers/sign.cpp b/src/controller/command-handlers/sign.cpp index d2b7bc45..94f123ef 100644 --- a/src/controller/command-handlers/sign.cpp +++ b/src/controller/command-handlers/sign.cpp @@ -30,11 +30,11 @@ using namespace electronic_id; namespace { -QPair signHash(const ElectronicID& eid, const pcsc_cpp::byte_vector& pin, +QPair signHash(const ElectronicID& eid, pcsc_cpp::byte_vector&& pin, const QByteArray& docHash, const HashAlgorithm hashAlgo) { const auto hashBytes = pcsc_cpp::byte_vector {docHash.begin(), docHash.end()}; - const auto signature = eid.signWithSigningKey(pin, hashBytes, hashAlgo); + const auto signature = eid.signWithSigningKey(std::move(pin), hashBytes, hashAlgo); const auto signatureBase64 = QByteArray::fromRawData(reinterpret_cast(signature.first.data()), @@ -95,16 +95,9 @@ void Sign::emitCertificatesReady(const std::vector& c QVariantMap Sign::onConfirm(WebEidUI* window, const CardCertificateAndPinInfo& cardCertAndPin) { - auto pin = getPin(cardCertAndPin.cardInfo->eid(), window); - try { - const auto signature = signHash(cardCertAndPin.cardInfo->eid(), pin, docHash, hashAlgo); - - // Erase PIN memory. - // TODO: Use a scope guard. Verify that the buffers are actually zeroed - // and no copies remain. - std::fill(pin.begin(), pin.end(), '\0'); - + auto pin = getPin(cardCertAndPin.cardInfo->eid(), window); + const auto signature = signHash(cardCertAndPin.cardInfo->eid(), std::move(pin), docHash, hashAlgo); return {{QStringLiteral("signature"), signature.first}, {QStringLiteral("signatureAlgorithm"), signature.second}};