1 | /* |
2 | * Copyright (C) 2003-2005 Justin Karneges <justin@affinix.com> |
3 | * |
4 | * This library is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Lesser General Public |
6 | * License as published by the Free Software Foundation; either |
7 | * version 2.1 of the License, or (at your option) any later version. |
8 | * |
9 | * This library is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Lesser General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU Lesser General Public |
15 | * License along with this library; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
17 | * |
18 | */ |
19 | |
20 | #ifndef GPGOP_H |
21 | #define GPGOP_H |
22 | |
23 | #include "qpipe.h" |
24 | #include <QtCrypto> |
25 | |
26 | namespace gpgQCAPlugin { |
27 | |
28 | class GpgOp : public QObject |
29 | { |
30 | Q_OBJECT |
31 | public: |
32 | enum Type |
33 | { |
34 | Check, // --version |
35 | SecretKeyringFile, // --list-secret-keys |
36 | PublicKeyringFile, // --list-public-keys |
37 | SecretKeys, // --fixed-list-mode --with-colons --list-secret-keys |
38 | PublicKeys, // --fixed-list-mode --with-colons --list-public-keys |
39 | Encrypt, // --encrypt |
40 | Decrypt, // --decrypt |
41 | Sign, // --sign |
42 | SignAndEncrypt, // --sign --encrypt |
43 | SignClearsign, // --clearsign |
44 | SignDetached, // --detach-sign |
45 | Verify, // --verify |
46 | VerifyDetached, // --verify |
47 | Import, // --import |
48 | Export, // --export |
49 | DeleteKey // --delete-key |
50 | }; |
51 | |
52 | enum VerifyResult |
53 | { |
54 | VerifyGood, // good sig |
55 | VerifyBad, // bad sig |
56 | VerifyNoKey // we don't have signer's public key |
57 | }; |
58 | |
59 | enum Error |
60 | { |
61 | ErrorProcess, // startup, process, or ipc error |
62 | ErrorPassphrase, // passphrase was either wrong or not provided |
63 | ErrorFormat, // input format was bad |
64 | ErrorSignerExpired, // signing key is expired |
65 | ErrorEncryptExpired, // encrypting key is expired |
66 | ErrorEncryptUntrusted, // encrypting key is untrusted |
67 | ErrorEncryptInvalid, // encrypting key is invalid in some way |
68 | ErrorDecryptNoKey, // missing decrypt key |
69 | ErrorUnknown, // other error |
70 | ErrorSignerRevoked, // signing key is revoked |
71 | ErrorSignatureExpired, // signature is expired |
72 | ErrorEncryptRevoked // encrypting key is revoked |
73 | }; |
74 | |
75 | class Event |
76 | { |
77 | public: |
78 | enum Type |
79 | { |
80 | None, |
81 | ReadyRead, |
82 | BytesWritten, |
83 | Finished, |
84 | NeedPassphrase, |
85 | NeedCard, |
86 | ReadyReadDiagnosticText |
87 | }; |
88 | |
89 | Type type; |
90 | int written; // BytesWritten |
91 | QString keyId; // NeedPassphrase |
92 | |
93 | Event() |
94 | : type(None) |
95 | , written(0) |
96 | { |
97 | } |
98 | }; |
99 | |
100 | class KeyItem |
101 | { |
102 | public: |
103 | enum Type |
104 | { |
105 | RSA, |
106 | DSA, |
107 | ElGamal, |
108 | Unknown |
109 | }; |
110 | |
111 | enum Caps |
112 | { |
113 | Encrypt = 0x01, |
114 | Sign = 0x02, |
115 | Certify = 0x04, |
116 | Auth = 0x08 |
117 | }; |
118 | |
119 | QString id; |
120 | Type type; |
121 | int bits; |
122 | QDateTime creationDate; |
123 | QDateTime expirationDate; |
124 | int caps; // flags OR'd together |
125 | QString fingerprint; |
126 | |
127 | KeyItem() |
128 | : type(Unknown) |
129 | , bits(0) |
130 | , caps(0) |
131 | { |
132 | } |
133 | }; |
134 | |
135 | class Key |
136 | { |
137 | public: |
138 | QList<KeyItem> keyItems; // first item is primary |
139 | QStringList userIds; |
140 | bool isTrusted; |
141 | |
142 | Key() |
143 | : isTrusted(false) |
144 | { |
145 | } |
146 | }; |
147 | typedef QList<Key> KeyList; |
148 | |
149 | explicit GpgOp(const QString &bin, QObject *parent = nullptr); |
150 | ~GpgOp() override; |
151 | |
152 | void reset(); |
153 | |
154 | bool isActive() const; |
155 | Type op() const; |
156 | |
157 | void setAsciiFormat(bool b); |
158 | void setDisableAgent(bool b); |
159 | void setAlwaysTrust(bool b); |
160 | void setKeyrings(const QString &pubfile, const QString &secfile); // for keylists and import |
161 | |
162 | void doCheck(); |
163 | void doSecretKeyringFile(); |
164 | void doPublicKeyringFile(); |
165 | void doSecretKeys(); |
166 | void doPublicKeys(); |
167 | void doEncrypt(const QStringList &recip_ids); |
168 | void doDecrypt(); |
169 | void doSign(const QString &signer_id); |
170 | void doSignAndEncrypt(const QString &signer_id, const QStringList &recip_ids); |
171 | void doSignClearsign(const QString &signer_id); |
172 | void doSignDetached(const QString &signer_id); |
173 | void doVerify(); |
174 | void doVerifyDetached(const QByteArray &sig); |
175 | void doImport(const QByteArray &in); |
176 | void doExport(const QString &key_id); |
177 | void doDeleteKey(const QString &key_fingerprint); |
178 | |
179 | #ifdef QPIPE_SECURE |
180 | void submitPassphrase(const QCA::SecureArray &a); |
181 | #else |
182 | void submitPassphrase(const QByteArray &a); |
183 | #endif |
184 | void cardOkay(); |
185 | |
186 | // for encrypt, decrypt, sign, verify, export |
187 | QByteArray read(); |
188 | void write(const QByteArray &in); |
189 | void endWrite(); |
190 | |
191 | QString readDiagnosticText(); |
192 | |
193 | // for synchronous operation |
194 | Event waitForEvent(int msecs = -1); |
195 | |
196 | // results |
197 | bool success() const; |
198 | Error errorCode() const; |
199 | KeyList keys() const; // Keys |
200 | QString keyringFile() const; // KeyringFile |
201 | QString homeDir() const; // GnuPG home directory |
202 | QString encryptedToId() const; // Decrypt (for ErrorDecryptNoKey) |
203 | bool wasSigned() const; // Decrypt |
204 | QString signerId() const; // Verify |
205 | QDateTime timestamp() const; // Verify |
206 | VerifyResult verifyResult() const; // Verify |
207 | |
208 | Q_SIGNALS: |
209 | void readyRead(); |
210 | void bytesWritten(int bytes); |
211 | void finished(); |
212 | void needPassphrase(const QString &keyId); |
213 | void needCard(); |
214 | void readyReadDiagnosticText(); |
215 | |
216 | private: |
217 | class Private; |
218 | friend class Private; |
219 | Private *d; |
220 | }; |
221 | |
222 | } |
223 | |
224 | #endif |
225 | |