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,
28 #include <unistd.h> /* for isatty() */
46 * Assume that the input is a signature and verify it without
47 * generating any output. With no arguments, the signature packet
48 * is read from stdin (it may be a detached signature when not
49 * used in batch mode). If only a sigfile is given, it may be a complete
50 * signature or a detached signature in which case the signed stuff
51 * is expected from stdin. With more than 1 argument, the first should
52 * be a detached signature and the remaining files are the signed stuff.
56 verify_signatures( int nfiles
, char **files
)
59 armor_filter_context_t afx
;
60 progress_filter_context_t pfx
;
65 memset( &afx
, 0, sizeof afx
);
66 /* decide whether we should handle a detached or a normal signature,
67 * which is needed so that the code later can hash the correct data and
68 * not have a normal signature act as detached signature and ignoring the
69 * indended signed material from the 2nd file or stdin.
70 * 1. gpg <file - normal
71 * 2. gpg file - normal (or detached)
72 * 3. gpg file <file2 - detached
73 * 4. gpg file file2 - detached
74 * The question is how decide between case 2 and 3? The only way
75 * we can do it is by reading one byte from stdin and the unget
76 * it; the problem here is that we may be reading from the
77 * terminal (which could be detected using isatty() but won't work
78 * when under contol of a pty using program (e.g. expect)) and
79 * might get us in trouble when stdin is used for another purpose
80 * (--passphrase-fd 0). So we have to break with the behaviour
81 * prior to gpg 1.0.4 by assuming that case 3 is a normal
82 * signature (where file2 is ignored and require for a detached
83 * signature to indicate signed material comes from stdin by using
84 * case 4 with a file2 of "-".
86 * Actually we don't have to change anything here but can handle
87 * that all quite easily in mainproc.c
91 sigfile
= nfiles
? *files
: NULL
;
93 /* open the signature file */
94 fp
= iobuf_open(sigfile
);
95 if (fp
&& is_secured_file (iobuf_get_fd (fp
)))
102 log_error(_("can't open `%s'\n"), print_fname_stdin(sigfile
));
103 return G10ERR_OPEN_FILE
;
105 handle_progress (&pfx
, fp
, sigfile
);
107 if( !opt
.no_armor
&& use_armor_filter( fp
) )
108 iobuf_push_filter( fp
, armor_filter
, &afx
);
111 for(i
=nfiles
-1 ; i
> 0 ; i
-- )
112 add_to_strlist( &sl
, files
[i
] );
113 rc
= proc_signature_packets( NULL
, fp
, sl
, sigfile
);
116 if( (afx
.no_openpgp_data
&& rc
== -1) || rc
== G10ERR_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") );
128 print_file_status( int status
, const char *name
, int what
)
130 char *p
= xmalloc(strlen(name
)+10);
131 sprintf(p
, "%d %s", what
, name
);
132 write_status_text( status
, p
);
138 verify_one_file( const char *name
)
141 armor_filter_context_t afx
;
142 progress_filter_context_t pfx
;
145 print_file_status( STATUS_FILE_START
, name
, 1 );
146 fp
= iobuf_open(name
);
148 iobuf_ioctl (fp
,3,1,NULL
); /* disable fd caching */
149 if (fp
&& is_secured_file (iobuf_get_fd (fp
)))
156 print_file_status( STATUS_FILE_ERROR
, name
, 1 );
157 log_error(_("can't open `%s'\n"), print_fname_stdin(name
));
158 return G10ERR_OPEN_FILE
;
160 handle_progress (&pfx
, fp
, name
);
162 if( !opt
.no_armor
) {
163 if( use_armor_filter( fp
) ) {
164 memset( &afx
, 0, sizeof afx
);
165 iobuf_push_filter( fp
, armor_filter
, &afx
);
169 rc
= proc_signature_packets( NULL
, fp
, NULL
, name
);
171 write_status( STATUS_FILE_DONE
);
176 * Verify each file given in the files array or read the names of the
178 * Note: This function can not handle detached signatures.
181 verify_files( int nfiles
, char **files
)
185 if( !nfiles
) { /* read the filenames from stdin */
187 unsigned int lno
= 0;
189 while( fgets(line
, DIM(line
), stdin
) ) {
191 if( !*line
|| line
[strlen(line
)-1] != '\n' ) {
192 log_error(_("input line %u too long or missing LF\n"), lno
);
193 return G10ERR_GENERAL
;
195 /* This code does not work on MSDOS but how cares there are
196 * also no script languages available. We don't strip any
197 * spaces, so that we can process nearly all filenames */
198 line
[strlen(line
)-1] = 0;
199 verify_one_file( line
);
203 else { /* take filenames from the array */
204 for(i
=0; i
< nfiles
; i
++ )
205 verify_one_file( files
[i
] );