QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
gpgkeystate.cpp File Reference

Implements the colon-listing parser: record classification, one handler per record type, and primary-fingerprint lookup. More...

#include "gpgkeystate.h"
#include "gpgkeystate_p.h"
#include "util.h"
#include <QDateTime>
#include <QRegularExpression>
#include <cctype>
Include dependency graph for gpgkeystate.cpp:

Go to the source code of this file.

Functions

auto classifyRecord (const QString &record_type) -> GpgRecordType
 Classify a record of a gpg colon listing by its first field.
void handlePubSecRecord (const QStringList &props, bool secret, UserInfo &current_user)
 Start a key from a pub or sec record: key ID, the user ID the record may carry, validity, creation and expiry time.
auto uidNameRank (const QString &validity) -> int
 How far a uid record's own validity vouches for its name.
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 ID as \xHH.
void handleUidRecord (const QStringList &props, UserInfo &current_user, int &nameRank)
 Handle a uid record in GPG colon output: its name is the key's name unless an earlier uid vouches at least as well for its own.
void handleFprRecord (const QStringList &props, UserInfo &current_user)
 Replace the key ID of the key being built with its fingerprint.
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.
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 subkey.

Variables

constexpr int GPG_MIN_FIELDS = 10
 Fewest fields a record must have to be read at all.
constexpr int GPG_FIELD_VALIDITY = 1
 Field index of the validity letter (u, f, r, ...).
constexpr int GPG_FIELD_KEY_ID = 4
 Field index of the key ID.
constexpr int GPG_FIELD_CREATED = 5
 Field index of the creation time, in seconds since the epoch.
constexpr int GPG_FIELD_EXPIRY = 6
 Field index of the expiry time; empty for a key without one.
constexpr int GPG_FIELD_USERID = 9
 Field index of the user ID.
constexpr int GPG_FIELD_FINGERPRINT = 9
 Field 9 of an fpr record: the fingerprint, in the slot the user ID occupies in a uid record.

Detailed Description

Implements the colon-listing parser: record classification, one handler per record type, and primary-fingerprint lookup.

Definition in file gpgkeystate.cpp.

Function Documentation

◆ blocksHolding()

auto blocksHolding ( const QString & output,
const QString & key )

The primary fingerprints of every key block in output that holds key, as its primary key or as a subkey.

gpg resolves a fingerprint or key ID among primary keys and subkeys alike, and anyone can bind another person's public key to their own key as a subkey: a .gpg-id line held by two blocks may be encrypted to either.

Parameters
outputAs for primaryFingerprintOf().
keyAs for primaryFingerprintOf(); a trailing ! is ignored.
Returns
The holders' primary fingerprints in upper case, in listing order, each once; empty when no block holds key.

Definition at line 357 of file gpgkeystate.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ classifyRecord()

auto classifyRecord ( const QString & record_type)

Classify a record of a gpg colon listing by its first field.

Parameters
record_typeThe first field of the record (pub, sec, uid, and so on).
Returns
The matching GpgRecordType, or GpgRecordType::Unknown.

Definition at line 36 of file gpgkeystate.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ decodeColonField()

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 ID as \xHH.

Parameters
fieldThe field as gpg listed it
Returns
The field with each \xHH replaced by its byte, read as UTF-8

Definition at line 121 of file gpgkeystate.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handleFprRecord()

void handleFprRecord ( const QStringList & props,
UserInfo & current_user )

Replace the key ID of the key being built with its fingerprint.

Only when the fpr record's value has the expected fingerprint shape and contains the exact 16-hex key ID in its version-specific position: the tail for a 40-hex v4 fingerprint, the head for a 64-hex v5/v6 fingerprint. A subkey's fingerprint therefore cannot replace the primary key's ID.

Parameters
propsThe colon-split fields of the record.
current_userThe key being built; its key ID may be replaced.

Definition at line 160 of file gpgkeystate.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handlePubSecRecord()

void handlePubSecRecord ( const QStringList & props,
bool secret,
UserInfo & current_user )

Start a key from a pub or sec record: key ID, the user ID the record may carry, validity, creation and expiry time.

An empty expiry field (gpg's "no expiry") leaves the date null. A record with fewer than ten fields changes nothing.

Parameters
propsThe colon-split fields of the record.
secretWhether to mark the key as having a secret part.
current_userThe key being built; filled in.

Definition at line 61 of file gpgkeystate.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handleUidRecord()

void handleUidRecord ( const QStringList & props,
UserInfo & current_user,
int & nameRank )

Handle a uid record in GPG colon output: its name is the key's name unless an earlier uid vouches at least as well for its own.

Parameters
propsThe colon-split fields of the record
current_userUserInfo to populate with user name
nameRankuidNameRank() of the name taken so far; updated

Definition at line 150 of file gpgkeystate.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parseGpgColonOutput()

auto parseGpgColonOutput ( const QString & output,
bool secret )

Parse a gpg colon listing into one UserInfo per key.

A pub or sec record starts a key; the first uid after it supplies the name when the key record carried none, and an fpr record whose value ends in the key ID replaces the key ID with the full fingerprint. Lines with fewer than ten fields are skipped.

Parameters
outputRaw output of gpg --with-colons --with-fingerprint.
secretTrue when output lists secret keys (--list-secret-keys); the keys are then marked as having a secret part.
Returns
The keys, in the order gpg listed them.

Definition at line 280 of file gpgkeystate.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ primaryFingerprintOf()

auto primaryFingerprintOf ( const QString & output,
const QString & key )

The primary key fingerprint of the key that key names.

gpg accepts a signature by a subkey for its primary key, and verification here takes either fingerprint from VALIDSIG: a key written as a subkey's fingerprint, or as a key ID, signs as its primary key. Only the block that holds key is taken, whatever else gpg printed; when several do (a short ID, which collides easily), none is.

Parameters
outputRaw output from GPG –with-colons –with-fingerprint –with-subkey-fingerprint –list-keys.
keyA fingerprint (40 or 64 hex) or a key ID (8 or 16 hex), optionally with 0x; any case. A key ID is matched against the record's own key ID, so it works for v4 keys (the fingerprint's tail) and v5/v6 keys (its head) alike.
Returns
The primary fingerprint in upper case, or empty when no block, or more than one, in output holds key.

Definition at line 319 of file gpgkeystate.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ uidNameRank()

auto uidNameRank ( const QString & validity) -> int

How far a uid record's own validity vouches for its name.

The key's validity on the pub record is that of its best uid, so the name shown beside it has to be one that earned it: a certified key whose owner adds an uncertified uid and makes it primary must not show that name as valid.

Parameters
validityThe record's validity field.
Returns
3 for ultimate or full, 2 for marginal, 1 for unknown or undefined, 0 for anything else (invalid, revoked, expired, never).

Definition at line 100 of file gpgkeystate.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ GPG_FIELD_CREATED

int GPG_FIELD_CREATED = 5
constexpr

Field index of the creation time, in seconds since the epoch.

Definition at line 25 of file gpgkeystate.cpp.

◆ GPG_FIELD_EXPIRY

int GPG_FIELD_EXPIRY = 6
constexpr

Field index of the expiry time; empty for a key without one.

Definition at line 27 of file gpgkeystate.cpp.

◆ GPG_FIELD_FINGERPRINT

int GPG_FIELD_FINGERPRINT = 9
constexpr

Field 9 of an fpr record: the fingerprint, in the slot the user ID occupies in a uid record.

Definition at line 34 of file gpgkeystate.cpp.

◆ GPG_FIELD_KEY_ID

int GPG_FIELD_KEY_ID = 4
constexpr

Field index of the key ID.

Definition at line 23 of file gpgkeystate.cpp.

◆ GPG_FIELD_USERID

int GPG_FIELD_USERID = 9
constexpr

Field index of the user ID.

Definition at line 29 of file gpgkeystate.cpp.

◆ GPG_FIELD_VALIDITY

int GPG_FIELD_VALIDITY = 1
constexpr

Field index of the validity letter (u, f, r, ...).

Definition at line 21 of file gpgkeystate.cpp.

◆ GPG_MIN_FIELDS

int GPG_MIN_FIELDS = 10
constexpr

Fewest fields a record must have to be read at all.

Definition at line 19 of file gpgkeystate.cpp.