14#include <QRegularExpression>
37 if (record_type ==
"pub") {
40 if (record_type ==
"sec") {
43 if (record_type ==
"uid") {
46 if (record_type ==
"fpr") {
49 if (record_type ==
"sub") {
52 if (record_type ==
"ssb") {
55 if (record_type ==
"grp") {
74 bool okCreated =
false;
77 current_user.
created = QDateTime::fromSecsSinceEpoch(createdSecs);
80 bool okExpiry =
false;
83 current_user.
expiry = QDateTime::fromSecsSinceEpoch(expirySecs);
101 switch (validity.isEmpty() ?
'\0' : validity.at(0).toLatin1()) {
122 if (!field.contains(QLatin1String(
"\\x"))) {
125 const QByteArray in = field.toUtf8();
126 const auto isHex = [](
char c) {
127 return std::isxdigit(
static_cast<unsigned char>(c)) != 0;
130 out.reserve(in.size());
131 for (
int i = 0; i < in.size(); ++i) {
132 if (in.at(i) ==
'\\' && i + 3 < in.size() && in.at(i + 1) ==
'x' &&
133 isHex(in.at(i + 2)) && isHex(in.at(i + 3))) {
134 out.append(
static_cast<char>(in.mid(i + 2, 2).toInt(
nullptr, 16)));
137 out.append(in.at(i));
140 return QString::fromUtf8(out);
154 if (current_user.
name.isEmpty() || rank > nameRank) {
168 static const QRegularExpression keyIdShape(
169 QStringLiteral(
"^[0-9A-Fa-f]{16}$"));
170 static const QRegularExpression v4FingerprintShape(
171 QStringLiteral(
"^[0-9A-Fa-f]{40}$"));
172 static const QRegularExpression v5v6FingerprintShape(
173 QStringLiteral(
"^[0-9A-Fa-f]{64}$"));
174 const QString &keyId = current_user.
key_id;
175 if (!keyIdShape.match(keyId).hasMatch()) {
180 const bool isV4 = v4FingerprintShape.match(fingerprint).hasMatch() &&
181 fingerprint.endsWith(keyId, Qt::CaseInsensitive);
182 const bool isV5V6 = v5v6FingerprintShape.match(fingerprint).hasMatch() &&
183 fingerprint.startsWith(keyId, Qt::CaseInsensitive);
184 if (isV4 || isV5V6) {
185 current_user.
key_id = fingerprint;
196 QList<UserInfo> users;
201 bool expectPrimaryFingerprint{};
203 bool expectSubkeyFingerprint{};
212 if (!current.key_id.isEmpty()) {
213 users.append(current);
221 expectPrimaryFingerprint =
false;
222 expectSubkeyFingerprint =
false;
231 void startKey(
const QStringList &props,
bool secret) {
235 nameRank = current.name.isEmpty() ? 0 : 1;
236 expectPrimaryFingerprint =
true;
237 expectSubkeyFingerprint =
false;
244 void takeUid(
const QStringList &props) {
245 expectPrimaryFingerprint =
false;
247 expectSubkeyFingerprint =
false;
254 void takeSubkey(
const QStringList &props) {
255 expectPrimaryFingerprint =
false;
259 expectSubkeyFingerprint =
true;
266 void takeFingerprint(
const QStringList &props) {
267 if (expectPrimaryFingerprint) {
269 expectPrimaryFingerprint =
false;
270 }
else if (expectSubkeyFingerprint &&
274 expectSubkeyFingerprint =
false;
282 const QStringList lines =
285 ColonListing listing;
286 for (
const QString &key : lines) {
287 QStringList props = key.split(
':');
293 const QString &record_type = props[0];
301 listing.takeUid(props);
305 listing.takeSubkey(props);
308 listing.takeFingerprint(props);
315 listing.keepCurrent();
316 return listing.users;
322 return holders.size() == 1 ? holders.first() : QString();
332auto bareKey(
const QString &key) -> QString {
333 QString wanted = key.trimmed().toUpper();
334 if (wanted.endsWith(QLatin1Char(
'!'))) {
337 if (wanted.startsWith(QLatin1String(
"0X"))) {
350void addHolder(QStringList &holders,
const QString &primary,
bool holds) {
351 if (holds && !primary.isEmpty() && !holders.contains(primary)) {
357auto blocksHolding(
const QString &output,
const QString &key) -> QStringList {
358 const QString wanted = bareKey(key);
359 if (wanted.isEmpty()) {
362 const bool isId = wanted.size() == 8 || wanted.size() == 16;
365 bool expectPrimary =
false;
369 const auto namesKeyId = [&](
const QStringList &props) {
372 const auto endBlock = [&] {
373 addHolder(holders, primary, holds);
377 const QStringList lines =
379 for (
const QString &line : lines) {
380 const QStringList props = line.split(QLatin1Char(
':'));
387 expectPrimary =
true;
388 holds = holds || namesKeyId(props);
391 expectPrimary =
false;
392 holds = holds || namesKeyId(props);
397 expectPrimary =
false;
static auto newLinesRegex() -> const QRegularExpression &
Returns a regex to match newline characters.
void handlePubSecRecord(const QStringList &props, bool secret, UserInfo ¤t_user)
Start a key from a pub or sec record: key ID, the user ID the record may carry, validity,...
auto parseGpgColonOutput(const QString &output, bool secret) -> QList< UserInfo >
Parse a gpg colon listing into one UserInfo per key.
auto primaryFingerprintOf(const QString &output, const QString &key) -> QString
The primary key fingerprint of the key that key names.
constexpr int GPG_FIELD_KEY_ID
Field index of the key ID.
constexpr int GPG_FIELD_VALIDITY
Field index of the validity letter (u, f, r, ...).
auto blocksHolding(const QString &output, const QString &key) -> QStringList
The primary fingerprints of every key block in output that holds key, as its primary key or as a subk...
constexpr int GPG_FIELD_EXPIRY
Field index of the expiry time; empty for a key without one.
auto uidNameRank(const QString &validity) -> int
How far a uid record's own validity vouches for its name.
constexpr int GPG_FIELD_CREATED
Field index of the creation time, in seconds since the epoch.
auto decodeColonField(const QString &field) -> QString
Decode a field of GPG colon output: gpg writes a colon, a backslash and control characters in a user ...
constexpr int GPG_MIN_FIELDS
Fewest fields a record must have to be read at all.
auto classifyRecord(const QString &record_type) -> GpgRecordType
Classify a record of a gpg colon listing by its first field.
void handleFprRecord(const QStringList &props, UserInfo ¤t_user)
Replace the key ID of the key being built with its fingerprint.
void handleUidRecord(const QStringList &props, UserInfo ¤t_user, int &nameRank)
Handle a uid record in GPG colon output: its name is the key's name unless an earlier uid vouches at ...
constexpr int GPG_FIELD_USERID
Field index of the user ID.
constexpr int GPG_FIELD_FINGERPRINT
Field 9 of an fpr record: the fingerprint, in the slot the user ID occupies in a uid record.
Parsing of gpg --with-colons key listings into UserInfo records.
GpgRecordType
The record types of a gpg --with-colons listing that the parser tells apart; the first field of each ...
Record handlers of the gpg colon-listing parser, declared separately only so the unit tests can call ...
Stores GPG key info including validity, creation date, and more.
bool have_secret
Whether secret key is available (can decrypt with this key).
QStringList uids
Every user ID on the key, revoked ones included, with gpg's colon escapes (such as \x3a) decoded: the...
UserInfo()=default
Construct a UserInfo with default (invalid/disabled) state.
QString key_id
Hexadecimal representation of the GnuPG key identifier.
QDateTime created
Date/time when key was created.
QString name
GPG user ID / full name.
char validity
GnuPG representation of validity. http://git.gnupg.org/cgi-bin/gitweb.cgi?p=gnupg....
QDateTime expiry
Date/time when key expires.
Util: static helpers for the process environment, binary discovery, store paths, URL safety and the l...