Imported Upstream version 1.11.0
[platform/upstream/gpgme.git] / src / op-support.c
1 /* op-support.c - Supporting functions.
2    Copyright (C) 2002, 2003, 2004, 2007 g10 Code GmbH
3
4    This file is part of GPGME.
5
6    GPGME is free software; you can redistribute it and/or modify it
7    under the terms of the GNU Lesser General Public License as
8    published by the Free Software Foundation; either version 2.1 of
9    the License, or (at your option) any later version.
10
11    GPGME is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with this program; if not, see <https://www.gnu.org/licenses/>.
18  */
19
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <string.h>
26 #ifdef HAVE_LOCALE_H
27 #include <locale.h>
28 #endif
29
30 #include "gpgme.h"
31 #include "context.h"
32 #include "ops.h"
33 #include "util.h"
34 #include "debug.h"
35
36 #if GPG_ERROR_VERSION_NUMBER < 0x011700  /* 1.23 */
37 # define GPG_ERR_SUBKEYS_EXP_OR_REV 217
38 #endif
39
40
41 \f
42 gpgme_error_t
43 _gpgme_op_data_lookup (gpgme_ctx_t ctx, ctx_op_data_id_t type, void **hook,
44                        int size, void (*cleanup) (void *))
45 {
46   struct ctx_op_data *data;
47
48   if (!ctx)
49     return gpg_error (GPG_ERR_INV_VALUE);
50
51   data = ctx->op_data;
52   while (data && data->type != type)
53     data = data->next;
54   if (!data)
55     {
56       if (size < 0)
57         {
58           *hook = NULL;
59           return 0;
60         }
61
62       data = calloc (1, sizeof (struct ctx_op_data) + size);
63       if (!data)
64         return gpg_error_from_syserror ();
65       data->magic = CTX_OP_DATA_MAGIC;
66       data->next = ctx->op_data;
67       data->type = type;
68       data->cleanup = cleanup;
69       data->hook = (void *) (((char *) data) + sizeof (struct ctx_op_data));
70       data->references = 1;
71       ctx->op_data = data;
72     }
73   *hook = data->hook;
74   return 0;
75 }
76
77
78 /* type is: 0: asynchronous operation (use global or user event loop).
79             1: synchronous operation (always use private event loop).
80             2: asynchronous private operation (use private or user
81             event loop).
82             256: Modification flag to suppress the engine reset.
83 */
84 gpgme_error_t
85 _gpgme_op_reset (gpgme_ctx_t ctx, int type)
86 {
87   gpgme_error_t err = 0;
88   struct gpgme_io_cbs io_cbs;
89   int no_reset = (type & 256);
90   int reuse_engine = 0;
91
92   type &= 255;
93
94   _gpgme_release_result (ctx);
95   LOCK (ctx->lock);
96   ctx->canceled = 0;
97   ctx->redraw_suggested = 0;
98   UNLOCK (ctx->lock);
99
100   if (ctx->engine && no_reset)
101     reuse_engine = 1;
102   else if (ctx->engine)
103     {
104       /* Attempt to reset an existing engine.  */
105
106       err = _gpgme_engine_reset (ctx->engine);
107       if (gpg_err_code (err) == GPG_ERR_NOT_IMPLEMENTED)
108         {
109           _gpgme_engine_release (ctx->engine);
110           ctx->engine = NULL;
111         }
112     }
113
114   if (!ctx->engine)
115     {
116       gpgme_engine_info_t info;
117       info = ctx->engine_info;
118       while (info && info->protocol != ctx->protocol)
119         info = info->next;
120
121       if (!info)
122         return gpg_error (GPG_ERR_UNSUPPORTED_PROTOCOL);
123
124       /* Create an engine object.  */
125       err = _gpgme_engine_new (info, &ctx->engine);
126       if (err)
127         return err;
128     }
129
130   if (!reuse_engine)
131     {
132       err = 0;
133 #ifdef LC_CTYPE
134       err = _gpgme_engine_set_locale (ctx->engine, LC_CTYPE, ctx->lc_ctype);
135 #endif
136 #ifdef LC_MESSAGES
137       if (!err)
138         err = _gpgme_engine_set_locale (ctx->engine,
139                                         LC_MESSAGES, ctx->lc_messages);
140 #endif
141       if (gpg_err_code (err) == GPG_ERR_NOT_IMPLEMENTED)
142         err = 0;
143
144       _gpgme_engine_set_engine_flags (ctx->engine, ctx);
145
146       if (!err)
147         {
148           err = _gpgme_engine_set_pinentry_mode (ctx->engine,
149                                                  ctx->pinentry_mode);
150           if (gpg_err_code (err) == GPG_ERR_NOT_IMPLEMENTED)
151             err = 0;
152         }
153
154       if (!err && ctx->status_cb && ctx->full_status)
155         {
156           _gpgme_engine_set_status_cb (ctx->engine,
157                                        ctx->status_cb, ctx->status_cb_value);
158         }
159
160       if (err)
161         {
162           _gpgme_engine_release (ctx->engine);
163           ctx->engine = NULL;
164           return err;
165         }
166     }
167
168   if (ctx->sub_protocol != GPGME_PROTOCOL_DEFAULT)
169     {
170       err = _gpgme_engine_set_protocol (ctx->engine, ctx->sub_protocol);
171       if (err)
172         return err;
173     }
174
175   if (type == 1 || (type == 2 && !ctx->io_cbs.add))
176     {
177       /* Use private event loop.  */
178       io_cbs.add = _gpgme_add_io_cb;
179       io_cbs.add_priv = ctx;
180       io_cbs.remove = _gpgme_remove_io_cb;
181       io_cbs.event = _gpgme_wait_private_event_cb;
182       io_cbs.event_priv = ctx;
183     }
184   else if (! ctx->io_cbs.add)
185     {
186       /* Use global event loop.  */
187       io_cbs.add = _gpgme_add_io_cb;
188       io_cbs.add_priv = ctx;
189       io_cbs.remove = _gpgme_remove_io_cb;
190       io_cbs.event = _gpgme_wait_global_event_cb;
191       io_cbs.event_priv = ctx;
192     }
193   else
194     {
195       /* Use user event loop.  */
196       io_cbs.add = _gpgme_wait_user_add_io_cb;
197       io_cbs.add_priv = ctx;
198       io_cbs.remove = _gpgme_wait_user_remove_io_cb;
199       io_cbs.event = _gpgme_wait_user_event_cb;
200       io_cbs.event_priv = ctx;
201     }
202   _gpgme_engine_set_io_cbs (ctx->engine, &io_cbs);
203   return err;
204 }
205
206 \f
207 /* Parse the INV_RECP or INV_SNDR status line in ARGS and return the
208    result in KEY.  If KC_FPR (from the KEY_CONSIDERED status line) is
209    not NULL take the KC_FLAGS in account. */
210 gpgme_error_t
211 _gpgme_parse_inv_recp (char *args, int for_signing,
212                        const char *kc_fpr, unsigned int kc_flags,
213                        gpgme_invalid_key_t *key)
214 {
215   gpgme_invalid_key_t inv_key;
216   char *tail;
217   long int reason;
218
219   (void)for_signing;
220
221   inv_key = calloc (1, sizeof (*inv_key));
222   if (!inv_key)
223     return gpg_error_from_syserror ();
224   inv_key->next = NULL;
225   gpg_err_set_errno (0);
226   reason = strtol (args, &tail, 0);
227   if (errno || args == tail || (*tail && *tail != ' '))
228     {
229       /* The crypto backend does not behave.  */
230       free (inv_key);
231       return trace_gpg_error (GPG_ERR_INV_ENGINE);
232     }
233
234   switch (reason)
235     {
236     case 0:
237       if (kc_fpr && (kc_flags & 2))
238         inv_key->reason = gpg_error (GPG_ERR_SUBKEYS_EXP_OR_REV);
239       else
240         inv_key->reason = gpg_error (GPG_ERR_GENERAL);
241       break;
242
243     case 1:
244       inv_key->reason = gpg_error (GPG_ERR_NO_PUBKEY);
245       break;
246
247     case 2:
248       inv_key->reason = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
249       break;
250
251     case 3:
252       inv_key->reason = gpg_error (GPG_ERR_WRONG_KEY_USAGE);
253       break;
254
255     case 4:
256       inv_key->reason = gpg_error (GPG_ERR_CERT_REVOKED);
257       break;
258
259     case 5:
260       inv_key->reason = gpg_error (GPG_ERR_CERT_EXPIRED);
261       break;
262
263     case 6:
264       inv_key->reason = gpg_error (GPG_ERR_NO_CRL_KNOWN);
265       break;
266
267     case 7:
268       inv_key->reason = gpg_error (GPG_ERR_CRL_TOO_OLD);
269       break;
270
271     case 8:
272       inv_key->reason = gpg_error (GPG_ERR_NO_POLICY_MATCH);
273       break;
274
275     case 9:
276       inv_key->reason = gpg_error (GPG_ERR_NO_SECKEY);
277       break;
278
279     case 10:
280       inv_key->reason = gpg_error (GPG_ERR_PUBKEY_NOT_TRUSTED);
281       break;
282
283     case 11:
284       inv_key->reason = gpg_error (GPG_ERR_MISSING_CERT);
285       break;
286
287     case 12:
288       inv_key->reason = gpg_error (GPG_ERR_MISSING_ISSUER_CERT);
289       break;
290
291     case 13:
292       inv_key->reason = gpg_error (252); /*GPG_ERR_KEY_DISABLED*/
293       break;
294
295     case 14:
296       inv_key->reason = gpg_error (GPG_ERR_INV_USER_ID);
297       break;
298
299     default:
300       inv_key->reason = gpg_error (GPG_ERR_GENERAL);
301       break;
302     }
303
304   while (*tail && *tail == ' ')
305     tail++;
306   if (*tail)
307     {
308       inv_key->fpr = strdup (tail);
309       if (!inv_key->fpr)
310         {
311           free (inv_key);
312           return gpg_error_from_syserror ();
313         }
314     }
315
316   *key = inv_key;
317   return 0;
318 }
319
320
321 \f
322 /* Parse a KEY_CONSIDERED status line in ARGS and store the
323  * fingerprint and the flags at R_FPR and R_FLAGS.  The caller must
324  * free the value at R_FPR on success.  */
325 gpgme_error_t
326 _gpgme_parse_key_considered (const char *args,
327                              char **r_fpr, unsigned int *r_flags)
328 {
329   char *pend;
330   size_t n;
331
332   *r_fpr = NULL;
333
334   pend = strchr (args, ' ');
335   if (!pend || pend == args)
336     return trace_gpg_error (GPG_ERR_INV_ENGINE);  /* Bogus status line.  */
337   n = pend - args;
338   *r_fpr = malloc (n + 1);
339   if (!*r_fpr)
340     return gpg_error_from_syserror ();
341   memcpy (*r_fpr, args, n);
342   (*r_fpr)[n] = 0;
343   args = pend + 1;
344
345   gpg_err_set_errno (0);
346   *r_flags = strtoul (args, &pend, 0);
347   if (errno || args == pend || (*pend && *pend != ' '))
348     {
349       free (*r_fpr);
350       *r_fpr = NULL;
351       return trace_gpg_error (GPG_ERR_INV_ENGINE);
352     }
353
354   return 0;
355 }
356
357
358 /* Parse the PLAINTEXT status line in ARGS and return the result in
359    FILENAMEP.  */
360 gpgme_error_t
361 _gpgme_parse_plaintext (char *args, char **filenamep, int *r_mime)
362 {
363   char *tail;
364
365   while (*args == ' ')
366     args++;
367   if (*args == '\0')
368     return 0;
369
370   /* First argument is file type (a one byte uppercase hex value).  */
371   if (args[0] == '6' && args[1] == 'D')
372     *r_mime = 1;
373   while (*args != ' ' && *args != '\0')
374     args++;
375   while (*args == ' ')
376     args++;
377   if (*args == '\0')
378     return 0;
379
380   /* Second argument is the timestamp.  */
381   while (*args != ' ' && *args != '\0')
382     args++;
383   while (*args == ' ')
384     args++;
385   if (*args == '\0')
386     return 0;
387
388   tail = args;
389   while (*tail != ' ' && *tail != '\0')
390     tail++;
391   *tail = '\0';
392   if (filenamep && *args != '\0')
393     {
394       char *filename = strdup (args);
395       if (!filename)
396         return gpg_error_from_syserror ();
397
398       *filenamep = filename;
399     }
400   return 0;
401 }
402
403
404 /* Parse a FAILURE status line and return the error code.  ARGS is
405  * modified to contain the location part.  Note that for now we ignore
406  * failure codes with a location of gpg-exit; they are too trouble
407  * some.  Instead we should eventually record that error in the
408  * context and provide a function to return a fuller error
409  * description; this could then also show the location of the error
410  * (e.g. "option- parser") to make it easier for the user to detect
411  * the actual error. */
412 gpgme_error_t
413 _gpgme_parse_failure (char *args)
414 {
415   char *where, *which;
416
417   where = strchr (args, ' ');
418   if (!where)
419     return trace_gpg_error (GPG_ERR_INV_ENGINE);
420
421   *where = '\0';
422   which = where + 1;
423
424   where = strchr (which, ' ');
425   if (where)
426     *where = '\0';
427
428   where = args;
429   if (!strcmp (where, "gpg-exit"))
430     return 0;
431
432   return atoi (which);
433 }