summaryrefslogtreecommitdiffstats
path: root/g10/decrypt.c
blob: 9589aff583da8a5bec30060b067e158524fbcc2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* decrypt.c - decrypt and verify data
 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
 *               2007, 2009 Free Software Foundation, Inc.
 *
 * This file is part of GnuPG.
 *
 * GnuPG is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * GnuPG is distributed in the hope that it will be useful,
 * 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
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "gpg.h"
#include "options.h"
#include "packet.h"
#include "../common/status.h"
#include "../common/iobuf.h"
#include "keydb.h"
#include "../common/util.h"
#include "main.h"
#include "../common/status.h"
#include "../common/i18n.h"

/* Assume that the input is an encrypted message and decrypt
 * (and if signed, verify the signature on) it.
 * This command differs from the default operation, as it never
 * writes to the filename which is included in the file and it
 * rejects files which don't begin with an encrypted message.
 */
int
decrypt_message (ctrl_t ctrl, const char *filename)
{
  IOBUF fp;
  armor_filter_context_t *afx = NULL;
  progress_filter_context_t *pfx;
  int rc;

  pfx = new_progress_context ();

  /* Open the message file.  */
  fp = iobuf_open (filename);
  if (fp && is_secured_file (iobuf_get_fd (fp)))
    {
      iobuf_close (fp);
      fp = NULL;
      gpg_err_set_errno (EPERM);
    }
  if ( !fp )
    {
      rc = gpg_error_from_syserror ();
      log_error (_("can't open '%s': %s\n"), print_fname_stdin(filename),
                 gpg_strerror (rc));
      release_progress_context (pfx);
      return rc;
    }

  handle_progress (pfx, fp, filename);

  if ( !opt.no_armor )
    {
      if ( use_armor_filter( fp ) )
        {
          afx = new_armor_context ();
          push_armor_filter ( afx, fp );
	}
    }

  if (!opt.outfile)
    {
      opt.outfile = "-";
      opt.flags.dummy_outfile = 1;
    }
  else
    opt.flags.dummy_outfile = 0;
  rc = proc_encryption_packets (ctrl, NULL, fp );
  if (opt.flags.dummy_outfile)
    opt.outfile = NULL;

  iobuf_close (fp);
  release_armor_context (afx);
  release_progress_context (pfx);
  return rc;
}


/* Same as decrypt_message but takes a file descriptor for input and
   output.  */
gpg_error_t
decrypt_message_fd (ctrl_t ctrl, int input_fd, int output_fd)
{
#ifdef HAVE_W32_SYSTEM
  /* No server mode yet.  */
  (void)ctrl;
  (void)input_fd;
  (void)output_fd;
  return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
#else
  gpg_error_t err;
  IOBUF fp;
  armor_filter_context_t *afx = NULL;
  progress_filter_context_t *pfx;

  if (opt.outfp)
    return gpg_error (GPG_ERR_BUG);

  pfx = new_progress_context ();

  /* Open the message file.  */
  fp = iobuf_fdopen_nc (FD2INT(input_fd), "rb");
  if (fp && is_secured_file (iobuf_get_fd (fp)))
    {
      iobuf_close (fp);
      fp = NULL;
      gpg_err_set_errno (EPERM);
    }
  if (!fp)
    {
      char xname[64];

      err = gpg_error_from_syserror ();
      snprintf (xname, sizeof xname, "[fd %d]", input_fd);
      log_error (_("can't open '%s': %s\n"), xname, gpg_strerror (err));
      release_progress_context (pfx);
      return err;
    }

#ifdef HAVE_W32CE_SYSTEM
#warning Need to fix this if we want to use g13
  opt.outfp = NULL;
#else
  opt.outfp = es_fdopen_nc (output_fd, "wb");
#endif
  if (!opt.outfp)
    {
      char xname[64];

      err = gpg_error_from_syserror ();
      snprintf (xname, sizeof xname, "[fd %d]", output_fd);
      log_error (_("can't open '%s': %s\n"), xname, gpg_strerror (err));
      iobuf_close (fp);
      release_progress_context (pfx);
      return err;
    }

  if (!opt.no_armor)
    {
      if (use_armor_filter (fp))
        {
          afx = new_armor_context ();
          push_armor_filter ( afx, fp );
	}
    }

  err = proc_encryption_packets (ctrl, NULL, fp );

  iobuf_close (fp);
  es_fclose (opt.outfp);
  opt.outfp = NULL;
  release_armor_context (afx);
  release_progress_context (pfx);
  return err;
#endif
}


void
decrypt_messages (ctrl_t ctrl, int nfiles, char *files[])
{
  IOBUF fp;
  progress_filter_context_t *pfx;
  char *p, *output = NULL;
  int rc=0,use_stdin=0;
  unsigned int lno=0;

  if (opt.outfile)
    {
      log_error(_("--output doesn't work for this command\n"));
      return;
    }

  pfx = new_progress_context ();

  if(!nfiles)
    use_stdin=1;

  for(;;)
    {
      char line[2048];
      char *filename=NULL;

      if(use_stdin)
	{
	  if(fgets(line, DIM(line), stdin))
	    {
	      lno++;
	      if (!*line || line[strlen(line)-1] != '\n')
		log_error("input line %u too long or missing LF\n", lno);
	      else
		{
		  line[strlen(line)-1] = '\0';
		  filename=line;
		}
	    }
	}
      else
	{
	  if(nfiles)
	    {
	      filename=*files;
	      nfiles--;
	      files++;
	    }
	}

      if(filename==NULL)
	break;

      print_file_status(STATUS_FILE_START, filename, 3);
      output = make_outfile_name(filename);
      if (!output)
        goto next_file;
      fp = iobuf_open(filename);
      if (fp)
        iobuf_ioctl (fp, IOBUF_IOCTL_NO_CACHE, 1, NULL);
      if (fp && is_secured_file (iobuf_get_fd (fp)))
        {
          iobuf_close (fp);
          fp = NULL;
          gpg_err_set_errno (EPERM);
        }
      if (!fp)
        {
          log_error(_("can't open '%s'\n"), print_fname_stdin(filename));
          goto next_file;
        }

      handle_progress (pfx, fp, filename);

      if (!opt.no_armor)
        {
          if (use_armor_filter(fp))
            {
              armor_filter_context_t *afx = new_armor_context ();
              rc = push_armor_filter (afx, fp);
              if (rc)
                log_error("failed to push armor filter");
              release_armor_context (afx);
            }
        }
      rc = proc_packets (ctrl,NULL, fp);
      iobuf_close(fp);
      if (rc)
        log_error("%s: decryption failed: %s\n", print_fname_stdin(filename),
                  gpg_strerror (rc));
      p = get_last_passphrase();
      set_next_passphrase(p);
      xfree (p);

    next_file:
      /* Note that we emit file_done even after an error. */
      write_status( STATUS_FILE_DONE );
      xfree(output);
      reset_literals_seen();
    }

  set_next_passphrase(NULL);
  release_progress_context (pfx);
}