1 /* gpgme.c - GnuPG Made Easy.
2 Copyright (C) 2000 Werner Koch (dd9jn)
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2012,
4 2014, 2015 g10 Code GmbH
6 This file is part of GPGME.
8 GPGME is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as
10 published by the Free Software Foundation; either version 2.1 of
11 the License, or (at your option) any later version.
13 GPGME is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with this program; if not, see <https://www.gnu.org/licenses/>.
41 #include "mbox-util.h"
44 /* The default locale. */
45 DEFINE_STATIC_LOCK (def_lc_lock);
46 static char *def_lc_ctype;
47 static char *def_lc_messages;
50 gpgme_error_t _gpgme_selftest = GPG_ERR_NOT_OPERATIONAL;
52 /* Protects all reference counters in result structures. All other
53 accesses to a result structure are read only. */
54 DEFINE_STATIC_LOCK (result_ref_lock);
57 /* Set the global flag NAME to VALUE. Return 0 on success. Note that
58 this function does not use gpgme_error and thus a non-zero return
59 value merely means "error". Certain flags may be set before
60 gpgme_check_version is called. See the manual for a description of
61 supported flags. The caller must assure that this function is
62 called only by one thread at a time. */
64 gpgme_set_global_flag (const char *name, const char *value)
68 else if (!strcmp (name, "debug"))
69 return _gpgme_debug_set_debug_envvar (value);
70 else if (!strcmp (name, "disable-gpgconf"))
72 _gpgme_dirinfo_disable_gpgconf ();
75 else if (!strcmp (name, "require-gnupg"))
76 return _gpgme_set_engine_minimal_version (value);
77 else if (!strcmp (name, "gpgconf-name"))
78 return _gpgme_set_default_gpgconf_name (value);
79 else if (!strcmp (name, "gpg-name"))
80 return _gpgme_set_default_gpg_name (value);
81 else if (!strcmp (name, "w32-inst-dir"))
82 return _gpgme_set_override_inst_dir (value);
89 /* Create a new context as an environment for GPGME crypto
92 gpgme_new (gpgme_ctx_t *r_ctx)
96 TRACE_BEG (DEBUG_CTX, "gpgme_new", r_ctx);
99 return TRACE_ERR (_gpgme_selftest);
102 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
104 ctx = calloc (1, sizeof *ctx);
106 return TRACE_ERR (gpg_error_from_syserror ());
108 INIT_LOCK (ctx->lock);
110 err = _gpgme_engine_info_copy (&ctx->engine_info);
111 if (!err && !ctx->engine_info)
112 err = gpg_error (GPG_ERR_NO_ENGINE);
116 return TRACE_ERR (err);
119 ctx->keylist_mode = GPGME_KEYLIST_MODE_LOCAL;
120 ctx->include_certs = GPGME_INCLUDE_CERTS_DEFAULT;
121 ctx->protocol = GPGME_PROTOCOL_OpenPGP;
122 ctx->sub_protocol = GPGME_PROTOCOL_DEFAULT;
123 _gpgme_fd_table_init (&ctx->fdt);
128 ctx->lc_ctype = strdup (def_lc_ctype);
131 int saved_err = gpg_error_from_syserror ();
132 UNLOCK (def_lc_lock);
133 _gpgme_engine_info_release (ctx->engine_info);
135 return TRACE_ERR (saved_err);
143 ctx->lc_messages = strdup (def_lc_messages);
144 if (!ctx->lc_messages)
146 int saved_err = gpg_error_from_syserror ();
147 UNLOCK (def_lc_lock);
149 free (ctx->lc_ctype);
150 _gpgme_engine_info_release (ctx->engine_info);
152 return TRACE_ERR (saved_err);
156 def_lc_messages = NULL;
157 UNLOCK (def_lc_lock);
161 return TRACE_SUC1 ("ctx=%p", ctx);
166 _gpgme_cancel_with_err (gpgme_ctx_t ctx, gpg_error_t ctx_err,
170 struct gpgme_io_event_done_data data;
172 TRACE_BEG2 (DEBUG_CTX, "_gpgme_cancel_with_err", ctx, "ctx_err=%i, op_err=%i",
177 err = _gpgme_engine_cancel (ctx->engine);
179 return TRACE_ERR (err);
183 err = _gpgme_engine_cancel_op (ctx->engine);
185 return TRACE_ERR (err);
189 data.op_err = op_err;
191 _gpgme_engine_io_event (ctx->engine, GPGME_EVENT_DONE, &data);
193 return TRACE_ERR (0);
197 /* Cancel a pending asynchronous operation. */
199 gpgme_cancel (gpgme_ctx_t ctx)
203 TRACE_BEG (DEBUG_CTX, "gpgme_cancel", ctx);
206 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
208 err = _gpgme_cancel_with_err (ctx, gpg_error (GPG_ERR_CANCELED), 0);
210 return TRACE_ERR (err);
214 /* Cancel a pending operation asynchronously. */
216 gpgme_cancel_async (gpgme_ctx_t ctx)
218 TRACE_BEG (DEBUG_CTX, "gpgme_cancel_async", ctx);
221 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
227 return TRACE_ERR (0);
231 /* Release all resources associated with the given context. */
233 gpgme_release (gpgme_ctx_t ctx)
235 TRACE (DEBUG_CTX, "gpgme_release", ctx);
240 _gpgme_engine_release (ctx->engine);
242 _gpgme_fd_table_deinit (&ctx->fdt);
243 _gpgme_release_result (ctx);
244 _gpgme_signers_clear (ctx);
245 _gpgme_sig_notation_clear (ctx);
248 free (ctx->lc_ctype);
249 free (ctx->lc_messages);
250 free (ctx->override_session_key);
251 free (ctx->request_origin);
252 free (ctx->auto_key_locate);
253 _gpgme_engine_info_release (ctx->engine_info);
254 ctx->engine_info = NULL;
255 DESTROY_LOCK (ctx->lock);
261 gpgme_result_ref (void *result)
263 struct ctx_op_data *data;
268 data = (void*)((char*)result - sizeof (struct ctx_op_data));
270 assert (data->magic == CTX_OP_DATA_MAGIC);
272 LOCK (result_ref_lock);
274 UNLOCK (result_ref_lock);
279 gpgme_result_unref (void *result)
281 struct ctx_op_data *data;
286 data = (void*)((char*)result - sizeof (struct ctx_op_data));
288 assert (data->magic == CTX_OP_DATA_MAGIC);
290 LOCK (result_ref_lock);
291 if (--data->references)
293 UNLOCK (result_ref_lock);
296 UNLOCK (result_ref_lock);
299 (*data->cleanup) (data->hook);
305 _gpgme_release_result (gpgme_ctx_t ctx)
307 struct ctx_op_data *data = ctx->op_data;
311 struct ctx_op_data *next_data = data->next;
313 gpgme_result_unref (data->hook);
321 gpgme_set_protocol (gpgme_ctx_t ctx, gpgme_protocol_t protocol)
323 TRACE_BEG2 (DEBUG_CTX, "gpgme_set_protocol", ctx, "protocol=%i (%s)",
324 protocol, gpgme_get_protocol_name (protocol)
325 ? gpgme_get_protocol_name (protocol) : "invalid");
327 if (protocol != GPGME_PROTOCOL_OpenPGP
328 && protocol != GPGME_PROTOCOL_CMS
329 && protocol != GPGME_PROTOCOL_GPGCONF
330 && protocol != GPGME_PROTOCOL_ASSUAN
331 && protocol != GPGME_PROTOCOL_G13
332 && protocol != GPGME_PROTOCOL_UISERVER
333 && protocol != GPGME_PROTOCOL_SPAWN)
334 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
337 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
339 if (ctx->protocol != protocol)
341 /* Shut down the engine when switching protocols. */
344 TRACE_LOG1 ("releasing ctx->engine=%p", ctx->engine);
345 _gpgme_engine_release (ctx->engine);
349 ctx->protocol = protocol;
351 return TRACE_ERR (0);
356 gpgme_get_protocol (gpgme_ctx_t ctx)
358 TRACE2 (DEBUG_CTX, "gpgme_get_protocol", ctx,
359 "ctx->protocol=%i (%s)", ctx->protocol,
360 gpgme_get_protocol_name (ctx->protocol)
361 ? gpgme_get_protocol_name (ctx->protocol) : "invalid");
363 return ctx->protocol;
368 gpgme_set_sub_protocol (gpgme_ctx_t ctx, gpgme_protocol_t protocol)
370 TRACE2 (DEBUG_CTX, "gpgme_set_sub_protocol", ctx, "protocol=%i (%s)",
371 protocol, gpgme_get_protocol_name (protocol)
372 ? gpgme_get_protocol_name (protocol) : "invalid");
375 return gpg_error (GPG_ERR_INV_VALUE);
377 ctx->sub_protocol = protocol;
383 gpgme_get_sub_protocol (gpgme_ctx_t ctx)
385 TRACE2 (DEBUG_CTX, "gpgme_get_sub_protocol", ctx,
386 "ctx->sub_protocol=%i (%s)", ctx->sub_protocol,
387 gpgme_get_protocol_name (ctx->sub_protocol)
388 ? gpgme_get_protocol_name (ctx->sub_protocol) : "invalid");
390 return ctx->sub_protocol;
395 gpgme_get_protocol_name (gpgme_protocol_t protocol)
399 case GPGME_PROTOCOL_OpenPGP:
402 case GPGME_PROTOCOL_CMS:
405 case GPGME_PROTOCOL_GPGCONF:
408 case GPGME_PROTOCOL_ASSUAN:
411 case GPGME_PROTOCOL_G13:
414 case GPGME_PROTOCOL_UISERVER:
417 case GPGME_PROTOCOL_SPAWN:
420 case GPGME_PROTOCOL_DEFAULT:
423 case GPGME_PROTOCOL_UNKNOWN:
432 /* Store the sender's address in the context. ADDRESS is addr-spec of
433 * mailbox but my also be a complete mailbox, in which case this
434 * function extracts the addr-spec from it. Returns 0 on success or
435 * an error code if no valid addr-spec could be extracted from
438 gpgme_set_sender (gpgme_ctx_t ctx, const char *address)
442 TRACE_BEG1 (DEBUG_CTX, "gpgme_set_sender", ctx, "sender='%s'",
443 address?address:"(null)");
445 if (!ctx || (address && !(p = _gpgme_mailbox_from_userid (address))))
446 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
450 return TRACE_ERR (0);
454 /* Return the sender's address (addr-spec part) from the context or
455 * NULL if none was set. The returned value is valid as long as the
456 * CTX is valid and gpgme_set_sender has not been used. */
458 gpgme_get_sender (gpgme_ctx_t ctx)
460 TRACE1 (DEBUG_CTX, "gpgme_get_sender", ctx, "sender='%s'",
467 /* Enable or disable the use of an ascii armor for all output. */
469 gpgme_set_armor (gpgme_ctx_t ctx, int use_armor)
471 TRACE2 (DEBUG_CTX, "gpgme_set_armor", ctx, "use_armor=%i (%s)",
472 use_armor, use_armor ? "yes" : "no");
477 ctx->use_armor = !!use_armor;
481 /* Return the state of the armor flag. */
483 gpgme_get_armor (gpgme_ctx_t ctx)
485 TRACE2 (DEBUG_CTX, "gpgme_get_armor", ctx, "ctx->use_armor=%i (%s)",
486 ctx->use_armor, ctx->use_armor ? "yes" : "no");
487 return ctx->use_armor;
491 /* Set the flag NAME for CTX to VALUE. Please consult the manual for
492 * a description of the flags.
495 gpgme_set_ctx_flag (gpgme_ctx_t ctx, const char *name, const char *value)
497 gpgme_error_t err = 0;
500 TRACE2 (DEBUG_CTX, "gpgme_set_ctx_flag", ctx,
501 "name='%s' value='%s'",
502 name? name:"(null)", value?value:"(null)");
504 abool = (value && *value)? !!atoi (value) : 0;
506 if (!ctx || !name || !value)
507 err = gpg_error (GPG_ERR_INV_VALUE);
508 else if (!strcmp (name, "redraw"))
510 ctx->redraw_suggested = abool;
512 else if (!strcmp (name, "full-status"))
514 ctx->full_status = abool;
516 else if (!strcmp (name, "raw-description"))
518 ctx->raw_description = abool;
520 else if (!strcmp (name, "export-session-key"))
522 ctx->export_session_keys = abool;
524 else if (!strcmp (name, "override-session-key"))
526 free (ctx->override_session_key);
527 ctx->override_session_key = strdup (value);
528 if (!ctx->override_session_key)
529 err = gpg_error_from_syserror ();
531 else if (!strcmp (name, "auto-key-retrieve"))
533 ctx->auto_key_retrieve = abool;
535 else if (!strcmp (name, "request-origin"))
537 free (ctx->request_origin);
538 ctx->request_origin = strdup (value);
539 if (!ctx->request_origin)
540 err = gpg_error_from_syserror ();
542 else if (!strcmp (name, "no-symkey-cache"))
544 ctx->no_symkey_cache = abool;
546 else if (!strcmp (name, "ignore-mdc-error"))
548 ctx->ignore_mdc_error = abool;
550 else if (!strcmp (name, "auto-key-locate"))
552 free (ctx->auto_key_locate);
553 ctx->auto_key_locate = strdup (value);
554 if (!ctx->auto_key_locate)
555 err = gpg_error_from_syserror ();
558 err = gpg_error (GPG_ERR_UNKNOWN_NAME);
564 /* Get the context flag named NAME. See gpgme_set_ctx_flag for a list
565 * of valid names. If the NAME is unknown NULL is returned. For a
566 * boolean flag an empty string is returned for False and the string
567 * "1" for True; thus either atoi or a simple string test can be
570 gpgme_get_ctx_flag (gpgme_ctx_t ctx, const char *name)
574 else if (!strcmp (name, "redraw"))
576 return ctx->redraw_suggested? "1":"";
578 else if (!strcmp (name, "full-status"))
580 return ctx->full_status? "1":"";
582 else if (!strcmp (name, "raw-description"))
584 return ctx->raw_description? "1":"";
586 else if (!strcmp (name, "export-session-key"))
588 return ctx->export_session_keys? "1":"";
590 else if (!strcmp (name, "override-session-key"))
592 return ctx->override_session_key? ctx->override_session_key : "";
594 else if (!strcmp (name, "auto-key-retrieve"))
596 return ctx->auto_key_retrieve? "1":"";
598 else if (!strcmp (name, "request-origin"))
600 return ctx->request_origin? ctx->request_origin : "";
602 else if (!strcmp (name, "no-symkey-cache"))
604 return ctx->no_symkey_cache? "1":"";
606 else if (!strcmp (name, "ignore-mdc-error"))
608 return ctx->ignore_mdc_error? "1":"";
610 else if (!strcmp (name, "auto-key-locate"))
612 return ctx->auto_key_locate? ctx->auto_key_locate : "";
619 /* Enable or disable the use of the special textmode. Textmode is for
620 example used for the RFC2015 signatures; note that the updated RFC
621 3156 mandates that the MUA does some preparations so that textmode
622 is not needed anymore. */
624 gpgme_set_textmode (gpgme_ctx_t ctx, int use_textmode)
626 TRACE2 (DEBUG_CTX, "gpgme_set_textmode", ctx, "use_textmode=%i (%s)",
627 use_textmode, use_textmode ? "yes" : "no");
632 ctx->use_textmode = !!use_textmode;
635 /* Return the state of the textmode flag. */
637 gpgme_get_textmode (gpgme_ctx_t ctx)
639 TRACE2 (DEBUG_CTX, "gpgme_get_textmode", ctx, "ctx->use_textmode=%i (%s)",
640 ctx->use_textmode, ctx->use_textmode ? "yes" : "no");
641 return ctx->use_textmode;
645 /* Enable offline mode for this context. In offline mode dirmngr
648 gpgme_set_offline (gpgme_ctx_t ctx, int offline)
650 TRACE2 (DEBUG_CTX, "gpgme_set_offline", ctx, "offline=%i (%s)",
651 offline, offline ? "yes" : "no");
656 ctx->offline = !!offline;
659 /* Return the state of the offline flag. */
661 gpgme_get_offline (gpgme_ctx_t ctx)
663 TRACE2 (DEBUG_CTX, "gpgme_get_offline", ctx, "ctx->offline=%i (%s)",
664 ctx->offline, ctx->offline ? "yes" : "no");
669 /* Set the number of certifications to include in an S/MIME message.
670 The default is GPGME_INCLUDE_CERTS_DEFAULT. -1 means all certs,
671 and -2 means all certs except the root cert. */
673 gpgme_set_include_certs (gpgme_ctx_t ctx, int nr_of_certs)
678 if (nr_of_certs == GPGME_INCLUDE_CERTS_DEFAULT)
679 ctx->include_certs = GPGME_INCLUDE_CERTS_DEFAULT;
680 else if (nr_of_certs < -2)
681 ctx->include_certs = -2;
683 ctx->include_certs = nr_of_certs;
685 TRACE2 (DEBUG_CTX, "gpgme_set_include_certs", ctx, "nr_of_certs=%i%s",
686 nr_of_certs, nr_of_certs == ctx->include_certs ? "" : " (-2)");
690 /* Get the number of certifications to include in an S/MIME
693 gpgme_get_include_certs (gpgme_ctx_t ctx)
695 TRACE1 (DEBUG_CTX, "gpgme_get_include_certs", ctx, "ctx->include_certs=%i",
697 return ctx->include_certs;
701 /* This function changes the default behaviour of the keylisting
702 functions. MODE is a bitwise-OR of the GPGME_KEYLIST_* flags. The
703 default mode is GPGME_KEYLIST_MODE_LOCAL. */
705 gpgme_set_keylist_mode (gpgme_ctx_t ctx, gpgme_keylist_mode_t mode)
707 TRACE1 (DEBUG_CTX, "gpgme_set_keylist_mode", ctx, "keylist_mode=0x%x",
711 return gpg_error (GPG_ERR_INV_VALUE);
713 ctx->keylist_mode = mode;
717 /* This function returns the default behaviour of the keylisting
720 gpgme_get_keylist_mode (gpgme_ctx_t ctx)
722 TRACE1 (DEBUG_CTX, "gpgme_get_keylist_mode", ctx,
723 "ctx->keylist_mode=0x%x", ctx->keylist_mode);
724 return ctx->keylist_mode;
728 /* Set the pinentry mode for CTX to MODE. */
730 gpgme_set_pinentry_mode (gpgme_ctx_t ctx, gpgme_pinentry_mode_t mode)
732 TRACE1 (DEBUG_CTX, "gpgme_set_pinentry_mode", ctx, "pinentry_mode=%u",
736 return gpg_error (GPG_ERR_INV_VALUE);
740 case GPGME_PINENTRY_MODE_DEFAULT:
741 case GPGME_PINENTRY_MODE_ASK:
742 case GPGME_PINENTRY_MODE_CANCEL:
743 case GPGME_PINENTRY_MODE_ERROR:
744 case GPGME_PINENTRY_MODE_LOOPBACK:
747 return gpg_error (GPG_ERR_INV_VALUE);
750 ctx->pinentry_mode = mode;
755 /* Get the pinentry mode of CTX. */
756 gpgme_pinentry_mode_t
757 gpgme_get_pinentry_mode (gpgme_ctx_t ctx)
759 TRACE1 (DEBUG_CTX, "gpgme_get_pinentry_mode", ctx,
760 "ctx->pinentry_mode=%u", (unsigned int)ctx->pinentry_mode);
761 return ctx->pinentry_mode;
765 /* This function sets a callback function to be used to pass a
766 passphrase to gpg. */
768 gpgme_set_passphrase_cb (gpgme_ctx_t ctx, gpgme_passphrase_cb_t cb,
771 TRACE2 (DEBUG_CTX, "gpgme_set_passphrase_cb", ctx,
772 "passphrase_cb=%p/%p", cb, cb_value);
777 ctx->passphrase_cb = cb;
778 ctx->passphrase_cb_value = cb_value;
782 /* This function returns the callback function to be used to pass a
783 passphrase to the crypto engine. */
785 gpgme_get_passphrase_cb (gpgme_ctx_t ctx, gpgme_passphrase_cb_t *r_cb,
788 TRACE2 (DEBUG_CTX, "gpgme_get_passphrase_cb", ctx,
789 "ctx->passphrase_cb=%p/%p",
790 ctx->passphrase_cb, ctx->passphrase_cb_value);
792 *r_cb = ctx->passphrase_cb;
794 *r_cb_value = ctx->passphrase_cb_value;
798 /* This function sets a callback function to be used as a progress
801 gpgme_set_progress_cb (gpgme_ctx_t ctx, gpgme_progress_cb_t cb, void *cb_value)
803 TRACE2 (DEBUG_CTX, "gpgme_set_progress_cb", ctx, "progress_cb=%p/%p",
809 ctx->progress_cb = cb;
810 ctx->progress_cb_value = cb_value;
814 /* This function returns the callback function to be used as a
815 progress indicator. */
817 gpgme_get_progress_cb (gpgme_ctx_t ctx, gpgme_progress_cb_t *r_cb,
820 TRACE2 (DEBUG_CTX, "gpgme_get_progress_cb", ctx, "ctx->progress_cb=%p/%p",
821 ctx->progress_cb, ctx->progress_cb_value);
823 *r_cb = ctx->progress_cb;
825 *r_cb_value = ctx->progress_cb_value;
829 /* This function sets a callback function to be used as a status
830 message forwarder. */
832 gpgme_set_status_cb (gpgme_ctx_t ctx, gpgme_status_cb_t cb, void *cb_value)
834 TRACE2 (DEBUG_CTX, "gpgme_set_status_cb", ctx, "status_cb=%p/%p",
841 ctx->status_cb_value = cb_value;
845 /* This function returns the callback function to be used as a
846 status message forwarder. */
848 gpgme_get_status_cb (gpgme_ctx_t ctx, gpgme_status_cb_t *r_cb,
851 TRACE2 (DEBUG_CTX, "gpgme_get_status_cb", ctx, "ctx->status_cb=%p/%p",
852 ctx ? ctx->status_cb : NULL, ctx ? ctx->status_cb_value : NULL);
860 if (!ctx || !ctx->status_cb)
864 *r_cb = ctx->status_cb;
866 *r_cb_value = ctx->status_cb_value;
870 /* Set the I/O callback functions for CTX to IO_CBS. */
872 gpgme_set_io_cbs (gpgme_ctx_t ctx, gpgme_io_cbs_t io_cbs)
879 TRACE6 (DEBUG_CTX, "gpgme_set_io_cbs", ctx,
880 "io_cbs=%p (add=%p/%p, remove=%p, event=%p/%p",
881 io_cbs, io_cbs->add, io_cbs->add_priv, io_cbs->remove,
882 io_cbs->event, io_cbs->event_priv);
883 ctx->io_cbs = *io_cbs;
887 TRACE1 (DEBUG_CTX, "gpgme_set_io_cbs", ctx,
888 "io_cbs=%p (default)", io_cbs);
889 ctx->io_cbs.add = NULL;
890 ctx->io_cbs.add_priv = NULL;
891 ctx->io_cbs.remove = NULL;
892 ctx->io_cbs.event = NULL;
893 ctx->io_cbs.event_priv = NULL;
898 /* This function provides access to the internal read function; it is
899 normally not used. */
901 gpgme_io_read (int fd, void *buffer, size_t count)
904 TRACE_BEG2 (DEBUG_GLOBAL, "gpgme_io_read", fd,
905 "buffer=%p, count=%u", buffer, count);
907 ret = _gpgme_io_read (fd, buffer, count);
909 return TRACE_SYSRES (ret);
913 /* This function provides access to the internal write function. It
914 is to be used by user callbacks to return data to gpgme. See
915 gpgme_passphrase_cb_t and gpgme_edit_cb_t. */
917 gpgme_io_write (int fd, const void *buffer, size_t count)
920 TRACE_BEG2 (DEBUG_GLOBAL, "gpgme_io_write", fd,
921 "buffer=%p, count=%u", buffer, count);
923 ret = _gpgme_io_write (fd, buffer, count);
925 return TRACE_SYSRES (ret);
928 /* This function provides access to the internal write function. It
929 is to be used by user callbacks to return data to gpgme. See
930 gpgme_passphrase_cb_t and gpgme_edit_cb_t. Note that this is a
931 variant of gpgme_io_write which guarantees that all COUNT bytes are
932 written or an error is return. Returns: 0 on success or -1 on
933 error and the sets errno. */
935 gpgme_io_writen (int fd, const void *buffer_arg, size_t count)
937 const char *buffer = buffer_arg;
939 TRACE_BEG2 (DEBUG_GLOBAL, "gpgme_io_writen", fd,
940 "buffer=%p, count=%u", buffer, count);
943 ret = _gpgme_io_write (fd, buffer, count);
950 return TRACE_SYSRES (ret);
954 /* This function returns the callback function for I/O. */
956 gpgme_get_io_cbs (gpgme_ctx_t ctx, gpgme_io_cbs_t io_cbs)
958 TRACE6 (DEBUG_CTX, "gpgme_get_io_cbs", ctx,
959 "io_cbs=%p, ctx->io_cbs.add=%p/%p, .remove=%p, .event=%p/%p",
960 io_cbs, io_cbs->add, io_cbs->add_priv, io_cbs->remove,
961 io_cbs->event, io_cbs->event_priv);
963 *io_cbs = ctx->io_cbs;
967 /* This function sets the locale for the context CTX, or the default
968 locale if CTX is a null pointer. */
970 gpgme_set_locale (gpgme_ctx_t ctx, int category, const char *value)
973 char *new_lc_ctype = NULL;
974 char *new_lc_messages = NULL;
976 TRACE_BEG2 (DEBUG_CTX, "gpgme_set_locale", ctx,
977 "category=%i, value=%s", category, value ? value : "(null)");
979 #define PREPARE_ONE_LOCALE(lcat, ucat) \
980 if (!failed && value \
981 && (category == LC_ALL || category == LC_ ## ucat)) \
983 new_lc_ ## lcat = strdup (value); \
984 if (!new_lc_ ## lcat) \
989 PREPARE_ONE_LOCALE (ctype, CTYPE);
992 PREPARE_ONE_LOCALE (messages, MESSAGES);
997 int saved_err = gpg_error_from_syserror ();
1000 free (new_lc_ctype);
1001 if (new_lc_messages)
1002 free (new_lc_messages);
1004 return TRACE_ERR (saved_err);
1007 #define SET_ONE_LOCALE(lcat, ucat) \
1008 if (category == LC_ALL || category == LC_ ## ucat) \
1012 if (ctx->lc_ ## lcat) \
1013 free (ctx->lc_ ## lcat); \
1014 ctx->lc_ ## lcat = new_lc_ ## lcat; \
1018 if (def_lc_ ## lcat) \
1019 free (def_lc_ ## lcat); \
1020 def_lc_ ## lcat = new_lc_ ## lcat; \
1027 SET_ONE_LOCALE (ctype, CTYPE);
1030 SET_ONE_LOCALE (messages, MESSAGES);
1033 UNLOCK (def_lc_lock);
1035 return TRACE_ERR (0);
1039 /* Get the information about the configured engines. A pointer to the
1040 first engine in the statically allocated linked list is returned.
1041 The returned data is valid until the next gpgme_ctx_set_engine_info. */
1043 gpgme_ctx_get_engine_info (gpgme_ctx_t ctx)
1045 TRACE1 (DEBUG_CTX, "gpgme_ctx_get_engine_info", ctx,
1046 "ctx->engine_info=%p", ctx->engine_info);
1047 return ctx->engine_info;
1051 /* Set the engine info for the context CTX, protocol PROTO, to the
1052 file name FILE_NAME and the home directory HOME_DIR. */
1054 gpgme_ctx_set_engine_info (gpgme_ctx_t ctx, gpgme_protocol_t proto,
1055 const char *file_name, const char *home_dir)
1058 TRACE_BEG4 (DEBUG_CTX, "gpgme_ctx_set_engine_info", ctx,
1059 "protocol=%i (%s), file_name=%s, home_dir=%s",
1060 proto, gpgme_get_protocol_name (proto)
1061 ? gpgme_get_protocol_name (proto) : "unknown",
1062 file_name ? file_name : "(default)",
1063 home_dir ? home_dir : "(default)");
1066 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
1068 /* Shut down the engine when changing engine info. */
1071 TRACE_LOG1 ("releasing ctx->engine=%p", ctx->engine);
1072 _gpgme_engine_release (ctx->engine);
1075 err = _gpgme_set_engine_info (ctx->engine_info, proto,
1076 file_name, home_dir);
1077 return TRACE_ERR (err);
1081 /* Clear all notation data from the context. */
1083 _gpgme_sig_notation_clear (gpgme_ctx_t ctx)
1085 gpgme_sig_notation_t notation;
1090 notation = ctx->sig_notations;
1093 gpgme_sig_notation_t next_notation = notation->next;
1094 _gpgme_sig_notation_free (notation);
1095 notation = next_notation;
1097 ctx->sig_notations = NULL;
1101 gpgme_sig_notation_clear (gpgme_ctx_t ctx)
1103 TRACE (DEBUG_CTX, "gpgme_sig_notation_clear", ctx);
1108 _gpgme_sig_notation_clear (ctx);
1112 /* Add the human-readable notation data with name NAME and value VALUE
1113 to the context CTX, using the flags FLAGS. If NAME is NULL, then
1114 VALUE should be a policy URL. The flag
1115 GPGME_SIG_NOTATION_HUMAN_READABLE is forced to be true for notation
1116 data, and false for policy URLs. */
1118 gpgme_sig_notation_add (gpgme_ctx_t ctx, const char *name,
1119 const char *value, gpgme_sig_notation_flags_t flags)
1122 gpgme_sig_notation_t notation;
1123 gpgme_sig_notation_t *lastp;
1125 TRACE_BEG3 (DEBUG_CTX, "gpgme_sig_notation_add", ctx,
1126 "name=%s, value=%s, flags=0x%x",
1127 name ? name : "(null)", value ? value : "(null)",
1131 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
1134 flags |= GPGME_SIG_NOTATION_HUMAN_READABLE;
1136 flags &= ~GPGME_SIG_NOTATION_HUMAN_READABLE;
1138 err = _gpgme_sig_notation_create (¬ation, name, name ? strlen (name) : 0,
1139 value, value ? strlen (value) : 0, flags);
1141 return TRACE_ERR (err);
1143 lastp = &ctx->sig_notations;
1145 lastp = &(*lastp)->next;
1148 return TRACE_ERR (0);
1152 /* Get the sig notations for this context. */
1153 gpgme_sig_notation_t
1154 gpgme_sig_notation_get (gpgme_ctx_t ctx)
1158 TRACE (DEBUG_CTX, "gpgme_sig_notation_get", ctx);
1161 TRACE1 (DEBUG_CTX, "gpgme_sig_notation_get", ctx,
1162 "ctx->sig_notations=%p", ctx->sig_notations);
1164 return ctx->sig_notations;
1169 /* Return a public key algorithm string made of the algorithm and size
1170 or the curve name. May return NULL on error. Caller must free the
1171 result using gpgme_free. */
1173 gpgme_pubkey_algo_string (gpgme_subkey_t subkey)
1175 const char *prefix = NULL;
1180 gpg_err_set_errno (EINVAL);
1184 switch (subkey->pubkey_algo)
1187 case GPGME_PK_RSA_E:
1188 case GPGME_PK_RSA_S: prefix = "rsa"; break;
1189 case GPGME_PK_ELG_E: prefix = "elg"; break;
1190 case GPGME_PK_DSA: prefix = "dsa"; break;
1191 case GPGME_PK_ELG: prefix = "xxx"; break;
1194 case GPGME_PK_ECDSA:
1195 case GPGME_PK_EDDSA: prefix = ""; break;
1198 if (prefix && *prefix)
1201 snprintf (buffer, sizeof buffer, "%s%u", prefix, subkey->length);
1202 result = strdup (buffer);
1204 else if (prefix && subkey->curve && *subkey->curve)
1205 result = strdup (subkey->curve);
1207 result = strdup ("E_error");
1209 result = strdup ("unknown");
1216 gpgme_pubkey_algo_name (gpgme_pubkey_algo_t algo)
1220 case GPGME_PK_RSA: return "RSA";
1221 case GPGME_PK_RSA_E: return "RSA-E";
1222 case GPGME_PK_RSA_S: return "RSA-S";
1223 case GPGME_PK_ELG_E: return "ELG-E";
1224 case GPGME_PK_DSA: return "DSA";
1225 case GPGME_PK_ECC: return "ECC";
1226 case GPGME_PK_ELG: return "ELG";
1227 case GPGME_PK_ECDSA: return "ECDSA";
1228 case GPGME_PK_ECDH: return "ECDH";
1229 case GPGME_PK_EDDSA: return "EdDSA";
1230 default: return NULL;
1236 gpgme_hash_algo_name (gpgme_hash_algo_t algo)
1246 case GPGME_MD_RMD160:
1252 case GPGME_MD_TIGER:
1255 case GPGME_MD_HAVAL:
1258 case GPGME_MD_SHA256:
1261 case GPGME_MD_SHA384:
1264 case GPGME_MD_SHA512:
1267 case GPGME_MD_SHA224:
1273 case GPGME_MD_CRC32:
1276 case GPGME_MD_CRC32_RFC1510:
1277 return "CRC32RFC1510";
1279 case GPGME_MD_CRC24_RFC2440:
1280 return "CRC24RFC2440";