2
* OpenConnect (SSL + DTLS) VPN client
4
* Copyright © 2008 Intel Corporation.
6
* Author: David Woodhouse <dwmw2@infradead.org>
8
* This program is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Lesser General Public License
10
* version 2.1, as published by the Free Software Foundation.
12
* This program is distributed in the hope that it will be useful, but
13
* WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to:
20
* Free Software Foundation, Inc.
21
* 51 Franklin Street, Fifth Floor,
22
* Boston, MA 02110-1301 USA
26
#include <sys/types.h>
27
#include <sys/socket.h>
30
#include <netinet/in.h>
31
#include <openssl/err.h>
35
#include "openconnect.h"
39
* Useful for catching test cases, where we want everything to be
40
* reproducible. *NEVER* do this in the wild.
42
time_t time(time_t *t)
44
time_t x = 0x3ab2d948;
49
int RAND_pseudo_bytes(char *buf, int len)
51
memset(buf, 0x5a, len);
52
printf("FAKE PSEUDO RANDOM!\n");
56
int RAND_bytes(char *buf, int len)
58
static int foo = 0x5b;
59
printf("FAKE RANDOM!\n");
60
memset(buf, foo, len);
66
* The master-secret is generated randomly by the client. The server
67
* responds with a DTLS Session-ID. These, done over the HTTPS
68
* connection, are enough to 'resume' a DTLS session, bypassing all
69
* the normal setup of a normal DTLS connection.
71
* Cisco use a version of the protocol which predates RFC4347, but
72
* isn't quite the same as the pre-RFC version of the protocol which
73
* was in OpenSSL 0.9.8e -- it includes backports of some later
76
* The openssl/ directory of this source tree should contain both a
77
* small patch against OpenSSL 0.9.8e to make it support Cisco's
78
* snapshot of the protocol, and a larger patch against newer OpenSSL
79
* which gives us an option to use the old protocol again.
81
* Cisco's server also seems to respond to the official version of the
82
* protocol, with a change in the ChangeCipherSpec packet which implies
83
* that it does know the difference and isn't just repeating the version
84
* number seen in the ClientHello. But although I can make the handshake
85
* complete by hacking tls1_mac() to use the _old_ protocol version
86
* number when calculating the MAC, the server still seems to be ignoring
87
* my subsequent data packets. So we use the old protocol, which is what
88
* their clients use anyway.
91
static unsigned char nybble(unsigned char n)
93
if (n >= '0' && n <= '9') return n - '0';
94
else if (n >= 'A' && n <= 'F') return n - ('A' - 10);
95
else if (n >= 'a' && n <= 'f') return n - ('a' - 10);
99
static unsigned char hex(const char *data)
101
return (nybble(data[0]) << 4) | nybble(data[1]);
104
int connect_dtls_socket(struct openconnect_info *vpninfo)
106
SSL_METHOD *dtls_method;
107
SSL_CIPHER *https_cipher;
112
dtls_fd = socket(vpninfo->peer_addr->sa_family, SOCK_DGRAM, IPPROTO_UDP);
114
perror("Open UDP socket for DTLS:");
118
if (connect(dtls_fd, vpninfo->peer_addr, vpninfo->peer_addrlen)) {
119
perror("UDP (DTLS) connect:\n");
124
fcntl(dtls_fd, F_SETFD, FD_CLOEXEC);
126
https_cipher = SSL_get_current_cipher(vpninfo->https_ssl);
128
if (!vpninfo->dtls_ctx) {
129
dtls_method = DTLSv1_client_method();
130
vpninfo->dtls_ctx = SSL_CTX_new(dtls_method);
131
if (!vpninfo->dtls_ctx) {
132
vpninfo->progress(vpninfo, PRG_ERR, "Initialise DTLSv1 CTX failed\n");
136
/* If we don't readahead, then we do short reads and throw
137
away the tail of data packets. */
138
SSL_CTX_set_read_ahead(vpninfo->dtls_ctx, 1);
141
if (!vpninfo->dtls_session) {
142
/* We're going to "resume" a session which never existed. Fake it... */
143
vpninfo->dtls_session = SSL_SESSION_new();
144
if (!vpninfo->dtls_session) {
145
vpninfo->progress(vpninfo, PRG_ERR, "Initialise DTLSv1 session failed\n");
148
vpninfo->dtls_session->ssl_version = 0x0100; // DTLS1_BAD_VER
150
vpninfo->dtls_session->master_key_length = sizeof(vpninfo->dtls_secret);
151
memcpy(vpninfo->dtls_session->master_key, vpninfo->dtls_secret,
152
sizeof(vpninfo->dtls_secret));
154
vpninfo->dtls_session->session_id_length = sizeof(vpninfo->dtls_session_id);
155
memcpy(vpninfo->dtls_session->session_id, vpninfo->dtls_session_id,
156
sizeof(vpninfo->dtls_session_id));
158
vpninfo->dtls_session->cipher = https_cipher;
159
vpninfo->dtls_session->cipher_id = https_cipher->id;
162
dtls_ssl = SSL_new(vpninfo->dtls_ctx);
163
SSL_set_connect_state(dtls_ssl);
164
SSL_set_cipher_list(dtls_ssl, SSL_CIPHER_get_name(https_cipher));
166
/* Add the generated session to the SSL */
167
if (!SSL_set_session(dtls_ssl, vpninfo->dtls_session)) {
168
vpninfo->progress(vpninfo, PRG_ERR,
169
"SSL_set_session() failed with old protocol version 0x%x\n"
170
"Your OpenSSL may lack Cisco compatibility support\n"
171
"See http://rt.openssl.org/Ticket/Display.html?id=1751\n"
172
"Use the --no-dtls command line option to avoid this message\n",
173
vpninfo->dtls_session->ssl_version);
174
vpninfo->dtls_attempt_period = 0;
179
dtls_bio = BIO_new_socket(dtls_fd, BIO_NOCLOSE);
180
SSL_set_bio(dtls_ssl, dtls_bio, dtls_bio);
182
#ifndef SSL_OP_CISCO_ANYCONNECT
183
#define SSL_OP_CISCO_ANYCONNECT 0x8000
185
SSL_set_options(dtls_ssl, SSL_OP_CISCO_ANYCONNECT);
187
/* Set non-blocking */
188
BIO_set_nbio(SSL_get_rbio(dtls_ssl),1);
189
BIO_set_nbio(SSL_get_wbio(dtls_ssl),1);
191
fcntl(dtls_fd, F_SETFL, fcntl(dtls_fd, F_GETFL) | O_NONBLOCK);
193
vpninfo->new_dtls_fd = dtls_fd;
194
vpninfo->new_dtls_ssl = dtls_ssl;
196
if (vpninfo->select_nfds <= dtls_fd)
197
vpninfo->select_nfds = dtls_fd + 1;
199
FD_SET(dtls_fd, &vpninfo->select_rfds);
200
FD_SET(dtls_fd, &vpninfo->select_efds);
202
time(&vpninfo->new_dtls_started);
203
return dtls_try_handshake(vpninfo);
206
int dtls_try_handshake(struct openconnect_info *vpninfo)
208
int ret = SSL_do_handshake(vpninfo->new_dtls_ssl);
211
vpninfo->progress(vpninfo, PRG_INFO, "Established DTLS connection\n");
213
if (vpninfo->dtls_ssl) {
214
/* We are replacing an old connection */
215
SSL_free(vpninfo->dtls_ssl);
216
close(vpninfo->dtls_fd);
217
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_rfds);
218
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_wfds);
219
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_efds);
221
vpninfo->dtls_ssl = vpninfo->new_dtls_ssl;
222
vpninfo->dtls_fd = vpninfo->new_dtls_fd;
224
vpninfo->new_dtls_ssl = NULL;
225
vpninfo->new_dtls_fd = -1;
227
vpninfo->dtls_times.last_rekey = vpninfo->dtls_times.last_rx =
228
vpninfo->dtls_times.last_tx = time(NULL);
233
ret = SSL_get_error(vpninfo->new_dtls_ssl, ret);
234
if (ret == SSL_ERROR_WANT_WRITE || ret == SSL_ERROR_WANT_READ) {
235
if (time(NULL) < vpninfo->new_dtls_started + 5)
237
vpninfo->progress(vpninfo, PRG_TRACE, "DTLS handshake timed out\n");
240
vpninfo->progress(vpninfo, PRG_ERR, "DTLS handshake failed: %d\n", ret);
241
ERR_print_errors_fp(stderr);
243
/* Kill the new (failed) connection... */
244
SSL_free(vpninfo->new_dtls_ssl);
245
FD_CLR(vpninfo->new_dtls_fd, &vpninfo->select_rfds);
246
FD_CLR(vpninfo->new_dtls_fd, &vpninfo->select_efds);
247
close(vpninfo->new_dtls_fd);
248
vpninfo->new_dtls_ssl = NULL;
249
vpninfo->new_dtls_fd = -1;
251
/* ... and kill the old one too. The only time there'll be a valid
252
existing session is when it was a rekey, and in that case it's
253
time for the old one to die. */
254
if (vpninfo->dtls_ssl) {
255
SSL_free(vpninfo->dtls_ssl);
256
close(vpninfo->dtls_fd);
257
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_rfds);
258
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_wfds);
259
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_efds);
260
vpninfo->dtls_ssl = NULL;
261
vpninfo->dtls_fd = -1;
264
time(&vpninfo->new_dtls_started);
268
static int dtls_restart(struct openconnect_info *vpninfo)
270
if (vpninfo->dtls_ssl) {
271
SSL_free(vpninfo->dtls_ssl);
272
close(vpninfo->dtls_fd);
273
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_rfds);
274
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_wfds);
275
FD_CLR(vpninfo->dtls_fd, &vpninfo->select_efds);
276
vpninfo->dtls_ssl = NULL;
277
vpninfo->dtls_fd = -1;
280
return connect_dtls_socket(vpninfo);
284
int setup_dtls(struct openconnect_info *vpninfo)
286
struct vpn_option *dtls_opt = vpninfo->dtls_options;
287
int sessid_found = 0;
292
vpninfo->progress(vpninfo, PRG_TRACE,
293
"DTLS option %s : %s\n",
294
dtls_opt->option, dtls_opt->value);
296
if (!strcmp(dtls_opt->option, "X-DTLS-Session-ID")) {
297
if (strlen(dtls_opt->value) != 64) {
298
vpninfo->progress(vpninfo, PRG_ERR, "X-DTLS-Session-ID not 64 characters\n");
299
vpninfo->progress(vpninfo, PRG_ERR, "Is: %s\n", dtls_opt->value);
302
for (i = 0; i < 64; i += 2)
303
vpninfo->dtls_session_id[i/2] = hex(dtls_opt->value + i);
305
} else if (!strcmp(dtls_opt->option + 7, "Port")) {
306
dtls_port = atol(dtls_opt->value);
307
} else if (!strcmp(dtls_opt->option + 7, "Keepalive")) {
308
vpninfo->dtls_times.keepalive = atol(dtls_opt->value);
309
} else if (!strcmp(dtls_opt->option + 7, "DPD")) {
310
vpninfo->dtls_times.dpd = atol(dtls_opt->value);
311
} else if (!strcmp(dtls_opt->option + 7, "Rekey-Time")) {
312
vpninfo->dtls_times.rekey = atol(dtls_opt->value);
315
dtls_opt = dtls_opt->next;
317
if (!sessid_found || !dtls_port)
320
if (vpninfo->peer_addr->sa_family == AF_INET) {
321
struct sockaddr_in *sin = (void *)vpninfo->peer_addr;
322
sin->sin_port = htons(dtls_port);
323
} else if (vpninfo->peer_addr->sa_family == AF_INET6) {
324
struct sockaddr_in6 *sin = (void *)vpninfo->peer_addr;
325
sin->sin6_port = htons(dtls_port);
327
vpninfo->progress(vpninfo, PRG_ERR, "Unknown protocol family %d. Cannot do DTLS\n",
328
vpninfo->peer_addr->sa_family);
333
if (connect_dtls_socket(vpninfo))
336
vpninfo->progress(vpninfo, PRG_TRACE,
337
"DTLS connected. DPD %d, Keepalive %d\n",
338
vpninfo->dtls_times.dpd, vpninfo->dtls_times.keepalive);
343
int dtls_mainloop(struct openconnect_info *vpninfo, int *timeout)
345
unsigned char buf[2000];
350
while ( (len = SSL_read(vpninfo->dtls_ssl, buf, sizeof(buf))) > 0 ) {
352
vpninfo->progress(vpninfo, PRG_TRACE,
353
"Received DTLS packet 0x%02x of %d bytes\n",
356
vpninfo->dtls_times.last_rx = time(NULL);
360
queue_new_packet(&vpninfo->incoming_queue, AF_INET, buf+1, len-1);
365
vpninfo->progress(vpninfo, PRG_TRACE, "Got DTLS DPD request\n");
367
/* FIXME: What if the packet doesn't get through? */
368
magic_pkt = AC_PKT_DPD_RESP;
369
if (SSL_write(vpninfo->dtls_ssl, &magic_pkt, 1) != 1)
370
vpninfo->progress(vpninfo, PRG_ERR, "Failed to send DPD response. Expect disconnect\n");
373
case AC_PKT_DPD_RESP:
374
vpninfo->progress(vpninfo, PRG_TRACE, "Got DTLS DPD response\n");
377
case AC_PKT_KEEPALIVE:
378
vpninfo->progress(vpninfo, PRG_TRACE, "Got DTLS Keepalive\n");
382
vpninfo->progress(vpninfo, PRG_ERR,
383
"Unknown DTLS packet type %02x, len %d\n", buf[0], len);
385
/* Some versions of OpenSSL have bugs with receiving out-of-order
386
* packets. Not only do they wrongly decide to drop packets if
387
* two packets get swapped in transit, but they also _fail_ to
388
* drop the packet in non-blocking mode; instead they return
389
* the appropriate length of garbage. So don't abort... for now. */
392
vpninfo->quit_reason = "Unknown packet received";
399
switch (keepalive_action(&vpninfo->dtls_times, timeout)) {
401
time(&vpninfo->dtls_times.last_rekey);
402
vpninfo->progress(vpninfo, PRG_TRACE, "DTLS rekey due\n");
403
if (connect_dtls_socket(vpninfo)) {
404
vpninfo->progress(vpninfo, PRG_ERR, "DTLS rekey failed\n");
412
vpninfo->progress(vpninfo, PRG_ERR, "DTLS Dead Peer Detection detected dead peer!\n");
413
/* Fall back to SSL, and start a new DTLS connection */
414
dtls_restart(vpninfo);
418
vpninfo->progress(vpninfo, PRG_TRACE, "Send DTLS DPD\n");
420
magic_pkt = AC_PKT_DPD_OUT;
421
SSL_write(vpninfo->dtls_ssl, &magic_pkt, 1);
422
/* last_dpd will just have been set */
423
vpninfo->dtls_times.last_tx = vpninfo->dtls_times.last_dpd;
428
/* No need to send an explicit keepalive
429
if we have real data to send */
430
if (vpninfo->outgoing_queue)
433
vpninfo->progress(vpninfo, PRG_TRACE, "Send DTLS Keepalive\n");
435
magic_pkt = AC_PKT_KEEPALIVE;
436
SSL_write(vpninfo->dtls_ssl, &magic_pkt, 1);
437
time(&vpninfo->dtls_times.last_tx);
445
/* Service outgoing packet queue */
446
while (vpninfo->outgoing_queue) {
447
struct pkt *this = vpninfo->outgoing_queue;
450
vpninfo->outgoing_queue = this->next;
451
vpninfo->outgoing_qlen--;
453
/* One byte of header */
454
this->hdr[7] = AC_PKT_DATA;
456
ret = SSL_write(vpninfo->dtls_ssl, &this->hdr[7], this->len + 1);
458
ret = SSL_get_error(vpninfo->dtls_ssl, ret);
460
/* If it's a real error, kill the DTLS connection and
461
requeue the packet to be sent over SSL */
462
if (ret != SSL_ERROR_WANT_READ && ret != SSL_ERROR_WANT_WRITE) {
463
vpninfo->progress(vpninfo, PRG_ERR,
464
"DTLS got write error %d. Falling back to SSL\n", ret);
465
ERR_print_errors_fp(stderr);
466
dtls_restart(vpninfo);
467
vpninfo->outgoing_queue = this;
468
vpninfo->outgoing_qlen++;
472
time(&vpninfo->dtls_times.last_tx);
473
vpninfo->progress(vpninfo, PRG_TRACE,
474
"Sent DTLS packet of %d bytes; SSL_write() returned %d\n",