From da3f5c35167eca74cc85e01780b41a18445fee1d Mon Sep 17 00:00:00 2001 From: SET Date: Mon, 16 Nov 2020 15:58:50 +0100 Subject: [PATCH] Add function to add a new user identity to a key. 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. --- GpgMEWorker.cpp | 29 +++++++++++++++++++++++++++++ GpgMEWorker.h | 31 +++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/GpgMEWorker.cpp b/GpgMEWorker.cpp index 038970d..bebc88f 100644 --- a/GpgMEWorker.cpp +++ b/GpgMEWorker.cpp @@ -172,6 +172,35 @@ const Error GpgMEWorker::SetExpiryTime(const char * keyFpr, return e; } +const Error GpgMEWorker::AddUserID(const char* keyFpr, const string& passphrase, + const string& name, const string& email, + const string& comment) +{ + Error e; + Key k = FindKey(keyFpr, e, true); + if (e.code() != 0) + return e; + e = m_ctx->addSigningKey(k); + if (e.code() != 0) + return e; + + m_ctx->setPinentryMode(Context::PinentryMode::PinentryLoopback); + if (m_ppp == NULL) + m_ppp = new LoopbackPassphraseProvider(); + m_ppp->SetPassphrase(passphrase); + m_ctx->setPassphraseProvider(m_ppp); + + AddUserIDEditInteractor * interactor = new AddUserIDEditInteractor(); + interactor->setNameUtf8(name); + interactor->setEmailUtf8(email); + interactor->setCommentUtf8(comment); + GpgME::Data d; + e = m_ctx->edit(k, std::unique_ptr (interactor), d); + m_ctx->clearSigningKeys(); + + return e; +} + /* * Using a temporary context for key creation. It is altered after secret key * creation, and subkey creation fails thereafter. This is observational. diff --git a/GpgMEWorker.h b/GpgMEWorker.h index 3778dc2..f99a5c1 100644 --- a/GpgMEWorker.h +++ b/GpgMEWorker.h @@ -16,6 +16,7 @@ #include #include #include +#include #include #include "LoopbackPassphraseProvider.h" @@ -84,6 +85,21 @@ public: const Error SetExpiryTime(const char * keyFpr, const string& passphrase, const string& timeString = "0"); + /** + * 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); /** * Creates a pair of secret and public keys with the default engine * algorithms. Default expiry time is 2 * 365 days. @@ -216,5 +232,20 @@ public: { }; }; + +class AddUserIDEditInteractor : public GpgAddUserIDEditInteractor +{ +public: + + AddUserIDEditInteractor() + : GpgAddUserIDEditInteractor() + { + } + + virtual ~AddUserIDEditInteractor() + { + }; +}; + #endif /* GPGMEWORKER_H */