2003-04-24 Marcus Brinkmann <marcus@g10code.de>
[gpgme.git] / gpgme / genkey.c
1 /* genkey.c - Key generation.
2    Copyright (C) 2000 Werner Koch (dd9jn)
3    Copyright (C) 2001, 2002, 2003 g10 Code GmbH
4
5    This file is part of GPGME.
6  
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.
11  
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.
16  
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.  */
20
21 #if HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "util.h"
28 #include "context.h"
29 #include "ops.h"
30
31
32 struct genkey_result
33 {
34   int created_primary : 1;
35   int created_sub : 1;
36   char *fpr;
37 };
38 typedef struct genkey_result *GenKeyResult;
39
40 static void
41 release_genkey_result (void *hook)
42 {
43   GenKeyResult result = (GenKeyResult) hook;
44   
45   if (result->fpr)
46     free (result->fpr);
47 }
48
49
50 static GpgmeError
51 genkey_status_handler (GpgmeCtx ctx, GpgmeStatusCode code, char *args)
52 {
53   GenKeyResult result;
54   GpgmeError err = _gpgme_progress_status_handler (ctx, code, args);
55   if (err)
56     return err;
57
58   err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, (void **) &result,
59                                sizeof (*result), release_genkey_result);
60   if (err)
61     return err;
62
63   switch (code)
64     {
65     case GPGME_STATUS_KEY_CREATED:
66       if (args && *args)
67         {
68           if (*args == 'B' || *args == 'P')
69             result->created_primary = 1;
70           if (*args == 'B' || *args == 'S')
71             result->created_sub = 1;
72           if (args[1] == ' ')
73             {
74               if (result->fpr)
75                 free (result->fpr);
76               result->fpr = strdup (&args[2]);
77               if (!result->fpr)
78                 return GPGME_Out_Of_Core;
79             }
80         }
81       break;
82
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;
88       break;
89
90     default:
91       break;
92     }
93   return 0;
94 }
95
96
97 static GpgmeError
98 _gpgme_op_genkey_start (GpgmeCtx ctx, int synchronous, const char *parms,
99                         GpgmeData pubkey, GpgmeData seckey)
100 {
101   int err = 0;
102   const char *s, *s2, *sx;
103
104   err = _gpgme_op_reset (ctx, synchronous);
105   if (err)
106     goto leave;
107
108   gpgme_data_release (ctx->help_data_1);
109   ctx->help_data_1 = NULL;
110
111   if ((parms = strstr (parms, "<GnupgKeyParms ")) 
112       && (s = strchr (parms, '>'))
113       && (sx = strstr (parms, "format=\"internal\""))
114       && sx < s
115       && (s2 = strstr (s+1, "</GnupgKeyParms>")))
116     {
117       /* FIXME: Check that there are no control statements inside.  */
118       s++;  /* Skip '>'.  */
119       while (*s == '\n')
120         s++;
121       err = gpgme_data_new_from_mem (&ctx->help_data_1, s, s2-s, 1);
122     }
123   else 
124     err = GPGME_Invalid_Value;
125
126   if (err)
127     goto leave;
128
129   _gpgme_engine_set_status_handler (ctx->engine, genkey_status_handler, ctx);
130
131   err = _gpgme_engine_op_genkey (ctx->engine, ctx->help_data_1, ctx->use_armor,
132                                  pubkey, seckey);
133
134  leave:
135   if (err)
136     {
137       _gpgme_engine_release (ctx->engine);
138       ctx->engine = NULL;
139     }
140   return err;
141 }
142
143
144 /**
145  * gpgme_op_genkey:
146  * @c: the context
147  * @parms: XML string with the key parameters
148  * @pubkey: Returns the public key
149  * @seckey: Returns the secret key
150  * 
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.
156  *
157  * Here is an example on how @parms should be formatted; for deatils
158  * see the file doc/DETAILS from the GnuPG distribution.
159  *
160  * <literal>
161  * <![CDATA[
162  * <GnupgKeyParms format="internal">
163  * Key-Type: DSA
164  * Key-Length: 1024
165  * Subkey-Type: ELG-E
166  * Subkey-Length: 1024
167  * Name-Real: Joe Tester
168  * Name-Comment: with stupid passphrase
169  * Name-Email: joe@foo.bar
170  * Expire-Date: 0
171  * Passphrase: abc
172  * </GnupgKeyParms>
173  * ]]>
174  * </literal> 
175  *
176  * Strings should be given in UTF-8 encoding.  The format we support
177  * for now is only "internal".  The content of the
178  * &lt;GnupgKeyParms&gt; container is passed verbatim to GnuPG.
179  * Control statements are not allowed.
180  * 
181  * Return value: 0 for success or an error code
182  **/
183 GpgmeError
184 gpgme_op_genkey_start (GpgmeCtx ctx, const char *parms,
185                        GpgmeData pubkey, GpgmeData seckey)
186 {
187   return _gpgme_op_genkey_start (ctx, 0, parms, pubkey, seckey);
188 }
189
190
191 /**
192  * gpgme_op_genkey:
193  * @c: the context
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.
198  *
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.
203  * 
204  * Return value: 0 for success or an error code
205  **/
206 GpgmeError
207 gpgme_op_genkey (GpgmeCtx ctx, const char *parms,
208                  GpgmeData pubkey, GpgmeData seckey,
209                  char **fpr)
210 {
211   GpgmeError err = _gpgme_op_genkey_start (ctx, 1, parms, pubkey, seckey);
212   if (!err)
213     err = _gpgme_wait_one (ctx);
214   if (!err && fpr)
215     {
216       GenKeyResult result;
217
218       err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, (void **) &result,
219                                    -1, NULL);
220       if (err)
221         return err;
222
223       if (result && result->fpr)
224         {
225           *fpr = strdup (result->fpr);
226           if (!*fpr)
227             return GPGME_Out_Of_Core;
228         }
229       else
230         *fpr = NULL;
231     }
232   return err;
233 }