File indexing completed on 2024-05-05 04:45:04

0001 /*
0002  * Copyright (C) 2003-2005  Justin Karneges <justin@affinix.com>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Lesser General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Lesser General Public
0015  * License along with this library; if not, write to the Free Software
0016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
0017  *
0018  */
0019 
0020 #ifndef GPGOP_H
0021 #define GPGOP_H
0022 
0023 #include "qpipe.h"
0024 #include <QtCrypto>
0025 
0026 namespace gpgQCAPlugin {
0027 
0028 class GpgOp : public QObject
0029 {
0030     Q_OBJECT
0031 public:
0032     enum Type
0033     {
0034         Check,             // --version
0035         SecretKeyringFile, // --list-secret-keys
0036         PublicKeyringFile, // --list-public-keys
0037         SecretKeys,        // --fixed-list-mode --with-colons --list-secret-keys
0038         PublicKeys,        // --fixed-list-mode --with-colons --list-public-keys
0039         Encrypt,           // --encrypt
0040         Decrypt,           // --decrypt
0041         Sign,              // --sign
0042         SignAndEncrypt,    // --sign --encrypt
0043         SignClearsign,     // --clearsign
0044         SignDetached,      // --detach-sign
0045         Verify,            // --verify
0046         VerifyDetached,    // --verify
0047         Import,            // --import
0048         Export,            // --export
0049         DeleteKey          // --delete-key
0050     };
0051 
0052     enum VerifyResult
0053     {
0054         VerifyGood, // good sig
0055         VerifyBad,  // bad sig
0056         VerifyNoKey // we don't have signer's public key
0057     };
0058 
0059     enum Error
0060     {
0061         ErrorProcess,          // startup, process, or ipc error
0062         ErrorPassphrase,       // passphrase was either wrong or not provided
0063         ErrorFormat,           // input format was bad
0064         ErrorSignerExpired,    // signing key is expired
0065         ErrorEncryptExpired,   // encrypting key is expired
0066         ErrorEncryptUntrusted, // encrypting key is untrusted
0067         ErrorEncryptInvalid,   // encrypting key is invalid in some way
0068         ErrorDecryptNoKey,     // missing decrypt key
0069         ErrorUnknown,          // other error
0070         ErrorSignerRevoked,    // signing key is revoked
0071         ErrorSignatureExpired, // signature is expired
0072         ErrorEncryptRevoked    // encrypting key is revoked
0073     };
0074 
0075     class Event
0076     {
0077     public:
0078         enum Type
0079         {
0080             None,
0081             ReadyRead,
0082             BytesWritten,
0083             Finished,
0084             NeedPassphrase,
0085             NeedCard,
0086             ReadyReadDiagnosticText
0087         };
0088 
0089         Type    type;
0090         int     written; // BytesWritten
0091         QString keyId;   // NeedPassphrase
0092 
0093         Event()
0094             : type(None)
0095             , written(0)
0096         {
0097         }
0098     };
0099 
0100     class KeyItem
0101     {
0102     public:
0103         enum Type
0104         {
0105             RSA,
0106             DSA,
0107             ElGamal,
0108             Unknown
0109         };
0110 
0111         enum Caps
0112         {
0113             Encrypt = 0x01,
0114             Sign    = 0x02,
0115             Certify = 0x04,
0116             Auth    = 0x08
0117         };
0118 
0119         QString   id;
0120         Type      type;
0121         int       bits;
0122         QDateTime creationDate;
0123         QDateTime expirationDate;
0124         int       caps; // flags OR'd together
0125         QString   fingerprint;
0126 
0127         KeyItem()
0128             : type(Unknown)
0129             , bits(0)
0130             , caps(0)
0131         {
0132         }
0133     };
0134 
0135     class Key
0136     {
0137     public:
0138         QList<KeyItem> keyItems; // first item is primary
0139         QStringList    userIds;
0140         bool           isTrusted;
0141 
0142         Key()
0143             : isTrusted(false)
0144         {
0145         }
0146     };
0147     typedef QList<Key> KeyList;
0148 
0149     explicit GpgOp(const QString &bin, QObject *parent = nullptr);
0150     ~GpgOp() override;
0151 
0152     void reset();
0153 
0154     bool isActive() const;
0155     Type op() const;
0156 
0157     void setAsciiFormat(bool b);
0158     void setDisableAgent(bool b);
0159     void setAlwaysTrust(bool b);
0160     void setKeyrings(const QString &pubfile, const QString &secfile); // for keylists and import
0161 
0162     void doCheck();
0163     void doSecretKeyringFile();
0164     void doPublicKeyringFile();
0165     void doSecretKeys();
0166     void doPublicKeys();
0167     void doEncrypt(const QStringList &recip_ids);
0168     void doDecrypt();
0169     void doSign(const QString &signer_id);
0170     void doSignAndEncrypt(const QString &signer_id, const QStringList &recip_ids);
0171     void doSignClearsign(const QString &signer_id);
0172     void doSignDetached(const QString &signer_id);
0173     void doVerify();
0174     void doVerifyDetached(const QByteArray &sig);
0175     void doImport(const QByteArray &in);
0176     void doExport(const QString &key_id);
0177     void doDeleteKey(const QString &key_fingerprint);
0178 
0179 #ifdef QPIPE_SECURE
0180     void submitPassphrase(const QCA::SecureArray &a);
0181 #else
0182     void submitPassphrase(const QByteArray &a);
0183 #endif
0184     void cardOkay();
0185 
0186     // for encrypt, decrypt, sign, verify, export
0187     QByteArray read();
0188     void       write(const QByteArray &in);
0189     void       endWrite();
0190 
0191     QString readDiagnosticText();
0192 
0193     // for synchronous operation
0194     Event waitForEvent(int msecs = -1);
0195 
0196     // results
0197     bool         success() const;
0198     Error        errorCode() const;
0199     KeyList      keys() const;          // Keys
0200     QString      keyringFile() const;   // KeyringFile
0201     QString      homeDir() const;       // GnuPG home directory
0202     QString      encryptedToId() const; // Decrypt (for ErrorDecryptNoKey)
0203     bool         wasSigned() const;     // Decrypt
0204     QString      signerId() const;      // Verify
0205     QDateTime    timestamp() const;     // Verify
0206     VerifyResult verifyResult() const;  // Verify
0207 
0208 Q_SIGNALS:
0209     void readyRead();
0210     void bytesWritten(int bytes);
0211     void finished();
0212     void needPassphrase(const QString &keyId);
0213     void needCard();
0214     void readyReadDiagnosticText();
0215 
0216 private:
0217     class Private;
0218     friend class Private;
0219     Private *d;
0220 };
0221 
0222 }
0223 
0224 #endif