1 /* genkey.c - Key generation.
2 Copyright (C) 2000 Werner Koch (dd9jn)
3 Copyright (C) 2001, 2002, 2003 g10 Code GmbH
5 This file is part of GPGME.
7 GPGME is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GPGME is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GPGME; if not, write to the Free Software Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
34 int created_primary : 1;
38 typedef struct genkey_result *GenKeyResult;
41 release_genkey_result (void *hook)
43 GenKeyResult result = (GenKeyResult) hook;
51 genkey_status_handler (GpgmeCtx ctx, GpgmeStatusCode code, char *args)
54 GpgmeError err = _gpgme_progress_status_handler (ctx, code, args);
58 err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, (void **) &result,
59 sizeof (*result), release_genkey_result);
65 case GPGME_STATUS_KEY_CREATED:
68 if (*args == 'B' || *args == 'P')
69 result->created_primary = 1;
70 if (*args == 'B' || *args == 'S')
71 result->created_sub = 1;
76 result->fpr = strdup (&args[2]);
78 return GPGME_Out_Of_Core;
83 case GPGME_STATUS_EOF:
84 /* FIXME: Should return some more useful error value. */
85 if (!result->created_primary
86 && !result->created_sub)
87 return GPGME_General_Error;
98 _gpgme_op_genkey_start (GpgmeCtx ctx, int synchronous, const char *parms,
99 GpgmeData pubkey, GpgmeData seckey)
102 const char *s, *s2, *sx;
104 err = _gpgme_op_reset (ctx, synchronous);
108 gpgme_data_release (ctx->help_data_1);
109 ctx->help_data_1 = NULL;
111 if ((parms = strstr (parms, "<GnupgKeyParms "))
112 && (s = strchr (parms, '>'))
113 && (sx = strstr (parms, "format=\"internal\""))
115 && (s2 = strstr (s+1, "</GnupgKeyParms>")))
117 /* FIXME: Check that there are no control statements inside. */
121 err = gpgme_data_new_from_mem (&ctx->help_data_1, s, s2-s, 1);
124 err = GPGME_Invalid_Value;
129 _gpgme_engine_set_status_handler (ctx->engine, genkey_status_handler, ctx);
131 err = _gpgme_engine_op_genkey (ctx->engine, ctx->help_data_1, ctx->use_armor,
137 _gpgme_engine_release (ctx->engine);
147 * @parms: XML string with the key parameters
148 * @pubkey: Returns the public key
149 * @seckey: Returns the secret key
151 * Generate a new key and store the key in the default keyrings if
152 * both @pubkey and @seckey are NULL. If @pubkey and @seckey are
153 * given, the newly created key will be returned in these data
154 * objects. This function just starts the gheneration and does not
155 * wait for completion.
157 * Here is an example on how @parms should be formatted; for deatils
158 * see the file doc/DETAILS from the GnuPG distribution.
162 * <GnupgKeyParms format="internal">
166 * Subkey-Length: 1024
167 * Name-Real: Joe Tester
168 * Name-Comment: with stupid passphrase
169 * Name-Email: joe@foo.bar
176 * Strings should be given in UTF-8 encoding. The format we support
177 * for now is only "internal". The content of the
178 * <GnupgKeyParms> container is passed verbatim to GnuPG.
179 * Control statements are not allowed.
181 * Return value: 0 for success or an error code
184 gpgme_op_genkey_start (GpgmeCtx ctx, const char *parms,
185 GpgmeData pubkey, GpgmeData seckey)
187 return _gpgme_op_genkey_start (ctx, 0, parms, pubkey, seckey);
194 * @parms: XML string with the key parameters
195 * @pubkey: Returns the public key
196 * @seckey: Returns the secret key
197 * @fpr: Returns the fingerprint of the key.
199 * Generate a new key and store the key in the default keyrings if both
200 * @pubkey and @seckey are NULL. If @pubkey and @seckey are given, the newly
201 * created key will be returned in these data objects.
202 * See gpgme_op_genkey_start() for a description of @parms.
204 * Return value: 0 for success or an error code
207 gpgme_op_genkey (GpgmeCtx ctx, const char *parms,
208 GpgmeData pubkey, GpgmeData seckey,
211 GpgmeError err = _gpgme_op_genkey_start (ctx, 1, parms, pubkey, seckey);
213 err = _gpgme_wait_one (ctx);
218 err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, (void **) &result,
223 if (result && result->fpr)
225 *fpr = strdup (result->fpr);
227 return GPGME_Out_Of_Core;