Skip to content

Commit e07eba5

Browse files
author
Lauris Kaplinski
committed
Preserve label data when re-encrypting
1 parent 1b0a005 commit e07eba5

2 files changed

Lines changed: 39 additions & 6 deletions

File tree

client/CDocSupport.cpp

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <QtCore/QJsonObject>
2323
#include <QtCore/QtEndian>
2424
#include <QtCore/QTemporaryFile>
25+
#include <QtCore/QUrlQuery>
2526
#include <QtNetwork/QNetworkAccessManager>
2627
#include <QtNetwork/QSslKey>
2728
#include <QLoggingCategory>
@@ -449,23 +450,55 @@ StreamListSource::next(std::string& name, int64_t& size)
449450
return libcdoc::OK;
450451
}
451452

453+
static void
454+
recode_label(std::string& label, uint64_t& expiry)
455+
{
456+
if(!label.starts_with("data:")) return;
457+
auto values = libcdoc::Lock::parseLabel(label);
458+
if (!values.contains("v") || !values.contains("type")) return;
459+
std::string v = values.at("v");
460+
values.erase("v");
461+
std::string type = values.at("type");
462+
values.erase("type");
463+
std::string expiry_str = values.at("server_exp");
464+
values.erase("server_exp");
465+
if (!expiry_str.empty()) expiry = std::stoll(expiry_str);
466+
QUrlQuery q;
467+
q.setQueryItems({
468+
{QStringLiteral("v"), QString::number(1)},
469+
{QStringLiteral("type"), QString::fromStdString(type)}
470+
});
471+
for (const auto& [key, value] : values) {
472+
if (!value.empty())
473+
q.addQueryItem(QString::fromStdString(key), QString::fromStdString(value));
474+
}
475+
label = "data:" + q.query(QUrl::FullyEncoded).toStdString();
476+
}
477+
452478
libcdoc::Recipient
453479
makeFromLock(const libcdoc::Lock& lock, const std::string& server_id)
454480
{
481+
uint64_t expiry_ts = 0;
482+
std::string label = lock.label;
483+
recode_label(label, expiry_ts);
455484
switch (lock.type) {
456485
case libcdoc::Lock::CDOC1:
457486
if (!server_id.empty()) {
458-
return libcdoc::Recipient::makeServer(lock.label, lock.getBytes(libcdoc::Lock::CERT), server_id);
487+
return libcdoc::Recipient::makeServer(label, lock.getBytes(libcdoc::Lock::CERT), server_id);
459488
} else {
460-
return libcdoc::Recipient::makeCertificate(lock.label, lock.getBytes(libcdoc::Lock::CERT));
489+
return libcdoc::Recipient::makeCertificate(label, lock.getBytes(libcdoc::Lock::CERT));
461490
}
462491
case libcdoc::Lock::PUBLIC_KEY:
463492
case libcdoc::Lock::SERVER: {
464493
libcdoc::Recipient::PKType rcpt_type = (lock.pk_type == libcdoc::Lock::RSA) ? libcdoc::Recipient::RSA : libcdoc::Recipient::ECC;
465494
if (!server_id.empty()) {
466-
return libcdoc::Recipient::makeServer(lock.label, lock.getBytes(libcdoc::Lock::RCPT_KEY), rcpt_type, server_id);
495+
libcdoc::Recipient rcpt = libcdoc::Recipient::makeServer(label, lock.getBytes(libcdoc::Lock::RCPT_KEY), rcpt_type, server_id);
496+
rcpt.expiry_ts = expiry_ts;
497+
return rcpt;
467498
} else {
468-
return libcdoc::Recipient::makePublicKey(lock.label, lock.getBytes(libcdoc::Lock::RCPT_KEY), rcpt_type);
499+
libcdoc::Recipient rcpt = libcdoc::Recipient::makePublicKey(label, lock.getBytes(libcdoc::Lock::RCPT_KEY), rcpt_type);
500+
rcpt.expiry_ts = expiry_ts;
501+
return rcpt;
469502
}
470503
}
471504
default:

client/widgets/AddressItem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ AddressItem::AddressItem(const CDKey &key, Type type, QWidget *parent)
6666
} else if (ui->key.lock.isValid()) {
6767
// Known lock type
6868
ui->code.clear();
69-
auto map = libcdoc::Recipient::parseLabel(ui->key.lock.label);
69+
auto map = libcdoc::Lock::parseLabel(ui->key.lock.label);
7070
if (map.contains("cn")) {
7171
ui->label = QString::fromStdString(map["cn"]);
7272
} else {
@@ -195,7 +195,7 @@ void AddressItem::setIdType() {
195195
// Known lock type
196196
// Needed to include translation for "ID-CARD"
197197
void(QT_TR_NOOP("ID-CARD"));
198-
auto items = libcdoc::Recipient::parseLabel(ui->key.lock.label);
198+
auto items = libcdoc::Lock::parseLabel(ui->key.lock.label);
199199
if (ui->key.lock.isCDoc1()) {
200200
const auto &bytes = ui->key.lock.getBytes(libcdoc::Lock::CERT);
201201
setIdType(SslCertificate(QByteArray::fromRawData((const char *)bytes.data(), bytes.size()), QSsl::Der));

0 commit comments

Comments
 (0)