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

Add function for retrieving a list of available smart card readers

Extracted from ReaderStatus in kleopatra.

GnuPG-bug-id: 5666
parent 34ba6f99
......@@ -53,6 +53,7 @@ target_sources(KF5Libkleo PRIVATE
utils/hex.cpp
utils/compat.cpp
utils/cryptoconfig.cpp
utils/scdaemon.cpp
utils/stringutils.cpp
utils/test.cpp
)
......@@ -184,6 +185,7 @@ ecm_generate_headers(libkleo_CamelCase_utils_HEADERS
Formatting
GnuPG
Compat
SCDaemon
StringUtils
Test
REQUIRED_HEADERS libkleo_utils_HEADERS
......
/*
utils/scdaemon.cpp
This file is part of libkleopatra
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <config-libkleo.h>
#include "scdaemon.h"
#include "utils/assuan.h"
#include "utils/hex.h"
#include "utils/stringutils.h"
#if __has_include(<QGpgME/Debug>)
# include <QGpgME/Debug>
#endif
#include <gpgme++/context.h>
#include "libkleo_debug.h"
using namespace Kleo;
using namespace GpgME;
std::vector<std::string> Kleo::SCDaemon::getReaders(Error &err)
{
std::vector<std::string> result;
auto c = Context::createForEngine(AssuanEngine, &err);
if (err) {
qCDebug(LIBKLEO_LOG) << "Creating context for Assuan engine failed:" << err;
return result;
}
auto assuanContext = std::shared_ptr<Context>(c.release());
const std::string command = "SCD GETINFO reader_list";
const auto readersData = Assuan::sendDataCommand(assuanContext, command.c_str(), err);
if (err) {
return result;
}
const auto readers = hexdecode(readersData);
result = split(readers, '\n');
// remove empty entries; in particular, the last entry
result.erase(std::remove_if(std::begin(result), std::end(result),
std::mem_fn(&std::string::empty)),
std::end(result));
return result;
}
/*
utils/scdaemon.h
This file is part of libkleopatra
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include <string>
#include <vector>
#include "kleo_export.h"
namespace GpgME
{
class Error;
}
namespace Kleo
{
/** This namespace collects higher-level functions for retrieving information
* from the GnuPG smart card daemon. */
namespace SCDaemon
{
/** Returns the list of available smart card readers. If an error occurred,
* then @p err provides details.
* The returned strings are mostly useful for configuring the reader to use
* via the reader-port option of scdaemon.
*/
KLEO_EXPORT std::vector<std::string> getReaders(GpgME::Error &err);
}
}
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