LCOV - code coverage report
Current view: top level - src - encrypt-sign.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 43 62 69.4 %
Date: 2016-11-29 15:07:43 Functions: 4 5 80.0 %

          Line data    Source code
       1             : /* encrypt-sign.c -  encrypt and verify functions
       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             : static gpgme_error_t
      36          99 : encrypt_sign_status_handler (void *priv, gpgme_status_code_t code, char *args)
      37             : {
      38             :   gpgme_error_t err;
      39             : 
      40          99 :   err = _gpgme_progress_status_handler (priv, code, args);
      41          99 :   if (!err)
      42          99 :     err = _gpgme_encrypt_status_handler (priv, code, args);
      43          99 :   if (!err)
      44          97 :     err = _gpgme_sign_status_handler (priv, code, args);
      45          99 :   return err;
      46             : }
      47             : 
      48             : 
      49             : static gpgme_error_t
      50          39 : encrypt_sym_status_handler (void *priv, gpgme_status_code_t code, char *args)
      51             : {
      52             :   gpgme_error_t err;
      53             : 
      54          39 :   err = _gpgme_progress_status_handler (priv, code, args);
      55          39 :   if (!err)
      56          39 :     err = _gpgme_sign_status_handler (priv, code, args);
      57          39 :   if (!err)
      58          39 :     err = _gpgme_passphrase_status_handler (priv, code, args);
      59          39 :   return err;
      60             : }
      61             : 
      62             : 
      63             : static gpgme_error_t
      64          12 : encrypt_sign_start (gpgme_ctx_t ctx, int synchronous, gpgme_key_t recp[],
      65             :                     gpgme_encrypt_flags_t flags,
      66             :                     gpgme_data_t plain, gpgme_data_t cipher)
      67             : {
      68             :   gpgme_error_t err;
      69             :   int symmetric;
      70             : 
      71          12 :   err = _gpgme_op_reset (ctx, synchronous);
      72          12 :   if (err)
      73           0 :     return err;
      74             : 
      75          12 :   symmetric = !recp || (flags & GPGME_ENCRYPT_SYMMETRIC);
      76             : 
      77          12 :   if (!plain)
      78           0 :     return gpg_error (GPG_ERR_NO_DATA);
      79          12 :   if (!cipher)
      80           0 :     return gpg_error (GPG_ERR_INV_VALUE);
      81          12 :   if (recp && !*recp)
      82           0 :     return gpg_error (GPG_ERR_INV_VALUE);
      83             : 
      84          12 :   err = _gpgme_op_encrypt_init_result (ctx);
      85          12 :   if (err)
      86           0 :     return err;
      87             : 
      88          12 :   err = _gpgme_op_sign_init_result (ctx);
      89          12 :   if (err)
      90           0 :     return err;
      91             : 
      92          12 :   if (ctx->passphrase_cb)
      93             :     {
      94           2 :       err = _gpgme_engine_set_command_handler
      95             :         (ctx->engine, _gpgme_passphrase_command_handler, ctx, NULL);
      96           2 :       if (err)
      97           0 :         return err;
      98             :     }
      99             : 
     100          12 :   _gpgme_engine_set_status_handler (ctx->engine,
     101             :                                     symmetric
     102             :                                     ? encrypt_sym_status_handler
     103             :                                     : encrypt_sign_status_handler,
     104             :                                     ctx);
     105             : 
     106          12 :   return _gpgme_engine_op_encrypt_sign (ctx->engine, recp, flags, plain,
     107          12 :                                         cipher, ctx->use_armor,
     108             :                                         ctx /* FIXME */);
     109             : }
     110             : 
     111             : 
     112             : /* Encrypt plaintext PLAIN within CTX for the recipients RECP and
     113             :    store the resulting ciphertext in CIPHER.  Also sign the ciphertext
     114             :    with the signers in CTX.  */
     115             : gpgme_error_t
     116           0 : gpgme_op_encrypt_sign_start (gpgme_ctx_t ctx, gpgme_key_t recp[],
     117             :                              gpgme_encrypt_flags_t flags,
     118             :                              gpgme_data_t plain, gpgme_data_t cipher)
     119             : {
     120             :   gpgme_error_t err;
     121             : 
     122           0 :   TRACE_BEG3 (DEBUG_CTX, "gpgme_op_encrypt_sign_start", ctx,
     123             :               "flags=0x%x, plain=%p, cipher=%p", flags, plain, cipher);
     124             : 
     125           0 :   if (!ctx)
     126           0 :     return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
     127             : 
     128           0 :   if (_gpgme_debug_trace () && recp)
     129             :     {
     130           0 :       int i = 0;
     131             : 
     132           0 :       while (recp[i])
     133             :         {
     134           0 :           TRACE_LOG3 ("recipient[%i] = %p (%s)", i, recp[i],
     135             :                       (recp[i]->subkeys && recp[i]->subkeys->fpr) ?
     136             :                       recp[i]->subkeys->fpr : "invalid");
     137           0 :           i++;
     138             :         }
     139             :     }
     140             : 
     141           0 :   err = encrypt_sign_start (ctx, 0, recp, flags, plain, cipher);
     142           0 :   return err;
     143             : }
     144             : 
     145             : 
     146             : /* Encrypt plaintext PLAIN within CTX for the recipients RECP and
     147             :    store the resulting ciphertext in CIPHER.  Also sign the ciphertext
     148             :    with the signers in CTX.  */
     149             : gpgme_error_t
     150          12 : gpgme_op_encrypt_sign (gpgme_ctx_t ctx, gpgme_key_t recp[],
     151             :                        gpgme_encrypt_flags_t flags,
     152             :                        gpgme_data_t plain, gpgme_data_t cipher)
     153             : {
     154             :   gpgme_error_t err;
     155             : 
     156          12 :   TRACE_BEG3 (DEBUG_CTX, "gpgme_op_encrypt_sign", ctx,
     157             :               "flags=0x%x, plain=%p, cipher=%p", flags, plain, cipher);
     158             : 
     159          12 :   if (!ctx)
     160           0 :     return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
     161             : 
     162          12 :   if (_gpgme_debug_trace () && recp)
     163             :     {
     164           9 :       int i = 0;
     165             : 
     166          36 :       while (recp[i])
     167             :         {
     168          18 :           TRACE_LOG3 ("recipient[%i] = %p (%s)", i, recp[i],
     169             :                       (recp[i]->subkeys && recp[i]->subkeys->fpr) ?
     170             :                       recp[i]->subkeys->fpr : "invalid");
     171          18 :           i++;
     172             :         }
     173             :     }
     174             : 
     175          12 :   err = encrypt_sign_start (ctx, 1, recp, flags, plain, cipher);
     176          12 :   if (!err)
     177          12 :     err = _gpgme_wait_one (ctx);
     178          12 :   return TRACE_ERR (err);
     179             : }

Generated by: LCOV version 1.11