~ubuntu-branches/ubuntu/oneiric/gnupg2/oneiric-updates

« back to all changes in this revision

Viewing changes to sm/encrypt.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* encrypt.c - Encrypt a message
2
 
 *      Copyright (C) 2001, 2003, 2004 Free Software Foundation, Inc.
 
2
 *      Copyright (C) 2001, 2003, 2004, 2007 Free Software Foundation, Inc.
3
3
 *
4
4
 * This file is part of GnuPG.
5
5
 *
6
6
 * GnuPG is free software; you can redistribute it and/or modify
7
7
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * the Free Software Foundation; either version 3 of the License, or
9
9
 * (at your option) any later version.
10
10
 *
11
11
 * GnuPG is distributed in the hope that it will be useful,
14
14
 * GNU General Public License for more details.
15
15
 *
16
16
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
17
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19
18
 */
20
19
 
21
20
#include <config.h>
75
74
      return gpg_error (GPG_ERR_UNSUPPORTED_ALGORITHM);
76
75
    }
77
76
  
78
 
  /* Extra check for algorithms we considere to be to weak for
79
 
     encryption, qlthough we suppor them fro decryption.  Note that
 
77
  /* Extra check for algorithms we consider to be too weak for
 
78
     encryption, although we support them for decryption.  Note that
80
79
     there is another check below discriminating on the key length. */
81
80
  switch (dek->algo)
82
81
    {
216
215
  buf = xtrymalloc (len);
217
216
  if (!buf)
218
217
    {
219
 
      gpg_error_t tmperr = OUT_OF_CORE (errno);
 
218
      gpg_error_t tmperr = out_of_core ();
220
219
      gcry_sexp_release (s_ciph);
221
220
      return tmperr;
222
221
    }
223
222
  len = gcry_sexp_sprint (s_ciph, GCRYSEXP_FMT_CANON, (char*)buf, len);
224
223
  assert (len);
 
224
  gcry_sexp_release (s_ciph);
225
225
 
226
226
  *encval = buf;
227
227
  return 0;
303
303
   recipients are take from the certificate given in recplist; if this
304
304
   is NULL it will be encrypted for a default recipient */
305
305
int
306
 
gpgsm_encrypt (CTRL ctrl, CERTLIST recplist, int data_fd, FILE *out_fp)
 
306
gpgsm_encrypt (ctrl_t ctrl, certlist_t recplist, int data_fd, FILE *out_fp)
307
307
{
308
308
  int rc = 0;
309
309
  Base64Context b64writer = NULL;
317
317
  DEK dek = NULL;
318
318
  int recpno;
319
319
  FILE *data_fp = NULL;
320
 
  CERTLIST cl;
 
320
  certlist_t cl;
 
321
  int count;
321
322
 
322
323
  memset (&encparm, 0, sizeof encparm);
323
324
 
 
325
  audit_set_type (ctrl->audit, AUDIT_TYPE_ENCRYPT);
 
326
 
324
327
  /* Check that the certificate list is not empty and that at least
325
328
     one certificate is not flagged as encrypt_to; i.e. is a real
326
329
     recipient. */
331
334
    {
332
335
      log_error(_("no valid recipients given\n"));
333
336
      gpgsm_status (ctrl, STATUS_NO_RECP, "0");
 
337
      audit_log_i (ctrl->audit, AUDIT_GOT_RECIPIENTS, 0);
334
338
      rc = gpg_error (GPG_ERR_NO_PUBKEY);
335
339
      goto leave;
336
340
    }
337
341
 
 
342
  for (count = 0, cl = recplist; cl; cl = cl->next)
 
343
    count++;
 
344
  audit_log_i (ctrl->audit, AUDIT_GOT_RECIPIENTS, count);
 
345
 
338
346
  kh = keydb_new (0);
339
347
  if (!kh)
340
348
    {
362
370
  encparm.fp = data_fp;
363
371
 
364
372
  ctrl->pem_name = "ENCRYPTED MESSAGE";
365
 
  rc = gpgsm_create_writer (&b64writer, ctrl, out_fp, &writer);
 
373
  rc = gpgsm_create_writer (&b64writer, ctrl, out_fp, NULL, &writer);
366
374
  if (rc)
367
375
    {
368
376
      log_error ("can't create writer: %s\n", gpg_strerror (rc));
385
393
      goto leave;
386
394
    }
387
395
 
 
396
  audit_log (ctrl->audit, AUDIT_GOT_DATA);
 
397
 
388
398
  /* We are going to create enveloped data with uninterpreted data as
389
399
     inner content */
390
400
  err = ksba_cms_set_content_type (cms, 0, KSBA_CT_ENVELOPED_DATA);
401
411
  /* Create a session key */
402
412
  dek = xtrycalloc_secure (1, sizeof *dek); 
403
413
  if (!dek)
404
 
    rc = OUT_OF_CORE (errno);
 
414
    rc = out_of_core ();
405
415
  else
406
416
  {
407
417
    dek->algoid = opt.def_cipher_algoid;
429
439
  encparm.buffer = xtrymalloc (encparm.bufsize);
430
440
  if (!encparm.buffer)
431
441
    {
432
 
      rc = OUT_OF_CORE (errno);
 
442
      rc = out_of_core ();
433
443
      goto leave;
434
444
    }
 
445
  
 
446
  audit_log_s (ctrl->audit, AUDIT_SESSION_KEY, dek->algoid);
435
447
 
436
448
  /* Gather certificates of recipients, encrypt the session key for
437
449
     each and store them in the CMS object */
442
454
      rc = encrypt_dek (dek, cl->cert, &encval);
443
455
      if (rc)
444
456
        {
 
457
          audit_log_cert (ctrl->audit, AUDIT_ENCRYPTED_TO, cl->cert, rc);
445
458
          log_error ("encryption failed for recipient no. %d: %s\n",
446
459
                     recpno, gpg_strerror (rc));
447
460
          goto leave;
450
463
      err = ksba_cms_add_recipient (cms, cl->cert);
451
464
      if (err)
452
465
        {
 
466
          audit_log_cert (ctrl->audit, AUDIT_ENCRYPTED_TO, cl->cert, err);
453
467
          log_error ("ksba_cms_add_recipient failed: %s\n",
454
468
                     gpg_strerror (err));
455
469
          rc = err;
459
473
      
460
474
      err = ksba_cms_set_enc_val (cms, recpno, encval);
461
475
      xfree (encval);
 
476
      audit_log_cert (ctrl->audit, AUDIT_ENCRYPTED_TO, cl->cert, err);
462
477
      if (err)
463
478
        {
464
479
          log_error ("ksba_cms_set_enc_val failed: %s\n",
466
481
          rc = err;
467
482
          goto leave;
468
483
        }
469
 
  }
 
484
    }
470
485
 
471
486
  /* Main control loop for encryption. */
472
487
  recpno = 0;
496
511
      log_error ("write failed: %s\n", gpg_strerror (rc));
497
512
      goto leave;
498
513
    }
 
514
  audit_log (ctrl->audit, AUDIT_ENCRYPTION_DONE);
499
515
  log_info ("encrypted data created\n");
500
516
 
501
517
 leave: