Blame lang/cpp/src/key.h

Packit Service 672cf4
/*
Packit Service 672cf4
  key.h - wraps a gpgme key
Packit Service 672cf4
  Copyright (C) 2003, 2005 Klarälvdalens Datakonsult AB
Packit Service 672cf4
Packit Service 672cf4
  This file is part of GPGME++.
Packit Service 672cf4
Packit Service 672cf4
  GPGME++ is free software; you can redistribute it and/or
Packit Service 672cf4
  modify it under the terms of the GNU Library General Public
Packit Service 672cf4
  License as published by the Free Software Foundation; either
Packit Service 672cf4
  version 2 of the License, or (at your option) any later version.
Packit Service 672cf4
Packit Service 672cf4
  GPGME++ is distributed in the hope that it will be useful,
Packit Service 672cf4
  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 672cf4
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 672cf4
  GNU Library General Public License for more details.
Packit Service 672cf4
Packit Service 672cf4
  You should have received a copy of the GNU Library General Public License
Packit Service 672cf4
  along with GPGME++; see the file COPYING.LIB.  If not, write to the
Packit Service 672cf4
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Packit Service 672cf4
  Boston, MA 02110-1301, USA.
Packit Service 672cf4
*/
Packit Service 672cf4
Packit Service 672cf4
// -*- c++ -*-
Packit Service 672cf4
#ifndef __GPGMEPP_KEY_H__
Packit Service 672cf4
#define __GPGMEPP_KEY_H__
Packit Service 672cf4
Packit Service 672cf4
#include "global.h"
Packit Service 672cf4
#include "notation.h"
Packit Service 672cf4
Packit Service 672cf4
#include "gpgmefw.h"
Packit Service 672cf4
Packit Service 672cf4
#include <memory>
Packit Service 672cf4
#include <sys/time.h>
Packit Service 672cf4
Packit Service 672cf4
#include <vector>
Packit Service 672cf4
#include <algorithm>
Packit Service 672cf4
#include <string>
Packit Service 672cf4
Packit Service 672cf4
namespace GpgME
Packit Service 672cf4
{
Packit Service 672cf4
Packit Service 672cf4
class Context;
Packit Service 672cf4
Packit Service 672cf4
class Subkey;
Packit Service 672cf4
class UserID;
Packit Service 672cf4
class TofuInfo;
Packit Service 672cf4
Packit Service 672cf4
typedef std::shared_ptr< std::remove_pointer<gpgme_key_t>::type > shared_gpgme_key_t;
Packit Service 672cf4
Packit Service 672cf4
//
Packit Service 672cf4
// class Key
Packit Service 672cf4
//
Packit Service 672cf4
Packit Service 672cf4
class GPGMEPP_EXPORT Key
Packit Service 672cf4
{
Packit Service 672cf4
    friend class ::GpgME::Context;
Packit Service 672cf4
    struct Null {
Packit Service 672cf4
		Null() {}
Packit Service 672cf4
	};
Packit Service 672cf4
public:
Packit Service 672cf4
    Key();
Packit Service 672cf4
    /* implicit */ Key(const Null &);
Packit Service 672cf4
    Key(const shared_gpgme_key_t &key);
Packit Service 672cf4
    Key(gpgme_key_t key, bool acquireRef);
Packit Service 672cf4
Packit Service 672cf4
    static const Null null;
Packit Service 672cf4
Packit Service 672cf4
    const Key &operator=(Key other)
Packit Service 672cf4
    {
Packit Service 672cf4
        swap(other);
Packit Service 672cf4
        return *this;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
    const Key &mergeWith(const Key &other);
Packit Service 672cf4
Packit Service 672cf4
    void swap(Key &other)
Packit Service 672cf4
    {
Packit Service 672cf4
        using std::swap;
Packit Service 672cf4
        swap(this->key, other.key);
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
    bool isNull() const
Packit Service 672cf4
    {
Packit Service 672cf4
        return !key;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
    UserID userID(unsigned int index) const;
Packit Service 672cf4
    Subkey subkey(unsigned int index) const;
Packit Service 672cf4
Packit Service 672cf4
    unsigned int numUserIDs() const;
Packit Service 672cf4
    unsigned int numSubkeys() const;
Packit Service 672cf4
Packit Service 672cf4
    std::vector<UserID> userIDs() const;
Packit Service 672cf4
    std::vector<Subkey> subkeys() const;
Packit Service 672cf4
Packit Service 672cf4
    bool isRevoked() const;
Packit Service 672cf4
    bool isExpired() const;
Packit Service 672cf4
    bool isDisabled() const;
Packit Service 672cf4
    bool isInvalid() const;
Packit Service 672cf4
Packit Service 672cf4
    bool canEncrypt() const;
Packit Service 672cf4
    /*!
Packit Service 672cf4
      This function contains a workaround for old gpgme's: all secret
Packit Service 672cf4
      OpenPGP keys canSign() == true, which canReallySign() doesn't
Packit Service 672cf4
      have. I don't have time to find what breaks when I remove this
Packit Service 672cf4
      workaround, but since Kleopatra merges secret into public keys,
Packit Service 672cf4
      the workaround is not necessary there (and actively harms), I've
Packit Service 672cf4
      added a new function instead.
Packit Service 672cf4
     */
Packit Service 672cf4
    bool canSign() const;
Packit Service 672cf4
    bool canReallySign() const;
Packit Service 672cf4
    bool canCertify() const;
Packit Service 672cf4
    bool canAuthenticate() const;
Packit Service 672cf4
    bool isQualified() const;
Packit Service 672cf4
    bool isDeVs() const;
Packit Service 672cf4
Packit Service 672cf4
    bool hasSecret() const;
Packit Service 672cf4
    GPGMEPP_DEPRECATED bool isSecret() const
Packit Service 672cf4
    {
Packit Service 672cf4
        return hasSecret();
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
    /*!
Packit Service 672cf4
      @return true if this is a X.509 root certificate (currently
Packit Service 672cf4
      equivalent to something like
Packit Service 672cf4
      strcmp( chainID(), subkey(0).fingerprint() ) == 0 )
Packit Service 672cf4
    */
Packit Service 672cf4
    bool isRoot() const;
Packit Service 672cf4
Packit Service 672cf4
    enum OwnerTrust { Unknown = 0, Undefined = 1, Never = 2,
Packit Service 672cf4
                      Marginal = 3, Full = 4, Ultimate = 5
Packit Service 672cf4
                    };
Packit Service 672cf4
Packit Service 672cf4
    OwnerTrust ownerTrust() const;
Packit Service 672cf4
    char ownerTrustAsString() const;
Packit Service 672cf4
Packit Service 672cf4
    Protocol protocol() const;
Packit Service 672cf4
    const char *protocolAsString() const;
Packit Service 672cf4
Packit Service 672cf4
    const char *issuerSerial() const;
Packit Service 672cf4
    const char *issuerName() const;
Packit Service 672cf4
    const char *chainID() const;
Packit Service 672cf4
Packit Service 672cf4
    const char *keyID() const;
Packit Service 672cf4
    const char *shortKeyID() const;
Packit Service 672cf4
    const char *primaryFingerprint() const;
Packit Service 672cf4
Packit Service 672cf4
    unsigned int keyListMode() const;
Packit Service 672cf4
Packit Service 672cf4
    /*! Update information about this key.
Packit Service 672cf4
     * Starts a keylisting for this key with validity
Packit Service 672cf4
     * and tofu information gathering. Blocks for
Packit Service 672cf4
     * how long the keylisting takes.*/
Packit Service 672cf4
    void update();
Packit Service 672cf4
Packit Service 672cf4
    /**
Packit Service 672cf4
     * @brief Add a user id to this key.
Packit Service 672cf4
     *
Packit Service 672cf4
     * Needs gnupg 2.1.13 and the key needs to be updated
Packit Service 672cf4
     * afterwards to see the new uid.
Packit Service 672cf4
     *
Packit Service 6c01f9
     * @param uid should be fully formated and UTF-8 encoded.
Packit Service 672cf4
     *
Packit Service 672cf4
     * @returns a possible error.
Packit Service 672cf4
     **/
Packit Service 672cf4
    Error addUid(const char *uid);
Packit Service 672cf4
private:
Packit Service 672cf4
    gpgme_key_t impl() const
Packit Service 672cf4
    {
Packit Service 672cf4
        return key.get();
Packit Service 672cf4
    }
Packit Service 672cf4
    shared_gpgme_key_t key;
Packit Service 672cf4
};
Packit Service 672cf4
Packit Service 672cf4
//
Packit Service 672cf4
// class Subkey
Packit Service 672cf4
//
Packit Service 672cf4
Packit Service 672cf4
class GPGMEPP_EXPORT Subkey
Packit Service 672cf4
{
Packit Service 672cf4
public:
Packit Service 672cf4
    Subkey();
Packit Service 672cf4
    Subkey(const shared_gpgme_key_t &key, gpgme_sub_key_t subkey);
Packit Service 672cf4
    Subkey(const shared_gpgme_key_t &key, unsigned int idx);
Packit Service 672cf4
Packit Service 672cf4
    const Subkey &operator=(Subkey other)
Packit Service 672cf4
    {
Packit Service 672cf4
        swap(other);
Packit Service 672cf4
        return *this;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
    void swap(Subkey &other)
Packit Service 672cf4
    {
Packit Service 672cf4
        using std::swap;
Packit Service 672cf4
        swap(this->key, other.key);
Packit Service 672cf4
        swap(this->subkey, other.subkey);
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
    bool isNull() const
Packit Service 672cf4
    {
Packit Service 672cf4
        return !key || !subkey;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
    Key parent() const;
Packit Service 672cf4
Packit Service 672cf4
    const char *keyID() const;
Packit Service 672cf4
    const char *fingerprint() const;
Packit Service 672cf4
Packit Service 672cf4
    time_t creationTime() const;
Packit Service 672cf4
    time_t expirationTime() const;
Packit Service 672cf4
    bool neverExpires() const;
Packit Service 672cf4
Packit Service 672cf4
    bool isRevoked() const;
Packit Service 672cf4
    bool isExpired() const;
Packit Service 672cf4
    bool isInvalid() const;
Packit Service 672cf4
    bool isDisabled() const;
Packit Service 672cf4
Packit Service 672cf4
    bool canEncrypt() const;
Packit Service 672cf4
    bool canSign() const;
Packit Service 672cf4
    bool canCertify() const;
Packit Service 672cf4
    bool canAuthenticate() const;
Packit Service 672cf4
    bool isQualified() const;
Packit Service 672cf4
    bool isDeVs() const;
Packit Service 672cf4
    bool isCardKey() const;
Packit Service 672cf4
Packit Service 672cf4
    bool isSecret() const;
Packit Service 672cf4
Packit Service 672cf4
    /** Same as gpgme_pubkey_algo_t */
Packit Service 672cf4
    enum PubkeyAlgo {
Packit Service 672cf4
        AlgoUnknown = 0,
Packit Service 672cf4
        AlgoRSA     = 1,
Packit Service 672cf4
        AlgoRSA_E   = 2,
Packit Service 672cf4
        AlgoRSA_S   = 3,
Packit Service 672cf4
        AlgoELG_E   = 16,
Packit Service 672cf4
        AlgoDSA     = 17,
Packit Service 672cf4
        AlgoECC     = 18,
Packit Service 672cf4
        AlgoELG     = 20,
Packit Service 672cf4
        AlgoECDSA   = 301,
Packit Service 672cf4
        AlgoECDH    = 302,
Packit Service 672cf4
        AlgoEDDSA   = 303,
Packit Service 672cf4
        AlgoMax     = 1 << 31
Packit Service 672cf4
    };
Packit Service 672cf4
Packit Service 672cf4
    PubkeyAlgo publicKeyAlgorithm() const;
Packit Service 672cf4
Packit Service 672cf4
    /**
Packit Service 672cf4
      @brief Get the public key algorithm name.
Packit Service 672cf4
Packit Service 672cf4
      This only works for the pre 2.1 algorithms for ECC NULL is returned.
Packit Service 672cf4
Packit Service 672cf4
      @returns a statically allocated string with the name of the public
Packit Service 672cf4
               key algorithm, or NULL if that name is not known.
Packit Service 672cf4
    */
Packit Service 672cf4
    const char *publicKeyAlgorithmAsString() const;
Packit Service 672cf4
Packit Service 672cf4
    /** @brief Same as publicKeyAlgorithmAsString but static. */
Packit Service 672cf4
    static const char *publicKeyAlgorithmAsString(PubkeyAlgo algo);
Packit Service 672cf4
Packit Service 672cf4
    /**
Packit Service 672cf4
       @brief Get the key algo string like GnuPG 2.1 prints it.
Packit Service 672cf4
Packit Service 672cf4
       This returns combinations of size and algorithm. Like
Packit Service 672cf4
       bp512 or rsa2048. Misnamed because publicKeyAlgorithmAsString
Packit Service 672cf4
       already used the older pubkey_algo_name.
Packit Service 672cf4
       Actually uses gpgme_pubkey_algo_string.
Packit Service 672cf4
Packit Service 672cf4
       @returns the key algorithm as string. Empty string on error.
Packit Service 672cf4
    */
Packit Service 672cf4
    std::string algoName() const;
Packit Service 672cf4
Packit Service 672cf4
    unsigned int length() const;
Packit Service 672cf4
Packit Service 672cf4
    const char *cardSerialNumber() const;
Packit Service 672cf4
Packit Service 672cf4
    const char *keyGrip() const;
Packit Service 672cf4
Packit Service 672cf4
private:
Packit Service 672cf4
    shared_gpgme_key_t key;
Packit Service 672cf4
    gpgme_sub_key_t subkey;
Packit Service 672cf4
};
Packit Service 672cf4
Packit Service 672cf4
//
Packit Service 672cf4
// class UserID
Packit Service 672cf4
//
Packit Service 672cf4
Packit Service 672cf4
class GPGMEPP_EXPORT UserID
Packit Service 672cf4
{
Packit Service 672cf4
public:
Packit Service 672cf4
    class Signature;
Packit Service 672cf4
Packit Service 672cf4
    UserID();
Packit Service 672cf4
    UserID(const shared_gpgme_key_t &key, gpgme_user_id_t uid);
Packit Service 672cf4
    UserID(const shared_gpgme_key_t &key, unsigned int idx);
Packit Service 672cf4
Packit Service 672cf4
    const UserID &operator=(UserID other)
Packit Service 672cf4
    {
Packit Service 672cf4
        swap(other);
Packit Service 672cf4
        return *this;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
    void swap(UserID &other)
Packit Service 672cf4
    {
Packit Service 672cf4
        using std::swap;
Packit Service 672cf4
        swap(this->key, other.key);
Packit Service 672cf4
        swap(this->uid, other.uid);
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
    bool isNull() const
Packit Service 672cf4
    {
Packit Service 672cf4
        return !key || !uid;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
    Key parent() const;
Packit Service 672cf4
Packit Service 672cf4
    unsigned int numSignatures() const;
Packit Service 672cf4
    Signature signature(unsigned int index) const;
Packit Service 672cf4
    std::vector<Signature> signatures() const;
Packit Service 672cf4
Packit Service 672cf4
    const char *id() const;
Packit Service 672cf4
    const char *name() const;
Packit Service 672cf4
    const char *email() const;
Packit Service 672cf4
    const char *comment() const;
Packit Service 672cf4
Packit Service 672cf4
    enum Validity { Unknown = 0, Undefined = 1, Never = 2,
Packit Service 672cf4
                    Marginal = 3, Full = 4, Ultimate = 5
Packit Service 672cf4
                  };
Packit Service 672cf4
Packit Service 672cf4
    Validity validity() const;
Packit Service 672cf4
    char validityAsString() const;
Packit Service 672cf4
Packit Service 672cf4
    bool isRevoked() const;
Packit Service 672cf4
    bool isInvalid() const;
Packit Service 672cf4
Packit Service 672cf4
    /** TOFU info for this userid.
Packit Service 672cf4
     * @returns The TOFU stats or a null TofuInfo.
Packit Service 672cf4
     */
Packit Service 672cf4
    GpgME::TofuInfo tofuInfo() const;
Packit Service 672cf4
Packit Service 672cf4
    /*! Wrapper around gpgme_addrspec_from_uid.
Packit Service 672cf4
     *
Packit Service 672cf4
     * The input string should match the format of
Packit Service 672cf4
     * a user id string.
Packit Service 672cf4
     *
Packit Service 672cf4
     * @returns a normalized mail address if found
Packit Service 672cf4
     * or an empty string. */
Packit Service 672cf4
    static std::string addrSpecFromString(const char *uid);
Packit Service 672cf4
Packit Service 672cf4
    /*! Wrapper around gpgme_addrspec_from_uid.
Packit Service 672cf4
     *
Packit Service 672cf4
     * @returns a normalized mail address for this userid
Packit Service 672cf4
     * or an empty string. */
Packit Service 672cf4
    std::string addrSpec() const;
Packit Service 672cf4
Packit Service 672cf4
    /*! Revoke the user id.
Packit Service 672cf4
     *
Packit Service 672cf4
     * Key needs update afterwards.
Packit Service 672cf4
     *
Packit Service 672cf4
     * @returns an error on error.*/
Packit Service 672cf4
    Error revoke();
Packit Service 672cf4
private:
Packit Service 672cf4
    shared_gpgme_key_t key;
Packit Service 672cf4
    gpgme_user_id_t uid;
Packit Service 672cf4
};
Packit Service 672cf4
Packit Service 672cf4
//
Packit Service 672cf4
// class UserID::Signature
Packit Service 672cf4
//
Packit Service 672cf4
Packit Service 672cf4
class GPGMEPP_EXPORT UserID::Signature
Packit Service 672cf4
{
Packit Service 672cf4
public:
Packit Service 672cf4
    typedef GPGMEPP_DEPRECATED GpgME::Notation Notation;
Packit Service 672cf4
Packit Service 672cf4
    Signature();
Packit Service 672cf4
    Signature(const shared_gpgme_key_t &key, gpgme_user_id_t uid, gpgme_key_sig_t sig);
Packit Service 672cf4
    Signature(const shared_gpgme_key_t &key, gpgme_user_id_t uid, unsigned int idx);
Packit Service 672cf4
Packit Service 672cf4
    const Signature &operator=(Signature other)
Packit Service 672cf4
    {
Packit Service 672cf4
        swap(other);
Packit Service 672cf4
        return *this;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
    void swap(Signature &other)
Packit Service 672cf4
    {
Packit Service 672cf4
        using std::swap;
Packit Service 672cf4
        swap(this->key, other.key);
Packit Service 672cf4
        swap(this->uid, other.uid);
Packit Service 672cf4
        swap(this->sig, other.sig);
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
    bool isNull() const
Packit Service 672cf4
    {
Packit Service 672cf4
        return !sig || !uid || !key ;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
    UserID parent() const;
Packit Service 672cf4
Packit Service 672cf4
    const char *signerKeyID() const;
Packit Service 672cf4
Packit Service 672cf4
    const char *algorithmAsString() const;
Packit Service 672cf4
    unsigned int algorithm() const;
Packit Service 672cf4
    time_t creationTime() const;
Packit Service 672cf4
    time_t expirationTime() const;
Packit Service 672cf4
    bool neverExpires() const;
Packit Service 672cf4
Packit Service 672cf4
    bool isRevokation() const;
Packit Service 672cf4
    bool isInvalid() const;
Packit Service 672cf4
    bool isExpired() const;
Packit Service 672cf4
    bool isExportable() const;
Packit Service 672cf4
Packit Service 672cf4
    const char *signerUserID() const;
Packit Service 672cf4
    const char *signerName() const;
Packit Service 672cf4
    const char *signerEmail() const;
Packit Service 672cf4
    const char *signerComment() const;
Packit Service 672cf4
Packit Service 672cf4
    unsigned int certClass() const;
Packit Service 672cf4
Packit Service 672cf4
    enum Status { NoError = 0, SigExpired, KeyExpired,
Packit Service 672cf4
                  BadSignature, NoPublicKey, GeneralError
Packit Service 672cf4
                };
Packit Service 672cf4
    Status status() const;
Packit Service 672cf4
    std::string statusAsString() const;
Packit Service 672cf4
Packit Service 672cf4
    const char *policyURL() const;
Packit Service 672cf4
Packit Service 672cf4
    unsigned int numNotations() const;
Packit Service 672cf4
    GpgME::Notation notation(unsigned int idx) const;
Packit Service 672cf4
    std::vector<GpgME::Notation> notations() const;
Packit Service 672cf4
Packit Service 672cf4
private:
Packit Service 672cf4
    shared_gpgme_key_t key;
Packit Service 672cf4
    gpgme_user_id_t uid;
Packit Service 672cf4
    gpgme_key_sig_t sig;
Packit Service 672cf4
};
Packit Service 672cf4
Packit Service 672cf4
GPGMEPP_EXPORT std::ostream &operator<<(std::ostream &os, const UserID &uid);
Packit Service 672cf4
GPGMEPP_EXPORT std::ostream &operator<<(std::ostream &os, const Key &key);
Packit Service 672cf4
Packit Service 672cf4
} // namespace GpgME
Packit Service 672cf4
Packit Service 672cf4
GPGMEPP_MAKE_STD_SWAP_SPECIALIZATION(Key)
Packit Service 672cf4
GPGMEPP_MAKE_STD_SWAP_SPECIALIZATION(Subkey)
Packit Service 672cf4
GPGMEPP_MAKE_STD_SWAP_SPECIALIZATION(UserID)
Packit Service 672cf4
GPGMEPP_MAKE_STD_SWAP_SPECIALIZATION(UserID::Signature)
Packit Service 672cf4
Packit Service 672cf4
GPGMEPP_MAKE_STRCMP(ByFingerprint, .primaryFingerprint());
Packit Service 672cf4
GPGMEPP_MAKE_STRCMP(ByKeyID, .keyID());
Packit Service 672cf4
GPGMEPP_MAKE_STRCMP(ByShortKeyID, .shortKeyID());
Packit Service 672cf4
GPGMEPP_MAKE_STRCMP(ByChainID, .chainID());
Packit Service 672cf4
Packit Service 672cf4
#endif // __GPGMEPP_KEY_H__