2004-02-26 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / decrypt.c
blob98a270cfba51362b5a8560e7a4e160c5a442162a
1 /* decrypt.c - verify signed data
2 * Copyright (C) 1998,1999,2000,2001,2002,2003 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
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 "options.h"
29 #include "packet.h"
30 #include "errors.h"
31 #include "iobuf.h"
32 #include "keydb.h"
33 #include "memory.h"
34 #include "util.h"
35 #include "main.h"
36 #include "status.h"
37 #include "i18n.h"
41 /****************
42 * Assume that the input is an encrypted message and decrypt
43 * (and if signed, verify the signature on) it.
44 * This command differs from the default operation, as it never
45 * writes to the filename which is included in the file and it
46 * rejects files which don't begin with an encrypted message.
49 int
50 decrypt_message( const char *filename )
52 iobuf_t fp;
53 armor_filter_context_t afx;
54 progress_filter_context_t pfx;
55 int rc;
56 int no_out=0;
58 /* open the message file */
59 fp = iobuf_open(filename);
60 if( !fp ) {
61 rc = gpg_error_from_errno (errno);
62 log_error(_("can't open `%s'\n"), print_fname_stdin(filename));
63 return rc;
66 handle_progress (&pfx, fp, filename);
68 if( !opt.no_armor ) {
69 if( use_armor_filter( fp ) ) {
70 memset( &afx, 0, sizeof afx);
71 iobuf_push_filter( fp, armor_filter, &afx );
75 if( !opt.outfile ) {
76 no_out = 1;
77 opt.outfile = "-";
79 rc = proc_encryption_packets( NULL, fp );
80 if( no_out )
81 opt.outfile = NULL;
82 iobuf_close(fp);
83 return rc;
86 void
87 decrypt_messages(int nfiles, char **files)
89 iobuf_t fp;
90 armor_filter_context_t afx;
91 progress_filter_context_t pfx;
92 char *p, *output = NULL;
93 int rc = 0;
95 if (opt.outfile)
97 log_error(_("--output doesn't work for this command\n"));
98 return;
102 while (nfiles--)
104 print_file_status(STATUS_FILE_START, *files, 3);
105 output = make_outfile_name(*files);
106 if (!output)
107 goto next_file;
108 fp = iobuf_open(*files);
109 if (!fp)
111 log_error(_("can't open `%s'\n"), print_fname_stdin(*files));
112 goto next_file;
115 handle_progress (&pfx, fp, *files);
117 if (!opt.no_armor)
119 if (use_armor_filter(fp))
121 memset(&afx, 0, sizeof afx);
122 iobuf_push_filter(fp, armor_filter, &afx);
125 rc = proc_packets(NULL, fp);
126 iobuf_close(fp);
127 if (rc)
128 log_error("%s: decryption failed: %s\n", print_fname_stdin(*files),
129 gpg_strerror (rc));
130 p = get_last_passphrase();
131 set_next_passphrase(p);
132 xfree (p);
134 next_file:
135 /* Note that we emit file_done even after an error. */
136 write_status( STATUS_FILE_DONE );
137 xfree (output);
138 files++;
140 set_next_passphrase(NULL);