libkpgp

kpgpkey.cpp

00001 /*
00002     kpgpkey.cpp
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 #include "kpgpkey.h"
00020 #include "kdebug.h"
00021 
00022 namespace Kpgp {
00023 
00024 /* member functions of Kpgp::KeyIDList --------------------------------- */
00025 
00028 QStringList KeyIDList::toStringList() const
00029 {
00030   QStringList res;
00031   for( KeyIDList::ConstIterator it = begin(); it != end(); ++it ) {
00032     res << (*it).data();
00033   }
00034   return res;
00035 }
00036 
00039 KeyIDList KeyIDList::fromStringList( const QStringList& l )
00040 {
00041   KeyIDList res;
00042   for( QStringList::ConstIterator it = l.begin(); it != l.end(); ++it ) {
00043     res << (*it).local8Bit();
00044   }
00045   return res;
00046 }
00047 
00048 /* member functions of Kpgp::UserID ------------------------------------ */
00049 
00050 UserID::UserID(const QString& str, const Validity validity,
00051                const bool revoked, const bool invalid)
00052 {
00053   mText = str;
00054   mValidity = validity;
00055   mRevoked = revoked;
00056   mInvalid = invalid;
00057 }
00058 
00059 
00060 /* member functions of Kpgp::Subkey ------------------------------------ */
00061 
00062 Subkey::Subkey(const KeyID& keyID, const bool secret)
00063 {
00064   mSecret = secret;
00065   mKeyID = keyID;
00066 
00067   mRevoked = false;
00068   mExpired = false;
00069   mDisabled = false;
00070   mInvalid = false;
00071   mCanEncrypt = false;
00072   mCanSign = false;
00073   mCanCertify = false;
00074   mKeyAlgo = 0;
00075   mKeyLen = 0;
00076   mFingerprint = 0;
00077   mTimestamp = 0;
00078   mExpiration = 0;
00079 }
00080 
00081 
00082 /* member functions of Kpgp::Key --------------------------------------- */
00083 
00084 Key::Key(const KeyID& keyid, const QString& uid, const bool secret) :
00085   mSubkeys(), mUserIDs()
00086 {
00087   mSecret = secret;
00088   if (!keyid.isEmpty())
00089     addSubkey(keyid, secret);
00090   if (!uid.isEmpty())
00091     addUserID(uid);
00092 
00093   mRevoked = false;
00094   mExpired = false;
00095   mDisabled = false;
00096   mInvalid = false;
00097   mCanEncrypt = false;
00098   mCanSign = false;
00099   mCanCertify = false;
00100 
00101   mEncryptPref = UnknownEncryptPref;
00102 }
00103 
00104 Key::~Key()
00105 {
00106   //kdDebug(5100) << "Kpgp::Key: Deleting key " << primaryUserID() << endl;
00107   mUserIDs.setAutoDelete(true);
00108   mUserIDs.clear();
00109   mSubkeys.setAutoDelete(true);
00110   mSubkeys.clear();
00111 }
00112 
00113 void
00114 Key::clear()
00115 {
00116   mSecret = false;
00117   mRevoked = false;
00118   mExpired = false;
00119   mDisabled = false;
00120   mInvalid = false;
00121   mCanEncrypt = false;
00122   mCanSign = false;
00123   mCanCertify = false;
00124 
00125   mEncryptPref = UnknownEncryptPref;
00126 
00127   mSubkeys.setAutoDelete(true);
00128   mSubkeys.clear();
00129   mUserIDs.setAutoDelete(true);
00130   mUserIDs.clear();
00131 }
00132 
00133 Validity
00134 Key::keyTrust() const
00135 {
00136   Validity trust = KPGP_VALIDITY_UNKNOWN;
00137 
00138   for( UserIDListIterator it(mUserIDs); it.current(); ++it )
00139   {
00140     if( (*it)->validity() > trust )
00141       trust = (*it)->validity();
00142   }
00143   
00144   return trust;
00145 }
00146 
00147 Validity
00148 Key::keyTrust( const QString& uid ) const
00149 {
00150   Validity trust = KPGP_VALIDITY_UNKNOWN;
00151 
00152   if( uid.isEmpty() )
00153     return trust;
00154 
00155   for( UserIDListIterator it(mUserIDs); it.current(); ++it )
00156   {
00157     if( (*it)->text() == uid )
00158       trust = (*it)->validity();
00159   }
00160   
00161   return trust;
00162 }
00163 
00164 void
00165 Key::cloneKeyTrust( const Key* key )
00166 {
00167   if( !key )
00168     return;
00169 
00170   for( UserIDListIterator it(mUserIDs); it.current(); ++it )
00171   {
00172     (*it)->setValidity( key->keyTrust( (*it)->text() ) );
00173   }
00174 }
00175 
00176 bool
00177 Key::isValid() const
00178 {
00179   return ( !mRevoked && !mExpired && !mDisabled && !mInvalid );
00180 }
00181 
00182 
00183 bool
00184 Key::isValidEncryptionKey() const
00185 {
00186   return ( !mRevoked && !mExpired && !mDisabled && !mInvalid && mCanEncrypt );
00187 }
00188 
00189 
00190 bool
00191 Key::isValidSigningKey() const
00192 {
00193   return ( !mRevoked && !mExpired && !mDisabled && !mInvalid && mCanSign );
00194 }
00195 
00196 
00197 void Key::addUserID(const QString &uid, const Validity validity,
00198                     const bool revoked, const bool invalid)
00199 { 
00200   if (!uid.isEmpty()) {
00201     UserID *userID = new UserID(uid, validity, revoked, invalid);
00202     mUserIDs.append(userID);
00203   }
00204 }
00205 
00206 bool Key::matchesUserID(const QString& str, bool cs)
00207 {
00208   if (str.isEmpty() || mUserIDs.isEmpty())
00209     return false;
00210   
00211   for (UserIDListIterator it(mUserIDs); it.current(); ++it) {
00212     if (((*it)->text().find(str, 0, cs)) != -1)
00213       return true;
00214   }
00215 
00216   return false;
00217 }
00218 
00219 void Key::addSubkey(const KeyID& keyID, const bool secret)
00220 {
00221   if (!keyID.isEmpty()) {
00222     Subkey *key = new Subkey(keyID, secret);
00223     mSubkeys.append(key);
00224   }
00225 }
00226 
00227 Subkey *Key::getSubkey(const KeyID& keyID)
00228 {
00229   if (keyID.isEmpty() || mSubkeys.isEmpty())
00230     return 0;
00231   
00232   // is the given key ID a long (16 chars) or a short (8 chars) key ID?
00233   bool longKeyID = (keyID.length() == 16);
00234 
00235   for (SubkeyListIterator it(mSubkeys); it.current(); ++it) {
00236     if (longKeyID) {
00237       if ((*it)->longKeyID() == keyID)
00238         return (*it);
00239     }
00240     else {
00241       if ((*it)->keyID() == keyID)
00242         return (*it);
00243     }
00244   }
00245 
00246   return 0;
00247 }
00248 
00249 void Key::setFingerprint(const KeyID& keyID, const QCString &fpr)
00250 {
00251   Subkey *key;
00252   if ((key = getSubkey(keyID)) != 0) {
00253     key->setFingerprint(fpr);
00254   }
00255   else
00256     kdDebug(5006) << "Error: Can't set fingerprint. A subkey with key ID 0x"
00257                   << keyID << " doesn't exist." << endl;
00258 }
00259 
00260 } // namespace Kpgp
KDE Home | KDE Accessibility Home | Description of Access Keys