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

Work directly with the app names instead of an app type enum

We need the app names anyway for switching to the required card
application when we support multiple cards with multiple apps.

GnuPG-bug-id: 5066
parent 044244a4
......@@ -115,21 +115,15 @@ void KeyToCardCommand::Private::start()
return;
}
switch (card->appType()) {
case SmartCard::Card::OpenPGPApplication: {
if (card->appName() == SmartCard::OpenPGPCard::AppName) {
startKeyToOpenPGPCard();
}
break;
case SmartCard::Card::PivApplication: {
} else if (card->appName() == SmartCard::PIVCard::AppName) {
startKeyToPIVCard();
}
break;
default: {
} else {
error(i18n("Sorry! Transferring keys to this card is not supported."));
finished();
return;
}
}
}
namespace {
......
......@@ -9,6 +9,7 @@
#include "subkeyswidget.h"
#include "ui_subkeyswidget.h"
#include "smartcard/openpgpcard.h"
#include "smartcard/readerstatus.h"
#include "commands/changeexpirycommand.h"
......@@ -125,7 +126,7 @@ void SubKeysWidget::Private::tableContextMenuRequested(const QPoint &p)
if (subkey.isSecret() && Kleo::Commands::KeyToCardCommand::supported()) {
const auto cards = SmartCard::ReaderStatus::instance()->getCards();
if (cards.size() && cards[0]->appType() == SmartCard::Card::OpenPGPApplication) {
if (cards.size() && cards[0]->appName() == SmartCard::OpenPGPCard::AppName) {
const auto card = cards[0];
if (!subkey.cardSerialNumber() || card->serialNumber() != subkey.cardSerialNumber()) {
......
......@@ -16,8 +16,8 @@ using namespace Kleo::SmartCard;
Card::Card(): mCanLearn(false),
mHasNullPin(false),
mStatus(Status::NoCard),
mAppType(UnknownApplication),
mAppVersion(-1) {
mAppVersion(-1)
{
}
void Card::setStatus(Status s)
......@@ -40,14 +40,14 @@ std::string Card::serialNumber() const
return mSerialNumber;
}
Card::AppType Card::appType() const
std::string Card::appName() const
{
return mAppType;
return mAppName;
}
void Card::setAppType(AppType t)
void Card::setAppName(const std::string &name)
{
mAppType = t;
mAppName = name;
}
void Card::setAppVersion(int version)
......@@ -104,7 +104,7 @@ bool Card::operator == (const Card& other) const
{
return mStatus == other.status()
&& mSerialNumber == other.serialNumber()
&& mAppType == other.appType()
&& mAppName == other.appName()
&& mAppVersion == other.appVersion()
&& mPinStates == other.pinStates()
&& mSlot == other.slot()
......
......@@ -22,18 +22,6 @@ namespace SmartCard
class Card
{
public:
enum AppType {
UnknownApplication,
OpenPGPApplication,
PivApplication,
NksApplication,
P15Application,
DinSigApplication,
GeldkarteApplication,
NumAppTypes
};
enum PinState {
UnknownPinState,
NullPin,
......@@ -69,8 +57,8 @@ public:
virtual void setSerialNumber(const std::string &sn);
std::string serialNumber() const;
AppType appType() const;
void setAppType(AppType type);
std::string appName() const;
void setAppName(const std::string &name);
void setAppVersion(int version);
int appVersion() const;
......@@ -95,7 +83,7 @@ private:
bool mHasNullPin;
Status mStatus;
std::string mSerialNumber;
AppType mAppType;
std::string mAppName;
int mAppVersion;
std::vector<PinState> mPinStates;
int mSlot;
......
......@@ -20,6 +20,9 @@
using namespace Kleo;
using namespace Kleo::SmartCard;
// static
const std::string NetKeyCard::AppName = "nks";
namespace
{
static std::string parse_keypairinfo(const std::string &kpi)
......@@ -50,7 +53,7 @@ static GpgME::Key parse_keypairinfo_and_lookup_key(GpgME::Context *ctx, const st
NetKeyCard::NetKeyCard()
{
setAppType(Card::NksApplication);
setAppName(AppName);
}
void NetKeyCard::setKeyPairInfo(const std::vector<std::string> &infos)
......
#ifndef SMARTCARD_NETKEYCARD_H
#define SMARTCARD_NETKEYCARD_H
/* smartcard/openpgpcard.h
/* smartcard/netkeycard.h
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2017 Intevation GmbH
......@@ -17,13 +17,15 @@ namespace Kleo
{
namespace SmartCard
{
/** Class to work with OpenPGP Smartcards or compatible tokens */
/** Class to work with NetKey smartcards or compatible tokens */
class NetKeyCard: public Card
{
public:
NetKeyCard ();
NetKeyCard();
void setKeyPairInfo (const std::vector<std::string> &infos);
static const std::string AppName;
void setKeyPairInfo(const std::vector<std::string> &infos);
bool hasSigGNullPin() const;
bool hasNKSNullPin() const;
......
......@@ -26,9 +26,12 @@
using namespace Kleo;
using namespace Kleo::SmartCard;
// static
const std::string OpenPGPCard::AppName = "openpgp";
OpenPGPCard::OpenPGPCard()
{
setAppType(Card::OpenPGPApplication);
setAppName(AppName);
}
OpenPGPCard::OpenPGPCard(const std::string &serialno): OpenPGPCard()
......
......@@ -17,12 +17,14 @@ namespace Kleo
{
namespace SmartCard
{
/** Class to work with OpenPGP Smartcards or compatible tokens */
/** Class to work with OpenPGP smartcards or compatible tokens */
class OpenPGPCard: public Card
{
public:
OpenPGPCard ();
OpenPGPCard (const std::string &serialno);
OpenPGPCard();
OpenPGPCard(const std::string &serialno);
static const std::string AppName;
static std::string pinKeyRef();
static std::string adminPinKeyRef();
......
......@@ -18,9 +18,12 @@
using namespace Kleo;
using namespace Kleo::SmartCard;
// static
const std::string PIVCard::AppName = "piv";
PIVCard::PIVCard()
{
setAppType(Card::PivApplication);
setAppName(AppName);
}
PIVCard::PIVCard(const std::string &serialno): PIVCard()
......
......@@ -21,8 +21,10 @@ namespace SmartCard
class PIVCard: public Card
{
public:
PIVCard ();
PIVCard (const std::string &serialno);
PIVCard();
PIVCard(const std::string &serialno);
static const std::string AppName;
static std::string pivAuthenticationKeyRef();
static std::string cardAuthenticationKeyRef();
......
......@@ -106,31 +106,6 @@ static QDebug operator<<(QDebug s, const std::vector< std::pair<std::string, std
return s << ')';
}
static const char *app_types[] = {
"_", // will hopefully never be used as an app-type :)
"openpgp",
"piv",
"nks",
"p15",
"dinsig",
"geldkarte",
};
static_assert(sizeof app_types / sizeof * app_types == Card::NumAppTypes, "");
static Card::AppType parse_app_type(const std::string &s)
{
qCDebug(KLEOPATRA_LOG) << "parse_app_type(" << s.c_str() << ")";
const char **it = std::find_if(std::begin(app_types), std::end(app_types),
[&s](const char *type) {
return ::strcasecmp(s.c_str(), type) == 0;
});
if (it == std::end(app_types)) {
qCDebug(KLEOPATRA_LOG) << "App type not found";
return Card::UnknownApplication;
}
return static_cast<Card::AppType>(it - std::begin(app_types));
}
static int parse_app_version(const std::string &s)
{
return std::atoi(s.c_str());
......@@ -470,27 +445,27 @@ static std::shared_ptr<Card> get_card_status(unsigned int slot, std::shared_ptr<
}
ci->setStatus(Card::CardPresent);
const auto verbatimType = scd_getattr_status(gpg_agent, "APPTYPE", err);
ci->setAppType(parse_app_type(verbatimType));
const auto appName = scd_getattr_status(gpg_agent, "APPTYPE", err);
ci->setAppName(appName);
if (err.code()) {
return ci;
}
// Handle different card types
if (ci->appType() == Card::NksApplication) {
if (ci->appName() == NetKeyCard::AppName) {
qCDebug(KLEOPATRA_LOG) << "get_card_status: found Netkey card" << ci->serialNumber().c_str() << "end";
handle_netkey_card(ci, gpg_agent);
return ci;
} else if (ci->appType() == Card::OpenPGPApplication) {
} else if (ci->appName() == OpenPGPCard::AppName) {
qCDebug(KLEOPATRA_LOG) << "get_card_status: found OpenPGP card" << ci->serialNumber().c_str() << "end";
handle_openpgp_card(ci, gpg_agent);
return ci;
} else if (ci->appType() == Card::PivApplication) {
} else if (ci->appName() == PIVCard::AppName) {
qCDebug(KLEOPATRA_LOG) << "get_card_status: found PIV card" << ci->serialNumber().c_str() << "end";
handle_piv_card(ci, gpg_agent);
return ci;
} else {
qCDebug(KLEOPATRA_LOG) << "get_card_status: unhandled application:" << verbatimType.c_str();
qCDebug(KLEOPATRA_LOG) << "get_card_status: unhandled application:" << appName.c_str();
return ci;
}
......
......@@ -113,13 +113,13 @@ public:
void setCard(std::shared_ptr<Card> card)
{
if (card->appType() == Card::OpenPGPApplication) {
if (card->appName() == SmartCard::OpenPGPCard::AppName) {
mPGPCardWidget->setCard(static_cast<OpenPGPCard *> (card.get()));
mStack->setCurrentWidget(mPGPCardWidget);
} else if (card->appType() == Card::NksApplication) {
} else if (card->appName() == SmartCard::NetKeyCard::AppName) {
mNetKeyWidget->setCard(static_cast<NetKeyCard *> (card.get()));
mStack->setCurrentWidget(mNetKeyWidget);
} else if (card->appType() == Card::PivApplication) {
} else if (card->appName() == SmartCard::PIVCard::AppName) {
mPIVCardWidget->setCard(static_cast<PIVCard *> (card.get()));
mStack->setCurrentWidget(mPIVCardWidget);
} else {
......
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