00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef KPGP_H
00020 #define KPGP_H
00021
00022 #include "libkpgp_export.h"
00023 #include "kpgpkey.h"
00024
00025 #include <QtCore/QByteArray>
00026 #include <QtCore/QString>
00027 #include <QtGui/QWidget>
00028 #include <QtGui/QComboBox>
00029 #include <QtGui/QLayout>
00030 #include <QtGui/QPushButton>
00031 #include <QtGui/QRadioButton>
00032 #include <QtGui/QCheckBox>
00033 #include <QtGui/QGridLayout>
00034
00035 #include <stdio.h>
00036
00037 class QCheckBox;
00038 class QGridLayout;
00039
00040 class KConfig;
00041
00042 namespace Kpgp {
00043
00054 enum { PublicKeys = 1,
00055 SecretKeys = 2,
00056 EncryptionKeys = 4,
00057 SigningKeys = 8,
00058 ValidKeys = 16,
00059 TrustedKeys = 32,
00060 AllKeys = PublicKeys | SecretKeys | EncryptionKeys | SigningKeys,
00061 PubSecKeys = PublicKeys | SecretKeys,
00062 EncrSignKeys = EncryptionKeys | SigningKeys
00063 };
00064
00065 enum Result
00066 {
00067 Failure = 0,
00068 Ok = 1,
00069 Canceled = 2
00070 };
00071
00072 class Base;
00073 class Block;
00074
00075 class KPGP_EXPORT Module
00076 {
00077 friend class Block;
00078
00079 private:
00080
00081 Base *pgp;
00082
00083 public:
00084 Module();
00085 virtual ~Module();
00086
00090 virtual void readConfig();
00091 virtual void writeConfig(bool sync);
00092 virtual void init();
00093
00096 bool decrypt( Block& block );
00097
00099 bool verify( Block& block );
00100
00108 Kpgp::Result clearsign( Block& block,
00109 const KeyID& keyId, const QByteArray& charset = 0 );
00110
00119 Kpgp::Result encrypt( Block& block,
00120 const QStringList& receivers, const KeyID& keyId,
00121 bool sign, const QByteArray& charset = 0 );
00122
00130 Kpgp::Result getEncryptionKeys( KeyIDList& encryptionKeyIds,
00131 const QStringList& recipients,
00132 const KeyID& keyId );
00133
00143 int encryptionPossible( const QStringList& recipients );
00144
00145 protected:
00146 int doEncSign( Block& block, const KeyIDList& recipientKeyIds, bool sign );
00147
00148 public:
00150 bool signKey( const KeyID& keyID );
00151
00153 const KeyList publicKeys();
00154
00156 const KeyList secretKeys();
00157
00160 void readPublicKeys( bool reread = false );
00161
00164 void readSecretKeys( bool reread = false );
00165
00167 QByteArray getAsciiPublicKey( const KeyID& keyID );
00168
00172 Key* publicKey( const KeyID& keyID );
00173
00177 Key* publicKey( const QString& userID );
00178
00182 Key* secretKey( const KeyID& keyID );
00183
00187 Validity keyTrust( const KeyID& keyID );
00188
00193 Validity keyTrust( const QString& userID );
00194
00198 bool isTrusted( const KeyID& keyID );
00199
00203 Key* rereadKey( const KeyID& keyID, const bool readTrust = true );
00204
00207 bool changePassPhrase();
00208
00211 void setUser(const KeyID& keyID);
00213 const KeyID user() const;
00214
00216 void setEncryptToSelf(bool flag);
00217 bool encryptToSelf(void) const;
00218
00224 void setStorePassPhrase(bool);
00225 bool storePassPhrase(void) const;
00226
00228 void clear(const bool erasePassPhrase = false);
00229
00231 const QString lastErrorMsg(void) const;
00232
00233
00234 enum PGPType { tAuto, tGPG, tPGP2, tPGP5, tPGP6, tOff } pgpType;
00235
00236
00237 bool havePGP(void) const;
00238
00240 bool usePGP(void) const { return (havePGP() && (pgpType != tOff)); }
00241
00242
00243 void setShowCipherText(const bool flag);
00244 bool showCipherText(void) const;
00245
00246
00247 void setShowKeyApprovalDlg(const bool flag);
00248 bool showKeyApprovalDlg(void) const;
00249
00254 KeyID selectSecretKey( const QString& title,
00255 const QString& text = QString(),
00256 const KeyID& keyId = KeyID() );
00257
00264 KeyID selectPublicKey( const QString& title,
00265 const QString& text = QString(),
00266 const KeyID& oldKeyId = KeyID(),
00267 const QString& address = QString(),
00268 const unsigned int allowedKeys = AllKeys );
00269
00276 KeyIDList selectPublicKeys( const QString& title,
00277 const QString& text = QString(),
00278 const KeyIDList& oldKeyIds = KeyIDList(),
00279 const QString& address = QString(),
00280 const unsigned int allowedKeys = AllKeys );
00281
00282
00283
00287 EncryptPref encryptionPreference( const QString& address );
00288
00292 void setEncryptionPreference( const QString& address,
00293 const EncryptPref pref );
00294
00295
00296
00298 static Kpgp::Module *getKpgp();
00299
00301 static KConfig *getConfig();
00302
00317 static bool prepareMessageForDecryption( const QByteArray& msg,
00318 QList<Block>& pgpBlocks,
00319 QList<QByteArray>& nonPgpBlocks );
00320
00321 private:
00323 bool haveTrustedEncryptionKey( const QString& person );
00324
00326 KeyIDList getEncryptionKeys( const QString& person );
00327
00329 bool setPassPhrase(const QString &pass);
00330
00337 int prepare(bool needPassPhrase=false, Block* block = 0 );
00338
00340 void cleanupPass() { if (!storePass) wipePassPhrase(); }
00341
00344 void wipePassPhrase(bool free=false);
00345
00346
00347 QString canonicalAddress( const QString& person );
00348
00351 KeyID selectKey( const KeyList& keys,
00352 const QString& title,
00353 const QString& text = QString(),
00354 const KeyID& keyId = KeyID(),
00355 const unsigned int allowedKeys = AllKeys );
00356
00359 KeyIDList selectKeys( const KeyList& keys,
00360 const QString& title,
00361 const QString& text = QString(),
00362 const KeyIDList& keyIds = KeyIDList(),
00363 const unsigned int allowedKeys = AllKeys );
00364
00369 KeyID selectKey( bool& rememberChoice,
00370 const KeyList& keys,
00371 const QString& title,
00372 const QString& text = QString(),
00373 const KeyID& keyId = KeyID(),
00374 const unsigned int allowedKeys = AllKeys );
00375
00380 KeyIDList selectKeys( bool& rememberChoice,
00381 const KeyList& keys,
00382 const QString& title,
00383 const QString& text = QString(),
00384 const KeyIDList& keyIds = KeyIDList(),
00385 const unsigned int allowedKeys = AllKeys );
00386
00390 KeyIDList keysForAddress( const QString& address );
00391
00394 void setKeysForAddress( const QString& address, const KeyIDList& keyIDs );
00395
00397 void removeKeyForAddress( const QString& address );
00398
00402 void readAddressData();
00403
00407 void writeAddressData();
00408
00409 bool checkForPGP(void);
00410 void assignPGPBase(void);
00411
00412 static Kpgp::Module *kpgpObject;
00413 KConfig *config;
00414
00415 struct AddressData {
00416 KeyIDList keyIds;
00417 EncryptPref encrPref;
00418 };
00419 typedef QMap<QString, AddressData> AddressDataDict;
00420 AddressDataDict addressDataDict;
00421
00422 KeyList mPublicKeys;
00423 bool mPublicKeysCached : 1;
00424 KeyList mSecretKeys;
00425 bool mSecretKeysCached : 1;
00426
00427 bool storePass : 1;
00428 char * passphrase;
00429 size_t passphrase_buffer_len;
00430
00431 QString errMsg;
00432
00433 KeyID pgpUser;
00434 bool flagEncryptToSelf : 1;
00435
00436 bool havePgp : 1;
00437 bool havePGP5 : 1;
00438 bool haveGpg : 1;
00439 bool havePassPhrase : 1;
00440 bool showEncryptionResult : 1;
00441 bool mShowKeyApprovalDlg : 1;
00442 };
00443
00444
00445
00446 inline void
00447 Module::setShowKeyApprovalDlg( const bool flag )
00448 {
00449 mShowKeyApprovalDlg = flag;
00450 }
00451
00452 inline bool
00453 Module::showKeyApprovalDlg( void ) const
00454 {
00455 return mShowKeyApprovalDlg;
00456 }
00457
00458
00459
00460 }
00461
00462 #endif
00463