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_stoken_mode(), openconnect_has_stoken_support()
41 * - OPENCONNECT_X509 is now an opaque type.
42 * - Add openconnect_has_pkcs11_support(), openconnect_has_tss_blob_support()
43 * - Rename openconnect_init_openssl() -> openconnect_init_ssl()
44 * - Rename openconnect_vpninfo_new_with_cbdata() -> openconnect_vpninfo_new()
45 * and kill the old openconnect_vpninfo_new() and its callback types.
48 * - Add openconnect_get_cert_details(), openconnect_get_cert_DER().
51 * - Add openconnect_set_cancel_fd()
54 * - Add openconnect_set_cert_expiry_warning() to change from default 60 days
57 * - Add openconnect_vpninfo_new_with_cbdata()
60 * - Add openconnect_vpninfo_free()
66 /* Before API version 1.4 (OpenConnect 3.19) this macro didn't exist.
67 * Somewhat ironic, that the API version check itself needs to be
68 * conditionally used depending on the API version. A very simple way
69 * for users to handle this with an approximately correct answer is
70 * #include <openconnect.h>
71 * #ifndef OPENCONNECT_CHECK_VER
72 * #define OPENCONNECT_CHECK_VER(x,y) 0
75 #define OPENCONNECT_CHECK_VER(maj,min) \
76 (OPENCONNECT_API_VERSION_MAJOR > (maj) || \
77 (OPENCONNECT_API_VERSION_MAJOR == (maj) && \
78 OPENCONNECT_API_VERSION_MINOR >= (min)))
80 /****************************************************************************/
82 /* Authentication form processing */
84 #define OC_FORM_OPT_TEXT 1
85 #define OC_FORM_OPT_PASSWORD 2
86 #define OC_FORM_OPT_SELECT 3
87 #define OC_FORM_OPT_HIDDEN 4
89 /* char * fields are static (owned by XML parser) and don't need to be
90 freed by the form handling code -- except for value, which for TEXT
91 and PASSWORD options is allocated by process_form() when
92 interacting with the user and must be freed. */
94 struct oc_form_opt *next;
101 /* All fields are static, owned by the XML parser */
107 char *override_label;
110 struct oc_form_opt_select {
111 struct oc_form_opt form;
113 struct oc_choice choices[0];
116 /* All char * fields are static, owned by the XML parser */
117 struct oc_auth_form {
124 struct oc_form_opt *opts;
127 /****************************************************************************/
134 struct openconnect_info;
136 #define OPENCONNECT_X509 void
138 /* Unless otherwise specified, all functions which set strings will take
139 ownership of those strings and the library will free them later in
140 openconnect_vpninfo_free() */
143 /* The buffer 'buf' must be at least 41 bytes. It will receive a hex string
144 with trailing NUL, representing the SHA1 fingerprint of the certificate. */
145 int openconnect_get_cert_sha1(struct openconnect_info *vpninfo,
146 OPENCONNECT_X509 *cert, char *buf);
147 char *openconnect_get_cert_details(struct openconnect_info *vpninfo,
148 OPENCONNECT_X509 *cert);
149 /* Returns the length of the created DER output, in a newly-allocated buffer
150 that will need to be freed by the caller. */
151 int openconnect_get_cert_DER(struct openconnect_info *vpninfo,
152 OPENCONNECT_X509 *cert, unsigned char **buf);
153 int openconnect_set_http_proxy(struct openconnect_info *vpninfo, char *proxy);
154 int openconnect_passphrase_from_fsid(struct openconnect_info *vpninfo);
155 int openconnect_obtain_cookie(struct openconnect_info *vpninfo);
156 void openconnect_init_ssl(void);
158 char *openconnect_get_vpn_name (struct openconnect_info *);
159 char *openconnect_get_hostname (struct openconnect_info *);
160 void openconnect_set_hostname (struct openconnect_info *, char *);
161 char *openconnect_get_urlpath (struct openconnect_info *);
162 void openconnect_set_urlpath (struct openconnect_info *, char *);
164 /* This function does *not* take ownership of the string; it is parsed
165 and then discarded. */
166 int openconnect_set_stoken_mode (struct openconnect_info *,
167 int use_stoken, const char *token_str);
169 /* This function does *not* take ownership of the string; it's copied
170 into a static buffer in the vpninfo. The size must be 41 bytes,
171 since that's the size of a 20-byte SHA1 represented as hex with
173 void openconnect_set_xmlsha1 (struct openconnect_info *, const char *, int size);
175 void openconnect_set_cafile (struct openconnect_info *, char *);
176 void openconnect_setup_csd (struct openconnect_info *, uid_t, int silent, char *wrapper);
177 void openconnect_set_client_cert (struct openconnect_info *, char *cert, char *sslkey);
179 /* This is *not* yours and must not be destroyed with X509_free(). It
180 * will be valid when a cookie has been obtained successfully, and will
181 * be valid until the connection is destroyed or another attempt it made
183 OPENCONNECT_X509 *openconnect_get_peer_cert (struct openconnect_info *);
185 int openconnect_get_port (struct openconnect_info *);
186 char *openconnect_get_cookie (struct openconnect_info *);
187 void openconnect_clear_cookie (struct openconnect_info *);
189 void openconnect_reset_ssl (struct openconnect_info *vpninfo);
190 int openconnect_parse_url (struct openconnect_info *vpninfo, char *url);
191 void openconnect_set_cert_expiry_warning (struct openconnect_info *vpninfo,
194 /* If this is set, then openconnect_obtain_cookie() will abort and return
195 failure if the file descriptor is readable. Typically a user may create
196 a pair of pipes with the pipe(2) system call, hand the readable one to
197 this function, and then write a byte to the other end if it ever wants
198 to cancel the connection. This way, a multi-threaded UI (which will be
199 running openconnect_obtain_cookie() in a separate thread since it blocks)
200 has the ability to cancel that call, reap its thread and free the
201 vpninfo structure (or retry). An 'fd' argument of -1 will render the
202 cancellation mechanism inactive. */
203 void openconnect_set_cancel_fd (struct openconnect_info *vpninfo, int fd);
205 const char *openconnect_get_version(void);
207 /* The first (privdata) argument to each of these functions is either
208 the privdata argument provided to openconnect_vpninfo_new_with_cbdata(),
209 or if that argument was NULL then it'll be the vpninfo itself. */
211 /* When the server's certificate fails validation via the normal means,
212 this function is called with the offending certificate along with
213 a textual reason for the failure (which may not be translated, if
214 it comes directly from OpenSSL, but will be if it is rejected for
215 "certificate does not match hostname", because that check is done
216 in OpenConnect and *is* translated). The function shall return zero
217 if the certificate is (or has in the past been) explicitly accepted
218 by the user, and non-zero to abort the connection. */
219 typedef int (*openconnect_validate_peer_cert_vfn) (void *privdata,
220 OPENCONNECT_X509 *cert,
222 /* On a successful connection, the server may provide us with a new XML
223 configuration file. This contains the list of servers that can be
224 chosen by the user to connect to, amongst other stuff that we mostly
225 ignore. By "new", we mean that the SHA1 indicated by the server does
226 not match the SHA1 set with the openconnect_set_xmlsha1() above. If
227 they don't match, or openconnect_set_xmlsha1() has not been called,
228 then the new XML is downloaded and this function is invoked. */
229 typedef int (*openconnect_write_new_config_vfn) (void *privdata, char *buf,
231 /* Handle an authentication form, requesting input from the user.
234 * = 0, when form was parsed and POST required
235 * = 1, when response was cancelled by user
237 typedef int (*openconnect_process_auth_form_vfn) (void *privdata,
238 struct oc_auth_form *form);
239 /* Logging output which the user *may* want to see. */
240 typedef void __attribute__ ((format(printf, 3, 4)))
241 (*openconnect_progress_vfn) (void *privdata, int level,
242 const char *fmt, ...);
243 struct openconnect_info *openconnect_vpninfo_new (char *useragent,
244 openconnect_validate_peer_cert_vfn,
245 openconnect_write_new_config_vfn,
246 openconnect_process_auth_form_vfn,
247 openconnect_progress_vfn,
249 void openconnect_vpninfo_free (struct openconnect_info *vpninfo);
251 /* SSL certificate capabilities. openconnect_has_pkcs11_support() means that we
252 can accept PKCS#11 URLs in place of filenames, for the certificate and key. */
253 int openconnect_has_pkcs11_support(void);
255 /* The OpenSSL TPM ENGINE stores keys in a PEM file labelled with the string
256 -----BEGIN TSS KEY BLOB-----. GnuTLS may learn to support this format too,
257 in the near future. */
258 int openconnect_has_tss_blob_support(void);
260 /* Software token capabilities. */
261 int openconnect_has_stoken_support(void);
263 #endif /* __OPENCONNECT_H__ */