2019-10-25 20:16:43 +02:00
|
|
|
/*
|
|
|
|
|
* File: GpgMEWorker.h
|
2019-10-28 14:12:50 +01:00
|
|
|
* Author: SET - nmset@yandex.com
|
2019-10-25 20:16:43 +02:00
|
|
|
* License : LGPL v2.1
|
|
|
|
|
* Copyright SET - © 2019
|
|
|
|
|
*
|
|
|
|
|
* Created on 11 octobre 2019, 16:34
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef GPGMEWORKER_H
|
|
|
|
|
#define GPGMEWORKER_H
|
|
|
|
|
|
|
|
|
|
#include <gpgme++/context.h>
|
|
|
|
|
#include <gpgme++/error.h>
|
|
|
|
|
#include <gpgme++/key.h>
|
2020-10-25 17:28:47 +01:00
|
|
|
#include <gpgme++/gpgsetownertrusteditinteractor.h>
|
2020-11-03 11:06:25 +01:00
|
|
|
#include <gpgme++/gpgsignkeyeditinteractor.h>
|
2020-11-07 22:17:44 +01:00
|
|
|
#include <gpgme++/gpgsetexpirytimeeditinteractor.h>
|
2020-11-16 15:58:50 +01:00
|
|
|
#include <gpgme++/gpgadduserideditinteractor.h>
|
2019-10-25 20:16:43 +02:00
|
|
|
#include <vector>
|
2020-11-03 11:06:25 +01:00
|
|
|
#include "LoopbackPassphraseProvider.h"
|
2019-10-25 20:16:43 +02:00
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace GpgME;
|
2020-10-25 17:28:47 +01:00
|
|
|
|
2019-10-25 20:16:43 +02:00
|
|
|
/**
|
|
|
|
|
* Main class for GPGME OP, except deleting keys.
|
|
|
|
|
*/
|
|
|
|
|
class GpgMEWorker
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
GpgMEWorker();
|
|
|
|
|
virtual ~GpgMEWorker();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find keys corresponding to pattern. GPGME searches in many fields :
|
|
|
|
|
* keyid, fingerprint, name, email...
|
|
|
|
|
* @param pattern
|
|
|
|
|
* @param hasSecret
|
|
|
|
|
* @param e
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
vector<GpgME::Key> FindKeys(const char * pattern, bool hasSecret, Error& e) const;
|
|
|
|
|
/**
|
|
|
|
|
* Search for a single key fully identified : keyid, short keyid, fingerprint.
|
|
|
|
|
* @param anyFullId
|
|
|
|
|
* @param e
|
|
|
|
|
* @param secret
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
GpgME::Key FindKey(const char * anyFullId, Error& e, bool secret = false) const;
|
|
|
|
|
/**
|
|
|
|
|
* Import a key from file.
|
|
|
|
|
* @param filePath
|
|
|
|
|
* @param e
|
2020-11-11 21:09:43 +01:00
|
|
|
* @return : the fingerprint
|
2019-10-25 20:16:43 +02:00
|
|
|
*/
|
|
|
|
|
const string ImportKey(const char * filePath, Error& e);
|
2020-10-25 17:28:47 +01:00
|
|
|
/**
|
|
|
|
|
* Inplace editing of owner trust if allowed
|
|
|
|
|
* @param anyFullId
|
|
|
|
|
* @param trustLevel : New trust level in key owner
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
const Error EditOwnerTrust(const char * anyFullId, GpgME::Key::OwnerTrust trustLevel);
|
2020-11-03 11:06:25 +01:00
|
|
|
/**
|
|
|
|
|
* Certify (sign) selected key.
|
|
|
|
|
* @param fprSigningKey
|
|
|
|
|
* @param fprKeyToSign
|
|
|
|
|
* @param userIDsToSign : index of each user identity in a vector.
|
|
|
|
|
* @param options : Exportable (1), Non revocable (2).
|
|
|
|
|
* \nTrust(4) is not implemented.
|
|
|
|
|
* @param passphrase
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
const Error CertifyKey(const char * fprSigningKey,
|
|
|
|
|
const char * fprKeyToSign,
|
|
|
|
|
vector<uint>& userIDsToSign, int options,
|
|
|
|
|
const string& passphrase);
|
2020-11-07 22:17:44 +01:00
|
|
|
/**
|
2020-11-22 14:22:08 +01:00
|
|
|
* Revoke UserID certifications.
|
|
|
|
|
* \n Requires GnuPG >= 2.2.24
|
|
|
|
|
* @param fprSigningKey
|
|
|
|
|
* @param fprKeyToSign
|
|
|
|
|
* @param userIDsToRevoke : vector of ::UserID
|
|
|
|
|
* @param passphrase
|
2020-11-07 22:17:44 +01:00
|
|
|
* @return
|
|
|
|
|
*/
|
2020-11-22 14:22:08 +01:00
|
|
|
const Error RevokeKeyCertifications(const char * fprSigningKey,
|
2020-11-24 22:15:46 +01:00
|
|
|
const char * fprKeyToSign,
|
|
|
|
|
vector<GpgME::UserID>& userIDsToRevoke,
|
|
|
|
|
const string& passphrase);
|
2020-11-22 09:21:38 +01:00
|
|
|
/**
|
2020-11-23 11:45:33 +01:00
|
|
|
* Sets the expiry time of a single (sub)key. Requires GPGME >= 1.15.0.
|
|
|
|
|
* \n If no subkey is found (wrong fpr) or not provided, the expiry time of
|
|
|
|
|
* key is set instead.
|
2020-11-22 09:21:38 +01:00
|
|
|
* @param keyFpr
|
|
|
|
|
* @param subkeyFpr
|
|
|
|
|
* @param passphrase
|
|
|
|
|
* @param expires : seconds from now. Use 0 for no expiry.
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2020-11-23 11:45:33 +01:00
|
|
|
const Error SetKeyExpiryTime(const char * keyFpr,
|
2020-11-24 22:15:46 +01:00
|
|
|
const char * subkeyFpr,
|
|
|
|
|
const string& passphrase,
|
|
|
|
|
ulong expires = 63072000);
|
2020-11-16 15:58:50 +01:00
|
|
|
/**
|
|
|
|
|
* Adds a user identity to a key.
|
|
|
|
|
* \n The email parameter must have a valid email address format here, else
|
|
|
|
|
* GPGME will refuse to create the identity. This is not the case when
|
|
|
|
|
* creating a key, where any string is accepted.
|
|
|
|
|
* @param keyFpr
|
|
|
|
|
* @param passphrase
|
|
|
|
|
* @param name
|
|
|
|
|
* @param email : string with a valid email format
|
|
|
|
|
* @param comment
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
const Error AddUserID(const char * keyFpr, const string& passphrase,
|
|
|
|
|
const string& name, const string& email,
|
|
|
|
|
const string& comment);
|
2020-11-18 20:48:15 +01:00
|
|
|
/**
|
|
|
|
|
* Revoke a user identity. Does not remove the identity.
|
|
|
|
|
* \n N.B. : if the uid is revoked a second time, Error::code is 0.
|
|
|
|
|
* @param keyFpr
|
|
|
|
|
* @param passphrase
|
|
|
|
|
* @param name
|
|
|
|
|
* @param email
|
|
|
|
|
* @param comment
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
const Error RevokeUserID(const char * keyFpr, const string& passphrase,
|
2020-11-21 20:53:01 +01:00
|
|
|
const string& name, const string& email,
|
|
|
|
|
const string& comment);
|
2020-11-11 14:47:05 +01:00
|
|
|
/**
|
|
|
|
|
* Creates a pair of secret and public keys with the default engine
|
|
|
|
|
* algorithms. Default expiry time is 2 * 365 days.
|
|
|
|
|
* @param k : must be a null key
|
|
|
|
|
* @param name
|
|
|
|
|
* @param email
|
|
|
|
|
* @param comment
|
|
|
|
|
* @param passphrase
|
|
|
|
|
* @param expires : seconds ahead of creation time. Use 0 for no expiry.
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
const Error CreateKeyWithEngineDefaultAlgo(GpgME::Key& k,
|
|
|
|
|
const string& name,
|
|
|
|
|
const string& email,
|
|
|
|
|
const string& comment,
|
|
|
|
|
const string& passphrase,
|
|
|
|
|
ulong expires = 63072000);
|
|
|
|
|
/**
|
|
|
|
|
* Creates a secret key with passed in algorithm name. Default expiry time
|
|
|
|
|
* is 2 * 365 days.
|
|
|
|
|
* @param k : must be a null key
|
|
|
|
|
* @param name
|
|
|
|
|
* @param email
|
|
|
|
|
* @param comment
|
|
|
|
|
* @param algo : a valid algorithm name for a secret key
|
|
|
|
|
* @param passphrase
|
|
|
|
|
* @param expires : seconds ahead of creation time. Use 0 for no expiry.
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
const Error CreateKey(GpgME::Key& k,
|
|
|
|
|
const string& name,
|
|
|
|
|
const string& email,
|
|
|
|
|
const string& comment,
|
|
|
|
|
const char * algo,
|
|
|
|
|
const string& passphrase,
|
|
|
|
|
ulong expires = 63072000);
|
|
|
|
|
/**
|
|
|
|
|
* Creates a public key with passed in algorithm name and adds it to secret
|
|
|
|
|
* key k. Default expiry time is 2 * 365 days.
|
|
|
|
|
* @param k : must be a secret key
|
|
|
|
|
* @param algo : : a valid algorithm name for a public key
|
|
|
|
|
* @param passphrase
|
|
|
|
|
* @param expires : seconds ahead of creation time. Use 0 for no expiry.
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
const Error CreateSubKey(GpgME::Key& k,
|
|
|
|
|
const char * algo,
|
|
|
|
|
const string& passphrase,
|
|
|
|
|
ulong expires = 63072000);
|
2020-11-15 11:47:30 +01:00
|
|
|
/**
|
2020-11-20 22:41:29 +01:00
|
|
|
* Export a secret key.
|
|
|
|
|
* @param pattern : a key fingerprint
|
|
|
|
|
* @param buffer : returns data in armor mode
|
2020-11-15 11:47:30 +01:00
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
const Error ExportPrivateKey(const char * pattern, string& buffer,
|
2020-11-15 11:58:07 +01:00
|
|
|
const string& passphrase = "");
|
|
|
|
|
/**
|
|
|
|
|
* Export a public key.
|
|
|
|
|
* @param pattern : a key fingerprint
|
|
|
|
|
* @param buffer : returns data in armor mode
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
const Error ExportPublicKey(const char * pattern, string& buffer);
|
2019-10-25 20:16:43 +02:00
|
|
|
private:
|
|
|
|
|
Context * m_ctx;
|
2020-11-03 11:06:25 +01:00
|
|
|
// GPG will fetch a password here.
|
|
|
|
|
LoopbackPassphraseProvider * m_ppp;
|
2020-11-16 14:39:57 +01:00
|
|
|
|
|
|
|
|
string MakeUidString(const string& name, const string& email,
|
|
|
|
|
const string& comment);
|
2019-10-25 20:16:43 +02:00
|
|
|
};
|
|
|
|
|
|
2020-10-25 17:28:47 +01:00
|
|
|
/**
|
|
|
|
|
* Edit trust in key owner
|
|
|
|
|
*/
|
|
|
|
|
class SetOwnerTrustEditInteractor : public GpgSetOwnerTrustEditInteractor
|
|
|
|
|
{
|
|
|
|
|
public:
|
2020-11-03 11:06:25 +01:00
|
|
|
|
2020-10-25 17:28:47 +01:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param ownerTrust : New trust level
|
|
|
|
|
*/
|
|
|
|
|
SetOwnerTrustEditInteractor(GpgME::Key::OwnerTrust ownerTrust)
|
2020-11-03 11:06:25 +01:00
|
|
|
: GpgSetOwnerTrustEditInteractor(ownerTrust)
|
|
|
|
|
{
|
|
|
|
|
}
|
2020-10-25 17:28:47 +01:00
|
|
|
|
2020-11-03 11:06:25 +01:00
|
|
|
virtual ~SetOwnerTrustEditInteractor()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Passed to GPG engine to certify (sign) a key.
|
|
|
|
|
*/
|
|
|
|
|
class SetSignKeyEditInteractor : public GpgSignKeyEditInteractor
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
SetSignKeyEditInteractor() : GpgSignKeyEditInteractor()
|
|
|
|
|
{
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
virtual ~SetSignKeyEditInteractor()
|
|
|
|
|
{
|
|
|
|
|
};
|
2020-10-25 17:28:47 +01:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-16 15:58:50 +01:00
|
|
|
class AddUserIDEditInteractor : public GpgAddUserIDEditInteractor
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
AddUserIDEditInteractor()
|
|
|
|
|
: GpgAddUserIDEditInteractor()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual ~AddUserIDEditInteractor()
|
|
|
|
|
{
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-25 20:16:43 +02:00
|
|
|
#endif /* GPGMEWORKER_H */
|
|
|
|
|
|