libkpgp

kpgp.h

00001 /*
00002     kpgp.h
00003 
00004     Copyright (C) 2001,2002 the KPGP authors
00005     See file AUTHORS.kpgp for details
00006 
00007     This file is part of KPGP, the KDE PGP/GnuPG support library.
00008 
00009     KPGP is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program; if not, write to the Free Software Foundation,
00016     Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
00017  */
00018 
00019 #ifndef KPGP_H
00020 #define KPGP_H
00021 
00022 #include <stdio.h>
00023 #include <qstring.h>
00024 #include <qstrlist.h>
00025 #include <qdialog.h>
00026 #include <qwidget.h>
00027 #include <qcombobox.h>
00028 #include <qlayout.h>
00029 #include <qpushbutton.h>
00030 #include <qlistview.h>
00031 #include <qbuttongroup.h>
00032 #include <qradiobutton.h>
00033 #include <qmultilineedit.h>
00034 #include <qcheckbox.h>
00035 
00036 #include <kdialogbase.h>
00037 
00038 #include "kpgpkey.h"
00039 #include <kdepimmacros.h>
00040 
00041 class QLineEdit;
00042 class QCursor;
00043 class QCheckBox;
00044 class QGridLayout;
00045 
00046 class KConfig;
00047 
00048 namespace Kpgp {
00049 
00060 enum { PublicKeys = 1,
00061        SecretKeys = 2,
00062        EncryptionKeys = 4,
00063        SigningKeys = 8,
00064        ValidKeys = 16,
00065        TrustedKeys = 32,
00066        AllKeys = PublicKeys | SecretKeys | EncryptionKeys | SigningKeys,
00067        PubSecKeys = PublicKeys | SecretKeys,
00068        EncrSignKeys = EncryptionKeys | SigningKeys
00069 };
00070 
00071 enum Result
00072 {
00073        Failure = 0,
00074        Ok = 1,
00075        Canceled = 2
00076 };
00077 
00078 class Base;
00079 class Block;
00080 
00081 class KDE_EXPORT Module
00082 {
00083   friend class Block;
00084 
00085 private:
00086     // the class running pgp
00087     Base *pgp;
00088 
00089 public:
00090     Module();
00091     virtual ~Module();
00092 
00096   virtual void readConfig();
00097   virtual void writeConfig(bool sync);
00098   virtual void init();
00099 
00102   bool decrypt( Block& block );
00103 
00105   bool verify( Block& block );
00106 
00114   Kpgp::Result clearsign( Block& block,
00115                   const KeyID& keyId, const QCString& charset = 0 );
00116 
00125   Kpgp::Result encrypt( Block& block,
00126                 const QStringList& receivers, const KeyID& keyId,
00127                 bool sign, const QCString& charset = 0 );
00128 
00136   Kpgp::Result getEncryptionKeys( KeyIDList& encryptionKeyIds,
00137                                   const QStringList& recipients,
00138                                   const KeyID& keyId );
00139 
00149   int encryptionPossible( const QStringList& recipients );
00150 
00151 protected:
00152   int doEncSign( Block& block, const KeyIDList& recipientKeyIds, bool sign );
00153 
00154 public:
00156   bool signKey( const KeyID& keyID );
00157 
00159   const KeyList publicKeys();
00160 
00162   const KeyList secretKeys();
00163 
00166   void readPublicKeys( bool reread = false );
00167 
00170   void readSecretKeys( bool reread = false );
00171 
00173   QCString getAsciiPublicKey( const KeyID& keyID );
00174 
00178   Key* publicKey( const KeyID& keyID );
00179 
00183   Key* publicKey( const QString& userID );
00184 
00188   Key* secretKey( const KeyID& keyID );
00189 
00193   Validity keyTrust( const KeyID& keyID );
00194 
00199   Validity keyTrust( const QString& userID );
00200 
00204   bool isTrusted( const KeyID& keyID );
00205 
00209   Key* rereadKey( const KeyID& keyID, const bool readTrust = true );
00210 
00213   bool changePassPhrase();
00214 
00217   void setUser(const KeyID& keyID);
00219   const KeyID user() const;
00220 
00222   void setEncryptToSelf(bool flag);
00223   bool encryptToSelf(void) const;
00224 
00230   void setStorePassPhrase(bool);
00231   bool storePassPhrase(void) const;
00232 
00234   void clear(const bool erasePassPhrase = FALSE);
00235 
00237   const QString lastErrorMsg(void) const;
00238 
00239   // what version of PGP/GPG should we use
00240   enum PGPType { tAuto, tGPG, tPGP2, tPGP5, tPGP6, tOff } pgpType;
00241 
00242   // did we find a pgp executable?
00243   bool havePGP(void) const;
00244 
00246   bool usePGP(void) const { return (havePGP() && (pgpType != tOff)); }
00247 
00248   // show the result of encryption/signing?
00249   void setShowCipherText(const bool flag);
00250   bool showCipherText(void) const;
00251 
00252   // show the encryption keys for approval?
00253   void setShowKeyApprovalDlg(const bool flag);
00254   bool showKeyApprovalDlg(void) const;
00255 
00260   KeyID selectSecretKey( const QString& title,
00261                          const QString& text = QString::null,
00262                          const KeyID& keyId = KeyID() );
00263 
00270   KeyID selectPublicKey( const QString& title,
00271                          const QString& text = QString::null,
00272                          const KeyID& oldKeyId = KeyID(),
00273                          const QString& address = QString::null,
00274                          const unsigned int allowedKeys = AllKeys );
00275 
00282   KeyIDList selectPublicKeys( const QString& title,
00283                               const QString& text = QString::null,
00284                               const KeyIDList& oldKeyIds = KeyIDList(),
00285                               const QString& address = QString::null,
00286                               const unsigned int allowedKeys = AllKeys );
00287 
00288   // FIXME: key management
00289 
00293   EncryptPref encryptionPreference( const QString& address );
00294 
00298   void setEncryptionPreference( const QString& address,
00299                                 const EncryptPref pref );
00300 
00301   // -- static member functions --------------------------------------------
00302 
00304   static Kpgp::Module *getKpgp();
00305 
00307   static KConfig *getConfig();
00308 
00323   static bool prepareMessageForDecryption( const QCString& msg,
00324                                            QPtrList<Block>& pgpBlocks,
00325                                            QStrList& nonPgpBlocks );
00326 
00327 private:
00329   bool haveTrustedEncryptionKey( const QString& person );
00330 
00332   KeyIDList getEncryptionKeys( const QString& person );
00333 
00335   bool setPassPhrase(const char* pass);
00336 
00343   int prepare(bool needPassPhrase=FALSE, Block* block = 0 );
00344 
00346   void cleanupPass() { if (!storePass) wipePassPhrase(); }
00347 
00350   void wipePassPhrase(bool free=false);
00351 
00352   // transform an address into canonical form
00353   QString canonicalAddress( const QString& person );
00354 
00357   KeyID selectKey( const KeyList& keys,
00358                    const QString& title,
00359                    const QString& text = QString::null,
00360                    const KeyID& keyId = KeyID(),
00361                    const unsigned int allowedKeys = AllKeys );
00362 
00365   KeyIDList selectKeys( const KeyList& keys,
00366                         const QString& title,
00367                         const QString& text = QString::null,
00368                         const KeyIDList& keyIds = KeyIDList(),
00369                         const unsigned int allowedKeys = AllKeys );
00370 
00375   KeyID selectKey( bool& rememberChoice,
00376                    const KeyList& keys,
00377                    const QString& title,
00378                    const QString& text = QString::null,
00379                    const KeyID& keyId = KeyID(),
00380                    const unsigned int allowedKeys = AllKeys );
00381 
00386   KeyIDList selectKeys( bool& rememberChoice,
00387                         const KeyList& keys,
00388                         const QString& title,
00389                         const QString& text = QString::null,
00390                         const KeyIDList& keyIds = KeyIDList(),
00391                         const unsigned int allowedKeys = AllKeys );
00392 
00396   KeyIDList keysForAddress( const QString& address );
00397 
00400   void setKeysForAddress( const QString& address, const KeyIDList& keyIDs );
00401 
00403   void removeKeyForAddress( const QString& address );
00404 
00408   void readAddressData();
00409 
00413   void writeAddressData();
00414 
00415   bool checkForPGP(void);
00416   void assignPGPBase(void);
00417 
00418   static Kpgp::Module *kpgpObject;
00419   KConfig *config;
00420 
00421   struct AddressData {
00422     KeyIDList keyIds;
00423     EncryptPref encrPref;
00424   };
00425   typedef QMap<QString, AddressData> AddressDataDict;
00426   AddressDataDict addressDataDict;
00427 
00428   KeyList mPublicKeys;
00429   bool mPublicKeysCached : 1; // did we already read the public keys?
00430   KeyList mSecretKeys;
00431   bool mSecretKeysCached : 1; // did we already read the secret keys?
00432 
00433   bool storePass : 1;
00434   char * passphrase;
00435   size_t passphrase_buffer_len;
00436 
00437   QString errMsg;
00438 
00439   KeyID pgpUser; // the key ID which is used to sign/encrypt to self
00440   bool flagEncryptToSelf : 1;
00441 
00442   bool havePgp : 1;
00443   bool havePGP5 : 1;
00444   bool haveGpg : 1;
00445   bool havePassPhrase : 1;
00446   bool showEncryptionResult : 1;
00447   bool mShowKeyApprovalDlg : 1;
00448 }; // class Module
00449 
00450 // -- inlined member functions ---------------------------------------------
00451 
00452 inline void
00453 Module::setShowKeyApprovalDlg( const bool flag )
00454 {
00455   mShowKeyApprovalDlg = flag;
00456 }
00457 
00458 inline bool
00459 Module::showKeyApprovalDlg( void ) const
00460 {
00461   return mShowKeyApprovalDlg;
00462 }
00463 
00464 // -------------------------------------------------------------------------
00465 
00466 } // namespace Kpgp
00467 
00468 #endif
00469 
KDE Home | KDE Accessibility Home | Description of Access Keys