Warning, file /utilities/kgpg/core/convert.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 SPDX-FileCopyrightText: 2006 Jimmy Gilles <jimmygilles@gmail.com> 0003 SPDX-FileCopyrightText: 2010, 2013, 2014, 2016, 2017 Rolf Eike Beer <kde@opensource.sf-tec.de> 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "convert.h" 0008 0009 0010 #include "kgpg_general_debug.h" 0011 0012 #include "kgpgsettings.h" 0013 0014 // Backport of gpgme enums from version 1.7.0 0015 #ifndef GPGME_PK_EDDSA 0016 #define GPGME_PK_EDDSA 303 0017 #endif 0018 #ifndef GPGME_PK_ECC 0019 #define GPGME_PK_ECC 18 0020 #endif 0021 0022 /* Conversion from OpenPGP algorithm number to GPGME algorithm numbers. */ 0023 static int _gpgme_map_pk_algo (int algo) 0024 { 0025 switch (algo) 0026 { 0027 case 18: algo = GPGME_PK_ECDH; break; 0028 case 19: algo = GPGME_PK_ECDSA; break; 0029 case 22: algo = GPGME_PK_EDDSA; break; 0030 default: break; 0031 } 0032 return algo; 0033 } 0034 0035 namespace KgpgCore 0036 { 0037 0038 namespace Convert 0039 { 0040 0041 QString toString(const KgpgKeyAlgo algorithm) 0042 { 0043 switch (algorithm) { 0044 case ALGO_RSA: 0045 return i18nc("Encryption algorithm", "RSA"); 0046 case ALGO_DSA: 0047 return i18nc("Encryption algorithm", "DSA"); 0048 case ALGO_ELGAMAL: 0049 return i18nc("Encryption algorithm", "ElGamal"); 0050 case ALGO_DSA_ELGAMAL: 0051 return i18nc("Encryption algorithm", "DSA & ElGamal"); 0052 case ALGO_RSA_RSA: 0053 return i18nc("Encryption algorithm RSA, Signing algorithm RSA", "RSA & RSA"); 0054 case ALGO_ECC: 0055 return i18nc("Encryption algorithm", "ECC"); 0056 case ALGO_ECDSA: 0057 return i18nc("Signing algorithm", "ECDSA"); 0058 case ALGO_ECDH: 0059 return i18nc("Encryption algorithm", "ECDH"); 0060 case ALGO_EDDSA: 0061 return i18nc("Signing algorithm", "EdDSA"); 0062 case ALGO_UNKNOWN: 0063 default: 0064 return i18nc("Unknown algorithm", "Unknown"); 0065 } 0066 } 0067 0068 QString toString(const gpgme_validity_t ownertrust) 0069 { 0070 switch (ownertrust) { 0071 case GPGME_VALIDITY_UNDEFINED: 0072 return i18n("I do not know"); 0073 case GPGME_VALIDITY_NEVER: 0074 return i18n("I do NOT trust"); 0075 case GPGME_VALIDITY_MARGINAL: 0076 return i18n("Marginally"); 0077 case GPGME_VALIDITY_FULL: 0078 return i18n("Fully"); 0079 case GPGME_VALIDITY_ULTIMATE: 0080 return i18n("Ultimately"); 0081 case GPGME_VALIDITY_UNKNOWN: 0082 default: 0083 return i18nc("Unknown trust in key owner", "Unknown"); 0084 } 0085 } 0086 0087 QString toString(const KgpgKeyTrust trust) 0088 { 0089 switch (trust) { 0090 case TRUST_INVALID: 0091 return i18nc("Invalid key", "Invalid"); 0092 case TRUST_DISABLED: 0093 return i18nc("Disabled key", "Disabled"); 0094 case TRUST_REVOKED: 0095 return i18n("Revoked"); 0096 case TRUST_EXPIRED: 0097 return i18nc("Expired key", "Expired"); 0098 case TRUST_UNDEFINED: 0099 return i18nc("Undefined key trust", "Undefined"); 0100 case TRUST_NONE: 0101 return i18nc("No trust in key", "None"); 0102 case TRUST_MARGINAL: 0103 return i18nc("Marginal trust in key", "Marginal"); 0104 case TRUST_FULL: 0105 return i18nc("Full trust in key", "Full"); 0106 case TRUST_ULTIMATE: 0107 return i18nc("Ultimate trust in key", "Ultimate"); 0108 case TRUST_UNKNOWN: 0109 default: 0110 return i18nc("Unknown trust in key", "Unknown"); 0111 } 0112 } 0113 0114 QString toString(const KgpgCore::KgpgSubKeyType type) 0115 { 0116 QStringList res; 0117 0118 if (type & SKT_SIGNATURE) 0119 res << i18nc("key capability", "Signature"); 0120 if (type & SKT_ENCRYPTION) 0121 res << i18nc("key capability", "Encryption"); 0122 if (type & SKT_AUTHENTICATION) 0123 res << i18nc("key capability", "Authentication"); 0124 if (type & SKT_CERTIFICATION) 0125 res << i18nc("key capability", "Certification"); 0126 0127 return res.join(i18nc("used to join a list of key types, e.g. 'encryption, signature'", ", ")); 0128 } 0129 0130 KgpgKeyAlgo toAlgo(const uint v) 0131 { 0132 uint gpgme_algo = _gpgme_map_pk_algo(v); 0133 switch (gpgme_algo) { 0134 case GPGME_PK_RSA: 0135 return ALGO_RSA; 0136 case GPGME_PK_ELG_E: 0137 case GPGME_PK_ELG: 0138 return ALGO_ELGAMAL; 0139 case GPGME_PK_DSA: 0140 return ALGO_DSA; 0141 case GPGME_PK_ECC: 0142 return ALGO_ECC; 0143 case GPGME_PK_ECDSA: 0144 return ALGO_ECDSA; 0145 case GPGME_PK_ECDH: 0146 return ALGO_ECDH; 0147 case GPGME_PK_EDDSA: 0148 return ALGO_EDDSA; 0149 default: 0150 return ALGO_UNKNOWN; 0151 } 0152 } 0153 0154 KgpgKeyAlgo toAlgo(const QString &s) 0155 { 0156 bool b; 0157 unsigned int u = s.toUInt(&b); 0158 return b ? toAlgo(u) : ALGO_UNKNOWN; 0159 } 0160 0161 KgpgKeyTrust toTrust(const QChar &c) 0162 { 0163 switch (c.toLatin1()) { 0164 case 'o': 0165 return TRUST_UNKNOWN; 0166 case 'i': 0167 return TRUST_INVALID; 0168 case 'd': 0169 return TRUST_DISABLED; 0170 case 'r': 0171 return TRUST_REVOKED; 0172 case 'e': 0173 return TRUST_EXPIRED; 0174 case 'q': 0175 return TRUST_UNDEFINED; 0176 case 'n': 0177 return TRUST_NONE; 0178 case 'm': 0179 return TRUST_MARGINAL; 0180 case 'f': 0181 return TRUST_FULL; 0182 case 'u': 0183 return TRUST_ULTIMATE; 0184 default: 0185 return TRUST_UNKNOWN; 0186 } 0187 } 0188 0189 KgpgKeyTrust toTrust(const QString &s) 0190 { 0191 return s.isEmpty() ? TRUST_UNKNOWN : toTrust(s[0]); 0192 } 0193 0194 gpgme_validity_t toOwnerTrust(const QChar &c) 0195 { 0196 switch (c.toLatin1()) { 0197 case 'n': 0198 return GPGME_VALIDITY_NEVER; 0199 case 'm': 0200 return GPGME_VALIDITY_MARGINAL; 0201 case 'f': 0202 return GPGME_VALIDITY_FULL; 0203 case 'u': 0204 return GPGME_VALIDITY_ULTIMATE; 0205 default: 0206 return GPGME_VALIDITY_UNDEFINED; 0207 } 0208 } 0209 0210 KgpgSubKeyType toSubType(const QString& capString, bool upper) 0211 { 0212 KgpgSubKeyType ret; 0213 0214 for (const QChar &ch : capString) { 0215 switch (ch.toLatin1()) { 0216 case 's': 0217 case 'S': 0218 if (upper != ch.isUpper()) 0219 continue; 0220 ret |= SKT_SIGNATURE; 0221 break; 0222 case 'e': 0223 case 'E': 0224 if (upper != ch.isUpper()) 0225 continue; 0226 ret |= SKT_ENCRYPTION; 0227 break; 0228 case 'a': 0229 case 'A': 0230 if (upper != ch.isUpper()) 0231 continue; 0232 ret |= SKT_AUTHENTICATION; 0233 break; 0234 case 'c': 0235 case 'C': 0236 if (upper != ch.isUpper()) 0237 continue; 0238 ret |= SKT_CERTIFICATION; 0239 break; 0240 case 'D': // disabled key 0241 case '?': // unknown to GnuPG 0242 continue; 0243 default: 0244 qCDebug(KGPG_LOG_GENERAL) << "unknown capability letter" << ch 0245 << "in cap string" << capString; 0246 } 0247 } 0248 0249 return ret; 0250 } 0251 0252 QDateTime toDateTime(const QString &s) 0253 { 0254 QDateTime ret; 0255 0256 if (s.isEmpty()) 0257 return ret; 0258 0259 if (s.contains(QLatin1Char('T'))) 0260 ret = QDateTime::fromString(s, QLatin1String("yyyyMMddTHHmmss")); 0261 else 0262 ret = QDateTime::fromSecsSinceEpoch(s.toUInt()); 0263 0264 return ret; 0265 } 0266 0267 } // namespace Convert 0268 0269 } // namespace KgpgCore