• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

libkpgp

  • sources
  • kde-4.14
  • kdepim
  • libkpgp
kpgpkey.cpp
Go to the documentation of this file.
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 #include <QByteArray>
23 
24 namespace Kpgp {
25 
26 /* member functions of Kpgp::KeyIDList --------------------------------- */
27 
30 QStringList KeyIDList::toStringList() const
31 {
32  QStringList res;
33  for( KeyIDList::ConstIterator it = begin(); it != end(); ++it ) {
34  res << QLatin1String((*it).data());
35  }
36  return res;
37 }
38 
41 KeyIDList KeyIDList::fromStringList( const QStringList& l )
42 {
43  KeyIDList res;
44  QStringList::ConstIterator end(l.end());
45  for( QStringList::ConstIterator it = l.begin(); it != end; ++it ) {
46  res << (*it).toLocal8Bit();
47  }
48  return res;
49 }
50 
51 /* member functions of Kpgp::UserID ------------------------------------ */
52 
53 UserID::UserID(const QString& str, const Validity validity,
54  const bool revoked, const bool invalid)
55 {
56  mText = str;
57  mValidity = validity;
58  mRevoked = revoked;
59  mInvalid = invalid;
60 }
61 
62 
63 /* member functions of Kpgp::Subkey ------------------------------------ */
64 
65 Subkey::Subkey(const KeyID& keyID, const bool secret)
66 {
67  mSecret = secret;
68  mKeyID = keyID;
69 
70  mRevoked = false;
71  mExpired = false;
72  mDisabled = false;
73  mInvalid = false;
74  mCanEncrypt = false;
75  mCanSign = false;
76  mCanCertify = false;
77  mKeyAlgo = 0;
78  mKeyLen = 0;
79  mFingerprint = 0;
80  mTimestamp = 0;
81  mExpiration = 0;
82 }
83 
84 
85 /* member functions of Kpgp::Key --------------------------------------- */
86 
87 Key::Key(const KeyID& keyid, const QString& uid, const bool secret) :
88  mSubkeys(), mUserIDs()
89 {
90  mSecret = secret;
91  if (!keyid.isEmpty())
92  addSubkey(keyid, secret);
93  if (!uid.isEmpty())
94  addUserID(uid);
95 
96  mRevoked = false;
97  mExpired = false;
98  mDisabled = false;
99  mInvalid = false;
100  mCanEncrypt = false;
101  mCanSign = false;
102  mCanCertify = false;
103 
104  mEncryptPref = UnknownEncryptPref;
105 }
106 
107 Key::~Key()
108 {
109  //kDebug( 5326 ) <<"Kpgp::Key: Deleting key" << primaryUserID();
110  qDeleteAll( mUserIDs );
111  mUserIDs.clear();
112  qDeleteAll( mSubkeys );
113  mSubkeys.clear();
114 }
115 
116 void
117 Key::clear()
118 {
119  mSecret = false;
120  mRevoked = false;
121  mExpired = false;
122  mDisabled = false;
123  mInvalid = false;
124  mCanEncrypt = false;
125  mCanSign = false;
126  mCanCertify = false;
127 
128  mEncryptPref = UnknownEncryptPref;
129 
130  qDeleteAll( mSubkeys );
131  mSubkeys.clear();
132  qDeleteAll( mUserIDs );
133  mUserIDs.clear();
134 }
135 
136 Validity
137 Key::keyTrust() const
138 {
139  Validity trust = KPGP_VALIDITY_UNKNOWN;
140 
141  foreach ( UserID* userId, mUserIDs )
142  {
143  if( userId->validity() > trust )
144  trust = userId->validity();
145  }
146 
147  return trust;
148 }
149 
150 Validity
151 Key::keyTrust( const QString& uid ) const
152 {
153  Validity trust = KPGP_VALIDITY_UNKNOWN;
154 
155  if( uid.isEmpty() )
156  return trust;
157 
158  foreach ( UserID* userId, mUserIDs )
159  {
160  if( userId->text() == uid )
161  trust = userId->validity();
162  }
163 
164  return trust;
165 }
166 
167 void
168 Key::cloneKeyTrust( const Key* key )
169 {
170  if( !key )
171  return;
172 
173  foreach ( UserID* userId, mUserIDs )
174  {
175  userId->setValidity( key->keyTrust( userId->text() ) );
176  }
177 }
178 
179 bool
180 Key::isValid() const
181 {
182  return ( !mRevoked && !mExpired && !mDisabled && !mInvalid );
183 }
184 
185 
186 bool
187 Key::isValidEncryptionKey() const
188 {
189  return ( !mRevoked && !mExpired && !mDisabled && !mInvalid && mCanEncrypt );
190 }
191 
192 
193 bool
194 Key::isValidSigningKey() const
195 {
196  return ( !mRevoked && !mExpired && !mDisabled && !mInvalid && mCanSign );
197 }
198 
199 
200 void Key::addUserID(const QString &uid, const Validity validity,
201  const bool revoked, const bool invalid)
202 {
203  if (!uid.isEmpty()) {
204  UserID *userID = new UserID(uid, validity, revoked, invalid);
205  mUserIDs.append(userID);
206  }
207 }
208 
209 bool Key::matchesUserID(const QString& str, bool cs)
210 {
211  if (str.isEmpty() || mUserIDs.isEmpty())
212  return false;
213 
214  foreach ( UserID *userId, mUserIDs ) {
215  if ((userId->text().indexOf(str, 0, cs?Qt::CaseSensitive:Qt::CaseInsensitive)) != -1)
216  return true;
217  }
218 
219  return false;
220 }
221 
222 void Key::addSubkey(const KeyID& keyID, const bool secret)
223 {
224  if (!keyID.isEmpty()) {
225  Subkey *key = new Subkey(keyID, secret);
226  mSubkeys.append(key);
227  }
228 }
229 
230 Subkey *Key::getSubkey(const KeyID& keyID)
231 {
232  if (keyID.isEmpty() || mSubkeys.isEmpty())
233  return 0;
234 
235  // is the given key ID a long (16 chars) or a short (8 chars) key ID?
236  bool longKeyID = (keyID.length() == 16);
237 
238  foreach ( Subkey* subKey, mSubkeys ) {
239  if (longKeyID) {
240  if (subKey->longKeyID() == keyID)
241  return subKey;
242  }
243  else {
244  if (subKey->keyID() == keyID)
245  return subKey;
246  }
247  }
248 
249  return 0;
250 }
251 
252 void Key::setFingerprint(const KeyID& keyID, const QByteArray &fpr)
253 {
254  Subkey *key;
255  if ((key = getSubkey(keyID)) != 0) {
256  key->setFingerprint(fpr);
257  }
258  else
259  kDebug(5326) <<"Error: Can't set fingerprint. A subkey with key ID 0x"
260  << keyID << "doesn't exist.";
261 }
262 
263 } // namespace Kpgp
Kpgp::Key::isValidEncryptionKey
bool isValidEncryptionKey() const
Returns true if the key is a valid encryption key.
Definition: kpgpkey.cpp:187
QList::clear
void clear()
Kpgp::Validity
Validity
These are the possible validity values for a PGP user id and for the owner trust. ...
Definition: kpgpkey.h:32
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
Kpgp::Subkey::mDisabled
bool mDisabled
Is this subkey disabled?
Definition: kpgpkey.h:338
Kpgp::Key::Key
Key(const KeyID &keyid=KeyID(), const QString &uid=QString(), 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:87
Kpgp::KeyIDList::fromStringList
static KeyIDList fromStringList(const QStringList &)
Converts from a QStringList to a KeyIDList.
Definition: kpgpkey.cpp:41
Kpgp::Key::mSubkeys
SubkeyList mSubkeys
Definition: kpgpkey.h:683
Kpgp::Key::mCanEncrypt
bool mCanEncrypt
Definition: kpgpkey.h:677
Kpgp::Key::setFingerprint
void setFingerprint(const KeyID &keyID, const QByteArray &fpr)
Sets the fingerprint of the given subkey to fpr .
Definition: kpgpkey.cpp:252
Kpgp::UserID::mValidity
Validity mValidity
Validity (assuming invalid flag is false).
Definition: kpgpkey.h:133
QByteArray
Kpgp::Key::mCanCertify
bool mCanCertify
Definition: kpgpkey.h:679
Kpgp::Subkey::mTimestamp
time_t mTimestamp
-1 for invalid, 0 for not available
Definition: kpgpkey.h:352
QByteArray::isEmpty
bool isEmpty() const
Kpgp::Subkey::mExpiration
time_t mExpiration
-1 for never, 0 for not available
Definition: kpgpkey.h:353
Kpgp::Key::mUserIDs
UserIDList mUserIDs
Definition: kpgpkey.h:684
Kpgp::Subkey::mCanSign
bool mCanSign
Can this subkey sign?
Definition: kpgpkey.h:344
QByteArray::length
int length() const
Kpgp::Key::isValid
bool isValid() const
Returns true if the key is valid, i.e.
Definition: kpgpkey.cpp:180
Kpgp::Subkey::mCanCertify
bool mCanCertify
Can this subkey certify?
Definition: kpgpkey.h:346
Kpgp::Subkey::mCanEncrypt
bool mCanEncrypt
Can this subkey encrypt?
Definition: kpgpkey.h:342
Kpgp::Key::mExpired
bool mExpired
Definition: kpgpkey.h:674
Kpgp::Key::secret
bool secret() const
Returns true if the key is a secret key.
Definition: kpgpkey.h:687
QList::append
void append(const T &value)
Kpgp::UserID::mInvalid
bool mInvalid
Invalid flag.
Definition: kpgpkey.h:131
Kpgp::Subkey::mSecret
bool mSecret
Is this subkey secret?
Definition: kpgpkey.h:332
Kpgp::Key::~Key
~Key()
Definition: kpgpkey.cpp:107
Kpgp::Subkey::longKeyID
KeyID longKeyID() const
Returns the long 64 bit key ID of the subkey if it's available.
Definition: kpgpkey.h:406
Kpgp::Subkey::keyID
KeyID keyID() const
Returns the (short) 32 bit key ID of the subkey.
Definition: kpgpkey.h:411
QList::isEmpty
bool isEmpty() const
Kpgp::Subkey
This class is used to store information about a subkey of a PGP key.
Definition: kpgpkey.h:183
QString::isEmpty
bool isEmpty() const
Kpgp::Key::addUserID
void addUserID(const QString &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:200
Kpgp::Subkey::mRevoked
bool mRevoked
Is this subkey revoked?
Definition: kpgpkey.h:334
Kpgp::UserID::invalid
bool invalid() const
Returns true if the user id is invalid.
Definition: kpgpkey.h:150
Kpgp::Key::mInvalid
bool mInvalid
Definition: kpgpkey.h:676
Kpgp::Subkey::mExpired
bool mExpired
Is this subkey expired?
Definition: kpgpkey.h:336
Kpgp::Key::mDisabled
bool mDisabled
Definition: kpgpkey.h:675
QString
Kpgp::KeyIDList
Definition: kpgpkey.h:57
Kpgp::UserID::UserID
UserID(const QString &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:53
Kpgp::UserID::mRevoked
bool mRevoked
Revoked flag.
Definition: kpgpkey.h:129
Kpgp::UserID::setValidity
void setValidity(const Validity validity)
Sets the validity of resp.
Definition: kpgpkey.h:175
QStringList
Kpgp::Key::keyTrust
Validity keyTrust() const
Returns the trust value of this key.
Definition: kpgpkey.cpp:137
Kpgp::UserID
This class is used to store information about a user id of a PGP key.
Definition: kpgpkey.h:72
QList< KeyID >::end
iterator end()
Kpgp::Key::isValidSigningKey
bool isValidSigningKey() const
Returns true if the key is a valid signing key.
Definition: kpgpkey.cpp:194
kpgpkey.h
Kpgp::UserID::validity
Validity validity() const
Returns the validity of resp.
Definition: kpgpkey.h:155
Kpgp::UserID::text
QString text() const
Returns the text of the user id.
Definition: kpgpkey.h:140
Kpgp::Subkey::mKeyLen
unsigned int mKeyLen
Definition: kpgpkey.h:349
Kpgp::Subkey::mInvalid
bool mInvalid
Is this subkey invalid?
Definition: kpgpkey.h:340
Kpgp::Key::mRevoked
bool mRevoked
Definition: kpgpkey.h:673
QLatin1String
Kpgp::Subkey::setFingerprint
void setFingerprint(const QByteArray &fingerprint)
Sets the fingerprint of the subkey to fingerprint .
Definition: kpgpkey.h:486
Kpgp::Key::mSecret
bool mSecret
Definition: kpgpkey.h:671
Kpgp::Subkey::secret
bool secret() const
Returns true if the subkey is a secret subkey.
Definition: kpgpkey.h:356
Kpgp::Key::addSubkey
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:222
Kpgp::Subkey::mKeyAlgo
unsigned int mKeyAlgo
Definition: kpgpkey.h:348
QList< KeyID >::ConstIterator
typedef ConstIterator
Kpgp::Subkey::mKeyID
KeyID mKeyID
Definition: kpgpkey.h:350
Kpgp::UnknownEncryptPref
Definition: kpgpkey.h:47
Kpgp::Key
This class is used to store information about a PGP key.
Definition: kpgpkey.h:506
Kpgp::KPGP_VALIDITY_UNKNOWN
Definition: kpgpkey.h:34
Kpgp::Subkey::mFingerprint
QByteArray mFingerprint
Definition: kpgpkey.h:351
Kpgp::UserID::mText
QString mText
User id (descriptive text).
Definition: kpgpkey.h:135
Kpgp::KeyIDList::toStringList
QStringList toStringList() const
Converts from a KeyIDList to a QStringList.
Definition: kpgpkey.cpp:30
Kpgp::Key::mEncryptPref
EncryptPref mEncryptPref
Definition: kpgpkey.h:681
Kpgp::Subkey::Subkey
Subkey(const KeyID &keyID, const bool secret=false)
Constructs a new subkey with the given key ID.
Definition: kpgpkey.cpp:65
QList< KeyID >::begin
iterator begin()
Kpgp::Key::matchesUserID
bool matchesUserID(const QString &str, bool cs=true)
Returns true if the given string matches one of the user IDs.
Definition: kpgpkey.cpp:209
Kpgp::Key::getSubkey
Subkey * getSubkey(const KeyID &keyID)
Returns a pointer to the subkey with the given key ID.
Definition: kpgpkey.cpp:230
Kpgp::Key::mCanSign
bool mCanSign
Definition: kpgpkey.h:678
Kpgp::UserID::revoked
bool revoked() const
Returns true if the user id has been revoked.
Definition: kpgpkey.h:145
Kpgp::Key::clear
void clear()
Clears/resets all key data.
Definition: kpgpkey.cpp:117
Kpgp::Key::cloneKeyTrust
void cloneKeyTrust(const Key *key)
Set the validity values for the user ids to the validity values of the given key. ...
Definition: kpgpkey.cpp:168
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:22 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkpgp

Skip menu "libkpgp"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal