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
42 /* The default locale. */
43 DEFINE_STATIC_LOCK (def_lc_lock);
44 static char *def_lc_ctype;
45 static char *def_lc_messages;
48 gpgme_error_t _gpgme_selftest = GPG_ERR_NOT_OPERATIONAL;
50 /* Protects all reference counters in result structures. All other
51 accesses to a result structure are read only. */
52 DEFINE_STATIC_LOCK (result_ref_lock);
55 /* Create a new context as an environment for GPGME crypto
58 gpgme_new (gpgme_ctx_t *r_ctx)
61 TRACE_BEG (DEBUG_CTX, "gpgme_new", r_ctx);
64 return TRACE_ERR (gpgme_error (_gpgme_selftest));
66 ctx = calloc (1, sizeof *ctx);
68 return TRACE_ERR (gpg_error_from_errno (errno));
70 INIT_LOCK (ctx->lock);
72 _gpgme_engine_info_copy (&ctx->engine_info);
73 if (!ctx->engine_info)
76 return TRACE_ERR (gpg_error_from_errno (errno));
79 ctx->keylist_mode = GPGME_KEYLIST_MODE_LOCAL;
80 ctx->include_certs = GPGME_INCLUDE_CERTS_DEFAULT;
81 ctx->protocol = GPGME_PROTOCOL_OpenPGP;
82 ctx->sub_protocol = GPGME_PROTOCOL_DEFAULT;
83 _gpgme_fd_table_init (&ctx->fdt);
88 ctx->lc_ctype = strdup (def_lc_ctype);
92 _gpgme_engine_info_release (ctx->engine_info);
94 return TRACE_ERR (gpg_error_from_errno (errno));
102 ctx->lc_messages = strdup (def_lc_messages);
103 if (!ctx->lc_messages)
105 UNLOCK (def_lc_lock);
107 free (ctx->lc_ctype);
108 _gpgme_engine_info_release (ctx->engine_info);
110 return TRACE_ERR (gpg_error_from_errno (errno));
114 def_lc_messages = NULL;
115 UNLOCK (def_lc_lock);
119 return TRACE_SUC1 ("ctx=%p", ctx);
124 _gpgme_cancel_with_err (gpgme_ctx_t ctx, gpg_error_t ctx_err,
128 struct gpgme_io_event_done_data data;
130 TRACE_BEG2 (DEBUG_CTX, "_gpgme_cancel_with_err", ctx, "ctx_err=%i, op_err=%i",
135 err = _gpgme_engine_cancel (ctx->engine);
137 return TRACE_ERR (err);
141 err = _gpgme_engine_cancel_op (ctx->engine);
143 return TRACE_ERR (err);
147 data.op_err = op_err;
149 _gpgme_engine_io_event (ctx->engine, GPGME_EVENT_DONE, &data);
151 return TRACE_ERR (0);
155 /* Cancel a pending asynchronous operation. */
157 gpgme_cancel (gpgme_ctx_t ctx)
161 TRACE_BEG (DEBUG_CTX, "gpgme_cancel", ctx);
163 err = _gpgme_cancel_with_err (ctx, gpg_error (GPG_ERR_CANCELED), 0);
165 return TRACE_ERR (err);
169 /* Cancel a pending operation asynchronously. */
171 gpgme_cancel_async (gpgme_ctx_t ctx)
173 TRACE_BEG (DEBUG_CTX, "gpgme_cancel_async", ctx);
179 return TRACE_ERR (0);
183 /* Release all resources associated with the given context. */
185 gpgme_release (gpgme_ctx_t ctx)
187 TRACE (DEBUG_CTX, "gpgme_release", ctx);
189 _gpgme_engine_release (ctx->engine);
190 _gpgme_fd_table_deinit (&ctx->fdt);
191 _gpgme_release_result (ctx);
192 _gpgme_signers_clear (ctx);
193 _gpgme_sig_notation_clear (ctx);
197 free (ctx->lc_ctype);
198 if (ctx->lc_messages)
199 free (ctx->lc_messages);
200 _gpgme_engine_info_release (ctx->engine_info);
201 DESTROY_LOCK (ctx->lock);
207 gpgme_result_ref (void *result)
209 struct ctx_op_data *data;
214 data = result - sizeof (struct ctx_op_data);
216 assert (data->magic == CTX_OP_DATA_MAGIC);
218 LOCK (result_ref_lock);
220 UNLOCK (result_ref_lock);
225 gpgme_result_unref (void *result)
227 struct ctx_op_data *data;
232 data = result - sizeof (struct ctx_op_data);
234 assert (data->magic == CTX_OP_DATA_MAGIC);
236 LOCK (result_ref_lock);
237 if (--data->references)
239 UNLOCK (result_ref_lock);
242 UNLOCK (result_ref_lock);
245 (*data->cleanup) (data->hook);
251 _gpgme_release_result (gpgme_ctx_t ctx)
253 struct ctx_op_data *data = ctx->op_data;
257 struct ctx_op_data *next_data = data->next;
259 gpgme_result_unref (data->hook);
267 gpgme_set_protocol (gpgme_ctx_t ctx, gpgme_protocol_t protocol)
269 TRACE_BEG2 (DEBUG_CTX, "gpgme_set_protocol", ctx, "protocol=%i (%s)",
270 protocol, gpgme_get_protocol_name (protocol)
271 ? gpgme_get_protocol_name (protocol) : "invalid");
273 if (protocol != GPGME_PROTOCOL_OpenPGP
274 && protocol != GPGME_PROTOCOL_CMS
275 && protocol != GPGME_PROTOCOL_GPGCONF
276 && protocol != GPGME_PROTOCOL_ASSUAN
277 && protocol != GPGME_PROTOCOL_G13
278 && protocol != GPGME_PROTOCOL_UISERVER)
279 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
281 if (ctx->protocol != protocol)
283 /* Shut down the engine when switching protocols. */
286 TRACE_LOG1 ("releasing ctx->engine=%p", ctx->engine);
287 _gpgme_engine_release (ctx->engine);
291 ctx->protocol = protocol;
293 return TRACE_ERR (0);
298 gpgme_get_protocol (gpgme_ctx_t ctx)
300 TRACE2 (DEBUG_CTX, "gpgme_get_protocol", ctx,
301 "ctx->protocol=%i (%s)", ctx->protocol,
302 gpgme_get_protocol_name (ctx->protocol)
303 ? gpgme_get_protocol_name (ctx->protocol) : "invalid");
304 return ctx->protocol;
309 gpgme_set_sub_protocol (gpgme_ctx_t ctx, gpgme_protocol_t protocol)
311 TRACE2 (DEBUG_CTX, "gpgme_set_sub_protocol", ctx, "protocol=%i (%s)",
312 protocol, gpgme_get_protocol_name (protocol)
313 ? gpgme_get_protocol_name (protocol) : "invalid");
314 ctx->sub_protocol = protocol;
320 gpgme_get_sub_protocol (gpgme_ctx_t ctx)
322 TRACE2 (DEBUG_CTX, "gpgme_get_sub_protocol", ctx,
323 "ctx->sub_protocol=%i (%s)", ctx->sub_protocol,
324 gpgme_get_protocol_name (ctx->sub_protocol)
325 ? gpgme_get_protocol_name (ctx->sub_protocol) : "invalid");
326 return ctx->sub_protocol;
331 gpgme_get_protocol_name (gpgme_protocol_t protocol)
335 case GPGME_PROTOCOL_OpenPGP:
338 case GPGME_PROTOCOL_CMS:
341 case GPGME_PROTOCOL_GPGCONF:
344 case GPGME_PROTOCOL_ASSUAN:
347 case GPGME_PROTOCOL_G13:
350 case GPGME_PROTOCOL_UISERVER:
353 case GPGME_PROTOCOL_DEFAULT:
356 case GPGME_PROTOCOL_UNKNOWN:
364 /* Enable or disable the use of an ascii armor for all output. */
366 gpgme_set_armor (gpgme_ctx_t ctx, int use_armor)
368 TRACE2 (DEBUG_CTX, "gpgme_set_armor", ctx, "use_armor=%i (%s)",
369 use_armor, use_armor ? "yes" : "no");
370 ctx->use_armor = use_armor;
374 /* Return the state of the armor flag. */
376 gpgme_get_armor (gpgme_ctx_t ctx)
378 TRACE2 (DEBUG_CTX, "gpgme_get_armor", ctx, "ctx->use_armor=%i (%s)",
379 ctx->use_armor, ctx->use_armor ? "yes" : "no");
380 return ctx->use_armor;
384 /* Enable or disable the use of the special textmode. Textmode is for
385 example used for the RFC2015 signatures; note that the updated RFC
386 3156 mandates that the MUA does some preparations so that textmode
387 is not needed anymore. */
389 gpgme_set_textmode (gpgme_ctx_t ctx, int use_textmode)
391 TRACE2 (DEBUG_CTX, "gpgme_set_textmode", ctx, "use_textmode=%i (%s)",
392 use_textmode, use_textmode ? "yes" : "no");
393 ctx->use_textmode = use_textmode;
396 /* Return the state of the textmode flag. */
398 gpgme_get_textmode (gpgme_ctx_t ctx)
400 TRACE2 (DEBUG_CTX, "gpgme_get_textmode", ctx, "ctx->use_textmode=%i (%s)",
401 ctx->use_textmode, ctx->use_textmode ? "yes" : "no");
402 return ctx->use_textmode;
406 /* Set the number of certifications to include in an S/MIME message.
407 The default is GPGME_INCLUDE_CERTS_DEFAULT. -1 means all certs,
408 and -2 means all certs except the root cert. */
410 gpgme_set_include_certs (gpgme_ctx_t ctx, int nr_of_certs)
412 if (nr_of_certs == GPGME_INCLUDE_CERTS_DEFAULT)
413 ctx->include_certs = GPGME_INCLUDE_CERTS_DEFAULT;
414 else if (nr_of_certs < -2)
415 ctx->include_certs = -2;
417 ctx->include_certs = nr_of_certs;
419 TRACE2 (DEBUG_CTX, "gpgme_set_include_certs", ctx, "nr_of_certs=%i%s",
420 nr_of_certs, nr_of_certs == ctx->include_certs ? "" : " (-2)");
424 /* Get the number of certifications to include in an S/MIME
427 gpgme_get_include_certs (gpgme_ctx_t ctx)
429 TRACE1 (DEBUG_CTX, "gpgme_get_include_certs", ctx, "ctx->include_certs=%i",
431 return ctx->include_certs;
435 /* This function changes the default behaviour of the keylisting
436 functions. MODE is a bitwise-OR of the GPGME_KEYLIST_* flags. The
437 default mode is GPGME_KEYLIST_MODE_LOCAL. */
439 gpgme_set_keylist_mode (gpgme_ctx_t ctx, gpgme_keylist_mode_t mode)
441 TRACE1 (DEBUG_CTX, "gpgme_set_keylist_mode", ctx, "keylist_mode=0x%x",
444 ctx->keylist_mode = mode;
448 /* This function returns the default behaviour of the keylisting
451 gpgme_get_keylist_mode (gpgme_ctx_t ctx)
453 TRACE1 (DEBUG_CTX, "gpgme_get_keylist_mode", ctx,
454 "ctx->keylist_mode=0x%x", ctx->keylist_mode);
455 return ctx->keylist_mode;
459 /* This function sets a callback function to be used to pass a
460 passphrase to gpg. */
462 gpgme_set_passphrase_cb (gpgme_ctx_t ctx, gpgme_passphrase_cb_t cb,
465 TRACE2 (DEBUG_CTX, "gpgme_set_passphrase_cb", ctx,
466 "passphrase_cb=%p/%p", cb, cb_value);
467 ctx->passphrase_cb = cb;
468 ctx->passphrase_cb_value = cb_value;
472 /* This function returns the callback function to be used to pass a
473 passphrase to the crypto engine. */
475 gpgme_get_passphrase_cb (gpgme_ctx_t ctx, gpgme_passphrase_cb_t *r_cb,
478 TRACE2 (DEBUG_CTX, "gpgme_get_passphrase_cb", ctx,
479 "ctx->passphrase_cb=%p/%p",
480 ctx->passphrase_cb, ctx->passphrase_cb_value);
482 *r_cb = ctx->passphrase_cb;
484 *r_cb_value = ctx->passphrase_cb_value;
488 /* This function sets a callback function to be used as a progress
491 gpgme_set_progress_cb (gpgme_ctx_t ctx, gpgme_progress_cb_t cb, void *cb_value)
493 TRACE2 (DEBUG_CTX, "gpgme_set_progress_cb", ctx, "progress_cb=%p/%p",
495 ctx->progress_cb = cb;
496 ctx->progress_cb_value = cb_value;
500 /* This function returns the callback function to be used as a
501 progress indicator. */
503 gpgme_get_progress_cb (gpgme_ctx_t ctx, gpgme_progress_cb_t *r_cb,
506 TRACE2 (DEBUG_CTX, "gpgme_get_progress_cb", ctx, "ctx->progress_cb=%p/%p",
507 ctx->progress_cb, ctx->progress_cb_value);
509 *r_cb = ctx->progress_cb;
511 *r_cb_value = ctx->progress_cb_value;
515 /* Set the I/O callback functions for CTX to IO_CBS. */
517 gpgme_set_io_cbs (gpgme_ctx_t ctx, gpgme_io_cbs_t io_cbs)
521 TRACE6 (DEBUG_CTX, "gpgme_set_io_cbs", ctx,
522 "io_cbs=%p (add=%p/%p, remove=%p, event=%p/%p",
523 io_cbs, io_cbs->add, io_cbs->add_priv, io_cbs->remove,
524 io_cbs->event, io_cbs->event_priv);
525 ctx->io_cbs = *io_cbs;
529 TRACE1 (DEBUG_CTX, "gpgme_set_io_cbs", ctx,
530 "io_cbs=%p (default)", io_cbs);
531 ctx->io_cbs.add = NULL;
532 ctx->io_cbs.add_priv = NULL;
533 ctx->io_cbs.remove = NULL;
534 ctx->io_cbs.event = NULL;
535 ctx->io_cbs.event_priv = NULL;
540 /* This function provides access to the internal read function; it is
541 normally not used. */
543 gpgme_io_read (int fd, void *buffer, size_t count)
546 TRACE_BEG2 (DEBUG_GLOBAL, "gpgme_io_read", fd,
547 "buffer=%p, count=%u", buffer, count);
549 ret = _gpgme_io_read (fd, buffer, count);
551 return TRACE_SYSRES (ret);
555 /* This function provides access to the internal write function. It
556 is to be used by user callbacks to return data to gpgme. See
557 gpgme_passphrase_cb_t and gpgme_edit_cb_t. */
559 gpgme_io_write (int fd, const void *buffer, size_t count)
562 TRACE_BEG2 (DEBUG_GLOBAL, "gpgme_io_write", fd,
563 "buffer=%p, count=%u", buffer, count);
565 ret = _gpgme_io_write (fd, buffer, count);
567 return TRACE_SYSRES (ret);
571 /* This function returns the callback function for I/O. */
573 gpgme_get_io_cbs (gpgme_ctx_t ctx, gpgme_io_cbs_t io_cbs)
575 TRACE6 (DEBUG_CTX, "gpgme_get_io_cbs", ctx,
576 "io_cbs=%p, ctx->io_cbs.add=%p/%p, .remove=%p, .event=%p/%p",
577 io_cbs, io_cbs->add, io_cbs->add_priv, io_cbs->remove,
578 io_cbs->event, io_cbs->event_priv);
580 *io_cbs = ctx->io_cbs;
584 /* This function sets the locale for the context CTX, or the default
585 locale if CTX is a null pointer. */
587 gpgme_set_locale (gpgme_ctx_t ctx, int category, const char *value)
590 char *new_lc_ctype = NULL;
591 char *new_lc_messages = NULL;
593 TRACE_BEG2 (DEBUG_CTX, "gpgme_set_locale", ctx,
594 "category=%i, value=%s", category, value ? value : "(null)");
596 #define PREPARE_ONE_LOCALE(lcat, ucat) \
597 if (!failed && value \
598 && (category == LC_ALL || category == LC_ ## ucat)) \
600 new_lc_ ## lcat = strdup (value); \
601 if (!new_lc_ ## lcat) \
606 PREPARE_ONE_LOCALE (ctype, CTYPE);
609 PREPARE_ONE_LOCALE (messages, MESSAGES);
614 int saved_errno = errno;
619 free (new_lc_messages);
621 return TRACE_ERR (gpg_error_from_errno (saved_errno));
624 #define SET_ONE_LOCALE(lcat, ucat) \
625 if (category == LC_ALL || category == LC_ ## ucat) \
629 if (ctx->lc_ ## lcat) \
630 free (ctx->lc_ ## lcat); \
631 ctx->lc_ ## lcat = new_lc_ ## lcat; \
635 if (def_lc_ ## lcat) \
636 free (def_lc_ ## lcat); \
637 def_lc_ ## lcat = new_lc_ ## lcat; \
644 SET_ONE_LOCALE (ctype, CTYPE);
647 SET_ONE_LOCALE (messages, MESSAGES);
650 UNLOCK (def_lc_lock);
652 return TRACE_ERR (0);
656 /* Get the information about the configured engines. A pointer to the
657 first engine in the statically allocated linked list is returned.
658 The returned data is valid until the next gpgme_ctx_set_engine_info. */
660 gpgme_ctx_get_engine_info (gpgme_ctx_t ctx)
662 TRACE1 (DEBUG_CTX, "gpgme_ctx_get_engine_info", ctx,
663 "ctx->engine_info=%p", ctx->engine_info);
664 return ctx->engine_info;
668 /* Set the engine info for the context CTX, protocol PROTO, to the
669 file name FILE_NAME and the home directory HOME_DIR. */
671 gpgme_ctx_set_engine_info (gpgme_ctx_t ctx, gpgme_protocol_t proto,
672 const char *file_name, const char *home_dir)
675 TRACE_BEG4 (DEBUG_CTX, "gpgme_ctx_set_engine_info", ctx,
676 "protocol=%i (%s), file_name=%s, home_dir=%s",
677 proto, gpgme_get_protocol_name (proto)
678 ? gpgme_get_protocol_name (proto) : "unknown",
679 file_name ? file_name : "(default)",
680 home_dir ? home_dir : "(default)");
682 /* Shut down the engine when changing engine info. */
685 TRACE_LOG1 ("releasing ctx->engine=%p", ctx->engine);
686 _gpgme_engine_release (ctx->engine);
689 err = _gpgme_set_engine_info (ctx->engine_info, proto,
690 file_name, home_dir);
691 return TRACE_ERR (err);
695 /* Clear all notation data from the context. */
697 _gpgme_sig_notation_clear (gpgme_ctx_t ctx)
699 gpgme_sig_notation_t notation;
704 notation = ctx->sig_notations;
707 gpgme_sig_notation_t next_notation = notation->next;
708 _gpgme_sig_notation_free (notation);
709 notation = next_notation;
711 ctx->sig_notations = NULL;
715 gpgme_sig_notation_clear (gpgme_ctx_t ctx)
717 TRACE (DEBUG_CTX, "gpgme_sig_notation_clear", ctx);
718 _gpgme_sig_notation_clear (ctx);
722 /* Add the human-readable notation data with name NAME and value VALUE
723 to the context CTX, using the flags FLAGS. If NAME is NULL, then
724 VALUE should be a policy URL. The flag
725 GPGME_SIG_NOTATION_HUMAN_READABLE is forced to be true for notation
726 data, and false for policy URLs. */
728 gpgme_sig_notation_add (gpgme_ctx_t ctx, const char *name,
729 const char *value, gpgme_sig_notation_flags_t flags)
732 gpgme_sig_notation_t notation;
733 gpgme_sig_notation_t *lastp;
735 TRACE_BEG3 (DEBUG_CTX, "gpgme_sig_notation_add", ctx,
736 "name=%s, value=%s, flags=0x%x",
737 name ? name : "(null)", value ? value : "(null)",
741 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
744 flags |= GPGME_SIG_NOTATION_HUMAN_READABLE;
746 flags &= ~GPGME_SIG_NOTATION_HUMAN_READABLE;
748 err = _gpgme_sig_notation_create (¬ation, name, name ? strlen (name) : 0,
749 value, value ? strlen (value) : 0, flags);
751 return TRACE_ERR (err);
753 lastp = &ctx->sig_notations;
755 lastp = &(*lastp)->next;
758 return TRACE_ERR (0);
762 /* Get the sig notations for this context. */
764 gpgme_sig_notation_get (gpgme_ctx_t ctx)
768 TRACE (DEBUG_CTX, "gpgme_sig_notation_get", ctx);
771 TRACE1 (DEBUG_CTX, "gpgme_sig_notation_get", ctx,
772 "ctx->sig_notations=%p", ctx->sig_notations);
774 return ctx->sig_notations;
779 gpgme_pubkey_algo_name (gpgme_pubkey_algo_t algo)
814 gpgme_hash_algo_name (gpgme_hash_algo_t algo)
824 case GPGME_MD_RMD160:
836 case GPGME_MD_SHA256:
839 case GPGME_MD_SHA384:
842 case GPGME_MD_SHA512:
851 case GPGME_MD_CRC32_RFC1510:
852 return "CRC32RFC1510";
854 case GPGME_MD_CRC24_RFC2440:
855 return "CRC24RFC2440";