QtPass Developer documentation
QtPass test coverage
Current view: top level - src/gpgidsigner.h Coverage Total Hit
Test: QtPass Lines: 100.0 % 2 2
Test Date: 2026-12-03 23:34:20 Functions: - 0 0
Branches: 100.0 % 2 2

             Branch data     Line data    Source code
       1                 :             : // SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
       2                 :             : // SPDX-License-Identifier: GPL-3.0-or-later
       3                 :             : /**
       4                 :             :  * @file
       5                 :             :  * @brief GpgIdSigner, which signs a store's `.gpg-id` and verifies an existing
       6                 :             :  * `.gpg-id.sig` against the configured signing keys.
       7                 :             :  */
       8                 :             : #ifndef SRC_GPGIDSIGNER_H_
       9                 :             : #define SRC_GPGIDSIGNER_H_
      10                 :             : 
      11                 :             : #include <QString>
      12                 :             : #include <QStringList>
      13                 :             : #include <functional>
      14                 :             : #include <optional>
      15                 :             : 
      16                 :             : /**
      17                 :             :  * @class GpgIdSigner
      18                 :             :  * @ingroup keys
      19                 :             :  * @brief The detached signature on a store's `.gpg-id`: whether the
      20                 :             :  * configured signing key is usable, signing the file, and checking that an
      21                 :             :  * existing `.gpg-id.sig` was made by one of the configured keys.
      22                 :             :  *
      23                 :             :  * Every gpg run goes through the @c Exec given at construction, so the
      24                 :             :  * re-encryption worker can hand in its cancellable runner and tests a fake.
      25                 :             :  * With no signing keys configured signing is off and verification passes:
      26                 :             :  * `pass` itself only checks signatures when `PASSWORD_STORE_SIGNING_KEY` is
      27                 :             :  * set.
      28                 :             :  */
      29                 :             : class GpgIdSigner {
      30                 :             : public:
      31                 :             :   /**
      32                 :             :    * @brief Most bytes of a .gpg-id.sig verify() hands to gpg.
      33                 :             :    *
      34                 :             :    * A detached signature is well under a KiB; one by a 16384-bit RSA key,
      35                 :             :    * armored, about 3 KiB. The cap also bounds gpg's work: it checks every
      36                 :             :    * signature packet in the file, and one made with a key built to be slow
      37                 :             :    * (a huge public exponent) takes seconds each. 64 KiB held enough of
      38                 :             :    * them to freeze %QtPass for minutes (scan ANT-2026-XQUAU1HG); 4 KiB holds
      39                 :             :    * two.
      40                 :             :    */
      41                 :             :   static constexpr qint64 kMaxSignatureBytes = qint64{4} * 1024;
      42                 :             : 
      43                 :             :   /**
      44                 :             :    * @brief Blocking process runner: Executor::executeBlocking()'s shape.
      45                 :             :    * @return The exit code, or -1 when the process could not run.
      46                 :             :    */
      47                 :             :   using Exec =
      48                 :             :       std::function<int(const QString &app, const QStringList &args,
      49                 :             :                         const QString &input, QString *out, QString *err)>;
      50                 :             : 
      51                 :             :   /**
      52                 :             :    * @brief Create a signer.
      53                 :             :    * @param gpgExecutable The gpg binary; a `wsl ` prefix routes file paths
      54                 :             :    *        through wslpath.
      55                 :             :    * @param signingKeys Full fingerprints from the settings (40 hex characters
      56                 :             :    *        for v4 keys, 64 for v5/v6), as pass's PASSWORD_STORE_SIGNING_KEY
      57                 :             :    *        requires: gpg reports fingerprints in its KEY_CONSIDERED and
      58                 :             :    *        VALIDSIG status lines, so a short key ID would never pass
      59                 :             :    *        haveSecretKey() or verify(). The first one signs, any of them
      60                 :             :    *        verifies.
      61                 :             :    * @param exec Process runner; Executor::executeBlocking() when empty.
      62                 :             :    */
      63                 :             :   GpgIdSigner(QString gpgExecutable, QStringList signingKeys,
      64                 :             :               Exec exec = Exec());
      65                 :             : 
      66                 :             :   /**
      67                 :             :    * @brief The store the lists and signatures this signer reads belong to.
      68                 :             :    *
      69                 :             :    * With a root set, verify() and verifyFile() read their files with
      70                 :             :    * Util::readRegularFileInStore(): no folder on the way down from the
      71                 :             :    * store is followed, so none swapped for a link between a check and the
      72                 :             :    * read leads to another folder's list. Without one, only the last part
      73                 :             :    * of the name is not followed (Util::readRegularFile()).
      74                 :             :    * @param root The store root as configured.
      75                 :             :    */
      76                 :         918 :   void setStoreRoot(const QString &root) { m_storeRoot = root; }
      77                 :             : 
      78                 :             :   /**
      79                 :             :    * @brief Split the settings value into keys, upper-cased the way gpg
      80                 :             :    * prints fingerprints.
      81                 :             :    * @param passSigningKey The space-separated setting.
      82                 :             :    * @return The keys, without empties.
      83                 :             :    */
      84                 :             :   static auto keysFromSetting(const QString &passSigningKey) -> QStringList;
      85                 :             : 
      86                 :             :   /**
      87                 :             :    * @brief The signing key to use: the setting, or with none set, the
      88                 :             :    *        PASSWORD_STORE_SIGNING_KEY exported for pass.
      89                 :             :    *
      90                 :             :    * pass checks `.gpg-id.sig` against that variable. QtPass removed it from
      91                 :             :    * the environment of every pass it ran whenever its own setting was
      92                 :             :    * empty, and checked nothing itself: a list someone appended their key to
      93                 :             :    * was used without a word. An exported key now counts as the setting.
      94                 :             :    * @param passSigningKey The setting.
      95                 :             :    * @return The setting when it names a key, else the exported variable;
      96                 :             :    *         empty when neither does.
      97                 :             :    */
      98                 :             :   static auto settingOrExported(const QString &passSigningKey) -> QString;
      99                 :             : 
     100                 :             :   /**
     101                 :             :    * @brief Whether any signing key is configured at all.
     102                 :             :    * @return true when sign() will do something.
     103                 :             :    */
     104         [ +  + ]:         240 :   auto enabled() const -> bool { return !m_keys.isEmpty(); }
     105                 :             : 
     106                 :             :   /**
     107                 :             :    * @brief The keys this signer was given.
     108                 :             :    * @return The list from the constructor.
     109                 :             :    */
     110                 :             :   auto keys() const -> const QStringList & { return m_keys; }
     111                 :             :   /**
     112                 :             :    * @brief Why the last verify() refused a signature gpg called good, for
     113                 :             :    *        the user: the signing key held by more than one key block, with
     114                 :             :    *        their names and what to do. Empty for any other outcome.
     115                 :             :    * @return The explanation, translated.
     116                 :             :    */
     117                 :             :   auto refusal() const -> QString { return m_refusal; }
     118                 :             : 
     119                 :             :   /**
     120                 :             :    * @brief Whether gpg has the secret key that sign() will use, i.e. the
     121                 :             :    * first configured key. Configured as a subkey, that subkey must sign;
     122                 :             :    * as a primary key, it or one of its subkeys (gpg's --local-user picks
     123                 :             :    * one). Signing means able to, not revoked or expired, and with the
     124                 :             :    * secret part present (not a stub).
     125                 :             :    * @return true when signing can work; false when no key is configured.
     126                 :             :    */
     127                 :             :   auto haveSecretKey() const -> bool;
     128                 :             : 
     129                 :             :   /**
     130                 :             :    * @brief Write `<file>.sig`, a detached signature by the first key over
     131                 :             :    * @p contents, the bytes the caller just wrote as @p gpgIdFile.
     132                 :             :    *
     133                 :             :    * gpg signs what it is fed on stdin, not what is under the file's name by
     134                 :             :    * the time it reads it (a co-writer of the store could have swapped the
     135                 :             :    * list between the write and the signing, and the user's key would have
     136                 :             :    * vouched for theirs), and writes the signature into a directory of
     137                 :             :    * QtPass's own; the signature then goes next to the list through
     138                 :             :    * Util::writeFileReplacing(), never by opening the `.sig` name.
     139                 :             :    *
     140                 :             :    * Only the first key signs, as `--local-user`: gpg then fails when that
     141                 :             :    * secret key is missing, where `--default-key` would quietly sign with
     142                 :             :    * another one. The new signature is verified against @p contents the way
     143                 :             :    * verify() does before it is placed; one that is not by a configured key
     144                 :             :    * is not written.
     145                 :             :    * @param gpgIdFile The `.gpg-id` the signature is for; `<gpgIdFile>.sig`
     146                 :             :    *        is written.
     147                 :             :    * @param contents The exact bytes of that file, as written. UTF-8 text,
     148                 :             :    *        as a recipient list is; anything else is refused, as verify()
     149                 :             :    *        refuses it.
     150                 :             :    * @param error Receives gpg's stderr, or the reason, when it fails.
     151                 :             :    * @return true when the signature is in place and verified. Does nothing
     152                 :             :    *         and returns true when no key is configured.
     153                 :             :    */
     154                 :             :   auto sign(const QString &gpgIdFile, const QByteArray &contents,
     155                 :             :             QString *error = nullptr) const -> bool;
     156                 :             : 
     157                 :             :   /**
     158                 :             :    * @brief Check a detached signature against the given bytes: gpg must
     159                 :             :    * report a VALIDSIG whose key or primary-key fingerprint is one of the
     160                 :             :    * configured keys.
     161                 :             :    *
     162                 :             :    * The data goes to gpg on stdin, so the caller verifies exactly the bytes
     163                 :             :    * it is about to use as the recipient list; verifying a path and reading
     164                 :             :    * it again afterwards would let anyone who can write to the store swap
     165                 :             :    * the file in between (#1842).
     166                 :             :    * @param contents The `.gpg-id` bytes.
     167                 :             :    * @param signatureFile The `.gpg-id.sig` next to it.
     168                 :             :    * @return true when the signature is good, or when no key is configured.
     169                 :             :    */
     170                 :             :   auto verify(const QByteArray &contents, const QString &signatureFile) const
     171                 :             :       -> bool;
     172                 :             : 
     173                 :             :   /**
     174                 :             :    * @brief Read @p gpgIdFile and verify it against `<file>.sig`.
     175                 :             :    * @param gpgIdFile The `.gpg-id`.
     176                 :             :    * @param contents Receives the bytes that were verified, so the caller can
     177                 :             :    *        parse those and nothing else.
     178                 :             :    * @return true when the signature is good, or when no key is configured.
     179                 :             :    *         false when the file cannot be read.
     180                 :             :    */
     181                 :             :   auto verifyFile(const QString &gpgIdFile, QByteArray *contents) const -> bool;
     182                 :             : 
     183                 :             :   /**
     184                 :             :    * @brief Pick the fingerprints out of gpg's `--status-fd` VALIDSIG line.
     185                 :             :    * @param statusOutput gpg's status output.
     186                 :             :    * @return The signing key's fingerprint and the primary key's fingerprint,
     187                 :             :    *         or an empty list when there is no VALIDSIG, or when gpg says the
     188                 :             :    *         key that made that signature is revoked (REVKEYSIG, KEYREVOKED).
     189                 :             :    */
     190                 :             :   static auto validSigFingerprints(const QString &statusOutput) -> QStringList;
     191                 :             : 
     192                 :             : private:
     193                 :             :   /**
     194                 :             :    * @brief Whether exactly one key block holds the key that made a
     195                 :             :    *        signature.
     196                 :             :    *
     197                 :             :    * gpg judges a signature by the block it picks for the signing key, and
     198                 :             :    * reports a revocation only for that block. Someone holding a revoked
     199                 :             :    * signing key's secret could bind it as a subkey of their own key: gpg
     200                 :             :    * could then pick their block, where it is not revoked, and call the
     201                 :             :    * signature good. A signing key two blocks hold is refused, as a
     202                 :             :    * recipient line two blocks hold is (Pass::keyLinesHeldTwice()).
     203                 :             :    * @param signingFingerprint The fingerprint VALIDSIG names first.
     204                 :             :    * @return true when gpg lists one block for it; false for none, more, or
     205                 :             :    *         when gpg cannot list.
     206                 :             :    */
     207                 :             :   auto heldByOneKey(const QString &signingFingerprint) const -> bool;
     208                 :             : 
     209                 :             :   /** @brief The gpg executable, as configured. */
     210                 :             :   QString m_gpg;
     211                 :             :   /**
     212                 :             :    * @brief The configured signing keys. The first one signs; a signature by
     213                 :             :    * any of them verifies.
     214                 :             :    */
     215                 :             :   QStringList m_keys;
     216                 :             :   /// See refusal(); set by the last verify().
     217                 :             :   mutable QString m_refusal;
     218                 :             :   /**
     219                 :             :    * @brief Read a list or signature of the store: under m_storeRoot when
     220                 :             :    *        set (see setStoreRoot()).
     221                 :             :    * @param path The file.
     222                 :             :    * @param maxBytes The most bytes accepted.
     223                 :             :    * @return The contents, or nothing when it cannot be read that way.
     224                 :             :    */
     225                 :             :   [[nodiscard]] auto readOwnFile(const QString &path, qint64 maxBytes) const
     226                 :             :       -> std::optional<QByteArray>;
     227                 :             :   /** @brief See setStoreRoot(); empty when not set. */
     228                 :             :   QString m_storeRoot;
     229                 :             :   /** @brief How gpg is run; replaced by a fake in the unit tests. */
     230                 :             :   Exec m_exec;
     231                 :             : 
     232                 :             :   /**
     233                 :             :    * @brief Run gpg with @p args through m_exec.
     234                 :             :    * @param args Arguments for gpg.
     235                 :             :    * @param out If non-null, receives stdout.
     236                 :             :    * @param err If non-null, receives stderr.
     237                 :             :    * @param input Data for gpg's stdin; empty for none.
     238                 :             :    * @return gpg's exit code.
     239                 :             :    */
     240                 :             :   auto run(const QStringList &args, QString *out = nullptr,
     241                 :             :            QString *err = nullptr, const QString &input = QString()) const
     242                 :             :       -> int;
     243                 :             : };
     244                 :             : 
     245                 :             : #endif // SRC_GPGIDSIGNER_H_
        

Generated by: LCOV version 2.0-1