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 * This code works when run against Cisco's own libssl.so.0.9.8, but
38 * fails (Bad Record MAC on receipt of the Server Hello) when run
39 * against my own build of OpenSSL-0.9.8f.
41 * It lookslike they've reverted _some_ of the changes beween 0.9.8e
42 * and 0.9.8f, but not all of them. In particular, they use
43 * DTLS1_BAD_VER for the protocol version.
45 * Using OpenSSL-0.9.8e, which was the last release of OpenSSL to use
46 * DTLS1_BAD_VER, also fails similarly.
48 * Hopefully they're just using something equivalent to a snapshot
49 * between 0.9.8e and 0.9.8f, and they don't have their own "special"
53 static unsigned char nybble(unsigned char n)
55 if (n >= '0' && n <= '9') return n - '0';
56 else if (n >= 'A' && n <= 'F') return n - ('A' - 10);
57 else if (n >= 'a' && n <= 'f') return n - ('a' - 10);
61 static unsigned char hex(const char *data)
63 return (nybble(data[0]) << 4) | nybble(data[1]);
66 static int connect_dtls_socket(struct anyconnect_info *vpninfo, int dtls_port)
68 SSL_METHOD *dtls_method;
70 SSL_SESSION *dtls_session;
71 SSL_CIPHER *https_cipher;
77 if (vpninfo->peer_addr->sa_family == AF_INET) {
78 struct sockaddr_in *sin = (void *)vpninfo->peer_addr;
79 sin->sin_port = htons(dtls_port);
80 } else if (vpninfo->peer_addr->sa_family == AF_INET6) {
81 struct sockaddr_in6 *sin = (void *)vpninfo->peer_addr;
82 sin->sin6_port = htons(dtls_port);
84 fprintf(stderr, "Unknown protocol family %d. Cannot do DTLS\n",
85 vpninfo->peer_addr->sa_family);
89 dtls_fd = socket(vpninfo->peer_addr->sa_family, SOCK_DGRAM, IPPROTO_UDP);
91 perror("Open UDP socket for DTLS:");
95 if (connect(dtls_fd, vpninfo->peer_addr, vpninfo->peer_addrlen)) {
96 perror("UDP (DTLS) connect:\n");
101 dtls_method = DTLSv1_client_method();
102 dtls_ctx = SSL_CTX_new(dtls_method);
103 SSL_CTX_set_read_ahead(dtls_ctx, 1);
104 https_cipher = SSL_get_current_cipher(vpninfo->https_ssl);
106 dtls_ssl = SSL_new(dtls_ctx);
107 SSL_set_connect_state(dtls_ssl);
108 SSL_set_cipher_list(dtls_ssl, SSL_CIPHER_get_name(https_cipher));
109 printf("SSL_SESSION is %d bytes\n", sizeof(*dtls_session));
110 /* We're going to "resume" a session which never existed. Fake it... */
111 dtls_session = SSL_SESSION_new();
113 dtls_session->ssl_version = DTLS1_BAD_VER;
115 dtls_session->master_key_length = sizeof(vpninfo->dtls_secret);
116 memcpy(dtls_session->master_key, vpninfo->dtls_secret,
117 sizeof(vpninfo->dtls_secret));
119 dtls_session->session_id_length = sizeof(vpninfo->dtls_session_id);
120 memcpy(dtls_session->session_id, vpninfo->dtls_session_id,
121 sizeof(vpninfo->dtls_session_id));
123 dtls_session->cipher = https_cipher;
124 dtls_session->cipher_id = https_cipher->id;
126 /* Having faked a session, add it to the CTX and the SSL */
127 if (!SSL_set_session(dtls_ssl, dtls_session)) {
128 printf("SSL_set_session() failed with old protocol version 0x%x\n", dtls_session->ssl_version);
129 printf("Trying the official version %x\n", DTLS1_VERSION);
130 dtls_session->ssl_version = DTLS1_VERSION;
131 if (!SSL_set_session(dtls_ssl, dtls_session)) {
132 printf("SSL_set_session() failed still. Is your build ABI-compatible with your libssl?\n");
136 if (!SSL_CTX_add_session(dtls_ctx, dtls_session))
137 printf("SSL_CTX_add_session() failed\n");
141 dtls_bio = BIO_new_socket(dtls_fd, BIO_NOCLOSE);
142 SSL_set_bio(dtls_ssl, dtls_bio, dtls_bio);
144 ret = SSL_do_handshake(dtls_ssl);
147 fprintf(stderr, "DTLS connection returned %d\n", ret);
149 fprintf(stderr, "DTLS handshake error: %d\n", SSL_get_error(dtls_ssl, ret));
150 ERR_print_errors_fp(stderr);
152 SSL_CTX_free(dtls_ctx);
156 printf("DTLS Connection successful!\n");
157 /* FIXME: implement data transfer over it! */
158 vpninfo->dtls_fd = dtls_fd;
159 vpninfo->dtls_ssl = dtls_ssl;
163 int setup_dtls(struct anyconnect_info *vpninfo)
165 struct vpn_option *dtls_opt = vpninfo->dtls_options;
166 int sessid_found = 0;
172 printf("DTLS option %s : %s\n", dtls_opt->option, dtls_opt->value);
174 if (!strcmp(dtls_opt->option, "X-DTLS-Session-ID")) {
175 if (strlen(dtls_opt->value) != 64) {
176 fprintf(stderr, "X-DTLS-Session-ID not 64 characters\n");
177 fprintf(stderr, "Is: %s\n", dtls_opt->value);
180 for (i = 0; i < 64; i += 2)
181 vpninfo->dtls_session_id[i/2] = hex(dtls_opt->value + i);
183 } else if (!strcmp(dtls_opt->option + 7, "Port")) {
184 dtls_port = atol(dtls_opt->value);
185 } else if (!strcmp(dtls_opt->option + 7, "Keepalive")) {
186 vpninfo->dtls_keepalive = atol(dtls_opt->value);
187 } else if (!strcmp(dtls_opt->option + 7, "DPD")) {
188 vpninfo->dtls_dpd = atol(dtls_opt->value);
191 dtls_opt = dtls_opt->next;
193 if (!sessid_found || !dtls_port)
196 if (connect_dtls_socket(vpninfo, dtls_port))
199 BIO_set_nbio(SSL_get_rbio(vpninfo->dtls_ssl),1);
200 BIO_set_nbio(SSL_get_wbio(vpninfo->dtls_ssl),1);
202 fcntl(vpninfo->dtls_fd, F_SETFL, fcntl(vpninfo->dtls_fd, F_GETFL) | O_NONBLOCK);
204 vpn_add_pollfd(vpninfo, vpninfo->ssl_fd, POLLIN|POLLHUP|POLLERR);
205 vpninfo->last_ssl_tx = vpninfo->last_ssl_tx = time(NULL);
210 int dtls_mainloop(struct anyconnect_info *vpninfo, int *timeout)
216 while ( (len = SSL_read(vpninfo->dtls_ssl, buf, sizeof(buf))) > 0 ) {
218 printf("Received DTLS packet of %d bytes\n", len);
219 printf("Packet starts %02x %02x %02x %02x %02x %02x %02x %02x\n",
220 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
224 queue_new_packet(&vpninfo->incoming_queue, AF_INET, buf+1, len-1);
228 case 4: /* keepalive response */
232 fprintf(stderr, "Unknown DTLS packet type %02x\n", buf[0]);
237 while (vpninfo->outgoing_queue) {
238 struct pkt *this = vpninfo->outgoing_queue;
241 vpninfo->outgoing_queue = this->next;
244 memcpy(buf + 1, this->data, this->len);
246 ret = SSL_write(vpninfo->dtls_ssl, buf, this->len + 1);
248 printf("Sent DTLS packet of %d bytes; SSL_write() returned %d\n",
253 /* FIXME: Keepalive */