From 5263c03889032b5af0500eae76b9196670babbb0 Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Thu, 25 Jun 2026 22:59:33 +0800 Subject: [PATCH] Migrate OpenConnect patches into GitHub submodule fork Point openconnect at yuezk/openconnect globalprotect (d5c1925f) with GP patches 0001-0019 baked in. Point libxml2 at github.com/GNOME/libxml2 keeping the v2.9.9 pin. Remove compile-time patch application from build.rs and delete deps/patches/. --- .gitmodules | 5 +- crates/openconnect/build.rs | 40 +- crates/openconnect/deps/openconnect | 2 +- ...r-GlobalProtect-app-version-handling.patch | 114 -- ...t-for-common-headers-in-GlobalProtec.patch | 32 - ...OS-version-handling-in-GlobalProtect.patch | 121 -- ...on-and-OS-information-to-HIP-script-.patch | 39 - ...xpose-GlobalProtect-session-metadata.patch | 124 -- ...-host-id-to-GlobalProtect-HIP-script.patch | 101 -- ...log-GlobalProtect-getconfig-response.patch | 15 - ...007-Support-GlobalProtect-NLB-config.patch | 238 --- ...dd-GlobalProtect-NLB-module-skeleton.patch | 459 ------ ...ement-GlobalProtect-NLB-ESP-envelope.patch | 487 ------ ...ement-GlobalProtect-NLB-SSL-failover.patch | 389 ----- ...mplement-GlobalProtect-NLB-lifecycle.patch | 307 ---- ...-NLB-integration-and-GCM-recognition.patch | 177 --- ...-ESP-GCM-and-NLB-enc-hs-key-handling.patch | 1324 ----------------- ...exact-PanGPS-do_ssl_decrypt-handling.patch | 453 ------ ...tunnel-opaque-auth-into-ESP-envelope.patch | 387 ----- .../0016-Add-NLB-troubleshooting-logs.patch | 143 -- ...-Harden-NLB-crypto-and-envelope-auth.patch | 446 ------ ...p-NLB-dead-fields-and-error-handling.patch | 146 -- ...0019-Fix-NLB-prepare-opaque-key-wipe.patch | 98 -- 23 files changed, 7 insertions(+), 5640 deletions(-) delete mode 100644 crates/openconnect/deps/patches/0001-Add-support-for-GlobalProtect-app-version-handling.patch delete mode 100644 crates/openconnect/deps/patches/0002-Update-user-agent-for-common-headers-in-GlobalProtec.patch delete mode 100644 crates/openconnect/deps/patches/0003-Add-support-for-OS-version-handling-in-GlobalProtect.patch delete mode 100644 crates/openconnect/deps/patches/0004-Add-client-version-and-OS-information-to-HIP-script-.patch delete mode 100644 crates/openconnect/deps/patches/0005-Expose-GlobalProtect-session-metadata.patch delete mode 100644 crates/openconnect/deps/patches/0006-Add-host-id-to-GlobalProtect-HIP-script.patch delete mode 100644 crates/openconnect/deps/patches/0006-Debug-log-GlobalProtect-getconfig-response.patch delete mode 100644 crates/openconnect/deps/patches/0007-Support-GlobalProtect-NLB-config.patch delete mode 100644 crates/openconnect/deps/patches/0008-Add-GlobalProtect-NLB-module-skeleton.patch delete mode 100644 crates/openconnect/deps/patches/0009-Implement-GlobalProtect-NLB-ESP-envelope.patch delete mode 100644 crates/openconnect/deps/patches/0010-Implement-GlobalProtect-NLB-SSL-failover.patch delete mode 100644 crates/openconnect/deps/patches/0011-Implement-GlobalProtect-NLB-lifecycle.patch delete mode 100644 crates/openconnect/deps/patches/0012-Add-GlobalProtect-NLB-integration-and-GCM-recognition.patch delete mode 100644 crates/openconnect/deps/patches/0013-Implement-ESP-GCM-and-NLB-enc-hs-key-handling.patch delete mode 100644 crates/openconnect/deps/patches/0014-Implement-exact-PanGPS-do_ssl_decrypt-handling.patch delete mode 100644 crates/openconnect/deps/patches/0015-Wire-NLB-tunnel-opaque-auth-into-ESP-envelope.patch delete mode 100644 crates/openconnect/deps/patches/0016-Add-NLB-troubleshooting-logs.patch delete mode 100644 crates/openconnect/deps/patches/0017-Harden-NLB-crypto-and-envelope-auth.patch delete mode 100644 crates/openconnect/deps/patches/0018-Cleanup-NLB-dead-fields-and-error-handling.patch delete mode 100644 crates/openconnect/deps/patches/0019-Fix-NLB-prepare-opaque-key-wipe.patch diff --git a/.gitmodules b/.gitmodules index 9f23140..7822bd4 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,7 @@ [submodule "crates/openconnect/deps/openconnect"] path = crates/openconnect/deps/openconnect - url = https://gitlab.com/openconnect/openconnect.git + url = https://github.com/yuezk/openconnect.git + branch = globalprotect [submodule "crates/openconnect/deps/libxml2"] path = crates/openconnect/deps/libxml2 - url = https://gitlab.gnome.org/GNOME/libxml2.git + url = https://github.com/GNOME/libxml2.git \ No newline at end of file diff --git a/crates/openconnect/build.rs b/crates/openconnect/build.rs index c63f764..d95977a 100644 --- a/crates/openconnect/build.rs +++ b/crates/openconnect/build.rs @@ -1,34 +1,4 @@ -use std::{env, path::PathBuf, process::Command}; - -fn apply_patches(patches_dir: &PathBuf, build_src: &PathBuf) { - let patches = std::fs::read_dir(patches_dir) - .expect("Failed to read patches directory") - .filter_map(|entry| entry.ok()) - .filter(|entry| entry.path().extension().and_then(|s| s.to_str()) == Some("patch")) - .map(|entry| entry.path()) - .collect::>(); - - // Sort patches to ensure they are applied in order - let mut patches = patches; - patches.sort(); - - for patch in patches { - println!("cargo:rerun-if-changed={}", patch.display()); - println!("Applying patch: {}", patch.display()); - - let status = Command::new("patch") - .arg("-p1") // Strip one level of path (standard for git diffs) - .arg("-i") // Input file - .arg(&patch) - .current_dir(build_src) // Run INSIDE the copied dir - .status() - .expect("Failed to execute patch command"); - - if !status.success() { - panic!("Failed to apply patch file: {}", patch.display()); - } - } -} +use std::{env, path::PathBuf}; fn build_libxml2(deps_dir: &PathBuf, out_dir: &PathBuf) -> PathBuf { let libxml2_dir = deps_dir.join("libxml2"); @@ -91,12 +61,11 @@ fn build_libxml2(deps_dir: &PathBuf, out_dir: &PathBuf) -> PathBuf { fn build_openconnect(deps_dir: &PathBuf, out_dir: &PathBuf) -> PathBuf { let openconnect_dir = deps_dir.join("openconnect"); - let patches_dir = deps_dir.join("patches"); // The temporary location where we will build let build_src = out_dir.join("openconnect_build"); - // Track changes so cargo rebuilds if patch or submodule changes + // Track changes so cargo rebuilds if submodule changes println!("cargo:rerun-if-changed={}", openconnect_dir.display()); // Prepare the build directory @@ -114,9 +83,6 @@ fn build_openconnect(deps_dir: &PathBuf, out_dir: &PathBuf) -> PathBuf { fs_extra::dir::copy(&openconnect_dir, &build_src, &options).expect("Failed to copy C source code to OUT_DIR"); - // Apply all patches from the patches directory - apply_patches(&patches_dir, &build_src); - // Build the OpenConnect library using autotools // We explicitly enable static and disable shared to ensure we get a .a file. // .reconf("-ivf") is CRITICAL for git submodules because 'configure' @@ -215,4 +181,4 @@ fn main() { .include("src/ffi") .include(&oc_dst.join("include")) .compile("vpn"); -} +} \ No newline at end of file diff --git a/crates/openconnect/deps/openconnect b/crates/openconnect/deps/openconnect index 0dcdff8..d5c1925 160000 --- a/crates/openconnect/deps/openconnect +++ b/crates/openconnect/deps/openconnect @@ -1 +1 @@ -Subproject commit 0dcdff87db65daf692dc323732831391d595d98d +Subproject commit d5c1925fe3044c00e9867fe3bb66700c5b99a7f7 diff --git a/crates/openconnect/deps/patches/0001-Add-support-for-GlobalProtect-app-version-handling.patch b/crates/openconnect/deps/patches/0001-Add-support-for-GlobalProtect-app-version-handling.patch deleted file mode 100644 index 2e25858..0000000 --- a/crates/openconnect/deps/patches/0001-Add-support-for-GlobalProtect-app-version-handling.patch +++ /dev/null @@ -1,114 +0,0 @@ -From a8d5ec198ca9ae47a0e6b4f1ea244d2738acf055 Mon Sep 17 00:00:00 2001 -From: Kevin Yue -Date: Sun, 14 Dec 2025 17:10:02 +0800 -Subject: [PATCH 1/4] Add support for GlobalProtect app version handling - -- Introduced `gp_app_version` field in `openconnect_info` structure. -- Implemented `openconnect_set_gp_app_version` and `openconnect_get_gp_app_version` functions. -- Updated `gpst_get_config` to use the app version in requests. -- Free `gp_app_version` in `openconnect_vpninfo_free` to prevent memory leaks. ---- - gpst.c | 8 +++++--- - library.c | 15 +++++++++++++++ - openconnect-internal.h | 4 ++++ - openconnect.h | 2 ++ - 4 files changed, 26 insertions(+), 3 deletions(-) - -diff --git a/gpst.c b/gpst.c -index 244aaf04..5c24bbd2 100644 ---- a/gpst.c -+++ b/gpst.c -@@ -650,8 +650,11 @@ static int gpst_get_config(struct openconnect_info *vpninfo) - - - /* submit getconfig request */ -+ const char *app_version = openconnect_get_gp_app_version(vpninfo); -+ vpn_progress(vpninfo, PRG_DEBUG, _("Using GlobalProtect app version: %s\n"), app_version); -+ - buf_append(request_body, "client-type=1&protocol-version=p1&internal=no"); -- append_opt(request_body, "app-version", vpninfo->csd_ticket ? : "6.3.0-33"); -+ append_opt(request_body, "app-version", app_version); - append_opt(request_body, "ipv6-support", vpninfo->disable_ipv6 ? "no" : "yes"); - append_opt(request_body, "clientos", gpst_os_name(vpninfo)); - append_opt(request_body, "os-version", vpninfo->platname); -@@ -1040,8 +1043,7 @@ static int run_hip_script(struct openconnect_info *vpninfo) - * accept new ones. - */ - unsetenv("APP_VERSION"); -- if (vpninfo->csd_ticket) -- if (setenv("APP_VERSION", vpninfo->csd_ticket, 1)) -+ if (setenv("APP_VERSION", openconnect_get_gp_app_version(vpninfo), 1)) - goto out; - - execv(hip_argv[0], (char **)hip_argv); -diff --git a/library.c b/library.c -index b5e74e9b..18334c76 100644 ---- a/library.c -+++ b/library.c -@@ -896,6 +896,7 @@ void openconnect_vpninfo_free(struct openconnect_info *vpninfo) - - free(vpninfo->localname); - free(vpninfo->useragent); -+ free(vpninfo->gp_app_version); - free(vpninfo->authgroup); - #ifdef HAVE_LIBSTOKEN - if (vpninfo->stoken_pin) -@@ -1019,6 +1020,20 @@ int openconnect_set_useragent(struct openconnect_info *vpninfo, - return 0; - } - -+int openconnect_set_gp_app_version(struct openconnect_info *vpninfo, -+ const char *gp_app_version) -+{ -+ UTF8CHECK(gp_app_version); -+ -+ STRDUP(vpninfo->gp_app_version, gp_app_version); -+ return 0; -+} -+ -+const char *openconnect_get_gp_app_version(struct openconnect_info *vpninfo) -+{ -+ return vpninfo->csd_ticket ?: (vpninfo->gp_app_version ?: "6.3.0-33"); -+} -+ - int openconnect_set_urlpath(struct openconnect_info *vpninfo, - const char *urlpath) - { -diff --git a/openconnect-internal.h b/openconnect-internal.h -index abf45bbd..988f85f0 100644 ---- a/openconnect-internal.h -+++ b/openconnect-internal.h -@@ -786,6 +786,7 @@ struct openconnect_info { - int is_dyndns; /* Attempt to redo DNS lookup on each CSTP reconnect */ - char *useragent; - char *version_string; -+ char *gp_app_version; - - const char *quit_reason; - const char *delay_tunnel_reason; /* If non-null, provides a reason why protocol is not yet ready for tunnel setup */ -@@ -1677,6 +1678,9 @@ void openconnect_set_juniper(struct openconnect_info *vpninfo); - /* hpke.c */ - int handle_external_browser(struct openconnect_info *vpninfo); - -+/* Get the app version */ -+const char *openconnect_get_gp_app_version(struct openconnect_info *vpninfo); -+ - /* version.c */ - extern const char openconnect_version_str[]; - -diff --git a/openconnect.h b/openconnect.h -index 136f181a..7d4f3399 100644 ---- a/openconnect.h -+++ b/openconnect.h -@@ -470,6 +470,8 @@ int openconnect_set_http_proxy(struct openconnect_info *vpninfo, - int openconnect_set_useragent(struct openconnect_info *vpninfo, - const char *useragent); - -+int openconnect_set_gp_app_version(struct openconnect_info *vpninfo, -+ const char *gp_app_version); - int openconnect_passphrase_from_fsid(struct openconnect_info *vpninfo); - int openconnect_obtain_cookie(struct openconnect_info *vpninfo); - int openconnect_init_ssl(void); --- -2.50.1 (Apple Git-155) - diff --git a/crates/openconnect/deps/patches/0002-Update-user-agent-for-common-headers-in-GlobalProtec.patch b/crates/openconnect/deps/patches/0002-Update-user-agent-for-common-headers-in-GlobalProtec.patch deleted file mode 100644 index 919bf58..0000000 --- a/crates/openconnect/deps/patches/0002-Update-user-agent-for-common-headers-in-GlobalProtec.patch +++ /dev/null @@ -1,32 +0,0 @@ -From 8591eba702f52dae335601c997271786be50dff7 Mon Sep 17 00:00:00 2001 -From: Kevin Yue -Date: Sun, 14 Dec 2025 17:10:52 +0800 -Subject: [PATCH 2/4] Update user agent for common headers in GlobalProtect - authentication - ---- - auth-globalprotect.c | 7 ------- - 1 file changed, 7 deletions(-) - -diff --git a/auth-globalprotect.c b/auth-globalprotect.c -index 8d094273..27e41c2b 100644 ---- a/auth-globalprotect.c -+++ b/auth-globalprotect.c -@@ -34,14 +34,7 @@ struct login_context { - void gpst_common_headers(struct openconnect_info *vpninfo, - struct oc_text_buf *buf) - { -- char *orig_ua = vpninfo->useragent; -- -- /* XX: more recent servers don't appear to require this specific UA value, -- * but we don't have any good way to detect them. -- */ -- vpninfo->useragent = (char *)"PAN GlobalProtect"; - http_common_headers(vpninfo, buf); -- vpninfo->useragent = orig_ua; - } - - /* Translate platform names (derived from AnyConnect) into the values --- -2.50.1 (Apple Git-155) - diff --git a/crates/openconnect/deps/patches/0003-Add-support-for-OS-version-handling-in-GlobalProtect.patch b/crates/openconnect/deps/patches/0003-Add-support-for-OS-version-handling-in-GlobalProtect.patch deleted file mode 100644 index 08cd680..0000000 --- a/crates/openconnect/deps/patches/0003-Add-support-for-OS-version-handling-in-GlobalProtect.patch +++ /dev/null @@ -1,121 +0,0 @@ -From dda2aceca7e614af65b968e632065ef223cb6612 Mon Sep 17 00:00:00 2001 -From: Kevin Yue -Date: Sun, 14 Dec 2025 17:14:08 +0800 -Subject: [PATCH 3/4] Add support for OS version handling in GlobalProtect - authentication - ---- - auth-globalprotect.c | 2 +- - gpst.c | 4 ++-- - library.c | 15 +++++++++++++++ - openconnect-internal.h | 4 ++++ - openconnect.h | 3 +++ - 5 files changed, 25 insertions(+), 3 deletions(-) - -diff --git a/auth-globalprotect.c b/auth-globalprotect.c -index 27e41c2b..59d6de3b 100644 ---- a/auth-globalprotect.c -+++ b/auth-globalprotect.c -@@ -731,7 +731,7 @@ static int gpst_login(struct openconnect_info *vpninfo, int portal, struct login - buf_append(request_body, "jnlpReady=jnlpReady&ok=Login&direct=yes&clientVer=4100&prot=https:&internal=no"); - append_opt(request_body, "ipv6-support", vpninfo->disable_ipv6 ? "no" : "yes"); - append_opt(request_body, "clientos", gpst_os_name(vpninfo)); -- append_opt(request_body, "os-version", vpninfo->platname); -+ append_opt(request_body, "os-version", openconnect_get_gp_os_version(vpninfo)); - append_opt(request_body, "server", vpninfo->hostname); - append_opt(request_body, "computer", vpninfo->localname); - if (ctx->portal_userauthcookie) -diff --git a/gpst.c b/gpst.c -index 5c24bbd2..0373fce0 100644 ---- a/gpst.c -+++ b/gpst.c -@@ -657,7 +657,7 @@ static int gpst_get_config(struct openconnect_info *vpninfo) - append_opt(request_body, "app-version", app_version); - append_opt(request_body, "ipv6-support", vpninfo->disable_ipv6 ? "no" : "yes"); - append_opt(request_body, "clientos", gpst_os_name(vpninfo)); -- append_opt(request_body, "os-version", vpninfo->platname); -+ append_opt(request_body, "os-version", openconnect_get_gp_os_version(vpninfo)); - append_opt(request_body, "hmac-algo", "sha1,md5,sha256"); - append_opt(request_body, "enc-algo", "aes-128-cbc,aes-256-cbc"); - if (old_addr || old_addr6) { -@@ -1044,7 +1044,7 @@ static int run_hip_script(struct openconnect_info *vpninfo) - */ - unsetenv("APP_VERSION"); - if (setenv("APP_VERSION", openconnect_get_gp_app_version(vpninfo), 1)) -- goto out; -+ goto out; - - execv(hip_argv[0], (char **)hip_argv); - -diff --git a/library.c b/library.c -index 18334c76..e2169054 100644 ---- a/library.c -+++ b/library.c -@@ -897,6 +897,7 @@ void openconnect_vpninfo_free(struct openconnect_info *vpninfo) - free(vpninfo->localname); - free(vpninfo->useragent); - free(vpninfo->gp_app_version); -+ free(vpninfo->gp_os_version); - free(vpninfo->authgroup); - #ifdef HAVE_LIBSTOKEN - if (vpninfo->stoken_pin) -@@ -1034,6 +1035,20 @@ const char *openconnect_get_gp_app_version(struct openconnect_info *vpninfo) - return vpninfo->csd_ticket ?: (vpninfo->gp_app_version ?: "6.3.0-33"); - } - -+int openconnect_set_gp_os_version(struct openconnect_info *vpninfo, -+ const char *gp_os_version) -+{ -+ UTF8CHECK(gp_os_version); -+ -+ STRDUP(vpninfo->gp_os_version, gp_os_version); -+ return 0; -+} -+ -+const char *openconnect_get_gp_os_version(struct openconnect_info *vpninfo) -+{ -+ return vpninfo->gp_os_version ?: vpninfo->platname; -+} -+ - int openconnect_set_urlpath(struct openconnect_info *vpninfo, - const char *urlpath) - { -diff --git a/openconnect-internal.h b/openconnect-internal.h -index 988f85f0..5c8d2909 100644 ---- a/openconnect-internal.h -+++ b/openconnect-internal.h -@@ -787,6 +787,7 @@ struct openconnect_info { - char *useragent; - char *version_string; - char *gp_app_version; -+ char *gp_os_version; - - const char *quit_reason; - const char *delay_tunnel_reason; /* If non-null, provides a reason why protocol is not yet ready for tunnel setup */ -@@ -1681,6 +1682,9 @@ int handle_external_browser(struct openconnect_info *vpninfo); - /* Get the app version */ - const char *openconnect_get_gp_app_version(struct openconnect_info *vpninfo); - -+/* Get the OS version string for GP */ -+const char *openconnect_get_gp_os_version(struct openconnect_info *vpninfo); -+ - /* version.c */ - extern const char openconnect_version_str[]; - -diff --git a/openconnect.h b/openconnect.h -index 7d4f3399..aa96a53b 100644 ---- a/openconnect.h -+++ b/openconnect.h -@@ -472,6 +472,9 @@ int openconnect_set_useragent(struct openconnect_info *vpninfo, - - int openconnect_set_gp_app_version(struct openconnect_info *vpninfo, - const char *gp_app_version); -+int openconnect_set_gp_os_version(struct openconnect_info *vpninfo, -+ const char *gp_os_version); -+ - int openconnect_passphrase_from_fsid(struct openconnect_info *vpninfo); - int openconnect_obtain_cookie(struct openconnect_info *vpninfo); - int openconnect_init_ssl(void); --- -2.50.1 (Apple Git-155) - diff --git a/crates/openconnect/deps/patches/0004-Add-client-version-and-OS-information-to-HIP-script-.patch b/crates/openconnect/deps/patches/0004-Add-client-version-and-OS-information-to-HIP-script-.patch deleted file mode 100644 index 80a2571..0000000 --- a/crates/openconnect/deps/patches/0004-Add-client-version-and-OS-information-to-HIP-script-.patch +++ /dev/null @@ -1,39 +0,0 @@ -From 3915a51c81831a1a9b087d82f0c760365682f23b Mon Sep 17 00:00:00 2001 -From: Kevin Yue -Date: Mon, 15 Dec 2025 19:20:44 +0800 -Subject: [PATCH 4/4] Add client version and OS information to HIP script - arguments - ---- - gpst.c | 8 ++++++-- - 1 file changed, 6 insertions(+), 2 deletions(-) - -diff --git a/gpst.c b/gpst.c -index 0373fce0..d04c42c2 100644 ---- a/gpst.c -+++ b/gpst.c -@@ -1013,6 +1013,12 @@ static int run_hip_script(struct openconnect_info *vpninfo) - exit(1); - - hip_argv[i++] = openconnect_utf8_to_legacy(vpninfo, vpninfo->csd_wrapper); -+ hip_argv[i++] = "--client-version"; -+ hip_argv[i++] = openconnect_get_gp_app_version(vpninfo); -+ hip_argv[i++] = "--client-os"; -+ hip_argv[i++] = gpst_os_name(vpninfo); -+ hip_argv[i++] = "--os-version"; -+ hip_argv[i++] = openconnect_get_gp_os_version(vpninfo); - hip_argv[i++] = "--cookie"; - hip_argv[i++] = vpninfo->cookie; - if (vpninfo->ip_info.addr) { -@@ -1025,8 +1031,6 @@ static int run_hip_script(struct openconnect_info *vpninfo) - } - hip_argv[i++] = "--md5"; - hip_argv[i++] = vpninfo->csd_token; -- hip_argv[i++] = "--client-os"; -- hip_argv[i++] = gpst_os_name(vpninfo); - hip_argv[i++] = NULL; - - /* XX: Sending the above parameters as --long-options was a mistake that was --- -2.50.1 (Apple Git-155) - diff --git a/crates/openconnect/deps/patches/0005-Expose-GlobalProtect-session-metadata.patch b/crates/openconnect/deps/patches/0005-Expose-GlobalProtect-session-metadata.patch deleted file mode 100644 index 48c538d..0000000 --- a/crates/openconnect/deps/patches/0005-Expose-GlobalProtect-session-metadata.patch +++ /dev/null @@ -1,124 +0,0 @@ -diff --git a/gpst.c b/gpst.c -index 244aaf04..78c16d1c 100644 ---- a/gpst.c -+++ b/gpst.c -@@ -438,8 +438,19 @@ static int gpst_parse_config_xml(struct openconnect_info *vpninfo, xmlNode *xml_ - new_ip_info.netmask = add_option_steal(&new_opts, "netmask", &s); - } else if (!xmlnode_get_val(xml_node, "mtu", &s)) - new_ip_info.mtu = atoi(s); -- else if (!xmlnode_get_val(xml_node, "lifetime", &s)) -+ else if (!xmlnode_get_val(xml_node, "lifetime", &s)) { -+ vpninfo->gp_session_lifetime = atoi(s); - vpninfo->auth_expiration = time(NULL) + atol(s); -+ } else if (!xmlnode_get_val(xml_node, "user-expires", &s) || -+ !xmlnode_get_val(xml_node, "user_expires", &s)) { -+ vpninfo->gp_user_expires = atol(s); -+ } else if (!xmlnode_get_val(xml_node, "lifetime-notify-prior", &s)) { -+ vpninfo->gp_lifetime_notify_prior = atoi(s); -+ } else if (!xmlnode_get_val(xml_node, "lifetime-notify-message", &s)) { -+ free(vpninfo->gp_lifetime_notify_message); -+ vpninfo->gp_lifetime_notify_message = s; -+ s = NULL; -+ } - else if (!xmlnode_get_val(xml_node, "quarantine", &s)) { - if (strcmp(s, "no")) - vpn_progress(vpninfo, PRG_DEBUG, -diff --git a/libopenconnect.map.in b/libopenconnect.map.in -index f036c071..44110486 100644 ---- a/libopenconnect.map.in -+++ b/libopenconnect.map.in -@@ -113,6 +113,10 @@ OPENCONNECT_5_7 { - openconnect_set_cookie; - openconnect_set_allow_insecure_crypto; - openconnect_get_auth_expiration; -+ openconnect_get_gp_session_lifetime; -+ openconnect_get_gp_user_expires; -+ openconnect_get_gp_lifetime_notify_prior; -+ openconnect_get_gp_lifetime_notify_message; - openconnect_disable_dtls; - openconnect_get_connect_url; - openconnect_set_webview_callback; -diff --git a/libopenconnect5.symbols b/libopenconnect5.symbols -index 7cb04d05..2c7af781 100644 ---- a/libopenconnect5.symbols -+++ b/libopenconnect5.symbols -@@ -94,6 +94,10 @@ libopenconnect.so.5 libopenconnect5 #MINVER# - openconnect_set_cookie@OPENCONNECT_5_7 8.20 - openconnect_set_allow_insecure_crypto@OPENCONNECT_5_7 8.20 - openconnect_get_auth_expiration@OPENCONNECT_5_7 8.20 -+ openconnect_get_gp_session_lifetime@OPENCONNECT_5_7 8.20 -+ openconnect_get_gp_user_expires@OPENCONNECT_5_7 8.20 -+ openconnect_get_gp_lifetime_notify_prior@OPENCONNECT_5_7 8.20 -+ openconnect_get_gp_lifetime_notify_message@OPENCONNECT_5_7 8.20 - openconnect_disable_dtls@OPENCONNECT_5_7 8.20 - openconnect_get_connect_url@OPENCONNECT_5_7 8.20 - openconnect_set_webview_callback@OPENCONNECT_5_7 8.20 -diff --git a/library.c b/library.c -index b5e74e9b..03f47004 100644 ---- a/library.c -+++ b/library.c -@@ -838,6 +838,7 @@ void openconnect_vpninfo_free(struct openconnect_info *vpninfo) - free(vpninfo->dtls_cipher); - free(vpninfo->peer_cert_hash); - free(vpninfo->ciphersuite_config); -+ free(vpninfo->gp_lifetime_notify_message); - #if defined(OPENCONNECT_OPENSSL) - free(vpninfo->cstp_cipher); - #if defined(HAVE_BIO_METH_FREE) -@@ -1137,6 +1138,26 @@ time_t openconnect_get_auth_expiration(struct openconnect_info *vpninfo) - return vpninfo->auth_expiration; - } - -+int openconnect_get_gp_session_lifetime(struct openconnect_info *vpninfo) -+{ -+ return vpninfo->gp_session_lifetime; -+} -+ -+time_t openconnect_get_gp_user_expires(struct openconnect_info *vpninfo) -+{ -+ return vpninfo->gp_user_expires; -+} -+ -+int openconnect_get_gp_lifetime_notify_prior(struct openconnect_info *vpninfo) -+{ -+ return vpninfo->gp_lifetime_notify_prior; -+} -+ -+const char *openconnect_get_gp_lifetime_notify_message(struct openconnect_info *vpninfo) -+{ -+ return vpninfo->gp_lifetime_notify_message; -+} -+ - int openconnect_get_ip_info(struct openconnect_info *vpninfo, - const struct oc_ip_info **info, - const struct oc_vpn_option **cstp_options, -diff --git a/openconnect-internal.h b/openconnect-internal.h -index abf45bbd..dd5507ca 100644 ---- a/openconnect-internal.h -+++ b/openconnect-internal.h -@@ -647,6 +647,10 @@ struct openconnect_info { - int dtls_attempt_period; - int udp_probes_sent; - time_t auth_expiration; -+ int gp_session_lifetime; -+ time_t gp_user_expires; -+ int gp_lifetime_notify_prior; -+ char *gp_lifetime_notify_message; - time_t new_dtls_started; - #if defined(OPENCONNECT_OPENSSL) - SSL_CTX *dtls_ctx; -diff --git a/openconnect.h b/openconnect.h -index 136f181a..febf15cc 100644 ---- a/openconnect.h -+++ b/openconnect.h -@@ -655,6 +655,10 @@ void openconnect_set_dpd(struct openconnect_info *, int min_seconds); - void openconnect_set_trojan_interval(struct openconnect_info *, int seconds); - int openconnect_get_idle_timeout(struct openconnect_info *); - time_t openconnect_get_auth_expiration(struct openconnect_info *); -+int openconnect_get_gp_session_lifetime(struct openconnect_info *); -+time_t openconnect_get_gp_user_expires(struct openconnect_info *); -+int openconnect_get_gp_lifetime_notify_prior(struct openconnect_info *); -+const char *openconnect_get_gp_lifetime_notify_message(struct openconnect_info *); - - /* The returned structures are owned by the library and may be freed/replaced - due to rekey or reconnect. Assume that once the mainloop starts, the diff --git a/crates/openconnect/deps/patches/0006-Add-host-id-to-GlobalProtect-HIP-script.patch b/crates/openconnect/deps/patches/0006-Add-host-id-to-GlobalProtect-HIP-script.patch deleted file mode 100644 index 3dda55d..0000000 --- a/crates/openconnect/deps/patches/0006-Add-host-id-to-GlobalProtect-HIP-script.patch +++ /dev/null @@ -1,101 +0,0 @@ -From 78078b21f53801b31fef352ec0de40d319edcae9 Mon Sep 17 00:00:00 2001 -From: Kevin Yue -Date: Sat, 13 Jun 2026 12:00:00 +0800 -Subject: [PATCH 6/6] Add host ID to GlobalProtect HIP script arguments - ---- - gpst.c | 4 ++++ - library.c | 15 +++++++++++++++ - openconnect-internal.h | 4 ++++ - openconnect.h | 3 +++ - 4 files changed, 26 insertions(+) - -diff --git a/gpst.c b/gpst.c -index 78c16d1c..f1e1f6bb 100644 ---- a/gpst.c -+++ b/gpst.c -@@ -1019,6 +1019,10 @@ static int run_hip_script(struct openconnect_info *vpninfo) - hip_argv[i++] = "--client-os"; - hip_argv[i++] = gpst_os_name(vpninfo); - hip_argv[i++] = "--os-version"; - hip_argv[i++] = openconnect_get_gp_os_version(vpninfo); -+ if (openconnect_get_gp_host_id(vpninfo)) { -+ hip_argv[i++] = "--host-id"; -+ hip_argv[i++] = openconnect_get_gp_host_id(vpninfo); -+ } - hip_argv[i++] = "--cookie"; - hip_argv[i++] = vpninfo->cookie; - if (vpninfo->ip_info.addr) { -diff --git a/library.c b/library.c -index 03f47004..9e585119 100644 ---- a/library.c -+++ b/library.c -@@ -900,6 +900,7 @@ void openconnect_vpninfo_free(struct openconnect_info *vpninfo) - free(vpninfo->useragent); - free(vpninfo->gp_app_version); - free(vpninfo->gp_os_version); -+ free(vpninfo->gp_host_id); - free(vpninfo->authgroup); - #ifdef HAVE_LIBSTOKEN - if (vpninfo->stoken_pin) -@@ -1049,6 +1050,20 @@ const char *openconnect_get_gp_os_version(struct openconnect_info *vpninfo) - return vpninfo->gp_os_version ?: vpninfo->platname; - } - -+int openconnect_set_gp_host_id(struct openconnect_info *vpninfo, -+ const char *gp_host_id) -+{ -+ UTF8CHECK(gp_host_id); -+ -+ STRDUP(vpninfo->gp_host_id, gp_host_id); -+ return 0; -+} -+ -+const char *openconnect_get_gp_host_id(struct openconnect_info *vpninfo) -+{ -+ return vpninfo->gp_host_id; -+} -+ - int openconnect_set_urlpath(struct openconnect_info *vpninfo, - const char *urlpath) - { -diff --git a/openconnect-internal.h b/openconnect-internal.h -index a7e80659..76034fbd 100644 ---- a/openconnect-internal.h -+++ b/openconnect-internal.h -@@ -788,6 +788,7 @@ struct openconnect_info { - char *version_string; - char *gp_app_version; - char *gp_os_version; -+ char *gp_host_id; - - const char *quit_reason; - const char *delay_tunnel_reason; /* If non-null, provides a reason why protocol is not yet ready for tunnel setup */ -@@ -1684,6 +1685,9 @@ const char *openconnect_get_gp_app_version(struct openconnect_info *vpninfo); - /* Get the OS version string for GP */ - const char *openconnect_get_gp_os_version(struct openconnect_info *vpninfo); - -+/* Get the host ID string for GP */ -+const char *openconnect_get_gp_host_id(struct openconnect_info *vpninfo); -+ - /* version.c */ - extern const char openconnect_version_str[]; - -diff --git a/openconnect.h b/openconnect.h -index 40de2811..3c65c090 100644 ---- a/openconnect.h -+++ b/openconnect.h -@@ -472,8 +472,11 @@ int openconnect_set_useragent(struct openconnect_info *vpninfo, - int openconnect_set_gp_app_version(struct openconnect_info *vpninfo, - const char *gp_app_version); - int openconnect_set_gp_os_version(struct openconnect_info *vpninfo, - const char *gp_os_version); -+int openconnect_set_gp_host_id(struct openconnect_info *vpninfo, -+ const char *gp_host_id); -+ - - int openconnect_passphrase_from_fsid(struct openconnect_info *vpninfo); - int openconnect_obtain_cookie(struct openconnect_info *vpninfo); - int openconnect_init_ssl(void); --- -2.50.1 (Apple Git-155) diff --git a/crates/openconnect/deps/patches/0006-Debug-log-GlobalProtect-getconfig-response.patch b/crates/openconnect/deps/patches/0006-Debug-log-GlobalProtect-getconfig-response.patch deleted file mode 100644 index b3a3e50..0000000 --- a/crates/openconnect/deps/patches/0006-Debug-log-GlobalProtect-getconfig-response.patch +++ /dev/null @@ -1,15 +0,0 @@ -diff --git a/gpst.c b/gpst.c -index 244aaf04..fb8d945d 100644 ---- a/gpst.c -+++ b/gpst.c -@@ -669,6 +669,10 @@ static int gpst_get_config(struct openconnect_info *vpninfo) - result = do_https_request(vpninfo, "POST", "application/x-www-form-urlencoded", request_body, &xml_buf, NULL, HTTP_NO_FLAGS); - free(vpninfo->urlpath); - vpninfo->urlpath = orig_path; -+ if (result >= 0 && xml_buf) -+ vpn_progress(vpninfo, PRG_DEBUG, -+ "GlobalProtect getconfig.esp response:\n%s\n", -+ xml_buf); - - /* parse getconfig result */ - if (result >= 0) diff --git a/crates/openconnect/deps/patches/0007-Support-GlobalProtect-NLB-config.patch b/crates/openconnect/deps/patches/0007-Support-GlobalProtect-NLB-config.patch deleted file mode 100644 index 9e085a2..0000000 --- a/crates/openconnect/deps/patches/0007-Support-GlobalProtect-NLB-config.patch +++ /dev/null @@ -1,238 +0,0 @@ -From 0000000000000000000000000000000000000001 Mon Sep 17 00:00:00 2001 -From: Kevin Yue -Date: Wed, 25 Jun 2026 18:00:00 +0800 -Subject: [PATCH 7/7] Support GlobalProtect NLB config - -Parse NLB fields from getconfig.esp XML, store them in vpninfo, and -align the getconfig request with the official GlobalProtect client. ---- - gpst.c | 58 ++++++++++++++++++++++++++++++++++++++-- - libopenconnect.map.in | 2 ++ - libopenconnect5.symbols | 2 ++ - library.c | 16 +++++++++++ - openconnect-internal.h | 15 ++++++++++ - openconnect.h | 2 ++ - 6 files changed, 93 insertions(+), 2 deletions(-) - -diff --git a/gpst.c b/gpst.c -index f1e1f6bb..a8b3c4d5 100644 ---- a/gpst.c -+++ b/gpst.c -@@ -477,6 +477,10 @@ static int gpst_parse_config_xml(struct openconnect_info *vpninfo, xmlNode *xml_ - * gateway is meaningless." See esp_send_probes_gp for the - * gory details of what this field actually means. - */ -+ if (!strcmp(s, "127.127.127.127")) { -+ free(vpninfo->gp_nlb.tunnel_vip); -+ vpninfo->gp_nlb.tunnel_vip = strdup(s); -+ } - if (vpninfo->peer_addr->sa_family == IPPROTO_IP && - vpninfo->ip_info.gateway_addr && strcmp(s, vpninfo->ip_info.gateway_addr)) - vpn_progress(vpninfo, PRG_DEBUG, -@@ -495,12 +499,17 @@ static int gpst_parse_config_xml(struct openconnect_info *vpninfo, xmlNode *xml_ - inet_pton(AF_INET6, s, &esp_magic_v6); - #endif /* HAVE_ESP */ - } else if (!xmlnode_get_val(xml_node, "connected-gw-ip", &s)) { -- if (vpninfo->ip_info.gateway_addr && strcmp(s, vpninfo->ip_info.gateway_addr)) -+ free(vpninfo->gp_nlb.connected_gw_ip); -+ vpninfo->gp_nlb.connected_gw_ip = s; -+ s = NULL; -+ if (vpninfo->ip_info.gateway_addr && -+ strcmp(vpninfo->gp_nlb.connected_gw_ip, vpninfo->ip_info.gateway_addr)) - vpn_progress(vpninfo, PRG_DEBUG, _("Config XML address (%s) differs from external\n" - "gateway address (%s). Please report this to\n" - "<%s>, including any problems\n" - "with ESP or other apparent loss of connectivity or performance.\n"), -- s, vpninfo->ip_info.gateway_addr, "openconnect-devel@lists.infradead.org"); -+ vpninfo->gp_nlb.connected_gw_ip, vpninfo->ip_info.gateway_addr, -+ "openconnect-devel@lists.infradead.org"); - } else if (xmlnode_is_named(xml_node, "dns-v6") || - xmlnode_is_named(xml_node, "dns")) { - for (member = xml_node->children; member && n_dns<3; member=member->next) { -@@ -575,6 +584,36 @@ static int gpst_parse_config_xml(struct openconnect_info *vpninfo, xmlNode *xml_ - #else - vpn_progress(vpninfo, PRG_DEBUG, _("Ignoring ESP keys since ESP support not available in this build\n")); - #endif -+ } else if (xmlnode_is_named(xml_node, "hs-key")) { -+#ifdef HAVE_ESP -+ int keylen = xml_to_key(xml_node, vpninfo->gp_nlb.hs_key, sizeof(vpninfo->gp_nlb.hs_key)); -+ if (keylen > 0) { -+ vpninfo->gp_nlb.hs_key_len = keylen; -+ vpninfo->gp_nlb.hs_key_bits = keylen * 8; -+ vpninfo->gp_nlb.enabled = 1; -+ } -+#endif /* HAVE_ESP */ -+ } else if (!xmlnode_get_val(xml_node, "enc-hs-key", &s)) { -+ free(vpninfo->gp_nlb.enc_hs_key); -+ vpninfo->gp_nlb.enc_hs_key = s; -+ s = NULL; -+ vpninfo->gp_nlb.enabled = 1; -+ } else if (xmlnode_is_named(xml_node, "tunnel-opaque")) { -+ if (!xmlnode_get_val(xml_node, "val", &s)) { -+ free(vpninfo->gp_nlb.tunnel_opaque); -+ vpninfo->gp_nlb.tunnel_opaque = s; -+ s = NULL; -+ vpninfo->gp_nlb.enabled = 1; -+ } -+ if (!xmlnode_get_val(xml_node, "valid-period", &s)) { -+ vpninfo->gp_nlb.opaque_valid_until = time(NULL) + atol(s); -+ } -+ } else if (!xmlnode_get_val(xml_node, "valid-period", &s)) { -+ vpninfo->gp_nlb.opaque_valid_until = time(NULL) + atol(s); -+ } else if (!xmlnode_get_val(xml_node, "in-tunnel-gw-cert-chksum", &s)) { -+ free(vpninfo->gp_nlb.in_tunnel_gw_cert_chksum); -+ vpninfo->gp_nlb.in_tunnel_gw_cert_chksum = s; -+ s = NULL; - } else if (xmlnode_is_named(xml_node, "need-tunnel") - || xmlnode_is_named(xml_node, "bw-c2s") - || xmlnode_is_named(xml_node, "bw-s2c") -@@ -585,7 +624,8 @@ static int gpst_parse_config_xml(struct openconnect_info *vpninfo, xmlNode *xml_ - || xmlnode_is_named(xml_node, "ip-address-v6-preferred") - || xmlnode_is_named(xml_node, "ipv6-connection") - || xmlnode_is_named(xml_node, "portal") -- || xmlnode_is_named(xml_node, "user")) { -+ || xmlnode_is_named(xml_node, "user") -+ || xmlnode_is_named(xml_node, "use-ssl-tunnel")) { - /* XX: Do these have any potential value at all for routing configuration or diagnostics? */ - } else if (xml_node->type == XML_ELEMENT_NODE) { - free(s); -@@ -597,6 +637,15 @@ static int gpst_parse_config_xml(struct openconnect_info *vpninfo, xmlNode *xml_ - } - } - -+ if (vpninfo->gp_nlb.tunnel_opaque) -+ vpninfo->gp_nlb.enabled = 1; -+ -+ if (vpninfo->gp_nlb.enabled) -+ vpn_progress(vpninfo, PRG_INFO, _("NLB enabled\n")); -+ else -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("hs-key section is not specified. Non-nlb\n")); -+ - /* Set 10-second DPD/keepalive (same as Windows client) unless - * overridden with --force-dpd */ - if (!vpninfo->ssl_times.dpd) -@@ -670,13 +719,17 @@ static int gpst_get_config(struct openconnect_info *vpninfo) - append_opt(request_body, "clientos", gpst_os_name(vpninfo)); - append_opt(request_body, "os-version", openconnect_get_gp_os_version(vpninfo)); - append_opt(request_body, "hmac-algo", "sha1,md5,sha256"); -- append_opt(request_body, "enc-algo", "aes-128-cbc,aes-256-cbc"); -+ append_opt(request_body, "enc-algo", "aes-256-gcm,aes-128-gcm,aes-128-cbc"); -+ append_opt(request_body, "clientgpversion", app_version); -+ if (openconnect_get_gp_host_id(vpninfo)) -+ append_opt(request_body, "host-id", openconnect_get_gp_host_id(vpninfo)); - if (old_addr || old_addr6) { - append_opt(request_body, "preferred-ip", old_addr); - append_opt(request_body, "preferred-ipv6", old_addr6); - filter_opts(request_body, vpninfo->cookie, "preferred-ip,preferred-ipv6", 0); - } else - buf_append(request_body, "&%s", vpninfo->cookie); -+ filter_opts(request_body, vpninfo->cookie, "serialno,clientgpversion,host-id", 1); - if ((result = buf_error(request_body))) - goto out; - -diff --git a/libopenconnect.map.in b/libopenconnect.map.in -index 44110486..8c2f1a3b 100644 ---- a/libopenconnect.map.in -+++ b/libopenconnect.map.in -@@ -117,6 +117,8 @@ OPENCONNECT_5_7 { - openconnect_get_gp_user_expires; - openconnect_get_gp_lifetime_notify_prior; - openconnect_get_gp_lifetime_notify_message; -+ openconnect_get_gp_nlb_enabled; -+ openconnect_get_gp_nlb_connected_gw_ip; - openconnect_disable_dtls; - openconnect_get_connect_url; - openconnect_set_webview_callback; -diff --git a/libopenconnect5.symbols b/libopenconnect5.symbols -index 2c7af781..b4e8d9a2 100644 ---- a/libopenconnect5.symbols -+++ b/libopenconnect5.symbols -@@ -98,6 +98,8 @@ libopenconnect.so.5 libopenconnect5 #MINVER# - openconnect_get_gp_user_expires@OPENCONNECT_5_7 8.20 - openconnect_get_gp_lifetime_notify_prior@OPENCONNECT_5_7 8.20 - openconnect_get_gp_lifetime_notify_message@OPENCONNECT_5_7 8.20 -+ openconnect_get_gp_nlb_enabled@OPENCONNECT_5_7 8.20 -+ openconnect_get_gp_nlb_connected_gw_ip@OPENCONNECT_5_7 8.20 - openconnect_disable_dtls@OPENCONNECT_5_7 8.20 - openconnect_get_connect_url@OPENCONNECT_5_7 8.20 - openconnect_set_webview_callback@OPENCONNECT_5_7 8.20 -diff --git a/library.c b/library.c -index 9e585119..c3d4e5f6 100644 ---- a/library.c -+++ b/library.c -@@ -839,6 +839,12 @@ void openconnect_vpninfo_free(struct openconnect_info *vpninfo) - free(vpninfo->peer_cert_hash); - free(vpninfo->ciphersuite_config); - free(vpninfo->gp_lifetime_notify_message); -+ free(vpninfo->gp_nlb.enc_hs_key); -+ free(vpninfo->gp_nlb.tunnel_opaque); -+ free(vpninfo->gp_nlb.inner_gw_ip); -+ free(vpninfo->gp_nlb.tunnel_vip); -+ free(vpninfo->gp_nlb.connected_gw_ip); -+ free(vpninfo->gp_nlb.in_tunnel_gw_cert_chksum); - #if defined(OPENCONNECT_OPENSSL) - free(vpninfo->cstp_cipher); - #if defined(HAVE_BIO_METH_FREE) -@@ -1201,6 +1207,16 @@ const char *openconnect_get_gp_lifetime_notify_message(struct openconnect_info *v - return vpninfo->gp_lifetime_notify_message; - } - -+int openconnect_get_gp_nlb_enabled(struct openconnect_info *vpninfo) -+{ -+ return vpninfo->gp_nlb.enabled; -+} -+ -+const char *openconnect_get_gp_nlb_connected_gw_ip(struct openconnect_info *vpninfo) -+{ -+ return vpninfo->gp_nlb.connected_gw_ip; -+} -+ - int openconnect_get_ip_info(struct openconnect_info *vpninfo, - const struct oc_ip_info **info, - const struct oc_vpn_option **cstp_options, -diff --git a/openconnect-internal.h b/openconnect-internal.h -index 76034fbd..91a2b3c4 100644 ---- a/openconnect-internal.h -+++ b/openconnect-internal.h -@@ -391,6 +391,20 @@ struct oc_ip_info { - - struct openconnect_info; - -+struct gp_nlb_config { -+ int enabled; -+ unsigned char hs_key[32]; -+ int hs_key_bits; -+ int hs_key_len; -+ char *enc_hs_key; -+ char *tunnel_opaque; -+ time_t opaque_valid_until; -+ char *inner_gw_ip; -+ char *tunnel_vip; -+ char *connected_gw_ip; -+ char *in_tunnel_gw_cert_chksum; -+}; -+ - struct cert_info { - struct openconnect_info *vpninfo; - char *cert; -@@ -651,6 +665,7 @@ struct openconnect_info { - time_t gp_user_expires; - int gp_lifetime_notify_prior; - char *gp_lifetime_notify_message; -+ struct gp_nlb_config gp_nlb; - time_t new_dtls_started; - #if defined(OPENCONNECT_OPENSSL) - SSL_CTX *dtls_ctx; -diff --git a/openconnect.h b/openconnect.h -index febf15cc..d7e8f9a0 100644 ---- a/openconnect.h -+++ b/openconnect.h -@@ -667,6 +667,8 @@ time_t openconnect_get_gp_user_expires(struct openconnect_info *); - int openconnect_get_gp_lifetime_notify_prior(struct openconnect_info *); - const char *openconnect_get_gp_lifetime_notify_message(struct openconnect_info *); -+int openconnect_get_gp_nlb_enabled(struct openconnect_info *); -+const char *openconnect_get_gp_nlb_connected_gw_ip(struct openconnect_info *); - - /* The returned structures are owned by the library and may be freed/replaced - due to rekey or reconnect. Assume that once the mainloop starts, the diff --git a/crates/openconnect/deps/patches/0008-Add-GlobalProtect-NLB-module-skeleton.patch b/crates/openconnect/deps/patches/0008-Add-GlobalProtect-NLB-module-skeleton.patch deleted file mode 100644 index eca5061..0000000 --- a/crates/openconnect/deps/patches/0008-Add-GlobalProtect-NLB-module-skeleton.patch +++ /dev/null @@ -1,459 +0,0 @@ -From 0000000000000000000000000000000000000002 Mon Sep 17 00:00:00 2001 -From: Kevin Yue -Date: Wed, 25 Jun 2026 18:30:00 +0800 -Subject: [PATCH 8/8] Add GlobalProtect NLB module skeleton - -Add gpst_nlb.c with PanGPS 6.3.3.1 RE notes, SSL response parser, -opaque blob decoding, and stub ESP encap/decap for Phase 3. ---- - Makefile.am | 2 +- - gpst.c | 4 + - gpst_nlb.c | 271 + - tests/gp-nlb-ssl-parse | 90 +++++++++++++++++++++++++++++++++++++++++ - library.c | 4 + - openconnect-internal.h | 8 ++ - 6 files changed, 378 insertions(+), 1 deletion(-) - create mode 100644 gpst_nlb.c - -diff --git a/Makefile.am b/Makefile.am -index 12345678..abcdef01 100644 ---- a/Makefile.am -+++ b/Makefile.am -@@ -45,7 +45,7 @@ - lib_srcs_cisco = auth.c cstp.c hpke.c multicert.c - lib_srcs_juniper = oncp.c lzo.c auth-juniper.c - lib_srcs_pulse = pulse.c --lib_srcs_globalprotect = gpst.c win32-ipicmp.h auth-globalprotect.c -+lib_srcs_globalprotect = gpst.c gpst_nlb.c win32-ipicmp.h auth-globalprotect.c - lib_srcs_array = array.c - lib_srcs_oath = oath.c - lib_srcs_oidc = oidc.c -diff --git a/gpst.c b/gpst.c -index a8b3c4d5..b9c4d5e6 100644 ---- a/gpst.c -+++ b/gpst.c -@@ -1162,6 +1162,10 @@ - if (ret) - goto out; - -+ ret = gpst_nlb_prepare(vpninfo); -+ if (ret) -+ goto out; -+ - /* Always check HIP after getting configuration */ - ret = check_and_maybe_submit_hip_report(vpninfo); - if (ret) -diff --git a/gpst_nlb.c b/gpst_nlb.c -new file mode 100644 -index 00000000..c0ffee00 100644 ---- /dev/null -+++ b/gpst_nlb.c -@@ -0,0 +1,271 @@ -+/* -+ * GlobalProtect NLB (Network Load Balancer) tunnel support -+ * -+ * Reverse-engineering notes from PanGPS 6.3.3.1 (GlobalProtect_UI_deb-6.3.3.1-619): -+ * -+ * Detection (ClientConfig.cpp): -+ * - NLB gateways return , , , -+ * - Non-NLB logs: "hs-key section is not specified. Non-nlb" -+ * -+ * Config field usage: -+ * hs-key Handshake key ( + hex); fed to pan_esp_encap -+ * enc-hs-key Base64 encrypted auxiliary key; likely decrypts tunnel-opaque -+ * tunnel-opaque Base64 blob stored internally as opaqueBlob -+ * valid-period Seconds until tunnel-opaque must be refreshed via getconfig.esp -+ * gw-address ESP magic / VIP; NLB gateways use 127.127.127.127 -+ * connected-gw-ip NLB frontend address (may differ from DNS peer) -+ * -+ * ESP path (pan_esp_encap.cpp, PanNATTunnelVPN.cpp): -+ * - Standard ESP keys from XML are installed via pan_esp_encap_init -+ * - Outbound UDP payloads wrapped by EncapSendData using opaqueBlob + pan_hmac -+ * - Inbound unwrapped by DecapRecvData; failures log: -+ * "invalid tunnel packet len %u magic 0x%x type %u" -+ * - ESP probes use 56-byte ICMP payload (not openconnect's 16-byte minimum): -+ * "monitor\\0\\0pan ha 0123456789:;<=>? !\"#$%&'()*+,-./" + 0x10..0x17 -+ * - Probes target tunnel VIP (), UDP still sent to public peer_addr -+ * - NLB keepalive sent before data: "Send keepalive first for NLB tunnel restoration" -+ * -+ * SSL failover path (PanSslVpn.cpp): -+ * - SendTunnelRequest: user=%s&authcookie=%s&install=yes&preferred-ip=%s&preferred-ipv6=%s -+ * - Response is raw line protocol, not an HTTP status line. NLB format (issue #526): -+ * "110 <40-hex-sha1> START_TUNNEL" -+ * Example: "110 127.127.127.127192.168.255.43 c057fc7f... START_TUNNEL" -+ * - vip and inner_gw_ip are concatenated without a separator -+ * -+ * Post-handshake (HandleNLB in PanNATTunnelVPN.cpp): -+ * - getNlbRealGwIp() resolves inner gateway IP -+ * - "NLB: add an access route for NLB private ip: %s." -+ * -+ * Crypto RE status: opaque envelope magic/type constants and enc-hs-key -+ * decipherment are not yet recovered from stripped ELF; encap/decap remain stubs. -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+ -+#include "openconnect-internal.h" -+ -+/* Official 56-byte ESP probe payload (PanGPS / Windows client) */ -+static const unsigned char gp_nlb_probe_template[56] = -+ "monitor\x00\x00pan ha 0123456789:;<=>? !\"#$%&'()*+,-./" -+ "\x10\x11\x12\x13\x14\x15\x16\x17"; -+ -+#define GP_NLB_SSL_PROTO_VERSION 110 -+ -+static int gp_nlb_is_ipv4(const char *s) -+{ -+ struct in_addr a; -+ return s && inet_pton(AF_INET, s, &a) == 1; -+} -+ -+static int gp_nlb_split_concat_ips(const char *concat, const char *hint_vip, -+ char **vip, char **inner) -+{ -+ const char *split; -+ size_t vip_len; -+ -+ if (!concat || !*concat) -+ return -EINVAL; -+ -+ if (hint_vip && !strncmp(concat, hint_vip, strlen(hint_vip))) { -+ vip_len = strlen(hint_vip); -+ if (concat[vip_len] && gp_nlb_is_ipv4(concat + vip_len)) { -+ STRDUP(*vip, hint_vip); -+ STRDUP(*inner, concat + vip_len); -+ return 0; -+ } -+ } -+ -+ for (split = concat; *split; split++) { -+ if (*split != '.' && !isdigit((unsigned char)*split)) -+ continue; -+ if (!gp_nlb_is_ipv4(concat) || !gp_nlb_is_ipv4(split)) -+ continue; -+ vip_len = split - concat; -+ if (!vip_len) -+ continue; -+ *vip = strndup(concat, vip_len); -+ *inner = strdup(split); -+ if (!*vip || !*inner) { -+ free(*vip); -+ free(*inner); -+ *vip = *inner = NULL; -+ return -ENOMEM; -+ } -+ return 0; -+ } -+ -+ return -EINVAL; -+} -+ -+static void gp_nlb_clear_opaque_blob(struct gp_nlb_config *nlb) -+{ -+ if (nlb->opaque_blob) { -+ memset(nlb->opaque_blob, 0, nlb->opaque_blob_len); -+ free(nlb->opaque_blob); -+ nlb->opaque_blob = NULL; -+ } -+ nlb->opaque_blob_len = 0; -+} -+ -+int gpst_nlb_prepare(struct openconnect_info *vpninfo) -+{ -+ struct gp_nlb_config *nlb = &vpninfo->gp_nlb; -+ int len; -+ -+ if (!nlb->enabled) -+ return 0; -+ -+ if (!nlb->tunnel_vip && nlb->tunnel_opaque) -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("NLB enabled but no tunnel VIP yet; expecting 127.127.127.127 from \n")); -+ -+ gp_nlb_clear_opaque_blob(nlb); -+ if (nlb->tunnel_opaque) { -+ nlb->opaque_blob = openconnect_base64_decode(&len, nlb->tunnel_opaque); -+ if (!nlb->opaque_blob || len <= 0) { -+ vpn_progress(vpninfo, PRG_ERR, -+ _("Failed to base64-decode tunnel-opaque\n")); -+ free(nlb->opaque_blob); -+ nlb->opaque_blob = NULL; -+ return -EINVAL; -+ } -+ nlb->opaque_blob_len = len; -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("Decoded tunnel-opaque (%d bytes); enc-hs-key decipherment not yet implemented\n"), -+ len); -+ } -+ -+ if (nlb->enc_hs_key) -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("enc-hs-key present (%zu chars); crypto RE pending\n"), -+ strlen(nlb->enc_hs_key)); -+ -+ return 0; -+} -+ -+const unsigned char *gpst_nlb_probe_payload(size_t *len) -+{ -+ if (len) -+ *len = sizeof(gp_nlb_probe_template); -+ return gp_nlb_probe_template; -+} -+ -+int gpst_nlb_esp_encap(struct openconnect_info *vpninfo, struct pkt *pkt) -+{ -+ if (!vpninfo->gp_nlb.enabled) -+ return 0; -+ -+ /* Phase 3: wrap pkt with NLB opaque envelope via hs-key + opaque_blob */ -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("gpst_nlb_esp_encap: stub (opaque envelope RE incomplete)\n")); -+ return -ENOTSUP; -+} -+ -+int gpst_nlb_esp_decap(struct openconnect_info *vpninfo, struct pkt *pkt) -+{ -+ if (!vpninfo->gp_nlb.enabled) -+ return 0; -+ -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("gpst_nlb_esp_decap: stub (opaque envelope RE incomplete)\n")); -+ return -ENOTSUP; -+} -+ -+int gpst_nlb_parse_ssl_response(const char *buf, int len, struct gp_nlb_config *nlb) -+{ -+ char *line, *vip = NULL, *inner = NULL, *hash, *start, *concat; -+ const char *hint_vip; -+ long version; -+ int ret = -EINVAL; -+ -+ if (!buf || len <= 0 || !nlb) -+ return -EINVAL; -+ -+ line = strndup(buf, len); -+ if (!line) -+ return -ENOMEM; -+ -+ /* Trim trailing CR/LF */ -+ for (char *p = line + strlen(line); p > line; p--) { -+ if (p[-1] == '\r' || p[-1] == '\n') -+ p[-1] = '\0'; -+ else -+ break; -+ } -+ -+ if (!strncmp(line, "START_TUNNEL", 12)) { -+ ret = 0; -+ goto out; -+ } -+ -+ start = strstr(line, " START_TUNNEL"); -+ if (!start) { -+ ret = -EINVAL; -+ goto out; -+ } -+ *start = '\0'; -+ hash = strrchr(line, ' '); -+ if (!hash || strlen(hash + 1) != 40) { -+ ret = -EINVAL; -+ goto out; -+ } -+ *hash++ = '\0'; -+ -+ if (sscanf(line, "%ld", &version) != 1 || version != GP_NLB_SSL_PROTO_VERSION) { -+ ret = -EINVAL; -+ goto out; -+ } -+ -+ concat = strchr(line, ' '); -+ if (!concat) { -+ ret = -EINVAL; -+ goto out; -+ } -+ concat++; -+ -+ hint_vip = nlb->tunnel_vip ?: "127.127.127.127"; -+ if (gp_nlb_split_concat_ips(concat, hint_vip, &vip, &inner)) { -+ ret = -EINVAL; -+ goto out; -+ } -+ -+ free(nlb->tunnel_vip); -+ free(nlb->inner_gw_ip); -+ free(nlb->in_tunnel_gw_cert_chksum); -+ nlb->tunnel_vip = vip; -+ nlb->inner_gw_ip = inner; -+ vip = inner = NULL; -+ STRDUP(nlb->in_tunnel_gw_cert_chksum, hash); -+ nlb->enabled = 1; -+ ret = 0; -+ -+out: -+ free(vip); -+ free(inner); -+ free(line); -+ return ret; -+} -+ -+int gpst_nlb_apply_routes(struct openconnect_info *vpninfo) -+{ -+ struct gp_nlb_config *nlb = &vpninfo->gp_nlb; -+ -+ if (!nlb->enabled) -+ return 0; -+ -+ if (nlb->inner_gw_ip) -+ vpn_progress(vpninfo, PRG_INFO, -+ _("NLB: add an access route for NLB private ip: %s.\n"), -+ nlb->inner_gw_ip); -+ else -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("NLB route setup deferred: inner gateway IP not yet known\n")); -+ -+ /* Phase 4: install split-include route via install_vpn_opts/script */ -+ return 0; -+} -diff --git a/library.c b/library.c -index c3d4e5f6..d4e5f6a7 100644 ---- a/library.c -+++ b/library.c -@@ -845,6 +845,10 @@ - free(vpninfo->gp_nlb.tunnel_vip); - free(vpninfo->gp_nlb.connected_gw_ip); - free(vpninfo->gp_nlb.in_tunnel_gw_cert_chksum); -+ if (vpninfo->gp_nlb.opaque_blob) { -+ memset(vpninfo->gp_nlb.opaque_blob, 0, vpninfo->gp_nlb.opaque_blob_len); -+ free(vpninfo->gp_nlb.opaque_blob); -+ } - #if defined(OPENCONNECT_OPENSSL) - free(vpninfo->cstp_cipher); - #if defined(HAVE_BIO_METH_FREE) -diff --git a/openconnect-internal.h b/openconnect-internal.h -index 91a2b3c4..a2b3c4d5 100644 ---- a/openconnect-internal.h -+++ b/openconnect-internal.h -@@ -403,6 +403,8 @@ - char *tunnel_vip; - char *connected_gw_ip; - char *in_tunnel_gw_cert_chksum; -+ unsigned char *opaque_blob; -+ int opaque_blob_len; - }; - - struct cert_info { -@@ -1420,6 +1422,12 @@ - int (*challenge_cb)(struct openconnect_info *, char *prompt, char *inputStr, void *cb_data), - void *cb_data); - int gpst_setup(struct openconnect_info *vpninfo); -+int gpst_nlb_prepare(struct openconnect_info *vpninfo); -+const unsigned char *gpst_nlb_probe_payload(size_t *len); -+int gpst_nlb_esp_encap(struct openconnect_info *vpninfo, struct pkt *pkt); -+int gpst_nlb_esp_decap(struct openconnect_info *vpninfo, struct pkt *pkt); -+int gpst_nlb_parse_ssl_response(const char *buf, int len, struct gp_nlb_config *nlb); -+int gpst_nlb_apply_routes(struct openconnect_info *vpninfo); - int gpst_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable); - int gpst_esp_send_probes(struct openconnect_info *vpninfo); - int gpst_esp_catch_probe(struct openconnect_info *vpninfo, struct pkt *pkt); -diff --git a/tests/gp-nlb-ssl-parse b/tests/gp-nlb-ssl-parse -new file mode 100755 -index 00000000..deadbeef 100755 ---- /dev/null -+++ b/tests/gp-nlb-ssl-parse -@@ -0,0 +1,90 @@ -+#!/bin/sh -+# Validate gpst_nlb_parse_ssl_response against issue #526 sample (tringgiling log) -+set -eu -+cd "$(dirname "$0")/.." -+CC="${CC:-cc}" -+TMP=$(mktemp -d) -+trap 'rm -rf "$TMP"' EXIT -+ -+cat > "$TMP/test.c" <<'EOF' -+#include -+#include -+#include -+#include -+#include -+ -+struct gp_nlb_config { -+ char *tunnel_vip; -+ char *inner_gw_ip; -+ char *in_tunnel_gw_cert_chksum; -+ int enabled; -+}; -+ -+#define GP_NLB_SSL_PROTO_VERSION 110 -+ -+static int gp_nlb_is_ipv4(const char *s) -+{ -+ struct in_addr a; -+ return s && inet_pton(AF_INET, s, &a) == 1; -+} -+ -+static int gp_nlb_split_concat_ips(const char *concat, const char *hint_vip, -+ char **vip, char **inner) -+{ -+ const char *split; -+ size_t vip_len; -+ -+ if (!concat || !*concat) -+ return -1; -+ -+ if (hint_vip && !strncmp(concat, hint_vip, strlen(hint_vip))) { -+ vip_len = strlen(hint_vip); -+ if (concat[vip_len] && gp_nlb_is_ipv4(concat + vip_len)) { -+ *vip = strdup(hint_vip); -+ *inner = strdup(concat + vip_len); -+ return 0; -+ } -+ } -+ return -1; -+} -+ -+static int gpst_nlb_parse_ssl_response(const char *buf, int len, struct gp_nlb_config *nlb) -+{ -+ char *line, *vip = NULL, *inner = NULL, *hash, *start, *concat; -+ const char *hint_vip; -+ long version; -+ int ret = -1; -+ -+ line = strndup(buf, len); -+ start = strstr(line, " START_TUNNEL"); -+ *start = '\0'; -+ hash = strrchr(line, ' '); -+ *hash++ = '\0'; -+ sscanf(line, "%ld", &version); -+ concat = strchr(line, ' ') + 1; -+ hint_vip = "127.127.127.127"; -+ gp_nlb_split_concat_ips(concat, hint_vip, &vip, &inner); -+ nlb->tunnel_vip = vip; -+ nlb->inner_gw_ip = inner; -+ nlb->in_tunnel_gw_cert_chksum = strdup(hash); -+ return 0; -+} -+ -+int main(void) -+{ -+ struct gp_nlb_config nlb = {0}; -+ const char *sample = "110 127.127.127.127192.168.255.43 c057fc7f7c6b74f9ac68b639bb056732404cbacf START_TUNNEL"; -+ gpst_nlb_parse_ssl_response(sample, strlen(sample), &nlb); -+ if (strcmp(nlb.tunnel_vip, "127.127.127.127") || -+ strcmp(nlb.inner_gw_ip, "192.168.255.43") || -+ strcmp(nlb.in_tunnel_gw_cert_chksum, "c057fc7f7c6b74f9ac68b639bb056732404cbacf")) { -+ fprintf(stderr, "parse mismatch: vip=%s inner=%s hash=%s\n", -+ nlb.tunnel_vip, nlb.inner_gw_ip, nlb.in_tunnel_gw_cert_chksum); -+ return 1; -+ } -+ printf("gp-nlb-ssl-parse: OK\n"); -+ return 0; -+} -+EOF -+$CC -o "$TMP/test" "$TMP/test.c" -+"$TMP/test" diff --git a/crates/openconnect/deps/patches/0009-Implement-GlobalProtect-NLB-ESP-envelope.patch b/crates/openconnect/deps/patches/0009-Implement-GlobalProtect-NLB-ESP-envelope.patch deleted file mode 100644 index cf0f6c2..0000000 --- a/crates/openconnect/deps/patches/0009-Implement-GlobalProtect-NLB-ESP-envelope.patch +++ /dev/null @@ -1,487 +0,0 @@ -From 0000000000000000000000000000000000000003 Mon Sep 17 00:00:00 2001 -From: Kevin Yue -Date: Wed, 25 Jun 2026 19:00:00 +0800 -Subject: [PATCH 9/9] Implement GlobalProtect NLB ESP envelope - -Wire the 16-byte NLB UDP envelope around ESP ciphertext for GlobalProtect -gateways: encap/decap in gpst_nlb.c, hooks in esp.c, 56-byte NLB probes, -and keepalive-before-data in gpst_esp_send_probes(). ---- ---- a/esp.c 2026-06-25 18:30:22 -+++ b/esp.c 2026-06-25 18:29:48 -@@ -184,6 +184,15 @@ - } - - work_done = 1; -+ -+ if (vpninfo->proto->proto == PROTO_GPST && vpninfo->gp_nlb.enabled) { -+ int nlb_ret = gpst_nlb_esp_decap(vpninfo, pkt, &len); -+ -+ if (nlb_ret > 0) -+ continue; -+ if (nlb_ret < 0) -+ continue; -+ } - - /* both supported algos (SHA1 and MD5) have 12-byte MAC lengths (RFC2403 and RFC2404) */ - if (len <= sizeof(pkt->esp) + vpninfo->hmac_out_len) -@@ -406,6 +415,13 @@ - work_done = 1; - continue; - } -+ -+ if (vpninfo->proto->proto == PROTO_GPST && vpninfo->gp_nlb.enabled && -+ gpst_nlb_esp_encap(vpninfo, this, &len) < 0) { -+ free_pkt(vpninfo, this); -+ work_done = 1; -+ continue; -+ } - } - - ret = send(vpninfo->dtls_fd, (void *)&this->esp, len, 0); ---- a/gpst.c 2026-06-25 18:30:23 -+++ b/gpst.c 2026-06-25 18:30:06 -@@ -1534,14 +1534,24 @@ - * - * Don't blame me. I didn't design this. - */ -- const int icmplen = ICMP_MINLEN + sizeof(magic_ping_payload); -- int plen, seq = vpninfo->udp_probes_sent; -+ const unsigned char *probe_payload; -+ size_t probe_payload_len; -+ int icmplen, plen, seq = vpninfo->udp_probes_sent; -+ -+ if (vpninfo->gp_nlb.enabled) { -+ probe_payload = gpst_nlb_probe_payload(&probe_payload_len); -+ } else { -+ probe_payload = (const unsigned char *)magic_ping_payload; -+ probe_payload_len = sizeof(magic_ping_payload); -+ } -+ icmplen = ICMP_MINLEN + probe_payload_len; - - if (vpninfo->esp_magic_af == AF_INET6) - plen = sizeof(struct ip6_hdr) + icmplen; - else - plen = sizeof(struct ip) + icmplen; -- struct pkt *pkt = alloc_pkt(vpninfo, plen + vpninfo->pkt_trailer); -+ struct pkt *pkt = alloc_pkt(vpninfo, plen + vpninfo->pkt_trailer + -+ gpst_nlb_pkt_slack(vpninfo)); - if (!pkt) - return -ENOMEM; - -@@ -1559,6 +1569,11 @@ - monitor_except_fd(vpninfo, dtls); - } - -+ if (vpninfo->gp_nlb.enabled && !vpninfo->gp_nlb.keepalive_sent) { -+ gpst_nlb_send_esp_keepalive(vpninfo); -+ vpninfo->gp_nlb.keepalive_sent = 1; -+ } -+ - if (vpninfo->esp_magic_af == AF_INET6) { - memset(pkt, 0, sizeof(*pkt) + plen); - pkt->len = plen; -@@ -1585,7 +1600,7 @@ - icmph->icmp6_data16[1] = htons(seq); /* sequence */ - - /* required to get gateway to respond */ -- memcpy(&icmph[1], magic_ping_payload, sizeof(magic_ping_payload)); -+ memcpy(&icmph[1], probe_payload, probe_payload_len); - - /* - * IPv6 upper-layer checksums include a pseudo-header -@@ -1614,7 +1629,7 @@ - * being just LLLL + NH. - */ - sum += IPPROTO_ICMPV6; -- sum += ICMP_MINLEN + sizeof(magic_ping_payload); -+ sum += ICMP_MINLEN + probe_payload_len; - - sum += csum_partial(icmph, icmplen / 2); - icmph->icmp6_cksum = csum_finish(sum); -@@ -1641,8 +1656,8 @@ - icmph->icmp_type = ICMP_ECHO; - icmph->icmp_hun.ih_idseq.icd_id = htons(0x4747); - icmph->icmp_hun.ih_idseq.icd_seq = htons(seq); -- memcpy(pmagic, magic_ping_payload, sizeof(magic_ping_payload)); /* required to get gateway to respond */ -- icmph->icmp_cksum = csum(icmph, (ICMP_MINLEN+sizeof(magic_ping_payload))/2); -+ memcpy(pmagic, probe_payload, probe_payload_len); /* required to get gateway to respond */ -+ icmph->icmp_cksum = csum(icmph, (ICMP_MINLEN + probe_payload_len) / 2); - } - - if (vpninfo->dtls_state != DTLS_ESTABLISHED) { -@@ -1652,8 +1667,12 @@ - } - - int pktlen = construct_esp_packet(vpninfo, pkt, vpninfo->esp_magic_af == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IPIP); -- if (pktlen < 0 || send(vpninfo->dtls_fd, (void *)&pkt->esp, pktlen, 0) < 0) -+ if (pktlen < 0) - vpn_progress(vpninfo, PRG_DEBUG, _("Failed to send ESP probe\n")); -+ else if (vpninfo->gp_nlb.enabled && gpst_nlb_esp_encap(vpninfo, pkt, &pktlen) < 0) -+ vpn_progress(vpninfo, PRG_DEBUG, _("Failed to NLB-wrap ESP probe\n")); -+ else if (send(vpninfo->dtls_fd, (void *)&pkt->esp, pktlen, 0) < 0) -+ vpn_progress(vpninfo, PRG_DEBUG, _("Failed to send ESP probe\n")); - - free_pkt(vpninfo, pkt); - -@@ -1662,23 +1681,33 @@ - - int gpst_esp_catch_probe(struct openconnect_info *vpninfo, struct pkt *pkt) - { -+ const unsigned char *probe_payload; -+ size_t probe_payload_len; -+ -+ if (vpninfo->gp_nlb.enabled) -+ probe_payload = gpst_nlb_probe_payload(&probe_payload_len); -+ else { -+ probe_payload = (const unsigned char *)magic_ping_payload; -+ probe_payload_len = sizeof(magic_ping_payload); -+ } -+ - if (vpninfo->esp_magic_af == AF_INET6) { - struct ip6_hdr *iph = (void *)(pkt->data); - return ( pkt->len >= 41 && (ntohl(iph->ip6_flow) >> 28)==6 /* IPv6 header */ - && iph->ip6_nxt == IPPROTO_ICMPV6 /* IPv6 next header field = ICMPv6 */ - && !memcmp(&iph->ip6_src, vpninfo->esp_magic, 16) /* source == magic address */ -- && pkt->len >= 40 + ICMP_MINLEN + sizeof(magic_ping_payload) /* No short-packet segfaults */ -+ && pkt->len >= 40 + ICMP_MINLEN + probe_payload_len /* No short-packet segfaults */ - && pkt->data[40]==ICMP6_ECHO_REPLY /* ICMPv6 reply */ -- && !memcmp(&pkt->data[40 + ICMP_MINLEN], magic_ping_payload, sizeof(magic_ping_payload)) /* Same magic payload in response */ -+ && !memcmp(&pkt->data[40 + ICMP_MINLEN], probe_payload, probe_payload_len) /* Same magic payload in response */ - ); - } else { - struct ip *iph = (void *)(pkt->data); - return ( pkt->len >= 21 && iph->ip_v==4 /* IPv4 header */ - && iph->ip_p==IPPROTO_ICMP /* IPv4 protocol field == ICMP */ - && !memcmp(&iph->ip_src.s_addr, vpninfo->esp_magic, 4) /* source == magic address */ -- && pkt->len >= (iph->ip_hl<<2) + ICMP_MINLEN + sizeof(magic_ping_payload) /* No short-packet segfaults */ -+ && pkt->len >= (iph->ip_hl<<2) + ICMP_MINLEN + probe_payload_len /* No short-packet segfaults */ - && pkt->data[iph->ip_hl<<2]==ICMP_ECHOREPLY /* ICMP reply */ -- && !memcmp(&pkt->data[(iph->ip_hl<<2) + ICMP_MINLEN], magic_ping_payload, sizeof(magic_ping_payload)) /* Same magic payload in response */ -+ && !memcmp(&pkt->data[(iph->ip_hl<<2) + ICMP_MINLEN], probe_payload, probe_payload_len) /* Same magic payload in response */ - ); - } - } ---- a/gpst_nlb.c 2026-06-25 18:30:23 -+++ b/gpst_nlb.c 2026-06-25 18:29:42 -@@ -25,6 +25,15 @@ - * - Probes target tunnel VIP (), UDP still sent to public peer_addr - * - NLB keepalive sent before data: "Send keepalive first for NLB tunnel restoration" - * -+ * NLB UDP envelope (PanGPS 6.3.3.1 RE, outside ESP ciphertext): -+ * 0-3 u32 magic 0x1a2b3c4d (same as GPST SSL tunnel header) -+ * 4-5 u16 (unused on decap for standard types) -+ * 6-7 u16 inner_len BE (= wire_len - 16) -+ * 8 u8 type: 0=keepalive, 1=data -+ * 9 u8 flag -+ * 10-15 reserved (zero) -+ * 16+ inner payload (ESP ciphertext for type 1) -+ * - * SSL failover path (PanSslVpn.cpp): - * - SendTunnelRequest: user=%s&authcookie=%s&install=yes&preferred-ip=%s&preferred-ipv6=%s - * - Response is raw line protocol, not an HTTP status line. NLB format (issue #526): -@@ -36,8 +45,8 @@ - * - getNlbRealGwIp() resolves inner gateway IP - * - "NLB: add an access route for NLB private ip: %s." - * -- * Crypto RE status: opaque envelope magic/type constants and enc-hs-key -- * decipherment are not yet recovered from stripped ELF; encap/decap remain stubs. -+ * Crypto RE status: enc-hs-key decipherment and opaque_blob HMAC fields are not -+ * yet recovered from stripped ELF; the 16-byte envelope wrap/unwrap is implemented. - */ - - #include -@@ -49,6 +58,11 @@ - - #include "openconnect-internal.h" - -+#define GP_NLB_HDR_LEN 16 -+#define GP_NLB_MAGIC 0x1a2b3c4d -+#define GP_NLB_TYPE_KEEPALIVE 0 -+#define GP_NLB_TYPE_DATA 1 -+ - /* Official 56-byte ESP probe payload (PanGPS / Windows client) */ - static const unsigned char gp_nlb_probe_template[56] = - "monitor\x00\x00pan ha 0123456789:;<=>? !\"#$%&'()*+,-./" -@@ -112,17 +126,39 @@ - nlb->opaque_blob_len = 0; - } - -+static void gp_nlb_write_hdr(unsigned char *hdr, uint16_t inner_len, uint8_t type) -+{ -+ store_be32(hdr, GP_NLB_MAGIC); -+ store_be16(hdr + 4, 0); -+ store_be16(hdr + 6, inner_len); -+ hdr[8] = type; -+ hdr[9] = 0; -+ memset(hdr + 10, 0, 6); -+} -+ -+int gpst_nlb_pkt_slack(struct openconnect_info *vpninfo) -+{ -+ return vpninfo->gp_nlb.enabled ? GP_NLB_HDR_LEN : 0; -+} -+ - int gpst_nlb_prepare(struct openconnect_info *vpninfo) - { - struct gp_nlb_config *nlb = &vpninfo->gp_nlb; -+ struct in_addr vip; - int len; - - if (!nlb->enabled) - return 0; - -- if (!nlb->tunnel_vip && nlb->tunnel_opaque) -+ nlb->keepalive_sent = 0; -+ -+ if (nlb->tunnel_vip && inet_pton(AF_INET, nlb->tunnel_vip, &vip) == 1) { -+ vpninfo->esp_magic_af = AF_INET; -+ memcpy(vpninfo->esp_magic, &vip, sizeof(vip)); -+ } else if (!nlb->tunnel_vip && nlb->tunnel_opaque) { - vpn_progress(vpninfo, PRG_DEBUG, - _("NLB enabled but no tunnel VIP yet; expecting 127.127.127.127 from \n")); -+ } - - gp_nlb_clear_opaque_blob(nlb); - if (nlb->tunnel_opaque) { -@@ -155,27 +191,88 @@ - return gp_nlb_probe_template; - } - --int gpst_nlb_esp_encap(struct openconnect_info *vpninfo, struct pkt *pkt) -+int gpst_nlb_send_esp_keepalive(struct openconnect_info *vpninfo) - { -- if (!vpninfo->gp_nlb.enabled) -+ unsigned char hdr[GP_NLB_HDR_LEN]; -+ -+ if (!vpninfo->gp_nlb.enabled || vpninfo->dtls_fd < 0) - return 0; - -- /* Phase 3: wrap pkt with NLB opaque envelope via hs-key + opaque_blob */ -- vpn_progress(vpninfo, PRG_DEBUG, -- _("gpst_nlb_esp_encap: stub (opaque envelope RE incomplete)\n")); -- return -ENOTSUP; -+ gp_nlb_write_hdr(hdr, 0, GP_NLB_TYPE_KEEPALIVE); -+ vpn_progress(vpninfo, PRG_DEBUG, _("Send NLB ESP keepalive\n")); -+ if (send(vpninfo->dtls_fd, hdr, sizeof(hdr), 0) < 0) { -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("Failed to send NLB ESP keepalive: %s\n"), -+ strerror(errno)); -+ return -errno; -+ } -+ return 0; - } - --int gpst_nlb_esp_decap(struct openconnect_info *vpninfo, struct pkt *pkt) -+int gpst_nlb_esp_encap(struct openconnect_info *vpninfo, struct pkt *pkt, int *len) - { -+ int esplen; -+ unsigned char *wire; -+ - if (!vpninfo->gp_nlb.enabled) - return 0; - -- vpn_progress(vpninfo, PRG_DEBUG, -- _("gpst_nlb_esp_decap: stub (opaque envelope RE incomplete)\n")); -- return -ENOTSUP; -+ esplen = *len; -+ wire = (unsigned char *)&pkt->esp; -+ -+ if (esplen <= 0) -+ return -EINVAL; -+ -+ if ((char *)wire + esplen + GP_NLB_HDR_LEN > -+ (char *)pkt + sizeof(*pkt) + pkt->alloc_len) -+ return -ENOSPC; -+ -+ memmove(wire + GP_NLB_HDR_LEN, wire, esplen); -+ gp_nlb_write_hdr(wire, esplen, GP_NLB_TYPE_DATA); -+ *len = esplen + GP_NLB_HDR_LEN; -+ return 0; - } - -+int gpst_nlb_esp_decap(struct openconnect_info *vpninfo, struct pkt *pkt, int *len) -+{ -+ unsigned char *wire; -+ uint16_t inner_len; -+ uint8_t type; -+ uint32_t magic; -+ int wire_len; -+ -+ if (!vpninfo->gp_nlb.enabled) -+ return 0; -+ -+ wire = (unsigned char *)&pkt->esp; -+ wire_len = *len; -+ -+ if (wire_len < GP_NLB_HDR_LEN) -+ return -EINVAL; -+ -+ magic = load_be32(wire); -+ type = wire[8]; -+ inner_len = load_be16(wire + 6); -+ -+ if (magic != GP_NLB_MAGIC || type > 8 || -+ wire_len != GP_NLB_HDR_LEN + inner_len) { -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("invalid tunnel packet len %d magic 0x%x type %u\n"), -+ wire_len, magic, type); -+ return -EINVAL; -+ } -+ -+ if (type == GP_NLB_TYPE_KEEPALIVE) { -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("Got NLB ESP keepalive response\n")); -+ return 1; -+ } -+ -+ memmove(wire, wire + GP_NLB_HDR_LEN, inner_len); -+ *len = inner_len; -+ return 0; -+} -+ - int gpst_nlb_parse_ssl_response(const char *buf, int len, struct gp_nlb_config *nlb) - { - char *line, *vip = NULL, *inner = NULL, *hash, *start, *concat; -@@ -268,4 +365,4 @@ - - /* Phase 4: install split-include route via install_vpn_opts/script */ - return 0; --} -+} -\ No newline at end of file ---- a/openconnect-internal.h 2026-06-25 18:30:23 -+++ b/openconnect-internal.h 2026-06-25 18:29:42 -@@ -405,6 +405,7 @@ - char *in_tunnel_gw_cert_chksum; - unsigned char *opaque_blob; - int opaque_blob_len; -+ int keepalive_sent; - }; - - struct cert_info { -@@ -1423,9 +1424,11 @@ - void *cb_data); - int gpst_setup(struct openconnect_info *vpninfo); - int gpst_nlb_prepare(struct openconnect_info *vpninfo); -+int gpst_nlb_pkt_slack(struct openconnect_info *vpninfo); - const unsigned char *gpst_nlb_probe_payload(size_t *len); --int gpst_nlb_esp_encap(struct openconnect_info *vpninfo, struct pkt *pkt); --int gpst_nlb_esp_decap(struct openconnect_info *vpninfo, struct pkt *pkt); -+int gpst_nlb_send_esp_keepalive(struct openconnect_info *vpninfo); -+int gpst_nlb_esp_encap(struct openconnect_info *vpninfo, struct pkt *pkt, int *len); -+int gpst_nlb_esp_decap(struct openconnect_info *vpninfo, struct pkt *pkt, int *len); - int gpst_nlb_parse_ssl_response(const char *buf, int len, struct gp_nlb_config *nlb); - int gpst_nlb_apply_routes(struct openconnect_info *vpninfo); - int gpst_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable); ---- a/tests/gp-nlb-esp-envelope 2026-06-25 18:31:30 -+++ b/tests/gp-nlb-esp-envelope 2026-06-25 18:30:14 -@@ -0,0 +1,107 @@ -+#!/bin/sh -+# Validate NLB ESP envelope header encode/decode roundtrip -+set -eu -+cd "$(dirname "$0")/.." -+CC="${CC:-cc}" -+TMP=$(mktemp -d) -+trap 'rm -rf "$TMP"' EXIT -+ -+cat > "$TMP/test.c" <<'EOF' -+#include -+#include -+#include -+#include -+ -+#define GP_NLB_HDR_LEN 16 -+#define GP_NLB_MAGIC 0x1a2b3c4d -+#define GP_NLB_TYPE_KEEPALIVE 0 -+#define GP_NLB_TYPE_DATA 1 -+ -+static inline void store_be32(void *p, uint32_t d) -+{ -+ uint32_t n = htonl(d); -+ memcpy(p, &n, 4); -+} -+ -+static inline void store_be16(void *p, uint16_t d) -+{ -+ uint16_t n = htons(d); -+ memcpy(p, &n, 2); -+} -+ -+static inline uint32_t load_be32(const void *p) -+{ -+ uint32_t n; -+ memcpy(&n, p, 4); -+ return ntohl(n); -+} -+ -+static inline uint16_t load_be16(const void *p) -+{ -+ uint16_t n; -+ memcpy(&n, p, 2); -+ return ntohs(n); -+} -+ -+static void gp_nlb_write_hdr(unsigned char *hdr, uint16_t inner_len, uint8_t type) -+{ -+ store_be32(hdr, GP_NLB_MAGIC); -+ store_be16(hdr + 4, 0); -+ store_be16(hdr + 6, inner_len); -+ hdr[8] = type; -+ hdr[9] = 0; -+ memset(hdr + 10, 0, 6); -+} -+ -+static int gp_nlb_decap(unsigned char *wire, int *len) -+{ -+ int wire_len = *len; -+ uint16_t inner_len; -+ uint8_t type; -+ -+ if (wire_len < GP_NLB_HDR_LEN) -+ return -1; -+ if (load_be32(wire) != GP_NLB_MAGIC) -+ return -1; -+ type = wire[8]; -+ inner_len = load_be16(wire + 6); -+ if (wire_len != GP_NLB_HDR_LEN + inner_len) -+ return -1; -+ if (type == GP_NLB_TYPE_KEEPALIVE) -+ return 1; -+ memmove(wire, wire + GP_NLB_HDR_LEN, inner_len); -+ *len = inner_len; -+ return 0; -+} -+ -+int main(void) -+{ -+ unsigned char buf[128]; -+ unsigned char esp[] = { 0xde, 0xad, 0xbe, 0xef, 0x01, 0x02, 0x03, 0x04 }; -+ int len, ret; -+ -+ /* keepalive: header only */ -+ gp_nlb_write_hdr(buf, 0, GP_NLB_TYPE_KEEPALIVE); -+ len = GP_NLB_HDR_LEN; -+ ret = gp_nlb_decap(buf, &len); -+ if (ret != 1 || len != GP_NLB_HDR_LEN) { -+ fprintf(stderr, "keepalive decap failed ret=%d len=%d\n", ret, len); -+ return 1; -+ } -+ -+ /* data: header + fake ESP payload */ -+ memcpy(buf + GP_NLB_HDR_LEN, esp, sizeof(esp)); -+ gp_nlb_write_hdr(buf, sizeof(esp), GP_NLB_TYPE_DATA); -+ len = GP_NLB_HDR_LEN + sizeof(esp); -+ ret = gp_nlb_decap(buf, &len); -+ if (ret != 0 || len != (int)sizeof(esp) || memcmp(buf, esp, sizeof(esp))) { -+ fprintf(stderr, "data decap failed ret=%d len=%d\n", ret, len); -+ return 1; -+ } -+ -+ printf("gp-nlb-esp-envelope: OK\n"); -+ return 0; -+} -+EOF -+$CC -o "$TMP/test" "$TMP/test.c" -+"$TMP/test" -\ No newline at end of file diff --git a/crates/openconnect/deps/patches/0010-Implement-GlobalProtect-NLB-SSL-failover.patch b/crates/openconnect/deps/patches/0010-Implement-GlobalProtect-NLB-SSL-failover.patch deleted file mode 100644 index fc2959d..0000000 --- a/crates/openconnect/deps/patches/0010-Implement-GlobalProtect-NLB-SSL-failover.patch +++ /dev/null @@ -1,389 +0,0 @@ -From 0000000000000000000000000000000000000004 Mon Sep 17 00:00:00 2001 -From: Kevin Yue -Date: Wed, 25 Jun 2026 20:00:00 +0800 -Subject: [PATCH 10/10] Implement GlobalProtect NLB SSL failover - -Extend gpst_connect for NLB gateways: send install=yes with preferred IPs, -accept the 110...START_TUNNEL line protocol response, parse VIP/inner GW, -and install a split-include route for the NLB private gateway IP. ---- ---- a/gpst.c 2026-06-25 18:35:09 -+++ b/gpst.c 2026-06-25 18:35:46 -@@ -789,10 +789,10 @@ - - static int gpst_connect(struct openconnect_info *vpninfo) - { -- int ret; -+ int ret, nread; - struct oc_text_buf *reqbuf; - static const char start_tunnel[12] __attribute__((nonstring)) = "START_TUNNEL"; /* NOT NUL-terminated */ -- char buf[256]; -+ char buf[512]; - - /* We do NOT actually start the HTTPS tunnel if ESP is enabled and we received - * ESP keys, because the ESP keys become invalid as soon as the HTTPS tunnel -@@ -812,6 +812,13 @@ - reqbuf = buf_alloc(); - buf_append(reqbuf, "GET %s?", vpninfo->urlpath); - filter_opts(reqbuf, vpninfo->cookie, "user,authcookie", 1); -+ if (vpninfo->gp_nlb.enabled) { -+ append_opt(reqbuf, "install", "yes"); -+ if (vpninfo->ip_info.addr) -+ append_opt(reqbuf, "preferred-ip", vpninfo->ip_info.addr); -+ if (vpninfo->ip_info.addr6) -+ append_opt(reqbuf, "preferred-ipv6", vpninfo->ip_info.addr6); -+ } - buf_append(reqbuf, " HTTP/1.1\r\n\r\n"); - if ((ret = buf_error(reqbuf))) - goto out; -@@ -821,36 +828,51 @@ - - vpninfo->ssl_write(vpninfo, reqbuf->data, reqbuf->pos); - -- if ((ret = vpninfo->ssl_read(vpninfo, buf, 12)) < 0) { -- if (ret == -EINTR) -+ nread = vpninfo->ssl_read(vpninfo, buf, sizeof(buf) - 1); -+ if (nread < 0) { -+ if (nread == -EINTR) - goto out; - vpn_progress(vpninfo, PRG_ERR, - _("Error fetching GET-tunnel HTTPS response.\n")); - ret = -EINVAL; - goto out; - } -- -- if (!strncmp(buf, start_tunnel, sizeof(start_tunnel))) { -- ret = 0; -- } else if (ret==0) { -+ if (!nread) { - vpn_progress(vpninfo, PRG_ERR, - _("Gateway disconnected immediately after GET-tunnel request.\n")); - ret = -EPIPE; -- } else { -- if (ret==sizeof(start_tunnel)) { -- ret = vpninfo->ssl_gets(vpninfo, buf+sizeof(start_tunnel), sizeof(buf)-sizeof(start_tunnel)); -- ret = (ret>0 ? ret : 0) + sizeof(start_tunnel); -- dump_buf(vpninfo, '<', buf); -- } -- int status = check_http_status(buf, ret); -+ goto out; -+ } -+ -+ buf[nread] = '\0'; -+ if (nread == (int)sizeof(start_tunnel) && !strstr(buf, start_tunnel)) { -+ int more = vpninfo->ssl_gets(vpninfo, buf + nread, sizeof(buf) - 1 - nread); -+ -+ if (more > 0) -+ nread += more; -+ buf[nread] = '\0'; -+ } -+ -+ if (vpninfo->dump_http_traffic) -+ dump_buf(vpninfo, '<', buf); -+ -+ if (!gpst_nlb_handle_ssl_connect_response(vpninfo, buf, nread)) -+ ret = 0; -+ else if (nread > 4 && !strncmp(buf, "HTTP", 4)) { -+ int status = check_http_status(buf, nread); -+ - /* XX: GP servers return 502 when they don't like the cookie */ - if (status == 502) - ret = -EPERM; - else { - vpn_progress(vpninfo, PRG_ERR, _("Got unexpected HTTP response: %.*s\n"), -- ret, buf); -+ nread, buf); - ret = -EINVAL; - } -+ } else { -+ vpn_progress(vpninfo, PRG_ERR, _("Got unexpected SSL tunnel response: %.*s\n"), -+ nread, buf); -+ ret = -EINVAL; - } - - if (ret < 0) ---- a/gpst_nlb.c 2026-06-25 18:35:09 -+++ b/gpst_nlb.c 2026-06-25 18:36:07 -@@ -346,23 +346,147 @@ - free(inner); - free(line); - return ret; -+} -+ -+static struct oc_split_include *gp_nlb_clone_split_list(struct oc_split_include *list, -+ struct oc_vpn_option **opts, -+ const char *optname) -+{ -+ struct oc_split_include *head = NULL, **tail = &head; -+ -+ for (; list; list = list->next) { -+ struct oc_split_include *inc = malloc(sizeof(*inc)); -+ -+ if (!inc) -+ return NULL; -+ inc->route = add_option_dup(opts, optname, list->route, -1); -+ if (!inc->route) { -+ free(inc); -+ return NULL; -+ } -+ inc->next = NULL; -+ *tail = inc; -+ tail = &inc->next; -+ } -+ return head; - } - -+static int gp_nlb_route_list_contains(struct oc_split_include *list, const char *ip) -+{ -+ for (; list; list = list->next) { -+ if (list->route && strstr(list->route, ip)) -+ return 1; -+ } -+ return 0; -+} -+ -+static int gpst_nlb_update_esp_magic(struct openconnect_info *vpninfo) -+{ -+ struct gp_nlb_config *nlb = &vpninfo->gp_nlb; -+ struct in_addr vip; -+ -+ if (!nlb->tunnel_vip || inet_pton(AF_INET, nlb->tunnel_vip, &vip) != 1) -+ return 0; -+ -+ vpninfo->esp_magic_af = AF_INET; -+ memcpy(vpninfo->esp_magic, &vip, sizeof(vip)); -+ return 0; -+} -+ - int gpst_nlb_apply_routes(struct openconnect_info *vpninfo) - { - struct gp_nlb_config *nlb = &vpninfo->gp_nlb; -+ struct oc_ip_info new_ip_info; -+ struct oc_vpn_option *new_opts = NULL; -+ struct oc_vpn_option *opt; -+ struct oc_split_include *inc; -+ char route[64]; -+ int ret = 0; - -- if (!nlb->enabled) -+ if (!nlb->enabled || !nlb->inner_gw_ip) - return 0; - -- if (nlb->inner_gw_ip) -- vpn_progress(vpninfo, PRG_INFO, -- _("NLB: add an access route for NLB private ip: %s.\n"), -- nlb->inner_gw_ip); -- else -- vpn_progress(vpninfo, PRG_DEBUG, -- _("NLB route setup deferred: inner gateway IP not yet known\n")); -+ if (gp_nlb_route_list_contains(vpninfo->ip_info.split_includes, nlb->inner_gw_ip)) -+ return 0; - -- /* Phase 4: install split-include route via install_vpn_opts/script */ -+ if (snprintf(route, sizeof(route), "%s/32", nlb->inner_gw_ip) >= (int)sizeof(route)) -+ return -EINVAL; -+ -+ for (opt = vpninfo->cstp_options; opt; opt = opt->next) { -+ if (!add_option_dup(&new_opts, opt->option, opt->value, -1)) { -+ ret = -ENOMEM; -+ goto err; -+ } -+ } -+ -+ inc = malloc(sizeof(*inc)); -+ if (!inc) { -+ ret = -ENOMEM; -+ goto err; -+ } -+ inc->route = add_option_dup(&new_opts, "split-include", route, -1); -+ if (!inc->route) { -+ free(inc); -+ ret = -ENOMEM; -+ goto err; -+ } -+ inc->next = NULL; -+ -+ new_ip_info = vpninfo->ip_info; -+ new_ip_info.split_includes = gp_nlb_clone_split_list(vpninfo->ip_info.split_includes, -+ &new_opts, "split-include"); -+ new_ip_info.split_excludes = gp_nlb_clone_split_list(vpninfo->ip_info.split_excludes, -+ &new_opts, "split-exclude"); -+ if ((vpninfo->ip_info.split_includes && !new_ip_info.split_includes) || -+ (vpninfo->ip_info.split_excludes && !new_ip_info.split_excludes)) { -+ free(inc); -+ ret = -ENOMEM; -+ goto err; -+ } -+ -+ inc->next = new_ip_info.split_includes; -+ new_ip_info.split_includes = inc; -+ inc = NULL; -+ -+ vpn_progress(vpninfo, PRG_INFO, -+ _("NLB: add an access route for NLB private ip: %s.\n"), -+ nlb->inner_gw_ip); -+ -+ if (install_vpn_opts(vpninfo, new_opts, &new_ip_info)) { -+ free_split_routes(&new_ip_info); -+ ret = -EINVAL; -+ goto err; -+ } - return 0; -+ -+err: -+ free_optlist(new_opts); -+ free(inc); -+ return ret; -+} -+ -+int gpst_nlb_on_ssl_tunnel(struct openconnect_info *vpninfo) -+{ -+ gpst_nlb_update_esp_magic(vpninfo); -+ return gpst_nlb_apply_routes(vpninfo); -+} -+ -+int gpst_nlb_handle_ssl_connect_response(struct openconnect_info *vpninfo, -+ const char *buf, int len) -+{ -+ static const char start_tunnel[] = "START_TUNNEL"; -+ -+ if (!buf || len <= 0) -+ return -EINVAL; -+ -+ if (len >= 12 && !strncmp(buf, start_tunnel, 12)) -+ return gpst_nlb_on_ssl_tunnel(vpninfo); -+ -+ if (!strstr(buf, start_tunnel)) -+ return -EINVAL; -+ -+ if (gpst_nlb_parse_ssl_response(buf, len, &vpninfo->gp_nlb) < 0) -+ return -EINVAL; -+ -+ return gpst_nlb_on_ssl_tunnel(vpninfo); - } -\ No newline at end of file ---- a/openconnect-internal.h 2026-06-25 18:35:09 -+++ b/openconnect-internal.h 2026-06-25 18:35:46 -@@ -1431,6 +1431,9 @@ - int gpst_nlb_esp_decap(struct openconnect_info *vpninfo, struct pkt *pkt, int *len); - int gpst_nlb_parse_ssl_response(const char *buf, int len, struct gp_nlb_config *nlb); - int gpst_nlb_apply_routes(struct openconnect_info *vpninfo); -+int gpst_nlb_on_ssl_tunnel(struct openconnect_info *vpninfo); -+int gpst_nlb_handle_ssl_connect_response(struct openconnect_info *vpninfo, -+ const char *buf, int len); - int gpst_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable); - int gpst_esp_send_probes(struct openconnect_info *vpninfo); - int gpst_esp_catch_probe(struct openconnect_info *vpninfo, struct pkt *pkt); ---- a/tests/gp-nlb-ssl-connect 2026-06-25 18:36:12 -+++ b/tests/gp-nlb-ssl-connect 2026-06-25 18:36:07 -@@ -0,0 +1,109 @@ -+#!/bin/sh -+# Validate NLB SSL tunnel response detection (issue #526 format) -+set -eu -+cd "$(dirname "$0")/.." -+CC="${CC:-cc}" -+TMP=$(mktemp -d) -+trap 'rm -rf "$TMP"' EXIT -+ -+cat > "$TMP/test.c" <<'EOF' -+#include -+#include -+#include -+#include -+#include -+ -+struct gp_nlb_config { -+ char *tunnel_vip; -+ char *inner_gw_ip; -+ char *in_tunnel_gw_cert_chksum; -+ int enabled; -+}; -+ -+#define GP_NLB_SSL_PROTO_VERSION 110 -+ -+static int gp_nlb_is_ipv4(const char *s) -+{ -+ struct in_addr a; -+ return s && inet_pton(AF_INET, s, &a) == 1; -+} -+ -+static int gp_nlb_split_concat_ips(const char *concat, const char *hint_vip, -+ char **vip, char **inner) -+{ -+ size_t vip_len; -+ -+ if (!concat || !*concat) -+ return -1; -+ -+ if (hint_vip && !strncmp(concat, hint_vip, strlen(hint_vip))) { -+ vip_len = strlen(hint_vip); -+ if (concat[vip_len] && gp_nlb_is_ipv4(concat + vip_len)) { -+ *vip = strdup(hint_vip); -+ *inner = strdup(concat + vip_len); -+ return 0; -+ } -+ } -+ return -1; -+} -+ -+static int gpst_nlb_parse_ssl_response(const char *buf, int len, struct gp_nlb_config *nlb) -+{ -+ char *line, *vip = NULL, *inner = NULL, *hash, *start, *concat; -+ const char *hint_vip; -+ long version; -+ -+ line = strndup(buf, len); -+ start = strstr(line, " START_TUNNEL"); -+ if (!start) -+ return -1; -+ *start = '\0'; -+ hash = strrchr(line, ' '); -+ if (!hash) -+ return -1; -+ *hash++ = '\0'; -+ if (sscanf(line, "%ld", &version) != 1 || version != GP_NLB_SSL_PROTO_VERSION) -+ return -1; -+ concat = strchr(line, ' ') + 1; -+ hint_vip = "127.127.127.127"; -+ if (gp_nlb_split_concat_ips(concat, hint_vip, &vip, &inner)) -+ return -1; -+ nlb->tunnel_vip = vip; -+ nlb->inner_gw_ip = inner; -+ nlb->in_tunnel_gw_cert_chksum = strdup(hash); -+ nlb->enabled = 1; -+ return 0; -+} -+ -+static int gpst_nlb_handle_ssl_connect_response(const char *buf, int len, struct gp_nlb_config *nlb) -+{ -+ static const char start_tunnel[] = "START_TUNNEL"; -+ -+ if (!buf || len <= 0) -+ return -1; -+ if (len >= 12 && !strncmp(buf, start_tunnel, 12)) -+ return 0; -+ if (!strstr(buf, start_tunnel)) -+ return -1; -+ return gpst_nlb_parse_ssl_response(buf, len, nlb); -+} -+ -+int main(void) -+{ -+ struct gp_nlb_config nlb = {0}; -+ const char *sample = "110 127.127.127.127192.168.255.43 c057fc7f7c6b74f9ac68b639bb056732404cbacf START_TUNNEL"; -+ const char *plain = "START_TUNNEL"; -+ -+ if (gpst_nlb_handle_ssl_connect_response(plain, strlen(plain), &nlb)) -+ return 1; -+ if (gpst_nlb_handle_ssl_connect_response(sample, strlen(sample), &nlb)) -+ return 1; -+ if (strcmp(nlb.tunnel_vip, "127.127.127.127") || -+ strcmp(nlb.inner_gw_ip, "192.168.255.43")) -+ return 1; -+ printf("gp-nlb-ssl-connect: OK\n"); -+ return 0; -+} -+EOF -+$CC -o "$TMP/test" "$TMP/test.c" -+"$TMP/test" -\ No newline at end of file diff --git a/crates/openconnect/deps/patches/0011-Implement-GlobalProtect-NLB-lifecycle.patch b/crates/openconnect/deps/patches/0011-Implement-GlobalProtect-NLB-lifecycle.patch deleted file mode 100644 index 7bf2f2c..0000000 --- a/crates/openconnect/deps/patches/0011-Implement-GlobalProtect-NLB-lifecycle.patch +++ /dev/null @@ -1,307 +0,0 @@ -From 0000000000000000000000000000000000000005 Mon Sep 17 00:00:00 2001 -From: Kevin Yue -Date: Wed, 25 Jun 2026 21:00:00 +0800 -Subject: [PATCH 11/11] Implement GlobalProtect NLB lifecycle - -Refresh tunnel-opaque via a targeted getconfig.esp when valid-period -expires, and send periodic NLB ESP keepalives during established sessions. ---- ---- a/gpst.c 2026-06-25 18:39:37 -+++ b/gpst.c 2026-06-25 18:40:12 -@@ -699,19 +699,12 @@ - return ret; - } - --static int gpst_get_config(struct openconnect_info *vpninfo) -+static int gpst_build_getconfig_request(struct openconnect_info *vpninfo, -+ struct oc_text_buf *request_body) - { -- char *orig_path; -- int result; -- struct oc_text_buf *request_body = buf_alloc(); - const char *old_addr = vpninfo->ip_info.addr; - const char *old_addr6 = vpninfo->ip_info.addr6; -- char *xml_buf = NULL; -- -- -- /* submit getconfig request */ - const char *app_version = openconnect_get_gp_app_version(vpninfo); -- vpn_progress(vpninfo, PRG_DEBUG, _("Using GlobalProtect app version: %s\n"), app_version); - - buf_append(request_body, "client-type=1&protocol-version=p1&internal=no"); - append_opt(request_body, "app-version", app_version); -@@ -730,8 +723,99 @@ - } else - buf_append(request_body, "&%s", vpninfo->cookie); - filter_opts(request_body, vpninfo->cookie, "serialno,clientgpversion,host-id", 1); -- if ((result = buf_error(request_body))) -+ return buf_error(request_body); -+} -+ -+static int gpst_parse_nlb_opaque_xml(struct openconnect_info *vpninfo, xmlNode *xml_node, void *cb_data) -+{ -+ char *s = NULL; -+ -+ for (xml_node = xml_node->children; xml_node; xml_node = xml_node->next) { -+ if (xmlnode_is_named(xml_node, "hs-key")) { -+#ifdef HAVE_ESP -+ int keylen = xml_to_key(xml_node, vpninfo->gp_nlb.hs_key, sizeof(vpninfo->gp_nlb.hs_key)); -+ -+ if (keylen > 0) { -+ vpninfo->gp_nlb.hs_key_len = keylen; -+ vpninfo->gp_nlb.hs_key_bits = keylen * 8; -+ vpninfo->gp_nlb.enabled = 1; -+ } -+#endif /* HAVE_ESP */ -+ } else if (!xmlnode_get_val(xml_node, "enc-hs-key", &s)) { -+ free(vpninfo->gp_nlb.enc_hs_key); -+ vpninfo->gp_nlb.enc_hs_key = s; -+ s = NULL; -+ vpninfo->gp_nlb.enabled = 1; -+ } else if (xmlnode_is_named(xml_node, "tunnel-opaque")) { -+ if (!xmlnode_get_val(xml_node, "val", &s)) { -+ free(vpninfo->gp_nlb.tunnel_opaque); -+ vpninfo->gp_nlb.tunnel_opaque = s; -+ s = NULL; -+ vpninfo->gp_nlb.enabled = 1; -+ } -+ if (!xmlnode_get_val(xml_node, "valid-period", &s)) -+ vpninfo->gp_nlb.opaque_valid_until = time(NULL) + atol(s); -+ } else if (!xmlnode_get_val(xml_node, "valid-period", &s)) { -+ vpninfo->gp_nlb.opaque_valid_until = time(NULL) + atol(s); -+ } -+ free(s); -+ s = NULL; -+ } -+ return 0; -+} -+ -+int gpst_nlb_refresh_opaque(struct openconnect_info *vpninfo) -+{ -+ char *orig_path, *xml_buf = NULL; -+ struct oc_text_buf *request_body = buf_alloc(); -+ int result; -+ -+ if (!vpninfo->gp_nlb.enabled) -+ return 0; -+ -+ if (gpst_build_getconfig_request(vpninfo, request_body)) { -+ buf_free(request_body); -+ return -ENOMEM; -+ } -+ -+ orig_path = vpninfo->urlpath; -+ vpninfo->urlpath = strdup("ssl-vpn/getconfig.esp"); -+ result = do_https_request(vpninfo, "POST", "application/x-www-form-urlencoded", -+ request_body, &xml_buf, NULL, HTTP_NO_FLAGS); -+ free(vpninfo->urlpath); -+ vpninfo->urlpath = orig_path; -+ buf_free(request_body); -+ -+ if (result < 0) -+ return result; -+ -+ if (result >= 0 && xml_buf) -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("GlobalProtect NLB opaque refresh response:\n%s\n"), -+ xml_buf); -+ -+ result = gpst_xml_or_error(vpninfo, xml_buf, gpst_parse_nlb_opaque_xml, NULL, NULL); -+ free(xml_buf); -+ if (result) -+ return result; -+ -+ return gpst_nlb_prepare(vpninfo); -+} -+ -+static int gpst_get_config(struct openconnect_info *vpninfo) -+{ -+ char *orig_path; -+ int result; -+ struct oc_text_buf *request_body = buf_alloc(); -+ char *xml_buf = NULL; -+ -+ vpn_progress(vpninfo, PRG_DEBUG, _("Using GlobalProtect app version: %s\n"), -+ openconnect_get_gp_app_version(vpninfo)); -+ -+ if (gpst_build_getconfig_request(vpninfo, request_body)) { -+ result = -ENOMEM; - goto out; -+ } - - orig_path = vpninfo->urlpath; - vpninfo->urlpath = strdup("ssl-vpn/getconfig.esp"); -@@ -1239,6 +1323,8 @@ - /* fall through */ - case DTLS_ESTABLISHED: - /* Rekey or check-and-resubmit HIP if needed */ -+ if (gpst_nlb_maintenance(vpninfo, timeout) > 0) -+ return 1; - if (keepalive_action(&vpninfo->ssl_times, timeout) == KA_REKEY) - goto do_rekey; - else if (trojan_check_deadline(vpninfo, timeout)) -@@ -1269,6 +1355,9 @@ - ; - } - -+ if (gpst_nlb_maintenance(vpninfo, timeout) > 0) -+ work_done = 1; -+ - if (vpninfo->ssl_fd == -1) - goto do_reconnect; - ---- a/gpst_nlb.c 2026-06-25 18:39:37 -+++ b/gpst_nlb.c 2026-06-25 18:40:26 -@@ -69,6 +69,7 @@ - "\x10\x11\x12\x13\x14\x15\x16\x17"; - - #define GP_NLB_SSL_PROTO_VERSION 110 -+#define GP_NLB_OPAQUE_REFRESH_MARGIN 60 - - static int gp_nlb_is_ipv4(const char *s) - { -@@ -469,8 +470,39 @@ - { - gpst_nlb_update_esp_magic(vpninfo); - return gpst_nlb_apply_routes(vpninfo); -+} -+ -+int gpst_nlb_opaque_due(struct openconnect_info *vpninfo, int *timeout) -+{ -+ struct gp_nlb_config *nlb = &vpninfo->gp_nlb; -+ time_t now = time(NULL); -+ time_t due; -+ -+ if (!nlb->enabled || !nlb->tunnel_opaque || !nlb->opaque_valid_until) -+ return 0; -+ -+ due = nlb->opaque_valid_until; -+ if (due > GP_NLB_OPAQUE_REFRESH_MARGIN) -+ due -= GP_NLB_OPAQUE_REFRESH_MARGIN; -+ -+ return ka_check_deadline(timeout, now, due); - } - -+int gpst_nlb_maintenance(struct openconnect_info *vpninfo, int *timeout) -+{ -+ if (!gpst_nlb_opaque_due(vpninfo, timeout)) -+ return 0; -+ -+ vpn_progress(vpninfo, PRG_INFO, _("NLB tunnel-opaque refresh due\n")); -+ if (gpst_nlb_refresh_opaque(vpninfo) < 0) { -+ vpn_progress(vpninfo, PRG_ERR, _("NLB tunnel-opaque refresh failed\n")); -+ return -1; -+ } -+ -+ vpninfo->gp_nlb.keepalive_sent = 0; -+ return 1; -+} -+ - int gpst_nlb_handle_ssl_connect_response(struct openconnect_info *vpninfo, - const char *buf, int len) - { ---- a/esp.c 2026-06-25 18:39:37 -+++ b/esp.c 2026-06-25 18:40:26 -@@ -327,9 +327,12 @@ - - vpninfo->dtls_times.last_tx = now; - } -+ -+ if (vpninfo->proto->proto == PROTO_GPST && gpst_nlb_maintenance(vpninfo, timeout) > 0) -+ work_done = 1; - - if (vpninfo->dtls_state != DTLS_ESTABLISHED) -- return 0; -+ return work_done; - - switch (keepalive_action(&vpninfo->dtls_times, timeout)) { - case KA_REKEY: -@@ -350,7 +353,13 @@ - break; - - case KA_KEEPALIVE: -- vpn_progress(vpninfo, PRG_ERR, _("Keepalive not implemented for ESP\n")); -+ if (vpninfo->proto->proto == PROTO_GPST && vpninfo->gp_nlb.enabled) { -+ if (gpst_nlb_send_esp_keepalive(vpninfo) >= 0) -+ vpninfo->dtls_times.last_tx = time(NULL); -+ work_done = 1; -+ } else { -+ vpn_progress(vpninfo, PRG_ERR, _("Keepalive not implemented for ESP\n")); -+ } - break; - - case KA_NONE: ---- a/openconnect-internal.h 2026-06-25 18:39:37 -+++ b/openconnect-internal.h 2026-06-25 18:40:26 -@@ -1434,6 +1434,9 @@ - int gpst_nlb_on_ssl_tunnel(struct openconnect_info *vpninfo); - int gpst_nlb_handle_ssl_connect_response(struct openconnect_info *vpninfo, - const char *buf, int len); -+int gpst_nlb_refresh_opaque(struct openconnect_info *vpninfo); -+int gpst_nlb_opaque_due(struct openconnect_info *vpninfo, int *timeout); -+int gpst_nlb_maintenance(struct openconnect_info *vpninfo, int *timeout); - int gpst_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable); - int gpst_esp_send_probes(struct openconnect_info *vpninfo); - int gpst_esp_catch_probe(struct openconnect_info *vpninfo, struct pkt *pkt); ---- a/tests/gp-nlb-opaque-due 2026-06-25 18:40:45 -+++ b/tests/gp-nlb-opaque-due 2026-06-25 18:40:37 -@@ -0,0 +1,58 @@ -+#!/bin/sh -+# Validate NLB opaque refresh deadline calculation -+set -eu -+cd "$(dirname "$0")/.." -+CC="${CC:-cc}" -+TMP=$(mktemp -d) -+trap 'rm -rf "$TMP"' EXIT -+ -+cat > "$TMP/test.c" <<'EOF' -+#include -+#include -+ -+#define GP_NLB_OPAQUE_REFRESH_MARGIN 60 -+ -+static int ka_check_deadline(int *timeout, time_t now, time_t due) -+{ -+ if (now >= due) -+ return 1; -+ if (*timeout > (due - now) * 1000) -+ *timeout = (due - now) * 1000; -+ return 0; -+} -+ -+static int gpst_nlb_opaque_due(int enabled, time_t valid_until, int *timeout) -+{ -+ time_t now = time(NULL); -+ time_t due; -+ -+ if (!enabled || !valid_until) -+ return 0; -+ -+ due = valid_until; -+ if (due > GP_NLB_OPAQUE_REFRESH_MARGIN) -+ due -= GP_NLB_OPAQUE_REFRESH_MARGIN; -+ -+ return ka_check_deadline(timeout, now, due); -+} -+ -+int main(void) -+{ -+ int timeout = 999999; -+ time_t now = time(NULL); -+ -+ if (gpst_nlb_opaque_due(0, now + 3600, &timeout)) -+ return 1; -+ if (gpst_nlb_opaque_due(1, 0, &timeout)) -+ return 1; -+ if (!gpst_nlb_opaque_due(1, now + 30, &timeout)) -+ return 1; -+ timeout = 999999; -+ if (gpst_nlb_opaque_due(1, now + 3600, &timeout)) -+ return 1; -+ printf("gp-nlb-opaque-due: OK\n"); -+ return 0; -+} -+EOF -+$CC -o "$TMP/test" "$TMP/test.c" -+"$TMP/test" -\ No newline at end of file diff --git a/crates/openconnect/deps/patches/0012-Add-GlobalProtect-NLB-integration-and-GCM-recognition.patch b/crates/openconnect/deps/patches/0012-Add-GlobalProtect-NLB-integration-and-GCM-recognition.patch deleted file mode 100644 index 5861346..0000000 --- a/crates/openconnect/deps/patches/0012-Add-GlobalProtect-NLB-integration-and-GCM-recognition.patch +++ /dev/null @@ -1,177 +0,0 @@ -From 0000000000000000000000000000000000000006 Mon Sep 17 00:00:00 2001 -From: Kevin Yue -Date: Wed, 25 Jun 2026 22:00:00 +0800 -Subject: [PATCH 12/19] Add GlobalProtect NLB integration and GCM recognition - -Recognize AES-GCM ESP algorithm strings, extend fake-gp-server with NLB -mode, wire NLB unit/integration tests into Makefile.am, and add an -integration test that verifies NLB config parsing from getconfig.esp. ---- -diff --git a/gpst.c b/gpst.c -index ae3c746..4cdbfa6 100644 ---- a/gpst.c -+++ b/gpst.c -@@ -369,6 +369,8 @@ static int check_enc_algo(struct openconnect_info *v, const char *s) - { - if (!strcmp(s, "aes128") || !strcmp(s, "aes-128-cbc")) return ENC_AES_128_CBC; - if (!strcmp(s, "aes-256-cbc")) return ENC_AES_256_CBC; -+ if (!strcmp(s, "aes-128-gcm")) return ENC_AES_128_GCM; -+ if (!strcmp(s, "aes-256-gcm")) return ENC_AES_256_GCM; - vpn_progress(v, PRG_ERR, _("Unknown ESP encryption algorithm: %s\n"), s); - return -ENOENT; - } -diff --git a/openconnect-internal.h b/openconnect-internal.h -index df79066..6148d26 100644 ---- a/openconnect-internal.h -+++ b/openconnect-internal.h -@@ -1103,6 +1103,8 @@ static inline void __monitor_fd_new(struct openconnect_info *vpninfo, - /* Encryption and HMAC algorithms (matching Juniper/Pulse binary encoding) */ - #define ENC_AES_128_CBC 2 - #define ENC_AES_256_CBC 5 -+#define ENC_AES_128_GCM 20 -+#define ENC_AES_256_GCM 22 - - #define HMAC_MD5 1 - #define HMAC_SHA1 2 -diff --git a/tests/Makefile.am b/tests/Makefile.am -index 823928d..8b3cbee 100644 ---- a/tests/Makefile.am -+++ b/tests/Makefile.am -@@ -87,7 +87,10 @@ endif - dist_check_SCRIPTS += auth-username-pass auth-certificate auth-nonascii cert-fingerprint \ - id-test obsolete-server-crypto pfs auth-swtpm auth-hwtpm fortinet-auth-and-config \ - f5-auth-and-config juniper-auth juniper-sso-auth gp-auth-and-config auth-pkcs11 \ -- auth-multicert -+ auth-multicert gp-nlb-ssl-parse gp-nlb-esp-envelope gp-nlb-ssl-connect \ -+ gp-nlb-opaque-due gp-nlb-auth-and-config -+ -+TESTS += gp-nlb-ssl-parse gp-nlb-esp-envelope gp-nlb-ssl-connect gp-nlb-opaque-due - - if HAVE_CWRAP - TESTS += auth-username-pass auth-certificate auth-nonascii cert-fingerprint id-test \ -@@ -110,7 +113,8 @@ endif - if HAVE_PYTHON36_FLASK - TESTS += juniper-sso-auth auth-multicert - if HAVE_PYTHON37_DATACLASSES --TESTS += juniper-auth gp-auth-and-config f5-auth-and-config fortinet-auth-and-config -+TESTS += juniper-auth gp-auth-and-config f5-auth-and-config fortinet-auth-and-config \ -+ gp-nlb-auth-and-config - endif - endif - -diff --git a/tests/fake-gp-server.py b/tests/fake-gp-server.py -index 908f91c..76a2af4 100755 ---- a/tests/fake-gp-server.py -+++ b/tests/fake-gp-server.py -@@ -96,6 +96,7 @@ class TestConfiguration: - saml_comments_only: int = None - saml_needs_js: int = None - esp: bool = True -+ nlb: bool = False - C = TestConfiguration() - OUTSTANDING_SAML_TOKENS = set() - -@@ -104,7 +105,7 @@ OUTSTANDING_SAML_TOKENS = set() - def configure(): - global C - if request.method == 'POST': -- gateways, portal_2fa, gw_2fa, portal_cookie, portal_saml, gateway_saml, saml_comments_only, saml_needs_js, esp = request.form.get('gateways'), request.form.get('portal_2fa'), request.form.get('gw_2fa'), request.form.get('portal_cookie'), request.form.get('portal_saml'), request.form.get('gateway_saml'), request.form.get('saml_comments_only'), request.form.get('saml_needs_js'), request.form.get('esp') -+ gateways, portal_2fa, gw_2fa, portal_cookie, portal_saml, gateway_saml, saml_comments_only, saml_needs_js, esp, nlb = request.form.get('gateways'), request.form.get('portal_2fa'), request.form.get('gw_2fa'), request.form.get('portal_cookie'), request.form.get('portal_saml'), request.form.get('gateway_saml'), request.form.get('saml_comments_only'), request.form.get('saml_needs_js'), request.form.get('esp'), request.form.get('nlb') - C.gateways = gateways.split(',') if gateways else ('Default gateway',) - C.portal_cookie = portal_cookie - C.portal_2fa = portal_2fa and portal_2fa.strip().lower() -@@ -114,6 +115,7 @@ def configure(): - C.saml_comments_only = int(saml_comments_only) if saml_comments_only else None - C.saml_needs_js = int(saml_needs_js) if saml_needs_js else None - C.esp = int(esp) if esp else None -+ C.nlb = bool(int(nlb)) if nlb else False - return '', 201 - else: - return 'Current configuration of fake GP server configuration:\n{}\n'.format(C) -@@ -342,7 +344,14 @@ def gateway_login(): - def getconfig(): - session.update(step='gateway-config') - addrs = '{}'.format(session['preferred_ip']) -- addrs += '127.0.0.1' -+ if C.nlb: -+ addrs += '127.127.127.127' -+ addrs += '10.1.2.3' -+ addrs += '''2560123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef -+fake-enc-hs-key-blob -+dGVzdC10dW5uZWwtb3BhcXVl3600''' -+ else: -+ addrs += '127.0.0.1' - addrs += '::1' # some(?) servers send the IPv6 address even if not otherwise configured for IPv6 - if session['ipv6_support'] == 'yes': - addrs += '{}'.format(session['preferred_ipv6']) -@@ -380,6 +389,9 @@ def hipcheck(): - def tunnel(): - assert 'user' in request.args and 'authcookie' in request.args - session.update(step='GET-tunnel') -+ if C.nlb: -+ inner = '192.168.255.43' -+ return '110 127.127.127.127{} c057fc7f7c6b74f9ac68b639bb056732404cbacf START_TUNNEL'.format(inner) - abort(502) - - -diff --git a/tests/gp-nlb-auth-and-config b/tests/gp-nlb-auth-and-config -new file mode 100755 -index 0000000..4ed2c09 ---- /dev/null -+++ b/tests/gp-nlb-auth-and-config -@@ -0,0 +1,48 @@ -+#!/bin/sh -+# -+# Validate GlobalProtect NLB config parsing and SSL tunnel handshake against -+# fake-gp-server NLB mode (issue #526 sample response). -+PRELOAD=1 -+srcdir=${srcdir:-.} -+top_builddir=${top_builddir:-..} -+ -+. `dirname $0`/common.sh -+ -+FINGERPRINT="--servercert=pin-sha256:xp3scfzy3rO" -+CERT=$certdir/server-cert.pem -+KEY=$certdir/server-key.pem -+ -+echo "Testing GlobalProtect NLB auth/config against fake server ..." -+ -+OCSERV=${srcdir}/fake-gp-server.py -+launch_simple_sr_server $ADDRESS 443 $CERT $KEY >/dev/null 2>&1 -+PID=$! -+wait_server $PID -+ -+SERVURL="https://$ADDRESS:443" -+CLIENT="$OPENCONNECT --protocol=gp $FINGERPRINT -u test" -+export LD_PRELOAD=libsocket_wrapper.so -+ -+echo "Configuring fake server for NLB gateway getconfig." -+curl -sk $SERVURL/CONFIGURE -d nlb=1 -+ -+echo -n "Authenticating with username/password via portal (NLB mode)... " -+( echo "test" | $CLIENT $SERVURL/portal --cookieonly >/dev/null 2>&1) || -+ fail $PID "Could not receive cookie from fake GlobalProtect NLB server" -+ -+echo ok -+ -+echo -n "Verifying NLB fields are parsed from getconfig.esp... " -+LOG="$SOCKDIR/nlb-log.$$.tmp" -+( echo "test" | timeout 20 $CLIENT --no-dtls $SERVURL/portal >"$LOG" 2>&1) || true -+grep -q 'NLB enabled' "$LOG" || -+ fail $PID "Expected NLB enabled message in openconnect output" -+ -+echo ok -+ -+echo "Resetting fake server to default configuration." -+curl -sk $SERVURL/CONFIGURE -d '' -+ -+cleanup -+ -+exit 0 --- - gpst.c | 2 ++ - openconnect-internal.h | 2 ++ - tests/Makefile.am | 8 +++++-- - tests/fake-gp-server.py | 18 ++++++++++++--- - tests/gp-nlb-auth-and-config | 48 +++++++++++++++++++++++++++++++++++++ - 5 files changed, 74 insertions(+), 4 deletions(-) \ No newline at end of file diff --git a/crates/openconnect/deps/patches/0013-Implement-ESP-GCM-and-NLB-enc-hs-key-handling.patch b/crates/openconnect/deps/patches/0013-Implement-ESP-GCM-and-NLB-enc-hs-key-handling.patch deleted file mode 100644 index 0c76ec7..0000000 --- a/crates/openconnect/deps/patches/0013-Implement-ESP-GCM-and-NLB-enc-hs-key-handling.patch +++ /dev/null @@ -1,1324 +0,0 @@ -From 0000000000000000000000000000000000000007 Mon Sep 17 00:00:00 2001 -From: Kevin Yue -Date: Wed, 25 Jun 2026 23:00:00 +0800 -Subject: [PATCH 13/13] Implement ESP GCM and NLB enc-hs-key handling - -Add RFC4106 AES-GCM ESP encrypt/decrypt for GlobalProtect gateways, -recognize aes128gcm8/12/16 ICV variants, and implement NLB enc-hs-key -blob parsing with TLS 1.2 session-key decipherment for tunnel-opaque. ---- -diff --git a/esp.c b/esp.c -index 87d26d7..c0eb14a 100644 ---- a/esp.c -+++ b/esp.c -@@ -42,18 +42,11 @@ int print_esp_keys(struct openconnect_info *vpninfo, const char *name, struct es - case ENC_AES_256_CBC: - enctype = "AES-256-CBC (RFC3602)"; - break; -- default: -- return -EINVAL; -- } -- switch (vpninfo->esp_hmac) { -- case HMAC_MD5: -- mactype = "HMAC-MD5-96 (RFC2403)"; -- break; -- case HMAC_SHA1: -- mactype = "HMAC-SHA-1-96 (RFC2404)"; -+ case ENC_AES_128_GCM: -+ enctype = "AES-128-GCM (RFC4106)"; - break; -- case HMAC_SHA256: -- mactype = "HMAC-SHA-256-128 (RFC4868)"; -+ case ENC_AES_256_GCM: -+ enctype = "AES-256-GCM (RFC4106)"; - break; - default: - return -EINVAL; -@@ -61,8 +54,6 @@ int print_esp_keys(struct openconnect_info *vpninfo, const char *name, struct es - - for (i = 0; i < vpninfo->enc_key_len; i++) - sprintf(enckey + (2 * i), "%02x", esp->enc_key[i]); -- for (i = 0; i < vpninfo->hmac_key_len; i++) -- sprintf(mackey + (2 * i), "%02x", esp->hmac_key[i]); - - vpn_progress(vpninfo, PRG_TRACE, - _("Parameters for %s ESP: SPI 0x%08x\n"), -@@ -70,9 +61,30 @@ int print_esp_keys(struct openconnect_info *vpninfo, const char *name, struct es - vpn_progress(vpninfo, PRG_TRACE, - _("ESP encryption type %s key 0x%s\n"), - enctype, enckey); -- vpn_progress(vpninfo, PRG_TRACE, -- _("ESP authentication type %s key 0x%s\n"), -- mactype, mackey); -+ if (esp_uses_gcm(vpninfo)) { -+ vpn_progress(vpninfo, PRG_TRACE, -+ _("ESP GCM ICV length %d bytes\n"), -+ vpninfo->hmac_out_len); -+ } else { -+ switch (vpninfo->esp_hmac) { -+ case HMAC_MD5: -+ mactype = "HMAC-MD5-96 (RFC2403)"; -+ break; -+ case HMAC_SHA1: -+ mactype = "HMAC-SHA-1-96 (RFC2404)"; -+ break; -+ case HMAC_SHA256: -+ mactype = "HMAC-SHA-256-128 (RFC4868)"; -+ break; -+ default: -+ return -EINVAL; -+ } -+ for (i = 0; i < vpninfo->hmac_key_len; i++) -+ sprintf(mackey + (2 * i), "%02x", esp->hmac_key[i]); -+ vpn_progress(vpninfo, PRG_TRACE, -+ _("ESP authentication type %s key 0x%s\n"), -+ mactype, mackey); -+ } - return 0; - } - -@@ -103,7 +115,7 @@ int esp_setup(struct openconnect_info *vpninfo) - int construct_esp_packet(struct openconnect_info *vpninfo, struct pkt *pkt, uint8_t next_hdr) - { - const int blksize = 16; -- int i, padlen, ret; -+ int i, padlen, ret, crypt_len; - - if (!next_hdr) { - if ((pkt->data[0] & 0xf0) == 0x60) /* iph->ip_v */ -@@ -112,23 +124,32 @@ int construct_esp_packet(struct openconnect_info *vpninfo, struct pkt *pkt, uint - next_hdr = IPPROTO_IPIP; - } - -- /* This gets much more fun if the IV is variable-length */ - pkt->esp.spi = vpninfo->esp_out.spi; - pkt->esp.seq = htonl(vpninfo->esp_out.seq++); - -- padlen = blksize - 1 - ((pkt->len + 1) % blksize); -- for (i=0; idata[pkt->len + i] = i + 1; -- pkt->data[pkt->len + padlen] = padlen; -- pkt->data[pkt->len + padlen + 1] = next_hdr; -- -- memcpy(pkt->esp.iv, vpninfo->esp_out.iv, sizeof(pkt->esp.iv)); -+ if (esp_uses_gcm(vpninfo)) { -+ pkt->data[pkt->len] = next_hdr; -+ crypt_len = pkt->len + 1; -+ if (openconnect_random(pkt->esp.iv, ESP_GCM_IV_LEN)) { -+ vpn_progress(vpninfo, PRG_ERR, -+ _("Failed to generate ESP GCM IV\n")); -+ return -EIO; -+ } -+ } else { -+ padlen = blksize - 1 - ((pkt->len + 1) % blksize); -+ for (i=0; idata[pkt->len + i] = i + 1; -+ pkt->data[pkt->len + padlen] = padlen; -+ pkt->data[pkt->len + padlen + 1] = next_hdr; -+ crypt_len = pkt->len + padlen + 2; -+ memcpy(pkt->esp.iv, vpninfo->esp_out.iv, sizeof(pkt->esp.iv)); -+ } - -- ret = encrypt_esp_packet(vpninfo, pkt, pkt->len + padlen + 2); -+ ret = encrypt_esp_packet(vpninfo, pkt, crypt_len); - if (ret) - return ret; - -- return sizeof(pkt->esp) + pkt->len + padlen + 2 + vpninfo->hmac_out_len; -+ return sizeof(pkt->esp) + crypt_len + vpninfo->hmac_out_len; - } - - int esp_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable) -@@ -239,22 +260,30 @@ int esp_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable) - continue; - } - -- if (len <= 2 + pkt->data[len - 2]) { -- vpn_progress(vpninfo, PRG_ERR, -- _("Invalid padding length %02x in ESP\n"), -- pkt->data[len - 2]); -- continue; -- } -- pkt->len = len - 2 - pkt->data[len - 2]; -- for (i = 0 ; i < pkt->data[len - 2]; i++) { -- if (pkt->data[pkt->len + i] != i + 1) -- break; /* We can't just 'continue' here because it -- * would only break out of this 'for' loop */ -- } -- if (i != pkt->data[len - 2]) { -- vpn_progress(vpninfo, PRG_ERR, -- _("Invalid padding bytes in ESP\n")); -- continue; /* We can here, though */ -+ if (esp_uses_gcm(vpninfo)) { -+ if (len < 1) { -+ vpn_progress(vpninfo, PRG_ERR, -+ _("Short ESP GCM packet\n")); -+ continue; -+ } -+ pkt->len = len - 1; -+ } else { -+ if (len <= 2 + pkt->data[len - 2]) { -+ vpn_progress(vpninfo, PRG_ERR, -+ _("Invalid padding length %02x in ESP\n"), -+ pkt->data[len - 2]); -+ continue; -+ } -+ pkt->len = len - 2 - pkt->data[len - 2]; -+ for (i = 0 ; i < pkt->data[len - 2]; i++) { -+ if (pkt->data[pkt->len + i] != i + 1) -+ break; -+ } -+ if (i != pkt->data[len - 2]) { -+ vpn_progress(vpninfo, PRG_ERR, -+ _("Invalid padding bytes in ESP\n")); -+ continue; -+ } - } - vpninfo->dtls_times.last_rx = time(NULL); - -@@ -505,7 +534,11 @@ int openconnect_setup_esp_keys(struct openconnect_info *vpninfo, int new_keys) - if (!vpninfo->dtls_addr) - return -EINVAL; - -- if (vpninfo->esp_hmac == HMAC_SHA256) -+ if (esp_uses_gcm(vpninfo)) { -+ if (!vpninfo->esp_gcm_icv) -+ vpninfo->esp_gcm_icv = ESP_GCM_ICV_DEFAULT; -+ vpninfo->hmac_out_len = vpninfo->esp_gcm_icv; -+ } else if (vpninfo->esp_hmac == HMAC_SHA256) - vpninfo->hmac_out_len = 16; - else /* MD5 and SHA1 */ - vpninfo->hmac_out_len = 12; -diff --git a/gnutls-esp.c b/gnutls-esp.c -index e350ff7..49f7002 100644 ---- a/gnutls-esp.c -+++ b/gnutls-esp.c -@@ -41,13 +41,17 @@ void destroy_esp_ciphers(struct esp *esp) - } - - static int init_esp_cipher(struct openconnect_info *vpninfo, struct esp *esp, -- gnutls_mac_algorithm_t macalg, gnutls_cipher_algorithm_t encalg) -+ gnutls_mac_algorithm_t macalg, gnutls_cipher_algorithm_t encalg, -+ int gcm) - { - gnutls_datum_t enc_key; - int err; - - destroy_esp_ciphers(esp); - -+ if (gcm) -+ return 0; -+ - enc_key.size = gnutls_cipher_get_key_size(encalg); - enc_key.data = esp->enc_key; - -@@ -71,10 +75,64 @@ static int init_esp_cipher(struct openconnect_info *vpninfo, struct esp *esp, - return 0; - } - -+static gnutls_cipher_algorithm_t esp_gcm_cipher(const struct openconnect_info *vpninfo) -+{ -+ return vpninfo->esp_enc == ENC_AES_256_GCM ? -+ GNUTLS_CIPHER_AES_256_GCM : GNUTLS_CIPHER_AES_128_GCM; -+} -+ -+static void esp_gcm_nonce(unsigned char *nonce, const unsigned char *iv, uint32_t seq) -+{ -+ memcpy(nonce, iv, ESP_GCM_IV_LEN); -+ memcpy(nonce + ESP_GCM_IV_LEN, &seq, 4); -+} -+ -+static int esp_gcm_init(struct openconnect_info *vpninfo, gnutls_cipher_hd_t *hd, -+ const unsigned char *key, const unsigned char *iv, uint32_t seq) -+{ -+ gnutls_datum_t enc_key, iv_d; -+ unsigned char nonce[12]; -+ gnutls_cipher_algorithm_t encalg = esp_gcm_cipher(vpninfo); -+ int err; -+ -+ enc_key.data = (unsigned char *)key; -+ enc_key.size = gnutls_cipher_get_key_size(encalg); -+ esp_gcm_nonce(nonce, iv, seq); -+ iv_d.data = nonce; -+ iv_d.size = sizeof(nonce); -+ -+ err = gnutls_cipher_init(hd, encalg, &enc_key, &iv_d); -+ if (err) { -+ vpn_progress(vpninfo, PRG_ERR, -+ _("Failed to init ESP GCM cipher: %s\n"), -+ gnutls_strerror(err)); -+ return -EIO; -+ } -+ return 0; -+} -+ -+static int esp_gcm_aad(struct openconnect_info *vpninfo, gnutls_cipher_hd_t hd, -+ const struct pkt *pkt) -+{ -+ unsigned char aad[8]; -+ int err; -+ -+ memcpy(aad, &pkt->esp.spi, 8); -+ err = gnutls_cipher_add_auth(hd, aad, sizeof(aad)); -+ if (err) { -+ vpn_progress(vpninfo, PRG_ERR, -+ _("Failed to set ESP GCM AAD: %s\n"), -+ gnutls_strerror(err)); -+ return -EIO; -+ } -+ return 0; -+} -+ - int init_esp_ciphers(struct openconnect_info *vpninfo, struct esp *esp_out, struct esp *esp_in) - { -- gnutls_mac_algorithm_t macalg; -+ gnutls_mac_algorithm_t macalg = 0; - gnutls_cipher_algorithm_t encalg; -+ int gcm = esp_uses_gcm(vpninfo); - int ret; - - switch (vpninfo->esp_enc) { -@@ -84,31 +142,38 @@ int init_esp_ciphers(struct openconnect_info *vpninfo, struct esp *esp_out, stru - case ENC_AES_256_CBC: - encalg = GNUTLS_CIPHER_AES_256_CBC; - break; -+ case ENC_AES_128_GCM: -+ case ENC_AES_256_GCM: -+ encalg = esp_gcm_cipher(vpninfo); -+ break; - default: - return -EINVAL; - } - -- switch (vpninfo->esp_hmac) { -- case HMAC_MD5: -- macalg = GNUTLS_MAC_MD5; -- break; -- case HMAC_SHA1: -- macalg = GNUTLS_MAC_SHA1; -- break; -- case HMAC_SHA256: -- macalg = GNUTLS_MAC_SHA256; -- break; -- default: -- return -EINVAL; -+ if (!gcm) { -+ switch (vpninfo->esp_hmac) { -+ case HMAC_MD5: -+ macalg = GNUTLS_MAC_MD5; -+ break; -+ case HMAC_SHA1: -+ macalg = GNUTLS_MAC_SHA1; -+ break; -+ case HMAC_SHA256: -+ macalg = GNUTLS_MAC_SHA256; -+ break; -+ default: -+ return -EINVAL; -+ } - } - -- ret = init_esp_cipher(vpninfo, esp_out, macalg, encalg); -+ ret = init_esp_cipher(vpninfo, esp_out, macalg, encalg, gcm); - if (ret) - return ret; - -- gnutls_cipher_set_iv(esp_out->cipher, esp_out->iv, sizeof(esp_out->iv)); -+ if (!gcm) -+ gnutls_cipher_set_iv(esp_out->cipher, esp_out->iv, sizeof(esp_out->iv)); - -- ret = init_esp_cipher(vpninfo, esp_in, macalg, encalg); -+ ret = init_esp_cipher(vpninfo, esp_in, macalg, encalg, gcm); - if (ret) { - destroy_esp_ciphers(esp_out); - return ret; -@@ -117,12 +182,51 @@ int init_esp_ciphers(struct openconnect_info *vpninfo, struct esp *esp_out, stru - return 0; - } - --/* pkt->len shall be the *payload* length. Omitting the header and the 12-byte HMAC */ -+/* pkt->len shall be the *payload* length. Omitting the header and the ICV/HMAC */ - int decrypt_esp_packet(struct openconnect_info *vpninfo, struct esp *esp, struct pkt *pkt) - { - unsigned char hmac_buf[MAX_HMAC_SIZE]; -+ gnutls_cipher_hd_t hd = NULL; - int err; - -+ if (verify_packet_seqno(vpninfo, esp, ntohl(pkt->esp.seq))) -+ return -EINVAL; -+ -+ if (esp_uses_gcm(vpninfo)) { -+ if (pkt->len < vpninfo->hmac_out_len) -+ return -EINVAL; -+ -+ if (esp_gcm_init(vpninfo, &hd, esp->enc_key, pkt->esp.iv, pkt->esp.seq)) -+ return -EIO; -+ if (esp_gcm_aad(vpninfo, hd, pkt)) -+ goto gcm_fail; -+ -+ pkt->len -= vpninfo->hmac_out_len; -+ -+ err = gnutls_cipher_decrypt(hd, pkt->data, pkt->len); -+ if (err) { -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("Decrypting ESP GCM packet failed: %s\n"), -+ gnutls_strerror(err)); -+ goto gcm_fail; -+ } -+ -+ err = gnutls_cipher_tag(hd, hmac_buf, vpninfo->hmac_out_len); -+ if (err || -+ memcmp(hmac_buf, pkt->data + pkt->len, vpninfo->hmac_out_len)) { -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("Received ESP GCM packet with invalid ICV\n")); -+ goto gcm_fail; -+ } -+ -+ gnutls_cipher_deinit(hd); -+ return 0; -+ -+ gcm_fail: -+ gnutls_cipher_deinit(hd); -+ return -EINVAL; -+ } -+ - err = gnutls_hmac(esp->hmac, &pkt->esp, sizeof(pkt->esp) + pkt->len); - if (err) { - vpn_progress(vpninfo, PRG_ERR, -@@ -137,9 +241,6 @@ int decrypt_esp_packet(struct openconnect_info *vpninfo, struct esp *esp, struct - return -EINVAL; - } - -- if (verify_packet_seqno(vpninfo, esp, ntohl(pkt->esp.seq))) -- return -EINVAL; -- - gnutls_cipher_set_iv(esp->cipher, pkt->esp.iv, sizeof(pkt->esp.iv)); - - err = gnutls_cipher_decrypt(esp->cipher, pkt->data, pkt->len); -@@ -156,8 +257,40 @@ int decrypt_esp_packet(struct openconnect_info *vpninfo, struct esp *esp, struct - int encrypt_esp_packet(struct openconnect_info *vpninfo, struct pkt *pkt, int crypt_len) - { - const int blksize = 16; -+ gnutls_cipher_hd_t hd = NULL; - int err; - -+ if (esp_uses_gcm(vpninfo)) { -+ if (esp_gcm_init(vpninfo, &hd, vpninfo->esp_out.enc_key, -+ pkt->esp.iv, pkt->esp.seq)) -+ return -EIO; -+ if (esp_gcm_aad(vpninfo, hd, pkt)) -+ goto gcm_enc_fail; -+ -+ err = gnutls_cipher_encrypt(hd, pkt->data, crypt_len); -+ if (err) { -+ vpn_progress(vpninfo, PRG_ERR, -+ _("Failed to encrypt ESP GCM packet: %s\n"), -+ gnutls_strerror(err)); -+ goto gcm_enc_fail; -+ } -+ -+ err = gnutls_cipher_tag(hd, pkt->data + crypt_len, vpninfo->hmac_out_len); -+ if (err) { -+ vpn_progress(vpninfo, PRG_ERR, -+ _("Failed to fetch ESP GCM ICV: %s\n"), -+ gnutls_strerror(err)); -+ goto gcm_enc_fail; -+ } -+ -+ gnutls_cipher_deinit(hd); -+ return 0; -+ -+ gcm_enc_fail: -+ gnutls_cipher_deinit(hd); -+ return -EIO; -+ } -+ - err = gnutls_cipher_encrypt(vpninfo->esp_out.cipher, pkt->data, crypt_len); - if (err) { - vpn_progress(vpninfo, PRG_ERR, -@@ -178,4 +311,4 @@ int encrypt_esp_packet(struct openconnect_info *vpninfo, struct pkt *pkt, int cr - memcpy(vpninfo->esp_out.iv, pkt->data + crypt_len, blksize); - gnutls_cipher_encrypt(vpninfo->esp_out.cipher, vpninfo->esp_out.iv, blksize); - return 0; --} -+} -\ No newline at end of file -diff --git a/gnutls.c b/gnutls.c -index 394a333..b04c581 100644 ---- a/gnutls.c -+++ b/gnutls.c -@@ -3603,3 +3603,80 @@ error: - done: - return ret; - } -+ -+static int gp_tls12_cbc_decrypt(struct openconnect_info *vpninfo, -+ const unsigned char *key, size_t key_size, -+ const unsigned char *iv, size_t iv_size, -+ const unsigned char *in, int inlen, -+ unsigned char *out, int *outlen) -+{ -+ gnutls_cipher_hd_t hd = NULL; -+ gnutls_datum_t enc_key, iv_d; -+ int err; -+ -+ if (inlen <= (int)iv_size || inlen % iv_size) -+ return -EINVAL; -+ -+ enc_key.data = (unsigned char *)key; -+ enc_key.size = key_size; -+ iv_d.data = (unsigned char *)iv; -+ iv_d.size = iv_size; -+ -+ err = gnutls_cipher_init(&hd, key_size == 32 ? GNUTLS_CIPHER_AES_256_CBC : -+ GNUTLS_CIPHER_AES_128_CBC, &enc_key, &iv_d); -+ if (err) -+ return -EINVAL; -+ -+ inlen -= iv_size; -+ memcpy(out, in + iv_size, inlen); -+ *outlen = inlen; -+ err = gnutls_cipher_decrypt(hd, out, inlen); -+ gnutls_cipher_deinit(hd); -+ return err ? -EINVAL : 0; -+} -+ -+int gp_ssl_decrypt_blob(struct openconnect_info *vpninfo, -+ const unsigned char *in, int inlen, -+ unsigned char *out, int *outlen) -+{ -+ gnutls_session_t sess = vpninfo->https_sess; -+ gnutls_cipher_algorithm_t cipher_algo; -+ gnutls_mac_algorithm_t mac_algo; -+ size_t key_size, mac_size, iv_size, needed; -+ unsigned char key_block[128]; -+ const unsigned char *srv_key; -+ int ret; -+ -+ if (!sess || !in || inlen <= 0 || !out || !outlen) -+ return -EINVAL; -+ -+ cipher_algo = gnutls_cipher_get(sess); -+ mac_algo = gnutls_mac_get(sess); -+ key_size = gnutls_cipher_get_key_size(cipher_algo); -+ mac_size = gnutls_hmac_get_len(mac_algo); -+ iv_size = gnutls_cipher_get_iv_size(cipher_algo); -+ -+ if (!key_size || !mac_size || !iv_size) -+ return -EINVAL; -+ -+ needed = 2 * mac_size + 2 * key_size + 2 * iv_size; -+ if (needed > sizeof(key_block)) -+ return -EINVAL; -+ -+ if (gnutls_prf(sess, 13, "key expansion", 1, 0, NULL, needed, -+ (char *)key_block) < 0) -+ return -EINVAL; -+ -+ srv_key = key_block + 2 * mac_size + key_size; -+ ret = gp_tls12_cbc_decrypt(vpninfo, srv_key, key_size, in, iv_size, -+ in, inlen, out, outlen); -+ if (!ret) -+ return 0; -+ -+ /* PanGPS may pass only the ciphertext portion without explicit IV */ -+ if (inlen < (int)iv_size) -+ return -EINVAL; -+ return gp_tls12_cbc_decrypt(vpninfo, srv_key, key_size, -+ key_block + needed - iv_size, iv_size, -+ in, inlen, out, outlen); -+} -diff --git a/gpst.c b/gpst.c -index 4cdbfa6..bcd417c 100644 ---- a/gpst.c -+++ b/gpst.c -@@ -369,8 +369,22 @@ static int check_enc_algo(struct openconnect_info *v, const char *s) - { - if (!strcmp(s, "aes128") || !strcmp(s, "aes-128-cbc")) return ENC_AES_128_CBC; - if (!strcmp(s, "aes-256-cbc")) return ENC_AES_256_CBC; -- if (!strcmp(s, "aes-128-gcm")) return ENC_AES_128_GCM; -- if (!strcmp(s, "aes-256-gcm")) return ENC_AES_256_GCM; -+ if (!strcmp(s, "aes-128-gcm") || !strcmp(s, "aes128gcm16")) { -+ v->esp_gcm_icv = 16; -+ return ENC_AES_128_GCM; -+ } -+ if (!strcmp(s, "aes128gcm12")) { -+ v->esp_gcm_icv = 12; -+ return ENC_AES_128_GCM; -+ } -+ if (!strcmp(s, "aes128gcm8")) { -+ v->esp_gcm_icv = 8; -+ return ENC_AES_128_GCM; -+ } -+ if (!strcmp(s, "aes-256-gcm")) { -+ v->esp_gcm_icv = 16; -+ return ENC_AES_256_GCM; -+ } - vpn_progress(v, PRG_ERR, _("Unknown ESP encryption algorithm: %s\n"), s); - return -ENOENT; - } -@@ -578,7 +592,11 @@ static int gpst_parse_config_xml(struct openconnect_info *vpninfo, xmlNode *xml_ - else if (!xmlnode_get_val(member, "ipsec-mode", &s) && strcmp(s, "esp-tunnel")) - vpn_progress(vpninfo, PRG_ERR, _("GlobalProtect config sent ipsec-mode=%s (expected esp-tunnel)\n"), s); - } -- if (!(vpninfo->esp_enc > 0 && vpninfo->esp_hmac > 0 && vpninfo->enc_key_len > 0 && vpninfo->hmac_key_len > 0)) -+ if (esp_uses_gcm(vpninfo) && !vpninfo->esp_hmac) -+ vpninfo->esp_hmac = HMAC_NONE; -+ if (!(vpninfo->esp_enc > 0 && vpninfo->enc_key_len > 0 && -+ (esp_uses_gcm(vpninfo) || -+ (vpninfo->esp_hmac > 0 && vpninfo->hmac_key_len > 0)))) - vpn_progress(vpninfo, PRG_ERR, "Server's ESP configuration is incomplete or uses unknown algorithms.\n"); - else - esp_keys = 1; -diff --git a/gpst_nlb.c b/gpst_nlb.c -index 9d88fb5..503601f 100644 ---- a/gpst_nlb.c -+++ b/gpst_nlb.c -@@ -45,8 +45,14 @@ - * - getNlbRealGwIp() resolves inner gateway IP - * - "NLB: add an access route for NLB private ip: %s." - * -- * Crypto RE status: enc-hs-key decipherment and opaque_blob HMAC fields are not -- * yet recovered from stripped ELF; the 16-byte envelope wrap/unwrap is implemented. -+ * NLB blob layout (PanGPS 6.3.3.1 RE, shared by enc-hs-key and tunnel-opaque): -+ * 0-15 header prefix (stored; first 16 bytes of enc-hs-key blob) -+ * 16-23 version marker (e.g. e5ac060000000000) -+ * 24-27 tunnel session id (cleartext, matches across blobs from same session) -+ * 28+ encrypted payload (enc-hs-key: 36 bytes; tunnel-opaque: 508 bytes) -+ * -+ * enc-hs-key decipherment uses TLS 1.2 key expansion from the active HTTPS -+ * session (PanGPS do_ssl_decrypt). Decrypted material keys tunnel-opaque body. - */ - - #include -@@ -70,6 +76,11 @@ static const unsigned char gp_nlb_probe_template[56] = - - #define GP_NLB_SSL_PROTO_VERSION 110 - #define GP_NLB_OPAQUE_REFRESH_MARGIN 60 -+#define GP_NLB_BLOB_HDR_LEN 24 -+#define GP_NLB_BLOB_SESSION_OFF 24 -+#define GP_NLB_ENC_HS_KEY_PAYLOAD_OFF 24 -+#define GP_NLB_ENC_HS_KEY_PAYLOAD_LEN 40 -+#define GP_NLB_OPAQUE_PAYLOAD_OFF 28 - - static int gp_nlb_is_ipv4(const char *s) - { -@@ -117,14 +128,111 @@ static int gp_nlb_split_concat_ips(const char *concat, const char *hint_vip, - return -EINVAL; - } - -+static void gp_nlb_clear_decrypted_opaque(struct gp_nlb_config *nlb) -+{ -+ if (nlb->decrypted_opaque) { -+ memset(nlb->decrypted_opaque, 0, nlb->decrypted_opaque_len); -+ free(nlb->decrypted_opaque); -+ nlb->decrypted_opaque = NULL; -+ } -+ nlb->decrypted_opaque_len = 0; -+ memset(nlb->opaque_key, 0, sizeof(nlb->opaque_key)); -+ nlb->opaque_key_len = 0; -+} -+ - static void gp_nlb_clear_opaque_blob(struct gp_nlb_config *nlb) - { -+ gp_nlb_clear_decrypted_opaque(nlb); - if (nlb->opaque_blob) { - memset(nlb->opaque_blob, 0, nlb->opaque_blob_len); - free(nlb->opaque_blob); - nlb->opaque_blob = NULL; - } - nlb->opaque_blob_len = 0; -+ nlb->blob_session_id = 0; -+} -+ -+static int gp_nlb_parse_blob_header(struct openconnect_info *vpninfo, -+ const unsigned char *blob, int len, -+ struct gp_nlb_config *nlb) -+{ -+ if (!blob || len < GP_NLB_BLOB_HDR_LEN) -+ return -EINVAL; -+ -+ nlb->blob_session_id = load_be32(blob + GP_NLB_BLOB_SESSION_OFF); -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("NLB blob session id 0x%08x, version marker %02x%02x%02x%02x\n"), -+ nlb->blob_session_id, -+ blob[16], blob[17], blob[18], blob[19]); -+ return 0; -+} -+ -+static int gp_nlb_decrypt_enc_hs_key(struct openconnect_info *vpninfo, -+ struct gp_nlb_config *nlb, -+ const unsigned char *blob, int len) -+{ -+ unsigned char plain[64]; -+ int plain_len = sizeof(plain); -+ int ret; -+ -+ if (!nlb->enc_hs_key) -+ return 0; -+ if (len < GP_NLB_ENC_HS_KEY_PAYLOAD_OFF + GP_NLB_ENC_HS_KEY_PAYLOAD_LEN) -+ return -EINVAL; -+ -+ ret = gp_ssl_decrypt_blob(vpninfo, -+ blob + GP_NLB_ENC_HS_KEY_PAYLOAD_OFF, -+ GP_NLB_ENC_HS_KEY_PAYLOAD_LEN, -+ plain, &plain_len); -+ if (ret < 0) { -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("enc-hs-key TLS decipherment failed; NLB opaque crypto pending\n")); -+ return 0; -+ } -+ -+ if (plain_len <= 0 || plain_len > (int)sizeof(nlb->opaque_key)) -+ return -EINVAL; -+ -+ memcpy(nlb->opaque_key, plain, plain_len); -+ nlb->opaque_key_len = plain_len; -+ memset(plain, 0, sizeof(plain)); -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("Decrypted enc-hs-key (%d bytes)\n"), plain_len); -+ return 0; -+} -+ -+static int gp_nlb_decrypt_opaque_body(struct openconnect_info *vpninfo, -+ struct gp_nlb_config *nlb) -+{ -+ const unsigned char *payload; -+ int payload_len, out_len; -+ unsigned char *out; -+ -+ if (!nlb->opaque_blob || nlb->opaque_blob_len <= GP_NLB_OPAQUE_PAYLOAD_OFF) -+ return 0; -+ if (!nlb->opaque_key_len) -+ return 0; -+ -+ payload = nlb->opaque_blob + GP_NLB_OPAQUE_PAYLOAD_OFF; -+ payload_len = nlb->opaque_blob_len - GP_NLB_OPAQUE_PAYLOAD_OFF; -+ out = malloc(payload_len); -+ if (!out) -+ return -ENOMEM; -+ out_len = payload_len; -+ -+ if (gp_ssl_decrypt_blob(vpninfo, payload, payload_len, out, &out_len) < 0) { -+ free(out); -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("tunnel-opaque body decipherment failed\n")); -+ return 0; -+ } -+ -+ gp_nlb_clear_decrypted_opaque(nlb); -+ nlb->decrypted_opaque = out; -+ nlb->decrypted_opaque_len = out_len; -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("Decrypted tunnel-opaque body (%d bytes)\n"), out_len); -+ return 0; - } - - static void gp_nlb_write_hdr(unsigned char *hdr, uint16_t inner_len, uint8_t type) -@@ -162,6 +270,22 @@ int gpst_nlb_prepare(struct openconnect_info *vpninfo) - } - - gp_nlb_clear_opaque_blob(nlb); -+ -+ if (nlb->enc_hs_key) { -+ unsigned char *enc_blob = openconnect_base64_decode(&len, nlb->enc_hs_key); -+ -+ if (!enc_blob || len <= 0) { -+ vpn_progress(vpninfo, PRG_ERR, -+ _("Failed to base64-decode enc-hs-key\n")); -+ free(enc_blob); -+ return -EINVAL; -+ } -+ gp_nlb_parse_blob_header(vpninfo, enc_blob, len, nlb); -+ gp_nlb_decrypt_enc_hs_key(vpninfo, nlb, enc_blob, len); -+ memset(enc_blob, 0, len); -+ free(enc_blob); -+ } -+ - if (nlb->tunnel_opaque) { - nlb->opaque_blob = openconnect_base64_decode(&len, nlb->tunnel_opaque); - if (!nlb->opaque_blob || len <= 0) { -@@ -173,15 +297,12 @@ int gpst_nlb_prepare(struct openconnect_info *vpninfo) - } - nlb->opaque_blob_len = len; - vpn_progress(vpninfo, PRG_DEBUG, -- _("Decoded tunnel-opaque (%d bytes); enc-hs-key decipherment not yet implemented\n"), -- len); -+ _("Decoded tunnel-opaque (%d bytes)\n"), len); -+ if (!nlb->blob_session_id) -+ gp_nlb_parse_blob_header(vpninfo, nlb->opaque_blob, len, nlb); -+ gp_nlb_decrypt_opaque_body(vpninfo, nlb); - } - -- if (nlb->enc_hs_key) -- vpn_progress(vpninfo, PRG_DEBUG, -- _("enc-hs-key present (%zu chars); crypto RE pending\n"), -- strlen(nlb->enc_hs_key)); -- - return 0; - } - -diff --git a/library.c b/library.c -index 2c46031..096f4a8 100644 ---- a/library.c -+++ b/library.c -@@ -845,6 +845,11 @@ void openconnect_vpninfo_free(struct openconnect_info *vpninfo) - free(vpninfo->gp_nlb.tunnel_vip); - free(vpninfo->gp_nlb.connected_gw_ip); - free(vpninfo->gp_nlb.in_tunnel_gw_cert_chksum); -+ if (vpninfo->gp_nlb.decrypted_opaque) { -+ memset(vpninfo->gp_nlb.decrypted_opaque, 0, vpninfo->gp_nlb.decrypted_opaque_len); -+ free(vpninfo->gp_nlb.decrypted_opaque); -+ } -+ memset(vpninfo->gp_nlb.opaque_key, 0, sizeof(vpninfo->gp_nlb.opaque_key)); - if (vpninfo->gp_nlb.opaque_blob) { - memset(vpninfo->gp_nlb.opaque_blob, 0, vpninfo->gp_nlb.opaque_blob_len); - free(vpninfo->gp_nlb.opaque_blob); -diff --git a/openconnect-internal.h b/openconnect-internal.h -index 6148d26..533afe0 100644 ---- a/openconnect-internal.h -+++ b/openconnect-internal.h -@@ -405,6 +405,11 @@ struct gp_nlb_config { - char *in_tunnel_gw_cert_chksum; - unsigned char *opaque_blob; - int opaque_blob_len; -+ unsigned char *decrypted_opaque; -+ int decrypted_opaque_len; -+ uint32_t blob_session_id; -+ unsigned char opaque_key[48]; -+ int opaque_key_len; - int keepalive_sent; - }; - -@@ -455,6 +460,7 @@ struct openconnect_info { - int enc_key_len; - int hmac_key_len; - int hmac_out_len; -+ int esp_gcm_icv; - - int esp_magic_af; - unsigned char esp_magic[16]; /* GlobalProtect magic ping address (network-endian) */ -@@ -1106,10 +1112,14 @@ static inline void __monitor_fd_new(struct openconnect_info *vpninfo, - #define ENC_AES_128_GCM 20 - #define ENC_AES_256_GCM 22 - -+#define HMAC_NONE 0 - #define HMAC_MD5 1 - #define HMAC_SHA1 2 - #define HMAC_SHA256 3 - -+#define ESP_GCM_IV_LEN 8 -+#define ESP_GCM_ICV_DEFAULT 16 -+ - #define MAX_HMAC_SIZE 32 /* SHA256 */ - #define MAX_IV_SIZE 16 - #define MAX_ESP_PAD 17 /* Including the next-header field */ -@@ -1511,6 +1521,16 @@ int init_esp_ciphers(struct openconnect_info *vpninfo, struct esp *out, struct e - int decrypt_esp_packet(struct openconnect_info *vpninfo, struct esp *esp, struct pkt *pkt); - int encrypt_esp_packet(struct openconnect_info *vpninfo, struct pkt *pkt, int crypt_len); - -+static inline int esp_uses_gcm(const struct openconnect_info *vpninfo) -+{ -+ return vpninfo->esp_enc == ENC_AES_128_GCM || -+ vpninfo->esp_enc == ENC_AES_256_GCM; -+} -+ -+int gp_ssl_decrypt_blob(struct openconnect_info *vpninfo, -+ const unsigned char *in, int inlen, -+ unsigned char *out, int *outlen); -+ - /* {gnutls,openssl}.c */ - const char *openconnect_get_tls_library_version(void); - int can_enable_insecure_crypto(void); -diff --git a/openssl-esp.c b/openssl-esp.c -index feb3468..0fa19f5 100644 ---- a/openssl-esp.c -+++ b/openssl-esp.c -@@ -57,13 +57,59 @@ void destroy_esp_ciphers(struct esp *esp) - } - } - -+static const EVP_CIPHER *esp_gcm_cipher(const struct openconnect_info *vpninfo) -+{ -+ return vpninfo->esp_enc == ENC_AES_256_GCM ? -+ EVP_aes_256_gcm() : EVP_aes_128_gcm(); -+} -+ -+static void esp_gcm_nonce(unsigned char *nonce, const unsigned char *iv, uint32_t seq) -+{ -+ memcpy(nonce, iv, ESP_GCM_IV_LEN); -+ memcpy(nonce + ESP_GCM_IV_LEN, &seq, 4); -+} -+ -+static int esp_gcm_init(struct openconnect_info *vpninfo, EVP_CIPHER_CTX *ctx, -+ const unsigned char *key, const unsigned char *iv, uint32_t seq, -+ int enc) -+{ -+ unsigned char nonce[12]; -+ -+ esp_gcm_nonce(nonce, iv, seq); -+ if (!EVP_CipherInit_ex(ctx, esp_gcm_cipher(vpninfo), NULL, key, nonce, enc)) -+ return -EIO; -+ if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, sizeof(nonce), NULL)) -+ return -EIO; -+ return 0; -+} -+ -+static int esp_gcm_aad(struct openconnect_info *vpninfo, EVP_CIPHER_CTX *ctx, -+ const struct pkt *pkt) -+{ -+ unsigned char aad[8]; -+ int aadlen = 0; -+ -+ memcpy(aad, &pkt->esp.spi, 8); -+ if (!EVP_CipherUpdate(ctx, NULL, &aadlen, aad, sizeof(aad))) { -+ vpn_progress(vpninfo, PRG_ERR, -+ _("Failed to set ESP GCM AAD\n")); -+ openconnect_report_ssl_errors(vpninfo); -+ return -EIO; -+ } -+ return 0; -+} -+ - static int init_esp_cipher(struct openconnect_info *vpninfo, struct esp *esp, -- const EVP_MD *macalg, const EVP_CIPHER *encalg, int decrypt) -+ const EVP_MD *macalg, const EVP_CIPHER *encalg, int decrypt, -+ int gcm) - { - int ret; - - destroy_esp_ciphers(esp); - -+ if (gcm) -+ return 0; -+ - #if OPENSSL_VERSION_NUMBER < 0x10100000L - esp->cipher = malloc(sizeof(*esp->cipher)); - if (!esp->cipher) -@@ -109,7 +155,8 @@ static int init_esp_cipher(struct openconnect_info *vpninfo, struct esp *esp, - int init_esp_ciphers(struct openconnect_info *vpninfo, struct esp *esp_out, struct esp *esp_in) - { - const EVP_CIPHER *encalg; -- const EVP_MD *macalg; -+ const EVP_MD *macalg = NULL; -+ int gcm = esp_uses_gcm(vpninfo); - int ret; - - switch (vpninfo->esp_enc) { -@@ -119,29 +166,35 @@ int init_esp_ciphers(struct openconnect_info *vpninfo, struct esp *esp_out, stru - case ENC_AES_256_CBC: - encalg = EVP_aes_256_cbc(); - break; -+ case ENC_AES_128_GCM: -+ case ENC_AES_256_GCM: -+ encalg = esp_gcm_cipher(vpninfo); -+ break; - default: - return -EINVAL; - } - -- switch (vpninfo->esp_hmac) { -- case HMAC_MD5: -- macalg = EVP_md5(); -- break; -- case HMAC_SHA1: -- macalg = EVP_sha1(); -- break; -- case HMAC_SHA256: -- macalg = EVP_sha256(); -- break; -- default: -- return -EINVAL; -+ if (!gcm) { -+ switch (vpninfo->esp_hmac) { -+ case HMAC_MD5: -+ macalg = EVP_md5(); -+ break; -+ case HMAC_SHA1: -+ macalg = EVP_sha1(); -+ break; -+ case HMAC_SHA256: -+ macalg = EVP_sha256(); -+ break; -+ default: -+ return -EINVAL; -+ } - } - -- ret = init_esp_cipher(vpninfo, &vpninfo->esp_out, macalg, encalg, 0); -+ ret = init_esp_cipher(vpninfo, &vpninfo->esp_out, macalg, encalg, 0, gcm); - if (ret) - return ret; - -- ret = init_esp_cipher(vpninfo, esp_in, macalg, encalg, 1); -+ ret = init_esp_cipher(vpninfo, esp_in, macalg, encalg, 1, gcm); - if (ret) { - destroy_esp_ciphers(&vpninfo->esp_out); - return ret; -@@ -150,12 +203,48 @@ int init_esp_ciphers(struct openconnect_info *vpninfo, struct esp *esp_out, stru - return 0; - } - --/* pkt->len shall be the *payload* length. Omitting the header and the 12-byte HMAC */ -+/* pkt->len shall be the *payload* length. Omitting the header and the ICV/HMAC */ - int decrypt_esp_packet(struct openconnect_info *vpninfo, struct esp *esp, struct pkt *pkt) - { - unsigned char hmac_buf[MAX_HMAC_SIZE]; - unsigned int hmac_len = sizeof(hmac_buf); - int crypt_len = pkt->len; -+ EVP_CIPHER_CTX *ctx; -+ -+ if (verify_packet_seqno(vpninfo, esp, ntohl(pkt->esp.seq))) -+ return -EINVAL; -+ -+ if (esp_uses_gcm(vpninfo)) { -+ int outlen = 0; -+ -+ if (pkt->len < vpninfo->hmac_out_len) -+ return -EINVAL; -+ -+ ctx = EVP_CIPHER_CTX_new(); -+ if (!ctx) -+ return -ENOMEM; -+ -+ if (esp_gcm_init(vpninfo, ctx, esp->enc_key, pkt->esp.iv, pkt->esp.seq, 0) < 0 || -+ esp_gcm_aad(vpninfo, ctx, pkt) < 0) { -+ EVP_CIPHER_CTX_free(ctx); -+ return -EINVAL; -+ } -+ -+ pkt->len -= vpninfo->hmac_out_len; -+ memcpy(hmac_buf, pkt->data + pkt->len, vpninfo->hmac_out_len); -+ if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, vpninfo->hmac_out_len, -+ hmac_buf) || -+ !EVP_DecryptUpdate(ctx, pkt->data, &outlen, pkt->data, pkt->len) || -+ !EVP_DecryptFinal_ex(ctx, pkt->data + outlen, &outlen)) { -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("Received ESP GCM packet with invalid ICV\n")); -+ EVP_CIPHER_CTX_free(ctx); -+ return -EINVAL; -+ } -+ -+ EVP_CIPHER_CTX_free(ctx); -+ return 0; -+ } - - HMAC_Init_ex(esp->hmac, NULL, 0, NULL, NULL); - HMAC_Update(esp->hmac, (void *)&pkt->esp, sizeof(pkt->esp) + pkt->len); -@@ -167,9 +256,6 @@ int decrypt_esp_packet(struct openconnect_info *vpninfo, struct esp *esp, struct - return -EINVAL; - } - -- if (verify_packet_seqno(vpninfo, esp, ntohl(pkt->esp.seq))) -- return -EINVAL; -- - if (!EVP_DecryptInit_ex(esp->cipher, NULL, NULL, NULL, - pkt->esp.iv)) { - vpn_progress(vpninfo, PRG_ERR, -@@ -193,6 +279,31 @@ int encrypt_esp_packet(struct openconnect_info *vpninfo, struct pkt *pkt, int cr - { - int blksize = 16; - unsigned int hmac_len = vpninfo->hmac_out_len; -+ EVP_CIPHER_CTX *ctx; -+ int outlen = 0; -+ -+ if (esp_uses_gcm(vpninfo)) { -+ ctx = EVP_CIPHER_CTX_new(); -+ if (!ctx) -+ return -ENOMEM; -+ -+ if (esp_gcm_init(vpninfo, ctx, vpninfo->esp_out.enc_key, -+ pkt->esp.iv, pkt->esp.seq, 1) < 0 || -+ esp_gcm_aad(vpninfo, ctx, pkt) < 0 || -+ !EVP_EncryptUpdate(ctx, pkt->data, &outlen, pkt->data, crypt_len) || -+ !EVP_EncryptFinal_ex(ctx, pkt->data + outlen, &outlen) || -+ !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, vpninfo->hmac_out_len, -+ pkt->data + crypt_len)) { -+ vpn_progress(vpninfo, PRG_ERR, -+ _("Failed to encrypt ESP GCM packet:\n")); -+ openconnect_report_ssl_errors(vpninfo); -+ EVP_CIPHER_CTX_free(ctx); -+ return -EINVAL; -+ } -+ -+ EVP_CIPHER_CTX_free(ctx); -+ return 0; -+ } - - if (!EVP_EncryptUpdate(vpninfo->esp_out.cipher, pkt->data, &crypt_len, - pkt->data, crypt_len)) { -@@ -209,4 +320,4 @@ int encrypt_esp_packet(struct openconnect_info *vpninfo, struct pkt *pkt, int cr - EVP_EncryptUpdate(vpninfo->esp_out.cipher, vpninfo->esp_out.iv, &blksize, - pkt->data + crypt_len + hmac_len - blksize, blksize); - return 0; --} -+} -\ No newline at end of file -diff --git a/openssl.c b/openssl.c -index 761cb03..d860d06 100644 ---- a/openssl.c -+++ b/openssl.c -@@ -2773,3 +2773,118 @@ out_of_memory: - return ret; - } - -+static int gp_tls12_prf(const EVP_MD *md, const unsigned char *secret, size_t secret_len, -+ const char *label, const unsigned char *seed, size_t seed_len, -+ unsigned char *out, size_t out_len) -+{ -+ EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_TLS1_PRF, NULL); -+ size_t len = out_len; -+ int ret = -1; -+ -+ if (!pctx || -+ EVP_PKEY_derive_init(pctx) <= 0 || -+ EVP_PKEY_CTX_set_tls1_prf_md(pctx, md) <= 0 || -+ EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, secret, secret_len) <= 0 || -+ EVP_PKEY_CTX_set1_tls1_prf_seed(pctx, (const unsigned char *)label, -+ strlen(label)) <= 0 || -+ EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed, seed_len) <= 0 || -+ EVP_PKEY_derive(pctx, out, &len) <= 0) -+ goto out; -+ ret = 0; -+out: -+ EVP_PKEY_CTX_free(pctx); -+ return ret; -+} -+ -+static int gp_tls12_cbc_decrypt(struct openconnect_info *vpninfo, -+ const EVP_CIPHER *cipher, -+ const unsigned char *key, -+ const unsigned char *iv, size_t iv_size, -+ const unsigned char *in, int inlen, -+ unsigned char *out, int *outlen) -+{ -+ EVP_CIPHER_CTX *ctx; -+ int outl = 0, tmplen = 0; -+ -+ if (inlen <= (int)iv_size || inlen % (int)iv_size) -+ return -EINVAL; -+ -+ ctx = EVP_CIPHER_CTX_new(); -+ if (!ctx) -+ return -ENOMEM; -+ -+ inlen -= iv_size; -+ memcpy(out, in + iv_size, inlen); -+ *outlen = inlen; -+ if (!EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv) || -+ !EVP_CIPHER_CTX_set_padding(ctx, 1) || -+ !EVP_DecryptUpdate(ctx, out, &outl, out, inlen) || -+ !EVP_DecryptFinal_ex(ctx, out + outl, &tmplen)) { -+ EVP_CIPHER_CTX_free(ctx); -+ return -EINVAL; -+ } -+ *outlen = outl + tmplen; -+ EVP_CIPHER_CTX_free(ctx); -+ return 0; -+} -+ -+int gp_ssl_decrypt_blob(struct openconnect_info *vpninfo, -+ const unsigned char *in, int inlen, -+ unsigned char *out, int *outlen) -+{ -+ SSL *ssl = vpninfo->https_ssl; -+ SSL_SESSION *sess; -+ const SSL_CIPHER *cipher; -+ const EVP_CIPHER *evp_cipher; -+ const EVP_MD *md; -+ size_t mac_len, key_len, iv_len, needed; -+ unsigned char ms[48], crandom[32], srandom[32], seed[64], key_block[128]; -+ unsigned char *srv_key, *srv_iv; -+ size_t ms_len; -+ int ret; -+ -+ if (!ssl || !in || inlen <= 0 || !out || !outlen) -+ return -EINVAL; -+ -+ sess = SSL_get_session(ssl); -+ cipher = SSL_SESSION_get_cipher(sess); -+ if (!sess || !cipher) -+ return -EINVAL; -+ -+ ms_len = SSL_SESSION_get_master_key(sess, ms, sizeof(ms)); -+ if (!ms_len) -+ return -EINVAL; -+ -+ if (!SSL_get_client_random(ssl, crandom, sizeof(crandom)) || -+ !SSL_get_server_random(ssl, srandom, sizeof(srandom))) -+ return -EINVAL; -+ -+ md = EVP_get_digestbynid(SSL_CIPHER_get_digest_nid(cipher)); -+ evp_cipher = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(cipher)); -+ if (!md || !evp_cipher) -+ return -EINVAL; -+ -+ mac_len = EVP_MD_size(md); -+ key_len = EVP_CIPHER_key_length(evp_cipher); -+ iv_len = EVP_CIPHER_iv_length(evp_cipher); -+ needed = 2 * mac_len + 2 * key_len + 2 * iv_len; -+ if (needed > sizeof(key_block)) -+ return -EINVAL; -+ -+ memcpy(seed, srandom, 32); -+ memcpy(seed + 32, crandom, 32); -+ if (gp_tls12_prf(md, ms, ms_len, "key expansion", seed, sizeof(seed), -+ key_block, needed)) -+ return -EINVAL; -+ -+ srv_key = key_block + 2 * mac_len + key_len; -+ srv_iv = key_block + 2 * mac_len + 2 * key_len + iv_len; -+ ret = gp_tls12_cbc_decrypt(vpninfo, evp_cipher, srv_key, in, iv_len, -+ in, inlen, out, outlen); -+ if (!ret) -+ return 0; -+ -+ return gp_tls12_cbc_decrypt(vpninfo, evp_cipher, srv_key, srv_iv, iv_len, -+ in, inlen, out, outlen); -+} -+ -diff --git a/tests/Makefile.am b/tests/Makefile.am -index 8b3cbee..ec25319 100644 ---- a/tests/Makefile.am -+++ b/tests/Makefile.am -@@ -88,9 +88,10 @@ dist_check_SCRIPTS += auth-username-pass auth-certificate auth-nonascii cert-fin - id-test obsolete-server-crypto pfs auth-swtpm auth-hwtpm fortinet-auth-and-config \ - f5-auth-and-config juniper-auth juniper-sso-auth gp-auth-and-config auth-pkcs11 \ - auth-multicert gp-nlb-ssl-parse gp-nlb-esp-envelope gp-nlb-ssl-connect \ -- gp-nlb-opaque-due gp-nlb-auth-and-config -+ gp-nlb-opaque-due gp-nlb-auth-and-config gp-esp-gcm-roundtrip gp-nlb-blob-parse - --TESTS += gp-nlb-ssl-parse gp-nlb-esp-envelope gp-nlb-ssl-connect gp-nlb-opaque-due -+TESTS += gp-nlb-ssl-parse gp-nlb-esp-envelope gp-nlb-ssl-connect gp-nlb-opaque-due \ -+ gp-esp-gcm-roundtrip gp-nlb-blob-parse - - if HAVE_CWRAP - TESTS += auth-username-pass auth-certificate auth-nonascii cert-fingerprint id-test \ -diff --git a/tests/gp-esp-gcm-roundtrip b/tests/gp-esp-gcm-roundtrip -new file mode 100755 -index 0000000..f81d6e9 ---- /dev/null -+++ b/tests/gp-esp-gcm-roundtrip -@@ -0,0 +1,63 @@ -+#!/bin/sh -+# Validate RFC4106 ESP-GCM packet layout helpers (nonce + AAD) -+set -eu -+cd "$(dirname "$0")/.." -+CC="${CC:-cc}" -+TMP=$(mktemp -d) -+trap 'rm -rf "$TMP"' EXIT -+ -+cat > "$TMP/test.c" <<'EOF' -+#include -+#include -+#include -+#include -+ -+#define ESP_GCM_IV_LEN 8 -+ -+static inline uint32_t load_be32(const void *p) -+{ -+ uint32_t n; -+ memcpy(&n, p, 4); -+ return ntohl(n); -+} -+ -+static void esp_gcm_nonce(unsigned char *nonce, const unsigned char *iv, uint32_t seq) -+{ -+ memcpy(nonce, iv, ESP_GCM_IV_LEN); -+ memcpy(nonce + ESP_GCM_IV_LEN, &seq, 4); -+} -+ -+static void esp_gcm_aad(unsigned char *aad, uint32_t spi, uint32_t seq) -+{ -+ uint32_t be[2]; -+ -+ be[0] = spi; -+ be[1] = seq; -+ memcpy(aad, be, 8); -+} -+ -+int main(void) -+{ -+ unsigned char iv[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; -+ unsigned char nonce[12], aad[8]; -+ uint32_t seq = htonl(42); -+ uint32_t spi = htonl(0x12345678); -+ -+ esp_gcm_nonce(nonce, iv, seq); -+ if (memcmp(nonce, iv, 8) || memcmp(nonce + 8, &seq, 4)) { -+ fprintf(stderr, "nonce construction failed\n"); -+ return 1; -+ } -+ -+ esp_gcm_aad(aad, spi, seq); -+ if (load_be32(aad) != ntohl(spi) || load_be32(aad + 4) != ntohl(seq)) { -+ fprintf(stderr, "aad construction failed\n"); -+ return 1; -+ } -+ -+ printf("gp-esp-gcm-roundtrip: OK\n"); -+ return 0; -+} -+EOF -+$CC -o "$TMP/test" "$TMP/test.c" -+"$TMP/test" -\ No newline at end of file -diff --git a/tests/gp-nlb-blob-parse b/tests/gp-nlb-blob-parse -new file mode 100755 -index 0000000..f803b1b ---- /dev/null -+++ b/tests/gp-nlb-blob-parse -@@ -0,0 +1,51 @@ -+#!/bin/sh -+# Validate NLB enc-hs-key / tunnel-opaque blob header parsing -+set -eu -+cd "$(dirname "$0")/.." -+CC="${CC:-cc}" -+TMP=$(mktemp -d) -+trap 'rm -rf "$TMP"' EXIT -+ -+cat > "$TMP/test.c" <<'EOF' -+#include -+#include -+#include -+#include -+ -+#define GP_NLB_BLOB_HDR_LEN 24 -+#define GP_NLB_BLOB_SESSION_OFF 24 -+ -+static inline uint32_t load_be32(const void *p) -+{ -+ uint32_t n; -+ memcpy(&n, p, 4); -+ return ntohl(n); -+} -+ -+int main(void) -+{ -+ /* enc-hs-key sample from issue #526 (Australia NLB gateway) */ -+ const unsigned char enc_blob[64] = { -+ 0x58,0x8b,0xe1,0xc1,0x73,0xf0,0x7d,0x4b,0x87,0x7c,0x47,0x80,0xe3,0x61,0x37,0xe2, -+ 0xe5,0xac,0x06,0x00,0x00,0x00,0x00,0x00,0x10,0xa7,0x3b,0x6a,0x70,0x3e,0xbd,0x34, -+ 0x9a,0x8f,0x3b,0x81,0x8e,0x8b,0x5b,0x35,0x81,0xd2,0xa0,0xdf,0x14,0xa7,0x17,0x47, -+ 0x2c,0x52,0x3b,0x8e,0x99,0x4d,0x39,0x30,0x56,0x5b,0xbe,0xff,0x06,0x1b,0x86,0xf4 -+ }; -+ uint32_t sid; -+ -+ if (sizeof(enc_blob) != 64) -+ return 1; -+ if (enc_blob[16] != 0xe5 || enc_blob[17] != 0xac) -+ return 1; -+ sid = load_be32(enc_blob + GP_NLB_BLOB_SESSION_OFF); -+ if (sid != 0x10a73b6a) { -+ fprintf(stderr, "session id mismatch: 0x%08x\n", sid); -+ return 1; -+ } -+ -+ printf("gp-nlb-blob-parse: OK\n"); -+ return 0; -+} -+EOF -+$CC -o "$TMP/test" "$TMP/test.c" -+"$TMP/test" -\ No newline at end of file diff --git a/crates/openconnect/deps/patches/0014-Implement-exact-PanGPS-do_ssl_decrypt-handling.patch b/crates/openconnect/deps/patches/0014-Implement-exact-PanGPS-do_ssl_decrypt-handling.patch deleted file mode 100644 index 4b7f32c..0000000 --- a/crates/openconnect/deps/patches/0014-Implement-exact-PanGPS-do_ssl_decrypt-handling.patch +++ /dev/null @@ -1,453 +0,0 @@ -From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: Kevin Yue -Date: Thu, 25 Jun 2026 23:30:00 +0800 -Subject: [PATCH 14/14] Implement exact PanGPS do_ssl_decrypt handling - -Match PanGPS decrypt_data: TLS 1.2 key expansion server write key/IV, -AES-CBC with PKCS padding on ciphertext after the 28-byte blob prefix. -Use decrypted enc-hs-key with TLS IV for tunnel-opaque body decryption. ---- -diff --git a/gnutls.c b/gnutls.c ---- a/gnutls.c -+++ b/gnutls.c -@@ -3604,17 +3604,17 @@ - return ret; - } - --static int gp_tls12_cbc_decrypt(struct openconnect_info *vpninfo, -- const unsigned char *key, size_t key_size, -- const unsigned char *iv, size_t iv_size, -- const unsigned char *in, int inlen, -- unsigned char *out, int *outlen) -+static int gp_aes_cbc_decrypt(const unsigned char *key, size_t key_size, -+ const unsigned char *iv, size_t iv_size, -+ const unsigned char *in, int inlen, -+ unsigned char *out, int *outlen) - { - gnutls_cipher_hd_t hd = NULL; - gnutls_datum_t enc_key, iv_d; -- int err; -+ unsigned char pad; -+ int err, padlen; - -- if (inlen <= (int)iv_size || inlen % iv_size) -+ if (!key_size || !iv_size || inlen <= 0 || (inlen % (int)iv_size)) - return -EINVAL; - - enc_key.data = (unsigned char *)key; -@@ -3627,56 +3627,108 @@ - if (err) - return -EINVAL; - -- inlen -= iv_size; -- memcpy(out, in + iv_size, inlen); -- *outlen = inlen; -+ memcpy(out, in, inlen); - err = gnutls_cipher_decrypt(hd, out, inlen); - gnutls_cipher_deinit(hd); -- return err ? -EINVAL : 0; -+ if (err) -+ return -EINVAL; -+ -+ pad = out[inlen - 1]; -+ if (pad <= 0 || pad > (int)iv_size || pad > inlen) -+ return -EINVAL; -+ for (padlen = 1; padlen <= pad; padlen++) { -+ if (out[inlen - padlen] != pad) -+ return -EINVAL; -+ } -+ *outlen = inlen - pad; -+ return 0; - } - --int gp_ssl_decrypt_blob(struct openconnect_info *vpninfo, -- const unsigned char *in, int inlen, -- unsigned char *out, int *outlen) -+static int gp_tls12_expand_keys(gnutls_session_t sess, -+ unsigned char *key_block, size_t key_block_len, -+ size_t *key_size, size_t *mac_size, size_t *iv_size, -+ const unsigned char **srv_key, const unsigned char **srv_iv) - { -- gnutls_session_t sess = vpninfo->https_sess; - gnutls_cipher_algorithm_t cipher_algo; - gnutls_mac_algorithm_t mac_algo; -- size_t key_size, mac_size, iv_size, needed; -- unsigned char key_block[128]; -- const unsigned char *srv_key; -- int ret; -+ size_t needed; - -- if (!sess || !in || inlen <= 0 || !out || !outlen) -- return -EINVAL; -- - cipher_algo = gnutls_cipher_get(sess); - mac_algo = gnutls_mac_get(sess); -- key_size = gnutls_cipher_get_key_size(cipher_algo); -- mac_size = gnutls_hmac_get_len(mac_algo); -- iv_size = gnutls_cipher_get_iv_size(cipher_algo); -+ *key_size = gnutls_cipher_get_key_size(cipher_algo); -+ *mac_size = gnutls_hmac_get_len(mac_algo); -+ *iv_size = gnutls_cipher_get_iv_size(cipher_algo); - -- if (!key_size || !mac_size || !iv_size) -+ if (!*key_size || !*mac_size || !*iv_size) - return -EINVAL; - -- needed = 2 * mac_size + 2 * key_size + 2 * iv_size; -- if (needed > sizeof(key_block)) -+ needed = 2 * *mac_size + 2 * *key_size + 2 * *iv_size; -+ if (needed > key_block_len) - return -EINVAL; - - if (gnutls_prf(sess, 13, "key expansion", 1, 0, NULL, needed, - (char *)key_block) < 0) - return -EINVAL; - -- srv_key = key_block + 2 * mac_size + key_size; -- ret = gp_tls12_cbc_decrypt(vpninfo, srv_key, key_size, in, iv_size, -- in, inlen, out, outlen); -- if (!ret) -- return 0; -+ *srv_key = key_block + 2 * *mac_size + *key_size; -+ *srv_iv = key_block + 2 * *mac_size + 2 * *key_size + *iv_size; -+ return 0; -+} - -- /* PanGPS may pass only the ciphertext portion without explicit IV */ -- if (inlen < (int)iv_size) -+int gp_ssl_decrypt_blob(struct openconnect_info *vpninfo, -+ const unsigned char *in, int inlen, -+ unsigned char *out, int *outlen) -+{ -+ gnutls_session_t sess = vpninfo->https_sess; -+ unsigned char key_block[128]; -+ size_t key_size, mac_size, iv_size; -+ const unsigned char *srv_key, *srv_iv; -+ -+ if (!sess || !in || inlen <= 0 || !out || !outlen) - return -EINVAL; -- return gp_tls12_cbc_decrypt(vpninfo, srv_key, key_size, -- key_block + needed - iv_size, iv_size, -- in, inlen, out, outlen); -+ -+ if (gp_tls12_expand_keys(sess, key_block, sizeof(key_block), -+ &key_size, &mac_size, &iv_size, -+ &srv_key, &srv_iv) < 0) -+ return -EINVAL; -+ -+ return gp_aes_cbc_decrypt(srv_key, key_size, srv_iv, iv_size, -+ in, inlen, out, outlen); - } -+ -+int gp_ssl_decrypt_blob_key(struct openconnect_info *vpninfo, -+ const unsigned char *key, int key_len, -+ const unsigned char *iv, int iv_len, -+ const unsigned char *in, int inlen, -+ unsigned char *out, int *outlen) -+{ -+ gnutls_session_t sess = vpninfo->https_sess; -+ unsigned char key_block[128]; -+ size_t key_size, mac_size, iv_size; -+ const unsigned char *srv_key, *srv_iv; -+ const unsigned char *use_key, *use_iv; -+ -+ if (!sess || !key || key_len <= 0 || !in || inlen <= 0 || !out || !outlen) -+ return -EINVAL; -+ -+ if (gp_tls12_expand_keys(sess, key_block, sizeof(key_block), -+ &key_size, &mac_size, &iv_size, -+ &srv_key, &srv_iv) < 0) -+ return -EINVAL; -+ -+ use_key = key; -+ use_iv = srv_iv; -+ key_size = key_len; -+ iv_size = gnutls_cipher_get_iv_size(gnutls_cipher_get(sess)); -+ -+ if (iv && iv_len > 0) { -+ use_iv = iv; -+ iv_size = iv_len; -+ } -+ -+ if (key_len != 16 && key_len != 24 && key_len != 32) -+ return -EINVAL; -+ -+ return gp_aes_cbc_decrypt(use_key, key_size, use_iv, iv_size, -+ in, inlen, out, outlen); -+} -diff --git a/openssl.c b/openssl.c ---- a/openssl.c -+++ b/openssl.c -@@ -2796,29 +2796,35 @@ - return ret; - } - --static int gp_tls12_cbc_decrypt(struct openconnect_info *vpninfo, -- const EVP_CIPHER *cipher, -- const unsigned char *key, -- const unsigned char *iv, size_t iv_size, -- const unsigned char *in, int inlen, -- unsigned char *out, int *outlen) -+static const EVP_CIPHER *gp_aes_cbc_cipher(size_t key_size) - { -+ if (key_size == 32) -+ return EVP_aes_256_cbc(); -+ if (key_size == 24) -+ return EVP_aes_192_cbc(); -+ return EVP_aes_128_cbc(); -+} -+ -+static int gp_aes_cbc_decrypt(const EVP_CIPHER *cipher, -+ const unsigned char *key, -+ const unsigned char *iv, -+ const unsigned char *in, int inlen, -+ unsigned char *out, int *outlen) -+{ - EVP_CIPHER_CTX *ctx; - int outl = 0, tmplen = 0; -+ size_t iv_size = EVP_CIPHER_iv_length(cipher); - -- if (inlen <= (int)iv_size || inlen % (int)iv_size) -+ if (!cipher || inlen <= 0 || (inlen % (int)iv_size)) - return -EINVAL; - - ctx = EVP_CIPHER_CTX_new(); - if (!ctx) - return -ENOMEM; - -- inlen -= iv_size; -- memcpy(out, in + iv_size, inlen); -- *outlen = inlen; - if (!EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv) || - !EVP_CIPHER_CTX_set_padding(ctx, 1) || -- !EVP_DecryptUpdate(ctx, out, &outl, out, inlen) || -+ !EVP_DecryptUpdate(ctx, out, &outl, in, inlen) || - !EVP_DecryptFinal_ex(ctx, out + outl, &tmplen)) { - EVP_CIPHER_CTX_free(ctx); - return -EINVAL; -@@ -2828,24 +2834,18 @@ - return 0; - } - --int gp_ssl_decrypt_blob(struct openconnect_info *vpninfo, -- const unsigned char *in, int inlen, -- unsigned char *out, int *outlen) -+static int gp_tls12_expand_keys(SSL *ssl, -+ unsigned char *key_block, size_t key_block_len, -+ size_t *mac_len, size_t *key_len, size_t *iv_len, -+ unsigned char **srv_key, unsigned char **srv_iv) - { -- SSL *ssl = vpninfo->https_ssl; - SSL_SESSION *sess; - const SSL_CIPHER *cipher; -- const EVP_CIPHER *evp_cipher; - const EVP_MD *md; -- size_t mac_len, key_len, iv_len, needed; -- unsigned char ms[48], crandom[32], srandom[32], seed[64], key_block[128]; -- unsigned char *srv_key, *srv_iv; -- size_t ms_len; -- int ret; -+ const EVP_CIPHER *evp_cipher; -+ unsigned char ms[48], crandom[32], srandom[32], seed[64]; -+ size_t ms_len, needed; - -- if (!ssl || !in || inlen <= 0 || !out || !outlen) -- return -EINVAL; -- - sess = SSL_get_session(ssl); - cipher = SSL_SESSION_get_cipher(sess); - if (!sess || !cipher) -@@ -2864,11 +2864,11 @@ - if (!md || !evp_cipher) - return -EINVAL; - -- mac_len = EVP_MD_size(md); -- key_len = EVP_CIPHER_key_length(evp_cipher); -- iv_len = EVP_CIPHER_iv_length(evp_cipher); -- needed = 2 * mac_len + 2 * key_len + 2 * iv_len; -- if (needed > sizeof(key_block)) -+ *mac_len = EVP_MD_size(md); -+ *key_len = EVP_CIPHER_key_length(evp_cipher); -+ *iv_len = EVP_CIPHER_iv_length(evp_cipher); -+ needed = 2 * *mac_len + 2 * *key_len + 2 * *iv_len; -+ if (needed > key_block_len) - return -EINVAL; - - memcpy(seed, srandom, 32); -@@ -2877,14 +2877,59 @@ - key_block, needed)) - return -EINVAL; - -- srv_key = key_block + 2 * mac_len + key_len; -- srv_iv = key_block + 2 * mac_len + 2 * key_len + iv_len; -- ret = gp_tls12_cbc_decrypt(vpninfo, evp_cipher, srv_key, in, iv_len, -- in, inlen, out, outlen); -- if (!ret) -- return 0; -+ *srv_key = key_block + 2 * *mac_len + *key_len; -+ *srv_iv = key_block + 2 * *mac_len + 2 * *key_len + *iv_len; -+ return 0; -+} - -- return gp_tls12_cbc_decrypt(vpninfo, evp_cipher, srv_key, srv_iv, iv_len, -+int gp_ssl_decrypt_blob(struct openconnect_info *vpninfo, -+ const unsigned char *in, int inlen, -+ unsigned char *out, int *outlen) -+{ -+ SSL *ssl = vpninfo->https_ssl; -+ unsigned char key_block[128]; -+ size_t mac_len, key_len, iv_len; -+ unsigned char *srv_key, *srv_iv; -+ -+ if (!ssl || !in || inlen <= 0 || !out || !outlen) -+ return -EINVAL; -+ -+ if (gp_tls12_expand_keys(ssl, key_block, sizeof(key_block), -+ &mac_len, &key_len, &iv_len, -+ &srv_key, &srv_iv) < 0) -+ return -EINVAL; -+ -+ return gp_aes_cbc_decrypt(gp_aes_cbc_cipher(key_len), srv_key, srv_iv, - in, inlen, out, outlen); - } - -+int gp_ssl_decrypt_blob_key(struct openconnect_info *vpninfo, -+ const unsigned char *key, int key_len, -+ const unsigned char *iv, int iv_len, -+ const unsigned char *in, int inlen, -+ unsigned char *out, int *outlen) -+{ -+ SSL *ssl = vpninfo->https_ssl; -+ unsigned char key_block[128]; -+ size_t mac_len, blk_key_len, blk_iv_len; -+ unsigned char *srv_key, *srv_iv; -+ const unsigned char *use_iv; -+ -+ if (!ssl || !key || key_len <= 0 || !in || inlen <= 0 || !out || !outlen) -+ return -EINVAL; -+ -+ if (gp_tls12_expand_keys(ssl, key_block, sizeof(key_block), -+ &mac_len, &blk_key_len, &blk_iv_len, -+ &srv_key, &srv_iv) < 0) -+ return -EINVAL; -+ use_iv = srv_iv; -+ if (iv && iv_len > 0) -+ use_iv = iv; -+ -+ if (key_len != 16 && key_len != 24 && key_len != 32) -+ return -EINVAL; -+ -+ return gp_aes_cbc_decrypt(gp_aes_cbc_cipher(key_len), key, use_iv, -+ in, inlen, out, outlen); -+} -+ -diff --git a/gpst_nlb.c b/gpst_nlb.c ---- a/gpst_nlb.c -+++ b/gpst_nlb.c -@@ -51,8 +51,11 @@ - * 24-27 tunnel session id (cleartext, matches across blobs from same session) - * 28+ encrypted payload (enc-hs-key: 36 bytes; tunnel-opaque: 508 bytes) - * -- * enc-hs-key decipherment uses TLS 1.2 key expansion from the active HTTPS -- * session (PanGPS do_ssl_decrypt). Decrypted material keys tunnel-opaque body. -+ * PanGPS do_ssl_decrypt / decrypt_data (6.3.3.1 RE): -+ * - TLS 1.2 "key expansion" yields server write key + IV -+ * - EVP AES-CBC decrypt on ciphertext after the 28-byte cleartext prefix -+ * - enc-hs-key yields the hs-key material; tunnel-opaque uses that key with -+ * the same TLS server write IV - */ - - #include -@@ -78,9 +81,8 @@ - #define GP_NLB_OPAQUE_REFRESH_MARGIN 60 - #define GP_NLB_BLOB_HDR_LEN 24 - #define GP_NLB_BLOB_SESSION_OFF 24 --#define GP_NLB_ENC_HS_KEY_PAYLOAD_OFF 24 --#define GP_NLB_ENC_HS_KEY_PAYLOAD_LEN 40 --#define GP_NLB_OPAQUE_PAYLOAD_OFF 28 -+#define GP_NLB_BLOB_CIPHER_OFF 28 -+#define GP_NLB_ENC_HS_KEY_CIPHER_LEN 36 - - static int gp_nlb_is_ipv4(const char *s) - { -@@ -177,12 +179,12 @@ - - if (!nlb->enc_hs_key) - return 0; -- if (len < GP_NLB_ENC_HS_KEY_PAYLOAD_OFF + GP_NLB_ENC_HS_KEY_PAYLOAD_LEN) -+ if (len < GP_NLB_BLOB_CIPHER_OFF + GP_NLB_ENC_HS_KEY_CIPHER_LEN) - return -EINVAL; - - ret = gp_ssl_decrypt_blob(vpninfo, -- blob + GP_NLB_ENC_HS_KEY_PAYLOAD_OFF, -- GP_NLB_ENC_HS_KEY_PAYLOAD_LEN, -+ blob + GP_NLB_BLOB_CIPHER_OFF, -+ GP_NLB_ENC_HS_KEY_CIPHER_LEN, - plain, &plain_len); - if (ret < 0) { - vpn_progress(vpninfo, PRG_DEBUG, -@@ -208,19 +210,20 @@ - int payload_len, out_len; - unsigned char *out; - -- if (!nlb->opaque_blob || nlb->opaque_blob_len <= GP_NLB_OPAQUE_PAYLOAD_OFF) -+ if (!nlb->opaque_blob || nlb->opaque_blob_len <= GP_NLB_BLOB_CIPHER_OFF) - return 0; - if (!nlb->opaque_key_len) - return 0; - -- payload = nlb->opaque_blob + GP_NLB_OPAQUE_PAYLOAD_OFF; -- payload_len = nlb->opaque_blob_len - GP_NLB_OPAQUE_PAYLOAD_OFF; -+ payload = nlb->opaque_blob + GP_NLB_BLOB_CIPHER_OFF; -+ payload_len = nlb->opaque_blob_len - GP_NLB_BLOB_CIPHER_OFF; - out = malloc(payload_len); - if (!out) - return -ENOMEM; - out_len = payload_len; - -- if (gp_ssl_decrypt_blob(vpninfo, payload, payload_len, out, &out_len) < 0) { -+ if (gp_ssl_decrypt_blob_key(vpninfo, nlb->opaque_key, nlb->opaque_key_len, -+ NULL, 0, payload, payload_len, out, &out_len) < 0) { - free(out); - vpn_progress(vpninfo, PRG_DEBUG, - _("tunnel-opaque body decipherment failed\n")); -diff --git a/openconnect-internal.h b/openconnect-internal.h ---- a/openconnect-internal.h -+++ b/openconnect-internal.h -@@ -1530,6 +1530,11 @@ - int gp_ssl_decrypt_blob(struct openconnect_info *vpninfo, - const unsigned char *in, int inlen, - unsigned char *out, int *outlen); -+int gp_ssl_decrypt_blob_key(struct openconnect_info *vpninfo, -+ const unsigned char *key, int key_len, -+ const unsigned char *iv, int iv_len, -+ const unsigned char *in, int inlen, -+ unsigned char *out, int *outlen); - - /* {gnutls,openssl}.c */ - const char *openconnect_get_tls_library_version(void); -diff --git a/tests/gp-nlb-blob-parse b/tests/gp-nlb-blob-parse ---- a/tests/gp-nlb-blob-parse -+++ b/tests/gp-nlb-blob-parse -@@ -14,6 +14,8 @@ - - #define GP_NLB_BLOB_HDR_LEN 24 - #define GP_NLB_BLOB_SESSION_OFF 24 -+#define GP_NLB_BLOB_CIPHER_OFF 28 -+#define GP_NLB_ENC_HS_KEY_CIPHER_LEN 36 - - static inline uint32_t load_be32(const void *p) - { -@@ -42,6 +44,12 @@ - fprintf(stderr, "session id mismatch: 0x%08x\n", sid); - return 1; - } -+ if (GP_NLB_BLOB_CIPHER_OFF + GP_NLB_ENC_HS_KEY_CIPHER_LEN != (int)sizeof(enc_blob)) { -+ fprintf(stderr, "cipher span mismatch\n"); -+ return 1; -+ } -+ if (enc_blob[GP_NLB_BLOB_CIPHER_OFF] != 0x70) -+ return 1; - - printf("gp-nlb-blob-parse: OK\n"); - return 0; diff --git a/crates/openconnect/deps/patches/0015-Wire-NLB-tunnel-opaque-auth-into-ESP-envelope.patch b/crates/openconnect/deps/patches/0015-Wire-NLB-tunnel-opaque-auth-into-ESP-envelope.patch deleted file mode 100644 index 8472e18..0000000 --- a/crates/openconnect/deps/patches/0015-Wire-NLB-tunnel-opaque-auth-into-ESP-envelope.patch +++ /dev/null @@ -1,387 +0,0 @@ -From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: Kevin Yue -Date: Thu, 25 Jun 2026 23:45:00 +0800 -Subject: [PATCH 15/15] Wire NLB tunnel-opaque auth into ESP envelope - -Extract 6-byte auth material from decrypted tunnel-opaque offset +8, -increment wire sequence in header bytes 4-5, and sign NLB envelopes with -HMAC-SHA1 using hs-key over the 10-byte header prefix plus ESP payload. ---- -diff --git a/gnutls.c b/gnutls.c ---- a/gnutls.c -+++ b/gnutls.c -@@ -3732,3 +3732,30 @@ - return gp_aes_cbc_decrypt(use_key, key_size, use_iv, iv_size, - in, inlen, out, outlen); - } -+ -+int gp_nlb_hmac_sha1(const unsigned char *key, int key_len, -+ const unsigned char *data1, int len1, -+ const unsigned char *data2, int len2, -+ unsigned char *out) -+{ -+ gnutls_hmac_hd_t hd; -+ -+ if (!key || key_len <= 0 || !data1 || len1 < 0 || !out) -+ return -EINVAL; -+ if (len2 < 0 || (len2 > 0 && !data2)) -+ return -EINVAL; -+ -+ if (gnutls_hmac_init(&hd, GNUTLS_MAC_SHA1, key, key_len) < 0) -+ return -EINVAL; -+ if (gnutls_hmac(hd, data1, len1) < 0) { -+ gnutls_hmac_deinit(hd, NULL); -+ return -EINVAL; -+ } -+ if (len2 > 0 && gnutls_hmac(hd, data2, len2) < 0) { -+ gnutls_hmac_deinit(hd, NULL); -+ return -EINVAL; -+ } -+ gnutls_hmac_output(hd, out); -+ gnutls_hmac_deinit(hd, NULL); -+ return 0; -+} -diff --git a/gpst_nlb.c b/gpst_nlb.c ---- a/gpst_nlb.c -+++ b/gpst_nlb.c -@@ -27,11 +27,12 @@ - * - * NLB UDP envelope (PanGPS 6.3.3.1 RE, outside ESP ciphertext): - * 0-3 u32 magic 0x1a2b3c4d (same as GPST SSL tunnel header) -- * 4-5 u16 (unused on decap for standard types) -+ * 4-5 u16 wire sequence (pan_hmac counter) - * 6-7 u16 inner_len BE (= wire_len - 16) - * 8 u8 type: 0=keepalive, 1=data - * 9 u8 flag -- * 10-15 reserved (zero) -+ * 10-15 HMAC-SHA1(hs-key, header prefix [+ ESP]) truncated to 6 bytes, -+ * or static auth material from decrypted tunnel-opaque offset +8 - * 16+ inner payload (ESP ciphertext for type 1) - * - * SSL failover path (PanSslVpn.cpp): -@@ -83,7 +84,13 @@ - #define GP_NLB_BLOB_SESSION_OFF 24 - #define GP_NLB_BLOB_CIPHER_OFF 28 - #define GP_NLB_ENC_HS_KEY_CIPHER_LEN 36 -+#define GP_NLB_HDR_AUTH_LEN 6 -+#define GP_NLB_OPAQUE_AUTH_OFF 8 -+#define GP_NLB_HDR_SIGN_LEN 10 - -+static void gp_nlb_apply_decrypted_opaque(struct openconnect_info *vpninfo, -+ struct gp_nlb_config *nlb); -+ - static int gp_nlb_is_ipv4(const char *s) - { - struct in_addr a; -@@ -140,6 +147,8 @@ - nlb->decrypted_opaque_len = 0; - memset(nlb->opaque_key, 0, sizeof(nlb->opaque_key)); - nlb->opaque_key_len = 0; -+ memset(nlb->hdr_auth, 0, sizeof(nlb->hdr_auth)); -+ nlb->opaque_auth_ready = 0; - } - - static void gp_nlb_clear_opaque_blob(struct gp_nlb_config *nlb) -@@ -233,21 +242,85 @@ - gp_nlb_clear_decrypted_opaque(nlb); - nlb->decrypted_opaque = out; - nlb->decrypted_opaque_len = out_len; -+ gp_nlb_apply_decrypted_opaque(vpninfo, nlb); - vpn_progress(vpninfo, PRG_DEBUG, - _("Decrypted tunnel-opaque body (%d bytes)\n"), out_len); - return 0; - } - --static void gp_nlb_write_hdr(unsigned char *hdr, uint16_t inner_len, uint8_t type) -+static void gp_nlb_apply_decrypted_opaque(struct openconnect_info *vpninfo, -+ struct gp_nlb_config *nlb) - { -+ if (!nlb->decrypted_opaque || -+ nlb->decrypted_opaque_len < GP_NLB_OPAQUE_AUTH_OFF + GP_NLB_HDR_AUTH_LEN) -+ return; -+ -+ memcpy(nlb->hdr_auth, -+ nlb->decrypted_opaque + GP_NLB_OPAQUE_AUTH_OFF, -+ GP_NLB_HDR_AUTH_LEN); -+ nlb->opaque_auth_ready = 1; -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("NLB tunnel-opaque auth material ready\n")); -+} -+ -+static void gp_nlb_write_hdr(unsigned char *hdr, struct gp_nlb_config *nlb, -+ uint16_t inner_len, uint8_t type) -+{ - store_be32(hdr, GP_NLB_MAGIC); -- store_be16(hdr + 4, 0); -+ store_be16(hdr + 4, nlb->wire_seq++); - store_be16(hdr + 6, inner_len); - hdr[8] = type; - hdr[9] = 0; -- memset(hdr + 10, 0, 6); -+ memset(hdr + 10, 0, GP_NLB_HDR_AUTH_LEN); - } - -+static int gp_nlb_sign_hdr(struct openconnect_info *vpninfo, -+ struct gp_nlb_config *nlb, -+ unsigned char *hdr, -+ const unsigned char *esp, int esplen) -+{ -+ unsigned char mac[SHA1_SIZE]; -+ -+ if (nlb->hs_key_len) { -+ if (gp_nlb_hmac_sha1(nlb->hs_key, nlb->hs_key_len, -+ hdr, GP_NLB_HDR_SIGN_LEN, -+ esp, esplen, mac) < 0) -+ return -EINVAL; -+ memcpy(hdr + GP_NLB_HDR_SIGN_LEN, mac, GP_NLB_HDR_AUTH_LEN); -+ return 0; -+ } -+ -+ if (nlb->opaque_auth_ready) -+ memcpy(hdr + GP_NLB_HDR_SIGN_LEN, nlb->hdr_auth, GP_NLB_HDR_AUTH_LEN); -+ -+ return 0; -+} -+ -+static int gp_nlb_verify_hdr(struct openconnect_info *vpninfo, -+ struct gp_nlb_config *nlb, -+ const unsigned char *hdr, -+ const unsigned char *esp, int esplen) -+{ -+ unsigned char mac[SHA1_SIZE]; -+ const unsigned char *expect; -+ -+ if (!nlb->hs_key_len) -+ return 0; -+ -+ if (gp_nlb_hmac_sha1(nlb->hs_key, nlb->hs_key_len, -+ hdr, GP_NLB_HDR_SIGN_LEN, -+ esp, esplen, mac) < 0) -+ return -EINVAL; -+ -+ expect = hdr + GP_NLB_HDR_SIGN_LEN; -+ if (memcmp(expect, mac, GP_NLB_HDR_AUTH_LEN)) { -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("NLB envelope HMAC mismatch\n")); -+ return -EINVAL; -+ } -+ return 0; -+} -+ - int gpst_nlb_pkt_slack(struct openconnect_info *vpninfo) - { - return vpninfo->gp_nlb.enabled ? GP_NLB_HDR_LEN : 0; -@@ -263,6 +336,7 @@ - return 0; - - nlb->keepalive_sent = 0; -+ nlb->wire_seq = 0; - - if (nlb->tunnel_vip && inet_pton(AF_INET, nlb->tunnel_vip, &vip) == 1) { - vpninfo->esp_magic_af = AF_INET; -@@ -323,7 +397,9 @@ - if (!vpninfo->gp_nlb.enabled || vpninfo->dtls_fd < 0) - return 0; - -- gp_nlb_write_hdr(hdr, 0, GP_NLB_TYPE_KEEPALIVE); -+ gp_nlb_write_hdr(hdr, &vpninfo->gp_nlb, 0, GP_NLB_TYPE_KEEPALIVE); -+ if (gp_nlb_sign_hdr(vpninfo, &vpninfo->gp_nlb, hdr, NULL, 0) < 0) -+ return -EINVAL; - vpn_progress(vpninfo, PRG_DEBUG, _("Send NLB ESP keepalive\n")); - if (send(vpninfo->dtls_fd, hdr, sizeof(hdr), 0) < 0) { - vpn_progress(vpninfo, PRG_DEBUG, -@@ -353,7 +429,10 @@ - return -ENOSPC; - - memmove(wire + GP_NLB_HDR_LEN, wire, esplen); -- gp_nlb_write_hdr(wire, esplen, GP_NLB_TYPE_DATA); -+ gp_nlb_write_hdr(wire, &vpninfo->gp_nlb, esplen, GP_NLB_TYPE_DATA); -+ if (gp_nlb_sign_hdr(vpninfo, &vpninfo->gp_nlb, wire, -+ wire + GP_NLB_HDR_LEN, esplen) < 0) -+ return -EINVAL; - *len = esplen + GP_NLB_HDR_LEN; - return 0; - } -@@ -388,11 +467,17 @@ - } - - if (type == GP_NLB_TYPE_KEEPALIVE) { -+ if (gp_nlb_verify_hdr(vpninfo, &vpninfo->gp_nlb, wire, NULL, 0) < 0) -+ return -EINVAL; - vpn_progress(vpninfo, PRG_DEBUG, - _("Got NLB ESP keepalive response\n")); - return 1; - } - -+ if (gp_nlb_verify_hdr(vpninfo, &vpninfo->gp_nlb, wire, -+ wire + GP_NLB_HDR_LEN, inner_len) < 0) -+ return -EINVAL; -+ - memmove(wire, wire + GP_NLB_HDR_LEN, inner_len); - *len = inner_len; - return 0; -diff --git a/openconnect-internal.h b/openconnect-internal.h ---- a/openconnect-internal.h -+++ b/openconnect-internal.h -@@ -410,6 +410,9 @@ - uint32_t blob_session_id; - unsigned char opaque_key[48]; - int opaque_key_len; -+ unsigned char hdr_auth[6]; -+ int opaque_auth_ready; -+ uint16_t wire_seq; - int keepalive_sent; - }; - -@@ -1535,6 +1538,10 @@ - const unsigned char *iv, int iv_len, - const unsigned char *in, int inlen, - unsigned char *out, int *outlen); -+int gp_nlb_hmac_sha1(const unsigned char *key, int key_len, -+ const unsigned char *data1, int len1, -+ const unsigned char *data2, int len2, -+ unsigned char *out); - - /* {gnutls,openssl}.c */ - const char *openconnect_get_tls_library_version(void); -diff --git a/openssl.c b/openssl.c ---- a/openssl.c -+++ b/openssl.c -@@ -2933,3 +2933,30 @@ - in, inlen, out, outlen); - } - -+int gp_nlb_hmac_sha1(const unsigned char *key, int key_len, -+ const unsigned char *data1, int len1, -+ const unsigned char *data2, int len2, -+ unsigned char *out) -+{ -+ HMAC_CTX *ctx; -+ unsigned int mdlen = SHA1_SIZE; -+ -+ if (!key || key_len <= 0 || !data1 || len1 < 0 || !out) -+ return -EINVAL; -+ if (len2 < 0 || (len2 > 0 && !data2)) -+ return -EINVAL; -+ -+ ctx = HMAC_CTX_new(); -+ if (!ctx) -+ return -ENOMEM; -+ if (!HMAC_Init_ex(ctx, key, key_len, EVP_sha1(), NULL) || -+ !HMAC_Update(ctx, data1, len1) || -+ (len2 > 0 && !HMAC_Update(ctx, data2, len2)) || -+ !HMAC_Final(ctx, out, &mdlen)) { -+ HMAC_CTX_free(ctx); -+ return -EINVAL; -+ } -+ HMAC_CTX_free(ctx); -+ return 0; -+} -+ -diff --git a/tests/Makefile.am b/tests/Makefile.am ---- a/tests/Makefile.am -+++ b/tests/Makefile.am -@@ -88,10 +88,11 @@ - id-test obsolete-server-crypto pfs auth-swtpm auth-hwtpm fortinet-auth-and-config \ - f5-auth-and-config juniper-auth juniper-sso-auth gp-auth-and-config auth-pkcs11 \ - auth-multicert gp-nlb-ssl-parse gp-nlb-esp-envelope gp-nlb-ssl-connect \ -- gp-nlb-opaque-due gp-nlb-auth-and-config gp-esp-gcm-roundtrip gp-nlb-blob-parse -+ gp-nlb-opaque-due gp-nlb-auth-and-config gp-esp-gcm-roundtrip gp-nlb-blob-parse \ -+ gp-nlb-opaque-auth - - TESTS += gp-nlb-ssl-parse gp-nlb-esp-envelope gp-nlb-ssl-connect gp-nlb-opaque-due \ -- gp-esp-gcm-roundtrip gp-nlb-blob-parse -+ gp-esp-gcm-roundtrip gp-nlb-blob-parse gp-nlb-opaque-auth - - if HAVE_CWRAP - TESTS += auth-username-pass auth-certificate auth-nonascii cert-fingerprint id-test \ -diff --git a/tests/gp-nlb-opaque-auth b/tests/gp-nlb-opaque-auth -new file mode 100755 -index 0000000..1111111 ---- /dev/null -+++ b/tests/gp-nlb-opaque-auth -@@ -0,0 +1,83 @@ -+#!/bin/sh -+# Validate NLB tunnel-opaque auth extraction and HMAC-SHA1 header signing -+set -eu -+cd "$(dirname "$0")/.." -+CC="${CC:-cc}" -+TMP=$(mktemp -d) -+trap 'rm -rf "$TMP"' EXIT -+ -+cat > "$TMP/test.c" <<'EOF' -+#include -+#include -+#include -+#include -+ -+#define GP_NLB_HDR_LEN 16 -+#define GP_NLB_MAGIC 0x1a2b3c4d -+#define GP_NLB_HDR_AUTH_LEN 6 -+#define GP_NLB_OPAQUE_AUTH_OFF 8 -+#define GP_NLB_HDR_SIGN_LEN 10 -+ -+static inline void store_be32(void *p, uint32_t d) -+{ -+ uint32_t n = htonl(d); -+ memcpy(p, &n, 4); -+} -+ -+static inline void store_be16(void *p, uint16_t d) -+{ -+ uint16_t n = htons(d); -+ memcpy(p, &n, 2); -+} -+ -+static inline uint32_t load_be32(const void *p) -+{ -+ uint32_t n; -+ memcpy(&n, p, 4); -+ return ntohl(n); -+} -+ -+static void write_hdr(unsigned char *hdr, uint16_t seq, uint16_t inner_len, uint8_t type) -+{ -+ store_be32(hdr, GP_NLB_MAGIC); -+ store_be16(hdr + 4, seq); -+ store_be16(hdr + 6, inner_len); -+ hdr[8] = type; -+ hdr[9] = 0; -+ memset(hdr + 10, 0, GP_NLB_HDR_AUTH_LEN); -+} -+ -+int main(void) -+{ -+ unsigned char opaque[32]; -+ unsigned char hdr_auth[GP_NLB_HDR_AUTH_LEN]; -+ unsigned char hdr[GP_NLB_HDR_LEN]; -+ int i; -+ -+ for (i = 0; i < (int)sizeof(opaque); i++) -+ opaque[i] = (unsigned char)i; -+ memcpy(hdr_auth, opaque + GP_NLB_OPAQUE_AUTH_OFF, GP_NLB_HDR_AUTH_LEN); -+ if (hdr_auth[0] != 0x08 || hdr_auth[1] != 0x09 || hdr_auth[5] != 0x0d) -+ return 1; -+ -+ write_hdr(hdr, 0x0102, 0x0304, 1); -+ if (load_be32(hdr) != GP_NLB_MAGIC) -+ return 1; -+ if (hdr[8] != 1 || hdr[9] != 0) -+ return 1; -+ -+ /* Precomputed with hs-key=0xa0..0xaf, seq=1, inner_len=8, type=1, esp=11223344... */ -+ write_hdr(hdr, 1, 8, 1); -+ memcpy(hdr + GP_NLB_HDR_SIGN_LEN, -+ (unsigned char[]){ 0x92, 0x72, 0xfd, 0x6e, 0xa1, 0xd6 }, GP_NLB_HDR_AUTH_LEN); -+ -+ write_hdr(hdr, 2, 0, 0); -+ memcpy(hdr + GP_NLB_HDR_SIGN_LEN, -+ (unsigned char[]){ 0x82, 0xb4, 0xd9, 0xc5, 0x67, 0xbf }, GP_NLB_HDR_AUTH_LEN); -+ -+ printf("gp-nlb-opaque-auth: OK\n"); -+ return 0; -+} -+EOF -+$CC -o "$TMP/test" "$TMP/test.c" -+"$TMP/test" -\ No newline at end of file diff --git a/crates/openconnect/deps/patches/0016-Add-NLB-troubleshooting-logs.patch b/crates/openconnect/deps/patches/0016-Add-NLB-troubleshooting-logs.patch deleted file mode 100644 index 0e1d03a..0000000 --- a/crates/openconnect/deps/patches/0016-Add-NLB-troubleshooting-logs.patch +++ /dev/null @@ -1,143 +0,0 @@ -From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: Kevin Yue -Date: Thu, 25 Jun 2026 23:55:00 +0800 -Subject: [PATCH 16/16] Add NLB troubleshooting logs - -Add verbose logging for NLB opaque crypto, envelope signing/verification, -and invalid packet diagnostics to aid live gateway testing. ---- ---- a/gpst_nlb.c -+++ b/gpst_nlb.c -@@ -196,8 +196,8 @@ - GP_NLB_ENC_HS_KEY_CIPHER_LEN, - plain, &plain_len); - if (ret < 0) { -- vpn_progress(vpninfo, PRG_DEBUG, -- _("enc-hs-key TLS decipherment failed; NLB opaque crypto pending\n")); -+ vpn_progress(vpninfo, PRG_INFO, -+ _("enc-hs-key TLS decipherment failed; tunnel-opaque decrypt unavailable\n")); - return 0; - } - -@@ -234,7 +234,7 @@ - if (gp_ssl_decrypt_blob_key(vpninfo, nlb->opaque_key, nlb->opaque_key_len, - NULL, 0, payload, payload_len, out, &out_len) < 0) { - free(out); -- vpn_progress(vpninfo, PRG_DEBUG, -+ vpn_progress(vpninfo, PRG_INFO, - _("tunnel-opaque body decipherment failed\n")); - return 0; - } -@@ -251,16 +251,27 @@ - static void gp_nlb_apply_decrypted_opaque(struct openconnect_info *vpninfo, - struct gp_nlb_config *nlb) - { -- if (!nlb->decrypted_opaque || -- nlb->decrypted_opaque_len < GP_NLB_OPAQUE_AUTH_OFF + GP_NLB_HDR_AUTH_LEN) -+ if (!nlb->decrypted_opaque) { -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("NLB decrypted opaque missing; cannot extract hdr auth material\n")); -+ return; -+ } -+ if (nlb->decrypted_opaque_len < GP_NLB_OPAQUE_AUTH_OFF + GP_NLB_HDR_AUTH_LEN) { -+ vpn_progress(vpninfo, PRG_INFO, -+ _("NLB decrypted opaque too short (%d bytes, need %d); auth material unavailable\n"), -+ nlb->decrypted_opaque_len, -+ GP_NLB_OPAQUE_AUTH_OFF + GP_NLB_HDR_AUTH_LEN); - return; -+ } - - memcpy(nlb->hdr_auth, - nlb->decrypted_opaque + GP_NLB_OPAQUE_AUTH_OFF, - GP_NLB_HDR_AUTH_LEN); - nlb->opaque_auth_ready = 1; - vpn_progress(vpninfo, PRG_DEBUG, -- _("NLB tunnel-opaque auth material ready\n")); -+ _("NLB tunnel-opaque auth material ready (offset %d, %d bytes)\n"), -+ GP_NLB_OPAQUE_AUTH_OFF, GP_NLB_HDR_AUTH_LEN); -+ dump_buf_hex(vpninfo, PRG_DEBUG, '=', nlb->hdr_auth, GP_NLB_HDR_AUTH_LEN); - } - - static void gp_nlb_write_hdr(unsigned char *hdr, struct gp_nlb_config *nlb, -@@ -287,12 +298,21 @@ - esp, esplen, mac) < 0) - return -EINVAL; - memcpy(hdr + GP_NLB_HDR_SIGN_LEN, mac, GP_NLB_HDR_AUTH_LEN); -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("NLB sign: seq=%u type=%u inner_len=%u esplen=%d mode=hmac-sha1\n"), -+ load_be16(hdr + 4), hdr[8], load_be16(hdr + 6), esplen); -+ dump_buf_hex(vpninfo, PRG_DEBUG, '>', hdr, GP_NLB_HDR_LEN); - return 0; - } - - if (nlb->opaque_auth_ready) - memcpy(hdr + GP_NLB_HDR_SIGN_LEN, nlb->hdr_auth, GP_NLB_HDR_AUTH_LEN); - -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("NLB sign: seq=%u type=%u inner_len=%u esplen=%d mode=%s\n"), -+ load_be16(hdr + 4), hdr[8], load_be16(hdr + 6), esplen, -+ nlb->opaque_auth_ready ? "static-opaque" : "unsigned"); -+ dump_buf_hex(vpninfo, PRG_DEBUG, '>', hdr, GP_NLB_HDR_LEN); - return 0; - } - -@@ -304,8 +324,13 @@ - unsigned char mac[SHA1_SIZE]; - const unsigned char *expect; - -- if (!nlb->hs_key_len) -+ if (!nlb->hs_key_len) { -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("NLB verify skipped (no hs-key): seq=%u type=%u inner_len=%u esplen=%d opaque_auth=%d\n"), -+ load_be16(hdr + 4), hdr[8], load_be16(hdr + 6), esplen, -+ nlb->opaque_auth_ready); - return 0; -+ } - - if (gp_nlb_hmac_sha1(nlb->hs_key, nlb->hs_key_len, - hdr, GP_NLB_HDR_SIGN_LEN, -@@ -315,9 +340,18 @@ - expect = hdr + GP_NLB_HDR_SIGN_LEN; - if (memcmp(expect, mac, GP_NLB_HDR_AUTH_LEN)) { - vpn_progress(vpninfo, PRG_DEBUG, -- _("NLB envelope HMAC mismatch\n")); -+ _("NLB envelope HMAC mismatch: seq=%u type=%u inner_len=%u esplen=%d\n"), -+ load_be16(hdr + 4), hdr[8], load_be16(hdr + 6), esplen); -+ dump_buf_hex(vpninfo, PRG_DEBUG, '<', (unsigned char *)expect, -+ GP_NLB_HDR_AUTH_LEN); -+ dump_buf_hex(vpninfo, PRG_DEBUG, '=', mac, GP_NLB_HDR_AUTH_LEN); -+ dump_buf_hex(vpninfo, PRG_DEBUG, '>', (unsigned char *)hdr, -+ GP_NLB_HDR_LEN); - return -EINVAL; - } -+ vpn_progress(vpninfo, PRG_DEBUG, -+ _("NLB verify ok: seq=%u type=%u inner_len=%u esplen=%d\n"), -+ load_be16(hdr + 4), hdr[8], load_be16(hdr + 6), esplen); - return 0; - } - -@@ -380,6 +414,11 @@ - gp_nlb_decrypt_opaque_body(vpninfo, nlb); - } - -+ vpn_progress(vpninfo, PRG_INFO, -+ _("NLB crypto ready: hs_key=%d bytes opaque_key=%d bytes opaque_auth=%s decrypted_opaque=%d bytes\n"), -+ nlb->hs_key_len, nlb->opaque_key_len, -+ nlb->opaque_auth_ready ? "yes" : "no", -+ nlb->decrypted_opaque_len); - return 0; - } - -@@ -461,8 +500,10 @@ - if (magic != GP_NLB_MAGIC || type > 8 || - wire_len != GP_NLB_HDR_LEN + inner_len) { - vpn_progress(vpninfo, PRG_DEBUG, -- _("invalid tunnel packet len %d magic 0x%x type %u\n"), -- wire_len, magic, type); -+ _("invalid NLB tunnel packet: wire_len=%d inner_len=%u magic=0x%x type=%u (expected %d)\n"), -+ wire_len, inner_len, magic, type, -+ GP_NLB_HDR_LEN + inner_len); -+ dump_buf_hex(vpninfo, PRG_DEBUG, '<', wire, GP_NLB_HDR_LEN); - return -EINVAL; - } - diff --git a/crates/openconnect/deps/patches/0017-Harden-NLB-crypto-and-envelope-auth.patch b/crates/openconnect/deps/patches/0017-Harden-NLB-crypto-and-envelope-auth.patch deleted file mode 100644 index aad0ccc..0000000 --- a/crates/openconnect/deps/patches/0017-Harden-NLB-crypto-and-envelope-auth.patch +++ /dev/null @@ -1,446 +0,0 @@ -From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: Kevin Yue -Date: Thu, 26 Jun 2026 00:15:00 +0800 -Subject: [PATCH 17/17] Harden NLB crypto and envelope auth - -Fail hard on opaque decrypt errors, require hs-key for envelope HMAC, -reject TLS 1.3 portal sessions for blob decrypt, fix GnuTLS AES-192, -tighten packet type validation, and add a real HMAC-SHA1 vector test. ---- ---- a/gpst_nlb.c -+++ b/gpst_nlb.c -@@ -31,8 +31,7 @@ - * 6-7 u16 inner_len BE (= wire_len - 16) - * 8 u8 type: 0=keepalive, 1=data - * 9 u8 flag -- * 10-15 HMAC-SHA1(hs-key, header prefix [+ ESP]) truncated to 6 bytes, -- * or static auth material from decrypted tunnel-opaque offset +8 -+ * 10-15 HMAC-SHA1(hs-key, header prefix [+ ESP]) truncated to 6 bytes - * 16+ inner payload (ESP ciphertext for type 1) - * - * SSL failover path (PanSslVpn.cpp): -@@ -196,9 +195,9 @@ - GP_NLB_ENC_HS_KEY_CIPHER_LEN, - plain, &plain_len); - if (ret < 0) { -- vpn_progress(vpninfo, PRG_INFO, -- _("enc-hs-key TLS decipherment failed; tunnel-opaque decrypt unavailable\n")); -- return 0; -+ vpn_progress(vpninfo, PRG_ERR, -+ _("enc-hs-key TLS decipherment failed\n")); -+ return -EINVAL; - } - - if (plain_len <= 0 || plain_len > (int)sizeof(nlb->opaque_key)) -@@ -222,7 +221,7 @@ - if (!nlb->opaque_blob || nlb->opaque_blob_len <= GP_NLB_BLOB_CIPHER_OFF) - return 0; - if (!nlb->opaque_key_len) -- return 0; -+ return -EINVAL; - - payload = nlb->opaque_blob + GP_NLB_BLOB_CIPHER_OFF; - payload_len = nlb->opaque_blob_len - GP_NLB_BLOB_CIPHER_OFF; -@@ -234,9 +233,9 @@ - if (gp_ssl_decrypt_blob_key(vpninfo, nlb->opaque_key, nlb->opaque_key_len, - NULL, 0, payload, payload_len, out, &out_len) < 0) { - free(out); -- vpn_progress(vpninfo, PRG_INFO, -+ vpn_progress(vpninfo, PRG_ERR, - _("tunnel-opaque body decipherment failed\n")); -- return 0; -+ return -EINVAL; - } - - gp_nlb_clear_decrypted_opaque(nlb); -@@ -292,26 +291,17 @@ - { - unsigned char mac[SHA1_SIZE]; - -- if (nlb->hs_key_len) { -- if (gp_nlb_hmac_sha1(nlb->hs_key, nlb->hs_key_len, -- hdr, GP_NLB_HDR_SIGN_LEN, -- esp, esplen, mac) < 0) -- return -EINVAL; -- memcpy(hdr + GP_NLB_HDR_SIGN_LEN, mac, GP_NLB_HDR_AUTH_LEN); -- vpn_progress(vpninfo, PRG_DEBUG, -- _("NLB sign: seq=%u type=%u inner_len=%u esplen=%d mode=hmac-sha1\n"), -- load_be16(hdr + 4), hdr[8], load_be16(hdr + 6), esplen); -- dump_buf_hex(vpninfo, PRG_DEBUG, '>', hdr, GP_NLB_HDR_LEN); -- return 0; -- } -+ if (!nlb->hs_key_len) -+ return -EINVAL; - -- if (nlb->opaque_auth_ready) -- memcpy(hdr + GP_NLB_HDR_SIGN_LEN, nlb->hdr_auth, GP_NLB_HDR_AUTH_LEN); -- -+ if (gp_nlb_hmac_sha1(nlb->hs_key, nlb->hs_key_len, -+ hdr, GP_NLB_HDR_SIGN_LEN, -+ esp, esplen, mac) < 0) -+ return -EINVAL; -+ memcpy(hdr + GP_NLB_HDR_SIGN_LEN, mac, GP_NLB_HDR_AUTH_LEN); - vpn_progress(vpninfo, PRG_DEBUG, -- _("NLB sign: seq=%u type=%u inner_len=%u esplen=%d mode=%s\n"), -- load_be16(hdr + 4), hdr[8], load_be16(hdr + 6), esplen, -- nlb->opaque_auth_ready ? "static-opaque" : "unsigned"); -+ _("NLB sign: seq=%u type=%u inner_len=%u esplen=%d\n"), -+ load_be16(hdr + 4), hdr[8], load_be16(hdr + 6), esplen); - dump_buf_hex(vpninfo, PRG_DEBUG, '>', hdr, GP_NLB_HDR_LEN); - return 0; - } -@@ -324,13 +314,8 @@ - unsigned char mac[SHA1_SIZE]; - const unsigned char *expect; - -- if (!nlb->hs_key_len) { -- vpn_progress(vpninfo, PRG_DEBUG, -- _("NLB verify skipped (no hs-key): seq=%u type=%u inner_len=%u esplen=%d opaque_auth=%d\n"), -- load_be16(hdr + 4), hdr[8], load_be16(hdr + 6), esplen, -- nlb->opaque_auth_ready); -- return 0; -- } -+ if (!nlb->hs_key_len) -+ return -EINVAL; - - if (gp_nlb_hmac_sha1(nlb->hs_key, nlb->hs_key_len, - hdr, GP_NLB_HDR_SIGN_LEN, -@@ -364,7 +349,7 @@ - { - struct gp_nlb_config *nlb = &vpninfo->gp_nlb; - struct in_addr vip; -- int len; -+ int len, ret; - - if (!nlb->enabled) - return 0; -@@ -392,9 +377,11 @@ - return -EINVAL; - } - gp_nlb_parse_blob_header(vpninfo, enc_blob, len, nlb); -- gp_nlb_decrypt_enc_hs_key(vpninfo, nlb, enc_blob, len); -+ ret = gp_nlb_decrypt_enc_hs_key(vpninfo, nlb, enc_blob, len); - memset(enc_blob, 0, len); - free(enc_blob); -+ if (ret < 0) -+ return ret; - } - - if (nlb->tunnel_opaque) { -@@ -411,14 +398,30 @@ - _("Decoded tunnel-opaque (%d bytes)\n"), len); - if (!nlb->blob_session_id) - gp_nlb_parse_blob_header(vpninfo, nlb->opaque_blob, len, nlb); -- gp_nlb_decrypt_opaque_body(vpninfo, nlb); -+ ret = gp_nlb_decrypt_opaque_body(vpninfo, nlb); -+ if (ret < 0) -+ return ret; -+ } -+ -+ if (!nlb->hs_key_len) { -+ vpn_progress(vpninfo, PRG_ERR, -+ _("NLB requires hs-key for envelope authentication\n")); -+ return -EINVAL; -+ } -+ if (nlb->enc_hs_key && !nlb->opaque_key_len) { -+ vpn_progress(vpninfo, PRG_ERR, -+ _("NLB enc-hs-key present but decipherment produced no key\n")); -+ return -EINVAL; - } -+ if (nlb->tunnel_opaque && !nlb->decrypted_opaque_len) { -+ vpn_progress(vpninfo, PRG_ERR, -+ _("NLB tunnel-opaque present but decipherment failed\n")); -+ return -EINVAL; -+ } - - vpn_progress(vpninfo, PRG_INFO, -- _("NLB crypto ready: hs_key=%d bytes opaque_key=%d bytes opaque_auth=%s decrypted_opaque=%d bytes\n"), -- nlb->hs_key_len, nlb->opaque_key_len, -- nlb->opaque_auth_ready ? "yes" : "no", -- nlb->decrypted_opaque_len); -+ _("NLB crypto ready: hs_key=%d bytes opaque_key=%d bytes decrypted_opaque=%d bytes\n"), -+ nlb->hs_key_len, nlb->opaque_key_len, nlb->decrypted_opaque_len); - return 0; - } - -@@ -497,7 +500,8 @@ - type = wire[8]; - inner_len = load_be16(wire + 6); - -- if (magic != GP_NLB_MAGIC || type > 8 || -+ if (magic != GP_NLB_MAGIC || -+ (type != GP_NLB_TYPE_KEEPALIVE && type != GP_NLB_TYPE_DATA) || - wire_len != GP_NLB_HDR_LEN + inner_len) { - vpn_progress(vpninfo, PRG_DEBUG, - _("invalid NLB tunnel packet: wire_len=%d inner_len=%u magic=0x%x type=%u (expected %d)\n"), -@@ -624,8 +628,13 @@ - - static int gp_nlb_route_list_contains(struct oc_split_include *list, const char *ip) - { -+ char route[64]; -+ -+ if (snprintf(route, sizeof(route), "%s/32", ip) >= (int)sizeof(route)) -+ return 0; -+ - for (; list; list = list->next) { -- if (list->route && strstr(list->route, ip)) -+ if (list->route && !strcmp(list->route, route)) - return 1; - } - return 0; ---- a/gnutls.c -+++ b/gnutls.c -@@ -3602,6 +3602,29 @@ - - done: - return ret; -+} -+ -+static gnutls_cipher_algorithm_t gp_gnutls_aes_cbc_cipher(size_t key_size) -+{ -+ if (key_size == 32) -+ return GNUTLS_CIPHER_AES_256_CBC; -+ if (key_size == 24) -+ return GNUTLS_CIPHER_AES_192_CBC; -+ return GNUTLS_CIPHER_AES_128_CBC; -+} -+ -+static int gp_ssl_decrypt_session_ok(struct openconnect_info *vpninfo) -+{ -+ gnutls_session_t sess = vpninfo->https_sess; -+ -+ if (!sess) -+ return -EINVAL; -+ if (gnutls_protocol_get_version(sess) > GNUTLS_TLS1_2) { -+ vpn_progress(vpninfo, PRG_ERR, -+ _("NLB blob decrypt requires TLS 1.2 portal session\n")); -+ return -EINVAL; -+ } -+ return 0; - } - - static int gp_aes_cbc_decrypt(const unsigned char *key, size_t key_size, -@@ -3622,8 +3645,7 @@ - iv_d.data = (unsigned char *)iv; - iv_d.size = iv_size; - -- err = gnutls_cipher_init(&hd, key_size == 32 ? GNUTLS_CIPHER_AES_256_CBC : -- GNUTLS_CIPHER_AES_128_CBC, &enc_key, &iv_d); -+ err = gnutls_cipher_init(&hd, gp_gnutls_aes_cbc_cipher(key_size), &enc_key, &iv_d); - if (err) - return -EINVAL; - -@@ -3687,6 +3709,9 @@ - if (!sess || !in || inlen <= 0 || !out || !outlen) - return -EINVAL; - -+ if (gp_ssl_decrypt_session_ok(vpninfo) < 0) -+ return -EINVAL; -+ - if (gp_tls12_expand_keys(sess, key_block, sizeof(key_block), - &key_size, &mac_size, &iv_size, - &srv_key, &srv_iv) < 0) -@@ -3711,6 +3736,9 @@ - if (!sess || !key || key_len <= 0 || !in || inlen <= 0 || !out || !outlen) - return -EINVAL; - -+ if (gp_ssl_decrypt_session_ok(vpninfo) < 0) -+ return -EINVAL; -+ - if (gp_tls12_expand_keys(sess, key_block, sizeof(key_block), - &key_size, &mac_size, &iv_size, - &srv_key, &srv_iv) < 0) ---- a/openssl.c -+++ b/openssl.c -@@ -2834,6 +2834,21 @@ - return 0; - } - -+static int gp_ssl_decrypt_session_ok(struct openconnect_info *vpninfo) -+{ -+ SSL *ssl = vpninfo->https_ssl; -+ -+ if (!ssl) -+ return -EINVAL; -+ if (SSL_version(ssl) > TLS1_2_VERSION) { -+ vpn_progress(vpninfo, PRG_ERR, -+ _("NLB blob decrypt requires TLS 1.2 portal session (got %s)\n"), -+ SSL_get_version(ssl)); -+ return -EINVAL; -+ } -+ return 0; -+} -+ - static int gp_tls12_expand_keys(SSL *ssl, - unsigned char *key_block, size_t key_block_len, - size_t *mac_len, size_t *key_len, size_t *iv_len, -@@ -2894,6 +2909,9 @@ - if (!ssl || !in || inlen <= 0 || !out || !outlen) - return -EINVAL; - -+ if (gp_ssl_decrypt_session_ok(vpninfo) < 0) -+ return -EINVAL; -+ - if (gp_tls12_expand_keys(ssl, key_block, sizeof(key_block), - &mac_len, &key_len, &iv_len, - &srv_key, &srv_iv) < 0) -@@ -2918,6 +2936,9 @@ - if (!ssl || !key || key_len <= 0 || !in || inlen <= 0 || !out || !outlen) - return -EINVAL; - -+ if (gp_ssl_decrypt_session_ok(vpninfo) < 0) -+ return -EINVAL; -+ - if (gp_tls12_expand_keys(ssl, key_block, sizeof(key_block), - &mac_len, &blk_key_len, &blk_iv_len, - &srv_key, &srv_iv) < 0) ---- a/esp.c -+++ b/esp.c -@@ -211,8 +211,11 @@ - - if (nlb_ret > 0) - continue; -- if (nlb_ret < 0) -+ if (nlb_ret < 0) { -+ vpn_progress(vpninfo, PRG_INFO, -+ _("Dropped invalid NLB ESP envelope packet\n")); - continue; -+ } - } - - /* both supported algos (SHA1 and MD5) have 12-byte MAC lengths (RFC2403 and RFC2404) */ ---- a/tests/gp-nlb-opaque-auth -+++ b/tests/gp-nlb-opaque-auth -@@ -1,21 +1,35 @@ - #!/bin/sh --# Validate NLB tunnel-opaque auth extraction and HMAC-SHA1 header signing -+# Validate NLB envelope HMAC-SHA1 signing vectors - set -eu - cd "$(dirname "$0")/.." - CC="${CC:-cc}" - TMP=$(mktemp -d) - trap 'rm -rf "$TMP"' EXIT - -+CFLAGS="${CFLAGS:-}" -+LIBS="${LIBS:-}" -+if pkg-config --exists openssl 2>/dev/null; then -+ CFLAGS="$CFLAGS $(pkg-config --cflags openssl)" -+ LIBS="$LIBS $(pkg-config --libs openssl)" -+elif pkg-config --exists libssl 2>/dev/null; then -+ CFLAGS="$CFLAGS $(pkg-config --cflags libssl)" -+ LIBS="$LIBS $(pkg-config --libs libssl)" -+else -+ CFLAGS="$CFLAGS -I/usr/include -I/usr/local/include -I/opt/homebrew/include" -+ LIBS="$LIBS -lcrypto -lssl" -+fi -+ - cat > "$TMP/test.c" <<'EOF' - #include - #include - #include - #include -+#include -+#include - - #define GP_NLB_HDR_LEN 16 - #define GP_NLB_MAGIC 0x1a2b3c4d - #define GP_NLB_HDR_AUTH_LEN 6 --#define GP_NLB_OPAQUE_AUTH_OFF 8 - #define GP_NLB_HDR_SIGN_LEN 10 - - static inline void store_be32(void *p, uint32_t d) -@@ -30,13 +44,6 @@ - memcpy(p, &n, 2); - } - --static inline uint32_t load_be32(const void *p) --{ -- uint32_t n; -- memcpy(&n, p, 4); -- return ntohl(n); --} -- - static void write_hdr(unsigned char *hdr, uint16_t seq, uint16_t inner_len, uint8_t type) - { - store_be32(hdr, GP_NLB_MAGIC); -@@ -47,37 +54,61 @@ - memset(hdr + 10, 0, GP_NLB_HDR_AUTH_LEN); - } - -+static int nlb_hmac_sha1(const unsigned char *key, int key_len, -+ const unsigned char *hdr, int hdrlen, -+ const unsigned char *esp, int esplen, -+ unsigned char *out) -+{ -+ unsigned char mac[SHA_DIGEST_LENGTH]; -+ unsigned int maclen = sizeof(mac); -+ HMAC_CTX *ctx; -+ -+ ctx = HMAC_CTX_new(); -+ if (!ctx) -+ return -1; -+ if (!HMAC_Init_ex(ctx, key, key_len, EVP_sha1(), NULL) || -+ !HMAC_Update(ctx, hdr, hdrlen) || -+ (esplen > 0 && !HMAC_Update(ctx, esp, esplen)) || -+ !HMAC_Final(ctx, mac, &maclen)) { -+ HMAC_CTX_free(ctx); -+ return -1; -+ } -+ HMAC_CTX_free(ctx); -+ memcpy(out, mac, GP_NLB_HDR_AUTH_LEN); -+ return 0; -+} -+ -+static int check_tag(const unsigned char *got, const unsigned char *expect) -+{ -+ return memcmp(got, expect, GP_NLB_HDR_AUTH_LEN) == 0 ? 0 : 1; -+} -+ - int main(void) - { -- unsigned char opaque[32]; -- unsigned char hdr_auth[GP_NLB_HDR_AUTH_LEN]; -+ unsigned char key[16]; - unsigned char hdr[GP_NLB_HDR_LEN]; -+ unsigned char mac[GP_NLB_HDR_AUTH_LEN]; -+ unsigned char esp[8] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 }; - int i; - -- for (i = 0; i < (int)sizeof(opaque); i++) -- opaque[i] = (unsigned char)i; -- memcpy(hdr_auth, opaque + GP_NLB_OPAQUE_AUTH_OFF, GP_NLB_HDR_AUTH_LEN); -- if (hdr_auth[0] != 0x08 || hdr_auth[1] != 0x09 || hdr_auth[5] != 0x0d) -- return 1; -+ for (i = 0; i < (int)sizeof(key); i++) -+ key[i] = (unsigned char)(0xa0 + i); - -- write_hdr(hdr, 0x0102, 0x0304, 1); -- if (load_be32(hdr) != GP_NLB_MAGIC) -+ write_hdr(hdr, 1, 8, 1); -+ if (nlb_hmac_sha1(key, sizeof(key), hdr, GP_NLB_HDR_SIGN_LEN, esp, sizeof(esp), mac) < 0) - return 1; -- if (hdr[8] != 1 || hdr[9] != 0) -+ if (check_tag(mac, (unsigned char[]){ 0x92, 0x72, 0xfd, 0x6e, 0xa1, 0xd6 })) - return 1; - -- /* Precomputed with hs-key=0xa0..0xaf, seq=1, inner_len=8, type=1, esp=11223344... */ -- write_hdr(hdr, 1, 8, 1); -- memcpy(hdr + GP_NLB_HDR_SIGN_LEN, -- (unsigned char[]){ 0x92, 0x72, 0xfd, 0x6e, 0xa1, 0xd6 }, GP_NLB_HDR_AUTH_LEN); -- - write_hdr(hdr, 2, 0, 0); -- memcpy(hdr + GP_NLB_HDR_SIGN_LEN, -- (unsigned char[]){ 0x82, 0xb4, 0xd9, 0xc5, 0x67, 0xbf }, GP_NLB_HDR_AUTH_LEN); -+ if (nlb_hmac_sha1(key, sizeof(key), hdr, GP_NLB_HDR_SIGN_LEN, NULL, 0, mac) < 0) -+ return 1; -+ if (check_tag(mac, (unsigned char[]){ 0x82, 0xb4, 0xd9, 0xc5, 0x67, 0xbf })) -+ return 1; - - printf("gp-nlb-opaque-auth: OK\n"); - return 0; - } - EOF --$CC -o "$TMP/test" "$TMP/test.c" -+$CC $CFLAGS -o "$TMP/test" "$TMP/test.c" $LIBS - "$TMP/test" -\ No newline at end of file diff --git a/crates/openconnect/deps/patches/0018-Cleanup-NLB-dead-fields-and-error-handling.patch b/crates/openconnect/deps/patches/0018-Cleanup-NLB-dead-fields-and-error-handling.patch deleted file mode 100644 index 7a614fc..0000000 --- a/crates/openconnect/deps/patches/0018-Cleanup-NLB-dead-fields-and-error-handling.patch +++ /dev/null @@ -1,146 +0,0 @@ -From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: Kevin Yue -Date: Thu, 26 Jun 2026 01:00:00 +0800 -Subject: [PATCH 18/18] Cleanup NLB dead fields and error handling - -Remove unused hdr_auth/opaque_auth_ready state, reconnect on opaque -refresh failure, and log outbound NLB encap failures at ERR level. ---- ---- a/gpst_nlb.c -+++ b/gpst_nlb.c -@@ -84,12 +84,8 @@ - #define GP_NLB_BLOB_CIPHER_OFF 28 - #define GP_NLB_ENC_HS_KEY_CIPHER_LEN 36 - #define GP_NLB_HDR_AUTH_LEN 6 --#define GP_NLB_OPAQUE_AUTH_OFF 8 - #define GP_NLB_HDR_SIGN_LEN 10 - --static void gp_nlb_apply_decrypted_opaque(struct openconnect_info *vpninfo, -- struct gp_nlb_config *nlb); -- - static int gp_nlb_is_ipv4(const char *s) - { - struct in_addr a; -@@ -146,8 +142,6 @@ - nlb->decrypted_opaque_len = 0; - memset(nlb->opaque_key, 0, sizeof(nlb->opaque_key)); - nlb->opaque_key_len = 0; -- memset(nlb->hdr_auth, 0, sizeof(nlb->hdr_auth)); -- nlb->opaque_auth_ready = 0; - } - - static void gp_nlb_clear_opaque_blob(struct gp_nlb_config *nlb) -@@ -241,38 +235,11 @@ - gp_nlb_clear_decrypted_opaque(nlb); - nlb->decrypted_opaque = out; - nlb->decrypted_opaque_len = out_len; -- gp_nlb_apply_decrypted_opaque(vpninfo, nlb); - vpn_progress(vpninfo, PRG_DEBUG, - _("Decrypted tunnel-opaque body (%d bytes)\n"), out_len); - return 0; - } - --static void gp_nlb_apply_decrypted_opaque(struct openconnect_info *vpninfo, -- struct gp_nlb_config *nlb) --{ -- if (!nlb->decrypted_opaque) { -- vpn_progress(vpninfo, PRG_DEBUG, -- _("NLB decrypted opaque missing; cannot extract hdr auth material\n")); -- return; -- } -- if (nlb->decrypted_opaque_len < GP_NLB_OPAQUE_AUTH_OFF + GP_NLB_HDR_AUTH_LEN) { -- vpn_progress(vpninfo, PRG_INFO, -- _("NLB decrypted opaque too short (%d bytes, need %d); auth material unavailable\n"), -- nlb->decrypted_opaque_len, -- GP_NLB_OPAQUE_AUTH_OFF + GP_NLB_HDR_AUTH_LEN); -- return; -- } -- -- memcpy(nlb->hdr_auth, -- nlb->decrypted_opaque + GP_NLB_OPAQUE_AUTH_OFF, -- GP_NLB_HDR_AUTH_LEN); -- nlb->opaque_auth_ready = 1; -- vpn_progress(vpninfo, PRG_DEBUG, -- _("NLB tunnel-opaque auth material ready (offset %d, %d bytes)\n"), -- GP_NLB_OPAQUE_AUTH_OFF, GP_NLB_HDR_AUTH_LEN); -- dump_buf_hex(vpninfo, PRG_DEBUG, '=', nlb->hdr_auth, GP_NLB_HDR_AUTH_LEN); --} -- - static void gp_nlb_write_hdr(unsigned char *hdr, struct gp_nlb_config *nlb, - uint16_t inner_len, uint8_t type) - { ---- a/openconnect-internal.h -+++ b/openconnect-internal.h -@@ -410,8 +410,6 @@ - uint32_t blob_session_id; - unsigned char opaque_key[48]; - int opaque_key_len; -- unsigned char hdr_auth[6]; -- int opaque_auth_ready; - uint16_t wire_seq; - int keepalive_sent; - }; ---- a/gpst.c -+++ b/gpst.c -@@ -1343,7 +1343,12 @@ - /* fall through */ - case DTLS_ESTABLISHED: - /* Rekey or check-and-resubmit HIP if needed */ -- if (gpst_nlb_maintenance(vpninfo, timeout) > 0) -+ ret = gpst_nlb_maintenance(vpninfo, timeout); -+ if (ret < 0) { -+ vpninfo->dtls_need_reconnect = 1; -+ return 1; -+ } -+ if (ret > 0) - return 1; - if (keepalive_action(&vpninfo->ssl_times, timeout) == KA_REKEY) - goto do_rekey; -@@ -1375,7 +1380,10 @@ - ; - } - -- if (gpst_nlb_maintenance(vpninfo, timeout) > 0) -+ ret = gpst_nlb_maintenance(vpninfo, timeout); -+ if (ret < 0) -+ goto do_reconnect; -+ if (ret > 0) - work_done = 1; - - if (vpninfo->ssl_fd == -1) -@@ -1801,7 +1809,7 @@ - if (pktlen < 0) - vpn_progress(vpninfo, PRG_DEBUG, _("Failed to send ESP probe\n")); - else if (vpninfo->gp_nlb.enabled && gpst_nlb_esp_encap(vpninfo, pkt, &pktlen) < 0) -- vpn_progress(vpninfo, PRG_DEBUG, _("Failed to NLB-wrap ESP probe\n")); -+ vpn_progress(vpninfo, PRG_ERR, _("Failed to NLB-wrap ESP probe\n")); - else if (send(vpninfo->dtls_fd, (void *)&pkt->esp, pktlen, 0) < 0) - vpn_progress(vpninfo, PRG_DEBUG, _("Failed to send ESP probe\n")); - ---- a/esp.c -+++ b/esp.c -@@ -360,8 +360,13 @@ - vpninfo->dtls_times.last_tx = now; - } - -- if (vpninfo->proto->proto == PROTO_GPST && gpst_nlb_maintenance(vpninfo, timeout) > 0) -- work_done = 1; -+ if (vpninfo->proto->proto == PROTO_GPST) { -+ ret = gpst_nlb_maintenance(vpninfo, timeout); -+ if (ret < 0) -+ goto need_reconnect; -+ if (ret > 0) -+ work_done = 1; -+ } - - if (vpninfo->dtls_state != DTLS_ESTABLISHED) - return work_done; -@@ -459,6 +464,8 @@ - - if (vpninfo->proto->proto == PROTO_GPST && vpninfo->gp_nlb.enabled && - gpst_nlb_esp_encap(vpninfo, this, &len) < 0) { -+ vpn_progress(vpninfo, PRG_ERR, -+ _("Failed to NLB-wrap outbound ESP packet\n")); - free_pkt(vpninfo, this); - work_done = 1; - continue; diff --git a/crates/openconnect/deps/patches/0019-Fix-NLB-prepare-opaque-key-wipe.patch b/crates/openconnect/deps/patches/0019-Fix-NLB-prepare-opaque-key-wipe.patch deleted file mode 100644 index 7ddef57..0000000 --- a/crates/openconnect/deps/patches/0019-Fix-NLB-prepare-opaque-key-wipe.patch +++ /dev/null @@ -1,98 +0,0 @@ -From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: Kevin Yue -Date: Thu, 26 Jun 2026 01:30:00 +0800 -Subject: [PATCH 19/19] Fix NLB prepare opaque-key wipe - -Stop gp_nlb_clear_decrypted_opaque from zeroing opaque_key after -enc-hs-key decipherment, and replace unused decrypted_opaque storage -with an opaque_body_ready flag that discards plaintext after verify. ---- ---- a/gpst_nlb.c -+++ b/gpst_nlb.c -@@ -132,21 +132,11 @@ - return -EINVAL; - } - --static void gp_nlb_clear_decrypted_opaque(struct gp_nlb_config *nlb) -+static void gp_nlb_clear_opaque_blob(struct gp_nlb_config *nlb) - { -- if (nlb->decrypted_opaque) { -- memset(nlb->decrypted_opaque, 0, nlb->decrypted_opaque_len); -- free(nlb->decrypted_opaque); -- nlb->decrypted_opaque = NULL; -- } -- nlb->decrypted_opaque_len = 0; -+ nlb->opaque_body_ready = 0; - memset(nlb->opaque_key, 0, sizeof(nlb->opaque_key)); - nlb->opaque_key_len = 0; --} -- --static void gp_nlb_clear_opaque_blob(struct gp_nlb_config *nlb) --{ -- gp_nlb_clear_decrypted_opaque(nlb); - if (nlb->opaque_blob) { - memset(nlb->opaque_blob, 0, nlb->opaque_blob_len); - free(nlb->opaque_blob); -@@ -232,9 +222,9 @@ - return -EINVAL; - } - -- gp_nlb_clear_decrypted_opaque(nlb); -- nlb->decrypted_opaque = out; -- nlb->decrypted_opaque_len = out_len; -+ memset(out, 0, out_len); -+ free(out); -+ nlb->opaque_body_ready = 1; - vpn_progress(vpninfo, PRG_DEBUG, - _("Decrypted tunnel-opaque body (%d bytes)\n"), out_len); - return 0; -@@ -375,20 +365,16 @@ - _("NLB requires hs-key for envelope authentication\n")); - return -EINVAL; - } -- if (nlb->enc_hs_key && !nlb->opaque_key_len) { -- vpn_progress(vpninfo, PRG_ERR, -- _("NLB enc-hs-key present but decipherment produced no key\n")); -- return -EINVAL; -- } -- if (nlb->tunnel_opaque && !nlb->decrypted_opaque_len) { -+ if (nlb->tunnel_opaque && !nlb->opaque_body_ready) { - vpn_progress(vpninfo, PRG_ERR, - _("NLB tunnel-opaque present but decipherment failed\n")); - return -EINVAL; - } - - vpn_progress(vpninfo, PRG_INFO, -- _("NLB crypto ready: hs_key=%d bytes opaque_key=%d bytes decrypted_opaque=%d bytes\n"), -- nlb->hs_key_len, nlb->opaque_key_len, nlb->decrypted_opaque_len); -+ _("NLB crypto ready: hs_key=%d bytes opaque_key=%d bytes opaque_body=%s\n"), -+ nlb->hs_key_len, nlb->opaque_key_len, -+ nlb->opaque_body_ready ? "ready" : "none"); - return 0; - } - ---- a/openconnect-internal.h -+++ b/openconnect-internal.h -@@ -405,8 +405,7 @@ - char *in_tunnel_gw_cert_chksum; - unsigned char *opaque_blob; - int opaque_blob_len; -- unsigned char *decrypted_opaque; -- int decrypted_opaque_len; -+ int opaque_body_ready; - uint32_t blob_session_id; - unsigned char opaque_key[48]; - int opaque_key_len; ---- a/library.c -+++ b/library.c -@@ -845,10 +845,6 @@ - free(vpninfo->gp_nlb.tunnel_vip); - free(vpninfo->gp_nlb.connected_gw_ip); - free(vpninfo->gp_nlb.in_tunnel_gw_cert_chksum); -- if (vpninfo->gp_nlb.decrypted_opaque) { -- memset(vpninfo->gp_nlb.decrypted_opaque, 0, vpninfo->gp_nlb.decrypted_opaque_len); -- free(vpninfo->gp_nlb.decrypted_opaque); -- } - memset(vpninfo->gp_nlb.opaque_key, 0, sizeof(vpninfo->gp_nlb.opaque_key)); - if (vpninfo->gp_nlb.opaque_blob) { - memset(vpninfo->gp_nlb.opaque_blob, 0, vpninfo->gp_nlb.opaque_blob_len); \ No newline at end of file