Imported Upstream version 2.1.10
[platform/upstream/gpg2.git] / g10 / call-dirmngr.c
1 /* call-dirmngr.c - GPG operations to the Dirmngr.
2  * Copyright (C) 2011 Free Software Foundation, Inc.
3  * Copyright (C) 2015  g10 Code GmbH
4  *
5  * This file is part of GnuPG.
6  *
7  * GnuPG is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * GnuPG is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <time.h>
28 #include <assert.h>
29 #ifdef HAVE_LOCALE_H
30 # include <locale.h>
31 #endif
32
33 #include "gpg.h"
34 #include <assuan.h>
35 #include "util.h"
36 #include "membuf.h"
37 #include "options.h"
38 #include "i18n.h"
39 #include "asshelp.h"
40 #include "keyserver.h"
41 #include "call-dirmngr.h"
42
43
44 /* Parameter structure used to gather status info.  */
45 struct ks_status_parm_s
46 {
47   const char *keyword; /* Look for this keyword or NULL for "SOURCE". */
48   char *source;
49 };
50
51
52 /* Parameter structure used with the KS_SEARCH command.  */
53 struct ks_search_parm_s
54 {
55   gpg_error_t lasterr;  /* Last error code.  */
56   membuf_t saveddata;   /* Buffer to build complete lines.  */
57   char *helpbuf;        /* NULL or malloced buffer.  */
58   size_t helpbufsize;   /* Allocated size of HELPBUF.  */
59   gpg_error_t (*data_cb)(void*, int, char*);  /* Callback.  */
60   void *data_cb_value;  /* First argument for DATA_CB.  */
61   struct ks_status_parm_s *stparm; /* Link to the status parameter.  */
62 };
63
64
65 /* Parameter structure used with the KS_GET command.  */
66 struct ks_get_parm_s
67 {
68   estream_t memfp;
69 };
70
71
72 /* Parameter structure used with the KS_PUT command.  */
73 struct ks_put_parm_s
74 {
75   assuan_context_t ctx;
76   kbnode_t keyblock;  /* The optional keyblock.  */
77   const void *data;   /* The key in OpenPGP binary format.  */
78   size_t datalen;     /* The length of DATA.  */
79 };
80
81
82 /* Parameter structure used with the DNS_CERT command.  */
83 struct dns_cert_parm_s
84 {
85   estream_t memfp;
86   unsigned char *fpr;
87   size_t fprlen;
88   char *url;
89 };
90
91
92 /* Data used to associate an session with dirmngr contexts.  We can't
93    use a simple one to one mapping because we sometimes need two
94    connections to the dirmngr; for example while doing a listing and
95    being in a data callback we may want to retrieve a key.  The local
96    dirmngr data takes care of this.  At the end of the session the
97    function dirmngr_deinit_session_data is called by gpg.c to cleanup
98    these resources.  Note that gpg.h defines a typedef dirmngr_local_t
99    for this structure. */
100 struct dirmngr_local_s
101 {
102   /* Link to other contexts which are used simultaneously.  */
103   struct dirmngr_local_s *next;
104
105   /* The active Assuan context. */
106   assuan_context_t ctx;
107
108   /* Flag set when the keyserver names have been send.  */
109   int set_keyservers_done;
110
111   /* Flag set to true while an operation is running on CTX.  */
112   int is_active;
113 };
114
115
116 \f
117 /* Deinitialize all session data of dirmngr pertaining to CTRL.  */
118 void
119 gpg_dirmngr_deinit_session_data (ctrl_t ctrl)
120 {
121   dirmngr_local_t dml;
122
123   while ((dml = ctrl->dirmngr_local))
124     {
125       ctrl->dirmngr_local = dml->next;
126       if (dml->is_active)
127         log_error ("oops: trying to cleanup an active dirmngr context\n");
128       else
129         assuan_release (dml->ctx);
130       xfree (dml);
131     }
132 }
133
134
135 /* Try to connect to the Dirmngr via a socket or spawn it if possible.
136    Handle the server's initial greeting and set global options.  */
137 static gpg_error_t
138 create_context (ctrl_t ctrl, assuan_context_t *r_ctx)
139 {
140   gpg_error_t err;
141   assuan_context_t ctx;
142
143   *r_ctx = NULL;
144   err = start_new_dirmngr (&ctx,
145                            GPG_ERR_SOURCE_DEFAULT,
146                            opt.homedir,
147                            opt.dirmngr_program,
148                            opt.autostart, opt.verbose, DBG_IPC,
149                            NULL /*gpg_status2*/, ctrl);
150   if (!opt.autostart && gpg_err_code (err) == GPG_ERR_NO_DIRMNGR)
151     {
152       static int shown;
153
154       if (!shown)
155         {
156           shown = 1;
157           log_info (_("no dirmngr running in this session\n"));
158         }
159     }
160   else if (!err)
161     {
162       char *line;
163
164       /* Tell the dirmngr that we want to collect audit event. */
165       /* err = assuan_transact (agent_ctx, "OPTION audit-events=1", */
166       /*                        NULL, NULL, NULL, NULL, NULL, NULL); */
167       if (opt.keyserver_options.http_proxy)
168         {
169           line = xtryasprintf ("OPTION http-proxy=%s",
170                                opt.keyserver_options.http_proxy);
171           if (!line)
172             err = gpg_error_from_syserror ();
173           else
174             {
175               err = assuan_transact (ctx, line, NULL, NULL, NULL,
176                                      NULL, NULL, NULL);
177               xfree (line);
178             }
179         }
180
181       if (err)
182         ;
183       else if ((opt.keyserver_options.options & KEYSERVER_HONOR_KEYSERVER_URL))
184         {
185           /* Tell the dirmngr that this possibly privacy invading
186              option is in use.  If Dirmngr is running in Tor mode, it
187              will return an error.  */
188           err = assuan_transact (ctx, "OPTION honor-keyserver-url-used",
189                                  NULL, NULL, NULL, NULL, NULL, NULL);
190           if (gpg_err_code (err) == GPG_ERR_FORBIDDEN)
191             log_error (_("keyserver option \"honor-keyserver-url\""
192                          " may not be used in Tor mode\n"));
193           else if (gpg_err_code (err) == GPG_ERR_UNKNOWN_OPTION)
194             err = 0; /* Old dirmngr versions do not support this option.  */
195         }
196     }
197
198   if (err)
199     assuan_release (ctx);
200   else
201     {
202       /* audit_log_ok (ctrl->audit, AUDIT_DIRMNGR_READY, err); */
203       *r_ctx = ctx;
204     }
205
206   return err;
207 }
208
209
210 /* Get a context for accessing dirmngr.  If no context is available a
211    new one is created and - if required - dirmngr started.  On success
212    an assuan context is stored at R_CTX.  This context may only be
213    released by means of close_context.  Note that NULL is stored at
214    R_CTX on error.  */
215 static gpg_error_t
216 open_context (ctrl_t ctrl, assuan_context_t *r_ctx)
217 {
218   gpg_error_t err;
219   dirmngr_local_t dml;
220
221   *r_ctx = NULL;
222   for (;;)
223     {
224       for (dml = ctrl->dirmngr_local; dml && dml->is_active; dml = dml->next)
225         ;
226       if (dml)
227         {
228           /* Found an inactive local session - return that.  */
229           assert (!dml->is_active);
230
231           /* But first do the per session init if not yet done.  */
232           if (!dml->set_keyservers_done)
233             {
234               keyserver_spec_t ksi;
235
236               /* Set all configured keyservers.  We clear existing
237                  keyservers so that any keyserver configured in GPG
238                  overrides keyservers possibly still configured in Dirmngr
239                  for the session (Note that the keyserver list of a
240                  session in Dirmngr survives a RESET. */
241               for (ksi = opt.keyserver; ksi; ksi = ksi->next)
242                 {
243                   char *line;
244
245                   line = xtryasprintf
246                     ("KEYSERVER%s %s",
247                      ksi == opt.keyserver? " --clear":"", ksi->uri);
248                   if (!line)
249                     err = gpg_error_from_syserror ();
250                   else
251                     {
252                       err = assuan_transact (dml->ctx, line, NULL, NULL, NULL,
253                                              NULL, NULL, NULL);
254                       xfree (line);
255                     }
256
257                   if (err)
258                     return err;
259                 }
260
261               dml->set_keyservers_done = 1;
262             }
263
264           dml->is_active = 1;
265
266           *r_ctx = dml->ctx;
267           return 0;
268         }
269
270       dml = xtrycalloc (1, sizeof *dml);
271       if (!dml)
272         return gpg_error_from_syserror ();
273       err = create_context (ctrl, &dml->ctx);
274       if (err)
275         {
276           xfree (dml);
277           return err;
278         }
279
280       /* To be on the nPth thread safe site we need to add it to a
281          list; this is far easier than to have a lock for this
282          function.  It should not happen anyway but the code is free
283          because we need it for the is_active check above.  */
284       dml->next = ctrl->dirmngr_local;
285       ctrl->dirmngr_local = dml;
286     }
287 }
288
289
290 /* Close the assuan context CTX or return it to a pool of unused
291    contexts.  If CTX is NULL, the function does nothing.  */
292 static void
293 close_context (ctrl_t ctrl, assuan_context_t ctx)
294 {
295   dirmngr_local_t dml;
296
297   if (!ctx)
298     return;
299
300   for (dml = ctrl->dirmngr_local; dml; dml = dml->next)
301     {
302       if (dml->ctx == ctx)
303         {
304           if (!dml->is_active)
305             log_fatal ("closing inactive dirmngr context %p\n", ctx);
306           dml->is_active = 0;
307           return;
308         }
309     }
310   log_fatal ("closing unknown dirmngr ctx %p\n", ctx);
311 }
312
313
314 /* Clear the set_keyservers_done flag on context CTX.  */
315 static void
316 clear_context_flags (ctrl_t ctrl, assuan_context_t ctx)
317 {
318   dirmngr_local_t dml;
319
320   if (!ctx)
321     return;
322
323   for (dml = ctrl->dirmngr_local; dml; dml = dml->next)
324     {
325       if (dml->ctx == ctx)
326         {
327           if (!dml->is_active)
328             log_fatal ("clear_context_flags on inactive dirmngr ctx %p\n", ctx);
329           dml->set_keyservers_done = 0;
330           return;
331         }
332     }
333   log_fatal ("clear_context_flags on unknown dirmngr ctx %p\n", ctx);
334 }
335
336
337 \f
338 /* Status callback for ks_list, ks_get and ks_search.  */
339 static gpg_error_t
340 ks_status_cb (void *opaque, const char *line)
341 {
342   struct ks_status_parm_s *parm = opaque;
343   gpg_error_t err = 0;
344   const char *s;
345
346   if ((s = has_leading_keyword (line, parm->keyword? parm->keyword : "SOURCE")))
347     {
348       if (!parm->source)
349         {
350           parm->source = xtrystrdup (s);
351           if (!parm->source)
352             err = gpg_error_from_syserror ();
353         }
354     }
355
356   return err;
357 }
358
359
360 \f
361 /* Run the "KEYSERVER" command to return the name of the used
362    keyserver at R_KEYSERVER.  */
363 gpg_error_t
364 gpg_dirmngr_ks_list (ctrl_t ctrl, char **r_keyserver)
365 {
366   gpg_error_t err;
367   assuan_context_t ctx;
368   struct ks_status_parm_s stparm;
369
370   memset (&stparm, 0, sizeof stparm);
371   stparm.keyword = "KEYSERVER";
372   *r_keyserver = NULL;
373
374   err = open_context (ctrl, &ctx);
375   if (err)
376     return err;
377
378   err = assuan_transact (ctx, "KEYSERVER", NULL, NULL,
379                          NULL, NULL, ks_status_cb, &stparm);
380   if (err)
381     goto leave;
382   if (!stparm.source)
383     {
384       err = gpg_error (GPG_ERR_NO_KEYSERVER);
385       goto leave;
386     }
387
388   *r_keyserver = stparm.source;
389   stparm.source = NULL;
390
391  leave:
392   xfree (stparm.source);
393   close_context (ctrl, ctx);
394   return err;
395 }
396
397
398 \f
399 /* Data callback for the KS_SEARCH command. */
400 static gpg_error_t
401 ks_search_data_cb (void *opaque, const void *data, size_t datalen)
402 {
403   gpg_error_t err = 0;
404   struct ks_search_parm_s *parm = opaque;
405   const char *line, *s;
406   size_t rawlen, linelen;
407   char fixedbuf[256];
408
409   if (parm->lasterr)
410     return 0;
411
412   if (parm->stparm->source)
413     {
414       err = parm->data_cb (parm->data_cb_value, 1, parm->stparm->source);
415       if (err)
416         {
417           parm->lasterr = err;
418           return err;
419         }
420       /* Clear it so that we won't get back here unless the server
421          accidentally sends a second source status line.  Note that
422          will not see all accidentally sent source lines because it
423          depends on whether data lines have been send in between.  */
424       xfree (parm->stparm->source);
425       parm->stparm->source = NULL;
426     }
427
428   if (!data)
429     return 0;  /* Ignore END commands.  */
430
431   put_membuf (&parm->saveddata, data, datalen);
432
433  again:
434   line = peek_membuf (&parm->saveddata, &rawlen);
435   if (!line)
436     {
437       parm->lasterr = gpg_error_from_syserror ();
438       return parm->lasterr; /* Tell the server about our problem.  */
439     }
440   if ((s = memchr (line, '\n', rawlen)))
441     {
442       linelen = s - line;  /* That is the length excluding the LF.  */
443       if (linelen + 1 < sizeof fixedbuf)
444         {
445           /* We can use the static buffer.  */
446           memcpy (fixedbuf, line, linelen);
447           fixedbuf[linelen] = 0;
448           if (linelen && fixedbuf[linelen-1] == '\r')
449             fixedbuf[linelen-1] = 0;
450           err = parm->data_cb (parm->data_cb_value, 0, fixedbuf);
451         }
452       else
453         {
454           if (linelen + 1 >= parm->helpbufsize)
455             {
456               xfree (parm->helpbuf);
457               parm->helpbufsize = linelen + 1 + 1024;
458               parm->helpbuf = xtrymalloc (parm->helpbufsize);
459               if (!parm->helpbuf)
460                 {
461                   parm->lasterr = gpg_error_from_syserror ();
462                   return parm->lasterr;
463                 }
464             }
465           memcpy (parm->helpbuf, line, linelen);
466           parm->helpbuf[linelen] = 0;
467           if (linelen && parm->helpbuf[linelen-1] == '\r')
468             parm->helpbuf[linelen-1] = 0;
469           err = parm->data_cb (parm->data_cb_value, 0, parm->helpbuf);
470         }
471       if (err)
472         parm->lasterr = err;
473       else
474         {
475           clear_membuf (&parm->saveddata, linelen+1);
476           goto again;  /* There might be another complete line.  */
477         }
478     }
479
480   return err;
481 }
482
483
484 /* Run the KS_SEARCH command using the search string SEARCHSTR.  All
485    data lines are passed to the CB function.  That function is called
486    with CB_VALUE as its first argument, a 0 as second argument, and
487    the decoded data line as third argument.  The callback function may
488    modify the data line and it is guaranteed that this data line is a
489    complete line with a terminating 0 character but without the
490    linefeed.  NULL is passed to the callback to indicate EOF.  */
491 gpg_error_t
492 gpg_dirmngr_ks_search (ctrl_t ctrl, const char *searchstr,
493                        gpg_error_t (*cb)(void*, int, char *), void *cb_value)
494 {
495   gpg_error_t err;
496   assuan_context_t ctx;
497   struct ks_status_parm_s stparm;
498   struct ks_search_parm_s parm;
499   char line[ASSUAN_LINELENGTH];
500
501   err = open_context (ctrl, &ctx);
502   if (err)
503     return err;
504
505   {
506     char *escsearchstr = percent_plus_escape (searchstr);
507     if (!escsearchstr)
508       {
509         err = gpg_error_from_syserror ();
510         close_context (ctrl, ctx);
511         return err;
512       }
513     snprintf (line, sizeof line, "KS_SEARCH -- %s", escsearchstr);
514     xfree (escsearchstr);
515   }
516
517   memset (&stparm, 0, sizeof stparm);
518   memset (&parm, 0, sizeof parm);
519   init_membuf (&parm.saveddata, 1024);
520   parm.data_cb = cb;
521   parm.data_cb_value = cb_value;
522   parm.stparm = &stparm;
523
524   err = assuan_transact (ctx, line, ks_search_data_cb, &parm,
525                         NULL, NULL, ks_status_cb, &stparm);
526   if (!err)
527     err = cb (cb_value, 0, NULL);  /* Send EOF.  */
528
529   xfree (get_membuf (&parm.saveddata, NULL));
530   xfree (parm.helpbuf);
531   xfree (stparm.source);
532
533   close_context (ctrl, ctx);
534   return err;
535 }
536
537
538 \f
539 /* Data callback for the KS_GET and KS_FETCH commands. */
540 static gpg_error_t
541 ks_get_data_cb (void *opaque, const void *data, size_t datalen)
542 {
543   gpg_error_t err = 0;
544   struct ks_get_parm_s *parm = opaque;
545   size_t nwritten;
546
547   if (!data)
548     return 0;  /* Ignore END commands.  */
549
550   if (es_write (parm->memfp, data, datalen, &nwritten))
551     err = gpg_error_from_syserror ();
552
553   return err;
554 }
555
556
557 /* Run the KS_GET command using the patterns in the array PATTERN.  On
558    success an estream object is returned to retrieve the keys.  On
559    error an error code is returned and NULL stored at R_FP.
560
561    The pattern may only use search specification which a keyserver can
562    use to retrieve keys.  Because we know the format of the pattern we
563    don't need to escape the patterns before sending them to the
564    server.
565
566    If R_SOURCE is not NULL the source of the data is stored as a
567    malloced string there.  If a source is not known NULL is stored.
568
569    If there are too many patterns the function returns an error.  That
570    could be fixed by issuing several search commands or by
571    implementing a different interface.  However with long keyids we
572    are able to ask for (1000-10-1)/(2+8+1) = 90 keys at once.  */
573 gpg_error_t
574 gpg_dirmngr_ks_get (ctrl_t ctrl, char **pattern,
575                     keyserver_spec_t override_keyserver,
576                     estream_t *r_fp, char **r_source)
577 {
578   gpg_error_t err;
579   assuan_context_t ctx;
580   struct ks_status_parm_s stparm;
581   struct ks_get_parm_s parm;
582   char *line = NULL;
583   size_t linelen;
584   membuf_t mb;
585   int idx;
586
587   memset (&stparm, 0, sizeof stparm);
588   memset (&parm, 0, sizeof parm);
589
590   *r_fp = NULL;
591   if (r_source)
592     *r_source = NULL;
593
594   err = open_context (ctrl, &ctx);
595   if (err)
596     return err;
597
598   /* If we have an override keyserver we first indicate that the next
599      user of the context needs to again setup the global keyservers and
600      them we send the override keyserver.  */
601   if (override_keyserver)
602     {
603       clear_context_flags (ctrl, ctx);
604       line = xtryasprintf ("KEYSERVER --clear %s", override_keyserver->uri);
605       if (!line)
606         {
607           err = gpg_error_from_syserror ();
608           goto leave;
609         }
610       err = assuan_transact (ctx, line, NULL, NULL, NULL,
611                              NULL, NULL, NULL);
612       if (err)
613         goto leave;
614
615       xfree (line);
616       line = NULL;
617     }
618
619   /* Lump all patterns into one string.  */
620   init_membuf (&mb, 1024);
621   put_membuf_str (&mb, "KS_GET --");
622   for (idx=0; pattern[idx]; idx++)
623     {
624       put_membuf (&mb, " ", 1); /* Append Delimiter.  */
625       put_membuf_str (&mb, pattern[idx]);
626     }
627   put_membuf (&mb, "", 1); /* Append Nul.  */
628   line = get_membuf (&mb, &linelen);
629   if (!line)
630     {
631       err = gpg_error_from_syserror ();
632       goto leave;
633     }
634   if (linelen + 2 >= ASSUAN_LINELENGTH)
635     {
636       err = gpg_error (GPG_ERR_TOO_MANY);
637       goto leave;
638     }
639
640   parm.memfp = es_fopenmem (0, "rwb");
641   if (!parm.memfp)
642     {
643       err = gpg_error_from_syserror ();
644       goto leave;
645     }
646   err = assuan_transact (ctx, line, ks_get_data_cb, &parm,
647                          NULL, NULL, ks_status_cb, &stparm);
648   if (err)
649     goto leave;
650
651   es_rewind (parm.memfp);
652   *r_fp = parm.memfp;
653   parm.memfp = NULL;
654
655   if (r_source)
656     {
657       *r_source = stparm.source;
658       stparm.source = NULL;
659     }
660
661  leave:
662   es_fclose (parm.memfp);
663   xfree (stparm.source);
664   xfree (line);
665   close_context (ctrl, ctx);
666   return err;
667 }
668
669
670 /* Run the KS_FETCH and pass URL as argument.  On success an estream
671    object is returned to retrieve the keys.  On error an error code is
672    returned and NULL stored at R_FP.
673
674    The url is expected to point to a small set of keys; in many cases
675    only to one key.  However, schemes like finger may return several
676    keys.  Note that the configured keyservers are ignored by the
677    KS_FETCH command.  */
678 gpg_error_t
679 gpg_dirmngr_ks_fetch (ctrl_t ctrl, const char *url, estream_t *r_fp)
680 {
681   gpg_error_t err;
682   assuan_context_t ctx;
683   struct ks_get_parm_s parm;
684   char *line = NULL;
685
686   memset (&parm, 0, sizeof parm);
687
688   *r_fp = NULL;
689
690   err = open_context (ctrl, &ctx);
691   if (err)
692     return err;
693
694   line = strconcat ("KS_FETCH -- ", url, NULL);
695   if (!line)
696     {
697       err = gpg_error_from_syserror ();
698       goto leave;
699     }
700   if (strlen (line) + 2 >= ASSUAN_LINELENGTH)
701     {
702       err = gpg_error (GPG_ERR_TOO_LARGE);
703       goto leave;
704     }
705
706   parm.memfp = es_fopenmem (0, "rwb");
707   if (!parm.memfp)
708     {
709       err = gpg_error_from_syserror ();
710       goto leave;
711     }
712   err = assuan_transact (ctx, line, ks_get_data_cb, &parm,
713                          NULL, NULL, NULL, NULL);
714   if (err)
715     goto leave;
716
717   es_rewind (parm.memfp);
718   *r_fp = parm.memfp;
719   parm.memfp = NULL;
720
721  leave:
722   es_fclose (parm.memfp);
723   xfree (line);
724   close_context (ctrl, ctx);
725   return err;
726 }
727
728
729 \f
730 static void
731 record_output (estream_t output,
732                pkttype_t type,
733                const char *validity,
734                /* The public key length or -1.  */
735                int pub_key_length,
736                /* The public key algo or -1.  */
737                int pub_key_algo,
738                /* 2 ulongs or NULL.  */
739                const u32 *keyid,
740                /* The creation / expiration date or 0.  */
741                u32 creation_date,
742                u32 expiration_date,
743                const char *userid)
744 {
745   const char *type_str = NULL;
746   char *pub_key_length_str = NULL;
747   char *pub_key_algo_str = NULL;
748   char *keyid_str = NULL;
749   char *creation_date_str = NULL;
750   char *expiration_date_str = NULL;
751   char *userid_escaped = NULL;
752
753   switch (type)
754     {
755     case PKT_PUBLIC_KEY:
756       type_str = "pub";
757       break;
758     case PKT_PUBLIC_SUBKEY:
759       type_str = "sub";
760       break;
761     case PKT_USER_ID:
762       type_str = "uid";
763       break;
764     case PKT_SIGNATURE:
765       type_str = "sig";
766       break;
767     default:
768       assert (! "Unhandled type.");
769     }
770
771   if (pub_key_length > 0)
772     pub_key_length_str = xasprintf ("%d", pub_key_length);
773
774   if (pub_key_algo != -1)
775     pub_key_algo_str = xasprintf ("%d", pub_key_algo);
776
777   if (keyid)
778     keyid_str = xasprintf ("%08lX%08lX", (ulong) keyid[0], (ulong) keyid[1]);
779
780   if (creation_date)
781     creation_date_str = xstrdup (colon_strtime (creation_date));
782
783   if (expiration_date)
784     expiration_date_str = xstrdup (colon_strtime (expiration_date));
785
786   /* Quote ':', '%', and any 8-bit characters.  */
787   if (userid)
788     {
789       int r;
790       int w = 0;
791
792       int len = strlen (userid);
793       /* A 100k character limit on the uid should be way more than
794          enough.  */
795       if (len > 100 * 1024)
796         len = 100 * 1024;
797
798       /* The minimum amount of space that we need.  */
799       userid_escaped = xmalloc (len * 3 + 1);
800
801       for (r = 0; r < len; r++)
802         {
803           if (userid[r] == ':' || userid[r]== '%' || (userid[r] & 0x80))
804             {
805               sprintf (&userid_escaped[w], "%%%02X", (byte) userid[r]);
806               w += 3;
807             }
808           else
809             userid_escaped[w ++] = userid[r];
810         }
811       userid_escaped[w] = '\0';
812     }
813
814   es_fprintf (output, "%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s\n",
815               type_str,
816               validity ?: "",
817               pub_key_length_str ?: "",
818               pub_key_algo_str ?: "",
819               keyid_str ?: "",
820               creation_date_str ?: "",
821               expiration_date_str ?: "",
822               "" /* Certificate S/N */,
823               "" /* Ownertrust.  */,
824               userid_escaped ?: "",
825               "" /* Signature class.  */,
826               "" /* Key capabilities.  */,
827               "" /* Issuer certificate fingerprint.  */,
828               "" /* Flag field.  */,
829               "" /* S/N of a token.  */,
830               "" /* Hash algo.  */,
831               "" /* Curve name.  */);
832
833   xfree (userid_escaped);
834   xfree (expiration_date_str);
835   xfree (creation_date_str);
836   xfree (keyid_str);
837   xfree (pub_key_algo_str);
838   xfree (pub_key_length_str);
839 }
840
841 /* Handle the KS_PUT inquiries. */
842 static gpg_error_t
843 ks_put_inq_cb (void *opaque, const char *line)
844 {
845   struct ks_put_parm_s *parm = opaque;
846   gpg_error_t err = 0;
847
848   if (has_leading_keyword (line, "KEYBLOCK"))
849     {
850       if (parm->data)
851         err = assuan_send_data (parm->ctx, parm->data, parm->datalen);
852     }
853   else if (has_leading_keyword (line, "KEYBLOCK_INFO"))
854     {
855       kbnode_t node;
856       estream_t fp;
857
858       /* Parse the keyblock and send info lines back to the server.  */
859       fp = es_fopenmem (0, "rw,samethread");
860       if (!fp)
861         err = gpg_error_from_syserror ();
862
863       /* Note: the output format for the INFO block follows the colon
864          format as described in doc/DETAILS.  We don't actually reuse
865          the functionality from g10/keylist.c to produce the output,
866          because we don't need all of it and some of it is quite
867          expensive to generate.
868
869          The fields are (the starred fields are the ones we need):
870
871            * Field 1 - Type of record
872            * Field 2 - Validity
873            * Field 3 - Key length
874            * Field 4 - Public key algorithm
875            * Field 5 - KeyID
876            * Field 6 - Creation date
877            * Field 7 - Expiration date
878              Field 8 - Certificate S/N, UID hash, trust signature info
879              Field 9 -  Ownertrust
880            * Field 10 - User-ID
881              Field 11 - Signature class
882              Field 12 - Key capabilities
883              Field 13 - Issuer certificate fingerprint or other info
884              Field 14 - Flag field
885              Field 15 - S/N of a token
886              Field 16 - Hash algorithm
887              Field 17 - Curve name
888        */
889       for (node = parm->keyblock; !err && node; node=node->next)
890         {
891           switch (node->pkt->pkttype)
892             {
893             case PKT_PUBLIC_KEY:
894             case PKT_PUBLIC_SUBKEY:
895               {
896                 PKT_public_key *pk = node->pkt->pkt.public_key;
897
898                 char validity[3];
899                 int i;
900
901                 i = 0;
902                 if (pk->flags.revoked)
903                   validity[i ++] = 'r';
904                 if (pk->has_expired)
905                   validity[i ++] = 'e';
906                 validity[i] = '\0';
907
908                 keyid_from_pk (pk, NULL);
909
910                 record_output (fp, node->pkt->pkttype, validity,
911                                nbits_from_pk (pk), pk->pubkey_algo,
912                                pk->keyid, pk->timestamp, pk->expiredate,
913                                NULL);
914               }
915               break;
916
917             case PKT_USER_ID:
918               {
919                 PKT_user_id *uid = node->pkt->pkt.user_id;
920
921                 if (!uid->attrib_data)
922                   {
923                     char validity[3];
924                     int i;
925
926                     i = 0;
927                     if (uid->is_revoked)
928                       validity[i ++] = 'r';
929                     if (uid->is_expired)
930                       validity[i ++] = 'e';
931                     validity[i] = '\0';
932
933                     record_output (fp, node->pkt->pkttype, validity,
934                                    -1, -1, NULL,
935                                    uid->created, uid->expiredate,
936                                    uid->name);
937                   }
938               }
939               break;
940
941               /* This bit is really for the benefit of people who
942                  store their keys in LDAP servers.  It makes it easy
943                  to do queries for things like "all keys signed by
944                  Isabella".  */
945             case PKT_SIGNATURE:
946               {
947                 PKT_signature *sig = node->pkt->pkt.signature;
948
949                 if (IS_UID_SIG (sig))
950                   record_output (fp, node->pkt->pkttype, NULL,
951                                  -1, -1, sig->keyid,
952                                  sig->timestamp, sig->expiredate, NULL);
953               }
954               break;
955
956             default:
957               continue;
958             }
959           /* Given that the last operation was an es_fprintf we should
960              get the correct ERRNO if ferror indicates an error.  */
961           if (es_ferror (fp))
962             err = gpg_error_from_syserror ();
963         }
964
965       /* Without an error and if we have an keyblock at all, send the
966          data back.  */
967       if (!err && parm->keyblock)
968         {
969           int rc;
970           char buffer[512];
971           size_t nread;
972
973           es_rewind (fp);
974           while (!(rc=es_read (fp, buffer, sizeof buffer, &nread)) && nread)
975             {
976               err = assuan_send_data (parm->ctx, buffer, nread);
977               if (err)
978                 break;
979             }
980           if (!err && rc)
981             err = gpg_error_from_syserror ();
982         }
983       es_fclose (fp);
984     }
985   else
986     return gpg_error (GPG_ERR_ASS_UNKNOWN_INQUIRE);
987
988   return err;
989 }
990
991
992 /* Send a key to the configured server.  {DATA,DATLEN} contains the
993    key in OpenPGP binary transport format.  If KEYBLOCK is not NULL it
994    has the internal representaion of that key; this is for example
995    used to convey meta data to LDAP keyservers.  */
996 gpg_error_t
997 gpg_dirmngr_ks_put (ctrl_t ctrl, void *data, size_t datalen, kbnode_t keyblock)
998 {
999   gpg_error_t err;
1000   assuan_context_t ctx;
1001   struct ks_put_parm_s parm;
1002
1003   memset (&parm, 0, sizeof parm);
1004
1005   /* We are going to parse the keyblock, thus we better make sure the
1006      all information is readily available.  */
1007   if (keyblock)
1008     merge_keys_and_selfsig (keyblock);
1009
1010   err = open_context (ctrl, &ctx);
1011   if (err)
1012     return err;
1013
1014   parm.ctx = ctx;
1015   parm.keyblock = keyblock;
1016   parm.data = data;
1017   parm.datalen = datalen;
1018
1019   err = assuan_transact (ctx, "KS_PUT", NULL, NULL,
1020                          ks_put_inq_cb, &parm, NULL, NULL);
1021
1022   close_context (ctrl, ctx);
1023   return err;
1024 }
1025
1026
1027 \f
1028 /* Data callback for the DNS_CERT command. */
1029 static gpg_error_t
1030 dns_cert_data_cb (void *opaque, const void *data, size_t datalen)
1031 {
1032   struct dns_cert_parm_s *parm = opaque;
1033   gpg_error_t err = 0;
1034   size_t nwritten;
1035
1036   if (!data)
1037     return 0;  /* Ignore END commands.  */
1038   if (!parm->memfp)
1039     return 0;  /* Data is not required.  */
1040
1041   if (es_write (parm->memfp, data, datalen, &nwritten))
1042     err = gpg_error_from_syserror ();
1043
1044   return err;
1045 }
1046
1047
1048 /* Status callback for the DNS_CERT command.  */
1049 static gpg_error_t
1050 dns_cert_status_cb (void *opaque, const char *line)
1051 {
1052   struct dns_cert_parm_s *parm = opaque;
1053   gpg_error_t err = 0;
1054   const char *s;
1055   size_t nbytes;
1056
1057   if ((s = has_leading_keyword (line, "FPR")))
1058     {
1059       char *buf;
1060
1061       if (!(buf = xtrystrdup (s)))
1062         err = gpg_error_from_syserror ();
1063       else if (parm->fpr)
1064         err = gpg_error (GPG_ERR_DUP_KEY);
1065       else if (!hex2str (buf, buf, strlen (buf)+1, &nbytes))
1066         err = gpg_error_from_syserror ();
1067       else if (nbytes < 20)
1068         err = gpg_error (GPG_ERR_TOO_SHORT);
1069       else
1070         {
1071           parm->fpr = xtrymalloc (nbytes);
1072           if (!parm->fpr)
1073             err = gpg_error_from_syserror ();
1074           else
1075             memcpy (parm->fpr, buf, (parm->fprlen = nbytes));
1076         }
1077       xfree (buf);
1078     }
1079   else if ((s = has_leading_keyword (line, "URL")) && *s)
1080     {
1081       if (parm->url)
1082         err = gpg_error (GPG_ERR_DUP_KEY);
1083       else if (!(parm->fpr = xtrymalloc (nbytes)))
1084         err = gpg_error_from_syserror ();
1085       else
1086         memcpy (parm->fpr, line, (parm->fprlen = nbytes));
1087     }
1088
1089   return err;
1090 }
1091
1092 /* Ask the dirmngr for a DNS CERT record.  Depending on the found
1093    subtypes different return values are set:
1094
1095    - For a PGP subtype a new estream with that key will be returned at
1096      R_KEY and the other return parameters are set to NULL/0.
1097
1098    - For an IPGP subtype the fingerprint is stored as a malloced block
1099      at (R_FPR,R_FPRLEN).  If an URL is available it is stored as a
1100      malloced string at R_URL; NULL is stored if there is no URL.
1101
1102    If CERTTYPE is DNS_CERTTYPE_ANY this function returns the first
1103    CERT record found with a supported type; it is expected that only
1104    one CERT record is used.  If CERTTYPE is one of the supported
1105    certtypes, only records with this certtype are considered and the
1106    first one found is returned.  All R_* args are optional.
1107
1108    If CERTTYPE is NULL the DANE method is used to fetch the key.
1109  */
1110 gpg_error_t
1111 gpg_dirmngr_dns_cert (ctrl_t ctrl, const char *name, const char *certtype,
1112                       estream_t *r_key,
1113                       unsigned char **r_fpr, size_t *r_fprlen,
1114                       char **r_url)
1115 {
1116   gpg_error_t err;
1117   assuan_context_t ctx;
1118   struct dns_cert_parm_s parm;
1119   char *line = NULL;
1120
1121   memset (&parm, 0, sizeof parm);
1122   if (r_key)
1123     *r_key = NULL;
1124   if (r_fpr)
1125     *r_fpr = NULL;
1126   if (r_fprlen)
1127     *r_fprlen = 0;
1128   if (r_url)
1129     *r_url = NULL;
1130
1131   err = open_context (ctrl, &ctx);
1132   if (err)
1133     return err;
1134
1135   line = es_bsprintf ("DNS_CERT %s %s", certtype? certtype : "--dane", name);
1136   if (!line)
1137     {
1138       err = gpg_error_from_syserror ();
1139       goto leave;
1140     }
1141   if (strlen (line) + 2 >= ASSUAN_LINELENGTH)
1142     {
1143       err = gpg_error (GPG_ERR_TOO_LARGE);
1144       goto leave;
1145     }
1146
1147   parm.memfp = es_fopenmem (0, "rwb");
1148   if (!parm.memfp)
1149     {
1150       err = gpg_error_from_syserror ();
1151       goto leave;
1152     }
1153   err = assuan_transact (ctx, line, dns_cert_data_cb, &parm,
1154                          NULL, NULL, dns_cert_status_cb, &parm);
1155   if (err)
1156     goto leave;
1157
1158   if (r_key)
1159     {
1160       es_rewind (parm.memfp);
1161       *r_key = parm.memfp;
1162       parm.memfp = NULL;
1163     }
1164
1165   if (r_fpr && parm.fpr)
1166     {
1167       *r_fpr = parm.fpr;
1168       parm.fpr = NULL;
1169     }
1170   if (r_fprlen)
1171     *r_fprlen = parm.fprlen;
1172
1173   if (r_url && parm.url)
1174     {
1175       *r_url = parm.url;
1176       parm.url = NULL;
1177     }
1178
1179  leave:
1180   xfree (parm.fpr);
1181   xfree (parm.url);
1182   es_fclose (parm.memfp);
1183   xfree (line);
1184   close_context (ctrl, ctx);
1185   return err;
1186 }
1187
1188
1189 /* Ask the dirmngr for PKA info.  On success the retrieved fingerprint
1190    is returned in a malloced buffer at R_FPR and its length is stored
1191    at R_FPRLEN.  If an URL is available it is stored as a malloced
1192    string at R_URL.  On error all return values are set to NULL/0.  */
1193 gpg_error_t
1194 gpg_dirmngr_get_pka (ctrl_t ctrl, const char *userid,
1195                      unsigned char **r_fpr, size_t *r_fprlen,
1196                      char **r_url)
1197 {
1198   gpg_error_t err;
1199   assuan_context_t ctx;
1200   struct dns_cert_parm_s parm;
1201   char *line = NULL;
1202
1203   memset (&parm, 0, sizeof parm);
1204   if (r_fpr)
1205     *r_fpr = NULL;
1206   if (r_fprlen)
1207     *r_fprlen = 0;
1208   if (r_url)
1209     *r_url = NULL;
1210
1211   err = open_context (ctrl, &ctx);
1212   if (err)
1213     return err;
1214
1215   line = es_bsprintf ("DNS_CERT --pka -- %s", userid);
1216   if (!line)
1217     {
1218       err = gpg_error_from_syserror ();
1219       goto leave;
1220     }
1221   if (strlen (line) + 2 >= ASSUAN_LINELENGTH)
1222     {
1223       err = gpg_error (GPG_ERR_TOO_LARGE);
1224       goto leave;
1225     }
1226
1227   err = assuan_transact (ctx, line, dns_cert_data_cb, &parm,
1228                          NULL, NULL, dns_cert_status_cb, &parm);
1229   if (err)
1230     goto leave;
1231
1232   if (r_fpr && parm.fpr)
1233     {
1234       *r_fpr = parm.fpr;
1235       parm.fpr = NULL;
1236     }
1237   if (r_fprlen)
1238     *r_fprlen = parm.fprlen;
1239
1240   if (r_url && parm.url)
1241     {
1242       *r_url = parm.url;
1243       parm.url = NULL;
1244     }
1245
1246  leave:
1247   xfree (parm.fpr);
1248   xfree (parm.url);
1249   xfree (line);
1250   close_context (ctrl, ctx);
1251   return err;
1252 }