2009-10-02 14:57:55 +00:00
|
|
|
/* decrypt-data.c - Decrypt an encrypted data packet
|
2018-01-21 16:24:43 +01:00
|
|
|
* Copyright (C) 1998-2001, 2005-2006, 2009 Free Software Foundation, Inc.
|
|
|
|
* Copyright (C) 1998-2001, 2005-2006, 2009, 2018 Werner Koch
|
1997-11-18 14:06:00 +00:00
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* This file is part of GnuPG.
|
1997-11-18 14:06:00 +00:00
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
1997-11-18 14:06:00 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 19:49:40 +00:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
1997-11-18 14:06:00 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
1997-11-18 14:06:00 +00: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
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2020-04-15 22:23:10 +02:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
1997-11-18 14:06:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2003-06-18 19:56:13 +00:00
|
|
|
|
|
|
|
#include "gpg.h"
|
2017-03-07 20:21:23 +09:00
|
|
|
#include "../common/util.h"
|
1997-11-18 14:06:00 +00:00
|
|
|
#include "packet.h"
|
1998-04-04 20:16:55 +00:00
|
|
|
#include "options.h"
|
2017-03-07 20:21:23 +09:00
|
|
|
#include "../common/i18n.h"
|
|
|
|
#include "../common/status.h"
|
2017-06-06 16:01:40 +02:00
|
|
|
#include "../common/compliance.h"
|
1997-11-18 14:06:00 +00:00
|
|
|
|
|
|
|
|
2018-01-21 16:24:43 +01:00
|
|
|
static int aead_decode_filter (void *opaque, int control, iobuf_t a,
|
|
|
|
byte *buf, size_t *ret_len);
|
2006-10-02 11:54:35 +00: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 20:03:24 +00:00
|
|
|
byte *buf, size_t *ret_len);
|
1997-11-18 14:06:00 +00:00
|
|
|
|
2018-01-21 16:24:43 +01:00
|
|
|
/* Our context object. */
|
|
|
|
struct decode_filter_context_s
|
2006-04-19 11:26:11 +00:00
|
|
|
{
|
2020-04-15 22:23:10 +02:00
|
|
|
/* Redcounter (max value is 2). We need it because we do not know
|
2018-01-21 16:24:43 +01:00
|
|
|
* whether the iobuf or the outer control code frees this object
|
|
|
|
* first. */
|
|
|
|
int refcount;
|
|
|
|
|
|
|
|
/* The cipher handle. */
|
2006-04-19 11:26:11 +00:00
|
|
|
gcry_cipher_hd_t cipher_hd;
|
2018-01-21 16:24:43 +01:00
|
|
|
|
|
|
|
/* The hash handle for use in MDC mode. */
|
2006-04-19 11:26:11 +00:00
|
|
|
gcry_md_hd_t mdc_hash;
|
2018-01-21 16:24:43 +01:00
|
|
|
|
|
|
|
/* The start IV for AEAD encryption. */
|
|
|
|
byte startiv[16];
|
|
|
|
|
2018-01-24 13:45:05 +01:00
|
|
|
/* The holdback buffer and its used length. For AEAD we need 32+1
|
2020-04-15 22:23:10 +02:00
|
|
|
* bytes but we use 48 byte. For MDC we need 22 bytes; here
|
|
|
|
* holdbacklen will either 0 or 22. */
|
2018-01-24 08:59:06 +01:00
|
|
|
char holdback[48];
|
|
|
|
unsigned int holdbacklen;
|
2018-01-21 16:24:43 +01:00
|
|
|
|
|
|
|
/* Working on a partial length packet. */
|
|
|
|
unsigned int partial : 1;
|
|
|
|
|
|
|
|
/* EOF indicator with these true values:
|
|
|
|
* 1 = normal EOF
|
2020-04-15 22:23:10 +02:00
|
|
|
* 2 = premature EOF (tag or hash incomplete)
|
2018-01-21 16:24:43 +01:00
|
|
|
* 3 = premature EOF (general) */
|
|
|
|
unsigned int eof_seen : 2;
|
|
|
|
|
2024-09-19 10:00:24 +02:00
|
|
|
/* Flag to convey an error from aead_checktag. */
|
|
|
|
unsigned int checktag_failed : 1;
|
|
|
|
|
2018-01-21 16:24:43 +01:00
|
|
|
/* The actually used cipher algo for AEAD. */
|
|
|
|
byte cipher_algo;
|
|
|
|
|
|
|
|
/* The AEAD algo. */
|
|
|
|
byte aead_algo;
|
|
|
|
|
|
|
|
/* The encoded chunk byte for AEAD. */
|
|
|
|
byte chunkbyte;
|
|
|
|
|
|
|
|
/* The decoded CHUNKBYTE. */
|
|
|
|
uint64_t chunksize;
|
|
|
|
|
|
|
|
/* The chunk index for AEAD. */
|
|
|
|
uint64_t chunkindex;
|
|
|
|
|
|
|
|
/* The number of bytes in the current chunk. */
|
|
|
|
uint64_t chunklen;
|
|
|
|
|
|
|
|
/* The total count of decrypted plaintext octets. */
|
|
|
|
uint64_t total;
|
|
|
|
|
|
|
|
/* Remaining bytes in the packet according to the packet header.
|
|
|
|
* Not used if PARTIAL is true. */
|
|
|
|
size_t length;
|
|
|
|
};
|
|
|
|
typedef struct decode_filter_context_s *decode_filter_ctx_t;
|
2006-12-06 10:16:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Helper to release the decode context. */
|
|
|
|
static void
|
|
|
|
release_dfx_context (decode_filter_ctx_t dfx)
|
|
|
|
{
|
|
|
|
if (!dfx)
|
|
|
|
return;
|
|
|
|
|
2016-04-29 11:05:24 +02:00
|
|
|
log_assert (dfx->refcount);
|
2006-12-06 10:16:50 +00:00
|
|
|
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 14:06:00 +00:00
|
|
|
|
2018-02-27 21:11:20 +01:00
|
|
|
/* Set the nonce and the additional data for the current chunk. This
|
2020-04-15 22:23:10 +02:00
|
|
|
* also reset the decryption machinery so that the handle can be
|
2018-02-27 21:11:20 +01:00
|
|
|
* used for a new chunk. If FINAL is set the final AEAD chunk is
|
|
|
|
* processed. */
|
2018-01-21 16:24:43 +01:00
|
|
|
static gpg_error_t
|
2018-02-27 21:11:20 +01:00
|
|
|
aead_set_nonce_and_ad (decode_filter_ctx_t dfx, int final)
|
2018-01-21 16:24:43 +01:00
|
|
|
{
|
2018-02-27 21:11:20 +01:00
|
|
|
gpg_error_t err;
|
|
|
|
unsigned char ad[21];
|
2018-01-21 16:24:43 +01:00
|
|
|
unsigned char nonce[16];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
switch (dfx->aead_algo)
|
|
|
|
{
|
|
|
|
case AEAD_ALGO_OCB:
|
|
|
|
memcpy (nonce, dfx->startiv, 15);
|
|
|
|
i = 7;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AEAD_ALGO_EAX:
|
|
|
|
memcpy (nonce, dfx->startiv, 16);
|
|
|
|
i = 8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
BUG ();
|
|
|
|
}
|
|
|
|
nonce[i++] ^= dfx->chunkindex >> 56;
|
|
|
|
nonce[i++] ^= dfx->chunkindex >> 48;
|
|
|
|
nonce[i++] ^= dfx->chunkindex >> 40;
|
|
|
|
nonce[i++] ^= dfx->chunkindex >> 32;
|
|
|
|
nonce[i++] ^= dfx->chunkindex >> 24;
|
|
|
|
nonce[i++] ^= dfx->chunkindex >> 16;
|
|
|
|
nonce[i++] ^= dfx->chunkindex >> 8;
|
|
|
|
nonce[i++] ^= dfx->chunkindex;
|
|
|
|
|
2018-01-23 19:08:16 +01:00
|
|
|
if (DBG_CRYPTO)
|
|
|
|
log_printhex (nonce, i, "nonce:");
|
2018-02-27 21:11:20 +01:00
|
|
|
err = gcry_cipher_setiv (dfx->cipher_hd, nonce, i);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2018-01-21 16:24:43 +01:00
|
|
|
|
|
|
|
ad[0] = (0xc0 | PKT_ENCRYPTED_AEAD);
|
|
|
|
ad[1] = 1;
|
|
|
|
ad[2] = dfx->cipher_algo;
|
|
|
|
ad[3] = dfx->aead_algo;
|
|
|
|
ad[4] = dfx->chunkbyte;
|
|
|
|
ad[5] = dfx->chunkindex >> 56;
|
|
|
|
ad[6] = dfx->chunkindex >> 48;
|
|
|
|
ad[7] = dfx->chunkindex >> 40;
|
|
|
|
ad[8] = dfx->chunkindex >> 32;
|
|
|
|
ad[9] = dfx->chunkindex >> 24;
|
|
|
|
ad[10]= dfx->chunkindex >> 16;
|
|
|
|
ad[11]= dfx->chunkindex >> 8;
|
|
|
|
ad[12]= dfx->chunkindex;
|
|
|
|
if (final)
|
|
|
|
{
|
|
|
|
ad[13] = dfx->total >> 56;
|
|
|
|
ad[14] = dfx->total >> 48;
|
|
|
|
ad[15] = dfx->total >> 40;
|
|
|
|
ad[16] = dfx->total >> 32;
|
|
|
|
ad[17] = dfx->total >> 24;
|
|
|
|
ad[18] = dfx->total >> 16;
|
|
|
|
ad[19] = dfx->total >> 8;
|
|
|
|
ad[20] = dfx->total;
|
|
|
|
}
|
2018-01-23 19:08:16 +01:00
|
|
|
if (DBG_CRYPTO)
|
|
|
|
log_printhex (ad, final? 21 : 13, "authdata:");
|
2018-01-21 16:24:43 +01:00
|
|
|
return gcry_cipher_authenticate (dfx->cipher_hd, ad, final? 21 : 13);
|
|
|
|
}
|
|
|
|
|
1997-11-18 14:06:00 +00:00
|
|
|
|
2018-02-27 19:50:54 +01:00
|
|
|
/* Helper to check the 16 byte tag in TAGBUF. The FINAL flag is only
|
|
|
|
* for debug messages. */
|
|
|
|
static gpg_error_t
|
|
|
|
aead_checktag (decode_filter_ctx_t dfx, int final, const void *tagbuf)
|
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
|
|
|
|
if (DBG_FILTER)
|
|
|
|
log_printhex (tagbuf, 16, "tag:");
|
|
|
|
err = gcry_cipher_checktag (dfx->cipher_hd, tagbuf, 16);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
log_error ("gcry_cipher_checktag%s failed: %s\n",
|
|
|
|
final? " (final)":"", gpg_strerror (err));
|
2024-03-14 21:41:15 +01:00
|
|
|
write_status_error ("aead_checktag", err);
|
2024-09-19 10:00:24 +02:00
|
|
|
dfx->checktag_failed = 1;
|
2018-02-27 19:50:54 +01:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
if (DBG_FILTER)
|
|
|
|
log_debug ("%stag is valid\n", final?"final ":"");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-11-18 14:06:00 +00:00
|
|
|
/****************
|
2022-03-18 11:13:23 +01:00
|
|
|
* Decrypt the data, specified by ED with the key DEK. On return
|
|
|
|
* COMPLIANCE_ERROR is set to true iff the decryption can claim that
|
|
|
|
* it was compliant in the current mode; otherwise this flag is set to
|
|
|
|
* false.
|
1997-11-18 14:06:00 +00:00
|
|
|
*/
|
|
|
|
int
|
2022-03-18 11:13:23 +01:00
|
|
|
decrypt_data (ctrl_t ctrl, void *procctx, PKT_encrypted *ed, DEK *dek,
|
|
|
|
int *compliance_error)
|
1997-11-18 14:06:00 +00:00
|
|
|
{
|
2006-10-02 11:54:35 +00:00
|
|
|
decode_filter_ctx_t dfx;
|
2020-04-16 08:52:29 +02:00
|
|
|
enum gcry_cipher_modes ciphermode;
|
|
|
|
unsigned int startivlen;
|
2006-10-02 11:54:35 +00:00
|
|
|
byte *p;
|
|
|
|
int rc=0, c, i;
|
|
|
|
byte temp[32];
|
2018-01-21 16:24:43 +01:00
|
|
|
unsigned int blocksize;
|
|
|
|
unsigned int nprefix;
|
2011-02-03 20:59:01 +01:00
|
|
|
|
2022-03-18 11:13:23 +01:00
|
|
|
*compliance_error = 0;
|
|
|
|
|
2006-12-06 10:16:50 +00:00
|
|
|
dfx = xtrycalloc (1, sizeof *dfx);
|
|
|
|
if (!dfx)
|
|
|
|
return gpg_error_from_syserror ();
|
|
|
|
dfx->refcount = 1;
|
|
|
|
|
2006-10-02 11:54:35 +00:00
|
|
|
if ( opt.verbose && !dek->algo_info_printed )
|
|
|
|
{
|
2007-12-12 10:28:30 +00:00
|
|
|
if (!openpgp_cipher_test_algo (dek->algo))
|
2022-03-18 13:50:18 +01:00
|
|
|
log_info (_("%s encrypted data\n"),
|
|
|
|
openpgp_cipher_algo_mode_name (dek->algo, ed->aead_algo));
|
2006-10-02 11:54:35 +00:00
|
|
|
else
|
2007-02-26 20:24:29 +00:00
|
|
|
log_info (_("encrypted with unknown algorithm %d\n"), dek->algo );
|
2006-10-02 11:54:35 +00:00
|
|
|
dek->algo_info_printed = 1;
|
|
|
|
}
|
2011-02-03 20:59:01 +01:00
|
|
|
|
2020-04-16 08:52:29 +02:00
|
|
|
if (ed->aead_algo)
|
|
|
|
{
|
|
|
|
rc = openpgp_aead_algo_info (ed->aead_algo, &ciphermode, &startivlen);
|
|
|
|
if (rc)
|
|
|
|
goto leave;
|
|
|
|
log_assert (startivlen <= sizeof dfx->startiv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ciphermode = GCRY_CIPHER_MODE_CFB;
|
|
|
|
|
2017-06-06 16:01:40 +02:00
|
|
|
/* Check compliance. */
|
2020-04-16 08:52:29 +02:00
|
|
|
if (!gnupg_cipher_is_allowed (opt.compliance, 0, dek->algo, ciphermode))
|
2017-06-06 16:01:40 +02:00
|
|
|
{
|
2024-06-24 16:31:24 +02:00
|
|
|
gpgrt_log (opt.show_only_session_key? GPGRT_LOGLVL_INFO
|
|
|
|
/* */: GPGRT_LOGLVL_ERROR,
|
|
|
|
_("cipher algorithm '%s' may not be used in %s mode\n"),
|
2022-03-18 13:50:18 +01:00
|
|
|
openpgp_cipher_algo_mode_name (dek->algo,ed->aead_algo),
|
2017-06-06 16:01:40 +02:00
|
|
|
gnupg_compliance_option_string (opt.compliance));
|
2022-03-18 11:13:23 +01:00
|
|
|
*compliance_error = 1;
|
2024-06-24 16:31:24 +02:00
|
|
|
if (opt.flags.require_compliance && !opt.show_only_session_key)
|
2022-03-18 11:13:23 +01:00
|
|
|
{
|
|
|
|
/* We fail early in this case because it does not make sense
|
|
|
|
* to first decrypt everything. */
|
|
|
|
rc = gpg_error (GPG_ERR_CIPHER_ALGO);
|
|
|
|
goto leave;
|
|
|
|
}
|
2017-06-06 16:01:40 +02:00
|
|
|
}
|
|
|
|
|
2024-06-24 16:31:24 +02:00
|
|
|
write_status_printf (STATUS_DECRYPTION_INFO, "%d %d %d %d",
|
|
|
|
ed->mdc_method, dek->algo, ed->aead_algo,
|
|
|
|
*compliance_error);
|
2011-02-03 20:59:01 +01:00
|
|
|
|
2013-12-11 10:20:15 +01:00
|
|
|
if (opt.show_session_key)
|
|
|
|
{
|
2018-01-21 16:24:43 +01:00
|
|
|
char numbuf[30];
|
2013-12-11 10:20:15 +01:00
|
|
|
char *hexbuf;
|
|
|
|
|
2018-01-21 16:24:43 +01:00
|
|
|
if (ed->aead_algo)
|
|
|
|
snprintf (numbuf, sizeof numbuf, "%d.%u:", dek->algo, ed->aead_algo);
|
|
|
|
else
|
|
|
|
snprintf (numbuf, sizeof numbuf, "%d:", dek->algo);
|
2013-12-11 10:20:15 +01:00
|
|
|
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);
|
2024-06-24 16:31:24 +02:00
|
|
|
if (opt.show_only_session_key)
|
|
|
|
{
|
|
|
|
rc = 0;
|
|
|
|
goto leave;
|
|
|
|
}
|
2013-12-11 10:20:15 +01:00
|
|
|
}
|
|
|
|
|
2006-10-02 11:54:35 +00: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 14:11:03 +00:00
|
|
|
blocksize = openpgp_cipher_get_algo_blklen (dek->algo);
|
2006-10-02 11:54:35 +00:00
|
|
|
if ( !blocksize || blocksize > 16 )
|
2006-12-06 10:16:50 +00:00
|
|
|
log_fatal ("unsupported blocksize %u\n", blocksize );
|
2020-04-15 22:23:10 +02:00
|
|
|
|
2018-01-21 16:24:43 +01:00
|
|
|
if (ed->aead_algo)
|
2014-11-26 10:21:01 +01:00
|
|
|
{
|
2018-01-21 16:24:43 +01:00
|
|
|
if (blocksize != 16)
|
|
|
|
{
|
|
|
|
rc = gpg_error (GPG_ERR_CIPHER_ALGO);
|
|
|
|
goto leave;
|
|
|
|
}
|
1999-10-26 12:14:37 +00:00
|
|
|
|
2018-01-22 16:23:02 +01:00
|
|
|
if (ed->chunkbyte > 56)
|
2018-01-21 16:24:43 +01:00
|
|
|
{
|
2018-01-22 16:23:02 +01:00
|
|
|
log_error ("invalid AEAD chunkbyte %u\n", ed->chunkbyte);
|
|
|
|
rc = gpg_error (GPG_ERR_INV_PACKET);
|
2018-01-21 16:24:43 +01:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the Start-IV. */
|
|
|
|
if (ed->len)
|
|
|
|
{
|
|
|
|
for (i=0; i < startivlen && ed->len; i++, ed->len--)
|
|
|
|
{
|
|
|
|
if ((c=iobuf_get (ed->buf)) == -1)
|
|
|
|
break;
|
|
|
|
dfx->startiv[i] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i=0; i < startivlen; i++ )
|
|
|
|
if ( (c=iobuf_get (ed->buf)) == -1 )
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
dfx->startiv[i] = c;
|
|
|
|
}
|
|
|
|
if (i != startivlen)
|
|
|
|
{
|
|
|
|
log_error ("Start-IV in AEAD packet too short (%d/%u)\n",
|
|
|
|
i, startivlen);
|
|
|
|
rc = gpg_error (GPG_ERR_TOO_SHORT);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
dfx->cipher_algo = ed->cipher_algo;
|
|
|
|
dfx->aead_algo = ed->aead_algo;
|
|
|
|
dfx->chunkbyte = ed->chunkbyte;
|
|
|
|
dfx->chunksize = (uint64_t)1 << (dfx->chunkbyte + 6);
|
|
|
|
|
|
|
|
if (dek->algo != dfx->cipher_algo)
|
|
|
|
log_info ("Note: different cipher algorithms used (%s/%s)\n",
|
|
|
|
openpgp_cipher_algo_name (dek->algo),
|
|
|
|
openpgp_cipher_algo_name (dfx->cipher_algo));
|
|
|
|
|
|
|
|
rc = openpgp_cipher_open (&dfx->cipher_hd,
|
|
|
|
dfx->cipher_algo,
|
|
|
|
ciphermode,
|
|
|
|
GCRY_CIPHER_SECURE);
|
|
|
|
if (rc)
|
|
|
|
goto leave; /* Should never happen. */
|
|
|
|
|
2018-01-23 19:08:16 +01:00
|
|
|
if (DBG_CRYPTO)
|
|
|
|
log_printhex (dek->key, dek->keylen, "thekey:");
|
2018-01-21 16:24:43 +01:00
|
|
|
rc = gcry_cipher_setkey (dfx->cipher_hd, dek->key, dek->keylen);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
log_error("key setup failed: %s\n", gpg_strerror (rc));
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ed->buf)
|
|
|
|
{
|
|
|
|
log_error(_("problem handling encrypted packet\n"));
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2006-10-02 11:54:35 +00:00
|
|
|
}
|
2018-01-21 16:24:43 +01:00
|
|
|
else /* CFB encryption. */
|
2006-10-02 11:54:35 +00:00
|
|
|
{
|
2018-01-21 16:24:43 +01:00
|
|
|
nprefix = blocksize;
|
|
|
|
if ( ed->len && ed->len < (nprefix+2) )
|
|
|
|
{
|
|
|
|
/* An invalid message. We can't check that during parsing
|
2020-04-15 22:23:10 +02:00
|
|
|
* because we may not know the used cipher then. */
|
2018-01-21 16:24:43 +01:00
|
|
|
rc = gpg_error (GPG_ERR_INV_PACKET);
|
|
|
|
goto leave;
|
|
|
|
}
|
2006-04-19 11:26:11 +00:00
|
|
|
|
2018-01-21 16:24:43 +01:00
|
|
|
if ( ed->mdc_method )
|
|
|
|
{
|
|
|
|
if (gcry_md_open (&dfx->mdc_hash, ed->mdc_method, 0 ))
|
|
|
|
BUG ();
|
|
|
|
if ( DBG_HASHING )
|
|
|
|
gcry_md_debug (dfx->mdc_hash, "checkmdc");
|
|
|
|
}
|
1998-09-14 15:49:56 +00:00
|
|
|
|
2018-01-21 16:24:43 +01: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)));
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
/* We should never get an error here cause we already checked
|
|
|
|
* that the algorithm is available. */
|
|
|
|
BUG();
|
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
|
2018-01-21 16:24:43 +01:00
|
|
|
|
|
|
|
/* log_hexdump( "thekey", dek->key, dek->keylen );*/
|
|
|
|
rc = gcry_cipher_setkey (dfx->cipher_hd, dek->key, dek->keylen);
|
|
|
|
if ( gpg_err_code (rc) == GPG_ERR_WEAK_KEY )
|
2006-10-02 11:54:35 +00:00
|
|
|
{
|
2020-04-15 22:23:10 +02:00
|
|
|
log_info (_("WARNING: message was encrypted with"
|
|
|
|
" a weak key in the symmetric cipher.\n"));
|
2018-01-21 16:24:43 +01:00
|
|
|
rc=0;
|
|
|
|
}
|
2020-04-15 22:23:10 +02:00
|
|
|
else if (rc)
|
2018-01-21 16:24:43 +01:00
|
|
|
{
|
2020-04-15 22:23:10 +02:00
|
|
|
log_error ("key setup failed: %s\n", gpg_strerror (rc) );
|
2018-01-21 16:24:43 +01:00
|
|
|
goto leave;
|
2006-10-02 11:54:35 +00:00
|
|
|
}
|
2011-02-03 20:59:01 +01:00
|
|
|
|
2018-01-21 16:24:43 +01:00
|
|
|
if (!ed->buf)
|
|
|
|
{
|
2020-04-15 22:23:10 +02:00
|
|
|
log_error (_("problem handling encrypted packet\n"));
|
2022-03-18 11:13:23 +01:00
|
|
|
rc = gpg_error (GPG_ERR_INV_PACKET);
|
2018-01-21 16:24:43 +01:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
gcry_cipher_setiv (dfx->cipher_hd, NULL, 0);
|
|
|
|
|
|
|
|
if ( ed->len )
|
|
|
|
{
|
|
|
|
for (i=0; i < (nprefix+2) && ed->len; i++, ed->len-- )
|
|
|
|
{
|
|
|
|
if ( (c=iobuf_get(ed->buf)) == -1 )
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
temp[i] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i=0; i < (nprefix+2); i++ )
|
|
|
|
if ( (c=iobuf_get(ed->buf)) == -1 )
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
temp[i] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
gcry_cipher_decrypt (dfx->cipher_hd, temp, nprefix+2, NULL, 0);
|
|
|
|
gcry_cipher_sync (dfx->cipher_hd);
|
|
|
|
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
|
|
|
|
2018-01-21 16:24:43 +01:00
|
|
|
if ( dfx->mdc_hash )
|
|
|
|
gcry_md_write (dfx->mdc_hash, temp, nprefix+2);
|
|
|
|
}
|
2006-12-06 10:16:50 +00:00
|
|
|
|
|
|
|
dfx->refcount++;
|
2018-01-21 16:24:43 +01:00
|
|
|
dfx->partial = !!ed->is_partial;
|
2009-10-02 12:31:14 +00:00
|
|
|
dfx->length = ed->len;
|
2024-09-19 10:00:24 +02:00
|
|
|
dfx->checktag_failed = 0;
|
2018-01-21 16:24:43 +01:00
|
|
|
if (ed->aead_algo)
|
|
|
|
iobuf_push_filter ( ed->buf, aead_decode_filter, dfx );
|
|
|
|
else if (ed->mdc_method)
|
2006-12-06 10:16:50 +00:00
|
|
|
iobuf_push_filter ( ed->buf, mdc_decode_filter, dfx );
|
2006-10-02 11:54:35 +00:00
|
|
|
else
|
2006-12-06 10:16:50 +00:00
|
|
|
iobuf_push_filter ( ed->buf, decode_filter, dfx );
|
2006-04-19 11:26:11 +00:00
|
|
|
|
2015-11-05 12:20:42 +01:00
|
|
|
if (opt.unwrap_encryption)
|
|
|
|
{
|
2017-03-28 12:10:28 +02:00
|
|
|
char *filename = NULL;
|
2015-11-05 12:20:42 +01:00
|
|
|
estream_t fp;
|
2019-06-21 10:09:52 +02:00
|
|
|
|
2015-11-05 12:20:42 +01:00
|
|
|
rc = get_output_file ("", 0, ed->buf, &filename, &fp);
|
|
|
|
if (! rc)
|
|
|
|
{
|
2019-09-10 15:45:58 +02:00
|
|
|
iobuf_t output = iobuf_esopen (fp, "w", 0, 0);
|
2015-11-05 12:20:42 +01:00
|
|
|
armor_filter_context_t *afx = NULL;
|
|
|
|
|
2022-02-13 15:13:28 +02:00
|
|
|
es_setbuf (fp, NULL);
|
|
|
|
|
2015-11-05 12:20:42 +01:00
|
|
|
if (opt.armor)
|
|
|
|
{
|
|
|
|
afx = new_armor_context ();
|
|
|
|
push_armor_filter (afx, output);
|
|
|
|
}
|
|
|
|
|
|
|
|
iobuf_copy (output, ed->buf);
|
|
|
|
if ((rc = iobuf_error (ed->buf)))
|
2015-11-06 13:29:01 +01:00
|
|
|
log_error (_("error reading '%s': %s\n"),
|
2015-11-05 12:20:42 +01:00
|
|
|
filename, gpg_strerror (rc));
|
|
|
|
else if ((rc = iobuf_error (output)))
|
2015-11-06 13:29:01 +01:00
|
|
|
log_error (_("error writing '%s': %s\n"),
|
2015-11-05 12:20:42 +01:00
|
|
|
filename, gpg_strerror (rc));
|
|
|
|
|
|
|
|
iobuf_close (output);
|
2019-06-21 10:09:52 +02:00
|
|
|
release_armor_context (afx);
|
2015-11-05 12:20:42 +01:00
|
|
|
}
|
2017-03-28 12:10:28 +02:00
|
|
|
xfree (filename);
|
2015-11-05 12:20:42 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
proc_packets (ctrl, procctx, ed->buf );
|
|
|
|
|
2006-10-02 11:54:35 +00:00
|
|
|
ed->buf = NULL;
|
2009-10-02 12:31:14 +00:00
|
|
|
if (dfx->eof_seen > 1 )
|
2006-10-02 11:54:35 +00:00
|
|
|
rc = gpg_error (GPG_ERR_INV_PACKET);
|
2024-09-19 10:00:24 +02:00
|
|
|
else if (dfx->checktag_failed)
|
|
|
|
{
|
|
|
|
rc = gpg_error (GPG_ERR_BAD_SIGNATURE);
|
|
|
|
}
|
2006-10-02 11:54:35 +00:00
|
|
|
else if ( ed->mdc_method )
|
2011-02-03 20:59:01 +01:00
|
|
|
{
|
2006-10-02 11:54:35 +00: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 17:34:53 +00:00
|
|
|
|
2016-04-29 11:05:24 +02:00
|
|
|
log_assert (dfx->cipher_hd);
|
|
|
|
log_assert (dfx->mdc_hash);
|
2018-01-24 08:59:06 +01:00
|
|
|
gcry_cipher_decrypt (dfx->cipher_hd, dfx->holdback, 22, NULL, 0);
|
|
|
|
gcry_md_write (dfx->mdc_hash, dfx->holdback, 2);
|
2006-12-06 10:16:50 +00:00
|
|
|
gcry_md_final (dfx->mdc_hash);
|
2000-07-14 17:34:53 +00:00
|
|
|
|
2018-01-24 08:59:06 +01:00
|
|
|
if ( dfx->holdback[0] != '\xd3'
|
|
|
|
|| dfx->holdback[1] != '\x14'
|
2014-05-14 08:55:58 +02:00
|
|
|
|| datalen != 20
|
2018-01-24 08:59:06 +01:00
|
|
|
|| memcmp (gcry_md_read (dfx->mdc_hash, 0), dfx->holdback+2, datalen))
|
2006-10-02 11:54:35 +00:00
|
|
|
rc = gpg_error (GPG_ERR_BAD_SIGNATURE);
|
2018-01-24 08:59:06 +01:00
|
|
|
/* log_printhex("MDC message:", dfx->holdback, 22); */
|
2006-12-06 10:16:50 +00:00
|
|
|
/* log_printhex("MDC calc:", gcry_md_read (dfx->mdc_hash,0), datalen); */
|
1999-05-17 20:03:24 +00:00
|
|
|
}
|
2011-02-03 20:59:01 +01:00
|
|
|
|
2006-10-02 11:54:35 +00:00
|
|
|
leave:
|
2006-12-06 10:16:50 +00:00
|
|
|
release_dfx_context (dfx);
|
2006-10-02 11:54:35 +00:00
|
|
|
return rc;
|
1999-05-17 20:03:24 +00:00
|
|
|
}
|
|
|
|
|
1999-10-26 12:14:37 +00:00
|
|
|
|
2018-02-27 19:50:54 +01:00
|
|
|
/* Fill BUFFER with up to NBYTES-OFFSET from STREAM utilizing
|
|
|
|
* information from the context DFX. Returns the new offset which is
|
|
|
|
* the number of bytes read plus the original offset. On EOF the
|
|
|
|
* respective flag in DFX is set. */
|
|
|
|
static size_t
|
|
|
|
fill_buffer (decode_filter_ctx_t dfx, iobuf_t stream,
|
|
|
|
byte *buffer, size_t nbytes, size_t offset)
|
|
|
|
{
|
|
|
|
size_t nread = offset;
|
2018-11-08 21:31:12 +02:00
|
|
|
size_t curr;
|
|
|
|
int ret;
|
2018-02-27 19:50:54 +01:00
|
|
|
|
|
|
|
if (dfx->partial)
|
|
|
|
{
|
2018-11-08 21:31:12 +02:00
|
|
|
while (nread < nbytes)
|
2018-02-27 19:50:54 +01:00
|
|
|
{
|
2018-11-08 21:31:12 +02:00
|
|
|
curr = nbytes - nread;
|
|
|
|
|
|
|
|
ret = iobuf_read (stream, &buffer[nread], curr);
|
|
|
|
if (ret == -1)
|
2018-02-27 19:50:54 +01:00
|
|
|
{
|
|
|
|
dfx->eof_seen = 1; /* Normal EOF. */
|
|
|
|
break;
|
|
|
|
}
|
2018-11-08 21:31:12 +02:00
|
|
|
|
|
|
|
nread += ret;
|
2018-02-27 19:50:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-08 21:31:12 +02:00
|
|
|
while (nread < nbytes && dfx->length)
|
2018-02-27 19:50:54 +01:00
|
|
|
{
|
2018-11-08 21:31:12 +02:00
|
|
|
curr = nbytes - nread;
|
|
|
|
if (curr > dfx->length)
|
|
|
|
curr = dfx->length;
|
|
|
|
|
|
|
|
ret = iobuf_read (stream, &buffer[nread], curr);
|
|
|
|
if (ret == -1)
|
2018-02-27 19:50:54 +01:00
|
|
|
{
|
|
|
|
dfx->eof_seen = 3; /* Premature EOF. */
|
|
|
|
break;
|
|
|
|
}
|
2018-11-08 21:31:12 +02:00
|
|
|
|
|
|
|
nread += ret;
|
|
|
|
dfx->length -= ret;
|
2018-02-27 19:50:54 +01:00
|
|
|
}
|
|
|
|
if (!dfx->length)
|
|
|
|
dfx->eof_seen = 1; /* Normal EOF. */
|
|
|
|
}
|
|
|
|
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-21 16:24:43 +01:00
|
|
|
/* The core of the AEAD decryption. This is the underflow function of
|
|
|
|
* the aead_decode_filter. */
|
|
|
|
static gpg_error_t
|
|
|
|
aead_underflow (decode_filter_ctx_t dfx, iobuf_t a, byte *buf, size_t *ret_len)
|
|
|
|
{
|
2018-01-24 13:45:05 +01:00
|
|
|
const size_t size = *ret_len; /* The allocated size of BUF. */
|
2018-01-21 16:24:43 +01:00
|
|
|
gpg_error_t err;
|
2018-01-24 13:45:05 +01:00
|
|
|
size_t totallen = 0; /* The number of bytes to return on success or EOF. */
|
|
|
|
size_t off = 0; /* The offset into the buffer. */
|
|
|
|
size_t len; /* The current number of bytes in BUF+OFF. */
|
2018-01-21 16:24:43 +01:00
|
|
|
|
2018-01-24 13:45:05 +01:00
|
|
|
log_assert (size > 48); /* Our code requires at least this size. */
|
2018-01-21 16:24:43 +01:00
|
|
|
|
2018-01-24 13:45:05 +01:00
|
|
|
/* Copy the rest from the last call of this function into BUF. */
|
|
|
|
len = dfx->holdbacklen;
|
|
|
|
dfx->holdbacklen = 0;
|
|
|
|
memcpy (buf, dfx->holdback, len);
|
|
|
|
|
|
|
|
if (DBG_FILTER)
|
2018-02-27 13:53:52 +01:00
|
|
|
log_debug ("aead_underflow: size=%zu len=%zu%s%s\n", size, len,
|
|
|
|
dfx->partial? " partial":"", dfx->eof_seen? " eof":"");
|
2018-01-24 13:45:05 +01:00
|
|
|
|
2018-02-27 13:53:52 +01:00
|
|
|
/* Read and fill up BUF. We need to watch out for an EOF so that we
|
2018-01-24 13:45:05 +01:00
|
|
|
* can detect the last chunk which is commonly shorter than the
|
|
|
|
* chunksize. After the last data byte from the last chunk 32 more
|
|
|
|
* bytes are expected for the last chunk's tag and the following
|
2018-02-27 13:53:52 +01:00
|
|
|
* final chunk's tag. To detect the EOF we need to try reading at least
|
2018-02-27 19:50:54 +01:00
|
|
|
* one further byte; however we try to read 16 extra bytes to avoid
|
2018-02-27 13:53:52 +01:00
|
|
|
* single byte reads in some lower layers. The outcome is that we
|
2018-01-24 13:45:05 +01:00
|
|
|
* have up to 48 extra extra octets which we will later put into the
|
|
|
|
* holdback buffer for the next invocation (which handles the EOF
|
|
|
|
* case). */
|
2018-02-27 19:50:54 +01:00
|
|
|
len = fill_buffer (dfx, a, buf, size, len);
|
2018-01-24 13:45:05 +01:00
|
|
|
if (len < 32)
|
2018-01-21 16:24:43 +01:00
|
|
|
{
|
2018-01-24 13:45:05 +01:00
|
|
|
/* Not enough data for the last two tags. */
|
|
|
|
err = gpg_error (GPG_ERR_TRUNCATED);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
if (dfx->eof_seen)
|
|
|
|
{
|
|
|
|
/* If have seen an EOF we copy only the last two auth tags into
|
|
|
|
* the holdback buffer. */
|
|
|
|
dfx->holdbacklen = 32;
|
|
|
|
memcpy (dfx->holdback, buf+len-32, 32);
|
|
|
|
len -= 32;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If have not seen an EOF we copy the entire extra 48 bytes
|
|
|
|
* into the holdback buffer for processing at the next call of
|
|
|
|
* this function. */
|
|
|
|
dfx->holdbacklen = len > 48? 48 : len;
|
|
|
|
memcpy (dfx->holdback, buf+len-dfx->holdbacklen, dfx->holdbacklen);
|
|
|
|
len -= dfx->holdbacklen;
|
|
|
|
}
|
|
|
|
/* log_printhex (dfx->holdback, dfx->holdbacklen, "holdback:"); */
|
|
|
|
|
2018-02-27 21:11:20 +01:00
|
|
|
/* Decrypt the buffer. This first requires a loop to handle the
|
|
|
|
* case when a chunk ends within the buffer. */
|
2018-01-24 13:45:05 +01:00
|
|
|
if (DBG_FILTER)
|
2022-12-16 15:33:46 +01:00
|
|
|
log_debug ("decrypt: chunklen=%llu total=%llu size=%zu len=%zu%s\n",
|
|
|
|
(unsigned long long)dfx->chunklen,
|
|
|
|
(unsigned long long)dfx->total,
|
|
|
|
size, len,
|
2018-01-24 13:45:05 +01:00
|
|
|
dfx->eof_seen? " eof":"");
|
|
|
|
|
|
|
|
while (len && dfx->chunklen + len >= dfx->chunksize)
|
|
|
|
{
|
|
|
|
size_t n = dfx->chunksize - dfx->chunklen;
|
|
|
|
byte tagbuf[16];
|
|
|
|
|
|
|
|
if (DBG_FILTER)
|
|
|
|
log_debug ("chunksize will be reached: n=%zu\n", n);
|
2018-02-27 21:11:20 +01:00
|
|
|
|
|
|
|
if (!dfx->chunklen)
|
|
|
|
{
|
|
|
|
/* First data for this chunk - prepare. */
|
|
|
|
err = aead_set_nonce_and_ad (dfx, 0);
|
|
|
|
if (err)
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2018-01-24 13:45:05 +01:00
|
|
|
/* log_printhex (buf, n, "ciph:"); */
|
|
|
|
gcry_cipher_final (dfx->cipher_hd);
|
|
|
|
err = gcry_cipher_decrypt (dfx->cipher_hd, buf+off, n, NULL, 0);
|
|
|
|
if (err)
|
2018-01-21 16:24:43 +01:00
|
|
|
{
|
2018-01-24 13:45:05 +01:00
|
|
|
log_error ("gcry_cipher_decrypt failed (1): %s\n",
|
|
|
|
gpg_strerror (err));
|
|
|
|
goto leave;
|
2018-01-21 16:24:43 +01:00
|
|
|
}
|
2018-01-24 13:45:05 +01:00
|
|
|
/* log_printhex (buf, n, "plai:"); */
|
|
|
|
totallen += n;
|
|
|
|
dfx->chunklen += n;
|
|
|
|
dfx->total += n;
|
|
|
|
off += n;
|
|
|
|
len -= n;
|
|
|
|
|
|
|
|
if (DBG_FILTER)
|
2022-12-16 15:33:46 +01:00
|
|
|
log_debug ("ndecrypted: %zu (nchunk=%llu) bytes left: %zu at off=%zu\n",
|
|
|
|
totallen, (unsigned long long)dfx->chunklen, len, off);
|
2018-01-21 16:24:43 +01:00
|
|
|
|
2018-01-24 13:45:05 +01:00
|
|
|
/* Check the tag. */
|
|
|
|
if (len < 16)
|
2018-01-21 16:24:43 +01:00
|
|
|
{
|
2018-01-24 13:45:05 +01:00
|
|
|
/* The tag is not entirely in the buffer. Read the rest of
|
2018-02-27 13:53:52 +01:00
|
|
|
* the tag from the holdback buffer. Then shift the holdback
|
2018-01-24 13:45:05 +01:00
|
|
|
* buffer and fill it up again. */
|
|
|
|
memcpy (tagbuf, buf+off, len);
|
|
|
|
memcpy (tagbuf + len, dfx->holdback, 16 - len);
|
|
|
|
dfx->holdbacklen -= 16-len;
|
|
|
|
memmove (dfx->holdback, dfx->holdback + (16-len), dfx->holdbacklen);
|
|
|
|
|
2018-02-27 13:53:52 +01:00
|
|
|
if (dfx->eof_seen)
|
2018-01-21 16:24:43 +01:00
|
|
|
{
|
2018-02-27 13:53:52 +01:00
|
|
|
/* We should have the last chunk's tag in TAGBUF and the
|
|
|
|
* final tag in HOLDBACKBUF. */
|
|
|
|
if (len || dfx->holdbacklen != 16)
|
2018-01-21 16:24:43 +01:00
|
|
|
{
|
2018-02-27 13:53:52 +01:00
|
|
|
/* Not enough data for the last two tags. */
|
|
|
|
err = gpg_error (GPG_ERR_TRUNCATED);
|
|
|
|
goto leave;
|
2018-01-21 16:24:43 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-24 13:45:05 +01:00
|
|
|
else
|
2018-01-21 16:24:43 +01:00
|
|
|
{
|
2018-02-27 19:50:54 +01:00
|
|
|
len = 0;
|
|
|
|
dfx->holdbacklen = fill_buffer (dfx, a, dfx->holdback, 48,
|
|
|
|
dfx->holdbacklen);
|
|
|
|
if (dfx->holdbacklen < 32)
|
2018-02-27 13:53:52 +01:00
|
|
|
{
|
|
|
|
/* Not enough data for the last two tags. */
|
|
|
|
err = gpg_error (GPG_ERR_TRUNCATED);
|
|
|
|
goto leave;
|
|
|
|
}
|
2018-01-24 13:45:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* We already have the full tag. */
|
|
|
|
{
|
|
|
|
memcpy (tagbuf, buf+off, 16);
|
|
|
|
/* Remove that tag from the output. */
|
|
|
|
memmove (buf + off, buf + off + 16, len - 16);
|
|
|
|
len -= 16;
|
|
|
|
}
|
2018-02-27 19:50:54 +01:00
|
|
|
err = aead_checktag (dfx, 0, tagbuf);
|
2018-01-24 13:45:05 +01:00
|
|
|
if (err)
|
2018-02-27 19:50:54 +01:00
|
|
|
goto leave;
|
2018-02-27 21:11:20 +01:00
|
|
|
dfx->chunklen = 0;
|
|
|
|
dfx->chunkindex++;
|
2018-01-21 16:24:43 +01:00
|
|
|
|
2018-02-27 21:11:20 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The bulk decryption of our buffer. */
|
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
if (!dfx->chunklen)
|
2018-02-27 13:53:52 +01:00
|
|
|
{
|
2018-02-27 21:11:20 +01:00
|
|
|
/* First data for this chunk - prepare. */
|
|
|
|
err = aead_set_nonce_and_ad (dfx, 0);
|
2018-02-27 13:53:52 +01:00
|
|
|
if (err)
|
|
|
|
goto leave;
|
|
|
|
}
|
2018-01-21 16:24:43 +01:00
|
|
|
|
2018-02-27 13:53:52 +01:00
|
|
|
if (dfx->eof_seen)
|
|
|
|
{
|
|
|
|
/* This is the last block of the last chunk. Its length may
|
|
|
|
* not be a multiple of the block length. */
|
|
|
|
gcry_cipher_final (dfx->cipher_hd);
|
|
|
|
}
|
|
|
|
err = gcry_cipher_decrypt (dfx->cipher_hd, buf + off, len, NULL, 0);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
log_error ("gcry_cipher_decrypt failed (2): %s\n",
|
|
|
|
gpg_strerror (err));
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
totallen += len;
|
|
|
|
dfx->chunklen += len;
|
|
|
|
dfx->total += len;
|
|
|
|
if (DBG_FILTER)
|
2022-12-16 15:33:46 +01:00
|
|
|
log_debug ("ndecrypted: %zu (nchunk=%llu)\n",
|
|
|
|
totallen, (unsigned long long)dfx->chunklen);
|
2018-01-21 16:24:43 +01:00
|
|
|
}
|
2018-02-27 13:53:52 +01:00
|
|
|
|
2018-01-24 13:45:05 +01:00
|
|
|
if (dfx->eof_seen)
|
2018-01-21 16:24:43 +01:00
|
|
|
{
|
|
|
|
|
2018-02-27 21:11:20 +01:00
|
|
|
if (dfx->chunklen)
|
2018-01-21 16:24:43 +01:00
|
|
|
{
|
2018-02-28 09:31:39 +01:00
|
|
|
if (DBG_FILTER)
|
|
|
|
log_debug ("eof seen: holdback has the last and final tag\n");
|
|
|
|
log_assert (dfx->holdbacklen >= 32);
|
2018-02-27 19:50:54 +01:00
|
|
|
err = aead_checktag (dfx, 0, dfx->holdback);
|
2018-02-27 13:53:52 +01:00
|
|
|
if (err)
|
2018-02-27 19:50:54 +01:00
|
|
|
goto leave;
|
2018-02-27 21:11:20 +01:00
|
|
|
dfx->chunklen = 0;
|
|
|
|
dfx->chunkindex++;
|
2018-02-28 09:31:39 +01:00
|
|
|
off = 16;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (DBG_FILTER)
|
|
|
|
log_debug ("eof seen: holdback has the final tag\n");
|
|
|
|
log_assert (dfx->holdbacklen >= 16);
|
|
|
|
off = 0;
|
2018-01-21 16:24:43 +01:00
|
|
|
}
|
2018-01-24 13:45:05 +01:00
|
|
|
|
|
|
|
/* Check the final chunk. */
|
2018-02-27 21:11:20 +01:00
|
|
|
err = aead_set_nonce_and_ad (dfx, 1);
|
2018-01-24 13:45:05 +01:00
|
|
|
if (err)
|
|
|
|
goto leave;
|
|
|
|
gcry_cipher_final (dfx->cipher_hd);
|
2018-02-27 21:11:20 +01:00
|
|
|
/* Decrypt an empty string (using HOLDBACK as a dummy). */
|
2018-01-24 13:45:05 +01:00
|
|
|
err = gcry_cipher_decrypt (dfx->cipher_hd, dfx->holdback, 0, NULL, 0);
|
2018-01-21 16:24:43 +01:00
|
|
|
if (err)
|
|
|
|
{
|
2018-01-24 13:45:05 +01:00
|
|
|
log_error ("gcry_cipher_decrypt failed (final): %s\n",
|
2018-01-23 19:08:16 +01:00
|
|
|
gpg_strerror (err));
|
2018-01-21 16:24:43 +01:00
|
|
|
goto leave;
|
|
|
|
}
|
2018-02-28 09:31:39 +01:00
|
|
|
err = aead_checktag (dfx, 1, dfx->holdback+off);
|
2018-01-24 13:45:05 +01:00
|
|
|
if (err)
|
2018-02-27 19:50:54 +01:00
|
|
|
goto leave;
|
2018-01-24 13:45:05 +01:00
|
|
|
err = gpg_error (GPG_ERR_EOF);
|
2018-01-21 16:24:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
leave:
|
2018-01-24 13:45:05 +01:00
|
|
|
if (DBG_FILTER)
|
|
|
|
log_debug ("aead_underflow: returning %zu (%s)\n",
|
|
|
|
totallen, gpg_strerror (err));
|
|
|
|
|
|
|
|
/* In case of an auth error we map the error code to the same as
|
|
|
|
* used by the MDC decryption. */
|
|
|
|
if (gpg_err_code (err) == GPG_ERR_CHECKSUM)
|
|
|
|
err = gpg_error (GPG_ERR_BAD_SIGNATURE);
|
|
|
|
|
|
|
|
/* In case of an error we better wipe out the buffer than to convey
|
|
|
|
* partly decrypted data. */
|
2018-01-21 16:24:43 +01:00
|
|
|
if (err && gpg_err_code (err) != GPG_ERR_EOF)
|
|
|
|
memset (buf, 0, size);
|
2018-01-24 13:45:05 +01:00
|
|
|
|
|
|
|
*ret_len = totallen;
|
2018-01-21 16:24:43 +01:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* The IOBUF filter used to decrypt AEAD encrypted data. */
|
|
|
|
static int
|
|
|
|
aead_decode_filter (void *opaque, int control, IOBUF a,
|
|
|
|
byte *buf, size_t *ret_len)
|
|
|
|
{
|
|
|
|
decode_filter_ctx_t dfx = opaque;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
if ( control == IOBUFCTRL_UNDERFLOW && dfx->eof_seen )
|
|
|
|
{
|
|
|
|
*ret_len = 0;
|
|
|
|
rc = -1;
|
|
|
|
}
|
|
|
|
else if ( control == IOBUFCTRL_UNDERFLOW )
|
|
|
|
{
|
|
|
|
log_assert (a);
|
|
|
|
|
|
|
|
rc = aead_underflow (dfx, a, buf, ret_len);
|
|
|
|
if (gpg_err_code (rc) == GPG_ERR_EOF)
|
|
|
|
rc = -1; /* We need to use the old convention in the filter. */
|
|
|
|
|
|
|
|
}
|
|
|
|
else if ( control == IOBUFCTRL_FREE )
|
|
|
|
{
|
|
|
|
release_dfx_context (dfx);
|
|
|
|
}
|
|
|
|
else if ( control == IOBUFCTRL_DESC )
|
|
|
|
{
|
|
|
|
mem2str (buf, "aead_decode_filter", *ret_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2000-07-14 17:34:53 +00:00
|
|
|
|
1999-05-17 20:03:24 +00:00
|
|
|
static int
|
2006-10-02 11:54:35 +00:00
|
|
|
mdc_decode_filter (void *opaque, int control, IOBUF a,
|
|
|
|
byte *buf, size_t *ret_len)
|
1999-05-17 20:03:24 +00:00
|
|
|
{
|
2006-12-06 10:16:50 +00:00
|
|
|
decode_filter_ctx_t dfx = opaque;
|
2006-10-02 11:54:35 +00:00
|
|
|
size_t n, size = *ret_len;
|
|
|
|
int rc = 0;
|
2009-10-02 12:31:14 +00: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 11:54:35 +00:00
|
|
|
if ( control == IOBUFCTRL_UNDERFLOW && dfx->eof_seen )
|
|
|
|
{
|
|
|
|
*ret_len = 0;
|
|
|
|
rc = -1;
|
1999-05-17 20:03:24 +00:00
|
|
|
}
|
2006-10-02 11:54:35 +00:00
|
|
|
else if( control == IOBUFCTRL_UNDERFLOW )
|
|
|
|
{
|
2016-04-29 11:05:24 +02:00
|
|
|
log_assert (a);
|
|
|
|
log_assert (size > 44); /* Our code requires at least this size. */
|
2011-02-03 20:59:01 +01:00
|
|
|
|
2009-10-02 12:31:14 +00:00
|
|
|
/* Get at least 22 bytes and put it ahead in the buffer. */
|
2018-11-08 21:31:12 +02:00
|
|
|
n = fill_buffer (dfx, a, buf, 44, 22);
|
2011-02-03 20:59:01 +01:00
|
|
|
if (n == 44)
|
2006-10-02 11:54:35 +00:00
|
|
|
{
|
2018-01-24 08:59:06 +01:00
|
|
|
/* We have enough stuff - flush the holdback buffer. */
|
|
|
|
if ( !dfx->holdbacklen ) /* First time. */
|
2006-10-02 11:54:35 +00:00
|
|
|
{
|
2009-10-02 12:31:14 +00:00
|
|
|
memcpy (buf, buf+22, 22);
|
2006-10-02 11:54:35 +00:00
|
|
|
n = 22;
|
2000-07-14 17:34:53 +00:00
|
|
|
}
|
2006-10-02 11:54:35 +00:00
|
|
|
else
|
|
|
|
{
|
2018-01-24 08:59:06 +01:00
|
|
|
memcpy (buf, dfx->holdback, 22);
|
2000-07-14 17:34:53 +00:00
|
|
|
}
|
2018-11-08 21:31:12 +02:00
|
|
|
|
2009-10-02 12:31:14 +00:00
|
|
|
/* Fill up the buffer. */
|
2018-11-08 21:31:12 +02:00
|
|
|
n = fill_buffer (dfx, a, buf, size, n);
|
2011-02-03 20:59:01 +01:00
|
|
|
|
2018-01-24 08:59:06 +01:00
|
|
|
/* Move the trailing 22 bytes back to the holdback buffer. We
|
2009-10-02 12:31:14 +00:00
|
|
|
have at least 44 bytes thus a memmove is not needed. */
|
2006-10-02 11:54:35 +00:00
|
|
|
n -= 22;
|
2018-01-24 08:59:06 +01:00
|
|
|
memcpy (dfx->holdback, buf+n, 22 );
|
|
|
|
dfx->holdbacklen = 22;
|
1999-05-17 20:03:24 +00:00
|
|
|
}
|
2018-01-24 08:59:06 +01:00
|
|
|
else if ( !dfx->holdbacklen ) /* EOF seen but empty holdback. */
|
2006-10-02 11:54:35 +00:00
|
|
|
{
|
|
|
|
/* 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 20:03:24 +00:00
|
|
|
}
|
2006-10-02 11:54:35 +00:00
|
|
|
else /* EOF seen (i.e. read less than 22 bytes). */
|
|
|
|
{
|
2018-01-24 08:59:06 +01:00
|
|
|
memcpy (buf, dfx->holdback, 22 );
|
2006-10-02 11:54:35 +00:00
|
|
|
n -= 22;
|
2018-01-24 08:59:06 +01:00
|
|
|
memcpy (dfx->holdback, buf+n, 22 );
|
2006-10-02 11:54:35 +00:00
|
|
|
dfx->eof_seen = 1; /* Normal EOF. */
|
1999-05-17 20:03:24 +00:00
|
|
|
}
|
|
|
|
|
2006-10-02 11:54:35 +00:00
|
|
|
if ( n )
|
|
|
|
{
|
2006-12-06 10:16:50 +00: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 20:03:24 +00:00
|
|
|
}
|
2006-10-02 11:54:35 +00:00
|
|
|
else
|
|
|
|
{
|
2016-04-29 11:05:24 +02:00
|
|
|
log_assert ( dfx->eof_seen );
|
2009-10-02 12:31:14 +00:00
|
|
|
rc = -1; /* Return EOF. */
|
1999-05-17 20:03:24 +00:00
|
|
|
}
|
2006-10-02 11:54:35 +00:00
|
|
|
*ret_len = n;
|
1999-05-17 20:03:24 +00:00
|
|
|
}
|
2011-02-03 20:59:01 +01:00
|
|
|
else if ( control == IOBUFCTRL_FREE )
|
2006-12-06 10:16:50 +00:00
|
|
|
{
|
|
|
|
release_dfx_context (dfx);
|
|
|
|
}
|
2011-02-03 20:59:01 +01:00
|
|
|
else if ( control == IOBUFCTRL_DESC )
|
2006-10-02 11:54:35 +00:00
|
|
|
{
|
common: Fix iobuf API of filter function for alignment.
* common/iobuf.h (IOBUFCTRL_DESC): Change the call semantics.
* common/iobuf.c (iobuf_desc): Add the second argument DESC.
(print_chain, iobuf_close, do_open, iobuf_sockopen, iobuf_ioctl)
(iobuf_push_filter2, pop_filter, iobuf_write_temp): Change calls
of iobuf_desc.
(file_filter, file_es_filter, sock_filter, block_filter): Fill the
description.
* common/t-iobuf.c (every_other_filter, double_filter): Likewise.
* g10/armor.c, g10/cipher.c, g10/compress-bz2.c, g10/compress.c,
g10/decrypt-data.c, g10/encrypt.c, g10/mdfilter.c, g10/progress.c,
g10/textfilter.c: Likewise.
--
Newer GCC warns against possible alignment difference of pointers.
This change can silence those warnings.
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2016-01-12 10:32:20 +09:00
|
|
|
mem2str (buf, "mdc_decode_filter", *ret_len);
|
1999-05-17 20:03:24 +00:00
|
|
|
}
|
2006-10-02 11:54:35 +00:00
|
|
|
return rc;
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
|
2006-10-02 11:54:35 +00:00
|
|
|
|
1997-11-18 14:06:00 +00:00
|
|
|
static int
|
2006-04-19 11:26:11 +00:00
|
|
|
decode_filter( void *opaque, int control, IOBUF a, byte *buf, size_t *ret_len)
|
1997-11-18 14:06:00 +00:00
|
|
|
{
|
2006-12-06 10:16:50 +00:00
|
|
|
decode_filter_ctx_t fc = opaque;
|
2009-10-02 12:31:14 +00:00
|
|
|
size_t size = *ret_len;
|
|
|
|
size_t n;
|
2018-11-08 21:31:12 +02:00
|
|
|
int rc = 0;
|
2009-10-02 12:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
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 11:54:35 +00:00
|
|
|
{
|
2016-04-29 11:05:24 +02:00
|
|
|
log_assert (a);
|
2011-02-03 20:59:01 +01:00
|
|
|
|
2018-11-08 21:31:12 +02:00
|
|
|
n = fill_buffer (fc, a, buf, size, 0);
|
2009-10-02 12:31:14 +00:00
|
|
|
if (n)
|
2006-12-06 10:16:50 +00:00
|
|
|
{
|
|
|
|
if (fc->cipher_hd)
|
|
|
|
gcry_cipher_decrypt (fc->cipher_hd, buf, n, NULL, 0);
|
|
|
|
}
|
2006-10-02 11:54:35 +00:00
|
|
|
else
|
2009-10-02 12:31:14 +00:00
|
|
|
{
|
|
|
|
if (!fc->eof_seen)
|
|
|
|
fc->eof_seen = 1;
|
|
|
|
rc = -1; /* Return EOF. */
|
|
|
|
}
|
2006-10-02 11:54:35 +00:00
|
|
|
*ret_len = n;
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
2011-02-03 20:59:01 +01:00
|
|
|
else if ( control == IOBUFCTRL_FREE )
|
2006-12-06 10:16:50 +00:00
|
|
|
{
|
|
|
|
release_dfx_context (fc);
|
|
|
|
}
|
2006-10-02 11:54:35 +00:00
|
|
|
else if ( control == IOBUFCTRL_DESC )
|
|
|
|
{
|
common: Fix iobuf API of filter function for alignment.
* common/iobuf.h (IOBUFCTRL_DESC): Change the call semantics.
* common/iobuf.c (iobuf_desc): Add the second argument DESC.
(print_chain, iobuf_close, do_open, iobuf_sockopen, iobuf_ioctl)
(iobuf_push_filter2, pop_filter, iobuf_write_temp): Change calls
of iobuf_desc.
(file_filter, file_es_filter, sock_filter, block_filter): Fill the
description.
* common/t-iobuf.c (every_other_filter, double_filter): Likewise.
* g10/armor.c, g10/cipher.c, g10/compress-bz2.c, g10/compress.c,
g10/decrypt-data.c, g10/encrypt.c, g10/mdfilter.c, g10/progress.c,
g10/textfilter.c: Likewise.
--
Newer GCC warns against possible alignment difference of pointers.
This change can silence those warnings.
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2016-01-12 10:32:20 +09:00
|
|
|
mem2str (buf, "decode_filter", *ret_len);
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
2006-10-02 11:54:35 +00:00
|
|
|
return rc;
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|