libkpgp

kpgp.h
1/*
2 kpgp.h
3
4 Copyright (C) 2001,2002 the KPGP authors
5 See file AUTHORS.kpgp for details
6
7 This file is part of KPGP, the KDE PGP/GnuPG support library.
8
9 KPGP is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19#ifndef KPGP_H
20#define KPGP_H
21
22#include <stdio.h>
23#include <tqstring.h>
24#include <tqstrlist.h>
25#include <tqdialog.h>
26#include <tqwidget.h>
27#include <tqcombobox.h>
28#include <tqlayout.h>
29#include <tqpushbutton.h>
30#include <tqlistview.h>
31#include <tqbuttongroup.h>
32#include <tqradiobutton.h>
33#include <tqmultilineedit.h>
34#include <tqcheckbox.h>
35
36#include <kdialogbase.h>
37
38#include "kpgpkey.h"
39#include <tdemacros.h>
40
41class TQLineEdit;
42class TQCursor;
43class TQCheckBox;
44class TQGridLayout;
45
46class TDEConfig;
47
48namespace Kpgp {
49
60enum { PublicKeys = 1,
61 SecretKeys = 2,
62 EncryptionKeys = 4,
63 SigningKeys = 8,
64 ValidKeys = 16,
65 TrustedKeys = 32,
66 AllKeys = PublicKeys | SecretKeys | EncryptionKeys | SigningKeys,
67 PubSecKeys = PublicKeys | SecretKeys,
68 EncrSignKeys = EncryptionKeys | SigningKeys
69};
70
71enum Result
72{
73 Failure = 0,
74 Ok = 1,
75 Canceled = 2
76};
77
78class Base;
79class Block;
80
81class TDE_EXPORT Module
82{
83 friend class Block;
84
85private:
86 // the class running pgp
87 Base *pgp;
88
89public:
90 Module();
91 virtual ~Module();
92
96 virtual void readConfig();
97 virtual void writeConfig(bool sync);
98 virtual void init();
99
102 bool decrypt( Block& block );
103
105 bool verify( Block& block );
106
114 Kpgp::Result clearsign( Block& block,
115 const KeyID& keyId, const TQCString& charset = 0 );
116
125 Kpgp::Result encrypt( Block& block,
126 const TQStringList& receivers, const KeyID& keyId,
127 bool sign, const TQCString& charset = 0 );
128
136 Kpgp::Result getEncryptionKeys( KeyIDList& encryptionKeyIds,
137 const TQStringList& recipients,
138 const KeyID& keyId );
139
149 int encryptionPossible( const TQStringList& recipients );
150
151protected:
152 int doEncSign( Block& block, const KeyIDList& recipientKeyIds, bool sign );
153
154public:
156 bool signKey( const KeyID& keyID );
157
159 const KeyList publicKeys();
160
162 const KeyList secretKeys();
163
166 void readPublicKeys( bool reread = false );
167
170 void readSecretKeys( bool reread = false );
171
173 TQCString getAsciiPublicKey( const KeyID& keyID );
174
178 Key* publicKey( const KeyID& keyID );
179
183 Key* publicKey( const TQString& userID );
184
188 Key* secretKey( const KeyID& keyID );
189
193 Validity keyTrust( const KeyID& keyID );
194
199 Validity keyTrust( const TQString& userID );
200
204 bool isTrusted( const KeyID& keyID );
205
209 Key* rereadKey( const KeyID& keyID, const bool readTrust = true );
210
213 bool changePassPhrase();
214
217 void setUser(const KeyID& keyID);
219 const KeyID user() const;
220
222 void setEncryptToSelf(bool flag);
223 bool encryptToSelf(void) const;
224
230 void setStorePassPhrase(bool);
231 bool storePassPhrase(void) const;
232
234 void clear(const bool erasePassPhrase = FALSE);
235
237 const TQString lastErrorMsg(void) const;
238
239 // what version of PGP/GPG should we use
240 enum PGPType { tAuto, tGPG, tPGP2, tPGP5, tPGP6, tOff } pgpType;
241
242 // did we find a pgp executable?
243 bool havePGP(void) const;
244
246 bool usePGP(void) const { return (havePGP() && (pgpType != tOff)); }
247
248 // show the result of encryption/signing?
249 void setShowCipherText(const bool flag);
250 bool showCipherText(void) const;
251
252 // show the encryption keys for approval?
253 void setShowKeyApprovalDlg(const bool flag);
254 bool showKeyApprovalDlg(void) const;
255
260 KeyID selectSecretKey( const TQString& title,
261 const TQString& text = TQString(),
262 const KeyID& keyId = KeyID() );
263
270 KeyID selectPublicKey( const TQString& title,
271 const TQString& text = TQString(),
272 const KeyID& oldKeyId = KeyID(),
273 const TQString& address = TQString(),
274 const unsigned int allowedKeys = AllKeys );
275
282 KeyIDList selectPublicKeys( const TQString& title,
283 const TQString& text = TQString(),
284 const KeyIDList& oldKeyIds = KeyIDList(),
285 const TQString& address = TQString(),
286 const unsigned int allowedKeys = AllKeys );
287
288 // FIXME: key management
289
293 EncryptPref encryptionPreference( const TQString& address );
294
298 void setEncryptionPreference( const TQString& address,
299 const EncryptPref pref );
300
301 // -- static member functions --------------------------------------------
302
304 static Kpgp::Module *getKpgp();
305
307 static TDEConfig *getConfig();
308
323 static bool prepareMessageForDecryption( const TQCString& msg,
324 TQPtrList<Block>& pgpBlocks,
325 TQStrList& nonPgpBlocks );
326
327private:
329 bool haveTrustedEncryptionKey( const TQString& person );
330
332 KeyIDList getEncryptionKeys( const TQString& person );
333
335 bool setPassPhrase(const TQString &pass);
336
343 int prepare(bool needPassPhrase=FALSE, Block* block = 0 );
344
346 void cleanupPass() { if (!storePass) wipePassPhrase(); }
347
350 void wipePassPhrase(bool free=false);
351
352 // transform an address into canonical form
353 TQString canonicalAddress( const TQString& person );
354
357 KeyID selectKey( const KeyList& keys,
358 const TQString& title,
359 const TQString& text = TQString(),
360 const KeyID& keyId = KeyID(),
361 const unsigned int allowedKeys = AllKeys );
362
365 KeyIDList selectKeys( const KeyList& keys,
366 const TQString& title,
367 const TQString& text = TQString(),
368 const KeyIDList& keyIds = KeyIDList(),
369 const unsigned int allowedKeys = AllKeys );
370
375 KeyID selectKey( bool& rememberChoice,
376 const KeyList& keys,
377 const TQString& title,
378 const TQString& text = TQString(),
379 const KeyID& keyId = KeyID(),
380 const unsigned int allowedKeys = AllKeys );
381
386 KeyIDList selectKeys( bool& rememberChoice,
387 const KeyList& keys,
388 const TQString& title,
389 const TQString& text = TQString(),
390 const KeyIDList& keyIds = KeyIDList(),
391 const unsigned int allowedKeys = AllKeys );
392
396 KeyIDList keysForAddress( const TQString& address );
397
400 void setKeysForAddress( const TQString& address, const KeyIDList& keyIDs );
401
403 void removeKeyForAddress( const TQString& address );
404
408 void readAddressData();
409
413 void writeAddressData();
414
415 bool checkForPGP(void);
416 void assignPGPBase(void);
417
418 static Kpgp::Module *kpgpObject;
419 TDEConfig *config;
420
421 struct AddressData {
422 KeyIDList keyIds;
423 EncryptPref encrPref;
424 };
425 typedef TQMap<TQString, AddressData> AddressDataDict;
426 AddressDataDict addressDataDict;
427
428 KeyList mPublicKeys;
429 bool mPublicKeysCached : 1; // did we already read the public keys?
430 KeyList mSecretKeys;
431 bool mSecretKeysCached : 1; // did we already read the secret keys?
432
433 bool storePass : 1;
434 TQString passphrase;
435
436 TQString errMsg;
437
438 KeyID pgpUser; // the key ID which is used to sign/encrypt to self
439 bool flagEncryptToSelf : 1;
440
441 bool havePgp : 1;
442 bool havePGP5 : 1;
443 bool haveGpg : 1;
444 bool havePassPhrase : 1;
445 bool showEncryptionResult : 1;
446 bool mShowKeyApprovalDlg : 1;
447}; // class Module
448
449// -- inlined member functions ---------------------------------------------
450
451inline void
452Module::setShowKeyApprovalDlg( const bool flag )
453{
454 mShowKeyApprovalDlg = flag;
455}
456
457inline bool
458Module::showKeyApprovalDlg( void ) const
459{
460 return mShowKeyApprovalDlg;
461}
462
463// -------------------------------------------------------------------------
464
465} // namespace Kpgp
466
467#endif
468