1998-03-09 22:44:06 +01:00
|
|
|
/* verify.c - verify signed data
|
2002-06-29 15:46:34 +02:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
|
1998-03-09 22:44:06 +01:00
|
|
|
*
|
1998-12-23 13:41:40 +01:00
|
|
|
* This file is part of GnuPG.
|
1998-03-09 22:44:06 +01:00
|
|
|
*
|
1998-12-23 13:41:40 +01:00
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
1998-03-09 22:44:06 +01:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
1998-12-23 13:41:40 +01:00
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
1998-03-09 22:44:06 +01:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
2002-06-29 15:46:34 +02:00
|
|
|
#include <unistd.h> /* for isatty() */
|
1998-03-09 22:44:06 +01:00
|
|
|
|
|
|
|
#include "options.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "errors.h"
|
|
|
|
#include "iobuf.h"
|
|
|
|
#include "keydb.h"
|
2002-06-29 15:46:34 +02:00
|
|
|
#include "memory.h"
|
1998-03-09 22:44:06 +01:00
|
|
|
#include "util.h"
|
|
|
|
#include "main.h"
|
2000-07-14 19:34:53 +02:00
|
|
|
#include "status.h"
|
1998-03-09 22:44:06 +01:00
|
|
|
#include "filter.h"
|
|
|
|
#include "ttyio.h"
|
|
|
|
#include "i18n.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Assume that the input is a signature and verify it without
|
1998-04-14 19:51:16 +02:00
|
|
|
* generating any output. With no arguments, the signature packet
|
1998-03-09 22:44:06 +01:00
|
|
|
* is read from stdin (it may be a detached signature when not
|
1998-04-14 19:51:16 +02:00
|
|
|
* used in batch mode). If only a sigfile is given, it may be a complete
|
1998-03-09 22:44:06 +01:00
|
|
|
* signature or a detached signature in which case the signed stuff
|
|
|
|
* is expected from stdin. With more than 1 argument, the first should
|
|
|
|
* be a detached signature and the remaining files are the signed stuff.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
verify_signatures( int nfiles, char **files )
|
|
|
|
{
|
|
|
|
IOBUF fp;
|
|
|
|
armor_filter_context_t afx;
|
|
|
|
const char *sigfile;
|
|
|
|
int i, rc;
|
|
|
|
STRLIST sl;
|
|
|
|
|
2000-07-14 19:34:53 +02:00
|
|
|
memset( &afx, 0, sizeof afx);
|
2002-06-29 15:46:34 +02:00
|
|
|
/* decide whether we should handle a detached or a normal signature,
|
|
|
|
* which is needed so that the code later can hash the correct data and
|
|
|
|
* not have a normal signature act as detached signature and ignoring the
|
|
|
|
* indended signed material from the 2nd file or stdin.
|
|
|
|
* 1. gpg <file - normal
|
|
|
|
* 2. gpg file - normal (or detached)
|
|
|
|
* 3. gpg file <file2 - detached
|
|
|
|
* 4. gpg file file2 - detached
|
|
|
|
* The question is how decide between case 2 and 3? The only way
|
|
|
|
* we can do it is by reading one byte from stdin and the unget
|
|
|
|
* it; the problem here is that we may be reading from the
|
|
|
|
* terminal (which could be detected using isatty() but won't work
|
|
|
|
* when under contol of a pty using program (e.g. expect)) and
|
|
|
|
* might get us in trouble when stdin is used for another purpose
|
|
|
|
* (--passphrase-fd 0). So we have to break with the behaviour
|
|
|
|
* prior to gpg 1.0.4 by assuming that case 3 is a normal
|
|
|
|
* signature (where file2 is ignored and require for a detached
|
|
|
|
* signature to indicate signed material comes from stdin by using
|
|
|
|
* case 4 with a file2 of "-".
|
|
|
|
*
|
|
|
|
* Actually we don't have to change anything here but can handle
|
|
|
|
* that all quite easily in mainproc.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
1998-03-09 22:44:06 +01:00
|
|
|
sigfile = nfiles? *files : NULL;
|
|
|
|
|
|
|
|
/* open the signature file */
|
|
|
|
fp = iobuf_open(sigfile);
|
|
|
|
if( !fp ) {
|
1998-12-29 14:47:31 +01:00
|
|
|
log_error(_("can't open `%s'\n"), print_fname_stdin(sigfile));
|
2002-06-29 15:46:34 +02:00
|
|
|
return G10ERR_OPEN_FILE;
|
1998-03-09 22:44:06 +01:00
|
|
|
}
|
|
|
|
|
2000-07-14 19:34:53 +02:00
|
|
|
if( !opt.no_armor && use_armor_filter( fp ) )
|
|
|
|
iobuf_push_filter( fp, armor_filter, &afx );
|
|
|
|
|
|
|
|
sl = NULL;
|
|
|
|
for(i=1 ; i < nfiles; i++ )
|
|
|
|
add_to_strlist( &sl, files[i] );
|
|
|
|
rc = proc_signature_packets( NULL, fp, sl, sigfile );
|
|
|
|
free_strlist(sl);
|
|
|
|
iobuf_close(fp);
|
|
|
|
if( afx.no_openpgp_data && rc == -1 ) {
|
|
|
|
log_error(_("the signature could not be verified.\n"
|
|
|
|
"Please remember that the signature file (.sig or .asc)\n"
|
|
|
|
"should be the first file given on the command line.\n") );
|
|
|
|
rc = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
void
|
2000-07-14 19:34:53 +02:00
|
|
|
print_file_status( int status, const char *name, int what )
|
|
|
|
{
|
2002-06-29 15:46:34 +02:00
|
|
|
char *p = m_alloc(strlen(name)+10);
|
2000-07-14 19:34:53 +02:00
|
|
|
sprintf(p, "%d %s", what, name );
|
|
|
|
write_status_text( status, p );
|
2002-06-29 15:46:34 +02:00
|
|
|
m_free(p);
|
2000-07-14 19:34:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
verify_one_file( const char *name )
|
|
|
|
{
|
|
|
|
IOBUF fp;
|
|
|
|
armor_filter_context_t afx;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
print_file_status( STATUS_FILE_START, name, 1 );
|
|
|
|
fp = iobuf_open(name);
|
|
|
|
if( !fp ) {
|
|
|
|
print_file_status( STATUS_FILE_ERROR, name, 1 );
|
|
|
|
log_error(_("can't open `%s'\n"), print_fname_stdin(name));
|
2002-06-29 15:46:34 +02:00
|
|
|
return G10ERR_OPEN_FILE;
|
2000-07-14 19:34:53 +02:00
|
|
|
}
|
|
|
|
|
1998-03-09 22:44:06 +01:00
|
|
|
if( !opt.no_armor ) {
|
|
|
|
if( use_armor_filter( fp ) ) {
|
|
|
|
memset( &afx, 0, sizeof afx);
|
|
|
|
iobuf_push_filter( fp, armor_filter, &afx );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-14 19:34:53 +02:00
|
|
|
rc = proc_signature_packets( NULL, fp, NULL, name );
|
1998-03-09 22:44:06 +01:00
|
|
|
iobuf_close(fp);
|
2000-07-14 19:34:53 +02:00
|
|
|
write_status( STATUS_FILE_DONE );
|
1998-03-09 22:44:06 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2000-07-14 19:34:53 +02:00
|
|
|
/****************
|
|
|
|
* Verify each file given in the files array or read the names of the
|
|
|
|
* files from stdin.
|
|
|
|
* Note: This function can not handle detached signatures.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
verify_files( int nfiles, char **files )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if( !nfiles ) { /* read the filenames from stdin */
|
|
|
|
char line[2048];
|
|
|
|
unsigned int lno = 0;
|
1998-03-09 22:44:06 +01:00
|
|
|
|
2000-07-14 19:34:53 +02:00
|
|
|
while( fgets(line, DIM(line), stdin) ) {
|
|
|
|
lno++;
|
|
|
|
if( !*line || line[strlen(line)-1] != '\n' ) {
|
|
|
|
log_error(_("input line %u too long or missing LF\n"), lno );
|
2002-06-29 15:46:34 +02:00
|
|
|
return G10ERR_GENERAL;
|
2000-07-14 19:34:53 +02:00
|
|
|
}
|
|
|
|
/* This code does not work on MSDOS but how cares there are
|
|
|
|
* also no script languages available. We don't strip any
|
|
|
|
* spaces, so that we can process nearly all filenames */
|
|
|
|
line[strlen(line)-1] = 0;
|
|
|
|
verify_one_file( line );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else { /* take filenames from the array */
|
|
|
|
for(i=0; i < nfiles; i++ )
|
|
|
|
verify_one_file( files[i] );
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|