/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 | 0 | #define NONCE_LENGTH 16 |
30 | 0 | #define CMAC_LENGTH 16 |
31 | | |
32 | 0 | #define NTP_EX_HDR_LNG 4 |
33 | 0 | #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 | 0 | #define CookieLenUnknown -1 |
126 | 0 | bool extens_server_recv(struct ntspacket_t *ntspacket, uint8_t *pkt, int lng) { |
127 | 0 | struct BufCtl_t buf; |
128 | 0 | uint16_t aead; |
129 | 0 | unsigned noncelen, ctlen, noncepad, ctpad, fieldlen; |
130 | 0 | bool sawcookie, sawAEEF; |
131 | 0 | int cookielen; // cookie and placeholder(s) |
132 | |
|
133 | 0 | nts_cnt.server_recv_bad++; // assume bad, undo if OK |
134 | |
|
135 | 0 | buf.next = pkt+LEN_PKT_NOMAC; |
136 | 0 | buf.left = lng-LEN_PKT_NOMAC; |
137 | |
|
138 | 0 | sawcookie = sawAEEF = false; |
139 | 0 | cookielen = CookieLenUnknown; |
140 | 0 | ntspacket->uidlen = 0; |
141 | 0 | ntspacket->needed = 0; |
142 | |
|
143 | 0 | while (buf.left >= NTS_KE_HDR_LNG) { |
144 | 0 | uint16_t type; |
145 | 0 | int length, adlength; |
146 | 0 | size_t outlen; |
147 | 0 | uint8_t *nonce, *ciphertext; |
148 | 0 | uint8_t plaintext[MAX_EXT_LEN]; |
149 | 0 | bool ok; |
150 | |
|
151 | 0 | type = ex_next_record(&buf, &length); // length excludes header |
152 | 0 | if (length&3 || length > buf.left || length < 0) { |
153 | 0 | return false; |
154 | 0 | } |
155 | 0 | switch (type) { |
156 | 0 | case Unique_Identifier: |
157 | 0 | if (length > NTS_UID_MAX_LENGTH || length < NTS_UID_LENGTH) { |
158 | 0 | return false; |
159 | 0 | } |
160 | 0 | ntspacket->uidlen = length; |
161 | 0 | next_bytes(&buf, ntspacket->UID, length); |
162 | 0 | break; |
163 | 0 | case NTS_Cookie: |
164 | | /* cookies and placeholders must be the same length |
165 | | * in order to avoid amplification attacks. |
166 | | */ |
167 | 0 | if (sawcookie) { |
168 | 0 | return false; // second cookie |
169 | 0 | } |
170 | 0 | if (CookieLenUnknown == cookielen) { |
171 | 0 | cookielen = length; |
172 | 0 | } |
173 | 0 | else if (length != cookielen) { |
174 | 0 | return false; |
175 | 0 | } |
176 | 0 | ok = nts_unpack_cookie(buf.next, length, &aead, |
177 | 0 | ntspacket->c2s, |
178 | 0 | ntspacket->s2c, |
179 | 0 | &ntspacket->keylen); |
180 | 0 | if (!ok) { |
181 | 0 | return false; |
182 | 0 | } |
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 | 0 | case NTS_Cookie_Placeholder: |
190 | 0 | if (CookieLenUnknown == cookielen) { |
191 | 0 | cookielen = length; |
192 | 0 | } |
193 | 0 | else if (length != cookielen) { |
194 | 0 | return false; |
195 | 0 | } |
196 | 0 | ntspacket->needed++; |
197 | 0 | if (NTS_MAX_COOKIES < ntspacket->needed) { |
198 | 0 | return false; |
199 | 0 | } |
200 | 0 | buf.next += length; |
201 | 0 | buf.left -= length; |
202 | 0 | break; |
203 | 0 | case NTS_AEEF: |
204 | 0 | if (!sawcookie) { |
205 | 0 | return false; // no cookie yet, no c2s |
206 | 0 | } |
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 | 0 | 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 | 0 | buf.next += length; |
255 | 0 | buf.left -= length; |
256 | 0 | break; |
257 | 0 | } |
258 | 0 | } |
259 | | |
260 | 0 | if (!sawAEEF) { |
261 | 0 | return false; |
262 | 0 | } |
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 | 0 | bool extens_client_recv(struct peer *peer, uint8_t *pkt, int lng) { |
365 | 0 | struct BufCtl_t buf; |
366 | 0 | int idx; |
367 | 0 | bool sawAEEF = false; |
368 | |
|
369 | 0 | nts_cnt.client_recv_bad++; // assume bad, undo if OK |
370 | |
|
371 | 0 | buf.next = pkt+LEN_PKT_NOMAC; |
372 | 0 | buf.left = lng-LEN_PKT_NOMAC; |
373 | |
|
374 | 0 | while (buf.left >= NTS_KE_HDR_LNG) { |
375 | 0 | uint16_t type; |
376 | 0 | int length, adlength, noncelen; |
377 | 0 | uint8_t *nonce, *ciphertext, *plaintext; |
378 | 0 | size_t outlen; |
379 | 0 | bool ok; |
380 | |
|
381 | 0 | type = ex_next_record(&buf, &length); // length excludes header |
382 | 0 | if (length&3 || length > buf.left || length < 0) { |
383 | 0 | return false; |
384 | 0 | } |
385 | | // printf("ECR: %d, %d, %d\n", type, length, buf.left); |
386 | 0 | switch (type) { |
387 | 0 | case Unique_Identifier: |
388 | 0 | if (NTS_UID_LENGTH != length) { |
389 | 0 | return false; |
390 | 0 | } |
391 | 0 | if (0 != memcmp(buf.next, peer->nts_state.UID, |
392 | 0 | NTS_UID_LENGTH)) { |
393 | 0 | return false; |
394 | 0 | } |
395 | 0 | buf.next += length; |
396 | 0 | buf.left -= length; |
397 | 0 | break; |
398 | 0 | case NTS_Cookie: |
399 | 0 | if (!sawAEEF) |
400 | 0 | 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 | 0 | case NTS_AEEF: |
415 | | // backup over header |
416 | 0 | adlength = buf.next-NTP_EX_HDR_LNG-pkt; |
417 | 0 | if (NTP_EX_U16_LNG*2 > length) |
418 | 0 | return false; // garbage packet |
419 | 0 | noncelen = next_uint16(&buf); |
420 | 0 | outlen = next_uint16(&buf); |
421 | 0 | if (NONCE_LENGTH != noncelen) |
422 | 0 | return false; // garbage packet |
423 | 0 | if (outlen&3) |
424 | 0 | return false; // else round up |
425 | 0 | if (noncelen+CMAC_LENGTH > buf.left) |
426 | 0 | return false; // garbage packet |
427 | 0 | nonce = buf.next; |
428 | 0 | ciphertext = nonce+noncelen; |
429 | 0 | plaintext = ciphertext+CMAC_LENGTH; |
430 | 0 | outlen = buf.left-noncelen-CMAC_LENGTH; |
431 | | // printf("ECRa: %lu, %d\n", (long unsigned)outlen, |
432 | | // noncelen); |
433 | 0 | ok = AES_SIV_Decrypt(wire_ctx, |
434 | 0 | plaintext, &outlen, |
435 | 0 | peer->nts_state.s2c, |
436 | 0 | peer->nts_state.keylen, |
437 | 0 | nonce, noncelen, |
438 | 0 | ciphertext, outlen+CMAC_LENGTH, |
439 | 0 | pkt, adlength); |
440 | | // printf("ECRb: %d, %lu\n", ok, (long unsigned)outlen); |
441 | 0 | if (!ok) |
442 | 0 | 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 | 0 | 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 | 0 | buf.next += length; |
453 | 0 | buf.left -= length; |
454 | 0 | break; |
455 | 0 | } |
456 | 0 | } |
457 | | |
458 | | // printf("ECRx: %d, %d %d, %d\n", sawAEEF, peer->nts_state.count, |
459 | | // peer->nts_state.writeIdx, peer->nts_state.readIdx); |
460 | 0 | if (!sawAEEF) { |
461 | 0 | return false; |
462 | 0 | } |
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 |