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 <sys/types.h>
37 #include <openssl/ssl.h>
38 #include <openssl/err.h>
39 #include <openssl/engine.h>
41 #include "openconnect.h"
43 #define MAX_BUF_LEN 131072
45 * We didn't really want to have to do this for ourselves -- one might have
46 * thought that it would be available in a library somewhere. But neither
47 * cURL nor Neon have reliable cross-platform ways of either using a cert
48 * from the TPM, or just reading from / writing to a transport which is
49 * provided by their caller.
52 int http_add_cookie(struct openconnect_info *vpninfo, const char *option, const char *value)
54 struct vpn_option *new, **this;
57 new = malloc(sizeof(*new));
59 vpninfo->progress(vpninfo, PRG_ERR, "No memory for allocating cookies\n");
63 new->option = strdup(option);
64 new->value = strdup(value);
65 if (!new->option || !new->value) {
72 /* Kill cookie; don't replace it */
75 for (this = &vpninfo->cookies; *this; this = &(*this)->next) {
76 if (!strcmp(option, (*this)->option)) {
77 /* Replace existing cookie */
79 new->next = (*this)->next;
83 free((*this)->option);
97 static int process_http_response(struct openconnect_info *vpninfo, int *result,
98 int (*header_cb)(struct openconnect_info *, char *, char *),
99 char *body, int buf_len)
101 char buf[MAX_BUF_LEN];
104 int http10 = 0, closeconn = 0;
107 if (openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)) < 0) {
108 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching HTTPS response\n");
113 if (!strncmp(buf, "HTTP/1.0 ", 9)) {
118 if ((!http10 && strncmp(buf, "HTTP/1.1 ", 9)) || !(*result = atoi(buf+9))) {
119 vpninfo->progress(vpninfo, PRG_ERR, "Failed to parse HTTP response '%s'\n", buf);
123 vpninfo->progress(vpninfo, PRG_TRACE, "Got HTTP response: %s\n", buf);
126 while ((i = openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)))) {
129 vpninfo->progress(vpninfo, PRG_TRACE, "%s\n", buf);
132 vpninfo->progress(vpninfo, PRG_ERR, "Error processing HTTP response\n");
135 colon = strchr(buf, ':');
137 vpninfo->progress(vpninfo, PRG_ERR, "Ignoring unknown HTTP response line '%s'\n", buf);
144 if (!strcmp(buf, "Connection") && !strcmp(colon, "Close"))
147 if (!strcmp(buf, "Location")) {
148 vpninfo->redirect_url = strdup(colon);
149 if (!vpninfo->redirect_url)
152 if (!strcmp(buf, "Content-Length")) {
153 bodylen = atoi(colon);
154 if (bodylen < 0 || bodylen > buf_len) {
155 vpninfo->progress(vpninfo, PRG_ERR, "Response body too large for buffer (%d > %d)\n",
160 if (!strcmp(buf, "Set-Cookie")) {
161 char *semicolon = strchr(colon, ';');
162 char *equals = strchr(colon, '=');
169 vpninfo->progress(vpninfo, PRG_ERR, "Invalid cookie offered: %s\n", buf);
174 ret = http_add_cookie(vpninfo, colon, equals);
178 if (!strcmp(buf, "Transfer-Encoding")) {
179 if (!strcmp(colon, "chunked"))
182 vpninfo->progress(vpninfo, PRG_ERR, "Unknown Transfer-Encoding: %s\n", colon);
186 if (header_cb && !strncmp(buf, "X-", 2))
187 header_cb(vpninfo, buf, colon);
190 /* Handle 'HTTP/1.1 100 Continue'. Not that we should ever see it */
194 /* Now the body, if there is one */
199 /* HTTP 1.0 response. Just eat all we can. */
201 i = SSL_read(vpninfo->https_ssl, body + done, bodylen - done);
207 /* If we were given Content-Length, it's nice and easy... */
209 while (done < bodylen) {
210 i = SSL_read(vpninfo->https_ssl, body + done, bodylen - done);
212 vpninfo->progress(vpninfo, PRG_ERR, "Error reading HTTP response body\n");
220 /* ... else, chunked */
221 while ((i = openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)))) {
222 int chunklen, lastchunk = 0;
225 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching chunk header\n");
228 chunklen = strtol(buf, NULL, 16);
233 if (chunklen + done > buf_len) {
234 vpninfo->progress(vpninfo, PRG_ERR, "Response body too large for buffer (%d > %d)\n",
235 chunklen + done, buf_len);
239 i = SSL_read(vpninfo->https_ssl, body + done, chunklen);
241 vpninfo->progress(vpninfo, PRG_ERR, "Error reading HTTP response body\n");
248 if ((i = openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)))) {
250 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching HTTP response body\n");
252 vpninfo->progress(vpninfo, PRG_ERR, "Error in chunked decoding. Expected '', got: '%s'",
263 SSL_free(vpninfo->https_ssl);
264 vpninfo->https_ssl = NULL;
265 close(vpninfo->ssl_fd);
266 vpninfo->ssl_fd = -1;
272 static int fetch_config(struct openconnect_info *vpninfo, char *fu, char *bu,
275 struct vpn_option *opt;
276 char buf[MAX_BUF_LEN];
278 unsigned char local_sha1_bin[SHA_DIGEST_LENGTH];
279 char local_sha1_ascii[(SHA_DIGEST_LENGTH * 2)+1];
283 sprintf(buf, "GET %s%s HTTP/1.1\r\n", fu, bu);
284 sprintf(buf + strlen(buf), "Host: %s\r\n", vpninfo->hostname);
285 sprintf(buf + strlen(buf), "User-Agent: %s\r\n", vpninfo->useragent);
286 sprintf(buf + strlen(buf), "Accept: */*\r\n");
287 sprintf(buf + strlen(buf), "Accept-Encoding: identity\r\n");
289 if (vpninfo->cookies) {
290 sprintf(buf + strlen(buf), "Cookie: ");
291 for (opt = vpninfo->cookies; opt; opt = opt->next)
292 sprintf(buf + strlen(buf), "%s=%s%s", opt->option,
293 opt->value, opt->next ? "; " : "\r\n");
295 sprintf(buf + strlen(buf), "X-Transcend-Version: 1\r\n\r\n");
297 SSL_write(vpninfo->https_ssl, buf, strlen(buf));
299 buflen = process_http_response(vpninfo, &result, NULL, buf, MAX_BUF_LEN);
301 /* We'll already have complained about whatever offended us */
310 EVP_Digest(buf, buflen, local_sha1_bin, NULL, EVP_sha1(), NULL);
311 EVP_MD_CTX_cleanup(&c);
313 for (i = 0; i < SHA_DIGEST_LENGTH; i++)
314 sprintf(&local_sha1_ascii[i*2], "%02x", local_sha1_bin[i]);
316 if (strcasecmp(server_sha1, local_sha1_ascii)) {
317 vpninfo->progress(vpninfo, PRG_ERR, "Downloaded config file did not match intended SHA1\n");
321 return vpninfo->write_new_config(vpninfo, buf, buflen);
324 static int run_csd_script(struct openconnect_info *vpninfo, char *buf, int buflen)
329 if (!vpninfo->uid_csd_given) {
330 vpninfo->progress(vpninfo, PRG_ERR, "Error: You are trying to "
331 "run insecure CSD code without specifying the CSD user.\n"
332 " Use command line option \"--csd-user\"\n");
337 vpninfo->progress(vpninfo, PRG_INFO,
338 "Trying to run Linux CSD trojan script.");
341 sprintf(fname, "/tmp/csdXXXXXX");
345 vpninfo->progress(vpninfo, PRG_ERR, "Failed to open temporary CSD script file: %s\n",
349 write(fd, buf, buflen);
354 X509 *scert = SSL_get_peer_certificate(vpninfo->https_ssl);
355 X509 *ccert = SSL_get_certificate(vpninfo->https_ssl);
356 char scertbuf[EVP_MAX_MD_SIZE * 2 + 1];
357 char ccertbuf[EVP_MAX_MD_SIZE * 2 + 1];
361 if (vpninfo->uid_csd != getuid()) {
364 if (setuid(vpninfo->uid_csd)) {
365 fprintf(stderr, "Failed to set uid %d\n",
369 if (!(pw = getpwuid(vpninfo->uid_csd))) {
370 fprintf(stderr, "Invalid user uid=%d\n",
374 setenv("HOME", pw->pw_dir, 1);
377 if (vpninfo->uid_csd == 0) {
378 fprintf(stderr, "Warning: you are running insecure "
379 "CSD code with root privileges\n"
380 "\t Use command line option \"--csd-user\"\n");
383 csd_argv[i++] = fname;
384 csd_argv[i++] = "-ticket";
385 asprintf(&csd_argv[i++], "\"%s\"", vpninfo->csd_ticket);
386 csd_argv[i++] = "-stub";
387 csd_argv[i++] = "\"0\"";
388 csd_argv[i++] = "-group";
389 asprintf(&csd_argv[i++], "\"%s\"", vpninfo->authgroup?:"");
390 get_cert_md5_fingerprint(vpninfo, scert, scertbuf);
392 get_cert_md5_fingerprint(vpninfo, ccert, ccertbuf);
396 csd_argv[i++] = "-certhash";
397 asprintf(&csd_argv[i++], "\"%s:%s\"", scertbuf, ccertbuf);
398 csd_argv[i++] = "-url";
399 asprintf(&csd_argv[i++], "\"https://%s%s\"", vpninfo->hostname, vpninfo->csd_starturl);
400 /* WTF would it want to know this for? */
401 csd_argv[i++] = "-vpnclient";
402 csd_argv[i++] = "\"/opt/cisco/vpn/bin/vpnui";
403 csd_argv[i++] = "-connect";
404 asprintf(&csd_argv[i++], "https://%s/%s", vpninfo->hostname, vpninfo->csd_preurl);
405 csd_argv[i++] = "-connectparam";
406 asprintf(&csd_argv[i++], "#csdtoken=%s\"", vpninfo->csd_token);
407 csd_argv[i++] = "-langselen";
408 csd_argv[i++] = NULL;
410 execv(fname, csd_argv);
411 vpninfo->progress(vpninfo, PRG_ERR, "Failed to exec CSD script %s\n", fname);
415 free(vpninfo->csd_stuburl);
416 vpninfo->csd_stuburl = NULL;
417 vpninfo->urlpath = strdup(vpninfo->csd_waiturl +
418 (vpninfo->csd_waiturl[0] == '/' ? 1 : 0));
419 vpninfo->csd_waiturl = NULL;
420 vpninfo->csd_scriptname = strdup(fname);
422 http_add_cookie(vpninfo, "sdesktop", vpninfo->csd_token);
428 char *local_strcasestr(const char *haystack, const char *needle)
430 int hlen = strlen(haystack);
431 int nlen = strlen(needle);
434 for (i = 0; i < hlen - nlen + 1; i++) {
435 for (j = 0; j < nlen; j++) {
436 if (tolower(haystack[i + j]) !=
441 return (char *)haystack + i;
445 #define strcasestr local_strcasestr
450 * = 0, no cookie (user cancel)
451 * = 1, obtained cookie
453 int openconnect_obtain_cookie(struct openconnect_info *vpninfo)
455 struct vpn_option *opt, *next;
456 char buf[MAX_BUF_LEN];
458 char request_body[2048];
459 char *request_body_type = NULL;
460 char *method = "GET";
463 if (!vpninfo->https_ssl && openconnect_open_https(vpninfo)) {
464 vpninfo->progress(vpninfo, PRG_ERR, "Failed to open HTTPS connection to %s\n",
470 * It would be nice to use cURL for this, but we really need to guarantee
471 * that we'll be using OpenSSL (for the TPM stuff), and it doesn't seem
472 * to have any way to let us provide our own socket read/write functions.
473 * We can only provide a socket _open_ function. Which would require having
474 * a socketpair() and servicing the "other" end of it.
476 * So we process the HTTP for ourselves...
478 sprintf(buf, "%s /%s HTTP/1.1\r\n", method, vpninfo->urlpath ?: "");
479 sprintf(buf + strlen(buf), "Host: %s\r\n", vpninfo->hostname);
480 sprintf(buf + strlen(buf), "User-Agent: %s\r\n", vpninfo->useragent);
481 sprintf(buf + strlen(buf), "Accept: */*\r\n");
482 sprintf(buf + strlen(buf), "Accept-Encoding: identity\r\n");
484 if (vpninfo->cookies) {
485 sprintf(buf + strlen(buf), "Cookie: ");
486 for (opt = vpninfo->cookies; opt; opt = opt->next)
487 sprintf(buf + strlen(buf), "%s=%s%s", opt->option,
488 opt->value, opt->next ? "; " : "\r\n");
490 if (request_body_type) {
491 sprintf(buf + strlen(buf), "Content-Type: %s\r\n",
493 sprintf(buf + strlen(buf), "Content-Length: %zd\r\n",
494 strlen(request_body));
496 sprintf(buf + strlen(buf), "X-Transcend-Version: 1\r\n\r\n");
497 if (request_body_type)
498 sprintf(buf + strlen(buf), "%s", request_body);
500 if (vpninfo->port == 443)
501 vpninfo->progress(vpninfo, PRG_INFO, "%s https://%s/%s\n",
502 method, vpninfo->hostname,
503 vpninfo->urlpath ?: "");
505 vpninfo->progress(vpninfo, PRG_INFO, "%s https://%s:%d/%s\n",
506 method, vpninfo->hostname, vpninfo->port,
507 vpninfo->urlpath ?: "");
509 SSL_write(vpninfo->https_ssl, buf, strlen(buf));
511 buflen = process_http_response(vpninfo, &result, NULL, buf, MAX_BUF_LEN);
513 /* We'll already have complained about whatever offended us */
517 if (result != 200 && vpninfo->redirect_url) {
519 if (!strncmp(vpninfo->redirect_url, "https://", 8)) {
520 /* New host. Tear down the existing connection and make a new one */
521 char *host = vpninfo->redirect_url + 8;
522 char *path = strchr(host, '/');
526 free(vpninfo->urlpath);
529 vpninfo->urlpath = strdup(path);
531 vpninfo->urlpath = NULL;
533 port_str = strrchr(host, ':');
537 port = strtol(port_str + 1, &end, 10);
543 /* Check for IPv6 literal (RFC2732) */
544 if (host[0] == '[' && host[strlen(host)-1] == ']') {
545 host[strlen(host)-1] = 0;
548 if (strcmp(vpninfo->hostname, host) ||
549 port != vpninfo->port) {
550 free(vpninfo->hostname);
551 vpninfo->hostname = strdup(host);
552 vpninfo->port = port;
554 /* Kill the existing connection, and a new one will happen */
555 free(vpninfo->peer_addr);
556 vpninfo->peer_addr = NULL;
557 SSL_free(vpninfo->https_ssl);
558 vpninfo->https_ssl = NULL;
559 close(vpninfo->ssl_fd);
560 vpninfo->ssl_fd = -1;
562 for (opt = vpninfo->cookies; opt; opt = next) {
569 vpninfo->cookies = NULL;
571 free(vpninfo->redirect_url);
572 vpninfo->redirect_url = NULL;
575 } else if (vpninfo->redirect_url[0] == '/') {
576 /* Absolute redirect within same host */
577 free(vpninfo->urlpath);
578 vpninfo->urlpath = strdup(vpninfo->redirect_url + 1);
579 free(vpninfo->redirect_url);
580 vpninfo->redirect_url = NULL;
583 vpninfo->progress(vpninfo, PRG_ERR, "Relative redirect (to '%s') not supported\n",
584 vpninfo->redirect_url);
589 if (vpninfo->csd_stuburl) {
590 /* This is the CSD stub script, which we now need to run */
591 result = run_csd_script(vpninfo, buf, buflen);
595 /* Now we'll be redirected to the waiturl */
598 if (strncmp(buf, "<?xml", 5)) {
599 /* Not XML? Perhaps it's HTML with a refresh... */
600 if (strcasestr(buf, "http-equiv=\"refresh\"")) {
601 vpninfo->progress(vpninfo, PRG_INFO, "Refreshing %s after 1 second...\n",
606 vpninfo->progress(vpninfo, PRG_ERR, "Unknown response from server\n");
610 result = parse_xml_response(vpninfo, buf, request_body, sizeof(request_body),
611 &method, &request_body_type);
617 /* A return value of 2 means the XML form indicated
618 success. We _should_ have a cookie... */
620 for (opt = vpninfo->cookies; opt; opt = opt->next) {
622 if (!strcmp(opt->option, "webvpn"))
623 vpninfo->cookie = opt->value;
624 else if (vpninfo->write_new_config && !strcmp(opt->option, "webvpnc")) {
625 char *tok = opt->value;
626 char *bu = NULL, *fu = NULL, *sha = NULL;
629 if (tok != opt->value)
632 if (!strncmp(tok, "bu:", 3))
634 else if (!strncmp(tok, "fu:", 3))
636 else if (!strncmp(tok, "fh:", 3)) {
637 if (!strncasecmp(tok+3, vpninfo->xmlsha1,
638 SHA_DIGEST_LENGTH * 2))
642 } while ((tok = strchr(tok, '&')));
645 fetch_config(vpninfo, bu, fu, sha);
648 if (vpninfo->csd_scriptname) {
649 unlink(vpninfo->csd_scriptname);
650 free(vpninfo->csd_scriptname);
651 vpninfo->csd_scriptname = NULL;
656 char *openconnect_create_useragent(char *base)
660 if (asprintf(&uagent, "%s %s", base, openconnect_version) < 0)