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