Tag version 4.00
[platform/upstream/openconnect.git] / openconnect.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_H__
27 #define __OPENCONNECT_H__
28
29 #include <stdint.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32
33 #define OPENCONNECT_API_VERSION_MAJOR 2
34 #define OPENCONNECT_API_VERSION_MINOR 0
35
36 /*
37  * API version 2.0:
38  *  - OPENCONNECT_X509 is now an opaque type.
39  *  - Add openconnect_has_pkcs11_support(), openconnect_has_tss_blob_support()
40  *  - Rename openconnect_init_openssl() -> openconnect_init_ssl()
41  *  - Rename openconnect_vpninfo_new_with_cbdata() -> openconnect_vpninfo_new()
42  *    and kill the old openconnect_vpninfo_new() and its callback types.
43  *
44  * API version 1.5:
45  *  - Add openconnect_get_cert_details(), openconnect_get_cert_DER().
46  *
47  * API version 1.4:
48  *  - Add openconnect_set_cancel_fd()
49  *
50  * API version 1.3:
51  *  - Add openconnect_set_cert_expiry_warning() to change from default 60 days
52  *
53  * API version 1.2:
54  *  - Add openconnect_vpninfo_new_with_cbdata()
55  *
56  * API version 1.1:
57  *  - Add openconnect_vpninfo_free()
58  *
59  * API version 1.0:
60  *  - Initial version
61  */
62
63 /* Before API version 1.4 (OpenConnect 3.19) this macro didn't exist.
64  * Somewhat ironic, that the API version check itself needs to be
65  * conditionally used depending on the API version. A very simple way
66  * for users to handle this with an approximately correct answer is
67  *   #include <openconnect.h>
68  *   #ifndef OPENCONNECT_CHECK_VER
69  *   #define OPENCONNECT_CHECK_VER(x,y) 0
70  *   #endif
71  */
72 #define OPENCONNECT_CHECK_VER(maj,min) \
73         (OPENCONNECT_API_VERSION_MAJOR > (maj) || \
74         (OPENCONNECT_API_VERSION_MAJOR == (maj) && \
75          OPENCONNECT_API_VERSION_MINOR >= (min)))
76
77 /****************************************************************************/
78
79 /* Authentication form processing */
80
81 #define OC_FORM_OPT_TEXT        1
82 #define OC_FORM_OPT_PASSWORD    2
83 #define OC_FORM_OPT_SELECT      3
84 #define OC_FORM_OPT_HIDDEN      4
85
86 /* char * fields are static (owned by XML parser) and don't need to be
87    freed by the form handling code -- except for value, which for TEXT
88    and PASSWORD options is allocated by process_form() when
89    interacting with the user and must be freed. */
90 struct oc_form_opt {
91         struct oc_form_opt *next;
92         int type;
93         char *name;
94         char *label;
95         char *value;
96 };
97
98 /* All fields are static, owned by the XML parser */
99 struct oc_choice {
100         char *name;
101         char *label;
102         char *auth_type;
103         char *override_name;
104         char *override_label;
105 };
106
107 struct oc_form_opt_select {
108         struct oc_form_opt form;
109         int nr_choices;
110         struct oc_choice choices[0];
111 };
112
113 /* All char * fields are static, owned by the XML parser */
114 struct oc_auth_form {
115         char *banner;
116         char *message;
117         char *error;
118         char *auth_id;
119         char *method;
120         char *action;
121         struct oc_form_opt *opts;
122 };
123
124 /****************************************************************************/
125
126 #define PRG_ERR         0
127 #define PRG_INFO        1
128 #define PRG_DEBUG       2
129 #define PRG_TRACE       3
130
131 struct openconnect_info;
132
133 #define OPENCONNECT_X509 void
134
135 /* Unless otherwise specified, all functions which set strings will take
136    ownership of those strings and the library will free them later in
137    openconnect_vpninfo_free() */
138
139
140 /* The buffer 'buf' must be at least 41 bytes. It will receive a hex string
141    with trailing NUL, representing the SHA1 fingerprint of the certificate. */
142 int openconnect_get_cert_sha1(struct openconnect_info *vpninfo,
143                               OPENCONNECT_X509 *cert, char *buf);
144 char *openconnect_get_cert_details(struct openconnect_info *vpninfo,
145                                    OPENCONNECT_X509 *cert);
146 /* Returns the length of the created DER output, in a newly-allocated buffer
147    that will need to be freed by the caller. */
148 int openconnect_get_cert_DER(struct openconnect_info *vpninfo,
149                              OPENCONNECT_X509 *cert, unsigned char **buf);
150 int openconnect_set_http_proxy(struct openconnect_info *vpninfo, char *proxy);
151 int openconnect_passphrase_from_fsid(struct openconnect_info *vpninfo);
152 int openconnect_obtain_cookie(struct openconnect_info *vpninfo);
153 void openconnect_init_ssl(void);
154
155 char *openconnect_get_vpn_name (struct openconnect_info *);
156 char *openconnect_get_hostname (struct openconnect_info *);
157 void openconnect_set_hostname (struct openconnect_info *, char *);
158 char *openconnect_get_urlpath (struct openconnect_info *);
159 void openconnect_set_urlpath (struct openconnect_info *, char *);
160
161 /* This function does *not* take ownership of the string; it's copied
162    into a static buffer in the vpninfo. The size must be 41 bytes,
163    since that's the size of a 20-byte SHA1 represented as hex with
164    a trailing NUL. */
165 void openconnect_set_xmlsha1 (struct openconnect_info *, const char *, int size);
166
167 void openconnect_set_cafile (struct openconnect_info *, char *);
168 void openconnect_setup_csd (struct openconnect_info *, uid_t, int silent, char *wrapper);
169 void openconnect_set_client_cert (struct openconnect_info *, char *cert, char *sslkey);
170
171 /* This is *not* yours and must not be destroyed with X509_free(). It
172  * will be valid when a cookie has been obtained successfully, and will
173  * be valid until the connection is destroyed or another attempt it made
174  * to use it. */
175 OPENCONNECT_X509 *openconnect_get_peer_cert (struct openconnect_info *);
176
177 int openconnect_get_port (struct openconnect_info *);
178 char *openconnect_get_cookie (struct openconnect_info *);
179 void openconnect_clear_cookie (struct openconnect_info *);
180
181 void openconnect_reset_ssl (struct openconnect_info *vpninfo);
182 int openconnect_parse_url (struct openconnect_info *vpninfo, char *url);
183 void openconnect_set_cert_expiry_warning (struct openconnect_info *vpninfo,
184                                           int seconds);
185
186 /* If this is set, then openconnect_obtain_cookie() will abort and return
187    failure if the file descriptor is readable. Typically a user may create
188    a pair of pipes with the pipe(2) system call, hand the readable one to
189    this function, and then write a byte to the other end if it ever wants
190    to cancel the connection. This way, a multi-threaded UI (which will be
191    running openconnect_obtain_cookie() in a separate thread since it blocks)
192    has the ability to cancel that call, reap its thread and free the
193    vpninfo structure (or retry). An 'fd' argument of -1 will render the
194    cancellation mechanism inactive. */
195 void openconnect_set_cancel_fd (struct openconnect_info *vpninfo, int fd);
196
197 const char *openconnect_get_version(void);
198
199 /* The first (privdata) argument to each of these functions is either
200    the privdata argument provided to openconnect_vpninfo_new_with_cbdata(),
201    or if that argument was NULL then it'll be the vpninfo itself. */
202
203 /* When the server's certificate fails validation via the normal means,
204    this function is called with the offending certificate along with 
205    a textual reason for the failure (which may not be translated, if
206    it comes directly from OpenSSL, but will be if it is rejected for
207    "certificate does not match hostname", because that check is done
208    in OpenConnect and *is* translated). The function shall return zero
209    if the certificate is (or has in the past been) explicitly accepted
210    by the user, and non-zero to abort the connection. */
211 typedef int (*openconnect_validate_peer_cert_vfn) (void *privdata,
212                                                    OPENCONNECT_X509 *cert,
213                                                    const char *reason);
214 /* On a successful connection, the server may provide us with a new XML
215    configuration file. This contains the list of servers that can be
216    chosen by the user to connect to, amongst other stuff that we mostly
217    ignore. By "new", we mean that the SHA1 indicated by the server does
218    not match the SHA1 set with the openconnect_set_xmlsha1() above. If
219    they don't match, or openconnect_set_xmlsha1() has not been called,
220    then the new XML is downloaded and this function is invoked. */
221 typedef int (*openconnect_write_new_config_vfn) (void *privdata, char *buf,
222                                                 int buflen);
223 /* Handle an authentication form, requesting input from the user. 
224  * Return value:
225  *  < 0, on error
226  *  = 0, when form was parsed and POST required
227  *  = 1, when response was cancelled by user
228  */
229 typedef int (*openconnect_process_auth_form_vfn) (void *privdata,
230                                                  struct oc_auth_form *form);
231 /* Logging output which the user *may* want to see. */
232 typedef void __attribute__ ((format(printf, 3, 4)))
233                 (*openconnect_progress_vfn) (void *privdata, int level,
234                                             const char *fmt, ...);
235 struct openconnect_info *openconnect_vpninfo_new (char *useragent,
236                                                   openconnect_validate_peer_cert_vfn,
237                                                   openconnect_write_new_config_vfn,
238                                                   openconnect_process_auth_form_vfn,
239                                                   openconnect_progress_vfn,
240                                                   void *privdata);
241 void openconnect_vpninfo_free (struct openconnect_info *vpninfo);
242
243 /* SSL certificate capabilities. openconnect_has_pkcs11_support() means that we
244    can accept PKCS#11 URLs in place of filenames, for the certificate and key. */
245 int openconnect_has_pkcs11_support(void);
246
247 /* The OpenSSL TPM ENGINE stores keys in a PEM file labelled with the string
248    -----BEGIN TSS KEY BLOB-----. GnuTLS may learn to support this format too,
249    in the near future. */
250 int openconnect_has_tss_blob_support(void);
251
252 #endif /* __OPENCONNECT_H__ */