1997-11-18 15:06:00 +01:00
|
|
|
/* encr-data.c - process an encrypted data packet
|
2006-10-02 15:22:27 +02:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2005,
|
|
|
|
* 2006 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-10-23 12:48:09 +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-10-23 12:48:09 +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>
|
|
|
|
#include "util.h"
|
2002-06-29 15:46:34 +02:00
|
|
|
#include "memory.h"
|
1997-11-18 15:06:00 +01:00
|
|
|
#include "packet.h"
|
2002-06-29 15:46:34 +02:00
|
|
|
#include "mpi.h"
|
|
|
|
#include "cipher.h"
|
1998-04-04 22:16:55 +02:00
|
|
|
#include "options.h"
|
1998-09-14 17:49:56 +02:00
|
|
|
#include "i18n.h"
|
2012-01-13 16:20:53 +01:00
|
|
|
#include "status.h"
|
1997-11-18 15:06:00 +01:00
|
|
|
|
1999-05-17 22:03:24 +02:00
|
|
|
static int mdc_decode_filter( void *opaque, int control, IOBUF a,
|
2000-07-14 19:34:53 +02:00
|
|
|
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
|
|
|
|
|
|
|
typedef struct {
|
2002-06-29 15:46:34 +02:00
|
|
|
CIPHER_HANDLE cipher_hd;
|
|
|
|
MD_HANDLE mdc_hash;
|
2006-10-02 15:22:27 +02:00
|
|
|
char defer[22];
|
1999-05-17 22:03:24 +02:00
|
|
|
int defer_filled;
|
|
|
|
int eof_seen;
|
2006-12-06 10:52:40 +01:00
|
|
|
int refcount;
|
|
|
|
} *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 )
|
|
|
|
{
|
|
|
|
cipher_close (dfx->cipher_hd);
|
|
|
|
dfx->cipher_hd = NULL;
|
|
|
|
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
|
1999-04-26 17:53:01 +02:00
|
|
|
decrypt_data( void *procctx, PKT_encrypted *ed, DEK *dek )
|
1997-11-18 15:06:00 +01:00
|
|
|
{
|
|
|
|
decode_filter_ctx_t dfx;
|
|
|
|
byte *p;
|
1999-05-17 22:03:24 +02:00
|
|
|
int rc=0, c, i;
|
1998-07-29 21:35:05 +02:00
|
|
|
byte temp[32];
|
2002-06-29 15:46:34 +02:00
|
|
|
unsigned blocksize;
|
|
|
|
unsigned nprefix;
|
1997-11-18 15:06:00 +01:00
|
|
|
|
2006-12-06 10:52:40 +01:00
|
|
|
|
|
|
|
dfx = xcalloc (1, sizeof *dfx);
|
|
|
|
dfx->refcount = 1;
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
if( opt.verbose && !dek->algo_info_printed ) {
|
|
|
|
const char *s = cipher_algo_to_string( dek->algo );
|
|
|
|
if( s )
|
|
|
|
log_info(_("%s encrypted data\n"), s );
|
|
|
|
else
|
1998-11-10 13:59:59 +01:00
|
|
|
log_info(_("encrypted with unknown algorithm %d\n"), dek->algo );
|
2002-06-29 15:46:34 +02:00
|
|
|
dek->algo_info_printed = 1;
|
1999-09-18 12:17:16 +02:00
|
|
|
}
|
2012-01-13 16:20:53 +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 *buf = xmalloc (dek->keylen*2 + 20);
|
|
|
|
sprintf (buf, "%d:", dek->algo);
|
|
|
|
for (i=0; i < dek->keylen; i++ )
|
|
|
|
sprintf(buf+strlen(buf), "%02X", dek->key[i] );
|
|
|
|
log_info ("session key: `%s'\n", buf);
|
|
|
|
write_status_text (STATUS_SESSION_KEY, buf);
|
|
|
|
}
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
if( (rc=check_cipher_algo(dek->algo)) )
|
|
|
|
goto leave;
|
|
|
|
blocksize = cipher_get_blocksize(dek->algo);
|
2000-07-14 19:34:53 +02:00
|
|
|
if( !blocksize || blocksize > 16 )
|
1998-07-29 21:35:05 +02:00
|
|
|
log_fatal("unsupported blocksize %u\n", blocksize );
|
1999-04-09 12:34:44 +02:00
|
|
|
nprefix = blocksize;
|
1999-04-07 20:58:34 +02:00
|
|
|
if( ed->len && ed->len < (nprefix+2) )
|
|
|
|
BUG();
|
1999-10-26 14:14:37 +02:00
|
|
|
|
|
|
|
if( ed->mdc_method ) {
|
2006-12-06 10:52:40 +01:00
|
|
|
dfx->mdc_hash = md_open ( ed->mdc_method, 0 );
|
2000-07-14 19:34:53 +02:00
|
|
|
if ( DBG_HASHING )
|
2006-12-06 10:52:40 +01:00
|
|
|
md_start_debug (dfx->mdc_hash, "checkmdc");
|
1999-10-26 14:14:37 +02:00
|
|
|
}
|
2006-12-06 10:52:40 +01:00
|
|
|
dfx->cipher_hd = cipher_open ( dek->algo,
|
|
|
|
ed->mdc_method? CIPHER_MODE_CFB
|
|
|
|
: CIPHER_MODE_AUTO_CFB, 1 );
|
2004-10-08 23:54:26 +02:00
|
|
|
/* log_hexdump( "thekey", dek->key, dek->keylen );*/
|
2006-12-06 10:52:40 +01:00
|
|
|
rc = cipher_setkey ( dfx->cipher_hd, dek->key, dek->keylen );
|
2002-06-29 15:46:34 +02:00
|
|
|
if( rc == G10ERR_WEAK_KEY )
|
2004-10-08 23:54:26 +02:00
|
|
|
{
|
|
|
|
log_info(_("WARNING: message was encrypted with"
|
|
|
|
" a weak key in the symmetric cipher.\n"));
|
|
|
|
rc=0;
|
|
|
|
}
|
|
|
|
else if( rc )
|
|
|
|
{
|
2002-06-29 15:46:34 +02:00
|
|
|
log_error("key setup failed: %s\n", g10_errstr(rc) );
|
1999-05-17 22:03:24 +02:00
|
|
|
goto leave;
|
2012-01-13 16:20:53 +01:00
|
|
|
|
2006-06-25 14:03:38 +02:00
|
|
|
}
|
2002-06-29 15:46:34 +02:00
|
|
|
if (!ed->buf) {
|
|
|
|
log_error(_("problem handling encrypted packet\n"));
|
|
|
|
goto leave;
|
|
|
|
}
|
1998-09-14 17:49:56 +02:00
|
|
|
|
2006-12-06 10:52:40 +01:00
|
|
|
cipher_setiv ( dfx->cipher_hd, NULL, 0 );
|
1997-11-18 15:06:00 +01:00
|
|
|
|
|
|
|
if( ed->len ) {
|
1999-04-07 20:58:34 +02:00
|
|
|
for(i=0; i < (nprefix+2) && ed->len; i++, ed->len-- ) {
|
1999-01-09 16:06:59 +01:00
|
|
|
if( (c=iobuf_get(ed->buf)) == -1 )
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
temp[i] = c;
|
|
|
|
}
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
|
|
|
else {
|
1999-04-07 20:58:34 +02:00
|
|
|
for(i=0; i < (nprefix+2); i++ )
|
1997-11-18 15:06:00 +01:00
|
|
|
if( (c=iobuf_get(ed->buf)) == -1 )
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
temp[i] = c;
|
|
|
|
}
|
2006-12-06 10:52:40 +01:00
|
|
|
cipher_decrypt ( dfx->cipher_hd, temp, temp, nprefix+2);
|
|
|
|
cipher_sync ( dfx->cipher_hd );
|
1997-11-18 15:06:00 +01:00
|
|
|
p = temp;
|
1999-04-18 10:18:52 +02:00
|
|
|
/* log_hexdump( "prefix", temp, nprefix+2 ); */
|
2005-02-10 05:06:30 +01:00
|
|
|
if(dek->symmetric
|
|
|
|
&& (p[nprefix-2] != p[nprefix] || p[nprefix-1] != p[nprefix+1]) )
|
|
|
|
{
|
2002-06-29 15:46:34 +02:00
|
|
|
rc = G10ERR_BAD_KEY;
|
1999-05-17 22:03:24 +02:00
|
|
|
goto leave;
|
2005-02-10 05:06:30 +01:00
|
|
|
}
|
2000-07-14 19:34:53 +02:00
|
|
|
|
2006-12-06 10:52:40 +01:00
|
|
|
if ( dfx->mdc_hash )
|
|
|
|
md_write ( dfx->mdc_hash, temp, nprefix+2 );
|
2000-07-14 19:34:53 +02:00
|
|
|
|
2006-12-06 10:52:40 +01:00
|
|
|
dfx->refcount++;
|
|
|
|
if ( ed->mdc_method )
|
|
|
|
iobuf_push_filter( ed->buf, mdc_decode_filter, dfx );
|
1999-05-17 22:03:24 +02:00
|
|
|
else
|
2006-12-06 10:52:40 +01:00
|
|
|
iobuf_push_filter( ed->buf, decode_filter, dfx );
|
2000-07-14 19:34:53 +02:00
|
|
|
|
|
|
|
proc_packets( procctx, ed->buf );
|
1997-11-18 15:06:00 +01:00
|
|
|
ed->buf = NULL;
|
2006-12-06 10:52:40 +01:00
|
|
|
if( ed->mdc_method && dfx->eof_seen == 2 )
|
2002-06-29 15:46:34 +02:00
|
|
|
rc = G10ERR_INVALID_PACKET;
|
1999-05-17 22:03:24 +02:00
|
|
|
else if( ed->mdc_method ) { /* check the mdc */
|
2006-10-02 15:22:27 +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. */
|
2002-06-29 15:46:34 +02:00
|
|
|
int datalen = md_digest_length( ed->mdc_method );
|
2000-07-14 19:34:53 +02:00
|
|
|
|
2006-12-06 10:52:40 +01:00
|
|
|
assert (dfx->cipher_hd);
|
|
|
|
assert (dfx->mdc_hash);
|
|
|
|
cipher_decrypt ( dfx->cipher_hd, dfx->defer, dfx->defer, 22);
|
|
|
|
md_write ( dfx->mdc_hash, dfx->defer, 2);
|
|
|
|
md_final ( dfx->mdc_hash );
|
2014-06-23 13:24:43 +02:00
|
|
|
if ( dfx->defer[0] != '\xd3'
|
|
|
|
|| dfx->defer[1] != '\x14'
|
|
|
|
|| datalen != 20
|
|
|
|
|| memcmp (md_read (dfx->mdc_hash, 0 ), dfx->defer+2, datalen))
|
2002-06-29 15:46:34 +02:00
|
|
|
rc = G10ERR_BAD_SIGN;
|
2006-12-06 10:52:40 +01:00
|
|
|
/*log_hexdump("MDC calculated:",md_read( dfx->mdc_hash, 0), datalen);*/
|
|
|
|
/*log_hexdump("MDC message :", dfx->defer, 20);*/
|
1999-05-17 22:03:24 +02:00
|
|
|
}
|
2012-01-13 16:20:53 +01:00
|
|
|
|
2000-07-14 19:34:53 +02:00
|
|
|
|
1999-05-17 22:03:24 +02:00
|
|
|
leave:
|
2006-12-06 10:52:40 +01:00
|
|
|
release_dfx_context (dfx);
|
1999-05-17 22:03:24 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
1999-10-26 14:14:37 +02:00
|
|
|
|
2000-07-14 19:34:53 +02:00
|
|
|
|
1999-05-17 22:03:24 +02:00
|
|
|
/* I think we should merge this with cipher_filter */
|
|
|
|
static int
|
|
|
|
mdc_decode_filter( void *opaque, int control, IOBUF a,
|
|
|
|
byte *buf, size_t *ret_len)
|
|
|
|
{
|
2006-12-06 10:52:40 +01:00
|
|
|
decode_filter_ctx_t dfx = opaque;
|
1999-05-17 22:03:24 +02:00
|
|
|
size_t n, size = *ret_len;
|
|
|
|
int rc = 0;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if( control == IOBUFCTRL_UNDERFLOW && dfx->eof_seen ) {
|
|
|
|
*ret_len = 0;
|
|
|
|
rc = -1;
|
|
|
|
}
|
|
|
|
else if( control == IOBUFCTRL_UNDERFLOW ) {
|
|
|
|
assert(a);
|
2006-10-02 15:22:27 +02:00
|
|
|
assert( size > 44 );
|
1999-05-17 22:03:24 +02:00
|
|
|
|
|
|
|
/* get at least 20 bytes and put it somewhere ahead in the buffer */
|
2006-10-02 15:22:27 +02:00
|
|
|
for(n=22; n < 44 ; n++ ) {
|
1999-05-17 22:03:24 +02:00
|
|
|
if( (c = iobuf_get(a)) == -1 )
|
|
|
|
break;
|
|
|
|
buf[n] = c;
|
|
|
|
}
|
2006-10-02 15:22:27 +02:00
|
|
|
if( n == 44 ) {
|
1999-05-17 22:03:24 +02:00
|
|
|
/* we have enough stuff - flush the deferred stuff */
|
2000-07-14 19:34:53 +02:00
|
|
|
/* (we have asserted that the buffer is large enough) */
|
|
|
|
if( !dfx->defer_filled ) { /* the first time */
|
2006-10-02 15:22:27 +02:00
|
|
|
memcpy(buf, buf+22, 22 );
|
|
|
|
n = 22;
|
2000-07-14 19:34:53 +02:00
|
|
|
}
|
|
|
|
else {
|
2006-10-02 15:22:27 +02:00
|
|
|
memcpy(buf, dfx->defer, 22 );
|
2000-07-14 19:34:53 +02:00
|
|
|
}
|
1999-05-17 22:03:24 +02:00
|
|
|
/* now fill up */
|
|
|
|
for(; n < size; n++ ) {
|
|
|
|
if( (c = iobuf_get(a)) == -1 )
|
|
|
|
break;
|
|
|
|
buf[n] = c;
|
|
|
|
}
|
2006-10-02 15:22:27 +02:00
|
|
|
/* Move the last 22 bytes back to the defer buffer. */
|
|
|
|
/* (okay, we are wasting 22 bytes of supplied buffer) */
|
|
|
|
n -= 22;
|
|
|
|
memcpy( dfx->defer, buf+n, 22 );
|
1999-05-17 22:03:24 +02:00
|
|
|
dfx->defer_filled = 1;
|
|
|
|
}
|
|
|
|
else if( !dfx->defer_filled ) { /* eof seen buf empty defer */
|
2000-07-14 19:34:53 +02:00
|
|
|
/* this is bad because there is an incomplete hash */
|
2006-10-02 15:22:27 +02:00
|
|
|
n -= 22;
|
|
|
|
memcpy(buf, buf+22, n );
|
1999-05-17 22:03:24 +02:00
|
|
|
dfx->eof_seen = 2; /* eof with incomplete hash */
|
|
|
|
}
|
|
|
|
else { /* eof seen */
|
2006-10-02 15:22:27 +02:00
|
|
|
memcpy (buf, dfx->defer, 22 );
|
|
|
|
n -= 22;
|
|
|
|
memcpy( dfx->defer, buf+n, 22 );
|
1999-05-17 22:03:24 +02:00
|
|
|
dfx->eof_seen = 1; /* normal eof */
|
|
|
|
}
|
|
|
|
|
|
|
|
if( n ) {
|
2006-12-06 10:52:40 +01:00
|
|
|
if (dfx->cipher_hd)
|
|
|
|
cipher_decrypt( dfx->cipher_hd, buf, buf, n);
|
|
|
|
if (dfx->mdc_hash)
|
|
|
|
md_write( dfx->mdc_hash, buf, n );
|
1999-05-17 22:03:24 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert( dfx->eof_seen );
|
|
|
|
rc = -1; /* eof */
|
|
|
|
}
|
|
|
|
*ret_len = n;
|
|
|
|
}
|
2006-12-06 10:52:40 +01:00
|
|
|
else if ( control == IOBUFCTRL_FREE ) {
|
|
|
|
release_dfx_context (dfx);
|
|
|
|
}
|
1999-05-17 22:03:24 +02:00
|
|
|
else if( control == IOBUFCTRL_DESC ) {
|
|
|
|
*(char**)buf = "mdc_decode_filter";
|
|
|
|
}
|
|
|
|
return rc;
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
decode_filter( void *opaque, int control, IOBUF a, byte *buf, size_t *ret_len)
|
|
|
|
{
|
2006-12-06 10:52:40 +01:00
|
|
|
decode_filter_ctx_t fc = opaque;
|
1997-11-18 15:06:00 +01:00
|
|
|
size_t n, size = *ret_len;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
if( control == IOBUFCTRL_UNDERFLOW ) {
|
|
|
|
assert(a);
|
1999-05-31 19:49:37 +02:00
|
|
|
n = iobuf_read( a, buf, size );
|
2010-10-20 08:51:11 +02:00
|
|
|
if (n == (size_t)(-1))
|
|
|
|
n = 0;
|
2006-12-06 10:52:40 +01:00
|
|
|
if( n ) {
|
|
|
|
if (fc->cipher_hd)
|
|
|
|
cipher_decrypt( fc->cipher_hd, buf, buf, n);
|
|
|
|
}
|
1997-11-18 15:06:00 +01:00
|
|
|
else
|
|
|
|
rc = -1; /* eof */
|
|
|
|
*ret_len = n;
|
|
|
|
}
|
2006-12-06 10:52:40 +01:00
|
|
|
else if ( control == IOBUFCTRL_FREE ) {
|
|
|
|
release_dfx_context (fc);
|
|
|
|
}
|
1997-11-18 15:06:00 +01:00
|
|
|
else if( control == IOBUFCTRL_DESC ) {
|
|
|
|
*(char**)buf = "decode_filter";
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|