Skip to content

Commit

Permalink
better memory management and better logs for end-to-end encryption
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu Gallien <[email protected]>
  • Loading branch information
mgallien committed Jun 14, 2024
1 parent 20421ca commit 5c7042c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 35 deletions.
24 changes: 2 additions & 22 deletions src/libsync/clientsideencryption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1693,6 +1693,8 @@ void ClientSideEncryption::forgetSensitiveData(const AccountPtr &account)
_usbTokenInformation.setSha256Fingerprint({});
account->setEncryptionCertificateFingerprint({});
_encryptionCertificate.clear();
//_tokenSlots.reset();
_context = Pkcs11Context{Pkcs11Context::State::EmptyContext};
Q_EMIT canDecryptChanged();
Q_EMIT canEncryptChanged();
Q_EMIT userCertificateNeedsMigrationChanged();
Expand Down Expand Up @@ -3104,26 +3106,4 @@ void CertificateInformation::checkEncryptionCertificate()
}
}

Pkcs11Context::Pkcs11Context(State initState)
: _pkcsS11Ctx(initState == State::CreateContext ? PKCS11_CTX_new() : nullptr)
{
}

Pkcs11Context::Pkcs11Context(Pkcs11Context &&otherContext)
: _pkcsS11Ctx(otherContext._pkcsS11Ctx)
{
otherContext._pkcsS11Ctx = nullptr;
}

Pkcs11Context::~Pkcs11Context()
{
qCWarning(lcCse()) << "destructor" << this;
if (_pkcsS11Ctx) {
PKCS11_CTX_free(_pkcsS11Ctx);
_pkcsS11Ctx = nullptr;
} else {
qCWarning(lcCse()) << "destructor" << this << "nullptr";
}
}

}
48 changes: 48 additions & 0 deletions src/libsync/clientsideencryptionprimitives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/

#include "clientsideencryptionprimitives.h"

#include <QLoggingCategory>

#include <openssl/pem.h>

namespace OCC
{

Q_LOGGING_CATEGORY(lcCseUtility, "nextcloud.sync.clientsideencryption.utility", QtInfoMsg)

Bio::operator const BIO *() const
{
return _bio;
Expand Down Expand Up @@ -111,4 +118,45 @@ PKey::operator EVP_PKEY *() const
return _pkey;
}

Pkcs11Context::Pkcs11Context(State initState)
: _pkcsS11Ctx(initState == State::CreateContext ? PKCS11_CTX_new() : nullptr)
{
qCDebug(lcCseUtility()) << "[[Pkcs11Context]] constructor" << this << _pkcsS11Ctx;
}

Pkcs11Context::Pkcs11Context(Pkcs11Context &&otherContext)
: _pkcsS11Ctx(otherContext._pkcsS11Ctx)
{
otherContext._pkcsS11Ctx = nullptr;
qCDebug(lcCseUtility()) << "[[Pkcs11Context]] constructor" << this << _pkcsS11Ctx;
}

Pkcs11Context::~Pkcs11Context()
{
qCDebug(lcCseUtility()) << "[[Pkcs11Context]] destructor" << this << _pkcsS11Ctx;
if (_pkcsS11Ctx) {
qCDebug(lcCseUtility()) << "[[Pkcs11Context]] PKCS11_CTX_free" << _pkcsS11Ctx;
PKCS11_CTX_free(_pkcsS11Ctx);
_pkcsS11Ctx = nullptr;
} else {
qCDebug(lcCseUtility()) << "destructor" << this << "nullptr";
}
}

Pkcs11Context &Pkcs11Context::operator=(Pkcs11Context &&otherContext)
{
qCDebug(lcCseUtility()) << "[[Pkcs11Context]] operator=" << this << _pkcsS11Ctx;
qCDebug(lcCseUtility()) << "[[Pkcs11Context]] operator=" << &otherContext << otherContext._pkcsS11Ctx;
if (&otherContext != this) {
if (_pkcsS11Ctx) {
qCDebug(lcCseUtility()) << "[[Pkcs11Context]] PKCS11_CTX_free" << _pkcsS11Ctx;
PKCS11_CTX_free(_pkcsS11Ctx);
_pkcsS11Ctx = nullptr;
}
std::swap(_pkcsS11Ctx, otherContext._pkcsS11Ctx);
}

return *this;
}

}
13 changes: 1 addition & 12 deletions src/libsync/clientsideencryptionprimitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,7 @@ class Pkcs11Context {

~Pkcs11Context();

Pkcs11Context& operator=(Pkcs11Context &&otherContext)
{
if (&otherContext != this) {
if (_pkcsS11Ctx) {
PKCS11_CTX_free(_pkcsS11Ctx);
_pkcsS11Ctx = nullptr;
}
std::swap(_pkcsS11Ctx, otherContext._pkcsS11Ctx);
}

return *this;
}
Pkcs11Context& operator=(Pkcs11Context &&otherContext);

Pkcs11Context& operator=(const Pkcs11Context&) = delete;

Expand Down
4 changes: 3 additions & 1 deletion src/libsync/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,10 @@ void Logger::doLog(QtMsgType type, const QMessageLogContext &ctx, const QString
QString prefix;
switch (type) {
case QtDebugMsg:
prefix = QStringLiteral("[DEBUG] ");
break;
case QtInfoMsg:
prefix = QStringLiteral("[INFO] ");
break;
case QtWarningMsg:
prefix = QStringLiteral("[WARNING] ");
Expand All @@ -137,7 +139,7 @@ void Logger::doLog(QtMsgType type, const QMessageLogContext &ctx, const QString
prefix = QStringLiteral("[FATAL ERROR] ");
break;
}
auto msgW = QString(prefix + message).toStdWString();
auto msgW = QString(prefix + msg).toStdWString();
msgW.append(L"\n");
OutputDebugString(msgW.c_str());
}
Expand Down

0 comments on commit 5c7042c

Please sign in to comment.