Better wording
[kdepim.git] / libkpgp / kpgpkey.h
blob159ba54c82685103d26a86ca4b84997791f8109d
1 /*
2 kpgpkey.h
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
19 #ifndef KPGPKEY_H
20 #define KPGPKEY_H
22 #include <QtCore/QString>
23 #include <QtCore/QStringList>
25 #include <time.h>
27 namespace Kpgp {
29 /** These are the possible validity values for a PGP user id and for the owner
30 trust.
32 typedef enum
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
40 } Validity;
42 /** These are the possible preferences for encryption.
44 typedef enum
46 NeverEncrypt = -1,
47 UnknownEncryptPref = 0,
48 AlwaysEncrypt = 1,
49 AlwaysEncryptIfPossible = 2,
50 AlwaysAskForEncryption = 3,
51 AskWheneverPossible = 4
52 } EncryptPref;
55 typedef QByteArray KeyID;
57 class KeyIDList : public QList<KeyID>
59 public:
60 KeyIDList() { }
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.
72 class UserID
74 public:
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);
87 ~UserID() {}
89 /** Returns the text of the user id. */
90 QString text() const;
92 /** Returns true if the user id has been revoked. */
93 bool revoked() const;
95 /** Returns true if the user id is invalid. */
96 bool invalid() const;
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
117 * setText().
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);
127 protected:
128 /** Revoked flag. @see setRevoked() @see revoked() */
129 bool mRevoked : 1;
130 /** Invalid flag. @see setInvalid() @see invalid() */
131 bool mInvalid : 1;
132 /** Validity (assuming invalid flag is @c false). @see setValidity() @see validity() */
133 Validity mValidity;
134 /** User id (descriptive text). @see setText() @see text() */
135 QString mText;
138 typedef QList<UserID*> UserIDList;
140 inline QString UserID::text() const
142 return mText;
145 inline bool UserID::revoked() const
147 return mRevoked;
150 inline bool UserID::invalid() const
152 return mInvalid;
155 inline Validity UserID::validity() const
157 return mValidity;
160 inline void UserID::setText(const QString& str)
162 mText = str;
165 inline void UserID::setRevoked(const bool revoked)
167 mRevoked = revoked;
170 inline void UserID::setInvalid(const bool invalid)
172 mInvalid = 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.
183 class Subkey
185 public:
186 /** Constructs a new subkey with the given key ID. */
187 explicit Subkey(const KeyID& keyID, const bool secret = false);
188 ~Subkey() {}
190 /** Returns true if the subkey is a secret subkey. */
191 bool secret() const;
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. */
225 KeyID keyID() const;
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.
238 * @see secret()
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.
244 * @see revoked()
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.
250 * @see expired()
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.
256 * @see disabled()
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.
262 * @see invalid()
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.
268 @see canEncrypt()
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.
274 @see canSign()
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.
280 @see canCertify()
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.
286 @see keyAlgorithm
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.
294 @see keyLength()
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)
301 @see keyID()
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
308 @see fingerprint()
309 @todo What are legal fingerprints?
311 void setFingerprint(const QByteArray& fingerprint);
313 /** Sets the creation date of the subkey to @p creationDate seconds
314 since Epoch.
315 @param creationDate Creation time of this subkey, in seconds
316 since the epoch (still midnight, january 1, 1970 in most
317 places).
319 void setCreationDate(const time_t creationDate);
321 /** Sets the expiration date of the subkey to @p expirationDate seconds
322 since Epoch.
323 @param expirationDate Expiry time of this subkey, in seconds
324 since the epoch (still midnight, january 1, 1970 in most
325 places).
326 @todo Does this expiry influence any of the other flags?
328 void setExpirationDate(const time_t expirationDate);
330 protected:
331 /** Is this subkey secret? @see secret() @see setSecret() */
332 bool mSecret : 1;
333 /** Is this subkey revoked? @see revoked() @see setRevoked() */
334 bool mRevoked : 1;
335 /** Is this subkey expired? @see expired() @see setExpired() */
336 bool mExpired : 1;
337 /** Is this subkey disabled? @see disabled() @see setDisabled() */
338 bool mDisabled : 1;
339 /** Is this subkey invalid? @see invalid() @see setInvalid() */
340 bool mInvalid : 1;
341 /** Can this subkey encrypt? @see canEncrypt() @see setCanEncrypt() */
342 bool mCanEncrypt : 1;
343 /** Can this subkey sign? @see canSign() @see setCanSign() */
344 bool mCanSign : 1;
345 /** Can this subkey certify? @see canCertify() @see setCanCertify() */
346 bool mCanCertify : 1;
348 unsigned int mKeyAlgo;
349 unsigned int mKeyLen;
350 KeyID mKeyID;
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
358 return mSecret;
361 inline bool Subkey::revoked() const
363 return mRevoked;
366 inline bool Subkey::expired() const
368 return mExpired;
371 inline bool Subkey::disabled() const
373 return mDisabled;
376 inline bool Subkey::invalid() const
378 return mInvalid;
381 inline bool Subkey::canEncrypt() const
383 return mCanEncrypt;
386 inline bool Subkey::canSign() const
388 return mCanSign;
391 inline bool Subkey::canCertify() const
393 return mCanCertify;
396 inline unsigned int Subkey::keyAlgorithm() const
398 return mKeyAlgo;
401 inline unsigned int Subkey::keyLength() const
403 return mKeyLen;
406 inline KeyID Subkey::longKeyID() const
408 return mKeyID;
411 inline KeyID Subkey::keyID() const
413 return mKeyID.right(8);
416 inline QByteArray Subkey::fingerprint() const
418 return mFingerprint;
421 inline time_t Subkey::creationDate() const
423 return mTimestamp;
426 inline time_t Subkey::expirationDate() const
428 return mExpiration;
431 inline void Subkey::setSecret(const bool secret)
433 mSecret = secret;
436 inline void Subkey::setRevoked(const bool revoked)
438 mRevoked = revoked;
441 inline void Subkey::setExpired(const bool expired)
443 mExpired = expired;
446 inline void Subkey::setDisabled(const bool disabled)
448 mDisabled = disabled;
451 inline void Subkey::setInvalid(const bool invalid)
453 mInvalid = invalid;
456 inline void Subkey::setCanEncrypt(const bool canEncrypt)
458 mCanEncrypt = canEncrypt;
461 inline void Subkey::setCanSign(const bool canSign)
463 mCanSign = canSign;
466 inline void Subkey::setCanCertify(const bool canCertify)
468 mCanCertify = canCertify;
471 inline void Subkey::setKeyAlgorithm(const unsigned int keyAlgo)
473 mKeyAlgo = keyAlgo;
476 inline void Subkey::setKeyLength(const unsigned int keyLen)
478 mKeyLen = keyLen;
481 inline void Subkey::setKeyID(const KeyID& keyID)
483 mKeyID = 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.
506 class Key
508 public:
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);
518 ~Key();
520 /** Clears/resets all key data. */
521 void clear();
523 /** Returns true if the key is a secret key. */
524 bool secret() const;
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
549 @see secret()
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
555 @see revoked()
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
569 <em>canEncrypt . */
570 void setCanEncrypt(const bool canEncrypt);
572 /** Sets the flag if the key can be used to sign data to
573 <em>canSign . */
574 void setCanSign(const bool canSign);
576 /** Sets the flag if the key can be used to certify keys to
577 <em>canCertify . */
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
589 user IDs. */
590 QString primaryUserID() const;
592 /** Returns the key ID of the primary key or a null string if there
593 are no subkeys. */
594 KeyID primaryKeyID() const;
596 /** Returns the fingerprint of the primary key or a null string if there
597 are no subkeys. */
598 QByteArray primaryFingerprint() const;
600 /** Returns true if there are no user IDs or no subkeys.*/
601 bool isNull() const;
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
618 trust checking.
620 void cloneKeyTrust( const Key* key );
622 /** Returns true if the key is valid, i.e. not revoked, expired, disabled
623 or invalid.
625 bool isValid() const;
627 /** Returns true if the key is a valid encryption key. The trust is not
628 checked.
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
643 an empty string. */
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
658 an empty string. */
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);
670 protected:
671 bool mSecret : 1;
672 /* global flags */
673 bool mRevoked : 1;
674 bool mExpired : 1;
675 bool mDisabled : 1;
676 bool mInvalid : 1;
677 bool mCanEncrypt : 1;
678 bool mCanSign : 1;
679 bool mCanCertify : 1;
681 EncryptPref mEncryptPref;
683 SubkeyList mSubkeys;
684 UserIDList mUserIDs;
687 inline bool Key::secret() const
689 return mSecret;
692 inline bool Key::revoked() const
694 return mRevoked;
697 inline bool Key::expired() const
699 return mExpired;
702 inline bool Key::disabled() const
704 return mDisabled;
707 inline bool Key::invalid() const
709 return mInvalid;
712 inline bool Key::canEncrypt() const
714 return mCanEncrypt;
717 inline bool Key::canSign() const
719 return mCanSign;
722 inline bool Key::canCertify() const
724 return mCanCertify;
727 inline void Key::setSecret(const bool secret)
729 mSecret = secret;
732 inline void Key::setRevoked(const bool revoked)
734 mRevoked = revoked;
737 inline void Key::setExpired(const bool expired)
739 mExpired = expired;
742 inline void Key::setDisabled(const bool disabled)
744 mDisabled = disabled;
747 inline void Key::setInvalid(const bool invalid)
749 mInvalid = invalid;
752 inline void Key::setCanEncrypt(const bool canEncrypt)
754 mCanEncrypt = canEncrypt;
757 inline void Key::setCanSign(const bool canSign)
759 mCanSign = canSign;
762 inline void Key::setCanCertify(const bool canCertify)
764 mCanCertify = canCertify;
767 inline EncryptPref Key::encryptionPreference()
769 return mEncryptPref;
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();
781 if (uid)
782 return uid->text();
783 else
784 return QString();
787 inline KeyID Key::primaryKeyID() const
789 Subkey *key = mSubkeys.isEmpty() ? 0 : mSubkeys.first();
791 if (key)
792 return key->keyID();
793 else
794 return KeyID();
797 inline QByteArray Key::primaryFingerprint() const
799 Subkey *key = mSubkeys.isEmpty() ? 0 : mSubkeys.first();
801 if (key)
802 return key->fingerprint();
803 else
804 return QByteArray();
807 inline const UserIDList Key::userIDs() const
809 return mUserIDs;
812 inline const SubkeyList Key::subkeys() const
814 return mSubkeys;
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();
826 else
827 return -1;
830 inline void Key::addUserID(const UserID *userID)
832 if (userID)
833 mUserIDs.append(const_cast<UserID*>(userID));
836 inline void Key::addSubkey(const Subkey *subkey)
838 if (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() );
848 return result == -1;
851 } // namespace Kpgp
853 #endif