From e5c7fc3e022841be8f68fad8b9b64dd601b89ba7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingo=20Kl=C3=B6cker?= Date: Tue, 4 Jan 2022 15:50:59 +0100 Subject: [PATCH] cpp: Allow export of secret subkeys * lang/cpp/src/context.h (enum Context::ExportMode): Add value ExportSecretSubkey. (class Context): Add member functions exportSecretSubkeys and startSecretSubkeyExport. * lang/cpp/src/context.cpp (Context::exportPublicKeys, Context::startPublicKeyExport): Return error if ExportSecretSubkey mode flag is set. (Context::exportSecretSubkeys, Context::startSecretSubkeyExport): Implement. -- GnuPG-bug-id: 5757 --- lang/cpp/src/context.cpp | 28 ++++++++++++++++++++++++---- lang/cpp/src/context.h | 6 ++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lang/cpp/src/context.cpp b/lang/cpp/src/context.cpp index 174a3c3b..e8e57dc8 100644 --- a/lang/cpp/src/context.cpp +++ b/lang/cpp/src/context.cpp @@ -612,7 +612,7 @@ Error Context::startKeyExport(const char *patterns[], Data &keyData, unsigned in Error Context::exportPublicKeys(const char *pattern, Data &keyData, unsigned int mode) { - if (mode & ExportSecret) { + if (mode & (ExportSecret | ExportSecretSubkey)) { return Error::fromCode(GPG_ERR_INV_FLAG); } return exportKeys(pattern, keyData, mode); @@ -620,7 +620,7 @@ Error Context::exportPublicKeys(const char *pattern, Data &keyData, unsigned int Error Context::exportPublicKeys(const char *patterns[], Data &keyData, unsigned int mode) { - if (mode & ExportSecret) { + if (mode & (ExportSecret | ExportSecretSubkey)) { return Error::fromCode(GPG_ERR_INV_FLAG); } return exportKeys(patterns, keyData, mode); @@ -628,7 +628,7 @@ Error Context::exportPublicKeys(const char *patterns[], Data &keyData, unsigned Error Context::startPublicKeyExport(const char *pattern, Data &keyData, unsigned int mode) { - if (mode & ExportSecret) { + if (mode & (ExportSecret | ExportSecretSubkey)) { return Error::fromCode(GPG_ERR_INV_FLAG); } return startKeyExport(pattern, keyData, mode); @@ -636,7 +636,7 @@ Error Context::startPublicKeyExport(const char *pattern, Data &keyData, unsigned Error Context::startPublicKeyExport(const char *patterns[], Data &keyData, unsigned int mode) { - if (mode & ExportSecret) { + if (mode & (ExportSecret | ExportSecretSubkey)) { return Error::fromCode(GPG_ERR_INV_FLAG); } return startKeyExport(patterns, keyData, mode); @@ -695,6 +695,26 @@ Error Context::startSecretKeyExport(const char *patterns[], Data &keyData, unsig return startKeyExport(patterns, keyData, mode|ExportSecret); } +Error Context::exportSecretSubkeys(const char *pattern, Data &keyData, unsigned int mode) +{ + return exportKeys(pattern, keyData, mode|ExportSecretSubkey); +} + +Error Context::exportSecretSubkeys(const char *patterns[], Data &keyData, unsigned int mode) +{ + return exportKeys(patterns, keyData, mode|ExportSecretSubkey); +} + +Error Context::startSecretSubkeyExport(const char *pattern, Data &keyData, unsigned int mode) +{ + return startKeyExport(pattern, keyData, mode|ExportSecretSubkey); +} + +Error Context::startSecretSubkeyExport(const char *patterns[], Data &keyData, unsigned int mode) +{ + return startKeyExport(patterns, keyData, mode|ExportSecretSubkey); +} + ImportResult Context::importKeys(const Data &data) { d->lastop = Private::Import; diff --git a/lang/cpp/src/context.h b/lang/cpp/src/context.h index 2631678a..bfa6b8d8 100644 --- a/lang/cpp/src/context.h +++ b/lang/cpp/src/context.h @@ -191,6 +191,7 @@ public: ExportPKCS12 = 64, ExportNoUID = 128, // obsolete; has no effect ExportSSH = 256, + ExportSecretSubkey = 512, }; GpgME::Error exportPublicKeys(const char *pattern, Data &keyData); @@ -207,6 +208,11 @@ public: GpgME::Error startSecretKeyExport(const char *pattern, Data &keyData, unsigned int mode = ExportSecret); GpgME::Error startSecretKeyExport(const char *pattern[], Data &keyData, unsigned int mode = ExportSecret); + GpgME::Error exportSecretSubkeys(const char *pattern, Data &keyData, unsigned int mode = ExportSecretSubkey); + GpgME::Error exportSecretSubkeys(const char *pattern[], Data &keyData, unsigned int mode = ExportSecretSubkey); + GpgME::Error startSecretSubkeyExport(const char *pattern, Data &keyData, unsigned int mode = ExportSecretSubkey); + GpgME::Error startSecretSubkeyExport(const char *pattern[], Data &keyData, unsigned int mode = ExportSecretSubkey); + // generic export functions; prefer using the specific public/secret key export functions GpgME::Error exportKeys(const char *pattern, Data &keyData, unsigned int mode = ExportDefault); GpgME::Error exportKeys(const char *pattern[], Data &keyData, unsigned int mode = ExportDefault);