Line data Source code
1 : /* verify.c - Verify signed data
2 : * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006,
3 : * 2007, 2010 Free Software Foundation, Inc.
4 : *
5 : * This file is part of GnuPG.
6 : *
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.
11 : *
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.
16 : *
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/>.
19 : */
20 :
21 : #include <config.h>
22 : #include <stdio.h>
23 : #include <stdlib.h>
24 : #include <string.h>
25 : #include <errno.h>
26 : #include <assert.h>
27 :
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"
40 :
41 :
42 : /****************
43 : * Assume that the input is a signature and verify it without
44 : * generating any output. With no arguments, the signature packet
45 : * is read from stdin (it may be a detached signature when not
46 : * used in batch mode). If only a sigfile is given, it may be a complete
47 : * signature or a detached signature in which case the signed stuff
48 : * is expected from stdin. With more than 1 argument, the first should
49 : * be a detached signature and the remaining files are the signed stuff.
50 : */
51 :
52 : int
53 42 : verify_signatures (ctrl_t ctrl, int nfiles, char **files )
54 : {
55 : IOBUF fp;
56 42 : armor_filter_context_t *afx = NULL;
57 42 : progress_filter_context_t *pfx = new_progress_context ();
58 : const char *sigfile;
59 : int i, rc;
60 : strlist_t sl;
61 :
62 : /* Decide whether we should handle a detached or a normal signature,
63 : * which is needed so that the code later can hash the correct data and
64 : * not have a normal signature act as detached signature and ignoring the
65 : * indended signed material from the 2nd file or stdin.
66 : * 1. gpg <file - normal
67 : * 2. gpg file - normal (or detached)
68 : * 3. gpg file <file2 - detached
69 : * 4. gpg file file2 - detached
70 : * The question is how decide between case 2 and 3? The only way
71 : * we can do it is by reading one byte from stdin and then unget
72 : * it; the problem here is that we may be reading from the
73 : * terminal (which could be detected using isatty() but won't work
74 : * when under contol of a pty using program (e.g. expect)) and
75 : * might get us in trouble when stdin is used for another purpose
76 : * (--passphrase-fd 0). So we have to break with the behaviour
77 : * prior to gpg 1.0.4 by assuming that case 3 is a normal
78 : * signature (where file2 is ignored and require for a detached
79 : * signature to indicate signed material comes from stdin by using
80 : * case 4 with a file2 of "-".
81 : *
82 : * Actually we don't have to change anything here but can handle
83 : * that all quite easily in mainproc.c
84 : */
85 :
86 42 : sigfile = nfiles? *files : NULL;
87 :
88 : /* open the signature file */
89 42 : fp = iobuf_open(sigfile);
90 42 : if (fp && is_secured_file (iobuf_get_fd (fp)))
91 : {
92 0 : iobuf_close (fp);
93 0 : fp = NULL;
94 0 : gpg_err_set_errno (EPERM);
95 : }
96 42 : if( !fp ) {
97 0 : rc = gpg_error_from_syserror ();
98 0 : log_error(_("can't open '%s': %s\n"),
99 : print_fname_stdin(sigfile), gpg_strerror (rc));
100 0 : goto leave;
101 : }
102 42 : handle_progress (pfx, fp, sigfile);
103 :
104 42 : if ( !opt.no_armor && use_armor_filter( fp ) )
105 : {
106 32 : afx = new_armor_context ();
107 32 : push_armor_filter (afx, fp);
108 : }
109 :
110 42 : sl = NULL;
111 44 : for(i=nfiles-1 ; i > 0 ; i-- )
112 2 : add_to_strlist( &sl, files[i] );
113 42 : rc = proc_signature_packets (ctrl, NULL, fp, sl, sigfile );
114 38 : free_strlist(sl);
115 38 : iobuf_close(fp);
116 38 : if( (afx && afx->no_openpgp_data && rc == -1)
117 37 : || gpg_err_code (rc) == GPG_ERR_NO_DATA ) {
118 2 : 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 2 : rc = 0;
122 : }
123 :
124 : leave:
125 38 : release_armor_context (afx);
126 38 : release_progress_context (pfx);
127 38 : return rc;
128 : }
129 :
130 :
131 :
132 : void
133 0 : print_file_status( int status, const char *name, int what )
134 : {
135 0 : char *p = xmalloc(strlen(name)+10);
136 0 : sprintf(p, "%d %s", what, name );
137 0 : write_status_text( status, p );
138 0 : xfree(p);
139 0 : }
140 :
141 :
142 : static int
143 0 : verify_one_file (ctrl_t ctrl, const char *name )
144 : {
145 : IOBUF fp;
146 0 : armor_filter_context_t *afx = NULL;
147 0 : progress_filter_context_t *pfx = new_progress_context ();
148 : int rc;
149 :
150 0 : print_file_status( STATUS_FILE_START, name, 1 );
151 0 : fp = iobuf_open(name);
152 0 : if (fp)
153 0 : iobuf_ioctl (fp, IOBUF_IOCTL_NO_CACHE, 1, NULL);
154 0 : if (fp && is_secured_file (iobuf_get_fd (fp)))
155 : {
156 0 : iobuf_close (fp);
157 0 : fp = NULL;
158 0 : gpg_err_set_errno (EPERM);
159 : }
160 0 : if( !fp ) {
161 0 : rc = gpg_error_from_syserror ();
162 0 : log_error(_("can't open '%s': %s\n"),
163 0 : print_fname_stdin(name), strerror (errno));
164 0 : print_file_status( STATUS_FILE_ERROR, name, 1 );
165 0 : goto leave;
166 : }
167 0 : handle_progress (pfx, fp, name);
168 :
169 0 : if( !opt.no_armor ) {
170 0 : if( use_armor_filter( fp ) ) {
171 0 : afx = new_armor_context ();
172 0 : push_armor_filter (afx, fp);
173 : }
174 : }
175 :
176 0 : rc = proc_signature_packets (ctrl, NULL, fp, NULL, name );
177 0 : iobuf_close(fp);
178 0 : write_status( STATUS_FILE_DONE );
179 :
180 0 : reset_literals_seen();
181 :
182 : leave:
183 0 : release_armor_context (afx);
184 0 : release_progress_context (pfx);
185 0 : return rc;
186 : }
187 :
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.
192 : */
193 : int
194 0 : verify_files (ctrl_t ctrl, int nfiles, char **files )
195 : {
196 : int i;
197 :
198 0 : if( !nfiles ) { /* read the filenames from stdin */
199 : char line[2048];
200 0 : unsigned int lno = 0;
201 :
202 0 : while( fgets(line, DIM(line), stdin) ) {
203 0 : lno++;
204 0 : if( !*line || line[strlen(line)-1] != '\n' ) {
205 0 : log_error(_("input line %u too long or missing LF\n"), lno );
206 0 : return GPG_ERR_GENERAL;
207 : }
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 0 : line[strlen(line)-1] = 0;
212 0 : verify_one_file (ctrl, line );
213 : }
214 :
215 : }
216 : else { /* take filenames from the array */
217 0 : for(i=0; i < nfiles; i++ )
218 0 : verify_one_file (ctrl, files[i] );
219 : }
220 0 : return 0;
221 : }
222 :
223 :
224 :
225 :
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.
230 :
231 : FIXME: OUTFP is not yet implemented.
232 : */
233 : int
234 0 : gpg_verify (ctrl_t ctrl, int sig_fd, int data_fd, estream_t out_fp)
235 : {
236 : int rc;
237 : iobuf_t fp;
238 0 : armor_filter_context_t *afx = NULL;
239 0 : progress_filter_context_t *pfx = new_progress_context ();
240 :
241 : (void)ctrl;
242 : (void)out_fp;
243 :
244 0 : if (is_secured_file (sig_fd))
245 : {
246 0 : fp = NULL;
247 0 : gpg_err_set_errno (EPERM);
248 : }
249 : else
250 0 : fp = iobuf_fdopen_nc (sig_fd, "rb");
251 0 : if (!fp)
252 : {
253 0 : rc = gpg_error_from_syserror ();
254 0 : log_error (_("can't open fd %d: %s\n"), sig_fd, strerror (errno));
255 0 : goto leave;
256 : }
257 :
258 0 : handle_progress (pfx, fp, NULL);
259 :
260 0 : if ( !opt.no_armor && use_armor_filter (fp) )
261 : {
262 0 : afx = new_armor_context ();
263 0 : push_armor_filter (afx, fp);
264 : }
265 :
266 0 : rc = proc_signature_packets_by_fd (ctrl, NULL, fp, data_fd);
267 :
268 0 : if ( afx && afx->no_openpgp_data
269 0 : && (rc == -1 || gpg_err_code (rc) == GPG_ERR_EOF) )
270 0 : rc = gpg_error (GPG_ERR_NO_DATA);
271 :
272 : leave:
273 0 : iobuf_close (fp);
274 0 : release_progress_context (pfx);
275 0 : release_armor_context (afx);
276 0 : return rc;
277 : }
|