Skip to content

Commit

Permalink
Fix coverity warnings
Browse files Browse the repository at this point in the history
IB-7552

Signed-off-by: Raul Metsma <raul@metsma.ee>
  • Loading branch information
metsma committed Sep 19, 2023
1 parent a7a80bd commit 7377a40
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
6 changes: 3 additions & 3 deletions client/CDoc1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ bool CDoc1::save(const QString &path)
}});
writeElement(w,DENC, QStringLiteral("CipherData"), [&]{
writeBase64Element(w, DENC, QStringLiteral("CipherValue"),
Crypto::cipher(ENC_MTH[method], transportKey, data.data(), true)
Crypto::cipher(ENC_MTH[method], transportKey, data.buffer(), true)
);
});
writeElement(w, DENC, QStringLiteral("EncryptionProperties"), [&]{
Expand Down Expand Up @@ -622,15 +622,15 @@ void CDoc1::writeDDoc(QIODevice *ddoc)
x.writeEndDocument();
}

void CDoc1::writeElement(QXmlStreamWriter &x, const QString &ns, const QString &name, const std::function<void()> &f)
void CDoc1::writeElement(QXmlStreamWriter &x, const QString &ns, const QString &name, std::function<void()> &&f)
{
x.writeStartElement(ns, name);
if(f)
f();
x.writeEndElement();
}

void CDoc1::writeElement(QXmlStreamWriter &x, const QString &ns, const QString &name, const QMap<QString,QString> &attrs, const std::function<void()> &f)
void CDoc1::writeElement(QXmlStreamWriter &x, const QString &ns, const QString &name, const QMap<QString,QString> &attrs, std::function<void()> &&f)
{
x.writeStartElement(ns, name);
writeAttributes(x, attrs);
Expand Down
4 changes: 2 additions & 2 deletions client/CDoc1.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class CDoc1 final: public CDoc, private QFile
static void readXML(QIODevice *io, const std::function<void (QXmlStreamReader &)> &f);
static void writeAttributes(QXmlStreamWriter &x, const QMap<QString,QString> &attrs);
static void writeBase64Element(QXmlStreamWriter &x, const QString &ns, const QString &name, const QByteArray &data);
static void writeElement(QXmlStreamWriter &x, const QString &ns, const QString &name, const std::function<void()> &f = {});
static void writeElement(QXmlStreamWriter &x, const QString &ns, const QString &name, const QMap<QString,QString> &attrs, const std::function<void()> &f = {});
static void writeElement(QXmlStreamWriter &x, const QString &ns, const QString &name, std::function<void ()> &&f = {});
static void writeElement(QXmlStreamWriter &x, const QString &ns, const QString &name, const QMap<QString,QString> &attrs, std::function<void ()> &&f = {});

QString method, mime;
QHash<QString,QString> properties;
Expand Down
8 changes: 4 additions & 4 deletions client/CDoc2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ namespace cdoc20 {
{
if(io->isReadable())
{
inflateInit2(&s, MAX_WBITS);
open(QIODevice::ReadOnly);
if(inflateInit2(&s, MAX_WBITS) == Z_OK)
open(QIODevice::ReadOnly);
}
if(io->isWritable())
{
deflateInit(&s, Z_DEFAULT_COMPRESSION);
open(QIODevice::WriteOnly);
if(deflateInit(&s, Z_DEFAULT_COMPRESSION) == Z_OK)
open(QIODevice::WriteOnly);
}
}

Expand Down
31 changes: 17 additions & 14 deletions client/Crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Crypto::Cipher::Cipher(const EVP_CIPHER *cipher, const QByteArray &key, const QB
: ctx(SCOPE(EVP_CIPHER_CTX, EVP_CIPHER_CTX_new()))
{
EVP_CIPHER_CTX_set_flags(ctx.get(), EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
isError(EVP_CipherInit_ex(ctx.get(), cipher, nullptr, pcuchar(key.data()), iv.isEmpty() ? nullptr : pcuchar(iv.data()), int(encrypt)));
Q_UNUSED(isError(EVP_CipherInit_ex(ctx.get(), cipher, nullptr, pcuchar(key.data()), iv.isEmpty() ? nullptr : pcuchar(iv.data()), int(encrypt))));
}

bool Crypto::Cipher::updateAAD(const QByteArray &data) const
Expand Down Expand Up @@ -88,29 +88,32 @@ QByteArray Crypto::Cipher::resultTAG() const
QByteArray Crypto::aes_wrap(const QByteArray &key, const QByteArray &data, bool encrypt)
{
Cipher c(key.size() == 32 ? EVP_aes_256_wrap() : EVP_aes_128_wrap(), key, {}, encrypt);
QByteArray result = c.update(data);
return c.result() ? result : QByteArray();
if(QByteArray result = c.update(data); c.result())
return result;
return {};
}

QByteArray Crypto::cipher(const EVP_CIPHER *cipher, const QByteArray &key, const QByteArray &data, bool encrypt)
QByteArray Crypto::cipher(const EVP_CIPHER *cipher, const QByteArray &key, QByteArray &data, bool encrypt)
{
QByteArray iv(EVP_CIPHER_iv_length(cipher), 0), _data = data, tag;
QByteArray iv(EVP_CIPHER_iv_length(cipher), 0), tag;
if(!encrypt)
{
iv = data.left(iv.length());
data.remove(0, iv.length());
if(EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE)
tag = data.right(16);
_data = data.mid(iv.size(), data.size() - iv.size() - tag.size());
data.resize(data.size() - tag.size());
}

auto ctx = SCOPE(EVP_CIPHER_CTX, EVP_CIPHER_CTX_new());
if(isError(EVP_CipherInit(ctx.get(), cipher, pcuchar(key.constData()), pcuchar(iv.constData()), encrypt)))
return {};

QByteArray result(_data.size() + EVP_CIPHER_CTX_block_size(ctx.get()), Qt::Uninitialized);
int size = int(result.size());
auto resultPointer = puchar(result.data()); //Detach only once
if(isError(EVP_CipherUpdate(ctx.get(), resultPointer, &size, pcuchar(_data.constData()), int(_data.size()))))
int dataSize = int(data.size());
data.resize(data.size() + EVP_CIPHER_CTX_block_size(ctx.get()));
int size = int(data.size());
auto resultPointer = puchar(data.data()); //Detach only once
if(isError(EVP_CipherUpdate(ctx.get(), resultPointer, &size, pcuchar(data.constData()), dataSize)))
return {};

if(!encrypt && EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE)
Expand All @@ -119,18 +122,18 @@ QByteArray Crypto::cipher(const EVP_CIPHER *cipher, const QByteArray &key, const
int size2 = 0;
if(isError(EVP_CipherFinal(ctx.get(), resultPointer + size, &size2)))
return {};
result.resize(size + size2);
data.resize(size + size2);
if(encrypt)
{
result.prepend(iv);
data.prepend(iv);
if(EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE)
{
tag = QByteArray(16, 0);
EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_GET_TAG, int(tag.size()), tag.data());
result.append(tag);
data.append(tag);
}
}
return result;
return data;
}

QByteArray Crypto::concatKDF(QCryptographicHash::Algorithm hashAlg, quint32 keyDataLen, const QByteArray &z, const QByteArray &otherInfo)
Expand Down
2 changes: 1 addition & 1 deletion client/Crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Crypto
};

static QByteArray aes_wrap(const QByteArray &key, const QByteArray &data, bool encrypt);
static QByteArray cipher(const EVP_CIPHER *cipher, const QByteArray &key, const QByteArray &data, bool encrypt);
static QByteArray cipher(const EVP_CIPHER *cipher, const QByteArray &key, QByteArray &data, bool encrypt);
static QByteArray curve_oid(EVP_PKEY *key);
static QByteArray concatKDF(QCryptographicHash::Algorithm digestMethod,
quint32 keyDataLen, const QByteArray &z, const QByteArray &otherInfo);
Expand Down

0 comments on commit 7377a40

Please sign in to comment.