Commit adf23c42 authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Store the rest of the key parameters with their native types

GnuPG-bug-id: 5832
parent 3de03624
......@@ -4,7 +4,7 @@
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB
SPDX-FileCopyrightText: 2020 g10 Code GmbH
SPDX-FileCopyrightText: 2020, 2022 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
......@@ -49,28 +49,28 @@ class KeyParameters::Private
Subkey::PubkeyAlgo keyType = Subkey::AlgoUnknown;
QString cardKeyRef;
unsigned int keyLength = 0;
QString keyCurve;
KeyUsage keyUsage;
Subkey::PubkeyAlgo subkeyType = Subkey::AlgoUnknown;
unsigned int subkeyLength = 0;
QString subkeyCurve;
KeyUsage subkeyUsage;
QMap<QString, QStringList> parameters;
QString name;
QString dn;
std::vector<QString> emailAdresses;
std::vector<QString> domainNames;
std::vector<QString> uris;
QDate expirationDate;
public:
explicit Private(Protocol proto)
: protocol(proto)
{
}
void setValue(const QString &key, const QString &value)
{
parameters[key] = QStringList() << value;
}
void addValue(const QString &key, const QString &value)
{
parameters[key].push_back(value);
}
};
KeyParameters::KeyParameters(Protocol protocol)
......@@ -95,6 +95,11 @@ KeyParameters::KeyParameters(KeyParameters &&other) = default;
KeyParameters &KeyParameters::operator=(KeyParameters &&other) = default;
KeyParameters::Protocol KeyParameters::protocol() const
{
return d->protocol;
}
void KeyParameters::setKeyType(Subkey::PubkeyAlgo type)
{
d->keyType = type;
......@@ -117,12 +122,22 @@ QString KeyParameters::cardKeyRef() const
void KeyParameters::setKeyLength(unsigned int length)
{
d->setValue(QStringLiteral("Key-Length"), QString::number(length));
d->keyLength = length;
}
unsigned int KeyParameters::keyLength() const
{
return d->keyLength;
}
void KeyParameters::setKeyCurve(const QString &curve)
{
d->setValue(QStringLiteral("Key-Curve"), curve);
d->keyCurve = curve;
}
QString KeyParameters::keyCurve() const
{
return d->keyCurve;
}
void KeyParameters::setKeyUsage(const KeyUsage &usage)
......@@ -147,12 +162,22 @@ Subkey::PubkeyAlgo KeyParameters::subkeyType() const
void KeyParameters::setSubkeyLength(unsigned int length)
{
d->setValue(QStringLiteral("Subkey-Length"), QString::number(length));
d->subkeyLength = length;
}
unsigned int KeyParameters::subkeyLength() const
{
return d->subkeyLength;
}
void KeyParameters::setSubkeyCurve(const QString &curve)
{
d->setValue(QStringLiteral("Subkey-Curve"), curve);
d->subkeyCurve = curve;
}
QString KeyParameters::subkeyCurve() const
{
return d->subkeyCurve;
}
void KeyParameters::setSubkeyUsage(const KeyUsage &usage)
......@@ -167,43 +192,81 @@ KeyUsage KeyParameters::subkeyUsage() const
void KeyParameters::setExpirationDate(const QDate &date)
{
d->setValue(QStringLiteral("Expire-Date"), date.toString(Qt::ISODate));
d->expirationDate = date;
}
QDate KeyParameters::expirationDate() const
{
return d->expirationDate;
}
void KeyParameters::setName(const QString &name)
{
d->setValue(QStringLiteral("Name-Real"), name);
d->name = name;
}
QString KeyParameters::name() const
{
return d->name;
}
void KeyParameters::setDN(const QString &dn)
{
d->setValue(QStringLiteral("Name-DN"), dn);
d->dn = dn;
}
QString KeyParameters::dn() const
{
return d->dn;
}
void KeyParameters::setEmail(const QString &email)
{
d->setValue(QStringLiteral("Name-Email"),
(d->protocol == CMS) ? encodeEmail(email) : email);
d->emailAdresses = {email};
}
void KeyParameters::addEmail(const QString& email)
{
d->addValue(QStringLiteral("Name-Email"),
(d->protocol == CMS) ? encodeEmail(email) : email);
d->emailAdresses.push_back(email);
}
std::vector<QString> KeyParameters::emails() const
{
return d->emailAdresses;
}
void KeyParameters::addDomainName(const QString& domain)
{
d->addValue(QStringLiteral("Name-DNS"), encodeDomainName(domain));
d->domainNames.push_back(domain);
}
std::vector<QString> KeyParameters::domainNames() const
{
return d->domainNames;
}
void KeyParameters::addURI(const QString& uri)
{
d->addValue(QStringLiteral("Name-URI"), uri);
d->uris.push_back(uri);
}
std::vector<QString> KeyParameters::uris() const
{
return d->uris;
}
namespace
{
QString serialize(Subkey::PubkeyAlgo algo)
{
return QString::fromLatin1(Subkey::publicKeyAlgorithmAsString(algo));
}
QString serialize(unsigned int number)
{
return QString::number(number);
}
QString serialize(KeyUsage keyUsage)
{
QStringList usages;
......@@ -221,6 +284,16 @@ QString serialize(KeyUsage keyUsage)
}
return usages.join(QLatin1Char{' '});
}
QString serialize(const QDate &date)
{
return date.toString(Qt::ISODate);
}
QString serialize(const char *key, const QString &value)
{
return QString::fromLatin1(key) + QLatin1Char(':') + value;
}
}
QString KeyParameters::toString() const
......@@ -236,26 +309,52 @@ QString KeyParameters::toString() const
// add Key-Type as first parameter
if (!d->cardKeyRef.isEmpty()) {
keyParameters.push_back(QLatin1String{"Key-Type:card:"} + d->cardKeyRef);
keyParameters.push_back(serialize("Key-Type", QLatin1String{"card:"} + d->cardKeyRef));
} else if (d->keyType != Subkey::AlgoUnknown) {
keyParameters.push_back(QLatin1String{"Key-Type:"} + QString::fromLatin1(Subkey::publicKeyAlgorithmAsString(d->keyType)));
keyParameters.push_back(serialize("Key-Type", serialize(d->keyType)));
} else {
qCWarning(KLEOPATRA_LOG) << "KeyParameters::toString(): Key type is unset/empty";
}
keyParameters.push_back(QLatin1String{"Key-Usage:"} + serialize(d->keyUsage));
if (d->keyLength) {
keyParameters.push_back(serialize("Key-Length", serialize(d->keyLength)));
}
if (!d->keyCurve.isEmpty()) {
keyParameters.push_back(serialize("Key-Curve", d->keyCurve));
}
keyParameters.push_back(serialize("Key-Usage", serialize(d->keyUsage)));
if (d->subkeyType != Subkey::AlgoUnknown) {
keyParameters.push_back(QLatin1String{"Subkey-Type:"} + QString::fromLatin1(Subkey::publicKeyAlgorithmAsString(d->subkeyType)));
keyParameters.push_back(serialize("Subkey-Type", serialize(d->subkeyType)));
if (d->subkeyUsage.value()) {
keyParameters.push_back(QLatin1String{"Subkey-Usage:"} + serialize(d->subkeyUsage));
keyParameters.push_back(serialize("Subkey-Usage", serialize(d->subkeyUsage)));
}
if (d->subkeyLength) {
keyParameters.push_back(serialize("Subkey-Length", serialize(d->subkeyLength)));
}
if (!d->subkeyCurve.isEmpty()) {
keyParameters.push_back(serialize("Subkey-Curve", d->subkeyCurve));
}
}
for (auto it = d->parameters.constBegin(); it != d->parameters.constEnd(); ++it) {
for (const auto &v : it.value()) {
keyParameters.push_back(it.key() + QLatin1Char(':') + v);
}
if (d->expirationDate.isValid()) {
keyParameters.push_back(serialize("Expire-Date", serialize(d->expirationDate)));
}
if (!d->name.isEmpty()) {
keyParameters.push_back(serialize("Name-Real", d->name));
}
if (!d->dn.isEmpty()) {
keyParameters.push_back(serialize("Name-DN", d->dn));
}
std::transform(std::cbegin(d->emailAdresses), std::cend(d->emailAdresses), std::back_inserter(keyParameters), [this](const auto &email) {
return serialize("Name-Email", (d->protocol == CMS) ? encodeEmail(email) : email);
});
std::transform(std::cbegin(d->domainNames), std::cend(d->domainNames), std::back_inserter(keyParameters), [](const auto &domain) {
return serialize("Name-DNS", encodeDomainName(domain));
});
std::transform(std::cbegin(d->uris), std::cend(d->uris), std::back_inserter(keyParameters), [](const auto &uri) {
return serialize("Name-URI", uri);
});
keyParameters.push_back(QLatin1String("</GnupgKeyParms>"));
......
......@@ -2,7 +2,7 @@
utils/keyparameters.h
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2020 g10 Code GmbH
SPDX-FileCopyrightText: 2020, 2022 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
......@@ -26,7 +26,7 @@ class KeyParameters
public:
enum Protocol {
OpenPGP,
CMS
CMS,
};
explicit KeyParameters(Protocol protocol);
......@@ -38,30 +38,42 @@ public:
KeyParameters(KeyParameters &&other);
KeyParameters &operator=(KeyParameters &&other);
Protocol protocol() const;
void setKeyType(GpgME::Subkey::PubkeyAlgo type);
GpgME::Subkey::PubkeyAlgo keyType() const;
void setCardKeyRef(const QString &cardKeyRef);
QString cardKeyRef() const;
void setKeyLength(unsigned int length);
unsigned int keyLength() const;
void setKeyCurve(const QString &curve);
QString keyCurve() const;
void setKeyUsage(const KeyUsage &usage);
KeyUsage keyUsage() const;
void setSubkeyType(GpgME::Subkey::PubkeyAlgo type);
GpgME::Subkey::PubkeyAlgo subkeyType() const;
void setSubkeyLength(unsigned int length);
unsigned int subkeyLength() const;
void setSubkeyCurve(const QString &curve);
QString subkeyCurve() const;
void setSubkeyUsage(const KeyUsage &usage);
KeyUsage subkeyUsage() const;
void setExpirationDate(const QDate &date);
QDate expirationDate() const;
void setName(const QString &name);
QString name() const;
void setDN(const QString &dn);
QString dn() const;
void setEmail(const QString &email);
void addEmail(const QString &email);
std::vector<QString> emails() const;
void addDomainName(const QString &domain);
std::vector<QString> domainNames() const;
void addURI(const QString &uri);
std::vector<QString> uris() const;
QString toString() const;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment