check DPD config
[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 #include <fcntl.h>
28
29 #include "anyconnect.h"
30
31 /*
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.
36  *
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.
40  *
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.
44  *
45  * Using OpenSSL-0.9.8e, which was the last release of OpenSSL to use
46  * DTLS1_BAD_VER, also fails similarly.
47  *
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"
50  * changes on top.
51  */   
52
53 static unsigned char nybble(unsigned char n)
54 {
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);
58         return 0;
59 }
60
61 static unsigned char hex(const char *data)
62 {
63         return (nybble(data[0]) << 4) | nybble(data[1]);
64 }
65
66 static int connect_dtls_socket(struct anyconnect_info *vpninfo, int dtls_port)
67 {
68         SSL_METHOD *dtls_method;
69         SSL_CTX *dtls_ctx;
70         SSL_SESSION *dtls_session;
71         SSL_CIPHER *https_cipher;
72         SSL *dtls_ssl;
73         BIO *dtls_bio;
74         int dtls_fd;
75         int ret;
76
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);
83         } else {
84                 fprintf(stderr, "Unknown protocol family %d. Cannot do DTLS\n",
85                         vpninfo->peer_addr->sa_family);
86                 return -EINVAL;
87         }
88
89         dtls_fd = socket(vpninfo->peer_addr->sa_family, SOCK_DGRAM, IPPROTO_UDP);
90         if (dtls_fd < 0) {
91                 perror("Open UDP socket for DTLS:");
92                 return -EINVAL;
93         }
94         
95         if (connect(dtls_fd, vpninfo->peer_addr, vpninfo->peer_addrlen)) {
96                 perror("UDP (DTLS) connect:\n");
97                 close(dtls_fd);
98                 return -EINVAL;
99         }
100
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);
105
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();
112
113         dtls_session->ssl_version = DTLS1_BAD_VER;
114
115         dtls_session->master_key_length = sizeof(vpninfo->dtls_secret);
116         memcpy(dtls_session->master_key, vpninfo->dtls_secret,
117                sizeof(vpninfo->dtls_secret));
118
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));
122
123         dtls_session->cipher = https_cipher;
124         dtls_session->cipher_id = https_cipher->id;
125
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");
133                         return -EINVAL;
134                 }
135         }
136         if (!SSL_CTX_add_session(dtls_ctx, dtls_session))
137                 printf("SSL_CTX_add_session() failed\n");
138
139
140         /* Go Go Go! */
141         dtls_bio = BIO_new_socket(dtls_fd, BIO_NOCLOSE);
142         SSL_set_bio(dtls_ssl, dtls_bio, dtls_bio);
143
144         ret = SSL_do_handshake(dtls_ssl);
145         
146         if (ret != 1) {
147                 fprintf(stderr, "DTLS connection returned %d\n", ret);
148                 if (ret < 0)
149                         fprintf(stderr, "DTLS handshake error: %d\n", SSL_get_error(dtls_ssl, ret));
150                 ERR_print_errors_fp(stderr);
151                 SSL_free(dtls_ssl);
152                 SSL_CTX_free(dtls_ctx);
153                 close(dtls_fd);
154                 return -EINVAL;
155         }
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;
160         return 0;
161 }
162
163 int setup_dtls(struct anyconnect_info *vpninfo)
164 {
165         struct vpn_option *dtls_opt = vpninfo->dtls_options;
166         int sessid_found = 0;
167         int dtls_port = 0;
168         int i;
169
170         while (dtls_opt) {
171                 if (verbose)
172                         printf("DTLS option %s : %s\n", dtls_opt->option, dtls_opt->value);
173
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);
178                                 return -EINVAL;
179                         }
180                         for (i = 0; i < 64; i += 2)
181                                 vpninfo->dtls_session_id[i/2] = hex(dtls_opt->value + i);
182                         sessid_found = 1;
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);
189                 }
190                         
191                 dtls_opt = dtls_opt->next;
192         }
193         if (!sessid_found || !dtls_port)
194                 return -EINVAL;
195
196         if (connect_dtls_socket(vpninfo, dtls_port))
197                 return -EINVAL;
198
199         BIO_set_nbio(SSL_get_rbio(vpninfo->dtls_ssl),1);
200         BIO_set_nbio(SSL_get_wbio(vpninfo->dtls_ssl),1);
201
202         fcntl(vpninfo->dtls_fd, F_SETFL, fcntl(vpninfo->dtls_fd, F_GETFL) | O_NONBLOCK);
203
204         vpn_add_pollfd(vpninfo, vpninfo->ssl_fd, POLLIN|POLLHUP|POLLERR);
205         vpninfo->last_ssl_tx = vpninfo->last_ssl_tx = time(NULL);
206
207         return 0;
208 }
209
210 int dtls_mainloop(struct anyconnect_info *vpninfo, int *timeout)
211 {
212         char buf[2000];
213         int len;
214         int work_done = 0;
215
216         while ( (len = SSL_read(vpninfo->dtls_ssl, buf, sizeof(buf))) > 0 ) {
217                 if (verbose) {
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]);
221                 }       
222                 switch(buf[0]) {
223                 case 0:
224                         queue_new_packet(&vpninfo->incoming_queue, AF_INET, buf+1, len-1);
225                         work_done = 1;
226                         break;
227
228                 case 4: /* keepalive response */
229                         break;
230
231                 default:
232                         fprintf(stderr, "Unknown DTLS packet type %02x\n", buf[0]);
233                         break;
234                 }
235
236         }
237         while (vpninfo->outgoing_queue) {
238                 struct pkt *this = vpninfo->outgoing_queue;
239                 int ret;
240
241                 vpninfo->outgoing_queue = this->next;
242
243                 buf[0] = 0;
244                 memcpy(buf + 1, this->data, this->len);
245                 
246                 ret = SSL_write(vpninfo->dtls_ssl, buf, this->len + 1);
247                 if (verbose) {
248                         printf("Sent DTLS packet of %d bytes; SSL_write() returned %d\n",
249                                this->len, ret);
250                 }
251         }
252
253         /* FIXME: Keepalive */
254         return work_done;
255 }
256
257