1 /* http.h  -  HTTP protocol handler
2  * Copyright (C) 1999, 2000, 2001, 2003, 2006,
3  *               2010 Free Software Foundation, Inc.
4  * Copyright (C) 2015  g10 Code GmbH
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 #ifndef GNUPG_COMMON_HTTP_H
32 #define GNUPG_COMMON_HTTP_H
33 
34 #include <gpg-error.h>
35 #include "../common/fwddecl.h"
36 
37 struct uri_tuple_s
38 {
39   struct uri_tuple_s *next;
40   const char *name;	/* A pointer into name. */
41   char  *value;         /* A pointer to value (a Nul is always appended). */
42   size_t valuelen;	/* The real length of the value; we need it
43 			   because the value may contain embedded Nuls. */
44   int no_value;         /* True if no value has been given in the URL. */
45 };
46 typedef struct uri_tuple_s *uri_tuple_t;
47 
48 struct parsed_uri_s
49 {
50   /* All these pointers point into BUFFER; most stuff is not escaped. */
51   char *original;       /* Unmodified copy of the parsed URI.  */
52   char *scheme;	        /* Pointer to the scheme string (always lowercase). */
53   unsigned int is_http:1; /* This is a HTTP style URI.   */
54   unsigned int is_ldap:1; /* This is a LDAP style URI.   */
55   unsigned int use_tls:1; /* Whether TLS should be used. */
56   unsigned int opaque:1;  /* Unknown scheme; PATH has the rest.  */
57   unsigned int v6lit:1;   /* Host was given as a literal v6 address.  */
58   unsigned int onion:1;   /* .onion address given.  */
59   unsigned int explicit_port :1; /* The port was explicitly specified.  */
60   unsigned int ad_current:1;    /* Use Active Directory's current user.  */
61   char *auth;           /* username/password for basic auth.  */
62   char *host; 	        /* Host (converted to lowercase). */
63   unsigned short port;  /* Port (always set if the host is set). */
64   unsigned short off_host; /* Offset to the HOST respective PATH parts  */
65   unsigned short off_path; /* in the original URI buffer.               */
66   char *path; 	        /* Path. */
67   uri_tuple_t params;	/* ";xxxxx" */
68   uri_tuple_t query;	/* "?xxx=yyy" */
69   char buffer[1];	/* Buffer which holds a (modified) copy of the URI. */
70 };
71 typedef struct parsed_uri_s *parsed_uri_t;
72 
73 struct uri_tuple_s *uri_query_lookup (parsed_uri_t uri, const char *key);
74 const char *uri_query_value (parsed_uri_t url, const char *key);
75 
76 typedef enum
77   {
78     HTTP_REQ_GET  = 1,
79     HTTP_REQ_HEAD = 2,
80     HTTP_REQ_POST = 3,
81     HTTP_REQ_OPAQUE = 4  /* Internal use.  */
82   }
83 http_req_t;
84 
85 /* We put the flag values into an enum, so that gdb can display them. */
86 enum
87   {
88     HTTP_FLAG_TRY_PROXY = 1,     /* Try to use a proxy.  */
89     HTTP_FLAG_SHUTDOWN = 2,      /* Close sending end after the request.  */
90     HTTP_FLAG_FORCE_TOR = 4,     /* Force a TOR connection.  */
91     HTTP_FLAG_LOG_RESP = 8,      /* Log the server response.  */
92     HTTP_FLAG_FORCE_TLS = 16,    /* Force the use of TLS.  */
93     HTTP_FLAG_IGNORE_CL = 32,    /* Ignore content-length.  */
94     HTTP_FLAG_IGNORE_IPv4 = 64,  /* Do not use IPv4.  */
95     HTTP_FLAG_IGNORE_IPv6 = 128, /* Do not use IPv6.  */
96     HTTP_FLAG_TRUST_DEF   = 256, /* Use the CAs configured for HKP.  */
97     HTTP_FLAG_TRUST_SYS   = 512, /* Also use the system defined CAs. */
98     HTTP_FLAG_TRUST_CFG  = 1024, /* Also use configured CAs.         */
99     HTTP_FLAG_NO_CRL     = 2048  /* Do not consult CRLs for https.   */
100   };
101 
102 
103 struct http_session_s;
104 typedef struct http_session_s *http_session_t;
105 
106 struct http_context_s;
107 typedef struct http_context_s *http_t;
108 
109 /* An object used to track redirection infos.  */
110 struct http_redir_info_s
111 {
112   unsigned int redirects_left;   /* Number of still possible redirects.    */
113   ctrl_t ctrl;                   /* The usual connection info or NULL.     */
114   const char *orig_url;          /* The original requested URL.            */
115   unsigned int orig_onion:1;     /* Original request was an onion address. */
116   unsigned int orig_https:1;     /* Original request was a http address.   */
117   unsigned int silent:1;         /* No diagnostics.                        */
118   unsigned int allow_downgrade:1;/* Allow a downgrade from https to http.  */
119   unsigned int trust_location:1; /* Trust the received Location header.    */
120 };
121 typedef struct http_redir_info_s http_redir_info_t;
122 
123 
124 
125 /* A TLS verify callback function.  */
126 typedef gpg_error_t (*http_verify_cb_t) (void *opaque,
127                                          http_t http,
128                                          http_session_t session,
129                                          unsigned int flags,
130                                          void *tls_context);
131 
132 void http_set_verbose (int verbose, int debug);
133 
134 void http_register_tls_callback (gpg_error_t (*cb)(http_t,http_session_t,int));
135 void http_register_tls_ca (const char *fname);
136 void http_register_cfg_ca (const char *fname);
137 void http_register_netactivity_cb (void (*cb)(void));
138 
139 
140 gpg_error_t http_session_new (http_session_t *r_session,
141                               const char *intended_hostname,
142                               unsigned int flags,
143                               http_verify_cb_t cb,
144                               void *cb_value);
145 http_session_t http_session_ref (http_session_t sess);
146 void http_session_release (http_session_t sess);
147 
148 void http_session_set_log_cb (http_session_t sess,
149                               void (*cb)(http_session_t, gpg_error_t,
150                                          const char *,
151                                          const void **, size_t *));
152 void http_session_set_timeout (http_session_t sess, unsigned int timeout);
153 
154 
155 #define HTTP_PARSE_NO_SCHEME_CHECK 1
156 gpg_error_t http_parse_uri (parsed_uri_t *ret_uri, const char *uri,
157                             unsigned int flags);
158 
159 void http_release_parsed_uri (parsed_uri_t uri);
160 
161 gpg_error_t http_raw_connect (ctrl_t ctrl, http_t *r_hd,
162                               const char *server, unsigned short port,
163                               unsigned int flags, const char *srvtag,
164                               unsigned int timeout);
165 
166 gpg_error_t http_open (ctrl_t ctrl, http_t *r_hd, http_req_t reqtype,
167                        const char *url,
168                        const char *httphost,
169                        const char *auth,
170                        unsigned int flags,
171                        const char *proxy,
172                        http_session_t session,
173                        const char *srvtag,
174                        strlist_t headers);
175 
176 void http_start_data (http_t hd);
177 
178 gpg_error_t http_wait_response (http_t hd);
179 
180 void http_close (http_t hd, int keep_read_stream);
181 
182 gpg_error_t http_open_document (ctrl_t ctrl, http_t *r_hd,
183                                 const char *document,
184                                 const char *auth,
185                                 unsigned int flags,
186                                 const char *proxy,
187                                 http_session_t session,
188                                 const char *srvtag,
189                                 strlist_t headers);
190 
191 estream_t http_get_read_ptr (http_t hd);
192 estream_t http_get_write_ptr (http_t hd);
193 unsigned int http_get_status_code (http_t hd);
194 const char *http_get_tls_info (http_t hd, const char *what);
195 const char *http_get_header (http_t hd, const char *name);
196 const char **http_get_header_names (http_t hd);
197 gpg_error_t http_verify_server_credentials (http_session_t sess);
198 
199 char *http_escape_string (const char *string, const char *specials);
200 char *http_escape_data (const void *data, size_t datalen, const char *specials);
201 
202 gpg_error_t http_prepare_redirect (http_redir_info_t *info,
203                                    unsigned int status_code,
204                                    const char *location, char **r_url);
205 
206 const char *http_status2string (unsigned int status);
207 
208 
209 #endif /*GNUPG_COMMON_HTTP_H*/
210