Bump to 2.4.3
[platform/upstream/gpg2.git] / g10 / gpgv.c
1 /* gpgv.c - The GnuPG signature verify utility
2  * Copyright (C) 1998-2020 Free Software Foundation, Inc.
3  * Copyright (C) 1997-2019 Werner Koch
4  * Copyright (C) 2015-2020 g10 Code GmbH
5  *
6  * This file is part of GnuPG.
7  *
8  * GnuPG is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * GnuPG is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, see <https://www.gnu.org/licenses/>.
20  * SPDX-License-Identifier: GPL-3.0-or-later
21  */
22
23 #include <config.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <unistd.h>
30 #ifdef HAVE_DOSISH_SYSTEM
31 #include <fcntl.h> /* for setmode() */
32 #endif
33 #ifdef HAVE_LIBREADLINE
34 #define GNUPG_LIBREADLINE_H_INCLUDED
35 #include <readline/readline.h>
36 #endif
37
38 #define INCLUDED_BY_MAIN_MODULE 1
39 #include "gpg.h"
40 #include "../common/util.h"
41 #include "packet.h"
42 #include "../common/iobuf.h"
43 #include "main.h"
44 #include "options.h"
45 #include "keydb.h"
46 #include "trustdb.h"
47 #include "filter.h"
48 #include "../common/ttyio.h"
49 #include "../common/i18n.h"
50 #include "../common/sysutils.h"
51 #include "../common/status.h"
52 #include "call-agent.h"
53 #include "../common/init.h"
54
55
56 enum cmd_and_opt_values {
57   aNull = 0,
58   oQuiet          = 'q',
59   oVerbose        = 'v',
60   oOutput         = 'o',
61   oBatch          = 500,
62   oKeyring,
63   oIgnoreTimeConflict,
64   oStatusFD,
65   oLoggerFD,
66   oLoggerFile,
67   oHomedir,
68   oWeakDigest,
69   oEnableSpecialFilenames,
70   oDebug,
71   aTest
72 };
73
74
75 static gpgrt_opt_t opts[] = {
76   ARGPARSE_group (300, N_("@\nOptions:\n ")),
77
78   ARGPARSE_s_n (oVerbose, "verbose", N_("verbose")),
79   ARGPARSE_s_n (oQuiet,   "quiet",   N_("be somewhat more quiet")),
80   ARGPARSE_s_s (oKeyring, "keyring",
81                 N_("|FILE|take the keys from the keyring FILE")),
82   ARGPARSE_s_s (oOutput, "output", N_("|FILE|write output to FILE")),
83   ARGPARSE_s_n (oIgnoreTimeConflict, "ignore-time-conflict",
84                 N_("make timestamp conflicts only a warning")),
85   ARGPARSE_s_i (oStatusFD, "status-fd",
86                 N_("|FD|write status info to this FD")),
87   ARGPARSE_s_i (oLoggerFD, "logger-fd", "@"),
88   ARGPARSE_s_s (oLoggerFile, "log-file", "@"),
89   ARGPARSE_s_s (oHomedir, "homedir", "@"),
90   ARGPARSE_s_s (oWeakDigest, "weak-digest",
91                 N_("|ALGO|reject signatures made with ALGO")),
92   ARGPARSE_s_n (oEnableSpecialFilenames, "enable-special-filenames", "@"),
93   ARGPARSE_s_s (oDebug, "debug", "@"),
94
95   ARGPARSE_end ()
96 };
97
98
99 /* The list of supported debug flags.  */
100 static struct debug_flags_s debug_flags [] =
101   {
102     { DBG_PACKET_VALUE , "packet"  },
103     { DBG_MPI_VALUE    , "mpi"     },
104     { DBG_CRYPTO_VALUE , "crypto"  },
105     { DBG_FILTER_VALUE , "filter"  },
106     { DBG_IOBUF_VALUE  , "iobuf"   },
107     { DBG_MEMORY_VALUE , "memory"  },
108     { DBG_CACHE_VALUE  , "cache"   },
109     { DBG_MEMSTAT_VALUE, "memstat" },
110     { DBG_TRUST_VALUE  , "trust"   },
111     { DBG_HASHING_VALUE, "hashing" },
112     { DBG_IPC_VALUE    , "ipc"     },
113     { DBG_CLOCK_VALUE  , "clock"   },
114     { DBG_LOOKUP_VALUE , "lookup"  },
115     { DBG_EXTPROG_VALUE, "extprog" },
116     { 0, NULL }
117   };
118
119
120 int g10_errors_seen = 0;
121 int assert_signer_true = 0;
122
123 static char *
124 make_libversion (const char *libname, const char *(*getfnc)(const char*))
125 {
126   const char *s;
127   char *result;
128
129   s = getfnc (NULL);
130   result = xmalloc (strlen (libname) + 1 + strlen (s) + 1);
131   strcpy (stpcpy (stpcpy (result, libname), " "), s);
132   return result;
133 }
134
135 static const char *
136 my_strusage( int level )
137 {
138   static char *ver_gcry;
139   const char *p;
140
141   switch (level)
142     {
143     case  9: p = "GPL-3.0-or-later"; break;
144     case 11: p = "@GPG@v (GnuPG)";
145       break;
146     case 13: p = VERSION; break;
147     case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break;
148     case 17: p = PRINTABLE_OS_NAME; break;
149     case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break;
150
151     case 1:
152     case 40: p = _("Usage: gpgv [options] [files] (-h for help)");
153       break;
154     case 41: p = _("Syntax: gpgv [options] [files]\n"
155                    "Check signatures against known trusted keys\n");
156         break;
157
158     case 20:
159       if (!ver_gcry)
160         ver_gcry = make_libversion ("libgcrypt", gcry_check_version);
161       p = ver_gcry;
162       break;
163
164
165     default: p = NULL;
166     }
167   return p;
168 }
169
170
171
172 int
173 main( int argc, char **argv )
174 {
175   gpgrt_argparse_t pargs;
176   int rc=0;
177   strlist_t sl;
178   strlist_t nrings = NULL;
179   ctrl_t ctrl;
180
181   early_system_init ();
182   gpgrt_set_strusage (my_strusage);
183   log_set_prefix ("gpgv", GPGRT_LOG_WITH_PREFIX);
184
185   /* Make sure that our subsystems are ready.  */
186   i18n_init();
187   init_common_subsystems (&argc, &argv);
188
189   gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
190
191   gnupg_init_signals (0, NULL);
192
193   opt.command_fd = -1; /* no command fd */
194   opt.keyserver_options.options |= KEYSERVER_AUTO_KEY_RETRIEVE;
195   opt.trust_model = TM_ALWAYS;
196   opt.no_sig_cache = 1;
197   opt.flags.require_cross_cert = 1;
198   opt.batch = 1;
199   opt.answer_yes = 1;
200
201   opt.weak_digests = NULL;
202
203   tty_no_terminal(1);
204   tty_batchmode(1);
205   dotlock_disable ();
206   gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
207   additional_weak_digest("MD5");
208   gnupg_initialize_compliance (GNUPG_MODULE_NAME_GPG);
209
210   pargs.argc = &argc;
211   pargs.argv = &argv;
212   pargs.flags= ARGPARSE_FLAG_KEEP;
213   while (gpgrt_argparser (&pargs, opts, NULL))
214     {
215       switch (pargs.r_opt)
216         {
217         case ARGPARSE_CONFFILE: break;
218
219         case oQuiet: opt.quiet = 1; break;
220         case oVerbose:
221           opt.verbose++;
222           opt.list_sigs=1;
223           gcry_control (GCRYCTL_SET_VERBOSITY, (int)opt.verbose);
224           break;
225         case oDebug:
226           if (parse_debug_flag (pargs.r.ret_str, &opt.debug, debug_flags))
227             {
228               pargs.r_opt = ARGPARSE_INVALID_ARG;
229               pargs.err = ARGPARSE_PRINT_ERROR;
230             }
231           break;
232         case oKeyring: append_to_strlist( &nrings, pargs.r.ret_str); break;
233         case oOutput: opt.outfile = pargs.r.ret_str; break;
234         case oStatusFD:
235           set_status_fd (translate_sys2libc_fd_int (pargs.r.ret_int, 1));
236           break;
237         case oLoggerFD:
238           log_set_fd (translate_sys2libc_fd_int (pargs.r.ret_int, 1));
239           break;
240         case oLoggerFile:
241           log_set_file (pargs.r.ret_str);
242           log_set_prefix (NULL, (GPGRT_LOG_WITH_PREFIX
243                                  | GPGRT_LOG_WITH_TIME
244                                  | GPGRT_LOG_WITH_PID) );
245           break;
246         case oHomedir: gnupg_set_homedir (pargs.r.ret_str); break;
247         case oWeakDigest:
248           additional_weak_digest(pargs.r.ret_str);
249           break;
250         case oIgnoreTimeConflict: opt.ignore_time_conflict = 1; break;
251         case oEnableSpecialFilenames:
252           enable_special_filenames ();
253           break;
254         default : pargs.err = ARGPARSE_PRINT_ERROR; break;
255         }
256     }
257
258   gpgrt_argparse (NULL, &pargs, NULL);  /* Release internal state.  */
259
260   if (log_get_errorcount (0))
261     g10_exit(2);
262
263   if (opt.verbose > 1)
264     set_packet_list_mode(1);
265
266   /* Note: We open all keyrings in read-only mode.  */
267   if (!nrings)  /* No keyring given: use default one. */
268     keydb_add_resource ("trustedkeys" EXTSEP_S "kbx",
269                         (KEYDB_RESOURCE_FLAG_READONLY
270                          |KEYDB_RESOURCE_FLAG_GPGVDEF));
271   for (sl = nrings; sl; sl = sl->next)
272     keydb_add_resource (sl->d, KEYDB_RESOURCE_FLAG_READONLY);
273
274   FREE_STRLIST (nrings);
275
276   ctrl = xcalloc (1, sizeof *ctrl);
277
278   if ((rc = verify_signatures (ctrl, argc, argv)))
279     log_error("verify signatures failed: %s\n", gpg_strerror (rc) );
280
281   keydb_release (ctrl->cached_getkey_kdb);
282   xfree (ctrl);
283
284   /* cleanup */
285   g10_exit (0);
286   return 8; /*NOTREACHED*/
287 }
288
289
290 void
291 g10_exit( int rc )
292 {
293   rc = rc? rc : log_get_errorcount(0)? 2 : g10_errors_seen? 1 : 0;
294   exit(rc );
295 }
296
297
298 /* Stub:
299  * We have to override the trustcheck from pkclist.c because
300  * this utility assumes that all keys in the keyring are trustworthy
301  */
302 gpg_error_t
303 check_signatures_trust (ctrl_t ctrl, kbnode_t kblock,
304                         PKT_public_key *pk, PKT_signature *sig)
305 {
306   (void)ctrl;
307   (void)kblock;
308   (void)pk;
309   (void)sig;
310   return 0;
311 }
312
313 void
314 read_trust_options (ctrl_t ctrl,
315                     byte *trust_model, ulong *created, ulong *nextcheck,
316                     byte *marginals, byte *completes, byte *cert_depth,
317                     byte *min_cert_level)
318 {
319   (void)ctrl;
320   (void)trust_model;
321   (void)created;
322   (void)nextcheck;
323   (void)marginals;
324   (void)completes;
325   (void)cert_depth;
326   (void)min_cert_level;
327 }
328
329 /* Stub:
330  * We don't have the trustdb , so we have to provide some stub functions
331  * instead
332  */
333
334 int
335 cache_disabled_value (ctrl_t ctrl, PKT_public_key *pk)
336 {
337   (void)ctrl;
338   (void)pk;
339   return 0;
340 }
341
342 void
343 check_trustdb_stale (ctrl_t ctrl)
344 {
345   (void)ctrl;
346 }
347
348 int
349 get_validity_info (ctrl_t ctrl, kbnode_t kb, PKT_public_key *pk,
350                    PKT_user_id *uid)
351 {
352   (void)ctrl;
353   (void)kb;
354   (void)pk;
355   (void)uid;
356   return '?';
357 }
358
359 unsigned int
360 get_validity (ctrl_t ctrl, kbnode_t kb, PKT_public_key *pk, PKT_user_id *uid,
361               PKT_signature *sig, int may_ask)
362 {
363   (void)ctrl;
364   (void)kb;
365   (void)pk;
366   (void)uid;
367   (void)sig;
368   (void)may_ask;
369   return 0;
370 }
371
372 const char *
373 trust_value_to_string (unsigned int value)
374 {
375   (void)value;
376   return "err";
377 }
378
379 const char *
380 uid_trust_string_fixed (ctrl_t ctrl, PKT_public_key *key, PKT_user_id *uid)
381 {
382   (void)ctrl;
383   (void)key;
384   (void)uid;
385   return "err";
386 }
387
388 int
389 get_ownertrust_info (ctrl_t ctrl, PKT_public_key *pk, int no_create)
390 {
391   (void)ctrl;
392   (void)pk;
393   (void)no_create;
394   return '?';
395 }
396
397 unsigned int
398 get_ownertrust (ctrl_t ctrl, PKT_public_key *pk)
399 {
400   (void)ctrl;
401   (void)pk;
402   return TRUST_UNKNOWN;
403 }
404
405
406 /* Stubs:
407  * Because we only work with trusted keys, it does not make sense to
408  * get them from a keyserver
409  */
410
411 struct keyserver_spec *
412 keyserver_match (struct keyserver_spec *spec)
413 {
414   (void)spec;
415   return NULL;
416 }
417
418 int
419 keyserver_any_configured (ctrl_t ctrl)
420 {
421   (void)ctrl;
422   return 0;
423 }
424
425 int
426 keyserver_import_keyid (u32 *keyid, void *dummy, unsigned int flags)
427 {
428   (void)keyid;
429   (void)dummy;
430   (void)flags;
431   return -1;
432 }
433
434 int
435 keyserver_import_fprint (ctrl_t ctrl, const byte *fprint,size_t fprint_len,
436                          struct keyserver_spec *keyserver, unsigned int flags)
437 {
438   (void)ctrl;
439   (void)fprint;
440   (void)fprint_len;
441   (void)keyserver;
442   (void)flags;
443   return -1;
444 }
445
446 int
447 keyserver_import_fprint_ntds (ctrl_t ctrl,
448                               const byte *fprint, size_t fprint_len)
449 {
450   (void)ctrl;
451   (void)fprint;
452   (void)fprint_len;
453   return -1;
454 }
455
456 int
457 keyserver_import_cert (const char *name)
458 {
459   (void)name;
460   return -1;
461 }
462
463 gpg_error_t
464 keyserver_import_wkd (ctrl_t ctrl, const char *name, unsigned int flags,
465                       unsigned char **fpr, size_t *fpr_len)
466 {
467   (void)ctrl;
468   (void)name;
469   (void)flags;
470   (void)fpr;
471   (void)fpr_len;
472   return GPG_ERR_BUG;
473 }
474
475 int
476 keyserver_import_mbox (const char *name,struct keyserver_spec *spec)
477 {
478   (void)name;
479   (void)spec;
480   return -1;
481 }
482
483 int
484 keyserver_import_ntds (ctrl_t ctrl, const char *mbox,
485                        unsigned char **fpr, size_t *fprlen)
486 {
487   (void)ctrl;
488   (void)mbox;
489   (void)fpr;
490   (void)fprlen;
491   return -1;
492 }
493
494 int
495 keyserver_import_ldap (const char *name)
496 {
497   (void)name;
498   return -1;
499 }
500
501
502 gpg_error_t
503 read_key_from_file_or_buffer (ctrl_t ctrl, const char *fname,
504                               const void *buffer, size_t buflen,
505                               kbnode_t *r_keyblock)
506 {
507   (void)ctrl;
508   (void)fname;
509   (void)buffer;
510   (void)buflen;
511   (void)r_keyblock;
512   return -1;
513 }
514
515 gpg_error_t
516 import_included_key_block (ctrl_t ctrl, kbnode_t keyblock)
517 {
518   (void)ctrl;
519   (void)keyblock;
520   return -1;
521 }
522
523
524 /* Stub:
525  * No encryption here but mainproc links to these functions.
526  */
527 gpg_error_t
528 get_session_key (ctrl_t ctrl, struct pubkey_enc_list *k, DEK *dek)
529 {
530   (void)ctrl;
531   (void)k;
532   (void)dek;
533   return GPG_ERR_GENERAL;
534 }
535
536 /* Stub: */
537 gpg_error_t
538 get_override_session_key (DEK *dek, const char *string)
539 {
540   (void)dek;
541   (void)string;
542   return GPG_ERR_GENERAL;
543 }
544
545 /* Stub: */
546 int
547 decrypt_data (ctrl_t ctrl, void *procctx, PKT_encrypted *ed, DEK *dek,
548               int *compliance_error)
549 {
550   (void)ctrl;
551   (void)procctx;
552   (void)ed;
553   (void)dek;
554   (void)compliance_error;
555   return GPG_ERR_GENERAL;
556 }
557
558
559 /* Stub:
560  * No interactive commands, so we don't need the helptexts
561  */
562 void
563 display_online_help (const char *keyword)
564 {
565   (void)keyword;
566 }
567
568 /* Stub:
569  * We don't use secret keys, but getkey.c links to this
570  */
571 int
572 check_secret_key (PKT_public_key *pk, int n)
573 {
574   (void)pk;
575   (void)n;
576   return GPG_ERR_GENERAL;
577 }
578
579 /* Stub:
580  * No secret key, so no passphrase needed
581  */
582 DEK *
583 passphrase_to_dek (int cipher_algo, STRING2KEY *s2k, int create, int nocache,
584                    const char *tmp, unsigned int flags, int *canceled)
585 {
586   (void)cipher_algo;
587   (void)s2k;
588   (void)create;
589   (void)nocache;
590   (void)tmp;
591   (void)flags;
592
593   if (canceled)
594     *canceled = 0;
595   return NULL;
596 }
597
598 void
599 passphrase_clear_cache (const char *cacheid)
600 {
601   (void)cacheid;
602 }
603
604 struct keyserver_spec *
605 parse_preferred_keyserver(PKT_signature *sig)
606 {
607   (void)sig;
608   return NULL;
609 }
610
611 struct keyserver_spec *
612 parse_keyserver_uri (const char *uri, int require_scheme,
613                      const char *configname, unsigned int configlineno)
614 {
615   (void)uri;
616   (void)require_scheme;
617   (void)configname;
618   (void)configlineno;
619   return NULL;
620 }
621
622 void
623 free_keyserver_spec (struct keyserver_spec *keyserver)
624 {
625   (void)keyserver;
626 }
627
628 /* Stubs to avoid linking to photoid.c */
629 void
630 show_photos (const struct user_attribute *attrs, int count, PKT_public_key *pk)
631 {
632   (void)attrs;
633   (void)count;
634   (void)pk;
635 }
636
637 int
638 parse_image_header (const struct user_attribute *attr, byte *type, u32 *len)
639 {
640   (void)attr;
641   (void)type;
642   (void)len;
643   return 0;
644 }
645
646 char *
647 image_type_to_string (byte type, int string)
648 {
649   (void)type;
650   (void)string;
651   return NULL;
652 }
653
654 #ifdef ENABLE_CARD_SUPPORT
655 int
656 agent_scd_getattr (const char *name, struct agent_card_info_s *info)
657 {
658   (void)name;
659   (void)info;
660   return 0;
661 }
662 #endif /* ENABLE_CARD_SUPPORT */
663
664 /* We do not do any locking, so use these stubs here */
665 void
666 dotlock_disable (void)
667 {
668 }
669
670 dotlock_t
671 dotlock_create (const char *file_to_lock, unsigned int flags)
672 {
673   (void)file_to_lock;
674   (void)flags;
675   return NULL;
676 }
677
678 void
679 dotlock_destroy (dotlock_t h)
680 {
681   (void)h;
682 }
683
684 int
685 dotlock_take (dotlock_t h, long timeout)
686 {
687   (void)h;
688   (void)timeout;
689   return 0;
690 }
691
692 int
693 dotlock_release (dotlock_t h)
694 {
695   (void)h;
696   return 0;
697 }
698
699 void
700 dotlock_remove_lockfiles (void)
701 {
702 }
703
704 int
705 agent_probe_secret_key (ctrl_t ctrl, PKT_public_key *pk)
706 {
707   (void)ctrl;
708   (void)pk;
709   return 0;
710 }
711
712 gpg_error_t
713 agent_probe_any_secret_key (ctrl_t ctrl, kbnode_t keyblock)
714 {
715   (void)ctrl;
716   (void)keyblock;
717   return gpg_error (GPG_ERR_NO_SECKEY);
718 }
719
720 gpg_error_t
721 agent_get_keyinfo (ctrl_t ctrl, const char *hexkeygrip,
722                    char **r_serialno, int *r_cleartext)
723 {
724   (void)ctrl;
725   (void)hexkeygrip;
726   (void)r_cleartext;
727   *r_serialno = NULL;
728   return gpg_error (GPG_ERR_NO_SECKEY);
729 }
730
731 gpg_error_t
732 export_pubkey_buffer (ctrl_t ctrl, const char *keyspec, unsigned int options,
733                       const void *prefix, size_t prefixlen,
734                       export_stats_t stats,
735                       kbnode_t *r_keyblock, void **r_data, size_t *r_datalen)
736 {
737   (void)ctrl;
738   (void)keyspec;
739   (void)options;
740   (void)prefix;
741   (void)prefixlen;
742   (void)stats;
743
744   *r_keyblock = NULL;
745   *r_data = NULL;
746   *r_datalen = 0;
747   return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
748 }
749
750 gpg_error_t
751 tofu_write_tfs_record (ctrl_t ctrl, estream_t fp,
752                        PKT_public_key *pk, const char *user_id)
753 {
754   (void)ctrl;
755   (void)fp;
756   (void)pk;
757   (void)user_id;
758   return gpg_error (GPG_ERR_GENERAL);
759 }
760
761 gpg_error_t
762 tofu_get_policy (ctrl_t ctrl, PKT_public_key *pk, PKT_user_id *user_id,
763                  enum tofu_policy *policy)
764 {
765   (void)ctrl;
766   (void)pk;
767   (void)user_id;
768   (void)policy;
769   return gpg_error (GPG_ERR_GENERAL);
770 }
771
772 const char *
773 tofu_policy_str (enum tofu_policy policy)
774 {
775   (void)policy;
776
777   return "unknown";
778 }
779
780 void
781 tofu_begin_batch_update (ctrl_t ctrl)
782 {
783   (void)ctrl;
784 }
785
786 void
787 tofu_end_batch_update (ctrl_t ctrl)
788 {
789   (void)ctrl;
790 }
791
792 gpg_error_t
793 tofu_notice_key_changed (ctrl_t ctrl, kbnode_t kb)
794 {
795   (void) ctrl;
796   (void) kb;
797
798   return 0;
799 }
800
801
802 int
803 get_revocation_reason (PKT_signature *sig, char **r_reason,
804                        char **r_comment, size_t *r_commentlen)
805 {
806   (void)sig;
807   (void)r_commentlen;
808
809   if (r_reason)
810     *r_reason = NULL;
811   if (r_comment)
812     *r_comment = NULL;
813   return 0;
814 }
815
816 const char *
817 impex_filter_getval (void *cookie, const char *propname)
818 {
819   (void)cookie;
820   (void)propname;
821   return NULL;
822 }