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

          Line data    Source code
       1             : /* run-verify.c  - Helper to perform a verify operation
       2             :    Copyright (C) 2009 g10 Code GmbH
       3             : 
       4             :    This file is part of GPGME.
       5             : 
       6             :    GPGME is free software; you can redistribute it and/or modify it
       7             :    under the terms of the GNU Lesser General Public License as
       8             :    published by the Free Software Foundation; either version 2.1 of
       9             :    the License, or (at your option) any later version.
      10             : 
      11             :    GPGME is distributed in the hope that it will be useful, but
      12             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :    Lesser General Public License for more details.
      15             : 
      16             :    You should have received a copy of the GNU Lesser General Public
      17             :    License along with this program; if not, see <https://www.gnu.org/licenses/>.
      18             : */
      19             : 
      20             : /* We need to include config.h so that we know whether we are building
      21             :    with large file system (LFS) support. */
      22             : #ifdef HAVE_CONFIG_H
      23             : #include <config.h>
      24             : #endif
      25             : 
      26             : #include <stdlib.h>
      27             : #include <stdio.h>
      28             : #include <string.h>
      29             : #include <time.h>
      30             : 
      31             : #include <gpgme.h>
      32             : 
      33             : #define PGM "run-verify"
      34             : 
      35             : #include "run-support.h"
      36             : 
      37             : 
      38             : static int verbose;
      39             : 
      40             : 
      41             : static const char *
      42           0 : isotimestr (unsigned long value)
      43             : {
      44             :   time_t t;
      45             :   static char buffer[25+5];
      46             :   struct tm *tp;
      47             : 
      48           0 :   if (!value)
      49           0 :     return "none";
      50           0 :   t = value;
      51             : 
      52           0 :   tp = gmtime (&t);
      53           0 :   snprintf (buffer, sizeof buffer, "%04d-%02d-%02d %02d:%02d:%02d",
      54           0 :             1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday,
      55             :             tp->tm_hour, tp->tm_min, tp->tm_sec);
      56           0 :   return buffer;
      57             : }
      58             : 
      59             : 
      60             : static gpg_error_t
      61           0 : status_cb (void *opaque, const char *keyword, const char *value)
      62             : {
      63             :   (void)opaque;
      64           0 :   fprintf (stderr, "status_cb: %s %s\n", keyword, value);
      65           0 :   return 0;
      66             : }
      67             : 
      68             : 
      69             : static void
      70           0 : print_summary (gpgme_sigsum_t summary)
      71             : {
      72           0 :   if ( (summary & GPGME_SIGSUM_VALID      ))
      73           0 :     fputs (" valid", stdout);
      74           0 :   if ( (summary & GPGME_SIGSUM_GREEN      ))
      75           0 :     fputs (" green", stdout);
      76           0 :   if ( (summary & GPGME_SIGSUM_RED        ))
      77           0 :     fputs (" red", stdout);
      78           0 :   if ( (summary & GPGME_SIGSUM_KEY_REVOKED))
      79           0 :     fputs (" revoked", stdout);
      80           0 :   if ( (summary & GPGME_SIGSUM_KEY_EXPIRED))
      81           0 :     fputs (" key-expired", stdout);
      82           0 :   if ( (summary & GPGME_SIGSUM_SIG_EXPIRED))
      83           0 :     fputs (" sig-expired", stdout);
      84           0 :   if ( (summary & GPGME_SIGSUM_KEY_MISSING))
      85           0 :     fputs (" key-missing", stdout);
      86           0 :   if ( (summary & GPGME_SIGSUM_CRL_MISSING))
      87           0 :     fputs (" crl-missing", stdout);
      88           0 :   if ( (summary & GPGME_SIGSUM_CRL_TOO_OLD))
      89           0 :     fputs (" crl-too-old", stdout);
      90           0 :   if ( (summary & GPGME_SIGSUM_BAD_POLICY ))
      91           0 :     fputs (" bad-policy", stdout);
      92           0 :   if ( (summary & GPGME_SIGSUM_SYS_ERROR  ))
      93           0 :     fputs (" sys-error", stdout);
      94           0 : }
      95             : 
      96             : static void
      97           0 : print_validity (gpgme_validity_t val)
      98             : {
      99           0 :   const char *s = NULL;
     100             : 
     101           0 :   switch (val)
     102             :     {
     103           0 :     case GPGME_VALIDITY_UNKNOWN:  s = "unknown"; break;
     104           0 :     case GPGME_VALIDITY_UNDEFINED:s = "undefined"; break;
     105           0 :     case GPGME_VALIDITY_NEVER:    s = "never"; break;
     106           0 :     case GPGME_VALIDITY_MARGINAL: s = "marginal"; break;
     107           0 :     case GPGME_VALIDITY_FULL:     s = "full"; break;
     108           0 :     case GPGME_VALIDITY_ULTIMATE: s = "ultimate"; break;
     109             :     }
     110           0 :   if (s)
     111           0 :     fputs (s, stdout);
     112             :   else
     113           0 :     printf ("[bad validity value %u]", (unsigned int)val);
     114           0 : }
     115             : 
     116             : 
     117             : static void
     118           0 : print_description (const char *text, int indent)
     119             : {
     120           0 :   for (; *text; text++)
     121             :     {
     122           0 :       putchar (*text);
     123           0 :       if (*text == '\n')
     124           0 :         printf ("%*s", indent, "");
     125             :     }
     126           0 :   putchar ('\n');
     127           0 : }
     128             : 
     129             : 
     130             : static void
     131           0 : print_result (gpgme_verify_result_t result)
     132             : {
     133             :   gpgme_signature_t sig;
     134             :   gpgme_sig_notation_t nt;
     135             :   gpgme_user_id_t uid;
     136             :   gpgme_tofu_info_t ti;
     137           0 :   int count = 0;
     138             : 
     139           0 :   printf ("Original file name: %s\n", nonnull(result->file_name));
     140           0 :   for (sig = result->signatures; sig; sig = sig->next)
     141             :     {
     142           0 :       printf ("Signature %d\n", count++);
     143           0 :       printf ("  status ....: %s\n", gpgme_strerror (sig->status));
     144           0 :       printf ("  summary ...:"); print_summary (sig->summary); putchar ('\n');
     145           0 :       printf ("  fingerprint: %s\n", nonnull (sig->fpr));
     146           0 :       printf ("  created ...: %lu\n", sig->timestamp);
     147           0 :       printf ("  expires ...: %lu\n", sig->exp_timestamp);
     148           0 :       printf ("  validity ..: ");
     149           0 :       print_validity (sig->validity); putchar ('\n');
     150           0 :       printf ("  val.reason : %s\n", gpgme_strerror (sig->status));
     151           0 :       printf ("  pubkey algo: %d (%s)\n", sig->pubkey_algo,
     152             :               nonnull(gpgme_pubkey_algo_name (sig->pubkey_algo)));
     153           0 :       printf ("  digest algo: %d (%s)\n", sig->hash_algo,
     154             :               nonnull(gpgme_hash_algo_name (sig->hash_algo)));
     155           0 :       printf ("  pka address: %s\n", nonnull (sig->pka_address));
     156           0 :       printf ("  pka trust .: %s\n",
     157           0 :               sig->pka_trust == 0? "n/a" :
     158           0 :               sig->pka_trust == 1? "bad" :
     159           0 :               sig->pka_trust == 2? "okay": "RFU");
     160           0 :       printf ("  other flags:%s%s\n",
     161           0 :               sig->wrong_key_usage? " wrong-key-usage":"",
     162           0 :               sig->chain_model? " chain-model":""
     163             :               );
     164           0 :       for (nt = sig->notations; nt; nt = nt->next)
     165             :         {
     166           0 :           printf ("  notation ..: '%s'\n", nt->name);
     167           0 :           if (strlen (nt->name) != nt->name_len)
     168           0 :             printf ("    warning : name larger (%d)\n", nt->name_len);
     169           0 :           printf ("    flags ...:%s%s (0x%02x)\n",
     170           0 :                   nt->critical? " critical":"",
     171           0 :                   nt->human_readable? " human":"",
     172             :                   nt->flags);
     173           0 :           if (nt->value)
     174           0 :             printf ("    value ...: '%s'\n", nt->value);
     175           0 :           if ((nt->value?strlen (nt->value):0) != nt->value_len)
     176           0 :             printf ("    warning : value larger (%d)\n", nt->value_len);
     177             :         }
     178           0 :       if (sig->key)
     179             :         {
     180           0 :           printf ("  primary fpr: %s\n", nonnull (sig->key->fpr));
     181           0 :           for (uid = sig->key->uids; uid; uid = uid->next)
     182             :             {
     183           0 :               printf ("  tofu addr .: %s\n", nonnull (uid->address));
     184           0 :               ti = uid->tofu;
     185           0 :               if (!ti)
     186           0 :                 continue;
     187           0 :               printf ("    validity : %u (%s)\n", ti->validity,
     188           0 :                       ti->validity == 0? "conflict" :
     189           0 :                       ti->validity == 1? "no history" :
     190           0 :                       ti->validity == 2? "little history" :
     191           0 :                       ti->validity == 3? "enough history" :
     192           0 :                       ti->validity == 4? "lot of history" : "?");
     193           0 :               printf ("    policy ..: %u (%s)\n", ti->policy,
     194           0 :                       ti->policy == GPGME_TOFU_POLICY_NONE? "none" :
     195           0 :                       ti->policy == GPGME_TOFU_POLICY_AUTO? "auto" :
     196           0 :                       ti->policy == GPGME_TOFU_POLICY_GOOD? "good" :
     197           0 :                       ti->policy == GPGME_TOFU_POLICY_UNKNOWN? "unknown" :
     198           0 :                       ti->policy == GPGME_TOFU_POLICY_BAD? "bad" :
     199           0 :                       ti->policy == GPGME_TOFU_POLICY_ASK? "ask" : "?");
     200           0 :               printf ("    signcount: %hu\n", ti->signcount);
     201           0 :               printf ("      first..: %s\n", isotimestr (ti->signfirst));
     202           0 :               printf ("      last ..: %s\n", isotimestr (ti->signlast));
     203           0 :               printf ("    encrcount: %hu\n", ti->encrcount);
     204           0 :               printf ("      first..: %s\n", isotimestr (ti->encrfirst));
     205           0 :               printf ("      last ..: %s\n", isotimestr (ti->encrlast));
     206           0 :               printf ("    desc ....: ");
     207           0 :               print_description (nonnull (ti->description), 15);
     208             :             }
     209             :         }
     210             :     }
     211           0 : }
     212             : 
     213             : 
     214             : 
     215             : static int
     216           0 : show_usage (int ex)
     217             : {
     218           0 :   fputs ("usage: " PGM " [options] [DETACHEDSIGFILE] FILE\n\n"
     219             :          "Options:\n"
     220             :          "  --verbose        run in verbose mode\n"
     221             :          "  --status         print status lines from the backend\n"
     222             :          "  --openpgp        use the OpenPGP protocol (default)\n"
     223             :          "  --cms            use the CMS protocol\n"
     224             :          "  --sender MBOX    use MBOX as sender address\n"
     225             :          , stderr);
     226           0 :   exit (ex);
     227             : }
     228             : 
     229             : 
     230             : int
     231           0 : main (int argc, char **argv)
     232             : {
     233           0 :   int last_argc = -1;
     234             :   gpgme_error_t err;
     235             :   gpgme_ctx_t ctx;
     236           0 :   gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP;
     237           0 :   FILE *fp_sig = NULL;
     238           0 :   gpgme_data_t sig = NULL;
     239           0 :   FILE *fp_msg = NULL;
     240           0 :   gpgme_data_t msg = NULL;
     241             :   gpgme_verify_result_t result;
     242           0 :   int print_status = 0;
     243           0 :   const char *sender = NULL;
     244             : 
     245           0 :   if (argc)
     246           0 :     { argc--; argv++; }
     247             : 
     248           0 :   while (argc && last_argc != argc )
     249             :     {
     250           0 :       last_argc = argc;
     251           0 :       if (!strcmp (*argv, "--"))
     252             :         {
     253           0 :           argc--; argv++;
     254           0 :           break;
     255             :         }
     256           0 :       else if (!strcmp (*argv, "--help"))
     257           0 :         show_usage (0);
     258           0 :       else if (!strcmp (*argv, "--verbose"))
     259             :         {
     260           0 :           verbose = 1;
     261           0 :           argc--; argv++;
     262             :         }
     263           0 :       else if (!strcmp (*argv, "--status"))
     264             :         {
     265           0 :           print_status = 1;
     266           0 :           argc--; argv++;
     267             :         }
     268           0 :       else if (!strcmp (*argv, "--openpgp"))
     269             :         {
     270           0 :           protocol = GPGME_PROTOCOL_OpenPGP;
     271           0 :           argc--; argv++;
     272             :         }
     273           0 :       else if (!strcmp (*argv, "--cms"))
     274             :         {
     275           0 :           protocol = GPGME_PROTOCOL_CMS;
     276           0 :           argc--; argv++;
     277             :         }
     278           0 :       else if (!strcmp (*argv, "--sender"))
     279             :         {
     280           0 :           argc--; argv++;
     281           0 :           if (!argc)
     282           0 :             show_usage (1);
     283           0 :           sender = *argv;
     284           0 :           argc--; argv++;
     285             :         }
     286           0 :       else if (!strncmp (*argv, "--", 2))
     287           0 :         show_usage (1);
     288             : 
     289             :     }
     290             : 
     291           0 :   if (argc < 1 || argc > 2)
     292           0 :     show_usage (1);
     293             : 
     294           0 :   fp_sig = fopen (argv[0], "rb");
     295           0 :   if (!fp_sig)
     296             :     {
     297           0 :       err = gpgme_error_from_syserror ();
     298           0 :       fprintf (stderr, PGM ": can't open `%s': %s\n",
     299             :                argv[0], gpgme_strerror (err));
     300           0 :       exit (1);
     301             :     }
     302           0 :   if (argc > 1)
     303             :     {
     304           0 :       fp_msg = fopen (argv[1], "rb");
     305           0 :       if (!fp_msg)
     306             :         {
     307           0 :           err = gpgme_error_from_syserror ();
     308           0 :           fprintf (stderr, PGM ": can't open `%s': %s\n",
     309           0 :                    argv[1], gpgme_strerror (err));
     310           0 :           exit (1);
     311             :         }
     312             :     }
     313             : 
     314           0 :   init_gpgme (protocol);
     315             : 
     316           0 :   err = gpgme_new (&ctx);
     317           0 :   fail_if_err (err);
     318           0 :   gpgme_set_protocol (ctx, protocol);
     319           0 :   if (print_status)
     320             :     {
     321           0 :       gpgme_set_status_cb (ctx, status_cb, NULL);
     322           0 :       gpgme_set_ctx_flag (ctx, "full-status", "1");
     323             :     }
     324             :   /* gpgme_set_ctx_flag (ctx, "raw-description", "1"); */
     325             : 
     326           0 :   if (sender)
     327             :     {
     328           0 :       err = gpgme_set_sender (ctx, sender);
     329           0 :       fail_if_err (err);
     330             :     }
     331             : 
     332           0 :   err = gpgme_data_new_from_stream (&sig, fp_sig);
     333           0 :   if (err)
     334             :     {
     335           0 :       fprintf (stderr, PGM ": error allocating data object: %s\n",
     336             :                gpgme_strerror (err));
     337           0 :       exit (1);
     338             :     }
     339           0 :   if (fp_msg)
     340             :     {
     341           0 :       err = gpgme_data_new_from_stream (&msg, fp_msg);
     342           0 :       if (err)
     343             :         {
     344           0 :           fprintf (stderr, PGM ": error allocating data object: %s\n",
     345             :                    gpgme_strerror (err));
     346           0 :           exit (1);
     347             :         }
     348             :     }
     349             : 
     350           0 :   err = gpgme_op_verify (ctx, sig, msg, NULL);
     351           0 :   result = gpgme_op_verify_result (ctx);
     352           0 :   if (result)
     353           0 :     print_result (result);
     354           0 :   if (err)
     355             :     {
     356           0 :       fprintf (stderr, PGM ": verify failed: %s\n", gpgme_strerror (err));
     357           0 :       exit (1);
     358             :     }
     359             : 
     360           0 :   gpgme_data_release (msg);
     361           0 :   gpgme_data_release (sig);
     362             : 
     363           0 :   gpgme_release (ctx);
     364           0 :   return 0;
     365             : }

Generated by: LCOV version 1.11