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

Parse key pair info for NetKey cards using the base class

GnuPG-bug-id: 5128
parent 7519910b
......@@ -8,6 +8,8 @@
#include "netkeycard.h"
#include "keypairinfo.h"
#include "kleopatra_debug.h"
#include <gpgme++/error.h>
......@@ -25,18 +27,12 @@ const std::string NetKeyCard::AppName = "nks";
namespace
{
static std::string parse_keypairinfo(const std::string &kpi)
{
static const char hexchars[] = "0123456789abcdefABCDEF";
return '&' + kpi.substr(0, kpi.find_first_not_of(hexchars));
}
static GpgME::Key parse_keypairinfo_and_lookup_key(GpgME::Context *ctx, const std::string &kpi)
static GpgME::Key lookup_key(GpgME::Context *ctx, const std::string &keyGrip)
{
if (!ctx) {
if (!ctx || keyGrip.empty()) {
return GpgME::Key();
}
const std::string pattern = parse_keypairinfo(kpi);
const std::string pattern = '&' + keyGrip;
qCDebug(KLEOPATRA_LOG) << "parse_keypairinfo_and_lookup_key: pattern=" << pattern.c_str();
if (const auto err = ctx->startKeyListing(pattern.c_str())) {
qCDebug(KLEOPATRA_LOG) << "parse_keypairinfo_and_lookup_key: startKeyListing failed:" << err.asString();
......@@ -57,7 +53,17 @@ NetKeyCard::NetKeyCard(const Card &card)
setAppName(AppName);
}
void NetKeyCard::setKeyPairInfo(const std::vector<std::string> &infos)
void NetKeyCard::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);
}
setKeyPairInfo(keyInfos());
}
void NetKeyCard::setKeyPairInfo(const std::vector<KeyPairInfo> &infos)
{
// check that any of the keys are new
const std::unique_ptr<GpgME::Context> klc(GpgME::Context::createForProtocol(GpgME::CMS));
......@@ -70,7 +76,7 @@ void NetKeyCard::setKeyPairInfo(const std::vector<std::string> &infos)
setCanLearnKeys(false);
mKeys.clear();
for (const auto &info: infos) {
const auto key = parse_keypairinfo_and_lookup_key(klc.get(), info);
const auto key = lookup_key(klc.get(), info.grip);
if (key.isNull()) {
setCanLearnKeys(true);
}
......
......@@ -8,7 +8,6 @@
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "card.h"
#include <gpgme++/key.h>
......@@ -17,6 +16,8 @@ namespace Kleo
{
namespace SmartCard
{
struct KeyPairInfo;
/** Class to work with NetKey smartcards or compatible tokens */
class NetKeyCard: public Card
{
......@@ -25,13 +26,16 @@ public:
static const std::string AppName;
void setKeyPairInfo(const std::vector<std::string> &infos);
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 setKeyPairInfo(const std::vector<KeyPairInfo> &infos);
private:
std::vector <GpgME::Key> mKeys;
};
......
......@@ -558,21 +558,12 @@ static void handle_netkey_card(std::shared_ptr<Card> &ci, std::shared_ptr<Contex
}
nkCard->setPinStates(states);
// check for keys to learn:
const std::unique_ptr<DefaultAssuanTransaction> result = gpgagent_default_transact(gpg_agent, "SCD LEARN --keypairinfo", err);
if (err.code() || !result.get()) {
if (err) {
ci->setErrorMsg(QString::fromLatin1(err.asString()));
} else {
ci->setErrorMsg(QStringLiteral("Invalid internal state. No result."));
}
return;
}
const std::vector<std::string> keyPairInfos = result->statusLine("KEYPAIRINFO");
if (keyPairInfos.empty()) {
const auto info = gpgagent_statuslines(gpg_agent, "SCD LEARN --force", err);
if (err) {
ci->setStatus(Card::CardError);
return;
}
nkCard->setKeyPairInfo(keyPairInfos);
nkCard->setCardInfo(info);
}
static std::shared_ptr<Card> get_card_status(const std::string &serialNumber, const std::string &appName, std::shared_ptr<Context> &gpg_agent)
......
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