Add openconnect_sha1() function and use it instead of using OpenSSL directly
[platform/upstream/openconnect.git] / openconnect-internal.h
1 /*
2  * OpenConnect (SSL + DTLS) VPN client
3  *
4  * Copyright © 2008-2012 Intel Corporation.
5  * Copyright © 2008 Nick Andrew <nick@nick-andrew.net>
6  *
7  * Author: David Woodhouse <dwmw2@infradead.org>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * version 2.1, as published by the Free Software Foundation.
12  *
13  * This program 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.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to:
20  *
21  *   Free Software Foundation, Inc.
22  *   51 Franklin Street, Fifth Floor,
23  *   Boston, MA 02110-1301 USA
24  */
25
26 #ifndef __OPENCONNECT_INTERNAL_H__
27 #define __OPENCONNECT_INTERNAL_H__
28
29 #include "openconnect.h"
30
31 #include <openssl/ssl.h>
32 #include <zlib.h>
33 #include <stdint.h>
34 #include <sys/socket.h>
35 #include <sys/select.h>
36 #include <sys/time.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39 #ifdef LIBPROXY_HDR
40 #include LIBPROXY_HDR
41 #endif
42 #ifdef ENABLE_NLS
43 #include <locale.h>
44 #include <libintl.h>
45 #define _(s) dgettext("openconnect", s)
46 #else
47 #define _(s) s
48 #endif
49 #define N_(s) s
50
51 #define SHA1_SIZE 20
52
53 /****************************************************************************/
54
55 struct pkt {
56         int len;
57         struct pkt *next;
58         unsigned char hdr[8];
59         unsigned char data[];
60 };
61
62 struct vpn_option {
63         char *option;
64         char *value;
65         struct vpn_option *next;
66 };
67
68 #define KA_NONE         0
69 #define KA_DPD          1
70 #define KA_DPD_DEAD     2
71 #define KA_KEEPALIVE    3
72 #define KA_REKEY        4
73
74 struct keepalive_info {
75         int dpd;
76         int keepalive;
77         int rekey;
78         time_t last_rekey;
79         time_t last_tx;
80         time_t last_rx;
81         time_t last_dpd;
82 };
83
84 struct split_include {
85         char *route;
86         struct split_include *next;
87 };
88
89 #define RECONNECT_INTERVAL_MIN  10
90 #define RECONNECT_INTERVAL_MAX  100
91
92 #define CERT_TYPE_UNKNOWN       0
93 #define CERT_TYPE_PEM           1
94 #define CERT_TYPE_PKCS12        2
95 #define CERT_TYPE_TPM           3
96
97 struct openconnect_info {
98         char *redirect_url;
99
100         char *csd_token;
101         char *csd_ticket;
102         char *csd_stuburl;
103         char *csd_starturl;
104         char *csd_waiturl;
105         char *csd_preurl;
106
107         char *csd_scriptname;
108
109 #ifdef LIBPROXY_HDR
110         pxProxyFactory *proxy_factory;
111 #endif
112         char *proxy_type;
113         char *proxy;
114         int proxy_port;
115
116         const char *localname;
117         char *hostname;
118         int port;
119         char *urlpath;
120         int cert_expire_warning;
121         const char *cert;
122         const char *sslkey;
123         X509 *cert_x509;
124         int cert_type;
125         char *cert_password;
126         const char *cafile;
127         const char *servercert;
128         const char *xmlconfig;
129         char xmlsha1[(SHA1_SIZE * 2) + 1];
130         char *username;
131         char *password;
132         char *authgroup;
133         int nopasswd;
134         char *dtls_ciphers;
135         uid_t uid_csd;
136         char *csd_wrapper;
137         int uid_csd_given;
138         int no_http_keepalive;
139
140         X509 *peer_cert;
141
142         char *cookie; /* Pointer to within cookies list */
143         struct vpn_option *cookies;
144         struct vpn_option *cstp_options;
145         struct vpn_option *dtls_options;
146
147         SSL_CTX *https_ctx;
148         SSL *https_ssl;
149         struct keepalive_info ssl_times;
150         int owe_ssl_dpd_response;
151         struct pkt *deflate_pkt;
152         struct pkt *current_ssl_pkt;
153         struct pkt *pending_deflated_pkt;
154
155         z_stream inflate_strm;
156         uint32_t inflate_adler32;
157         z_stream deflate_strm;
158         uint32_t deflate_adler32;
159
160         int disable_ipv6;
161         int reconnect_timeout;
162         int reconnect_interval;
163         int dtls_attempt_period;
164         time_t new_dtls_started;
165         SSL_CTX *dtls_ctx;
166         SSL *dtls_ssl;
167         SSL *new_dtls_ssl;
168         SSL_SESSION *dtls_session;
169         struct keepalive_info dtls_times;
170         unsigned char dtls_session_id[32];
171         unsigned char dtls_secret[48];
172
173         char *dtls_cipher;
174         const char *vpnc_script;
175         int script_tun;
176         char *ifname;
177
178         int mtu;
179         const char *banner;
180         const char *vpn_addr;
181         const char *vpn_netmask;
182         const char *vpn_addr6;
183         const char *vpn_netmask6;
184         const char *vpn_dns[3];
185         const char *vpn_nbns[3];
186         const char *vpn_domain;
187         const char *vpn_proxy_pac;
188         struct split_include *split_includes;
189         struct split_include *split_excludes;
190
191         int select_nfds;
192         fd_set select_rfds;
193         fd_set select_wfds;
194         fd_set select_efds;
195
196 #ifdef __sun__
197         int ip_fd;
198         int ip6_fd;
199 #endif
200         int tun_fd;
201         int ssl_fd;
202         int dtls_fd;
203         int new_dtls_fd;
204         int cancel_fd;
205
206         struct pkt *incoming_queue;
207         struct pkt *outgoing_queue;
208         int outgoing_qlen;
209         int max_qlen;
210
211         socklen_t peer_addrlen;
212         struct sockaddr *peer_addr;
213         struct sockaddr *dtls_addr;
214
215         int deflate;
216         char *useragent;
217
218         const char *quit_reason;
219
220         void *cbdata;
221         openconnect_validate_peer_cert_vfn validate_peer_cert;
222         openconnect_write_new_config_vfn write_new_config;
223         openconnect_process_auth_form_vfn process_auth_form;
224         openconnect_progress_vfn progress;
225 };
226
227 /* Packet types */
228
229 #define AC_PKT_DATA             0       /* Uncompressed data */
230 #define AC_PKT_DPD_OUT          3       /* Dead Peer Detection */
231 #define AC_PKT_DPD_RESP         4       /* DPD response */
232 #define AC_PKT_DISCONN          5       /* Client disconnection notice */
233 #define AC_PKT_KEEPALIVE        7       /* Keepalive */
234 #define AC_PKT_COMPRESSED       8       /* Compressed data */
235 #define AC_PKT_TERM_SERVER      9       /* Server kick */
236
237 /* Ick */
238 #if OPENSSL_VERSION_NUMBER >= 0x00909000L
239 #define method_const const
240 #else
241 #define method_const
242 #endif
243
244 #define vpn_progress(vpninfo, ...) (vpninfo)->progress ((vpninfo)->cbdata, __VA_ARGS__)
245
246 /****************************************************************************/
247 /* Oh Solaris how we hate thee! */
248 #ifdef __sun__
249 #define time(x) openconnect__time(x)
250 time_t openconnect__time(time_t *t);
251 #endif
252 #ifndef HAVE_ASPRINTF
253 #define asprintf openconnect__asprintf
254 int openconnect__asprintf(char **strp, const char *fmt, ...);
255 #endif
256 #ifndef HAVE_GETLINE
257 #define getline openconnect__getline
258 ssize_t openconnect__getline(char **lineptr, size_t *n, FILE *stream);
259 #endif
260
261 /****************************************************************************/
262
263 /* tun.c */
264 int setup_tun(struct openconnect_info *vpninfo);
265 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout);
266 void shutdown_tun(struct openconnect_info *vpninfo);
267 int script_config_tun (struct openconnect_info *vpninfo, const char *reason);
268
269 /* dtls.c */
270 unsigned char unhex(const char *data);
271 int setup_dtls(struct openconnect_info *vpninfo);
272 int dtls_mainloop(struct openconnect_info *vpninfo, int *timeout);
273 int dtls_try_handshake(struct openconnect_info *vpninfo);
274 int connect_dtls_socket(struct openconnect_info *vpninfo);
275
276 /* cstp.c */
277 int make_cstp_connection(struct openconnect_info *vpninfo);
278 int cstp_mainloop(struct openconnect_info *vpninfo, int *timeout);
279 int cstp_bye(struct openconnect_info *vpninfo, const char *reason);
280 int cstp_reconnect(struct openconnect_info *vpninfo);
281
282 /* ssl.c */
283 int  __attribute__ ((format (printf, 2, 3)))
284     openconnect_SSL_printf(struct openconnect_info *vpninfo, const char *fmt, ...);
285 int openconnect_SSL_gets(struct openconnect_info *vpninfo, char *buf, size_t len);
286 int openconnect_SSL_write(struct openconnect_info *vpninfo, char *buf, size_t len);
287 int openconnect_SSL_read(struct openconnect_info *vpninfo, char *buf, size_t len);
288 int openconnect_open_https(struct openconnect_info *vpninfo);
289 void openconnect_close_https(struct openconnect_info *vpninfo);
290 int get_cert_md5_fingerprint(struct openconnect_info *vpninfo, X509 *cert,
291                              char *buf);
292 void openconnect_report_ssl_errors(struct openconnect_info *vpninfo);
293
294 /* ${SSL_LIBRARY}.c */
295 int openconnect_sha1(unsigned char *result, void *data, int len);
296
297 /* mainloop.c */
298 int vpn_add_pollfd(struct openconnect_info *vpninfo, int fd, short events);
299 int vpn_mainloop(struct openconnect_info *vpninfo);
300 int queue_new_packet(struct pkt **q, void *buf, int len);
301 void queue_packet(struct pkt **q, struct pkt *new);
302 int keepalive_action(struct keepalive_info *ka, int *timeout);
303 int ka_stalled_dpd_time(struct keepalive_info *ka, int *timeout);
304
305 extern int killed;
306
307 /* xml.c */
308 int config_lookup_host(struct openconnect_info *vpninfo, const char *host);
309
310 /* auth.c */
311 int parse_xml_response(struct openconnect_info *vpninfo, char *response,
312                        char *request_body, int req_len, const char **method,
313                        const char **request_body_type);
314
315 /* http.c */
316 char *openconnect_create_useragent(const char *base);
317 int process_proxy(struct openconnect_info *vpninfo, int ssl_sock);
318 int internal_parse_url(char *url, char **res_proto, char **res_host,
319                        int *res_port, char **res_path, int default_port);
320
321 /* ssl_ui.c */
322 int set_openssl_ui(void);
323
324 /* securid.c */
325 int generate_securid_tokencodes(struct openconnect_info *vpninfo);
326 int add_securid_pin(char *token, char *pin);
327
328 /* version.c */
329 extern const char *openconnect_version_str;
330
331 #endif /* __OPENCONNECT_INTERNAL_H__ */