Closed DBLouis closed 3 years ago
I think think this fails because recent(ish) versions of gpg don't seem to allow export of a password protected secret key without user interaction (see T2324: gpgme uses --batch
internally). I'm not sure why it doesn't return an error (might be worth reporting upstream). If the key doesn't need to be password protected you can use CreateKeyFlags::NOPASSWD to create it without one (for example see tests/export.rs).
I need the keys to be password protected. I don't get why is it not working because I do have the password callback set and the engine configured to loopback. I know I successfully made this work before in a shell script so it shouldn't be any different.
This works without user interaction:
gpg2 --passphrase "${user_pass}" --pinentry-mode loopback \
--batch --export-secret-keys --armor "${fingerprint}" \
>"${fingerprint}.asc"
The command that gpgme actually executes can be seen by setting the GPGME_DEBUG
environment variable. With GPGME_DEBUG=9
, the above Context::export
call produces something like this. The command can be seen on lines 26-49. It appears that a passphrase is requested but is unable to be retrieved. Comparing this to the command used by an earlier successful call to Context::create_key
, the major difference seems to be in the --command-fd 7
argument that is passed. After the passphrase is requested, the passphrase callback writes the passphrase to other end (fd 8) of the pipe.
Looking at the source for gpgme_op_createkey
, it appears that gpgme_op_createkey
(really createkey_start
) sets a command handler if a passphrase callback is set on the context. I would suggest opening a bug report (or submitting a patch) upstream to request gpgme_op_export
be modified to do this if secret key export is requested.
It works with the patch I posted there: https://dev.gnupg.org/T5046.
It looks like the patch has been merged upstream and is included in the 1.15 release.
The following code fails to export secret keys. The result is an empty file. Public keys however seems to be exported correctly. I ran it using the master branch.