Blame tests/gpgsm/t-genkey.c

Packit Service 672cf4
/* t-genkey.c - Regression test.
Packit Service 6c01f9
   Copyright (C) 2000 Werner Koch (dd9jn)
Packit Service 6c01f9
   Copyright (C) 2001, 2003, 2004 g10 Code GmbH
Packit Service 6c01f9
Packit Service 6c01f9
   This file is part of GPGME.
Packit Service 6c01f9
Packit Service 6c01f9
   GPGME is free software; you can redistribute it and/or modify it
Packit Service 6c01f9
   under the terms of the GNU Lesser General Public License as
Packit Service 6c01f9
   published by the Free Software Foundation; either version 2.1 of
Packit Service 6c01f9
   the License, or (at your option) any later version.
Packit Service 6c01f9
Packit Service 6c01f9
   GPGME is distributed in the hope that it will be useful, but
Packit Service 6c01f9
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 6c01f9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 6c01f9
   Lesser General Public License for more details.
Packit Service 6c01f9
Packit Service 6c01f9
   You should have received a copy of the GNU Lesser General Public
Packit Service 6c01f9
   License along with this program; if not, write to the Free Software
Packit Service 6c01f9
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
Packit Service 6c01f9
   02111-1307, USA.  */
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
#include <errno.h>
Packit Service 672cf4
Packit Service 672cf4
#include <gpgme.h>
Packit Service 672cf4
#include "t-support.h"
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* True if progress function printed something on the screen.  */
Packit Service 672cf4
static int progress_called;
Packit Service 672cf4
Packit Service 672cf4
static void
Packit Service 672cf4
progress (void *self, const char *what, int type, int current, int total)
Packit Service 672cf4
{
Packit Service 672cf4
  (void)self;
Packit Service 672cf4
Packit Service 672cf4
  if (!strcmp (what, "primegen") && !current && !total
Packit Service 672cf4
      && (type == '.' || type == '+' || type == '!'
Packit Service 672cf4
	  || type == '^' || type == '<' || type == '>'))
Packit Service 672cf4
    {
Packit Service 672cf4
      printf ("%c", type);
Packit Service 672cf4
      fflush (stdout);
Packit Service 672cf4
      progress_called = 1;
Packit Service 672cf4
    }
Packit Service 672cf4
  else
Packit Service 672cf4
    {
Packit Service 672cf4
      fprintf (stderr, "unknown progress `%s' %d %d %d\n", what, type,
Packit Service 672cf4
	       current, total);
Packit Service 672cf4
      exit (1);
Packit Service 672cf4
    }
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
int
Packit Service 672cf4
main (void)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_ctx_t ctx;
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
  const char *parms = "<GnupgKeyParms format=\"internal\">\n"
Packit Service 672cf4
    "Key-Type: RSA\n"
Packit Service 672cf4
    "Key-Length: 1024\n"
Packit Service 672cf4
    "Name-DN: C=de,O=g10 code,OU=Testlab,CN=Joe 2 Tester\n"
Packit Service 672cf4
    "Name-Email: joe@foo.bar\n"
Packit Service 672cf4
    "</GnupgKeyParms>\n";
Packit Service 672cf4
  gpgme_genkey_result_t result;
Packit Service 672cf4
  gpgme_data_t certreq;
Packit Service 672cf4
Packit Service 672cf4
  init_gpgme (GPGME_PROTOCOL_CMS);
Packit Service 672cf4
Packit Service 672cf4
  err = gpgme_data_new (&certreq);
Packit Service 672cf4
  fail_if_err (err);
Packit Service 672cf4
Packit Service 672cf4
  err = gpgme_new (&ctx;;
Packit Service 672cf4
  fail_if_err (err);
Packit Service 672cf4
Packit Service 672cf4
  gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS);
Packit Service 672cf4
  gpgme_set_armor (ctx, 1);
Packit Service 672cf4
Packit Service 672cf4
  gpgme_set_progress_cb (ctx, progress, NULL);
Packit Service 672cf4
Packit Service 672cf4
  err = gpgme_op_genkey (ctx, parms, certreq, NULL);
Packit Service 672cf4
  fail_if_err (err);
Packit Service 672cf4
Packit Service 672cf4
  result = gpgme_op_genkey_result (ctx);
Packit Service 672cf4
  if (!result)
Packit Service 672cf4
    {
Packit Service 672cf4
      fprintf (stderr, "%s:%d: gpgme_op_genkey_result returns NULL\n",
Packit Service 672cf4
	       __FILE__, __LINE__);
Packit Service 672cf4
      exit (1);
Packit Service 672cf4
    }
Packit Service 672cf4
  if (progress_called)
Packit Service 672cf4
    printf ("\n");
Packit Service 672cf4
Packit Service 672cf4
  printf ("Generated key: %s (%s)\n", result->fpr ? result->fpr : "none",
Packit Service 672cf4
	  result->primary ? (result->sub ? "primary, sub" : "primary")
Packit Service 672cf4
	  : (result->sub ? "sub" : "none"));
Packit Service 672cf4
Packit Service 672cf4
  if (result->fpr)
Packit Service 672cf4
    {
Packit Service 6c01f9
      fprintf (stderr, "%s:%d: generated key has (unexpectdly) a fingerprint\n",
Packit Service 672cf4
	       __FILE__, __LINE__);
Packit Service 672cf4
      exit (1);
Packit Service 672cf4
    }
Packit Service 672cf4
  if (!result->primary)
Packit Service 672cf4
    {
Packit Service 672cf4
      fprintf (stderr, "%s:%d: primary key was not generated\n",
Packit Service 672cf4
	       __FILE__, __LINE__);
Packit Service 672cf4
      exit (1);
Packit Service 672cf4
    }
Packit Service 672cf4
  if (result->sub)
Packit Service 672cf4
    {
Packit Service 672cf4
      fprintf (stderr, "%s:%d: sub key was (unexpectedly) generated\n",
Packit Service 672cf4
	       __FILE__, __LINE__);
Packit Service 672cf4
      exit (1);
Packit Service 672cf4
    }
Packit Service 672cf4
  gpgme_release (ctx);
Packit Service 672cf4
Packit Service 672cf4
  print_data (certreq);
Packit Service 672cf4
  gpgme_data_release (certreq);
Packit Service 672cf4
Packit Service 672cf4
  return 0;
Packit Service 672cf4
}