[Git][NTPsec/ntpsec][master] 3 commits: ntpd/nts_server.c: send Bad Request on an NTS-KE timeout

Matt Selsky (@selsky) gitlab at mg.gitlab.com
Sat Nov 7 22:41:42 UTC 2026



Matt Selsky pushed to branch master at NTPsec / ntpsec


Commits:
06339aa3 by Matt Selsky at 2026-11-07T18:15:26+00:00
ntpd/nts_server.c: send Bad Request on an NTS-KE timeout

RFC 8915 section 4.1.3 says the server MUST respond with the Bad
Request error if, when its timeout expires, it has not received a
complete request. When nts_ssl_read() returned 0 on a timeout,
nts_ke_request() closed the connection without sending anything.

Send an Error record with Bad Request and an End of Message record.
The log keeps the read's error text, not the write's.

nts_ssl_read() also returned 0 when the peer closed the connection.
It now returns -1 then, so the server doesn't write to a closed
connection. With OpenSSL 1.1.1, an unclean close still reads as a
timeout. The client treats both values as failure.

- - - - -
b50f9987 by Matt Selsky at 2026-11-07T18:15:26+00:00
ntpd/nts_server.c: answer an unmatched NTS-KE request without error

RFC 8915 reserves the Bad Request error for a request that is not
complete and syntactically well-formed (section 4.1.3). A well-formed
request whose protocols or AEAD algorithms the server doesn't support
gets an empty NTS Next Protocol Negotiation record (section 4.1.2:
"the response MAY be empty") or an empty AEAD Algorithm Negotiation
record (section 4.1.5: "It is empty if the server supports none of
the algorithms offered"). nts_ke_process_receive() treated both as
Bad Request, which tells the client not to retry without changing its
request.

Return the new Process_NoMatch for an unsupported protocol or
algorithm list, which sends the negotiation records without an Error
record. An empty list in the request still gets Bad Request, since a
request MUST list at least one protocol and one algorithm. The "bad
length" test now uses an odd length, since two protocol IDs are a
valid length.

- - - - -
31ae80be by Matt Selsky at 2026-11-07T18:15:26+00:00
ntpd/nts_server.c: handle the NTS-KE record types it knows

RFC 8915 reserves Error code 0, Unrecognized Critical Record, for a
critical record the server did not understand (section 4.1.3).
nts_ke_process_receive() sent it for any record type it did not parse,
including ones the RFC defines:

- Clients MUST NOT send Warning or New Cookie records (sections 4.1.4
  and 4.1.6), and a Warning record always has the Critical Bit set, so
  a client Warning got error 0 rather than Bad Request.
- Honoring a client's NTPv4 Server or Port Negotiation record is
  optional (sections 4.1.7 and 4.1.8), and clients SHOULD NOT, but
  may, set the Critical Bit on them, so a request with a critical one
  was refused with error 0.

Reject a Warning or New Cookie record as Bad Request, and skip Server
and Port Negotiation records whether or not they are critical.

- - - - -


4 changed files:

- include/nts.h
- ntpd/nts.c
- ntpd/nts_server.c
- tests/ntpd/nts_server.c


Changes:

=====================================
include/nts.h
=====================================
@@ -51,7 +51,8 @@ typedef struct BufCtl_t BufCtl;
 
 // Here for test routines
 enum process_result {
-        Process_OK, Process_Critical, Process_Error };
+        Process_OK, Process_Critical, Process_Error,
+        Process_NoMatch };      // well-formed, but nothing in common
 struct NTS_Server_Info_t {
   char hostaddr[100];
   int aead;


=====================================
ntpd/nts.c
=====================================
@@ -317,7 +317,7 @@ int nts_ssl_read(SSL *ssl, uint8_t *buff, int buff_length, const char** errtxt)
       unsigned long err = ERR_peek_error();
       if (SSL_ERROR_ZERO_RETURN == code) {
         *errtxt = "SSL_read Connection reset1";
-        return 0;
+        return -1;
       }
       if (SSL_ERROR_WANT_READ == code) {
         *errtxt = "SSL_read Timeout";
@@ -327,7 +327,7 @@ int nts_ssl_read(SSL *ssl, uint8_t *buff, int buff_length, const char** errtxt)
       if (SSL_ERROR_SSL == code &&
           SSL_R_UNEXPECTED_EOF_WHILE_READING == ERR_GET_REASON(err)) {
         *errtxt = "SSL_read Connection reset2";
-        return 0;
+        return -1;
       }
 #else
       // Hack for OpenSSL 1.1.1w  11 Sep 2023


=====================================
ntpd/nts_server.c
=====================================
@@ -375,8 +375,22 @@ void nts_ke_request(NTS_Server_Info *nsi, SSL *ssl) {
         enum process_result result;
 
         bytes_read = nts_ssl_read(ssl, buff, sizeof(buff), &nsi->errtxt);
-        if (0 >= bytes_read)
+        if (0 > bytes_read)
                 return;
+        if (0 == bytes_read) {
+                // RFC 8915 4.1.3: Bad Request on timeout without a request.
+                // Keep the read's error text for the log.
+                const char *werrtxt = NULL;
+
+                buf.next = buff;
+                buf.left = sizeof(buff);
+                ke_append_record_uint16(&buf, NTS_CRITICAL+nts_error,
+                  nts_bad_request);
+                ke_append_record_null(&buf, NTS_CRITICAL+nts_end_of_message);
+                used = sizeof(buff)-buf.left;
+                nts_ssl_write(ssl, buff, used, &werrtxt);
+                return;
+        }
 
         buf.next = buff;
         buf.left = bytes_read;
@@ -403,6 +417,8 @@ void nts_ke_request(NTS_Server_Info *nsi, SSL *ssl) {
                             NTS_CRITICAL+nts_next_protocol_negotiation);
                     }
                 }
+                // RFC 8915 4.1.2, 4.1.5: if nothing matched, the
+                // empty records above are the answer, without an error
                 switch (result) {
                   case Process_Critical:
                     ke_append_record_uint16(&buf, NTS_CRITICAL+nts_error,
@@ -412,6 +428,8 @@ void nts_ke_request(NTS_Server_Info *nsi, SSL *ssl) {
                     ke_append_record_uint16(&buf, NTS_CRITICAL+nts_error,
                       nts_bad_request);
                     break;
+                  case Process_NoMatch:
+                    FALLTHRU
                   case Process_OK:  // keep compiler happy
                     break;
                   default:
@@ -551,6 +569,8 @@ bool create_listener6(int port) {
 }
 
 enum process_result nts_ke_process_receive(NTS_Server_Info *nsi, BufCtl *buf) {
+        bool npn_empty = false, aead_empty = false;
+
         nsi->aead = NO_AEAD;
         nsi->saw_next_protocol = false;
         nsi->next_protocol_matched = false;
@@ -598,6 +618,7 @@ enum process_result nts_ke_process_receive(NTS_Server_Info *nsi, BufCtl *buf) {
                                 return Process_Error;
                         }
                         nsi->saw_next_protocol = true;
+                        npn_empty = (0 == length);
                         for (int i=0; i<length; i+=sizeof(uint16_t)) {
                                 data = next_uint16(buf);
                                 if (data == nts_protocol_NTP) {
@@ -617,6 +638,7 @@ enum process_result nts_ke_process_receive(NTS_Server_Info *nsi, BufCtl *buf) {
                                 return Process_Error;
                         }
                         nsi->saw_aead = true;
+                        aead_empty = (0 == length);
                         for (int i=0; i<length; i+=sizeof(uint16_t)) {
                                 data = next_uint16(buf);
 // FIXME: config option, need bitmap or ???
@@ -648,19 +670,46 @@ enum process_result nts_ke_process_receive(NTS_Server_Info *nsi, BufCtl *buf) {
                                 nsi->errtxt = "NPN Missing";
                                 return Process_Error;
                         }
+                        // RFC 8915 4.1.2: the request lists at least one
+                        if (npn_empty) {
+                                nsi->errtxt = "NPN Empty";
+                                return Process_Error;
+                        }
                         if (!nsi->next_protocol_matched) {
                                 nsi->errtxt = "NPN No match";
-                                return Process_Error;
+                                return Process_NoMatch;
                         }
                         if (!nsi->saw_aead) {
                                 nsi->errtxt = "AEAD Missing";
                                 return Process_Error;
                         }
+                        // RFC 8915 4.1.5: the request lists at least one
+                        if (aead_empty) {
+                                nsi->errtxt = "AEAD Empty";
+                                return Process_Error;
+                        }
                         if (NO_AEAD == nsi->aead) {
                                 nsi->errtxt = "AEAD No match";
-                                return Process_Error;
+                                return Process_NoMatch;
                         }
                         return Process_OK;
+                    case nts_warning:
+                        FALLTHRU
+                    case nts_new_cookie:
+                        // RFC 8915 4.1.4, 4.1.6: clients MUST NOT send these
+                        snprintf(nsi->errbuf, sizeof(nsi->errbuf),
+                                "Client sent record type %d", type);
+                        nsi->errtxt = nsi->errbuf;
+                        return Process_Error;
+                    case nts_server_negotiation:
+                        FALLTHRU
+                    case nts_port_negotiation:
+                        // RFC 8915 4.1.7, 4.1.8: honoring these is
+                        // optional, and a client SHOULD NOT, but may, set
+                        // the Critical Bit
+                        buf->next += length;
+                        buf->left -= length;
+                        break;
                     default:
                         if (critical) {
                           // This only logs the first one from a connection


=====================================
tests/ntpd/nts_server.c
=====================================
@@ -64,7 +64,7 @@ TEST(nts_server, nts_ke_process_receive) {
 
 	/* ===== Test: nts_next_protocol_negotiation, bad length ===== */
 	uint8_t buf2[] = {
-		0x80, nts_next_protocol_negotiation, 0, 4, 0x11, 0x22, 0x33, 0x44,
+		0x80, nts_next_protocol_negotiation, 0, 3, 0x11, 0x22, 0x33,
 		0x80, nts_end_of_message, 0, 0,
 	};
 	buf.next = buf2;
@@ -73,7 +73,7 @@ TEST(nts_server, nts_ke_process_receive) {
 	success = nts_ke_process_receive(&nsi, &buf);
 	TEST_ASSERT_EQUAL(Process_Error, success);
 
-	/* ===== Test: nts_next_protocol_negotiation, bad protocol ===== */
+	/* ===== Test: nts_next_protocol_negotiation, unsupported protocol ===== */
 	uint8_t buf3[] = {
 		0x80, nts_next_protocol_negotiation, 0, 2, 0x11, 0x22,
 		0x80, nts_end_of_message, 0, 0,
@@ -82,6 +82,38 @@ TEST(nts_server, nts_ke_process_receive) {
 	buf.left = sizeof(buf3);
 	/* test */
 	success = nts_ke_process_receive(&nsi, &buf);
+	TEST_ASSERT_EQUAL(Process_NoMatch, success);
+
+	/* ===== Test: nts_next_protocol_negotiation, empty ===== */
+	uint8_t bufne[] = {
+		0x80, nts_next_protocol_negotiation, 0, 0,
+		0x80, nts_end_of_message, 0, 0,
+	};
+	buf.next = bufne;
+	buf.left = sizeof(bufne);
+	success = nts_ke_process_receive(&nsi, &buf);
+	TEST_ASSERT_EQUAL(Process_Error, success);
+
+	/* ===== Test: nts_algorithm_negotiation, no supported AEAD ===== */
+	uint8_t bufna[] = {
+		0x80, nts_next_protocol_negotiation, 0, 2, 0x00, nts_protocol_NTP,
+		0x80, nts_algorithm_negotiation, 0, 2, 0x11, 0x22,
+		0x80, nts_end_of_message, 0, 0,
+	};
+	buf.next = bufna;
+	buf.left = sizeof(bufna);
+	success = nts_ke_process_receive(&nsi, &buf);
+	TEST_ASSERT_EQUAL(Process_NoMatch, success);
+
+	/* ===== Test: nts_algorithm_negotiation, empty ===== */
+	uint8_t bufae[] = {
+		0x80, nts_next_protocol_negotiation, 0, 2, 0x00, nts_protocol_NTP,
+		0x80, nts_algorithm_negotiation, 0, 0,
+		0x80, nts_end_of_message, 0, 0,
+	};
+	buf.next = bufae;
+	buf.left = sizeof(bufae);
+	success = nts_ke_process_receive(&nsi, &buf);
 	TEST_ASSERT_EQUAL(Process_Error, success);
 
 	/* ===== Test: nts_end_of_message, bad length ===== */
@@ -124,6 +156,28 @@ TEST(nts_server, nts_ke_process_receive) {
 	/* test */
 	success = nts_ke_process_receive(&nsi, &buf);
 	TEST_ASSERT_EQUAL(Process_Critical, success);
+
+	/* ===== Test: Warning from a client is a Bad Request ===== */
+	uint8_t bufw[] = {
+		0x80, nts_warning, 0, 2, 0, 1,
+		0x80, nts_end_of_message, 0, 0,
+	};
+	buf.next = bufw;
+	buf.left = sizeof(bufw);
+	success = nts_ke_process_receive(&nsi, &buf);
+	TEST_ASSERT_EQUAL(Process_Error, success);
+
+	/* ===== Test: critical Server Negotiation is ignored ===== */
+	uint8_t bufsn[] = {
+		0x80, nts_next_protocol_negotiation, 0, 2, 0x00, nts_protocol_NTP,
+		0x80, nts_algorithm_negotiation, 0, 2, 0x00, AEAD_AES_SIV_CMAC_256,
+		0x80, nts_server_negotiation, 0, 3, 'a', '.', 'b',
+		0x80, nts_end_of_message, 0, 0,
+	};
+	buf.next = bufsn;
+	buf.left = sizeof(bufsn);
+	success = nts_ke_process_receive(&nsi, &buf);
+	TEST_ASSERT_EQUAL(Process_OK, success);
 }
 
 TEST_GROUP_RUNNER(nts_server) {



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/-/compare/66aba586b59d05fa9d3deecf653f609a1f5f02a3...31ae80be6038e48ae7d927a19faca63dec85c411

-- 
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/-/compare/66aba586b59d05fa9d3deecf653f609a1f5f02a3...31ae80be6038e48ae7d927a19faca63dec85c411
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/20261107/ef990f9d/attachment-0001.htm>


More information about the vc mailing list