agent/
[gnupg.git] / g10 / decrypt.c
blob68b668864878e5ef3e28884b85428ad74d2b58a2
1 /* decrypt.c - decrypt and verify data
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 * 2007, 2009 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
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.
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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <assert.h>
28 #include "gpg.h"
29 #include "options.h"
30 #include "packet.h"
31 #include "status.h"
32 #include "iobuf.h"
33 #include "keydb.h"
34 #include "util.h"
35 #include "main.h"
36 #include "status.h"
37 #include "i18n.h"
39 /* Assume that the input is an encrypted message and decrypt
40 * (and if signed, verify the signature on) it.
41 * This command differs from the default operation, as it never
42 * writes to the filename which is included in the file and it
43 * rejects files which don't begin with an encrypted message.
45 int
46 decrypt_message (const char *filename)
48 IOBUF fp;
49 armor_filter_context_t *afx = NULL;
50 progress_filter_context_t *pfx;
51 int rc;
52 int no_out = 0;
54 pfx = new_progress_context ();
56 /* Open the message file. */
57 fp = iobuf_open (filename);
58 if (fp && is_secured_file (iobuf_get_fd (fp)))
60 iobuf_close (fp);
61 fp = NULL;
62 errno = EPERM;
64 if ( !fp )
66 rc = gpg_error_from_syserror ();
67 log_error (_("can't open `%s': %s\n"), print_fname_stdin(filename),
68 gpg_strerror (rc));
69 release_progress_context (pfx);
70 return rc;
73 handle_progress (pfx, fp, filename);
75 if ( !opt.no_armor )
77 if ( use_armor_filter( fp ) )
79 afx = new_armor_context ();
80 push_armor_filter ( afx, fp );
84 if (!opt.outfile)
86 no_out = 1;
87 opt.outfile = "-";
89 rc = proc_encryption_packets ( NULL, fp );
90 if (no_out)
91 opt.outfile = NULL;
93 iobuf_close (fp);
94 release_armor_context (afx);
95 release_progress_context (pfx);
96 return rc;
100 /* Same as decrypt_message but takes a file descriptor for input and
101 output. */
102 gpg_error_t
103 decrypt_message_fd (int input_fd, int output_fd)
105 gpg_error_t err;
106 IOBUF fp;
107 armor_filter_context_t *afx = NULL;
108 progress_filter_context_t *pfx;
110 if (opt.outfp)
111 return gpg_error (GPG_ERR_BUG);
113 pfx = new_progress_context ();
115 /* Open the message file. */
116 fp = iobuf_open_fd_or_name (input_fd, NULL, "rb");
117 if (fp && is_secured_file (iobuf_get_fd (fp)))
119 iobuf_close (fp);
120 fp = NULL;
121 errno = EPERM;
123 if (!fp)
125 char xname[64];
127 err = gpg_error_from_syserror ();
128 snprintf (xname, sizeof xname, "[fd %d]", input_fd);
129 log_error (_("can't open `%s': %s\n"), xname, gpg_strerror (err));
130 release_progress_context (pfx);
131 return err;
134 opt.outfp = fdopen (dup (output_fd), "wb");
135 if (!opt.outfp)
137 char xname[64];
139 err = gpg_error_from_syserror ();
140 snprintf (xname, sizeof xname, "[fd %d]", output_fd);
141 log_error (_("can't open `%s': %s\n"), xname, gpg_strerror (err));
142 iobuf_close (fp);
143 release_progress_context (pfx);
144 return err;
147 if (!opt.no_armor)
149 if (use_armor_filter (fp))
151 afx = new_armor_context ();
152 push_armor_filter ( afx, fp );
156 err = proc_encryption_packets ( NULL, fp );
158 iobuf_close (fp);
159 fclose (opt.outfp);
160 opt.outfp = NULL;
161 release_armor_context (afx);
162 release_progress_context (pfx);
163 return err;
167 void
168 decrypt_messages (int nfiles, char *files[])
170 IOBUF fp;
171 armor_filter_context_t *afx = NULL;
172 progress_filter_context_t *pfx;
173 char *p, *output = NULL;
174 int rc=0,use_stdin=0;
175 unsigned int lno=0;
177 if (opt.outfile)
179 log_error(_("--output doesn't work for this command\n"));
180 return;
183 pfx = new_progress_context ();
185 if(!nfiles)
186 use_stdin=1;
188 for(;;)
190 char line[2048];
191 char *filename=NULL;
193 if(use_stdin)
195 if(fgets(line, DIM(line), stdin))
197 lno++;
198 if (!*line || line[strlen(line)-1] != '\n')
199 log_error("input line %u too long or missing LF\n", lno);
200 else
202 line[strlen(line)-1] = '\0';
203 filename=line;
207 else
209 if(nfiles)
211 filename=*files;
212 nfiles--;
213 files++;
217 if(filename==NULL)
218 break;
220 print_file_status(STATUS_FILE_START, filename, 3);
221 output = make_outfile_name(filename);
222 if (!output)
223 goto next_file;
224 fp = iobuf_open(filename);
225 if (fp)
226 iobuf_ioctl (fp,3,1,NULL); /* disable fd caching */
227 if (fp && is_secured_file (iobuf_get_fd (fp)))
229 iobuf_close (fp);
230 fp = NULL;
231 errno = EPERM;
233 if (!fp)
235 log_error(_("can't open `%s'\n"), print_fname_stdin(filename));
236 goto next_file;
239 handle_progress (pfx, fp, filename);
241 if (!opt.no_armor)
243 if (use_armor_filter(fp))
245 afx = new_armor_context ();
246 push_armor_filter ( afx, fp );
249 rc = proc_packets(NULL, fp);
250 iobuf_close(fp);
251 if (rc)
252 log_error("%s: decryption failed: %s\n", print_fname_stdin(filename),
253 g10_errstr(rc));
254 p = get_last_passphrase();
255 set_next_passphrase(p);
256 xfree (p);
258 next_file:
259 /* Note that we emit file_done even after an error. */
260 write_status( STATUS_FILE_DONE );
261 xfree(output);
262 reset_literals_seen();
265 set_next_passphrase(NULL);
266 release_armor_context (afx);
267 release_progress_context (pfx);