aboutsummaryrefslogtreecommitdiff
path: root/lib/events/filesourceinfo.cpp
diff options
context:
space:
mode:
authorAlexey Rusakov <Kitsune-Ral@users.sf.net>2022-09-26 15:20:53 +0200
committerGitHub <noreply@github.com>2022-09-26 15:20:53 +0200
commit4c8dcbc308eb0f4900e416e698f5f30e71daaad8 (patch)
treeaa5fdaa81234a21c6919fac4958f84d7c26cd397 /lib/events/filesourceinfo.cpp
parent5904a61c59f0eef00aef07ef998658fd791ff139 (diff)
parent15b840d82d4794358fbf1271ea76e446b47db7e5 (diff)
downloadlibquotient-4c8dcbc308eb0f4900e416e698f5f30e71daaad8.tar.gz
libquotient-4c8dcbc308eb0f4900e416e698f5f30e71daaad8.zip
Merge #571: Unify reporting and handling of Olm errors
Diffstat (limited to 'lib/events/filesourceinfo.cpp')
-rw-r--r--lib/events/filesourceinfo.cpp39
1 files changed, 15 insertions, 24 deletions
diff --git a/lib/events/filesourceinfo.cpp b/lib/events/filesourceinfo.cpp
index e8b6794b..a60d86d2 100644
--- a/lib/events/filesourceinfo.cpp
+++ b/lib/events/filesourceinfo.cpp
@@ -59,23 +59,17 @@ std::pair<EncryptedFileMetadata, QByteArray> Quotient::encryptFile(
const QByteArray& plainText)
{
#ifdef Quotient_E2EE_ENABLED
- QByteArray k = getRandom(32);
- auto kBase64 = k.toBase64();
- QByteArray iv = getRandom(16);
- JWK key = { "oct"_ls,
- { "encrypt"_ls, "decrypt"_ls },
- "A256CTR"_ls,
- QString(k.toBase64())
- .replace(u'/', u'_')
- .replace(u'+', u'-')
- .left(kBase64.indexOf('=')),
- true };
-
- int length;
+ auto k = RandomBuffer(32);
+ auto kBase64 = k.toBase64(QByteArray::Base64UrlEncoding
+ | QByteArray::OmitTrailingEquals);
+ auto iv = RandomBuffer(16);
+ JWK key = {
+ "oct"_ls, { "encrypt"_ls, "decrypt"_ls }, "A256CTR"_ls, kBase64, true
+ };
+
+ int length = -1;
auto* ctx = EVP_CIPHER_CTX_new();
- EVP_EncryptInit_ex(ctx, EVP_aes_256_ctr(), nullptr,
- reinterpret_cast<const unsigned char*>(k.data()),
- reinterpret_cast<const unsigned char*>(iv.data()));
+ EVP_EncryptInit_ex(ctx, EVP_aes_256_ctr(), nullptr, k.bytes(), iv.bytes());
const auto blockSize = EVP_CIPHER_CTX_block_size(ctx);
QByteArray cipherText(plainText.size() + blockSize - 1, '\0');
EVP_EncryptUpdate(ctx, reinterpret_cast<unsigned char*>(cipherText.data()),
@@ -89,14 +83,11 @@ std::pair<EncryptedFileMetadata, QByteArray> Quotient::encryptFile(
EVP_CIPHER_CTX_free(ctx);
auto hash = QCryptographicHash::hash(cipherText, QCryptographicHash::Sha256)
- .toBase64();
- auto ivBase64 = iv.toBase64();
- EncryptedFileMetadata efm = { {},
- key,
- ivBase64.left(ivBase64.indexOf('=')),
- { { QStringLiteral("sha256"),
- hash.left(hash.indexOf('=')) } },
- "v2"_ls };
+ .toBase64(QByteArray::OmitTrailingEquals);
+ auto ivBase64 = iv.toBase64(QByteArray::OmitTrailingEquals);
+ EncryptedFileMetadata efm = {
+ {}, key, ivBase64, { { QStringLiteral("sha256"), hash } }, "v2"_ls
+ };
return { efm, cipherText };
#else
return {};