Imported Upstream version 2.4.3
[platform/upstream/gpg2.git] / dirmngr / t-http.c
1 /* t-http.c
2  * Copyright (C) 1999, 2001, 2002, 2003, 2004, 2006, 2009, 2010,
3  *               2011 Free Software Foundation, Inc.
4  * Copyright (C) 2014 Werner Koch
5  *
6  * This file is part of GnuPG.
7  *
8  * This file is free software; you can redistribute it and/or modify
9  * it under the terms of either
10  *
11  *   - the GNU Lesser General Public License as published by the Free
12  *     Software Foundation; either version 3 of the License, or (at
13  *     your option) any later version.
14  *
15  * or
16  *
17  *   - the GNU General Public License as published by the Free
18  *     Software Foundation; either version 2 of the License, or (at
19  *     your option) any later version.
20  *
21  * or both in parallel, as here.
22  *
23  * This file is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, see <https://www.gnu.org/licenses/>.
30  */
31
32 #include <config.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #include <assuan.h>
40
41 #include "../common/util.h"
42 #include "../common/logging.h"
43 #include "dns-stuff.h"
44 #include "http.h"
45
46 #include <ksba.h>
47 #if HTTP_USE_NTBTLS
48 # include <ntbtls.h>
49 #elif HTTP_USE_GNUTLS
50 # include <gnutls/gnutls.h>  /* For init, logging, and deinit.  */
51 #endif /*HTTP_USE_GNUTLS*/
52
53 #define PGM "t-http"
54
55 static int verbose;
56 static int debug;
57 static int no_verify;
58
59 /* static void */
60 /* read_dh_params (const char *fname) */
61 /* { */
62 /*   gpg_error_t err; */
63 /*   int rc; */
64 /*   FILE *fp; */
65 /*   struct stat st; */
66 /*   char *buf; */
67 /*   size_t buflen; */
68 /*   gnutls_datum_t datum; */
69
70 /*   fp = fopen (fname, "rb"); */
71 /*   if (!fp) */
72 /*     { */
73 /*       err = gpg_error_from_syserror (); */
74 /*       log_fatal ("can't open '%s': %s\n", fname, gpg_strerror (err)); */
75 /*     } */
76
77 /*   if (fstat (fileno(fp), &st)) */
78 /*     { */
79 /*       err = gpg_error_from_syserror (); */
80 /*       log_fatal ("can't stat '%s': %s\n", fname, gpg_strerror (err)); */
81 /*     } */
82
83 /*   buflen = st.st_size; */
84 /*   buf = xmalloc (buflen+1); */
85 /*   if (fread (buf, buflen, 1, fp) != 1) */
86 /*     { */
87 /*       err = gpg_error_from_syserror (); */
88 /*       log_fatal ("error reading '%s': %s\n", fname, gpg_strerror (err)); */
89 /*     } */
90 /*   fclose (fp); */
91
92 /*   datum.size = buflen; */
93 /*   datum.data = buf; */
94
95 /*   rc = gnutls_dh_params_import_pkcs3 (dh_params, &datum, GNUTLS_X509_FMT_PEM); */
96 /*   if (rc < 0) */
97 /*     log_fatal ("gnutls_dh_param_import failed: %s\n", gnutls_strerror (rc)); */
98
99 /*   xfree (buf); */
100 /* } */
101
102
103
104 #if HTTP_USE_GNUTLS
105 static gpg_error_t
106 verify_callback (http_t hd, http_session_t session, int reserved)
107 {
108   (void)hd;
109   (void)reserved;
110   return no_verify? 0 : http_verify_server_credentials (session);
111 }
112 #endif
113
114 #if HTTP_USE_GNUTLS
115 static void
116 my_gnutls_log (int level, const char *text)
117 {
118   fprintf (stderr, "gnutls:L%d: %s", level, text);
119 }
120 #endif
121
122 #if HTTP_USE_NTBTLS
123 static gpg_error_t
124 my_http_tls_verify_cb (void *opaque,
125                        http_t http,
126                        http_session_t session,
127                        unsigned int http_flags,
128                        void *tls_context)
129 {
130   gpg_error_t err;
131   int idx;
132   ksba_cert_t cert;
133   ksba_cert_t hostcert = NULL;
134
135   (void)opaque;
136   (void)http;
137   (void)session;
138   (void)http_flags;
139
140   /* Get the peer's certs from ntbtls.  */
141   for (idx = 0;
142        (cert = ntbtls_x509_get_peer_cert (tls_context, idx)); idx++)
143     {
144       if (!idx)
145         {
146           log_info ("Received host certificate\n");
147           hostcert = cert;
148         }
149       else
150         {
151
152           log_info ("Received additional certificate\n");
153           ksba_cert_release (cert);
154         }
155     }
156   if (!idx)
157     {
158       err  = gpg_error (GPG_ERR_MISSING_CERT);
159       goto leave;
160     }
161
162   err = 0;
163
164  leave:
165   ksba_cert_release (hostcert);
166   log_info ("my_http_tls_verify_cb returns: %s\n", gpg_strerror (err));
167   return err;
168 }
169 #endif /*HTTP_USE_NTBTLS*/
170
171
172
173 /* Prepend FNAME with the srcdir environment variable's value and
174    return an allocated filename. */
175 static char *
176 prepend_srcdir (const char *fname)
177 {
178   static const char *srcdir;
179   char *result;
180
181   if (!srcdir && !(srcdir = getenv ("srcdir")))
182     srcdir = ".";
183
184   result = xmalloc (strlen (srcdir) + 1 + strlen (fname) + 1);
185   strcpy (result, srcdir);
186   strcat (result, "/");
187   strcat (result, fname);
188   return result;
189 }
190
191
192 int
193 main (int argc, char **argv)
194 {
195   int last_argc = -1;
196   gpg_error_t err;
197   int rc;  parsed_uri_t uri;
198   uri_tuple_t r;
199   http_t hd;
200   int c;
201   unsigned int my_http_flags = 0;
202   int no_out = 0;
203   int tls_dbg = 0;
204   int no_crl = 0;
205   const char *cafile = NULL;
206   http_session_t session = NULL;
207   unsigned int timeout = 0;
208
209   gpgrt_init ();
210   log_set_prefix (PGM, GPGRT_LOG_WITH_PREFIX | GPGRT_LOG_WITH_PID);
211   if (argc)
212     { argc--; argv++; }
213   while (argc && last_argc != argc )
214     {
215       last_argc = argc;
216       if (!strcmp (*argv, "--"))
217         {
218           argc--; argv++;
219           break;
220         }
221       else if (!strcmp (*argv, "--help"))
222         {
223           fputs ("usage: " PGM " URL\n"
224                  "Options:\n"
225                  "  --verbose         print timings etc.\n"
226                  "  --debug           flyswatter\n"
227                  "  --tls-debug N     use TLS debug level N\n"
228                  "  --cacert FNAME    expect CA certificate in file FNAME\n"
229                  "  --timeout MS      timeout for connect in MS\n"
230                  "  --no-verify       do not verify the certificate\n"
231                  "  --force-tls       use HTTP_FLAG_FORCE_TLS\n"
232                  "  --force-tor       use HTTP_FLAG_FORCE_TOR\n"
233                  "  --no-out          do not print the content\n"
234                  "  --no-crl          do not consuilt a CRL\n",
235                  stdout);
236           exit (0);
237         }
238       else if (!strcmp (*argv, "--verbose"))
239         {
240           verbose++;
241           argc--; argv++;
242         }
243       else if (!strcmp (*argv, "--debug"))
244         {
245           verbose += 2;
246           debug++;
247           argc--; argv++;
248         }
249       else if (!strcmp (*argv, "--tls-debug"))
250         {
251           argc--; argv++;
252           if (argc)
253             {
254               tls_dbg = atoi (*argv);
255               argc--; argv++;
256             }
257         }
258       else if (!strcmp (*argv, "--cacert"))
259         {
260           argc--; argv++;
261           if (argc)
262             {
263               cafile = *argv;
264               argc--; argv++;
265             }
266         }
267       else if (!strcmp (*argv, "--timeout"))
268         {
269           argc--; argv++;
270           if (argc)
271             {
272               timeout = strtoul (*argv, NULL, 10);
273               argc--; argv++;
274             }
275         }
276       else if (!strcmp (*argv, "--no-verify"))
277         {
278           no_verify = 1;
279           argc--; argv++;
280         }
281       else if (!strcmp (*argv, "--force-tls"))
282         {
283           my_http_flags |= HTTP_FLAG_FORCE_TLS;
284           argc--; argv++;
285         }
286       else if (!strcmp (*argv, "--force-tor"))
287         {
288           my_http_flags |= HTTP_FLAG_FORCE_TOR;
289           argc--; argv++;
290         }
291       else if (!strcmp (*argv, "--no-out"))
292         {
293           no_out = 1;
294           argc--; argv++;
295         }
296       else if (!strcmp (*argv, "--no-crl"))
297         {
298           no_crl = 1;
299           argc--; argv++;
300         }
301       else if (!strncmp (*argv, "--", 2))
302         {
303           fprintf (stderr, PGM ": unknown option '%s'\n", *argv);
304           exit (1);
305         }
306     }
307   if (argc != 1)
308     {
309       fprintf (stderr, PGM ": no or too many URLS given\n");
310       exit (1);
311     }
312
313   if (!cafile)
314     cafile = prepend_srcdir ("tls-ca.pem");
315
316   if (verbose)
317     my_http_flags |= HTTP_FLAG_LOG_RESP;
318
319   if (verbose || debug)
320     http_set_verbose (verbose, debug);
321
322   /* http.c makes use of the assuan socket wrapper.  */
323   assuan_sock_init ();
324
325   if ((my_http_flags & HTTP_FLAG_FORCE_TOR))
326     {
327       enable_dns_tormode (1);
328       if (assuan_sock_set_flag (ASSUAN_INVALID_FD, "tor-mode", 1))
329         {
330           log_error ("error enabling Tor mode: %s\n", strerror (errno));
331           log_info ("(is your Libassuan recent enough?)\n");
332         }
333     }
334
335 #if HTTP_USE_NTBTLS
336   log_info ("new session.\n");
337   err = http_session_new (&session, NULL,
338                           ((no_crl? HTTP_FLAG_NO_CRL : 0)
339                            | HTTP_FLAG_TRUST_DEF),
340                           my_http_tls_verify_cb, NULL);
341   if (err)
342     log_error ("http_session_new failed: %s\n", gpg_strerror (err));
343   ntbtls_set_debug (tls_dbg, NULL, NULL);
344
345 #elif HTTP_USE_GNUTLS
346
347   rc = gnutls_global_init ();
348   if (rc)
349     log_error ("gnutls_global_init failed: %s\n", gnutls_strerror (rc));
350
351   http_register_tls_callback (verify_callback);
352   http_register_tls_ca (cafile);
353
354   err = http_session_new (&session, NULL,
355                           ((no_crl? HTTP_FLAG_NO_CRL : 0)
356                            | HTTP_FLAG_TRUST_DEF),
357                           NULL, NULL);
358   if (err)
359     log_error ("http_session_new failed: %s\n", gpg_strerror (err));
360
361   /* rc = gnutls_dh_params_init(&dh_params); */
362   /* if (rc) */
363   /*   log_error ("gnutls_dh_params_init failed: %s\n", gnutls_strerror (rc)); */
364   /* read_dh_params ("dh_param.pem"); */
365
366   /* rc = gnutls_certificate_set_x509_trust_file */
367   /*   (certcred, "ca.pem", GNUTLS_X509_FMT_PEM); */
368   /* if (rc) */
369   /*   log_error ("gnutls_certificate_set_x509_trust_file failed: %s\n", */
370   /*              gnutls_strerror (rc)); */
371
372   /* gnutls_certificate_set_dh_params (certcred, dh_params); */
373
374   gnutls_global_set_log_function (my_gnutls_log);
375   if (tls_dbg)
376     gnutls_global_set_log_level (tls_dbg);
377
378 #else
379   (void)err;
380   (void)tls_dbg;
381   (void)no_crl;
382 #endif /*HTTP_USE_GNUTLS*/
383
384   rc = http_parse_uri (&uri, *argv, HTTP_PARSE_NO_SCHEME_CHECK);
385   if (rc)
386     {
387       log_error ("'%s': %s\n", *argv, gpg_strerror (rc));
388       return 1;
389     }
390
391   printf ("Scheme: %s\n", uri->scheme);
392   if (uri->opaque)
393     printf ("Value : %s\n", uri->path);
394   else
395     {
396       printf ("Auth  : %s\n", uri->auth? uri->auth:"[none]");
397       printf ("Host  : %s (off=%hu)\n", uri->host, uri->off_host);
398       printf ("Port  : %u\n", uri->port);
399       printf ("Path  : %s (off=%hu)\n", uri->path, uri->off_path);
400       for (r = uri->params; r; r = r->next)
401         {
402           printf ("Params: %s", r->name);
403           if (!r->no_value)
404             {
405               printf ("=%s", r->value);
406               if (strlen (r->value) != r->valuelen)
407                 printf (" [real length=%d]", (int) r->valuelen);
408             }
409           putchar ('\n');
410         }
411       for (r = uri->query; r; r = r->next)
412         {
413           printf ("Query : %s", r->name);
414           if (!r->no_value)
415             {
416               printf ("=%s", r->value);
417               if (strlen (r->value) != r->valuelen)
418                 printf (" [real length=%d]", (int) r->valuelen);
419             }
420           putchar ('\n');
421         }
422       printf ("Flags :%s%s%s%s%s\n",
423               uri->is_http? " http":"",
424               uri->is_ldap? " ldap":"",
425               uri->opaque?  " opaque":"",
426               uri->v6lit?   " v6lit":"",
427               uri->onion?   " onion":"");
428       printf ("TLS   : %s\n",
429               uri->use_tls? "yes":
430               (my_http_flags&HTTP_FLAG_FORCE_TLS)? "forced" : "no");
431       printf ("Tor   : %s\n",
432               (my_http_flags&HTTP_FLAG_FORCE_TOR)? "yes" : "no");
433
434     }
435   fflush (stdout);
436   http_release_parsed_uri (uri);
437   uri = NULL;
438
439   if (session)
440     http_session_set_timeout (session, timeout);
441
442   rc = http_open_document (NULL, &hd, *argv, NULL, my_http_flags,
443                            NULL, session, NULL, NULL);
444   if (rc)
445     {
446       log_error ("can't get '%s': %s\n", *argv, gpg_strerror (rc));
447       return 1;
448     }
449   log_info ("open_http_document succeeded; status=%u\n",
450             http_get_status_code (hd));
451
452   {
453     const char **names;
454     int i;
455
456     names = http_get_header_names (hd);
457     if (!names)
458       log_fatal ("http_get_header_names failed: %s\n",
459                  gpg_strerror (gpg_error_from_syserror ()));
460     for (i = 0; names[i]; i++)
461       printf ("HDR: %s: %s\n", names[i], http_get_header (hd, names[i]));
462     xfree (names);
463   }
464   fflush (stdout);
465
466   switch (http_get_status_code (hd))
467     {
468     case 200:
469     case 400:
470     case 401:
471     case 403:
472     case 404:
473       {
474         unsigned long count = 0;
475         while ((c = es_getc (http_get_read_ptr (hd))) != EOF)
476           {
477             count++;
478             if (!no_out)
479               putchar (c);
480           }
481         log_info ("Received bytes: %lu\n", count);
482       }
483       break;
484     case 301:
485     case 302:
486     case 307:
487       log_info ("Redirected to: %s\n", http_get_header (hd, "Location"));
488       break;
489     }
490   http_close (hd, 0);
491
492   http_session_release (session);
493 #ifdef HTTP_USE_GNUTLS
494   gnutls_global_deinit ();
495 #endif /*HTTP_USE_GNUTLS*/
496
497   return 0;
498 }