Imported Upstream version 2.1.8
[platform/upstream/gpg2.git] / g10 / encrypt.c
1 /* encrypt.c - Main encryption driver
2  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3  *               2006, 2009 Free Software Foundation, Inc.
4  *
5  * This file is part of GnuPG.
6  *
7  * GnuPG is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * GnuPG is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <assert.h>
27
28 #include "gpg.h"
29 #include "options.h"
30 #include "packet.h"
31 #include "status.h"
32 #include "iobuf.h"
33 #include "keydb.h"
34 #include "util.h"
35 #include "main.h"
36 #include "filter.h"
37 #include "trustdb.h"
38 #include "i18n.h"
39 #include "status.h"
40 #include "pkglue.h"
41
42
43 static int encrypt_simple( const char *filename, int mode, int use_seskey );
44 static int write_pubkey_enc_from_list( PK_LIST pk_list, DEK *dek, iobuf_t out );
45
46 /****************
47  * Encrypt FILENAME with only the symmetric cipher.  Take input from
48  * stdin if FILENAME is NULL.
49  */
50 int
51 encrypt_symmetric (const char *filename)
52 {
53   return encrypt_simple( filename, 1, 0 );
54 }
55
56
57 /****************
58  * Encrypt FILENAME as a literal data packet only. Take input from
59  * stdin if FILENAME is NULL.
60  */
61 int
62 encrypt_store (const char *filename)
63 {
64   return encrypt_simple( filename, 0, 0 );
65 }
66
67
68 static void
69 encrypt_seskey (DEK *dek, DEK **seskey, byte *enckey)
70 {
71   gcry_cipher_hd_t hd;
72   byte buf[33];
73
74   assert ( dek->keylen <= 32 );
75   if (!*seskey)
76     {
77       *seskey=xmalloc_clear(sizeof(DEK));
78       (*seskey)->keylen=dek->keylen;
79       (*seskey)->algo=dek->algo;
80       make_session_key(*seskey);
81       /*log_hexdump( "thekey", c->key, c->keylen );*/
82     }
83
84   /* The encrypted session key is prefixed with a one-octet algorithm id.  */
85   buf[0] = (*seskey)->algo;
86   memcpy( buf + 1, (*seskey)->key, (*seskey)->keylen );
87
88   /* We only pass already checked values to the following fucntion,
89      thus we consider any failure as fatal.  */
90   if (openpgp_cipher_open (&hd, dek->algo, GCRY_CIPHER_MODE_CFB, 1))
91     BUG ();
92   if (gcry_cipher_setkey (hd, dek->key, dek->keylen))
93     BUG ();
94   gcry_cipher_setiv (hd, NULL, 0);
95   gcry_cipher_encrypt (hd, buf, (*seskey)->keylen + 1, NULL, 0);
96   gcry_cipher_close (hd);
97
98   memcpy( enckey, buf, (*seskey)->keylen + 1 );
99   wipememory( buf, sizeof buf ); /* burn key */
100 }
101
102
103 /* We try very hard to use a MDC */
104 static int
105 use_mdc(PK_LIST pk_list,int algo)
106 {
107   /* RFC-2440 don't has MDC */
108   if (RFC2440)
109     return 0;
110
111   /* --force-mdc overrides --disable-mdc */
112   if(opt.force_mdc)
113     return 1;
114
115   if(opt.disable_mdc)
116     return 0;
117
118   /* Do the keys really support MDC? */
119
120   if(select_mdc_from_pklist(pk_list))
121     return 1;
122
123   /* The keys don't support MDC, so now we do a bit of a hack - if any
124      of the AESes or TWOFISH are in the prefs, we assume that the user
125      can handle a MDC.  This is valid for PGP 7, which can handle MDCs
126      though it will not generate them.  2440bis allows this, by the
127      way. */
128
129   if(select_algo_from_prefs(pk_list,PREFTYPE_SYM,
130                             CIPHER_ALGO_AES,NULL)==CIPHER_ALGO_AES)
131     return 1;
132
133   if(select_algo_from_prefs(pk_list,PREFTYPE_SYM,
134                             CIPHER_ALGO_AES192,NULL)==CIPHER_ALGO_AES192)
135     return 1;
136
137   if(select_algo_from_prefs(pk_list,PREFTYPE_SYM,
138                             CIPHER_ALGO_AES256,NULL)==CIPHER_ALGO_AES256)
139     return 1;
140
141   if(select_algo_from_prefs(pk_list,PREFTYPE_SYM,
142                             CIPHER_ALGO_TWOFISH,NULL)==CIPHER_ALGO_TWOFISH)
143     return 1;
144
145   /* Last try.  Use MDC for the modern ciphers. */
146
147   if (openpgp_cipher_get_algo_blklen (algo) != 8)
148     return 1;
149
150   if (opt.verbose)
151     warn_missing_mdc_from_pklist (pk_list);
152
153   return 0; /* No MDC */
154 }
155
156
157 /* We don't want to use use_seskey yet because older gnupg versions
158    can't handle it, and there isn't really any point unless we're
159    making a message that can be decrypted by a public key or
160    passphrase. */
161 static int
162 encrypt_simple (const char *filename, int mode, int use_seskey)
163 {
164   iobuf_t inp, out;
165   PACKET pkt;
166   PKT_plaintext *pt = NULL;
167   STRING2KEY *s2k = NULL;
168   byte enckey[33];
169   int rc = 0;
170   int seskeylen = 0;
171   u32 filesize;
172   cipher_filter_context_t cfx;
173   armor_filter_context_t  *afx = NULL;
174   compress_filter_context_t zfx;
175   text_filter_context_t tfx;
176   progress_filter_context_t *pfx;
177   int do_compress = !!default_compress_algo();
178
179   pfx = new_progress_context ();
180   memset( &cfx, 0, sizeof cfx);
181   memset( &zfx, 0, sizeof zfx);
182   memset( &tfx, 0, sizeof tfx);
183   init_packet(&pkt);
184
185   /* Prepare iobufs. */
186   inp = iobuf_open(filename);
187   if (inp)
188     iobuf_ioctl (inp, IOBUF_IOCTL_NO_CACHE, 1, NULL);
189   if (inp && is_secured_file (iobuf_get_fd (inp)))
190     {
191       iobuf_close (inp);
192       inp = NULL;
193       gpg_err_set_errno (EPERM);
194     }
195   if (!inp)
196     {
197       rc = gpg_error_from_syserror ();
198       log_error(_("can't open '%s': %s\n"), filename? filename: "[stdin]",
199                 strerror(errno) );
200       release_progress_context (pfx);
201       return rc;
202     }
203
204   handle_progress (pfx, inp, filename);
205
206   if (opt.textmode)
207     iobuf_push_filter( inp, text_filter, &tfx );
208
209   cfx.dek = NULL;
210   if ( mode )
211     {
212       int canceled;
213
214       s2k = xmalloc_clear( sizeof *s2k );
215       s2k->mode = opt.s2k_mode;
216       s2k->hash_algo = S2K_DIGEST_ALGO;
217       cfx.dek = passphrase_to_dek (NULL, 0,
218                                    default_cipher_algo(), s2k, 4,
219                                    NULL, &canceled);
220       if ( !cfx.dek || !cfx.dek->keylen )
221         {
222           rc = gpg_error (canceled? GPG_ERR_CANCELED:GPG_ERR_INV_PASSPHRASE);
223           xfree (cfx.dek);
224           xfree (s2k);
225           iobuf_close (inp);
226           log_error (_("error creating passphrase: %s\n"), gpg_strerror (rc));
227           release_progress_context (pfx);
228           return rc;
229         }
230       if (use_seskey && s2k->mode != 1 && s2k->mode != 3)
231         {
232           use_seskey = 0;
233           log_info (_("can't use a symmetric ESK packet "
234                       "due to the S2K mode\n"));
235         }
236
237       if ( use_seskey )
238         {
239           DEK *dek = NULL;
240
241           seskeylen = openpgp_cipher_get_algo_keylen (default_cipher_algo ());
242           encrypt_seskey( cfx.dek, &dek, enckey );
243           xfree( cfx.dek ); cfx.dek = dek;
244         }
245
246       if (opt.verbose)
247         log_info(_("using cipher %s\n"),
248                  openpgp_cipher_algo_name (cfx.dek->algo));
249
250       cfx.dek->use_mdc=use_mdc(NULL,cfx.dek->algo);
251     }
252
253   if (do_compress && cfx.dek && cfx.dek->use_mdc
254       && is_file_compressed(filename, &rc))
255     {
256       if (opt.verbose)
257         log_info(_("'%s' already compressed\n"), filename);
258       do_compress = 0;
259     }
260
261   if ( rc || (rc = open_outfile (-1, filename, opt.armor? 1:0, 0, &out )))
262     {
263       iobuf_cancel (inp);
264       xfree (cfx.dek);
265       xfree (s2k);
266       release_progress_context (pfx);
267       return rc;
268     }
269
270   if ( opt.armor )
271     {
272       afx = new_armor_context ();
273       push_armor_filter (afx, out);
274     }
275
276   if ( s2k )
277     {
278       PKT_symkey_enc *enc = xmalloc_clear( sizeof *enc + seskeylen + 1 );
279       enc->version = 4;
280       enc->cipher_algo = cfx.dek->algo;
281       enc->s2k = *s2k;
282       if ( use_seskey && seskeylen )
283         {
284           enc->seskeylen = seskeylen + 1; /* algo id */
285           memcpy (enc->seskey, enckey, seskeylen + 1 );
286         }
287       pkt.pkttype = PKT_SYMKEY_ENC;
288       pkt.pkt.symkey_enc = enc;
289       if ((rc = build_packet( out, &pkt )))
290         log_error("build symkey packet failed: %s\n", gpg_strerror (rc) );
291       xfree (enc);
292     }
293
294   if (!opt.no_literal)
295     pt = setup_plaintext_name (filename, inp);
296
297   /* Note that PGP 5 has problems decrypting symmetrically encrypted
298      data if the file length is in the inner packet. It works when
299      only partial length headers are use.  In the past, we always used
300      partial body length here, but since PGP 2, PGP 6, and PGP 7 need
301      the file length, and nobody should be using PGP 5 nowadays
302      anyway, this is now set to the file length.  Note also that this
303      only applies to the RFC-1991 style symmetric messages, and not
304      the RFC-2440 style.  PGP 6 and 7 work with either partial length
305      or fixed length with the new style messages. */
306
307   if ( !iobuf_is_pipe_filename (filename) && *filename && !opt.textmode )
308     {
309       off_t tmpsize;
310       int overflow;
311
312       if ( !(tmpsize = iobuf_get_filelength(inp, &overflow))
313            && !overflow && opt.verbose)
314         log_info(_("WARNING: '%s' is an empty file\n"), filename );
315       /* We can't encode the length of very large files because
316          OpenPGP uses only 32 bit for file sizes.  So if the the
317          size of a file is larger than 2^32 minus some bytes for
318          packet headers, we switch to partial length encoding. */
319       if ( tmpsize < (IOBUF_FILELENGTH_LIMIT - 65536) )
320         filesize = tmpsize;
321       else
322         filesize = 0;
323     }
324   else
325     filesize = opt.set_filesize ? opt.set_filesize : 0; /* stdin */
326
327   if (!opt.no_literal)
328     {
329       pt->timestamp = make_timestamp();
330       pt->mode = opt.textmode? 't' : 'b';
331       pt->len = filesize;
332       pt->new_ctb = !pt->len;
333       pt->buf = inp;
334       pkt.pkttype = PKT_PLAINTEXT;
335       pkt.pkt.plaintext = pt;
336       cfx.datalen = filesize && !do_compress ? calc_packet_length( &pkt ) : 0;
337     }
338   else
339     {
340       cfx.datalen = filesize && !do_compress ? filesize : 0;
341       pkt.pkttype = 0;
342       pkt.pkt.generic = NULL;
343     }
344
345   /* Register the cipher filter. */
346   if (mode)
347     iobuf_push_filter ( out, cipher_filter, &cfx );
348
349   /* Register the compress filter. */
350   if ( do_compress )
351     {
352       if (cfx.dek && cfx.dek->use_mdc)
353         zfx.new_ctb = 1;
354       push_compress_filter (out, &zfx, default_compress_algo());
355     }
356
357   /* Do the work. */
358   if (!opt.no_literal)
359     {
360       if ( (rc = build_packet( out, &pkt )) )
361         log_error("build_packet failed: %s\n", gpg_strerror (rc) );
362     }
363   else
364     {
365       /* User requested not to create a literal packet, so we copy the
366          plain data.  */
367     byte copy_buffer[4096];
368     int  bytes_copied;
369     while ((bytes_copied = iobuf_read(inp, copy_buffer, 4096)) != -1)
370       if ( (rc=iobuf_write(out, copy_buffer, bytes_copied)) ) {
371         log_error ("copying input to output failed: %s\n",
372                    gpg_strerror (rc) );
373         break;
374       }
375     wipememory (copy_buffer, 4096); /* burn buffer */
376     }
377
378   /* Finish the stuff.  */
379   iobuf_close (inp);
380   if (rc)
381     iobuf_cancel(out);
382   else
383     {
384       iobuf_close (out); /* fixme: check returncode */
385       if (mode)
386         write_status ( STATUS_END_ENCRYPTION );
387     }
388   if (pt)
389     pt->buf = NULL;
390   free_packet (&pkt);
391   xfree (cfx.dek);
392   xfree (s2k);
393   release_armor_context (afx);
394   release_progress_context (pfx);
395   return rc;
396 }
397
398
399 int
400 setup_symkey (STRING2KEY **symkey_s2k,DEK **symkey_dek)
401 {
402   int canceled;
403
404   *symkey_s2k=xmalloc_clear(sizeof(STRING2KEY));
405   (*symkey_s2k)->mode = opt.s2k_mode;
406   (*symkey_s2k)->hash_algo = S2K_DIGEST_ALGO;
407
408   *symkey_dek=passphrase_to_dek(NULL,0,opt.s2k_cipher_algo,
409                                 *symkey_s2k, 4, NULL, &canceled);
410   if(!*symkey_dek || !(*symkey_dek)->keylen)
411     {
412       xfree(*symkey_dek);
413       xfree(*symkey_s2k);
414       return gpg_error (canceled?GPG_ERR_CANCELED:GPG_ERR_BAD_PASSPHRASE);
415     }
416
417   return 0;
418 }
419
420
421 static int
422 write_symkey_enc (STRING2KEY *symkey_s2k, DEK *symkey_dek, DEK *dek,
423                   iobuf_t out)
424 {
425   int rc, seskeylen = openpgp_cipher_get_algo_keylen (dek->algo);
426
427   PKT_symkey_enc *enc;
428   byte enckey[33];
429   PACKET pkt;
430
431   enc=xmalloc_clear(sizeof(PKT_symkey_enc)+seskeylen+1);
432   encrypt_seskey(symkey_dek,&dek,enckey);
433
434   enc->version = 4;
435   enc->cipher_algo = opt.s2k_cipher_algo;
436   enc->s2k = *symkey_s2k;
437   enc->seskeylen = seskeylen + 1; /* algo id */
438   memcpy( enc->seskey, enckey, seskeylen + 1 );
439
440   pkt.pkttype = PKT_SYMKEY_ENC;
441   pkt.pkt.symkey_enc = enc;
442
443   if ((rc=build_packet(out,&pkt)))
444     log_error("build symkey_enc packet failed: %s\n",gpg_strerror (rc));
445
446   xfree(enc);
447   return rc;
448 }
449
450
451 /*
452  * Encrypt the file with the given userids (or ask if none is
453  * supplied).  Either FILENAME or FILEFD must be given, but not both.
454  * The caller may provide a checked list of public keys in
455  * PROVIDED_PKS; if not the function builds a list of keys on its own.
456  *
457  * Note that FILEFD is currently only used by cmd_encrypt in the the
458  * not yet finished server.c.
459  */
460 int
461 encrypt_crypt (ctrl_t ctrl, int filefd, const char *filename,
462                strlist_t remusr, int use_symkey, pk_list_t provided_keys,
463                int outputfd)
464 {
465   iobuf_t inp = NULL;
466   iobuf_t out = NULL;
467   PACKET pkt;
468   PKT_plaintext *pt = NULL;
469   DEK *symkey_dek = NULL;
470   STRING2KEY *symkey_s2k = NULL;
471   int rc = 0, rc2 = 0;
472   u32 filesize;
473   cipher_filter_context_t cfx;
474   armor_filter_context_t *afx = NULL;
475   compress_filter_context_t zfx;
476   text_filter_context_t tfx;
477   progress_filter_context_t *pfx;
478   PK_LIST pk_list;
479   int do_compress;
480
481   if (filefd != -1 && filename)
482     return gpg_error (GPG_ERR_INV_ARG);  /* Both given.  */
483
484   do_compress = !!opt.compress_algo;
485
486   pfx = new_progress_context ();
487   memset( &cfx, 0, sizeof cfx);
488   memset( &zfx, 0, sizeof zfx);
489   memset( &tfx, 0, sizeof tfx);
490   init_packet(&pkt);
491
492   if (use_symkey
493       && (rc=setup_symkey(&symkey_s2k,&symkey_dek)))
494     {
495       release_progress_context (pfx);
496       return rc;
497     }
498
499   if (provided_keys)
500     pk_list = provided_keys;
501   else
502     {
503       if ((rc = build_pk_list (ctrl, remusr, &pk_list, PUBKEY_USAGE_ENC)))
504         {
505           release_progress_context (pfx);
506           return rc;
507         }
508     }
509
510   /* Prepare iobufs. */
511 #ifdef HAVE_W32_SYSTEM
512   if (filefd == -1)
513     inp = iobuf_open (filename);
514   else
515     {
516       inp = NULL;
517       gpg_err_set_errno (ENOSYS);
518     }
519 #else
520   if (filefd == GNUPG_INVALID_FD)
521     inp = iobuf_open (filename);
522   else
523     inp = iobuf_fdopen_nc (FD2INT(filefd), "rb");
524 #endif
525   if (inp)
526     iobuf_ioctl (inp, IOBUF_IOCTL_NO_CACHE, 1, NULL);
527   if (inp && is_secured_file (iobuf_get_fd (inp)))
528     {
529       iobuf_close (inp);
530       inp = NULL;
531       gpg_err_set_errno (EPERM);
532     }
533   if (!inp)
534     {
535       char xname[64];
536
537       rc = gpg_error_from_syserror ();
538       if (filefd != -1)
539         snprintf (xname, sizeof xname, "[fd %d]", filefd);
540       else if (!filename)
541         strcpy (xname, "[stdin]");
542       else
543         *xname = 0;
544       log_error (_("can't open '%s': %s\n"),
545                  *xname? xname : filename, gpg_strerror (rc) );
546       goto leave;
547     }
548
549   if (opt.verbose)
550     log_info (_("reading from '%s'\n"), iobuf_get_fname_nonnull (inp));
551
552   handle_progress (pfx, inp, filename);
553
554   if (opt.textmode)
555     iobuf_push_filter (inp, text_filter, &tfx);
556
557   rc = open_outfile (outputfd, filename, opt.armor? 1:0, 0, &out);
558   if (rc)
559     goto leave;
560
561   if (opt.armor)
562     {
563       afx = new_armor_context ();
564       push_armor_filter (afx, out);
565     }
566
567   /* Create a session key. */
568   cfx.dek = xmalloc_secure_clear (sizeof *cfx.dek);
569   if (!opt.def_cipher_algo)
570     {
571       /* Try to get it from the prefs.  */
572       cfx.dek->algo = select_algo_from_prefs (pk_list, PREFTYPE_SYM, -1, NULL);
573       /* The only way select_algo_from_prefs can fail here is when
574          mixing v3 and v4 keys, as v4 keys have an implicit preference
575          entry for 3DES, and the pk_list cannot be empty.  In this
576          case, use 3DES anyway as it's the safest choice - perhaps the
577          v3 key is being used in an OpenPGP implementation and we know
578          that the implementation behind any v4 key can handle 3DES. */
579       if (cfx.dek->algo == -1)
580         {
581           cfx.dek->algo = CIPHER_ALGO_3DES;
582         }
583
584       /* In case 3DES has been selected, print a warning if any key
585          does not have a preference for AES.  This should help to
586          indentify why encrypting to several recipients falls back to
587          3DES. */
588       if (opt.verbose && cfx.dek->algo == CIPHER_ALGO_3DES)
589         warn_missing_aes_from_pklist (pk_list);
590     }
591   else
592     {
593       if (!opt.expert
594           && (select_algo_from_prefs (pk_list, PREFTYPE_SYM,
595                                       opt.def_cipher_algo, NULL)
596               != opt.def_cipher_algo))
597         {
598           log_info(_("WARNING: forcing symmetric cipher %s (%d)"
599                      " violates recipient preferences\n"),
600                    openpgp_cipher_algo_name (opt.def_cipher_algo),
601                    opt.def_cipher_algo);
602         }
603
604       cfx.dek->algo = opt.def_cipher_algo;
605     }
606
607   cfx.dek->use_mdc = use_mdc (pk_list,cfx.dek->algo);
608
609   /* Only do the is-file-already-compressed check if we are using a
610      MDC.  This forces compressed files to be re-compressed if we do
611      not have a MDC to give some protection against chosen ciphertext
612      attacks. */
613
614   if (do_compress && cfx.dek->use_mdc && is_file_compressed(filename, &rc2))
615     {
616       if (opt.verbose)
617         log_info(_("'%s' already compressed\n"), filename);
618       do_compress = 0;
619     }
620   if (rc2)
621     {
622       rc = rc2;
623       goto leave;
624     }
625
626   make_session_key (cfx.dek);
627   if (DBG_CRYPTO)
628     log_printhex ("DEK is: ", cfx.dek->key, cfx.dek->keylen );
629
630   rc = write_pubkey_enc_from_list (pk_list, cfx.dek, out);
631   if (rc)
632     goto leave;
633
634   /* We put the passphrase (if any) after any public keys as this
635      seems to be the most useful on the recipient side - there is no
636      point in prompting a user for a passphrase if they have the
637      secret key needed to decrypt.  */
638   if(use_symkey && (rc = write_symkey_enc(symkey_s2k,symkey_dek,cfx.dek,out)))
639     goto leave;
640
641   if (!opt.no_literal)
642     pt = setup_plaintext_name (filename, inp);
643
644   /* Get the size of the file if possible, i.e., if it is a real file.  */
645   if (filename && *filename
646       && !iobuf_is_pipe_filename (filename) && !opt.textmode )
647     {
648       off_t tmpsize;
649       int overflow;
650
651       if ( !(tmpsize = iobuf_get_filelength(inp, &overflow))
652            && !overflow && opt.verbose)
653         log_info(_("WARNING: '%s' is an empty file\n"), filename );
654       /* We can't encode the length of very large files because
655          OpenPGP uses only 32 bit for file sizes.  So if the the size
656          of a file is larger than 2^32 minus some bytes for packet
657          headers, we switch to partial length encoding. */
658       if (tmpsize < (IOBUF_FILELENGTH_LIMIT - 65536) )
659         filesize = tmpsize;
660       else
661         filesize = 0;
662     }
663   else
664     filesize = opt.set_filesize ? opt.set_filesize : 0; /* stdin */
665
666   if (!opt.no_literal)
667     {
668       pt->timestamp = make_timestamp();
669       pt->mode = opt.textmode ? 't' : 'b';
670       pt->len = filesize;
671       pt->new_ctb = !pt->len;
672       pt->buf = inp;
673       pkt.pkttype = PKT_PLAINTEXT;
674       pkt.pkt.plaintext = pt;
675       cfx.datalen = filesize && !do_compress? calc_packet_length( &pkt ) : 0;
676     }
677   else
678     cfx.datalen = filesize && !do_compress ? filesize : 0;
679
680   /* Register the cipher filter. */
681   iobuf_push_filter (out, cipher_filter, &cfx);
682
683   /* Register the compress filter. */
684   if (do_compress)
685     {
686       int compr_algo = opt.compress_algo;
687
688       if (compr_algo == -1)
689         {
690           compr_algo = select_algo_from_prefs (pk_list, PREFTYPE_ZIP, -1, NULL);
691           if (compr_algo == -1)
692             compr_algo = DEFAULT_COMPRESS_ALGO;
693           /* Theoretically impossible to get here since uncompressed
694              is implicit.  */
695         }
696       else if (!opt.expert
697                && select_algo_from_prefs(pk_list, PREFTYPE_ZIP,
698                                          compr_algo, NULL) != compr_algo)
699         {
700           log_info (_("WARNING: forcing compression algorithm %s (%d)"
701                       " violates recipient preferences\n"),
702                     compress_algo_to_string(compr_algo), compr_algo);
703         }
704
705       /* Algo 0 means no compression. */
706       if (compr_algo)
707         {
708           if (cfx.dek && cfx.dek->use_mdc)
709             zfx.new_ctb = 1;
710           push_compress_filter (out,&zfx,compr_algo);
711         }
712     }
713
714   /* Do the work. */
715   if (!opt.no_literal)
716     {
717       if ((rc = build_packet( out, &pkt )))
718         log_error ("build_packet failed: %s\n", gpg_strerror (rc));
719     }
720   else
721     {
722       /* User requested not to create a literal packet, so we copy the
723          plain data. */
724       byte copy_buffer[4096];
725       int  bytes_copied;
726       while ((bytes_copied = iobuf_read (inp, copy_buffer, 4096)) != -1)
727         {
728           rc = iobuf_write (out, copy_buffer, bytes_copied);
729           if (rc)
730             {
731               log_error ("copying input to output failed: %s\n",
732                          gpg_strerror (rc));
733               break;
734             }
735         }
736       wipememory (copy_buffer, 4096); /* Burn the buffer. */
737     }
738
739   /* Finish the stuff. */
740  leave:
741   iobuf_close (inp);
742   if (rc)
743     iobuf_cancel (out);
744   else
745     {
746       iobuf_close (out); /* fixme: check returncode */
747       write_status (STATUS_END_ENCRYPTION);
748     }
749   if (pt)
750     pt->buf = NULL;
751   free_packet (&pkt);
752   xfree (cfx.dek);
753   xfree (symkey_dek);
754   xfree (symkey_s2k);
755   if (!provided_keys)
756     release_pk_list (pk_list);
757   release_armor_context (afx);
758   release_progress_context (pfx);
759   return rc;
760 }
761
762
763 /*
764  * Filter to do a complete public key encryption.
765  */
766 int
767 encrypt_filter (void *opaque, int control,
768                 iobuf_t a, byte *buf, size_t *ret_len)
769 {
770   size_t size = *ret_len;
771   encrypt_filter_context_t *efx = opaque;
772   int rc = 0;
773
774   if (control == IOBUFCTRL_UNDERFLOW) /* decrypt */
775     {
776       BUG(); /* not used */
777     }
778   else if ( control == IOBUFCTRL_FLUSH ) /* encrypt */
779     {
780       if ( !efx->header_okay )
781         {
782           efx->cfx.dek = xmalloc_secure_clear ( sizeof *efx->cfx.dek );
783           if ( !opt.def_cipher_algo  )
784             {
785               /* Try to get it from the prefs. */
786               efx->cfx.dek->algo =
787                 select_algo_from_prefs (efx->pk_list, PREFTYPE_SYM, -1, NULL);
788               if (efx->cfx.dek->algo == -1 )
789                 {
790                   /* Because 3DES is implicitly in the prefs, this can
791                      only happen if we do not have any public keys in
792                      the list.  */
793                   efx->cfx.dek->algo = DEFAULT_CIPHER_ALGO;
794                 }
795
796               /* In case 3DES has been selected, print a warning if
797                  any key does not have a preference for AES.  This
798                  should help to indentify why encrypting to several
799                  recipients falls back to 3DES. */
800               if (opt.verbose
801                   && efx->cfx.dek->algo == CIPHER_ALGO_3DES)
802                 warn_missing_aes_from_pklist (efx->pk_list);
803             }
804           else
805             {
806               if (!opt.expert
807                   && select_algo_from_prefs (efx->pk_list,PREFTYPE_SYM,
808                                              opt.def_cipher_algo,
809                                              NULL) != opt.def_cipher_algo)
810                 log_info(_("forcing symmetric cipher %s (%d) "
811                            "violates recipient preferences\n"),
812                          openpgp_cipher_algo_name (opt.def_cipher_algo),
813                          opt.def_cipher_algo);
814
815               efx->cfx.dek->algo = opt.def_cipher_algo;
816             }
817
818           efx->cfx.dek->use_mdc = use_mdc (efx->pk_list,efx->cfx.dek->algo);
819
820           make_session_key ( efx->cfx.dek );
821           if (DBG_CRYPTO)
822             log_printhex ("DEK is: ", efx->cfx.dek->key, efx->cfx.dek->keylen);
823
824           rc = write_pubkey_enc_from_list (efx->pk_list, efx->cfx.dek, a);
825           if (rc)
826             return rc;
827
828             if(efx->symkey_s2k && efx->symkey_dek)
829               {
830                 rc=write_symkey_enc(efx->symkey_s2k,efx->symkey_dek,
831                                     efx->cfx.dek,a);
832                 if(rc)
833                   return rc;
834               }
835
836             iobuf_push_filter (a, cipher_filter, &efx->cfx);
837
838             efx->header_okay = 1;
839         }
840       rc = iobuf_write (a, buf, size);
841
842     }
843   else if (control == IOBUFCTRL_FREE)
844     {
845       xfree (efx->symkey_dek);
846       xfree (efx->symkey_s2k);
847     }
848   else if ( control == IOBUFCTRL_DESC )
849     {
850       *(char**)buf = "encrypt_filter";
851     }
852   return rc;
853 }
854
855
856 /*
857  * Write pubkey-enc packets from the list of PKs to OUT.
858  */
859 static int
860 write_pubkey_enc_from_list (PK_LIST pk_list, DEK *dek, iobuf_t out)
861 {
862   PACKET pkt;
863   PKT_public_key *pk;
864   PKT_pubkey_enc  *enc;
865   int rc;
866
867   for ( ; pk_list; pk_list = pk_list->next )
868     {
869       gcry_mpi_t frame;
870
871       pk = pk_list->pk;
872
873       print_pubkey_algo_note ( pk->pubkey_algo );
874       enc = xmalloc_clear ( sizeof *enc );
875       enc->pubkey_algo = pk->pubkey_algo;
876       keyid_from_pk( pk, enc->keyid );
877       enc->throw_keyid = (opt.throw_keyids || (pk_list->flags&1));
878
879       if (opt.throw_keyids && (PGP6 || PGP7 || PGP8))
880         {
881           log_info(_("you may not use %s while in %s mode\n"),
882                    "--throw-keyids",compliance_option_string());
883           compliance_failure();
884         }
885
886       /* Okay, what's going on: We have the session key somewhere in
887        * the structure DEK and want to encode this session key in an
888        * integer value of n bits. pubkey_nbits gives us the number of
889        * bits we have to use.  We then encode the session key in some
890        * way and we get it back in the big intger value FRAME.  Then
891        * we use FRAME, the public key PK->PKEY and the algorithm
892        * number PK->PUBKEY_ALGO and pass it to pubkey_encrypt which
893        * returns the encrypted value in the array ENC->DATA.  This
894        * array has a size which depends on the used algorithm (e.g. 2
895        * for Elgamal).  We don't need frame anymore because we have
896        * everything now in enc->data which is the passed to
897        * build_packet().  */
898       frame = encode_session_key (pk->pubkey_algo, dek,
899                                   pubkey_nbits (pk->pubkey_algo, pk->pkey));
900       rc = pk_encrypt (pk->pubkey_algo, enc->data, frame, pk, pk->pkey);
901       gcry_mpi_release (frame);
902       if (rc)
903         log_error ("pubkey_encrypt failed: %s\n", gpg_strerror (rc) );
904       else
905         {
906           if ( opt.verbose )
907             {
908               char *ustr = get_user_id_string_native (enc->keyid);
909               log_info (_("%s/%s encrypted for: \"%s\"\n"),
910                         openpgp_pk_algo_name (enc->pubkey_algo),
911                         openpgp_cipher_algo_name (dek->algo),
912                         ustr );
913               xfree (ustr);
914             }
915           /* And write it. */
916           init_packet (&pkt);
917           pkt.pkttype = PKT_PUBKEY_ENC;
918           pkt.pkt.pubkey_enc = enc;
919           rc = build_packet (out, &pkt);
920           if (rc)
921             log_error ("build_packet(pubkey_enc) failed: %s\n",
922                        gpg_strerror (rc));
923         }
924       free_pubkey_enc(enc);
925       if (rc)
926         return rc;
927     }
928   return 0;
929 }
930
931
932 void
933 encrypt_crypt_files (ctrl_t ctrl, int nfiles, char **files, strlist_t remusr)
934 {
935   int rc = 0;
936
937   if (opt.outfile)
938     {
939       log_error(_("--output doesn't work for this command\n"));
940       return;
941     }
942
943   if (!nfiles)
944     {
945       char line[2048];
946       unsigned int lno = 0;
947       while ( fgets(line, DIM(line), stdin) )
948         {
949           lno++;
950           if (!*line || line[strlen(line)-1] != '\n')
951             {
952               log_error("input line %u too long or missing LF\n", lno);
953               return;
954             }
955           line[strlen(line)-1] = '\0';
956           print_file_status(STATUS_FILE_START, line, 2);
957           rc = encrypt_crypt (ctrl, -1, line, remusr, 0, NULL, -1);
958           if (rc)
959             log_error ("encryption of '%s' failed: %s\n",
960                        print_fname_stdin(line), gpg_strerror (rc) );
961           write_status( STATUS_FILE_DONE );
962         }
963     }
964   else
965     {
966       while (nfiles--)
967         {
968           print_file_status(STATUS_FILE_START, *files, 2);
969           if ( (rc = encrypt_crypt (ctrl, -1, *files, remusr, 0, NULL, -1)) )
970             log_error("encryption of '%s' failed: %s\n",
971                       print_fname_stdin(*files), gpg_strerror (rc) );
972           write_status( STATUS_FILE_DONE );
973           files++;
974         }
975     }
976 }