libkpgp

kpgpkey.h

00001 /*
00002     kpgpkey.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 KPGPKEY_H
00020 #define KPGPKEY_H
00021 
00022 #include <time.h>
00023 
00024 #include <qcstring.h>
00025 #include <qstring.h>
00026 #include <qstringlist.h>
00027 #include <qvaluelist.h>
00028 
00029 namespace Kpgp {
00030 
00034 typedef enum
00035 { // this is copied from gpgme.h which is a part of GPGME
00036   KPGP_VALIDITY_UNKNOWN = 0,   // the trust hasn't been determined
00037   KPGP_VALIDITY_UNDEFINED = 1, // trust is undefined
00038   KPGP_VALIDITY_NEVER = 2,
00039   KPGP_VALIDITY_MARGINAL = 3,
00040   KPGP_VALIDITY_FULL = 4,
00041   KPGP_VALIDITY_ULTIMATE = 5
00042 } Validity;
00043 
00046 typedef enum
00047 {
00048   NeverEncrypt = -1,
00049   UnknownEncryptPref = 0,
00050   AlwaysEncrypt = 1,
00051   AlwaysEncryptIfPossible = 2,
00052   AlwaysAskForEncryption = 3,
00053   AskWheneverPossible = 4
00054 } EncryptPref;
00055 
00056 
00057 typedef QCString KeyID;
00058 
00059 class KeyIDList : public QValueList<KeyID>
00060 {
00061  public:
00062   KeyIDList() { }
00063   KeyIDList( const KeyIDList& l ) : QValueList<KeyID>(l) { }
00064   KeyIDList( const QValueList<KeyID>& l ) : QValueList<KeyID>(l) { }
00065   KeyIDList( const KeyID& i ) { append(i); }
00066 
00067   QStringList toStringList() const;
00068 
00069   static KeyIDList fromStringList( const QStringList& );
00070 };
00071 
00074 class UserID
00075 {
00076  public:
00078   UserID(const QString& str,
00079              const Validity validity = KPGP_VALIDITY_UNKNOWN,
00080              const bool revoked = false,
00081              const bool invalid = false);
00082   ~UserID() {};
00083   
00085   QString text() const;
00086 
00088   bool revoked() const;
00089 
00091   bool invalid() const;
00092 
00094   Validity validity() const;
00095 
00097   void setText(const QString& str);
00098 
00100   void setRevoked(const bool revoked);
00101 
00103   void setInvalid(const bool invalid);
00104 
00107   void setValidity(const Validity validity);
00108 
00109  protected:
00110   bool mRevoked : 1;
00111   bool mInvalid : 1;
00112   Validity mValidity;
00113   QString mText;
00114 };
00115 
00116 typedef QPtrList<UserID> UserIDList;
00117 typedef QPtrListIterator<UserID> UserIDListIterator;
00118 
00119 inline QString UserID::text() const
00120 {
00121   return mText;
00122 }
00123 
00124 inline bool UserID::revoked() const
00125 {
00126   return mRevoked;
00127 }
00128 
00129 inline bool UserID::invalid() const
00130 {
00131   return mInvalid;
00132 }
00133 
00134 inline Validity UserID::validity() const
00135 {
00136   return mValidity;
00137 }
00138 
00139 inline void UserID::setText(const QString& str)
00140 {
00141   mText = str;
00142 }
00143 
00144 inline void UserID::setRevoked(const bool revoked)
00145 {
00146   mRevoked = revoked;
00147 }
00148 
00149 inline void UserID::setInvalid(const bool invalid)
00150 {
00151   mInvalid = invalid;
00152 }
00153 
00154 inline void UserID::setValidity(const Validity validity)
00155 {
00156   mValidity = validity;
00157 }
00158 
00159 
00162 class Subkey
00163 {
00164  public:
00166   Subkey(const KeyID& keyID, const bool secret = false);
00167   ~Subkey() {};
00168 
00170   bool secret() const;
00171 
00173   bool revoked() const;
00174 
00176   bool expired() const;
00177 
00179   bool disabled() const;
00180 
00182   bool invalid() const;
00183 
00185   bool canEncrypt() const;
00186 
00188   bool canSign() const;
00189 
00191   bool canCertify() const;
00192 
00194   unsigned int keyAlgorithm() const;
00195 
00197   unsigned int keyLength() const;
00198 
00201   KeyID longKeyID() const;
00202 
00204   KeyID keyID() const;
00205   
00207   QCString fingerprint() const;
00208 
00210   time_t creationDate() const;
00211 
00213   time_t expirationDate() const;
00214 
00216   void setSecret(const bool secret);
00217 
00219   void setRevoked(const bool revoked);
00220 
00222   void setExpired(const bool expired);
00223 
00225   void setDisabled(const bool disabled);
00226 
00228   void setInvalid(const bool invalid);
00229 
00232   void setCanEncrypt(const bool canEncrypt);
00233 
00236   void setCanSign(const bool canSign);
00237 
00240   void setCanCertify(const bool canCertify);
00241 
00243   void setKeyAlgorithm(const unsigned int keyAlgo);
00244 
00246   void setKeyLength(const unsigned int keyLen);
00247 
00249   void setKeyID(const KeyID& keyID);
00250 
00252   void setFingerprint(const QCString& fingerprint);
00253 
00256   void setCreationDate(const time_t creationDate);
00257 
00260   void setExpirationDate(const time_t expirationDate);
00261 
00262  protected:
00263   bool mSecret : 1;
00264   /* various flags */
00265   bool mRevoked : 1;
00266   bool mExpired : 1;
00267   bool mDisabled : 1;
00268   bool mInvalid : 1;
00269   bool mCanEncrypt : 1;
00270   bool mCanSign : 1;
00271   bool mCanCertify : 1;
00272 
00273   unsigned int mKeyAlgo;
00274   unsigned int mKeyLen;
00275   KeyID mKeyID;
00276   QCString mFingerprint;
00277   time_t mTimestamp; /* -1 for invalid, 0 for not available */
00278   time_t mExpiration; /* -1 for never, 0 for not available */
00279 };
00280 
00281 inline bool Subkey::secret() const
00282 {
00283   return mSecret;
00284 }
00285 
00286 inline bool Subkey::revoked() const
00287 {
00288   return mRevoked;
00289 }
00290 
00291 inline bool Subkey::expired() const
00292 {
00293   return mExpired;
00294 }
00295 
00296 inline bool Subkey::disabled() const
00297 {
00298   return mDisabled;
00299 }
00300 
00301 inline bool Subkey::invalid() const
00302 {
00303   return mInvalid;
00304 }
00305 
00306 inline bool Subkey::canEncrypt() const
00307 {
00308   return mCanEncrypt;
00309 }
00310 
00311 inline bool Subkey::canSign() const
00312 {
00313   return mCanSign;
00314 }
00315 
00316 inline bool Subkey::canCertify() const
00317 {
00318   return mCanCertify;
00319 }
00320 
00321 inline unsigned int Subkey::keyAlgorithm() const
00322 {
00323   return mKeyAlgo;
00324 }
00325 
00326 inline unsigned int Subkey::keyLength() const
00327 {
00328   return mKeyLen;
00329 }
00330 
00331 inline KeyID Subkey::longKeyID() const
00332 {
00333   return mKeyID;
00334 }
00335 
00336 inline KeyID Subkey::keyID() const
00337 {
00338   return mKeyID.right(8);
00339 }
00340 
00341 inline QCString Subkey::fingerprint() const
00342 {
00343   return mFingerprint;
00344 }
00345 
00346 inline time_t Subkey::creationDate() const
00347 {
00348   return mTimestamp;
00349 }
00350 
00351 inline time_t Subkey::expirationDate() const
00352 {
00353   return mExpiration;
00354 }
00355 
00356 inline void Subkey::setSecret(const bool secret)
00357 {
00358   mSecret = secret;
00359 }
00360 
00361 inline void Subkey::setRevoked(const bool revoked)
00362 {
00363   mRevoked = revoked;
00364 }
00365 
00366 inline void Subkey::setExpired(const bool expired)
00367 {
00368   mExpired = expired;
00369 }
00370 
00371 inline void Subkey::setDisabled(const bool disabled)
00372 {
00373   mDisabled = disabled;
00374 }
00375 
00376 inline void Subkey::setInvalid(const bool invalid)
00377 {
00378   mInvalid = invalid;
00379 }
00380 
00381 inline void Subkey::setCanEncrypt(const bool canEncrypt)
00382 {
00383   mCanEncrypt = canEncrypt;
00384 }
00385 
00386 inline void Subkey::setCanSign(const bool canSign)
00387 {
00388   mCanSign = canSign;
00389 }
00390 
00391 inline void Subkey::setCanCertify(const bool canCertify)
00392 {
00393   mCanCertify = canCertify;
00394 }
00395 
00396 inline void Subkey::setKeyAlgorithm(const unsigned int keyAlgo)
00397 {
00398   mKeyAlgo = keyAlgo;
00399 }
00400 
00401 inline void Subkey::setKeyLength(const unsigned int keyLen)
00402 {
00403   mKeyLen = keyLen;
00404 }
00405 
00406 inline void Subkey::setKeyID(const KeyID& keyID)
00407 {
00408   mKeyID = keyID;
00409 }
00410 
00411 inline void Subkey::setFingerprint(const QCString& fingerprint)
00412 {
00413   mFingerprint = fingerprint;
00414 }
00415 
00416 inline void Subkey::setCreationDate(const time_t creationDate)
00417 {
00418   mTimestamp = creationDate;
00419 }
00420 
00421 inline void Subkey::setExpirationDate(const time_t expirationDate)
00422 {
00423   mExpiration = expirationDate;
00424 }
00425 
00426 typedef QPtrList<Subkey> SubkeyList;
00427 typedef QPtrListIterator<Subkey> SubkeyListIterator;
00428 
00429 
00432 class Key
00433 {
00434  public:
00437   Key( const KeyID& keyid = KeyID(),
00438        const QString& uid = QString::null,
00439        const bool secret = false);
00440   ~Key();
00441 
00443   void clear();
00444 
00446   bool secret() const;
00447 
00449   bool revoked() const;
00450 
00452   bool expired() const;
00453 
00455   bool disabled() const;
00456 
00458   bool invalid() const;
00459 
00461   bool canEncrypt() const;
00462 
00464   bool canSign() const;
00465 
00467   bool canCertify() const;
00468 
00470   void setSecret(const bool secret);
00471 
00473   void setRevoked(const bool revoked);
00474 
00476   void setExpired(const bool expired);
00477 
00479   void setDisabled(const bool disabled);
00480 
00482   void setInvalid(const bool invalid);
00483 
00486   void setCanEncrypt(const bool canEncrypt);
00487 
00490   void setCanSign(const bool canSign);
00491 
00494   void setCanCertify(const bool canCertify);
00495 
00496 
00498   EncryptPref encryptionPreference();
00499 
00501   void setEncryptionPreference( const EncryptPref encrPref );
00502 
00503 
00506   QString primaryUserID() const;
00507 
00510   KeyID primaryKeyID() const;
00511 
00514   QCString primaryFingerprint() const;
00515 
00517   bool isNull() const;
00518 
00521   time_t creationDate() const;
00522 
00526   Validity keyTrust() const;
00527 
00530   Validity keyTrust( const QString& uid ) const;
00531 
00536   void cloneKeyTrust( const Key* key );
00537 
00541   bool isValid() const;
00542 
00546   bool isValidEncryptionKey() const;
00547 
00550   bool isValidSigningKey() const;
00551 
00553   const UserIDList userIDs() const;
00554 
00556   const SubkeyList subkeys() const;
00557 
00560   void addUserID(const QString& uid,
00561                  const Validity validity = KPGP_VALIDITY_UNKNOWN,
00562                  const bool revoked = false,
00563                  const bool invalid = false);
00564 
00566   void addUserID(const UserID *userID);
00567 
00571   bool matchesUserID(const QString& str, bool cs = true);
00572 
00575   void addSubkey(const KeyID& keyID, const bool secret = false);
00576 
00578   void addSubkey(const Subkey *subkey);
00579 
00581   Subkey *getSubkey(const KeyID& keyID);
00582 
00584   void setFingerprint(const KeyID& keyID, const QCString& fpr);
00585 
00586  protected:
00587   bool mSecret : 1;
00588   /* global flags */
00589   bool mRevoked : 1;
00590   bool mExpired : 1;
00591   bool mDisabled : 1;
00592   bool mInvalid : 1;
00593   bool mCanEncrypt : 1;
00594   bool mCanSign : 1;
00595   bool mCanCertify : 1;
00596   
00597   EncryptPref mEncryptPref;
00598 
00599   SubkeyList mSubkeys;
00600   UserIDList mUserIDs;
00601 };
00602 
00603 inline bool Key::secret() const
00604 {
00605   return mSecret;
00606 }
00607 
00608 inline bool Key::revoked() const
00609 {
00610   return mRevoked;
00611 }
00612 
00613 inline bool Key::expired() const
00614 {
00615   return mExpired;
00616 }
00617 
00618 inline bool Key::disabled() const
00619 {
00620   return mDisabled;
00621 }
00622 
00623 inline bool Key::invalid() const
00624 {
00625   return mInvalid;
00626 }
00627 
00628 inline bool Key::canEncrypt() const
00629 {
00630   return mCanEncrypt;
00631 }
00632 
00633 inline bool Key::canSign() const
00634 {
00635   return mCanSign;
00636 }
00637 
00638 inline bool Key::canCertify() const
00639 {
00640   return mCanCertify;
00641 }
00642 
00643 inline void Key::setSecret(const bool secret)
00644 {
00645   mSecret = secret;
00646 }
00647 
00648 inline void Key::setRevoked(const bool revoked)
00649 {
00650   mRevoked = revoked;
00651 }
00652 
00653 inline void Key::setExpired(const bool expired)
00654 {
00655   mExpired = expired;
00656 }
00657 
00658 inline void Key::setDisabled(const bool disabled)
00659 {
00660   mDisabled = disabled;
00661 }
00662 
00663 inline void Key::setInvalid(const bool invalid)
00664 {
00665   mInvalid = invalid;
00666 }
00667 
00668 inline void Key::setCanEncrypt(const bool canEncrypt)
00669 {
00670   mCanEncrypt = canEncrypt;
00671 }
00672 
00673 inline void Key::setCanSign(const bool canSign)
00674 {
00675   mCanSign = canSign;
00676 }
00677 
00678 inline void Key::setCanCertify(const bool canCertify)
00679 {
00680   mCanCertify = canCertify;
00681 }
00682 
00683 inline EncryptPref Key::encryptionPreference()
00684 {
00685   return mEncryptPref;
00686 }
00687 
00688 inline void Key::setEncryptionPreference( const EncryptPref encrPref )
00689 {
00690   mEncryptPref = encrPref;
00691 }
00692 
00693 inline QString Key::primaryUserID() const
00694 {
00695   UserID *uid = mUserIDs.getFirst();
00696 
00697   if (uid)
00698     return uid->text();
00699   else
00700     return QString::null;
00701 }
00702 
00703 inline KeyID Key::primaryKeyID() const 
00704 {
00705   Subkey *key = mSubkeys.getFirst();
00706 
00707   if (key)
00708     return key->keyID();
00709   else
00710     return KeyID();
00711 }
00712 
00713 inline QCString Key::primaryFingerprint() const
00714 {
00715   Subkey *key = mSubkeys.getFirst();
00716 
00717   if (key)
00718     return key->fingerprint();
00719   else
00720     return QCString();
00721 }
00722 
00723 inline const UserIDList Key::userIDs() const
00724 { 
00725   return mUserIDs;
00726 }
00727 
00728 inline const SubkeyList Key::subkeys() const
00729 { 
00730   return mSubkeys;
00731 }
00732 
00733 inline bool Key::isNull() const
00734 {
00735   return (mUserIDs.isEmpty() || mSubkeys.isEmpty());
00736 }
00737 
00738 inline time_t Key::creationDate() const
00739 {
00740   if( !mSubkeys.isEmpty() )
00741     return mSubkeys.getFirst()->creationDate();
00742   else
00743     return -1;
00744 }
00745 
00746 inline void Key::addUserID(const UserID *userID)
00747 {
00748   if (userID)
00749     mUserIDs.append(userID);
00750 }
00751 
00752 inline void Key::addSubkey(const Subkey *subkey)
00753 {
00754   if (subkey)
00755     mSubkeys.append(subkey);
00756 }
00757 
00758 
00759 
00760 typedef QPtrList<Key> KeyListBase;
00761 typedef QPtrListIterator<Key> KeyListIterator;
00762 
00763 class KeyList : public KeyListBase
00764 {
00765  public:
00766   ~KeyList()
00767     { clear(); }
00768 
00769  private:
00770   int compareItems( QPtrCollection::Item s1, QPtrCollection::Item s2 )
00771     { 
00772       // sort case insensitively by the primary User IDs
00773       return QString::compare((static_cast<Key*>(s1))->primaryUserID().lower(),
00774                               (static_cast<Key*>(s2))->primaryUserID().lower());
00775     }
00776 };
00777 
00778 } // namespace Kpgp
00779 
00780 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys