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 <https://www.gnu.org/licenses/>.
38 /* Assume that the input is an encrypted message and decrypt
39 * (and if signed, verify the signature on) it.
40 * This command differs from the default operation, as it never
41 * writes to the filename which is included in the file and it
42 * rejects files which don't begin with an encrypted message.
45 decrypt_message (ctrl_t ctrl, const char *filename)
48 armor_filter_context_t *afx = NULL;
49 progress_filter_context_t *pfx;
53 pfx = new_progress_context ();
55 /* Open the message file. */
56 fp = iobuf_open (filename);
57 if (fp && is_secured_file (iobuf_get_fd (fp)))
61 gpg_err_set_errno (EPERM);
65 rc = gpg_error_from_syserror ();
66 log_error (_("can't open '%s': %s\n"), print_fname_stdin(filename),
68 release_progress_context (pfx);
72 handle_progress (pfx, fp, filename);
76 if ( use_armor_filter( fp ) )
78 afx = new_armor_context ();
79 push_armor_filter ( afx, fp );
88 rc = proc_encryption_packets (ctrl, NULL, fp );
93 release_armor_context (afx);
94 release_progress_context (pfx);
99 /* Same as decrypt_message but takes a file descriptor for input and
102 decrypt_message_fd (ctrl_t ctrl, int input_fd, int output_fd)
104 #ifdef HAVE_W32_SYSTEM
105 /* No server mode yet. */
109 return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
113 armor_filter_context_t *afx = NULL;
114 progress_filter_context_t *pfx;
117 return gpg_error (GPG_ERR_BUG);
119 pfx = new_progress_context ();
121 /* Open the message file. */
122 fp = iobuf_fdopen_nc (FD2INT(input_fd), "rb");
123 if (fp && is_secured_file (iobuf_get_fd (fp)))
127 gpg_err_set_errno (EPERM);
133 err = gpg_error_from_syserror ();
134 snprintf (xname, sizeof xname, "[fd %d]", input_fd);
135 log_error (_("can't open '%s': %s\n"), xname, gpg_strerror (err));
136 release_progress_context (pfx);
140 #ifdef HAVE_W32CE_SYSTEM
141 #warning Need to fix this if we want to use g13
144 opt.outfp = es_fdopen_nc (output_fd, "wb");
150 err = gpg_error_from_syserror ();
151 snprintf (xname, sizeof xname, "[fd %d]", output_fd);
152 log_error (_("can't open '%s': %s\n"), xname, gpg_strerror (err));
154 release_progress_context (pfx);
160 if (use_armor_filter (fp))
162 afx = new_armor_context ();
163 push_armor_filter ( afx, fp );
167 err = proc_encryption_packets (ctrl, NULL, fp );
170 es_fclose (opt.outfp);
172 release_armor_context (afx);
173 release_progress_context (pfx);
180 decrypt_messages (ctrl_t ctrl, int nfiles, char *files[])
183 progress_filter_context_t *pfx;
184 char *p, *output = NULL;
185 int rc=0,use_stdin=0;
190 log_error(_("--output doesn't work for this command\n"));
194 pfx = new_progress_context ();
206 if(fgets(line, DIM(line), stdin))
209 if (!*line || line[strlen(line)-1] != '\n')
210 log_error("input line %u too long or missing LF\n", lno);
213 line[strlen(line)-1] = '\0';
231 print_file_status(STATUS_FILE_START, filename, 3);
232 output = make_outfile_name(filename);
235 fp = iobuf_open(filename);
237 iobuf_ioctl (fp, IOBUF_IOCTL_NO_CACHE, 1, NULL);
238 if (fp && is_secured_file (iobuf_get_fd (fp)))
242 gpg_err_set_errno (EPERM);
246 log_error(_("can't open '%s'\n"), print_fname_stdin(filename));
250 handle_progress (pfx, fp, filename);
254 if (use_armor_filter(fp))
256 armor_filter_context_t *afx = new_armor_context ();
257 rc = push_armor_filter (afx, fp);
259 log_error("failed to push armor filter");
260 release_armor_context (afx);
263 rc = proc_packets (ctrl,NULL, fp);
266 log_error("%s: decryption failed: %s\n", print_fname_stdin(filename),
268 p = get_last_passphrase();
269 set_next_passphrase(p);
273 /* Note that we emit file_done even after an error. */
274 write_status( STATUS_FILE_DONE );
276 reset_literals_seen();
279 set_next_passphrase(NULL);
280 release_progress_context (pfx);