diff options
author | Werner Koch <[email protected]> | 2000-10-04 11:16:19 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 2000-10-04 11:16:19 +0000 |
commit | 9c20f65cbe9d3d806b02f0f90a2051a20f3857db (patch) | |
tree | 6c784f870b191b2a1b3f16f86cff3a5af59ff2f6 /g10/encode.c | |
parent | See ChangeLog: Mon Sep 18 16:35:45 CEST 2000 Werner Koch (diff) | |
download | gnupg-9c20f65cbe9d3d806b02f0f90a2051a20f3857db.tar.gz gnupg-9c20f65cbe9d3d806b02f0f90a2051a20f3857db.zip |
See ChangeLog: Wed Oct 4 13:16:18 CEST 2000 Werner Koch
Diffstat (limited to 'g10/encode.c')
-rw-r--r-- | g10/encode.c | 135 |
1 files changed, 60 insertions, 75 deletions
diff --git a/g10/encode.c b/g10/encode.c index a817f9094..f033c76ae 100644 --- a/g10/encode.c +++ b/g10/encode.c @@ -293,18 +293,17 @@ encode_crypt( const char *filename, STRLIST remusr ) PKT_plaintext *pt = NULL; int rc = 0; u32 filesize; - cipher_filter_context_t cfx; armor_filter_context_t afx; compress_filter_context_t zfx; text_filter_context_t tfx; + encrypt_filter_context_t efx; PK_LIST pk_list; int do_compress = opt.compress && !opt.rfc1991; - - memset( &cfx, 0, sizeof cfx); memset( &afx, 0, sizeof afx); memset( &zfx, 0, sizeof zfx); memset( &tfx, 0, sizeof tfx); + memset( &efx, 0, sizeof efx); init_packet(&pkt); if( (rc=build_pk_list( remusr, &pk_list, GCRY_PK_USAGE_ENCR)) ) @@ -320,83 +319,67 @@ encode_crypt( const char *filename, STRLIST remusr ) else if( opt.verbose ) log_info(_("reading from `%s'\n"), filename? filename: "[stdin]"); + /* If the user selected textmode, push the text filter onto the input */ if( opt.textmode ) iobuf_push_filter( inp, text_filter, &tfx ); + /* Now we can create the outputfile */ if( (rc = open_outfile( filename, opt.armor? 1:0, &out )) ) goto leave; - + /* The first thing we have to push on the output stream + * is the armor filter */ if( opt.armor ) iobuf_push_filter( out, armor_filter, &afx ); - #ifdef ENABLE_COMMENT_PACKETS - else { - write_comment( out, "#created by GNUPG v" VERSION " (" - PRINTABLE_OS_NAME ")"); - if( opt.comment_string ) - write_comment( out, opt.comment_string ); - } - #endif - /* create a session key */ - cfx.dek = gcry_xmalloc_secure( sizeof *cfx.dek ); - if( !opt.def_cipher_algo ) { /* try to get it from the prefs */ - cfx.dek->algo = select_algo_from_prefs( pk_list, PREFTYPE_SYM ); - if( cfx.dek->algo == -1 ) - cfx.dek->algo = DEFAULT_CIPHER_ALGO; - } - else - cfx.dek->algo = opt.def_cipher_algo; - make_session_key( cfx.dek ); - if( DBG_CIPHER ) - log_hexdump("DEK is: ", cfx.dek->key, cfx.dek->keylen ); - - rc = write_pubkey_enc_from_list( pk_list, cfx.dek, out ); - if( rc ) - goto leave; - - 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 = gcry_xmalloc( sizeof *pt + strlen(s) - 1 ); - pt->namelen = strlen(s); - memcpy(pt->name, s, pt->namelen ); - gcry_free(s); - } - else { /* no filename */ - pt = gcry_xmalloc( sizeof *pt - 1 ); - pt->namelen = 0; - } - } - - if( filename && !opt.textmode ) { - if( !(filesize = iobuf_get_filelength(inp)) ) - log_info(_("%s: WARNING: empty file\n"), filename ); - /* 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; - } - else - 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; - - /* register the cipher filter */ - iobuf_push_filter( out, cipher_filter, &cfx ); - /* register the compress filter */ + /* Prepare the plaintext packet */ + { + if (!opt.no_literal) { + if( filename || opt.set_filename ) { + char *s = make_basename( opt.set_filename ? + opt.set_filename : filename ); + pt = gcry_xmalloc( sizeof *pt + strlen(s) - 1 ); + pt->namelen = strlen(s); + memcpy(pt->name, s, pt->namelen ); + gcry_free(s); + } + else { /* no filename */ + pt = gcry_xmalloc( sizeof *pt - 1 ); + pt->namelen = 0; + } + } + + if( filename && !opt.textmode ) { + if( !(filesize = iobuf_get_filelength(inp)) ) + log_info(_("%s: WARNING: empty file\n"), filename ); + /* 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; + } + else + 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; + efx.cfx.datalen = filesize && !do_compress? + calc_packet_length( &pkt ) : 0; + } + else + efx.cfx.datalen = filesize && !do_compress ? filesize : 0; + } /* end preparation of plaintext packet */ + + /* push in the actual encryption filter */ + efx.pk_list = pk_list; + iobuf_push_filter( out, encrypt_filter, &efx ); + + /* register the compress filter (so that it is done before encryption) */ if( do_compress ) { int compr_algo = select_algo_from_prefs( pk_list, PREFTYPE_COMPR ); if( !compr_algo ) @@ -414,7 +397,8 @@ encode_crypt( const char *filename, STRLIST remusr ) log_error("build_packet failed: %s\n", gpg_errstr(rc) ); } else { - /* user requested not to create a literal packet, so we copy the plain data */ + /* 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) @@ -423,7 +407,7 @@ encode_crypt( const char *filename, STRLIST remusr ) log_error("copying input to output failed: %s\n", gpg_errstr(rc) ); break; } - memset(copy_buffer, 0, 4096); /* burn buffer */ + memset(copy_buffer, 0, DIM(copy_buffer)); /* burn buffer */ } /* finish the stuff */ @@ -436,7 +420,8 @@ encode_crypt( const char *filename, STRLIST remusr ) if( pt ) pt->buf = NULL; free_packet(&pkt); - gcry_free(cfx.dek); + gcry_free(efx.cfx.dek); /* Hmmm, why does the encrypt filter does not + * take care about this? */ release_pk_list( pk_list ); return rc; } @@ -445,7 +430,7 @@ encode_crypt( const char *filename, STRLIST remusr ) /**************** - * Filter to do a complete public key encryption. + * Filter to handle the entire public key encryption. */ int encrypt_filter( void *opaque, int control, |