LCOV - code coverage report
Current view: top level - tests - run-decrypt.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 105 0.0 %
Date: 2016-12-01 18:45:36 Functions: 0 4 0.0 %

          Line data    Source code
       1             : /* run-decrypt.c  - Helper to perform a verify operation
       2             :    Copyright (C) 2009 g10 Code GmbH
       3             :                  2016 Intevation 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, see <https://www.gnu.org/licenses/>.
      19             : */
      20             : 
      21             : /* We need to include config.h so that we know whether we are building
      22             :    with large file system (LFS) support. */
      23             : #ifdef HAVE_CONFIG_H
      24             : #include <config.h>
      25             : #endif
      26             : 
      27             : #include <stdlib.h>
      28             : #include <stdio.h>
      29             : #include <string.h>
      30             : 
      31             : #include <gpgme.h>
      32             : 
      33             : #define PGM "run-decrypt"
      34             : 
      35             : #include "run-support.h"
      36             : 
      37             : 
      38             : static int verbose;
      39             : 
      40             : static gpg_error_t
      41           0 : status_cb (void *opaque, const char *keyword, const char *value)
      42             : {
      43             :   (void)opaque;
      44           0 :   fprintf (stderr, "status_cb: %s %s\n", keyword, value);
      45           0 :   return 0;
      46             : }
      47             : 
      48             : 
      49             : static void
      50           0 : print_result (gpgme_decrypt_result_t result)
      51             : {
      52             :   gpgme_recipient_t recp;
      53           0 :   int count = 0;
      54             : 
      55           0 :   printf ("Original file name: %s\n", nonnull(result->file_name));
      56           0 :   printf ("Wrong key usage: %i\n", result->wrong_key_usage);
      57           0 :   printf ("Unsupported algorithm: %s\n",
      58           0 :           nonnull(result->unsupported_algorithm));
      59           0 :   if (result->session_key)
      60           0 :     printf ("Session key: %s\n", result->session_key);
      61             : 
      62           0 :   for (recp = result->recipients; recp->next; recp = recp->next)
      63             :     {
      64           0 :       printf ("recipient %d\n", count++);
      65           0 :       printf ("  status ....: %s\n", gpgme_strerror (recp->status));
      66           0 :       printf ("  keyid: %s\n", nonnull (recp->keyid));
      67           0 :       printf ("  algo ...: %s\n", gpgme_pubkey_algo_name (recp->pubkey_algo));
      68             :     }
      69           0 : }
      70             : 
      71             : 
      72             : static int
      73           0 : show_usage (int ex)
      74             : {
      75           0 :   fputs ("usage: " PGM " [options] FILE\n\n"
      76             :          "Options:\n"
      77             :          "  --verbose        run in verbose mode\n"
      78             :          "  --status         print status lines from the backend\n"
      79             :          "  --openpgp        use the OpenPGP protocol (default)\n"
      80             :          "  --cms            use the CMS protocol\n"
      81             :          "  --export-session-key            show the session key\n"
      82             :          "  --override-session-key STRING   use STRING as session key\n"
      83             :          , stderr);
      84           0 :   exit (ex);
      85             : }
      86             : 
      87             : 
      88             : int
      89           0 : main (int argc, char **argv)
      90             : {
      91           0 :   int last_argc = -1;
      92             :   gpgme_error_t err;
      93             :   gpgme_ctx_t ctx;
      94           0 :   gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP;
      95           0 :   FILE *fp_in = NULL;
      96           0 :   gpgme_data_t in = NULL;
      97           0 :   gpgme_data_t out = NULL;
      98             :   gpgme_decrypt_result_t result;
      99           0 :   int print_status = 0;
     100           0 :   int export_session_key = 0;
     101           0 :   const char *override_session_key = NULL;
     102             : 
     103           0 :   if (argc)
     104           0 :     { argc--; argv++; }
     105             : 
     106           0 :   while (argc && last_argc != argc )
     107             :     {
     108           0 :       last_argc = argc;
     109           0 :       if (!strcmp (*argv, "--"))
     110             :         {
     111           0 :           argc--; argv++;
     112           0 :           break;
     113             :         }
     114           0 :       else if (!strcmp (*argv, "--help"))
     115           0 :         show_usage (0);
     116           0 :       else if (!strcmp (*argv, "--verbose"))
     117             :         {
     118           0 :           verbose = 1;
     119           0 :           argc--; argv++;
     120             :         }
     121           0 :       else if (!strcmp (*argv, "--status"))
     122             :         {
     123           0 :           print_status = 1;
     124           0 :           argc--; argv++;
     125             :         }
     126           0 :       else if (!strcmp (*argv, "--openpgp"))
     127             :         {
     128           0 :           protocol = GPGME_PROTOCOL_OpenPGP;
     129           0 :           argc--; argv++;
     130             :         }
     131           0 :       else if (!strcmp (*argv, "--cms"))
     132             :         {
     133           0 :           protocol = GPGME_PROTOCOL_CMS;
     134           0 :           argc--; argv++;
     135             :         }
     136           0 :       else if (!strcmp (*argv, "--export-session-key"))
     137             :         {
     138           0 :           export_session_key = 1;
     139           0 :           argc--; argv++;
     140             :         }
     141           0 :       else if (!strcmp (*argv, "--override-session-key"))
     142             :         {
     143           0 :           argc--; argv++;
     144           0 :           if (!argc)
     145           0 :             show_usage (1);
     146           0 :           override_session_key = *argv;
     147           0 :           argc--; argv++;
     148             :         }
     149           0 :       else if (!strncmp (*argv, "--", 2))
     150           0 :         show_usage (1);
     151             : 
     152             :     }
     153             : 
     154           0 :   if (argc < 1 || argc > 2)
     155           0 :     show_usage (1);
     156             : 
     157           0 :   fp_in = fopen (argv[0], "rb");
     158           0 :   if (!fp_in)
     159             :     {
     160           0 :       err = gpgme_error_from_syserror ();
     161           0 :       fprintf (stderr, PGM ": can't open `%s': %s\n",
     162             :                argv[0], gpgme_strerror (err));
     163           0 :       exit (1);
     164             :     }
     165             : 
     166           0 :   init_gpgme (protocol);
     167             : 
     168           0 :   err = gpgme_new (&ctx);
     169           0 :   fail_if_err (err);
     170           0 :   gpgme_set_protocol (ctx, protocol);
     171           0 :   if (print_status)
     172             :     {
     173           0 :       gpgme_set_status_cb (ctx, status_cb, NULL);
     174           0 :       gpgme_set_ctx_flag (ctx, "full-status", "1");
     175             :     }
     176           0 :   if (export_session_key)
     177             :     {
     178           0 :       err = gpgme_set_ctx_flag (ctx, "export-session-key", "1");
     179           0 :       if (err)
     180             :         {
     181           0 :           fprintf (stderr, PGM ": error requesting exported session key: %s\n",
     182             :                    gpgme_strerror (err));
     183           0 :           exit (1);
     184             :         }
     185             :     }
     186           0 :   if (override_session_key)
     187             :     {
     188           0 :       err = gpgme_set_ctx_flag (ctx, "override-session-key",
     189             :                                 override_session_key);
     190           0 :       if (err)
     191             :         {
     192           0 :           fprintf (stderr, PGM ": error overriding session key: %s\n",
     193             :                    gpgme_strerror (err));
     194           0 :           exit (1);
     195             :         }
     196             :     }
     197             : 
     198           0 :   err = gpgme_data_new_from_stream (&in, fp_in);
     199           0 :   if (err)
     200             :     {
     201           0 :       fprintf (stderr, PGM ": error allocating data object: %s\n",
     202             :                gpgme_strerror (err));
     203           0 :       exit (1);
     204             :     }
     205             : 
     206           0 :   err = gpgme_data_new (&out);
     207           0 :   if (err)
     208             :     {
     209           0 :       fprintf (stderr, PGM ": error allocating data object: %s\n",
     210             :                gpgme_strerror (err));
     211           0 :       exit (1);
     212             :     }
     213             : 
     214           0 :   err = gpgme_op_decrypt (ctx, in, out);
     215           0 :   result = gpgme_op_decrypt_result (ctx);
     216           0 :   if (err)
     217             :     {
     218           0 :       fprintf (stderr, PGM ": decrypt failed: %s\n", gpgme_strerror (err));
     219           0 :       exit (1);
     220             :     }
     221           0 :   if (result)
     222             :     {
     223           0 :       print_result (result);
     224           0 :       print_data (out);
     225             :     }
     226             : 
     227           0 :   gpgme_data_release (out);
     228           0 :   gpgme_data_release (in);
     229             : 
     230           0 :   gpgme_release (ctx);
     231           0 :   return 0;
     232             : }

Generated by: LCOV version 1.11