1997-11-24 12:04:11 +01:00
|
|
|
/* encode.c - encode data
|
2002-04-29 16:42:34 +02:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
1997-11-18 15:06:00 +01:00
|
|
|
*
|
1998-12-23 13:41:40 +01:00
|
|
|
* This file is part of GnuPG.
|
1997-11-18 15:06:00 +01:00
|
|
|
*
|
1998-12-23 13:41:40 +01:00
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
1997-11-18 15:06:00 +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,
|
1997-11-18 15:06:00 +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"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "main.h"
|
1997-11-21 15:53:57 +01:00
|
|
|
#include "filter.h"
|
1998-08-05 18:51:59 +02:00
|
|
|
#include "trustdb.h"
|
1998-07-06 12:23:57 +02:00
|
|
|
#include "i18n.h"
|
2000-12-06 13:06:19 +01:00
|
|
|
#include "status.h"
|
1997-11-18 15:06:00 +01:00
|
|
|
|
|
|
|
static int encode_simple( const char *filename, int mode );
|
1998-06-29 14:30:57 +02:00
|
|
|
static int write_pubkey_enc_from_list( PK_LIST pk_list, DEK *dek, IOBUF out );
|
1997-11-18 15:06:00 +01:00
|
|
|
|
|
|
|
|
1997-12-31 13:32:54 +01:00
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
/****************
|
1998-04-14 19:51:16 +02:00
|
|
|
* Encode FILENAME with only the symmetric cipher. Take input from
|
1997-11-18 15:06:00 +01:00
|
|
|
* stdin if FILENAME is NULL.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
encode_symmetric( const char *filename )
|
|
|
|
{
|
|
|
|
return encode_simple( filename, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************
|
1998-04-14 19:51:16 +02:00
|
|
|
* Encode FILENAME as a literal data packet only. Take input from
|
1997-11-18 15:06:00 +01:00
|
|
|
* stdin if FILENAME is NULL.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
encode_store( const char *filename )
|
|
|
|
{
|
|
|
|
return encode_simple( filename, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-05-17 22:03:24 +02:00
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
static int
|
|
|
|
encode_simple( const char *filename, int mode )
|
|
|
|
{
|
|
|
|
IOBUF inp, out;
|
|
|
|
PACKET pkt;
|
1999-07-26 09:44:46 +02:00
|
|
|
PKT_plaintext *pt = NULL;
|
1998-05-04 20:49:26 +02:00
|
|
|
STRING2KEY *s2k = NULL;
|
1997-11-18 15:06:00 +01:00
|
|
|
int rc = 0;
|
|
|
|
u32 filesize;
|
|
|
|
cipher_filter_context_t cfx;
|
1997-11-19 14:12:23 +01:00
|
|
|
armor_filter_context_t afx;
|
1997-11-23 16:38:27 +01:00
|
|
|
compress_filter_context_t zfx;
|
1999-03-17 13:13:04 +01:00
|
|
|
text_filter_context_t tfx;
|
1998-09-28 21:25:31 +02:00
|
|
|
int do_compress = opt.compress && !opt.rfc1991;
|
1997-11-18 15:06:00 +01:00
|
|
|
|
|
|
|
memset( &cfx, 0, sizeof cfx);
|
1997-11-19 14:12:23 +01:00
|
|
|
memset( &afx, 0, sizeof afx);
|
1997-11-23 16:38:27 +01:00
|
|
|
memset( &zfx, 0, sizeof zfx);
|
1999-03-17 13:13:04 +01:00
|
|
|
memset( &tfx, 0, sizeof tfx);
|
1998-08-11 19:29:34 +02:00
|
|
|
init_packet(&pkt);
|
2001-12-22 13:46:47 +01:00
|
|
|
|
2002-04-08 17:10:51 +02:00
|
|
|
if (opt.compress == -1 && is_file_compressed(filename, &rc))
|
2001-12-22 14:12:24 +01:00
|
|
|
{
|
|
|
|
if (opt.verbose)
|
|
|
|
log_info(_("`%s' already compressed\n"), filename);
|
|
|
|
do_compress = 0;
|
|
|
|
}
|
2001-12-22 13:46:47 +01:00
|
|
|
if (rc)
|
|
|
|
return rc;
|
1997-11-18 15:06:00 +01:00
|
|
|
|
|
|
|
/* prepare iobufs */
|
|
|
|
if( !(inp = iobuf_open(filename)) ) {
|
1998-07-06 12:23:57 +02:00
|
|
|
log_error(_("%s: can't open: %s\n"), filename? filename: "[stdin]",
|
1997-11-18 15:06:00 +01:00
|
|
|
strerror(errno) );
|
|
|
|
return G10ERR_OPEN_FILE;
|
|
|
|
}
|
|
|
|
|
1999-03-17 13:13:04 +01:00
|
|
|
if( opt.textmode )
|
|
|
|
iobuf_push_filter( inp, text_filter, &tfx );
|
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
cfx.dek = NULL;
|
|
|
|
if( mode ) {
|
1998-05-04 20:49:26 +02:00
|
|
|
s2k = m_alloc_clear( sizeof *s2k );
|
1998-09-28 21:25:31 +02:00
|
|
|
s2k->mode = opt.rfc1991? 0:opt.s2k_mode;
|
1998-05-15 20:49:19 +02:00
|
|
|
s2k->hash_algo = opt.def_digest_algo ? opt.def_digest_algo
|
1998-09-28 21:25:31 +02:00
|
|
|
: opt.s2k_digest_algo;
|
1999-07-01 12:53:35 +02:00
|
|
|
cfx.dek = passphrase_to_dek( NULL, 0,
|
2002-01-20 19:33:04 +01:00
|
|
|
opt.def_cipher_algo ? opt.def_cipher_algo
|
|
|
|
: opt.s2k_cipher_algo , s2k, 2, NULL );
|
1998-05-04 20:49:26 +02:00
|
|
|
if( !cfx.dek || !cfx.dek->keylen ) {
|
|
|
|
rc = G10ERR_PASSPHRASE;
|
1997-11-18 15:06:00 +01:00
|
|
|
m_free(cfx.dek);
|
1998-05-04 20:49:26 +02:00
|
|
|
m_free(s2k);
|
1997-11-18 15:06:00 +01:00
|
|
|
iobuf_close(inp);
|
1998-07-06 12:23:57 +02:00
|
|
|
log_error(_("error creating passphrase: %s\n"), g10_errstr(rc) );
|
1997-11-18 15:06:00 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-08-11 19:29:34 +02:00
|
|
|
if( (rc = open_outfile( filename, opt.armor? 1:0, &out )) ) {
|
2000-02-09 15:35:37 +01:00
|
|
|
iobuf_cancel(inp);
|
1997-11-18 15:06:00 +01:00
|
|
|
m_free(cfx.dek);
|
1998-05-04 20:49:26 +02:00
|
|
|
m_free(s2k);
|
1998-08-11 19:29:34 +02:00
|
|
|
return rc;
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( opt.armor )
|
1997-11-19 14:12:23 +01:00
|
|
|
iobuf_push_filter( out, armor_filter, &afx );
|
1998-10-16 18:00:17 +02:00
|
|
|
#ifdef ENABLE_COMMENT_PACKETS
|
1998-08-11 19:29:34 +02:00
|
|
|
else {
|
1998-05-26 15:38:00 +02:00
|
|
|
write_comment( out, "#created by GNUPG v" VERSION " ("
|
1998-02-16 21:05:02 +01:00
|
|
|
PRINTABLE_OS_NAME ")");
|
1998-08-11 19:29:34 +02:00
|
|
|
if( opt.comment_string )
|
|
|
|
write_comment( out, opt.comment_string );
|
|
|
|
}
|
1998-10-16 18:00:17 +02:00
|
|
|
#endif
|
1998-06-11 09:16:50 +02:00
|
|
|
if( s2k && !opt.rfc1991 ) {
|
1998-05-04 20:49:26 +02:00
|
|
|
PKT_symkey_enc *enc = m_alloc_clear( sizeof *enc );
|
|
|
|
enc->version = 4;
|
|
|
|
enc->cipher_algo = cfx.dek->algo;
|
|
|
|
enc->s2k = *s2k;
|
|
|
|
pkt.pkttype = PKT_SYMKEY_ENC;
|
|
|
|
pkt.pkt.symkey_enc = enc;
|
|
|
|
if( (rc = build_packet( out, &pkt )) )
|
|
|
|
log_error("build symkey packet failed: %s\n", g10_errstr(rc) );
|
|
|
|
m_free(enc);
|
|
|
|
}
|
1997-11-18 15:06:00 +01:00
|
|
|
|
1999-07-26 09:44:46 +02:00
|
|
|
if (!opt.no_literal) {
|
|
|
|
/* setup the inner packet */
|
|
|
|
if( filename || opt.set_filename ) {
|
|
|
|
char *s = make_basename( opt.set_filename ? opt.set_filename
|
|
|
|
: filename );
|
|
|
|
pt = m_alloc( sizeof *pt + strlen(s) - 1 );
|
|
|
|
pt->namelen = strlen(s);
|
|
|
|
memcpy(pt->name, s, pt->namelen );
|
|
|
|
m_free(s);
|
|
|
|
}
|
|
|
|
else { /* no filename */
|
|
|
|
pt = m_alloc( sizeof *pt - 1 );
|
|
|
|
pt->namelen = 0;
|
|
|
|
}
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
1999-07-26 09:44:46 +02:00
|
|
|
|
2002-01-05 00:26:30 +01:00
|
|
|
/* Note that PGP 5 has problems decrypting symmetrically encrypted
|
|
|
|
data if the file length is in the inner packet. It works when
|
|
|
|
only partial length headers are use. In the past, we always
|
|
|
|
used partial body length here, but since PGP 2, PGP 6, and PGP
|
|
|
|
7 need the file length, and nobody should be using PGP 5
|
|
|
|
nowadays anyway, this is now set to the file length. Note also
|
|
|
|
that this only applies to the RFC-1991 style symmetric
|
|
|
|
messages, and not the RFC-2440 style. PGP 6 and 7 work with
|
|
|
|
either partial length or fixed length with the new style
|
|
|
|
messages. */
|
|
|
|
|
|
|
|
if( filename && !opt.textmode ) {
|
1998-08-11 19:29:34 +02:00
|
|
|
if( !(filesize = iobuf_get_filelength(inp)) )
|
1998-11-10 13:59:59 +01:00
|
|
|
log_info(_("%s: WARNING: empty file\n"), filename );
|
2000-09-06 17:57:12 +02:00
|
|
|
/* we can't yet encode the length of very large files,
|
|
|
|
* so we switch to partial lengthn encoding in this case */
|
|
|
|
if ( filesize >= IOBUF_FILELENGTH_LIMIT )
|
|
|
|
filesize = 0;
|
|
|
|
|
1998-08-11 19:29:34 +02:00
|
|
|
}
|
|
|
|
else
|
1999-07-26 09:44:46 +02:00
|
|
|
filesize = opt.set_filesize ? opt.set_filesize : 0; /* stdin */
|
|
|
|
|
|
|
|
if (!opt.no_literal) {
|
|
|
|
pt->timestamp = make_timestamp();
|
|
|
|
pt->mode = opt.textmode? 't' : 'b';
|
|
|
|
pt->len = filesize;
|
1999-09-28 21:00:49 +02:00
|
|
|
pt->new_ctb = !pt->len && !opt.rfc1991;
|
1999-07-26 09:44:46 +02:00
|
|
|
pt->buf = inp;
|
|
|
|
pkt.pkttype = PKT_PLAINTEXT;
|
|
|
|
pkt.pkt.plaintext = pt;
|
|
|
|
cfx.datalen = filesize && !do_compress ? calc_packet_length( &pkt ) : 0;
|
|
|
|
}
|
|
|
|
else
|
2002-05-29 15:44:19 +02:00
|
|
|
{
|
|
|
|
cfx.datalen = filesize && !do_compress ? filesize : 0;
|
|
|
|
pkt.pkttype = 0;
|
|
|
|
pkt.pkt.generic = NULL;
|
|
|
|
}
|
1997-11-18 15:06:00 +01:00
|
|
|
|
|
|
|
/* register the cipher filter */
|
|
|
|
if( mode )
|
|
|
|
iobuf_push_filter( out, cipher_filter, &cfx );
|
1998-05-26 15:38:00 +02:00
|
|
|
/* register the compress filter */
|
1998-09-28 21:25:31 +02:00
|
|
|
if( do_compress )
|
1998-05-26 15:38:00 +02:00
|
|
|
iobuf_push_filter( out, compress_filter, &zfx );
|
1997-11-18 15:06:00 +01:00
|
|
|
|
|
|
|
/* do the work */
|
1999-07-26 09:44:46 +02:00
|
|
|
if (!opt.no_literal) {
|
|
|
|
if( (rc = build_packet( out, &pkt )) )
|
|
|
|
log_error("build_packet failed: %s\n", g10_errstr(rc) );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* user requested not to create a literal packet,
|
|
|
|
* so we copy the plain data */
|
|
|
|
byte copy_buffer[4096];
|
|
|
|
int bytes_copied;
|
|
|
|
while ((bytes_copied = iobuf_read(inp, copy_buffer, 4096)) != -1)
|
|
|
|
if (iobuf_write(out, copy_buffer, bytes_copied) == -1) {
|
|
|
|
rc = G10ERR_WRITE_FILE;
|
|
|
|
log_error("copying input to output failed: %s\n", g10_errstr(rc) );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
memset(copy_buffer, 0, 4096); /* burn buffer */
|
|
|
|
}
|
1997-11-18 15:06:00 +01:00
|
|
|
|
|
|
|
/* finish the stuff */
|
|
|
|
iobuf_close(inp);
|
1999-07-26 09:44:46 +02:00
|
|
|
if (rc)
|
|
|
|
iobuf_cancel(out);
|
2000-12-06 13:06:19 +01:00
|
|
|
else {
|
1999-07-26 09:44:46 +02:00
|
|
|
iobuf_close(out); /* fixme: check returncode */
|
2000-12-06 13:06:19 +01:00
|
|
|
if (mode)
|
|
|
|
write_status( STATUS_END_ENCRYPTION );
|
|
|
|
}
|
1999-07-26 09:44:46 +02:00
|
|
|
if (pt)
|
|
|
|
pt->buf = NULL;
|
1997-11-18 15:06:00 +01:00
|
|
|
free_packet(&pkt);
|
|
|
|
m_free(cfx.dek);
|
1998-05-04 20:49:26 +02:00
|
|
|
m_free(s2k);
|
1997-11-18 15:06:00 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Encrypt the file with the given userids (or ask if none
|
|
|
|
* is supplied).
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
encode_crypt( const char *filename, STRLIST remusr )
|
|
|
|
{
|
1997-12-31 13:32:54 +01:00
|
|
|
IOBUF inp = NULL, out = NULL;
|
1997-11-18 15:06:00 +01:00
|
|
|
PACKET pkt;
|
1998-01-13 20:04:23 +01:00
|
|
|
PKT_plaintext *pt = NULL;
|
2001-12-22 13:46:47 +01:00
|
|
|
int rc = 0, rc2 = 0;
|
1997-11-18 15:06:00 +01:00
|
|
|
u32 filesize;
|
|
|
|
cipher_filter_context_t cfx;
|
1997-11-19 14:12:23 +01:00
|
|
|
armor_filter_context_t afx;
|
1997-11-23 16:38:27 +01:00
|
|
|
compress_filter_context_t zfx;
|
1999-03-17 13:13:04 +01:00
|
|
|
text_filter_context_t tfx;
|
2001-12-07 16:24:32 +01:00
|
|
|
PK_LIST pk_list,work_list;
|
1998-09-28 21:25:31 +02:00
|
|
|
int do_compress = opt.compress && !opt.rfc1991;
|
1997-11-18 15:06:00 +01:00
|
|
|
|
1999-04-26 17:53:01 +02:00
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
memset( &cfx, 0, sizeof cfx);
|
1997-11-19 14:12:23 +01:00
|
|
|
memset( &afx, 0, sizeof afx);
|
1997-11-23 16:38:27 +01:00
|
|
|
memset( &zfx, 0, sizeof zfx);
|
1999-03-17 13:13:04 +01:00
|
|
|
memset( &tfx, 0, sizeof tfx);
|
1998-08-11 19:29:34 +02:00
|
|
|
init_packet(&pkt);
|
1997-11-18 15:06:00 +01:00
|
|
|
|
1998-07-06 12:23:57 +02:00
|
|
|
if( (rc=build_pk_list( remusr, &pk_list, PUBKEY_USAGE_ENC)) )
|
1997-12-31 13:32:54 +01:00
|
|
|
return rc;
|
1997-11-18 15:06:00 +01:00
|
|
|
|
2001-12-15 19:27:13 +01:00
|
|
|
if(opt.pgp2) {
|
|
|
|
for(work_list=pk_list; work_list; work_list=work_list->next)
|
2001-12-07 16:24:32 +01:00
|
|
|
if(!(is_RSA(work_list->pk->pubkey_algo) &&
|
|
|
|
nbits_from_pk(work_list->pk)<=2048))
|
|
|
|
{
|
2001-12-17 22:45:40 +01:00
|
|
|
log_info(_("you can only encrypt to RSA keys of 2048 bits or "
|
2001-12-07 16:24:32 +01:00
|
|
|
"less in --pgp2 mode\n"));
|
2002-01-22 21:39:10 +01:00
|
|
|
log_info(_("this message may not be usable by PGP 2.x\n"));
|
2001-12-17 22:45:40 +01:00
|
|
|
opt.pgp2=0;
|
2001-12-07 16:24:32 +01:00
|
|
|
break;
|
|
|
|
}
|
2001-12-15 19:27:13 +01:00
|
|
|
}
|
2001-12-07 16:24:32 +01:00
|
|
|
|
2002-04-08 17:10:51 +02:00
|
|
|
if (opt.compress == -1 && is_file_compressed(filename, &rc2))
|
2001-12-22 14:12:24 +01:00
|
|
|
{
|
|
|
|
if (opt.verbose)
|
|
|
|
log_info(_("`%s' already compressed\n"), filename);
|
|
|
|
do_compress = 0;
|
|
|
|
}
|
|
|
|
if (rc2)
|
|
|
|
{
|
|
|
|
rc = rc2;
|
|
|
|
goto leave;
|
|
|
|
}
|
2001-12-22 13:46:47 +01:00
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
/* prepare iobufs */
|
|
|
|
if( !(inp = iobuf_open(filename)) ) {
|
1998-07-06 12:23:57 +02:00
|
|
|
log_error(_("can't open %s: %s\n"), filename? filename: "[stdin]",
|
1997-11-18 15:06:00 +01:00
|
|
|
strerror(errno) );
|
1997-12-31 13:32:54 +01:00
|
|
|
rc = G10ERR_OPEN_FILE;
|
|
|
|
goto leave;
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
|
|
|
else if( opt.verbose )
|
1998-12-29 14:47:31 +01:00
|
|
|
log_info(_("reading from `%s'\n"), filename? filename: "[stdin]");
|
1997-11-18 15:06:00 +01:00
|
|
|
|
1999-03-17 13:13:04 +01:00
|
|
|
if( opt.textmode )
|
|
|
|
iobuf_push_filter( inp, text_filter, &tfx );
|
|
|
|
|
1998-08-11 19:29:34 +02:00
|
|
|
if( (rc = open_outfile( filename, opt.armor? 1:0, &out )) )
|
1997-12-31 13:32:54 +01:00
|
|
|
goto leave;
|
1998-08-11 19:29:34 +02:00
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
|
|
|
|
if( opt.armor )
|
1997-11-19 14:12:23 +01:00
|
|
|
iobuf_push_filter( out, armor_filter, &afx );
|
1998-10-16 18:00:17 +02:00
|
|
|
#ifdef ENABLE_COMMENT_PACKETS
|
1998-08-11 19:29:34 +02:00
|
|
|
else {
|
1998-05-26 15:38:00 +02:00
|
|
|
write_comment( out, "#created by GNUPG v" VERSION " ("
|
1998-02-16 21:05:02 +01:00
|
|
|
PRINTABLE_OS_NAME ")");
|
1998-08-11 19:29:34 +02:00
|
|
|
if( opt.comment_string )
|
|
|
|
write_comment( out, opt.comment_string );
|
|
|
|
}
|
1998-10-16 18:00:17 +02:00
|
|
|
#endif
|
1997-11-18 15:06:00 +01:00
|
|
|
/* create a session key */
|
2001-08-20 21:10:34 +02:00
|
|
|
cfx.dek = m_alloc_secure_clear (sizeof *cfx.dek);
|
1998-08-05 18:51:59 +02:00
|
|
|
if( !opt.def_cipher_algo ) { /* try to get it from the prefs */
|
|
|
|
cfx.dek->algo = select_algo_from_prefs( pk_list, PREFTYPE_SYM );
|
2002-01-22 21:39:10 +01:00
|
|
|
/* The only way select_algo_from_prefs can fail here is when
|
|
|
|
mixing v3 and v4 keys, as v4 keys have an implicit
|
|
|
|
preference entry for 3DES, and the pk_list cannot be empty.
|
|
|
|
In this case, use 3DES anyway as it's the safest choice -
|
|
|
|
perhaps the v3 key is being used in an OpenPGP
|
|
|
|
implementation and we know that the implementation behind
|
|
|
|
any v4 key can handle 3DES. */
|
|
|
|
if( cfx.dek->algo == -1 ) {
|
|
|
|
cfx.dek->algo = CIPHER_ALGO_3DES;
|
|
|
|
|
|
|
|
if( opt.pgp2 ) {
|
|
|
|
log_info(_("unable to use the IDEA cipher for all of the keys "
|
|
|
|
"you are encrypting to.\n"));
|
|
|
|
log_info(_("this message may not be usable by PGP 2.x\n"));
|
|
|
|
opt.pgp2=0;
|
|
|
|
}
|
|
|
|
}
|
1998-08-05 18:51:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
cfx.dek->algo = opt.def_cipher_algo;
|
2001-08-30 18:39:23 +02:00
|
|
|
cfx.dek->use_mdc = select_mdc_from_pklist (pk_list);
|
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
make_session_key( cfx.dek );
|
|
|
|
if( DBG_CIPHER )
|
|
|
|
log_hexdump("DEK is: ", cfx.dek->key, cfx.dek->keylen );
|
|
|
|
|
1998-06-29 14:30:57 +02:00
|
|
|
rc = write_pubkey_enc_from_list( pk_list, cfx.dek, out );
|
1998-01-02 21:40:10 +01:00
|
|
|
if( rc )
|
|
|
|
goto leave;
|
1997-11-18 15:06:00 +01:00
|
|
|
|
1999-08-04 10:45:27 +02:00
|
|
|
if (!opt.no_literal) {
|
1999-07-26 09:44:46 +02:00
|
|
|
/* setup the inner packet */
|
|
|
|
if( filename || opt.set_filename ) {
|
|
|
|
char *s = make_basename( opt.set_filename ? opt.set_filename : filename );
|
|
|
|
pt = m_alloc( sizeof *pt + strlen(s) - 1 );
|
|
|
|
pt->namelen = strlen(s);
|
|
|
|
memcpy(pt->name, s, pt->namelen );
|
|
|
|
m_free(s);
|
|
|
|
}
|
|
|
|
else { /* no filename */
|
|
|
|
pt = m_alloc( sizeof *pt - 1 );
|
|
|
|
pt->namelen = 0;
|
|
|
|
}
|
1999-08-04 10:45:27 +02:00
|
|
|
}
|
1999-07-26 09:44:46 +02:00
|
|
|
|
1999-03-17 13:13:04 +01:00
|
|
|
if( filename && !opt.textmode ) {
|
1998-08-11 19:29:34 +02:00
|
|
|
if( !(filesize = iobuf_get_filelength(inp)) )
|
1998-11-10 13:59:59 +01:00
|
|
|
log_info(_("%s: WARNING: empty file\n"), filename );
|
2000-09-06 17:57:12 +02:00
|
|
|
/* we can't yet encode the length of very large files,
|
2000-12-06 13:06:19 +01:00
|
|
|
* so we switch to partial length encoding in this case */
|
2000-09-06 17:57:12 +02:00
|
|
|
if ( filesize >= IOBUF_FILELENGTH_LIMIT )
|
|
|
|
filesize = 0;
|
1998-08-11 19:29:34 +02:00
|
|
|
}
|
|
|
|
else
|
1999-07-26 09:44:46 +02:00
|
|
|
filesize = opt.set_filesize ? opt.set_filesize : 0; /* stdin */
|
|
|
|
|
|
|
|
if (!opt.no_literal) {
|
|
|
|
pt->timestamp = make_timestamp();
|
|
|
|
pt->mode = opt.textmode ? 't' : 'b';
|
|
|
|
pt->len = filesize;
|
|
|
|
pt->new_ctb = !pt->len && !opt.rfc1991;
|
|
|
|
pt->buf = inp;
|
|
|
|
pkt.pkttype = PKT_PLAINTEXT;
|
|
|
|
pkt.pkt.plaintext = pt;
|
|
|
|
cfx.datalen = filesize && !do_compress? calc_packet_length( &pkt ) : 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cfx.datalen = filesize && !do_compress ? filesize : 0;
|
1997-11-18 15:06:00 +01:00
|
|
|
|
|
|
|
/* register the cipher filter */
|
|
|
|
iobuf_push_filter( out, cipher_filter, &cfx );
|
1999-07-26 09:44:46 +02:00
|
|
|
|
1998-05-26 15:38:00 +02:00
|
|
|
/* register the compress filter */
|
1998-09-28 21:25:31 +02:00
|
|
|
if( do_compress ) {
|
2002-05-09 21:57:08 +02:00
|
|
|
int compr_algo = opt.def_compress_algo;
|
|
|
|
|
|
|
|
if(compr_algo==-1)
|
|
|
|
{
|
|
|
|
if((compr_algo=select_algo_from_prefs( pk_list, PREFTYPE_ZIP))==-1)
|
|
|
|
compr_algo=DEFAULT_COMPRESS_ALGO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* algo 0 means no compression */
|
|
|
|
if( compr_algo )
|
|
|
|
{
|
|
|
|
zfx.algo = compr_algo;
|
1998-08-05 18:51:59 +02:00
|
|
|
iobuf_push_filter( out, compress_filter, &zfx );
|
2002-05-09 21:57:08 +02:00
|
|
|
}
|
1998-08-05 18:51:59 +02:00
|
|
|
}
|
1997-11-18 15:06:00 +01:00
|
|
|
|
|
|
|
/* do the work */
|
1999-07-26 09:44:46 +02:00
|
|
|
if (!opt.no_literal) {
|
|
|
|
if( (rc = build_packet( out, &pkt )) )
|
|
|
|
log_error("build_packet failed: %s\n", g10_errstr(rc) );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* user requested not to create a literal packet, so we copy the plain data */
|
|
|
|
byte copy_buffer[4096];
|
|
|
|
int bytes_copied;
|
|
|
|
while ((bytes_copied = iobuf_read(inp, copy_buffer, 4096)) != -1)
|
|
|
|
if (iobuf_write(out, copy_buffer, bytes_copied) == -1) {
|
|
|
|
rc = G10ERR_WRITE_FILE;
|
|
|
|
log_error("copying input to output failed: %s\n", g10_errstr(rc) );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
memset(copy_buffer, 0, 4096); /* burn buffer */
|
|
|
|
}
|
1997-11-18 15:06:00 +01:00
|
|
|
|
|
|
|
/* finish the stuff */
|
1997-12-31 13:32:54 +01:00
|
|
|
leave:
|
1997-11-18 15:06:00 +01:00
|
|
|
iobuf_close(inp);
|
1997-12-31 13:32:54 +01:00
|
|
|
if( rc )
|
|
|
|
iobuf_cancel(out);
|
2000-12-06 13:06:19 +01:00
|
|
|
else {
|
1997-12-31 13:32:54 +01:00
|
|
|
iobuf_close(out); /* fixme: check returncode */
|
2000-12-06 13:06:19 +01:00
|
|
|
write_status( STATUS_END_ENCRYPTION );
|
|
|
|
}
|
1998-01-13 20:04:23 +01:00
|
|
|
if( pt )
|
|
|
|
pt->buf = NULL;
|
1997-11-18 15:06:00 +01:00
|
|
|
free_packet(&pkt);
|
|
|
|
m_free(cfx.dek);
|
1998-06-29 14:30:57 +02:00
|
|
|
release_pk_list( pk_list );
|
1997-11-18 15:06:00 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-04-26 17:53:01 +02:00
|
|
|
|
|
|
|
|
1997-12-31 13:32:54 +01:00
|
|
|
/****************
|
|
|
|
* Filter to do a complete public key encryption.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
encrypt_filter( void *opaque, int control,
|
|
|
|
IOBUF a, byte *buf, size_t *ret_len)
|
|
|
|
{
|
|
|
|
size_t size = *ret_len;
|
|
|
|
encrypt_filter_context_t *efx = opaque;
|
|
|
|
int rc=0;
|
|
|
|
|
|
|
|
if( control == IOBUFCTRL_UNDERFLOW ) { /* decrypt */
|
1998-01-16 22:15:24 +01:00
|
|
|
BUG(); /* not used */
|
1997-12-31 13:32:54 +01:00
|
|
|
}
|
|
|
|
else if( control == IOBUFCTRL_FLUSH ) { /* encrypt */
|
1998-01-02 21:40:10 +01:00
|
|
|
if( !efx->header_okay ) {
|
2001-08-20 21:10:34 +02:00
|
|
|
efx->cfx.dek = m_alloc_secure_clear( sizeof *efx->cfx.dek );
|
1998-08-05 18:51:59 +02:00
|
|
|
|
|
|
|
if( !opt.def_cipher_algo ) { /* try to get it from the prefs */
|
|
|
|
efx->cfx.dek->algo =
|
|
|
|
select_algo_from_prefs( efx->pk_list, PREFTYPE_SYM );
|
2001-08-20 21:10:34 +02:00
|
|
|
if( efx->cfx.dek->algo == -1 ) {
|
|
|
|
/* because 3DES is implicitly in the prefs, this can only
|
|
|
|
* happen if we do not have any public keys in the list */
|
1998-08-05 18:51:59 +02:00
|
|
|
efx->cfx.dek->algo = DEFAULT_CIPHER_ALGO;
|
2001-08-20 21:10:34 +02:00
|
|
|
}
|
1998-08-05 18:51:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
efx->cfx.dek->algo = opt.def_cipher_algo;
|
2001-08-30 18:39:23 +02:00
|
|
|
|
|
|
|
efx->cfx.dek->use_mdc = select_mdc_from_pklist (efx->pk_list);
|
|
|
|
|
1998-01-02 21:40:10 +01:00
|
|
|
make_session_key( efx->cfx.dek );
|
|
|
|
if( DBG_CIPHER )
|
|
|
|
log_hexdump("DEK is: ",
|
|
|
|
efx->cfx.dek->key, efx->cfx.dek->keylen );
|
|
|
|
|
1998-06-29 14:30:57 +02:00
|
|
|
rc = write_pubkey_enc_from_list( efx->pk_list, efx->cfx.dek, a );
|
1998-01-02 21:40:10 +01:00
|
|
|
if( rc )
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
iobuf_push_filter( a, cipher_filter, &efx->cfx );
|
|
|
|
|
|
|
|
efx->header_okay = 1;
|
|
|
|
}
|
|
|
|
rc = iobuf_write( a, buf, size );
|
|
|
|
|
1997-12-31 13:32:54 +01:00
|
|
|
}
|
|
|
|
else if( control == IOBUFCTRL_FREE ) {
|
|
|
|
}
|
|
|
|
else if( control == IOBUFCTRL_DESC ) {
|
|
|
|
*(char**)buf = "encrypt_filter";
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
1998-01-02 21:40:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
/****************
|
1998-06-29 14:30:57 +02:00
|
|
|
* Write pubkey-enc packets from the list of PKs to OUT.
|
1998-01-02 21:40:10 +01:00
|
|
|
*/
|
|
|
|
static int
|
1998-06-29 14:30:57 +02:00
|
|
|
write_pubkey_enc_from_list( PK_LIST pk_list, DEK *dek, IOBUF out )
|
1998-01-02 21:40:10 +01:00
|
|
|
{
|
|
|
|
PACKET pkt;
|
1998-06-29 14:30:57 +02:00
|
|
|
PKT_public_key *pk;
|
1998-01-02 21:40:10 +01:00
|
|
|
PKT_pubkey_enc *enc;
|
|
|
|
int rc;
|
|
|
|
|
1998-06-29 14:30:57 +02:00
|
|
|
for( ; pk_list; pk_list = pk_list->next ) {
|
1998-06-13 08:59:14 +02:00
|
|
|
MPI frame;
|
1998-01-02 21:40:10 +01:00
|
|
|
|
1998-06-29 14:30:57 +02:00
|
|
|
pk = pk_list->pk;
|
1998-12-10 20:20:47 +01:00
|
|
|
|
|
|
|
print_pubkey_algo_note( pk->pubkey_algo );
|
1998-01-02 21:40:10 +01:00
|
|
|
enc = m_alloc_clear( sizeof *enc );
|
1998-06-29 14:30:57 +02:00
|
|
|
enc->pubkey_algo = pk->pubkey_algo;
|
|
|
|
keyid_from_pk( pk, enc->keyid );
|
1998-09-11 07:47:32 +02:00
|
|
|
enc->throw_keyid = opt.throw_keyid;
|
1999-06-15 14:31:07 +02:00
|
|
|
|
|
|
|
/* Okay, what's going on: We have the session key somewhere in
|
|
|
|
* the structure DEK and want to encode this session key in
|
|
|
|
* an integer value of n bits. pubkey_nbits gives us the
|
|
|
|
* number of bits we have to use. We then encode the session
|
|
|
|
* key in some way and we get it back in the big intger value
|
|
|
|
* FRAME. Then we use FRAME, the public key PK->PKEY and the
|
|
|
|
* algorithm number PK->PUBKEY_ALGO and pass it to pubkey_encrypt
|
|
|
|
* which returns the encrypted value in the array ENC->DATA.
|
|
|
|
* This array has a size which depends on the used algorithm
|
|
|
|
* (e.g. 2 for ElGamal). We don't need frame anymore because we
|
|
|
|
* have everything now in enc->data which is the passed to
|
|
|
|
* build_packet()
|
|
|
|
*/
|
1998-06-29 14:30:57 +02:00
|
|
|
frame = encode_session_key( dek, pubkey_nbits( pk->pubkey_algo,
|
|
|
|
pk->pkey ) );
|
|
|
|
rc = pubkey_encrypt( pk->pubkey_algo, enc->data, frame, pk->pkey );
|
1998-06-13 08:59:14 +02:00
|
|
|
mpi_free( frame );
|
|
|
|
if( rc )
|
|
|
|
log_error("pubkey_encrypt failed: %s\n", g10_errstr(rc) );
|
|
|
|
else {
|
|
|
|
if( opt.verbose ) {
|
1999-11-12 20:32:00 +01:00
|
|
|
char *ustr = get_user_id_string_native( enc->keyid );
|
1998-12-10 20:20:47 +01:00
|
|
|
log_info(_("%s/%s encrypted for: %s\n"),
|
|
|
|
pubkey_algo_to_string(enc->pubkey_algo),
|
|
|
|
cipher_algo_to_string(dek->algo), ustr );
|
1998-06-13 08:59:14 +02:00
|
|
|
m_free(ustr);
|
|
|
|
}
|
|
|
|
/* and write it */
|
|
|
|
init_packet(&pkt);
|
|
|
|
pkt.pkttype = PKT_PUBKEY_ENC;
|
|
|
|
pkt.pkt.pubkey_enc = enc;
|
|
|
|
rc = build_packet( out, &pkt );
|
|
|
|
if( rc )
|
|
|
|
log_error("build_packet(pubkey_enc) failed: %s\n", g10_errstr(rc));
|
|
|
|
}
|
1998-01-02 21:40:10 +01:00
|
|
|
free_pubkey_enc(enc);
|
1998-06-13 08:59:14 +02:00
|
|
|
if( rc )
|
1998-01-02 21:40:10 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
1997-11-18 15:06:00 +01:00
|
|
|
|
2002-01-03 20:47:20 +01:00
|
|
|
void
|
2002-01-09 19:52:19 +01:00
|
|
|
encode_crypt_files(int nfiles, char **files, STRLIST remusr)
|
2002-01-03 20:47:20 +01:00
|
|
|
{
|
2002-01-15 17:52:36 +01:00
|
|
|
int rc = 0;
|
2002-01-12 23:31:41 +01:00
|
|
|
|
2002-01-15 17:52:36 +01:00
|
|
|
if (opt.outfile)
|
|
|
|
{
|
|
|
|
log_error(_("--output doesn't work for this command\n"));
|
|
|
|
return;
|
|
|
|
}
|
2002-01-09 19:52:19 +01:00
|
|
|
|
2002-01-15 17:52:36 +01:00
|
|
|
if (!nfiles)
|
|
|
|
{
|
|
|
|
char line[2048];
|
|
|
|
unsigned int lno = 0;
|
|
|
|
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);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
line[strlen(line)-1] = '\0';
|
|
|
|
print_file_status(STATUS_FILE_START, line, 2);
|
|
|
|
if ( (rc = encode_crypt(line, remusr)) )
|
|
|
|
log_error("%s: encryption failed: %s\n",
|
|
|
|
print_fname_stdin(line), g10_errstr(rc) );
|
|
|
|
write_status( STATUS_FILE_DONE );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (nfiles--)
|
|
|
|
{
|
|
|
|
print_file_status(STATUS_FILE_START, *files, 2);
|
|
|
|
if ( (rc = encode_crypt(*files, remusr)) )
|
|
|
|
log_error("%s: encryption failed: %s\n",
|
|
|
|
print_fname_stdin(*files), g10_errstr(rc) );
|
|
|
|
write_status( STATUS_FILE_DONE );
|
|
|
|
files++;
|
|
|
|
}
|
|
|
|
}
|
2002-01-03 20:47:20 +01:00
|
|
|
}
|