File indexing completed on 2024-06-23 05:14:10
0001 /* smartcard/keypairinfo.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 "keypairinfo.h" 0011 0012 #include <QString> 0013 #include <QStringList> 0014 0015 using namespace Kleo::SmartCard; 0016 0017 // static 0018 KeyPairInfo KeyPairInfo::fromStatusLine(const std::string &s) 0019 { 0020 // The format of a KEYPAIRINFO line is 0021 // KEYPAIRINFO <hexgrip> <keyref> [usage] [keytime] [algostr] 0022 // The string s does not contain the leading "KEYPAIRINFO ". 0023 KeyPairInfo info; 0024 const auto values = QString::fromStdString(s).split(QLatin1Char(' ')); 0025 if (values.size() < 2) { 0026 return info; 0027 } 0028 info.grip = values[0].toStdString(); 0029 info.keyRef = values[1].toStdString(); 0030 if (values.size() >= 3) { 0031 info.usage = values[2].toStdString(); 0032 } 0033 if (values.size() >= 4) { 0034 info.keyTime = values[3].toStdString(); 0035 } 0036 if (values.size() >= 5) { 0037 info.algorithm = values[4].toStdString(); 0038 } 0039 return info; 0040 } 0041 0042 bool KeyPairInfo::canAuthenticate() const 0043 { 0044 return usage.find('a') != std::string::npos; 0045 } 0046 0047 bool KeyPairInfo::canCertify() const 0048 { 0049 return usage.find('c') != std::string::npos; 0050 } 0051 0052 bool KeyPairInfo::canEncrypt() const 0053 { 0054 return usage.find('e') != std::string::npos; 0055 } 0056 0057 bool KeyPairInfo::canSign() const 0058 { 0059 return usage.find('s') != std::string::npos; 0060 } 0061 0062 void KeyPairInfo::update(const KeyPairInfo &other) 0063 { 0064 Q_ASSERT(keyRef == other.keyRef); 0065 if (keyRef != other.keyRef) { 0066 return; 0067 } 0068 if (grip != other.grip) { 0069 // reset all infos if the grip changed 0070 grip = other.grip; 0071 usage = std::string(); 0072 keyTime = std::string(); 0073 algorithm = std::string(); 0074 } 0075 // now update all infos from other's infos unless other's infos are empty or not specified 0076 if (!other.usage.empty() && other.usage != "-") { 0077 usage = other.usage; 0078 } 0079 if (!other.keyTime.empty() && other.keyTime != "-") { 0080 keyTime = other.keyTime; 0081 } 0082 if (!other.algorithm.empty() && other.algorithm != "-") { 0083 algorithm = other.algorithm; 0084 } 0085 } 0086 0087 // C++20: Replace with defaulted equality operator 0088 bool KeyPairInfo::operator==(const KeyPairInfo &other) const 0089 { 0090 return keyRef == other.keyRef && grip == other.grip && usage == other.usage && keyTime == other.keyTime && algorithm == other.algorithm; 0091 } 0092 0093 // C++20: Remove 0094 bool KeyPairInfo::operator!=(const KeyPairInfo &other) const 0095 { 0096 return !operator==(other); 0097 }