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