File indexing completed on 2024-06-23 05:14:11

0001 /*  smartcard/utils.cpp
0002 
0003     This file is part of Kleopatra, the KDE keymanager
0004     SPDX-FileCopyrightText: 2020 g10 Code GmbH
0005     SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "utils.h"
0011 
0012 #include "algorithminfo.h"
0013 #include "netkeycard.h"
0014 #include "openpgpcard.h"
0015 #include "pivcard.h"
0016 
0017 #include <kleopatra_debug.h>
0018 
0019 #include <Libkleo/Algorithm>
0020 #include <Libkleo/Compliance>
0021 #include <Libkleo/GnuPG>
0022 
0023 #include <KLocalizedString>
0024 
0025 #include <QString>
0026 
0027 using namespace Kleo::SmartCard;
0028 
0029 QString Kleo::SmartCard::displayAppName(const std::string &appName)
0030 {
0031     if (appName == NetKeyCard::AppName) {
0032         return i18nc("proper name of a type of smartcard", "NetKey");
0033     } else if (appName == OpenPGPCard::AppName) {
0034         return i18nc("proper name of a type of smartcard", "OpenPGP");
0035     } else if (appName == PIVCard::AppName) {
0036         return i18nc("proper name of a type of smartcard", "PIV");
0037     } else {
0038         return QString::fromStdString(appName);
0039     }
0040 }
0041 
0042 std::vector<AlgorithmInfo> Kleo::SmartCard::getAllowedAlgorithms(const std::vector<AlgorithmInfo> &supportedAlgorithms)
0043 {
0044     std::vector<AlgorithmInfo> result;
0045     result.reserve(supportedAlgorithms.size());
0046     Kleo::copy_if(supportedAlgorithms, std::back_inserter(result), [](const auto &algoInfo) {
0047         return DeVSCompliance::algorithmIsCompliant(algoInfo.id);
0048     });
0049     return result;
0050 }
0051 
0052 std::string Kleo::SmartCard::getPreferredAlgorithm(const std::vector<AlgorithmInfo> &allowedAlgorithms)
0053 {
0054     const auto isAllowedAlgorithm = [&allowedAlgorithms](const std::string &algoId) {
0055         return Kleo::any_of(allowedAlgorithms, [&algoId](const auto &algoInfo) {
0056             return algoInfo.id == algoId;
0057         });
0058     };
0059 
0060     const auto &preferredAlgos = Kleo::preferredAlgorithms();
0061     const auto defaultAlgoIt = Kleo::find_if(preferredAlgos, isAllowedAlgorithm);
0062     if (defaultAlgoIt != preferredAlgos.end()) {
0063         return *defaultAlgoIt;
0064     } else {
0065         qCWarning(KLEOPATRA_LOG) << __func__ << "- No preferred algorithm is allowed. Using first allowed algorithm as default.";
0066         return !allowedAlgorithms.empty() ? allowedAlgorithms.front().id : std::string{};
0067     }
0068 }