1 /* op-support.c - Supporting functions.
2 Copyright (C) 2002, 2003, 2004, 2007 g10 Code GmbH
4 This file is part of GPGME.
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.
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.
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/>.
36 #if GPG_ERROR_VERSION_NUMBER < 0x011700 /* 1.23 */
37 # define GPG_ERR_SUBKEYS_EXP_OR_REV 217
43 _gpgme_op_data_lookup (gpgme_ctx_t ctx, ctx_op_data_id_t type, void **hook,
44 int size, void (*cleanup) (void *))
46 struct ctx_op_data *data;
49 return gpg_error (GPG_ERR_INV_VALUE);
52 while (data && data->type != type)
62 data = calloc (1, sizeof (struct ctx_op_data) + size);
64 return gpg_error_from_syserror ();
65 data->magic = CTX_OP_DATA_MAGIC;
66 data->next = ctx->op_data;
68 data->cleanup = cleanup;
69 data->hook = (void *) (((char *) data) + sizeof (struct ctx_op_data));
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
82 256: Modification flag to suppress the engine reset.
85 _gpgme_op_reset (gpgme_ctx_t ctx, int type)
87 gpgme_error_t err = 0;
88 struct gpgme_io_cbs io_cbs;
89 int no_reset = (type & 256);
94 _gpgme_release_result (ctx);
97 ctx->redraw_suggested = 0;
100 if (ctx->engine && no_reset)
102 else if (ctx->engine)
104 /* Attempt to reset an existing engine. */
106 err = _gpgme_engine_reset (ctx->engine);
107 if (gpg_err_code (err) == GPG_ERR_NOT_IMPLEMENTED)
109 _gpgme_engine_release (ctx->engine);
116 gpgme_engine_info_t info;
117 info = ctx->engine_info;
118 while (info && info->protocol != ctx->protocol)
122 return gpg_error (GPG_ERR_UNSUPPORTED_PROTOCOL);
124 /* Create an engine object. */
125 err = _gpgme_engine_new (info, &ctx->engine);
134 err = _gpgme_engine_set_locale (ctx->engine, LC_CTYPE, ctx->lc_ctype);
138 err = _gpgme_engine_set_locale (ctx->engine,
139 LC_MESSAGES, ctx->lc_messages);
141 if (gpg_err_code (err) == GPG_ERR_NOT_IMPLEMENTED)
144 _gpgme_engine_set_engine_flags (ctx->engine, ctx);
148 err = _gpgme_engine_set_pinentry_mode (ctx->engine,
150 if (gpg_err_code (err) == GPG_ERR_NOT_IMPLEMENTED)
154 if (!err && ctx->status_cb && ctx->full_status)
156 _gpgme_engine_set_status_cb (ctx->engine,
157 ctx->status_cb, ctx->status_cb_value);
162 _gpgme_engine_release (ctx->engine);
168 if (ctx->sub_protocol != GPGME_PROTOCOL_DEFAULT)
170 err = _gpgme_engine_set_protocol (ctx->engine, ctx->sub_protocol);
175 if (type == 1 || (type == 2 && !ctx->io_cbs.add))
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;
184 else if (! ctx->io_cbs.add)
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;
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;
202 _gpgme_engine_set_io_cbs (ctx->engine, &io_cbs);
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. */
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)
215 gpgme_invalid_key_t inv_key;
221 inv_key = calloc (1, sizeof (*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 != ' '))
229 /* The crypto backend does not behave. */
231 return trace_gpg_error (GPG_ERR_INV_ENGINE);
237 if (kc_fpr && (kc_flags & 2))
238 inv_key->reason = gpg_error (GPG_ERR_SUBKEYS_EXP_OR_REV);
240 inv_key->reason = gpg_error (GPG_ERR_GENERAL);
244 inv_key->reason = gpg_error (GPG_ERR_NO_PUBKEY);
248 inv_key->reason = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
252 inv_key->reason = gpg_error (GPG_ERR_WRONG_KEY_USAGE);
256 inv_key->reason = gpg_error (GPG_ERR_CERT_REVOKED);
260 inv_key->reason = gpg_error (GPG_ERR_CERT_EXPIRED);
264 inv_key->reason = gpg_error (GPG_ERR_NO_CRL_KNOWN);
268 inv_key->reason = gpg_error (GPG_ERR_CRL_TOO_OLD);
272 inv_key->reason = gpg_error (GPG_ERR_NO_POLICY_MATCH);
276 inv_key->reason = gpg_error (GPG_ERR_NO_SECKEY);
280 inv_key->reason = gpg_error (GPG_ERR_PUBKEY_NOT_TRUSTED);
284 inv_key->reason = gpg_error (GPG_ERR_MISSING_CERT);
288 inv_key->reason = gpg_error (GPG_ERR_MISSING_ISSUER_CERT);
292 inv_key->reason = gpg_error (252); /*GPG_ERR_KEY_DISABLED*/
296 inv_key->reason = gpg_error (GPG_ERR_INV_USER_ID);
300 inv_key->reason = gpg_error (GPG_ERR_GENERAL);
304 while (*tail && *tail == ' ')
308 inv_key->fpr = strdup (tail);
312 return gpg_error_from_syserror ();
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. */
326 _gpgme_parse_key_considered (const char *args,
327 char **r_fpr, unsigned int *r_flags)
334 pend = strchr (args, ' ');
335 if (!pend || pend == args)
336 return trace_gpg_error (GPG_ERR_INV_ENGINE); /* Bogus status line. */
338 *r_fpr = malloc (n + 1);
340 return gpg_error_from_syserror ();
341 memcpy (*r_fpr, args, n);
345 gpg_err_set_errno (0);
346 *r_flags = strtoul (args, &pend, 0);
347 if (errno || args == pend || (*pend && *pend != ' '))
351 return trace_gpg_error (GPG_ERR_INV_ENGINE);
358 /* Parse the PLAINTEXT status line in ARGS and return the result in
361 _gpgme_parse_plaintext (char *args, char **filenamep, int *r_mime)
370 /* First argument is file type (a one byte uppercase hex value). */
371 if (args[0] == '6' && args[1] == 'D')
373 while (*args != ' ' && *args != '\0')
380 /* Second argument is the timestamp. */
381 while (*args != ' ' && *args != '\0')
389 while (*tail != ' ' && *tail != '\0')
392 if (filenamep && *args != '\0')
394 char *filename = strdup (args);
396 return gpg_error_from_syserror ();
398 *filenamep = filename;
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. */
413 _gpgme_parse_failure (char *args)
417 where = strchr (args, ' ');
419 return trace_gpg_error (GPG_ERR_INV_ENGINE);
424 where = strchr (which, ' ');
429 if (!strcmp (where, "gpg-exit"))