Blame src/genkey.c

Packit Service 672cf4
/* genkey.c - Key generation.
Packit Service 672cf4
 * Copyright (C) 2000 Werner Koch (dd9jn)
Packit Service 672cf4
 * Copyright (C) 2001, 2002, 2003, 2004, 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
#if HAVE_CONFIG_H
Packit Service 672cf4
#include <config.h>
Packit Service 672cf4
#endif
Packit Service 672cf4
#include <stdlib.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 "debug.h"
Packit Service 672cf4
#include "context.h"
Packit Service 672cf4
#include "ops.h"
Packit Service 672cf4
#include "util.h"
Packit Service 672cf4
Packit Service 672cf4

Packit Service 672cf4
typedef struct
Packit Service 672cf4
{
Packit Service 672cf4
  struct _gpgme_op_genkey_result result;
Packit Service 672cf4
Packit Service 672cf4
  /* The error code from a FAILURE status line or 0.  */
Packit Service 672cf4
  gpg_error_t failure_code;
Packit Service 672cf4
Packit Service 672cf4
  /* The error code from certain ERROR status lines or 0.  */
Packit Service 672cf4
  gpg_error_t error_code;
Packit Service 672cf4
Packit Service 672cf4
  /* Flag to indicate that a UID is to be added.  */
Packit Service 672cf4
  gpg_error_t uidmode;
Packit Service 672cf4
Packit Service 672cf4
  /* The key parameters passed to the crypto engine.  */
Packit Service 672cf4
  gpgme_data_t key_parameter;
Packit Service 672cf4
} *op_data_t;
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
static void
Packit Service 672cf4
release_op_data (void *hook)
Packit Service 672cf4
{
Packit Service 672cf4
  op_data_t opd = (op_data_t) hook;
Packit Service 672cf4
Packit Service 672cf4
  if (opd->result.fpr)
Packit Service 672cf4
    free (opd->result.fpr);
Packit Service 672cf4
  if (opd->key_parameter)
Packit Service 672cf4
    gpgme_data_release (opd->key_parameter);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
gpgme_genkey_result_t
Packit Service 672cf4
gpgme_op_genkey_result (gpgme_ctx_t ctx)
Packit Service 672cf4
{
Packit Service 672cf4
  void *hook;
Packit Service 672cf4
  op_data_t opd;
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
Packit Service 6c01f9
  TRACE_BEG (DEBUG_CTX, "gpgme_op_genkey_result", ctx);
Packit Service 672cf4
Packit Service 672cf4
  err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook, -1, NULL);
Packit Service 672cf4
  opd = hook;
Packit Service 672cf4
  if (err || !opd)
Packit Service 672cf4
    {
Packit Service 6c01f9
      TRACE_SUC0 ("result=(null)");
Packit Service 672cf4
      return NULL;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 6c01f9
  TRACE_LOG3 ("fpr = %s, %s, %s", opd->result.fpr,
Packit Service 672cf4
	      opd->result.primary ? "primary" : "no primary",
Packit Service 672cf4
	      opd->result.sub ? "sub" : "no sub");
Packit Service 672cf4
Packit Service 6c01f9
  TRACE_SUC1 ("result=%p", &opd->result);
Packit Service 672cf4
  return &opd->result;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4

Packit Service 672cf4
/* Parse an error status line.  Return the error location and the
Packit Service 672cf4
   error code.  The function may modify ARGS. */
Packit Service 672cf4
static char *
Packit Service 672cf4
parse_error (char *args, gpg_error_t *r_err)
Packit Service 672cf4
{
Packit Service 672cf4
  char *where = strchr (args, ' ');
Packit Service 672cf4
  char *which;
Packit Service 672cf4
Packit Service 672cf4
  if (where)
Packit Service 672cf4
    {
Packit Service 672cf4
      *where = '\0';
Packit Service 672cf4
      which = where + 1;
Packit Service 672cf4
Packit Service 672cf4
      where = strchr (which, ' ');
Packit Service 672cf4
      if (where)
Packit Service 672cf4
	*where = '\0';
Packit Service 672cf4
Packit Service 672cf4
      where = args;
Packit Service 672cf4
    }
Packit Service 672cf4
  else
Packit Service 672cf4
    {
Packit Service 672cf4
      *r_err = trace_gpg_error (GPG_ERR_INV_ENGINE);
Packit Service 672cf4
      return NULL;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
  *r_err = atoi (which);
Packit Service 672cf4
Packit Service 672cf4
  return where;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
static gpgme_error_t
Packit Service 672cf4
genkey_status_handler (void *priv, gpgme_status_code_t code, char *args)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_ctx_t ctx = (gpgme_ctx_t) priv;
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
  void *hook;
Packit Service 672cf4
  op_data_t opd;
Packit Service 672cf4
  char *loc;
Packit Service 672cf4
Packit Service 672cf4
  /* Pipe the status code through the progress status handler.  */
Packit Service 672cf4
  err = _gpgme_progress_status_handler (ctx, code, args);
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    return err;
Packit Service 672cf4
Packit Service 672cf4
  err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook, -1, NULL);
Packit Service 672cf4
  opd = hook;
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    return err;
Packit Service 672cf4
Packit Service 672cf4
  switch (code)
Packit Service 672cf4
    {
Packit Service 672cf4
    case GPGME_STATUS_KEY_CREATED:
Packit Service 672cf4
      if (args && *args)
Packit Service 672cf4
	{
Packit Service 672cf4
	  if (*args == 'B' || *args == 'P')
Packit Service 672cf4
            {
Packit Service 672cf4
              opd->result.primary = 1;
Packit Service 672cf4
              opd->result.uid = 1;
Packit Service 672cf4
            }
Packit Service 672cf4
	  if (*args == 'B' || *args == 'S')
Packit Service 672cf4
	    opd->result.sub = 1;
Packit Service 672cf4
	  if (args[1] == ' ')
Packit Service 672cf4
	    {
Packit Service 672cf4
	      if (opd->result.fpr)
Packit Service 672cf4
		free (opd->result.fpr);
Packit Service 672cf4
	      opd->result.fpr = strdup (&args[2]);
Packit Service 672cf4
	      if (!opd->result.fpr)
Packit Service 672cf4
		return gpg_error_from_syserror ();
Packit Service 672cf4
	    }
Packit Service 672cf4
	}
Packit Service 672cf4
      break;
Packit Service 672cf4
Packit Service 672cf4
    case GPGME_STATUS_ERROR:
Packit Service 672cf4
      loc = parse_error (args, &err;;
Packit Service 672cf4
      if (!loc)
Packit Service 672cf4
        return err;
Packit Service 672cf4
      if (!opd->error_code)
Packit Service 672cf4
        opd->error_code = err;
Packit Service 672cf4
      break;
Packit Service 672cf4
Packit Service 672cf4
    case GPGME_STATUS_FAILURE:
Packit Service 672cf4
      opd->failure_code = _gpgme_parse_failure (args);
Packit Service 672cf4
      break;
Packit Service 672cf4
Packit Service 672cf4
    case GPGME_STATUS_EOF:
Packit Service 672cf4
      if (opd->error_code)
Packit Service 672cf4
        return opd->error_code;
Packit Service 672cf4
      else if (!opd->uidmode && !opd->result.primary && !opd->result.sub)
Packit Service 672cf4
	return gpg_error (GPG_ERR_GENERAL);
Packit Service 672cf4
      else if (opd->failure_code)
Packit Service 672cf4
        return opd->failure_code;
Packit Service 672cf4
      else if (opd->uidmode == 1)
Packit Service 672cf4
        opd->result.uid = 1;  /* We have no status line, thus this hack.  */
Packit Service 672cf4
      break;
Packit Service 672cf4
Packit Service 672cf4
    case GPGME_STATUS_INQUIRE_MAXLEN:
Packit Service 672cf4
      if (ctx->status_cb && !ctx->full_status)
Packit Service 672cf4
        {
Packit Service 672cf4
          err = ctx->status_cb (ctx->status_cb_value, "INQUIRE_MAXLEN", args);
Packit Service 672cf4
          if (err)
Packit Service 672cf4
            return err;
Packit Service 672cf4
        }
Packit Service 672cf4
      break;
Packit Service 672cf4
Packit Service 672cf4
    default:
Packit Service 672cf4
      break;
Packit Service 672cf4
    }
Packit Service 672cf4
  return 0;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
static gpgme_error_t
Packit Service 672cf4
get_key_parameter (const char *parms, gpgme_data_t *key_parameter)
Packit Service 672cf4
{
Packit Service 672cf4
  const char *content;
Packit Service 672cf4
  const char *attrib;
Packit Service 672cf4
  const char *endtag;
Packit Service 672cf4
Packit Service 672cf4
  /* Extract the key parameter from the XML structure.  */
Packit Service 672cf4
  parms = strstr (parms, "
Packit Service 672cf4
  if (!parms)
Packit Service 672cf4
    return gpg_error (GPG_ERR_INV_VALUE);
Packit Service 672cf4
Packit Service 672cf4
  content = strchr (parms, '>');
Packit Service 672cf4
  if (!content)
Packit Service 672cf4
    return gpg_error (GPG_ERR_INV_VALUE);
Packit Service 672cf4
  content++;
Packit Service 672cf4
Packit Service 672cf4
  attrib = strstr (parms, "format=\"internal\"");
Packit Service 672cf4
  if (!attrib || attrib >= content)
Packit Service 672cf4
    return gpg_error (GPG_ERR_INV_VALUE);
Packit Service 672cf4
Packit Service 672cf4
  endtag = strstr (content, "</GnupgKeyParms>");
Packit Service 672cf4
  /* FIXME: Check that there are no control statements inside.  */
Packit Service 6c01f9
  while (content[0] == '\n'
Packit Service 6c01f9
	 || (content[0] == '\r' && content[1] == '\n'))
Packit Service 672cf4
    content++;
Packit Service 672cf4
Packit Service 672cf4
  return gpgme_data_new_from_mem (key_parameter, content,
Packit Service 672cf4
				  endtag - content, 1);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
static gpgme_error_t
Packit Service 672cf4
genkey_start (gpgme_ctx_t ctx, int synchronous, const char *parms,
Packit Service 672cf4
	      gpgme_data_t pubkey, gpgme_data_t seckey)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
  void *hook;
Packit Service 672cf4
  op_data_t opd;
Packit Service 672cf4
  err = _gpgme_op_reset (ctx, synchronous);
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    return err;
Packit Service 672cf4
Packit Service 672cf4
  err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook,
Packit Service 672cf4
			       sizeof (*opd), release_op_data);
Packit Service 672cf4
  opd = hook;
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    return err;
Packit Service 672cf4
Packit Service 672cf4
  err = get_key_parameter (parms, &opd->key_parameter);
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    return err;
Packit Service 672cf4
Packit Service 672cf4
  _gpgme_engine_set_status_handler (ctx->engine, genkey_status_handler, ctx);
Packit Service 672cf4
Packit Service 672cf4
  if (ctx->passphrase_cb)
Packit Service 672cf4
    {
Packit Service 672cf4
      err = _gpgme_engine_set_command_handler
Packit Service 6c01f9
        (ctx->engine, _gpgme_passphrase_command_handler, ctx, NULL);
Packit Service 672cf4
      if (err)
Packit Service 672cf4
        return err;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
  return _gpgme_engine_op_genkey (ctx->engine,
Packit Service 672cf4
                                  NULL, NULL, 0, 0, NULL, 0,
Packit Service 672cf4
                                  opd->key_parameter,
Packit Service 672cf4
				  ctx->use_armor? GENKEY_EXTRAFLAG_ARMOR:0,
Packit Service 672cf4
                                  pubkey, seckey);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* Generate a new keypair and add it to the keyring.  PUBKEY and
Packit Service 672cf4
   SECKEY should be null for now.  PARMS specifies what keys should be
Packit Service 672cf4
   generated.  */
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
gpgme_op_genkey_start (gpgme_ctx_t ctx, const char *parms,
Packit Service 672cf4
		       gpgme_data_t pubkey, gpgme_data_t seckey)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
Packit Service 6c01f9
  TRACE_BEG2 (DEBUG_CTX, "gpgme_op_genkey_start", ctx,
Packit Service 672cf4
	      "pubkey=%p, seckey=%p", pubkey, seckey);
Packit Service 6c01f9
  TRACE_LOGBUF (parms, strlen (parms));
Packit Service 672cf4
Packit Service 6c01f9
  if (!ctx)
Packit Service 672cf4
    return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
Packit Service 672cf4
Packit Service 672cf4
  err = genkey_start (ctx, 0, parms, pubkey, seckey);
Packit Service 672cf4
  return TRACE_ERR (err);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* Generate a new keypair and add it to the keyring.  PUBKEY and
Packit Service 672cf4
   SECKEY should be null for now.  PARMS specifies what keys should be
Packit Service 672cf4
   generated.  */
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
gpgme_op_genkey (gpgme_ctx_t ctx, const char *parms, gpgme_data_t pubkey,
Packit Service 672cf4
		 gpgme_data_t seckey)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
Packit Service 6c01f9
  TRACE_BEG2 (DEBUG_CTX, "gpgme_op_genkey", ctx,
Packit Service 672cf4
	      "pubkey=%p, seckey=%p", pubkey, seckey);
Packit Service 6c01f9
  TRACE_LOGBUF (parms, strlen (parms));
Packit Service 672cf4
Packit Service 672cf4
  if (!ctx)
Packit Service 672cf4
    return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
Packit Service 672cf4
Packit Service 672cf4
  err = genkey_start (ctx, 1, parms, pubkey, seckey);
Packit Service 672cf4
  if (!err)
Packit Service 672cf4
    err = _gpgme_wait_one (ctx);
Packit Service 672cf4
  return TRACE_ERR (err);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4

Packit Service 672cf4
static gpgme_error_t
Packit Service 672cf4
createkey_start (gpgme_ctx_t ctx, int synchronous,
Packit Service 672cf4
                 const char *userid, const char *algo,
Packit Service 672cf4
                 unsigned long reserved, unsigned long expires,
Packit Service 672cf4
                 gpgme_key_t anchorkey, unsigned int flags)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
  void *hook;
Packit Service 672cf4
  op_data_t opd;
Packit Service 672cf4
Packit Service 672cf4
  err = _gpgme_op_reset (ctx, synchronous);
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    return err;
Packit Service 672cf4
Packit Service 672cf4
  if (reserved || anchorkey || !userid)
Packit Service 672cf4
    return gpg_error (GPG_ERR_INV_ARG);
Packit Service 672cf4
Packit Service 672cf4
  err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook,
Packit Service 672cf4
			       sizeof (*opd), release_op_data);
Packit Service 672cf4
  opd = hook;
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    return err;
Packit Service 672cf4
Packit Service 672cf4
  _gpgme_engine_set_status_handler (ctx->engine, genkey_status_handler, ctx);
Packit Service 672cf4
Packit Service 672cf4
  if (ctx->passphrase_cb)
Packit Service 672cf4
    {
Packit Service 672cf4
      err = _gpgme_engine_set_command_handler
Packit Service 6c01f9
        (ctx->engine, _gpgme_passphrase_command_handler, ctx, NULL);
Packit Service 672cf4
      if (err)
Packit Service 672cf4
        return err;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
  return _gpgme_engine_op_genkey (ctx->engine,
Packit Service 672cf4
                                  userid, algo, reserved, expires,
Packit Service 672cf4
                                  anchorkey, flags,
Packit Service 672cf4
                                  NULL,
Packit Service 672cf4
				  ctx->use_armor? GENKEY_EXTRAFLAG_ARMOR:0,
Packit Service 672cf4
                                  NULL, NULL);
Packit Service 672cf4
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
gpgme_op_createkey_start (gpgme_ctx_t ctx, const char *userid, const char *algo,
Packit Service 672cf4
                          unsigned long reserved, unsigned long expires,
Packit Service 672cf4
                          gpgme_key_t anchorkey, unsigned int flags)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
Packit Service 6c01f9
  TRACE_BEG3 (DEBUG_CTX, "gpgme_op_createkey_start", ctx,
Packit Service 672cf4
	      "userid='%s', algo='%s' flags=0x%x", userid, algo, flags);
Packit Service 672cf4
Packit Service 672cf4
  if (!ctx)
Packit Service 672cf4
    return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
Packit Service 672cf4
Packit Service 672cf4
  err = createkey_start (ctx, 0,
Packit Service 672cf4
                         userid, algo, reserved, expires, anchorkey, flags);
Packit Service 672cf4
  return TRACE_ERR (err);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
gpgme_op_createkey (gpgme_ctx_t ctx, const char *userid, const char *algo,
Packit Service 672cf4
                    unsigned long reserved, unsigned long expires,
Packit Service 672cf4
                    gpgme_key_t anchorkey, unsigned int flags)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
Packit Service 6c01f9
  TRACE_BEG3 (DEBUG_CTX, "gpgme_op_createkey", ctx,
Packit Service 672cf4
	      "userid='%s', algo='%s' flags=0x%x", userid, algo, flags);
Packit Service 672cf4
Packit Service 672cf4
  if (!ctx)
Packit Service 672cf4
    return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
Packit Service 672cf4
Packit Service 672cf4
  err = createkey_start (ctx, 1,
Packit Service 672cf4
                         userid, algo, reserved, expires, anchorkey, flags);
Packit Service 672cf4
  if (!err)
Packit Service 672cf4
    err = _gpgme_wait_one (ctx);
Packit Service 672cf4
  return TRACE_ERR (err);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4

Packit Service 672cf4
static gpgme_error_t
Packit Service 672cf4
createsubkey_start (gpgme_ctx_t ctx, int synchronous,
Packit Service 672cf4
                    gpgme_key_t key,
Packit Service 672cf4
                    const char *algo,
Packit Service 672cf4
                    unsigned long reserved, unsigned long expires,
Packit Service 672cf4
                    unsigned int flags)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
  void *hook;
Packit Service 672cf4
  op_data_t opd;
Packit Service 672cf4
Packit Service 672cf4
  if (ctx->protocol != GPGME_PROTOCOL_OPENPGP)
Packit Service 672cf4
    return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL);
Packit Service 672cf4
Packit Service 672cf4
  err = _gpgme_op_reset (ctx, synchronous);
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    return err;
Packit Service 672cf4
Packit Service 672cf4
  if (reserved || !key)
Packit Service 672cf4
    return gpg_error (GPG_ERR_INV_ARG);
Packit Service 672cf4
Packit Service 672cf4
  err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook,
Packit Service 672cf4
			       sizeof (*opd), release_op_data);
Packit Service 672cf4
  opd = hook;
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    return err;
Packit Service 672cf4
Packit Service 672cf4
  _gpgme_engine_set_status_handler (ctx->engine, genkey_status_handler, ctx);
Packit Service 672cf4
Packit Service 672cf4
  if (ctx->passphrase_cb)
Packit Service 672cf4
    {
Packit Service 672cf4
      err = _gpgme_engine_set_command_handler
Packit Service 6c01f9
        (ctx->engine, _gpgme_passphrase_command_handler, ctx, NULL);
Packit Service 672cf4
      if (err)
Packit Service 672cf4
        return err;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
  return _gpgme_engine_op_genkey (ctx->engine,
Packit Service 672cf4
                                  NULL, algo, reserved, expires,
Packit Service 672cf4
                                  key, flags,
Packit Service 672cf4
                                  NULL,
Packit Service 672cf4
				  ctx->use_armor? GENKEY_EXTRAFLAG_ARMOR:0,
Packit Service 672cf4
                                  NULL, NULL);
Packit Service 672cf4
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* Add a subkey to an existing KEY.  */
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
gpgme_op_createsubkey_start (gpgme_ctx_t ctx, gpgme_key_t key, const char *algo,
Packit Service 672cf4
                             unsigned long reserved, unsigned long expires,
Packit Service 672cf4
                             unsigned int flags)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
Packit Service 6c01f9
  TRACE_BEG3 (DEBUG_CTX, "gpgme_op_createsubkey_start", ctx,
Packit Service 672cf4
	      "key=%p, algo='%s' flags=0x%x", key, algo, flags);
Packit Service 672cf4
Packit Service 672cf4
  if (!ctx)
Packit Service 672cf4
    return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
Packit Service 672cf4
Packit Service 672cf4
  err = createsubkey_start (ctx, 0, key, algo, reserved, expires, flags);
Packit Service 672cf4
  return TRACE_ERR (err);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
gpgme_op_createsubkey (gpgme_ctx_t ctx, gpgme_key_t key, const char *algo,
Packit Service 672cf4
                       unsigned long reserved, unsigned long expires,
Packit Service 672cf4
                       unsigned int flags)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
Packit Service 6c01f9
  TRACE_BEG3 (DEBUG_CTX, "gpgme_op_createsubkey", ctx,
Packit Service 672cf4
	      "key=%p, algo='%s' flags=0x%x", key, algo, flags);
Packit Service 672cf4
Packit Service 672cf4
  if (!ctx)
Packit Service 672cf4
    return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
Packit Service 672cf4
Packit Service 672cf4
  err = createsubkey_start (ctx, 1, key, algo, reserved, expires, flags);
Packit Service 672cf4
  if (!err)
Packit Service 672cf4
    err = _gpgme_wait_one (ctx);
Packit Service 672cf4
  return TRACE_ERR (err);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4

Packit Service 672cf4
static gpgme_error_t
Packit Service 672cf4
addrevuid_start (gpgme_ctx_t ctx, int synchronous, int extraflags,
Packit Service 672cf4
                 gpgme_key_t key, const char *userid, unsigned int flags)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
  void *hook;
Packit Service 672cf4
  op_data_t opd;
Packit Service 672cf4
Packit Service 672cf4
  if (ctx->protocol != GPGME_PROTOCOL_OPENPGP)
Packit Service 672cf4
    return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL);
Packit Service 672cf4
Packit Service 672cf4
  if (!key || !userid)
Packit Service 672cf4
    return gpg_error (GPG_ERR_INV_ARG);
Packit Service 672cf4
Packit Service 672cf4
  err = _gpgme_op_reset (ctx, synchronous);
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    return err;
Packit Service 672cf4
Packit Service 672cf4
  err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook,
Packit Service 672cf4
			       sizeof (*opd), release_op_data);
Packit Service 672cf4
  opd = hook;
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    return err;
Packit Service 672cf4
Packit Service 672cf4
  opd->uidmode = extraflags? 2 : 1;
Packit Service 672cf4
Packit Service 672cf4
  _gpgme_engine_set_status_handler (ctx->engine, genkey_status_handler, ctx);
Packit Service 672cf4
Packit Service 672cf4
  if (ctx->passphrase_cb)
Packit Service 672cf4
    {
Packit Service 672cf4
      err = _gpgme_engine_set_command_handler
Packit Service 6c01f9
        (ctx->engine, _gpgme_passphrase_command_handler, ctx, NULL);
Packit Service 672cf4
      if (err)
Packit Service 672cf4
        return err;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
  return _gpgme_engine_op_genkey (ctx->engine,
Packit Service 672cf4
                                  userid, NULL, 0, 0,
Packit Service 672cf4
                                  key, flags,
Packit Service 672cf4
                                  NULL,
Packit Service 672cf4
				  extraflags,
Packit Service 672cf4
                                  NULL, NULL);
Packit Service 672cf4
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* Add USERID to an existing KEY.  */
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
gpgme_op_adduid_start (gpgme_ctx_t ctx,
Packit Service 672cf4
                       gpgme_key_t key, const char *userid, unsigned int flags)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
Packit Service 6c01f9
  TRACE_BEG2 (DEBUG_CTX, "gpgme_op_adduid_start", ctx,
Packit Service 672cf4
	      "uid='%s' flags=0x%x", userid, flags);
Packit Service 672cf4
Packit Service 672cf4
  if (!ctx)
Packit Service 672cf4
    return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
Packit Service 672cf4
Packit Service 672cf4
  err = addrevuid_start (ctx, 0, 0, key, userid, flags);
Packit Service 672cf4
  return TRACE_ERR (err);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
gpgme_op_adduid (gpgme_ctx_t ctx,
Packit Service 672cf4
                 gpgme_key_t key, const char *userid, unsigned int flags)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
Packit Service 6c01f9
  TRACE_BEG2 (DEBUG_CTX, "gpgme_op_adduid", ctx,
Packit Service 672cf4
	      "uid='%s' flags=0x%x", userid, flags);
Packit Service 672cf4
Packit Service 672cf4
  if (!ctx)
Packit Service 672cf4
    return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
Packit Service 672cf4
Packit Service 672cf4
  err = addrevuid_start (ctx, 1, 0, key, userid, flags);
Packit Service 672cf4
  if (!err)
Packit Service 672cf4
    err = _gpgme_wait_one (ctx);
Packit Service 672cf4
  return TRACE_ERR (err);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* Revoke USERID from KEY.  */
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
gpgme_op_revuid_start (gpgme_ctx_t ctx,
Packit Service 672cf4
                       gpgme_key_t key, const char *userid, unsigned int flags)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
Packit Service 6c01f9
  TRACE_BEG2 (DEBUG_CTX, "gpgme_op_revuid_start", ctx,
Packit Service 672cf4
	      "uid='%s' flags=0x%x", userid, flags);
Packit Service 672cf4
Packit Service 672cf4
  if (!ctx)
Packit Service 672cf4
    return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
Packit Service 672cf4
Packit Service 672cf4
  err = addrevuid_start (ctx, 0, GENKEY_EXTRAFLAG_REVOKE, key, userid, flags);
Packit Service 672cf4
  return TRACE_ERR (err);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
gpgme_op_revuid (gpgme_ctx_t ctx,
Packit Service 672cf4
                 gpgme_key_t key, const char *userid, unsigned int flags)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
Packit Service 6c01f9
  TRACE_BEG2 (DEBUG_CTX, "gpgme_op_revuid", ctx,
Packit Service 672cf4
	      "uid='%s' flags=0x%x", userid, flags);
Packit Service 672cf4
Packit Service 672cf4
  if (!ctx)
Packit Service 672cf4
    return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
Packit Service 672cf4
Packit Service 672cf4
  err = addrevuid_start (ctx, 1, GENKEY_EXTRAFLAG_REVOKE, key, userid, flags);
Packit Service 672cf4
  if (!err)
Packit Service 672cf4
    err = _gpgme_wait_one (ctx);
Packit Service 672cf4
  return TRACE_ERR (err);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* Set a flag on the USERID of KEY.  The only supported flag right now
Packit Service 672cf4
 * is "primary" to mark the primary key.  */
Packit Service 672cf4
static gpg_error_t
Packit Service 672cf4
set_uid_flag (gpgme_ctx_t ctx, int synchronous,
Packit Service 672cf4
              gpgme_key_t key, const char *userid,
Packit Service 672cf4
              const char *name, const char *value)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
Packit Service 6c01f9
  TRACE_BEG4 (DEBUG_CTX, "gpgme_op_set_uid_flag", ctx,
Packit Service 672cf4
	      "%d uid='%s' '%s'='%s'", synchronous, userid, name, value);
Packit Service 672cf4
Packit Service 672cf4
  if (!ctx || !name || !key || !userid)
Packit Service 672cf4
    return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
Packit Service 672cf4
Packit Service 672cf4
  if (!strcmp (name, "primary"))
Packit Service 672cf4
    {
Packit Service 672cf4
      if (value)
Packit Service 672cf4
        err = gpg_error (GPG_ERR_INV_ARG);
Packit Service 672cf4
      else
Packit Service 672cf4
        err = addrevuid_start (ctx, synchronous,
Packit Service 672cf4
                               GENKEY_EXTRAFLAG_SETPRIMARY, key, userid, 0);
Packit Service 672cf4
    }
Packit Service 672cf4
  else
Packit Service 672cf4
    return err = gpg_error (GPG_ERR_UNKNOWN_NAME);
Packit Service 672cf4
Packit Service 672cf4
  if (synchronous && !err)
Packit Service 672cf4
    err = _gpgme_wait_one (ctx);
Packit Service 672cf4
  return TRACE_ERR (err);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* See set_uid_flag. */
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
gpgme_op_set_uid_flag_start (gpgme_ctx_t ctx,
Packit Service 672cf4
                             gpgme_key_t key, const char *userid,
Packit Service 672cf4
                             const char *name, const char *value)
Packit Service 672cf4
{
Packit Service 672cf4
  return set_uid_flag (ctx, 0, key, userid, name, value);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* See set_uid_flag.  This is the synchronous variant.  */
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
gpgme_op_set_uid_flag (gpgme_ctx_t ctx,
Packit Service 672cf4
                       gpgme_key_t key, const char *userid,
Packit Service 672cf4
                       const char *name, const char *value)
Packit Service 672cf4
{
Packit Service 672cf4
  return set_uid_flag (ctx, 1, key, userid, name, value);
Packit Service 672cf4
}