Coverage Report

Created: 2026-12-01 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ntpsec/ntpd/nts_extens.c
Line
Count
Source
1
/*
2
 * ntp_extens.c - Network Time Protocol (NTP) extension processing
3
 * Copyright the NTPsec project contributors
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 *
6
 * NB: This module is working with the wire format packet.
7
 *     It must do byte swapping.
8
 *
9
 * We carefully arrange things so that no padding is necessary.
10
 *
11
 * This is only called by the main ntpd thread, so we don't need
12
 * a lock to protect wire_ctx.
13
 */
14
15
#include "config.h"
16
17
#include <stdbool.h>
18
#include <stdint.h>
19
#include <string.h>
20
21
#include <aes_siv.h>
22
23
#include "ntp_stdlib.h"
24
#include "ntp.h"
25
#include "ntpd.h"
26
#include "nts.h"
27
#include "nts2.h"
28
29
50
#define NONCE_LENGTH 16
30
74
#define CMAC_LENGTH 16
31
32
51
#define NTP_EX_HDR_LNG 4
33
51
#define NTP_EX_U16_LNG 2
34
35
enum NtpExtFieldType {
36
        Unique_Identifier = 0x104,
37
        NTS_Cookie = 0x204,
38
        NTS_Cookie_Placeholder = 0x304,
39
        NTS_AEEF = 0x404  // Authenticated and Encrypted Extension Fields
40
};
41
42
// This is only called by the main thread so we don't need a lock.
43
AES_SIV_CTX* wire_ctx = NULL;
44
45
46
0
bool extens_init(void) {
47
0
        wire_ctx = AES_SIV_CTX_new();
48
0
        if (NULL == wire_ctx) {
49
0
                msyslog(LOG_ERR, "NTS: Can't init wire_ctx");
50
0
                exit(1);
51
0
        }
52
0
        return true;
53
0
}
54
55
0
int extens_client_send(struct peer *peer, struct pkt *xpkt) {
56
0
        struct BufCtl_t buf;
57
0
        int used, adlength, idx;
58
0
        size_t left;
59
0
        uint8_t *nonce, *packet;
60
0
        bool ok;
61
62
0
        packet = (uint8_t*)xpkt;
63
0
        buf.next = xpkt->exten;
64
0
        buf.left = MAX_EXT_LEN;
65
66
        // UID
67
0
        ntp_RAND_bytes(peer->nts_state.UID, NTS_UID_LENGTH);
68
0
        ex_append_record_bytes(&buf, Unique_Identifier,
69
0
                               peer->nts_state.UID, NTS_UID_LENGTH);
70
71
        // cookie
72
0
        idx = peer->nts_state.readIdx++;
73
0
        ex_append_record_bytes(&buf, NTS_Cookie,
74
0
                               peer->nts_state.cookies[idx],
75
0
                               peer->nts_state.cookielen);
76
0
        peer->nts_state.readIdx = peer->nts_state.readIdx % NTS_MAX_COOKIES;
77
0
        peer->nts_state.count--;
78
79
        // Need more cookies?
80
0
        for (int i=peer->nts_state.count+1; i<NTS_MAX_COOKIES; i++) {
81
                // WARN: This may get too big for the MTU.
82
0
                ex_append_header(&buf, NTS_Cookie_Placeholder,
83
0
                                 peer->nts_state.cookielen);
84
0
                memset(buf.next, 0, peer->nts_state.cookielen);
85
0
                buf.next += peer->nts_state.cookielen;
86
0
                buf.left -= peer->nts_state.cookielen;
87
0
        }
88
89
        // AEAD
90
0
        adlength = buf.next-packet;
91
0
        ex_append_header(&buf, NTS_AEEF,
92
0
                         NTP_EX_U16_LNG*2+NONCE_LENGTH+CMAC_LENGTH);
93
0
        append_uint16(&buf, NONCE_LENGTH);
94
0
        append_uint16(&buf, CMAC_LENGTH);
95
0
        nonce = buf.next;
96
0
        ntp_RAND_bytes(nonce, NONCE_LENGTH);
97
0
        buf.next += NONCE_LENGTH;
98
0
        buf.left -= NONCE_LENGTH;
99
0
        left = buf.left;
100
0
        ok = AES_SIV_Encrypt(wire_ctx,
101
                            // left: in: max out length, out: length used
102
0
                             buf.next, &left,
103
0
                             peer->nts_state.c2s, peer->nts_state.keylen,
104
0
                             nonce, NONCE_LENGTH,
105
0
                             (const uint8_t *)"", 0,  // no plaintext
106
0
                             packet, adlength);
107
0
        if (!ok) {
108
0
                msyslog(LOG_ERR,
109
0
                        "NTS: extens_client_send - Error from AES_SIV_Encrypt");
110
                /* I don't think this should happen,
111
                 * so crash rather than work incorrectly.
112
                 * Hal, 2019-Feb-17
113
                 * Similar code in nts_cookie
114
                 */
115
0
                exit(1);
116
0
        }
117
0
        buf.next += left;
118
0
        buf.left -= left;
119
120
0
        used = buf.next-xpkt->exten;
121
0
        nts_cnt.client_send++;
122
0
        return used;
123
0
}
124
125
262
#define CookieLenUnknown -1
126
149
bool extens_server_recv(struct ntspacket_t *ntspacket, uint8_t *pkt, int lng) {
127
149
        struct BufCtl_t buf;
128
149
        uint16_t aead;
129
149
        unsigned noncelen, ctlen, noncepad, ctpad, fieldlen;
130
149
        bool sawcookie, sawAEEF;
131
149
        int cookielen;                  // cookie and placeholder(s)
132
133
149
        nts_cnt.server_recv_bad++;              // assume bad, undo if OK
134
135
149
        buf.next = pkt+LEN_PKT_NOMAC;
136
149
        buf.left = lng-LEN_PKT_NOMAC;
137
138
149
        sawcookie = sawAEEF = false;
139
149
        cookielen = CookieLenUnknown;
140
149
        ntspacket->uidlen = 0;
141
149
        ntspacket->needed = 0;
142
143
462
        while (buf.left >= NTS_KE_HDR_LNG) {
144
421
                uint16_t type;
145
421
                int length, adlength;
146
421
                size_t outlen;
147
421
                uint8_t *nonce, *ciphertext;
148
421
                uint8_t plaintext[MAX_EXT_LEN];
149
421
                bool ok;
150
151
421
                type = ex_next_record(&buf, &length); // length excludes header
152
421
                if (length&3 || length > buf.left || length < 0) {
153
56
                        return false;
154
56
                }
155
365
                switch (type) {
156
54
                    case Unique_Identifier:
157
54
                        if (length > NTS_UID_MAX_LENGTH || length < NTS_UID_LENGTH) {
158
12
                                return false;
159
12
                        }
160
42
                        ntspacket->uidlen = length;
161
42
                        next_bytes(&buf, ntspacket->UID, length);
162
42
                        break;
163
21
                    case NTS_Cookie:
164
                        /* cookies and placeholders must be the same length
165
                         * in order to avoid amplification attacks.
166
                         */
167
21
                        if (sawcookie) {
168
0
                                return false; // second cookie
169
0
                        }
170
21
                        if (CookieLenUnknown == cookielen) {
171
2
                                cookielen = length;
172
2
                        }
173
19
                        else if (length != cookielen) {
174
18
                                return false;
175
18
                        }
176
3
                        ok = nts_unpack_cookie(buf.next, length, &aead,
177
3
                                               ntspacket->c2s,
178
3
                                               ntspacket->s2c,
179
3
                                               &ntspacket->keylen);
180
3
                        if (!ok) {
181
3
                                return false;
182
3
                        }
183
0
                        buf.next += length;
184
0
                        buf.left -= length;
185
0
                        sawcookie = true;
186
0
                        ntspacket->needed++;
187
0
                        ntspacket->aead = aead;
188
0
                        break;
189
92
                    case NTS_Cookie_Placeholder:
190
92
                        if (CookieLenUnknown == cookielen) {
191
44
                                cookielen = length;
192
44
                        }
193
48
                        else if (length != cookielen) {
194
17
                                return false;
195
17
                        }
196
75
                        ntspacket->needed++;
197
75
                        if (NTS_MAX_COOKIES < ntspacket->needed) {
198
1
                                return false;
199
1
                        }
200
74
                        buf.next += length;
201
74
                        buf.left -= length;
202
74
                        break;
203
1
                    case NTS_AEEF:
204
1
                        if (!sawcookie) {
205
1
                                return false; // no cookie yet, no c2s
206
1
                        }
207
0
                        if (length < NTP_EX_U16_LNG*2+NONCE_LENGTH+CMAC_LENGTH) {
208
0
                                return false;
209
0
                        }
210
                        /* Additional data is up to this exten.
211
                         * backup over header */
212
0
                        adlength = buf.next-NTP_EX_HDR_LNG-pkt;
213
0
                        noncelen = next_uint16(&buf);
214
0
                        ctlen = next_uint16(&buf);
215
                        // nonce, ciphertext, and additional padding
216
0
                        fieldlen = (unsigned)length - NTP_EX_U16_LNG*2;
217
0
                        noncepad = (noncelen + 3) & ~3U;
218
0
                        ctpad = (ctlen + 3) & ~3U;
219
0
                        if (noncepad + ctpad > fieldlen) {
220
0
                                return false;
221
0
                        }
222
                        // RFC 8915 5.6: nonce plus additional padding
223
                        // must be at least N_REQ, 16 for AES-SIV
224
0
                        if (fieldlen - ctpad < NONCE_LENGTH) {
225
0
                                return false;
226
0
                        }
227
0
                        nonce = buf.next;
228
0
                        ciphertext = nonce+noncepad;
229
                        // Encrypted extension fields are decrypted
230
                        // but not used.
231
0
                        outlen = sizeof(plaintext);
232
0
                        ok = AES_SIV_Decrypt(wire_ctx,
233
0
                                             plaintext, &outlen,
234
0
                                             ntspacket->c2s, ntspacket->keylen,
235
0
                                             nonce, noncelen,
236
0
                                             ciphertext, ctlen,
237
0
                                             pkt, adlength);
238
0
                        if (!ok) {
239
0
                                return false;
240
0
                        }
241
                        // we already used 2 length slots way above*/
242
0
                        length -= (NTP_EX_U16_LNG+NTP_EX_U16_LNG);
243
0
                        buf.next += length;
244
0
                        buf.left -= length;
245
0
                        if (0 != buf.left) {
246
0
                                return false;  // Reject extens after AEEF block
247
0
                        }
248
0
                        sawAEEF = true;
249
0
                        break;
250
197
                    default:
251
                        /* Non NTS extensions on requests at server.
252
                         * Call out when we get some that we want.
253
                         * Until then, just ignore it */
254
197
                        buf.next += length;
255
197
                        buf.left -= length;
256
197
                        break;
257
365
                }
258
365
        }
259
260
41
        if (!sawAEEF) {
261
41
                return false;
262
41
        }
263
0
        if (buf.left > 0)
264
0
                return false;
265
266
        //  printf("ESRx: %d, %d, %d\n",
267
        //      lng-LEN_PKT_NOMAC, ntspacket->needed, ntspacket->keylen);
268
0
        ntspacket->valid = true;
269
0
        nts_cnt.server_recv_good++;
270
0
        nts_cnt.server_recv_bad--;
271
0
        return true;
272
0
}
273
274
0
int extens_server_send(struct ntspacket_t *ntspacket, struct pkt *xpkt) {
275
0
        struct BufCtl_t buf;
276
0
        int used, adlength;
277
0
        size_t left;
278
0
        uint8_t *nonce, *packet;
279
0
        uint8_t *plaintext, *ciphertext;;
280
0
        uint8_t cookie[NTS_MAX_COOKIELEN];
281
0
        int cookielen, plainleng, aeadlen;
282
0
        bool ok;
283
284
        // get first cookie now so we have length**/
285
0
        cookielen = nts_make_cookie(cookie, ntspacket->aead,
286
0
                                    ntspacket->c2s, ntspacket->s2c,
287
0
                                    ntspacket->keylen);
288
289
0
        packet = (uint8_t*)xpkt;
290
0
        buf.next = xpkt->exten;
291
0
        buf.left = MAX_EXT_LEN;
292
293
        // UID
294
0
        if (0 < ntspacket->uidlen) {
295
0
                ex_append_record_bytes(&buf, Unique_Identifier,
296
0
                                       ntspacket->UID, ntspacket->uidlen);
297
0
        }
298
299
0
        adlength = buf.next-packet;           // up to here is Additional Data
300
301
        // length of whole AEEF
302
0
        plainleng = ntspacket->needed*(NTP_EX_HDR_LNG+cookielen);
303
        // length of whole AEEF header
304
0
        aeadlen = NTP_EX_U16_LNG*2+NONCE_LENGTH+CMAC_LENGTH + plainleng;
305
0
        ex_append_header(&buf, NTS_AEEF, aeadlen);
306
0
        append_uint16(&buf, NONCE_LENGTH);
307
0
        append_uint16(&buf, plainleng+CMAC_LENGTH);
308
309
0
        nonce = buf.next;
310
0
        ntp_RAND_bytes(nonce, NONCE_LENGTH);
311
0
        buf.next += NONCE_LENGTH;
312
0
        buf.left -= NONCE_LENGTH;
313
314
0
        ciphertext = buf.next;  // cipher text starts here
315
0
        left = buf.left;
316
0
        buf.next += CMAC_LENGTH;        // skip space for CMAC
317
0
        buf.left -= CMAC_LENGTH;
318
0
        plaintext = buf.next;           // encrypt in place
319
320
0
        ex_append_record_bytes(&buf, NTS_Cookie,
321
0
                               cookie, cookielen);
322
0
        for (int i=1; i<ntspacket->needed; i++) {
323
                /* WARN: This may get too big for the MTU. See length
324
                 * calculation above.  Responses are the same length as
325
                 * requests to avoid DDoS amplification.  So if it got to
326
                 * us, there is a good chance it will get back. */
327
0
                nts_make_cookie(cookie, ntspacket->aead,
328
0
                                ntspacket->c2s, ntspacket->s2c,
329
0
                                ntspacket->keylen);
330
0
                ex_append_record_bytes(&buf, NTS_Cookie,
331
0
                                       cookie, cookielen);
332
0
        }
333
334
        //printf("ESSa: %d, %d, %d, %d\n",
335
        //  adlength, plainleng, cookielen, ntspacket->needed);
336
337
0
        ok = AES_SIV_Encrypt(wire_ctx,
338
                             // left: in: max out length, out: length used
339
0
                             ciphertext, &left,
340
0
                             ntspacket->s2c, ntspacket->keylen,
341
0
                             nonce, NONCE_LENGTH,
342
0
                             plaintext, plainleng,
343
0
                             packet, adlength);
344
0
        if (!ok) {
345
0
                msyslog(LOG_ERR,
346
0
                        "NTS: extens_server_send - Error from AES_SIV_Encrypt");
347
0
                nts_log_ssl_error();
348
                /* I don't think this should happen,
349
                 * so crash rather than work incorrectly.
350
                 * Hal, 2019-Feb-17
351
                 * Similar code in nts_cookie
352
                 */
353
0
                exit(1);
354
0
        }
355
356
0
        used = buf.next-xpkt->exten;
357
358
        // printf("ESSx: %lu, %d\n", (long unsigned)left, used);
359
360
0
        nts_cnt.server_send++;
361
0
        return used;
362
0
}
363
364
322
bool extens_client_recv(struct peer *peer, uint8_t *pkt, int lng) {
365
322
        struct BufCtl_t buf;
366
322
        int idx;
367
322
        bool sawAEEF = false;
368
369
322
        nts_cnt.client_recv_bad++;      // assume bad, undo if OK
370
371
322
        buf.next = pkt+LEN_PKT_NOMAC;
372
322
        buf.left = lng-LEN_PKT_NOMAC;
373
374
613
        while (buf.left >= NTS_KE_HDR_LNG) {
375
532
                uint16_t type;
376
532
                int length, adlength, noncelen;
377
532
                uint8_t *nonce, *ciphertext, *plaintext;
378
532
                size_t outlen;
379
532
                bool ok;
380
381
532
                type = ex_next_record(&buf, &length); // length excludes header
382
532
                if (length&3 || length > buf.left || length < 0) {
383
49
                        return false;
384
49
                }
385
                //     printf("ECR: %d, %d, %d\n", type, length, buf.left);
386
483
                switch (type) {
387
181
                    case Unique_Identifier:
388
181
                        if (NTS_UID_LENGTH != length) {
389
10
                                return false;
390
10
                        }
391
171
                        if (0 != memcmp(buf.next, peer->nts_state.UID,
392
171
                                        NTS_UID_LENGTH)) {
393
130
                                return false;
394
130
                        }
395
41
                        buf.next += length;
396
41
                        buf.left -= length;
397
41
                        break;
398
1
                    case NTS_Cookie:
399
1
                        if (!sawAEEF)
400
1
                                return false;     // reject unencrypted cookies
401
0
                        if (NTS_MAX_COOKIES <= peer->nts_state.count)
402
0
                                return false;     // reject extra cookies
403
0
                        if (length != peer->nts_state.cookielen)
404
0
                                return false;     // reject length change
405
0
                        idx = peer->nts_state.writeIdx++;
406
0
                        memcpy((uint8_t*)&peer->nts_state.cookies[idx],
407
0
                               buf.next, length);
408
0
                        peer->nts_state.writeIdx = peer->nts_state.writeIdx %
409
0
                                                   NTS_MAX_COOKIES;
410
0
                        peer->nts_state.count++;
411
0
                        buf.next += length;
412
0
                        buf.left -= length;
413
0
                        break;
414
51
                    case NTS_AEEF:
415
                        // backup over header
416
51
                        adlength = buf.next-NTP_EX_HDR_LNG-pkt;
417
51
                        if (NTP_EX_U16_LNG*2 > length)
418
1
                                return false;        // garbage packet
419
50
                        noncelen = next_uint16(&buf);
420
50
                        outlen = next_uint16(&buf);
421
50
                        if (NONCE_LENGTH != noncelen)
422
22
                                return false;        // garbage packet
423
28
                        if (outlen&3)
424
2
                                return false;        // else round up
425
26
                        if (noncelen+CMAC_LENGTH > buf.left)
426
10
                                return false;        // garbage packet
427
16
                        nonce = buf.next;
428
16
                        ciphertext = nonce+noncelen;
429
16
                        plaintext = ciphertext+CMAC_LENGTH;
430
16
                        outlen = buf.left-noncelen-CMAC_LENGTH;
431
                        //  printf("ECRa: %lu, %d\n", (long unsigned)outlen,
432
                       //          noncelen);
433
16
                        ok = AES_SIV_Decrypt(wire_ctx,
434
16
                                             plaintext, &outlen,
435
16
                                             peer->nts_state.s2c,
436
16
                                             peer->nts_state.keylen,
437
16
                                             nonce, noncelen,
438
16
                                             ciphertext, outlen+CMAC_LENGTH,
439
16
                                             pkt, adlength);
440
                        // printf("ECRb: %d, %lu\n", ok, (long unsigned)outlen);
441
16
                        if (!ok)
442
16
                                return false;
443
                        // setup to process encrypted headers
444
0
                        buf.next += noncelen+CMAC_LENGTH;
445
0
                        buf.left -= noncelen+CMAC_LENGTH;
446
0
                        sawAEEF = true;
447
0
                        break;
448
250
                    default:
449
                        /* Non NTS extensions on reply from server.
450
                         * Call out when we get some that we want.
451
                         * For now, just ignore it */
452
250
                        buf.next += length;
453
250
                        buf.left -= length;
454
250
                        break;
455
483
                }
456
483
        }
457
458
        //  printf("ECRx: %d, %d  %d, %d\n", sawAEEF, peer->nts_state.count,
459
        //      peer->nts_state.writeIdx, peer->nts_state.readIdx);
460
81
        if (!sawAEEF) {
461
81
                return false;
462
81
        }
463
0
        if (buf.left > 0)
464
0
                return false;
465
0
        nts_cnt.client_recv_good++;
466
0
        nts_cnt.client_recv_bad--;
467
        return true;
468
0
}
469
// end