1 /* genkey.c - key generation
2 * Copyright (C) 2000 Werner Koch (dd9jn)
3 * Copyright (C) 2001 g10 Code GmbH
5 * This file is part of GPGME.
7 * GPGME is free software; you can redistribute it and/or modify
8 * it 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,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
33 genkey_status_handler ( GpgmeCtx ctx, GpgStatusCode code, char *args )
35 if ( code == STATUS_PROGRESS && *args ) {
36 if (ctx->progress_cb) {
38 int type=0, current=0, total=0;
40 if ( (p = strchr (args, ' ')) ) {
44 if ( (p = strchr (p+1, ' ')) ) {
48 if ( (p = strchr (p+1, ' ')) ) {
57 ctx->progress_cb ( ctx->progress_cb_value, args, type,
63 DEBUG2 ("genkey_status: code=%d args=`%s'\n", code, args );
64 /* FIXME: Need to do more */
71 * @parms: XML string with the key parameters
72 * @pubkey: Returns the public key
73 * @seckey: Returns the secret key
75 * Generate a new key and store the key in the default keyrings if
76 * both @pubkey and @seckey are NULL. If @pubkey and @seckey are
77 * given, the newly created key will be returned in these data
78 * objects. This function just starts the gheneration and does not
79 * wait for completion.
81 * Here is an example on how @parms should be formatted; for deatils
82 * see the file doc/DETAILS from the GnuPG distribution.
86 * <GnupgKeyParms format="internal">
91 * Name-Real: Joe Tester
92 * Name-Comment: with stupid passphrase
93 * Name-Email: joe@foo.bar
100 * Strings should be given in UTF-8 encoding. The format we support
101 * for now is only "internal". The content of the
102 * <GnupgKeyParms> container is passed verbatim to GnuPG.
103 * Control statements are not allowed.
105 * Return value: 0 for success or an error code
109 gpgme_op_genkey_start ( GpgmeCtx c, const char *parms,
110 GpgmeData pubkey, GpgmeData seckey )
114 const char *s, *s2, *sx;
116 fail_on_pending_request( c );
119 gpgme_data_release (c->help_data_1); c->help_data_1 = NULL;
121 /* create a process object */
122 _gpgme_gpg_release (c->gpg); c->gpg = NULL;
123 rc = _gpgme_gpg_new ( &c->gpg );
127 /* We need a special mechanism to get the fd of a pipe here, so
128 * that we can use this for the %pubring and %secring parameters.
129 * We don't have this yet, so we implement only the adding to the
130 * standard keyrings */
131 if ( pubkey || seckey ) {
132 rc = mk_error (Not_Implemented);
136 _gpgme_gpg_set_status_handler ( c->gpg, genkey_status_handler, c );
138 /* build the commandline */
139 _gpgme_gpg_add_arg ( c->gpg, "--gen-key" );
141 _gpgme_gpg_add_arg ( c->gpg, "--armor" );
142 for ( i=0; i < c->verbosity; i++ )
143 _gpgme_gpg_add_arg ( c->gpg, "--verbose" );
145 if ( !pubkey && !seckey )
146 ; /* okay: Add key to the keyrings */
148 || gpgme_data_get_type (pubkey) != GPGME_DATA_TYPE_NONE ) {
149 rc = mk_error (Invalid_Value);
153 || gpgme_data_get_type (seckey) != GPGME_DATA_TYPE_NONE ) {
154 rc = mk_error (Invalid_Value);
159 _gpgme_data_set_mode (pubkey, GPGME_DATA_MODE_IN );
160 _gpgme_data_set_mode (seckey, GPGME_DATA_MODE_IN );
161 /* need some more things here */
165 if ( (parms = strstr (parms, "<GnupgKeyParms "))
166 && (s = strchr (parms, '>'))
167 && (sx = strstr (parms, "format=\"internal\""))
169 && (s2 = strstr (s+1, "</GnupgKeyParms>")) ) {
170 /* fixme: check that there are no control statements inside */
171 rc = gpgme_data_new_from_mem ( &c->help_data_1, s+1, s2-s-1, 1 );
174 rc = mk_error (Invalid_Value);
179 _gpgme_data_set_mode (c->help_data_1, GPGME_DATA_MODE_OUT );
180 _gpgme_gpg_add_data (c->gpg, c->help_data_1, 0);
182 rc = _gpgme_gpg_spawn ( c->gpg, c );
187 _gpgme_gpg_release ( c->gpg ); c->gpg = NULL;
197 * @parms: XML string with the key parameters
198 * @pubkey: Returns the public key
199 * @seckey: Returns the secret key
201 * Generate a new key and store the key in the default keyrings if both
202 * @pubkey and @seckey are NULL. If @pubkey and @seckey are given, the newly
203 * created key will be returned in these data objects.
204 * See gpgme_op_genkey_start() for a description of @parms.
206 * Return value: 0 for success or an error code
209 gpgme_op_genkey( GpgmeCtx c, const char *parms,
210 GpgmeData pubkey, GpgmeData seckey )
212 int rc = gpgme_op_genkey_start ( c, parms, pubkey, seckey );