2009-09-30 18:47:11 +00:00
|
|
|
/* decrypt.c - decrypt and verify data
|
2007-05-03 04:44:12 +00:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
|
2009-09-30 18:47:11 +00:00
|
|
|
* 2007, 2009 Free Software Foundation, Inc.
|
2003-06-05 07:14:21 +00:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 19:49:40 +00:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-06-05 07:14:21 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2003-06-05 07:14:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2006-04-19 11:26:11 +00:00
|
|
|
#include "gpg.h"
|
2003-06-05 07:14:21 +00:00
|
|
|
#include "options.h"
|
|
|
|
#include "packet.h"
|
2017-03-07 20:21:23 +09:00
|
|
|
#include "../common/status.h"
|
|
|
|
#include "../common/iobuf.h"
|
2003-06-05 07:14:21 +00:00
|
|
|
#include "keydb.h"
|
2017-03-07 20:21:23 +09:00
|
|
|
#include "../common/util.h"
|
2003-06-05 07:14:21 +00:00
|
|
|
#include "main.h"
|
2017-03-07 20:21:23 +09:00
|
|
|
#include "../common/status.h"
|
|
|
|
#include "../common/i18n.h"
|
2003-06-05 07:14:21 +00:00
|
|
|
|
2009-09-30 18:47:11 +00:00
|
|
|
/* Assume that the input is an encrypted message and decrypt
|
2003-06-05 07:14:21 +00:00
|
|
|
* (and if signed, verify the signature on) it.
|
|
|
|
* This command differs from the default operation, as it never
|
|
|
|
* writes to the filename which is included in the file and it
|
|
|
|
* rejects files which don't begin with an encrypted message.
|
|
|
|
*/
|
|
|
|
int
|
2010-10-01 20:33:53 +00:00
|
|
|
decrypt_message (ctrl_t ctrl, const char *filename)
|
2003-06-05 07:14:21 +00:00
|
|
|
{
|
2009-09-30 18:47:11 +00:00
|
|
|
IOBUF fp;
|
|
|
|
armor_filter_context_t *afx = NULL;
|
|
|
|
progress_filter_context_t *pfx;
|
|
|
|
int rc;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
2009-09-30 18:47:11 +00:00
|
|
|
pfx = new_progress_context ();
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-09-30 18:47:11 +00:00
|
|
|
/* Open the message file. */
|
|
|
|
fp = iobuf_open (filename);
|
|
|
|
if (fp && is_secured_file (iobuf_get_fd (fp)))
|
|
|
|
{
|
|
|
|
iobuf_close (fp);
|
|
|
|
fp = NULL;
|
2010-04-01 13:24:55 +00:00
|
|
|
gpg_err_set_errno (EPERM);
|
2009-09-30 18:47:11 +00:00
|
|
|
}
|
|
|
|
if ( !fp )
|
|
|
|
{
|
|
|
|
rc = gpg_error_from_syserror ();
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error (_("can't open '%s': %s\n"), print_fname_stdin(filename),
|
2009-09-30 18:47:11 +00:00
|
|
|
gpg_strerror (rc));
|
|
|
|
release_progress_context (pfx);
|
|
|
|
return rc;
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
|
|
|
|
2009-09-30 18:47:11 +00:00
|
|
|
handle_progress (pfx, fp, filename);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
2009-09-30 18:47:11 +00:00
|
|
|
if ( !opt.no_armor )
|
|
|
|
{
|
|
|
|
if ( use_armor_filter( fp ) )
|
|
|
|
{
|
|
|
|
afx = new_armor_context ();
|
|
|
|
push_armor_filter ( afx, fp );
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-30 18:47:11 +00:00
|
|
|
if (!opt.outfile)
|
|
|
|
{
|
|
|
|
opt.outfile = "-";
|
2019-05-17 13:40:24 +02:00
|
|
|
opt.flags.dummy_outfile = 1;
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
2019-05-17 13:40:24 +02:00
|
|
|
else
|
|
|
|
opt.flags.dummy_outfile = 0;
|
2010-10-01 20:33:53 +00:00
|
|
|
rc = proc_encryption_packets (ctrl, NULL, fp );
|
2019-05-17 13:40:24 +02:00
|
|
|
if (opt.flags.dummy_outfile)
|
2009-09-30 18:47:11 +00:00
|
|
|
opt.outfile = NULL;
|
|
|
|
|
|
|
|
iobuf_close (fp);
|
|
|
|
release_armor_context (afx);
|
|
|
|
release_progress_context (pfx);
|
|
|
|
return rc;
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
|
|
|
|
2009-09-30 18:47:11 +00:00
|
|
|
|
2009-10-02 14:57:55 +00:00
|
|
|
/* Same as decrypt_message but takes a file descriptor for input and
|
|
|
|
output. */
|
|
|
|
gpg_error_t
|
2023-07-05 10:21:23 +09:00
|
|
|
decrypt_message_fd (ctrl_t ctrl, gnupg_fd_t input_fd,
|
|
|
|
gnupg_fd_t output_fd)
|
2009-10-02 14:57:55 +00:00
|
|
|
{
|
2014-03-07 09:46:44 +01:00
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
/* No server mode yet. */
|
|
|
|
(void)ctrl;
|
|
|
|
(void)input_fd;
|
|
|
|
(void)output_fd;
|
|
|
|
return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
|
|
|
|
#else
|
2009-10-02 14:57:55 +00:00
|
|
|
gpg_error_t err;
|
|
|
|
IOBUF fp;
|
|
|
|
armor_filter_context_t *afx = NULL;
|
|
|
|
progress_filter_context_t *pfx;
|
|
|
|
|
|
|
|
if (opt.outfp)
|
|
|
|
return gpg_error (GPG_ERR_BUG);
|
|
|
|
|
|
|
|
pfx = new_progress_context ();
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-10-02 14:57:55 +00:00
|
|
|
/* Open the message file. */
|
2022-10-07 11:16:55 +09:00
|
|
|
fp = iobuf_fdopen_nc (input_fd, "rb");
|
2009-10-02 14:57:55 +00:00
|
|
|
if (fp && is_secured_file (iobuf_get_fd (fp)))
|
|
|
|
{
|
|
|
|
iobuf_close (fp);
|
|
|
|
fp = NULL;
|
2010-04-01 13:24:55 +00:00
|
|
|
gpg_err_set_errno (EPERM);
|
2009-10-02 14:57:55 +00:00
|
|
|
}
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
char xname[64];
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-10-02 14:57:55 +00:00
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
snprintf (xname, sizeof xname, "[fd %d]", input_fd);
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error (_("can't open '%s': %s\n"), xname, gpg_strerror (err));
|
2009-10-02 14:57:55 +00:00
|
|
|
release_progress_context (pfx);
|
2023-07-11 13:49:41 +09:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_secured_file (output_fd))
|
|
|
|
{
|
|
|
|
char xname[64];
|
|
|
|
|
|
|
|
err = gpg_error (GPG_ERR_EPERM);
|
2023-07-18 11:54:16 +09:00
|
|
|
snprintf (xname, sizeof xname, "[fd %d]", FD_DBG (output_fd));
|
2023-07-11 13:49:41 +09:00
|
|
|
log_error (_("can't open '%s': %s\n"), xname, gpg_strerror (err));
|
|
|
|
iobuf_close (fp);
|
|
|
|
release_progress_context (pfx);
|
2009-10-02 14:57:55 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-07-07 15:07:34 +09:00
|
|
|
opt.outfp = open_stream_nc (output_fd, "w");
|
2009-10-02 14:57:55 +00:00
|
|
|
if (!opt.outfp)
|
|
|
|
{
|
|
|
|
char xname[64];
|
|
|
|
|
|
|
|
err = gpg_error_from_syserror ();
|
2023-07-18 11:54:16 +09:00
|
|
|
snprintf (xname, sizeof xname, "[fd %d]", FD_DBG (output_fd));
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error (_("can't open '%s': %s\n"), xname, gpg_strerror (err));
|
2009-10-02 14:57:55 +00:00
|
|
|
iobuf_close (fp);
|
|
|
|
release_progress_context (pfx);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!opt.no_armor)
|
|
|
|
{
|
|
|
|
if (use_armor_filter (fp))
|
|
|
|
{
|
|
|
|
afx = new_armor_context ();
|
|
|
|
push_armor_filter ( afx, fp );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-01 20:33:53 +00:00
|
|
|
err = proc_encryption_packets (ctrl, NULL, fp );
|
2009-10-02 14:57:55 +00:00
|
|
|
|
|
|
|
iobuf_close (fp);
|
2010-11-23 18:46:41 +00:00
|
|
|
es_fclose (opt.outfp);
|
2009-10-02 14:57:55 +00:00
|
|
|
opt.outfp = NULL;
|
|
|
|
release_armor_context (afx);
|
|
|
|
release_progress_context (pfx);
|
|
|
|
return err;
|
2014-03-07 09:46:44 +01:00
|
|
|
#endif
|
2009-10-02 14:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
void
|
2010-10-01 20:33:53 +00:00
|
|
|
decrypt_messages (ctrl_t ctrl, int nfiles, char *files[])
|
2003-06-05 07:14:21 +00:00
|
|
|
{
|
2006-04-19 11:26:11 +00:00
|
|
|
IOBUF fp;
|
2006-12-06 10:16:50 +00:00
|
|
|
progress_filter_context_t *pfx;
|
2003-06-05 07:14:21 +00:00
|
|
|
char *p, *output = NULL;
|
2006-04-19 11:26:11 +00:00
|
|
|
int rc=0,use_stdin=0;
|
|
|
|
unsigned int lno=0;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
if (opt.outfile)
|
|
|
|
{
|
|
|
|
log_error(_("--output doesn't work for this command\n"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-12-06 10:16:50 +00:00
|
|
|
pfx = new_progress_context ();
|
|
|
|
|
2006-04-19 11:26:11 +00:00
|
|
|
if(!nfiles)
|
|
|
|
use_stdin=1;
|
|
|
|
|
|
|
|
for(;;)
|
2003-06-05 07:14:21 +00:00
|
|
|
{
|
2006-04-19 11:26:11 +00: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;
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
print_file_status(STATUS_FILE_START, filename, 3);
|
2006-04-19 11:26:11 +00:00
|
|
|
output = make_outfile_name(filename);
|
2003-06-05 07:14:21 +00:00
|
|
|
if (!output)
|
|
|
|
goto next_file;
|
2006-04-19 11:26:11 +00:00
|
|
|
fp = iobuf_open(filename);
|
|
|
|
if (fp)
|
2010-03-08 17:05:37 +00:00
|
|
|
iobuf_ioctl (fp, IOBUF_IOCTL_NO_CACHE, 1, NULL);
|
2006-04-19 11:26:11 +00:00
|
|
|
if (fp && is_secured_file (iobuf_get_fd (fp)))
|
|
|
|
{
|
|
|
|
iobuf_close (fp);
|
|
|
|
fp = NULL;
|
2010-04-01 13:24:55 +00:00
|
|
|
gpg_err_set_errno (EPERM);
|
2006-04-19 11:26:11 +00:00
|
|
|
}
|
2003-06-05 07:14:21 +00:00
|
|
|
if (!fp)
|
|
|
|
{
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error(_("can't open '%s'\n"), print_fname_stdin(filename));
|
2003-06-05 07:14:21 +00:00
|
|
|
goto next_file;
|
|
|
|
}
|
|
|
|
|
2006-12-06 10:16:50 +00:00
|
|
|
handle_progress (pfx, fp, filename);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
if (!opt.no_armor)
|
|
|
|
{
|
|
|
|
if (use_armor_filter(fp))
|
|
|
|
{
|
2016-11-29 16:48:35 +01:00
|
|
|
armor_filter_context_t *afx = new_armor_context ();
|
|
|
|
rc = push_armor_filter (afx, fp);
|
|
|
|
if (rc)
|
|
|
|
log_error("failed to push armor filter");
|
|
|
|
release_armor_context (afx);
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
|
|
|
}
|
2010-10-01 20:33:53 +00:00
|
|
|
rc = proc_packets (ctrl,NULL, fp);
|
2003-06-05 07:14:21 +00:00
|
|
|
iobuf_close(fp);
|
|
|
|
if (rc)
|
2006-04-19 11:26:11 +00:00
|
|
|
log_error("%s: decryption failed: %s\n", print_fname_stdin(filename),
|
2015-01-22 12:06:11 +01:00
|
|
|
gpg_strerror (rc));
|
2003-06-05 07:14:21 +00:00
|
|
|
p = get_last_passphrase();
|
|
|
|
set_next_passphrase(p);
|
2003-06-18 19:56:13 +00:00
|
|
|
xfree (p);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
next_file:
|
|
|
|
/* Note that we emit file_done even after an error. */
|
|
|
|
write_status( STATUS_FILE_DONE );
|
2006-04-19 11:26:11 +00:00
|
|
|
xfree(output);
|
2007-05-03 04:44:12 +00:00
|
|
|
reset_literals_seen();
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
2006-04-19 11:26:11 +00:00
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
set_next_passphrase(NULL);
|
2006-12-06 10:16:50 +00:00
|
|
|
release_progress_context (pfx);
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|