1 /* verify.c - Verify signed data
2 * Copyright (C) 1998, 1999, 2000, 2001, 2004 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
45 * Assume that the input is a signature and verify it without
46 * generating any output. With no arguments, the signature packet
47 * is read from stdin (it may be a detached signature when not
48 * used in batch mode). If only a sigfile is given, it may be a complete
49 * signature or a detached signature in which case the signed stuff
50 * is expected from stdin. With more than 1 argument, the first should
51 * be a detached signature and the remaining files are the signed stuff.
55 verify_signatures( int nfiles
, char **files
)
58 armor_filter_context_t afx
;
59 progress_filter_context_t pfx
;
64 memset( &afx
, 0, sizeof afx
);
65 /* decide whether we should handle a detached or a normal signature,
66 * which is needed so that the code later can hash the correct data and
67 * not have a normal signature act as detached signature and ignoring the
68 * indended signed material from the 2nd file or stdin.
69 * 1. gpg <file - normal
70 * 2. gpg file - normal (or detached)
71 * 3. gpg file <file2 - detached
72 * 4. gpg file file2 - detached
73 * The question is how decide between case 2 and 3? The only way
74 * we can do it is by reading one byte from stdin and the unget
75 * it; the problem here is that we may be reading from the
76 * terminal (which could be detected using isatty() but won't work
77 * when under contol of a pty using program (e.g. expect)) and
78 * might get us in trouble when stdin is used for another purpose
79 * (--passphrase-fd 0). So we have to break with the behaviour
80 * prior to gpg 1.0.4 by assuming that case 3 is a normal
81 * signature (where file2 is ignored and require for a detached
82 * signature to indicate signed material comes from stdin by using
83 * case 4 with a file2 of "-".
85 * Actually we don't have to change anything here but can handle
86 * that all quite easily in mainproc.c
90 sigfile
= nfiles
? *files
: NULL
;
92 /* open the signature file */
93 fp
= iobuf_open(sigfile
);
94 if (fp
&& is_secured_file (iobuf_get_fd (fp
)))
101 rc
= gpg_error_from_syserror ();
102 log_error(_("can't open `%s': %s\n"),
103 print_fname_stdin(sigfile
), strerror (errno
));
106 handle_progress (&pfx
, fp
, sigfile
);
108 if( !opt
.no_armor
&& use_armor_filter( fp
) )
109 iobuf_push_filter( fp
, armor_filter
, &afx
);
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
.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") );
129 print_file_status( int status
, const char *name
, int what
)
131 char *p
= xmalloc(strlen(name
)+10);
132 sprintf(p
, "%d %s", what
, name
);
133 write_status_text( status
, p
);
139 verify_one_file( const char *name
)
142 armor_filter_context_t afx
;
143 progress_filter_context_t pfx
;
146 print_file_status( STATUS_FILE_START
, name
, 1 );
147 fp
= iobuf_open(name
);
149 iobuf_ioctl (fp
,3,1,NULL
); /* disable fd caching */
150 if (fp
&& is_secured_file (iobuf_get_fd (fp
)))
157 rc
= gpg_error_from_syserror ();
158 log_error(_("can't open `%s': %s\n"),
159 print_fname_stdin(name
), strerror (errno
));
160 print_file_status( STATUS_FILE_ERROR
, name
, 1 );
163 handle_progress (&pfx
, fp
, name
);
165 if( !opt
.no_armor
) {
166 if( use_armor_filter( fp
) ) {
167 memset( &afx
, 0, sizeof afx
);
168 iobuf_push_filter( fp
, armor_filter
, &afx
);
172 rc
= proc_signature_packets( NULL
, fp
, NULL
, name
);
174 write_status( STATUS_FILE_DONE
);
179 * Verify each file given in the files array or read the names of the
181 * Note: This function can not handle detached signatures.
184 verify_files( int nfiles
, char **files
)
188 if( !nfiles
) { /* read the filenames from stdin */
190 unsigned int lno
= 0;
192 while( fgets(line
, DIM(line
), stdin
) ) {
194 if( !*line
|| line
[strlen(line
)-1] != '\n' ) {
195 log_error(_("input line %u too long or missing LF\n"), lno
);
196 return G10ERR_GENERAL
;
198 /* This code does not work on MSDOS but how cares there are
199 * also no script languages available. We don't strip any
200 * spaces, so that we can process nearly all filenames */
201 line
[strlen(line
)-1] = 0;
202 verify_one_file( line
);
206 else { /* take filenames from the array */
207 for(i
=0; i
< nfiles
; i
++ )
208 verify_one_file( files
[i
] );