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

Set and store the usage flags of key and subkey using new KeyUsage

This makes users of KeyParameter independent of the details of the
serialization.

GnuPG-bug-id: 5832
parent be31bd7a
......@@ -23,6 +23,7 @@
#include "utils/filedialog.h"
#include "utils/keyparameters.h"
#include "utils/keyusage.h"
#include <Libkleo/Formatting>
......@@ -77,7 +78,7 @@ private:
private:
std::string appName;
std::string keyRef;
QStringList keyUsages;
KeyUsage keyUsage;
QPointer<CreateCSRForCardKeyDialog> dialog;
};
......@@ -107,20 +108,20 @@ CreateCSRForCardKeyCommand::Private::~Private()
namespace
{
QStringList getKeyUsages(const KeyPairInfo &keyInfo)
KeyUsage getKeyUsage(const KeyPairInfo &keyInfo)
{
// note: gpgsm does not support creating CSRs for authentication certificates
QStringList usages;
KeyUsage usage;
if (keyInfo.canCertify()) {
usages.push_back(QStringLiteral("cert"));
usage.setCanCertify(true);
}
if (keyInfo.canSign()) {
usages.push_back(QStringLiteral("sign"));
usage.setCanSign(true);
}
if (keyInfo.canEncrypt()) {
usages.push_back(QStringLiteral("encrypt"));
usage.setCanEncrypt(true);
}
return usages;
return usage;
}
}
......@@ -140,7 +141,7 @@ void CreateCSRForCardKeyCommand::Private::start()
}
const KeyPairInfo &keyInfo = card->keyInfo(keyRef);
keyUsages = getKeyUsages(keyInfo);
keyUsage = getKeyUsage(keyInfo);
ensureDialogCreated();
......@@ -180,7 +181,7 @@ void CreateCSRForCardKeyCommand::Private::slotDialogAccepted()
KeyParameters keyParameters(KeyParameters::CMS);
keyParameters.setCardKeyRef(QString::fromStdString(keyRef));
keyParameters.setKeyUsages(keyUsages);
keyParameters.setKeyUsage(keyUsage);
keyParameters.setDN(dialog->dn());
keyParameters.setEmail(dialog->email());
......
......@@ -20,6 +20,7 @@
#include "kleopatraapplication.h"
#include "utils/keyparameters.h"
#include "utils/keyusage.h"
#include <Libkleo/Formatting>
#include <Libkleo/KeyCache>
......@@ -114,36 +115,36 @@ void KeyCreationPage::startJob()
}
}
QStringList KeyCreationPage::keyUsages() const
KeyUsage KeyCreationPage::keyUsage() const
{
QStringList usages;
KeyUsage usage;
if (signingAllowed()) {
usages << QStringLiteral("sign");
usage.setCanSign(true);
}
if (encryptionAllowed() && !is_ecdh(subkeyType()) &&
!is_dsa(keyType()) && !is_rsa(subkeyType())) {
usages << QStringLiteral("encrypt");
usage.setCanEncrypt(true);
}
if (authenticationAllowed()) {
usages << QStringLiteral("auth");
usage.setCanAuthenticate(true);
}
if (usages.empty() && certificationAllowed()) {
if (!usage.value() && certificationAllowed()) {
/* Empty usages cause an error so we need to
* add at least certify if nothing else is selected */
usages << QStringLiteral("cert");
usage.setCanCertify(true);
}
return usages;
return usage;
}
QStringList KeyCreationPage::subkeyUsages() const
KeyUsage KeyCreationPage::subkeyUsage() const
{
QStringList usages;
KeyUsage usage;
if (encryptionAllowed() && (is_dsa(keyType()) || is_rsa(subkeyType()) ||
is_ecdh(subkeyType()))) {
Q_ASSERT(subkeyType());
usages << QStringLiteral("encrypt");
usage.setCanEncrypt(true);
}
return usages;
return usage;
}
QString KeyCreationPage::createGnupgKeyParms() const
......@@ -156,7 +157,7 @@ QString KeyCreationPage::createGnupgKeyParms() const
} else if (const unsigned int strength = keyStrength()) {
keyParameters.setKeyLength(strength);
}
keyParameters.setKeyUsages(keyUsages());
keyParameters.setKeyUsage(keyUsage());
if (subkeyType()) {
keyParameters.setSubkeyType(subkeyType());
......@@ -165,7 +166,7 @@ QString KeyCreationPage::createGnupgKeyParms() const
} else if (const unsigned int strength = subkeyStrength()) {
keyParameters.setSubkeyLength(strength);
}
keyParameters.setSubkeyUsages(subkeyUsages());
keyParameters.setSubkeyUsage(subkeyUsage());
}
if (pgp()) {
......
......@@ -23,6 +23,10 @@ namespace QGpgME
{
class KeyGenerationJob;
}
namespace Kleo
{
class KeyUsage;
}
class KeyCreationPage : public Kleo::NewCertificateUi::WizardPage
{
......@@ -37,8 +41,8 @@ public:
private:
void startJob();
QStringList keyUsages() const;
QStringList subkeyUsages() const;
Kleo::KeyUsage keyUsage() const;
Kleo::KeyUsage subkeyUsage() const;
QString createGnupgKeyParms() const;
private Q_SLOTS:
......
......@@ -12,6 +12,8 @@
#include "keyparameters.h"
#include "keyusage.h"
#include <QDate>
#include <QMap>
#include <QUrl>
......@@ -47,8 +49,10 @@ class KeyParameters::Private
Subkey::PubkeyAlgo keyType = Subkey::AlgoUnknown;
QString cardKeyRef;
KeyUsage keyUsage;
Subkey::PubkeyAlgo subkeyType = Subkey::AlgoUnknown;
KeyUsage subkeyUsage;
QMap<QString, QStringList> parameters;
......@@ -121,9 +125,14 @@ void KeyParameters::setKeyCurve(const QString &curve)
d->setValue(QStringLiteral("Key-Curve"), curve);
}
void KeyParameters::setKeyUsages(const QStringList &usages)
void KeyParameters::setKeyUsage(const KeyUsage &usage)
{
d->setValue(QStringLiteral("Key-Usage"), usages.join(QLatin1Char(' ')));
d->keyUsage = usage;
}
KeyUsage KeyParameters::keyUsage() const
{
return d->keyUsage;
}
void KeyParameters::setSubkeyType(Subkey::PubkeyAlgo type)
......@@ -146,9 +155,14 @@ void KeyParameters::setSubkeyCurve(const QString &curve)
d->setValue(QStringLiteral("Subkey-Curve"), curve);
}
void KeyParameters::setSubkeyUsages(const QStringList &usages)
void KeyParameters::setSubkeyUsage(const KeyUsage &usage)
{
d->setValue(QStringLiteral("Subkey-Usage"), usages.join(QLatin1Char(' ')));
d->subkeyUsage = usage;
}
KeyUsage KeyParameters::subkeyUsage() const
{
return d->subkeyUsage;
}
void KeyParameters::setExpirationDate(const QDate &date)
......@@ -188,6 +202,27 @@ void KeyParameters::addURI(const QString& uri)
d->addValue(QStringLiteral("Name-URI"), uri);
}
namespace
{
QString serialize(KeyUsage keyUsage)
{
QStringList usages;
if (keyUsage.canSign()) {
usages << QStringLiteral("sign");
}
if (keyUsage.canEncrypt()) {
usages << QStringLiteral("encrypt");
}
if (keyUsage.canAuthenticate()) {
usages << QStringLiteral("auth");
}
if (keyUsage.canCertify()) {
usages << QStringLiteral("cert");
}
return usages.join(QLatin1Char{' '});
}
}
QString KeyParameters::toString() const
{
QStringList keyParameters;
......@@ -207,9 +242,13 @@ QString KeyParameters::toString() const
} else {
qCWarning(KLEOPATRA_LOG) << "KeyParameters::toString(): Key type is unset/empty";
}
keyParameters.push_back(QLatin1String{"Key-Usage:"} + serialize(d->keyUsage));
if (d->subkeyType != Subkey::AlgoUnknown) {
keyParameters.push_back(QLatin1String{"Subkey-Type:"} + QString::fromLatin1(Subkey::publicKeyAlgorithmAsString(d->subkeyType)));
if (d->subkeyUsage.value()) {
keyParameters.push_back(QLatin1String{"Subkey-Usage:"} + serialize(d->subkeyUsage));
}
}
for (auto it = d->parameters.constBegin(); it != d->parameters.constEnd(); ++it) {
......
......@@ -16,10 +16,10 @@
class QDate;
class QString;
#include <QStringList>
namespace Kleo
{
class KeyUsage;
class KeyParameters
{
......@@ -44,13 +44,15 @@ public:
QString cardKeyRef() const;
void setKeyLength(unsigned int length);
void setKeyCurve(const QString &curve);
void setKeyUsages(const QStringList &usages);
void setKeyUsage(const KeyUsage &usage);
KeyUsage keyUsage() const;
void setSubkeyType(GpgME::Subkey::PubkeyAlgo type);
GpgME::Subkey::PubkeyAlgo subkeyType() const;
void setSubkeyLength(unsigned int length);
void setSubkeyCurve(const QString &curve);
void setSubkeyUsages(const QStringList &usages);
void setSubkeyUsage(const KeyUsage &usage);
KeyUsage subkeyUsage() const;
void setExpirationDate(const QDate &date);
......@@ -69,4 +71,3 @@ private:
};
}
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