2 * OpenConnect (SSL + DTLS) VPN client
4 * Copyright © 2008 Intel Corporation.
5 * Copyright © 2008 Nick Andrew <nick@nick-andrew.net>
7 * Author: David Woodhouse <dwmw2@infradead.org>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * version 2.1, as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to:
21 * Free Software Foundation, Inc.
22 * 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301 USA
35 #include <openssl/ssl.h>
36 #include <openssl/err.h>
37 #include <openssl/engine.h>
39 #include "openconnect.h"
41 #define MAX_BUF_LEN 131072
43 * We didn't really want to have to do this for ourselves -- one might have
44 * thought that it would be available in a library somewhere. But neither
45 * cURL nor Neon have reliable cross-platform ways of either using a cert
46 * from the TPM, or just reading from / writing to a transport which is
47 * provided by their caller.
50 static int process_http_response(struct openconnect_info *vpninfo, int *result,
51 int (*header_cb)(struct openconnect_info *, char *, char *),
52 char *body, int buf_len)
54 char buf[MAX_BUF_LEN];
57 int http10 = 0, closeconn = 0;
60 if (openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)) < 0) {
61 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching HTTPS response\n");
66 if (!strncmp(buf, "HTTP/1.0 ", 9)) {
71 if ((!http10 && strncmp(buf, "HTTP/1.1 ", 9)) || !(*result = atoi(buf+9))) {
72 vpninfo->progress(vpninfo, PRG_ERR, "Failed to parse HTTP response '%s'\n", buf);
76 vpninfo->progress(vpninfo, PRG_TRACE, "Got HTTP response: %s\n", buf);
79 while ((i = openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)))) {
82 vpninfo->progress(vpninfo, PRG_TRACE, "%s\n", buf);
85 vpninfo->progress(vpninfo, PRG_ERR, "Error processing HTTP response\n");
88 colon = strchr(buf, ':');
90 vpninfo->progress(vpninfo, PRG_ERR, "Ignoring unknown HTTP response line '%s'\n", buf);
97 if (!strcmp(buf, "Connection") && !strcmp(colon, "Close"))
100 if (!strcmp(buf, "Location")) {
101 vpninfo->redirect_url = strdup(colon);
102 if (!vpninfo->redirect_url)
105 if (!strcmp(buf, "Content-Length")) {
106 bodylen = atoi(colon);
107 if (bodylen < 0 || bodylen > buf_len) {
108 vpninfo->progress(vpninfo, PRG_ERR, "Response body too large for buffer (%d > %d)\n",
113 if (!strcmp(buf, "Set-Cookie")) {
114 struct vpn_option *new, **this;
115 char *semicolon = strchr(colon, ';');
116 char *equals = strchr(colon, '=');
122 vpninfo->progress(vpninfo, PRG_ERR, "Invalid cookie offered: %s\n", buf);
128 new = malloc(sizeof(*new));
130 vpninfo->progress(vpninfo, PRG_ERR, "No memory for allocating cookies\n");
134 new->option = strdup(colon);
135 new->value = strdup(equals);
137 /* Kill cookie; don't replace it */
140 for (this = &vpninfo->cookies; *this; this = &(*this)->next) {
141 if (!strcmp(colon, (*this)->option)) {
142 /* Replace existing cookie */
144 new->next = (*this)->next;
148 free((*this)->option);
149 free((*this)->value);
160 if (!strcmp(buf, "Transfer-Encoding")) {
161 if (!strcmp(colon, "chunked"))
164 vpninfo->progress(vpninfo, PRG_ERR, "Unknown Transfer-Encoding: %s\n", colon);
168 if (header_cb && !strncmp(buf, "X-", 2))
169 header_cb(vpninfo, buf, colon);
172 /* Handle 'HTTP/1.1 100 Continue'. Not that we should ever see it */
176 /* Now the body, if there is one */
181 /* HTTP 1.0 response. Just eat all we can. */
183 i = SSL_read(vpninfo->https_ssl, body + done, bodylen - done);
189 /* If we were given Content-Length, it's nice and easy... */
191 while (done < bodylen) {
192 i = SSL_read(vpninfo->https_ssl, body + done, bodylen - done);
194 vpninfo->progress(vpninfo, PRG_ERR, "Error reading HTTP response body\n");
202 /* ... else, chunked */
203 while ((i = openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)))) {
204 int chunklen, lastchunk = 0;
207 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching chunk header\n");
210 chunklen = strtol(buf, NULL, 16);
215 if (chunklen + done > buf_len) {
216 vpninfo->progress(vpninfo, PRG_ERR, "Response body too large for buffer (%d > %d)\n",
217 chunklen + done, buf_len);
221 i = SSL_read(vpninfo->https_ssl, body + done, chunklen);
223 vpninfo->progress(vpninfo, PRG_ERR, "Error reading HTTP response body\n");
230 if ((i = openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)))) {
232 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching HTTP response body\n");
234 vpninfo->progress(vpninfo, PRG_ERR, "Error in chunked decoding. Expected '', got: '%s'",
245 SSL_free(vpninfo->https_ssl);
246 vpninfo->https_ssl = NULL;
247 close(vpninfo->ssl_fd);
248 vpninfo->ssl_fd = -1;
254 static int fetch_config(struct openconnect_info *vpninfo, char *fu, char *bu,
257 struct vpn_option *opt;
258 char buf[MAX_BUF_LEN];
260 unsigned char local_sha1_bin[SHA_DIGEST_LENGTH];
261 char local_sha1_ascii[(SHA_DIGEST_LENGTH * 2)+1];
265 sprintf(buf, "GET %s%s HTTP/1.1\r\n", fu, bu);
266 sprintf(buf + strlen(buf), "Host: %s\r\n", vpninfo->hostname);
267 sprintf(buf + strlen(buf), "User-Agent: %s\r\n", vpninfo->useragent);
268 sprintf(buf + strlen(buf), "Accept: */*\r\n");
269 sprintf(buf + strlen(buf), "Accept-Encoding: identity\r\n");
271 if (vpninfo->cookies) {
272 sprintf(buf + strlen(buf), "Cookie: ");
273 for (opt = vpninfo->cookies; opt; opt = opt->next)
274 sprintf(buf + strlen(buf), "%s=%s%s", opt->option,
275 opt->value, opt->next ? "; " : "\r\n");
277 sprintf(buf + strlen(buf), "X-Transcend-Version: 1\r\n\r\n");
279 SSL_write(vpninfo->https_ssl, buf, strlen(buf));
281 buflen = process_http_response(vpninfo, &result, NULL, buf, MAX_BUF_LEN);
283 /* We'll already have complained about whatever offended us */
292 EVP_Digest(buf, buflen, local_sha1_bin, NULL, EVP_sha1(), NULL);
293 EVP_MD_CTX_cleanup(&c);
295 for (i = 0; i < SHA_DIGEST_LENGTH; i++)
296 sprintf(&local_sha1_ascii[i*2], "%02x", local_sha1_bin[i]);
298 if (strcasecmp(server_sha1, local_sha1_ascii)) {
299 vpninfo->progress(vpninfo, PRG_ERR, "Downloaded config file did not match intended SHA1\n");
303 return vpninfo->write_new_config(vpninfo, buf, buflen);
306 static int run_csd_script(struct openconnect_info *vpninfo, char *buf, int buflen)
311 sprintf(fname, "/tmp/csdXXXXXX");
315 vpninfo->progress(vpninfo, PRG_ERR, "Failed to open temporary CSD script file: %s\n",
319 write(fd, buf, buflen);
324 X509 *cert = SSL_get_peer_certificate(vpninfo->https_ssl);
325 char certbuf[EVP_MAX_MD_SIZE * 2 + 1];
329 csd_argv[i++] = fname;
330 csd_argv[i++] = "-ticket";
331 asprintf(&csd_argv[i++], "\"%s\"", vpninfo->csd_ticket);
332 csd_argv[i++] = "-stub";
333 csd_argv[i++] = "\"0\"";
334 csd_argv[i++] = "-group";
335 asprintf(&csd_argv[i++], "\"%s\"", vpninfo->authgroup?:"");
336 get_cert_md5_fingerprint(vpninfo, cert, certbuf);
337 csd_argv[i++] = "-certhash";
338 asprintf(&csd_argv[i++], "\"%s:%s\"", certbuf, vpninfo->cert_md5_fingerprint ?: "");
339 csd_argv[i++] = "-url";
340 asprintf(&csd_argv[i++], "\"https://%s%s\"", vpninfo->hostname, vpninfo->csd_starturl);
341 /* WTF would it want to know this for? */
342 csd_argv[i++] = "-vpnclient";
343 csd_argv[i++] = "\"/opt/cisco/vpn/bin/vpnui";
344 csd_argv[i++] = "-connect";
345 asprintf(&csd_argv[i++], "https://%s/%s", vpninfo->hostname, vpninfo->csd_preurl);
346 csd_argv[i++] = "-connectparam";
347 asprintf(&csd_argv[i++], "#csdtoken=%s\"", vpninfo->csd_token);
348 csd_argv[i++] = "-langselen";
349 csd_argv[i++] = NULL;
351 execv(fname, csd_argv);
352 vpninfo->progress(vpninfo, PRG_ERR, "Failed to exec CSD script %s\n", fname);
356 free(vpninfo->csd_stuburl);
357 vpninfo->csd_stuburl = NULL;
358 vpninfo->urlpath = strdup(vpninfo->csd_waiturl +
359 (vpninfo->csd_waiturl[0] == '/' ? 1 : 0));
360 vpninfo->csd_waiturl = NULL;
361 vpninfo->csd_scriptname = strdup(fname);
363 /* AB: add cookie "sdesktop=__TOKEN__" */
365 struct vpn_option *new, **this;
366 new = malloc(sizeof(*new));
368 vpninfo->progress(vpninfo, PRG_ERR, "No memory for allocating cookies\n");
372 new->option = strdup("sdesktop");
373 new->value = strdup(vpninfo->csd_token);
375 for (this = &vpninfo->cookies; *this; this = &(*this)->next) {
376 if (!strcmp(new->option, (*this)->option)) {
377 /* Replace existing cookie */
379 new->next = (*this)->next;
383 free((*this)->option);
384 free((*this)->value);
400 * = 0, no cookie (user cancel)
401 * = 1, obtained cookie
403 int openconnect_obtain_cookie(struct openconnect_info *vpninfo)
405 struct vpn_option *opt, *next;
406 char buf[MAX_BUF_LEN];
408 char request_body[2048];
409 char *request_body_type = NULL;
410 char *method = "GET";
413 if (!vpninfo->https_ssl && openconnect_open_https(vpninfo)) {
414 vpninfo->progress(vpninfo, PRG_ERR, "Failed to open HTTPS connection to %s\n",
420 * It would be nice to use cURL for this, but we really need to guarantee
421 * that we'll be using OpenSSL (for the TPM stuff), and it doesn't seem
422 * to have any way to let us provide our own socket read/write functions.
423 * We can only provide a socket _open_ function. Which would require having
424 * a socketpair() and servicing the "other" end of it.
426 * So we process the HTTP for ourselves...
428 sprintf(buf, "%s /%s HTTP/1.1\r\n", method, vpninfo->urlpath ?: "");
429 sprintf(buf + strlen(buf), "Host: %s\r\n", vpninfo->hostname);
430 sprintf(buf + strlen(buf), "User-Agent: %s\r\n", vpninfo->useragent);
431 sprintf(buf + strlen(buf), "Accept: */*\r\n");
432 sprintf(buf + strlen(buf), "Accept-Encoding: identity\r\n");
434 if (vpninfo->cookies) {
435 sprintf(buf + strlen(buf), "Cookie: ");
436 for (opt = vpninfo->cookies; opt; opt = opt->next)
437 sprintf(buf + strlen(buf), "%s=%s%s", opt->option,
438 opt->value, opt->next ? "; " : "\r\n");
440 if (request_body_type) {
441 sprintf(buf + strlen(buf), "Content-Type: %s\r\n",
443 sprintf(buf + strlen(buf), "Content-Length: %zd\r\n",
444 strlen(request_body));
446 sprintf(buf + strlen(buf), "X-Transcend-Version: 1\r\n\r\n");
447 if (request_body_type)
448 sprintf(buf + strlen(buf), "%s", request_body);
450 vpninfo->progress(vpninfo, PRG_INFO, "%s %s/%s\n", method,
451 vpninfo->hostname, vpninfo->urlpath ?: "");
453 SSL_write(vpninfo->https_ssl, buf, strlen(buf));
455 buflen = process_http_response(vpninfo, &result, NULL, buf, MAX_BUF_LEN);
457 /* We'll already have complained about whatever offended us */
461 if (result != 200 && vpninfo->redirect_url) {
463 if (!strncmp(vpninfo->redirect_url, "https://", 8)) {
464 /* New host. Tear down the existing connection and make a new one */
465 char *host = vpninfo->redirect_url + 8;
466 char *path = strchr(host, '/');
468 free(vpninfo->urlpath);
471 vpninfo->urlpath = strdup(path);
473 vpninfo->urlpath = NULL;
475 if (strcmp(vpninfo->hostname, host)) {
476 free(vpninfo->hostname);
477 vpninfo->hostname = strdup(host);
479 /* Kill the existing connection, and a new one will happen */
480 SSL_free(vpninfo->https_ssl);
481 vpninfo->https_ssl = NULL;
482 close(vpninfo->ssl_fd);
483 vpninfo->ssl_fd = -1;
485 for (opt = vpninfo->cookies; opt; opt = next) {
492 vpninfo->cookies = NULL;
494 free(vpninfo->redirect_url);
495 vpninfo->redirect_url = NULL;
498 } else if (vpninfo->redirect_url[0] == '/') {
499 /* Absolute redirect within same host */
500 free(vpninfo->urlpath);
501 vpninfo->urlpath = strdup(vpninfo->redirect_url + 1);
502 free(vpninfo->redirect_url);
503 vpninfo->redirect_url = NULL;
506 vpninfo->progress(vpninfo, PRG_ERR, "Relative redirect (to '%s') not supported\n",
507 vpninfo->redirect_url);
512 if (vpninfo->csd_stuburl) {
513 /* This is the CSD stub script, which we now need to run */
514 result = run_csd_script(vpninfo, buf, buflen);
518 /* Now we'll be redirected to the waiturl */
521 if (strncmp(buf, "<?xml", 5)) {
522 /* Not XML? Perhaps it's HTML with a refresh... */
523 if (strcasestr(buf, "http-equiv=\"refresh\"")) {
524 vpninfo->progress(vpninfo, PRG_INFO, "Refreshing %s after 1 second...\n",
529 vpninfo->progress(vpninfo, PRG_ERR, "Unknown response from server\n");
533 result = parse_xml_response(vpninfo, buf, request_body, sizeof(request_body),
534 &method, &request_body_type);
540 /* A return value of 2 means the XML form indicated
541 success. We _should_ have a cookie... */
543 for (opt = vpninfo->cookies; opt; opt = opt->next) {
545 if (!strcmp(opt->option, "webvpn"))
546 vpninfo->cookie = opt->value;
547 else if (vpninfo->write_new_config && !strcmp(opt->option, "webvpnc")) {
548 char *tok = opt->value;
549 char *bu = NULL, *fu = NULL, *sha = NULL;
552 if (tok != opt->value)
555 if (!strncmp(tok, "bu:", 3))
557 else if (!strncmp(tok, "fu:", 3))
559 else if (!strncmp(tok, "fh:", 3)) {
560 if (!strncasecmp(tok+3, vpninfo->xmlsha1,
561 SHA_DIGEST_LENGTH * 2))
565 } while ((tok = strchr(tok, '&')));
568 fetch_config(vpninfo, bu, fu, sha);
571 if (vpninfo->csd_scriptname) {
572 unlink(vpninfo->csd_scriptname);
573 free(vpninfo->csd_scriptname);
574 vpninfo->csd_scriptname = NULL;
579 char *openconnect_create_useragent(char *base)
581 char *uagent = malloc(strlen(base) + 1 + strlen(openconnect_version));
582 sprintf(uagent, "%s%s", base, openconnect_version);