File indexing completed on 2024-04-28 09:46:05

0001 /*
0002     SPDX-FileCopyrightText: 2009-2022 Rolf Eike Beer <kde@opensource.sf-tec.de>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #ifndef KGPGSIGNTRANSACTIONHELPER_H
0007 #define KGPGSIGNTRANSACTIONHELPER_H
0008 
0009 #include "kgpgtransaction.h"
0010 
0011 class KGpgKeyNode;
0012 class QString;
0013 
0014 /**
0015  * @brief helper class for key signing transactions
0016  */
0017 class KGpgSignTransactionHelper {
0018     Q_DISABLE_COPY(KGpgSignTransactionHelper)
0019     KGpgSignTransactionHelper() = delete;
0020 public:
0021     /**
0022      * @brief the outcomes of nextLine()
0023      */
0024     enum lineParseResults {
0025         handledFalse,   ///< the line was parsed successfully and transaction can continue
0026         handledTrue,    ///< the line was parsed successfully and the transaction shoult be shut down
0027         notHandled  ///< the line was not handled
0028     };
0029 
0030     enum carefulCheck {
0031         noAnswer = 0,
0032         notChecked = 1,
0033         normalChecking = 2,
0034         carefulChecking = 3
0035     };
0036 
0037     enum ts_signuid {
0038         TS_ALREADY_SIGNED = KGpgTransaction::TS_COMMON_END + 1  ///< user id is alredy signed by given key
0039     };
0040 
0041     /**
0042      * @brief destructor
0043      */
0044     virtual ~KGpgSignTransactionHelper() = default;
0045 
0046 protected:
0047     /**
0048      * @brief constructor
0049      * @param signer id of the key to sign with
0050      * @param local if signature should be local (not exportable)
0051      * @param checking how carefully the identity of the key owner was checked
0052      */
0053     KGpgSignTransactionHelper(const QString &signer, const bool local, const carefulCheck checking);
0054     /**
0055      * @brief handle signing commands from GnuPG
0056      * @param line input to parse
0057      *
0058      * This will handle the GnuPG commands specific to signing.
0059      */
0060     lineParseResults nextLine(const QString &line);
0061     KGpgTransaction::ts_boolanswer boolQuestion(const QString &line);
0062 
0063 public:
0064     /**
0065      * @brief set key node this transaction is using
0066      * @param node new key node
0067      */
0068     void setKey(const KGpgKeyNode *node);
0069 
0070     /**
0071      * @brief get the key node this transaction is using
0072      */
0073     const KGpgKeyNode *getKey(void) const;
0074 
0075     /**
0076      * @brief set if the signature should be local (not exportable)
0077      * @param local flag if local signature should be applied
0078      */
0079     void setLocal(const bool local);
0080 
0081     /**
0082      * @brief check if local signing is requested
0083      */
0084     bool getLocal(void) const;
0085 
0086     /**
0087      * @brief set the level how carefully the identity was checked
0088      * @param level level to set
0089      */
0090     void setChecking(const carefulCheck level);
0091 
0092     /**
0093      * @brief check if local signing is requested
0094      */
0095     carefulCheck getChecking(void) const;
0096 
0097     /**
0098      * @brief set which private key is used to sign
0099      * @param signer id of private key to use
0100      */
0101     void setSigner(const QString &signer);
0102 
0103     /**
0104      * @brief get key id which is used to sign
0105      */
0106     QString getSigner(void) const;
0107 
0108     /**
0109      * @brief add a secret keyring file
0110      *
0111      * This allows to specify an additional file where secret keys are
0112      * stored to be used by this operation. This is especially useful
0113      * if a different GnuPG home directory is set but the original keys
0114      * should be used for signing.
0115      */
0116     void setSecringFile(const QString &filename);
0117 
0118 private:
0119     const KGpgKeyNode *m_node;
0120     QString m_signer;
0121     bool m_local;
0122     carefulCheck m_checking;
0123 
0124 protected:
0125     int m_signerPos;    ///< position of the signer argument in GnuPG command line
0126 
0127     /**
0128      * @brief returns the transaction object to use
0129      *
0130      * This should really be static_cast<>(this) as you should
0131      * only use this class as one of two anchestors of a transaction.
0132      */
0133     virtual KGpgTransaction *asTransaction() = 0;
0134     /**
0135      * @brief replaces the command passed to GnuPG
0136      * @param cmd new command to use
0137      */
0138     virtual void replaceCmd(const QString &cmd) = 0;
0139 };
0140 
0141 #endif // KGPGSIGNTRANSACTIONHELPER_H