Mercurial > hg > nginx
comparison src/event/ngx_event_quic.c @ 8183:6091506af0f7 quic
Aded the "ngx_quic_hexdump" macro.
ngx_quic_hexdump0(log, format, buffer, buffer_size);
- logs hexdump of buffer to specified error log
ngx_quic_hexdump0(c->log, "this is foo:", foo.data, foo.len);
ngx_quic_hexdump(log, format, buffer, buffer_size, ...)
- same as hexdump0, but more format/args possible:
ngx_quic_hexdump(c->log, "a=%d b=%d, foo is:", foo.data, foo.len, a, b);
author | Vladimir Homutov <vl@nginx.com> |
---|---|
date | Mon, 02 Mar 2020 21:38:03 +0300 |
parents | b28ea685a56e |
children | ec1f84996990 |
comparison
equal
deleted
inserted
replaced
8182:b28ea685a56e | 8183:6091506af0f7 |
---|---|
43 #define ngx_quic_write_uint16_aligned(p, s) \ | 43 #define ngx_quic_write_uint16_aligned(p, s) \ |
44 (*(uint16_t *) (p) = htons((uint16_t) (s)), (p) + sizeof(uint16_t)) | 44 (*(uint16_t *) (p) = htons((uint16_t) (s)), (p) + sizeof(uint16_t)) |
45 | 45 |
46 #define ngx_quic_write_uint32_aligned(p, s) \ | 46 #define ngx_quic_write_uint32_aligned(p, s) \ |
47 (*(uint32_t *) (p) = htonl((uint32_t) (s)), (p) + sizeof(uint32_t)) | 47 (*(uint32_t *) (p) = htonl((uint32_t) (s)), (p) + sizeof(uint32_t)) |
48 | |
49 | |
50 | |
51 #if (NGX_DEBUG) | |
52 | |
53 #define ngx_quic_hexdump(log, fmt, data, len, ...) \ | |
54 do { \ | |
55 ngx_int_t m; \ | |
56 u_char buf[2048]; \ | |
57 \ | |
58 if (log->log_level & NGX_LOG_DEBUG_EVENT) { \ | |
59 m = ngx_hex_dump(buf, (u_char *) data, ngx_min(len, 1024)) - buf; \ | |
60 ngx_log_debug(NGX_LOG_DEBUG_EVENT, log, 0, \ | |
61 "%s: " fmt " %*s%s, len: %uz", \ | |
62 __FUNCTION__, __VA_ARGS__, m, buf, \ | |
63 len < 2048 ? "" : "...", len); \ | |
64 } \ | |
65 } while (0) | |
66 | |
67 #else | |
68 | |
69 #define ngx_quic_hexdump(log, fmt, data, len, ...) | |
70 | |
71 #endif | |
72 | |
73 #define ngx_quic_hexdump0(log, fmt, data, len) \ | |
74 ngx_quic_hexdump(log, fmt "%s", data, len, "") \ | |
75 | |
48 | 76 |
49 | 77 |
50 /* TODO: real states, these are stubs */ | 78 /* TODO: real states, these are stubs */ |
51 typedef enum { | 79 typedef enum { |
52 NGX_QUIC_ST_INITIAL, | 80 NGX_QUIC_ST_INITIAL, |
184 | 212 |
185 c = ngx_ssl_get_connection((ngx_ssl_conn_t *) ssl_conn); | 213 c = ngx_ssl_get_connection((ngx_ssl_conn_t *) ssl_conn); |
186 | 214 |
187 //ngx_ssl_handshake_log(c); // TODO: enable again | 215 //ngx_ssl_handshake_log(c); // TODO: enable again |
188 | 216 |
189 #if (NGX_DEBUG) | 217 ngx_quic_hexdump(c->log, "level:%d read", read_secret, secret_len, level); |
190 if (c->log->log_level & NGX_LOG_DEBUG_EVENT) { | 218 ngx_quic_hexdump(c->log, "level:%d read", write_secret, secret_len, level); |
191 u_char buf[64]; | |
192 size_t m; | |
193 | |
194 m = ngx_hex_dump(buf, (u_char *) read_secret, secret_len) - buf; | |
195 ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
196 "set_encryption_secrets: read %*s, len: %uz, level:%d", | |
197 m, buf, secret_len, (int) level); | |
198 | |
199 m = ngx_hex_dump(buf, (u_char *) write_secret, secret_len) - buf; | |
200 ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
201 "set_encryption_secrets: write %*s, len: %uz, level:%d", | |
202 m, buf, secret_len, (int) level); | |
203 } | |
204 #endif | |
205 | 219 |
206 name = (u_char *) SSL_get_cipher(ssl_conn); | 220 name = (u_char *) SSL_get_cipher(ssl_conn); |
207 | 221 |
208 if (ngx_strcasecmp(name, (u_char *) "TLS_AES_128_GCM_SHA256") == 0 | 222 if (ngx_strcasecmp(name, (u_char *) "TLS_AES_128_GCM_SHA256") == 0 |
209 || ngx_strcasecmp(name, (u_char *) "(NONE)") == 0) | 223 || ngx_strcasecmp(name, (u_char *) "(NONE)") == 0) |
277 static int | 291 static int |
278 ngx_quic_add_handshake_data(ngx_ssl_conn_t *ssl_conn, | 292 ngx_quic_add_handshake_data(ngx_ssl_conn_t *ssl_conn, |
279 enum ssl_encryption_level_t level, const uint8_t *data, size_t len) | 293 enum ssl_encryption_level_t level, const uint8_t *data, size_t len) |
280 { | 294 { |
281 u_char *p, *pnp, *name, *nonce, *sample; | 295 u_char *p, *pnp, *name, *nonce, *sample; |
282 ngx_int_t m; | |
283 ngx_str_t in, out, ad; | 296 ngx_str_t in, out, ad; |
284 static int pn; | 297 static int pn; |
285 const EVP_CIPHER *cipher; | 298 const EVP_CIPHER *cipher; |
286 ngx_connection_t *c; | 299 ngx_connection_t *c; |
287 ngx_quic_secret_t *secret; | 300 ngx_quic_secret_t *secret; |
288 ngx_quic_connection_t *qc; | 301 ngx_quic_connection_t *qc; |
289 u_char buf[2048], mask[16]; | 302 u_char mask[16]; |
290 | 303 |
291 c = ngx_ssl_get_connection((ngx_ssl_conn_t *) ssl_conn); | 304 c = ngx_ssl_get_connection((ngx_ssl_conn_t *) ssl_conn); |
292 qc = c->quic; | 305 qc = c->quic; |
293 | 306 |
294 //ngx_ssl_handshake_log(c); // TODO: enable again | 307 //ngx_ssl_handshake_log(c); // TODO: enable again |
305 | 318 |
306 default: | 319 default: |
307 return 0; | 320 return 0; |
308 } | 321 } |
309 | 322 |
310 m = ngx_hex_dump(buf, (u_char *) data, ngx_min(len, 1024)) - buf; | 323 ngx_quic_hexdump(c->log, "level:%d read", data, len, level); |
311 ngx_log_debug5(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
312 "ngx_quic_add_handshake_data: %*s%s, len: %uz, level:%d", | |
313 m, buf, len < 2048 ? "" : "...", len, (int) level); | |
314 | 324 |
315 in.data = ngx_alloc(4 + len + 5 /*minimal ACK*/, c->log); | 325 in.data = ngx_alloc(4 + len + 5 /*minimal ACK*/, c->log); |
316 if (in.data == 0) { | 326 if (in.data == 0) { |
317 return 0; | 327 return 0; |
318 } | 328 } |
367 *p++ = pn++; | 377 *p++ = pn++; |
368 } | 378 } |
369 | 379 |
370 ad.len = p - ad.data; | 380 ad.len = p - ad.data; |
371 | 381 |
372 m = ngx_hex_dump(buf, ad.data, ad.len) - buf; | 382 ngx_quic_hexdump0(c->log, "ad", data, len); |
373 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
374 "ngx_quic_add_handshake_data ad: %*s, len: %uz", | |
375 m, buf, ad.len); | |
376 | |
377 | 383 |
378 name = (u_char *) SSL_get_cipher(ssl_conn); | 384 name = (u_char *) SSL_get_cipher(ssl_conn); |
379 | 385 |
380 if (ngx_strcasecmp(name, (u_char *) "TLS_AES_128_GCM_SHA256") == 0 | 386 if (ngx_strcasecmp(name, (u_char *) "TLS_AES_128_GCM_SHA256") == 0 |
381 || ngx_strcasecmp(name, (u_char *) "(NONE)") == 0) | 387 || ngx_strcasecmp(name, (u_char *) "(NONE)") == 0) |
392 nonce = ngx_pstrdup(c->pool, &secret->iv); | 398 nonce = ngx_pstrdup(c->pool, &secret->iv); |
393 if (level == ssl_encryption_handshake) { | 399 if (level == ssl_encryption_handshake) { |
394 nonce[11] ^= (pn - 1); | 400 nonce[11] ^= (pn - 1); |
395 } | 401 } |
396 | 402 |
397 m = ngx_hex_dump(buf, (u_char *) secret->iv.data, 12) - buf; | 403 ngx_quic_hexdump0(c->log, "server_iv", secret->iv.data, 12); |
398 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0, | 404 ngx_quic_hexdump(c->log, "sample: n=%d nonce", nonce, 12, pn - 1); |
399 "ngx_quic_add_handshake_data sample: server_iv %*s", | |
400 m, buf); | |
401 m = ngx_hex_dump(buf, (u_char *) nonce, 12) - buf; | |
402 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
403 "ngx_quic_add_handshake_data sample: n=%d nonce %*s", | |
404 pn - 1, m, buf); | |
405 | 405 |
406 if (ngx_quic_tls_seal(c, cipher, secret, &out, nonce, &in, &ad) != NGX_OK) | 406 if (ngx_quic_tls_seal(c, cipher, secret, &out, nonce, &in, &ad) != NGX_OK) |
407 { | 407 { |
408 return 0; | 408 return 0; |
409 } | 409 } |
411 sample = &out.data[3]; // pnl=0 | 411 sample = &out.data[3]; // pnl=0 |
412 if (ngx_quic_tls_hp(c, EVP_aes_128_ecb(), secret, mask, sample) != NGX_OK) { | 412 if (ngx_quic_tls_hp(c, EVP_aes_128_ecb(), secret, mask, sample) != NGX_OK) { |
413 return 0; | 413 return 0; |
414 } | 414 } |
415 | 415 |
416 m = ngx_hex_dump(buf, (u_char *) sample, 16) - buf; | 416 ngx_quic_hexdump0(c->log, "sample", sample, 16); |
417 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0, | 417 ngx_quic_hexdump0(c->log, "mask", mask, 16); |
418 "ngx_quic_add_handshake_data sample: %*s, len: %uz", | 418 ngx_quic_hexdump0(c->log, "hp_key", secret->hp.data, 16); |
419 m, buf, 16); | |
420 | |
421 m = ngx_hex_dump(buf, (u_char *) mask, 16) - buf; | |
422 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
423 "ngx_quic_add_handshake_data mask: %*s, len: %uz", | |
424 m, buf, 16); | |
425 | |
426 m = ngx_hex_dump(buf, (u_char *) secret->hp.data, 16) - buf; | |
427 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
428 "ngx_quic_add_handshake_data hp_key: %*s, len: %uz", | |
429 m, buf, 16); | |
430 | 419 |
431 // header protection, pnl = 0 | 420 // header protection, pnl = 0 |
432 ad.data[0] ^= mask[0] & 0x0f; | 421 ad.data[0] ^= mask[0] & 0x0f; |
433 *pnp ^= mask[1]; | 422 *pnp ^= mask[1]; |
434 | 423 |
438 } | 427 } |
439 | 428 |
440 p = ngx_cpymem(packet, ad.data, ad.len); | 429 p = ngx_cpymem(packet, ad.data, ad.len); |
441 p = ngx_cpymem(p, out.data, out.len); | 430 p = ngx_cpymem(p, out.data, out.len); |
442 | 431 |
443 m = ngx_hex_dump(buf, (u_char *) packet, ngx_min(1024, p - packet)) - buf; | 432 ngx_quic_hexdump0(c->log, "packet", packet, p - packet); |
444 ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
445 "ngx_quic_add_handshake_data packet: %*s%s, len: %uz", | |
446 m, buf, len < 2048 ? "" : "...", p - packet); | |
447 | 433 |
448 // TODO: save state of data to send into qc (push into queue) | 434 // TODO: save state of data to send into qc (push into queue) |
449 | 435 |
450 qc->out.data = packet; | 436 qc->out.data = packet; |
451 qc->out.len = p - packet; | 437 qc->out.len = p - packet; |
488 | 474 |
489 | 475 |
490 static ngx_int_t | 476 static ngx_int_t |
491 ngx_quic_new_connection(ngx_connection_t *c, ngx_ssl_t *ssl, ngx_buf_t *b) | 477 ngx_quic_new_connection(ngx_connection_t *c, ngx_ssl_t *ssl, ngx_buf_t *b) |
492 { | 478 { |
493 int n, sslerr; | 479 int n, sslerr; |
494 #if (NGX_DEBUG) | 480 ngx_quic_connection_t *qc; |
495 u_char buf[512]; | |
496 size_t m; | |
497 #endif | |
498 | |
499 ngx_quic_connection_t *qc; | |
500 | 481 |
501 if ((b->pos[0] & 0xf0) != 0xc0) { | 482 if ((b->pos[0] & 0xf0) != 0xc0) { |
502 ngx_log_error(NGX_LOG_INFO, c->log, 0, "invalid initial packet"); | 483 ngx_log_error(NGX_LOG_INFO, c->log, 0, "invalid initial packet"); |
503 return NGX_ERROR; | 484 return NGX_ERROR; |
504 } | 485 } |
566 * draft-ietf-quic-tls-23#section-5.4.[34]: | 547 * draft-ietf-quic-tls-23#section-5.4.[34]: |
567 * AES-Based and ChaCha20-Based header protections sample 16 bytes | 548 * AES-Based and ChaCha20-Based header protections sample 16 bytes |
568 */ | 549 */ |
569 u_char *sample = b->pos + 4; | 550 u_char *sample = b->pos + 4; |
570 | 551 |
571 #if (NGX_DEBUG) | 552 ngx_quic_hexdump0(c->log, "DCID", qc->dcid.data, qc->dcid.len); |
572 if (c->log->log_level & NGX_LOG_DEBUG_EVENT) { | 553 ngx_quic_hexdump0(c->log, "SCID", qc->scid.data, qc->scid.len); |
573 m = ngx_hex_dump(buf, qc->dcid.data, qc->dcid.len) - buf; | 554 ngx_quic_hexdump0(c->log, "token", qc->token.data, qc->token.len); |
574 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0, | 555 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, |
575 "quic DCID: %*s, len: %uz", m, buf, qc->dcid.len); | 556 "quic packet length: %d", plen); |
576 | 557 ngx_quic_hexdump0(c->log, "sample", sample, 16); |
577 m = ngx_hex_dump(buf, qc->scid.data, qc->scid.len) - buf; | 558 |
578 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
579 "quic SCID: %*s, len: %uz", m, buf, qc->scid.len); | |
580 | |
581 m = ngx_hex_dump(buf, qc->token.data, qc->token.len) - buf; | |
582 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
583 "quic token: %*s, len: %uz", m, buf, qc->token.len); | |
584 | |
585 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
586 "quic packet length: %d", plen); | |
587 | |
588 m = ngx_hex_dump(buf, sample, 16) - buf; | |
589 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
590 "quic sample: %*s", m, buf); | |
591 } | |
592 #endif | |
593 | 559 |
594 // initial secret | 560 // initial secret |
595 | 561 |
596 size_t is_len; | 562 size_t is_len; |
597 uint8_t is[SHA256_DIGEST_LENGTH]; | 563 uint8_t is[SHA256_DIGEST_LENGTH]; |
617 ngx_str_t iss = { | 583 ngx_str_t iss = { |
618 .data = is, | 584 .data = is, |
619 .len = is_len | 585 .len = is_len |
620 }; | 586 }; |
621 | 587 |
622 #if (NGX_DEBUG) | 588 ngx_quic_hexdump0(c->log, "salt", salt, sizeof(salt)); |
623 if (c->log->log_level & NGX_LOG_DEBUG_EVENT) { | 589 ngx_quic_hexdump0(c->log, "initial secret", is, is_len); |
624 m = ngx_hex_dump(buf, (uint8_t *) salt, sizeof(salt)) - buf; | |
625 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
626 "quic salt: %*s, len: %uz", m, buf, sizeof(salt)); | |
627 | |
628 m = ngx_hex_dump(buf, is, is_len) - buf; | |
629 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
630 "quic initial secret: %*s, len: %uz", m, buf, is_len); | |
631 } | |
632 #endif | |
633 | 590 |
634 /* draft-ietf-quic-tls-23#section-5.2 */ | 591 /* draft-ietf-quic-tls-23#section-5.2 */ |
635 qc->client_in.secret.len = SHA256_DIGEST_LENGTH; | 592 qc->client_in.secret.len = SHA256_DIGEST_LENGTH; |
636 qc->server_in.secret.len = SHA256_DIGEST_LENGTH; | 593 qc->server_in.secret.len = SHA256_DIGEST_LENGTH; |
637 | 594 |
710 | 667 |
711 u_char clearflags = flags ^ (mask[0] & 0x0f); | 668 u_char clearflags = flags ^ (mask[0] & 0x0f); |
712 ngx_int_t pnl = (clearflags & 0x03) + 1; | 669 ngx_int_t pnl = (clearflags & 0x03) + 1; |
713 uint64_t pn = ngx_quic_parse_pn(&b->pos, pnl, &mask[1]); | 670 uint64_t pn = ngx_quic_parse_pn(&b->pos, pnl, &mask[1]); |
714 | 671 |
715 #if (NGX_DEBUG) | 672 ngx_quic_hexdump0(c->log, "sample", sample, 16); |
716 if (c->log->log_level & NGX_LOG_DEBUG_EVENT) { | 673 ngx_quic_hexdump0(c->log, "mask", mask, 5); |
717 m = ngx_hex_dump(buf, sample, 16) - buf; | 674 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0, |
718 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0, | 675 "quic packet number: %uL, len: %xi", pn, pnl); |
719 "quic sample: %*s", m, buf); | |
720 | |
721 m = ngx_hex_dump(buf, mask, 5) - buf; | |
722 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
723 "quic mask: %*s", m, buf); | |
724 | |
725 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
726 "quic packet number: %uL, len: %xi", pn, pnl); | |
727 } | |
728 #endif | |
729 | 676 |
730 // packet protection | 677 // packet protection |
731 | 678 |
732 ngx_str_t in; | 679 ngx_str_t in; |
733 in.data = b->pos; | 680 in.data = b->pos; |
745 ad.data[ad.len - pnl] = (u_char)pn; | 692 ad.data[ad.len - pnl] = (u_char)pn; |
746 | 693 |
747 uint8_t *nonce = ngx_pstrdup(c->pool, &qc->client_in.iv); | 694 uint8_t *nonce = ngx_pstrdup(c->pool, &qc->client_in.iv); |
748 nonce[11] ^= pn; | 695 nonce[11] ^= pn; |
749 | 696 |
750 #if (NGX_DEBUG) | 697 ngx_quic_hexdump0(c->log, "nonce", nonce, 12); |
751 if (c->log->log_level & NGX_LOG_DEBUG_EVENT) { | 698 ngx_quic_hexdump0(c->log, "ad", ad.data, ad.len); |
752 m = ngx_hex_dump(buf, nonce, 12) - buf; | |
753 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
754 "quic nonce: %*s, len: %uz", m, buf, 12); | |
755 | |
756 m = ngx_hex_dump(buf, ad.data, ad.len) - buf; | |
757 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
758 "quic ad: %*s, len: %uz", m, buf, ad.len); | |
759 } | |
760 #endif | |
761 | 699 |
762 ngx_str_t out; | 700 ngx_str_t out; |
763 | 701 |
764 if (ngx_quic_tls_open(c, EVP_aes_128_gcm(), &qc->client_in, &out, nonce, | 702 if (ngx_quic_tls_open(c, EVP_aes_128_gcm(), &qc->client_in, &out, nonce, |
765 &in, &ad) | 703 &in, &ad) |
766 != NGX_OK) | 704 != NGX_OK) |
767 { | 705 { |
768 return NGX_ERROR; | 706 return NGX_ERROR; |
769 } | 707 } |
770 | 708 |
771 #if (NGX_DEBUG) | 709 ngx_quic_hexdump0(c->log, "packet payload", out.data, out.len); |
772 if (c->log->log_level & NGX_LOG_DEBUG_EVENT) { | |
773 m = ngx_hex_dump(buf, out.data, ngx_min(out.len, 256)) - buf; | |
774 ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
775 "quic packet payload: %*s%s, len: %uz", | |
776 m, buf, m < 512 ? "" : "...", out.len); | |
777 } | |
778 #endif | |
779 | 710 |
780 if (out.data[0] != 0x06) { | 711 if (out.data[0] != 0x06) { |
781 ngx_log_error(NGX_LOG_INFO, c->log, 0, | 712 ngx_log_error(NGX_LOG_INFO, c->log, 0, |
782 "unexpected frame in initial packet"); | 713 "unexpected frame in initial packet"); |
783 return NGX_ERROR; | 714 return NGX_ERROR; |
860 | 791 |
861 | 792 |
862 static ngx_int_t | 793 static ngx_int_t |
863 ngx_quic_handshake_input(ngx_connection_t *c, ngx_buf_t *bb) | 794 ngx_quic_handshake_input(ngx_connection_t *c, ngx_buf_t *bb) |
864 { | 795 { |
865 size_t m; | |
866 ssize_t n; | 796 ssize_t n; |
867 ngx_str_t out; | 797 ngx_str_t out; |
868 const EVP_CIPHER *cipher; | 798 const EVP_CIPHER *cipher; |
869 ngx_quic_connection_t *qc; | 799 ngx_quic_connection_t *qc; |
870 u_char buf[4096], *p, *b; | 800 u_char *p, *b; |
871 | 801 |
872 qc = c->quic; | 802 qc = c->quic; |
873 | 803 |
874 n = bb->last - bb->pos; | 804 n = bb->last - bb->pos; |
875 p = bb->pos; | 805 p = bb->pos; |
876 b = bb->start; | 806 b = bb->start; |
877 | 807 |
878 m = ngx_hex_dump(buf, b, n) - buf; | 808 ngx_quic_hexdump0(c->log, "input", buf, n); |
879 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
880 "quic handshake handler: %*s, len: %uz", m, buf, n); | |
881 | 809 |
882 if ((p[0] & 0xf0) != 0xe0) { | 810 if ((p[0] & 0xf0) != 0xe0) { |
883 ngx_log_error(NGX_LOG_INFO, c->log, 0, "invalid packet type"); | 811 ngx_log_error(NGX_LOG_INFO, c->log, 0, "invalid packet type"); |
884 return NGX_ERROR; | 812 return NGX_ERROR; |
885 } | 813 } |
930 return NGX_ERROR; | 858 return NGX_ERROR; |
931 } | 859 } |
932 | 860 |
933 u_char *sample = p + 4; | 861 u_char *sample = p + 4; |
934 | 862 |
935 m = ngx_hex_dump(buf, sample, 16) - buf; | 863 ngx_quic_hexdump0(c->log, "sample", sample, 16); |
936 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0, "quic sample: %*s", m, buf); | |
937 | 864 |
938 // header protection | 865 // header protection |
939 | 866 |
940 uint8_t mask[16]; | 867 uint8_t mask[16]; |
941 if (ngx_quic_tls_hp(c, EVP_aes_128_ecb(), &qc->client_hs, mask, sample) | 868 if (ngx_quic_tls_hp(c, EVP_aes_128_ecb(), &qc->client_hs, mask, sample) |
946 | 873 |
947 u_char clearflags = flags ^ (mask[0] & 0x0f); | 874 u_char clearflags = flags ^ (mask[0] & 0x0f); |
948 ngx_int_t pnl = (clearflags & 0x03) + 1; | 875 ngx_int_t pnl = (clearflags & 0x03) + 1; |
949 uint64_t pn = ngx_quic_parse_pn(&p, pnl, &mask[1]); | 876 uint64_t pn = ngx_quic_parse_pn(&p, pnl, &mask[1]); |
950 | 877 |
951 #if (NGX_DEBUG) | 878 ngx_quic_hexdump0(c->log, "mask", mask, 5); |
952 if (c->log->log_level & NGX_LOG_DEBUG_EVENT) { | 879 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, |
953 m = ngx_hex_dump(buf, mask, 5) - buf; | 880 "quic clear flags: %xi", clearflags); |
954 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0, | 881 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0, |
955 "quic mask: %*s", m, buf); | 882 "quic packet number: %uL, len: %xi", pn, pnl); |
956 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
957 "quic clear flags: %xi", clearflags); | |
958 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
959 "quic packet number: %uL, len: %xi", pn, pnl); | |
960 } | |
961 #endif | |
962 | 883 |
963 // packet protection | 884 // packet protection |
964 | 885 |
965 ngx_str_t in; | 886 ngx_str_t in; |
966 in.data = p; | 887 in.data = p; |
978 ad.data[ad.len - pnl] = (u_char)pn; | 899 ad.data[ad.len - pnl] = (u_char)pn; |
979 | 900 |
980 uint8_t *nonce = ngx_pstrdup(c->pool, &qc->client_hs.iv); | 901 uint8_t *nonce = ngx_pstrdup(c->pool, &qc->client_hs.iv); |
981 nonce[11] ^= pn; | 902 nonce[11] ^= pn; |
982 | 903 |
983 #if (NGX_DEBUG) | 904 ngx_quic_hexdump0(c->log, "nonce", nonce, 12); |
984 if (c->log->log_level & NGX_LOG_DEBUG_EVENT) { | 905 ngx_quic_hexdump0(c->log, "ad", ad.data, ad.len); |
985 m = ngx_hex_dump(buf, nonce, 12) - buf; | |
986 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
987 "quic nonce: %*s, len: %uz", m, buf, 12); | |
988 | |
989 m = ngx_hex_dump(buf, ad.data, ad.len) - buf; | |
990 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
991 "quic ad: %*s, len: %uz", m, buf, ad.len); | |
992 } | |
993 #endif | |
994 | 906 |
995 u_char *name = (u_char *) SSL_get_cipher(c->ssl->connection); | 907 u_char *name = (u_char *) SSL_get_cipher(c->ssl->connection); |
996 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, | 908 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, |
997 "quic ssl cipher: %s", name); | 909 "quic ssl cipher: %s", name); |
998 | 910 |
1013 != NGX_OK) | 925 != NGX_OK) |
1014 { | 926 { |
1015 return NGX_ERROR; | 927 return NGX_ERROR; |
1016 } | 928 } |
1017 | 929 |
1018 #if (NGX_DEBUG) | 930 ngx_quic_hexdump0(c->log, "packet payload", out.data, out.len); |
1019 if (c->log->log_level & NGX_LOG_DEBUG_EVENT) { | |
1020 m = ngx_hex_dump(buf, out.data, ngx_min(out.len, 256)) - buf; | |
1021 ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
1022 "quic packet payload: %*s%s, len: %uz", | |
1023 m, buf, m < 512 ? "" : "...", out.len); | |
1024 } | |
1025 #endif | |
1026 | 931 |
1027 return NGX_OK; | 932 return NGX_OK; |
1028 } | 933 } |
1029 | 934 |
1030 | 935 |
1150 { | 1055 { |
1151 uint8_t *p; | 1056 uint8_t *p; |
1152 size_t info_len; | 1057 size_t info_len; |
1153 uint8_t info[20]; | 1058 uint8_t info[20]; |
1154 | 1059 |
1155 #if (NGX_DEBUG) | |
1156 u_char buf[512]; | |
1157 size_t m; | |
1158 #endif | |
1159 | |
1160 out->data = ngx_pnalloc(c->pool, out->len); | 1060 out->data = ngx_pnalloc(c->pool, out->len); |
1161 if (out->data == NULL) { | 1061 if (out->data == NULL) { |
1162 return NGX_ERROR; | 1062 return NGX_ERROR; |
1163 } | 1063 } |
1164 | 1064 |
1177 ngx_ssl_error(NGX_LOG_INFO, c->log, 0, | 1077 ngx_ssl_error(NGX_LOG_INFO, c->log, 0, |
1178 "ngx_hkdf_expand(%V) failed", label); | 1078 "ngx_hkdf_expand(%V) failed", label); |
1179 return NGX_ERROR; | 1079 return NGX_ERROR; |
1180 } | 1080 } |
1181 | 1081 |
1182 if (c->log->log_level & NGX_LOG_DEBUG_EVENT) { | 1082 ngx_quic_hexdump(c->log, "%V info", info, info_len, label); |
1183 m = ngx_hex_dump(buf, info, info_len) - buf; | 1083 ngx_quic_hexdump(c->log, "%V key", out->data, out->len, label); |
1184 ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
1185 "%V info: %*s, len: %uz", label, m, buf, info_len); | |
1186 | |
1187 m = ngx_hex_dump(buf, out->data, out->len) - buf; | |
1188 ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
1189 "%V key: %*s, len: %uz", label, m, buf, out->len); | |
1190 } | |
1191 | 1084 |
1192 return NGX_OK; | 1085 return NGX_OK; |
1193 } | 1086 } |
1194 | 1087 |
1195 | 1088 |