fix typo
[platform/upstream/openconnect.git] / dtls.c
1 /*
2  * Open AnyConnect (SSL + DTLS) client
3  *
4  * © 2008 David Woodhouse <dwmw2@infradead.org>
5  *
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
9  * in all copies.
10  *
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.
19  */
20
21 #include <errno.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netdb.h>
25 #include <unistd.h>
26 #include <openssl/err.h>
27
28 #include "anyconnect.h"
29
30 /*
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.
35  *
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.
39  *
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.
43  *
44  * Using OpenSSL-0.9.8e, which was the last release of OpenSSL to use
45  * DTLS1_BAD_VER, also fails similarly.
46  *
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"
49  * changes on top.
50  */   
51
52 static unsigned char nybble(unsigned char n)
53 {
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);
57         return 0;
58 }
59
60 static unsigned char hex(const char *data)
61 {
62         return (nybble(data[0]) << 4) | nybble(data[1]);
63 }
64
65 static int connect_dtls_socket(struct anyconnect_info *vpninfo, int dtls_port)
66 {
67         SSL_METHOD *dtls_method;
68         SSL_CTX *dtls_ctx;
69         SSL_SESSION *dtls_session;
70         SSL_CIPHER *https_cipher;
71         SSL *dtls_ssl;
72         BIO *dtls_bio;
73         int dtls_fd;
74         int ret;
75
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);
82         } else {
83                 fprintf(stderr, "Unknown protocol family %d. Cannot do DTLS\n",
84                         vpninfo->peer_addr->sa_family);
85                 return -EINVAL;
86         }
87
88         dtls_fd = socket(vpninfo->peer_addr->sa_family, SOCK_DGRAM, IPPROTO_UDP);
89         if (dtls_fd < 0) {
90                 perror("Open UDP socket for DTLS:");
91                 return -EINVAL;
92         }
93         
94         if (connect(dtls_fd, vpninfo->peer_addr, vpninfo->peer_addrlen)) {
95                 perror("UDP (DTLS) connect:\n");
96                 close(dtls_fd);
97                 return -EINVAL;
98         }
99
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);
104
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();
111
112         dtls_session->ssl_version = DTLS1_BAD_VER;
113
114         dtls_session->master_key_length = sizeof(vpninfo->dtls_secret);
115         memcpy(dtls_session->master_key, vpninfo->dtls_secret,
116                sizeof(vpninfo->dtls_secret));
117
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));
121
122         dtls_session->cipher = https_cipher;
123         dtls_session->cipher_id = https_cipher->id;
124
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-compatible with your libssl?\n");
132                         return -EINVAL;
133                 }
134         }
135         if (!SSL_CTX_add_session(dtls_ctx, dtls_session))
136                 printf("SSL_CTX_add_session() failed\n");
137
138
139         /* Go Go Go! */
140         dtls_bio = BIO_new_socket(dtls_fd, BIO_NOCLOSE);
141         SSL_set_bio(dtls_ssl, dtls_bio, dtls_bio);
142
143         ret = SSL_do_handshake(dtls_ssl);
144         
145         if (ret != 1) {
146                 fprintf(stderr, "DTLS connection returned %d\n", ret);
147                 if (ret < 0)
148                         fprintf(stderr, "DTLS handshake error: %d\n", SSL_get_error(dtls_ssl, ret));
149                 ERR_print_errors_fp(stderr);
150                 SSL_free(dtls_ssl);
151                 SSL_CTX_free(dtls_ctx);
152                 close(dtls_fd);
153                 return -EINVAL;
154         }
155         printf("DTLS Connection successful!\n");
156         /* FIXME: implement data transfer over it! */
157         vpninfo->dtls_fd = dtls_fd;
158         return 0;
159 }
160
161 int setup_dtls(struct anyconnect_info *vpninfo)
162 {
163         struct vpn_option *dtls_opt = vpninfo->dtls_options;
164         int sessid_found = 0;
165         int dtls_port = 0;
166         int i;
167
168         while (dtls_opt) {
169                 if (verbose)
170                         printf("DTLS option %s : %s\n", dtls_opt->option, dtls_opt->value);
171
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);
176                                 return -EINVAL;
177                         }
178                         for (i = 0; i < 64; i += 2)
179                                 vpninfo->dtls_session_id[i/2] = hex(dtls_opt->value + i);
180                         sessid_found = 1;
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);
185                 }
186                         
187                 dtls_opt = dtls_opt->next;
188         }
189         if (!sessid_found || !dtls_port)
190                 return -EINVAL;
191
192         if (connect_dtls_socket(vpninfo, dtls_port))
193                 return -EINVAL;
194
195         /* No idea how to do this yet */
196         close(vpninfo->dtls_fd);
197         vpninfo->dtls_fd = -1;
198         return -EINVAL;
199 }
200
201 int dtls_mainloop(struct anyconnect_info *vpninfo, int *timeout)
202 {
203         return 0;
204 }
205
206