Skip to content

Latest commit

 

History

History
871 lines (754 loc) · 26.1 KB

dtls.c

File metadata and controls

871 lines (754 loc) · 26.1 KB
 
Sep 22, 2008
Sep 22, 2008
1
/*
Nov 20, 2008
Nov 20, 2008
2
* OpenConnect (SSL + DTLS) VPN client
Sep 22, 2008
Sep 22, 2008
3
*
May 13, 2012
May 13, 2012
4
* Copyright © 2008-2012 Intel Corporation.
Sep 22, 2008
Sep 22, 2008
5
*
Nov 20, 2008
Nov 20, 2008
6
7
8
* Author: David Woodhouse <dwmw2@infradead.org>
*
* This program is free software; you can redistribute it and/or
Oct 4, 2008
Oct 4, 2008
9
* modify it under the terms of the GNU Lesser General Public License
Nov 20, 2008
Nov 20, 2008
10
* version 2.1, as published by the Free Software Foundation.
Sep 22, 2008
Sep 22, 2008
11
*
Nov 20, 2008
Nov 20, 2008
12
* This program is distributed in the hope that it will be useful, but
Oct 4, 2008
Oct 4, 2008
13
14
15
16
17
18
19
20
21
22
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to:
*
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
Sep 22, 2008
Sep 22, 2008
23
24
25
*/
#include <errno.h>
Sep 23, 2008
Sep 23, 2008
26
27
28
29
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
Dec 5, 2008
Dec 5, 2008
30
#include <netinet/in.h>
Sep 23, 2008
Sep 23, 2008
31
#include <fcntl.h>
Oct 15, 2008
Oct 15, 2008
32
#include <string.h>
Jun 11, 2012
Jun 11, 2012
33
34
35
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
Sep 22, 2008
Sep 22, 2008
36
Mar 9, 2011
Mar 9, 2011
37
#include "openconnect-internal.h"
Sep 22, 2008
Sep 22, 2008
38
Apr 14, 2010
Apr 14, 2010
39
40
41
42
43
44
45
46
47
48
49
50
51
static unsigned char nybble(unsigned char n)
{
if (n >= '0' && n <= '9') return n - '0';
else if (n >= 'A' && n <= 'F') return n - ('A' - 10);
else if (n >= 'a' && n <= 'f') return n - ('a' - 10);
return 0;
}
unsigned char unhex(const char *data)
{
return (nybble(data[0]) << 4) | nybble(data[1]);
}
Jun 7, 2012
Jun 7, 2012
52
53
#ifdef HAVE_DTLS
Oct 6, 2008
Oct 6, 2008
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#if 0
/*
* Useful for catching test cases, where we want everything to be
* reproducible. *NEVER* do this in the wild.
*/
time_t time(time_t *t)
{
time_t x = 0x3ab2d948;
if (t) *t = x;
return x;
}
int RAND_pseudo_bytes(char *buf, int len)
{
memset(buf, 0x5a, len);
printf("FAKE PSEUDO RANDOM!\n");
return 1;
Apr 9, 2009
Apr 9, 2009
71
Oct 6, 2008
Oct 6, 2008
72
73
74
75
76
77
78
79
80
81
}
int RAND_bytes(char *buf, int len)
{
static int foo = 0x5b;
printf("FAKE RANDOM!\n");
memset(buf, foo, len);
return 1;
}
#endif
Sep 22, 2008
Sep 22, 2008
82
83
84
85
86
87
/*
* The master-secret is generated randomly by the client. The server
* responds with a DTLS Session-ID. These, done over the HTTPS
* connection, are enough to 'resume' a DTLS session, bypassing all
* the normal setup of a normal DTLS connection.
*
Sep 29, 2008
Sep 29, 2008
88
89
90
91
* Cisco use a version of the protocol which predates RFC4347, but
* isn't quite the same as the pre-RFC version of the protocol which
* was in OpenSSL 0.9.8e -- it includes backports of some later
* OpenSSL patches.
Sep 23, 2008
Sep 23, 2008
92
*
Apr 9, 2009
Apr 9, 2009
93
94
* The openssl/ directory of this source tree should contain both a
* small patch against OpenSSL 0.9.8e to make it support Cisco's
Sep 29, 2008
Sep 29, 2008
95
96
* snapshot of the protocol, and a larger patch against newer OpenSSL
* which gives us an option to use the old protocol again.
Sep 23, 2008
Sep 23, 2008
97
*
Sep 29, 2008
Sep 29, 2008
98
99
100
101
102
103
* Cisco's server also seems to respond to the official version of the
* protocol, with a change in the ChangeCipherSpec packet which implies
* that it does know the difference and isn't just repeating the version
* number seen in the ClientHello. But although I can make the handshake
* complete by hacking tls1_mac() to use the _old_ protocol version
* number when calculating the MAC, the server still seems to be ignoring
Oct 2, 2008
Oct 2, 2008
104
105
* my subsequent data packets. So we use the old protocol, which is what
* their clients use anyway.
Apr 9, 2009
Apr 9, 2009
106
*/
Sep 22, 2008
Sep 22, 2008
107
Jun 11, 2012
Jun 11, 2012
108
#if defined (DTLS_OPENSSL)
Jun 7, 2012
Jun 7, 2012
109
110
#define DTLS_SEND SSL_write
#define DTLS_RECV SSL_read
Jun 13, 2012
Jun 13, 2012
111
112
113
114
115
116
117
#ifdef HAVE_DTLS1_STOP_TIMER
/* OpenSSL doesn't deliberately export this, but we need it to
workaround a DTLS bug in versions < 1.0.0e */
extern void dtls1_stop_timer (SSL *);
#endif
Jun 7, 2012
Jun 7, 2012
118
static int start_dtls_handshake(struct openconnect_info *vpninfo, int dtls_fd)
Sep 23, 2008
Sep 23, 2008
119
{
Apr 18, 2009
Apr 18, 2009
120
STACK_OF(SSL_CIPHER) *ciphers;
Oct 3, 2009
Oct 3, 2009
121
method_const SSL_METHOD *dtls_method;
Apr 18, 2009
Apr 18, 2009
122
SSL_CIPHER *dtls_cipher;
Sep 23, 2008
Sep 23, 2008
123
124
SSL *dtls_ssl;
BIO *dtls_bio;
Apr 9, 2009
Apr 9, 2009
125
Oct 2, 2008
Oct 2, 2008
126
127
128
129
if (!vpninfo->dtls_ctx) {
dtls_method = DTLSv1_client_method();
vpninfo->dtls_ctx = SSL_CTX_new(dtls_method);
if (!vpninfo->dtls_ctx) {
Sep 22, 2011
Sep 22, 2011
130
131
vpn_progress(vpninfo, PRG_ERR,
_("Initialise DTLSv1 CTX failed\n"));
Jun 11, 2012
Jun 11, 2012
132
openconnect_report_ssl_errors(vpninfo);
Apr 18, 2009
Apr 18, 2009
133
vpninfo->dtls_attempt_period = 0;
Oct 2, 2008
Oct 2, 2008
134
135
return -EINVAL;
}
Sep 23, 2008
Sep 23, 2008
136
Oct 2, 2008
Oct 2, 2008
137
138
139
/* If we don't readahead, then we do short reads and throw
away the tail of data packets. */
SSL_CTX_set_read_ahead(vpninfo->dtls_ctx, 1);
Apr 18, 2009
Apr 18, 2009
140
141
if (!SSL_CTX_set_cipher_list(vpninfo->dtls_ctx, vpninfo->dtls_cipher)) {
Sep 22, 2011
Sep 22, 2011
142
143
vpn_progress(vpninfo, PRG_ERR,
_("Set DTLS cipher list failed\n"));
Apr 18, 2009
Apr 18, 2009
144
145
146
147
148
SSL_CTX_free(vpninfo->dtls_ctx);
vpninfo->dtls_ctx = NULL;
vpninfo->dtls_attempt_period = 0;
return -EINVAL;
}
Oct 2, 2008
Oct 2, 2008
149
}
Sep 23, 2008
Sep 23, 2008
150
Oct 2, 2008
Oct 2, 2008
151
152
153
154
if (!vpninfo->dtls_session) {
/* We're going to "resume" a session which never existed. Fake it... */
vpninfo->dtls_session = SSL_SESSION_new();
if (!vpninfo->dtls_session) {
Sep 22, 2011
Sep 22, 2011
155
156
vpn_progress(vpninfo, PRG_ERR,
_("Initialise DTLSv1 session failed\n"));
Apr 18, 2009
Apr 18, 2009
157
vpninfo->dtls_attempt_period = 0;
Oct 2, 2008
Oct 2, 2008
158
return -EINVAL;
Apr 9, 2009
Apr 9, 2009
159
}
Nov 3, 2011
Nov 3, 2011
160
vpninfo->dtls_session->ssl_version = 0x0100; /* DTLS1_BAD_VER */
Aug 11, 2010
Aug 11, 2010
161
}
Oct 2, 2008
Oct 2, 2008
162
Aug 11, 2010
Aug 11, 2010
163
164
165
166
/* Do this every time; it may have changed due to a rekey */
vpninfo->dtls_session->master_key_length = sizeof(vpninfo->dtls_secret);
memcpy(vpninfo->dtls_session->master_key, vpninfo->dtls_secret,
sizeof(vpninfo->dtls_secret));
Oct 2, 2008
Oct 2, 2008
167
Aug 11, 2010
Aug 11, 2010
168
169
170
vpninfo->dtls_session->session_id_length = sizeof(vpninfo->dtls_session_id);
memcpy(vpninfo->dtls_session->session_id, vpninfo->dtls_session_id,
sizeof(vpninfo->dtls_session_id));
Sep 23, 2008
Sep 23, 2008
171
Oct 2, 2008
Oct 2, 2008
172
173
dtls_ssl = SSL_new(vpninfo->dtls_ctx);
SSL_set_connect_state(dtls_ssl);
Apr 18, 2009
Apr 18, 2009
174
175
176
ciphers = SSL_get_ciphers(dtls_ssl);
if (sk_SSL_CIPHER_num(ciphers) != 1) {
Sep 22, 2011
Sep 22, 2011
177
vpn_progress(vpninfo, PRG_ERR, _("Not precisely one DTLS cipher\n"));
Apr 18, 2009
Apr 18, 2009
178
179
180
181
182
183
184
185
186
187
188
189
190
SSL_CTX_free(vpninfo->dtls_ctx);
SSL_free(dtls_ssl);
SSL_SESSION_free(vpninfo->dtls_session);
vpninfo->dtls_ctx = NULL;
vpninfo->dtls_session = NULL;
vpninfo->dtls_attempt_period = 0;
return -EINVAL;
}
dtls_cipher = sk_SSL_CIPHER_value(ciphers, 0);
/* Set the appropriate cipher on our session to be resumed */
vpninfo->dtls_session->cipher = dtls_cipher;
vpninfo->dtls_session->cipher_id = dtls_cipher->id;
Sep 23, 2008
Sep 23, 2008
191
Oct 2, 2008
Oct 2, 2008
192
/* Add the generated session to the SSL */
Oct 2, 2008
Oct 2, 2008
193
if (!SSL_set_session(dtls_ssl, vpninfo->dtls_session)) {
Jun 27, 2011
Jun 27, 2011
194
vpn_progress(vpninfo, PRG_ERR,
Sep 22, 2011
Sep 22, 2011
195
196
197
198
199
_("SSL_set_session() failed with old protocol version 0x%x\n"
"Are you using a version of OpenSSL older than 0.9.8m?\n"
"See http://rt.openssl.org/Ticket/Display.html?id=1751\n"
"Use the --no-dtls command line option to avoid this message\n"),
vpninfo->dtls_session->ssl_version);
Nov 24, 2008
Nov 24, 2008
200
vpninfo->dtls_attempt_period = 0;
Oct 2, 2008
Oct 2, 2008
201
return -EINVAL;
Sep 23, 2008
Sep 23, 2008
202
}
Sep 23, 2008
Sep 23, 2008
203
Sep 23, 2008
Sep 23, 2008
204
dtls_bio = BIO_new_socket(dtls_fd, BIO_NOCLOSE);
May 14, 2012
May 14, 2012
205
206
/* Set non-blocking */
BIO_set_nbio(dtls_bio, 1);
Sep 23, 2008
Sep 23, 2008
207
208
SSL_set_bio(dtls_ssl, dtls_bio, dtls_bio);
Sep 29, 2008
Sep 29, 2008
209
SSL_set_options(dtls_ssl, SSL_OP_CISCO_ANYCONNECT);
Sep 23, 2008
Sep 23, 2008
210
Oct 2, 2008
Oct 2, 2008
211
vpninfo->new_dtls_ssl = dtls_ssl;
Apr 9, 2009
Apr 9, 2009
212
Jun 7, 2012
Jun 7, 2012
213
return 0;
Sep 23, 2008
Sep 23, 2008
214
215
}
Oct 5, 2008
Oct 5, 2008
216
int dtls_try_handshake(struct openconnect_info *vpninfo)
Sep 29, 2008
Sep 29, 2008
217
{
Oct 2, 2008
Oct 2, 2008
218
int ret = SSL_do_handshake(vpninfo->new_dtls_ssl);
Sep 29, 2008
Sep 29, 2008
219
Oct 2, 2008
Oct 2, 2008
220
if (ret == 1) {
Jun 11, 2012
Jun 11, 2012
221
vpn_progress(vpninfo, PRG_INFO, _("Established DTLS connection (using OpenSSL)\n"));
Oct 2, 2008
Oct 2, 2008
222
223
224
225
226
if (vpninfo->dtls_ssl) {
/* We are replacing an old connection */
SSL_free(vpninfo->dtls_ssl);
close(vpninfo->dtls_fd);
Oct 15, 2008
Oct 15, 2008
227
228
229
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_rfds);
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_wfds);
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_efds);
Oct 2, 2008
Oct 2, 2008
230
231
232
233
234
235
236
}
vpninfo->dtls_ssl = vpninfo->new_dtls_ssl;
vpninfo->dtls_fd = vpninfo->new_dtls_fd;
vpninfo->new_dtls_ssl = NULL;
vpninfo->new_dtls_fd = -1;
Aug 11, 2010
Aug 11, 2010
237
vpninfo->dtls_times.last_rx = vpninfo->dtls_times.last_tx = time(NULL);
Oct 2, 2008
Oct 2, 2008
238
Sep 8, 2011
Sep 8, 2011
239
/* From about 8.4.1(11) onwards, the ASA seems to get
Sep 15, 2011
Sep 15, 2011
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
very unhappy if we resend ChangeCipherSpec messages
after the initial setup. This was "fixed" in OpenSSL
1.0.0e for RT#2505, but it's not clear if that was
the right fix. What happens if the original packet
*does* get lost? Surely we *wanted* the retransmits,
because without them the server will never be able
to decrypt anything we send?
Oh well, our retransmitted packets upset the server
because we don't get the Cisco-compatibility right
(this is one of the areas in which Cisco's DTLS differs
from the RFC4347 spec), and DPD should help us notice
if *nothing* is getting through. */
#if OPENSSL_VERSION_NUMBER >= 0x1000005fL
/* OpenSSL 1.0.0e or above doesn't resend anyway; do nothing.
However, if we were *built* against 1.0.0e or newer, but at
runtime we find that we are being run against an older
version, warn about it. */
if (SSLeay() < 0x1000005fL) {
Sep 22, 2011
Sep 22, 2011
258
vpn_progress(vpninfo, PRG_ERR,
Sep 22, 2011
Sep 22, 2011
259
_("Your OpenSSL is older than the one you built against, so DTLS may fail!"));
Sep 15, 2011
Sep 15, 2011
260
261
}
#elif defined (HAVE_DTLS1_STOP_TIMER)
May 10, 2012
May 10, 2012
262
263
264
265
266
/*
* This works for any normal OpenSSL that supports
* Cisco DTLS compatibility (0.9.8m to 1.0.0d inclusive,
* and even later versions although it isn't needed there.
*/
Sep 8, 2011
Sep 8, 2011
267
dtls1_stop_timer(vpninfo->dtls_ssl);
May 10, 2012
May 10, 2012
268
269
270
271
272
273
#elif defined (BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT)
/*
* Debian restricts visibility of dtls1_stop_timer()
* so do it manually. This version also works on all
* sane versions of OpenSSL:
*/
Sep 15, 2011
Sep 15, 2011
274
275
276
277
278
279
memset (&(vpninfo->dtls_ssl->d1->next_timeout), 0,
sizeof((vpninfo->dtls_ssl->d1->next_timeout)));
vpninfo->dtls_ssl->d1->timeout_duration = 1;
BIO_ctrl(SSL_get_rbio(vpninfo->dtls_ssl),
BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
&(vpninfo->dtls_ssl->d1->next_timeout));
May 10, 2012
May 10, 2012
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#elif defined (BIO_CTRL_DGRAM_SET_TIMEOUT)
/*
* OK, here it gets more fun... this shoul handle the case
* of older OpenSSL which has the Cisco DTLS compatibility
* backported, but *not* the fix for RT#1922.
*/
BIO_ctrl(SSL_get_rbio(vpninfo->dtls_ssl),
BIO_CTRL_DGRAM_SET_TIMEOUT, 0, NULL);
#else
/*
* And if they don't have any of the above, they probably
* don't have RT#1829 fixed either, but that's OK because
* that's the "fix" that *introduces* the timeout we're
* trying to disable. So do nothing...
*/
Sep 15, 2011
Sep 15, 2011
295
#endif
Oct 2, 2008
Oct 2, 2008
296
297
298
299
300
301
302
return 0;
}
ret = SSL_get_error(vpninfo->new_dtls_ssl, ret);
if (ret == SSL_ERROR_WANT_WRITE || ret == SSL_ERROR_WANT_READ) {
if (time(NULL) < vpninfo->new_dtls_started + 5)
return 0;
Sep 22, 2011
Sep 22, 2011
303
vpn_progress(vpninfo, PRG_TRACE, _("DTLS handshake timed out\n"));
Oct 2, 2008
Oct 2, 2008
304
305
}
Sep 22, 2011
Sep 22, 2011
306
vpn_progress(vpninfo, PRG_ERR, _("DTLS handshake failed: %d\n"), ret);
May 14, 2012
May 14, 2012
307
openconnect_report_ssl_errors(vpninfo);
Oct 5, 2008
Oct 5, 2008
308
Oct 2, 2008
Oct 2, 2008
309
310
/* Kill the new (failed) connection... */
SSL_free(vpninfo->new_dtls_ssl);
Oct 15, 2008
Oct 15, 2008
311
312
FD_CLR(vpninfo->new_dtls_fd, &vpninfo->select_rfds);
FD_CLR(vpninfo->new_dtls_fd, &vpninfo->select_efds);
Oct 2, 2008
Oct 2, 2008
313
314
315
316
317
318
319
320
close(vpninfo->new_dtls_fd);
vpninfo->new_dtls_ssl = NULL;
vpninfo->new_dtls_fd = -1;
/* ... and kill the old one too. The only time there'll be a valid
existing session is when it was a rekey, and in that case it's
time for the old one to die. */
if (vpninfo->dtls_ssl) {
Oct 2, 2008
Oct 2, 2008
321
322
SSL_free(vpninfo->dtls_ssl);
close(vpninfo->dtls_fd);
Oct 15, 2008
Oct 15, 2008
323
324
325
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_rfds);
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_wfds);
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_efds);
Oct 2, 2008
Oct 2, 2008
326
327
328
vpninfo->dtls_ssl = NULL;
vpninfo->dtls_fd = -1;
}
Sep 29, 2008
Sep 29, 2008
329
Oct 2, 2008
Oct 2, 2008
330
331
332
time(&vpninfo->new_dtls_started);
return -EINVAL;
}
Sep 29, 2008
Sep 29, 2008
333
Jun 11, 2012
Jun 11, 2012
334
#elif defined (DTLS_GNUTLS)
Jun 28, 2012
Jun 28, 2012
335
336
#include <gnutls/dtls.h>
Jun 8, 2012
Jun 8, 2012
337
338
339
340
341
342
343
344
345
346
347
348
struct {
const char *name;
gnutls_cipher_algorithm_t cipher;
gnutls_mac_algorithm_t mac;
const char *prio;
} gnutls_dtls_ciphers[] = {
{ "AES128-SHA", GNUTLS_CIPHER_AES_128_CBC, GNUTLS_MAC_SHA1,
"NONE:+VERS-DTLS0.9:+COMP-NULL:+AES-128-CBC:+SHA1:+RSA:%COMPAT:%DISABLE_SAFE_RENEGOTIATION" },
{ "DES-CBC3-SHA", GNUTLS_CIPHER_3DES_CBC, GNUTLS_MAC_SHA1,
"NONE:+VERS-DTLS0.9:+COMP-NULL:+3DES-CBC:+SHA1:+RSA:%COMPAT:%DISABLE_SAFE_RENEGOTIATION" },
};
Jun 7, 2012
Jun 7, 2012
349
350
351
352
353
354
355
#define DTLS_SEND gnutls_record_send
#define DTLS_RECV gnutls_record_recv
static int start_dtls_handshake(struct openconnect_info *vpninfo, int dtls_fd)
{
gnutls_session_t dtls_ssl;
gnutls_datum_t master_secret, session_id;
int err;
Jun 8, 2012
Jun 8, 2012
356
357
358
359
360
361
362
363
364
365
366
int cipher;
for (cipher = 0; cipher < sizeof(gnutls_dtls_ciphers)/sizeof(gnutls_dtls_ciphers[0]); cipher++) {
if (!strcmp(vpninfo->dtls_cipher, gnutls_dtls_ciphers[cipher].name))
goto found_cipher;
}
vpn_progress(vpninfo, PRG_ERR, _("Unknown DTLS parameters for requested CipherSuite '%s'\n"),
vpninfo->dtls_cipher);
vpninfo->dtls_attempt_period = 0;
return -EINVAL;
Jun 7, 2012
Jun 7, 2012
367
Jun 8, 2012
Jun 8, 2012
368
found_cipher:
Jun 7, 2012
Jun 7, 2012
369
370
gnutls_init(&dtls_ssl, GNUTLS_CLIENT|GNUTLS_DATAGRAM|GNUTLS_NONBLOCK);
err = gnutls_priority_set_direct(dtls_ssl,
Jun 8, 2012
Jun 8, 2012
371
gnutls_dtls_ciphers[cipher].prio,
Jun 7, 2012
Jun 7, 2012
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
NULL);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to set DTLS priority: %s\n"),
gnutls_strerror(err));
gnutls_deinit(dtls_ssl);
vpninfo->dtls_attempt_period = 0;
return -EINVAL;
}
gnutls_transport_set_ptr(dtls_ssl,
(gnutls_transport_ptr_t)(long) dtls_fd);
gnutls_record_disable_padding(dtls_ssl);
master_secret.data = vpninfo->dtls_secret;
master_secret.size = sizeof(vpninfo->dtls_secret);
session_id.data = vpninfo->dtls_session_id;
session_id.size = sizeof(vpninfo->dtls_session_id);
Jun 7, 2012
Jun 7, 2012
388
err = gnutls_session_set_premaster(dtls_ssl, GNUTLS_CLIENT, GNUTLS_DTLS0_9,
Jun 8, 2012
Jun 8, 2012
389
390
GNUTLS_KX_RSA, gnutls_dtls_ciphers[cipher].cipher,
gnutls_dtls_ciphers[cipher].mac, GNUTLS_COMP_NULL,
Jun 7, 2012
Jun 7, 2012
391
&master_secret, &session_id);
Jun 7, 2012
Jun 7, 2012
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to set DTLS session parameters: %s\n"),
gnutls_strerror(err));
gnutls_deinit(dtls_ssl);
vpninfo->dtls_attempt_period = 0;
return -EINVAL;
}
vpninfo->new_dtls_ssl = dtls_ssl;
return 0;
}
int dtls_try_handshake(struct openconnect_info *vpninfo)
{
int err = gnutls_handshake(vpninfo->new_dtls_ssl);
if (!err) {
Jun 30, 2012
Jun 30, 2012
410
411
412
#ifdef HAVE_GNUTLS_DTLS_SET_DATA_MTU
/* Make sure GnuTLS's idea of the MTU is sufficient to take
a full VPN MTU (with 1-byte header) in a data record. */
Jun 30, 2012
Jun 30, 2012
413
err = gnutls_dtls_set_data_mtu(vpninfo->new_dtls_ssl, vpninfo->actual_mtu + 1);
Jun 30, 2012
Jun 30, 2012
414
415
416
417
418
419
420
421
422
423
424
425
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to set DTLS MTU: %s\n"),
gnutls_strerror(err));
goto error;
}
#else
/* If we don't have gnutls_dtls_set_data_mtu() then make sure
we leave enough headroom by adding the worst-case overhead.
We only support AES128-CBC and DES-CBC3-SHA anyway, so
working out the worst case isn't hard. */
gnutls_dtls_set_mtu(vpninfo->new_dtls_ssl,
Jun 30, 2012
Jun 30, 2012
426
vpninfo->actual_mtu + 1 /* packet + header */
Jun 30, 2012
Jun 30, 2012
427
428
429
430
431
432
+ 13 /* DTLS header */
+ 20 /* biggest supported MAC (SHA1) */
+ 16 /* biggest supported IV (AES-128) */
+ 16 /* max padding */);
#endif
Jun 11, 2012
Jun 11, 2012
433
vpn_progress(vpninfo, PRG_INFO, _("Established DTLS connection (using GnuTLS)\n"));
Jun 7, 2012
Jun 7, 2012
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
if (vpninfo->dtls_ssl) {
/* We are replacing an old connection */
gnutls_deinit(vpninfo->dtls_ssl);
close(vpninfo->dtls_fd);
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_rfds);
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_wfds);
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_efds);
}
vpninfo->dtls_ssl = vpninfo->new_dtls_ssl;
vpninfo->dtls_fd = vpninfo->new_dtls_fd;
vpninfo->new_dtls_ssl = NULL;
vpninfo->new_dtls_fd = -1;
vpninfo->dtls_times.last_rx = vpninfo->dtls_times.last_tx = time(NULL);
/* XXX: For OpenSSL we explicitly prevent retransmits here. */
return 0;
}
if (err == GNUTLS_E_AGAIN) {
if (time(NULL) < vpninfo->new_dtls_started + 5)
return 0;
vpn_progress(vpninfo, PRG_TRACE, _("DTLS handshake timed out\n"));
}
vpn_progress(vpninfo, PRG_ERR, _("DTLS handshake failed: %s\n"),
gnutls_strerror(err));
Jun 30, 2012
Jun 30, 2012
464
465
goto error;
error:
Jun 7, 2012
Jun 7, 2012
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/* Kill the new (failed) connection... */
gnutls_deinit(vpninfo->new_dtls_ssl);
FD_CLR(vpninfo->new_dtls_fd, &vpninfo->select_rfds);
FD_CLR(vpninfo->new_dtls_fd, &vpninfo->select_efds);
close(vpninfo->new_dtls_fd);
vpninfo->new_dtls_ssl = NULL;
vpninfo->new_dtls_fd = -1;
/* ... and kill the old one too. The only time there'll be a valid
existing session is when it was a rekey, and in that case it's
time for the old one to die. */
if (vpninfo->dtls_ssl) {
gnutls_deinit(vpninfo->dtls_ssl);
close(vpninfo->dtls_fd);
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_rfds);
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_wfds);
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_efds);
vpninfo->dtls_ssl = NULL;
vpninfo->dtls_fd = -1;
}
time(&vpninfo->new_dtls_started);
return -EINVAL;
}
#endif
Jun 7, 2012
Jun 7, 2012
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
int connect_dtls_socket(struct openconnect_info *vpninfo)
{
int dtls_fd, ret;
if (!vpninfo->dtls_addr) {
vpn_progress(vpninfo, PRG_ERR, _("No DTLS address\n"));
vpninfo->dtls_attempt_period = 0;
return -EINVAL;
}
if (!vpninfo->dtls_cipher) {
/* We probably didn't offer it any ciphers it liked */
vpn_progress(vpninfo, PRG_ERR, _("Server offered no DTLS cipher option\n"));
vpninfo->dtls_attempt_period = 0;
return -EINVAL;
}
if (vpninfo->proxy) {
/* XXX: Theoretically, SOCKS5 proxies can do UDP too */
vpn_progress(vpninfo, PRG_ERR, _("No DTLS when connected via proxy\n"));
vpninfo->dtls_attempt_period = 0;
return -EINVAL;
}
dtls_fd = socket(vpninfo->peer_addr->sa_family, SOCK_DGRAM, IPPROTO_UDP);
if (dtls_fd < 0) {
perror(_("Open UDP socket for DTLS:"));
return -EINVAL;
}
Jun 25, 2012
Jun 25, 2012
522
if (vpninfo->dtls_local_port) {
Jul 16, 2012
Jul 16, 2012
523
524
525
526
union {
struct sockaddr_in in;
struct sockaddr_in6 in6;
} dtls_bind_addr;
Jun 25, 2012
Jun 25, 2012
527
528
529
530
int dtls_bind_addrlen;
memset(&dtls_bind_addr, 0, sizeof(dtls_bind_addr));
if (vpninfo->peer_addr->sa_family == AF_INET) {
Jul 16, 2012
Jul 16, 2012
531
struct sockaddr_in *addr = &dtls_bind_addr.in;
Jun 25, 2012
Jun 25, 2012
532
533
534
535
536
dtls_bind_addrlen = sizeof(*addr);
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = INADDR_ANY;
addr->sin_port = htons(vpninfo->dtls_local_port);
} else if (vpninfo->peer_addr->sa_family == AF_INET6) {
Jul 16, 2012
Jul 16, 2012
537
struct sockaddr_in6 *addr = &dtls_bind_addr.in6;
Jun 25, 2012
Jun 25, 2012
538
539
540
541
542
543
544
545
546
dtls_bind_addrlen = sizeof(*addr);
addr->sin6_family = AF_INET6;
addr->sin6_addr = in6addr_any;
addr->sin6_port = htons(vpninfo->dtls_local_port);
} else {
vpn_progress(vpninfo, PRG_ERR,
_("Unknown protocol family %d. Cannot do DTLS\n"),
vpninfo->peer_addr->sa_family);
vpninfo->dtls_attempt_period = 0;
Sep 26, 2012
Sep 26, 2012
547
close(dtls_fd);
Jun 25, 2012
Jun 25, 2012
548
549
550
551
552
return -EINVAL;
}
if (bind(dtls_fd, (struct sockaddr *)&dtls_bind_addr, dtls_bind_addrlen)) {
perror(_("Bind UDP socket for DTLS"));
Sep 26, 2012
Sep 26, 2012
553
close(dtls_fd);
Jun 25, 2012
Jun 25, 2012
554
555
556
557
return -EINVAL;
}
}
Jun 7, 2012
Jun 7, 2012
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
if (connect(dtls_fd, vpninfo->dtls_addr, vpninfo->peer_addrlen)) {
perror(_("UDP (DTLS) connect:\n"));
close(dtls_fd);
return -EINVAL;
}
fcntl(dtls_fd, F_SETFD, FD_CLOEXEC);
fcntl(dtls_fd, F_SETFL, fcntl(dtls_fd, F_GETFL) | O_NONBLOCK);
ret = start_dtls_handshake(vpninfo, dtls_fd);
if (ret) {
close(dtls_fd);
return ret;
}
vpninfo->new_dtls_fd = dtls_fd;
if (vpninfo->select_nfds <= dtls_fd)
vpninfo->select_nfds = dtls_fd + 1;
FD_SET(dtls_fd, &vpninfo->select_rfds);
FD_SET(dtls_fd, &vpninfo->select_efds);
time(&vpninfo->new_dtls_started);
return dtls_try_handshake(vpninfo);
}
Oct 5, 2008
Oct 5, 2008
585
static int dtls_restart(struct openconnect_info *vpninfo)
Oct 2, 2008
Oct 2, 2008
586
587
{
if (vpninfo->dtls_ssl) {
Jun 11, 2012
Jun 11, 2012
588
#if defined (DTLS_OPENSSL)
Oct 2, 2008
Oct 2, 2008
589
SSL_free(vpninfo->dtls_ssl);
Jun 11, 2012
Jun 11, 2012
590
#elif defined (DTLS_GNUTLS)
Jun 7, 2012
Jun 7, 2012
591
592
gnutls_deinit(vpninfo->dtls_ssl);
#endif
Oct 2, 2008
Oct 2, 2008
593
close(vpninfo->dtls_fd);
Oct 15, 2008
Oct 15, 2008
594
595
596
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_rfds);
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_wfds);
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_efds);
Oct 2, 2008
Oct 2, 2008
597
598
599
vpninfo->dtls_ssl = NULL;
vpninfo->dtls_fd = -1;
}
Sep 29, 2008
Sep 29, 2008
600
Oct 2, 2008
Oct 2, 2008
601
return connect_dtls_socket(vpninfo);
Sep 29, 2008
Sep 29, 2008
602
603
}
Oct 2, 2008
Oct 2, 2008
604
Oct 5, 2008
Oct 5, 2008
605
int setup_dtls(struct openconnect_info *vpninfo)
Sep 22, 2008
Sep 22, 2008
606
607
{
struct vpn_option *dtls_opt = vpninfo->dtls_options;
Sep 23, 2008
Sep 23, 2008
608
int dtls_port = 0;
Sep 22, 2008
Sep 22, 2008
609
Jun 11, 2012
Jun 11, 2012
610
611
612
613
614
615
616
617
618
#if defined (OPENCONNECT_GNUTLS) && defined (DTLS_OPENSSL)
/* If we're using GnuTLS for authentication but OpenSSL for DTLS,
we'll need to initialise OpenSSL now... */
SSL_library_init ();
ERR_clear_error ();
SSL_load_error_strings ();
OpenSSL_add_all_algorithms ();
#endif
Sep 22, 2008
Sep 22, 2008
619
while (dtls_opt) {
Jun 27, 2011
Jun 27, 2011
620
vpn_progress(vpninfo, PRG_TRACE,
Sep 22, 2011
Sep 22, 2011
621
622
_("DTLS option %s : %s\n"),
dtls_opt->option, dtls_opt->value);
Sep 23, 2008
Sep 23, 2008
623
Aug 11, 2010
Aug 11, 2010
624
if (!strcmp(dtls_opt->option + 7, "Port")) {
Sep 23, 2008
Sep 23, 2008
625
dtls_port = atol(dtls_opt->value);
Sep 23, 2008
Sep 23, 2008
626
} else if (!strcmp(dtls_opt->option + 7, "Keepalive")) {
Oct 2, 2008
Oct 2, 2008
627
vpninfo->dtls_times.keepalive = atol(dtls_opt->value);
Sep 23, 2008
Sep 23, 2008
628
} else if (!strcmp(dtls_opt->option + 7, "DPD")) {
Aug 7, 2010
Aug 7, 2010
629
630
631
int j = atol(dtls_opt->value);
if (j && (!vpninfo->dtls_times.dpd || j < vpninfo->dtls_times.dpd))
vpninfo->dtls_times.dpd = j;
Sep 29, 2008
Sep 29, 2008
632
} else if (!strcmp(dtls_opt->option + 7, "Rekey-Time")) {
Oct 2, 2008
Oct 2, 2008
633
vpninfo->dtls_times.rekey = atol(dtls_opt->value);
Apr 18, 2009
Apr 18, 2009
634
} else if (!strcmp(dtls_opt->option + 7, "CipherSuite")) {
Jun 23, 2009
Jun 23, 2009
635
vpninfo->dtls_cipher = strdup(dtls_opt->value);
Sep 23, 2008
Sep 23, 2008
636
}
Apr 9, 2009
Apr 9, 2009
637
Sep 22, 2008
Sep 22, 2008
638
639
dtls_opt = dtls_opt->next;
}
Aug 11, 2010
Aug 11, 2010
640
if (!dtls_port) {
Jan 2, 2010
Jan 2, 2010
641
vpninfo->dtls_attempt_period = 0;
Sep 23, 2008
Sep 23, 2008
642
return -EINVAL;
Jan 2, 2010
Jan 2, 2010
643
644
645
646
647
648
649
650
}
vpninfo->dtls_addr = malloc(vpninfo->peer_addrlen);
if (!vpninfo->dtls_addr) {
vpninfo->dtls_attempt_period = 0;
return -ENOMEM;
}
memcpy(vpninfo->dtls_addr, vpninfo->peer_addr, vpninfo->peer_addrlen);
Sep 23, 2008
Sep 23, 2008
651
Sep 29, 2008
Sep 29, 2008
652
if (vpninfo->peer_addr->sa_family == AF_INET) {
Jan 2, 2010
Jan 2, 2010
653
struct sockaddr_in *sin = (void *)vpninfo->dtls_addr;
Sep 29, 2008
Sep 29, 2008
654
655
sin->sin_port = htons(dtls_port);
} else if (vpninfo->peer_addr->sa_family == AF_INET6) {
Jan 2, 2010
Jan 2, 2010
656
struct sockaddr_in6 *sin = (void *)vpninfo->dtls_addr;
Sep 29, 2008
Sep 29, 2008
657
658
sin->sin6_port = htons(dtls_port);
} else {
Sep 22, 2011
Sep 22, 2011
659
660
661
vpn_progress(vpninfo, PRG_ERR,
_("Unknown protocol family %d. Cannot do DTLS\n"),
vpninfo->peer_addr->sa_family);
Jan 2, 2010
Jan 2, 2010
662
vpninfo->dtls_attempt_period = 0;
Sep 29, 2008
Sep 29, 2008
663
664
665
return -EINVAL;
}
Oct 2, 2008
Oct 2, 2008
666
667
if (connect_dtls_socket(vpninfo))
return -EINVAL;
Sep 23, 2008
Sep 23, 2008
668
Jun 27, 2011
Jun 27, 2011
669
vpn_progress(vpninfo, PRG_TRACE,
Sep 22, 2011
Sep 22, 2011
670
671
_("DTLS connected. DPD %d, Keepalive %d\n"),
vpninfo->dtls_times.dpd, vpninfo->dtls_times.keepalive);
Sep 23, 2008
Sep 23, 2008
672
673
return 0;
Sep 22, 2008
Sep 22, 2008
674
675
}
Dec 12, 2011
Dec 12, 2011
676
677
static struct pkt *dtls_pkt;
Oct 5, 2008
Oct 5, 2008
678
int dtls_mainloop(struct openconnect_info *vpninfo, int *timeout)
Sep 22, 2008
Sep 22, 2008
679
{
Sep 23, 2008
Sep 23, 2008
680
int work_done = 0;
Oct 2, 2008
Oct 2, 2008
681
char magic_pkt;
Sep 23, 2008
Sep 23, 2008
682
Dec 12, 2011
Dec 12, 2011
683
while (1) {
Jun 30, 2012
Jun 30, 2012
684
int len = vpninfo->actual_mtu;
Dec 12, 2011
Dec 12, 2011
685
686
687
688
689
690
691
692
693
694
695
unsigned char *buf;
if (!dtls_pkt) {
dtls_pkt = malloc(sizeof(struct pkt) + len);
if (!dtls_pkt) {
vpn_progress(vpninfo, PRG_ERR, "Allocation failed\n");
break;
}
}
buf = dtls_pkt->data - 1;
Jun 7, 2012
Jun 7, 2012
696
len = DTLS_RECV(vpninfo->dtls_ssl, buf, len + 1);
Dec 12, 2011
Dec 12, 2011
697
698
if (len <= 0)
break;
Oct 5, 2008
Oct 5, 2008
699
Jun 27, 2011
Jun 27, 2011
700
vpn_progress(vpninfo, PRG_TRACE,
Sep 22, 2011
Sep 22, 2011
701
702
_("Received DTLS packet 0x%02x of %d bytes\n"),
buf[0], len);
Sep 30, 2008
Sep 30, 2008
703
Oct 2, 2008
Oct 2, 2008
704
vpninfo->dtls_times.last_rx = time(NULL);
Sep 30, 2008
Sep 30, 2008
705
Sep 23, 2008
Sep 23, 2008
706
switch(buf[0]) {
Oct 2, 2008
Oct 2, 2008
707
case AC_PKT_DATA:
Dec 14, 2011
Dec 14, 2011
708
709
710
dtls_pkt->len = len - 1;
queue_packet(&vpninfo->incoming_queue, dtls_pkt);
dtls_pkt = NULL;
Sep 23, 2008
Sep 23, 2008
711
712
713
work_done = 1;
break;
Oct 2, 2008
Oct 2, 2008
714
case AC_PKT_DPD_OUT:
Sep 22, 2011
Sep 22, 2011
715
vpn_progress(vpninfo, PRG_TRACE, _("Got DTLS DPD request\n"));
Oct 2, 2008
Oct 2, 2008
716
717
718
/* FIXME: What if the packet doesn't get through? */
magic_pkt = AC_PKT_DPD_RESP;
Jun 7, 2012
Jun 7, 2012
719
if (DTLS_SEND(vpninfo->dtls_ssl, &magic_pkt, 1) != 1)
Sep 22, 2011
Sep 22, 2011
720
721
vpn_progress(vpninfo, PRG_ERR,
_("Failed to send DPD response. Expect disconnect\n"));
Oct 2, 2008
Oct 2, 2008
722
723
continue;
Oct 2, 2008
Oct 2, 2008
724
case AC_PKT_DPD_RESP:
Sep 22, 2011
Sep 22, 2011
725
vpn_progress(vpninfo, PRG_TRACE, _("Got DTLS DPD response\n"));
Sep 23, 2008
Sep 23, 2008
726
727
break;
Oct 2, 2008
Oct 2, 2008
728
case AC_PKT_KEEPALIVE:
Sep 22, 2011
Sep 22, 2011
729
vpn_progress(vpninfo, PRG_TRACE, _("Got DTLS Keepalive\n"));
Oct 2, 2008
Oct 2, 2008
730
731
break;
Sep 23, 2008
Sep 23, 2008
732
default:
Jun 27, 2011
Jun 27, 2011
733
vpn_progress(vpninfo, PRG_ERR,
Sep 22, 2011
Sep 22, 2011
734
735
_("Unknown DTLS packet type %02x, len %d\n"),
buf[0], len);
Oct 6, 2008
Oct 6, 2008
736
737
if (1) {
/* Some versions of OpenSSL have bugs with receiving out-of-order
Apr 9, 2009
Apr 9, 2009
738
* packets. Not only do they wrongly decide to drop packets if
Oct 6, 2008
Oct 6, 2008
739
740
741
742
743
744
745
746
747
* two packets get swapped in transit, but they also _fail_ to
* drop the packet in non-blocking mode; instead they return
* the appropriate length of garbage. So don't abort... for now. */
break;
} else {
vpninfo->quit_reason = "Unknown packet received";
return 1;
}
Sep 23, 2008
Sep 23, 2008
748
}
Sep 23, 2008
Sep 23, 2008
749
}
Sep 23, 2008
Sep 23, 2008
750
Oct 2, 2008
Oct 2, 2008
751
752
switch (keepalive_action(&vpninfo->dtls_times, timeout)) {
case KA_REKEY:
Sep 22, 2011
Sep 22, 2011
753
vpn_progress(vpninfo, PRG_INFO, _("DTLS rekey due\n"));
Aug 11, 2010
Aug 11, 2010
754
755
756
757
/* There ought to be a method of rekeying DTLS without tearing down
the CSTP session and restarting, but we don't (yet) know it */
if (cstp_reconnect(vpninfo)) {
Sep 22, 2011
Sep 22, 2011
758
vpn_progress(vpninfo, PRG_ERR, _("Reconnect failed\n"));
Aug 11, 2010
Aug 11, 2010
759
760
761
762
763
vpninfo->quit_reason = "CSTP reconnect failed";
return 1;
}
if (dtls_restart(vpninfo)) {
Sep 22, 2011
Sep 22, 2011
764
vpn_progress(vpninfo, PRG_ERR, _("DTLS rekey failed\n"));
Sep 23, 2008
Sep 23, 2008
765
766
return 1;
}
Oct 2, 2008
Oct 2, 2008
767
768
769
770
771
work_done = 1;
break;
case KA_DPD_DEAD:
Sep 22, 2011
Sep 22, 2011
772
vpn_progress(vpninfo, PRG_ERR, _("DTLS Dead Peer Detection detected dead peer!\n"));
Oct 2, 2008
Oct 2, 2008
773
774
775
/* Fall back to SSL, and start a new DTLS connection */
dtls_restart(vpninfo);
return 1;
Oct 2, 2008
Oct 2, 2008
776
777
case KA_DPD:
Sep 22, 2011
Sep 22, 2011
778
vpn_progress(vpninfo, PRG_TRACE, _("Send DTLS DPD\n"));
Sep 23, 2008
Sep 23, 2008
779
Oct 2, 2008
Oct 2, 2008
780
magic_pkt = AC_PKT_DPD_OUT;
Jun 7, 2012
Jun 7, 2012
781
782
783
784
if (DTLS_SEND(vpninfo->dtls_ssl, &magic_pkt, 1) != 1)
vpn_progress(vpninfo, PRG_ERR,
_("Failed to send DPD request. Expect disconnect\n"));
Oct 2, 2008
Oct 2, 2008
785
786
787
788
/* last_dpd will just have been set */
vpninfo->dtls_times.last_tx = vpninfo->dtls_times.last_dpd;
work_done = 1;
break;
Sep 23, 2008
Sep 23, 2008
789
Oct 2, 2008
Oct 2, 2008
790
791
792
793
794
case KA_KEEPALIVE:
/* No need to send an explicit keepalive
if we have real data to send */
if (vpninfo->outgoing_queue)
break;
Sep 23, 2008
Sep 23, 2008
795
Sep 22, 2011
Sep 22, 2011
796
vpn_progress(vpninfo, PRG_TRACE, _("Send DTLS Keepalive\n"));
Oct 2, 2008
Oct 2, 2008
797
Oct 2, 2008
Oct 2, 2008
798
magic_pkt = AC_PKT_KEEPALIVE;
Jun 7, 2012
Jun 7, 2012
799
800
801
if (DTLS_SEND(vpninfo->dtls_ssl, &magic_pkt, 1) != 1)
vpn_progress(vpninfo, PRG_ERR,
_("Failed to send keepalive request. Expect disconnect\n"));
Oct 2, 2008
Oct 2, 2008
802
803
804
time(&vpninfo->dtls_times.last_tx);
work_done = 1;
break;
Sep 23, 2008
Sep 23, 2008
805
Oct 2, 2008
Oct 2, 2008
806
807
case KA_NONE:
;
Sep 23, 2008
Sep 23, 2008
808
809
}
Oct 2, 2008
Oct 2, 2008
810
/* Service outgoing packet queue */
Oct 2, 2008
Oct 2, 2008
811
812
813
while (vpninfo->outgoing_queue) {
struct pkt *this = vpninfo->outgoing_queue;
int ret;
Sep 29, 2008
Sep 29, 2008
814
Oct 2, 2008
Oct 2, 2008
815
vpninfo->outgoing_queue = this->next;
Oct 26, 2008
Oct 26, 2008
816
vpninfo->outgoing_qlen--;
Oct 2, 2008
Oct 2, 2008
817
818
819
/* One byte of header */
this->hdr[7] = AC_PKT_DATA;
Apr 9, 2009
Apr 9, 2009
820
Jun 11, 2012
Jun 11, 2012
821
#if defined(DTLS_OPENSSL)
Oct 2, 2008
Oct 2, 2008
822
ret = SSL_write(vpninfo->dtls_ssl, &this->hdr[7], this->len + 1);
Oct 2, 2008
Oct 2, 2008
823
824
825
826
827
828
if (ret <= 0) {
ret = SSL_get_error(vpninfo->dtls_ssl, ret);
/* If it's a real error, kill the DTLS connection and
requeue the packet to be sent over SSL */
if (ret != SSL_ERROR_WANT_READ && ret != SSL_ERROR_WANT_WRITE) {
Jun 27, 2011
Jun 27, 2011
829
vpn_progress(vpninfo, PRG_ERR,
Sep 22, 2011
Sep 22, 2011
830
831
_("DTLS got write error %d. Falling back to SSL\n"),
ret);
May 14, 2012
May 14, 2012
832
openconnect_report_ssl_errors(vpninfo);
Oct 2, 2008
Oct 2, 2008
833
834
dtls_restart(vpninfo);
vpninfo->outgoing_queue = this;
Oct 26, 2008
Oct 26, 2008
835
vpninfo->outgoing_qlen++;
Oct 2, 2008
Oct 2, 2008
836
837
838
}
return 1;
}
Jun 11, 2012
Jun 11, 2012
839
#elif defined (DTLS_GNUTLS)
Jun 7, 2012
Jun 7, 2012
840
841
842
843
844
845
846
847
848
849
850
851
852
ret = gnutls_record_send(vpninfo->dtls_ssl, &this->hdr[7], this->len + 1);
if (ret <= 0) {
if (ret != GNUTLS_E_AGAIN) {
vpn_progress(vpninfo, PRG_ERR,
_("DTLS got write error: %s. Falling back to SSL\n"),
gnutls_strerror(ret));
dtls_restart(vpninfo);
vpninfo->outgoing_queue = this;
vpninfo->outgoing_qlen++;
}
return 1;
}
#endif
Oct 2, 2008
Oct 2, 2008
853
time(&vpninfo->dtls_times.last_tx);
Jun 27, 2011
Jun 27, 2011
854
vpn_progress(vpninfo, PRG_TRACE,
Jun 7, 2012
Jun 7, 2012
855
_("Sent DTLS packet of %d bytes; DTLS send returned %d\n"),
Sep 22, 2011
Sep 22, 2011
856
this->len, ret);
Jan 28, 2009
Jan 28, 2009
857
free(this);
Sep 29, 2008
Sep 29, 2008
858
}
Sep 23, 2008
Sep 23, 2008
859
Sep 23, 2008
Sep 23, 2008
860
return work_done;
Sep 22, 2008
Sep 22, 2008
861
}
Jun 7, 2012
Jun 7, 2012
862
863
#else /* !HAVE_DTLS */
#warning Your SSL library does not seem to support Cisco DTLS compatibility
Sep 15, 2011
Sep 15, 2011
864
int setup_dtls(struct openconnect_info *vpninfo)
Jun 2, 2009
Jun 2, 2009
865
{
Sep 22, 2011
Sep 22, 2011
866
vpn_progress(vpninfo, PRG_ERR,
Jun 7, 2012
Jun 7, 2012
867
_("Built against SSL library with no Cisco DTLS support\n"));
Jun 2, 2009
Jun 2, 2009
868
869
870
return -EINVAL;
}
#endif