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 St, Fifth Floor, Boston, MA 02110-1301, USA
0017  *
0018  */
0019 
0020 #pragma once
0021 
0022 #include "gpgop.h"
0023 #include "gpgproc.h"
0024 #include "lineconverter.h"
0025 #include "qca_safetimer.h"
0026 #include <QByteArray>
0027 #include <QObject>
0028 #include <QStringList>
0029 
0030 #ifdef GPG_PROFILE
0031 #include <QTime>
0032 #endif
0033 
0034 namespace gpgQCAPlugin {
0035 
0036 class GpgAction : public QObject
0037 {
0038     Q_OBJECT
0039 public:
0040     struct Input
0041     {
0042         QString     bin;
0043         GpgOp::Type op;
0044         bool        opt_ascii, opt_noagent, opt_alwaystrust;
0045         QString     opt_pubfile, opt_secfile;
0046         QStringList recip_ids;
0047         QString     signer_id;
0048         QByteArray  sig;
0049         QByteArray  inkey;
0050         QString     export_key_id;
0051         QString     delete_key_fingerprint;
0052 
0053         Input()
0054             : opt_ascii(false)
0055             , opt_noagent(false)
0056             , opt_alwaystrust(false)
0057         {
0058         }
0059     };
0060 
0061     struct Output
0062     {
0063         bool                success;
0064         GpgOp::Error        errorCode;
0065         GpgOp::KeyList      keys;
0066         QString             keyringFile;
0067         QString             encryptedToId;
0068         bool                wasSigned;
0069         QString             signerId;
0070         QDateTime           timestamp;
0071         GpgOp::VerifyResult verifyResult;
0072         QString             homeDir;
0073 
0074         Output()
0075             : success(false)
0076             , errorCode(GpgOp::ErrorUnknown)
0077             , wasSigned(false)
0078         {
0079         }
0080     };
0081 
0082     Input  input;
0083     Output output;
0084 
0085     GpgAction(QObject *parent = nullptr);
0086     ~GpgAction() override;
0087     void reset();
0088     void start();
0089 #ifdef QPIPE_SECURE
0090     void submitPassphrase(const QCA::SecureArray &a);
0091 #else
0092     void submitPassphrase(const QByteArray &a);
0093 #endif
0094 
0095 public Q_SLOTS:
0096     QByteArray read();
0097     void       write(const QByteArray &in);
0098     void       endWrite();
0099     void       cardOkay();
0100     QString    readDiagnosticText();
0101 
0102 Q_SIGNALS:
0103     void readyRead();
0104     void bytesWritten(int bytes);
0105     void finished();
0106     void needPassphrase(const QString &keyId);
0107     void needCard();
0108     void readyReadDiagnosticText();
0109 
0110 private:
0111     void submitCommand(const QByteArray &a);
0112 
0113     // since str is taken as a value, it is ok to use the same variable for 'rest'
0114     QString nextArg(QString str, QString *rest = nullptr);
0115     void    processStatusLine(const QString &line);
0116     void    processResult(int code);
0117     void    ensureDTextEmit();
0118 
0119     GPGProc        proc;
0120     bool           collectOutput, allowInput;
0121     LineConverter  readConv, writeConv;
0122     bool           readText, writeText;
0123     QByteArray     buf_stdout, buf_stderr;
0124     bool           useAux;
0125     QString        passphraseKeyId;
0126     bool           signing, decryptGood, signGood;
0127     GpgOp::Error   curError;
0128     bool           badPassphrase;
0129     bool           need_submitPassphrase, need_cardOkay;
0130     QString        diagnosticText;
0131     QCA::SafeTimer dtextTimer;
0132     bool           utf8Output;
0133 
0134 #ifdef GPG_PROFILE
0135     QTime timer;
0136 #endif
0137 
0138 private Q_SLOTS:
0139     void t_dtext();
0140     void proc_error(gpgQCAPlugin::GPGProc::Error e);
0141     void proc_finished(int exitCode);
0142     void proc_readyReadStdout();
0143     void proc_readyReadStderr();
0144     void proc_readyReadStatusLines();
0145     void proc_bytesWrittenStdin(int bytes);
0146     void proc_bytesWrittenAux(int bytes);
0147     void proc_bytesWrittenCommand(int);
0148     void proc_debug(const QString &str);
0149     void appendDiagnosticText(const QString &line);
0150 };
0151 
0152 } // end namespace gpgQCAPlugin