2 * Open AnyConnect (SSL + DTLS) client
4 * © 2008 David Woodhouse <dwmw2@infradead.org>
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose with or without fee is hereby granted, provided
8 * that the above copyright notice and this permission notice appear
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
12 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
13 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
14 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
15 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
16 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
17 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <sys/types.h>
23 #include <sys/socket.h>
26 #include <openssl/err.h>
29 #include "anyconnect.h"
32 * The master-secret is generated randomly by the client. The server
33 * responds with a DTLS Session-ID. These, done over the HTTPS
34 * connection, are enough to 'resume' a DTLS session, bypassing all
35 * the normal setup of a normal DTLS connection.
37 * Cisco use a version of the protocol which predates RFC4347, but
38 * isn't quite the same as the pre-RFC version of the protocol which
39 * was in OpenSSL 0.9.8e -- it includes backports of some later
42 * The openssl/ directory of this source tree should contain both a
43 * small patch against OpenSSL 0.9.8e to make it support Cisco's
44 * snapshot of the protocol, and a larger patch against newer OpenSSL
45 * which gives us an option to use the old protocol again.
47 * Cisco's server also seems to respond to the official version of the
48 * protocol, with a change in the ChangeCipherSpec packet which implies
49 * that it does know the difference and isn't just repeating the version
50 * number seen in the ClientHello. But although I can make the handshake
51 * complete by hacking tls1_mac() to use the _old_ protocol version
52 * number when calculating the MAC, the server still seems to be ignoring
53 * my subsequent data packets.
56 static unsigned char nybble(unsigned char n)
58 if (n >= '0' && n <= '9') return n - '0';
59 else if (n >= 'A' && n <= 'F') return n - ('A' - 10);
60 else if (n >= 'a' && n <= 'f') return n - ('a' - 10);
64 static unsigned char hex(const char *data)
66 return (nybble(data[0]) << 4) | nybble(data[1]);
69 static int connect_dtls_socket(struct anyconnect_info *vpninfo, SSL **ret_ssl,
72 SSL_METHOD *dtls_method;
74 SSL_SESSION *dtls_session;
75 SSL_CIPHER *https_cipher;
81 dtls_fd = socket(vpninfo->peer_addr->sa_family, SOCK_DGRAM, IPPROTO_UDP);
83 perror("Open UDP socket for DTLS:");
87 if (connect(dtls_fd, vpninfo->peer_addr, vpninfo->peer_addrlen)) {
88 perror("UDP (DTLS) connect:\n");
93 dtls_method = DTLSv1_client_method();
94 dtls_ctx = SSL_CTX_new(dtls_method);
95 SSL_CTX_set_read_ahead(dtls_ctx, 1);
96 https_cipher = SSL_get_current_cipher(vpninfo->https_ssl);
98 dtls_ssl = SSL_new(dtls_ctx);
99 SSL_set_connect_state(dtls_ssl);
100 SSL_set_cipher_list(dtls_ssl, SSL_CIPHER_get_name(https_cipher));
103 printf("SSL_SESSION is %zd bytes\n", sizeof(*dtls_session));
104 /* We're going to "resume" a session which never existed. Fake it... */
105 dtls_session = SSL_SESSION_new();
107 dtls_session->ssl_version = 0x0100; // DTLS1_BAD_VER
109 dtls_session->master_key_length = sizeof(vpninfo->dtls_secret);
110 memcpy(dtls_session->master_key, vpninfo->dtls_secret,
111 sizeof(vpninfo->dtls_secret));
113 dtls_session->session_id_length = sizeof(vpninfo->dtls_session_id);
114 memcpy(dtls_session->session_id, vpninfo->dtls_session_id,
115 sizeof(vpninfo->dtls_session_id));
117 dtls_session->cipher = https_cipher;
118 dtls_session->cipher_id = https_cipher->id;
120 /* Having faked a session, add it to the CTX and the SSL */
121 if (!SSL_set_session(dtls_ssl, dtls_session)) {
122 printf("SSL_set_session() failed with old protocol version 0x%x\n", dtls_session->ssl_version);
123 printf("Trying the official version %x\n", 0xfeff);
124 dtls_session->ssl_version = 0xfeff;
125 if (!SSL_set_session(dtls_ssl, dtls_session)) {
126 printf("SSL_set_session() failed still. Is your build ABI-compatible with your libssl?\n");
130 if (!SSL_CTX_add_session(dtls_ctx, dtls_session))
131 printf("SSL_CTX_add_session() failed\n");
135 dtls_bio = BIO_new_socket(dtls_fd, BIO_NOCLOSE);
136 SSL_set_bio(dtls_ssl, dtls_bio, dtls_bio);
138 #ifndef SSL_OP_CISCO_ANYCONNECT
139 #define SSL_OP_CISCO_ANYCONNECT 0x8000
141 SSL_set_options(dtls_ssl, SSL_OP_CISCO_ANYCONNECT);
142 ret = SSL_do_handshake(dtls_ssl);
145 fprintf(stderr, "DTLS connection returned %d\n", ret);
147 fprintf(stderr, "DTLS handshake error: %d\n", SSL_get_error(dtls_ssl, ret));
148 ERR_print_errors_fp(stderr);
150 SSL_CTX_free(dtls_ctx);
161 static int dtls_rekey(struct anyconnect_info *vpninfo)
163 printf("FIXME: Implement DTLS rekey\n");
167 int setup_dtls(struct anyconnect_info *vpninfo)
169 struct vpn_option *dtls_opt = vpninfo->dtls_options;
170 int sessid_found = 0;
176 printf("DTLS option %s : %s\n", dtls_opt->option, dtls_opt->value);
178 if (!strcmp(dtls_opt->option, "X-DTLS-Session-ID")) {
179 if (strlen(dtls_opt->value) != 64) {
180 fprintf(stderr, "X-DTLS-Session-ID not 64 characters\n");
181 fprintf(stderr, "Is: %s\n", dtls_opt->value);
184 for (i = 0; i < 64; i += 2)
185 vpninfo->dtls_session_id[i/2] = hex(dtls_opt->value + i);
187 } else if (!strcmp(dtls_opt->option + 7, "Port")) {
188 dtls_port = atol(dtls_opt->value);
189 } else if (!strcmp(dtls_opt->option + 7, "Keepalive")) {
190 vpninfo->dtls_keepalive = atol(dtls_opt->value);
191 } else if (!strcmp(dtls_opt->option + 7, "DPD")) {
192 vpninfo->dtls_dpd = atol(dtls_opt->value);
193 } else if (!strcmp(dtls_opt->option + 7, "Rekey-Time")) {
194 vpninfo->dtls_rekey = atol(dtls_opt->value);
197 dtls_opt = dtls_opt->next;
199 if (!sessid_found || !dtls_port)
202 if (vpninfo->peer_addr->sa_family == AF_INET) {
203 struct sockaddr_in *sin = (void *)vpninfo->peer_addr;
204 sin->sin_port = htons(dtls_port);
205 } else if (vpninfo->peer_addr->sa_family == AF_INET6) {
206 struct sockaddr_in6 *sin = (void *)vpninfo->peer_addr;
207 sin->sin6_port = htons(dtls_port);
209 fprintf(stderr, "Unknown protocol family %d. Cannot do DTLS\n",
210 vpninfo->peer_addr->sa_family);
214 if (connect_dtls_socket(vpninfo, &vpninfo->dtls_ssl, &vpninfo->dtls_fd))
217 BIO_set_nbio(SSL_get_rbio(vpninfo->dtls_ssl),1);
218 BIO_set_nbio(SSL_get_wbio(vpninfo->dtls_ssl),1);
220 fcntl(vpninfo->dtls_fd, F_SETFL, fcntl(vpninfo->dtls_fd, F_GETFL) | O_NONBLOCK);
222 vpn_add_pollfd(vpninfo, vpninfo->dtls_fd, POLLIN|POLLHUP|POLLERR);
223 vpninfo->last_dtls_rekey = vpninfo->last_dtls_rx =
224 vpninfo->last_dtls_tx = time(NULL);
227 printf("DTLS connected. DPD %d, Keepalive %d\n",
228 vpninfo->dtls_dpd, vpninfo->dtls_keepalive);
233 int dtls_mainloop(struct anyconnect_info *vpninfo, int *timeout)
239 while ( (len = SSL_read(vpninfo->dtls_ssl, buf, sizeof(buf))) > 0 ) {
241 printf("Received DTLS packet of %d bytes\n", len);
242 printf("Packet starts %02x %02x %02x %02x %02x %02x %02x %02x\n",
243 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
245 vpninfo->last_dtls_rx = time(NULL);
248 queue_new_packet(&vpninfo->incoming_queue, AF_INET, buf+1, len-1);
252 case 4: /* DPD response */
254 printf("Got DTLS DPD response\n");
258 fprintf(stderr, "Unknown DTLS packet type %02x\n", buf[0]);
259 vpninfo->quit_reason = "Unknown packet received";
263 while (vpninfo->outgoing_queue) {
264 struct pkt *this = vpninfo->outgoing_queue;
267 vpninfo->outgoing_queue = this->next;
270 memcpy(buf + 1, this->data, this->len);
272 ret = SSL_write(vpninfo->dtls_ssl, buf, this->len + 1);
273 vpninfo->last_dtls_tx = time(NULL);
275 printf("Sent DTLS packet of %d bytes; SSL_write() returned %d\n",
280 /* DPD is bidirectional -- PKT 3 out, PKT 4 back */
281 if (vpninfo->dtls_dpd) {
282 time_t now = time(NULL);
283 time_t due = vpninfo->last_dtls_rx + vpninfo->dtls_dpd;
284 time_t overdue = vpninfo->last_dtls_rx + (5 * vpninfo->dtls_dpd);
286 /* If we already have DPD outstanding, don't flood */
287 if (vpninfo->last_dtls_dpd > vpninfo->last_dtls_rx)
288 due = vpninfo->last_dtls_dpd + vpninfo->dtls_dpd;
291 fprintf(stderr, "DTLS Dead Peer Detection detected dead peer!\n");
292 /* Fall back to SSL */
293 SSL_free(vpninfo->dtls_ssl);
294 close(vpninfo->dtls_fd);
295 vpninfo->dtls_ssl = NULL;
296 vpninfo->dtls_fd = -1;
300 static unsigned char dtls_dpd_pkt[1] = { 3 };
301 /* Haven't heard anything from the other end for a while.
302 Check if it's still there */
303 /* FIXME: If isn't, we should act on that */
304 SSL_write(vpninfo->dtls_ssl, dtls_dpd_pkt, 1);
305 vpninfo->last_dtls_tx = now;
307 due = now + vpninfo->dtls_dpd;
309 printf("Sent DTLS DPD\n");
313 printf("Next DTLS DPD due in %ld seconds\n", (due - now));
314 if (*timeout > (due - now) * 1000)
315 *timeout = (due - now) * 1000;
318 /* Keepalive is just client -> server */
319 if (vpninfo->dtls_keepalive) {
320 time_t now = time(NULL);
321 time_t due = vpninfo->last_dtls_tx + vpninfo->dtls_keepalive;
324 static unsigned char dtls_keepalive_pkt[1] = { 7 };
326 /* Send something (which is discarded), to keep
327 the connection alive. */
328 SSL_write(vpninfo->dtls_ssl, dtls_keepalive_pkt, 1);
329 vpninfo->last_dtls_tx = now;
331 due = now + vpninfo->dtls_keepalive;
333 printf("Sent DTLS Keepalive\n");
337 printf("Next DTLS Keepalive due in %ld seconds\n", (due - now));
338 if (*timeout > (due - now) * 1000)
339 *timeout = (due - now) * 1000;
342 if (vpninfo->dtls_rekey) {
343 time_t now = time(NULL);
344 time_t due = vpninfo->last_dtls_rekey + vpninfo->dtls_rekey;
348 printf("DTLS rekey due\n");
349 if (dtls_rekey(vpninfo)) {
350 fprintf(stderr, "DTLS rekey failed\n");
351 /* Fall back to SSL */
352 SSL_free(vpninfo->dtls_ssl);
353 close(vpninfo->dtls_fd);
354 vpninfo->dtls_ssl = NULL;
355 vpninfo->dtls_fd = -1;
358 vpninfo->last_dtls_rekey = time(NULL);
359 due = vpninfo->last_dtls_rekey + vpninfo->dtls_rekey;
362 printf("Next DTLS rekey due in %ld seconds\n", (due - now));
363 if (*timeout > (due - now) * 1000)
364 *timeout = (due - now) * 1000;
367 /* FIXME: Keepalive */