13#include "keyparameters.h"
15#include <Libkleo/KeyUsage>
20#include "libkleo_debug.h"
24using namespace Qt::StringLiterals;
40 return email.left(at + 1) + encodeDomainName(email.mid(at + 1));
44class KeyParameters::Private
46 friend class ::Kleo::KeyParameters;
50 Subkey::PubkeyAlgo keyType = Subkey::AlgoUnknown;
52 unsigned int keyLength = 0;
56 Subkey::PubkeyAlgo subkeyType = Subkey::AlgoUnknown;
57 unsigned int subkeyLength = 0;
64 std::vector<QString> emailAdresses;
65 std::vector<QString> domainNames;
66 std::vector<QString> uris;
75 explicit Private(Protocol proto)
81KeyParameters::KeyParameters()
82 : KeyParameters{NoProtocol}
86KeyParameters::KeyParameters(Protocol protocol)
87 : d{new Private{protocol}}
91KeyParameters::~KeyParameters() =
default;
93KeyParameters::KeyParameters(
const KeyParameters &other)
94 : d{new Private{*other.d}}
98KeyParameters &KeyParameters::operator=(
const KeyParameters &other)
104KeyParameters::KeyParameters(KeyParameters &&other) =
default;
106KeyParameters &KeyParameters::operator=(KeyParameters &&other) =
default;
108KeyParameters::Protocol KeyParameters::protocol()
const
113void KeyParameters::setKeyType(Subkey::PubkeyAlgo type)
118GpgME::Subkey::PubkeyAlgo KeyParameters::keyType()
const
123void KeyParameters::setCardKeyRef(
const QString &cardKeyRef)
125 d->cardKeyRef = cardKeyRef;
128QString KeyParameters::cardKeyRef()
const
130 return d->cardKeyRef;
133void KeyParameters::setKeyLength(
unsigned int length)
135 d->keyLength = length;
138unsigned int KeyParameters::keyLength()
const
143void KeyParameters::setKeyCurve(
const QString &curve)
148QString KeyParameters::keyCurve()
const
153void KeyParameters::setKeyUsage(
const KeyUsage &usage)
158KeyUsage KeyParameters::keyUsage()
const
163void KeyParameters::setSubkeyType(Subkey::PubkeyAlgo type)
165 d->subkeyType =
type;
168Subkey::PubkeyAlgo KeyParameters::subkeyType()
const
170 return d->subkeyType;
173void KeyParameters::setSubkeyLength(
unsigned int length)
175 d->subkeyLength = length;
178unsigned int KeyParameters::subkeyLength()
const
180 return d->subkeyLength;
183void KeyParameters::setSubkeyCurve(
const QString &curve)
185 d->subkeyCurve = curve;
188QString KeyParameters::subkeyCurve()
const
190 return d->subkeyCurve;
193void KeyParameters::setSubkeyUsage(
const KeyUsage &usage)
195 d->subkeyUsage = usage;
198KeyUsage KeyParameters::subkeyUsage()
const
200 return d->subkeyUsage;
203void KeyParameters::setExpirationDate(
const QDate &date)
205 d->expirationDate = date;
208QDate KeyParameters::expirationDate()
const
210 return d->expirationDate;
213void KeyParameters::setName(
const QString &name)
218QString KeyParameters::name()
const
223void KeyParameters::setComment(
const QString &comment)
225 d->comment = comment;
228QString KeyParameters::comment()
const
233void KeyParameters::setDN(
const QString &dn)
238QString KeyParameters::dn()
const
243void KeyParameters::setEmail(
const QString &email)
245 d->emailAdresses = {email};
248void KeyParameters::addEmail(
const QString &email)
250 d->emailAdresses.push_back(email);
253std::vector<QString> KeyParameters::emails()
const
255 return d->emailAdresses;
258void KeyParameters::addDomainName(
const QString &domain)
260 d->domainNames.push_back(domain);
263std::vector<QString> KeyParameters::domainNames()
const
265 return d->domainNames;
268void KeyParameters::addURI(
const QString &uri)
270 d->uris.push_back(uri);
273std::vector<QString> KeyParameters::uris()
const
278QString KeyParameters::serial()
const
283void KeyParameters::setSerial(
const QString &serial)
288void KeyParameters::setUseRandomSerial()
290 d->serial = u
"random"_s;
293QString KeyParameters::issuerDN()
const
298void KeyParameters::setIssuerDN(
const QString &issuerDN)
300 d->issuerDN = issuerDN;
303QString KeyParameters::signingKey()
const
305 return d->signingKey;
308void KeyParameters::setSigningKey(
const QString &signingKey)
310 d->signingKey = signingKey;
315QString serialize(Subkey::PubkeyAlgo algo)
320QString serialize(
unsigned int number)
325QString serialize(KeyUsage keyUsage)
328 if (keyUsage.canSign()) {
329 usages << QStringLiteral(
"sign");
331 if (keyUsage.canEncrypt()) {
332 usages << QStringLiteral(
"encrypt");
334 if (keyUsage.canAuthenticate()) {
335 usages << QStringLiteral(
"auth");
337 if (keyUsage.canCertify()) {
338 usages << QStringLiteral(
"cert");
340 return usages.
join(QLatin1Char{
' '});
343QString serialize(
const QDate &date)
348QString serialize(
const char *key,
const QString &value)
354QString KeyParameters::toString()
const
356 QStringList keyParameters;
358 keyParameters.
push_back(QLatin1StringView(
"<GnupgKeyParms format=\"internal\">"));
360 if (d->protocol == OpenPGP) {
362 keyParameters.
push_back(QStringLiteral(
"%ask-passphrase"));
366 if (!d->cardKeyRef.isEmpty()) {
367 keyParameters.
push_back(serialize(
"Key-Type", QLatin1StringView{
"card:"} + d->cardKeyRef));
368 }
else if (d->keyType != Subkey::AlgoUnknown) {
369 keyParameters.
push_back(serialize(
"Key-Type", serialize(d->keyType)));
371 qCWarning(LIBKLEO_LOG) <<
"KeyParameters::toString(): Key type is unset/empty";
374 keyParameters.
push_back(serialize(
"Key-Length", serialize(d->keyLength)));
376 if (!d->keyCurve.isEmpty()) {
377 keyParameters.
push_back(serialize(
"Key-Curve", d->keyCurve));
379 keyParameters.
push_back(serialize(
"Key-Usage", serialize(d->keyUsage)));
381 if (d->subkeyType != Subkey::AlgoUnknown) {
382 keyParameters.
push_back(serialize(
"Subkey-Type", serialize(d->subkeyType)));
383 if (d->subkeyUsage.value()) {
384 keyParameters.
push_back(serialize(
"Subkey-Usage", serialize(d->subkeyUsage)));
386 if (d->subkeyLength) {
387 keyParameters.
push_back(serialize(
"Subkey-Length", serialize(d->subkeyLength)));
389 if (!d->subkeyCurve.isEmpty()) {
390 keyParameters.
push_back(serialize(
"Subkey-Curve", d->subkeyCurve));
394 if (d->expirationDate.isValid()) {
395 keyParameters.
push_back(serialize(
"Expire-Date", serialize(d->expirationDate)));
398 if (!d->serial.isEmpty()) {
399 keyParameters.
push_back(serialize(
"Serial", d->serial));
402 if (!d->issuerDN.isEmpty()) {
403 keyParameters.
push_back(serialize(
"Issuer-DN", d->issuerDN));
406 if (!d->issuerDN.isEmpty()) {
407 keyParameters.
push_back(serialize(
"Signing-Key", d->signingKey));
410 if (!d->name.isEmpty()) {
411 keyParameters.
push_back(serialize(
"Name-Real", d->name));
413 if (!d->comment.isEmpty()) {
414 keyParameters.
push_back(serialize(
"Name-Comment", d->comment));
416 if (!d->dn.isEmpty()) {
417 keyParameters.
push_back(serialize(
"Name-DN", d->dn));
419 std::transform(std::cbegin(d->emailAdresses), std::cend(d->emailAdresses), std::back_inserter(keyParameters), [
this](
const auto &email) {
420 return serialize(
"Name-Email", (d->protocol == CMS) ? encodeEmail(email) : email);
422 std::transform(std::cbegin(d->domainNames), std::cend(d->domainNames), std::back_inserter(keyParameters), [](
const auto &domain) {
423 return serialize(
"Name-DNS", encodeDomainName(domain));
425 std::transform(std::cbegin(d->uris), std::cend(d->uris), std::back_inserter(keyParameters), [](
const auto &uri) {
426 return serialize(
"Name-URI", uri);
429 keyParameters.
push_back(QLatin1StringView(
"</GnupgKeyParms>"));
431 return keyParameters.
join(QLatin1Char(
'\n'));
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
bool isEmpty() const const
QString toString(QStringView format, QCalendar cal) const const
void push_back(parameter_type value)
QString fromLatin1(QByteArrayView str)
qsizetype lastIndexOf(QChar ch, Qt::CaseSensitivity cs) const const
QString number(double n, char format, int precision)
QString join(QChar separator) const const
QByteArray toAce(const QString &domain, AceProcessingOptions options)