2 * OpenConnect (SSL + DTLS) VPN client
4 * Copyright © 2008-2012 Intel Corporation.
5 * Copyright © 2008 Nick Andrew <nick@nick-andrew.net>
7 * Author: David Woodhouse <dwmw2@infradead.org>
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.
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.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to:
21 * Free Software Foundation, Inc.
22 * 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301 USA
26 #ifndef __OPENCONNECT_H__
27 #define __OPENCONNECT_H__
30 #include <sys/types.h>
33 #define OPENCONNECT_API_VERSION_MAJOR 2
34 #define OPENCONNECT_API_VERSION_MINOR 1
38 * - Add openconnect_set_reported_os()
39 * - Add openconnect_set_stoken_mode(), openconnect_has_stoken_support()
42 * - OPENCONNECT_X509 is now an opaque type.
43 * - Add openconnect_has_pkcs11_support(), openconnect_has_tss_blob_support()
44 * - Rename openconnect_init_openssl() -> openconnect_init_ssl()
45 * - Rename openconnect_vpninfo_new_with_cbdata() -> openconnect_vpninfo_new()
46 * and kill the old openconnect_vpninfo_new() and its callback types.
49 * - Add openconnect_get_cert_details(), openconnect_get_cert_DER().
52 * - Add openconnect_set_cancel_fd()
55 * - Add openconnect_set_cert_expiry_warning() to change from default 60 days
58 * - Add openconnect_vpninfo_new_with_cbdata()
61 * - Add openconnect_vpninfo_free()
67 /* Before API version 1.4 (OpenConnect 3.19) this macro didn't exist.
68 * Somewhat ironic, that the API version check itself needs to be
69 * conditionally used depending on the API version. A very simple way
70 * for users to handle this with an approximately correct answer is
71 * #include <openconnect.h>
72 * #ifndef OPENCONNECT_CHECK_VER
73 * #define OPENCONNECT_CHECK_VER(x,y) 0
76 #define OPENCONNECT_CHECK_VER(maj,min) \
77 (OPENCONNECT_API_VERSION_MAJOR > (maj) || \
78 (OPENCONNECT_API_VERSION_MAJOR == (maj) && \
79 OPENCONNECT_API_VERSION_MINOR >= (min)))
81 /****************************************************************************/
83 /* Authentication form processing */
85 #define OC_FORM_OPT_TEXT 1
86 #define OC_FORM_OPT_PASSWORD 2
87 #define OC_FORM_OPT_SELECT 3
88 #define OC_FORM_OPT_HIDDEN 4
89 #define OC_FORM_OPT_STOKEN 5
91 /* char * fields are static (owned by XML parser) and don't need to be
92 freed by the form handling code -- except for value, which for TEXT
93 and PASSWORD options is allocated by process_form() when
94 interacting with the user and must be freed. */
96 struct oc_form_opt *next;
103 /* All fields are static, owned by the XML parser */
109 char *override_label;
112 struct oc_form_opt_select {
113 struct oc_form_opt form;
115 struct oc_choice choices[0];
118 /* All char * fields are static, owned by the XML parser */
119 struct oc_auth_form {
126 struct oc_form_opt *opts;
129 /****************************************************************************/
136 struct openconnect_info;
138 #define OPENCONNECT_X509 void
140 /* Unless otherwise specified, all functions which set strings will take
141 ownership of those strings and the library will free them later in
142 openconnect_vpninfo_free() */
145 /* The buffer 'buf' must be at least 41 bytes. It will receive a hex string
146 with trailing NUL, representing the SHA1 fingerprint of the certificate. */
147 int openconnect_get_cert_sha1(struct openconnect_info *vpninfo,
148 OPENCONNECT_X509 *cert, char *buf);
149 char *openconnect_get_cert_details(struct openconnect_info *vpninfo,
150 OPENCONNECT_X509 *cert);
151 /* Returns the length of the created DER output, in a newly-allocated buffer
152 that will need to be freed by the caller. */
153 int openconnect_get_cert_DER(struct openconnect_info *vpninfo,
154 OPENCONNECT_X509 *cert, unsigned char **buf);
155 int openconnect_set_http_proxy(struct openconnect_info *vpninfo, char *proxy);
156 int openconnect_passphrase_from_fsid(struct openconnect_info *vpninfo);
157 int openconnect_obtain_cookie(struct openconnect_info *vpninfo);
158 void openconnect_init_ssl(void);
160 char *openconnect_get_vpn_name (struct openconnect_info *);
161 char *openconnect_get_hostname (struct openconnect_info *);
162 void openconnect_set_hostname (struct openconnect_info *, char *);
163 char *openconnect_get_urlpath (struct openconnect_info *);
164 void openconnect_set_urlpath (struct openconnect_info *, char *);
166 /* This function does *not* take ownership of the string; it is parsed
167 and then discarded. */
168 int openconnect_set_stoken_mode (struct openconnect_info *,
169 int use_stoken, const char *token_str);
171 /* This function does *not* take ownership of the string; it's copied
172 into a static buffer in the vpninfo. The size must be 41 bytes,
173 since that's the size of a 20-byte SHA1 represented as hex with
175 void openconnect_set_xmlsha1 (struct openconnect_info *, const char *, int size);
177 void openconnect_set_cafile (struct openconnect_info *, char *);
178 void openconnect_setup_csd (struct openconnect_info *, uid_t, int silent, char *wrapper);
179 int openconnect_set_reported_os (struct openconnect_info *, const char *os);
180 void openconnect_set_client_cert (struct openconnect_info *, char *cert, char *sslkey);
182 /* This is *not* yours and must not be destroyed with X509_free(). It
183 * will be valid when a cookie has been obtained successfully, and will
184 * be valid until the connection is destroyed or another attempt it made
186 OPENCONNECT_X509 *openconnect_get_peer_cert (struct openconnect_info *);
188 int openconnect_get_port (struct openconnect_info *);
189 char *openconnect_get_cookie (struct openconnect_info *);
190 void openconnect_clear_cookie (struct openconnect_info *);
192 void openconnect_reset_ssl (struct openconnect_info *vpninfo);
193 int openconnect_parse_url (struct openconnect_info *vpninfo, char *url);
194 void openconnect_set_cert_expiry_warning (struct openconnect_info *vpninfo,
197 /* If this is set, then openconnect_obtain_cookie() will abort and return
198 failure if the file descriptor is readable. Typically a user may create
199 a pair of pipes with the pipe(2) system call, hand the readable one to
200 this function, and then write a byte to the other end if it ever wants
201 to cancel the connection. This way, a multi-threaded UI (which will be
202 running openconnect_obtain_cookie() in a separate thread since it blocks)
203 has the ability to cancel that call, reap its thread and free the
204 vpninfo structure (or retry). An 'fd' argument of -1 will render the
205 cancellation mechanism inactive. */
206 void openconnect_set_cancel_fd (struct openconnect_info *vpninfo, int fd);
208 const char *openconnect_get_version(void);
210 /* The first (privdata) argument to each of these functions is either
211 the privdata argument provided to openconnect_vpninfo_new_with_cbdata(),
212 or if that argument was NULL then it'll be the vpninfo itself. */
214 /* When the server's certificate fails validation via the normal means,
215 this function is called with the offending certificate along with
216 a textual reason for the failure (which may not be translated, if
217 it comes directly from OpenSSL, but will be if it is rejected for
218 "certificate does not match hostname", because that check is done
219 in OpenConnect and *is* translated). The function shall return zero
220 if the certificate is (or has in the past been) explicitly accepted
221 by the user, and non-zero to abort the connection. */
222 typedef int (*openconnect_validate_peer_cert_vfn) (void *privdata,
223 OPENCONNECT_X509 *cert,
225 /* On a successful connection, the server may provide us with a new XML
226 configuration file. This contains the list of servers that can be
227 chosen by the user to connect to, amongst other stuff that we mostly
228 ignore. By "new", we mean that the SHA1 indicated by the server does
229 not match the SHA1 set with the openconnect_set_xmlsha1() above. If
230 they don't match, or openconnect_set_xmlsha1() has not been called,
231 then the new XML is downloaded and this function is invoked. */
232 typedef int (*openconnect_write_new_config_vfn) (void *privdata, char *buf,
234 /* Handle an authentication form, requesting input from the user.
237 * = 0, when form was parsed and POST required
238 * = 1, when response was cancelled by user
240 typedef int (*openconnect_process_auth_form_vfn) (void *privdata,
241 struct oc_auth_form *form);
242 /* Logging output which the user *may* want to see. */
243 typedef void __attribute__ ((format(printf, 3, 4)))
244 (*openconnect_progress_vfn) (void *privdata, int level,
245 const char *fmt, ...);
246 struct openconnect_info *openconnect_vpninfo_new (char *useragent,
247 openconnect_validate_peer_cert_vfn,
248 openconnect_write_new_config_vfn,
249 openconnect_process_auth_form_vfn,
250 openconnect_progress_vfn,
252 void openconnect_vpninfo_free (struct openconnect_info *vpninfo);
254 /* SSL certificate capabilities. openconnect_has_pkcs11_support() means that we
255 can accept PKCS#11 URLs in place of filenames, for the certificate and key. */
256 int openconnect_has_pkcs11_support(void);
258 /* The OpenSSL TPM ENGINE stores keys in a PEM file labelled with the string
259 -----BEGIN TSS KEY BLOB-----. GnuTLS may learn to support this format too,
260 in the near future. */
261 int openconnect_has_tss_blob_support(void);
263 /* Software token capabilities. */
264 int openconnect_has_stoken_support(void);
266 #endif /* __OPENCONNECT_H__ */