1 /*
2     SPDX-FileCopyrightText: 2006, 2007 Jimmy Gilles <jimmygilles@gmail.com>
3     SPDX-FileCopyrightText: 2007, 2008, 2009, 2010, 2012, 2013, 2014 Rolf Eike Beer <kde@opensource.sf-tec.de>
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #ifndef KGPGKEY_H
8 #define KGPGKEY_H
9 
10 #include <gpgme.h>
11 #include <QSharedDataPointer>
12 #include <QSharedData>
13 #include <QPointer>
14 #include <QObject>
15 #include <QList>
16 #include <QDateTime>
17 
18 class QStringList;
19 
20 namespace KgpgCore
21 {
22 
23 //BEGIN Enums
24 
25 enum KgpgKeyAlgoFlag
26 {
27     ALGO_UNKNOWN = 0,
28     ALGO_RSA = 1,
29     ALGO_DSA = 2,
30     ALGO_ELGAMAL = 4,
31     ALGO_DSA_ELGAMAL = ALGO_DSA | ALGO_ELGAMAL,
32     ALGO_ECC = 8,
33     ALGO_ECDSA = 16,
34     ALGO_ECDH = 32,
35     ALGO_EDDSA = 64,
36     ALGO_RSA_RSA = 0x10001
37 };
38 Q_DECLARE_FLAGS(KgpgKeyAlgo, KgpgKeyAlgoFlag)
39 Q_DECLARE_OPERATORS_FOR_FLAGS(KgpgKeyAlgo)
40 
41 /*! \brief trust levels of keys, uids and uats
42  *
43  * These values represent the trust that you have in a public key or obe if it's
44  * user ids or attributes (i.e. photo ids). They are more or less ordered by
45  * the level of trust. Every value but the first and the last matches one trust
46  * value that is
47  */
48 enum KgpgKeyTrustFlag
49 {
50     TRUST_MINIMUM = 0,		//!< internal value for use in filters
51     TRUST_INVALID = 1,		//!< key is invalid
52     TRUST_DISABLED = 2,		//!< key is disabled by user (not owner)
53     TRUST_REVOKED = 3,		//!< key is revoked by owner
54     TRUST_EXPIRED = 4,		//!< key is beyond it's expiry date
55     TRUST_UNDEFINED = 5,	//!< trust value undefined (i.e. you did not set a trust level)
56     TRUST_UNKNOWN = 6,		//!< trust value unknown (i.e. no entry in gpg's trust database)
57     TRUST_NONE = 7,		//!< there is no trusted path to this key
58     TRUST_MARGINAL = 8,		//!< there is a minimal level of trust
59     TRUST_FULL = 9,		//!< you can fully trust this key
60     TRUST_ULTIMATE = 10,	//!< this key has highest possible level of trust (e.g. your own secret keys)
61     TRUST_NOKEY = 11		//!< internal value, e.g. for key groups
62 };
63 Q_DECLARE_FLAGS(KgpgKeyTrust, KgpgKeyTrustFlag)
64 Q_DECLARE_OPERATORS_FOR_FLAGS(KgpgKeyTrust)
65 
66 enum KgpgSubKeyTypeFlag
67 {
68     SKT_ENCRYPTION = 0x1,
69     SKT_SIGNATURE = 0x2,
70     SKT_AUTHENTICATION = 0x4,
71     SKT_CERTIFICATION = 0x8
72 };
73 Q_DECLARE_FLAGS(KgpgSubKeyType, KgpgSubKeyTypeFlag)
74 Q_DECLARE_OPERATORS_FOR_FLAGS(KgpgSubKeyType)
75 
76 /*! \brief types of items in the item models
77  *
78  * Every item in the item models is of one of the following types. Some of the
79  * items can have properties of more than one basic type, e.g. a key pair can
80  * act both as a secret and a public key. Because of this the value for key
81  * pairs is a composite of the two "elementary" types for secret and public
82  * keys. Other compositions than the ones defined here must not be used to set
83  * an item type, but may of course be used as a mask for comparison.
84  */
85 enum KgpgItemTypeFlag
86 {
87     ITYPE_GROUP = 1,				//!< the element is a GnuPG key group
88     ITYPE_SECRET = 2,				//!< secret key
89     ITYPE_PUBLIC = 4,				//!< public key
90     ITYPE_PAIR = ITYPE_SECRET | ITYPE_PUBLIC,	//!< key pair
91     ITYPE_GSECRET = ITYPE_GROUP | ITYPE_SECRET,	//!< secret key as member of a key group
92     ITYPE_GPUBLIC = ITYPE_GROUP | ITYPE_PUBLIC,	//!< public key as member of a key group
93     ITYPE_GPAIR = ITYPE_GROUP | ITYPE_PAIR,	//!< key pair as member of a key group
94     ITYPE_SUB = 8,				//!< subkey of a public or secret key
95     ITYPE_UID = 16,				//!< additional user id
96     ITYPE_UAT = 32,				//!< user attribute to a key (i.e. photo id)
97     ITYPE_REVSIGN = 64,				//!< revokation signature
98     ITYPE_SIGN = 128				//!< signature (to a key, uid or uat)
99 };
Q_DECLARE_FLAGS(KgpgItemType,KgpgItemTypeFlag)100 Q_DECLARE_FLAGS(KgpgItemType, KgpgItemTypeFlag)
101 Q_DECLARE_OPERATORS_FOR_FLAGS(KgpgItemType)
102 
103 //END Enums
104 
105 //BEGIN KeySub
106 
107 class KgpgKeySubPrivate : public QSharedData
108 {
109     KgpgKeySubPrivate();
110 public:
111     KgpgKeySubPrivate(const QString &id, const uint size, const KgpgKeyTrust trust, const KgpgKeyAlgo algo, const KgpgSubKeyType type,
112                       const QDateTime &date, const QString &curve);
113 
114     bool            gpgsubvalid;
115     const QString   gpgsubid;
116     const uint      gpgsubsize;
117     QDateTime       gpgsubexpiration;
118     const QDateTime gpgsubcreation;
119     const KgpgKeyTrust gpgsubtrust;
120     const KgpgKeyAlgo gpgsubalgo;
121     const KgpgSubKeyType gpgsubtype;
122     const QString   gpgcurve;
123 
124     bool operator==(const KgpgKeySubPrivate &other) const;
125     inline bool operator!=(const KgpgKeySubPrivate &other) const
126     { return !operator==(other); }
127 };
128 
129 class KgpgKeySub
130 {
131     KgpgKeySub();
132 public:
133     KgpgKeySub(const QString &id, const uint size, const KgpgKeyTrust trust, const KgpgKeyAlgo algo, const KgpgSubKeyType type,
134                const QDateTime &date, const QString &curve);
135     KgpgKeySub(const KgpgKeySub &other);
136 
137     void setExpiration(const QDateTime &date);
138     void setValid(const bool valid); // FIXME : is it possible to have a subkey that is not valid (disabled)? Please give an example. Thx. If not, this method should be removed.
139 
140     QString id() const;
141     uint size() const;
142     QString strength() const;
143     bool unlimited() const;
144     QDateTime expirationDate() const;
145     QDateTime creationDate() const;
146     KgpgKeyTrust trust() const;
147     KgpgKeyAlgo algorithm() const;
148     bool valid() const;
149     KgpgSubKeyType type() const;
150     QString curve() const;
151 
152     bool operator==(const KgpgKeySub &other) const;
153     inline bool operator!=(const KgpgKeySub &other) const
154     { return !operator==(other); }
155     KgpgKeySub& operator=(const KgpgKeySub &other);
156 
157 private:
158     QSharedDataPointer<KgpgKeySubPrivate> d;
159 };
160 
161 class KgpgKeySubList : public QList<KgpgKeySub>, public QObject
162 {
163 public:
KgpgKeySubList()164     inline KgpgKeySubList() { }
KgpgKeySubList(const KgpgKeySub & sub)165     inline explicit KgpgKeySubList(const KgpgKeySub &sub) { append(sub); }
KgpgKeySubList(const KgpgKeySubList & other)166     inline KgpgKeySubList(const KgpgKeySubList &other) : QList<KgpgKeySub>(other), QObject() { }
KgpgKeySubList(const QList<KgpgKeySub> & other)167     inline KgpgKeySubList(const QList<KgpgKeySub> &other) : QList<KgpgKeySub>(other), QObject() { }
168 
169     inline KgpgKeySubList operator+(const KgpgKeySubList &other) const
170     {
171         KgpgKeySubList n = *this;
172         n += other;
173         return n;
174     }
175 
176     inline KgpgKeySubList &operator<<(KgpgKeySub sub)
177     {
178         append(sub);
179         return *this;
180     }
181 
182     inline KgpgKeySubList &operator<<(const KgpgKeySubList &l)
183     {
184         *this += l;
185         return *this;
186     }
187 };
188 typedef QPointer<KgpgKeySubList> KgpgKeySubListPtr;
189 
190 //END KeySub
191 
192 
193 //BEGIN Key
194 
195 class KgpgKeyPrivate : public QSharedData
196 {
197     KgpgKeyPrivate();
198 public:
199     /**
200      * @brief constructor
201      * @param id id of the key (i.e. fingerprint)
202      * @param size length of the key in bits
203      * @param trust trust to this key
204      * @param algo public key algorithm
205      * @param subtype key capabilities of this subkey
206      * @param keytype key capabilities of this and all subkeys combined
207      * @param creationDate date of key creation
208      * @param curve GnuPG ECC curve name
209      */
210     KgpgKeyPrivate(const QString &id, const uint size, const KgpgCore::KgpgKeyTrust trust, const KgpgCore::KgpgKeyAlgo algo,
211                    const KgpgCore::KgpgSubKeyType subtype, const KgpgCore::KgpgSubKeyType keytype, const QDateTime &creationDate,
212                    const QString &curve);
213 
214     bool          gpgkeysecret;
215     bool          gpgkeyvalid;
216     QString       gpgkeymail;
217     QString       gpgkeyname;
218     QString       gpgkeycomment;
219     QString       gpgkeyfingerprint;
220     const QString gpgkeyid;
221     const uint    gpgkeysize;
222     gpgme_validity_t gpgkeyownertrust;
223     const KgpgKeyTrust gpgkeytrust;
224     const QDateTime gpgkeycreation;
225     QDateTime     gpgkeyexpiration;
226     const KgpgKeyAlgo gpgkeyalgo;
227     const KgpgSubKeyType gpgsubtype;
228     const KgpgSubKeyType gpgkeytype;
229     QString       gpgcurve;
230 
231     KgpgKeySubListPtr gpgsublist;
232 
233     bool operator==(const KgpgKeyPrivate &other) const;
234     inline bool operator!=(const KgpgKeyPrivate &other) const
235     { return !operator==(other); }
236 };
237 
238 class KgpgKey
239 {
240 public:
241     /**
242      * @brief constructor
243      * @param id id of the key (i.e. fingerprint)
244      * @param size length of the key in bits
245      * @param trust trust to this key
246      * @param algo public key algorithm
247      * @param subtype key capabilities of this subkey
248      * @param keytype key capabilities of this and all subkeys combined
249      * @param creationDate date of key creation
250      * @param curve GnuPG ECC curve name
251      */
252     KgpgKey(const QString &id, const uint size, const KgpgCore::KgpgKeyTrust trust, const KgpgCore::KgpgKeyAlgo algo,
253             const KgpgCore::KgpgSubKeyType subtype, const KgpgCore::KgpgSubKeyType keytype, const QDateTime& creationDate,
254             const QString &curve);
255     KgpgKey(const KgpgKey &other);
256 
257     void setSecret(const bool secret);
258     void setValid(const bool valid);
259     void setName(const QString &name);
260     void setEmail(const QString &email);
261     void setComment(const QString &comment);
262     void setFingerprint(const QString &fingerprint);
263     void setOwnerTrust(const gpgme_validity_t owtrust);
264     void setExpiration(const QDateTime &date);
265 
266     bool secret() const;
267     bool valid() const;
268     QString id() const;
269     QString fullId() const;
270     QString name() const;
271     QString email() const;
272     QString comment() const;
273     const QString &fingerprint() const;
274     uint size() const;
275     QString strength() const;
276     uint encryptionSize() const;
277     QString encryptionStrength() const;
278     gpgme_validity_t ownerTrust() const;
279     KgpgKeyTrust trust() const;
280     QDateTime creationDate() const;
281     QDateTime expirationDate() const;
282     bool unlimited() const;
283     KgpgKeyAlgo algorithm() const;
284     KgpgKeyAlgo encryptionAlgorithm() const;
285     KgpgSubKeyType subtype() const;
286     KgpgSubKeyType keytype() const;
287     QString curve() const;
288 
289     KgpgKeySubListPtr subList() const;
290 
291     bool operator==(const KgpgKey &other) const;
292     inline bool operator!=(const KgpgKey &other) const
293     { return !operator==(other); }
294     KgpgKey& operator=(const KgpgKey &other);
295 
296 private:
297     QSharedDataPointer<KgpgKeyPrivate> d;
298 };
299 
300 class KgpgKeyList : public QList<KgpgKey>
301 {
302 public:
KgpgKeyList()303     inline KgpgKeyList() { }
KgpgKeyList(const KgpgKey & key)304     inline explicit KgpgKeyList(const KgpgKey &key) { append(key); }
KgpgKeyList(const KgpgKeyList & other)305     inline KgpgKeyList(const KgpgKeyList &other) : QList<KgpgKey>(other) { }
KgpgKeyList(const QList<KgpgKey> & other)306     inline KgpgKeyList(const QList<KgpgKey> &other) : QList<KgpgKey>(other) { }
307 
308     inline KgpgKeyList& operator=(const KgpgKeyList &other)
309     {
310         QList<KgpgKey>::operator=(static_cast<const QList<KgpgKey> >(other));
311         return *this;
312     }
313 
314     inline KgpgKeyList operator+(const KgpgKeyList &other) const
315     {
316         KgpgKeyList n = *this;
317         n += other;
318         return n;
319     }
320 
321     inline KgpgKeyList &operator<<(KgpgKey key)
322     {
323         append(key);
324         return *this;
325     }
326 
327     inline KgpgKeyList &operator<<(const KgpgKeyList &l)
328     {
329         *this += l;
330         return *this;
331     }
332 
333     operator QStringList() const;
334 };
335 
336 //END Key
337 
338 } // namespace
339 
340 #endif // KGPGKEY_H
341