Blame tests/run-encrypt.c

Packit Service 672cf4
/* run-encrypt.c  - Helper to perform an encrypt operation
Packit Service 672cf4
 * Copyright (C) 2016 g10 Code GmbH
Packit Service 672cf4
 *
Packit Service 672cf4
 * This file is part of GPGME.
Packit Service 672cf4
 *
Packit Service 672cf4
 * GPGME is free software; you can redistribute it and/or modify it
Packit Service 672cf4
 * under the terms of the GNU Lesser General Public License as
Packit Service 672cf4
 * published by the Free Software Foundation; either version 2.1 of
Packit Service 672cf4
 * the License, or (at your option) any later version.
Packit Service 672cf4
 *
Packit Service 672cf4
 * GPGME is distributed in the hope that it will be useful, but
Packit Service 672cf4
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 672cf4
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 672cf4
 * Lesser General Public License for more details.
Packit Service 672cf4
 *
Packit Service 672cf4
 * You should have received a copy of the GNU Lesser General Public
Packit Service 6c01f9
 * License along with this program; if not, see <https://www.gnu.org/licenses/>.
Packit Service 672cf4
 */
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-encrypt"
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
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
  fprintf (stderr, "status_cb: %s %s\n", nonnull(keyword), nonnull(value));
Packit Service 672cf4
  return 0;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
static void
Packit Service 672cf4
progress_cb (void *opaque, const char *what, int type, int current, int total)
Packit Service 672cf4
{
Packit Service 672cf4
  (void)opaque;
Packit Service 672cf4
  (void)type;
Packit Service 672cf4
Packit Service 672cf4
  if (total)
Packit Service 672cf4
    fprintf (stderr, "progress for '%s' %u%% (%d of %d)\n",
Packit Service 672cf4
             nonnull (what),
Packit Service 672cf4
             (unsigned)(((double)current / total) * 100), current, total);
Packit Service 672cf4
  else
Packit Service 672cf4
    fprintf (stderr, "progress for '%s' %d\n", nonnull(what), current);
Packit Service 672cf4
  fflush (stderr);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
static void
Packit Service 672cf4
print_result (gpgme_encrypt_result_t result)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_invalid_key_t invkey;
Packit Service 672cf4
Packit Service 672cf4
  for (invkey = result->invalid_recipients; invkey; invkey = invkey->next)
Packit Service 672cf4
    printf ("Encryption 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
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 6c01f9
         "  --verbose        run in verbose mode\n"
Packit Service 6c01f9
         "  --status         print status lines from the backend\n"
Packit Service 6c01f9
         "  --progress       print progress info\n"
Packit Service 6c01f9
         "  --openpgp        use the OpenPGP protocol (default)\n"
Packit Service 6c01f9
         "  --cms            use the CMS protocol\n"
Packit Service 6c01f9
         "  --uiserver       use the UI server\n"
Packit Service 6c01f9
         "  --loopback       use a loopback pinentry\n"
Packit Service 6c01f9
         "  --key NAME       encrypt to key NAME\n"
Packit Service 6c01f9
         "  --throw-keyids   use this option\n"
Packit Service 6c01f9
         "  --wrap           assume input is valid OpenPGP message\n"
Packit Service 6c01f9
         "  --symmetric      encrypt symmetric (OpenPGP only)\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 6c01f9
  const char *key_string = NULL;
Packit Service 672cf4
  gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP;
Packit Service 672cf4
  gpgme_data_t in, out;
Packit Service 672cf4
  gpgme_encrypt_result_t result;
Packit Service 672cf4
  int print_status = 0;
Packit Service 672cf4
  int print_progress = 0;
Packit Service 672cf4
  int use_loopback = 0;
Packit Service 672cf4
  char *keyargs[10];
Packit Service 672cf4
  gpgme_key_t keys[10+1];
Packit Service 672cf4
  int keycount = 0;
Packit Service 672cf4
  int i;
Packit Service 672cf4
  gpgme_encrypt_flags_t flags = GPGME_ENCRYPT_ALWAYS_TRUST;
Packit Service 672cf4
  gpgme_off_t offset;
Packit Service 672cf4
Packit Service 672cf4
  if (argc)
Packit Service 672cf4
    { argc--; argv++; }
Packit Service 672cf4
Packit Service 672cf4
  if (DIM(keys) != DIM(keyargs)+1)
Packit Service 672cf4
    abort ();
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, "--progress"))
Packit Service 672cf4
        {
Packit Service 672cf4
          print_progress = 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
          if (keycount == DIM (keyargs))
Packit Service 672cf4
            show_usage (1);
Packit Service 672cf4
          keyargs[keycount++] = *argv;
Packit Service 672cf4
          argc--; argv++;
Packit Service 672cf4
        }
Packit Service 672cf4
      else if (!strcmp (*argv, "--throw-keyids"))
Packit Service 672cf4
        {
Packit Service 672cf4
          flags |= GPGME_ENCRYPT_THROW_KEYIDS;
Packit Service 672cf4
          argc--; argv++;
Packit Service 672cf4
        }
Packit Service 672cf4
      else if (!strcmp (*argv, "--wrap"))
Packit Service 672cf4
        {
Packit Service 672cf4
          flags |= GPGME_ENCRYPT_WRAP;
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 (!strcmp (*argv, "--symmetric"))
Packit Service 672cf4
        {
Packit Service 672cf4
          flags |= GPGME_ENCRYPT_SYMMETRIC;
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 6c01f9
  if (key_string && protocol == GPGME_PROTOCOL_UISERVER)
Packit Service 6c01f9
    {
Packit Service 6c01f9
      fprintf (stderr, PGM ": ignoring --key in UI-server mode\n");
Packit Service 6c01f9
      key_string = NULL;
Packit Service 6c01f9
    }
Packit Service 6c01f9
Packit Service 6c01f9
  if (!key_string)
Packit Service 6c01f9
    key_string = "test";
Packit Service 6c01f9
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
    {
Packit Service 672cf4
      gpgme_set_status_cb (ctx, status_cb, NULL);
Packit Service 672cf4
      gpgme_set_ctx_flag (ctx, "full-status", "1");
Packit Service 672cf4
    }
Packit Service 672cf4
  if (print_progress)
Packit Service 672cf4
    gpgme_set_progress_cb (ctx, progress_cb, NULL);
Packit Service 672cf4
  if (use_loopback)
Packit Service 672cf4
    {
Packit Service 672cf4
      gpgme_set_pinentry_mode (ctx, GPGME_PINENTRY_MODE_LOOPBACK);
Packit Service 672cf4
      gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL);
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
  for (i=0; i < keycount; i++)
Packit Service 672cf4
    {
Packit Service 672cf4
      err = gpgme_get_key (ctx, keyargs[i], &keys[i], 0);
Packit Service 672cf4
      fail_if_err (err);
Packit Service 672cf4
    }
Packit Service 672cf4
  keys[i] = NULL;
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
  offset = gpgme_data_seek (in, 0, SEEK_END);
Packit Service 672cf4
  if (offset == (gpgme_off_t)(-1))
Packit Service 672cf4
    {
Packit Service 672cf4
      err = gpg_error_from_syserror ();
Packit Service 672cf4
      fprintf (stderr, PGM ": error seeking `%s': %s\n",
Packit Service 672cf4
               *argv, gpg_strerror (err));
Packit Service 672cf4
      exit (1);
Packit Service 672cf4
    }
Packit Service 672cf4
  if (gpgme_data_seek (in, 0, SEEK_SET) == (gpgme_off_t)(-1))
Packit Service 672cf4
    {
Packit Service 672cf4
      err = gpg_error_from_syserror ();
Packit Service 672cf4
      fprintf (stderr, PGM ": error seeking `%s': %s\n",
Packit Service 672cf4
               *argv, gpg_strerror (err));
Packit Service 672cf4
      exit (1);
Packit Service 672cf4
    }
Packit Service 672cf4
  {
Packit Service 672cf4
    char numbuf[50];
Packit Service 672cf4
    char *p;
Packit Service 672cf4
Packit Service 672cf4
    p = numbuf + sizeof numbuf;
Packit Service 672cf4
    *--p = 0;
Packit Service 672cf4
    do
Packit Service 672cf4
      {
Packit Service 672cf4
        *--p = '0' + (offset % 10);
Packit Service 672cf4
        offset /= 10;
Packit Service 672cf4
      }
Packit Service 672cf4
    while (offset);
Packit Service 672cf4
    err = gpgme_data_set_flag (in, "size-hint", p);
Packit Service 672cf4
    if (err)
Packit Service 672cf4
      {
Packit Service 672cf4
        fprintf (stderr, PGM ": error setting size-hint for `%s': %s\n",
Packit Service 672cf4
                 *argv, gpg_strerror (err));
Packit Service 672cf4
        exit (1);
Packit Service 672cf4
      }
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 6c01f9
  err = gpgme_op_encrypt (ctx, keycount ? keys : NULL, flags, in, out);
Packit Service 672cf4
  result = gpgme_op_encrypt_result (ctx);
Packit Service 672cf4
  if (result)
Packit Service 672cf4
    print_result (result);
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    {
Packit Service 672cf4
      fprintf (stderr, PGM ": encrypting failed: %s\n", gpg_strerror (err));
Packit Service 672cf4
      exit (1);
Packit Service 672cf4
    }
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
  for (i=0; i < keycount; i++)
Packit Service 672cf4
    gpgme_key_unref (keys[i]);
Packit Service 672cf4
  gpgme_release (ctx);
Packit Service 672cf4
  return 0;
Packit Service 672cf4
}