1999-04-07 20:58:34 +02:00
|
|
|
/* decrypt.c - verify signed data
|
2003-04-09 10:03:17 +02:00
|
|
|
* Copyright (C) 1998,1999,2000,2001,2002,2003 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>
|
|
|
|
|
|
|
|
#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"
|
2002-06-29 15:46:34 +02:00
|
|
|
#include "status.h"
|
1998-03-09 22:44:06 +01:00
|
|
|
#include "i18n.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Assume that the input is an encrypted message and decrypt
|
1998-04-14 19:51:16 +02:00
|
|
|
* (and if signed, verify the signature on) it.
|
1998-03-09 22:44:06 +01:00
|
|
|
* This command differs from the default operation, as it never
|
1998-04-14 19:51:16 +02:00
|
|
|
* writes to the filename which is included in the file and it
|
1998-03-09 22:44:06 +01:00
|
|
|
* rejects files which don't begin with an encrypted message.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
decrypt_message( const char *filename )
|
|
|
|
{
|
|
|
|
IOBUF fp;
|
|
|
|
armor_filter_context_t afx;
|
2003-04-15 17:49:41 +02:00
|
|
|
progress_filter_context_t pfx;
|
1998-03-09 22:44:06 +01:00
|
|
|
int rc;
|
|
|
|
int no_out=0;
|
|
|
|
|
|
|
|
/* open the message file */
|
|
|
|
fp = iobuf_open(filename);
|
|
|
|
if( !fp ) {
|
1998-12-29 14:47:31 +01:00
|
|
|
log_error(_("can't open `%s'\n"), print_fname_stdin(filename));
|
2002-06-29 15:46:34 +02:00
|
|
|
return G10ERR_OPEN_FILE;
|
1998-03-09 22:44:06 +01:00
|
|
|
}
|
|
|
|
|
2003-04-15 17:49:41 +02:00
|
|
|
handle_progress (&pfx, fp, filename);
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !opt.outfile ) {
|
|
|
|
no_out = 1;
|
|
|
|
opt.outfile = "-";
|
|
|
|
}
|
1999-04-26 17:53:01 +02:00
|
|
|
rc = proc_encryption_packets( NULL, fp );
|
1998-03-09 22:44:06 +01:00
|
|
|
if( no_out )
|
|
|
|
opt.outfile = NULL;
|
|
|
|
iobuf_close(fp);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
void
|
2004-01-26 21:50:01 +01:00
|
|
|
decrypt_messages(int nfiles, char *files[])
|
2002-06-29 15:46:34 +02:00
|
|
|
{
|
|
|
|
IOBUF fp;
|
|
|
|
armor_filter_context_t afx;
|
2003-04-15 17:49:41 +02:00
|
|
|
progress_filter_context_t pfx;
|
2002-06-29 15:46:34 +02:00
|
|
|
char *p, *output = NULL;
|
2004-01-26 21:50:01 +01:00
|
|
|
int rc=0,use_stdin=0;
|
|
|
|
unsigned int lno=0;
|
2002-06-29 15:46:34 +02:00
|
|
|
|
|
|
|
if (opt.outfile)
|
|
|
|
{
|
|
|
|
log_error(_("--output doesn't work for this command\n"));
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-01-26 21:50:01 +01:00
|
|
|
if(!nfiles)
|
|
|
|
use_stdin=1;
|
|
|
|
|
|
|
|
for(;;)
|
2002-06-29 15:46:34 +02:00
|
|
|
{
|
2004-01-26 21:50:01 +01:00
|
|
|
char line[2048];
|
|
|
|
char *filename=NULL;
|
|
|
|
|
|
|
|
if(use_stdin)
|
|
|
|
{
|
|
|
|
if(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);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
line[strlen(line)-1] = '\0';
|
|
|
|
filename=line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(nfiles)
|
|
|
|
{
|
|
|
|
filename=*files;
|
|
|
|
nfiles--;
|
|
|
|
files++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(filename==NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
print_file_status(STATUS_FILE_START, filename, 3);
|
|
|
|
output = make_outfile_name(filename);
|
2002-06-29 15:46:34 +02:00
|
|
|
if (!output)
|
2003-04-09 10:03:17 +02:00
|
|
|
goto next_file;
|
2004-01-26 21:50:01 +01:00
|
|
|
fp = iobuf_open(filename);
|
2002-06-29 15:46:34 +02:00
|
|
|
if (!fp)
|
|
|
|
{
|
2004-01-26 21:50:01 +01:00
|
|
|
log_error(_("can't open `%s'\n"), print_fname_stdin(filename));
|
2003-04-09 10:03:17 +02:00
|
|
|
goto next_file;
|
2002-06-29 15:46:34 +02:00
|
|
|
}
|
2003-04-15 17:49:41 +02:00
|
|
|
|
2004-01-26 21:50:01 +01:00
|
|
|
handle_progress (&pfx, fp, filename);
|
2003-04-15 17:49:41 +02:00
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
if (!opt.no_armor)
|
|
|
|
{
|
|
|
|
if (use_armor_filter(fp))
|
|
|
|
{
|
|
|
|
memset(&afx, 0, sizeof afx);
|
|
|
|
iobuf_push_filter(fp, armor_filter, &afx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rc = proc_packets(NULL, fp);
|
|
|
|
iobuf_close(fp);
|
|
|
|
if (rc)
|
2004-01-26 21:50:01 +01:00
|
|
|
log_error("%s: decryption failed: %s\n", print_fname_stdin(filename),
|
2002-06-29 15:46:34 +02:00
|
|
|
g10_errstr(rc));
|
|
|
|
p = get_last_passphrase();
|
|
|
|
set_next_passphrase(p);
|
|
|
|
m_free (p);
|
2003-04-09 10:03:17 +02:00
|
|
|
|
|
|
|
next_file:
|
|
|
|
/* Note that we emit file_done even after an error. */
|
2002-06-29 15:46:34 +02:00
|
|
|
write_status( STATUS_FILE_DONE );
|
2003-04-09 10:03:17 +02:00
|
|
|
m_free(output);
|
2002-06-29 15:46:34 +02:00
|
|
|
}
|
2004-01-26 21:50:01 +01:00
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
set_next_passphrase(NULL);
|
|
|
|
}
|