Blame tests/run-sign.c

Packit Service 672cf4
/* run-sign.c  - Helper to perform a sign operation
Packit Service 0ef63b
 * Copyright (C) 2009 g10 Code GmbH
Packit Service 0ef63b
 *
Packit Service 0ef63b
 * This file is part of GPGME.
Packit Service 0ef63b
 *
Packit Service 0ef63b
 * GPGME is free software; you can redistribute it and/or modify it
Packit Service 0ef63b
 * under the terms of the GNU Lesser General Public License as
Packit Service 0ef63b
 * published by the Free Software Foundation; either version 2.1 of
Packit Service 0ef63b
 * the License, or (at your option) any later version.
Packit Service 0ef63b
 *
Packit Service 0ef63b
 * GPGME is distributed in the hope that it will be useful, but
Packit Service 0ef63b
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 0ef63b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 0ef63b
 * Lesser General Public License for more details.
Packit Service 0ef63b
 *
Packit Service 0ef63b
 * You should have received a copy of the GNU Lesser General Public
Packit Service 0ef63b
 * License along with this program; if not, see <https://gnu.org/licenses/>.
Packit Service 0ef63b
 * SPDX-License-Identifier: LGPL-2.1-or-later
Packit Service 0ef63b
 */
Packit Service 672cf4
Packit Service 672cf4
/* We need to include config.h so that we know whether we are building
Packit Service 672cf4
   with large file system (LFS) support. */
Packit Service 672cf4
#ifdef HAVE_CONFIG_H
Packit Service 672cf4
#include <config.h>
Packit Service 672cf4
#endif
Packit Service 672cf4
Packit Service 672cf4
#include <stdlib.h>
Packit Service 672cf4
#include <stdio.h>
Packit Service 672cf4
#include <string.h>
Packit Service 672cf4
Packit Service 672cf4
#include <gpgme.h>
Packit Service 672cf4
Packit Service 672cf4
#define PGM "run-sign"
Packit Service 672cf4
Packit Service 672cf4
#include "run-support.h"
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
static int verbose;
Packit Service 672cf4
Packit Service 672cf4
static gpg_error_t
Packit Service 672cf4
status_cb (void *opaque, const char *keyword, const char *value)
Packit Service 672cf4
{
Packit Service 672cf4
  (void)opaque;
Packit Service 672cf4
  printf ("status_cb: %s %s\n", keyword, value);
Packit Service 672cf4
  return 0;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
static void
Packit Service 672cf4
print_result (gpgme_sign_result_t result, gpgme_sig_mode_t type)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_invalid_key_t invkey;
Packit Service 672cf4
  gpgme_new_signature_t sig;
Packit Service 672cf4
Packit Service 672cf4
  (void)type;
Packit Service 672cf4
Packit Service 672cf4
  for (invkey = result->invalid_signers; invkey; invkey = invkey->next)
Packit Service 672cf4
    printf ("Signing key `%s' not used: %s <%s>\n",
Packit Service 672cf4
            nonnull (invkey->fpr),
Packit Service 672cf4
            gpg_strerror (invkey->reason), gpg_strsource (invkey->reason));
Packit Service 672cf4
Packit Service 672cf4
  for (sig = result->signatures; sig; sig = sig->next)
Packit Service 672cf4
    {
Packit Service 672cf4
      printf ("Key fingerprint: %s\n", nonnull (sig->fpr));
Packit Service 672cf4
      printf ("Signature type : %d\n", sig->type);
Packit Service 672cf4
      printf ("Public key algo: %d\n", sig->pubkey_algo);
Packit Service 672cf4
      printf ("Hash algo .....: %d\n", sig->hash_algo);
Packit Service 672cf4
      printf ("Creation time .: %ld\n", sig->timestamp);
Packit Service 672cf4
      printf ("Sig class .....: 0x%u\n", sig->sig_class);
Packit Service 672cf4
    }
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
static int
Packit Service 672cf4
show_usage (int ex)
Packit Service 672cf4
{
Packit Service 672cf4
  fputs ("usage: " PGM " [options] FILE\n\n"
Packit Service 672cf4
         "Options:\n"
Packit Service 672cf4
         "  --verbose        run in verbose mode\n"
Packit Service 672cf4
         "  --status         print status lines from the backend\n"
Packit Service 672cf4
         "  --openpgp        use the OpenPGP protocol (default)\n"
Packit Service 672cf4
         "  --cms            use the CMS protocol\n"
Packit Service 672cf4
         "  --uiserver       use the UI server\n"
Packit Service 672cf4
         "  --loopback       use a loopback pinentry\n"
Packit Service 672cf4
         "  --key NAME       use key NAME for signing\n"
Packit Service 672cf4
         "  --sender MBOX    use MBOX as sender address\n"
Packit Service 672cf4
         , stderr);
Packit Service 672cf4
  exit (ex);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
int
Packit Service 672cf4
main (int argc, char **argv)
Packit Service 672cf4
{
Packit Service 672cf4
  int last_argc = -1;
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
  gpgme_ctx_t ctx;
Packit Service 672cf4
  const char *key_string = NULL;
Packit Service 672cf4
  gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP;
Packit Service 672cf4
  gpgme_sig_mode_t sigmode = GPGME_SIG_MODE_NORMAL;
Packit Service 672cf4
  gpgme_data_t in, out;
Packit Service 672cf4
  gpgme_sign_result_t result;
Packit Service 672cf4
  int print_status = 0;
Packit Service 672cf4
  int use_loopback = 0;
Packit Service 672cf4
  const char *sender = NULL;
Packit Service 672cf4
  const char *s;
Packit Service 672cf4
Packit Service 672cf4
  if (argc)
Packit Service 672cf4
    { argc--; argv++; }
Packit Service 672cf4
Packit Service 672cf4
  while (argc && last_argc != argc )
Packit Service 672cf4
    {
Packit Service 672cf4
      last_argc = argc;
Packit Service 672cf4
      if (!strcmp (*argv, "--"))
Packit Service 672cf4
        {
Packit Service 672cf4
          argc--; argv++;
Packit Service 672cf4
          break;
Packit Service 672cf4
        }
Packit Service 672cf4
      else if (!strcmp (*argv, "--help"))
Packit Service 672cf4
        show_usage (0);
Packit Service 672cf4
      else if (!strcmp (*argv, "--verbose"))
Packit Service 672cf4
        {
Packit Service 672cf4
          verbose = 1;
Packit Service 672cf4
          argc--; argv++;
Packit Service 672cf4
        }
Packit Service 672cf4
      else if (!strcmp (*argv, "--status"))
Packit Service 672cf4
        {
Packit Service 672cf4
          print_status = 1;
Packit Service 672cf4
          argc--; argv++;
Packit Service 672cf4
        }
Packit Service 672cf4
      else if (!strcmp (*argv, "--openpgp"))
Packit Service 672cf4
        {
Packit Service 672cf4
          protocol = GPGME_PROTOCOL_OpenPGP;
Packit Service 672cf4
          argc--; argv++;
Packit Service 672cf4
        }
Packit Service 672cf4
      else if (!strcmp (*argv, "--cms"))
Packit Service 672cf4
        {
Packit Service 672cf4
          protocol = GPGME_PROTOCOL_CMS;
Packit Service 672cf4
          argc--; argv++;
Packit Service 672cf4
        }
Packit Service 672cf4
      else if (!strcmp (*argv, "--uiserver"))
Packit Service 672cf4
        {
Packit Service 672cf4
          protocol = GPGME_PROTOCOL_UISERVER;
Packit Service 672cf4
          argc--; argv++;
Packit Service 672cf4
        }
Packit Service 672cf4
      else if (!strcmp (*argv, "--key"))
Packit Service 672cf4
        {
Packit Service 672cf4
          argc--; argv++;
Packit Service 672cf4
          if (!argc)
Packit Service 672cf4
            show_usage (1);
Packit Service 672cf4
          key_string = *argv;
Packit Service 672cf4
          argc--; argv++;
Packit Service 672cf4
        }
Packit Service 672cf4
      else if (!strcmp (*argv, "--sender"))
Packit Service 672cf4
        {
Packit Service 672cf4
          argc--; argv++;
Packit Service 672cf4
          if (!argc)
Packit Service 672cf4
            show_usage (1);
Packit Service 672cf4
          sender = *argv;
Packit Service 672cf4
          argc--; argv++;
Packit Service 672cf4
        }
Packit Service 672cf4
      else if (!strcmp (*argv, "--loopback"))
Packit Service 672cf4
        {
Packit Service 672cf4
          use_loopback = 1;
Packit Service 672cf4
          argc--; argv++;
Packit Service 672cf4
        }
Packit Service 672cf4
      else if (!strncmp (*argv, "--", 2))
Packit Service 672cf4
        show_usage (1);
Packit Service 672cf4
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
  if (argc != 1)
Packit Service 672cf4
    show_usage (1);
Packit Service 672cf4
Packit Service 672cf4
  if (key_string && protocol == GPGME_PROTOCOL_UISERVER)
Packit Service 672cf4
    {
Packit Service 672cf4
      fprintf (stderr, PGM ": ignoring --key in UI-server mode\n");
Packit Service 672cf4
      key_string = NULL;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
  init_gpgme (protocol);
Packit Service 672cf4
Packit Service 672cf4
  err = gpgme_new (&ctx;;
Packit Service 672cf4
  fail_if_err (err);
Packit Service 672cf4
  gpgme_set_protocol (ctx, protocol);
Packit Service 672cf4
  gpgme_set_armor (ctx, 1);
Packit Service 672cf4
  if (print_status)
Packit Service 672cf4
    gpgme_set_status_cb (ctx, status_cb, NULL);
Packit Service 672cf4
  if (use_loopback)
Packit Service 672cf4
    gpgme_set_pinentry_mode (ctx, GPGME_PINENTRY_MODE_LOOPBACK);
Packit Service 672cf4
Packit Service 672cf4
  if (key_string)
Packit Service 672cf4
    {
Packit Service 672cf4
      gpgme_key_t akey;
Packit Service 672cf4
Packit Service 672cf4
      err = gpgme_get_key (ctx, key_string, &akey, 1);
Packit Service 672cf4
      if (err)
Packit Service 672cf4
        {
Packit Service 672cf4
          exit (1);
Packit Service 672cf4
        }
Packit Service 672cf4
      err = gpgme_signers_add (ctx, akey);
Packit Service 672cf4
      fail_if_err (err);
Packit Service 672cf4
      gpgme_key_unref (akey);
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
  if (sender)
Packit Service 672cf4
    {
Packit Service 672cf4
      err = gpgme_set_sender (ctx, sender);
Packit Service 672cf4
      fail_if_err (err);
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
  err = gpgme_data_new_from_file (&in, *argv, 1);
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    {
Packit Service 672cf4
      fprintf (stderr, PGM ": error reading `%s': %s\n",
Packit Service 672cf4
               *argv, gpg_strerror (err));
Packit Service 672cf4
      exit (1);
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
  err = gpgme_data_new (&out;;
Packit Service 672cf4
  fail_if_err (err);
Packit Service 672cf4
Packit Service 672cf4
  err = gpgme_op_sign (ctx, in, out, sigmode);
Packit Service 672cf4
  result = gpgme_op_sign_result (ctx);
Packit Service 672cf4
  if (result)
Packit Service 672cf4
    print_result (result, sigmode);
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    {
Packit Service 672cf4
      fprintf (stderr, PGM ": signing failed: %s\n", gpg_strerror (err));
Packit Service 672cf4
      exit (1);
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
  if ((s = gpgme_get_ctx_flag (ctx, "redraw")) && *s)
Packit Service 672cf4
    fputs ("Screen redraw suggested\n", stdout);
Packit Service 672cf4
Packit Service 672cf4
  fputs ("Begin Output:\n", stdout);
Packit Service 672cf4
  print_data (out);
Packit Service 672cf4
  fputs ("End Output.\n", stdout);
Packit Service 672cf4
  gpgme_data_release (out);
Packit Service 672cf4
Packit Service 672cf4
  gpgme_data_release (in);
Packit Service 672cf4
Packit Service 672cf4
  gpgme_release (ctx);
Packit Service 672cf4
  return 0;
Packit Service 672cf4
}