LCOV - code coverage report
Current view: top level - src - encrypt.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 79 109 72.5 %
Date: 2015-11-05 17:14:26 Functions: 9 9 100.0 %

          Line data    Source code
       1             : /* encrypt.c - Encrypt function.
       2             :    Copyright (C) 2000 Werner Koch (dd9jn)
       3             :    Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH
       4             : 
       5             :    This file is part of GPGME.
       6             : 
       7             :    GPGME is free software; you can redistribute it and/or modify it
       8             :    under the terms of the GNU Lesser General Public License as
       9             :    published by the Free Software Foundation; either version 2.1 of
      10             :    the License, or (at your option) any later version.
      11             : 
      12             :    GPGME is distributed in the hope that it will be useful, but
      13             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :    Lesser General Public License for more details.
      16             : 
      17             :    You should have received a copy of the GNU Lesser General Public
      18             :    License along with this program; if not, write to the Free Software
      19             :    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
      20             :    02111-1307, USA.  */
      21             : 
      22             : #if HAVE_CONFIG_H
      23             : #include <config.h>
      24             : #endif
      25             : #include <stdlib.h>
      26             : #include <string.h>
      27             : #include <errno.h>
      28             : 
      29             : #include "gpgme.h"
      30             : #include "debug.h"
      31             : #include "context.h"
      32             : #include "ops.h"
      33             : 
      34             : 
      35             : typedef struct
      36             : {
      37             :   struct _gpgme_op_encrypt_result result;
      38             : 
      39             :   /* The error code from a FAILURE status line or 0.  */
      40             :   gpg_error_t failure_code;
      41             : 
      42             :   /* A pointer to the next pointer of the last invalid recipient in
      43             :      the list.  This makes appending new invalid recipients painless
      44             :      while preserving the order.  */
      45             :   gpgme_invalid_key_t *lastp;
      46             : } *op_data_t;
      47             : 
      48             : 
      49             : static void
      50          28 : release_op_data (void *hook)
      51             : {
      52          28 :   op_data_t opd = (op_data_t) hook;
      53          28 :   gpgme_invalid_key_t invalid_recipient = opd->result.invalid_recipients;
      54             : 
      55          56 :   while (invalid_recipient)
      56             :     {
      57           0 :       gpgme_invalid_key_t next = invalid_recipient->next;
      58           0 :       if (invalid_recipient->fpr)
      59           0 :         free (invalid_recipient->fpr);
      60           0 :       free (invalid_recipient);
      61           0 :       invalid_recipient = next;
      62             :     }
      63          28 : }
      64             : 
      65             : 
      66             : gpgme_encrypt_result_t
      67          24 : gpgme_op_encrypt_result (gpgme_ctx_t ctx)
      68             : {
      69             :   void *hook;
      70             :   op_data_t opd;
      71             :   gpgme_error_t err;
      72             : 
      73          24 :   TRACE_BEG (DEBUG_CTX, "gpgme_op_encrypt_result", ctx);
      74             : 
      75          24 :   err = _gpgme_op_data_lookup (ctx, OPDATA_ENCRYPT, &hook, -1, NULL);
      76          24 :   opd = hook;
      77             : 
      78          24 :   if (err || !opd)
      79             :     {
      80           0 :       TRACE_SUC0 ("result=(null)");
      81           0 :       return NULL;
      82             :     }
      83             : 
      84             :   if (_gpgme_debug_trace ())
      85             :     {
      86          24 :       gpgme_invalid_key_t invkeys = opd->result.invalid_recipients;
      87          24 :       int i = 0;
      88             : 
      89          48 :       while (invkeys)
      90             :         {
      91           0 :           TRACE_LOG3 ("invalid_recipients[%i] = %s (%s)",
      92             :                       i, invkeys->fpr ? invkeys->fpr : "(null)",
      93             :                       gpg_strerror (invkeys->reason));
      94           0 :           invkeys = invkeys->next;
      95           0 :           i++;
      96             :         }
      97             :     }
      98             : 
      99          24 :   TRACE_SUC1 ("result=%p", &opd->result);
     100          24 :   return &opd->result;
     101             : }
     102             : 
     103             : 
     104             : gpgme_error_t
     105         128 : _gpgme_encrypt_status_handler (void *priv, gpgme_status_code_t code,
     106             :                                char *args)
     107             : {
     108         128 :   gpgme_ctx_t ctx = (gpgme_ctx_t) priv;
     109             :   gpgme_error_t err;
     110             :   void *hook;
     111             :   op_data_t opd;
     112             : 
     113         128 :   err = _gpgme_op_data_lookup (ctx, OPDATA_ENCRYPT, &hook, -1, NULL);
     114         128 :   opd = hook;
     115         128 :   if (err)
     116           0 :     return err;
     117             : 
     118         128 :   switch (code)
     119             :     {
     120             :     case GPGME_STATUS_FAILURE:
     121           0 :       opd->failure_code = _gpgme_parse_failure (args);
     122           0 :       break;
     123             : 
     124             :     case GPGME_STATUS_EOF:
     125          26 :       if (opd->result.invalid_recipients)
     126           0 :         return gpg_error (GPG_ERR_UNUSABLE_PUBKEY);
     127          26 :       if (opd->failure_code)
     128           0 :         return opd->failure_code;
     129          26 :       break;
     130             : 
     131             :     case GPGME_STATUS_INV_RECP:
     132           0 :       err = _gpgme_parse_inv_recp (args, opd->lastp);
     133           0 :       if (err)
     134           0 :         return err;
     135             : 
     136           0 :       opd->lastp = &(*opd->lastp)->next;
     137           0 :       break;
     138             : 
     139             :     case GPGME_STATUS_NO_RECP:
     140             :       /* Should not happen, because we require at least one recipient.  */
     141           0 :       return gpg_error (GPG_ERR_GENERAL);
     142             : 
     143             :     default:
     144         102 :       break;
     145             :     }
     146         128 :   return 0;
     147             : }
     148             : 
     149             : 
     150             : static gpgme_error_t
     151           8 : encrypt_sym_status_handler (void *priv, gpgme_status_code_t code, char *args)
     152             : {
     153             :   gpgme_error_t err;
     154             : 
     155           8 :   err = _gpgme_progress_status_handler (priv, code, args);
     156           8 :   if (!err)
     157           8 :     err = _gpgme_passphrase_status_handler (priv, code, args);
     158           8 :   return err;
     159             : }
     160             : 
     161             : 
     162             : static gpgme_error_t
     163         121 : encrypt_status_handler (void *priv, gpgme_status_code_t code, char *args)
     164             : {
     165         242 :   return _gpgme_progress_status_handler (priv, code, args)
     166         121 :     || _gpgme_encrypt_status_handler (priv, code, args);
     167             : }
     168             : 
     169             : 
     170             : gpgme_error_t
     171          28 : _gpgme_op_encrypt_init_result (gpgme_ctx_t ctx)
     172             : {
     173             :   gpgme_error_t err;
     174             :   void *hook;
     175             :   op_data_t opd;
     176             : 
     177          28 :   err = _gpgme_op_data_lookup (ctx, OPDATA_ENCRYPT, &hook, sizeof (*opd),
     178             :                                release_op_data);
     179          28 :   opd = hook;
     180          28 :   if (err)
     181           0 :     return err;
     182             : 
     183          28 :   opd->lastp = &opd->result.invalid_recipients;
     184          28 :   return 0;
     185             : }
     186             : 
     187             : 
     188             : static gpgme_error_t
     189          26 : encrypt_start (gpgme_ctx_t ctx, int synchronous, gpgme_key_t recp[],
     190             :                gpgme_encrypt_flags_t flags,
     191             :                gpgme_data_t plain, gpgme_data_t cipher)
     192             : {
     193             :   gpgme_error_t err;
     194          26 :   int symmetric = 0;
     195             : 
     196          26 :   err = _gpgme_op_reset (ctx, synchronous);
     197          26 :   if (err)
     198           0 :     return err;
     199             : 
     200          26 :   err = _gpgme_op_encrypt_init_result (ctx);
     201          26 :   if (err)
     202           0 :     return err;
     203             : 
     204          26 :   if (!recp)
     205           1 :     symmetric = 1;
     206             : 
     207          26 :   if (!plain)
     208           0 :     return gpg_error (GPG_ERR_NO_DATA);
     209          26 :   if (!cipher)
     210           0 :     return gpg_error (GPG_ERR_INV_VALUE);
     211          26 :   if (recp && ! *recp)
     212           0 :     return gpg_error (GPG_ERR_INV_VALUE);
     213             : 
     214          26 :   if (symmetric && ctx->passphrase_cb)
     215             :     {
     216             :       /* Symmetric encryption requires a passphrase.  */
     217           1 :       err = _gpgme_engine_set_command_handler
     218             :         (ctx->engine, _gpgme_passphrase_command_handler, ctx, NULL);
     219           1 :       if (err)
     220           0 :         return err;
     221             :     }
     222             : 
     223          26 :   _gpgme_engine_set_status_handler (ctx->engine,
     224             :                                     symmetric
     225             :                                     ? encrypt_sym_status_handler
     226             :                                     : encrypt_status_handler,
     227             :                                     ctx);
     228             : 
     229          26 :   return _gpgme_engine_op_encrypt (ctx->engine, recp, flags, plain, cipher,
     230          26 :                                    ctx->use_armor);
     231             : }
     232             : 
     233             : 
     234             : gpgme_error_t
     235           1 : gpgme_op_encrypt_start (gpgme_ctx_t ctx, gpgme_key_t recp[],
     236             :                         gpgme_encrypt_flags_t flags,
     237             :                         gpgme_data_t plain, gpgme_data_t cipher)
     238             : {
     239             :   gpgme_error_t err;
     240             : 
     241           1 :   TRACE_BEG3 (DEBUG_CTX, "gpgme_op_encrypt_start", ctx,
     242             :               "flags=0x%x, plain=%p, cipher=%p", flags, plain, cipher);
     243             : 
     244           1 :   if (!ctx)
     245           0 :     return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
     246             : 
     247           1 :   if (_gpgme_debug_trace () && recp)
     248             :     {
     249           1 :       int i = 0;
     250             : 
     251           4 :       while (recp[i])
     252             :         {
     253           2 :           TRACE_LOG3 ("recipient[%i] = %p (%s)", i, recp[i],
     254             :                       (recp[i]->subkeys && recp[i]->subkeys->fpr) ?
     255             :                       recp[i]->subkeys->fpr : "invalid");
     256           2 :           i++;
     257             :         }
     258             :     }
     259             : 
     260           1 :   err = encrypt_start (ctx, 0, recp, flags, plain, cipher);
     261           1 :   return TRACE_ERR (err);
     262             : }
     263             : 
     264             : 
     265             : /* Encrypt plaintext PLAIN within CTX for the recipients RECP and
     266             :    store the resulting ciphertext in CIPHER.  */
     267             : gpgme_error_t
     268          25 : gpgme_op_encrypt (gpgme_ctx_t ctx, gpgme_key_t recp[],
     269             :                   gpgme_encrypt_flags_t flags,
     270             :                   gpgme_data_t plain, gpgme_data_t cipher)
     271             : {
     272             :   gpgme_error_t err;
     273             : 
     274          25 :   TRACE_BEG3 (DEBUG_CTX, "gpgme_op_encrypt", ctx,
     275             :               "flags=0x%x, plain=%p, cipher=%p", flags, plain, cipher);
     276             : 
     277          25 :   if (!ctx)
     278           0 :     return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
     279             : 
     280          25 :   if (_gpgme_debug_trace () && recp)
     281             :     {
     282          24 :       int i = 0;
     283             : 
     284          94 :       while (recp[i])
     285             :         {
     286          46 :           TRACE_LOG3 ("recipient[%i] = %p (%s)", i, recp[i],
     287             :                       (recp[i]->subkeys && recp[i]->subkeys->fpr) ?
     288             :                       recp[i]->subkeys->fpr : "invalid");
     289          46 :           i++;
     290             :         }
     291             :     }
     292             : 
     293          25 :   err = encrypt_start (ctx, 1, recp, flags, plain, cipher);
     294          25 :   if (!err)
     295          25 :     err = _gpgme_wait_one (ctx);
     296          25 :   return TRACE_ERR (err);
     297             : }

Generated by: LCOV version 1.11