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>
28 #include "anyconnect.h"
31 * The master-secret is generated randomly by the client. The server
32 * responds with a DTLS Session-ID. These, done over the HTTPS
33 * connection, are enough to 'resume' a DTLS session, bypassing all
34 * the normal setup of a normal DTLS connection.
36 * This code works when run against Cisco's own libssl.so.0.9.8, but
37 * fails (Bad Record MAC on receipt of the Server Hello) when run
38 * against my own build of OpenSSL-0.9.8f.
40 * It lookslike they've reverted _some_ of the changes beween 0.9.8e
41 * and 0.9.8f, but not all of them. In particular, they use
42 * DTLS1_BAD_VER for the protocol version.
44 * Using OpenSSL-0.9.8e, which was the last release of OpenSSL to use
45 * DTLS1_BAD_VER, also fails similarly.
47 * Hopefully they're just using something equivalent to a snapshot
48 * between 0.9.8e and 0.9.8f, and they don't have their own "special"
52 static unsigned char nybble(unsigned char n)
54 if (n >= '0' && n <= '9') return n - '0';
55 else if (n >= 'A' && n <= 'F') return n - ('A' - 10);
56 else if (n >= 'a' && n <= 'f') return n - ('a' - 10);
60 static unsigned char hex(const char *data)
62 return (nybble(data[0]) << 4) | nybble(data[1]);
65 static int connect_dtls_socket(struct anyconnect_info *vpninfo, int dtls_port)
67 SSL_METHOD *dtls_method;
69 SSL_SESSION *dtls_session;
70 SSL_CIPHER *https_cipher;
76 if (vpninfo->peer_addr->sa_family == AF_INET) {
77 struct sockaddr_in *sin = (void *)vpninfo->peer_addr;
78 sin->sin_port = htons(dtls_port);
79 } else if (vpninfo->peer_addr->sa_family == AF_INET6) {
80 struct sockaddr_in6 *sin = (void *)vpninfo->peer_addr;
81 sin->sin6_port = htons(dtls_port);
83 fprintf(stderr, "Unknown protocol family %d. Cannot do DTLS\n",
84 vpninfo->peer_addr->sa_family);
88 dtls_fd = socket(vpninfo->peer_addr->sa_family, SOCK_DGRAM, IPPROTO_UDP);
90 perror("Open UDP socket for DTLS:");
94 if (connect(dtls_fd, vpninfo->peer_addr, vpninfo->peer_addrlen)) {
95 perror("UDP (DTLS) connect:\n");
100 dtls_method = DTLSv1_client_method();
101 dtls_ctx = SSL_CTX_new(dtls_method);
102 SSL_CTX_set_read_ahead(dtls_ctx, 1);
103 https_cipher = SSL_get_current_cipher(vpninfo->https_ssl);
105 dtls_ssl = SSL_new(dtls_ctx);
106 SSL_set_connect_state(dtls_ssl);
107 SSL_set_cipher_list(dtls_ssl, SSL_CIPHER_get_name(https_cipher));
108 printf("SSL_SESSION is %d bytes\n", sizeof(*dtls_session));
109 /* We're going to "resume" a session which never existed. Fake it... */
110 dtls_session = SSL_SESSION_new();
112 dtls_session->ssl_version = DTLS1_BAD_VER;
114 dtls_session->master_key_length = sizeof(vpninfo->dtls_secret);
115 memcpy(dtls_session->master_key, vpninfo->dtls_secret,
116 sizeof(vpninfo->dtls_secret));
118 dtls_session->session_id_length = sizeof(vpninfo->dtls_session_id);
119 memcpy(dtls_session->session_id, vpninfo->dtls_session_id,
120 sizeof(vpninfo->dtls_session_id));
122 dtls_session->cipher = https_cipher;
123 dtls_session->cipher_id = https_cipher->id;
125 /* Having faked a session, add it to the CTX and the SSL */
126 if (!SSL_set_session(dtls_ssl, dtls_session)) {
127 printf("SSL_set_session() failed with old protocol version 0x%x\n", dtls_session->ssl_version);
128 printf("Trying the official version %x\n", DTLS1_VERSION);
129 dtls_session->ssl_version = DTLS1_VERSION;
130 if (!SSL_set_session(dtls_ssl, dtls_session)) {
131 printf("SSL_set_session() failed still. Is your build ABI-comptible?\n");
135 if (!SSL_CTX_add_session(dtls_ctx, dtls_session))
136 printf("SSL_CTX_add_session() failed\n");
140 dtls_bio = BIO_new_socket(dtls_fd, BIO_NOCLOSE);
141 SSL_set_bio(dtls_ssl, dtls_bio, dtls_bio);
143 ret = SSL_do_handshake(dtls_ssl);
146 fprintf(stderr, "DTLS connection returned %d\n", ret);
148 fprintf(stderr, "DTLS handshake error: %d\n", SSL_get_error(dtls_ssl, ret));
149 ERR_print_errors_fp(stderr);
151 SSL_CTX_free(dtls_ctx);
155 printf("DTLS Connection successful!\n");
156 /* FIXME: implement data transfer over it! */
157 vpninfo->dtls_fd = dtls_fd;
161 int setup_dtls(struct anyconnect_info *vpninfo)
163 struct vpn_option *dtls_opt = vpninfo->dtls_options;
164 int sessid_found = 0;
170 printf("DTLS option %s : %s\n", dtls_opt->option, dtls_opt->value);
172 if (!strcmp(dtls_opt->option, "X-DTLS-Session-ID")) {
173 if (strlen(dtls_opt->value) != 64) {
174 fprintf(stderr, "X-DTLS-Session-ID not 64 characters\n");
175 fprintf(stderr, "Is: %s\n", dtls_opt->value);
178 for (i = 0; i < 64; i += 2)
179 vpninfo->dtls_session_id[i/2] = hex(dtls_opt->value + i);
181 } else if (!strcmp(dtls_opt->option, "X-DTLS-Port")) {
182 dtls_port = atol(dtls_opt->value);
183 } else if (!strcmp(dtls_opt->option, "X-DTLS-Keepalive")) {
184 vpninfo->dtls_keepalive = atol(dtls_opt->value);
187 dtls_opt = dtls_opt->next;
189 if (!sessid_found || !dtls_port)
192 if (connect_dtls_socket(vpninfo, dtls_port))
195 /* No idea how to do this yet */
196 close(vpninfo->dtls_fd);
197 vpninfo->dtls_fd = -1;
201 int dtls_mainloop(struct anyconnect_info *vpninfo, int *timeout)