Move NKS card into card based subclass

Now that we support more then one card card specific code
should be in a respective subclass.
This also fixes PIN state parsing which currently only
worked by accident.
parent 3ad0b2a1
......@@ -221,6 +221,7 @@ set(_kleopatra_SRCS
smartcard/readerstatus.cpp
smartcard/card.cpp
smartcard/openpgpcard.cpp
smartcard/netkeycard.cpp
aboutdata.cpp
systrayicon.cpp
......
/* smartcard/netkeycard.cpp
This file is part of Kleopatra, the KDE keymanager
Copyright (c) 2017 Intevation GmbH
Kleopatra is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Kleopatra is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
In addition, as a special exception, the copyright holders give
permission to link the code of this program with any edition of
the Qt library by Trolltech AS, Norway (or with modified versions
of Qt that use the same license as Qt), and distribute linked
combinations including the two. You must obey the GNU General
Public License in all respects for all of the code used other than
Qt. If you modify this file, you may extend this exception to
your version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from
your version.
*/
#include "netkeycard.h"
#include "kleopatra_debug.h"
#include <gpgme++/error.h>
#include <gpgme++/context.h>
#include <gpgme++/keylistresult.h>
#include <memory>
#include <string>
using namespace Kleo;
using namespace Kleo::SmartCard;
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)
{
if (!ctx) {
return GpgME::Key();
}
const std::string pattern = parse_keypairinfo(kpi);
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();
return GpgME::Key();
}
GpgME::Error e;
const auto key = ctx->nextKey(e);
ctx->endKeyListing();
qCDebug(KLEOPATRA_LOG) << "parse_keypairinfo_and_lookup_key: e=" << e.code() << "; key.isNull()" << key.isNull();
return key;
}
} // namespace
NetKeyCard::NetKeyCard()
{
setAppType(Card::NksApplication);
}
void NetKeyCard::setKeyPairInfo(const std::vector<std::string> &infos)
{
// check that any of the keys are new
const std::unique_ptr<GpgME::Context> klc(GpgME::Context::createForProtocol(GpgME::CMS));
if (!klc.get()) {
return;
}
klc->setKeyListMode(GpgME::Ephemeral);
klc->addKeyListMode(GpgME::Validate);
setCanLearnKeys(false);
mKeys.clear();
for (const auto &info: infos) {
const auto key = parse_keypairinfo_and_lookup_key(klc.get(), info);
if (key.isNull()) {
setCanLearnKeys(true);
}
mKeys.push_back(key);
}
}
// State 0 -> NKS PIN Retry counter
// State 1 -> NKS PUK Retry counter
// State 2 -> SigG PIN Retry counter
// State 3 -> SigG PUK Retry counter
bool NetKeyCard::hasNKSNullPin() const
{
const auto states = pinStates();
if (states.size() < 2) {
qCWarning(KLEOPATRA_LOG) << "Invalid size of pin states:" << states.size();
}
return states[0] == Card::NullPin;
}
bool NetKeyCard::hasSigGNullPin() const
{
const auto states = pinStates();
if (states.size() < 4) {
qCWarning(KLEOPATRA_LOG) << "Invalid size of pin states:" << states.size();
}
return states[2] == Card::NullPin;
}
std::vector<GpgME::Key> NetKeyCard::keys() const
{
return mKeys;
}
#ifndef SMARTCARD_NETKEYCARD_H
#define SMARTCARD_NETKEYCARD_H
/* smartcard/openpgpcard.h
This file is part of Kleopatra, the KDE keymanager
Copyright (c) 2017 Intevation GmbH
Kleopatra is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Kleopatra is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
In addition, as a special exception, the copyright holders give
permission to link the code of this program with any edition of
the Qt library by Trolltech AS, Norway (or with modified versions
of Qt that use the same license as Qt), and distribute linked
combinations including the two. You must obey the GNU General
Public License in all respects for all of the code used other than
Qt. If you modify this file, you may extend this exception to
your version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from
your version.
*/
#include <QMap>
#include "card.h"
#include <gpgme++/key.h>
namespace Kleo
{
namespace SmartCard
{
/** Class to work with OpenPGP Smartcards or compatible tokens */
class NetKeyCard: public Card
{
public:
NetKeyCard ();
void setKeyPairInfo (const std::vector<std::string> &infos);
bool hasSigGNullPin() const;
bool hasNKSNullPin() const;
std::vector <GpgME::Key> keys() const;
private:
std::vector <GpgME::Key> mKeys;
};
} // namespace Smartcard
} // namespace Kleopatra
#endif // SMARTCARD_CARD_H
......@@ -42,12 +42,12 @@
#include <gpgme++/context.h>
#include <gpgme++/defaultassuantransaction.h>
#include <gpgme++/key.h>
#include <gpgme++/keylistresult.h>
#include <gpg-error.h>
#include "kleopatra_debug.h"
#include "openpgpcard.h"
#include "netkeycard.h"
#include <QStringList>
#include <QDir>
......@@ -157,6 +157,7 @@ static Card::PinState parse_pin_state(const QString &s)
bool ok;
int i = s.toInt(&ok);
if (!ok) {
qCDebug(KLEOPATRA_LOG) << "Failed to parse pin state" << s;
return Card::UnknownPinState;
}
switch (i) {
......@@ -235,30 +236,6 @@ static const std::string gpgagent_data(std::shared_ptr<Context> &gpgAgent, const
}
}
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 bool parse_keypairinfo_and_lookup_key(Context *ctx, const std::string &kpi)
{
if (!ctx) {
return false;
}
const std::string pattern = parse_keypairinfo(kpi);
qCDebug(KLEOPATRA_LOG) << "parse_keypairinfo_and_lookup_key: pattern=" << pattern.c_str();
if (const Error err = ctx->startKeyListing(pattern.c_str())) {
qCDebug(KLEOPATRA_LOG) << "parse_keypairinfo_and_lookup_key: startKeyListing failed:" << err.asString();
return false;
}
Error e;
const Key key = ctx->nextKey(e);
ctx->endKeyListing();
qCDebug(KLEOPATRA_LOG) << "parse_keypairinfo_and_lookup_key: e=" << e.code() << "; key.isNull()" << key.isNull();
return !e && !key.isNull();
}
static void handle_openpgp_card(std::shared_ptr<Card> &ci, std::shared_ptr<Context> &gpg_agent)
{
Error err;
......@@ -277,14 +254,17 @@ static void handle_openpgp_card(std::shared_ptr<Card> &ci, std::shared_ptr<Conte
static void handle_netkey_card(std::shared_ptr<Card> &ci, std::shared_ptr<Context> &gpg_agent)
{
Error err;
ci->setAppVersion(parse_app_version(scd_getattr_status(gpg_agent, "NKS-VERSION", err)));
if (err.code() || ci->appVersion() != 3) {
auto ret = new NetKeyCard();
ret->setSerialNumber(ci->serialNumber());
ret->setAppVersion(parse_app_version(scd_getattr_status(gpg_agent, "NKS-VERSION", err)));
if (err.code() || ret->appVersion() != 3) {
qCDebug(KLEOPATRA_LOG) << "not a NetKey v3 card, giving up";
return;
}
// the following only works for NKS v3...
const auto chvStatus = QString::fromStdString(
scd_getattr_status(gpg_agent, "CHV-STATUS", err)).split(QStringLiteral(" \t"));
scd_getattr_status(gpg_agent, "CHV-STATUS", err)).split(QStringLiteral(" "));
if (err.code()) {
return;
}
......@@ -294,10 +274,10 @@ static void handle_netkey_card(std::shared_ptr<Card> &ci, std::shared_ptr<Contex
const auto parsed = parse_pin_state (state);
states.push_back(parsed);
if (parsed == Card::NullPin) {
ci->setHasNullPin(true);
ret->setHasNullPin(true);
}
}
ci->setPinStates(states);
ret->setPinStates(states);
// check for keys to learn:
const std::unique_ptr<DefaultAssuanTransaction> result = gpgagent_transact(gpg_agent, "SCD LEARN --keypairinfo", err);
......@@ -308,21 +288,8 @@ static void handle_netkey_card(std::shared_ptr<Card> &ci, std::shared_ptr<Contex
if (keyPairInfos.empty()) {
return;
}
// check that any of the keys are new
const std::unique_ptr<Context> klc(Context::createForProtocol(CMS));
if (!klc.get()) {
return;
}
klc->setKeyListMode(Ephemeral);
if (std::any_of(keyPairInfos.cbegin(), keyPairInfos.cend(),
[&klc](const std::string &str) {
return !parse_keypairinfo_and_lookup_key(klc.get(), str);
})) {
ci->setCanLearnKeys(true);
}
return;
ret->setKeyPairInfo(keyPairInfos);
ci.reset(ret);
}
static std::shared_ptr<Card> get_card_status(unsigned int slot, 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