Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support passphrase callback #3

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions go_gpgme.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
#include "go_gpgme.h"
#include <errno.h>
#include <string.h>

gpgme_error_t passphrase_cb (void *opaque, const char *uid_hint, const char *passphrase_info, int last_was_bad, int fd) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The callback should probably fail if last_was_bad instead of trying again.

(void)uid_hint;
(void)passphrase_info;
(void)last_was_bad;

char *pass = (char*) opaque;
gpgme_io_writen(fd, pass, strlen(pass));
gpgme_io_writen (fd, "\n", 1);
return gpgme_error_from_errno (errno);
}

gpgme_error_t gogpgme_data_new_from_cbs(gpgme_data_t *dh, gpgme_data_cbs_t cbs, uintptr_t handle) {
return gpgme_data_new_from_cbs(dh, cbs, (void *)handle);
Expand Down
2 changes: 2 additions & 0 deletions go_gpgme.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
typedef off_t gpgme_off_t; /* Introduced in 1.4.2 */
#endif

extern gpgme_error_t passphrase_cb (void *opaque, const char *uid_hint, const char *passphrase_info, int last_was_bad, int fd);

extern ssize_t gogpgme_readfunc(void *handle, void *buffer, size_t size);
extern ssize_t gogpgme_writefunc(void *handle, void *buffer, size_t size);
extern off_t gogpgme_seekfunc(void *handle, off_t offset, int whence);
Expand Down
14 changes: 14 additions & 0 deletions gpgme.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"runtime"
"time"
"unsafe"

"github.com/sirupsen/logrus"
)

var Version string
Expand Down Expand Up @@ -546,6 +548,18 @@ func (c *Context) Encrypt(recipients []*Key, flags EncryptFlag, plaintext, ciphe
return handleError(err)
}

func (c *Context) SetPassphrase(passphrase string) {
logrus.Debugf("Setting GPGME passphrase callback", passphrase)
callback := C.gpgme_passphrase_cb_t(C.passphrase_cb)
cPass := C.CString(passphrase)
C.gpgme_set_pinentry_mode(c.ctx, C.GPGME_PINENTRY_MODE_LOOPBACK);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won’t compile with the GPGME 1.3.2 version this repo is currently targeting.

We should almost certainly just give up on that version, and move on, sure. If I’m reading https://access.redhat.com/downloads/content/package-browser correctly, neither of the changes in this repo are necessary for RHEL ≥ 8.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I.e., to be a bit more explicit, if I’m not mistaken about the supported versions, we can just revert c/image to use the upstream proglottis/gpgme package, and deprecate/abandon this fork.

C.gpgme_set_passphrase_cb(c.ctx, callback, unsafe.Pointer(cPass))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are all unsafe WRT CGo rules, per proglottis#23 .

There already is a Context.SetCallback exposing gpgme_set_passphrase_cb (and see how different that implementation needs to be), isn’t that sufficient already?

To the extent actually using SetCallback with a fixed string requires some code that could be widely reused, it might well make sense to provide that in this gpgme package, but I think it would be cleaner to just wrap SetCallback instead of reimplementing it, and it’s up to the upstream maintainer whether to add it.

}

func (c *Context) ClearPassphrase() {
C.gpgme_set_passphrase_cb(c.ctx, nil, nil)
}

func (c *Context) Sign(signers []*Key, plain, sig *Data, mode SigMode) error {
C.gpgme_signers_clear(c.ctx)
runtime.KeepAlive(c)
Expand Down