2003-04-25 Marcus Brinkmann <marcus@g10code.de>

* ops.h (_gpgme_passphrase_start): Remove prototype.
	* passphrase.c: Do not include <assert.h>, "util.h" or
	"debug.h", but "gpgme.h".
	(struct passphrase_result): Change to typedef for op_data_t.
	(release_passphrase_result): Rename to release_op_data.
	(_gpgme_passphrase_status_handler): Change first argument to void *.
	Use new op_data_t type.
	(_gpgme_passphrase_command_handler): Use new op_data_t type.
	(_gpgme_passphrase_start): Remove function.
	* decrypt.c (_gpgme_decrypt_start): Rewrite error handling.  Do
	not call _gpgme_passphrase_start, but install command handler.
	* encrypt.c (_gpgme_op_encrypt_start): Likewise.
	* encrypt-sign.c (_gpgme_op_encrypt_sign_start): Likewise.
	* sign.c (_gpgme_op_sign_start): Likewise.
This commit is contained in:
Marcus Brinkmann 2003-04-25 14:24:17 +00:00
parent 9dbd9c6dff
commit 8002f98d48
7 changed files with 148 additions and 188 deletions

View File

@ -1,5 +1,20 @@
2003-04-25 Marcus Brinkmann <marcus@g10code.de> 2003-04-25 Marcus Brinkmann <marcus@g10code.de>
* ops.h (_gpgme_passphrase_start): Remove prototype.
* passphrase.c: Do not include <assert.h>, "util.h" or
"debug.h", but "gpgme.h".
(struct passphrase_result): Change to typedef for op_data_t.
(release_passphrase_result): Rename to release_op_data.
(_gpgme_passphrase_status_handler): Change first argument to void *.
Use new op_data_t type.
(_gpgme_passphrase_command_handler): Use new op_data_t type.
(_gpgme_passphrase_start): Remove function.
* decrypt.c (_gpgme_decrypt_start): Rewrite error handling. Do
not call _gpgme_passphrase_start, but install command handler.
* encrypt.c (_gpgme_op_encrypt_start): Likewise.
* encrypt-sign.c (_gpgme_op_encrypt_sign_start): Likewise.
* sign.c (_gpgme_op_sign_start): Likewise.
* context.h (struct gpgme_context_s): Remove member initialized, * context.h (struct gpgme_context_s): Remove member initialized,
use_cms and help_data_1. Add member protocol. Make use_armor and use_cms and help_data_1. Add member protocol. Make use_armor and
use_textmode bit flags. Make keylist_mode, include_certs, use_textmode bit flags. Make keylist_mode, include_certs,

View File

@ -163,41 +163,34 @@ _gpgme_decrypt_status_handler (GpgmeCtx ctx, GpgmeStatusCode code, char *args)
GpgmeError GpgmeError
_gpgme_decrypt_start (GpgmeCtx ctx, int synchronous, _gpgme_decrypt_start (GpgmeCtx ctx, int synchronous,
GpgmeData ciph, GpgmeData plain, void *status_handler) GpgmeData cipher, GpgmeData plain, void *status_handler)
{ {
GpgmeError err = 0; GpgmeError err;
err = _gpgme_op_reset (ctx, synchronous); err = _gpgme_op_reset (ctx, synchronous);
if (err) if (err)
goto leave; return err;
/* Check the supplied data. */ if (!cipher)
if (!ciph) return GPGME_No_Data;
{
err = GPGME_No_Data;
goto leave;
}
if (!plain) if (!plain)
{ return GPGME_Invalid_Value;
err = GPGME_Invalid_Value;
goto leave;
}
err = _gpgme_passphrase_start (ctx);
if (err) if (err)
goto leave; return err;
if (ctx->passphrase_cb)
{
err = _gpgme_engine_set_command_handler (ctx->engine,
_gpgme_passphrase_command_handler,
ctx, NULL);
if (err)
return err;
}
_gpgme_engine_set_status_handler (ctx->engine, status_handler, ctx); _gpgme_engine_set_status_handler (ctx->engine, status_handler, ctx);
err = _gpgme_engine_op_decrypt (ctx->engine, ciph, plain); return _gpgme_engine_op_decrypt (ctx->engine, cipher, plain);
leave:
if (err)
{
_gpgme_engine_release (ctx->engine);
ctx->engine = NULL;
}
return err;
} }

View File

@ -33,12 +33,10 @@
static GpgmeError static GpgmeError
encrypt_sign_status_handler (GpgmeCtx ctx, GpgmeStatusCode code, char *args) encrypt_sign_status_handler (void *priv, GpgmeStatusCode code, char *args)
{ {
GpgmeError err = _gpgme_encrypt_status_handler (ctx, code, args); return _gpgme_encrypt_status_handler (priv, code, args)
if (err) || _gpgme_sign_status_handler (priv, code, args);
return err;
return _gpgme_sign_status_handler (ctx, code, args);
} }
@ -47,41 +45,31 @@ _gpgme_op_encrypt_sign_start (GpgmeCtx ctx, int synchronous,
GpgmeRecipients recp, GpgmeRecipients recp,
GpgmeData plain, GpgmeData cipher) GpgmeData plain, GpgmeData cipher)
{ {
GpgmeError err = 0; GpgmeError err;
err = _gpgme_op_reset (ctx, synchronous); err = _gpgme_op_reset (ctx, synchronous);
if (err) if (err)
goto leave; return err;
err = _gpgme_passphrase_start (ctx); if (!plain)
return GPGME_No_Data;
if (!cipher)
return GPGME_Invalid_Value;
if (ctx->passphrase_cb)
{
err = _gpgme_engine_set_command_handler (ctx->engine,
_gpgme_passphrase_command_handler,
ctx, NULL);
if (err) if (err)
goto leave; return err;
}
_gpgme_engine_set_status_handler (ctx->engine, _gpgme_engine_set_status_handler (ctx->engine,
encrypt_sign_status_handler, ctx); encrypt_sign_status_handler, ctx);
/* Check the supplied data */ return _gpgme_engine_op_encrypt_sign (ctx->engine, recp, plain, cipher,
if (!plain)
{
err = GPGME_No_Data;
goto leave;
}
if (!cipher)
{
err = GPGME_Invalid_Value;
goto leave;
}
err = _gpgme_engine_op_encrypt_sign (ctx->engine, recp, plain, cipher,
ctx->use_armor, ctx /* FIXME */); ctx->use_armor, ctx /* FIXME */);
leave:
if (err)
{
_gpgme_engine_release (ctx->engine);
ctx->engine = NULL;
}
return err;
} }
GpgmeError GpgmeError

View File

@ -158,29 +158,33 @@ _gpgme_encrypt_sym_status_handler (GpgmeCtx ctx, GpgmeStatusCode code,
static GpgmeError static GpgmeError
_gpgme_op_encrypt_start (GpgmeCtx ctx, int synchronous, _gpgme_op_encrypt_start (GpgmeCtx ctx, int synchronous,
GpgmeRecipients recp, GpgmeData plain, GpgmeData ciph) GpgmeRecipients recp, GpgmeData plain, GpgmeData cipher)
{ {
GpgmeError err = 0; GpgmeError err;
int symmetric = 0; int symmetric = 0;
/* Do some checks. */
if (!recp)
symmetric = 1;
else if (!gpgme_recipients_count (recp))
{
err = GPGME_No_UserID;
goto leave;
}
err = _gpgme_op_reset (ctx, synchronous); err = _gpgme_op_reset (ctx, synchronous);
if (err) if (err)
goto leave; return err;
if (symmetric) if (!recp)
symmetric = 1;
else if (gpgme_recipients_count (recp) == 0)
return GPGME_No_UserID;
if (!plain)
return GPGME_No_Data;
if (!cipher)
return GPGME_Invalid_Value;
if (symmetric && ctx->passphrase_cb)
{ {
err = _gpgme_passphrase_start (ctx); /* Symmetric encryption requires a passphrase. */
err = _gpgme_engine_set_command_handler (ctx->engine,
_gpgme_passphrase_command_handler,
ctx, NULL);
if (err) if (err)
goto leave; return err;
} }
_gpgme_engine_set_status_handler (ctx->engine, _gpgme_engine_set_status_handler (ctx->engine,
@ -189,28 +193,8 @@ _gpgme_op_encrypt_start (GpgmeCtx ctx, int synchronous,
: _gpgme_encrypt_status_handler, : _gpgme_encrypt_status_handler,
ctx); ctx);
/* Check the supplied data */ return _gpgme_engine_op_encrypt (ctx->engine, recp, plain, cipher,
if (!plain)
{
err = GPGME_No_Data;
goto leave;
}
if (!ciph)
{
err = GPGME_Invalid_Value;
goto leave;
}
err = _gpgme_engine_op_encrypt (ctx->engine, recp, plain, ciph,
ctx->use_armor); ctx->use_armor);
leave:
if (err)
{
_gpgme_engine_release (ctx->engine);
ctx->engine = NULL;
}
return err;
} }

View File

@ -83,12 +83,11 @@ GpgmeError _gpgme_encrypt_status_handler (GpgmeCtx ctx, GpgmeStatusCode code,
char *args); char *args);
/*-- passphrase.c --*/ /*-- passphrase.c --*/
GpgmeError _gpgme_passphrase_status_handler (GpgmeCtx ctx, GpgmeStatusCode code, GpgmeError _gpgme_passphrase_status_handler (void *priv, GpgmeStatusCode code,
char *args); char *args);
GpgmeError _gpgme_passphrase_command_handler (void *opaque, GpgmeError _gpgme_passphrase_command_handler (void *opaque,
GpgmeStatusCode code, GpgmeStatusCode code,
const char *key, const char **result); const char *key, const char **result);
GpgmeError _gpgme_passphrase_start (GpgmeCtx ctx);
/*-- progress.c --*/ /*-- progress.c --*/
GpgmeError _gpgme_progress_status_handler (GpgmeCtx ctx, GpgmeStatusCode code, GpgmeError _gpgme_progress_status_handler (GpgmeCtx ctx, GpgmeStatusCode code,

View File

@ -1,4 +1,4 @@
/* passphrase.c - passphrase functions /* passphrase.c - Passphrase callback.
Copyright (C) 2000 Werner Koch (dd9jn) Copyright (C) 2000 Werner Koch (dd9jn)
Copyright (C) 2001, 2002, 2003 g10 Code GmbH Copyright (C) 2001, 2002, 2003 g10 Code GmbH
@ -24,79 +24,81 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h>
#include "util.h" #include "gpgme.h"
#include "context.h" #include "context.h"
#include "ops.h" #include "ops.h"
#include "debug.h"
struct passphrase_result typedef struct
{ {
int no_passphrase; int no_passphrase;
void *last_pw_handle; void *last_pw_handle;
char *userid_hint; char *userid_hint;
char *passphrase_info; char *passphrase_info;
int bad_passphrase; int bad_passphrase;
}; } *op_data_t;
typedef struct passphrase_result *PassphraseResult;
static void static void
release_passphrase_result (void *hook) release_op_data (void *hook)
{ {
PassphraseResult result = (PassphraseResult) hook; op_data_t opd = (op_data_t) hook;
free (result->passphrase_info); free (opd->passphrase_info);
free (result->userid_hint); free (opd->userid_hint);
} }
GpgmeError GpgmeError
_gpgme_passphrase_status_handler (GpgmeCtx ctx, GpgmeStatusCode code, char *args) _gpgme_passphrase_status_handler (void *priv, GpgmeStatusCode code, char *args)
{ {
GpgmeCtx ctx = (GpgmeCtx) priv;
GpgmeError err; GpgmeError err;
PassphraseResult result; op_data_t opd;
err = _gpgme_op_data_lookup (ctx, OPDATA_PASSPHRASE, (void **) &result, if (!ctx->passphrase_cb)
sizeof (*result), release_passphrase_result); return 0;
err = _gpgme_op_data_lookup (ctx, OPDATA_PASSPHRASE, (void **) &opd,
sizeof (*opd), release_op_data);
if (err) if (err)
return err; return err;
switch (code) switch (code)
{ {
case GPGME_STATUS_USERID_HINT: case GPGME_STATUS_USERID_HINT:
free (result->userid_hint); if (opd->userid_hint)
if (!(result->userid_hint = strdup (args))) free (opd->userid_hint);
if (!(opd->userid_hint = strdup (args)))
return GPGME_Out_Of_Core; return GPGME_Out_Of_Core;
break; break;
case GPGME_STATUS_BAD_PASSPHRASE: case GPGME_STATUS_BAD_PASSPHRASE:
result->bad_passphrase++; opd->bad_passphrase++;
result->no_passphrase = 0; opd->no_passphrase = 0;
break; break;
case GPGME_STATUS_GOOD_PASSPHRASE: case GPGME_STATUS_GOOD_PASSPHRASE:
result->bad_passphrase = 0; opd->bad_passphrase = 0;
result->no_passphrase = 0; opd->no_passphrase = 0;
break; break;
case GPGME_STATUS_NEED_PASSPHRASE: case GPGME_STATUS_NEED_PASSPHRASE:
case GPGME_STATUS_NEED_PASSPHRASE_SYM: case GPGME_STATUS_NEED_PASSPHRASE_SYM:
free (result->passphrase_info); if (opd->passphrase_info)
result->passphrase_info = strdup (args); free (opd->passphrase_info);
if (!result->passphrase_info) opd->passphrase_info = strdup (args);
if (!opd->passphrase_info)
return GPGME_Out_Of_Core; return GPGME_Out_Of_Core;
break; break;
case GPGME_STATUS_MISSING_PASSPHRASE: case GPGME_STATUS_MISSING_PASSPHRASE:
DEBUG0 ("missing passphrase - stop\n");; opd->no_passphrase = 1;
result->no_passphrase = 1;
break; break;
case GPGME_STATUS_EOF: case GPGME_STATUS_EOF:
if (result->no_passphrase if (opd->no_passphrase || opd->bad_passphrase)
|| result->bad_passphrase)
return GPGME_Bad_Passphrase; return GPGME_Bad_Passphrase;
break; break;
@ -109,15 +111,18 @@ _gpgme_passphrase_status_handler (GpgmeCtx ctx, GpgmeStatusCode code, char *args
GpgmeError GpgmeError
_gpgme_passphrase_command_handler (void *opaque, GpgmeStatusCode code, _gpgme_passphrase_command_handler (void *priv, GpgmeStatusCode code,
const char *key, const char **result_r) const char *key, const char **result)
{ {
GpgmeCtx ctx = opaque; GpgmeCtx ctx = (GpgmeCtx) priv;
GpgmeError err; GpgmeError err;
PassphraseResult result; op_data_t opd;
err = _gpgme_op_data_lookup (ctx, OPDATA_PASSPHRASE, (void **) &result, if (!ctx->passphrase_cb)
sizeof (*result), release_passphrase_result); return 0;
err = _gpgme_op_data_lookup (ctx, OPDATA_PASSPHRASE, (void **) &opd,
sizeof (*opd), release_op_data);
if (err) if (err)
return err; return err;
@ -125,27 +130,27 @@ _gpgme_passphrase_command_handler (void *opaque, GpgmeStatusCode code,
{ {
/* We have been called for cleanup. */ /* We have been called for cleanup. */
if (ctx->passphrase_cb) if (ctx->passphrase_cb)
/* Fixme: Take the key in account. */ /* FIXME: Take the key in account. */
err = ctx->passphrase_cb (ctx->passphrase_cb_value, NULL, err = ctx->passphrase_cb (ctx->passphrase_cb_value, NULL,
&result->last_pw_handle, NULL); &opd->last_pw_handle, NULL);
*result_r = NULL; *result = NULL;
return err; return err;
} }
if (!key || !ctx->passphrase_cb) if (!key || !ctx->passphrase_cb)
{ {
*result_r = NULL; *result = NULL;
return 0; return 0;
} }
if (code == GPGME_STATUS_GET_HIDDEN && !strcmp (key, "passphrase.enter")) if (code == GPGME_STATUS_GET_HIDDEN && !strcmp (key, "passphrase.enter"))
{ {
const char *userid_hint = result->userid_hint; const char *userid_hint = opd->userid_hint;
const char *passphrase_info = result->passphrase_info; const char *passphrase_info = opd->passphrase_info;
int bad_passphrase = result->bad_passphrase; int bad_passphrase = opd->bad_passphrase;
char *buf; char *buf;
result->bad_passphrase = 0; opd->bad_passphrase = 0;
if (!userid_hint) if (!userid_hint)
userid_hint = "[User ID hint missing]"; userid_hint = "[User ID hint missing]";
if (!passphrase_info) if (!passphrase_info)
@ -159,24 +164,11 @@ _gpgme_passphrase_command_handler (void *opaque, GpgmeStatusCode code,
userid_hint, passphrase_info); userid_hint, passphrase_info);
err = ctx->passphrase_cb (ctx->passphrase_cb_value, buf, err = ctx->passphrase_cb (ctx->passphrase_cb_value, buf,
&result->last_pw_handle, result_r); &opd->last_pw_handle, result);
free (buf); free (buf);
return err; return err;
} }
*result_r = NULL; *result = NULL;
return 0; return 0;
} }
GpgmeError
_gpgme_passphrase_start (GpgmeCtx ctx)
{
GpgmeError err = 0;
if (ctx->passphrase_cb)
err = _gpgme_engine_set_command_handler (ctx->engine,
_gpgme_passphrase_command_handler,
ctx, NULL);
return err;
}

View File

@ -182,50 +182,39 @@ _gpgme_sign_status_handler (GpgmeCtx ctx, GpgmeStatusCode code, char *args)
static GpgmeError static GpgmeError
_gpgme_op_sign_start (GpgmeCtx ctx, int synchronous, _gpgme_op_sign_start (GpgmeCtx ctx, int synchronous,
GpgmeData in, GpgmeData out, GpgmeData plain, GpgmeData sig,
GpgmeSigMode mode) GpgmeSigMode mode)
{ {
GpgmeError err = 0; GpgmeError err;
if (mode != GPGME_SIG_MODE_NORMAL
&& mode != GPGME_SIG_MODE_DETACH
&& mode != GPGME_SIG_MODE_CLEAR)
return GPGME_Invalid_Value;
err = _gpgme_op_reset (ctx, synchronous); err = _gpgme_op_reset (ctx, synchronous);
if (err) if (err)
goto leave; return err;
/* Check the supplied data. */ if (mode != GPGME_SIG_MODE_NORMAL && mode != GPGME_SIG_MODE_DETACH
if (!in) && mode != GPGME_SIG_MODE_CLEAR)
{ return GPGME_Invalid_Value;
err = GPGME_No_Data;
goto leave;
}
if (!out)
{
err = GPGME_Invalid_Value;
goto leave;
}
err = _gpgme_passphrase_start (ctx); if (!plain)
return GPGME_No_Data;
if (!sig)
return GPGME_Invalid_Value;
if (ctx->passphrase_cb)
{
err = _gpgme_engine_set_command_handler (ctx->engine,
_gpgme_passphrase_command_handler,
ctx, NULL);
if (err) if (err)
goto leave; return err;
}
_gpgme_engine_set_status_handler (ctx->engine, _gpgme_sign_status_handler, _gpgme_engine_set_status_handler (ctx->engine, _gpgme_sign_status_handler,
ctx); ctx);
err = _gpgme_engine_op_sign (ctx->engine, in, out, mode, ctx->use_armor, return _gpgme_engine_op_sign (ctx->engine, plain, sig, mode, ctx->use_armor,
ctx->use_textmode, ctx->include_certs, ctx->use_textmode, ctx->include_certs,
ctx /* FIXME */); ctx /* FIXME */);
leave:
if (err)
{
_gpgme_engine_release (ctx->engine);
ctx->engine = NULL;
}
return err;
} }
GpgmeError GpgmeError