1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-11-13 22:08:52 +01:00
gnupg/g10/encode.c

337 lines
8.4 KiB
C
Raw Normal View History

1997-11-24 12:04:11 +01:00
/* encode.c - encode data
1998-02-24 19:50:46 +01:00
* Copyright (C) 1998 Free Software Foundation, Inc.
1997-11-18 15:06:00 +01:00
*
1998-02-24 19:50:46 +01:00
* This file is part of GNUPG.
1997-11-18 15:06:00 +01:00
*
1998-02-24 19:50:46 +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-02-24 19:50:46 +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"
1997-11-18 15:06:00 +01:00
static int encode_simple( const char *filename, int mode );
1998-01-02 21:40:10 +01:00
static int write_pubkey_enc_from_list( PKC_LIST pkc_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 );
}
static int
encode_simple( const char *filename, int mode )
{
IOBUF inp, out;
PACKET pkt;
PKT_plaintext *pt;
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;
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);
1997-11-18 15:06:00 +01:00
/* prepare iobufs */
if( !(inp = iobuf_open(filename)) ) {
log_error("can't open %s: %s\n", filename? filename: "[stdin]",
strerror(errno) );
return G10ERR_OPEN_FILE;
}
cfx.dek = NULL;
if( mode ) {
cfx.dek = m_alloc_secure( sizeof *cfx.dek );
1998-01-07 21:47:46 +01:00
cfx.dek->algo = opt.def_cipher_algo;
1998-02-24 19:50:46 +01:00
if( (rc = make_dek_from_passphrase( cfx.dek , 2, NULL )) ) {
1997-11-18 15:06:00 +01:00
m_free(cfx.dek);
iobuf_close(inp);
log_error("error creating passphrase: %s\n", g10_errstr(rc) );
return rc;
}
}
if( !(out = open_outfile( filename, opt.armor? 1:0 )) ) {
1997-11-18 15:06:00 +01:00
iobuf_close(inp);
m_free(cfx.dek);
return G10ERR_CREATE_FILE; /* or user said: do not overwrite */
}
if( opt.armor )
1997-11-19 14:12:23 +01:00
iobuf_push_filter( out, armor_filter, &afx );
1997-11-18 15:06:00 +01:00
1998-02-24 19:50:46 +01:00
write_comment( out, "#created by GNUPG v" VERSION " ("
1998-02-16 21:05:02 +01:00
PRINTABLE_OS_NAME ")");
1997-11-18 15:06:00 +01:00
if( opt.compress )
1997-11-23 16:38:27 +01:00
iobuf_push_filter( out, compress_filter, &zfx );
1997-11-18 15:06:00 +01:00
/* setup the inner packet */
if( filename ) {
pt = m_alloc( sizeof *pt + strlen(filename) - 1 );
pt->namelen = strlen(filename);
memcpy(pt->name, filename, pt->namelen );
if( !(filesize = iobuf_get_filelength(inp)) )
log_info("warning: '%s' is an empty file\n", filename );
}
else { /* no filename */
pt = m_alloc( sizeof *pt - 1 );
pt->namelen = 0;
filesize = 0; /* stdin */
}
pt->timestamp = make_timestamp();
pt->mode = 'b';
pt->len = filesize;
pt->buf = inp;
pkt.pkttype = PKT_PLAINTEXT;
pkt.pkt.plaintext = pt;
1997-11-23 16:38:27 +01:00
cfx.datalen = filesize? calc_packet_length( &pkt ) : 0;
1997-11-18 15:06:00 +01:00
/* register the cipher filter */
if( mode )
iobuf_push_filter( out, cipher_filter, &cfx );
/* do the work */
if( (rc = build_packet( out, &pkt )) )
log_error("build_packet failed: %s\n", g10_errstr(rc) );
/* finish the stuff */
iobuf_close(inp);
iobuf_close(out); /* fixme: check returncode */
pt->buf = NULL;
free_packet(&pkt);
m_free(cfx.dek);
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;
1997-12-31 13:32:54 +01:00
int rc = 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;
1998-01-02 21:40:10 +01:00
PKC_LIST pkc_list;
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);
1997-11-18 15:06:00 +01:00
1998-04-08 21:49:02 +02:00
if( (rc=build_pkc_list( remusr, &pkc_list, 2)) )
1997-12-31 13:32:54 +01:00
return rc;
1997-11-18 15:06:00 +01:00
/* prepare iobufs */
if( !(inp = iobuf_open(filename)) ) {
log_error("can't open %s: %s\n", filename? filename: "[stdin]",
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-02-13 21:58:50 +01:00
log_info("reading from '%s'\n", filename? filename: "[stdin]");
1997-11-18 15:06:00 +01:00
if( !(out = open_outfile( filename, opt.armor? 1:0 )) ) {
1997-12-31 13:32:54 +01:00
rc = G10ERR_CREATE_FILE; /* or user said: do not overwrite */
goto leave;
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 );
1997-11-18 15:06:00 +01:00
1998-02-24 19:50:46 +01:00
write_comment( out, "#created by GNUPG v" VERSION " ("
1998-02-16 21:05:02 +01:00
PRINTABLE_OS_NAME ")");
1997-11-18 15:06:00 +01:00
if( opt.compress )
1997-11-23 16:38:27 +01:00
iobuf_push_filter( out, compress_filter, &zfx );
1997-11-18 15:06:00 +01:00
/* create a session key */
cfx.dek = m_alloc_secure( sizeof *cfx.dek );
1998-01-07 21:47:46 +01:00
cfx.dek->algo = opt.def_cipher_algo;
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-01-02 21:40:10 +01:00
rc = write_pubkey_enc_from_list( pkc_list, cfx.dek, out );
if( rc )
goto leave;
1997-11-18 15:06:00 +01:00
/* setup the inner packet */
if( filename ) {
pt = m_alloc( sizeof *pt + strlen(filename) - 1 );
pt->namelen = strlen(filename);
memcpy(pt->name, filename, pt->namelen );
if( !(filesize = iobuf_get_filelength(inp)) )
log_info("warning: '%s' is an empty file\n", filename );
}
else { /* no filename */
pt = m_alloc( sizeof *pt - 1 );
pt->namelen = 0;
filesize = 0; /* stdin */
}
pt->timestamp = make_timestamp();
pt->mode = 'b';
pt->len = filesize;
pt->buf = inp;
init_packet(&pkt);
pkt.pkttype = PKT_PLAINTEXT;
pkt.pkt.plaintext = pt;
1997-11-23 16:38:27 +01:00
cfx.datalen = filesize? calc_packet_length( &pkt ) : 0;
1997-11-18 15:06:00 +01:00
/* register the cipher filter */
iobuf_push_filter( out, cipher_filter, &cfx );
/* do the work */
if( (rc = build_packet( out, &pkt )) )
log_error("build_packet failed: %s\n", g10_errstr(rc) );
/* 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);
else
iobuf_close(out); /* fixme: check returncode */
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);
1997-12-31 13:32:54 +01:00
release_pkc_list( pkc_list );
1997-11-18 15:06:00 +01:00
return rc;
}
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 ) {
efx->cfx.dek = m_alloc_secure( sizeof *efx->cfx.dek );
1998-01-07 21:47:46 +01:00
efx->cfx.dek->algo = opt.def_cipher_algo;
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 );
rc = write_pubkey_enc_from_list( efx->pkc_list, efx->cfx.dek, a );
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
/****************
* Write pubkey-enc packets from the list of PKCs to OUT.
*/
static int
write_pubkey_enc_from_list( PKC_LIST pkc_list, DEK *dek, IOBUF out )
{
PACKET pkt;
PKT_public_cert *pkc;
PKT_pubkey_enc *enc;
int rc;
for( ; pkc_list; pkc_list = pkc_list->next ) {
pkc = pkc_list->pkc;
enc = m_alloc_clear( sizeof *enc );
enc->pubkey_algo = pkc->pubkey_algo;
if( enc->pubkey_algo == PUBKEY_ALGO_ELGAMAL )
g10_elg_encrypt( pkc, enc, dek );
else if( enc->pubkey_algo == PUBKEY_ALGO_RSA )
g10_rsa_encrypt( pkc, enc, dek );
else
1998-01-16 22:15:24 +01:00
BUG();
1998-01-02 21:40:10 +01:00
/* and write it */
init_packet(&pkt);
pkt.pkttype = PKT_PUBKEY_ENC;
pkt.pkt.pubkey_enc = enc;
rc = build_packet( out, &pkt );
free_pubkey_enc(enc);
if( rc ) {
log_error("build pubkey_enc packet failed: %s\n", g10_errstr(rc) );
return rc;
}
}
return 0;
}
1997-11-18 15:06:00 +01:00