File indexing completed on 2024-12-08 05:13:53

0001 /*
0002     SPDX-FileCopyrightText: 2006, 2007 Jimmy Gilles <jimmygilles@gmail.com>
0003     SPDX-FileCopyrightText: 2007, 2008, 2009, 2010, 2012, 2013, 2014 Rolf Eike Beer <kde@opensource.sf-tec.de>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef KGPGKEY_H
0008 #define KGPGKEY_H
0009 
0010 #include <gpgme.h>
0011 #include <QSharedDataPointer>
0012 #include <QSharedData>
0013 #include <QPointer>
0014 #include <QObject>
0015 #include <QList>
0016 #include <QDateTime>
0017 #include <QStringList>
0018 
0019 namespace KgpgCore
0020 {
0021 
0022 //BEGIN Enums
0023 
0024 enum KgpgKeyAlgoFlag
0025 {
0026     ALGO_UNKNOWN = 0,
0027     ALGO_RSA = 1,
0028     ALGO_DSA = 2,
0029     ALGO_ELGAMAL = 4,
0030     ALGO_DSA_ELGAMAL = ALGO_DSA | ALGO_ELGAMAL,
0031     ALGO_ECC = 8,
0032     ALGO_ECDSA = 16,
0033     ALGO_ECDH = 32,
0034     ALGO_EDDSA = 64,
0035     ALGO_RSA_RSA = 0x10001
0036 };
0037 Q_DECLARE_FLAGS(KgpgKeyAlgo, KgpgKeyAlgoFlag)
0038 Q_DECLARE_OPERATORS_FOR_FLAGS(KgpgKeyAlgo)
0039 
0040 /*! \brief trust levels of keys, uids and uats
0041  *
0042  * These values represent the trust that you have in a public key or obe if it's
0043  * user ids or attributes (i.e. photo ids). They are more or less ordered by
0044  * the level of trust. Every value but the first and the last matches one trust
0045  * value that is 
0046  */
0047 enum KgpgKeyTrustFlag
0048 {
0049     TRUST_MINIMUM = 0,      //!< internal value for use in filters
0050     TRUST_INVALID = 1,      //!< key is invalid
0051     TRUST_DISABLED = 2,     //!< key is disabled by user (not owner)
0052     TRUST_REVOKED = 3,      //!< key is revoked by owner
0053     TRUST_EXPIRED = 4,      //!< key is beyond it's expiry date
0054     TRUST_UNDEFINED = 5,    //!< trust value undefined (i.e. you did not set a trust level)
0055     TRUST_UNKNOWN = 6,      //!< trust value unknown (i.e. no entry in gpg's trust database)
0056     TRUST_NONE = 7,     //!< there is no trusted path to this key
0057     TRUST_MARGINAL = 8,     //!< there is a minimal level of trust
0058     TRUST_FULL = 9,     //!< you can fully trust this key
0059     TRUST_ULTIMATE = 10,    //!< this key has highest possible level of trust (e.g. your own secret keys)
0060     TRUST_NOKEY = 11        //!< internal value, e.g. for key groups
0061 };
0062 Q_DECLARE_FLAGS(KgpgKeyTrust, KgpgKeyTrustFlag)
0063 Q_DECLARE_OPERATORS_FOR_FLAGS(KgpgKeyTrust)
0064 
0065 enum KgpgSubKeyTypeFlag
0066 {
0067     SKT_ENCRYPTION = 0x1,
0068     SKT_SIGNATURE = 0x2,
0069     SKT_AUTHENTICATION = 0x4,
0070     SKT_CERTIFICATION = 0x8
0071 };
0072 Q_DECLARE_FLAGS(KgpgSubKeyType, KgpgSubKeyTypeFlag)
0073 Q_DECLARE_OPERATORS_FOR_FLAGS(KgpgSubKeyType)
0074 
0075 /*! \brief types of items in the item models
0076  *
0077  * Every item in the item models is of one of the following types. Some of the
0078  * items can have properties of more than one basic type, e.g. a key pair can
0079  * act both as a secret and a public key. Because of this the value for key
0080  * pairs is a composite of the two "elementary" types for secret and public
0081  * keys. Other compositions than the ones defined here must not be used to set
0082  * an item type, but may of course be used as a mask for comparison.
0083  */
0084 enum KgpgItemTypeFlag
0085 {
0086     ITYPE_GROUP = 1,                //!< the element is a GnuPG key group
0087     ITYPE_SECRET = 2,               //!< secret key
0088     ITYPE_PUBLIC = 4,               //!< public key
0089     ITYPE_PAIR = ITYPE_SECRET | ITYPE_PUBLIC,   //!< key pair
0090     ITYPE_GSECRET = ITYPE_GROUP | ITYPE_SECRET, //!< secret key as member of a key group
0091     ITYPE_GPUBLIC = ITYPE_GROUP | ITYPE_PUBLIC, //!< public key as member of a key group
0092     ITYPE_GPAIR = ITYPE_GROUP | ITYPE_PAIR, //!< key pair as member of a key group
0093     ITYPE_SUB = 8,              //!< subkey of a public or secret key
0094     ITYPE_UID = 16,             //!< additional user id
0095     ITYPE_UAT = 32,             //!< user attribute to a key (i.e. photo id)
0096     ITYPE_REVSIGN = 64,             //!< revokation signature
0097     ITYPE_SIGN = 128                //!< signature (to a key, uid or uat)
0098 };
0099 Q_DECLARE_FLAGS(KgpgItemType, KgpgItemTypeFlag)
0100 Q_DECLARE_OPERATORS_FOR_FLAGS(KgpgItemType)
0101 
0102 //END Enums
0103 
0104 //BEGIN KeySub
0105 
0106 class KgpgKeySubPrivate : public QSharedData
0107 {
0108     KgpgKeySubPrivate();
0109 public:
0110     KgpgKeySubPrivate(const QString &id, const uint size, const KgpgKeyTrust trust, const KgpgKeyAlgo algo, const KgpgSubKeyType type,
0111                       const QDateTime &date, const QString &curve);
0112 
0113     bool            gpgsubvalid;
0114     const QString   gpgsubid;
0115     const uint      gpgsubsize;
0116     QDateTime       gpgsubexpiration;
0117     const QDateTime gpgsubcreation;
0118     const KgpgKeyTrust gpgsubtrust;
0119     const KgpgKeyAlgo gpgsubalgo;
0120     const KgpgSubKeyType gpgsubtype;
0121     const QString   gpgcurve;
0122 
0123     bool operator==(const KgpgKeySubPrivate &other) const;
0124     inline bool operator!=(const KgpgKeySubPrivate &other) const
0125     { return !operator==(other); }
0126 };
0127 
0128 class KgpgKeySub
0129 {
0130     KgpgKeySub();
0131 public:
0132     KgpgKeySub(const QString &id, const uint size, const KgpgKeyTrust trust, const KgpgKeyAlgo algo, const KgpgSubKeyType type,
0133                const QDateTime &date, const QString &curve);
0134     KgpgKeySub(const KgpgKeySub &other);
0135 
0136     void setExpiration(const QDateTime &date);
0137     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.
0138 
0139     QString id() const;
0140     uint size() const;
0141     QString strength() const;
0142     bool unlimited() const;
0143     QDateTime expirationDate() const;
0144     QDateTime creationDate() const;
0145     KgpgKeyTrust trust() const;
0146     KgpgKeyAlgo algorithm() const;
0147     bool valid() const;
0148     KgpgSubKeyType type() const;
0149     QString curve() const;
0150 
0151     bool operator==(const KgpgKeySub &other) const;
0152     inline bool operator!=(const KgpgKeySub &other) const
0153     { return !operator==(other); }
0154     KgpgKeySub& operator=(const KgpgKeySub &other);
0155 
0156 private:
0157     QSharedDataPointer<KgpgKeySubPrivate> d;
0158 };
0159 
0160 class KgpgKeySubList : public QList<KgpgKeySub>, public QObject
0161 {
0162 public:
0163     inline KgpgKeySubList() { }
0164     inline explicit KgpgKeySubList(const KgpgKeySub &sub) { append(sub); }
0165     inline KgpgKeySubList(const KgpgKeySubList &other) : QList<KgpgKeySub>(other), QObject() { }
0166     inline KgpgKeySubList(const QList<KgpgKeySub> &other) : QList<KgpgKeySub>(other), QObject() { }
0167 
0168     inline KgpgKeySubList operator+(const KgpgKeySubList &other) const
0169     {
0170         KgpgKeySubList n = *this;
0171         n += other;
0172         return n;
0173     }
0174 
0175     inline KgpgKeySubList &operator<<(KgpgKeySub sub)
0176     {
0177         append(sub);
0178         return *this;
0179     }
0180 
0181     inline KgpgKeySubList &operator<<(const KgpgKeySubList &l)
0182     {
0183         *this += l;
0184         return *this;
0185     }
0186 };
0187 typedef QPointer<KgpgKeySubList> KgpgKeySubListPtr;
0188 
0189 //END KeySub
0190 
0191 
0192 //BEGIN Key
0193 
0194 class KgpgKeyPrivate : public QSharedData
0195 {
0196     KgpgKeyPrivate();
0197 public:
0198     /**
0199      * @brief constructor
0200      * @param id id of the key (i.e. fingerprint)
0201      * @param size length of the key in bits
0202      * @param trust trust to this key
0203      * @param algo public key algorithm
0204      * @param subtype key capabilities of this subkey
0205      * @param keytype key capabilities of this and all subkeys combined
0206      * @param creationDate date of key creation
0207      * @param curve GnuPG ECC curve name
0208      */
0209     KgpgKeyPrivate(const QString &id, const uint size, const KgpgCore::KgpgKeyTrust trust, const KgpgCore::KgpgKeyAlgo algo,
0210                    const KgpgCore::KgpgSubKeyType subtype, const KgpgCore::KgpgSubKeyType keytype, const QDateTime &creationDate,
0211                    const QString &curve);
0212 
0213     bool          gpgkeysecret;
0214     bool          gpgkeyvalid;
0215     QString       gpgkeymail;
0216     QString       gpgkeyname;
0217     QString       gpgkeycomment;
0218     QString       gpgkeyfingerprint;
0219     const QString gpgkeyid;
0220     const uint    gpgkeysize;
0221     gpgme_validity_t gpgkeyownertrust;
0222     const KgpgKeyTrust gpgkeytrust;
0223     const QDateTime gpgkeycreation;
0224     QDateTime     gpgkeyexpiration;
0225     const KgpgKeyAlgo gpgkeyalgo;
0226     const KgpgSubKeyType gpgsubtype;
0227     const KgpgSubKeyType gpgkeytype;
0228     QString       gpgcurve;
0229 
0230     KgpgKeySubListPtr gpgsublist;
0231 
0232     bool operator==(const KgpgKeyPrivate &other) const;
0233     inline bool operator!=(const KgpgKeyPrivate &other) const
0234     { return !operator==(other); }
0235 };
0236 
0237 class KgpgKey
0238 {
0239 public:
0240     /**
0241      * @brief constructor
0242      * @param id id of the key (i.e. fingerprint)
0243      * @param size length of the key in bits
0244      * @param trust trust to this key
0245      * @param algo public key algorithm
0246      * @param subtype key capabilities of this subkey
0247      * @param keytype key capabilities of this and all subkeys combined
0248      * @param creationDate date of key creation
0249      * @param curve GnuPG ECC curve name
0250      */
0251     KgpgKey(const QString &id, const uint size, const KgpgCore::KgpgKeyTrust trust, const KgpgCore::KgpgKeyAlgo algo,
0252             const KgpgCore::KgpgSubKeyType subtype, const KgpgCore::KgpgSubKeyType keytype, const QDateTime& creationDate,
0253             const QString &curve);
0254     KgpgKey(const KgpgKey &other);
0255 
0256     void setSecret(const bool secret);
0257     void setValid(const bool valid);
0258     void setName(const QString &name);
0259     void setEmail(const QString &email);
0260     void setComment(const QString &comment);
0261     void setFingerprint(const QString &fingerprint);
0262     void setOwnerTrust(const gpgme_validity_t owtrust);
0263     void setExpiration(const QDateTime &date);
0264 
0265     bool secret() const;
0266     bool valid() const;
0267     QString id() const;
0268     QString fullId() const;
0269     QString name() const;
0270     QString email() const;
0271     QString comment() const;
0272     const QString &fingerprint() const;
0273     uint size() const;
0274     QString strength() const;
0275     uint encryptionSize() const;
0276     QString encryptionStrength() const;
0277     gpgme_validity_t ownerTrust() const;
0278     KgpgKeyTrust trust() const;
0279     QDateTime creationDate() const;
0280     QDateTime expirationDate() const;
0281     bool unlimited() const;
0282     KgpgKeyAlgo algorithm() const;
0283     KgpgKeyAlgo encryptionAlgorithm() const;
0284     KgpgSubKeyType subtype() const;
0285     KgpgSubKeyType keytype() const;
0286     QString curve() const;
0287 
0288     KgpgKeySubListPtr subList() const;
0289 
0290     bool operator==(const KgpgKey &other) const;
0291     inline bool operator!=(const KgpgKey &other) const
0292     { return !operator==(other); }
0293     KgpgKey& operator=(const KgpgKey &other);
0294 
0295 private:
0296     QSharedDataPointer<KgpgKeyPrivate> d;
0297 };
0298 
0299 class KgpgKeyList : public QList<KgpgKey>
0300 {
0301 public:
0302     inline KgpgKeyList() { }
0303     inline explicit KgpgKeyList(const KgpgKey &key) { append(key); }
0304     inline KgpgKeyList(const KgpgKeyList &other) : QList<KgpgKey>(other) { }
0305     inline KgpgKeyList(const QList<KgpgKey> &other) : QList<KgpgKey>(other) { }
0306 
0307     inline KgpgKeyList& operator=(const KgpgKeyList &other)
0308     {
0309         QList<KgpgKey>::operator=(static_cast<const QList<KgpgKey> >(other));
0310         return *this;
0311     }
0312 
0313     inline KgpgKeyList operator+(const KgpgKeyList &other) const
0314     {
0315         KgpgKeyList n = *this;
0316         n += other;
0317         return n;
0318     }
0319 
0320     inline KgpgKeyList &operator<<(KgpgKey key)
0321     {
0322         append(key);
0323         return *this;
0324     }
0325 
0326     inline KgpgKeyList &operator<<(const KgpgKeyList &l)
0327     {
0328         *this += l;
0329         return *this;
0330     }
0331 
0332     operator QStringList() const;
0333 };
0334 
0335 //END Key
0336 
0337 } // namespace
0338 
0339 #endif // KGPGKEY_H