aboutsummaryrefslogtreecommitdiff
path: root/lib/events
diff options
context:
space:
mode:
Diffstat (limited to 'lib/events')
-rw-r--r--lib/events/encryptedevent.cpp1
-rw-r--r--lib/events/encryptedevent.h7
-rw-r--r--lib/events/filesourceinfo.cpp39
3 files changed, 22 insertions, 25 deletions
diff --git a/lib/events/encryptedevent.cpp b/lib/events/encryptedevent.cpp
index 49df25c8..540594d1 100644
--- a/lib/events/encryptedevent.cpp
+++ b/lib/events/encryptedevent.cpp
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "encryptedevent.h"
+#include "e2ee/e2ee.h"
#include "logging.h"
using namespace Quotient;
diff --git a/lib/events/encryptedevent.h b/lib/events/encryptedevent.h
index 02d4c7aa..e24e5745 100644
--- a/lib/events/encryptedevent.h
+++ b/lib/events/encryptedevent.h
@@ -3,10 +3,15 @@
#pragma once
-#include "e2ee/e2ee.h"
#include "roomevent.h"
namespace Quotient {
+
+constexpr auto CiphertextKeyL = "ciphertext"_ls;
+constexpr auto SenderKeyKeyL = "sender_key"_ls;
+constexpr auto DeviceIdKeyL = "device_id"_ls;
+constexpr auto SessionIdKeyL = "session_id"_ls;
+
/*
* While the specification states:
*
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 {};