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

Deduplicate setCardInfo()

* Move (after previous commit) basically identical implementation of
  setCardInfo() in all subclasses into the base class.
* Add virtual processCardInfo() to allow subclasses to further process
  card info after it has been parsed.

GnuPG-bug-id: 4876
parent a396f548
......@@ -229,6 +229,16 @@ const KeyPairInfo & Card::keyInfo(const std::string &keyRef) const
return nullKey;
}
void Card::setCardInfo(const std::vector<std::pair<std::string, std::string>> &infos)
{
qCDebug(KLEOPATRA_LOG) << "Card" << serialNumber().c_str() << "info:";
for (const auto &pair: infos) {
qCDebug(KLEOPATRA_LOG) << pair.first.c_str() << ":" << pair.second.c_str();
parseCardInfo(pair.first, pair.second);
}
processCardInfo();
}
namespace {
static int parseHexEncodedVersionTuple(const std::string &s) {
// s is a hex-encoded, unsigned int-packed version tuple,
......@@ -239,23 +249,19 @@ static int parseHexEncodedVersionTuple(const std::string &s) {
}
}
bool Card::parseCardInfo(const std::string &name, const std::string &value)
void Card::parseCardInfo(const std::string &name, const std::string &value)
{
if (name == "APPVERSION") {
mAppVersion = parseHexEncodedVersionTuple(value);
return true;
} else if (name == "CARDTYPE") {
mCardType = value;
return true;
} else if (name == "CARDVERSION") {
mCardVersion = parseHexEncodedVersionTuple(value);
return true;
} else if (name == "DISP-NAME") {
auto list = QString::fromUtf8(QByteArray::fromStdString(value)).
split(QStringLiteral("<<"), Qt::SkipEmptyParts);
std::reverse(list.begin(), list.end());
mCardHolder = list.join(QLatin1Char(' '));
return true;
} else if (name == "KEYPAIRINFO") {
const KeyPairInfo info = KeyPairInfo::fromStatusLine(value);
if (info.grip.empty()) {
......@@ -264,7 +270,6 @@ bool Card::parseCardInfo(const std::string &name, const std::string &value)
} else {
updateKeyInfo(info);
}
return true;
} else if (name == "KEY-FPR") {
// handle OpenPGP key fingerprints
const auto values = QString::fromStdString(value).split(QLatin1Char(' '));
......@@ -281,12 +286,13 @@ bool Card::parseCardInfo(const std::string &name, const std::string &value)
// Maybe more keyslots in the future?
qCDebug(KLEOPATRA_LOG) << "Unhandled keyslot" << keyNumber;
}
return true;
} else {
mCardInfo.insert({name, value});
}
}
return false;
void Card::processCardInfo()
{
}
void Card::addCardInfo(const std::string &name, const std::string &value)
......
......@@ -60,6 +60,8 @@ public:
void setSerialNumber(const std::string &sn);
std::string serialNumber() const;
void setCardInfo(const std::vector<std::pair<std::string, std::string>> &infos);
QString displaySerialNumber() const;
void setDisplaySerialNumber(const QString &sn);
......@@ -103,12 +105,14 @@ protected:
void setAppName(const std::string &name);
void setInitialKeyInfos(const std::vector<KeyPairInfo> &infos);
bool parseCardInfo(const std::string &name, const std::string &value);
virtual void processCardInfo();
void addCardInfo(const std::string &name, const std::string &value);
std::string cardInfo(const std::string &name) const;
private:
void parseCardInfo(const std::string &name, const std::string &value);
void updateKeyInfo(const KeyPairInfo &keyPairInfo);
private:
......
......@@ -65,13 +65,8 @@ std::string NetKeyCard::sigGPinKeyRef()
return std::string("PW1.CH.SIG");
}
void NetKeyCard::setCardInfo(const std::vector< std::pair<std::string, std::string> > &infos)
void NetKeyCard::processCardInfo()
{
qCDebug(KLEOPATRA_LOG) << "Card" << serialNumber().c_str() << "info:";
for (const auto &pair: infos) {
qCDebug(KLEOPATRA_LOG) << pair.first.c_str() << ":" << pair.second.c_str();
parseCardInfo(pair.first, pair.second);
}
setKeyPairInfo(keyInfos());
}
......
......@@ -28,14 +28,13 @@ public:
static std::string nksPinKeyRef();
static std::string sigGPinKeyRef();
void setCardInfo(const std::vector< std::pair<std::string, std::string> > &infos);
bool hasSigGNullPin() const;
bool hasNKSNullPin() const;
std::vector <GpgME::Key> keys() const;
private:
void processCardInfo() override;
void setKeyPairInfo(const std::vector<KeyPairInfo> &infos);
private:
......
......@@ -99,17 +99,6 @@ QString OpenPGPCard::keyDisplayName(const std::string &keyRef)
return displayNames.value(keyRef);
}
void OpenPGPCard::setCardInfo(const std::vector< std::pair<std::string, std::string> > &infos)
{
qCDebug(KLEOPATRA_LOG) << "Card" << serialNumber().c_str() << "info:";
for (const auto &pair: infos) {
qCDebug(KLEOPATRA_LOG) << pair.first.c_str() << ":" << pair.second.c_str();
if (parseCardInfo(pair.first, pair.second)) {
continue;
}
}
}
std::string OpenPGPCard::keyFingerprint(const std::string &keyRef) const
{
return cardInfo("KLEO-FPR-" + keyRef);
......
......@@ -37,8 +37,6 @@ public:
static const std::vector<KeyPairInfo> & supportedKeys();
static QString keyDisplayName(const std::string &keyRef);
void setCardInfo(const std::vector< std::pair<std::string, std::string> > &infos);
std::string keyFingerprint(const std::string &keyRef) const;
bool operator == (const Card& other) const override;
......
......@@ -28,18 +28,6 @@ std::string P15Card::appKeyFingerprint(const std::string &appKeyRef) const
return cardInfo("KLEO-FPR-" + appKeyRef);
}
void P15Card::setCardInfo(const std::vector< std::pair<std::string, std::string> > &infos)
{
// XXX: This is a copy of OpenPGPCard::setCardInfo
qCDebug(KLEOPATRA_LOG) << "Card" << serialNumber().c_str() << "info:";
for (const auto &pair: infos) {
qCDebug(KLEOPATRA_LOG) << pair.first.c_str() << ":" << pair.second.c_str();
if (parseCardInfo(pair.first, pair.second)) {
continue;
}
}
}
void P15Card::setManufacturer(const std::string &manufacturer)
{
mManufacturer = manufacturer;
......
......@@ -41,7 +41,6 @@ public:
* OpenPGPCard::pgpSigKeyRef */
std::string appKeyFingerprint(const std::string &appKeyRef) const;
void setCardInfo(const std::vector< std::pair<std::string, std::string> > &infos);
void setManufacturer(const std::string &manufacturer);
std::string manufacturer() const;
......
......@@ -114,17 +114,6 @@ std::vector< std::pair<std::string, QString> > PIVCard::supportedAlgorithms(cons
};
}
void PIVCard::setCardInfo(const std::vector< std::pair<std::string, std::string> > &infos)
{
qCDebug(KLEOPATRA_LOG) << "Card" << serialNumber().c_str() << "info:";
for (const auto &pair: infos) {
qCDebug(KLEOPATRA_LOG) << pair.first.c_str() << ":" << pair.second.c_str();
if (parseCardInfo(pair.first, pair.second)) {
continue;
}
}
}
std::string PIVCard::keyAlgorithm(const std::string &keyRef) const
{
return cardInfo("KLEO-KEYALGO-" + keyRef);
......
......@@ -36,8 +36,6 @@ public:
static QString keyDisplayName(const std::string &keyRef);
static std::vector< std::pair<std::string, QString> > supportedAlgorithms(const std::string &keyRef);
void setCardInfo(const std::vector< std::pair<std::string, std::string> > &infos);
std::string keyAlgorithm(const std::string &keyRef) const;
void setKeyAlgorithm(const std::string &keyRef, const std::string &algorithm);
......
Markdown is supported
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