Better wording
[kdepim.git] / libkpgp / kpgp.h
blob5f6b26ffbf6b206ab08e778731bc7959135486ff
1 /*
2 kpgp.h
4 Copyright (C) 2001,2002 the KPGP authors
5 See file AUTHORS.kpgp for details
7 This file is part of KPGP, the KDE PGP/GnuPG support library.
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.
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
19 #ifndef KPGP_H
20 #define KPGP_H
22 #include "libkpgp_export.h"
23 #include "kpgpkey.h"
25 #include <QtCore/QByteArray>
26 #include <QtCore/QString>
27 #include <QtGui/QWidget>
28 #include <QtGui/QComboBox>
29 #include <QtGui/QLayout>
30 #include <QtGui/QPushButton>
31 #include <QtGui/QRadioButton>
32 #include <QtGui/QCheckBox>
33 #include <QtGui/QGridLayout>
35 #include <stdio.h>
37 class QCheckBox;
38 class QGridLayout;
40 class KConfig;
42 namespace Kpgp {
44 /** This enumerated type is used by Kpgp::* to indicate which keys can be
45 selected by the user. The following values are available:
46 - Kpgp::PublicKeys: Public keys are shown in the selection dialog.
47 - Kpgp::SecretKeys: Secret keys are shown in the selection dialog.
48 - Kpgp::EncryptionKeys: Encryption keys can be selected.
49 - Kpgp::SigningKeys: Signing keys can be selected.
50 - Kpgp::ValidKeys: Only valid keys can be selected.
51 - Kpgp::TrustedKeys: Only trusted keys can be selected.
52 - Kpgp::AllKeys == PublicKeys | SecretKeys | EncryptionKeys | SigningKeys
54 enum { PublicKeys = 1,
55 SecretKeys = 2,
56 EncryptionKeys = 4,
57 SigningKeys = 8,
58 ValidKeys = 16,
59 TrustedKeys = 32,
60 AllKeys = PublicKeys | SecretKeys | EncryptionKeys | SigningKeys,
61 PubSecKeys = PublicKeys | SecretKeys,
62 EncrSignKeys = EncryptionKeys | SigningKeys
65 enum Result
67 Failure = 0,
68 Ok = 1,
69 Canceled = 2
72 class Base;
73 class Block;
75 class KPGP_EXPORT Module
77 friend class Block;
79 private:
80 // the class running pgp
81 Base *pgp;
83 /**
84 * Singleton pattern: use getKpgp()
86 Module();
88 public:
89 virtual ~Module();
91 /** the following virtual function form the interface to the
92 application using Kpgp
94 virtual void readConfig();
95 virtual void writeConfig(bool sync);
96 virtual void init();
98 /** decrypts the given OpenPGP block if the passphrase is good.
99 returns false otherwise */
100 bool decrypt( Block& block );
102 /** Tries to verify the given OpenPGP block */
103 bool verify( Block& block );
105 /** clearsigns the given OpenPGP block with the key corresponding to the
106 given key id. The charset is needed to display the text correctly.
107 Returns
108 Failure if there was an unresolvable error
109 Canceled if signing was canceled
110 Ok if everything is o.k.
112 Kpgp::Result clearsign( Block& block,
113 const KeyID& keyId, const QByteArray& charset = 0 );
115 /** encrypts the given OpenPGP block for a list of persons. if sign is true
116 then the block is clearsigned with the key corresponding to the given
117 key id. The charset is needed to display the text correctly.
118 Returns
119 Failure if there was an unresolvable error
120 Canceled if encryption was canceled
121 Ok if everything is o.k.
123 Kpgp::Result encrypt( Block& block,
124 const QStringList& receivers, const KeyID& keyId,
125 bool sign, const QByteArray& charset = 0 );
127 /** Determines the keys which should be used for encrypting the message
128 to the given list of recipients.
129 Returns:
130 Failure if there was an unresolvable error
131 Canceled if encryption was canceled
132 Ok if everything is o.k.
134 Kpgp::Result getEncryptionKeys( KeyIDList& encryptionKeyIds,
135 const QStringList& recipients,
136 const KeyID& keyId );
138 /** checks if encrypting to the given list of persons is possible and
139 desired, i.e. if we have a (trusted) key for every recipient and
140 if encryption to all keys is allowed.
141 Returns
142 0 if encryption is not possible or not desired,
143 1 if encryption is possible and desired,
144 2 if encryption is possible, but the user wants to be asked and
145 -1 if there is a conflict which can't be automatically resolved.
147 int encryptionPossible( const QStringList& recipients );
149 protected:
150 int doEncSign( Block& block, const KeyIDList& recipientKeyIds, bool sign );
152 public:
153 /** sign a key in the keyring with users signature. */
154 bool signKey( const KeyID& keyID );
156 /** get the list of cached public keys. */
157 const KeyList publicKeys();
159 /** get the list of cached secret keys. */
160 const KeyList secretKeys();
162 /** Reads the list of public keys if necessary or if <em>reread</em> is true.
164 void readPublicKeys( bool reread = false );
166 /** Reads the list of secret keys if necessary or if <em>reread</em> is true.
168 void readSecretKeys( bool reread = false );
170 /** try to get an ascii armored key block for the given public key */
171 QByteArray getAsciiPublicKey( const KeyID& keyID );
173 /** Returns the public key with the given key ID or null if no matching
174 key is found.
176 Key* publicKey( const KeyID& keyID );
178 /** Returns the first public key with the given user ID or null if no
179 matching key is found.
181 Key* publicKey( const QString& userID );
183 /** Returns the secret key with the given key ID or null if no matching
184 key is found.
186 Key* secretKey( const KeyID& keyID );
188 /** Returns the trust value for the given key. This is the maximal trust
189 value of any of the user ids of this key.
191 Validity keyTrust( const KeyID& keyID );
193 /** Returns the trust value of a key with the given user id. If more than
194 one key have this user id then the first key with this user id will be
195 chosen.
197 Validity keyTrust( const QString& userID );
199 /** Returns TRUE if the given key is at least trusted marginally. Otherwise
200 FALSE is returned.
202 bool isTrusted( const KeyID& keyID );
204 /** Rereads the key data for the given key and returns the reread data. If
205 <em>readTrust</em> is true then the trust of this key will be determined.
207 Key* rereadKey( const KeyID& keyID, const bool readTrust = true );
209 /** Request the change of the passphrase of the actual secret
210 key. TBI */
211 bool changePassPhrase();
213 /** set a user identity to use (if you have more than one...)
214 by default, pgp uses the identity which was generated last. */
215 void setUser(const KeyID& keyID);
216 /** Returns the actual key ID of the currently set key. */
217 const KeyID user() const;
219 /** always encrypt message to oneself? */
220 void setEncryptToSelf(bool flag);
221 bool encryptToSelf(void) const;
223 /** store passphrase in pgp object
224 Problem: passphrase stays in memory.
225 Advantage: you can call en-/decrypt without always passing the
226 passphrase
228 void setStorePassPhrase(bool);
229 bool storePassPhrase(void) const;
231 /** clears everything from memory */
232 void clear(const bool erasePassPhrase = false);
234 /** returns the last error that occurred */
235 const QString lastErrorMsg(void) const;
237 // what version of PGP/GPG should we use
238 enum PGPType { tAuto, tGPG, tPGP2, tPGP5, tPGP6, tOff } pgpType;
240 // did we find a pgp executable?
241 bool havePGP(void) const;
243 /** Should PGP/GnuPG be used? */
244 bool usePGP(void) const { return (havePGP() && (pgpType != tOff)); }
246 // show the result of encryption/signing?
247 void setShowCipherText(const bool flag);
248 bool showCipherText(void) const;
250 // show the encryption keys for approval?
251 void setShowKeyApprovalDlg(const bool flag);
252 bool showKeyApprovalDlg(void) const;
254 /** Shows a key selection dialog with all secret keys and the given title
255 and the (optional) text. If <em>keyId</em> is given, then the
256 corresponding key is selected.
258 KeyID selectSecretKey( const QString& title,
259 const QString& text = QString(),
260 const KeyID& keyId = KeyID() );
262 /** Shows a key selection dialog with all public keys and the given title
263 and the (optional) text. If <em>oldKeyId</em> is given, then the
264 corresponding key is selected. If <em>address</em> is given, then the
265 chosen key will be stored (if the user wants it to be stored).
266 <em>mode</em> specifies which keys can be selected.
268 KeyID selectPublicKey( const QString& title,
269 const QString& text = QString(),
270 const KeyID& oldKeyId = KeyID(),
271 const QString& address = QString(),
272 const unsigned int allowedKeys = AllKeys );
274 /** Shows a key selection dialog with all public keys and the given title
275 and the (optional) text. If <em>oldKeyId</em> is given, then the
276 corresponding key is selected. If <em>address</em> is given, then the
277 chosen key will be stored (if the user wants it to be stored).
278 <em>mode</em> specifies which keys can be selected.
280 KeyIDList selectPublicKeys( const QString& title,
281 const QString& text = QString(),
282 const KeyIDList& oldKeyIds = KeyIDList(),
283 const QString& address = QString(),
284 const unsigned int allowedKeys = AllKeys );
286 // FIXME: key management
288 /** Reads the encryption preference for the given address
289 from the config file.
291 EncryptPref encryptionPreference( const QString& address );
293 /** Writes the given encryption preference for the given address
294 to the config file.
296 void setEncryptionPreference( const QString& address,
297 const EncryptPref pref );
299 // -- static member functions --------------------------------------------
301 /** return the actual pgp object */
302 static Kpgp::Module *getKpgp();
304 /** get the kpgp config object */
305 static KConfig *getConfig();
307 /** Parses the given message and splits it into OpenPGP blocks and
308 Non-OpenPGP blocks.
309 Returns TRUE if the message contains at least one OpenPGP block and
310 FALSE otherwise.
311 The format is then:
312 <pre>
313 1st Non-OpenPGP block
314 1st OpenPGP block
315 2nd Non-OpenPGP block
317 n-th OpenPGP block
318 (n+1)-th Non-OpenPGP block
319 </pre>
321 static bool prepareMessageForDecryption( const QByteArray& msg,
322 QList<Block>& pgpBlocks,
323 QList<QByteArray>& nonPgpBlocks );
325 private:
326 /** check if we have a trusted encryption key for the given person */
327 bool haveTrustedEncryptionKey( const QString& person );
329 /** get a list of encryption keys to be used for the given recipient */
330 KeyIDList getEncryptionKeys( const QString& person );
332 /** Set pass phrase */
333 bool setPassPhrase(const QString &pass);
335 /** test if the PGP executable is found and if there is a passphrase
336 set or given. Returns:
337 1 if everything is ok
338 0 (together with some warning message) if something is missing
339 -1 if the passphrase dialog was canceled
341 int prepare(bool needPassPhrase=false, Block* block = 0 );
343 /** cleanup passphrase if it should not be stored. */
344 void cleanupPass() { if (!storePass) wipePassPhrase(); }
346 /** Wipes and optionally frees the memory used to hold the
347 passphrase. */
348 void wipePassPhrase(bool free=false);
350 // transform an address into canonical form
351 QString canonicalAddress( const QString& person );
353 /** Shows a dialog to let the user select a key from the given list of keys
355 KeyID selectKey( const KeyList& keys,
356 const QString& title,
357 const QString& text = QString(),
358 const KeyID& keyId = KeyID(),
359 const unsigned int allowedKeys = AllKeys );
361 /** Shows a dialog to let the user select a key from the given list of keys
363 KeyIDList selectKeys( const KeyList& keys,
364 const QString& title,
365 const QString& text = QString(),
366 const KeyIDList& keyIds = KeyIDList(),
367 const unsigned int allowedKeys = AllKeys );
369 /** Shows a dialog to let the user select a key from the given list of keys.
370 The dialog includes a checkbox ("Remember decision"). The state of the
371 checkbox is returned in rememberChoice.
373 KeyID selectKey( bool& rememberChoice,
374 const KeyList& keys,
375 const QString& title,
376 const QString& text = QString(),
377 const KeyID& keyId = KeyID(),
378 const unsigned int allowedKeys = AllKeys );
380 /** Shows a dialog to let the user select a list of keys from the given
381 list of keys. The dialog includes a checkbox ("Remember decision").
382 The state of the checkbox is returned in rememberChoice.
384 KeyIDList selectKeys( bool& rememberChoice,
385 const KeyList& keys,
386 const QString& title,
387 const QString& text = QString(),
388 const KeyIDList& keyIds = KeyIDList(),
389 const unsigned int allowedKeys = AllKeys );
391 /** Returns the OpenPGP keys which should be used for encryption to the
392 given address.
394 KeyIDList keysForAddress( const QString& address );
396 /** Set an email address -> list of OpenPGP keys association.
398 void setKeysForAddress( const QString& address, const KeyIDList& keyIDs );
400 /** Remove an email address -> OpenPGP key association. */
401 void removeKeyForAddress( const QString& address );
403 /** Reads the email address -> OpenPGP key associations from the config
404 file.
406 void readAddressData();
408 /** Writes the email address -> OpenPGP key associations to the config
409 file.
411 void writeAddressData();
413 bool checkForPGP(void);
414 void assignPGPBase(void);
416 KConfig *config;
418 struct AddressData {
419 KeyIDList keyIds;
420 EncryptPref encrPref;
422 typedef QMap<QString, AddressData> AddressDataDict;
423 AddressDataDict addressDataDict;
425 KeyList mPublicKeys;
426 bool mPublicKeysCached : 1; // did we already read the public keys?
427 KeyList mSecretKeys;
428 bool mSecretKeysCached : 1; // did we already read the secret keys?
430 bool storePass : 1;
431 char * passphrase;
432 size_t passphrase_buffer_len;
434 QString errMsg;
436 KeyID pgpUser; // the key ID which is used to sign/encrypt to self
437 bool flagEncryptToSelf : 1;
439 bool havePgp : 1;
440 bool havePGP5 : 1;
441 bool haveGpg : 1;
442 bool havePassPhrase : 1;
443 bool showEncryptionResult : 1;
444 bool mShowKeyApprovalDlg : 1;
445 }; // class Module
447 // -- inlined member functions ---------------------------------------------
449 inline void
450 Module::setShowKeyApprovalDlg( const bool flag )
452 mShowKeyApprovalDlg = flag;
455 inline bool
456 Module::showKeyApprovalDlg( void ) const
458 return mShowKeyApprovalDlg;
461 // -------------------------------------------------------------------------
463 } // namespace Kpgp
465 #endif