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/>.
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.
46 decrypt_message (ctrl_t ctrl, const char *filename)
49 armor_filter_context_t *afx = NULL;
50 progress_filter_context_t *pfx;
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)))
62 gpg_err_set_errno (EPERM);
66 rc = gpg_error_from_syserror ();
67 log_error (_("can't open '%s': %s\n"), print_fname_stdin(filename),
69 release_progress_context (pfx);
73 handle_progress (pfx, fp, filename);
77 if ( use_armor_filter( fp ) )
79 afx = new_armor_context ();
80 push_armor_filter ( afx, fp );
89 rc = proc_encryption_packets (ctrl, NULL, fp );
94 release_armor_context (afx);
95 release_progress_context (pfx);
100 /* Same as decrypt_message but takes a file descriptor for input and
103 decrypt_message_fd (ctrl_t ctrl, int input_fd, int output_fd)
105 #ifdef HAVE_W32_SYSTEM
106 /* No server mode yet. */
110 return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
114 armor_filter_context_t *afx = NULL;
115 progress_filter_context_t *pfx;
118 return gpg_error (GPG_ERR_BUG);
120 pfx = new_progress_context ();
122 /* Open the message file. */
123 fp = iobuf_fdopen_nc (FD2INT(input_fd), "rb");
124 if (fp && is_secured_file (iobuf_get_fd (fp)))
128 gpg_err_set_errno (EPERM);
134 err = gpg_error_from_syserror ();
135 snprintf (xname, sizeof xname, "[fd %d]", input_fd);
136 log_error (_("can't open '%s': %s\n"), xname, gpg_strerror (err));
137 release_progress_context (pfx);
141 #ifdef HAVE_W32CE_SYSTEM
142 #warning Need to fix this if we want to use g13
145 opt.outfp = es_fdopen_nc (output_fd, "wb");
151 err = gpg_error_from_syserror ();
152 snprintf (xname, sizeof xname, "[fd %d]", output_fd);
153 log_error (_("can't open '%s': %s\n"), xname, gpg_strerror (err));
155 release_progress_context (pfx);
161 if (use_armor_filter (fp))
163 afx = new_armor_context ();
164 push_armor_filter ( afx, fp );
168 err = proc_encryption_packets (ctrl, NULL, fp );
171 es_fclose (opt.outfp);
173 release_armor_context (afx);
174 release_progress_context (pfx);
181 decrypt_messages (ctrl_t ctrl, int nfiles, char *files[])
184 armor_filter_context_t *afx = NULL;
185 progress_filter_context_t *pfx;
186 char *p, *output = NULL;
187 int rc=0,use_stdin=0;
192 log_error(_("--output doesn't work for this command\n"));
196 pfx = new_progress_context ();
208 if(fgets(line, DIM(line), stdin))
211 if (!*line || line[strlen(line)-1] != '\n')
212 log_error("input line %u too long or missing LF\n", lno);
215 line[strlen(line)-1] = '\0';
233 print_file_status(STATUS_FILE_START, filename, 3);
234 output = make_outfile_name(filename);
237 fp = iobuf_open(filename);
239 iobuf_ioctl (fp, IOBUF_IOCTL_NO_CACHE, 1, NULL);
240 if (fp && is_secured_file (iobuf_get_fd (fp)))
244 gpg_err_set_errno (EPERM);
248 log_error(_("can't open '%s'\n"), print_fname_stdin(filename));
252 handle_progress (pfx, fp, filename);
256 if (use_armor_filter(fp))
258 afx = new_armor_context ();
259 push_armor_filter ( afx, fp );
262 rc = proc_packets (ctrl,NULL, fp);
265 log_error("%s: decryption failed: %s\n", print_fname_stdin(filename),
267 p = get_last_passphrase();
268 set_next_passphrase(p);
272 /* Note that we emit file_done even after an error. */
273 write_status( STATUS_FILE_DONE );
275 reset_literals_seen();
278 set_next_passphrase(NULL);
279 release_armor_context (afx);
280 release_progress_context (pfx);