1 /* verify.c - Verify signed data
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006,
3 * 2007, 2010 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/>.
42 * Assume that the input is a signature and verify it without
43 * generating any output. With no arguments, the signature packet
44 * is read from stdin (it may be a detached signature when not
45 * used in batch mode). If only a sigfile is given, it may be a complete
46 * signature or a detached signature in which case the signed stuff
47 * is expected from stdin. With more than 1 argument, the first should
48 * be a detached signature and the remaining files are the signed stuff.
52 verify_signatures (ctrl_t ctrl, int nfiles, char **files )
55 armor_filter_context_t *afx = NULL;
56 progress_filter_context_t *pfx = new_progress_context ();
61 /* Decide whether we should handle a detached or a normal signature,
62 * which is needed so that the code later can hash the correct data and
63 * not have a normal signature act as detached signature and ignoring the
64 * indended signed material from the 2nd file or stdin.
65 * 1. gpg <file - normal
66 * 2. gpg file - normal (or detached)
67 * 3. gpg file <file2 - detached
68 * 4. gpg file file2 - detached
69 * The question is how decide between case 2 and 3? The only way
70 * we can do it is by reading one byte from stdin and then unget
71 * it; the problem here is that we may be reading from the
72 * terminal (which could be detected using isatty() but won't work
73 * when under contol of a pty using program (e.g. expect)) and
74 * might get us in trouble when stdin is used for another purpose
75 * (--passphrase-fd 0). So we have to break with the behaviour
76 * prior to gpg 1.0.4 by assuming that case 3 is a normal
77 * signature (where file2 is ignored and require for a detached
78 * signature to indicate signed material comes from stdin by using
79 * case 4 with a file2 of "-".
81 * Actually we don't have to change anything here but can handle
82 * that all quite easily in mainproc.c
85 sigfile = nfiles? *files : NULL;
87 /* open the signature file */
88 fp = iobuf_open(sigfile);
89 if (fp && is_secured_file (iobuf_get_fd (fp)))
93 gpg_err_set_errno (EPERM);
96 rc = gpg_error_from_syserror ();
97 log_error(_("can't open '%s': %s\n"),
98 print_fname_stdin(sigfile), gpg_strerror (rc));
101 handle_progress (pfx, fp, sigfile);
103 if ( !opt.no_armor && use_armor_filter( fp ) )
105 afx = new_armor_context ();
106 push_armor_filter (afx, fp);
110 for(i=nfiles-1 ; i > 0 ; i-- )
111 add_to_strlist( &sl, files[i] );
112 rc = proc_signature_packets (ctrl, NULL, fp, sl, sigfile );
115 if( (afx && afx->no_openpgp_data && rc == -1)
116 || gpg_err_code (rc) == GPG_ERR_NO_DATA ) {
117 log_error(_("the signature could not be verified.\n"
118 "Please remember that the signature file (.sig or .asc)\n"
119 "should be the first file given on the command line.\n") );
124 release_armor_context (afx);
125 release_progress_context (pfx);
132 print_file_status( int status, const char *name, int what )
134 char *p = xmalloc(strlen(name)+10);
135 sprintf(p, "%d %s", what, name );
136 write_status_text( status, p );
142 verify_one_file (ctrl_t ctrl, const char *name )
145 armor_filter_context_t *afx = NULL;
146 progress_filter_context_t *pfx = new_progress_context ();
149 print_file_status( STATUS_FILE_START, name, 1 );
150 fp = iobuf_open(name);
152 iobuf_ioctl (fp, IOBUF_IOCTL_NO_CACHE, 1, NULL);
153 if (fp && is_secured_file (iobuf_get_fd (fp)))
157 gpg_err_set_errno (EPERM);
160 rc = gpg_error_from_syserror ();
161 log_error(_("can't open '%s': %s\n"),
162 print_fname_stdin(name), strerror (errno));
163 print_file_status( STATUS_FILE_ERROR, name, 1 );
166 handle_progress (pfx, fp, name);
168 if( !opt.no_armor ) {
169 if( use_armor_filter( fp ) ) {
170 afx = new_armor_context ();
171 push_armor_filter (afx, fp);
175 rc = proc_signature_packets (ctrl, NULL, fp, NULL, name );
177 write_status( STATUS_FILE_DONE );
179 reset_literals_seen();
182 release_armor_context (afx);
183 release_progress_context (pfx);
188 * Verify each file given in the files array or read the names of the
190 * Note: This function can not handle detached signatures.
193 verify_files (ctrl_t ctrl, int nfiles, char **files )
197 if( !nfiles ) { /* read the filenames from stdin */
199 unsigned int lno = 0;
201 while( fgets(line, DIM(line), stdin) ) {
203 if( !*line || line[strlen(line)-1] != '\n' ) {
204 log_error(_("input line %u too long or missing LF\n"), lno );
205 return GPG_ERR_GENERAL;
207 /* This code does not work on MSDOS but how cares there are
208 * also no script languages available. We don't strip any
209 * spaces, so that we can process nearly all filenames */
210 line[strlen(line)-1] = 0;
211 verify_one_file (ctrl, line );
215 else { /* take filenames from the array */
216 for(i=0; i < nfiles; i++ )
217 verify_one_file (ctrl, files[i] );
225 /* Perform a verify operation. To verify detached signatures, DATA_FD
226 shall be the descriptor of the signed data; for regular signatures
227 it needs to be -1. If OUT_FP is not NULL and DATA_FD is not -1 the
228 the signed material gets written that stream.
230 FIXME: OUTFP is not yet implemented.
233 gpg_verify (ctrl_t ctrl, int sig_fd, int data_fd, estream_t out_fp)
237 armor_filter_context_t *afx = NULL;
238 progress_filter_context_t *pfx = new_progress_context ();
243 if (is_secured_file (sig_fd))
246 gpg_err_set_errno (EPERM);
249 fp = iobuf_fdopen_nc (sig_fd, "rb");
252 rc = gpg_error_from_syserror ();
253 log_error (_("can't open fd %d: %s\n"), sig_fd, strerror (errno));
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 (ctrl, 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);
273 release_progress_context (pfx);
274 release_armor_context (afx);