[Git][NTPsec/ntpsec][master] ntpd/nts_client.c: RFC 8915 fixes for NTS-KE response parsing

Gary E. Miller (@garyedmundsmiller) gitlab at mg.gitlab.com
Tue Nov 10 01:31:47 UTC 2026



Gary E. Miller pushed to branch master at NTPsec / ntpsec


Commits:
fd749194 by Matt Selsky at 2026-11-09T17:29:28-08:00
ntpd/nts_client.c: RFC 8915 fixes for NTS-KE response parsing

- - - - -


2 changed files:

- ntpd/nts_client.c
- tests/ntpd/nts_client.c


Changes:

=====================================
ntpd/nts_client.c
=====================================
@@ -673,9 +673,26 @@ bool nts_client_send_request(SSL *ssl, struct peer* peer) {
         return true;
 }
 
+// The AEAD algorithm the client offers
+static uint16_t nts_client_aead(const struct peer *peer) {
+        uint16_t aead = NO_AEAD;
+
+        // FIXME: the aead option should accept a colon-separated list of
+        // algorithms; only the first is offered
+        if (NULL != peer->cfg.nts_cfg.aead) {
+                aead = nts_string_to_aead(peer->cfg.nts_cfg.aead);
+        }
+        if ((NO_AEAD == aead) && (NULL != ntsconfig.aead)) {
+                aead = nts_string_to_aead(ntsconfig.aead);
+        }
+        if (NO_AEAD == aead) {
+                aead = AEAD_AES_SIV_CMAC_256;
+        }
+        return aead;
+}
+
 bool nts_client_send_request_core(uint8_t *buff, int buf_size, int *used, struct peer* peer) {
         struct  BufCtl_t buf;
-        uint16_t aead = NO_AEAD;
 
         buf.next = buff;
         buf.left = buf_size;
@@ -685,15 +702,8 @@ bool nts_client_send_request_core(uint8_t *buff, int buf_size, int *used, struct
                                 NTS_CRITICAL+nts_next_protocol_negotiation, nts_protocol_NTP);
 
         // 4.1.5 AEAD Algorithm List
-        // FIXME should be : separated list
-
-        if ((NO_AEAD == aead) && (NULL != peer->cfg.nts_cfg.aead))
-                aead = nts_string_to_aead(peer->cfg.nts_cfg.aead);
-        if ((NO_AEAD == aead) && (NULL != ntsconfig.aead))
-                aead = nts_string_to_aead(ntsconfig.aead);
-        if (NO_AEAD == aead)
-                aead = AEAD_AES_SIV_CMAC_256;
-        ke_append_record_uint16(&buf, nts_algorithm_negotiation, aead);
+        ke_append_record_uint16(&buf, nts_algorithm_negotiation,
+                                nts_client_aead(peer));
 
         // 4.1.1: End, Critical
         ke_append_record_null(&buf, NTS_CRITICAL+nts_end_of_message);
@@ -707,23 +717,34 @@ bool nts_client_send_request_core(uint8_t *buff, int buf_size, int *used, struct
         return true;
 }
 
+// RFC 8915 4: clients SHOULD accept responses of at least 65536 octets
+#define NTS_MAX_RESPONSE 65536
+
 bool nts_client_process_response(SSL *ssl, struct peer* peer) {
-        uint8_t  buff[2048];  // RFC 4. says SHOULD be 65K
+        // Read into a 65536-octet buffer, allocated on the heap, because
+        // the NTS-KE client runs in a thread whose stack may be small,
+        // such as musl's 128 KiB default.
+        uint8_t *buff = emalloc(NTS_MAX_RESPONSE);
         int transferred;
         const char *errtxt = NULL;
+        bool ok = false;
 
-        transferred = nts_ssl_read(ssl, buff, sizeof(buff), &errtxt);
-        if (0 >= transferred)
-                return false;
-        msyslog(LOG_ERR, "NTSc: read %d bytes", transferred);
-
-        return nts_client_process_response_core(buff, transferred, peer);
+        transferred = nts_ssl_read(ssl, buff, NTS_MAX_RESPONSE, &errtxt);
+        if (0 < transferred) {
+                msyslog(LOG_ERR, "NTSc: read %d bytes", transferred);
+                ok = nts_client_process_response_core(buff, transferred, peer);
+        }
+        free(buff);
+        return ok;
 }
 
 bool nts_client_process_response_core(uint8_t *buff, int transferred, struct peer* peer) {
         int idx;
         struct BufCtl_t buf;
         bool sawEOM = false;
+        bool sawNPN = false;
+        bool sawAEAD = false;
+        bool sawServerNegotiation = false;
 
         peer->nts_state.cookielen = 0;
         peer->nts_state.aead = NO_AEAD;
@@ -764,6 +785,18 @@ bool nts_client_process_response_core(uint8_t *buff, int transferred, struct pee
                         msyslog(LOG_ERR, "NTSc: error: %d", data);
                         return false;
                     case nts_next_protocol_negotiation:
+                        // RFC 8915 4.1.2: exactly once
+                        if (sawNPN) {
+                                msyslog(LOG_ERR, "NTSc: NPN-Duplicate");
+                                return false;
+                        }
+                        sawNPN = true;
+                        if (0 == length) {
+                                // RFC 8915 4.1.2: empty means no protocol
+                                // we offered was accepted
+                                msyslog(LOG_ERR, "NTSc: NPN-Empty: server accepted no offered protocol");
+                                return false;
+                        }
                         if (sizeof(data) != length) {
                                 msyslog(LOG_ERR, "NTSc: NPN-Wrong length: %d", length);
                                 return false;
@@ -775,11 +808,28 @@ bool nts_client_process_response_core(uint8_t *buff, int transferred, struct pee
                         }
                         break;
                     case nts_algorithm_negotiation:
+                        // RFC 8915 4.1.5: the response carries one list
+                        if (sawAEAD) {
+                                msyslog(LOG_ERR, "NTSc: AN-Duplicate");
+                                return false;
+                        }
+                        sawAEAD = true;
+                        if (0 == length) {
+                                // RFC 8915 4.1.5: empty means no algorithm
+                                // we offered was accepted
+                                msyslog(LOG_ERR, "NTSc: AN-Empty: server accepted no offered algorithm");
+                                return false;
+                        }
                         if (sizeof(data) != length) {
                                 msyslog(LOG_ERR, "NTSc: AN-Wrong length: %d", length);
                                 return false;
                         }
                         data = next_uint16(&buf);
+                        // RFC 8915 4.1.5: one of the offered algorithms
+                        if (nts_client_aead(peer) != data) {
+                                msyslog(LOG_ERR, "NTSc: AN-Not offered: %d", data);
+                                return false;
+                        }
                         keylength = nts_get_key_length(data);
                         if (0 == keylength) {
                                 msyslog(LOG_ERR, "NTSc: AN-Unsupported AEAN type: %d", data);
@@ -812,6 +862,12 @@ bool nts_client_process_response_core(uint8_t *buff, int transferred, struct pee
                         peer->nts_state.count++;
                         break;
                     case nts_server_negotiation:
+                        // RFC 8915 4.1.7: servers MUST NOT send more than one
+                        if (sawServerNegotiation) {
+                                msyslog(LOG_ERR, "NTSc: SN-Duplicate");
+                                return false;
+                        }
+                        sawServerNegotiation = true;
                         if (MAX_SERVER < (length+1)) {
                                 msyslog(LOG_ERR, "NTSc: server string too long %d.", length);
                                 return false;
@@ -836,6 +892,12 @@ bool nts_client_process_response_core(uint8_t *buff, int transferred, struct pee
                         SET_PORT(&sockaddr, port);
                         msyslog(LOG_ERR, "NTSc: Using port %d", port);
                         break;
+                    case nts_warning:
+                        // RFC 8915 4.1.4: no warning codes are defined,
+                        // and unrecognized ones are errors
+                        msyslog(LOG_ERR, "NTSc: warning record: C=%d, L=%d",
+                                critical, length);
+                        return false;
                     case nts_end_of_message:
                         if ((0 != length) || !critical) {
                                 msyslog(LOG_ERR, "NTSc: EOM-Wrong length or not Critical: %d, %d",
@@ -868,6 +930,10 @@ bool nts_client_process_response_core(uint8_t *buff, int transferred, struct pee
                 msyslog(LOG_ERR, "NTSc: No EOM.");
                 return false;
         }
+        if (!sawNPN) {
+                msyslog(LOG_ERR, "NTSc: No NPN.");
+                return false;
+        }
 
         if (NO_AEAD == peer->nts_state.aead) {
                 msyslog(LOG_ERR, "NTSc: No AEAD algorithm.");


=====================================
tests/ntpd/nts_client.c
=====================================
@@ -106,6 +106,8 @@ TEST(nts_client, nts_client_process_response_core) {
 	/* General init */
 	bool success;
 	struct peer peer;
+	peer.cfg.nts_cfg.aead = NULL;	/* offer AEAD_AES_SIV_CMAC_256 */
+	ntsconfig.aead = NULL;
 	peer.nts_state.aead = 42; /* Dummy init values */
 	peer.nts_state.cookielen = 0;
 	peer.nts_state.writeIdx = 0;
@@ -125,7 +127,8 @@ TEST(nts_client, nts_client_process_response_core) {
 			0, AEAD_AES_SIV_CMAC_256,
 		0x80, nts_new_cookie, 0, 8,
 			1, 2, 3, 4, 5, 6, 7, 8,
-		/* server_negotiation skipped due to getaddrinfo() containment breach */
+		0x80, nts_server_negotiation, 0, 9,
+			'1', '2', '7', '.', '0', '.', '0', '.', '1',
 		0x80, nts_port_negotiation, 0, 2,
 			0, 123,
 		0x80, nts_end_of_message, 0, 0
@@ -146,6 +149,21 @@ TEST(nts_client, nts_client_process_response_core) {
 	TEST_ASSERT_EQUAL_INT8(8, peer.nts_state.cookies[0][7]);
 	TEST_ASSERT_EQUAL_INT32(1, peer.nts_state.writeIdx);
 	TEST_ASSERT_EQUAL_INT32(1, peer.nts_state.count);
+	/* ===== Test: two server negotiation records ===== */
+	uint8_t buf_dup_server[] = {
+		0x80, nts_next_protocol_negotiation, 0, 2,
+			0, nts_protocol_NTP,
+		0x80, nts_algorithm_negotiation, 0, 2,
+			0, AEAD_AES_SIV_CMAC_256,
+		0x80, nts_server_negotiation, 0, 9,
+			'1', '2', '7', '.', '0', '.', '0', '.', '1',
+		0x80, nts_server_negotiation, 0, 9,
+			'1', '2', '7', '.', '0', '.', '0', '.', '2',
+		0x80, nts_end_of_message, 0, 0
+	};
+	success = nts_client_process_response_core(buf_dup_server,
+		sizeof(buf_dup_server), &peer);
+	TEST_ASSERT_EQUAL(false, success);
 	/* ===== Test: nts_error ===== */
 	/* data */
 	uint8_t buf1[] = {
@@ -191,6 +209,31 @@ TEST(nts_client, nts_client_process_response_core) {
 	/* run */
 	success = nts_client_process_response_core(buf5, sizeof(buf5), &peer);
 	TEST_ASSERT_EQUAL(false, success);
+	/* ===== Test: nts_algorithm_negotiation, not offered ===== */
+	uint8_t bufna[] = {
+		0x80, nts_next_protocol_negotiation, 0, 2,
+			0, nts_protocol_NTP,
+		0x80, nts_algorithm_negotiation, 0, 2,
+			0, AEAD_AES_SIV_CMAC_512,
+		0x80, nts_new_cookie, 0, 8,
+			1, 2, 3, 4, 5, 6, 7, 8,
+		0x80, nts_end_of_message, 0, 0
+	};
+	success = nts_client_process_response_core(bufna, sizeof(bufna), &peer);
+	TEST_ASSERT_EQUAL(false, success);
+	/* ===== Test: nts_warning, even without the critical bit ===== */
+	uint8_t bufw[] = {
+		0x80, nts_next_protocol_negotiation, 0, 2,
+			0, nts_protocol_NTP,
+		0x80, nts_algorithm_negotiation, 0, 2,
+			0, AEAD_AES_SIV_CMAC_256,
+		0x80, nts_new_cookie, 0, 8,
+			1, 2, 3, 4, 5, 6, 7, 8,
+		0x00, nts_warning, 0, 2, 0, 1,
+		0x80, nts_end_of_message, 0, 0
+	};
+	success = nts_client_process_response_core(bufw, sizeof(bufw), &peer);
+	TEST_ASSERT_EQUAL(false, success);
 	/* ===== Test: nts_new_cookie, over max cookie length ===== */
 	/* data */
 	uint8_t buf6[] = {
@@ -277,6 +320,8 @@ TEST(nts_client, nts_client_process_response_core) {
 	/* data */
 	peer.nts_state.count = 0;
 	uint8_t buf12[] = {
+		0x80, nts_next_protocol_negotiation, 0, 2, 0, nts_protocol_NTP,
+		0x80, nts_algorithm_negotiation, 0, 2, 0, AEAD_AES_SIV_CMAC_256,
 		0x80, nts_end_of_message, 0, 0
 	};
 	/* run */
@@ -287,11 +332,63 @@ TEST(nts_client, nts_client_process_response_core) {
 	peer.nts_state.count = 8; /* So this doesn't trigger an error */
 	peer.nts_state.aead = NO_AEAD;
 	uint8_t buf13[] = {
+		0x80, nts_next_protocol_negotiation, 0, 2, 0, nts_protocol_NTP,
 		0x80, nts_end_of_message, 0, 0
 	};
 	/* run */
 	success = nts_client_process_response_core(buf13, sizeof(buf13), &peer);
 	TEST_ASSERT_EQUAL(false, success);
+	/* ===== Test: nts_next_protocol_negotiation, duplicate ===== */
+	/* data */
+	uint8_t buf14[] = {
+		0x80, nts_next_protocol_negotiation, 0, 2, 0, nts_protocol_NTP,
+		0x80, nts_next_protocol_negotiation, 0, 2, 0, nts_protocol_NTP,
+		0x80, nts_algorithm_negotiation, 0, 2, 0, AEAD_AES_SIV_CMAC_256,
+		0x80, nts_end_of_message, 0, 0
+	};
+	/* run */
+	success = nts_client_process_response_core(buf14, sizeof(buf14), &peer);
+	TEST_ASSERT_EQUAL(false, success);
+	/* ===== Test: nts_next_protocol_negotiation, missing ===== */
+	/* data */
+	uint8_t buf15[] = {
+		0x80, nts_algorithm_negotiation, 0, 2, 0, AEAD_AES_SIV_CMAC_256,
+		0x80, nts_end_of_message, 0, 0
+	};
+	/* run */
+	success = nts_client_process_response_core(buf15, sizeof(buf15), &peer);
+	TEST_ASSERT_EQUAL(false, success);
+	/* ===== Test: nts_next_protocol_negotiation, empty (server accepted nothing) ===== */
+	/* data */
+	uint8_t buf16[] = {
+		0x80, nts_next_protocol_negotiation, 0, 0,
+		0x80, nts_algorithm_negotiation, 0, 2, 0, AEAD_AES_SIV_CMAC_256,
+		0x80, nts_end_of_message, 0, 0
+	};
+	/* run */
+	success = nts_client_process_response_core(buf16, sizeof(buf16), &peer);
+	TEST_ASSERT_EQUAL(false, success);
+	/* ===== Test: nts_algorithm_negotiation, duplicate ===== */
+	/* data */
+	uint8_t buf17[] = {
+		0x80, nts_next_protocol_negotiation, 0, 2, 0, nts_protocol_NTP,
+		0x80, nts_algorithm_negotiation, 0, 2, 0, AEAD_AES_SIV_CMAC_256,
+		0x80, nts_algorithm_negotiation, 0, 2, 0, AEAD_AES_SIV_CMAC_256,
+		0x80, nts_end_of_message, 0, 0
+	};
+	/* run */
+	success = nts_client_process_response_core(buf17, sizeof(buf17), &peer);
+	TEST_ASSERT_EQUAL(false, success);
+	/* ===== Test: nts_algorithm_negotiation, empty (server accepted no offered algorithm) ===== */
+	/* data */
+	uint8_t buf18[] = {
+		0x80, nts_next_protocol_negotiation, 0, 2, 0, nts_protocol_NTP,
+		0x80, nts_algorithm_negotiation, 0, 0,
+		0x80, nts_end_of_message, 0, 0
+	};
+	/* run */
+	success = nts_client_process_response_core(buf18, sizeof(buf18), &peer);
+	TEST_ASSERT_EQUAL(false, success);
 }
 
 /* Hacks to keep linker happy */



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/-/commit/fd749194f36d9795db234164a8943eadb064c5fc

-- 
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/-/commit/fd749194f36d9795db234164a8943eadb064c5fc
You're receiving this email because of your account on gitlab.com. Manage all notifications: https://gitlab.com/-/profile/notifications | Help: https://gitlab.com/help


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20261110/719e808b/attachment-0001.htm>


More information about the vc mailing list