4 Copyright (C) 2001,2002 the KPGP authors
5 See file AUTHORS.kpgp for details
7 This file is part of KPGP, the KDE PGP/GnuPG support library.
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.
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
22 #include <QtCore/QString>
23 #include <QtCore/QStringList>
29 /** These are the possible validity values for a PGP user id and for the owner
33 { // this is copied from gpgme.h which is a part of GPGME
34 KPGP_VALIDITY_UNKNOWN
= 0, // the trust hasn't been determined
35 KPGP_VALIDITY_UNDEFINED
= 1, // trust is undefined
36 KPGP_VALIDITY_NEVER
= 2,
37 KPGP_VALIDITY_MARGINAL
= 3,
38 KPGP_VALIDITY_FULL
= 4,
39 KPGP_VALIDITY_ULTIMATE
= 5
42 /** These are the possible preferences for encryption.
47 UnknownEncryptPref
= 0,
49 AlwaysEncryptIfPossible
= 2,
50 AlwaysAskForEncryption
= 3,
51 AskWheneverPossible
= 4
55 typedef QByteArray KeyID
;
57 class KeyIDList
: public QList
<KeyID
>
61 KeyIDList( const KeyIDList
& l
) : QList
<KeyID
>(l
) { }
62 KeyIDList( const QList
<KeyID
>& l
) : QList
<KeyID
>(l
) { }
63 KeyIDList( const KeyID
& i
) { append(i
); }
65 QStringList
toStringList() const;
67 static KeyIDList
fromStringList( const QStringList
& );
70 /** This class is used to store information about a user id of a PGP key.
75 /** Constructs a new user id with the given values.
76 * @param str User Id (descriptive text)
77 * @param validity Validity of key, relevant mostly if @p invalid is @c false
78 * @param revoked Is the key revoked?
79 * @param invalid Is the key invalid?
81 * @todo How exactly do invalid and validity affect each other?
83 explicit UserID(const QString
& str
,
84 const Validity validity
= KPGP_VALIDITY_UNKNOWN
,
85 const bool revoked
= false,
86 const bool invalid
= false);
89 /** Returns the text of the user id. */
92 /** Returns true if the user id has been revoked. */
95 /** Returns true if the user id is invalid. */
98 /** Returns the validity of resp. the trust in the user id. */
99 Validity
validity() const;
101 /** Sets the text of the user id to @p str.
102 * @param str The new user id text.
104 void setText(const QString
& str
);
106 /** Sets the flag if the user id has been revoked to @p revoked.
107 * @param revoked Whether the user id has been revoked or not.
108 * @todo Does one revoke a user id or a key? See also comments
109 * for setInvalid() and setText().
111 void setRevoked(const bool revoked
);
113 /** Sets the flag if the user id is invalid to @p invalid.
114 * @param invalid Whether the user id is invalid or not.
115 * @todo Is it the <em>user id</em> that is invalid or the
116 * key itself? This should then also be documented in
119 void setInvalid(const bool invalid
);
121 /** Sets the validity of resp. the trust in the user id to @p validity .
122 * @param validity New validity value.
123 * @todo Are there any restrictions on how you can change validities?
125 void setValidity(const Validity validity
);
128 /** Revoked flag. @see setRevoked() @see revoked() */
130 /** Invalid flag. @see setInvalid() @see invalid() */
132 /** Validity (assuming invalid flag is @c false). @see setValidity() @see validity() */
134 /** User id (descriptive text). @see setText() @see text() */
138 typedef QList
<UserID
*> UserIDList
;
140 inline QString
UserID::text() const
145 inline bool UserID::revoked() const
150 inline bool UserID::invalid() const
155 inline Validity
UserID::validity() const
160 inline void UserID::setText(const QString
& str
)
165 inline void UserID::setRevoked(const bool revoked
)
170 inline void UserID::setInvalid(const bool invalid
)
175 inline void UserID::setValidity(const Validity validity
)
177 mValidity
= validity
;
181 /** This class is used to store information about a subkey of a PGP key.
186 /** Constructs a new subkey with the given key ID. */
187 explicit Subkey(const KeyID
& keyID
, const bool secret
= false);
190 /** Returns true if the subkey is a secret subkey. */
193 /** Returns true if the subkey has been revoked. */
194 bool revoked() const;
196 /** Returns true if the subkey has expired. */
197 bool expired() const;
199 /** Returns true if the subkey has been disabled. */
200 bool disabled() const;
202 /** Returns true if the subkey is invalid. */
203 bool invalid() const;
205 /** Returns true if the subkey can be used to encrypt data. */
206 bool canEncrypt() const;
208 /** Returns true if the subkey can be used to sign data. */
209 bool canSign() const;
211 /** Returns true if the subkey can be used to certify keys. */
212 bool canCertify() const;
214 /** Returns the key algorithm of the subkey. */
215 unsigned int keyAlgorithm() const;
217 /** Returns the length of the subkey in bits. */
218 unsigned int keyLength() const;
220 /** Returns the long 64 bit key ID of the subkey if it's available.
221 Otherwise the short 32 bit key ID is returned. */
222 KeyID
longKeyID() const;
224 /** Returns the (short) 32 bit key ID of the subkey. */
227 /** Returns the fingerprint of the subkey. */
228 QByteArray
fingerprint() const;
230 /** Returns the creation date of the subkey. */
231 time_t creationDate() const;
233 /** Returns the expiration date of the subkey. */
234 time_t expirationDate() const;
236 /** Sets the flag if the subkey is a secret subkey to @p secret .
237 * @param secret Whether this subkey is secret or not.
240 void setSecret(const bool secret
);
242 /** Sets the flag if the subkey has been revoked to @p revoked .
243 * @param revoked Whether this subkey is revoked or not.
246 void setRevoked(const bool revoked
);
248 /** Sets the flag if the subkey has expired to @p expired .
249 * @param expired Whether this subkey has expired or not.
252 void setExpired(const bool expired
);
254 /** Sets the flag if the subkey has been disabled to @p disabled .
255 * @param disabled Whether this subkey is disabled or not.
258 void setDisabled(const bool disabled
);
260 /** Sets the flag if the subkey is invalid to @p invalid .
261 * @param invalid Whether this subkey is invalid or not.
264 void setInvalid(const bool invalid
);
266 /** Sets the flag if the subkey can be used to encrypt data to @p canEncrypt .
267 @param canEncrypt Whether this subkey can encrypt or not.
270 void setCanEncrypt(const bool canEncrypt
);
272 /** Sets the flag if the subkey can be used to sign data to @p canSign .
273 @param canSign Whether this subkey can sign or not.
276 void setCanSign(const bool canSign
);
278 /** Sets the flag if the subkey can be used to certify keys to @p canCertify .
279 @param canCertify Whether this subkey can certify or not.
282 void setCanCertify(const bool canCertify
);
284 /** Sets the key algorithm of the subkey to @p keyAlgo .
285 @param keyAlgo The key algorithm to use.
287 @todo What are legal values? What do they mean?
288 Where are they documented?
290 void setKeyAlgorithm(const unsigned int keyAlgo
);
292 /** Sets the key length of the subkey to @p keyLen bits.
293 @param keyLength Length of the subkey.
295 @todo What are the valid lengths? Is 0 ok?
297 void setKeyLength(const unsigned int keyLen
);
299 /** Sets the key ID of the subkey to @p keyID .
300 @param keyID the key ID for this subkey (just a string)
302 @todo What are legal key IDs? How about NULL IDs?
304 void setKeyID(const KeyID
& keyID
);
306 /** Sets the fingerprint of the subkey to @p fingerprint .
307 @param fingerprint to use for this subkey
309 @todo What are legal fingerprints?
311 void setFingerprint(const QByteArray
& fingerprint
);
313 /** Sets the creation date of the subkey to @p creationDate seconds
315 @param creationDate Creation time of this subkey, in seconds
316 since the epoch (still midnight, january 1, 1970 in most
319 void setCreationDate(const time_t creationDate
);
321 /** Sets the expiration date of the subkey to @p expirationDate seconds
323 @param expirationDate Expiry time of this subkey, in seconds
324 since the epoch (still midnight, january 1, 1970 in most
326 @todo Does this expiry influence any of the other flags?
328 void setExpirationDate(const time_t expirationDate
);
331 /** Is this subkey secret? @see secret() @see setSecret() */
333 /** Is this subkey revoked? @see revoked() @see setRevoked() */
335 /** Is this subkey expired? @see expired() @see setExpired() */
337 /** Is this subkey disabled? @see disabled() @see setDisabled() */
339 /** Is this subkey invalid? @see invalid() @see setInvalid() */
341 /** Can this subkey encrypt? @see canEncrypt() @see setCanEncrypt() */
342 bool mCanEncrypt
: 1;
343 /** Can this subkey sign? @see canSign() @see setCanSign() */
345 /** Can this subkey certify? @see canCertify() @see setCanCertify() */
346 bool mCanCertify
: 1;
348 unsigned int mKeyAlgo
;
349 unsigned int mKeyLen
;
351 QByteArray mFingerprint
;
352 time_t mTimestamp
; /**< -1 for invalid, 0 for not available */
353 time_t mExpiration
; /**< -1 for never, 0 for not available */
356 inline bool Subkey::secret() const
361 inline bool Subkey::revoked() const
366 inline bool Subkey::expired() const
371 inline bool Subkey::disabled() const
376 inline bool Subkey::invalid() const
381 inline bool Subkey::canEncrypt() const
386 inline bool Subkey::canSign() const
391 inline bool Subkey::canCertify() const
396 inline unsigned int Subkey::keyAlgorithm() const
401 inline unsigned int Subkey::keyLength() const
406 inline KeyID
Subkey::longKeyID() const
411 inline KeyID
Subkey::keyID() const
413 return mKeyID
.right(8);
416 inline QByteArray
Subkey::fingerprint() const
421 inline time_t Subkey::creationDate() const
426 inline time_t Subkey::expirationDate() const
431 inline void Subkey::setSecret(const bool secret
)
436 inline void Subkey::setRevoked(const bool revoked
)
441 inline void Subkey::setExpired(const bool expired
)
446 inline void Subkey::setDisabled(const bool disabled
)
448 mDisabled
= disabled
;
451 inline void Subkey::setInvalid(const bool invalid
)
456 inline void Subkey::setCanEncrypt(const bool canEncrypt
)
458 mCanEncrypt
= canEncrypt
;
461 inline void Subkey::setCanSign(const bool canSign
)
466 inline void Subkey::setCanCertify(const bool canCertify
)
468 mCanCertify
= canCertify
;
471 inline void Subkey::setKeyAlgorithm(const unsigned int keyAlgo
)
476 inline void Subkey::setKeyLength(const unsigned int keyLen
)
481 inline void Subkey::setKeyID(const KeyID
& keyID
)
486 inline void Subkey::setFingerprint(const QByteArray
& fingerprint
)
488 mFingerprint
= fingerprint
;
491 inline void Subkey::setCreationDate(const time_t creationDate
)
493 mTimestamp
= creationDate
;
496 inline void Subkey::setExpirationDate(const time_t expirationDate
)
498 mExpiration
= expirationDate
;
501 typedef QList
<Subkey
*> SubkeyList
;
504 /** This class is used to store information about a PGP key.
509 /** Constructs a new PGP key with @p keyid as key ID of the
510 primary key and @p uid as primary user ID.
511 @param keyid Key ID for this Key
512 @param uid UID for this key (user ID)
513 @param secret Is this key secret?
515 explicit Key( const KeyID
& keyid
= KeyID(),
516 const QString
& uid
= QString(),
517 const bool secret
= false);
520 /** Clears/resets all key data. */
523 /** Returns true if the key is a secret key. */
526 /** Returns true if the key has been revoked. */
527 bool revoked() const;
529 /** Returns true if the key has expired. */
530 bool expired() const;
532 /** Returns true if the key has been disabled. */
533 bool disabled() const;
535 /** Returns true if the key is invalid. */
536 bool invalid() const;
538 /** Returns true if the key can be used to encrypt data. */
539 bool canEncrypt() const;
541 /** Returns true if the key can be used to sign data. */
542 bool canSign() const;
544 /** Returns true if the key can be used to certify keys. */
545 bool canCertify() const;
547 /** Sets the flag if the key is a secret key to @p secret .
548 @param secret Whether this key is secret or not
551 void setSecret(const bool secret
);
553 /** Sets the flag if the key has been revoked to @p revoked .
554 @param revoked Whether the key is revoked or not
557 void setRevoked(const bool revoked
);
559 /** Sets the flag if the key has expired to <em>expired . */
560 void setExpired(const bool expired
);
562 /** Sets the flag if the key has been disabled to <em>disabled . */
563 void setDisabled(const bool disabled
);
565 /** Sets the flag if the key is invalid to <em>invalid . */
566 void setInvalid(const bool invalid
);
568 /** Sets the flag if the key can be used to encrypt data to
570 void setCanEncrypt(const bool canEncrypt
);
572 /** Sets the flag if the key can be used to sign data to
574 void setCanSign(const bool canSign
);
576 /** Sets the flag if the key can be used to certify keys to
578 void setCanCertify(const bool canCertify
);
581 /** Returns the encryption preference for this key. */
582 EncryptPref
encryptionPreference();
584 /** Sets the encryption preference for this key to <em>encrPref . */
585 void setEncryptionPreference( const EncryptPref encrPref
);
588 /** Returns the primary user ID or a null string if there are no
590 QString
primaryUserID() const;
592 /** Returns the key ID of the primary key or a null string if there
594 KeyID
primaryKeyID() const;
596 /** Returns the fingerprint of the primary key or a null string if there
598 QByteArray
primaryFingerprint() const;
600 /** Returns true if there are no user IDs or no subkeys.*/
603 /** Returns the creation date of the primary subkey.
605 time_t creationDate() const;
607 /** Returns the trust value of this key. This is the maximal trust value
608 of any of the user ids of this key.
610 Validity
keyTrust() const;
612 /** Returns the trust value for the given user id of this key.
614 Validity
keyTrust( const QString
& uid
) const;
616 /** Set the validity values for the user ids to the validity values of
617 the given key. This is useful after rereading a key without expensive
620 void cloneKeyTrust( const Key
* key
);
622 /** Returns true if the key is valid, i.e. not revoked, expired, disabled
625 bool isValid() const;
627 /** Returns true if the key is a valid encryption key. The trust is not
630 bool isValidEncryptionKey() const;
632 /** Returns true if the key is a valid signing key. The trust is not checked.
634 bool isValidSigningKey() const;
636 /** Returns the list of userIDs. */
637 const UserIDList
userIDs() const;
639 /** Returns the list of subkeys. */
640 const SubkeyList
subkeys() const;
642 /** Adds a user ID with the given values to the key if <em>uid isn't
644 void addUserID(const QString
& uid
,
645 const Validity validity
= KPGP_VALIDITY_UNKNOWN
,
646 const bool revoked
= false,
647 const bool invalid
= false);
649 /** Adds the given user ID to the key. */
650 void addUserID(const UserID
*userID
);
652 /** Returns true if the given string matches one of the user IDs.
653 The match is case sensitive if <em>cs is true or case insensitive
654 if <em>cs is false. */
655 bool matchesUserID(const QString
& str
, bool cs
= true);
657 /** Adds a subkey with the given values to the key if <em>keyID isn't
659 void addSubkey(const KeyID
& keyID
, const bool secret
= false);
661 /** Adds the given subkey to the key. */
662 void addSubkey(const Subkey
*subkey
);
664 /** Returns a pointer to the subkey with the given key ID. */
665 Subkey
*getSubkey(const KeyID
& keyID
);
667 /** Sets the fingerprint of the given subkey to <em>fpr . */
668 void setFingerprint(const KeyID
& keyID
, const QByteArray
& fpr
);
677 bool mCanEncrypt
: 1;
679 bool mCanCertify
: 1;
681 EncryptPref mEncryptPref
;
687 inline bool Key::secret() const
692 inline bool Key::revoked() const
697 inline bool Key::expired() const
702 inline bool Key::disabled() const
707 inline bool Key::invalid() const
712 inline bool Key::canEncrypt() const
717 inline bool Key::canSign() const
722 inline bool Key::canCertify() const
727 inline void Key::setSecret(const bool secret
)
732 inline void Key::setRevoked(const bool revoked
)
737 inline void Key::setExpired(const bool expired
)
742 inline void Key::setDisabled(const bool disabled
)
744 mDisabled
= disabled
;
747 inline void Key::setInvalid(const bool invalid
)
752 inline void Key::setCanEncrypt(const bool canEncrypt
)
754 mCanEncrypt
= canEncrypt
;
757 inline void Key::setCanSign(const bool canSign
)
762 inline void Key::setCanCertify(const bool canCertify
)
764 mCanCertify
= canCertify
;
767 inline EncryptPref
Key::encryptionPreference()
772 inline void Key::setEncryptionPreference( const EncryptPref encrPref
)
774 mEncryptPref
= encrPref
;
777 inline QString
Key::primaryUserID() const
779 UserID
*uid
= mUserIDs
.isEmpty() ? 0 : mUserIDs
.first();
787 inline KeyID
Key::primaryKeyID() const
789 Subkey
*key
= mSubkeys
.isEmpty() ? 0 : mSubkeys
.first();
797 inline QByteArray
Key::primaryFingerprint() const
799 Subkey
*key
= mSubkeys
.isEmpty() ? 0 : mSubkeys
.first();
802 return key
->fingerprint();
807 inline const UserIDList
Key::userIDs() const
812 inline const SubkeyList
Key::subkeys() const
817 inline bool Key::isNull() const
819 return (mUserIDs
.isEmpty() || mSubkeys
.isEmpty());
822 inline time_t Key::creationDate() const
824 if( !mSubkeys
.isEmpty() )
825 return mSubkeys
.first()->creationDate();
830 inline void Key::addUserID(const UserID
*userID
)
833 mUserIDs
.append(const_cast<UserID
*>(userID
));
836 inline void Key::addSubkey(const Subkey
*subkey
)
839 mSubkeys
.append(const_cast<Subkey
*>(subkey
));
843 typedef QList
<Key
*> KeyList
;
845 inline bool KeyCompare( Key
* left
, Key
* right
)
847 const int result
= QString::compare( left
->primaryUserID().toLower(), right
->primaryUserID().toLower() );