Harden NLB crypto and envelope authentication

Fail prepare when opaque decrypt or hs-key setup is incomplete, require
hs-key for all envelope sign/verify, reject TLS 1.3 for blob decrypt,
fix GnuTLS AES-192 CBC selection, and replace the opaque-auth test with
real HMAC-SHA1 vector checks.
This commit is contained in:
Kevin Yue committed 2026-06-25 20:53:55 +08:00
1 parent ebd5e69260
commit c93c9ec67f
1 file changed
+446
@@ -0,0 +1,446 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Kevin Yue <[email protected]>
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 <stdio.h>
#include <stdint.h>
#include <string.h>
#include <arpa/inet.h>
+#include <openssl/hmac.h>
+#include <openssl/sha.h>
#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