mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2026-11-19 14:36:15 -05:00
feat: add connection dashboard and improve desktop authentication
Add tunnel configuration reporting, an optional CLI dashboard, manual browser authentication, native authentication controls, and broader client identity support. Improve TLS recovery, certificate validation, desktop notifications, and platform integration.
This commit is contained in:
158 files changed
+13735
-1038
No files matched your search
@@ -0,0 +1,202 @@
|
||||
/* Exercise the production parser and probe dispatch, inspecting packets at the
|
||||
* encryption boundary. No VPN server, privileged networking, or real keys. */
|
||||
#define construct_esp_packet inspect_esp_packet
|
||||
#define openconnect_open_https fixture_open_https
|
||||
#include "gpst.c"
|
||||
#undef construct_esp_packet
|
||||
#undef openconnect_open_https
|
||||
#include "gpst_nlb.c"
|
||||
#include <assert.h>
|
||||
|
||||
static const char *expected_destination;
|
||||
static int packets_inspected;
|
||||
static enum openconnect_tunnel_event observed_events[4];
|
||||
static int observed_count;
|
||||
static int ssl_response_valid;
|
||||
|
||||
int fixture_open_https(struct openconnect_info *vpninfo)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int fixture_ssl_write(struct openconnect_info *vpninfo, char *buf, size_t len)
|
||||
{
|
||||
return len;
|
||||
}
|
||||
|
||||
static int fixture_ssl_read(struct openconnect_info *vpninfo, char *buf, size_t len)
|
||||
{
|
||||
const char *response = ssl_response_valid ? "START_TUNNEL" : "invalid";
|
||||
assert(len >= strlen(response));
|
||||
memcpy(buf, response, strlen(response));
|
||||
return strlen(response);
|
||||
}
|
||||
|
||||
static void observe_transport(void *data, enum openconnect_tunnel_event event)
|
||||
{
|
||||
assert(observed_count < 4);
|
||||
observed_events[observed_count++] = event;
|
||||
}
|
||||
|
||||
static void progress(void *data, int level, const char *format, ...)
|
||||
{
|
||||
if (level > PRG_INFO)
|
||||
return;
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vfprintf(stderr, format, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
int inspect_esp_packet(struct openconnect_info *vpninfo, struct pkt *pkt, uint8_t next_header)
|
||||
{
|
||||
struct in_addr destination;
|
||||
struct ip *ip = (void *)pkt->data;
|
||||
struct icmp *icmp = (void *)(pkt->data + sizeof(*ip));
|
||||
const unsigned char *payload;
|
||||
size_t payload_len;
|
||||
|
||||
assert(inet_pton(AF_INET, expected_destination, &destination) == 1);
|
||||
assert(next_header == IPPROTO_IPIP);
|
||||
assert(ip->ip_v == 4 && ip->ip_p == IPPROTO_ICMP);
|
||||
assert(ip->ip_dst.s_addr == destination.s_addr);
|
||||
assert(ip->ip_src.s_addr == inet_addr("10.253.0.120"));
|
||||
assert(icmp->icmp_type == ICMP_ECHO);
|
||||
if (vpninfo->gp_nlb.enabled)
|
||||
payload = gpst_nlb_probe_payload(&payload_len);
|
||||
else {
|
||||
payload = (const unsigned char *)magic_ping_payload_gcm;
|
||||
payload_len = sizeof(magic_ping_payload_gcm);
|
||||
}
|
||||
assert(pkt->len == sizeof(*ip) + ICMP_MINLEN + payload_len);
|
||||
assert(!memcmp(pkt->data + sizeof(*ip) + ICMP_MINLEN, payload, payload_len));
|
||||
packets_inspected++;
|
||||
/* Send a placeholder datagram over a local socket pair. */
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
static const char key[] =
|
||||
"000000000000000000000000000000000000000000000000000000000000000000000000";
|
||||
static const char nlb_config[] =
|
||||
"<hs-key><bits>256</bits><val>"
|
||||
"0000000000000000000000000000000000000000000000000000000000000000"
|
||||
"</val></hs-key><enc-hs-key>fixture</enc-hs-key>"
|
||||
"<tunnel-opaque><val>fixture</val><valid-period>3600</valid-period></tunnel-opaque>";
|
||||
const char *gateway = "0.0.0.0", *default_gateway = "192.0.2.10";
|
||||
char xml[4096], gateway_xml[128];
|
||||
struct sockaddr_in peer = { .sin_family = AF_INET };
|
||||
struct openconnect_info *vpninfo;
|
||||
xmlDoc *doc;
|
||||
int sockets[2], nlb, invalid, length;
|
||||
|
||||
assert(argc == 2);
|
||||
nlb = !strncmp(argv[1], "nlb-", 4);
|
||||
invalid = !strcmp(argv[1], "regular-invalid") || !strcmp(argv[1], "regular-missing");
|
||||
if (!strcmp(argv[1], "regular-address") || !strcmp(argv[1], "nlb-address"))
|
||||
gateway = "192.0.2.1";
|
||||
else if (!strcmp(argv[1], "regular-invalid"))
|
||||
gateway = "invalid";
|
||||
if (!strcmp(argv[1], "nlb-client-vip"))
|
||||
default_gateway = "0.0.0.0";
|
||||
if (!strcmp(argv[1], "regular-missing"))
|
||||
gateway_xml[0] = 0;
|
||||
else
|
||||
snprintf(gateway_xml, sizeof(gateway_xml), "<gw-address>%s</gw-address>", gateway);
|
||||
length = snprintf(xml, sizeof(xml),
|
||||
"<response>%s<ip-address>10.253.0.120</ip-address>"
|
||||
"<default-gateway>%s</default-gateway><ipsec>"
|
||||
"<udp-port>4501</udp-port><enc-algo>aes-256-gcm</enc-algo><hmac-algo>sha1</hmac-algo>"
|
||||
"<c2s-spi>0x12345678</c2s-spi><s2c-spi>0x87654321</s2c-spi>"
|
||||
"<ekey-c2s><bits>288</bits><val>%s</val></ekey-c2s>"
|
||||
"<ekey-s2c><bits>288</bits><val>%s</val></ekey-s2c>"
|
||||
"</ipsec>%s</response>", gateway_xml, default_gateway, key, key, nlb ? nlb_config : "");
|
||||
assert(length > 0 && length < sizeof(xml));
|
||||
doc = xmlReadMemory(xml, length, "config.xml", NULL, XML_PARSE_NONET);
|
||||
assert(doc);
|
||||
vpninfo = openconnect_vpninfo_new("ESP gateway test", NULL, NULL, NULL, progress, NULL);
|
||||
assert(vpninfo);
|
||||
assert(!openconnect_set_protocol(vpninfo, "gp"));
|
||||
vpninfo->peer_addr = malloc(sizeof(peer));
|
||||
assert(vpninfo->peer_addr);
|
||||
memcpy(vpninfo->peer_addr, &peer, sizeof(peer));
|
||||
assert(!gpst_parse_config_xml(vpninfo, xmlDocGetRootElement(doc), NULL));
|
||||
xmlFreeDoc(doc);
|
||||
assert(!!vpninfo->gp_nlb.enabled == nlb);
|
||||
if (invalid) {
|
||||
assert(!vpninfo->esp_magic_af);
|
||||
assert(!vpninfo->delay_tunnel_reason);
|
||||
openconnect_vpninfo_free(vpninfo);
|
||||
return 0;
|
||||
}
|
||||
assert(vpninfo->esp_magic_af == AF_INET);
|
||||
assert(!memcmp(vpninfo->esp_magic, &(struct in_addr){ .s_addr = inet_addr(gateway) }, 4));
|
||||
assert(vpninfo->delay_tunnel_reason);
|
||||
assert(vpninfo->dtls_state == DTLS_SECRET);
|
||||
assert(!socketpair(AF_UNIX, SOCK_DGRAM, 0, sockets));
|
||||
vpninfo->dtls_fd = sockets[0];
|
||||
if (nlb) {
|
||||
const char *vip = gateway;
|
||||
if (!strcmp(gateway, "0.0.0.0")) {
|
||||
vip = default_gateway;
|
||||
if (!strcmp(default_gateway, "0.0.0.0"))
|
||||
vip = "10.253.0.120";
|
||||
}
|
||||
assert(vpninfo->gp_nlb.tunnel_vip);
|
||||
assert(!strcmp(vpninfo->gp_nlb.tunnel_vip, vip));
|
||||
assert(!gpst_nlb_prepare(vpninfo));
|
||||
assert(vpninfo->gp_nlb.control_state == GP_NLB_CONTROL_PENDING);
|
||||
assert(!gpst_nlb_control_ready(vpninfo));
|
||||
assert(!memcmp(vpninfo->esp_magic, &(struct in_addr){ .s_addr = inet_addr(vip) }, 4));
|
||||
/* Model completed tunnel control: keepalives must use the real inner
|
||||
* gateway, not the zero probe address or the initial virtual IP. */
|
||||
vpninfo->gp_nlb.control_state = GP_NLB_CONTROL_READY;
|
||||
vpninfo->gp_nlb.inner_gw_ip = strdup("192.0.2.20");
|
||||
expected_destination = "192.0.2.20";
|
||||
assert(!gpst_esp_send_probes(vpninfo));
|
||||
assert(packets_inspected == 3);
|
||||
assert(vpninfo->gp_nlb.keepalive_sent);
|
||||
free(vpninfo->gp_nlb.inner_gw_ip);
|
||||
vpninfo->gp_nlb.inner_gw_ip = NULL;
|
||||
expected_destination = vip;
|
||||
assert(!gpst_nlb_send_esp_keepalive(vpninfo));
|
||||
assert(packets_inspected == 4);
|
||||
} else {
|
||||
assert(!vpninfo->gp_nlb.tunnel_vip || strcmp(vpninfo->gp_nlb.tunnel_vip, "0.0.0.0"));
|
||||
expected_destination = gateway;
|
||||
assert(!gpst_esp_send_probes(vpninfo));
|
||||
assert(packets_inspected == 1);
|
||||
/* Configuration/probes alone do not advertise ESP. Exercise the actual
|
||||
* GP activation and ESP shutdown paths rather than synthesizing events. */
|
||||
int timeout = 1000;
|
||||
openconnect_set_tunnel_handler(vpninfo, observe_transport);
|
||||
assert(observed_count == 0);
|
||||
vpninfo->dtls_state = DTLS_CONNECTED;
|
||||
memset(&vpninfo->ssl_times, 0, sizeof(vpninfo->ssl_times));
|
||||
vpninfo->trojan_interval = 0;
|
||||
assert(!gpst_mainloop(vpninfo, &timeout, 0));
|
||||
assert(vpninfo->dtls_state == DTLS_ESTABLISHED);
|
||||
assert(observed_count == 1 && observed_events[0] == OC_TUNNEL_IPSEC_ESP);
|
||||
esp_close(vpninfo);
|
||||
assert(observed_count == 2 && observed_events[1] == OC_TUNNEL_UNAVAILABLE);
|
||||
/* Repeated shutdown of an inactive transport cannot claim another loss. */
|
||||
esp_close(vpninfo);
|
||||
assert(observed_count == 2);
|
||||
/* Replace only HTTPS I/O: run the actual SSL response acceptance path. */
|
||||
vpninfo->dtls_state = DTLS_DISABLED;
|
||||
vpninfo->ssl_read = fixture_ssl_read;
|
||||
vpninfo->ssl_write = fixture_ssl_write;
|
||||
openconnect_set_cookie(vpninfo, "user=test&authcookie=fixture");
|
||||
free(vpninfo->urlpath);
|
||||
vpninfo->urlpath = strdup("ssl-tunnel-connect.sslvpn");
|
||||
assert(gpst_connect(vpninfo) == -EINVAL);
|
||||
assert(observed_count == 2);
|
||||
ssl_response_valid = 1;
|
||||
assert(!gpst_connect(vpninfo));
|
||||
assert(observed_count == 3 && observed_events[2] == OC_TUNNEL_SSL_TLS);
|
||||
}
|
||||
close(sockets[1]);
|
||||
openconnect_vpninfo_free(vpninfo);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
use std::{fs, path::PathBuf, process::Command};
|
||||
|
||||
#[test]
|
||||
fn c_esp_gateway_configuration_and_probes() {
|
||||
let manifest = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
let build = PathBuf::from(env!("OPENCONNECT_PROTOCOL_BUILD"));
|
||||
let source = PathBuf::from(env!("OPENCONNECT_PROTOCOL_SOURCE"));
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let executable = directory.path().join("esp-gateway");
|
||||
let mut packages = vec!["libxml-2.0", "gnutls"];
|
||||
if fs::read_to_string(build.join("config.h"))
|
||||
.unwrap()
|
||||
.contains("#define HAVE_LZ4 ")
|
||||
{
|
||||
packages.push("liblz4");
|
||||
}
|
||||
let flags = Command::new("pkg-config")
|
||||
.arg("--cflags")
|
||||
.args(packages)
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(flags.status.success(), "{}", String::from_utf8_lossy(&flags.stderr));
|
||||
let output = Command::new(build.join("libtool"))
|
||||
.args(["--mode=link", "cc"])
|
||||
.arg(manifest.join("tests/esp_gateway.c"))
|
||||
.arg("-I")
|
||||
.arg(&build)
|
||||
.arg("-I")
|
||||
.arg(&source)
|
||||
.arg("-I")
|
||||
.arg(source.join("json"))
|
||||
.args(String::from_utf8(flags.stdout).unwrap().split_whitespace())
|
||||
.arg(build.join("libopenconnect.la"))
|
||||
.arg("-o")
|
||||
.arg(&executable)
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(output.status.success(), "{}", String::from_utf8_lossy(&output.stderr));
|
||||
for case in [
|
||||
"regular-zero",
|
||||
"regular-address",
|
||||
"regular-invalid",
|
||||
"regular-missing",
|
||||
"nlb-zero",
|
||||
"nlb-address",
|
||||
"nlb-client-vip",
|
||||
] {
|
||||
let output = Command::new(build.join("libtool"))
|
||||
.arg("--mode=execute")
|
||||
.arg(&executable)
|
||||
.arg(case)
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"{case}: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -34,11 +34,7 @@ fn c_protocol_submits_initial_periodic_and_reconnect_reports() {
|
||||
fs::create_dir(&directory.0).unwrap();
|
||||
let executable = directory.0.join("hip-protocol");
|
||||
let flags = Command::new("pkg-config")
|
||||
.args([
|
||||
"--cflags",
|
||||
"libxml-2.0",
|
||||
if cfg!(target_os = "macos") { "openssl" } else { "gnutls" },
|
||||
])
|
||||
.args(["--cflags", "libxml-2.0", "gnutls"])
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(flags.status.success());
|
||||
|
||||
@@ -0,0 +1,203 @@
|
||||
/* No privileged networking: exercise the actual configuration dispatcher. */
|
||||
#include <config.h>
|
||||
#include "openconnect-internal.h"
|
||||
#include <assert.h>
|
||||
|
||||
struct state {
|
||||
pid_t parent;
|
||||
const char *reason;
|
||||
int calls;
|
||||
int result;
|
||||
const char *failure_reason;
|
||||
const char **expected_reasons;
|
||||
int expected_count;
|
||||
int tcp_connections;
|
||||
int reconnection_notifications;
|
||||
int reconnect_started;
|
||||
};
|
||||
|
||||
static int configure(void *data, const char *reason, const struct oc_vpn_option *env)
|
||||
{
|
||||
struct state *state = data;
|
||||
const struct oc_vpn_option *option;
|
||||
int found = 0;
|
||||
assert(getpid() == state->parent);
|
||||
if (state->expected_reasons) {
|
||||
assert(state->calls < state->expected_count);
|
||||
assert(!strcmp(reason, state->expected_reasons[state->calls]));
|
||||
} else {
|
||||
assert(!strcmp(reason, state->reason));
|
||||
}
|
||||
for (option = env; option; option = option->next) {
|
||||
if (!strcmp(option->option, "GP_TEST_CONFIG")) {
|
||||
assert(!strcmp(option->value, "child value"));
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
assert(found);
|
||||
assert(!strcmp(getenv("GP_TEST_CONFIG"), "parent value"));
|
||||
state->calls++;
|
||||
return !state->failure_reason || !strcmp(reason, state->failure_reason) ? state->result : 0;
|
||||
}
|
||||
|
||||
static void progress(void *data, int level, const char *format, ...) {}
|
||||
|
||||
|
||||
/* The production library.c is compiled with os_setup_tun redirected here.
|
||||
* All setup, configuration dispatch, attachment, and cleanup code stays real. */
|
||||
static int device_fd = -1;
|
||||
static int device_creations;
|
||||
intptr_t test_os_setup_tun(struct openconnect_info *vpninfo)
|
||||
{
|
||||
device_creations++;
|
||||
assert(device_fd >= 0);
|
||||
free(vpninfo->ifname);
|
||||
vpninfo->ifname = strdup("gp-test-tun");
|
||||
assert(vpninfo->ifname);
|
||||
return device_fd;
|
||||
}
|
||||
|
||||
static struct openconnect_info *new_vpn(struct state *state)
|
||||
{
|
||||
struct openconnect_info *vpninfo = openconnect_vpninfo_new("network callback test", NULL, NULL, NULL, progress, state);
|
||||
assert(vpninfo);
|
||||
script_setenv(vpninfo, "GP_TEST_CONFIG", "child value", 0, 0);
|
||||
openconnect_set_configure_tun_handler(vpninfo, state, configure);
|
||||
return vpninfo;
|
||||
}
|
||||
|
||||
static void connect_failure_rolls_back(void)
|
||||
{
|
||||
const char *expected[] = { "pre-init", "connect", "disconnect" };
|
||||
struct state state = {
|
||||
.parent = getpid(), .result = -EPERM, .failure_reason = "connect",
|
||||
.expected_reasons = expected, .expected_count = 3,
|
||||
};
|
||||
struct openconnect_info *vpninfo = new_vpn(&state);
|
||||
int fds[2];
|
||||
assert(!pipe(fds));
|
||||
device_fd = fds[0];
|
||||
assert(openconnect_setup_tun_device(vpninfo, NULL, NULL) == -EPERM);
|
||||
assert(device_creations == 1);
|
||||
assert(state.calls == 3);
|
||||
assert(vpninfo->tun_fd == -1);
|
||||
assert(fcntl(fds[0], F_GETFD) == -1 && errno == EBADF);
|
||||
close(fds[1]);
|
||||
openconnect_vpninfo_free(vpninfo);
|
||||
/* Freeing an already rolled-back connection must not disconnect again. */
|
||||
assert(state.calls == 3);
|
||||
device_fd = -1;
|
||||
}
|
||||
|
||||
static int tcp_connect(struct openconnect_info *vpninfo)
|
||||
{
|
||||
struct state *state = vpninfo->cbdata;
|
||||
state->tcp_connections++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void reconnected(void *data)
|
||||
{
|
||||
struct state *state = data;
|
||||
state->reconnection_notifications++;
|
||||
}
|
||||
|
||||
static void tunnel_event(void *data, enum openconnect_tunnel_event event)
|
||||
{
|
||||
struct state *state = data;
|
||||
assert(event == OC_TUNNEL_RECONNECTING);
|
||||
state->reconnect_started++;
|
||||
/* Start observation precedes configuration callbacks and new TCP work. */
|
||||
assert(state->calls == 0);
|
||||
}
|
||||
|
||||
static void reconnect_refusal_propagates(void)
|
||||
{
|
||||
const char *expected[] = { "attempt-reconnect", "reconnect" };
|
||||
struct state state = {
|
||||
.parent = getpid(), .result = -EPERM, .failure_reason = "attempt-reconnect",
|
||||
.expected_reasons = expected, .expected_count = 1,
|
||||
};
|
||||
struct openconnect_info *vpninfo = new_vpn(&state);
|
||||
const struct vpn_proto *original = vpninfo->proto;
|
||||
struct vpn_proto protocol = *original;
|
||||
int fds[2];
|
||||
assert(!pipe(fds));
|
||||
vpninfo->tun_fd = fds[0];
|
||||
protocol.tcp_connect = tcp_connect;
|
||||
protocol.proto = PROTO_GPST;
|
||||
vpninfo->proto = &protocol;
|
||||
openconnect_set_reconnected_handler(vpninfo, reconnected);
|
||||
openconnect_set_tunnel_handler(vpninfo, tunnel_event);
|
||||
|
||||
/* Attempt refusal prevents even the protocol's reconnect request. */
|
||||
assert(ssl_reconnect(vpninfo) == -EPERM);
|
||||
assert(state.calls == 1 && state.tcp_connections == 0);
|
||||
assert(state.reconnection_notifications == 0);
|
||||
|
||||
/* Post-reconnect refusal prevents the successful reconnection observer. */
|
||||
state.calls = 0;
|
||||
state.expected_count = 2;
|
||||
state.failure_reason = "reconnect";
|
||||
state.result = -EACCES;
|
||||
assert(ssl_reconnect(vpninfo) == -EACCES);
|
||||
assert(state.calls == 2 && state.tcp_connections == 1);
|
||||
assert(state.reconnection_notifications == 0);
|
||||
|
||||
state.calls = 0;
|
||||
state.result = 0;
|
||||
assert(!ssl_reconnect(vpninfo));
|
||||
assert(state.calls == 2 && state.tcp_connections == 2);
|
||||
assert(state.reconnection_notifications == 1);
|
||||
assert(state.reconnect_started == 3);
|
||||
|
||||
state.expected_reasons = NULL;
|
||||
state.reason = "disconnect";
|
||||
os_shutdown_tun(vpninfo);
|
||||
assert(fcntl(fds[0], F_GETFD) == -1 && errno == EBADF);
|
||||
close(fds[1]);
|
||||
vpninfo->proto = original;
|
||||
openconnect_vpninfo_free(vpninfo);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const char *reasons[] = { "pre-init", "connect", "attempt-reconnect", "reconnect", "disconnect" };
|
||||
struct state state = { .parent = getpid() };
|
||||
struct openconnect_info *vpninfo = new_vpn(&state);
|
||||
int fds[2], i;
|
||||
assert(vpninfo);
|
||||
assert(!setenv("GP_TEST_CONFIG", "parent value", 1));
|
||||
script_setenv(vpninfo, "GP_TEST_CONFIG", "child value", 0, 0);
|
||||
openconnect_set_configure_tun_handler(vpninfo, &state, configure);
|
||||
/* A callback works without an external script and executes before fork. */
|
||||
assert(!vpninfo->vpnc_script);
|
||||
for (i = 0; i < 5; i++) {
|
||||
state.reason = reasons[i];
|
||||
assert(!script_config_tun(vpninfo, reasons[i]));
|
||||
}
|
||||
assert(state.calls == 5);
|
||||
state.result = -EPERM;
|
||||
state.reason = "pre-init";
|
||||
/* Refusal propagates before attempting to create an OS tunnel device. */
|
||||
assert(openconnect_setup_tun_device(vpninfo, NULL, NULL) == -EPERM);
|
||||
assert(vpninfo->tun_fd == -1);
|
||||
assert(device_creations == 0);
|
||||
state.result = 0;
|
||||
state.reason = "disconnect";
|
||||
/* The callback path owns the created descriptor even with no script path. */
|
||||
assert(!pipe(fds));
|
||||
vpninfo->tun_fd = fds[0];
|
||||
os_shutdown_tun(vpninfo);
|
||||
assert(vpninfo->tun_fd == -1);
|
||||
assert(fcntl(fds[0], F_GETFD) == -1 && errno == EBADF);
|
||||
close(fds[1]);
|
||||
assert(state.calls == 7);
|
||||
openconnect_set_configure_tun_handler(vpninfo, NULL, NULL);
|
||||
assert(!script_config_tun(vpninfo, "connect"));
|
||||
assert(state.calls == 7);
|
||||
openconnect_vpninfo_free(vpninfo);
|
||||
connect_failure_rolls_back();
|
||||
reconnect_refusal_propagates();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
use std::{fs, path::PathBuf, process::Command};
|
||||
|
||||
#[test]
|
||||
fn c_network_configuration_lifecycle_and_failure_paths() {
|
||||
let manifest = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
let build = PathBuf::from(env!("OPENCONNECT_PROTOCOL_BUILD"));
|
||||
let source = PathBuf::from(env!("OPENCONNECT_PROTOCOL_SOURCE"));
|
||||
let directory = std::env::temp_dir().join(format!("gp-network-configuration-{}", std::process::id()));
|
||||
fs::create_dir_all(&directory).unwrap();
|
||||
let executable = directory.join("network-configuration");
|
||||
let flags = Command::new("pkg-config")
|
||||
.args(["--cflags", "libxml-2.0", "gnutls"])
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(flags.status.success());
|
||||
let output = Command::new(build.join("libtool"))
|
||||
.args(["--mode=link", "cc"])
|
||||
.arg(manifest.join("tests/network_configuration.c"))
|
||||
// Compile the production setup implementation with only OS device creation
|
||||
// replaced, so rollback is exercised without privileges or a real TUN.
|
||||
.arg(source.join("library.c"))
|
||||
.arg("-Dos_setup_tun=test_os_setup_tun")
|
||||
.arg("-I")
|
||||
.arg(&build)
|
||||
.arg("-I")
|
||||
.arg(&source)
|
||||
.arg("-I")
|
||||
.arg(source.join("json"))
|
||||
.args(String::from_utf8(flags.stdout).unwrap().split_whitespace())
|
||||
.arg(build.join("libopenconnect.la"))
|
||||
.arg("-o")
|
||||
.arg(&executable)
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"{}: {}",
|
||||
output.status,
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
let output = Command::new(&executable).output().unwrap();
|
||||
fs::remove_dir_all(&directory).unwrap();
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"{}: {}",
|
||||
output.status,
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
/* Exercise the production OpenConnect TLS client against a local GnuTLS peer. */
|
||||
#include <config.h>
|
||||
#include "openconnect-internal.h"
|
||||
#include <gnutls/gnutls.h>
|
||||
#include <assert.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
struct peer {
|
||||
int listener, legacy, expect_rejection, handshake, renegotiation, require_identity;
|
||||
gnutls_certificate_credentials_t credentials;
|
||||
};
|
||||
struct policy { int accept, calls, unsafe_renegotiation_denied; };
|
||||
|
||||
static void progress(void *data, int level, const char *format, ...)
|
||||
{
|
||||
va_list args;
|
||||
char message[2048];
|
||||
struct policy *policy = data;
|
||||
if (level > PRG_DEBUG) return;
|
||||
va_start(args, format);
|
||||
vsnprintf(message, sizeof(message), format, args);
|
||||
va_end(args);
|
||||
fputs(message, stderr);
|
||||
if (strstr(message, gnutls_strerror(GNUTLS_E_UNSAFE_RENEGOTIATION_DENIED)))
|
||||
policy->unsafe_renegotiation_denied = 1;
|
||||
}
|
||||
|
||||
static int certificate_policy(void *data, const char *reason)
|
||||
{
|
||||
struct policy *policy = data;
|
||||
assert(reason && *reason);
|
||||
policy->calls++;
|
||||
return policy->accept ? 0 : -EINVAL;
|
||||
}
|
||||
|
||||
static void *serve(void *data)
|
||||
{
|
||||
struct peer *peer = data;
|
||||
gnutls_session_t session;
|
||||
int fd = accept(peer->listener, NULL, NULL);
|
||||
struct timeval timeout = { .tv_sec = 5 };
|
||||
assert(fd >= 0);
|
||||
assert(!setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)));
|
||||
assert(!setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)));
|
||||
assert(!gnutls_init(&session, GNUTLS_SERVER));
|
||||
assert(!gnutls_priority_set_direct(session, peer->legacy
|
||||
? "NORMAL:-VERS-ALL:+VERS-TLS1.2:%DISABLE_SAFE_RENEGOTIATION"
|
||||
: "NORMAL:-VERS-ALL:+VERS-TLS1.2", NULL));
|
||||
assert(!gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, peer->credentials));
|
||||
if (peer->require_identity)
|
||||
gnutls_certificate_server_set_request(session, GNUTLS_CERT_REQUIRE);
|
||||
gnutls_transport_set_int(session, fd);
|
||||
gnutls_handshake_set_timeout(session, 5000);
|
||||
peer->handshake = gnutls_handshake(session);
|
||||
if (!peer->expect_rejection) {
|
||||
unsigned int status, count;
|
||||
assert(!peer->handshake);
|
||||
if (peer->require_identity) {
|
||||
assert(gnutls_certificate_get_peers(session, &count) && count > 0);
|
||||
assert(!gnutls_certificate_verify_peers2(session, &status));
|
||||
assert(!status);
|
||||
}
|
||||
assert(gnutls_safe_renegotiation_status(session) == !peer->legacy);
|
||||
assert(gnutls_record_send(session, "OK", 2) == 2);
|
||||
if (peer->legacy) {
|
||||
assert(!gnutls_rehandshake(session));
|
||||
peer->renegotiation = gnutls_handshake(session);
|
||||
}
|
||||
}
|
||||
gnutls_deinit(session);
|
||||
close(fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void check(const char *cert, const char *key, const char *ca,
|
||||
int legacy, int trusted, int accept_certificate, const char *identity, const char *identity_key)
|
||||
{
|
||||
struct peer peer = { .legacy = legacy, .expect_rejection = !trusted && !accept_certificate,
|
||||
.require_identity = identity != NULL };
|
||||
struct policy policy = { .accept = accept_certificate };
|
||||
struct sockaddr_in address = { .sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK) };
|
||||
socklen_t address_length = sizeof(address);
|
||||
pthread_t thread;
|
||||
char url[128], record[16];
|
||||
int result;
|
||||
struct openconnect_info *client;
|
||||
assert(!gnutls_certificate_allocate_credentials(&peer.credentials));
|
||||
assert(!gnutls_certificate_set_x509_key_file(peer.credentials, cert, key, GNUTLS_X509_FMT_PEM));
|
||||
if (peer.require_identity)
|
||||
assert(gnutls_certificate_set_x509_trust_file(peer.credentials, ca, GNUTLS_X509_FMT_PEM) > 0);
|
||||
peer.listener = socket(AF_INET, SOCK_STREAM, 0);
|
||||
assert(peer.listener >= 0);
|
||||
assert(!bind(peer.listener, (struct sockaddr *)&address, sizeof(address)));
|
||||
assert(!getsockname(peer.listener, (struct sockaddr *)&address, &address_length));
|
||||
assert(!listen(peer.listener, 1));
|
||||
assert(!pthread_create(&thread, NULL, serve, &peer));
|
||||
client = openconnect_vpninfo_new("TLS backend regression", certificate_policy, NULL, NULL, progress, &policy);
|
||||
assert(client);
|
||||
/* localhost matches the fixture certificate; no external DNS or connection. */
|
||||
snprintf(url, sizeof(url), "https://localhost:%u/", ntohs(address.sin_port));
|
||||
assert(!openconnect_parse_url(client, url));
|
||||
if (trusted) assert(!openconnect_set_cafile(client, ca));
|
||||
if (identity) assert(!openconnect_set_client_cert(client, identity, identity_key));
|
||||
result = openconnect_open_https(client);
|
||||
if (peer.expect_rejection) {
|
||||
assert(result < 0);
|
||||
assert(policy.calls == 1);
|
||||
} else {
|
||||
assert(!result);
|
||||
assert(policy.calls == !trusted);
|
||||
assert(client->ssl_read(client, record, sizeof(record)) == 2);
|
||||
assert(!memcmp(record, "OK", 2));
|
||||
if (legacy) {
|
||||
assert(client->ssl_read(client, record, sizeof(record)) < 0);
|
||||
/* OpenConnect translates backend failures to -EIO. Its progress
|
||||
* callback preserves the specific GnuTLS failure; require that
|
||||
* reason so a timeout/disconnect cannot pass as policy refusal. */
|
||||
assert(policy.unsafe_renegotiation_denied);
|
||||
}
|
||||
}
|
||||
openconnect_vpninfo_free(client);
|
||||
assert(!pthread_join(thread, NULL));
|
||||
if (legacy) {
|
||||
assert(peer.renegotiation < 0);
|
||||
}
|
||||
close(peer.listener);
|
||||
gnutls_certificate_free_credentials(peer.credentials);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
assert(argc == 6);
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
assert(!openconnect_init_ssl());
|
||||
#ifdef __APPLE__
|
||||
/* Read-only check of the source-built library's native Keychain integration.
|
||||
* Local fixture trust is tested separately, without changing OS trust settings. */
|
||||
{
|
||||
gnutls_certificate_credentials_t system;
|
||||
assert(!gnutls_certificate_allocate_credentials(&system));
|
||||
assert(gnutls_certificate_set_x509_system_trust(system) > 0);
|
||||
gnutls_certificate_free_credentials(system);
|
||||
}
|
||||
#endif
|
||||
check(argv[1], argv[2], argv[3], 0, 1, 0, NULL, NULL);
|
||||
check(argv[1], argv[2], argv[3], 0, 0, 0, NULL, NULL);
|
||||
check(argv[1], argv[2], argv[3], 0, 0, 1, NULL, NULL);
|
||||
check(argv[1], argv[2], argv[3], 1, 1, 0, NULL, NULL);
|
||||
check(argv[1], argv[2], argv[3], 0, 1, 0, argv[4], argv[5]);
|
||||
puts("OpenConnect: trusted TLS, certificate rejection/acceptance, legacy initial handshake, unsafe renegotiation refusal, mutual TLS passed");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
use std::{
|
||||
fs,
|
||||
os::unix::process::CommandExt,
|
||||
path::{Path, PathBuf},
|
||||
process::{Child, Command},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
struct TestDirectory(PathBuf);
|
||||
impl Drop for TestDirectory {
|
||||
fn drop(&mut self) {
|
||||
let _ = fs::remove_dir_all(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
struct ChildGuard(Child);
|
||||
impl Drop for ChildGuard {
|
||||
fn drop(&mut self) {
|
||||
unsafe { libc::kill(-(self.0.id() as i32), libc::SIGKILL) };
|
||||
let _ = self.0.wait();
|
||||
}
|
||||
}
|
||||
|
||||
fn run_bounded(command: &mut Command, log: &Path) {
|
||||
let output = fs::File::create(log).unwrap();
|
||||
let mut child = ChildGuard(
|
||||
command
|
||||
.process_group(0)
|
||||
.stdout(output.try_clone().unwrap())
|
||||
.stderr(output)
|
||||
.spawn()
|
||||
.unwrap(),
|
||||
);
|
||||
let deadline = Instant::now() + Duration::from_secs(45);
|
||||
loop {
|
||||
if let Some(status) = child.0.try_wait().unwrap() {
|
||||
assert!(status.success(), "{status}: {}", fs::read_to_string(log).unwrap());
|
||||
return;
|
||||
}
|
||||
assert!(
|
||||
Instant::now() < deadline,
|
||||
"TLS test timed out: {}",
|
||||
fs::read_to_string(log).unwrap()
|
||||
);
|
||||
std::thread::sleep(Duration::from_millis(20));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn openconnect_certificate_policy_and_legacy_renegotiation() {
|
||||
let manifest = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
let build = PathBuf::from(env!("OPENCONNECT_PROTOCOL_BUILD"));
|
||||
let source = PathBuf::from(env!("OPENCONNECT_PROTOCOL_SOURCE"));
|
||||
let directory = TestDirectory(std::env::temp_dir().join(format!("gp-tls-backend-{}", std::process::id())));
|
||||
fs::create_dir(&directory.0).unwrap();
|
||||
let executable = directory.0.join("tls-backend");
|
||||
let flags = Command::new("pkg-config")
|
||||
.args(["--cflags", "--libs", "libxml-2.0", "gnutls"])
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(flags.status.success(), "{}", String::from_utf8_lossy(&flags.stderr));
|
||||
run_bounded(
|
||||
Command::new(build.join("libtool"))
|
||||
.args(["--mode=link", "cc", "-pthread"])
|
||||
.arg(manifest.join("tests/tls_backend.c"))
|
||||
.arg("-I")
|
||||
.arg(&build)
|
||||
.arg("-I")
|
||||
.arg(&source)
|
||||
.arg("-I")
|
||||
.arg(source.join("json"))
|
||||
.arg(build.join("libopenconnect.la"))
|
||||
.args(String::from_utf8(flags.stdout).unwrap().split_whitespace())
|
||||
.arg("-o")
|
||||
.arg(&executable),
|
||||
&directory.0.join("compile.log"),
|
||||
);
|
||||
let certificates = manifest.join("deps/openconnect/tests/certs");
|
||||
run_bounded(
|
||||
Command::new(build.join("libtool"))
|
||||
.arg("--mode=execute")
|
||||
.arg(executable)
|
||||
.arg(certificates.join("server-cert.pem"))
|
||||
.arg(certificates.join("server-key.pem"))
|
||||
.arg(certificates.join("ca.pem"))
|
||||
.arg(certificates.join("user-cert.pem"))
|
||||
.arg(certificates.join("user-key-pkcs1.pem")),
|
||||
&directory.0.join("runtime.log"),
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user