Coverage Report

Created: 2026-08-13 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnupg/g10/plaintext.c
Line
Count
Source
1
/* plaintext.c -  process plaintext packets
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3
 *               2006, 2009, 2010 Free Software Foundation, Inc.
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
 */
20
21
#include <config.h>
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
#include <errno.h>
26
#include <sys/types.h>
27
#ifdef HAVE_DOSISH_SYSTEM
28
# include <fcntl.h> /* for setmode() */
29
#endif
30
31
#include "gpg.h"
32
#include "../common/util.h"
33
#include "options.h"
34
#include "packet.h"
35
#include "../common/ttyio.h"
36
#include "filter.h"
37
#include "main.h"
38
#include "../common/status.h"
39
#include "../common/i18n.h"
40
41
42
/* Get the output filename.  On success, the actual filename that is
43
   used is set in *FNAMEP and a filepointer is returned in *FP.
44
45
   EMBEDDED_NAME AND EMBEDDED_NAMELEN are normally stored in a
46
   plaintext packet.  EMBEDDED_NAMELEN should not include any NUL
47
   terminator (EMBEDDED_NAME does not need to be NUL terminated).
48
49
   DATA is the iobuf containing the input data.  We just use it to get
50
   the input file's filename.
51
52
   On success, the caller is responsible for calling xfree on *FNAMEP
53
   and calling es_close on *FPP.  */
54
static gpg_error_t
55
get_output_file (const byte *embedded_name, int embedded_namelen,
56
                 iobuf_t data, struct pfg *pfg)
57
1.47k
{
58
1.47k
  gpg_error_t err = 0;
59
1.47k
  char *fname = NULL;
60
1.47k
  char *fname_part = NULL;
61
1.47k
  estream_t fp = NULL;
62
1.47k
  int nooutput = 0;
63
1.47k
  int using_file = 0;
64
65
  /* Create the filename as C string.  */
66
1.47k
  if (opt.outfp)
67
0
    {
68
0
      fname = xtrystrdup ("[FP]");
69
0
      if (!fname)
70
0
        {
71
0
          err = gpg_error_from_syserror ();
72
0
          goto leave;
73
0
        }
74
0
    }
75
1.47k
  else if (opt.outfile
76
789
           && !(opt.flags.use_embedded_filename && opt.flags.dummy_outfile))
77
789
    {
78
789
      fname = xtrystrdup (opt.outfile);
79
789
      if (!fname)
80
0
        {
81
0
          err = gpg_error_from_syserror ();
82
0
          goto leave;
83
0
        }
84
789
    }
85
682
  else if (embedded_namelen == 8 && !memcmp (embedded_name, "_CONSOLE", 8))
86
5
    {
87
5
      log_info (_("data not saved; use option \"--output\" to save it\n"));
88
5
      nooutput = 1;
89
5
    }
90
677
  else if (!opt.flags.use_embedded_filename)
91
677
    {
92
677
      if (data)
93
677
        fname = make_outfile_name (iobuf_get_real_fname (data));
94
677
      if (!fname)
95
0
  fname = ask_outfile_name (embedded_name, embedded_namelen);
96
677
      if (!fname)
97
0
  {
98
0
    err = gpg_error (GPG_ERR_GENERAL);  /* Can't create file. */
99
0
    goto leave;
100
0
  }
101
677
    }
102
0
  else
103
0
    fname = utf8_to_native (embedded_name, embedded_namelen, 0);
104
105
1.47k
  if (nooutput)
106
5
    ;
107
1.46k
  else if (opt.outfp)
108
0
    {
109
0
      fp = opt.outfp;
110
0
      es_set_binary (fp);
111
0
    }
112
1.46k
  else if (iobuf_is_pipe_filename (fname) || !*fname)
113
789
    {
114
      /* Special file name, no filename, or "-" given; write to the
115
       * file descriptor or to stdout. */
116
789
      gnupg_fd_t fd;
117
789
      char xname[64];
118
119
789
      fd = gnupg_check_special_filename (fname);
120
789
      if (fd == GNUPG_INVALID_FD)
121
789
        {
122
          /* Not a special filename, thus we want stdout.  */
123
789
          fp = es_stdout;
124
789
          es_set_binary (fp);
125
789
        }
126
0
      else if (!(fp = open_stream_nc (fd, "wb")))
127
0
        {
128
0
          err = gpg_error_from_syserror ();
129
0
          snprintf (xname, sizeof xname, "[fd %d]", FD_DBG (fd));
130
0
          log_error (_("can't open '%s': %s\n"), xname, gpg_strerror (err));
131
0
          goto leave;
132
0
        }
133
789
    }
134
677
  else
135
677
    {
136
677
      while (!overwrite_filep (fname))
137
0
  {
138
0
    char *tmp = ask_outfile_name (NULL, 0);
139
0
    if (!tmp || !*tmp)
140
0
      {
141
0
        xfree (tmp);
142
0
              err = gpg_error (GPG_ERR_EEXIST);
143
0
        goto leave;
144
0
      }
145
0
    xfree (fname);
146
0
    fname = tmp;
147
0
  }
148
677
    }
149
150
1.47k
  if (fp || nooutput)
151
794
    ;
152
677
  else if (is_secured_filename (fname))
153
0
    {
154
0
      gpg_err_set_errno (EPERM);
155
0
      err = gpg_error_from_syserror ();
156
0
      log_error (_("error creating '%s': %s\n"), fname, gpg_strerror (err));
157
0
      goto leave;
158
0
    }
159
677
  else
160
677
    {
161
677
      char *filename;
162
163
677
      if ((opt.compat_flags & COMPAT_NO_PARTIALFILEGUARD)
164
          /* Don't enable the partial file guard if it's already prepared.  */
165
677
          || has_suffix (fname, EXTSEP_S "part"))
166
0
        filename = fname;
167
677
      else
168
677
        {
169
677
          filename = xstrconcat (fname, EXTSEP_S "part", NULL);
170
677
          if (!filename)
171
0
            {
172
0
              err = gpg_error_from_syserror ();
173
0
              log_error ("error building .part filename '%s': %s\n",
174
0
                         fname, gpg_strerror (err));
175
0
              goto leave;
176
0
            }
177
677
          fname_part = filename;
178
677
        }
179
180
677
      if (!(fp = es_fopen (filename, "wb")))
181
0
        {
182
0
          err = gpg_error_from_syserror ();
183
0
          log_error (_("error creating '%s': %s\n"), filename,
184
0
                     gpg_strerror (err));
185
0
          goto leave;
186
0
        }
187
677
      using_file = 1;
188
677
    }
189
190
1.47k
 leave:
191
1.47k
  if (err)
192
0
    {
193
0
      if (fp && fp != es_stdout && fp != opt.outfp)
194
0
        es_fclose (fp);
195
0
      xfree (fname);
196
0
      xfree (fname_part);
197
0
      return err;
198
0
    }
199
200
1.47k
  pfg->using_file = using_file;
201
1.47k
  pfg->fname_part = fname_part;
202
1.47k
  pfg->fname = fname;
203
1.47k
  pfg->fp = fp;
204
1.47k
  return 0;
205
1.47k
}
206
207
208
gpg_error_t
209
pfg_open_file (const byte *embedded_name, int embedded_namelen,
210
               iobuf_t data, struct pfg *pfg)
211
1.47k
{
212
1.47k
  return get_output_file (embedded_name, embedded_namelen, data, pfg);
213
1.47k
}
214
215
void
216
pfg_close_file (struct pfg *pfg, gpg_error_t err)
217
677
{
218
677
  if (err)
219
155
    {
220
155
      gpgrt_fcancel (pfg->fp);
221
155
      if (pfg->using_file)
222
155
        {
223
155
          const char *fname;
224
225
155
          if (pfg->fname_part)
226
155
            fname = pfg->fname_part;
227
0
          else
228
0
            fname = pfg->fname;
229
230
155
          if (opt.verbose)
231
155
            log_info (_("Note: partial file '%s' removed\n"), fname);
232
233
155
          gnupg_remove (fname);
234
155
        }
235
155
    }
236
522
  else
237
522
    {
238
522
      if (es_fclose (pfg->fp))
239
0
        {
240
0
          err = gpg_error_from_syserror ();
241
0
          log_error ("error closing '%s': %s\n",
242
0
                     pfg->fname_part ? pfg->fname_part : pfg->fname,
243
0
                     gpg_strerror (err));
244
0
        }
245
246
522
      if (pfg->fname_part)
247
522
        gnupg_register_partial_file (pfg->fname_part, pfg->fname);
248
522
    }
249
250
677
  xfree (pfg->fname);
251
677
  xfree (pfg->fname_part);
252
677
}
253
254
255
/* Handle a plaintext packet.  If MFX is not NULL, update the MDs
256
 * Note: We should have used the filter stuff here, but we have to add
257
 * some easy mimic to set a read limit, so we calculate only the bytes
258
 * from the plaintext.  */
259
int
260
handle_plaintext (PKT_plaintext * pt, md_filter_context_t * mfx,
261
      int nooutput, int clearsig)
262
1.47k
{
263
1.47k
  char *fname = NULL;
264
1.47k
  estream_t fp = NULL;
265
1.47k
  static off_t count = 0;
266
1.47k
  int err = 0;
267
1.47k
  int c;
268
1.47k
  int convert;
269
1.47k
  struct pfg pfg;
270
271
1.47k
  if (pt->mode == 't' || pt->mode == 'u' || pt->mode == 'm')
272
550
    convert = pt->mode;
273
921
  else
274
921
    convert = 0;
275
276
  /* Let people know what the plaintext info is. This allows the
277
     receiving program to try and do something different based on the
278
     format code (say, recode UTF-8 to local). */
279
1.47k
  if (!nooutput && is_status_enabled ())
280
0
    {
281
0
      char status[50];
282
283
      /* Better make sure that stdout has been flushed in case the
284
         output will be written to it.  This is to make sure that no
285
         not-yet-flushed stuff will be written after the plaintext
286
         status message.  */
287
0
      es_fflush (es_stdout);
288
289
0
      snprintf (status, sizeof status,
290
0
                "%X %lu ", (byte) pt->mode, (ulong) pt->timestamp);
291
0
      write_status_text_and_buffer (STATUS_PLAINTEXT,
292
0
            status, pt->name, pt->namelen, 0);
293
294
0
      if (!pt->is_partial)
295
0
  {
296
0
    snprintf (status, sizeof status, "%lu", (ulong) pt->len);
297
0
    write_status_text (STATUS_PLAINTEXT_LENGTH, status);
298
0
  }
299
0
    }
300
301
1.47k
  if (! nooutput)
302
1.47k
    {
303
1.47k
      err = pfg_open_file (pt->name, pt->namelen, pt->buf, &pfg);
304
1.47k
      if (err)
305
0
        goto leave;
306
1.47k
      fp = pfg.fp;
307
1.47k
      fname = pfg.fname;
308
1.47k
    }
309
310
1.47k
  if (!pt->is_partial)
311
705
    {
312
      /* We have an actual length (which might be zero). */
313
314
705
      if (clearsig)
315
0
  {
316
0
    log_error ("clearsig encountered while not expected\n");
317
0
    err = gpg_error (GPG_ERR_UNEXPECTED);
318
0
    goto leave;
319
0
  }
320
321
705
      if (convert) /* Text mode.  */
322
94
  {
323
686k
    for (; pt->len; pt->len--)
324
686k
      {
325
686k
        if ((c = iobuf_get (pt->buf)) == -1)
326
89
    {
327
89
      err = iobuf_error (pt->buf);
328
89
                  if (!err)
329
89
                    err = gpg_error_from_syserror ();
330
89
      log_error ("problem reading source (%u bytes remaining)\n",
331
89
           (unsigned) pt->len);
332
89
      goto leave;
333
89
    }
334
686k
        if (mfx->md)
335
686k
    gcry_md_putc (mfx->md, c);
336
686k
#ifndef HAVE_DOSISH_SYSTEM
337
              /* Convert to native line ending. */
338
              /* fixme: this hack might be too simple */
339
686k
        if (c == '\r' && convert != 'm')
340
115
    continue;
341
685k
#endif
342
685k
        if (fp)
343
685k
    {
344
685k
      if (opt.max_output && (++count) > opt.max_output)
345
0
        {
346
0
          log_error ("error writing to '%s': %s\n",
347
0
         fname, "exceeded --max-output limit\n");
348
0
          err = gpg_error (GPG_ERR_TOO_LARGE);
349
0
          goto leave;
350
0
        }
351
685k
      else if (es_putc (c, fp) == EOF)
352
0
        {
353
0
          if (es_ferror (fp))
354
0
      err = gpg_error_from_syserror ();
355
0
          else
356
0
      err = gpg_error (GPG_ERR_EOF);
357
0
          log_error ("error writing to '%s': %s\n",
358
0
         fname, gpg_strerror (err));
359
0
          goto leave;
360
0
        }
361
685k
    }
362
685k
      }
363
94
  }
364
611
      else  /* Binary mode.  */
365
611
  {
366
611
    size_t temp_size = iobuf_set_buffer_size(0) * 1024;
367
611
    byte *buffer;
368
369
611
    if (fp)
370
611
      {
371
        /* Disable buffering in estream as we are passing large
372
         * buffers to es_fwrite. */
373
611
        es_setbuf (fp, NULL);
374
611
      }
375
376
611
    buffer = xmalloc (temp_size);
377
611
          if (!buffer)
378
0
            {
379
0
              err = gpg_error_from_syserror ();
380
0
              goto leave;
381
0
            }
382
383
713
    while (pt->len)
384
299
      {
385
299
        int len = pt->len > temp_size ? temp_size : pt->len;
386
299
        len = iobuf_read (pt->buf, buffer, len);
387
299
        if (len == -1)
388
197
    {
389
197
      err = iobuf_error (pt->buf);
390
197
                  if (!err)
391
195
                    err = gpg_error_from_syserror ();
392
197
      log_error ("problem reading source (%u bytes remaining)\n",
393
197
           (unsigned) pt->len);
394
197
      xfree (buffer);
395
197
      goto leave;
396
197
    }
397
102
        if (mfx->md)
398
102
    gcry_md_write (mfx->md, buffer, len);
399
102
        if (fp)
400
102
    {
401
102
      if (opt.max_output && (count += len) > opt.max_output)
402
0
        {
403
0
          log_error ("error writing to '%s': %s\n",
404
0
         fname, "exceeded --max-output limit\n");
405
0
          err = gpg_error (GPG_ERR_TOO_LARGE);
406
0
          xfree (buffer);
407
0
          goto leave;
408
0
        }
409
102
      else if (es_fwrite (buffer, 1, len, fp) != len)
410
0
        {
411
0
          err = gpg_error_from_syserror ();
412
0
          log_error ("error writing to '%s': %s\n",
413
0
         fname, gpg_strerror (err));
414
0
          xfree (buffer);
415
0
          goto leave;
416
0
        }
417
102
    }
418
102
        pt->len -= len;
419
102
      }
420
414
    xfree (buffer);
421
414
  }
422
423
      /* Even if all data can be read successfully, by the AEAD tag
424
       * checking at last, it may be failed.  */
425
419
      if ((err = iobuf_error (pt->buf)))
426
419
        log_error ("problem reading source: %s\n", gpg_strerror (err));
427
419
      pt->buf = NULL;
428
419
    }
429
766
  else if (!clearsig)
430
430
    {
431
430
      if (convert)
432
120
  {     /* text mode */
433
390k
    while ((c = iobuf_get (pt->buf)) != -1)
434
390k
      {
435
390k
        if (mfx->md)
436
390k
    gcry_md_putc (mfx->md, c);
437
390k
#ifndef HAVE_DOSISH_SYSTEM
438
390k
        if (c == '\r' && convert != 'm')
439
5.75k
    continue;  /* fixme: this hack might be too simple */
440
384k
#endif
441
384k
        if (fp)
442
384k
    {
443
384k
      if (opt.max_output && (++count) > opt.max_output)
444
0
        {
445
0
          log_error ("Error writing to '%s': %s\n",
446
0
         fname, "exceeded --max-output limit\n");
447
0
          err = gpg_error (GPG_ERR_TOO_LARGE);
448
0
          goto leave;
449
0
        }
450
384k
      else if (es_putc (c, fp) == EOF)
451
0
        {
452
0
          if (es_ferror (fp))
453
0
      err = gpg_error_from_syserror ();
454
0
          else
455
0
      err = gpg_error (GPG_ERR_EOF);
456
0
          log_error ("error writing to '%s': %s\n",
457
0
         fname, gpg_strerror (err));
458
0
          goto leave;
459
0
        }
460
384k
    }
461
384k
      }
462
120
          if ((err = iobuf_error (pt->buf)))
463
120
            log_error ("problem reading source: %s\n", gpg_strerror (err));
464
120
  }
465
310
      else
466
310
  {     /* binary mode */
467
310
    size_t temp_size = iobuf_set_buffer_size(0) * 1024;
468
310
    byte *buffer;
469
310
    int eof_seen = 0;
470
471
310
    if (fp)
472
310
      {
473
        /* Disable buffering in estream as we are passing large
474
         * buffers to es_fwrite. */
475
310
        es_setbuf (fp, NULL);
476
310
      }
477
478
310
          buffer = xtrymalloc (temp_size);
479
310
          if (!buffer)
480
0
            {
481
0
              err = gpg_error_from_syserror ();
482
0
              goto leave;
483
0
            }
484
485
473
    while (!eof_seen)
486
310
      {
487
        /* Why do we check for len < temp_size:
488
         * If we won't, we would practically read 2 EOFs but
489
         * the first one has already popped the block_filter
490
         * off and therefore we don't catch the boundary.
491
         * So, always assume EOF if iobuf_read returns less bytes
492
         * then requested */
493
310
        int len = iobuf_read (pt->buf, buffer, temp_size);
494
310
        if (len == -1)
495
147
    break;
496
163
        if (len < temp_size)
497
163
    eof_seen = 1;
498
163
        if (mfx->md)
499
163
    gcry_md_write (mfx->md, buffer, len);
500
163
        if (fp)
501
163
    {
502
163
      if (opt.max_output && (count += len) > opt.max_output)
503
0
        {
504
0
          log_error ("error writing to '%s': %s\n",
505
0
         fname, "exceeded --max-output limit\n");
506
0
          err = gpg_error (GPG_ERR_TOO_LARGE);
507
0
          xfree (buffer);
508
0
          goto leave;
509
0
        }
510
163
      else if (es_fwrite (buffer, 1, len, fp) != len)
511
0
        {
512
0
          err = gpg_error_from_syserror ();
513
0
          log_error ("error writing to '%s': %s\n",
514
0
         fname, gpg_strerror (err));
515
0
          xfree (buffer);
516
0
          goto leave;
517
0
        }
518
163
    }
519
163
      }
520
310
          if ((err = iobuf_error (pt->buf)))
521
310
            log_error ("problem reading source: %s\n", gpg_strerror (err));
522
310
    xfree (buffer);
523
310
  }
524
430
      pt->buf = NULL;
525
430
    }
526
336
  else /* Clear text signature - don't hash the last CR,LF.   */
527
336
    {
528
336
      int state = 0;
529
530
1.07M
      while ((c = iobuf_get (pt->buf)) != -1)
531
1.07M
  {
532
1.07M
    if (fp)
533
1.07M
      {
534
1.07M
        if (opt.max_output && (++count) > opt.max_output)
535
0
    {
536
0
      log_error ("error writing to '%s': %s\n",
537
0
           fname, "exceeded --max-output limit\n");
538
0
      err = gpg_error (GPG_ERR_TOO_LARGE);
539
0
      goto leave;
540
0
    }
541
1.07M
        else if (es_putc (c, fp) == EOF)
542
0
    {
543
0
      err = gpg_error_from_syserror ();
544
0
      log_error ("error writing to '%s': %s\n",
545
0
           fname, gpg_strerror (err));
546
0
      goto leave;
547
0
    }
548
1.07M
      }
549
1.07M
    if (!mfx->md)
550
0
      continue;
551
1.07M
    if (state == 2)
552
204k
      {
553
204k
        gcry_md_putc (mfx->md, '\r');
554
204k
        gcry_md_putc (mfx->md, '\n');
555
204k
        state = 0;
556
204k
      }
557
1.07M
    if (!state)
558
652k
      {
559
652k
        if (c == '\r')
560
108k
    state = 1;
561
544k
        else if (c == '\n')
562
162k
    state = 2;
563
382k
        else
564
544k
    gcry_md_putc (mfx->md, c);
565
652k
      }
566
422k
    else if (state == 1)
567
422k
      {
568
422k
        if (c == '\n')
569
42.9k
    state = 2;
570
379k
        else
571
379k
    {
572
379k
      gcry_md_putc (mfx->md, '\r');
573
379k
      if (c == '\r')
574
314k
        state = 1;
575
65.2k
      else
576
65.2k
        {
577
65.2k
          state = 0;
578
65.2k
          gcry_md_putc (mfx->md, c);
579
65.2k
        }
580
379k
    }
581
422k
      }
582
1.07M
  }
583
336
      if ((err = iobuf_error (pt->buf)))
584
336
        log_error ("problem reading source: %s\n", gpg_strerror (err));
585
336
      pt->buf = NULL;
586
336
    }
587
588
1.18k
  if (fp && fp != es_stdout && fp != opt.outfp)
589
544
    pfg_close_file (&pfg, err);
590
1.18k
  fp = NULL;
591
592
1.47k
 leave:
593
  /* Make sure that stdout gets flushed after the plaintext has been
594
     handled.  This is for extra security as we do a flush anyway
595
     before checking the signature.  */
596
1.47k
  if (es_fflush (es_stdout))
597
0
    {
598
      /* We need to check the return code to detect errors like disk
599
         full for short plaintexts.  See bug#1207.  Checking return
600
         values is a good idea in any case.  */
601
0
      if (!err)
602
0
        err = gpg_error_from_syserror ();
603
0
      log_error ("error flushing '%s': %s\n", "[stdout]",
604
0
                 gpg_strerror (err));
605
0
    }
606
607
1.47k
  if (fp && fp != es_stdout && fp != opt.outfp)
608
133
    pfg_close_file (&pfg, err);
609
1.47k
  return err;
610
1.18k
}
611
612
613
static void
614
do_hash (gcry_md_hd_t md, gcry_md_hd_t md2, IOBUF fp, int textmode)
615
0
{
616
0
  text_filter_context_t tfx;
617
0
  int c;
618
619
0
  if (textmode)
620
0
    {
621
0
      memset (&tfx, 0, sizeof tfx);
622
0
      iobuf_push_filter (fp, text_filter, &tfx);
623
0
    }
624
0
  if (md2)
625
0
    {       /* work around a strange behaviour in pgp2 */
626
      /* It seems that at least PGP5 converts a single CR to a CR,LF too */
627
0
      int lc = -1;
628
0
      while ((c = iobuf_get (fp)) != -1)
629
0
  {
630
0
    if (c == '\n' && lc == '\r')
631
0
      gcry_md_putc (md2, c);
632
0
    else if (c == '\n')
633
0
      {
634
0
        gcry_md_putc (md2, '\r');
635
0
        gcry_md_putc (md2, c);
636
0
      }
637
0
    else if (c != '\n' && lc == '\r')
638
0
      {
639
0
        gcry_md_putc (md2, '\n');
640
0
        gcry_md_putc (md2, c);
641
0
      }
642
0
    else
643
0
      gcry_md_putc (md2, c);
644
645
0
    if (md)
646
0
      gcry_md_putc (md, c);
647
0
    lc = c;
648
0
  }
649
0
    }
650
0
  else
651
0
    {
652
0
      size_t temp_size = iobuf_set_buffer_size(0) * 1024;
653
0
      byte *buffer = xmalloc (temp_size);
654
0
      int ret;
655
656
0
      while ((ret = iobuf_read (fp, buffer, temp_size)) != -1)
657
0
  {
658
0
    if (md)
659
0
      gcry_md_write (md, buffer, ret);
660
0
  }
661
662
0
      xfree (buffer);
663
0
    }
664
0
}
665
666
667
/****************
668
 * Ask for the detached datafile and calculate the digest from it.
669
 * INFILE is the name of the input file.
670
 */
671
int
672
ask_for_detached_datafile (gcry_md_hd_t md, gcry_md_hd_t md2,
673
         const char *inname, int textmode)
674
0
{
675
0
  progress_filter_context_t *pfx;
676
0
  char *answer = NULL;
677
0
  IOBUF fp;
678
0
  int rc = 0;
679
680
0
  pfx = new_progress_context ();
681
0
  fp = open_sigfile (inname, pfx);  /* Open default file. */
682
683
0
  if (!fp && !opt.batch)
684
0
    {
685
0
      int any = 0;
686
0
      tty_printf (_("Detached signature.\n"));
687
0
      do
688
0
  {
689
0
    char *name;
690
691
0
    xfree (answer);
692
0
    tty_enable_completion (NULL);
693
0
    name = cpr_get ("detached_signature.filename",
694
0
        _("Please enter name of data file: "));
695
0
    tty_disable_completion ();
696
0
    cpr_kill_prompt ();
697
0
    answer = make_filename (name, (void *) NULL);
698
0
    xfree (name);
699
700
0
    if (any && !*answer)
701
0
      {
702
0
        rc = gpg_error (GPG_ERR_GENERAL); /*G10ERR_READ_FILE */
703
0
        goto leave;
704
0
      }
705
0
    fp = iobuf_open (answer);
706
0
    if (fp && is_secured_file (iobuf_get_fd (fp)))
707
0
      {
708
0
        iobuf_close (fp);
709
0
        fp = NULL;
710
0
        gpg_err_set_errno (EPERM);
711
0
      }
712
0
    if (!fp && errno == ENOENT)
713
0
      {
714
0
        tty_printf ("No such file, try again or hit enter to quit.\n");
715
0
        any++;
716
0
      }
717
0
    else if (!fp)
718
0
      {
719
0
        rc = gpg_error_from_syserror ();
720
0
        log_error (_("can't open '%s': %s\n"), answer,
721
0
       strerror (errno));
722
0
        goto leave;
723
0
      }
724
0
  }
725
0
      while (!fp);
726
0
    }
727
728
0
  if (!fp)
729
0
    {
730
0
      if (opt.verbose)
731
0
  log_info (_("reading stdin ...\n"));
732
0
      fp = iobuf_open (NULL);
733
0
      log_assert (fp);
734
0
    }
735
0
  do_hash (md, md2, fp, textmode);
736
0
  iobuf_close (fp);
737
738
0
leave:
739
0
  xfree (answer);
740
0
  release_progress_context (pfx);
741
0
  return rc;
742
0
}
743
744
745
746
/* Hash the given files and append the hash to hash contexts MD and
747
 * MD2.  If FILES is NULL, stdin is hashed.  */
748
int
749
hash_datafiles (gcry_md_hd_t md, gcry_md_hd_t md2, strlist_t files,
750
    const char *sigfilename, int textmode)
751
0
{
752
0
  progress_filter_context_t *pfx;
753
0
  IOBUF fp;
754
0
  strlist_t sl;
755
756
0
  pfx = new_progress_context ();
757
758
0
  if (!files)
759
0
    {
760
      /* Check whether we can open the signed material.  We avoid
761
         trying to open a file if run in batch mode.  This assumed
762
         data file for a sig file feature is just a convenience thing
763
         for the command line and the user needs to read possible
764
         warning messages. */
765
0
      if (!opt.batch)
766
0
        {
767
0
          fp = open_sigfile (sigfilename, pfx);
768
0
          if (fp)
769
0
            {
770
0
              do_hash (md, md2, fp, textmode);
771
0
              iobuf_close (fp);
772
0
              release_progress_context (pfx);
773
0
              return 0;
774
0
            }
775
0
        }
776
0
      log_error (_("no signed data\n"));
777
0
      release_progress_context (pfx);
778
0
      return gpg_error (GPG_ERR_NO_DATA);
779
0
    }
780
781
782
0
  for (sl = files; sl; sl = sl->next)
783
0
    {
784
0
      fp = iobuf_open (sl->d);
785
0
      if (fp && is_secured_file (iobuf_get_fd (fp)))
786
0
  {
787
0
    iobuf_close (fp);
788
0
    fp = NULL;
789
0
    gpg_err_set_errno (EPERM);
790
0
  }
791
0
      if (!fp)
792
0
  {
793
0
    int rc = gpg_error_from_syserror ();
794
0
    log_error (_("can't open signed data '%s'\n"),
795
0
         print_fname_stdin (sl->d));
796
0
    release_progress_context (pfx);
797
0
    return rc;
798
0
  }
799
0
      handle_progress (pfx, fp, sl->d);
800
0
      do_hash (md, md2, fp, textmode);
801
0
      iobuf_close (fp);
802
0
    }
803
804
0
  release_progress_context (pfx);
805
0
  return 0;
806
0
}
807
808
809
/* Hash the data from file descriptor DATA_FD and append the hash to hash
810
   contexts MD and MD2.  */
811
int
812
hash_datafile_by_fd (gcry_md_hd_t md, gcry_md_hd_t md2,
813
                     gnupg_fd_t data_fd, int textmode)
814
0
{
815
0
  progress_filter_context_t *pfx = new_progress_context ();
816
0
  iobuf_t fp;
817
818
0
  if (is_secured_file (data_fd))
819
0
    {
820
0
      fp = NULL;
821
0
      gpg_err_set_errno (EPERM);
822
0
    }
823
0
  else
824
0
    fp = iobuf_fdopen_nc (data_fd, "rb");
825
826
0
  if (!fp)
827
0
    {
828
0
      int rc = gpg_error_from_syserror ();
829
0
      log_error (_("can't open signed data fd=%d: %s\n"),
830
0
     FD_DBG (data_fd), strerror (errno));
831
0
      release_progress_context (pfx);
832
0
      return rc;
833
0
    }
834
835
0
  handle_progress (pfx, fp, NULL);
836
837
0
  do_hash (md, md2, fp, textmode);
838
839
0
  iobuf_close (fp);
840
841
0
  release_progress_context (pfx);
842
0
  return 0;
843
0
}
844
845
846
/* Set up a plaintext packet with the appropriate filename.  If there
847
   is a --set-filename, use it (it's already UTF8).  If there is a
848
   regular filename, UTF8-ize it if necessary.  If there is no
849
   filenames at all, set the field empty. */
850
851
PKT_plaintext *
852
setup_plaintext_name (const char *filename, IOBUF iobuf)
853
0
{
854
0
  PKT_plaintext *pt;
855
856
0
  if ((filename && !iobuf_is_pipe_filename (filename))
857
0
       || (opt.set_filename && !iobuf_is_pipe_filename (opt.set_filename)))
858
0
    {
859
0
      char *s;
860
861
0
      if (opt.set_filename)
862
0
  s = make_basename (opt.set_filename, iobuf_get_real_fname (iobuf));
863
0
      else if (filename && !opt.flags.utf8_filename)
864
0
  {
865
0
    char *tmp = native_to_utf8 (filename);
866
0
    s = make_basename (tmp, iobuf_get_real_fname (iobuf));
867
0
    xfree (tmp);
868
0
  }
869
0
      else
870
0
  s = make_basename (filename, iobuf_get_real_fname (iobuf));
871
872
0
      pt = xmalloc (sizeof *pt + strlen (s) - 1);
873
0
      pt->namelen = strlen (s);
874
0
      memcpy (pt->name, s, pt->namelen);
875
0
      xfree (s);
876
0
    }
877
0
  else
878
0
    {
879
      /* no filename */
880
0
      pt = xmalloc (sizeof *pt - 1);
881
0
      pt->namelen = 0;
882
0
    }
883
884
0
  return pt;
885
0
}