Check context pointers for null pointer on entry points.
[gpgme.git] / src / genkey.c
1 /* genkey.c - Key generation.
2    Copyright (C) 2000 Werner Koch (dd9jn)
3    Copyright (C) 2001, 2002, 2003, 2004 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 Lesser General Public License as
9    published by the Free Software Foundation; either version 2.1 of
10    the License, or (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    Lesser General Public License for more details.
16    
17    You should have received a copy of the GNU Lesser General Public
18    License along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 #if HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28
29 #include "gpgme.h"
30 #include "debug.h"
31 #include "context.h"
32 #include "ops.h"
33 #include "util.h"
34
35 \f
36 typedef struct
37 {
38   struct _gpgme_op_genkey_result result;
39
40   /* The key parameters passed to the crypto engine.  */
41   gpgme_data_t key_parameter;
42 } *op_data_t;
43
44
45 static void
46 release_op_data (void *hook)
47 {
48   op_data_t opd = (op_data_t) hook;
49   
50   if (opd->result.fpr)
51     free (opd->result.fpr);
52   if (opd->key_parameter)
53     gpgme_data_release (opd->key_parameter);
54 }
55
56
57 gpgme_genkey_result_t
58 gpgme_op_genkey_result (gpgme_ctx_t ctx)
59 {
60   void *hook;
61   op_data_t opd;
62   gpgme_error_t err;
63
64   TRACE_BEG (DEBUG_CTX, "gpgme_op_genkey_result", ctx);
65
66   err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook, -1, NULL);
67   opd = hook;
68   if (err || !opd)
69     {
70       TRACE_SUC0 ("result=(null)");
71       return NULL;
72     }
73
74   TRACE_LOG3 ("fpr = %s, %s, %s", opd->result.fpr,
75               opd->result.primary ? "primary" : "no primary",
76               opd->result.sub ? "sub" : "no sub");
77
78   TRACE_SUC1 ("result=%p", &opd->result);
79   return &opd->result;
80 }
81
82 \f
83 static gpgme_error_t
84 genkey_status_handler (void *priv, gpgme_status_code_t code, char *args)
85 {
86   gpgme_ctx_t ctx = (gpgme_ctx_t) priv;
87   gpgme_error_t err;
88   void *hook;
89   op_data_t opd;
90
91   /* Pipe the status code through the progress status handler.  */
92   err = _gpgme_progress_status_handler (ctx, code, args);
93   if (err)
94     return err;
95
96   err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook, -1, NULL);
97   opd = hook;
98   if (err)
99     return err;
100
101   switch (code)
102     {
103     case GPGME_STATUS_KEY_CREATED:
104       if (args && *args)
105         {
106           if (*args == 'B' || *args == 'P')
107             opd->result.primary = 1;
108           if (*args == 'B' || *args == 'S')
109             opd->result.sub = 1;
110           if (args[1] == ' ')
111             {
112               if (opd->result.fpr)
113                 free (opd->result.fpr);
114               opd->result.fpr = strdup (&args[2]);
115               if (!opd->result.fpr)
116                 return gpg_error_from_errno (errno);
117             }
118         }
119       break;
120
121     case GPGME_STATUS_EOF:
122       /* FIXME: Should return some more useful error value.  */
123       if (!opd->result.primary && !opd->result.sub)
124         return gpg_error (GPG_ERR_GENERAL);
125       break;
126
127     default:
128       break;
129     }
130   return 0;
131 }
132
133
134 static gpgme_error_t
135 get_key_parameter (const char *parms, gpgme_data_t *key_parameter)
136 {
137   const char *content;
138   const char *attrib;
139   const char *endtag;
140
141   /* Extract the key parameter from the XML structure.  */
142   parms = strstr (parms, "<GnupgKeyParms ");
143   if (!parms)
144     return gpg_error (GPG_ERR_INV_VALUE);
145
146   content = strchr (parms, '>');
147   if (!content)
148     return gpg_error (GPG_ERR_INV_VALUE);
149   content++;
150
151   attrib = strstr (parms, "format=\"internal\"");
152   if (!attrib || attrib >= content)
153     return gpg_error (GPG_ERR_INV_VALUE);
154
155   endtag = strstr (content, "</GnupgKeyParms>");
156   /* FIXME: Check that there are no control statements inside.  */
157   while (content[0] == '\n'
158          || (content[0] == '\r' && content[1] == '\n'))
159     content++;
160
161   return gpgme_data_new_from_mem (key_parameter, content,
162                                   endtag - content, 1);
163 }
164
165
166 static gpgme_error_t
167 genkey_start (gpgme_ctx_t ctx, int synchronous, const char *parms,
168               gpgme_data_t pubkey, gpgme_data_t seckey)
169 {
170   gpgme_error_t err;
171   void *hook;
172   op_data_t opd;
173   err = _gpgme_op_reset (ctx, synchronous);
174   if (err)
175     return err;
176   
177   err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook,
178                                sizeof (*opd), release_op_data);
179   opd = hook;
180   if (err)
181     return err;
182
183   err = get_key_parameter (parms, &opd->key_parameter);
184   if (err)
185     return err;
186
187   _gpgme_engine_set_status_handler (ctx->engine, genkey_status_handler, ctx);
188
189   return _gpgme_engine_op_genkey (ctx->engine, opd->key_parameter,
190                                   ctx->use_armor, pubkey, seckey);
191 }
192
193
194 /* Generate a new keypair and add it to the keyring.  PUBKEY and
195    SECKEY should be null for now.  PARMS specifies what keys should be
196    generated.  */
197 gpgme_error_t
198 gpgme_op_genkey_start (gpgme_ctx_t ctx, const char *parms,
199                        gpgme_data_t pubkey, gpgme_data_t seckey)
200 {
201   gpgme_error_t err;
202
203   TRACE_BEG2 (DEBUG_CTX, "gpgme_op_genkey_start", ctx,
204               "pubkey=%p, seckey=%p", pubkey, seckey);
205   TRACE_LOGBUF (parms, strlen (parms));
206
207   if (!ctx)
208     return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
209
210   err = genkey_start (ctx, 0, parms, pubkey, seckey);
211   return TRACE_ERR (err);
212 }
213
214
215 /* Generate a new keypair and add it to the keyring.  PUBKEY and
216    SECKEY should be null for now.  PARMS specifies what keys should be
217    generated.  */
218 gpgme_error_t
219 gpgme_op_genkey (gpgme_ctx_t ctx, const char *parms, gpgme_data_t pubkey,
220                  gpgme_data_t seckey)
221 {
222   gpgme_error_t err;
223
224   TRACE_BEG2 (DEBUG_CTX, "gpgme_op_genkey", ctx,
225               "pubkey=%p, seckey=%p", pubkey, seckey);
226   TRACE_LOGBUF (parms, strlen (parms));
227
228   if (!ctx)
229     return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
230
231   err = genkey_start (ctx, 1, parms, pubkey, seckey);
232   if (!err)
233     err = _gpgme_wait_one (ctx);
234   return TRACE_ERR (err);
235 }