From e944da9eda71837ad95b0d62d03b93e77afb9a44 Mon Sep 17 00:00:00 2001 From: SET Date: Sun, 15 Nov 2020 11:47:30 +0100 Subject: [PATCH] Test export private keys in C++. Result : fails. Reason : loopback passphrase provider is never called. With default pinentry mode, the passphrase is requested normally and the private key is exported. But this can't be done on a web server. Enclosed in #ifdef DEVTIME. --- GpgMEWorker.cpp | 25 +++++++++++++++++++++++++ GpgMEWorker.h | 15 +++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/GpgMEWorker.cpp b/GpgMEWorker.cpp index 1b53543..7a2d03f 100644 --- a/GpgMEWorker.cpp +++ b/GpgMEWorker.cpp @@ -255,3 +255,28 @@ const Error GpgMEWorker::CreateSubKey(GpgME::Key& k, delete ctx; return e; } + +#ifdef DEVTIME + +const Error GpgMEWorker::ExportPrivateKey(const char * pattern, string& buffer, + const string& passphrase) +{ + GpgME::Data kData; + Context * ctx = Context::createForProtocol(Protocol::OpenPGP); + LoopbackPassphraseProvider * ppp = new LoopbackPassphraseProvider(); + ppp->SetPassphrase(passphrase); + ctx->setPinentryMode(Context::PinentryMode::PinentryLoopback); + ctx->setPassphraseProvider(ppp); + + ctx->setArmor(true); + uint flags = Context::ExportSecret; + + Error e = ctx->exportPublicKeys(pattern, kData, flags); + buffer = kData.toString(); // Empty + + delete ppp; + delete ctx; + + return e; +} +#endif diff --git a/GpgMEWorker.h b/GpgMEWorker.h index 4fa594a..8bdee87 100644 --- a/GpgMEWorker.h +++ b/GpgMEWorker.h @@ -134,6 +134,21 @@ public: const string& passphrase, ulong expires = 63072000); +#ifdef DEVTIME + /** + * Status : testing + * \n Result : fails to export a private key + * \n Reason : loopback passphrase provider is never called + * \n With default pinentry mode, the password is requested normally + * and the private key is exported. But this can't be done on a web server. + * @param fpr + * @param e + * @return + */ + const Error ExportPrivateKey(const char * pattern, string& buffer, + const string& passphrase = ""); +#endif + private: Context * m_ctx; // GPG will fetch a password here.