Updated the german translation
[gnupg.git] / g10 / verify.c
blob484fd9c76d930cf1763d6aa97aad3ee6cacdbb25
1 /* verify.c - Verify signed data
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006,
3 * 2007 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 "filter.h"
38 #include "ttyio.h"
39 #include "i18n.h"
43 /****************
44 * Assume that the input is a signature and verify it without
45 * generating any output. With no arguments, the signature packet
46 * is read from stdin (it may be a detached signature when not
47 * used in batch mode). If only a sigfile is given, it may be a complete
48 * signature or a detached signature in which case the signed stuff
49 * is expected from stdin. With more than 1 argument, the first should
50 * be a detached signature and the remaining files are the signed stuff.
53 int
54 verify_signatures( int nfiles, char **files )
56 IOBUF fp;
57 armor_filter_context_t *afx = NULL;
58 progress_filter_context_t *pfx = new_progress_context ();
59 const char *sigfile;
60 int i, rc;
61 strlist_t sl;
63 /* Decide whether we should handle a detached or a normal signature,
64 * which is needed so that the code later can hash the correct data and
65 * not have a normal signature act as detached signature and ignoring the
66 * indended signed material from the 2nd file or stdin.
67 * 1. gpg <file - normal
68 * 2. gpg file - normal (or detached)
69 * 3. gpg file <file2 - detached
70 * 4. gpg file file2 - detached
71 * The question is how decide between case 2 and 3? The only way
72 * we can do it is by reading one byte from stdin and then unget
73 * it; the problem here is that we may be reading from the
74 * terminal (which could be detected using isatty() but won't work
75 * when under contol of a pty using program (e.g. expect)) and
76 * might get us in trouble when stdin is used for another purpose
77 * (--passphrase-fd 0). So we have to break with the behaviour
78 * prior to gpg 1.0.4 by assuming that case 3 is a normal
79 * signature (where file2 is ignored and require for a detached
80 * signature to indicate signed material comes from stdin by using
81 * case 4 with a file2 of "-".
83 * Actually we don't have to change anything here but can handle
84 * that all quite easily in mainproc.c
87 sigfile = nfiles? *files : NULL;
89 /* open the signature file */
90 fp = iobuf_open(sigfile);
91 if (fp && is_secured_file (iobuf_get_fd (fp)))
93 iobuf_close (fp);
94 fp = NULL;
95 errno = EPERM;
97 if( !fp ) {
98 rc = gpg_error_from_syserror ();
99 log_error(_("can't open `%s': %s\n"),
100 print_fname_stdin(sigfile), strerror (errno));
101 goto leave;
103 handle_progress (pfx, fp, sigfile);
105 if ( !opt.no_armor && use_armor_filter( fp ) )
107 afx = new_armor_context ();
108 push_armor_filter (afx, fp);
111 sl = NULL;
112 for(i=nfiles-1 ; i > 0 ; i-- )
113 add_to_strlist( &sl, files[i] );
114 rc = proc_signature_packets( NULL, fp, sl, sigfile );
115 free_strlist(sl);
116 iobuf_close(fp);
117 if( (afx && afx->no_openpgp_data && rc == -1) || rc == G10ERR_NO_DATA ) {
118 log_error(_("the signature could not be verified.\n"
119 "Please remember that the signature file (.sig or .asc)\n"
120 "should be the first file given on the command line.\n") );
121 rc = 0;
124 leave:
125 release_armor_context (afx);
126 release_progress_context (pfx);
127 return rc;
132 void
133 print_file_status( int status, const char *name, int what )
135 char *p = xmalloc(strlen(name)+10);
136 sprintf(p, "%d %s", what, name );
137 write_status_text( status, p );
138 xfree(p);
142 static int
143 verify_one_file( const char *name )
145 IOBUF fp;
146 armor_filter_context_t *afx = NULL;
147 progress_filter_context_t *pfx = new_progress_context ();
148 int rc;
150 print_file_status( STATUS_FILE_START, name, 1 );
151 fp = iobuf_open(name);
152 if (fp)
153 iobuf_ioctl (fp,3,1,NULL); /* disable fd caching */
154 if (fp && is_secured_file (iobuf_get_fd (fp)))
156 iobuf_close (fp);
157 fp = NULL;
158 errno = EPERM;
160 if( !fp ) {
161 rc = gpg_error_from_syserror ();
162 log_error(_("can't open `%s': %s\n"),
163 print_fname_stdin(name), strerror (errno));
164 print_file_status( STATUS_FILE_ERROR, name, 1 );
165 goto leave;
167 handle_progress (pfx, fp, name);
169 if( !opt.no_armor ) {
170 if( use_armor_filter( fp ) ) {
171 afx = new_armor_context ();
172 push_armor_filter (afx, fp);
176 rc = proc_signature_packets( NULL, fp, NULL, name );
177 iobuf_close(fp);
178 write_status( STATUS_FILE_DONE );
180 reset_literals_seen();
182 leave:
183 release_armor_context (afx);
184 release_progress_context (pfx);
185 return rc;
188 /****************
189 * Verify each file given in the files array or read the names of the
190 * files from stdin.
191 * Note: This function can not handle detached signatures.
194 verify_files( int nfiles, char **files )
196 int i;
198 if( !nfiles ) { /* read the filenames from stdin */
199 char line[2048];
200 unsigned int lno = 0;
202 while( fgets(line, DIM(line), stdin) ) {
203 lno++;
204 if( !*line || line[strlen(line)-1] != '\n' ) {
205 log_error(_("input line %u too long or missing LF\n"), lno );
206 return G10ERR_GENERAL;
208 /* This code does not work on MSDOS but how cares there are
209 * also no script languages available. We don't strip any
210 * spaces, so that we can process nearly all filenames */
211 line[strlen(line)-1] = 0;
212 verify_one_file( line );
216 else { /* take filenames from the array */
217 for(i=0; i < nfiles; i++ )
218 verify_one_file( files[i] );
220 return 0;
226 /* Perform a verify operation. To verify detached signatures, DATA_FD
227 shall be the descriptor of the signed data; for regular signatures
228 it needs to be -1. If OUT_FP is not NULL and DATA_FD is not -1 the
229 the signed material gets written that stream.
231 FIXME: OUTFP is not yet implemented.
234 gpg_verify (ctrl_t ctrl, int sig_fd, int data_fd, FILE *out_fp)
236 int rc;
237 iobuf_t fp;
238 armor_filter_context_t *afx = NULL;
239 progress_filter_context_t *pfx = new_progress_context ();
241 (void)ctrl;
242 (void)out_fp;
244 fp = iobuf_fdopen (sig_fd, "rb");
245 if (fp && is_secured_file (sig_fd))
247 fp = NULL;
248 errno = EPERM;
250 if ( !fp )
252 rc = gpg_error_from_syserror ();
253 log_error (_("can't open fd %d: %s\n"), sig_fd, strerror (errno));
254 goto leave;
257 handle_progress (pfx, fp, NULL);
259 if ( !opt.no_armor && use_armor_filter (fp) )
261 afx = new_armor_context ();
262 push_armor_filter (afx, fp);
265 rc = proc_signature_packets_by_fd ( NULL, fp, data_fd );
267 if ( afx && afx->no_openpgp_data
268 && (rc == -1 || gpg_err_code (rc) == GPG_ERR_EOF) )
269 rc = gpg_error (GPG_ERR_NO_DATA);
271 leave:
272 if (fp)
273 iobuf_close (fp);
274 release_progress_context (pfx);
275 release_armor_context (afx);
276 return rc;