2009-10-02 16:57:55 +02:00
|
|
|
/* decrypt-data.c - Decrypt an encrypted data packet
|
2006-04-19 13:26:11 +02:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2005,
|
Add full Camellia support.
* configure.ac: Remove Camellia restriction.
* gpg.c (main), misc.c (openpgp_cipher_test_algo): Remove Camellia
restriction.
* misc.c (map_cipher_openpgp_to_gcry), main.h: Add macros for
openpgp_cipher_open, openpgp_cipher_get_algo_keylen, and
openpgp_cipher_get_algo_blklen to wrap around the corresponding gcry_*
functions, but pass the algorithm number through
map_cipher_openpgp_to_gcry. This is needed in case the gcry algorithm
number doesn't match the OpenPGP number (c.f. Camellia).
* encr-data.c, pubkey-enc.c, mainproc.c, cipher.c, encode.c, seskey.c,
passphrase.c, seckey-cert.c: Use new openpgp_cipher_* macros here.
2009-06-05 16:11:03 +02:00
|
|
|
* 2006, 2009 Free Software Foundation, Inc.
|
1997-11-18 15:06:00 +01:00
|
|
|
*
|
1998-12-23 13:41:40 +01:00
|
|
|
* This file is part of GnuPG.
|
1997-11-18 15:06:00 +01:00
|
|
|
*
|
1998-12-23 13:41:40 +01:00
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
1997-11-18 15:06:00 +01:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 21:49:40 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
1997-11-18 15:06:00 +01:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
1998-12-23 13:41:40 +01:00
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
1997-11-18 15:06:00 +01:00
|
|
|
* 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
|
2007-07-04 21:49:40 +02:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
1997-11-18 15:06:00 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2003-06-18 21:56:13 +02:00
|
|
|
|
|
|
|
#include "gpg.h"
|
1997-11-18 15:06:00 +01:00
|
|
|
#include "util.h"
|
|
|
|
#include "packet.h"
|
1998-04-04 22:16:55 +02:00
|
|
|
#include "options.h"
|
1998-09-14 17:49:56 +02:00
|
|
|
#include "i18n.h"
|
2011-02-03 20:59:01 +01:00
|
|
|
#include "status.h"
|
1997-11-18 15:06:00 +01:00
|
|
|
|
|
|
|
|
2006-10-02 13:54:35 +02:00
|
|
|
static int mdc_decode_filter ( void *opaque, int control, IOBUF a,
|
|
|
|
byte *buf, size_t *ret_len);
|
|
|
|
static int decode_filter ( void *opaque, int control, IOBUF a,
|
1999-05-17 22:03:24 +02:00
|
|
|
byte *buf, size_t *ret_len);
|
1997-11-18 15:06:00 +01:00
|
|
|
|
2006-12-06 11:16:50 +01:00
|
|
|
typedef struct decode_filter_context_s
|
2006-04-19 13:26:11 +02:00
|
|
|
{
|
|
|
|
gcry_cipher_hd_t cipher_hd;
|
|
|
|
gcry_md_hd_t mdc_hash;
|
2006-10-02 13:54:35 +02:00
|
|
|
char defer[22];
|
2006-04-19 13:26:11 +02:00
|
|
|
int defer_filled;
|
|
|
|
int eof_seen;
|
2006-12-06 11:16:50 +01:00
|
|
|
int refcount;
|
2009-10-02 14:31:14 +02:00
|
|
|
int partial; /* Working on a partial length packet. */
|
|
|
|
size_t length; /* If !partial: Remaining bytes in the packet. */
|
2006-12-06 11:16:50 +01:00
|
|
|
} *decode_filter_ctx_t;
|
|
|
|
|
|
|
|
|
|
|
|
/* Helper to release the decode context. */
|
|
|
|
static void
|
|
|
|
release_dfx_context (decode_filter_ctx_t dfx)
|
|
|
|
{
|
|
|
|
if (!dfx)
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert (dfx->refcount);
|
|
|
|
if ( !--dfx->refcount )
|
|
|
|
{
|
|
|
|
gcry_cipher_close (dfx->cipher_hd);
|
|
|
|
dfx->cipher_hd = NULL;
|
|
|
|
gcry_md_close (dfx->mdc_hash);
|
|
|
|
dfx->mdc_hash = NULL;
|
|
|
|
xfree (dfx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Decrypt the data, specified by ED with the key DEK.
|
|
|
|
*/
|
|
|
|
int
|
2010-10-01 22:33:53 +02:00
|
|
|
decrypt_data (ctrl_t ctrl, void *procctx, PKT_encrypted *ed, DEK *dek)
|
1997-11-18 15:06:00 +01:00
|
|
|
{
|
2006-10-02 13:54:35 +02:00
|
|
|
decode_filter_ctx_t dfx;
|
|
|
|
byte *p;
|
|
|
|
int rc=0, c, i;
|
|
|
|
byte temp[32];
|
|
|
|
unsigned blocksize;
|
|
|
|
unsigned nprefix;
|
2011-02-03 20:59:01 +01:00
|
|
|
|
2006-12-06 11:16:50 +01:00
|
|
|
dfx = xtrycalloc (1, sizeof *dfx);
|
|
|
|
if (!dfx)
|
|
|
|
return gpg_error_from_syserror ();
|
|
|
|
dfx->refcount = 1;
|
|
|
|
|
2006-10-02 13:54:35 +02:00
|
|
|
if ( opt.verbose && !dek->algo_info_printed )
|
|
|
|
{
|
2007-12-12 11:28:30 +01:00
|
|
|
if (!openpgp_cipher_test_algo (dek->algo))
|
2011-02-03 20:59:01 +01:00
|
|
|
log_info (_("%s encrypted data\n"),
|
2007-12-12 11:28:30 +01:00
|
|
|
openpgp_cipher_algo_name (dek->algo));
|
2006-10-02 13:54:35 +02:00
|
|
|
else
|
2007-02-26 21:24:29 +01:00
|
|
|
log_info (_("encrypted with unknown algorithm %d\n"), dek->algo );
|
2006-10-02 13:54:35 +02:00
|
|
|
dek->algo_info_printed = 1;
|
|
|
|
}
|
2011-02-03 20:59:01 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
char buf[20];
|
|
|
|
|
|
|
|
snprintf (buf, sizeof buf, "%d %d", ed->mdc_method, dek->algo);
|
|
|
|
write_status_text (STATUS_DECRYPTION_INFO, buf);
|
|
|
|
}
|
|
|
|
|
2013-12-11 10:20:15 +01:00
|
|
|
if (opt.show_session_key)
|
|
|
|
{
|
|
|
|
char numbuf[25];
|
|
|
|
char *hexbuf;
|
|
|
|
|
|
|
|
snprintf (numbuf, sizeof numbuf, "%d:", dek->algo);
|
|
|
|
hexbuf = bin2hex (dek->key, dek->keylen, NULL);
|
|
|
|
if (!hexbuf)
|
|
|
|
{
|
|
|
|
rc = gpg_error_from_syserror ();
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
log_info ("session key: '%s%s'\n", numbuf, hexbuf);
|
|
|
|
write_status_strings (STATUS_SESSION_KEY, numbuf, hexbuf, NULL);
|
|
|
|
xfree (hexbuf);
|
|
|
|
}
|
|
|
|
|
2006-10-02 13:54:35 +02:00
|
|
|
rc = openpgp_cipher_test_algo (dek->algo);
|
|
|
|
if (rc)
|
|
|
|
goto leave;
|
Add full Camellia support.
* configure.ac: Remove Camellia restriction.
* gpg.c (main), misc.c (openpgp_cipher_test_algo): Remove Camellia
restriction.
* misc.c (map_cipher_openpgp_to_gcry), main.h: Add macros for
openpgp_cipher_open, openpgp_cipher_get_algo_keylen, and
openpgp_cipher_get_algo_blklen to wrap around the corresponding gcry_*
functions, but pass the algorithm number through
map_cipher_openpgp_to_gcry. This is needed in case the gcry algorithm
number doesn't match the OpenPGP number (c.f. Camellia).
* encr-data.c, pubkey-enc.c, mainproc.c, cipher.c, encode.c, seskey.c,
passphrase.c, seckey-cert.c: Use new openpgp_cipher_* macros here.
2009-06-05 16:11:03 +02:00
|
|
|
blocksize = openpgp_cipher_get_algo_blklen (dek->algo);
|
2006-10-02 13:54:35 +02:00
|
|
|
if ( !blocksize || blocksize > 16 )
|
2006-12-06 11:16:50 +01:00
|
|
|
log_fatal ("unsupported blocksize %u\n", blocksize );
|
2006-10-02 13:54:35 +02:00
|
|
|
nprefix = blocksize;
|
|
|
|
if ( ed->len && ed->len < (nprefix+2) )
|
2014-11-26 10:21:01 +01:00
|
|
|
{
|
|
|
|
/* An invalid message. We can't check that during parsing
|
|
|
|
because we may not know the used cipher then. */
|
|
|
|
rc = gpg_error (GPG_ERR_INV_PACKET);
|
|
|
|
goto leave;
|
|
|
|
}
|
1997-11-18 15:06:00 +01:00
|
|
|
|
2011-02-03 20:59:01 +01:00
|
|
|
if ( ed->mdc_method )
|
2006-10-02 13:54:35 +02:00
|
|
|
{
|
2006-12-06 11:16:50 +01:00
|
|
|
if (gcry_md_open (&dfx->mdc_hash, ed->mdc_method, 0 ))
|
2006-10-02 13:54:35 +02:00
|
|
|
BUG ();
|
|
|
|
if ( DBG_HASHING )
|
2011-09-20 09:54:27 +02:00
|
|
|
gcry_md_debug (dfx->mdc_hash, "checkmdc");
|
1999-09-18 12:17:16 +02:00
|
|
|
}
|
1999-10-26 14:14:37 +02:00
|
|
|
|
Add full Camellia support.
* configure.ac: Remove Camellia restriction.
* gpg.c (main), misc.c (openpgp_cipher_test_algo): Remove Camellia
restriction.
* misc.c (map_cipher_openpgp_to_gcry), main.h: Add macros for
openpgp_cipher_open, openpgp_cipher_get_algo_keylen, and
openpgp_cipher_get_algo_blklen to wrap around the corresponding gcry_*
functions, but pass the algorithm number through
map_cipher_openpgp_to_gcry. This is needed in case the gcry algorithm
number doesn't match the OpenPGP number (c.f. Camellia).
* encr-data.c, pubkey-enc.c, mainproc.c, cipher.c, encode.c, seskey.c,
passphrase.c, seckey-cert.c: Use new openpgp_cipher_* macros here.
2009-06-05 16:11:03 +02:00
|
|
|
rc = openpgp_cipher_open (&dfx->cipher_hd, dek->algo,
|
|
|
|
GCRY_CIPHER_MODE_CFB,
|
|
|
|
(GCRY_CIPHER_SECURE
|
|
|
|
| ((ed->mdc_method || dek->algo >= 100)?
|
|
|
|
0 : GCRY_CIPHER_ENABLE_SYNC)));
|
2006-10-02 13:54:35 +02:00
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
/* We should never get an error here cause we already checked
|
|
|
|
* that the algorithm is available. */
|
|
|
|
BUG();
|
1999-10-26 14:14:37 +02:00
|
|
|
}
|
2006-04-19 13:26:11 +02:00
|
|
|
|
|
|
|
|
2006-10-02 13:54:35 +02:00
|
|
|
/* log_hexdump( "thekey", dek->key, dek->keylen );*/
|
2006-12-06 11:16:50 +01:00
|
|
|
rc = gcry_cipher_setkey (dfx->cipher_hd, dek->key, dek->keylen);
|
2006-10-02 13:54:35 +02:00
|
|
|
if ( gpg_err_code (rc) == GPG_ERR_WEAK_KEY )
|
|
|
|
{
|
|
|
|
log_info(_("WARNING: message was encrypted with"
|
|
|
|
" a weak key in the symmetric cipher.\n"));
|
|
|
|
rc=0;
|
|
|
|
}
|
|
|
|
else if( rc )
|
|
|
|
{
|
2015-01-22 12:06:11 +01:00
|
|
|
log_error("key setup failed: %s\n", gpg_strerror (rc) );
|
2006-10-02 13:54:35 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
2006-04-19 13:26:11 +02:00
|
|
|
|
2011-02-03 20:59:01 +01:00
|
|
|
if (!ed->buf)
|
2006-10-02 13:54:35 +02:00
|
|
|
{
|
|
|
|
log_error(_("problem handling encrypted packet\n"));
|
|
|
|
goto leave;
|
2002-06-29 15:46:34 +02:00
|
|
|
}
|
1998-09-14 17:49:56 +02:00
|
|
|
|
2006-12-06 11:16:50 +01:00
|
|
|
gcry_cipher_setiv (dfx->cipher_hd, NULL, 0);
|
1997-11-18 15:06:00 +01:00
|
|
|
|
2006-10-02 13:54:35 +02:00
|
|
|
if ( ed->len )
|
|
|
|
{
|
2011-02-03 20:59:01 +01:00
|
|
|
for (i=0; i < (nprefix+2) && ed->len; i++, ed->len-- )
|
2006-10-02 13:54:35 +02:00
|
|
|
{
|
|
|
|
if ( (c=iobuf_get(ed->buf)) == -1 )
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
temp[i] = c;
|
|
|
|
}
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
2011-02-03 20:59:01 +01:00
|
|
|
else
|
2006-10-02 13:54:35 +02:00
|
|
|
{
|
|
|
|
for (i=0; i < (nprefix+2); i++ )
|
|
|
|
if ( (c=iobuf_get(ed->buf)) == -1 )
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
temp[i] = c;
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
2011-02-03 20:59:01 +01:00
|
|
|
|
2006-12-06 11:16:50 +01:00
|
|
|
gcry_cipher_decrypt (dfx->cipher_hd, temp, nprefix+2, NULL, 0);
|
|
|
|
gcry_cipher_sync (dfx->cipher_hd);
|
2006-10-02 13:54:35 +02:00
|
|
|
p = temp;
|
|
|
|
/* log_hexdump( "prefix", temp, nprefix+2 ); */
|
|
|
|
if (dek->symmetric
|
|
|
|
&& (p[nprefix-2] != p[nprefix] || p[nprefix-1] != p[nprefix+1]) )
|
|
|
|
{
|
|
|
|
rc = gpg_error (GPG_ERR_BAD_KEY);
|
|
|
|
goto leave;
|
|
|
|
}
|
2011-02-03 20:59:01 +01:00
|
|
|
|
2006-12-06 11:16:50 +01:00
|
|
|
if ( dfx->mdc_hash )
|
|
|
|
gcry_md_write (dfx->mdc_hash, temp, nprefix+2);
|
|
|
|
|
|
|
|
dfx->refcount++;
|
2009-10-02 14:31:14 +02:00
|
|
|
dfx->partial = ed->is_partial;
|
|
|
|
dfx->length = ed->len;
|
2006-10-02 13:54:35 +02:00
|
|
|
if ( ed->mdc_method )
|
2006-12-06 11:16:50 +01:00
|
|
|
iobuf_push_filter ( ed->buf, mdc_decode_filter, dfx );
|
2006-10-02 13:54:35 +02:00
|
|
|
else
|
2006-12-06 11:16:50 +01:00
|
|
|
iobuf_push_filter ( ed->buf, decode_filter, dfx );
|
2006-04-19 13:26:11 +02:00
|
|
|
|
2010-10-01 22:33:53 +02:00
|
|
|
proc_packets (ctrl, procctx, ed->buf );
|
2006-10-02 13:54:35 +02:00
|
|
|
ed->buf = NULL;
|
2009-10-02 14:31:14 +02:00
|
|
|
if (dfx->eof_seen > 1 )
|
2006-10-02 13:54:35 +02:00
|
|
|
rc = gpg_error (GPG_ERR_INV_PACKET);
|
|
|
|
else if ( ed->mdc_method )
|
2011-02-03 20:59:01 +01:00
|
|
|
{
|
2006-10-02 13:54:35 +02:00
|
|
|
/* We used to let parse-packet.c handle the MDC packet but this
|
|
|
|
turned out to be a problem with compressed packets: With old
|
|
|
|
style packets there is no length information available and
|
|
|
|
the decompressor uses an implicit end. However we can't know
|
|
|
|
this implicit end beforehand (:-) and thus may feed the
|
|
|
|
decompressor with more bytes than actually needed. It would
|
|
|
|
be possible to unread the extra bytes but due to our weird
|
|
|
|
iobuf system any unread is non reliable due to filters
|
|
|
|
already popped off. The easy and sane solution is to care
|
|
|
|
about the MDC packet only here and never pass it to the
|
|
|
|
packet parser. Fortunatley the OpenPGP spec requires a
|
|
|
|
strict format for the MDC packet so that we know that 22
|
|
|
|
bytes are appended. */
|
|
|
|
int datalen = gcry_md_get_algo_dlen (ed->mdc_method);
|
2000-07-14 19:34:53 +02:00
|
|
|
|
2006-12-06 11:16:50 +01:00
|
|
|
assert (dfx->cipher_hd);
|
|
|
|
assert (dfx->mdc_hash);
|
|
|
|
gcry_cipher_decrypt (dfx->cipher_hd, dfx->defer, 22, NULL, 0);
|
|
|
|
gcry_md_write (dfx->mdc_hash, dfx->defer, 2);
|
|
|
|
gcry_md_final (dfx->mdc_hash);
|
2000-07-14 19:34:53 +02:00
|
|
|
|
2014-05-14 08:55:58 +02:00
|
|
|
if ( dfx->defer[0] != '\xd3'
|
|
|
|
|| dfx->defer[1] != '\x14'
|
|
|
|
|| datalen != 20
|
|
|
|
|| memcmp (gcry_md_read (dfx->mdc_hash, 0), dfx->defer+2, datalen))
|
2006-10-02 13:54:35 +02:00
|
|
|
rc = gpg_error (GPG_ERR_BAD_SIGNATURE);
|
2006-12-06 11:16:50 +01:00
|
|
|
/* log_printhex("MDC message:", dfx->defer, 22); */
|
|
|
|
/* log_printhex("MDC calc:", gcry_md_read (dfx->mdc_hash,0), datalen); */
|
1999-05-17 22:03:24 +02:00
|
|
|
}
|
2011-02-03 20:59:01 +01:00
|
|
|
|
|
|
|
|
2006-10-02 13:54:35 +02:00
|
|
|
leave:
|
2006-12-06 11:16:50 +01:00
|
|
|
release_dfx_context (dfx);
|
2006-10-02 13:54:35 +02:00
|
|
|
return rc;
|
1999-05-17 22:03:24 +02:00
|
|
|
}
|
|
|
|
|
1999-10-26 14:14:37 +02:00
|
|
|
|
2000-07-14 19:34:53 +02:00
|
|
|
|
1999-05-17 22:03:24 +02:00
|
|
|
static int
|
2006-10-02 13:54:35 +02:00
|
|
|
mdc_decode_filter (void *opaque, int control, IOBUF a,
|
|
|
|
byte *buf, size_t *ret_len)
|
1999-05-17 22:03:24 +02:00
|
|
|
{
|
2006-12-06 11:16:50 +01:00
|
|
|
decode_filter_ctx_t dfx = opaque;
|
2006-10-02 13:54:35 +02:00
|
|
|
size_t n, size = *ret_len;
|
|
|
|
int rc = 0;
|
|
|
|
int c;
|
2009-10-02 14:31:14 +02:00
|
|
|
|
|
|
|
/* Note: We need to distinguish between a partial and a fixed length
|
|
|
|
packet. The first is the usual case as created by GPG. However
|
|
|
|
for short messages the format degrades to a fixed length packet
|
|
|
|
and other implementations might use fixed length as well. Only
|
|
|
|
looking for the EOF on fixed data works only if the encrypted
|
|
|
|
packet is not followed by other data. This used to be a long
|
|
|
|
standing bug which was fixed on 2009-10-02. */
|
2011-02-03 20:59:01 +01:00
|
|
|
|
2006-10-02 13:54:35 +02:00
|
|
|
if ( control == IOBUFCTRL_UNDERFLOW && dfx->eof_seen )
|
|
|
|
{
|
|
|
|
*ret_len = 0;
|
|
|
|
rc = -1;
|
1999-05-17 22:03:24 +02:00
|
|
|
}
|
2006-10-02 13:54:35 +02:00
|
|
|
else if( control == IOBUFCTRL_UNDERFLOW )
|
|
|
|
{
|
2006-12-06 11:16:50 +01:00
|
|
|
assert (a);
|
2009-10-02 14:31:14 +02:00
|
|
|
assert (size > 44); /* Our code requires at least this size. */
|
2011-02-03 20:59:01 +01:00
|
|
|
|
2009-10-02 14:31:14 +02:00
|
|
|
/* Get at least 22 bytes and put it ahead in the buffer. */
|
|
|
|
if (dfx->partial)
|
2006-10-02 13:54:35 +02:00
|
|
|
{
|
2009-10-02 14:31:14 +02:00
|
|
|
for (n=22; n < 44; n++)
|
|
|
|
{
|
|
|
|
if ( (c = iobuf_get(a)) == -1 )
|
|
|
|
break;
|
|
|
|
buf[n] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (n=22; n < 44 && dfx->length; n++, dfx->length--)
|
|
|
|
{
|
|
|
|
c = iobuf_get (a);
|
|
|
|
if (c == -1)
|
|
|
|
break; /* Premature EOF. */
|
|
|
|
buf[n] = c;
|
|
|
|
}
|
|
|
|
}
|
2011-02-03 20:59:01 +01:00
|
|
|
if (n == 44)
|
2006-10-02 13:54:35 +02:00
|
|
|
{
|
|
|
|
/* We have enough stuff - flush the deferred stuff. */
|
|
|
|
if ( !dfx->defer_filled ) /* First time. */
|
|
|
|
{
|
2009-10-02 14:31:14 +02:00
|
|
|
memcpy (buf, buf+22, 22);
|
2006-10-02 13:54:35 +02:00
|
|
|
n = 22;
|
2000-07-14 19:34:53 +02:00
|
|
|
}
|
2006-10-02 13:54:35 +02:00
|
|
|
else
|
|
|
|
{
|
2009-10-02 14:31:14 +02:00
|
|
|
memcpy (buf, dfx->defer, 22);
|
2000-07-14 19:34:53 +02:00
|
|
|
}
|
2009-10-02 14:31:14 +02:00
|
|
|
/* Fill up the buffer. */
|
|
|
|
if (dfx->partial)
|
2006-10-02 13:54:35 +02:00
|
|
|
{
|
2011-02-03 20:59:01 +01:00
|
|
|
for (; n < size; n++ )
|
2009-10-02 14:31:14 +02:00
|
|
|
{
|
|
|
|
if ( (c = iobuf_get(a)) == -1 )
|
|
|
|
{
|
|
|
|
dfx->eof_seen = 1; /* Normal EOF. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf[n] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-02-03 20:59:01 +01:00
|
|
|
for (; n < size && dfx->length; n++, dfx->length--)
|
2009-10-02 14:31:14 +02:00
|
|
|
{
|
|
|
|
c = iobuf_get(a);
|
|
|
|
if (c == -1)
|
|
|
|
{
|
|
|
|
dfx->eof_seen = 3; /* Premature EOF. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf[n] = c;
|
|
|
|
}
|
|
|
|
if (!dfx->length)
|
|
|
|
dfx->eof_seen = 1; /* Normal EOF. */
|
|
|
|
}
|
2011-02-03 20:59:01 +01:00
|
|
|
|
2009-10-02 14:31:14 +02:00
|
|
|
/* Move the trailing 22 bytes back to the defer buffer. We
|
|
|
|
have at least 44 bytes thus a memmove is not needed. */
|
2006-10-02 13:54:35 +02:00
|
|
|
n -= 22;
|
|
|
|
memcpy (dfx->defer, buf+n, 22 );
|
|
|
|
dfx->defer_filled = 1;
|
1999-05-17 22:03:24 +02:00
|
|
|
}
|
2006-10-02 13:54:35 +02:00
|
|
|
else if ( !dfx->defer_filled ) /* EOF seen but empty defer buffer. */
|
|
|
|
{
|
|
|
|
/* This is bad because it means an incomplete hash. */
|
|
|
|
n -= 22;
|
|
|
|
memcpy (buf, buf+22, n );
|
|
|
|
dfx->eof_seen = 2; /* EOF with incomplete hash. */
|
1999-05-17 22:03:24 +02:00
|
|
|
}
|
2006-10-02 13:54:35 +02:00
|
|
|
else /* EOF seen (i.e. read less than 22 bytes). */
|
|
|
|
{
|
|
|
|
memcpy (buf, dfx->defer, 22 );
|
|
|
|
n -= 22;
|
|
|
|
memcpy (dfx->defer, buf+n, 22 );
|
|
|
|
dfx->eof_seen = 1; /* Normal EOF. */
|
1999-05-17 22:03:24 +02:00
|
|
|
}
|
|
|
|
|
2006-10-02 13:54:35 +02:00
|
|
|
if ( n )
|
|
|
|
{
|
2006-12-06 11:16:50 +01:00
|
|
|
if ( dfx->cipher_hd )
|
|
|
|
gcry_cipher_decrypt (dfx->cipher_hd, buf, n, NULL, 0);
|
|
|
|
if ( dfx->mdc_hash )
|
|
|
|
gcry_md_write (dfx->mdc_hash, buf, n);
|
1999-05-17 22:03:24 +02:00
|
|
|
}
|
2006-10-02 13:54:35 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
assert ( dfx->eof_seen );
|
2009-10-02 14:31:14 +02:00
|
|
|
rc = -1; /* Return EOF. */
|
1999-05-17 22:03:24 +02:00
|
|
|
}
|
2006-10-02 13:54:35 +02:00
|
|
|
*ret_len = n;
|
1999-05-17 22:03:24 +02:00
|
|
|
}
|
2011-02-03 20:59:01 +01:00
|
|
|
else if ( control == IOBUFCTRL_FREE )
|
2006-12-06 11:16:50 +01:00
|
|
|
{
|
|
|
|
release_dfx_context (dfx);
|
|
|
|
}
|
2011-02-03 20:59:01 +01:00
|
|
|
else if ( control == IOBUFCTRL_DESC )
|
2006-10-02 13:54:35 +02:00
|
|
|
{
|
|
|
|
*(char**)buf = "mdc_decode_filter";
|
1999-05-17 22:03:24 +02:00
|
|
|
}
|
2006-10-02 13:54:35 +02:00
|
|
|
return rc;
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
|
|
|
|
2006-10-02 13:54:35 +02:00
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
static int
|
2006-04-19 13:26:11 +02:00
|
|
|
decode_filter( void *opaque, int control, IOBUF a, byte *buf, size_t *ret_len)
|
1997-11-18 15:06:00 +01:00
|
|
|
{
|
2006-12-06 11:16:50 +01:00
|
|
|
decode_filter_ctx_t fc = opaque;
|
2009-10-02 14:31:14 +02:00
|
|
|
size_t size = *ret_len;
|
|
|
|
size_t n;
|
|
|
|
int c, rc = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if ( control == IOBUFCTRL_UNDERFLOW && fc->eof_seen )
|
|
|
|
{
|
|
|
|
*ret_len = 0;
|
|
|
|
rc = -1;
|
|
|
|
}
|
2011-02-03 20:59:01 +01:00
|
|
|
else if ( control == IOBUFCTRL_UNDERFLOW )
|
2006-10-02 13:54:35 +02:00
|
|
|
{
|
|
|
|
assert(a);
|
2011-02-03 20:59:01 +01:00
|
|
|
|
2009-10-02 14:31:14 +02:00
|
|
|
if (fc->partial)
|
|
|
|
{
|
2011-02-03 20:59:01 +01:00
|
|
|
for (n=0; n < size; n++ )
|
2009-10-02 14:31:14 +02:00
|
|
|
{
|
|
|
|
c = iobuf_get(a);
|
|
|
|
if (c == -1)
|
|
|
|
{
|
|
|
|
fc->eof_seen = 1; /* Normal EOF. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf[n] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-02-03 20:59:01 +01:00
|
|
|
for (n=0; n < size && fc->length; n++, fc->length--)
|
2009-10-02 14:31:14 +02:00
|
|
|
{
|
|
|
|
c = iobuf_get(a);
|
|
|
|
if (c == -1)
|
|
|
|
{
|
|
|
|
fc->eof_seen = 3; /* Premature EOF. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf[n] = c;
|
|
|
|
}
|
|
|
|
if (!fc->length)
|
|
|
|
fc->eof_seen = 1; /* Normal EOF. */
|
|
|
|
}
|
|
|
|
if (n)
|
2006-12-06 11:16:50 +01:00
|
|
|
{
|
|
|
|
if (fc->cipher_hd)
|
|
|
|
gcry_cipher_decrypt (fc->cipher_hd, buf, n, NULL, 0);
|
|
|
|
}
|
2006-10-02 13:54:35 +02:00
|
|
|
else
|
2009-10-02 14:31:14 +02:00
|
|
|
{
|
|
|
|
if (!fc->eof_seen)
|
|
|
|
fc->eof_seen = 1;
|
|
|
|
rc = -1; /* Return EOF. */
|
|
|
|
}
|
2006-10-02 13:54:35 +02:00
|
|
|
*ret_len = n;
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
2011-02-03 20:59:01 +01:00
|
|
|
else if ( control == IOBUFCTRL_FREE )
|
2006-12-06 11:16:50 +01:00
|
|
|
{
|
|
|
|
release_dfx_context (fc);
|
|
|
|
}
|
2006-10-02 13:54:35 +02:00
|
|
|
else if ( control == IOBUFCTRL_DESC )
|
|
|
|
{
|
|
|
|
*(char**)buf = "decode_filter";
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
2006-10-02 13:54:35 +02:00
|
|
|
return rc;
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|