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));
67 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
69 ctx = calloc (1, sizeof *ctx);
71 return TRACE_ERR (gpg_error_from_errno (errno));
73 INIT_LOCK (ctx->lock);
75 _gpgme_engine_info_copy (&ctx->engine_info);
76 if (!ctx->engine_info)
79 return TRACE_ERR (gpg_error_from_errno (errno));
82 ctx->keylist_mode = GPGME_KEYLIST_MODE_LOCAL;
83 ctx->include_certs = GPGME_INCLUDE_CERTS_DEFAULT;
84 ctx->protocol = GPGME_PROTOCOL_OpenPGP;
85 ctx->sub_protocol = GPGME_PROTOCOL_DEFAULT;
86 _gpgme_fd_table_init (&ctx->fdt);
91 ctx->lc_ctype = strdup (def_lc_ctype);
95 _gpgme_engine_info_release (ctx->engine_info);
97 return TRACE_ERR (gpg_error_from_errno (errno));
105 ctx->lc_messages = strdup (def_lc_messages);
106 if (!ctx->lc_messages)
108 UNLOCK (def_lc_lock);
110 free (ctx->lc_ctype);
111 _gpgme_engine_info_release (ctx->engine_info);
113 return TRACE_ERR (gpg_error_from_errno (errno));
117 def_lc_messages = NULL;
118 UNLOCK (def_lc_lock);
122 return TRACE_SUC1 ("ctx=%p", ctx);
127 _gpgme_cancel_with_err (gpgme_ctx_t ctx, gpg_error_t ctx_err,
131 struct gpgme_io_event_done_data data;
133 TRACE_BEG2 (DEBUG_CTX, "_gpgme_cancel_with_err", ctx, "ctx_err=%i, op_err=%i",
138 err = _gpgme_engine_cancel (ctx->engine);
140 return TRACE_ERR (err);
144 err = _gpgme_engine_cancel_op (ctx->engine);
146 return TRACE_ERR (err);
150 data.op_err = op_err;
152 _gpgme_engine_io_event (ctx->engine, GPGME_EVENT_DONE, &data);
154 return TRACE_ERR (0);
158 /* Cancel a pending asynchronous operation. */
160 gpgme_cancel (gpgme_ctx_t ctx)
164 TRACE_BEG (DEBUG_CTX, "gpgme_cancel", ctx);
167 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
169 err = _gpgme_cancel_with_err (ctx, gpg_error (GPG_ERR_CANCELED), 0);
171 return TRACE_ERR (err);
175 /* Cancel a pending operation asynchronously. */
177 gpgme_cancel_async (gpgme_ctx_t ctx)
179 TRACE_BEG (DEBUG_CTX, "gpgme_cancel_async", ctx);
182 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
188 return TRACE_ERR (0);
192 /* Release all resources associated with the given context. */
194 gpgme_release (gpgme_ctx_t ctx)
196 TRACE (DEBUG_CTX, "gpgme_release", ctx);
201 _gpgme_engine_release (ctx->engine);
202 _gpgme_fd_table_deinit (&ctx->fdt);
203 _gpgme_release_result (ctx);
204 _gpgme_signers_clear (ctx);
205 _gpgme_sig_notation_clear (ctx);
209 free (ctx->lc_ctype);
210 if (ctx->lc_messages)
211 free (ctx->lc_messages);
212 _gpgme_engine_info_release (ctx->engine_info);
213 DESTROY_LOCK (ctx->lock);
219 gpgme_result_ref (void *result)
221 struct ctx_op_data *data;
226 data = (void*)((char*)result - sizeof (struct ctx_op_data));
228 assert (data->magic == CTX_OP_DATA_MAGIC);
230 LOCK (result_ref_lock);
232 UNLOCK (result_ref_lock);
237 gpgme_result_unref (void *result)
239 struct ctx_op_data *data;
244 data = (void*)((char*)result - sizeof (struct ctx_op_data));
246 assert (data->magic == CTX_OP_DATA_MAGIC);
248 LOCK (result_ref_lock);
249 if (--data->references)
251 UNLOCK (result_ref_lock);
254 UNLOCK (result_ref_lock);
257 (*data->cleanup) (data->hook);
263 _gpgme_release_result (gpgme_ctx_t ctx)
265 struct ctx_op_data *data = ctx->op_data;
269 struct ctx_op_data *next_data = data->next;
271 gpgme_result_unref (data->hook);
279 gpgme_set_protocol (gpgme_ctx_t ctx, gpgme_protocol_t protocol)
281 TRACE_BEG2 (DEBUG_CTX, "gpgme_set_protocol", ctx, "protocol=%i (%s)",
282 protocol, gpgme_get_protocol_name (protocol)
283 ? gpgme_get_protocol_name (protocol) : "invalid");
285 if (protocol != GPGME_PROTOCOL_OpenPGP
286 && protocol != GPGME_PROTOCOL_CMS
287 && protocol != GPGME_PROTOCOL_GPGCONF
288 && protocol != GPGME_PROTOCOL_ASSUAN
289 && protocol != GPGME_PROTOCOL_G13
290 && protocol != GPGME_PROTOCOL_UISERVER)
291 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
294 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
296 if (ctx->protocol != protocol)
298 /* Shut down the engine when switching protocols. */
301 TRACE_LOG1 ("releasing ctx->engine=%p", ctx->engine);
302 _gpgme_engine_release (ctx->engine);
306 ctx->protocol = protocol;
308 return TRACE_ERR (0);
313 gpgme_get_protocol (gpgme_ctx_t ctx)
315 TRACE2 (DEBUG_CTX, "gpgme_get_protocol", ctx,
316 "ctx->protocol=%i (%s)", ctx->protocol,
317 gpgme_get_protocol_name (ctx->protocol)
318 ? gpgme_get_protocol_name (ctx->protocol) : "invalid");
320 return ctx->protocol;
325 gpgme_set_sub_protocol (gpgme_ctx_t ctx, gpgme_protocol_t protocol)
327 TRACE2 (DEBUG_CTX, "gpgme_set_sub_protocol", ctx, "protocol=%i (%s)",
328 protocol, gpgme_get_protocol_name (protocol)
329 ? gpgme_get_protocol_name (protocol) : "invalid");
332 return gpg_error (GPG_ERR_INV_VALUE);
334 ctx->sub_protocol = protocol;
340 gpgme_get_sub_protocol (gpgme_ctx_t ctx)
342 TRACE2 (DEBUG_CTX, "gpgme_get_sub_protocol", ctx,
343 "ctx->sub_protocol=%i (%s)", ctx->sub_protocol,
344 gpgme_get_protocol_name (ctx->sub_protocol)
345 ? gpgme_get_protocol_name (ctx->sub_protocol) : "invalid");
347 return ctx->sub_protocol;
352 gpgme_get_protocol_name (gpgme_protocol_t protocol)
356 case GPGME_PROTOCOL_OpenPGP:
359 case GPGME_PROTOCOL_CMS:
362 case GPGME_PROTOCOL_GPGCONF:
365 case GPGME_PROTOCOL_ASSUAN:
368 case GPGME_PROTOCOL_G13:
371 case GPGME_PROTOCOL_UISERVER:
374 case GPGME_PROTOCOL_DEFAULT:
377 case GPGME_PROTOCOL_UNKNOWN:
385 /* Enable or disable the use of an ascii armor for all output. */
387 gpgme_set_armor (gpgme_ctx_t ctx, int use_armor)
389 TRACE2 (DEBUG_CTX, "gpgme_set_armor", ctx, "use_armor=%i (%s)",
390 use_armor, use_armor ? "yes" : "no");
395 ctx->use_armor = use_armor;
399 /* Return the state of the armor flag. */
401 gpgme_get_armor (gpgme_ctx_t ctx)
403 TRACE2 (DEBUG_CTX, "gpgme_get_armor", ctx, "ctx->use_armor=%i (%s)",
404 ctx->use_armor, ctx->use_armor ? "yes" : "no");
405 return ctx->use_armor;
409 /* Enable or disable the use of the special textmode. Textmode is for
410 example used for the RFC2015 signatures; note that the updated RFC
411 3156 mandates that the MUA does some preparations so that textmode
412 is not needed anymore. */
414 gpgme_set_textmode (gpgme_ctx_t ctx, int use_textmode)
416 TRACE2 (DEBUG_CTX, "gpgme_set_textmode", ctx, "use_textmode=%i (%s)",
417 use_textmode, use_textmode ? "yes" : "no");
422 ctx->use_textmode = use_textmode;
425 /* Return the state of the textmode flag. */
427 gpgme_get_textmode (gpgme_ctx_t ctx)
429 TRACE2 (DEBUG_CTX, "gpgme_get_textmode", ctx, "ctx->use_textmode=%i (%s)",
430 ctx->use_textmode, ctx->use_textmode ? "yes" : "no");
431 return ctx->use_textmode;
435 /* Set the number of certifications to include in an S/MIME message.
436 The default is GPGME_INCLUDE_CERTS_DEFAULT. -1 means all certs,
437 and -2 means all certs except the root cert. */
439 gpgme_set_include_certs (gpgme_ctx_t ctx, int nr_of_certs)
444 if (nr_of_certs == GPGME_INCLUDE_CERTS_DEFAULT)
445 ctx->include_certs = GPGME_INCLUDE_CERTS_DEFAULT;
446 else if (nr_of_certs < -2)
447 ctx->include_certs = -2;
449 ctx->include_certs = nr_of_certs;
451 TRACE2 (DEBUG_CTX, "gpgme_set_include_certs", ctx, "nr_of_certs=%i%s",
452 nr_of_certs, nr_of_certs == ctx->include_certs ? "" : " (-2)");
456 /* Get the number of certifications to include in an S/MIME
459 gpgme_get_include_certs (gpgme_ctx_t ctx)
461 TRACE1 (DEBUG_CTX, "gpgme_get_include_certs", ctx, "ctx->include_certs=%i",
463 return ctx->include_certs;
467 /* This function changes the default behaviour of the keylisting
468 functions. MODE is a bitwise-OR of the GPGME_KEYLIST_* flags. The
469 default mode is GPGME_KEYLIST_MODE_LOCAL. */
471 gpgme_set_keylist_mode (gpgme_ctx_t ctx, gpgme_keylist_mode_t mode)
473 TRACE1 (DEBUG_CTX, "gpgme_set_keylist_mode", ctx, "keylist_mode=0x%x",
477 return gpg_error (GPG_ERR_INV_VALUE);
479 ctx->keylist_mode = mode;
483 /* This function returns the default behaviour of the keylisting
486 gpgme_get_keylist_mode (gpgme_ctx_t ctx)
488 TRACE1 (DEBUG_CTX, "gpgme_get_keylist_mode", ctx,
489 "ctx->keylist_mode=0x%x", ctx->keylist_mode);
490 return ctx->keylist_mode;
494 /* This function sets a callback function to be used to pass a
495 passphrase to gpg. */
497 gpgme_set_passphrase_cb (gpgme_ctx_t ctx, gpgme_passphrase_cb_t cb,
500 TRACE2 (DEBUG_CTX, "gpgme_set_passphrase_cb", ctx,
501 "passphrase_cb=%p/%p", cb, cb_value);
506 ctx->passphrase_cb = cb;
507 ctx->passphrase_cb_value = cb_value;
511 /* This function returns the callback function to be used to pass a
512 passphrase to the crypto engine. */
514 gpgme_get_passphrase_cb (gpgme_ctx_t ctx, gpgme_passphrase_cb_t *r_cb,
517 TRACE2 (DEBUG_CTX, "gpgme_get_passphrase_cb", ctx,
518 "ctx->passphrase_cb=%p/%p",
519 ctx->passphrase_cb, ctx->passphrase_cb_value);
521 *r_cb = ctx->passphrase_cb;
523 *r_cb_value = ctx->passphrase_cb_value;
527 /* This function sets a callback function to be used as a progress
530 gpgme_set_progress_cb (gpgme_ctx_t ctx, gpgme_progress_cb_t cb, void *cb_value)
532 TRACE2 (DEBUG_CTX, "gpgme_set_progress_cb", ctx, "progress_cb=%p/%p",
538 ctx->progress_cb = cb;
539 ctx->progress_cb_value = cb_value;
543 /* This function returns the callback function to be used as a
544 progress indicator. */
546 gpgme_get_progress_cb (gpgme_ctx_t ctx, gpgme_progress_cb_t *r_cb,
549 TRACE2 (DEBUG_CTX, "gpgme_get_progress_cb", ctx, "ctx->progress_cb=%p/%p",
550 ctx->progress_cb, ctx->progress_cb_value);
552 *r_cb = ctx->progress_cb;
554 *r_cb_value = ctx->progress_cb_value;
558 /* Set the I/O callback functions for CTX to IO_CBS. */
560 gpgme_set_io_cbs (gpgme_ctx_t ctx, gpgme_io_cbs_t io_cbs)
567 TRACE6 (DEBUG_CTX, "gpgme_set_io_cbs", ctx,
568 "io_cbs=%p (add=%p/%p, remove=%p, event=%p/%p",
569 io_cbs, io_cbs->add, io_cbs->add_priv, io_cbs->remove,
570 io_cbs->event, io_cbs->event_priv);
571 ctx->io_cbs = *io_cbs;
575 TRACE1 (DEBUG_CTX, "gpgme_set_io_cbs", ctx,
576 "io_cbs=%p (default)", io_cbs);
577 ctx->io_cbs.add = NULL;
578 ctx->io_cbs.add_priv = NULL;
579 ctx->io_cbs.remove = NULL;
580 ctx->io_cbs.event = NULL;
581 ctx->io_cbs.event_priv = NULL;
586 /* This function provides access to the internal read function; it is
587 normally not used. */
589 gpgme_io_read (int fd, void *buffer, size_t count)
592 TRACE_BEG2 (DEBUG_GLOBAL, "gpgme_io_read", fd,
593 "buffer=%p, count=%u", buffer, count);
595 ret = _gpgme_io_read (fd, buffer, count);
597 return TRACE_SYSRES (ret);
601 /* This function provides access to the internal write function. It
602 is to be used by user callbacks to return data to gpgme. See
603 gpgme_passphrase_cb_t and gpgme_edit_cb_t. */
605 gpgme_io_write (int fd, const void *buffer, size_t count)
608 TRACE_BEG2 (DEBUG_GLOBAL, "gpgme_io_write", fd,
609 "buffer=%p, count=%u", buffer, count);
611 ret = _gpgme_io_write (fd, buffer, count);
613 return TRACE_SYSRES (ret);
617 /* This function returns the callback function for I/O. */
619 gpgme_get_io_cbs (gpgme_ctx_t ctx, gpgme_io_cbs_t io_cbs)
621 TRACE6 (DEBUG_CTX, "gpgme_get_io_cbs", ctx,
622 "io_cbs=%p, ctx->io_cbs.add=%p/%p, .remove=%p, .event=%p/%p",
623 io_cbs, io_cbs->add, io_cbs->add_priv, io_cbs->remove,
624 io_cbs->event, io_cbs->event_priv);
626 *io_cbs = ctx->io_cbs;
630 /* This function sets the locale for the context CTX, or the default
631 locale if CTX is a null pointer. */
633 gpgme_set_locale (gpgme_ctx_t ctx, int category, const char *value)
636 char *new_lc_ctype = NULL;
637 char *new_lc_messages = NULL;
639 TRACE_BEG2 (DEBUG_CTX, "gpgme_set_locale", ctx,
640 "category=%i, value=%s", category, value ? value : "(null)");
642 #define PREPARE_ONE_LOCALE(lcat, ucat) \
643 if (!failed && value \
644 && (category == LC_ALL || category == LC_ ## ucat)) \
646 new_lc_ ## lcat = strdup (value); \
647 if (!new_lc_ ## lcat) \
652 PREPARE_ONE_LOCALE (ctype, CTYPE);
655 PREPARE_ONE_LOCALE (messages, MESSAGES);
660 int saved_errno = errno;
665 free (new_lc_messages);
667 return TRACE_ERR (gpg_error_from_errno (saved_errno));
670 #define SET_ONE_LOCALE(lcat, ucat) \
671 if (category == LC_ALL || category == LC_ ## ucat) \
675 if (ctx->lc_ ## lcat) \
676 free (ctx->lc_ ## lcat); \
677 ctx->lc_ ## lcat = new_lc_ ## lcat; \
681 if (def_lc_ ## lcat) \
682 free (def_lc_ ## lcat); \
683 def_lc_ ## lcat = new_lc_ ## lcat; \
690 SET_ONE_LOCALE (ctype, CTYPE);
693 SET_ONE_LOCALE (messages, MESSAGES);
696 UNLOCK (def_lc_lock);
698 return TRACE_ERR (0);
702 /* Get the information about the configured engines. A pointer to the
703 first engine in the statically allocated linked list is returned.
704 The returned data is valid until the next gpgme_ctx_set_engine_info. */
706 gpgme_ctx_get_engine_info (gpgme_ctx_t ctx)
708 TRACE1 (DEBUG_CTX, "gpgme_ctx_get_engine_info", ctx,
709 "ctx->engine_info=%p", ctx->engine_info);
710 return ctx->engine_info;
714 /* Set the engine info for the context CTX, protocol PROTO, to the
715 file name FILE_NAME and the home directory HOME_DIR. */
717 gpgme_ctx_set_engine_info (gpgme_ctx_t ctx, gpgme_protocol_t proto,
718 const char *file_name, const char *home_dir)
721 TRACE_BEG4 (DEBUG_CTX, "gpgme_ctx_set_engine_info", ctx,
722 "protocol=%i (%s), file_name=%s, home_dir=%s",
723 proto, gpgme_get_protocol_name (proto)
724 ? gpgme_get_protocol_name (proto) : "unknown",
725 file_name ? file_name : "(default)",
726 home_dir ? home_dir : "(default)");
729 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
731 /* Shut down the engine when changing engine info. */
734 TRACE_LOG1 ("releasing ctx->engine=%p", ctx->engine);
735 _gpgme_engine_release (ctx->engine);
738 err = _gpgme_set_engine_info (ctx->engine_info, proto,
739 file_name, home_dir);
740 return TRACE_ERR (err);
744 /* Clear all notation data from the context. */
746 _gpgme_sig_notation_clear (gpgme_ctx_t ctx)
748 gpgme_sig_notation_t notation;
753 notation = ctx->sig_notations;
756 gpgme_sig_notation_t next_notation = notation->next;
757 _gpgme_sig_notation_free (notation);
758 notation = next_notation;
760 ctx->sig_notations = NULL;
764 gpgme_sig_notation_clear (gpgme_ctx_t ctx)
766 TRACE (DEBUG_CTX, "gpgme_sig_notation_clear", ctx);
771 _gpgme_sig_notation_clear (ctx);
775 /* Add the human-readable notation data with name NAME and value VALUE
776 to the context CTX, using the flags FLAGS. If NAME is NULL, then
777 VALUE should be a policy URL. The flag
778 GPGME_SIG_NOTATION_HUMAN_READABLE is forced to be true for notation
779 data, and false for policy URLs. */
781 gpgme_sig_notation_add (gpgme_ctx_t ctx, const char *name,
782 const char *value, gpgme_sig_notation_flags_t flags)
785 gpgme_sig_notation_t notation;
786 gpgme_sig_notation_t *lastp;
788 TRACE_BEG3 (DEBUG_CTX, "gpgme_sig_notation_add", ctx,
789 "name=%s, value=%s, flags=0x%x",
790 name ? name : "(null)", value ? value : "(null)",
794 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
797 flags |= GPGME_SIG_NOTATION_HUMAN_READABLE;
799 flags &= ~GPGME_SIG_NOTATION_HUMAN_READABLE;
801 err = _gpgme_sig_notation_create (¬ation, name, name ? strlen (name) : 0,
802 value, value ? strlen (value) : 0, flags);
804 return TRACE_ERR (err);
806 lastp = &ctx->sig_notations;
808 lastp = &(*lastp)->next;
811 return TRACE_ERR (0);
815 /* Get the sig notations for this context. */
817 gpgme_sig_notation_get (gpgme_ctx_t ctx)
821 TRACE (DEBUG_CTX, "gpgme_sig_notation_get", ctx);
824 TRACE1 (DEBUG_CTX, "gpgme_sig_notation_get", ctx,
825 "ctx->sig_notations=%p", ctx->sig_notations);
827 return ctx->sig_notations;
832 gpgme_pubkey_algo_name (gpgme_pubkey_algo_t algo)
867 gpgme_hash_algo_name (gpgme_hash_algo_t algo)
877 case GPGME_MD_RMD160:
889 case GPGME_MD_SHA256:
892 case GPGME_MD_SHA384:
895 case GPGME_MD_SHA512:
904 case GPGME_MD_CRC32_RFC1510:
905 return "CRC32RFC1510";
907 case GPGME_MD_CRC24_RFC2440:
908 return "CRC24RFC2440";