Coverage Report

Created: 2026-06-15 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnupg/g10/decrypt-data.c
Line
Count
Source
1
/* decrypt-data.c - Decrypt an encrypted data packet
2
 * Copyright (C) 1998-2001, 2005-2006, 2009 Free Software Foundation, Inc.
3
 * Copyright (C) 1998-2001, 2005-2006, 2009, 2018 Werner Koch
4
 *
5
 * This file is part of GnuPG.
6
 *
7
 * GnuPG is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * GnuPG is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
19
 * SPDX-License-Identifier: GPL-3.0-or-later
20
 */
21
22
#include <config.h>
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <string.h>
26
27
#include "gpg.h"
28
#include "../common/util.h"
29
#include "packet.h"
30
#include "options.h"
31
#include "../common/i18n.h"
32
#include "../common/status.h"
33
#include "../common/compliance.h"
34
35
36
static int aead_decode_filter (void *opaque, int control, iobuf_t a,
37
                               byte *buf, size_t *ret_len);
38
static int mdc_decode_filter ( void *opaque, int control, IOBUF a,
39
                               byte *buf, size_t *ret_len);
40
static int decode_filter ( void *opaque, int control, IOBUF a,
41
          byte *buf, size_t *ret_len);
42
43
/* Our context object.  */
44
struct decode_filter_context_s
45
{
46
  /* Redcounter (max value is 2).  We need it because we do not know
47
   * whether the iobuf or the outer control code frees this object
48
   * first.  */
49
  int  refcount;
50
51
  /* The cipher handle.  */
52
  gcry_cipher_hd_t cipher_hd;
53
54
  /* The hash handle for use in MDC mode.  */
55
  gcry_md_hd_t mdc_hash;
56
57
  /* The start IV for AEAD encryption.   */
58
  byte startiv[16];
59
60
  /* The holdback buffer and its used length.  For AEAD we need 32+1
61
   * bytes but we use 48 byte.  For MDC we need 22 bytes; here
62
   * holdbacklen will either 0 or 22.  */
63
  char holdback[48];
64
  unsigned int holdbacklen;
65
66
  /* Working on a partial length packet.  */
67
  unsigned int partial : 1;
68
69
  /* EOF indicator with these true values:
70
   *   1 = normal EOF
71
   *   2 = premature EOF (tag or hash incomplete)
72
   *   3 = premature EOF (general)       */
73
  unsigned int eof_seen : 2;
74
75
  /* Flag to convey an error from aead_checktag.  */
76
  unsigned int checktag_failed : 1;
77
78
  /* The actually used cipher algo for AEAD.  */
79
  byte cipher_algo;
80
81
  /* The AEAD algo.  */
82
  byte aead_algo;
83
84
  /* The encoded chunk byte for AEAD.  */
85
  byte chunkbyte;
86
87
  /* The decoded CHUNKBYTE.  */
88
  uint64_t chunksize;
89
90
  /* The chunk index for AEAD.  */
91
  uint64_t chunkindex;
92
93
  /* The number of bytes in the current chunk.  */
94
  uint64_t chunklen;
95
96
  /* The total count of decrypted plaintext octets.  */
97
  uint64_t total;
98
99
  /* Remaining bytes in the packet according to the packet header.
100
   * Not used if PARTIAL is true.  */
101
  size_t length;
102
};
103
typedef struct decode_filter_context_s *decode_filter_ctx_t;
104
105
106
/* Helper to release the decode context.  */
107
static void
108
release_dfx_context (decode_filter_ctx_t dfx)
109
0
{
110
0
  if (!dfx)
111
0
    return;
112
113
0
  log_assert (dfx->refcount);
114
0
  if ( !--dfx->refcount )
115
0
    {
116
0
      gcry_cipher_close (dfx->cipher_hd);
117
0
      dfx->cipher_hd = NULL;
118
0
      gcry_md_close (dfx->mdc_hash);
119
0
      dfx->mdc_hash = NULL;
120
0
      xfree (dfx);
121
0
    }
122
0
}
123
124
125
/* Set the nonce and the additional data for the current chunk.  This
126
 * also reset the decryption machinery so that the handle can be
127
 * used for a new chunk.  If FINAL is set the final AEAD chunk is
128
 * processed.  */
129
static gpg_error_t
130
aead_set_nonce_and_ad (decode_filter_ctx_t dfx, int final)
131
0
{
132
0
  gpg_error_t err;
133
0
  unsigned char ad[21];
134
0
  unsigned char nonce[16];
135
0
  int i;
136
137
0
  switch (dfx->aead_algo)
138
0
    {
139
0
    case AEAD_ALGO_OCB:
140
0
      memcpy (nonce, dfx->startiv, 15);
141
0
      i = 7;
142
0
      break;
143
144
0
    case AEAD_ALGO_EAX:
145
0
      memcpy (nonce, dfx->startiv, 16);
146
0
      i = 8;
147
0
      break;
148
149
0
    default:
150
0
      BUG ();
151
0
    }
152
0
  nonce[i++] ^= dfx->chunkindex >> 56;
153
0
  nonce[i++] ^= dfx->chunkindex >> 48;
154
0
  nonce[i++] ^= dfx->chunkindex >> 40;
155
0
  nonce[i++] ^= dfx->chunkindex >> 32;
156
0
  nonce[i++] ^= dfx->chunkindex >> 24;
157
0
  nonce[i++] ^= dfx->chunkindex >> 16;
158
0
  nonce[i++] ^= dfx->chunkindex >>  8;
159
0
  nonce[i++] ^= dfx->chunkindex;
160
161
0
  if (DBG_CRYPTO)
162
0
    log_printhex (nonce, i, "nonce:");
163
0
  err = gcry_cipher_setiv (dfx->cipher_hd, nonce, i);
164
0
  if (err)
165
0
    return err;
166
167
0
  ad[0] = (0xc0 | PKT_ENCRYPTED_AEAD);
168
0
  ad[1] = 1;
169
0
  ad[2] = dfx->cipher_algo;
170
0
  ad[3] = dfx->aead_algo;
171
0
  ad[4] = dfx->chunkbyte;
172
0
  ad[5] = dfx->chunkindex >> 56;
173
0
  ad[6] = dfx->chunkindex >> 48;
174
0
  ad[7] = dfx->chunkindex >> 40;
175
0
  ad[8] = dfx->chunkindex >> 32;
176
0
  ad[9] = dfx->chunkindex >> 24;
177
0
  ad[10]= dfx->chunkindex >> 16;
178
0
  ad[11]= dfx->chunkindex >>  8;
179
0
  ad[12]= dfx->chunkindex;
180
0
  if (final)
181
0
    {
182
0
      ad[13] = dfx->total >> 56;
183
0
      ad[14] = dfx->total >> 48;
184
0
      ad[15] = dfx->total >> 40;
185
0
      ad[16] = dfx->total >> 32;
186
0
      ad[17] = dfx->total >> 24;
187
0
      ad[18] = dfx->total >> 16;
188
0
      ad[19] = dfx->total >>  8;
189
0
      ad[20] = dfx->total;
190
0
    }
191
0
  if (DBG_CRYPTO)
192
0
    log_printhex (ad, final? 21 : 13, "authdata:");
193
0
  return gcry_cipher_authenticate (dfx->cipher_hd, ad, final? 21 : 13);
194
0
}
195
196
197
/* Helper to check the 16 byte tag in TAGBUF.  The FINAL flag is only
198
 * for debug messages.  */
199
static gpg_error_t
200
aead_checktag (decode_filter_ctx_t dfx, int final, const void *tagbuf)
201
0
{
202
0
  gpg_error_t err;
203
204
0
  if (DBG_FILTER)
205
0
    log_printhex (tagbuf, 16, "tag:");
206
0
  err = gcry_cipher_checktag (dfx->cipher_hd, tagbuf, 16);
207
0
  if (err)
208
0
    {
209
0
      log_error ("gcry_cipher_checktag%s failed: %s\n",
210
0
                 final? " (final)":"", gpg_strerror (err));
211
0
      write_status_error ("aead_checktag", err);
212
0
      dfx->checktag_failed = 1;
213
0
      return err;
214
0
    }
215
0
  if (DBG_FILTER)
216
0
    log_debug ("%stag is valid\n", final?"final ":"");
217
0
  return 0;
218
0
}
219
220
221
/****************
222
 * Decrypt the data, specified by ED with the key DEK.  On return
223
 * COMPLIANCE_ERROR is set to true iff the decryption can claim that
224
 * it was compliant in the current mode; otherwise this flag is set to
225
 * false.
226
 */
227
int
228
decrypt_data (ctrl_t ctrl, void *procctx, PKT_encrypted *ed, DEK *dek,
229
              int *compliance_error)
230
0
{
231
0
  decode_filter_ctx_t dfx;
232
0
  enum gcry_cipher_modes ciphermode;
233
0
  unsigned int startivlen;
234
0
  byte *p;
235
0
  int rc=0, c, i;
236
0
  byte temp[32];
237
0
  unsigned int blocksize;
238
0
  unsigned int nprefix;
239
240
0
  *compliance_error = 0;
241
242
0
  dfx = xtrycalloc (1, sizeof *dfx);
243
0
  if (!dfx)
244
0
    return gpg_error_from_syserror ();
245
0
  dfx->refcount = 1;
246
247
0
  if ( opt.verbose && !dek->algo_info_printed )
248
0
    {
249
0
      if (!openpgp_cipher_test_algo (dek->algo))
250
0
        log_info (_("%s encrypted data\n"),
251
0
                  openpgp_cipher_algo_mode_name (dek->algo, ed->aead_algo));
252
0
      else
253
0
        log_info (_("encrypted with unknown algorithm %d\n"), dek->algo );
254
0
      dek->algo_info_printed = 1;
255
0
    }
256
257
0
  if (ed->aead_algo)
258
0
    {
259
0
      rc = openpgp_aead_algo_info (ed->aead_algo, &ciphermode, &startivlen);
260
0
      if (rc)
261
0
        goto leave;
262
0
      log_assert (startivlen <= sizeof dfx->startiv);
263
0
    }
264
0
  else
265
0
    ciphermode = GCRY_CIPHER_MODE_CFB;
266
267
  /* Check compliance.  */
268
0
  if (!gnupg_cipher_is_allowed (opt.compliance, 0, dek->algo, ciphermode))
269
0
    {
270
0
      gpgrt_log (opt.show_only_session_key? GPGRT_LOGLVL_INFO
271
0
                 /*                     */: GPGRT_LOGLVL_ERROR,
272
0
                 _("cipher algorithm '%s' may not be used in %s mode\n"),
273
0
     openpgp_cipher_algo_mode_name (dek->algo,ed->aead_algo),
274
0
     gnupg_compliance_option_string (opt.compliance));
275
0
      *compliance_error = 1;
276
0
      if (opt.flags.require_compliance && !opt.show_only_session_key)
277
0
        {
278
          /* We fail early in this case because it does not make sense
279
           * to first decrypt everything.  */
280
0
          rc = gpg_error (GPG_ERR_CIPHER_ALGO);
281
0
          goto leave;
282
0
        }
283
0
    }
284
285
0
  write_status_printf (STATUS_DECRYPTION_INFO, "%d %d %d %d",
286
0
                       ed->mdc_method, dek->algo, ed->aead_algo,
287
0
                       *compliance_error);
288
289
0
  if (opt.show_session_key)
290
0
    {
291
0
      char numbuf[30];
292
0
      char *hexbuf;
293
294
0
      if (ed->aead_algo)
295
0
        snprintf (numbuf, sizeof numbuf, "%d.%u:", dek->algo, ed->aead_algo);
296
0
      else
297
0
        snprintf (numbuf, sizeof numbuf, "%d:", dek->algo);
298
0
      hexbuf = bin2hex (dek->key, dek->keylen, NULL);
299
0
      if (!hexbuf)
300
0
        {
301
0
          rc = gpg_error_from_syserror ();
302
0
          goto leave;
303
0
        }
304
0
      log_info ("session key: '%s%s'\n", numbuf, hexbuf);
305
0
      write_status_strings (STATUS_SESSION_KEY, numbuf, hexbuf, NULL);
306
0
      xfree (hexbuf);
307
0
    }
308
309
0
  if (opt.show_session_hash)
310
0
    {
311
0
      char hashbuf[32];  /* For SHA-256.  */
312
0
      char *rad64buf;
313
314
0
      gcry_md_hash_buffer (GCRY_MD_SHA256, hashbuf, dek->key, dek->keylen);
315
0
      rad64buf = make_radix64_string (hashbuf, sizeof hashbuf);
316
0
      write_status_text (STATUS_SESSION_HASH, rad64buf);
317
0
      xfree (rad64buf);
318
0
    }
319
320
0
  if (opt.show_only_session_key)
321
0
    {
322
0
      rc = 0;
323
0
      goto leave;
324
0
    }
325
326
0
  rc = openpgp_cipher_test_algo (dek->algo);
327
0
  if (rc)
328
0
    goto leave;
329
0
  blocksize = openpgp_cipher_get_algo_blklen (dek->algo);
330
0
  if ( !blocksize || blocksize > 16 )
331
0
    log_fatal ("unsupported blocksize %u\n", blocksize );
332
333
0
  if (ed->aead_algo)
334
0
    {
335
0
      if (blocksize != 16)
336
0
        {
337
0
          rc = gpg_error (GPG_ERR_CIPHER_ALGO);
338
0
          goto leave;
339
0
        }
340
341
0
      if (ed->chunkbyte > 56)
342
0
        {
343
0
          log_error ("invalid AEAD chunkbyte %u\n", ed->chunkbyte);
344
0
          rc = gpg_error (GPG_ERR_INV_PACKET);
345
0
          goto leave;
346
0
        }
347
348
      /* Read the Start-IV. */
349
0
      if (ed->len)
350
0
        {
351
0
          for (i=0; i < startivlen && ed->len; i++, ed->len--)
352
0
            {
353
0
              if ((c=iobuf_get (ed->buf)) == -1)
354
0
                break;
355
0
              dfx->startiv[i] = c;
356
0
            }
357
0
        }
358
0
      else
359
0
        {
360
0
          for (i=0; i < startivlen; i++ )
361
0
            if ( (c=iobuf_get (ed->buf)) == -1 )
362
0
              break;
363
0
            else
364
0
              dfx->startiv[i] = c;
365
0
        }
366
0
      if (i != startivlen)
367
0
        {
368
0
          log_error ("Start-IV in AEAD packet too short (%d/%u)\n",
369
0
                     i, startivlen);
370
0
          rc = gpg_error (GPG_ERR_TOO_SHORT);
371
0
          goto leave;
372
0
        }
373
374
0
      dfx->cipher_algo = ed->cipher_algo;
375
0
      dfx->aead_algo = ed->aead_algo;
376
0
      dfx->chunkbyte = ed->chunkbyte;
377
0
      dfx->chunksize = (uint64_t)1 << (dfx->chunkbyte + 6);
378
379
0
      if (dek->algo != dfx->cipher_algo)
380
0
        log_info ("Note: different cipher algorithms used (%s/%s)\n",
381
0
                  openpgp_cipher_algo_name (dek->algo),
382
0
                  openpgp_cipher_algo_name (dfx->cipher_algo));
383
384
0
      rc = openpgp_cipher_open (&dfx->cipher_hd,
385
0
                                dfx->cipher_algo,
386
0
                                ciphermode,
387
0
                                GCRY_CIPHER_SECURE);
388
0
      if (rc)
389
0
        goto leave; /* Should never happen.  */
390
391
0
      if (DBG_CRYPTO)
392
0
        log_printhex (dek->key, dek->keylen, "thekey:");
393
0
      rc = gcry_cipher_setkey (dfx->cipher_hd, dek->key, dek->keylen);
394
0
      if (gpg_err_code (rc) == GPG_ERR_WEAK_KEY)
395
0
        {
396
0
          log_info (_("WARNING: message was encrypted with"
397
0
                      " a weak key in the symmetric cipher.\n"));
398
0
          rc = 0;
399
0
        }
400
0
      else if (rc)
401
0
        {
402
0
          log_error("key setup failed: %s\n", gpg_strerror (rc));
403
0
          goto leave;
404
0
        }
405
406
0
      if (!ed->buf)
407
0
        {
408
0
          log_error(_("problem handling encrypted packet\n"));
409
0
          goto leave;
410
0
        }
411
412
0
    }
413
0
  else /* CFB encryption.  */
414
0
    {
415
0
      nprefix = blocksize;
416
0
      if ( ed->len && ed->len < (nprefix+2) )
417
0
        {
418
          /* An invalid message.  We can't check that during parsing
419
           * because we may not know the used cipher then.  */
420
0
          rc = gpg_error (GPG_ERR_INV_PACKET);
421
0
          goto leave;
422
0
        }
423
424
0
      if ( ed->mdc_method )
425
0
        {
426
0
          if (gcry_md_open (&dfx->mdc_hash, ed->mdc_method, 0 ))
427
0
            BUG ();
428
0
          if ( DBG_HASHING )
429
0
            gcry_md_debug (dfx->mdc_hash, "checkmdc");
430
0
        }
431
432
0
      rc = openpgp_cipher_open (&dfx->cipher_hd, dek->algo,
433
0
                                GCRY_CIPHER_MODE_CFB,
434
0
                                (GCRY_CIPHER_SECURE
435
0
                                 | ((ed->mdc_method || dek->algo >= 100)?
436
0
                                    0 : GCRY_CIPHER_ENABLE_SYNC)));
437
0
      if (rc)
438
0
        {
439
          /* We should never get an error here cause we already checked
440
           * that the algorithm is available.  */
441
0
          BUG();
442
0
        }
443
444
445
      /* log_hexdump( "thekey", dek->key, dek->keylen );*/
446
0
      rc = gcry_cipher_setkey (dfx->cipher_hd, dek->key, dek->keylen);
447
0
      if ( gpg_err_code (rc) == GPG_ERR_WEAK_KEY )
448
0
        {
449
0
          log_info (_("WARNING: message was encrypted with"
450
0
                      " a weak key in the symmetric cipher.\n"));
451
0
          rc=0;
452
0
        }
453
0
      else if (rc)
454
0
        {
455
0
          log_error ("key setup failed: %s\n", gpg_strerror (rc) );
456
0
          goto leave;
457
0
        }
458
459
0
      if (!ed->buf)
460
0
        {
461
0
          log_error (_("problem handling encrypted packet\n"));
462
0
          rc = gpg_error (GPG_ERR_INV_PACKET);
463
0
          goto leave;
464
0
        }
465
466
0
      gcry_cipher_setiv (dfx->cipher_hd, NULL, 0);
467
468
0
      if ( ed->len )
469
0
        {
470
0
          for (i=0; i < (nprefix+2) && ed->len; i++, ed->len-- )
471
0
            {
472
0
              if ( (c=iobuf_get(ed->buf)) == -1 )
473
0
                break;
474
0
              else
475
0
                temp[i] = c;
476
0
            }
477
0
        }
478
0
      else
479
0
        {
480
0
          for (i=0; i < (nprefix+2); i++ )
481
0
            if ( (c=iobuf_get(ed->buf)) == -1 )
482
0
              break;
483
0
            else
484
0
              temp[i] = c;
485
0
        }
486
487
0
      gcry_cipher_decrypt (dfx->cipher_hd, temp, nprefix+2, NULL, 0);
488
0
      gcry_cipher_sync (dfx->cipher_hd);
489
0
      p = temp;
490
      /* log_hexdump( "prefix", temp, nprefix+2 ); */
491
0
      if (dek->symmetric
492
0
          && (p[nprefix-2] != p[nprefix] || p[nprefix-1] != p[nprefix+1]) )
493
0
        {
494
0
          rc = gpg_error (GPG_ERR_BAD_KEY);
495
0
          goto leave;
496
0
        }
497
498
0
      if ( dfx->mdc_hash )
499
0
        gcry_md_write (dfx->mdc_hash, temp, nprefix+2);
500
0
    }
501
502
0
  dfx->refcount++;
503
0
  dfx->partial = !!ed->is_partial;
504
0
  dfx->length = ed->len;
505
0
  dfx->checktag_failed = 0;
506
0
  if (ed->aead_algo)
507
0
    iobuf_push_filter ( ed->buf, aead_decode_filter, dfx );
508
0
  else if (ed->mdc_method)
509
0
    iobuf_push_filter ( ed->buf, mdc_decode_filter, dfx );
510
0
  else
511
0
    iobuf_push_filter ( ed->buf, decode_filter, dfx );
512
513
0
  if (opt.unwrap_encryption)
514
0
    {
515
0
      struct pfg pfg;
516
517
0
      rc = pfg_open_file ("", 0, ed->buf, &pfg);
518
0
      if (! rc)
519
0
        {
520
0
          iobuf_t output = iobuf_esopen (pfg.fp, "w", 1, 0);
521
0
          armor_filter_context_t *afx = NULL;
522
523
0
    es_setbuf (pfg.fp, NULL);
524
525
0
          if (opt.armor)
526
0
            {
527
0
              afx = new_armor_context ();
528
0
              push_armor_filter (afx, output);
529
0
            }
530
531
0
          iobuf_copy (output, ed->buf);
532
0
          if ((rc = iobuf_error (ed->buf)))
533
0
            log_error (_("error reading '%s': %s\n"),
534
0
                       ed->buf->real_fname, gpg_strerror (rc));
535
0
          else if ((rc = iobuf_error (output)))
536
0
            log_error (_("error writing '%s': %s\n"),
537
0
                       pfg.fname, gpg_strerror (rc));
538
539
0
          iobuf_close (output);
540
0
          release_armor_context (afx);
541
0
          pfg_close_file (&pfg, rc);
542
0
        }
543
0
    }
544
0
  else
545
0
    proc_packets (ctrl, procctx, ed->buf );
546
547
0
  ed->buf = NULL;
548
0
  if (dfx->eof_seen > 1 )
549
0
    rc = gpg_error (GPG_ERR_INV_PACKET);
550
0
  else if (dfx->checktag_failed)
551
0
    {
552
0
      rc = gpg_error (GPG_ERR_BAD_SIGNATURE);
553
0
    }
554
0
  else if ( ed->mdc_method )
555
0
    {
556
      /* We used to let parse-packet.c handle the MDC packet but this
557
         turned out to be a problem with compressed packets: With old
558
         style packets there is no length information available and
559
         the decompressor uses an implicit end.  However we can't know
560
         this implicit end beforehand (:-) and thus may feed the
561
         decompressor with more bytes than actually needed.  It would
562
         be possible to unread the extra bytes but due to our weird
563
         iobuf system any unread is non reliable due to filters
564
         already popped off.  The easy and sane solution is to care
565
         about the MDC packet only here and never pass it to the
566
         packet parser.  Fortunatley the OpenPGP spec requires a
567
         strict format for the MDC packet so that we know that 22
568
         bytes are appended.  */
569
0
      int datalen = gcry_md_get_algo_dlen (ed->mdc_method);
570
571
0
      log_assert (dfx->cipher_hd);
572
0
      log_assert (dfx->mdc_hash);
573
0
      gcry_cipher_decrypt (dfx->cipher_hd, dfx->holdback, 22, NULL, 0);
574
0
      gcry_md_write (dfx->mdc_hash, dfx->holdback, 2);
575
0
      gcry_md_final (dfx->mdc_hash);
576
577
0
      if (   dfx->holdback[0] != '\xd3'
578
0
          || dfx->holdback[1] != '\x14'
579
0
          || datalen != 20
580
0
          || memcmp (gcry_md_read (dfx->mdc_hash, 0), dfx->holdback+2, datalen))
581
0
        {
582
0
          log_error ("MDC check failed: %s\n", gpg_strerror (GPG_ERR_CHECKSUM));
583
0
          write_status_error ("mdc_check", GPG_ERR_CHECKSUM);
584
0
          rc = gpg_error (GPG_ERR_BAD_SIGNATURE);
585
0
        }
586
      /* log_printhex("MDC message:", dfx->holdback, 22); */
587
      /* log_printhex("MDC calc:", gcry_md_read (dfx->mdc_hash,0), datalen); */
588
0
    }
589
590
0
 leave:
591
0
  release_dfx_context (dfx);
592
0
  return rc;
593
0
}
594
595
596
/* Fill BUFFER with up to NBYTES-OFFSET from STREAM utilizing
597
 * information from the context DFX.  Returns the new offset which is
598
 * the number of bytes read plus the original offset.  On EOF the
599
 * respective flag in DFX is set. */
600
static size_t
601
fill_buffer (decode_filter_ctx_t dfx, iobuf_t stream,
602
             byte *buffer, size_t nbytes, size_t offset)
603
0
{
604
0
  size_t nread = offset;
605
0
  size_t curr;
606
0
  int ret;
607
608
0
  if (dfx->partial)
609
0
    {
610
0
      while (nread < nbytes)
611
0
        {
612
0
          curr = nbytes - nread;
613
614
0
          ret = iobuf_read (stream, &buffer[nread], curr);
615
0
          if (ret == -1)
616
0
            {
617
0
              dfx->eof_seen = 1; /* Normal EOF. */
618
0
              break;
619
0
            }
620
621
0
          nread += ret;
622
0
        }
623
0
    }
624
0
  else
625
0
    {
626
0
      while (nread < nbytes && dfx->length)
627
0
        {
628
0
          curr = nbytes - nread;
629
0
          if (curr > dfx->length)
630
0
            curr = dfx->length;
631
632
0
          ret = iobuf_read (stream, &buffer[nread], curr);
633
0
          if (ret == -1)
634
0
            {
635
0
              dfx->eof_seen = 3; /* Premature EOF. */
636
0
              break;
637
0
            }
638
639
0
          nread += ret;
640
0
          dfx->length -= ret;
641
0
        }
642
0
      if (!dfx->length)
643
0
        dfx->eof_seen = 1; /* Normal EOF.  */
644
0
    }
645
646
0
  return nread;
647
0
}
648
649
650
/* The core of the AEAD decryption.  This is the underflow function of
651
 * the aead_decode_filter.  */
652
static gpg_error_t
653
aead_underflow (decode_filter_ctx_t dfx, iobuf_t a, byte *buf, size_t *ret_len)
654
0
{
655
0
  const size_t size = *ret_len; /* The allocated size of BUF.  */
656
0
  gpg_error_t err = 0;
657
0
  size_t totallen = 0; /* The number of bytes to return on success or EOF.  */
658
0
  size_t off = 0;      /* The offset into the buffer.  */
659
0
  size_t len;          /* The current number of bytes in BUF+OFF.  */
660
661
0
  log_assert (size > 48); /* Our code requires at least this size.  */
662
663
  /* Copy the rest from the last call of this function into BUF.  */
664
0
  len = dfx->holdbacklen;
665
0
  dfx->holdbacklen = 0;
666
0
  memcpy (buf, dfx->holdback, len);
667
668
0
  if (DBG_FILTER)
669
0
    log_debug ("aead_underflow: size=%zu len=%zu%s%s\n", size, len,
670
0
               dfx->partial? " partial":"", dfx->eof_seen? " eof":"");
671
672
  /* Read and fill up BUF.  We need to watch out for an EOF so that we
673
   * can detect the last chunk which is commonly shorter than the
674
   * chunksize.  After the last data byte from the last chunk 32 more
675
   * bytes are expected for the last chunk's tag and the following
676
   * final chunk's tag.  To detect the EOF we need to try reading at least
677
   * one further byte; however we try to read 16 extra bytes to avoid
678
   * single byte reads in some lower layers.  The outcome is that we
679
   * have up to 48 extra extra octets which we will later put into the
680
   * holdback buffer for the next invocation (which handles the EOF
681
   * case).  */
682
0
  len = fill_buffer (dfx, a, buf, size, len);
683
0
  if (len < 32)
684
0
    {
685
      /* Not enough data for the last two tags.  */
686
0
      err = gpg_error (GPG_ERR_TRUNCATED);
687
0
      goto leave;
688
0
    }
689
0
  if (dfx->eof_seen)
690
0
    {
691
      /* If have seen an EOF we copy only the last two auth tags into
692
       * the holdback buffer.  */
693
0
      dfx->holdbacklen = 32;
694
0
      memcpy (dfx->holdback, buf+len-32, 32);
695
0
      len -= 32;
696
0
    }
697
0
  else
698
0
    {
699
      /* If have not seen an EOF we copy the entire extra 48 bytes
700
       * into the holdback buffer for processing at the next call of
701
       * this function.  */
702
0
      dfx->holdbacklen = len > 48? 48 : len;
703
0
      memcpy (dfx->holdback, buf+len-dfx->holdbacklen, dfx->holdbacklen);
704
0
      len -= dfx->holdbacklen;
705
0
    }
706
  /* log_printhex (dfx->holdback, dfx->holdbacklen, "holdback:"); */
707
708
  /* Decrypt the buffer.  This first requires a loop to handle the
709
   * case when a chunk ends within the buffer.  */
710
0
  if (DBG_FILTER)
711
0
    log_debug ("decrypt: chunklen=%llu total=%llu size=%zu len=%zu%s\n",
712
0
               (unsigned long long)dfx->chunklen,
713
0
               (unsigned long long)dfx->total,
714
0
               size, len,
715
0
               dfx->eof_seen? " eof":"");
716
717
0
  while (len && dfx->chunklen + len >= dfx->chunksize)
718
0
    {
719
0
      size_t n = dfx->chunksize - dfx->chunklen;
720
0
      byte tagbuf[16];
721
722
0
      if (DBG_FILTER)
723
0
        log_debug ("chunksize will be reached: n=%zu\n", n);
724
725
0
      if (!dfx->chunklen)
726
0
        {
727
          /* First data for this chunk - prepare.  */
728
0
          err = aead_set_nonce_and_ad (dfx, 0);
729
0
          if (err)
730
0
            goto leave;
731
0
        }
732
733
      /* log_printhex (buf, n, "ciph:"); */
734
0
      gcry_cipher_final (dfx->cipher_hd);
735
0
      err = gcry_cipher_decrypt (dfx->cipher_hd, buf+off, n, NULL, 0);
736
0
      if (err)
737
0
        {
738
0
          log_error ("gcry_cipher_decrypt failed (1): %s\n",
739
0
                     gpg_strerror (err));
740
0
          goto leave;
741
0
        }
742
      /* log_printhex (buf, n, "plai:"); */
743
0
      totallen += n;
744
0
      dfx->chunklen += n;
745
0
      dfx->total += n;
746
0
      off += n;
747
0
      len -= n;
748
749
0
      if (DBG_FILTER)
750
0
        log_debug ("ndecrypted: %zu (nchunk=%llu) bytes left: %zu at off=%zu\n",
751
0
                   totallen, (unsigned long long)dfx->chunklen, len, off);
752
753
      /* Check the tag.  */
754
0
      if (len < 16)
755
0
        {
756
          /* The tag is not entirely in the buffer.  Read the rest of
757
           * the tag from the holdback buffer.  Then shift the holdback
758
           * buffer and fill it up again.  */
759
0
          memcpy (tagbuf, buf+off, len);
760
0
          memcpy (tagbuf + len, dfx->holdback, 16 - len);
761
0
          dfx->holdbacklen -= 16-len;
762
0
          memmove (dfx->holdback, dfx->holdback + (16-len), dfx->holdbacklen);
763
764
0
          if (dfx->eof_seen)
765
0
            {
766
              /* We should have the last chunk's tag in TAGBUF and the
767
               * final tag in HOLDBACKBUF.  */
768
0
              if (len || dfx->holdbacklen != 16)
769
0
                {
770
                  /* Not enough data for the last two tags.  */
771
0
                  err = gpg_error (GPG_ERR_TRUNCATED);
772
0
                  goto leave;
773
0
                }
774
0
            }
775
0
          else
776
0
            {
777
0
              len = 0;
778
0
              dfx->holdbacklen = fill_buffer (dfx, a, dfx->holdback, 48,
779
0
                                              dfx->holdbacklen);
780
0
              if (dfx->holdbacklen < 32)
781
0
                {
782
                  /* Not enough data for the last two tags.  */
783
0
                  err = gpg_error (GPG_ERR_TRUNCATED);
784
0
                  goto leave;
785
0
                }
786
0
            }
787
0
        }
788
0
      else /* We already have the full tag.  */
789
0
        {
790
0
          memcpy (tagbuf, buf+off, 16);
791
          /* Remove that tag from the output.  */
792
0
          memmove (buf + off, buf + off + 16, len - 16);
793
0
          len -= 16;
794
0
        }
795
0
      err = aead_checktag (dfx, 0, tagbuf);
796
0
      if (err)
797
0
        goto leave;
798
0
      dfx->chunklen = 0;
799
0
      dfx->chunkindex++;
800
801
0
      continue;
802
0
    }
803
804
  /* The bulk decryption of our buffer.  */
805
0
  if (len)
806
0
    {
807
0
      if (!dfx->chunklen)
808
0
        {
809
          /* First data for this chunk - prepare.  */
810
0
          err = aead_set_nonce_and_ad (dfx, 0);
811
0
          if (err)
812
0
            goto leave;
813
0
        }
814
815
0
      if (dfx->eof_seen)
816
0
        {
817
          /* This is the last block of the last chunk.  Its length may
818
           * not be a multiple of the block length.  */
819
0
          gcry_cipher_final (dfx->cipher_hd);
820
0
        }
821
0
      err = gcry_cipher_decrypt (dfx->cipher_hd, buf + off, len, NULL, 0);
822
0
      if (err)
823
0
        {
824
0
          log_error ("gcry_cipher_decrypt failed (2): %s\n",
825
0
                     gpg_strerror (err));
826
0
          goto leave;
827
0
        }
828
0
      totallen += len;
829
0
      dfx->chunklen += len;
830
0
      dfx->total += len;
831
0
      if (DBG_FILTER)
832
0
        log_debug ("ndecrypted: %zu (nchunk=%llu)\n",
833
0
                   totallen, (unsigned long long)dfx->chunklen);
834
0
    }
835
836
0
  if (dfx->eof_seen)
837
0
    {
838
839
0
      if (dfx->chunklen)
840
0
        {
841
0
          if (DBG_FILTER)
842
0
            log_debug ("eof seen: holdback has the last and final tag\n");
843
0
          log_assert (dfx->holdbacklen >= 32);
844
0
          err = aead_checktag (dfx, 0, dfx->holdback);
845
0
          if (err)
846
0
            goto leave;
847
0
          dfx->chunklen = 0;
848
0
          dfx->chunkindex++;
849
0
          off = 16;
850
0
        }
851
0
      else
852
0
        {
853
0
          if (DBG_FILTER)
854
0
            log_debug ("eof seen: holdback has the final tag\n");
855
0
          log_assert (dfx->holdbacklen >= 16);
856
0
          off = 0;
857
0
        }
858
859
      /* Check the final chunk.  */
860
0
      err = aead_set_nonce_and_ad (dfx, 1);
861
0
      if (err)
862
0
        goto leave;
863
0
      gcry_cipher_final (dfx->cipher_hd);
864
      /* Decrypt an empty string (using HOLDBACK as a dummy).  */
865
0
      err = gcry_cipher_decrypt (dfx->cipher_hd, dfx->holdback, 0, NULL, 0);
866
0
      if (err)
867
0
        {
868
0
          log_error ("gcry_cipher_decrypt failed (final): %s\n",
869
0
                     gpg_strerror (err));
870
0
          goto leave;
871
0
        }
872
0
      err = aead_checktag (dfx, 1, dfx->holdback+off);
873
0
      if (err)
874
0
        goto leave;
875
0
      err = gpg_error (GPG_ERR_EOF);
876
0
    }
877
878
0
 leave:
879
0
  if (DBG_FILTER)
880
0
    log_debug ("aead_underflow: returning %zu (%s)\n",
881
0
               totallen, gpg_strerror (err));
882
883
  /* In case of an error we better wipe out the buffer than to convey
884
   * partly decrypted data.  */
885
0
  if (err && gpg_err_code (err) != GPG_ERR_EOF)
886
0
    memset (buf, 0, size);
887
888
0
  *ret_len = totallen;
889
890
0
  return err;
891
0
}
892
893
894
/* The IOBUF filter used to decrypt AEAD encrypted data.  */
895
static int
896
aead_decode_filter (void *opaque, int control, IOBUF a,
897
                    byte *buf, size_t *ret_len)
898
0
{
899
0
  decode_filter_ctx_t dfx = opaque;
900
0
  int rc = 0;
901
902
0
  if ( control == IOBUFCTRL_UNDERFLOW && dfx->eof_seen )
903
0
    {
904
0
      *ret_len = 0;
905
0
      rc = -1;
906
0
    }
907
0
  else if ( control == IOBUFCTRL_UNDERFLOW )
908
0
    {
909
0
      log_assert (a);
910
911
0
      rc = aead_underflow (dfx, a, buf, ret_len);
912
0
      if (gpg_err_code (rc) == GPG_ERR_EOF)
913
0
        rc = -1; /* We need to use the old convention in the filter.  */
914
915
0
    }
916
0
  else if ( control == IOBUFCTRL_FREE )
917
0
    {
918
0
      release_dfx_context (dfx);
919
0
    }
920
0
  else if ( control == IOBUFCTRL_DESC )
921
0
    {
922
0
      mem2str (buf, "aead_decode_filter", *ret_len);
923
0
    }
924
925
0
  return rc;
926
0
}
927
928
929
static int
930
mdc_decode_filter (void *opaque, int control, IOBUF a,
931
                   byte *buf, size_t *ret_len)
932
0
{
933
0
  decode_filter_ctx_t dfx = opaque;
934
0
  size_t n, size = *ret_len;
935
0
  int rc = 0;
936
937
  /* Note: We need to distinguish between a partial and a fixed length
938
     packet.  The first is the usual case as created by GPG.  However
939
     for short messages the format degrades to a fixed length packet
940
     and other implementations might use fixed length as well.  Only
941
     looking for the EOF on fixed data works only if the encrypted
942
     packet is not followed by other data.  This used to be a long
943
     standing bug which was fixed on 2009-10-02.  */
944
945
0
  if ( control == IOBUFCTRL_UNDERFLOW && dfx->eof_seen )
946
0
    {
947
0
      *ret_len = 0;
948
0
      rc = -1;
949
0
    }
950
0
  else if( control == IOBUFCTRL_UNDERFLOW )
951
0
    {
952
0
      log_assert (a);
953
0
      log_assert (size > 44); /* Our code requires at least this size.  */
954
955
      /* Get at least 22 bytes and put it ahead in the buffer.  */
956
0
      n = fill_buffer (dfx, a, buf, 44, 22);
957
0
      if (n == 44)
958
0
        {
959
          /* We have enough stuff - flush the holdback buffer.  */
960
0
          if ( !dfx->holdbacklen )  /* First time. */
961
0
            {
962
0
              memcpy (buf, buf+22, 22);
963
0
              n = 22;
964
0
      }
965
0
          else
966
0
            {
967
0
              memcpy (buf, dfx->holdback, 22);
968
0
      }
969
970
          /* Fill up the buffer. */
971
0
          n = fill_buffer (dfx, a, buf, size, n);
972
973
          /* Move the trailing 22 bytes back to the holdback buffer.  We
974
             have at least 44 bytes thus a memmove is not needed.  */
975
0
          n -= 22;
976
0
          memcpy (dfx->holdback, buf+n, 22 );
977
0
          dfx->holdbacklen = 22;
978
0
  }
979
0
      else if ( !dfx->holdbacklen ) /* EOF seen but empty holdback. */
980
0
        {
981
          /* This is bad because it means an incomplete hash. */
982
0
          n -= 22;
983
0
          memcpy (buf, buf+22, n );
984
0
          dfx->eof_seen = 2; /* EOF with incomplete hash.  */
985
0
  }
986
0
      else  /* EOF seen (i.e. read less than 22 bytes). */
987
0
        {
988
0
          memcpy (buf, dfx->holdback, 22 );
989
0
          n -= 22;
990
0
          memcpy (dfx->holdback, buf+n, 22 );
991
0
          dfx->eof_seen = 1; /* Normal EOF. */
992
0
  }
993
994
0
      if ( n )
995
0
        {
996
0
          if ( dfx->cipher_hd )
997
0
            gcry_cipher_decrypt (dfx->cipher_hd, buf, n, NULL, 0);
998
0
          if ( dfx->mdc_hash )
999
0
            gcry_md_write (dfx->mdc_hash, buf, n);
1000
0
  }
1001
0
      else
1002
0
        {
1003
0
          log_assert ( dfx->eof_seen );
1004
0
          rc = -1; /* Return EOF.  */
1005
0
  }
1006
0
      *ret_len = n;
1007
0
    }
1008
0
  else if ( control == IOBUFCTRL_FREE )
1009
0
    {
1010
0
      release_dfx_context (dfx);
1011
0
    }
1012
0
  else if ( control == IOBUFCTRL_DESC )
1013
0
    {
1014
0
      mem2str (buf, "mdc_decode_filter", *ret_len);
1015
0
    }
1016
0
  return rc;
1017
0
}
1018
1019
1020
static int
1021
decode_filter( void *opaque, int control, IOBUF a, byte *buf, size_t *ret_len)
1022
0
{
1023
0
  decode_filter_ctx_t fc = opaque;
1024
0
  size_t size = *ret_len;
1025
0
  size_t n;
1026
0
  int rc = 0;
1027
1028
1029
0
  if ( control == IOBUFCTRL_UNDERFLOW && fc->eof_seen )
1030
0
    {
1031
0
      *ret_len = 0;
1032
0
      rc = -1;
1033
0
    }
1034
0
  else if ( control == IOBUFCTRL_UNDERFLOW )
1035
0
    {
1036
0
      log_assert (a);
1037
1038
0
      n = fill_buffer (fc, a, buf, size, 0);
1039
0
      if (n)
1040
0
        {
1041
0
          if (fc->cipher_hd)
1042
0
            gcry_cipher_decrypt (fc->cipher_hd, buf, n, NULL, 0);
1043
0
        }
1044
0
      else
1045
0
        {
1046
0
          if (!fc->eof_seen)
1047
0
            fc->eof_seen = 1;
1048
0
          rc = -1; /* Return EOF. */
1049
0
        }
1050
0
      *ret_len = n;
1051
0
    }
1052
0
  else if ( control == IOBUFCTRL_FREE )
1053
0
    {
1054
0
      release_dfx_context (fc);
1055
0
    }
1056
0
  else if ( control == IOBUFCTRL_DESC )
1057
0
    {
1058
0
      mem2str (buf, "decode_filter", *ret_len);
1059
0
    }
1060
0
  return rc;
1061
0
}