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