libkpgp

kpgpkey.cpp
1 /*
2  kpgpkey.cpp
3 
4  Copyright (C) 2001,2002 the KPGP authors
5  See file AUTHORS.kpgp for details
6 
7  This file is part of KPGP, the KDE PGP/GnuPG support library.
8 
9  KPGP is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software Foundation,
16  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #include "kpgpkey.h"
20 #include "kdebug.h"
21 
22 namespace Kpgp {
23 
24 /* member functions of Kpgp::KeyIDList --------------------------------- */
25 
28 TQStringList KeyIDList::toStringList() const
29 {
30  TQStringList res;
31  for( KeyIDList::ConstIterator it = begin(); it != end(); ++it ) {
32  res << (*it).data();
33  }
34  return res;
35 }
36 
39 KeyIDList KeyIDList::fromStringList( const TQStringList& l )
40 {
41  KeyIDList res;
42  for( TQStringList::ConstIterator it = l.begin(); it != l.end(); ++it ) {
43  res << (*it).local8Bit();
44  }
45  return res;
46 }
47 
48 /* member functions of Kpgp::UserID ------------------------------------ */
49 
50 UserID::UserID(const TQString& str, const Validity validity,
51  const bool revoked, const bool invalid)
52 {
53  mText = str;
54  mValidity = validity;
55  mRevoked = revoked;
56  mInvalid = invalid;
57 }
58 
59 
60 /* member functions of Kpgp::Subkey ------------------------------------ */
61 
62 Subkey::Subkey(const KeyID& keyID, const bool secret)
63 {
64  mSecret = secret;
65  mKeyID = keyID;
66 
67  mRevoked = false;
68  mExpired = false;
69  mDisabled = false;
70  mInvalid = false;
71  mCanEncrypt = false;
72  mCanSign = false;
73  mCanCertify = false;
74  mKeyAlgo = 0;
75  mKeyLen = 0;
76  mFingerprint = 0;
77  mTimestamp = 0;
78  mExpiration = 0;
79 }
80 
81 
82 /* member functions of Kpgp::Key --------------------------------------- */
83 
84 Key::Key(const KeyID& keyid, const TQString& uid, const bool secret) :
85  mSubkeys(), mUserIDs()
86 {
87  mSecret = secret;
88  if (!keyid.isEmpty())
89  addSubkey(keyid, secret);
90  if (!uid.isEmpty())
91  addUserID(uid);
92 
93  mRevoked = false;
94  mExpired = false;
95  mDisabled = false;
96  mInvalid = false;
97  mCanEncrypt = false;
98  mCanSign = false;
99  mCanCertify = false;
100 
101  mEncryptPref = UnknownEncryptPref;
102 }
103 
104 Key::~Key()
105 {
106  //kdDebug(5100) << "Kpgp::Key: Deleting key " << primaryUserID() << endl;
107  mUserIDs.setAutoDelete(true);
108  mUserIDs.clear();
109  mSubkeys.setAutoDelete(true);
110  mSubkeys.clear();
111 }
112 
113 void
115 {
116  mSecret = false;
117  mRevoked = false;
118  mExpired = false;
119  mDisabled = false;
120  mInvalid = false;
121  mCanEncrypt = false;
122  mCanSign = false;
123  mCanCertify = false;
124 
125  mEncryptPref = UnknownEncryptPref;
126 
127  mSubkeys.setAutoDelete(true);
128  mSubkeys.clear();
129  mUserIDs.setAutoDelete(true);
130  mUserIDs.clear();
131 }
132 
133 Validity
135 {
136  Validity trust = KPGP_VALIDITY_UNKNOWN;
137 
138  for( UserIDListIterator it(mUserIDs); it.current(); ++it )
139  {
140  if( (*it)->validity() > trust )
141  trust = (*it)->validity();
142  }
143 
144  return trust;
145 }
146 
147 Validity
148 Key::keyTrust( const TQString& uid ) const
149 {
150  Validity trust = KPGP_VALIDITY_UNKNOWN;
151 
152  if( uid.isEmpty() )
153  return trust;
154 
155  for( UserIDListIterator it(mUserIDs); it.current(); ++it )
156  {
157  if( (*it)->text() == uid )
158  trust = (*it)->validity();
159  }
160 
161  return trust;
162 }
163 
164 void
165 Key::cloneKeyTrust( const Key* key )
166 {
167  if( !key )
168  return;
169 
170  for( UserIDListIterator it(mUserIDs); it.current(); ++it )
171  {
172  (*it)->setValidity( key->keyTrust( (*it)->text() ) );
173  }
174 }
175 
176 bool
178 {
179  return ( !mRevoked && !mExpired && !mDisabled && !mInvalid );
180 }
181 
182 
183 bool
185 {
186  return ( !mRevoked && !mExpired && !mDisabled && !mInvalid && mCanEncrypt );
187 }
188 
189 
190 bool
192 {
193  return ( !mRevoked && !mExpired && !mDisabled && !mInvalid && mCanSign );
194 }
195 
196 
197 void Key::addUserID(const TQString &uid, const Validity validity,
198  const bool revoked, const bool invalid)
199 {
200  if (!uid.isEmpty()) {
201  UserID *userID = new UserID(uid, validity, revoked, invalid);
202  mUserIDs.append(userID);
203  }
204 }
205 
206 bool Key::matchesUserID(const TQString& str, bool cs)
207 {
208  if (str.isEmpty() || mUserIDs.isEmpty())
209  return false;
210 
211  for (UserIDListIterator it(mUserIDs); it.current(); ++it) {
212  if (((*it)->text().find(str, 0, cs)) != -1)
213  return true;
214  }
215 
216  return false;
217 }
218 
219 void Key::addSubkey(const KeyID& keyID, const bool secret)
220 {
221  if (!keyID.isEmpty()) {
222  Subkey *key = new Subkey(keyID, secret);
223  mSubkeys.append(key);
224  }
225 }
226 
227 Subkey *Key::getSubkey(const KeyID& keyID)
228 {
229  if (keyID.isEmpty() || mSubkeys.isEmpty())
230  return 0;
231 
232  // is the given key ID a long (16 chars) or a short (8 chars) key ID?
233  bool longKeyID = (keyID.length() == 16);
234 
235  for (SubkeyListIterator it(mSubkeys); it.current(); ++it) {
236  if (longKeyID) {
237  if ((*it)->longKeyID() == keyID)
238  return (*it);
239  }
240  else {
241  if ((*it)->keyID() == keyID)
242  return (*it);
243  }
244  }
245 
246  return 0;
247 }
248 
249 void Key::setFingerprint(const KeyID& keyID, const TQCString &fpr)
250 {
251  Subkey *key;
252  if ((key = getSubkey(keyID)) != 0) {
253  key->setFingerprint(fpr);
254  }
255  else
256  kdDebug(5006) << "Error: Can't set fingerprint. A subkey with key ID 0x"
257  << keyID << " doesn't exist." << endl;
258 }
259 
260 } // namespace Kpgp
This class is used to store information about a PGP key.
Definition: kpgpkey.h:433
bool matchesUserID(const TQString &str, bool cs=true)
Returns true if the given string matches one of the user IDs.
Definition: kpgpkey.cpp:206
bool invalid() const
Returns true if the key is invalid.
Definition: kpgpkey.h:623
void setFingerprint(const KeyID &keyID, const TQCString &fpr)
Sets the fingerprint of the given subkey to fpr.
Definition: kpgpkey.cpp:249
bool secret() const
Returns true if the key is a secret key.
Definition: kpgpkey.h:603
bool isValid() const
Returns true if the key is valid, i.e.
Definition: kpgpkey.cpp:177
void addUserID(const TQString &uid, const Validity validity=KPGP_VALIDITY_UNKNOWN, const bool revoked=false, const bool invalid=false)
Adds a user ID with the given values to the key if uid isn't an empty string.
Definition: kpgpkey.cpp:197
bool revoked() const
Returns true if the key has been revoked.
Definition: kpgpkey.h:608
void cloneKeyTrust(const Key *key)
Set the validity values for the user ids to the validity values of the given key.
Definition: kpgpkey.cpp:165
bool isValidEncryptionKey() const
Returns true if the key is a valid encryption key.
Definition: kpgpkey.cpp:184
Validity keyTrust() const
Returns the trust value of this key.
Definition: kpgpkey.cpp:134
bool isValidSigningKey() const
Returns true if the key is a valid signing key.
Definition: kpgpkey.cpp:191
void addSubkey(const KeyID &keyID, const bool secret=false)
Adds a subkey with the given values to the key if keyID isn't an empty string.
Definition: kpgpkey.cpp:219
void clear()
Clears/resets all key data.
Definition: kpgpkey.cpp:114
Subkey * getSubkey(const KeyID &keyID)
Returns a pointer to the subkey with the given key ID.
Definition: kpgpkey.cpp:227
Key(const KeyID &keyid=KeyID(), const TQString &uid=TQString(), const bool secret=false)
Constructs a new PGP key with keyid as key ID of the primary key and uid as primary user ID.
Definition: kpgpkey.cpp:84
This class is used to store information about a subkey of a PGP key.
Definition: kpgpkey.h:163
void setFingerprint(const TQCString &fingerprint)
Sets the fingerprint of the subkey to fingerprint.
Definition: kpgpkey.h:411
Subkey(const KeyID &keyID, const bool secret=false)
Constructs a new subkey with the given key ID.
Definition: kpgpkey.cpp:62
KeyID keyID() const
Returns the (short) 32 bit key ID of the subkey.
Definition: kpgpkey.h:336
bool secret() const
Returns true if the subkey is a secret subkey.
Definition: kpgpkey.h:281
This class is used to store information about a user id of a PGP key.
Definition: kpgpkey.h:75
UserID(const TQString &str, const Validity validity=KPGP_VALIDITY_UNKNOWN, const bool revoked=false, const bool invalid=false)
Constructs a new user id with the given values.
Definition: kpgpkey.cpp:50
bool invalid() const
Returns true if the user id is invalid.
Definition: kpgpkey.h:129
bool revoked() const
Returns true if the user id has been revoked.
Definition: kpgpkey.h:124
Validity validity() const
Returns the validity of resp.
Definition: kpgpkey.h:134