2000-11-16 13:15:48 +00:00
|
|
|
/* decrypt.c - decrypt functions
|
|
|
|
* Copyright (C) 2000 Werner Koch (dd9jn)
|
2001-04-02 08:40:32 +00:00
|
|
|
* Copyright (C) 2001 g10 Code GmbH
|
2000-11-16 13:15:48 +00:00
|
|
|
*
|
|
|
|
* This file is part of GPGME.
|
|
|
|
*
|
|
|
|
* GPGME is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GPGME is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "context.h"
|
|
|
|
#include "ops.h"
|
|
|
|
|
2001-11-15 21:32:09 +00:00
|
|
|
struct decrypt_result_s
|
|
|
|
{
|
|
|
|
int okay;
|
|
|
|
int failed;
|
2000-11-16 13:15:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2001-11-15 21:32:09 +00:00
|
|
|
_gpgme_release_decrypt_result (DecryptResult result)
|
2000-11-16 13:15:48 +00:00
|
|
|
{
|
2001-11-15 21:32:09 +00:00
|
|
|
if (!result)
|
|
|
|
return;
|
|
|
|
xfree (result);
|
2000-11-16 13:15:48 +00:00
|
|
|
}
|
|
|
|
|
2000-12-06 12:17:10 +00:00
|
|
|
static GpgmeError
|
2001-11-15 21:32:09 +00:00
|
|
|
create_result_struct (GpgmeCtx ctx)
|
2000-12-06 12:17:10 +00:00
|
|
|
{
|
2001-11-15 21:32:09 +00:00
|
|
|
assert (!ctx->result.decrypt);
|
|
|
|
ctx->result.decrypt = xtrycalloc (1, sizeof *ctx->result.decrypt);
|
|
|
|
if (!ctx->result.decrypt)
|
|
|
|
return mk_error (Out_Of_Core);
|
|
|
|
return 0;
|
2000-12-06 12:17:10 +00:00
|
|
|
}
|
2000-11-16 13:15:48 +00:00
|
|
|
|
2001-11-16 01:37:06 +00:00
|
|
|
void
|
|
|
|
_gpgme_decrypt_status_handler (GpgmeCtx ctx, GpgStatusCode code, char *args)
|
2000-11-16 13:15:48 +00:00
|
|
|
{
|
2001-11-16 00:20:11 +00:00
|
|
|
_gpgme_passphrase_status_handler (ctx, code, args);
|
2000-11-16 13:15:48 +00:00
|
|
|
|
2001-11-16 00:20:11 +00:00
|
|
|
if (ctx->out_of_core)
|
|
|
|
return;
|
2000-11-16 13:15:48 +00:00
|
|
|
|
2001-11-16 00:20:11 +00:00
|
|
|
if (! ctx->result.decrypt)
|
|
|
|
{
|
|
|
|
if (create_result_struct (ctx))
|
|
|
|
{
|
|
|
|
ctx->out_of_core = 1;
|
|
|
|
return;
|
|
|
|
}
|
2000-11-16 13:15:48 +00:00
|
|
|
}
|
|
|
|
|
2001-11-16 00:20:11 +00:00
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
case STATUS_EOF:
|
|
|
|
break;
|
2001-01-11 11:56:34 +00:00
|
|
|
|
2001-11-16 00:20:11 +00:00
|
|
|
case STATUS_DECRYPTION_OKAY:
|
|
|
|
ctx->result.decrypt->okay = 1;
|
|
|
|
break;
|
2000-12-12 13:31:25 +00:00
|
|
|
|
2001-11-16 00:20:11 +00:00
|
|
|
case STATUS_DECRYPTION_FAILED:
|
|
|
|
ctx->result.decrypt->failed = 1;
|
|
|
|
break;
|
2000-12-12 13:31:25 +00:00
|
|
|
|
2001-11-16 00:20:11 +00:00
|
|
|
default:
|
|
|
|
/* Ignore all other codes. */
|
|
|
|
break;
|
2000-12-12 13:31:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-11-16 13:15:48 +00:00
|
|
|
GpgmeError
|
2001-11-16 01:37:06 +00:00
|
|
|
_gpgme_decrypt_start (GpgmeCtx ctx, GpgmeData ciph, GpgmeData plain,
|
|
|
|
void *status_handler)
|
2000-11-16 13:15:48 +00:00
|
|
|
{
|
2001-11-16 00:20:11 +00:00
|
|
|
GpgmeError err = 0;
|
2000-11-16 13:15:48 +00:00
|
|
|
|
2001-11-16 00:20:11 +00:00
|
|
|
fail_on_pending_request (ctx);
|
|
|
|
ctx->pending = 1;
|
2000-11-16 13:15:48 +00:00
|
|
|
|
2001-11-16 00:20:11 +00:00
|
|
|
_gpgme_release_result (ctx);
|
|
|
|
ctx->out_of_core = 0;
|
2000-11-16 13:15:48 +00:00
|
|
|
|
2001-11-16 00:20:11 +00:00
|
|
|
/* Create a process object. */
|
2001-11-21 03:40:17 +00:00
|
|
|
_gpgme_engine_release (ctx->engine);
|
|
|
|
err = _gpgme_engine_new (ctx->use_cms ? GPGME_PROTOCOL_CMS
|
|
|
|
: GPGME_PROTOCOL_OpenPGP, &ctx->engine);
|
2001-11-16 00:20:11 +00:00
|
|
|
if (err)
|
|
|
|
goto leave;
|
2000-11-16 13:15:48 +00:00
|
|
|
|
2001-11-16 00:20:11 +00:00
|
|
|
/* Check the supplied data. */
|
|
|
|
if (!ciph || gpgme_data_get_type (ciph) == GPGME_DATA_TYPE_NONE)
|
|
|
|
{
|
|
|
|
err = mk_error (No_Data);
|
|
|
|
goto leave;
|
2000-11-16 13:15:48 +00:00
|
|
|
}
|
2001-11-16 00:20:11 +00:00
|
|
|
_gpgme_data_set_mode (ciph, GPGME_DATA_MODE_OUT);
|
2000-12-06 12:17:10 +00:00
|
|
|
|
2001-11-16 00:20:11 +00:00
|
|
|
if (gpgme_data_get_type (plain) != GPGME_DATA_TYPE_NONE)
|
|
|
|
{
|
|
|
|
err = mk_error (Invalid_Value);
|
|
|
|
goto leave;
|
2000-11-16 13:15:48 +00:00
|
|
|
}
|
2001-11-16 00:20:11 +00:00
|
|
|
_gpgme_data_set_mode (plain, GPGME_DATA_MODE_IN);
|
2000-11-16 13:15:48 +00:00
|
|
|
|
2001-11-21 03:40:17 +00:00
|
|
|
err = _gpgme_passphrase_start (ctx);
|
|
|
|
if (err)
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
_gpgme_engine_set_status_handler (ctx->engine, status_handler, ctx);
|
|
|
|
_gpgme_engine_set_verbosity (ctx->engine, ctx->verbosity);
|
|
|
|
|
|
|
|
err = _gpgme_engine_op_decrypt (ctx->engine, ciph, plain);
|
2000-11-16 13:15:48 +00:00
|
|
|
|
2001-11-21 03:40:17 +00:00
|
|
|
if (!err) /* And kick off the process. */
|
|
|
|
err = _gpgme_engine_start (ctx->engine, ctx);
|
2000-11-16 13:15:48 +00:00
|
|
|
|
|
|
|
leave:
|
2001-11-16 00:20:11 +00:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
ctx->pending = 0;
|
2001-11-21 03:40:17 +00:00
|
|
|
_gpgme_engine_release (ctx->engine);
|
|
|
|
ctx->engine = NULL;
|
2000-11-16 13:15:48 +00:00
|
|
|
}
|
2001-11-16 00:20:11 +00:00
|
|
|
return err;
|
2000-11-16 13:15:48 +00:00
|
|
|
}
|
|
|
|
|
2001-11-16 01:37:06 +00:00
|
|
|
GpgmeError
|
|
|
|
gpgme_op_decrypt_start (GpgmeCtx ctx, GpgmeData ciph, GpgmeData plain)
|
|
|
|
{
|
|
|
|
return _gpgme_decrypt_start (ctx, ciph, plain,
|
|
|
|
_gpgme_decrypt_status_handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
GpgmeError
|
|
|
|
_gpgme_decrypt_result (GpgmeCtx ctx)
|
|
|
|
{
|
|
|
|
GpgmeError err = 0;
|
|
|
|
|
|
|
|
if (!ctx->result.decrypt)
|
|
|
|
err = mk_error (General_Error);
|
|
|
|
else if (ctx->out_of_core)
|
|
|
|
err = mk_error (Out_Of_Core);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = _gpgme_passphrase_result (ctx);
|
|
|
|
if (! err)
|
|
|
|
{
|
|
|
|
if (ctx->result.decrypt->failed)
|
|
|
|
err = mk_error (Decryption_Failed);
|
|
|
|
else if (!ctx->result.decrypt->okay)
|
|
|
|
err = mk_error (No_Data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2000-11-16 13:15:48 +00:00
|
|
|
/**
|
|
|
|
* gpgme_op_decrypt:
|
2001-11-16 00:20:11 +00:00
|
|
|
* @ctx: The context
|
2000-11-16 13:15:48 +00:00
|
|
|
* @in: ciphertext input
|
|
|
|
* @out: plaintext output
|
|
|
|
*
|
|
|
|
* This function decrypts @in to @out.
|
2001-11-16 00:20:11 +00:00
|
|
|
* Other parameters are take from the context @ctx.
|
2000-11-16 13:15:48 +00:00
|
|
|
* The function does wait for the result.
|
|
|
|
*
|
|
|
|
* Return value: 0 on success or an errorcode.
|
|
|
|
**/
|
|
|
|
GpgmeError
|
2001-11-16 00:20:11 +00:00
|
|
|
gpgme_op_decrypt (GpgmeCtx ctx, GpgmeData in, GpgmeData out)
|
2000-11-16 13:15:48 +00:00
|
|
|
{
|
2001-11-16 00:20:11 +00:00
|
|
|
GpgmeError err = gpgme_op_decrypt_start (ctx, in, out);
|
2001-11-15 21:32:09 +00:00
|
|
|
if (!err)
|
|
|
|
{
|
2001-11-16 00:20:11 +00:00
|
|
|
gpgme_wait (ctx, 1);
|
2001-11-16 01:37:06 +00:00
|
|
|
err = _gpgme_decrypt_result (ctx);
|
2001-11-16 00:20:11 +00:00
|
|
|
ctx->pending = 0;
|
2000-11-16 13:15:48 +00:00
|
|
|
}
|
2001-11-15 21:32:09 +00:00
|
|
|
return err;
|
2000-11-16 13:15:48 +00:00
|
|
|
}
|