libkpgp

kpgpkey.h
1/*
2 kpgpkey.h
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#ifndef KPGPKEY_H
20#define KPGPKEY_H
21
22#include <time.h>
23
24#include <tqcstring.h>
25#include <tqstring.h>
26#include <tqstringlist.h>
27#include <tqvaluelist.h>
28
29namespace Kpgp {
30
34typedef enum
35{ // this is copied from gpgme.h which is a part of GPGME
36 KPGP_VALIDITY_UNKNOWN = 0, // the trust hasn't been determined
37 KPGP_VALIDITY_UNDEFINED = 1, // trust is undefined
38 KPGP_VALIDITY_NEVER = 2,
39 KPGP_VALIDITY_MARGINAL = 3,
40 KPGP_VALIDITY_FULL = 4,
41 KPGP_VALIDITY_ULTIMATE = 5
42} Validity;
43
46typedef enum
47{
48 NeverEncrypt = -1,
49 UnknownEncryptPref = 0,
50 AlwaysEncrypt = 1,
51 AlwaysEncryptIfPossible = 2,
52 AlwaysAskForEncryption = 3,
53 AskWheneverPossible = 4
54} EncryptPref;
55
56
57typedef TQCString KeyID;
58
59class KeyIDList : public TQValueList<KeyID>
60{
61 public:
62 KeyIDList() { }
63 KeyIDList( const KeyIDList& l ) : TQValueList<KeyID>(l) { }
64 KeyIDList( const TQValueList<KeyID>& l ) : TQValueList<KeyID>(l) { }
65 KeyIDList( const KeyID& i ) { append(i); }
66
67 TQStringList toStringList() const;
68
69 static KeyIDList fromStringList( const TQStringList& );
70};
71
74class UserID
75{
76 public:
78 UserID(const TQString& str,
79 const Validity validity = KPGP_VALIDITY_UNKNOWN,
80 const bool revoked = false,
81 const bool invalid = false);
82 ~UserID() {};
83
85 TQString text() const;
86
88 bool revoked() const;
89
91 bool invalid() const;
92
94 Validity validity() const;
95
97 void setText(const TQString& str);
98
100 void setRevoked(const bool revoked);
101
103 void setInvalid(const bool invalid);
104
107 void setValidity(const Validity validity);
108
109 protected:
110 bool mRevoked : 1;
111 bool mInvalid : 1;
112 Validity mValidity;
113 TQString mText;
114};
115
116typedef TQPtrList<UserID> UserIDList;
117typedef TQPtrListIterator<UserID> UserIDListIterator;
118
119inline TQString UserID::text() const
120{
121 return mText;
122}
123
124inline bool UserID::revoked() const
125{
126 return mRevoked;
127}
128
129inline bool UserID::invalid() const
130{
131 return mInvalid;
132}
133
134inline Validity UserID::validity() const
135{
136 return mValidity;
137}
138
139inline void UserID::setText(const TQString& str)
140{
141 mText = str;
142}
143
144inline void UserID::setRevoked(const bool revoked)
145{
146 mRevoked = revoked;
147}
148
149inline void UserID::setInvalid(const bool invalid)
150{
151 mInvalid = invalid;
152}
153
154inline void UserID::setValidity(const Validity validity)
155{
156 mValidity = validity;
157}
158
159
163{
164 public:
166 Subkey(const KeyID& keyID, const bool secret = false);
167 ~Subkey() {};
168
170 bool secret() const;
171
173 bool revoked() const;
174
176 bool expired() const;
177
179 bool disabled() const;
180
182 bool invalid() const;
183
185 bool canEncrypt() const;
186
188 bool canSign() const;
189
191 bool canCertify() const;
192
194 unsigned int keyAlgorithm() const;
195
197 unsigned int keyLength() const;
198
201 KeyID longKeyID() const;
202
204 KeyID keyID() const;
205
207 TQCString fingerprint() const;
208
210 time_t creationDate() const;
211
213 time_t expirationDate() const;
214
216 void setSecret(const bool secret);
217
219 void setRevoked(const bool revoked);
220
222 void setExpired(const bool expired);
223
225 void setDisabled(const bool disabled);
226
228 void setInvalid(const bool invalid);
229
232 void setCanEncrypt(const bool canEncrypt);
233
236 void setCanSign(const bool canSign);
237
240 void setCanCertify(const bool canCertify);
241
243 void setKeyAlgorithm(const unsigned int keyAlgo);
244
246 void setKeyLength(const unsigned int keyLen);
247
249 void setKeyID(const KeyID& keyID);
250
252 void setFingerprint(const TQCString& fingerprint);
253
256 void setCreationDate(const time_t creationDate);
257
260 void setExpirationDate(const time_t expirationDate);
261
262 protected:
263 bool mSecret : 1;
264 /* various flags */
265 bool mRevoked : 1;
266 bool mExpired : 1;
267 bool mDisabled : 1;
268 bool mInvalid : 1;
269 bool mCanEncrypt : 1;
270 bool mCanSign : 1;
271 bool mCanCertify : 1;
272
273 unsigned int mKeyAlgo;
274 unsigned int mKeyLen;
275 KeyID mKeyID;
276 TQCString mFingerprint;
277 time_t mTimestamp; /* -1 for invalid, 0 for not available */
278 time_t mExpiration; /* -1 for never, 0 for not available */
279};
280
281inline bool Subkey::secret() const
282{
283 return mSecret;
284}
285
286inline bool Subkey::revoked() const
287{
288 return mRevoked;
289}
290
291inline bool Subkey::expired() const
292{
293 return mExpired;
294}
295
296inline bool Subkey::disabled() const
297{
298 return mDisabled;
299}
300
301inline bool Subkey::invalid() const
302{
303 return mInvalid;
304}
305
306inline bool Subkey::canEncrypt() const
307{
308 return mCanEncrypt;
309}
310
311inline bool Subkey::canSign() const
312{
313 return mCanSign;
314}
315
316inline bool Subkey::canCertify() const
317{
318 return mCanCertify;
319}
320
321inline unsigned int Subkey::keyAlgorithm() const
322{
323 return mKeyAlgo;
324}
325
326inline unsigned int Subkey::keyLength() const
327{
328 return mKeyLen;
329}
330
331inline KeyID Subkey::longKeyID() const
332{
333 return mKeyID;
334}
335
336inline KeyID Subkey::keyID() const
337{
338 return mKeyID.right(8);
339}
340
341inline TQCString Subkey::fingerprint() const
342{
343 return mFingerprint;
344}
345
346inline time_t Subkey::creationDate() const
347{
348 return mTimestamp;
349}
350
351inline time_t Subkey::expirationDate() const
352{
353 return mExpiration;
354}
355
356inline void Subkey::setSecret(const bool secret)
357{
358 mSecret = secret;
359}
360
361inline void Subkey::setRevoked(const bool revoked)
362{
363 mRevoked = revoked;
364}
365
366inline void Subkey::setExpired(const bool expired)
367{
368 mExpired = expired;
369}
370
371inline void Subkey::setDisabled(const bool disabled)
372{
373 mDisabled = disabled;
374}
375
376inline void Subkey::setInvalid(const bool invalid)
377{
378 mInvalid = invalid;
379}
380
381inline void Subkey::setCanEncrypt(const bool canEncrypt)
382{
383 mCanEncrypt = canEncrypt;
384}
385
386inline void Subkey::setCanSign(const bool canSign)
387{
388 mCanSign = canSign;
389}
390
391inline void Subkey::setCanCertify(const bool canCertify)
392{
393 mCanCertify = canCertify;
394}
395
396inline void Subkey::setKeyAlgorithm(const unsigned int keyAlgo)
397{
398 mKeyAlgo = keyAlgo;
399}
400
401inline void Subkey::setKeyLength(const unsigned int keyLen)
402{
403 mKeyLen = keyLen;
404}
405
406inline void Subkey::setKeyID(const KeyID& keyID)
407{
408 mKeyID = keyID;
409}
410
411inline void Subkey::setFingerprint(const TQCString& fingerprint)
412{
413 mFingerprint = fingerprint;
414}
415
416inline void Subkey::setCreationDate(const time_t creationDate)
417{
418 mTimestamp = creationDate;
419}
420
421inline void Subkey::setExpirationDate(const time_t expirationDate)
422{
423 mExpiration = expirationDate;
424}
425
426typedef TQPtrList<Subkey> SubkeyList;
427typedef TQPtrListIterator<Subkey> SubkeyListIterator;
428
429
432class Key
433{
434 public:
437 Key( const KeyID& keyid = KeyID(),
438 const TQString& uid = TQString(),
439 const bool secret = false);
440 ~Key();
441
443 void clear();
444
446 bool secret() const;
447
449 bool revoked() const;
450
452 bool expired() const;
453
455 bool disabled() const;
456
458 bool invalid() const;
459
461 bool canEncrypt() const;
462
464 bool canSign() const;
465
467 bool canCertify() const;
468
470 void setSecret(const bool secret);
471
473 void setRevoked(const bool revoked);
474
476 void setExpired(const bool expired);
477
479 void setDisabled(const bool disabled);
480
482 void setInvalid(const bool invalid);
483
486 void setCanEncrypt(const bool canEncrypt);
487
490 void setCanSign(const bool canSign);
491
494 void setCanCertify(const bool canCertify);
495
496
498 EncryptPref encryptionPreference();
499
501 void setEncryptionPreference( const EncryptPref encrPref );
502
503
506 TQString primaryUserID() const;
507
510 KeyID primaryKeyID() const;
511
514 TQCString primaryFingerprint() const;
515
517 bool isNull() const;
518
521 time_t creationDate() const;
522
526 Validity keyTrust() const;
527
530 Validity keyTrust( const TQString& uid ) const;
531
536 void cloneKeyTrust( const Key* key );
537
541 bool isValid() const;
542
546 bool isValidEncryptionKey() const;
547
550 bool isValidSigningKey() const;
551
553 const UserIDList userIDs() const;
554
556 const SubkeyList subkeys() const;
557
560 void addUserID(const TQString& uid,
561 const Validity validity = KPGP_VALIDITY_UNKNOWN,
562 const bool revoked = false,
563 const bool invalid = false);
564
566 void addUserID(const UserID *userID);
567
571 bool matchesUserID(const TQString& str, bool cs = true);
572
575 void addSubkey(const KeyID& keyID, const bool secret = false);
576
578 void addSubkey(const Subkey *subkey);
579
581 Subkey *getSubkey(const KeyID& keyID);
582
584 void setFingerprint(const KeyID& keyID, const TQCString& fpr);
585
586 protected:
587 bool mSecret : 1;
588 /* global flags */
589 bool mRevoked : 1;
590 bool mExpired : 1;
591 bool mDisabled : 1;
592 bool mInvalid : 1;
593 bool mCanEncrypt : 1;
594 bool mCanSign : 1;
595 bool mCanCertify : 1;
596
597 EncryptPref mEncryptPref;
598
599 SubkeyList mSubkeys;
600 UserIDList mUserIDs;
601};
602
603inline bool Key::secret() const
604{
605 return mSecret;
606}
607
608inline bool Key::revoked() const
609{
610 return mRevoked;
611}
612
613inline bool Key::expired() const
614{
615 return mExpired;
616}
617
618inline bool Key::disabled() const
619{
620 return mDisabled;
621}
622
623inline bool Key::invalid() const
624{
625 return mInvalid;
626}
627
628inline bool Key::canEncrypt() const
629{
630 return mCanEncrypt;
631}
632
633inline bool Key::canSign() const
634{
635 return mCanSign;
636}
637
638inline bool Key::canCertify() const
639{
640 return mCanCertify;
641}
642
643inline void Key::setSecret(const bool secret)
644{
645 mSecret = secret;
646}
647
648inline void Key::setRevoked(const bool revoked)
649{
650 mRevoked = revoked;
651}
652
653inline void Key::setExpired(const bool expired)
654{
655 mExpired = expired;
656}
657
658inline void Key::setDisabled(const bool disabled)
659{
660 mDisabled = disabled;
661}
662
663inline void Key::setInvalid(const bool invalid)
664{
665 mInvalid = invalid;
666}
667
668inline void Key::setCanEncrypt(const bool canEncrypt)
669{
670 mCanEncrypt = canEncrypt;
671}
672
673inline void Key::setCanSign(const bool canSign)
674{
675 mCanSign = canSign;
676}
677
678inline void Key::setCanCertify(const bool canCertify)
679{
680 mCanCertify = canCertify;
681}
682
683inline EncryptPref Key::encryptionPreference()
684{
685 return mEncryptPref;
686}
687
688inline void Key::setEncryptionPreference( const EncryptPref encrPref )
689{
690 mEncryptPref = encrPref;
691}
692
693inline TQString Key::primaryUserID() const
694{
695 UserID *uid = mUserIDs.getFirst();
696
697 if (uid)
698 return uid->text();
699 else
700 return TQString();
701}
702
703inline KeyID Key::primaryKeyID() const
704{
705 Subkey *key = mSubkeys.getFirst();
706
707 if (key)
708 return key->keyID();
709 else
710 return KeyID();
711}
712
713inline TQCString Key::primaryFingerprint() const
714{
715 Subkey *key = mSubkeys.getFirst();
716
717 if (key)
718 return key->fingerprint();
719 else
720 return TQCString();
721}
722
723inline const UserIDList Key::userIDs() const
724{
725 return mUserIDs;
726}
727
728inline const SubkeyList Key::subkeys() const
729{
730 return mSubkeys;
731}
732
733inline bool Key::isNull() const
734{
735 return (mUserIDs.isEmpty() || mSubkeys.isEmpty());
736}
737
738inline time_t Key::creationDate() const
739{
740 if( !mSubkeys.isEmpty() )
741 return mSubkeys.getFirst()->creationDate();
742 else
743 return -1;
744}
745
746inline void Key::addUserID(const UserID *userID)
747{
748 if (userID)
749 mUserIDs.append(userID);
750}
751
752inline void Key::addSubkey(const Subkey *subkey)
753{
754 if (subkey)
755 mSubkeys.append(subkey);
756}
757
758
759
760typedef TQPtrList<Key> KeyListBase;
761typedef TQPtrListIterator<Key> KeyListIterator;
762
763class KeyList : public KeyListBase
764{
765 public:
766 ~KeyList()
767 { clear(); }
768
769 private:
770 int compareItems( TQPtrCollection::Item s1, TQPtrCollection::Item s2 )
771 {
772 // sort case insensitively by the primary User IDs
773 return TQString::compare((static_cast<Key*>(s1))->primaryUserID().lower(),
774 (static_cast<Key*>(s2))->primaryUserID().lower());
775 }
776};
777
778} // namespace Kpgp
779
780#endif
This class is used to store information about a PGP key.
Definition: kpgpkey.h:433
bool canCertify() const
Returns true if the key can be used to certify keys.
Definition: kpgpkey.h:638
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
TQString primaryUserID() const
Returns the primary user ID or a null string if there are no user IDs.
Definition: kpgpkey.h:693
void setSecret(const bool secret)
Sets the flag if the key is a secret key to secret.
Definition: kpgpkey.h:643
void setFingerprint(const KeyID &keyID, const TQCString &fpr)
Sets the fingerprint of the given subkey to fpr.
Definition: kpgpkey.cpp:249
void setInvalid(const bool invalid)
Sets the flag if the key is invalid to invalid.
Definition: kpgpkey.h:663
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 canSign() const
Returns true if the key can be used to sign data.
Definition: kpgpkey.h:633
void setCanEncrypt(const bool canEncrypt)
Sets the flag if the key can be used to encrypt data to canEncrypt.
Definition: kpgpkey.h:668
bool canEncrypt() const
Returns true if the key can be used to encrypt data.
Definition: kpgpkey.h:628
bool disabled() const
Returns true if the key has been disabled.
Definition: kpgpkey.h:618
bool revoked() const
Returns true if the key has been revoked.
Definition: kpgpkey.h:608
void setEncryptionPreference(const EncryptPref encrPref)
Sets the encryption preference for this key to encrPref.
Definition: kpgpkey.h:688
void setCanSign(const bool canSign)
Sets the flag if the key can be used to sign data to canSign.
Definition: kpgpkey.h:673
const UserIDList userIDs() const
Returns the list of userIDs.
Definition: kpgpkey.h:723
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 expired() const
Returns true if the key has expired.
Definition: kpgpkey.h:613
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 setCanCertify(const bool canCertify)
Sets the flag if the key can be used to certify keys to canCertify.
Definition: kpgpkey.h:678
EncryptPref encryptionPreference()
Returns the encryption preference for this key.
Definition: kpgpkey.h:683
time_t creationDate() const
Returns the creation date of the primary subkey.
Definition: kpgpkey.h:738
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
bool isNull() const
Returns true if there are no user IDs or no subkeys.
Definition: kpgpkey.h:733
const SubkeyList subkeys() const
Returns the list of subkeys.
Definition: kpgpkey.h:728
KeyID primaryKeyID() const
Returns the key ID of the primary key or a null string if there are no subkeys.
Definition: kpgpkey.h:703
void setRevoked(const bool revoked)
Sets the flag if the key has been revoked to revoked.
Definition: kpgpkey.h:648
void setExpired(const bool expired)
Sets the flag if the key has expired to expired.
Definition: kpgpkey.h:653
TQCString primaryFingerprint() const
Returns the fingerprint of the primary key or a null string if there are no subkeys.
Definition: kpgpkey.h:713
void setDisabled(const bool disabled)
Sets the flag if the key has been disabled to disabled.
Definition: kpgpkey.h:658
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 setSecret(const bool secret)
Sets the flag if the subkey is a secret subkey to secret.
Definition: kpgpkey.h:356
time_t expirationDate() const
Returns the expiration date of the subkey.
Definition: kpgpkey.h:351
time_t creationDate() const
Returns the creation date of the subkey.
Definition: kpgpkey.h:346
bool expired() const
Returns true if the subkey has expired.
Definition: kpgpkey.h:291
void setDisabled(const bool disabled)
Sets the flag if the subkey has been disabled to disabled.
Definition: kpgpkey.h:371
void setRevoked(const bool revoked)
Sets the flag if the subkey has been revoked to revoked.
Definition: kpgpkey.h:361
void setCanCertify(const bool canCertify)
Sets the flag if the subkey can be used to certify keys to canCertify.
Definition: kpgpkey.h:391
void setFingerprint(const TQCString &fingerprint)
Sets the fingerprint of the subkey to fingerprint.
Definition: kpgpkey.h:411
void setKeyAlgorithm(const unsigned int keyAlgo)
Sets the key algorithm of the subkey to keyAlgo.
Definition: kpgpkey.h:396
bool canEncrypt() const
Returns true if the subkey can be used to encrypt data.
Definition: kpgpkey.h:306
Subkey(const KeyID &keyID, const bool secret=false)
Constructs a new subkey with the given key ID.
Definition: kpgpkey.cpp:62
void setKeyLength(const unsigned int keyLen)
Sets the key length of the subkey to keyLen bits.
Definition: kpgpkey.h:401
void setCanEncrypt(const bool canEncrypt)
Sets the flag if the subkey can be used to encrypt data to canEncrypt.
Definition: kpgpkey.h:381
unsigned int keyLength() const
Returns the length of the subkey in bits.
Definition: kpgpkey.h:326
void setCanSign(const bool canSign)
Sets the flag if the subkey can be used to sign data to canSign.
Definition: kpgpkey.h:386
unsigned int keyAlgorithm() const
Returns the key algorithm of the subkey.
Definition: kpgpkey.h:321
bool canCertify() const
Returns true if the subkey can be used to certify keys.
Definition: kpgpkey.h:316
void setExpired(const bool expired)
Sets the flag if the subkey has expired to expired.
Definition: kpgpkey.h:366
bool disabled() const
Returns true if the subkey has been disabled.
Definition: kpgpkey.h:296
KeyID longKeyID() const
Returns the long 64 bit key ID of the subkey if it's available.
Definition: kpgpkey.h:331
bool invalid() const
Returns true if the subkey is invalid.
Definition: kpgpkey.h:301
KeyID keyID() const
Returns the (short) 32 bit key ID of the subkey.
Definition: kpgpkey.h:336
void setInvalid(const bool invalid)
Sets the flag if the subkey is invalid to invalid.
Definition: kpgpkey.h:376
void setKeyID(const KeyID &keyID)
Sets the key ID of the subkey to keyID.
Definition: kpgpkey.h:406
bool canSign() const
Returns true if the subkey can be used to sign data.
Definition: kpgpkey.h:311
void setCreationDate(const time_t creationDate)
Sets the creation date of the subkey to creationDate seconds since Epoch.
Definition: kpgpkey.h:416
bool revoked() const
Returns true if the subkey has been revoked.
Definition: kpgpkey.h:286
bool secret() const
Returns true if the subkey is a secret subkey.
Definition: kpgpkey.h:281
void setExpirationDate(const time_t expirationDate)
Sets the expiration date of the subkey to expirationDate seconds since Epoch.
Definition: kpgpkey.h:421
TQCString fingerprint() const
Returns the fingerprint of the subkey.
Definition: kpgpkey.h:341
This class is used to store information about a user id of a PGP key.
Definition: kpgpkey.h:75
void setValidity(const Validity validity)
Sets the validity of resp.
Definition: kpgpkey.h:154
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
void setInvalid(const bool invalid)
Sets the flag if the user id is invalid to invalid.
Definition: kpgpkey.h:149
bool invalid() const
Returns true if the user id is invalid.
Definition: kpgpkey.h:129
TQString text() const
Returns the text of the user id.
Definition: kpgpkey.h:119
void setRevoked(const bool revoked)
Sets the flag if the user id has been revoked to revoked.
Definition: kpgpkey.h:144
bool revoked() const
Returns true if the user id has been revoked.
Definition: kpgpkey.h:124
void setText(const TQString &str)
Sets the text of the user id to str.
Definition: kpgpkey.h:139
Validity validity() const
Returns the validity of resp.
Definition: kpgpkey.h:134