1 /* gpgme.c - GnuPG Made Easy.
2 Copyright (C) 2000 Werner Koch (dd9jn)
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007 g10 Code GmbH
5 This file is part of GPGME.
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.
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.
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
39 /* The default locale. */
40 DEFINE_STATIC_LOCK (def_lc_lock);
41 static char *def_lc_ctype;
42 static char *def_lc_messages;
45 /* Create a new context as an environment for GPGME crypto
48 gpgme_new (gpgme_ctx_t *r_ctx)
51 TRACE_BEG (DEBUG_CTX, "gpgme_new", r_ctx);
53 ctx = calloc (1, sizeof *ctx);
55 return TRACE_ERR (gpg_error_from_errno (errno));
57 INIT_LOCK (ctx->lock);
59 _gpgme_engine_info_copy (&ctx->engine_info);
60 if (!ctx->engine_info)
63 return TRACE_ERR (gpg_error_from_errno (errno));
66 ctx->keylist_mode = GPGME_KEYLIST_MODE_LOCAL;
67 ctx->include_certs = GPGME_INCLUDE_CERTS_DEFAULT;
68 ctx->protocol = GPGME_PROTOCOL_OpenPGP;
69 _gpgme_fd_table_init (&ctx->fdt);
74 ctx->lc_ctype = strdup (def_lc_ctype);
78 _gpgme_engine_info_release (ctx->engine_info);
80 return TRACE_ERR (gpg_error_from_errno (errno));
88 ctx->lc_messages = strdup (def_lc_messages);
89 if (!ctx->lc_messages)
94 _gpgme_engine_info_release (ctx->engine_info);
96 return TRACE_ERR (gpg_error_from_errno (errno));
100 def_lc_messages = NULL;
101 UNLOCK (def_lc_lock);
105 return TRACE_SUC1 ("ctx=%p", ctx);
110 _gpgme_cancel_with_err (gpgme_ctx_t ctx, gpg_error_t ctx_err)
113 TRACE_BEG1 (DEBUG_CTX, "_gpgme_cancel_with_err", ctx, "ctx_err=%i",
116 err = _gpgme_engine_cancel (ctx->engine);
118 return TRACE_ERR (err);
120 _gpgme_engine_io_event (ctx->engine, GPGME_EVENT_DONE, &ctx_err);
122 return TRACE_ERR (0);
126 /* Cancel a pending asynchronous operation. */
128 gpgme_cancel (gpgme_ctx_t ctx)
130 return _gpgme_cancel_with_err (ctx, gpg_error (GPG_ERR_CANCELED));
134 /* Cancel a pending operation asynchronously. */
136 gpgme_cancel_async (gpgme_ctx_t ctx)
138 TRACE_BEG (DEBUG_CTX, "gpgme_cancel_async", ctx);
144 return TRACE_ERR (0);
148 /* Release all resources associated with the given context. */
150 gpgme_release (gpgme_ctx_t ctx)
152 TRACE (DEBUG_CTX, "gpgme_release", ctx);
154 _gpgme_engine_release (ctx->engine);
155 _gpgme_fd_table_deinit (&ctx->fdt);
156 _gpgme_release_result (ctx);
157 gpgme_signers_clear (ctx);
158 gpgme_sig_notation_clear (ctx);
162 free (ctx->lc_ctype);
163 if (ctx->lc_messages)
164 free (ctx->lc_messages);
165 _gpgme_engine_info_release (ctx->engine_info);
166 DESTROY_LOCK (ctx->lock);
172 _gpgme_release_result (gpgme_ctx_t ctx)
174 struct ctx_op_data *data = ctx->op_data;
178 struct ctx_op_data *next_data = data->next;
180 (*data->cleanup) (data->hook);
189 gpgme_set_protocol (gpgme_ctx_t ctx, gpgme_protocol_t protocol)
191 TRACE_BEG2 (DEBUG_CTX, "gpgme_set_protocol", ctx, "protocol=%i (%s)",
192 protocol, gpgme_get_protocol_name (protocol)
193 ? gpgme_get_protocol_name (protocol) : "unknown");
195 if (protocol != GPGME_PROTOCOL_OpenPGP && protocol != GPGME_PROTOCOL_CMS)
196 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
198 if (ctx->protocol != protocol)
200 /* Shut down the engine when switching protocols. */
203 TRACE_LOG1 ("releasing ctx->engine=%p", ctx->engine);
204 _gpgme_engine_release (ctx->engine);
208 ctx->protocol = protocol;
210 return TRACE_ERR (0);
215 gpgme_get_protocol (gpgme_ctx_t ctx)
217 TRACE2 (DEBUG_CTX, "gpgme_get_protocol", ctx,
218 "ctx->protocol=%i (%s)", ctx->protocol,
219 gpgme_get_protocol_name (ctx->protocol)
220 ? gpgme_get_protocol_name (ctx->protocol) : "unknown");
221 return ctx->protocol;
226 gpgme_get_protocol_name (gpgme_protocol_t protocol)
230 case GPGME_PROTOCOL_OpenPGP:
233 case GPGME_PROTOCOL_CMS:
236 case GPGME_PROTOCOL_UNKNOWN:
244 /* Enable or disable the use of an ascii armor for all output. */
246 gpgme_set_armor (gpgme_ctx_t ctx, int use_armor)
248 TRACE2 (DEBUG_CTX, "gpgme_set_armor", ctx, "use_armor=%i (%s)",
249 use_armor, use_armor ? "yes" : "no");
250 ctx->use_armor = use_armor;
254 /* Return the state of the armor flag. */
256 gpgme_get_armor (gpgme_ctx_t ctx)
258 TRACE2 (DEBUG_CTX, "gpgme_get_armor", ctx, "ctx->use_armor=%i (%s)",
259 ctx->use_armor, ctx->use_armor ? "yes" : "no");
260 return ctx->use_armor;
264 /* Enable or disable the use of the special textmode. Textmode is for
265 example used for the RFC2015 signatures; note that the updated RFC
266 3156 mandates that the MUA does some preparations so that textmode
267 is not needed anymore. */
269 gpgme_set_textmode (gpgme_ctx_t ctx, int use_textmode)
271 TRACE2 (DEBUG_CTX, "gpgme_set_textmode", ctx, "use_textmode=%i (%s)",
272 use_textmode, use_textmode ? "yes" : "no");
273 ctx->use_textmode = use_textmode;
276 /* Return the state of the textmode flag. */
278 gpgme_get_textmode (gpgme_ctx_t ctx)
280 TRACE2 (DEBUG_CTX, "gpgme_get_textmode", ctx, "ctx->use_textmode=%i (%s)",
281 ctx->use_textmode, ctx->use_textmode ? "yes" : "no");
282 return ctx->use_textmode;
286 /* Set the number of certifications to include in an S/MIME message.
287 The default is GPGME_INCLUDE_CERTS_DEFAULT. -1 means all certs,
288 and -2 means all certs except the root cert. */
290 gpgme_set_include_certs (gpgme_ctx_t ctx, int nr_of_certs)
292 if (nr_of_certs == GPGME_INCLUDE_CERTS_DEFAULT)
293 ctx->include_certs = GPGME_INCLUDE_CERTS_DEFAULT;
294 else if (nr_of_certs < -2)
295 ctx->include_certs = -2;
297 ctx->include_certs = nr_of_certs;
299 TRACE2 (DEBUG_CTX, "gpgme_set_include_certs", ctx, "nr_of_certs=%i%s",
300 nr_of_certs, nr_of_certs == ctx->include_certs ? "" : " (-2)");
304 /* Get the number of certifications to include in an S/MIME
307 gpgme_get_include_certs (gpgme_ctx_t ctx)
309 TRACE1 (DEBUG_CTX, "gpgme_get_include_certs", ctx, "ctx->include_certs=%i",
311 return ctx->include_certs;
315 /* This function changes the default behaviour of the keylisting
316 functions. MODE is a bitwise-OR of the GPGME_KEYLIST_* flags. The
317 default mode is GPGME_KEYLIST_MODE_LOCAL. */
319 gpgme_set_keylist_mode (gpgme_ctx_t ctx, gpgme_keylist_mode_t mode)
321 TRACE1 (DEBUG_CTX, "gpgme_set_keylist_mode", ctx, "keylist_mode=0x%x",
324 ctx->keylist_mode = mode;
328 /* This function returns the default behaviour of the keylisting
331 gpgme_get_keylist_mode (gpgme_ctx_t ctx)
333 TRACE1 (DEBUG_CTX, "gpgme_get_keylist_mode", ctx,
334 "ctx->keylist_mode=0x%x", ctx->keylist_mode);
335 return ctx->keylist_mode;
339 /* This function sets a callback function to be used to pass a
340 passphrase to gpg. */
342 gpgme_set_passphrase_cb (gpgme_ctx_t ctx, gpgme_passphrase_cb_t cb,
345 TRACE2 (DEBUG_CTX, "gpgme_set_passphrase_cb", ctx,
346 "passphrase_cb=%p/%p", cb, cb_value);
347 ctx->passphrase_cb = cb;
348 ctx->passphrase_cb_value = cb_value;
352 /* This function returns the callback function to be used to pass a
353 passphrase to the crypto engine. */
355 gpgme_get_passphrase_cb (gpgme_ctx_t ctx, gpgme_passphrase_cb_t *r_cb,
358 TRACE2 (DEBUG_CTX, "gpgme_get_passphrase_cb", ctx,
359 "ctx->passphrase_cb=%p/%p",
360 ctx->passphrase_cb, ctx->passphrase_cb_value);
362 *r_cb = ctx->passphrase_cb;
364 *r_cb_value = ctx->passphrase_cb_value;
368 /* This function sets a callback function to be used as a progress
371 gpgme_set_progress_cb (gpgme_ctx_t ctx, gpgme_progress_cb_t cb, void *cb_value)
373 TRACE2 (DEBUG_CTX, "gpgme_set_progress_cb", ctx, "progress_cb=%p/%p",
375 ctx->progress_cb = cb;
376 ctx->progress_cb_value = cb_value;
380 /* This function returns the callback function to be used as a
381 progress indicator. */
383 gpgme_get_progress_cb (gpgme_ctx_t ctx, gpgme_progress_cb_t *r_cb,
386 TRACE2 (DEBUG_CTX, "gpgme_get_progress_cb", ctx, "ctx->progress_cb=%p/%p",
387 ctx->progress_cb, ctx->progress_cb_value);
389 *r_cb = ctx->progress_cb;
391 *r_cb_value = ctx->progress_cb_value;
395 /* Set the I/O callback functions for CTX to IO_CBS. */
397 gpgme_set_io_cbs (gpgme_ctx_t ctx, gpgme_io_cbs_t io_cbs)
401 TRACE6 (DEBUG_CTX, "gpgme_set_io_cbs", ctx,
402 "io_cbs=%p (add=%p/%p, remove=%p, event=%p/%p",
403 io_cbs, io_cbs->add, io_cbs->add_priv, io_cbs->remove,
404 io_cbs->event, io_cbs->event_priv);
405 ctx->io_cbs = *io_cbs;
409 TRACE1 (DEBUG_CTX, "gpgme_set_io_cbs", ctx,
410 "io_cbs=%p (default)", io_cbs);
411 ctx->io_cbs.add = NULL;
412 ctx->io_cbs.add_priv = NULL;
413 ctx->io_cbs.remove = NULL;
414 ctx->io_cbs.event = NULL;
415 ctx->io_cbs.event_priv = NULL;
420 /* This function returns the callback function for I/O. */
422 gpgme_get_io_cbs (gpgme_ctx_t ctx, gpgme_io_cbs_t io_cbs)
424 TRACE6 (DEBUG_CTX, "gpgme_get_io_cbs", ctx,
425 "io_cbs=%p, ctx->io_cbs.add=%p/%p, .remove=%p, .event=%p/%p",
426 io_cbs, io_cbs->add, io_cbs->add_priv, io_cbs->remove,
427 io_cbs->event, io_cbs->event_priv);
429 *io_cbs = ctx->io_cbs;
433 /* This function sets the locale for the context CTX, or the default
434 locale if CTX is a null pointer. */
436 gpgme_set_locale (gpgme_ctx_t ctx, int category, const char *value)
439 char *new_lc_ctype = NULL;
440 char *new_lc_messages = NULL;
442 TRACE_BEG2 (DEBUG_CTX, "gpgme_set_locale", ctx,
443 "category=%i, value=%s", category, value ? value : "(null)");
445 #define PREPARE_ONE_LOCALE(lcat, ucat) \
446 if (!failed && value \
447 && (category == LC_ALL || category == LC_ ## ucat)) \
449 new_lc_ ## lcat = strdup (value); \
450 if (!new_lc_ ## lcat) \
454 PREPARE_ONE_LOCALE (ctype, CTYPE);
456 PREPARE_ONE_LOCALE (messages, MESSAGES);
461 int saved_errno = errno;
466 free (new_lc_messages);
468 return TRACE_ERR (gpg_error_from_errno (saved_errno));
471 #define SET_ONE_LOCALE(lcat, ucat) \
472 if (category == LC_ALL || category == LC_ ## ucat) \
476 if (ctx->lc_ ## lcat) \
477 free (ctx->lc_ ## lcat); \
478 ctx->lc_ ## lcat = new_lc_ ## lcat; \
482 if (def_lc_ ## lcat) \
483 free (def_lc_ ## lcat); \
484 def_lc_ ## lcat = new_lc_ ## lcat; \
490 SET_ONE_LOCALE (ctype, CTYPE);
492 SET_ONE_LOCALE (messages, MESSAGES);
495 UNLOCK (def_lc_lock);
497 return TRACE_ERR (0);
501 /* Get the information about the configured engines. A pointer to the
502 first engine in the statically allocated linked list is returned.
503 The returned data is valid until the next gpgme_ctx_set_engine_info. */
505 gpgme_ctx_get_engine_info (gpgme_ctx_t ctx)
507 TRACE1 (DEBUG_CTX, "gpgme_ctx_get_engine_info", ctx,
508 "ctx->engine_info=%p", ctx->engine_info);
509 return ctx->engine_info;
513 /* Set the engine info for the context CTX, protocol PROTO, to the
514 file name FILE_NAME and the home directory HOME_DIR. */
516 gpgme_ctx_set_engine_info (gpgme_ctx_t ctx, gpgme_protocol_t proto,
517 const char *file_name, const char *home_dir)
520 TRACE_BEG4 (DEBUG_CTX, "gpgme_ctx_set_engine_info", ctx,
521 "protocol=%i (%s), file_name=%s, home_dir=%s",
522 proto, gpgme_get_protocol_name (proto)
523 ? gpgme_get_protocol_name (proto) : "unknown",
524 file_name ? file_name : "(default)",
525 home_dir ? home_dir : "(default)");
527 /* Shut down the engine when changing engine info. */
530 TRACE_LOG1 ("releasing ctx->engine=%p", ctx->engine);
531 _gpgme_engine_release (ctx->engine);
534 err = _gpgme_set_engine_info (ctx->engine_info, proto,
535 file_name, home_dir);
536 return TRACE_ERR (err);
540 /* Clear all notation data from the context. */
542 gpgme_sig_notation_clear (gpgme_ctx_t ctx)
544 gpgme_sig_notation_t notation;
545 TRACE (DEBUG_CTX, "gpgme_sig_notation_clear", ctx);
550 notation = ctx->sig_notations;
553 gpgme_sig_notation_t next_notation = notation->next;
554 _gpgme_sig_notation_free (notation);
555 notation = next_notation;
560 /* Add the human-readable notation data with name NAME and value VALUE
561 to the context CTX, using the flags FLAGS. If NAME is NULL, then
562 VALUE should be a policy URL. The flag
563 GPGME_SIG_NOTATION_HUMAN_READABLE is forced to be true for notation
564 data, and false for policy URLs. */
566 gpgme_sig_notation_add (gpgme_ctx_t ctx, const char *name,
567 const char *value, gpgme_sig_notation_flags_t flags)
570 gpgme_sig_notation_t notation;
571 gpgme_sig_notation_t *lastp;
573 TRACE_BEG3 (DEBUG_CTX, "gpgme_sig_notation_add", ctx,
574 "name=%s, value=%s, flags=0x%x",
575 name ? name : "(null)", value ? value : "(null)",
579 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
582 flags |= GPGME_SIG_NOTATION_HUMAN_READABLE;
584 flags &= ~GPGME_SIG_NOTATION_HUMAN_READABLE;
586 err = _gpgme_sig_notation_create (¬ation, name, name ? strlen (name) : 0,
587 value, value ? strlen (value) : 0, flags);
589 return TRACE_ERR (err);
591 lastp = &ctx->sig_notations;
593 lastp = &(*lastp)->next;
596 return TRACE_ERR (0);
600 /* Get the sig notations for this context. */
602 gpgme_sig_notation_get (gpgme_ctx_t ctx)
606 TRACE (DEBUG_CTX, "gpgme_sig_notation_get", ctx);
609 TRACE1 (DEBUG_CTX, "gpgme_sig_notation_get", ctx,
610 "ctx->sig_notations=%p", ctx->sig_notations);
612 return ctx->sig_notations;
617 gpgme_pubkey_algo_name (gpgme_pubkey_algo_t algo)
646 gpgme_hash_algo_name (gpgme_hash_algo_t algo)
656 case GPGME_MD_RMD160:
668 case GPGME_MD_SHA256:
671 case GPGME_MD_SHA384:
674 case GPGME_MD_SHA512:
683 case GPGME_MD_CRC32_RFC1510:
684 return "CRC32RFC1510";
686 case GPGME_MD_CRC24_RFC2440:
687 return "CRC24RFC2440";