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/>.
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.
54 verify_signatures( int nfiles
, char **files
)
57 armor_filter_context_t
*afx
= NULL
;
58 progress_filter_context_t
*pfx
= new_progress_context ();
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
)))
98 rc
= gpg_error_from_syserror ();
99 log_error(_("can't open `%s': %s\n"),
100 print_fname_stdin(sigfile
), strerror (errno
));
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
);
112 for(i
=nfiles
-1 ; i
> 0 ; i
-- )
113 add_to_strlist( &sl
, files
[i
] );
114 rc
= proc_signature_packets( NULL
, fp
, sl
, sigfile
);
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") );
125 release_armor_context (afx
);
126 release_progress_context (pfx
);
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
);
143 verify_one_file( const char *name
)
146 armor_filter_context_t
*afx
= NULL
;
147 progress_filter_context_t
*pfx
= new_progress_context ();
150 print_file_status( STATUS_FILE_START
, name
, 1 );
151 fp
= iobuf_open(name
);
153 iobuf_ioctl (fp
,3,1,NULL
); /* disable fd caching */
154 if (fp
&& is_secured_file (iobuf_get_fd (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 );
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
);
178 write_status( STATUS_FILE_DONE
);
180 reset_literals_seen();
183 release_armor_context (afx
);
184 release_progress_context (pfx
);
189 * Verify each file given in the files array or read the names of the
191 * Note: This function can not handle detached signatures.
194 verify_files( int nfiles
, char **files
)
198 if( !nfiles
) { /* read the filenames from stdin */
200 unsigned int lno
= 0;
202 while( fgets(line
, DIM(line
), stdin
) ) {
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
] );
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
)
238 armor_filter_context_t
*afx
= NULL
;
239 progress_filter_context_t
*pfx
= new_progress_context ();
241 fp
= iobuf_fdopen (sig_fd
, "rb");
242 if (fp
&& is_secured_file (sig_fd
))
249 rc
= gpg_error_from_syserror ();
250 log_error (_("can't open fd %d: %s\n"), sig_fd
, strerror (errno
));
254 handle_progress (pfx
, fp
, NULL
);
256 if ( !opt
.no_armor
&& use_armor_filter (fp
) )
258 afx
= new_armor_context ();
259 push_armor_filter (afx
, fp
);
262 rc
= proc_signature_packets_by_fd ( NULL
, fp
, data_fd
);
264 if ( afx
&& afx
->no_openpgp_data
265 && (rc
== -1 || gpg_err_code (rc
) == GPG_ERR_EOF
) )
266 rc
= gpg_error (GPG_ERR_NO_DATA
);
271 release_progress_context (pfx
);
272 release_armor_context (afx
);