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

Ensure that we use the correct card app if a card provides multiple apps

To ensure this we always look up the cards by serial number _and_
app name.

GnuPG-bug-id: 5066
parent 7ba9c16d
Pipeline #36144 passed with stage
in 37 minutes and 49 seconds
......@@ -34,7 +34,7 @@ class ChangePinCommand::Private : public CardCommand::Private
return static_cast<ChangePinCommand *>(q);
}
public:
explicit Private(ChangePinCommand *qq, const std::string &serialNumber, QWidget *p);
explicit Private(ChangePinCommand *qq, const std::string &serialNumber, const std::string &appName, QWidget *p);
~Private();
void init();
......@@ -46,6 +46,7 @@ private:
void changePin();
private:
std::string appName;
std::string keyRef;
};
......@@ -61,8 +62,9 @@ const ChangePinCommand::Private *ChangePinCommand::d_func() const
#define d d_func()
#define q q_func()
ChangePinCommand::Private::Private(ChangePinCommand *qq, const std::string &serialNumber, QWidget *p)
ChangePinCommand::Private::Private(ChangePinCommand *qq, const std::string &serialNumber, const std::string &appName_, QWidget *p)
: CardCommand::Private(qq, serialNumber, p)
, appName(appName_)
{
}
......@@ -71,8 +73,8 @@ ChangePinCommand::Private::~Private()
qCDebug(KLEOPATRA_LOG) << "ChangePinCommand::Private::~Private()";
}
ChangePinCommand::ChangePinCommand(const std::string &serialNumber, QWidget *p)
: CardCommand(new Private(this, serialNumber, p))
ChangePinCommand::ChangePinCommand(const std::string &serialNumber, const std::string &appName, QWidget *p)
: CardCommand(new Private(this, serialNumber, appName, p))
{
d->init();
}
......@@ -106,7 +108,7 @@ void ChangePinCommand::Private::changePin()
{
qCDebug(KLEOPATRA_LOG) << "ChangePinCommand::changePin()";
const auto card = SmartCard::ReaderStatus::instance()->getCard<Card>(serialNumber());
const auto card = SmartCard::ReaderStatus::instance()->getCard(serialNumber(), appName);
if (!card) {
error(i18n("Failed to find the smartcard with the serial number: %1", QString::fromStdString(serialNumber())));
finished();
......
......@@ -25,7 +25,7 @@ class ChangePinCommand : public CardCommand
{
Q_OBJECT
public:
explicit ChangePinCommand(const std::string &serialNumber, QWidget *parent);
explicit ChangePinCommand(const std::string &serialNumber, const std::string &appName, QWidget *parent);
~ChangePinCommand() override;
void setKeyRef(const std::string &keyRef);
......
......@@ -50,8 +50,8 @@ class KeyToCardCommand::Private : public CardCommand::Private
return static_cast<KeyToCardCommand *>(q);
}
public:
explicit Private(KeyToCardCommand *qq, const GpgME::Subkey &subkey, const std::string &serialno);
explicit Private(KeyToCardCommand *qq, const std::string &slot, const std::string &serialno);
explicit Private(KeyToCardCommand *qq, const GpgME::Subkey &subkey, const std::string &serialNumber, const std::string &appName);
explicit Private(KeyToCardCommand *qq, const std::string &slot, const std::string &serialNumber, const std::string &appName);
~Private();
private:
......@@ -67,6 +67,7 @@ private:
void authenticationCanceled();
private:
std::string appName;
GpgME::Subkey subkey;
std::string cardSlot;
bool overwriteExistingAlreadyApproved = false;
......@@ -88,14 +89,18 @@ const KeyToCardCommand::Private *KeyToCardCommand::d_func() const
KeyToCardCommand::Private::Private(KeyToCardCommand *qq,
const GpgME::Subkey &subkey_,
const std::string &serialno)
: CardCommand::Private(qq, serialno, nullptr),
subkey(subkey_)
const std::string &serialNumber,
const std::string &appName_
)
: CardCommand::Private(qq, serialNumber, nullptr)
, appName(appName_)
, subkey(subkey_)
{
}
KeyToCardCommand::Private::Private(KeyToCardCommand *qq, const std::string &slot, const std::string &serialno)
: CardCommand::Private(qq, serialno, nullptr)
KeyToCardCommand::Private::Private(KeyToCardCommand *qq, const std::string &slot, const std::string &serialNumber, const std::string &appName_)
: CardCommand::Private(qq, serialNumber, nullptr)
, appName(appName_)
, cardSlot(slot)
{
}
......@@ -108,7 +113,7 @@ void KeyToCardCommand::Private::start()
{
qCDebug(KLEOPATRA_LOG) << "KeyToCardCommand::Private::start()";
const auto card = SmartCard::ReaderStatus::instance()->getCard<Card>(serialNumber());
const auto card = SmartCard::ReaderStatus::instance()->getCard(serialNumber(), appName);
if (!card) {
error(i18n("Failed to find the card with the serial number: %1", QString::fromStdString(serialNumber())));
finished();
......@@ -359,13 +364,13 @@ void KeyToCardCommand::Private::authenticationCanceled()
canceled();
}
KeyToCardCommand::KeyToCardCommand(const GpgME::Subkey &key, const std::string &serialno)
: CardCommand(new Private(this, key, serialno))
KeyToCardCommand::KeyToCardCommand(const GpgME::Subkey &key, const std::string &serialNumber, const std::string &appName)
: CardCommand(new Private(this, key, serialNumber, appName))
{
}
KeyToCardCommand::KeyToCardCommand(const std::string& cardSlot, const std::string &serialno)
: CardCommand(new Private(this, cardSlot, serialno))
KeyToCardCommand::KeyToCardCommand(const std::string& cardSlot, const std::string &serialNumber, const std::string &appName)
: CardCommand(new Private(this, cardSlot, serialNumber, appName))
{
}
......
......@@ -25,8 +25,8 @@ class KeyToCardCommand : public CardCommand
{
Q_OBJECT
public:
KeyToCardCommand(const GpgME::Subkey &key, const std::string &serialno);
KeyToCardCommand(const std::string& cardSlot, const std::string &serialno);
KeyToCardCommand(const GpgME::Subkey &key, const std::string &serialNumber, const std::string &appName);
KeyToCardCommand(const std::string &cardSlot, const std::string &serialNumber, const std::string &appName);
~KeyToCardCommand() override;
static bool supported();
......
......@@ -134,7 +134,7 @@ void SubKeysWidget::Private::tableContextMenuRequested(const QPoint &p)
menu->addAction(QIcon::fromTheme(QStringLiteral("send-to-symbolic")),
i18n("Transfer to smartcard"),
q, [this, subkey, card]() {
auto cmd = new Kleo::Commands::KeyToCardCommand(subkey, card->serialNumber());
auto cmd = new Kleo::Commands::KeyToCardCommand(subkey, card->serialNumber(), card->appName());
ui.subkeysTree->setEnabled(false);
connect(cmd, &Kleo::Commands::KeyToCardCommand::finished,
q, [this]() { ui.subkeysTree->setEnabled(true); });
......
......@@ -934,4 +934,16 @@ std::vector <std::shared_ptr<Card> > ReaderStatus::getCards() const
return d->cardInfos();
}
std::shared_ptr<Card> ReaderStatus::getCard(const std::string &serialNumber, const std::string &appName) const
{
for (const auto &card: d->cardInfos()) {
if (card->serialNumber() == serialNumber && card->appName() == appName) {
qCDebug(KLEOPATRA_LOG) << "ReaderStatus::getCard() - Found card with serial number" << serialNumber << "and app" << appName;
return card;
}
}
qCWarning(KLEOPATRA_LOG) << "ReaderStatus::getCard() - Did not find card with serial number" << serialNumber << "and app" << appName;
return std::shared_ptr<Card>();
}
#include "readerstatus.moc"
......@@ -50,17 +50,12 @@ public:
std::vector<std::shared_ptr<Card> > getCards() const;
std::shared_ptr<Card> getCard(const std::string &serialNumber, const std::string &appName) const;
template <typename T>
std::shared_ptr<T> getCard(const std::string &serialNumber) const
{
for (const auto &card: getCards()) {
if (card->serialNumber() == serialNumber) {
qCDebug(KLEOPATRA_LOG) << "ReaderStatus::getCard() - Found card with serial number" << QString::fromStdString(serialNumber);
return std::dynamic_pointer_cast<T>(card);
}
}
qCDebug(KLEOPATRA_LOG) << "ReaderStatus::getCard() - Did not find card with serial number" << QString::fromStdString(serialNumber);
return std::shared_ptr<T>();
return std::dynamic_pointer_cast<T>(getCard(serialNumber, T::AppName));
}
public Q_SLOTS:
......
......@@ -237,7 +237,7 @@ void PGPCardWidget::setCard(const OpenPGPCard *card)
void PGPCardWidget::doChangePin(const std::string &keyRef)
{
auto cmd = new ChangePinCommand(mRealSerial, this);
auto cmd = new ChangePinCommand(mRealSerial, OpenPGPCard::AppName, this);
this->setEnabled(false);
connect(cmd, &ChangePinCommand::finished,
this, [this]() {
......
......@@ -329,7 +329,7 @@ void PIVCardWidget::importCertificateFromCard(const std::string &keyref)
void PIVCardWidget::writeKeyToCard(const std::string &keyref)
{
auto cmd = new KeyToCardCommand(keyref, mCardSerialNumber);
auto cmd = new KeyToCardCommand(keyref, mCardSerialNumber, PIVCard::AppName);
this->setEnabled(false);
connect(cmd, &KeyToCardCommand::finished,
this, [this]() {
......@@ -341,7 +341,7 @@ void PIVCardWidget::writeKeyToCard(const std::string &keyref)
void PIVCardWidget::changePin(const std::string &keyRef)
{
auto cmd = new ChangePinCommand(mCardSerialNumber, this);
auto cmd = new ChangePinCommand(mCardSerialNumber, PIVCard::AppName, this);
this->setEnabled(false);
connect(cmd, &ChangePinCommand::finished,
this, [this]() {
......
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