Exemplo n.º 1
0
gpgme_error_t
gpgmegtk_passphrase_cb(void *opaque, const char *uid_hint,
        const char *passphrase_hint, int prev_bad, int fd)
{
    const char *pass;
#ifdef G_OS_WIN32
    HANDLE hd = (HANDLE)fd;
    DWORD n;
#endif

    if (prefs_common.store_passphrase && last_pass != NULL && !prev_bad) {
#ifdef G_OS_WIN32
        WriteFile(hd, last_pass, strlen(last_pass), &n, NULL);
        WriteFile(hd, "\n", 1, &n, NULL);
#else
        write(fd, last_pass, strlen(last_pass));
        write(fd, "\n", 1);
#endif
        return GPG_ERR_NO_ERROR;
    }
    gpgmegtk_set_passphrase_grab (prefs_common.passphrase_grab);
    debug_print ("%% requesting passphrase for `%s':\n", uid_hint);
    pass = passphrase_mbox (uid_hint, passphrase_hint, prev_bad);
    gpgmegtk_free_passphrase();
    if (!pass) {
        debug_print ("%% cancel passphrase entry\n");
#ifdef G_OS_WIN32
        WriteFile(hd, "\n", 1, &n, NULL);
        CloseHandle(hd); /* somehow it will block without this */
#else
        write(fd, "\n", 1);
#endif
        return GPG_ERR_CANCELED;
    }
    else {
        if (prefs_common.store_passphrase) {
            last_pass = g_strdup(pass);
#if HAVE_MLOCK
            if (mlock(last_pass, strlen(last_pass)) == -1)
                debug_print("%% locking passphrase failed\n");
#endif

            if (prefs_common.store_passphrase_timeout > 0) {
                g_timeout_add_full(G_PRIORITY_LOW, prefs_common.store_passphrase_timeout * 60 * 1000, free_passphrase, NULL, NULL);
            }
        }
        debug_print ("%% sending passphrase\n");
    }
#ifdef G_OS_WIN32
    WriteFile(hd, pass, strlen(pass), &n, NULL);
    WriteFile(hd, "\n", 1, &n, NULL);
#else
    write(fd, pass, strlen(pass));
    write(fd, "\n", 1);
#endif
    return GPG_ERR_NO_ERROR;
}
Exemplo n.º 2
0
const char*
gpgmegtk_passphrase_cb (void *opaque, const char *desc, void **r_hd)
{
    struct passphrase_cb_info_s *info = opaque;
    GpgmeCtx ctx = info ? info->c : NULL;
    const char *pass;

    if (!desc) {
        /* FIXME: cleanup by looking at *r_hd */
        return NULL;
    }
    if (prefs_gpg_get_config()->store_passphrase && last_pass != NULL &&
        strncmp(desc, "TRY_AGAIN", 9) != 0)
        return g_strdup(last_pass);

    gpgmegtk_set_passphrase_grab (prefs_gpg_get_config()->passphrase_grab);
    debug_print ("%% requesting passphrase for `%s': ", desc);
    pass = passphrase_mbox (desc);
    gpgmegtk_free_passphrase();
    if (!pass) {
        debug_print ("%% cancel passphrase entry");
        gpgme_cancel (ctx);
    }
    else {
        if (prefs_gpg_get_config()->store_passphrase) {
            last_pass = g_strdup(pass);
            if (mlock(last_pass, strlen(last_pass)) == -1)
                debug_print("%% locking passphrase failed");

            if (prefs_gpg_get_config()->store_passphrase_timeout > 0) {
                gtk_timeout_add(prefs_gpg_get_config()->store_passphrase_timeout*60*1000,
                                free_passphrase, NULL);
            }
        }
        debug_print ("%% sending passphrase");
    }

    return pass;
}
Exemplo n.º 3
0
void sgpgme_done()
{
        gpgmegtk_free_passphrase();
}
Exemplo n.º 4
0
gpgme_data_t sgpgme_decrypt_verify(gpgme_data_t cipher, gpgme_verify_result_t *status, gpgme_ctx_t ctx)
{
	struct passphrase_cb_info_s info;
	gpgme_data_t plain;
	gpgme_error_t err;

	memset (&info, 0, sizeof info);
	
	if ((err = gpgme_data_new(&plain)) != GPG_ERR_NO_ERROR) {
		gpgme_release(ctx);
		privacy_set_error(_("Couldn't initialize data, %s"), gpgme_strerror(err));
		return NULL;
	}
	
	if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
		prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
    		if (!getenv("GPG_AGENT_INFO") || !prefs_gpg_get_config()->use_gpg_agent) {
        		info.c = ctx;
        		gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
    		}
	} else {
		prefs_gpg_enable_agent(TRUE);
        	info.c = ctx;
        	gpgme_set_passphrase_cb (ctx, NULL, &info);
	}
	
	
	if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
		err = gpgme_op_decrypt_verify(ctx, cipher, plain);
		if (err != GPG_ERR_NO_ERROR) {
			debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
			privacy_set_error("%s", gpgme_strerror(err));
			gpgmegtk_free_passphrase();
			gpgme_data_release(plain);
			return NULL;
		}

		err = cm_gpgme_data_rewind(plain);
		if (err) {
			debug_print("can't seek (%d %d %s)\n", err, errno, strerror(errno));
		}

		debug_print("decrypted.\n");
		*status = gpgme_op_verify_result (ctx);
	} else {
		err = gpgme_op_decrypt(ctx, cipher, plain);
		if (err != GPG_ERR_NO_ERROR) {
			debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
			privacy_set_error("%s", gpgme_strerror(err));
			gpgmegtk_free_passphrase();
			gpgme_data_release(plain);
			return NULL;
		}

		err = cm_gpgme_data_rewind(plain);
		if (err) {
			debug_print("can't seek (%d %d %s)\n", err, errno, strerror(errno));
		}

		debug_print("decrypted.\n");
		*status = gpgme_op_verify_result (ctx);
	}
	return plain;
}