5aa879a42d19b166a9940c47df86193f282e35af
[gpgme.git] / gpgme / gpgme.c
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
4
5    This file is part of GPGME.
6  
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.
11    
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.
16    
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
20    02111-1307, USA.  */
21
22 #if HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <errno.h>
30 #include <locale.h>
31
32 #include "util.h"
33 #include "context.h"
34 #include "ops.h"
35 #include "wait.h"
36 #include "debug.h"
37
38 \f
39 /* The default locale.  */
40 DEFINE_STATIC_LOCK (def_lc_lock);
41 static char *def_lc_ctype;
42 static char *def_lc_messages;
43
44 \f
45 /* Create a new context as an environment for GPGME crypto
46    operations.  */
47 gpgme_error_t
48 gpgme_new (gpgme_ctx_t *r_ctx)
49 {
50   gpgme_ctx_t ctx;
51   TRACE_BEG (DEBUG_CTX, "gpgme_new", r_ctx);
52
53   ctx = calloc (1, sizeof *ctx);
54   if (!ctx)
55     return TRACE_ERR (gpg_error_from_errno (errno));
56
57   INIT_LOCK (ctx->lock);
58   
59   _gpgme_engine_info_copy (&ctx->engine_info);
60   if (!ctx->engine_info)
61     {
62       free (ctx);
63       return TRACE_ERR (gpg_error_from_errno (errno));
64     }
65
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);
70
71   LOCK (def_lc_lock);
72   if (def_lc_ctype)
73     {
74       ctx->lc_ctype = strdup (def_lc_ctype);
75       if (!ctx->lc_ctype)
76         {
77           UNLOCK (def_lc_lock);
78           _gpgme_engine_info_release (ctx->engine_info);
79           free (ctx);
80           return TRACE_ERR (gpg_error_from_errno (errno));
81         }
82     }
83   else
84     def_lc_ctype = NULL;
85
86   if (def_lc_messages)
87     {
88       ctx->lc_messages = strdup (def_lc_messages);
89       if (!ctx->lc_messages)
90         {
91           UNLOCK (def_lc_lock);
92           if (ctx->lc_ctype)
93             free (ctx->lc_ctype);
94           _gpgme_engine_info_release (ctx->engine_info);
95           free (ctx);
96           return TRACE_ERR (gpg_error_from_errno (errno));
97         }
98     }
99   else
100     def_lc_messages = NULL;
101   UNLOCK (def_lc_lock);
102
103   *r_ctx = ctx;
104
105   return TRACE_SUC1 ("ctx=%p", ctx);
106 }
107
108
109 gpgme_error_t
110 _gpgme_cancel_with_err (gpgme_ctx_t ctx, gpg_error_t ctx_err)
111 {
112   gpgme_error_t err;
113   TRACE_BEG1 (DEBUG_CTX, "_gpgme_cancel_with_err", ctx, "ctx_err=%i",
114               ctx_err);
115
116   err = _gpgme_engine_cancel (ctx->engine);
117   if (err)
118     return TRACE_ERR (err);
119
120   _gpgme_engine_io_event (ctx->engine, GPGME_EVENT_DONE, &ctx_err);
121
122   return TRACE_ERR (0);
123 }
124
125
126 /* Cancel a pending asynchronous operation.  */
127 gpgme_error_t
128 gpgme_cancel (gpgme_ctx_t ctx)
129 {
130   return _gpgme_cancel_with_err (ctx, gpg_error (GPG_ERR_CANCELED));
131 }
132
133
134 /* Cancel a pending operation asynchronously.  */
135 gpgme_error_t
136 gpgme_cancel_async (gpgme_ctx_t ctx)
137 {
138   TRACE_BEG (DEBUG_CTX, "gpgme_cancel_async", ctx);
139
140   LOCK (ctx->lock);
141   ctx->canceled = 1;
142   UNLOCK (ctx->lock);
143
144   return TRACE_ERR (0);
145 }
146
147
148 /* Release all resources associated with the given context.  */
149 void
150 gpgme_release (gpgme_ctx_t ctx)
151 {
152   TRACE (DEBUG_CTX, "gpgme_release", ctx);
153
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);
159   if (ctx->signers)
160     free (ctx->signers);
161   if (ctx->lc_ctype)
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);
167   free (ctx);
168 }
169
170
171 void
172 _gpgme_release_result (gpgme_ctx_t ctx)
173 {
174   struct ctx_op_data *data = ctx->op_data;
175
176   while (data)
177     {
178       struct ctx_op_data *next_data = data->next;
179       if (data->cleanup)
180         (*data->cleanup) (data->hook);
181       free (data);
182       data = next_data;
183     }
184   ctx->op_data = NULL;
185 }
186
187
188 gpgme_error_t
189 gpgme_set_protocol (gpgme_ctx_t ctx, gpgme_protocol_t protocol)
190 {
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");
194
195   if (protocol != GPGME_PROTOCOL_OpenPGP && protocol != GPGME_PROTOCOL_CMS)
196     return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
197
198   if (ctx->protocol != protocol)
199     {
200       /* Shut down the engine when switching protocols.  */
201       if (ctx->engine)
202         {
203           TRACE_LOG1 ("releasing ctx->engine=%p", ctx->engine);
204           _gpgme_engine_release (ctx->engine);
205           ctx->engine = NULL;
206         }
207
208       ctx->protocol = protocol;
209     }
210   return TRACE_ERR (0);
211 }
212
213
214 gpgme_protocol_t
215 gpgme_get_protocol (gpgme_ctx_t ctx)
216 {
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;
222 }
223
224
225 const char *
226 gpgme_get_protocol_name (gpgme_protocol_t protocol)
227 {
228   switch (protocol)
229     {
230     case GPGME_PROTOCOL_OpenPGP:
231       return "OpenPGP";
232
233     case GPGME_PROTOCOL_CMS:
234       return "CMS";
235
236     case GPGME_PROTOCOL_UNKNOWN:
237       return "unknown";
238
239     default:
240       return NULL;
241     }
242 }
243
244 /* Enable or disable the use of an ascii armor for all output.  */
245 void
246 gpgme_set_armor (gpgme_ctx_t ctx, int use_armor)
247 {
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;
251 }
252
253
254 /* Return the state of the armor flag.  */
255 int
256 gpgme_get_armor (gpgme_ctx_t ctx)
257 {
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;
261 }
262
263
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.  */
268 void
269 gpgme_set_textmode (gpgme_ctx_t ctx, int use_textmode)
270 {
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;
274 }
275
276 /* Return the state of the textmode flag.  */
277 int
278 gpgme_get_textmode (gpgme_ctx_t ctx)
279 {
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;
283 }
284
285
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.  */
289 void
290 gpgme_set_include_certs (gpgme_ctx_t ctx, int nr_of_certs)
291 {
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;
296   else
297     ctx->include_certs = nr_of_certs;
298
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)");
301 }
302
303
304 /* Get the number of certifications to include in an S/MIME
305    message.  */
306 int
307 gpgme_get_include_certs (gpgme_ctx_t ctx)
308 {
309   TRACE1 (DEBUG_CTX, "gpgme_get_include_certs", ctx, "ctx->include_certs=%i",
310           ctx->include_certs);
311   return ctx->include_certs;
312 }
313
314
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.  */
318 gpgme_error_t
319 gpgme_set_keylist_mode (gpgme_ctx_t ctx, gpgme_keylist_mode_t mode)
320 {
321   TRACE1 (DEBUG_CTX, "gpgme_set_keylist_mode", ctx, "keylist_mode=0x%x",
322           mode);
323
324   ctx->keylist_mode = mode;
325   return 0;
326 }
327
328 /* This function returns the default behaviour of the keylisting
329    functions.  */
330 gpgme_keylist_mode_t
331 gpgme_get_keylist_mode (gpgme_ctx_t ctx)
332 {
333   TRACE1 (DEBUG_CTX, "gpgme_get_keylist_mode", ctx,
334           "ctx->keylist_mode=0x%x", ctx->keylist_mode);
335   return ctx->keylist_mode;
336 }
337
338
339 /* This function sets a callback function to be used to pass a
340    passphrase to gpg.  */
341 void
342 gpgme_set_passphrase_cb (gpgme_ctx_t ctx, gpgme_passphrase_cb_t cb,
343                          void *cb_value)
344 {
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;
349 }
350
351
352 /* This function returns the callback function to be used to pass a
353    passphrase to the crypto engine.  */
354 void
355 gpgme_get_passphrase_cb (gpgme_ctx_t ctx, gpgme_passphrase_cb_t *r_cb,
356                          void **r_cb_value)
357 {
358   TRACE2 (DEBUG_CTX, "gpgme_get_passphrase_cb", ctx,
359           "ctx->passphrase_cb=%p/%p",
360           ctx->passphrase_cb, ctx->passphrase_cb_value);
361   if (r_cb)
362     *r_cb = ctx->passphrase_cb;
363   if (r_cb_value)
364     *r_cb_value = ctx->passphrase_cb_value;
365 }
366
367
368 /* This function sets a callback function to be used as a progress
369    indicator.  */
370 void
371 gpgme_set_progress_cb (gpgme_ctx_t ctx, gpgme_progress_cb_t cb, void *cb_value)
372 {
373   TRACE2 (DEBUG_CTX, "gpgme_set_progress_cb", ctx, "progress_cb=%p/%p",
374           cb, cb_value);
375   ctx->progress_cb = cb;
376   ctx->progress_cb_value = cb_value;
377 }
378
379
380 /* This function returns the callback function to be used as a
381    progress indicator.  */
382 void
383 gpgme_get_progress_cb (gpgme_ctx_t ctx, gpgme_progress_cb_t *r_cb,
384                        void **r_cb_value)
385 {
386   TRACE2 (DEBUG_CTX, "gpgme_get_progress_cb", ctx, "ctx->progress_cb=%p/%p",
387           ctx->progress_cb, ctx->progress_cb_value);
388   if (r_cb)
389     *r_cb = ctx->progress_cb;
390   if (r_cb_value)
391     *r_cb_value = ctx->progress_cb_value;
392 }
393
394
395 /* Set the I/O callback functions for CTX to IO_CBS.  */
396 void
397 gpgme_set_io_cbs (gpgme_ctx_t ctx, gpgme_io_cbs_t io_cbs)
398 {
399   if (io_cbs)
400     {
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;
406     }
407   else
408     {
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;
416     }
417 }
418
419
420 /* This function returns the callback function for I/O.  */
421 void
422 gpgme_get_io_cbs (gpgme_ctx_t ctx, gpgme_io_cbs_t io_cbs)
423 {
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);
428
429   *io_cbs = ctx->io_cbs;
430 }
431
432 \f
433 /* This function sets the locale for the context CTX, or the default
434    locale if CTX is a null pointer.  */
435 gpgme_error_t
436 gpgme_set_locale (gpgme_ctx_t ctx, int category, const char *value)
437 {
438   int failed = 0;
439   char *new_lc_ctype = NULL;
440   char *new_lc_messages = NULL;
441
442   TRACE_BEG2 (DEBUG_CTX, "gpgme_set_locale", ctx,
443                "category=%i, value=%s", category, value ? value : "(null)");
444
445 #define PREPARE_ONE_LOCALE(lcat, ucat)                          \
446   if (!failed && value                                          \
447       && (category == LC_ALL || category == LC_ ## ucat))       \
448     {                                                           \
449       new_lc_ ## lcat = strdup (value);                         \
450       if (!new_lc_ ## lcat)                                     \
451         failed = 1;                                             \
452     }
453
454   PREPARE_ONE_LOCALE (ctype, CTYPE);
455 #ifdef LC_MESSAGES
456   PREPARE_ONE_LOCALE (messages, MESSAGES);
457 #endif
458
459   if (failed)
460     {
461       int saved_errno = errno;
462
463       if (new_lc_ctype)
464         free (new_lc_ctype);
465       if (new_lc_messages)
466         free (new_lc_messages);
467
468       return TRACE_ERR (gpg_error_from_errno (saved_errno));
469     }
470
471 #define SET_ONE_LOCALE(lcat, ucat)                      \
472   if (category == LC_ALL || category == LC_ ## ucat)    \
473     {                                                   \
474       if (ctx)                                          \
475         {                                               \
476           if (ctx->lc_ ## lcat)                         \
477             free (ctx->lc_ ## lcat);                    \
478           ctx->lc_ ## lcat = new_lc_ ## lcat;           \
479         }                                               \
480       else                                              \
481         {                                               \
482           if (def_lc_ ## lcat)                          \
483             free (def_lc_ ## lcat);                     \
484           def_lc_ ## lcat = new_lc_ ## lcat;            \
485         }                                               \
486     }
487
488   if (!ctx)
489     LOCK (def_lc_lock);
490   SET_ONE_LOCALE (ctype, CTYPE);
491 #ifdef LC_MESSAGES
492   SET_ONE_LOCALE (messages, MESSAGES);
493 #endif
494   if (!ctx)
495     UNLOCK (def_lc_lock);
496
497   return TRACE_ERR (0);
498 }
499
500 \f
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.  */
504 gpgme_engine_info_t
505 gpgme_ctx_get_engine_info (gpgme_ctx_t ctx)
506 {
507   TRACE1 (DEBUG_CTX, "gpgme_ctx_get_engine_info", ctx,
508           "ctx->engine_info=%p", ctx->engine_info);
509   return ctx->engine_info;
510 }
511
512
513 /* Set the engine info for the context CTX, protocol PROTO, to the
514    file name FILE_NAME and the home directory HOME_DIR.  */
515 gpgme_error_t
516 gpgme_ctx_set_engine_info (gpgme_ctx_t ctx, gpgme_protocol_t proto,
517                            const char *file_name, const char *home_dir)
518 {
519   gpgme_error_t err;
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)");
526               
527   /* Shut down the engine when changing engine info.  */
528   if (ctx->engine)
529     {
530       TRACE_LOG1 ("releasing ctx->engine=%p", ctx->engine);
531       _gpgme_engine_release (ctx->engine);
532       ctx->engine = NULL;
533     }
534   err = _gpgme_set_engine_info (ctx->engine_info, proto,
535                                 file_name, home_dir);
536   return TRACE_ERR (err);
537 }
538
539 \f
540 /* Clear all notation data from the context.  */
541 void
542 gpgme_sig_notation_clear (gpgme_ctx_t ctx)
543 {
544   gpgme_sig_notation_t notation;
545   TRACE (DEBUG_CTX, "gpgme_sig_notation_clear", ctx);
546
547   if (!ctx)
548     return;
549
550   notation = ctx->sig_notations;
551   while (notation)
552     {
553       gpgme_sig_notation_t next_notation = notation->next;
554       _gpgme_sig_notation_free (notation);
555       notation = next_notation;
556     }
557 }
558
559
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.  */
565 gpgme_error_t
566 gpgme_sig_notation_add (gpgme_ctx_t ctx, const char *name,
567                         const char *value, gpgme_sig_notation_flags_t flags)
568 {
569   gpgme_error_t err;
570   gpgme_sig_notation_t notation;
571   gpgme_sig_notation_t *lastp;
572
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)",
576               flags);
577   
578   if (!ctx)
579     return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
580
581   if (name)
582     flags |= GPGME_SIG_NOTATION_HUMAN_READABLE;
583   else
584     flags &= ~GPGME_SIG_NOTATION_HUMAN_READABLE;
585
586   err = _gpgme_sig_notation_create (&notation, name, name ? strlen (name) : 0,
587                                     value, value ? strlen (value) : 0, flags);
588   if (err)
589     return TRACE_ERR (err);
590
591   lastp = &ctx->sig_notations;
592   while (*lastp)
593     lastp = &(*lastp)->next;
594
595   *lastp = notation;
596   return TRACE_ERR (0);
597 }
598
599
600 /* Get the sig notations for this context.  */
601 gpgme_sig_notation_t
602 gpgme_sig_notation_get (gpgme_ctx_t ctx)
603 {
604   if (!ctx)
605     {
606       TRACE (DEBUG_CTX, "gpgme_sig_notation_get", ctx);
607       return NULL;
608     }
609   TRACE1 (DEBUG_CTX, "gpgme_sig_notation_get", ctx,
610           "ctx->sig_notations=%p", ctx->sig_notations);
611
612   return ctx->sig_notations;
613 }
614   
615 \f
616 const char *
617 gpgme_pubkey_algo_name (gpgme_pubkey_algo_t algo)
618 {
619   switch (algo)
620     {
621     case GPGME_PK_RSA:
622       return "RSA";
623
624     case GPGME_PK_RSA_E:
625       return "RSA-E";
626
627     case GPGME_PK_RSA_S:
628       return "RSA-S";
629
630     case GPGME_PK_ELG_E:
631       return "ELG-E";
632
633     case GPGME_PK_DSA:
634       return "DSA";
635
636     case GPGME_PK_ELG:
637       return "ELG";
638
639     default:
640       return NULL;
641     }
642 }
643
644
645 const char *
646 gpgme_hash_algo_name (gpgme_hash_algo_t algo)
647 {
648   switch (algo)
649     {
650     case GPGME_MD_MD5:
651       return "MD5";
652
653     case GPGME_MD_SHA1:
654       return "SHA1";
655
656     case GPGME_MD_RMD160:
657       return "RIPEMD160";
658
659     case GPGME_MD_MD2:
660       return "MD2";
661
662     case GPGME_MD_TIGER:
663       return "TIGER192";
664
665     case GPGME_MD_HAVAL:
666       return "HAVAL";
667
668     case GPGME_MD_SHA256:
669       return "SHA256";
670
671     case GPGME_MD_SHA384:
672       return "SHA384";
673
674     case GPGME_MD_SHA512:
675       return "SHA512";
676
677     case GPGME_MD_MD4:
678       return "MD4";
679
680     case GPGME_MD_CRC32:
681       return "CRC32";
682
683     case GPGME_MD_CRC32_RFC1510:
684       return "CRC32RFC1510";
685
686     case GPGME_MD_CRC24_RFC2440:
687       return "CRC24RFC2440";
688
689     default:
690       return NULL;
691     }
692 }