|
| 1 | +diff --git a/src/event/ngx_event_openssl.c b/src/event/ngx_event_openssl.c |
| 2 | +index 2f68a55de..d069b72ea 100644 |
| 3 | +--- a/src/event/ngx_event_openssl.c |
| 4 | ++++ b/src/event/ngx_event_openssl.c |
| 5 | +@@ -11,6 +11,7 @@ |
| 6 | + #include <ngx_event.h> |
| 7 | + |
| 8 | + |
| 9 | ++#define NGX_SSL_JA3_BUFFER_SIZE 256 /* = (sizeof(uint16_t) * 128) */ |
| 10 | + #define NGX_SSL_PASSWORD_BUFFER_SIZE 4096 |
| 11 | + |
| 12 | + #if (NGX_HAVE_NTLS) |
| 13 | +@@ -1971,6 +1972,40 @@ ngx_ssl_set_session(ngx_connection_t *c, ngx_ssl_session_t *session) |
| 14 | + return NGX_OK; |
| 15 | + } |
| 16 | + |
| 17 | ++int |
| 18 | ++ngx_ssl_client_hello_ja3_cb(SSL *s, int *al, void *arg) |
| 19 | ++{ |
| 20 | ++ ngx_connection_t *c = arg; |
| 21 | ++ ngx_str_t *ja; |
| 22 | ++ |
| 23 | ++ if (c == NULL) { |
| 24 | ++ return SSL_CLIENT_HELLO_SUCCESS; |
| 25 | ++ } |
| 26 | ++ |
| 27 | ++ if (c->ssl == NULL) { |
| 28 | ++ return SSL_CLIENT_HELLO_SUCCESS; |
| 29 | ++ } |
| 30 | ++ |
| 31 | ++ ja = &c->ssl->fp_ja3_data; |
| 32 | ++ ja->len = NGX_SSL_JA3_BUFFER_SIZE; |
| 33 | ++ |
| 34 | ++ ja->data = ngx_pnalloc(c->pool, c->ssl->fp_ja3_data.len); |
| 35 | ++ if (ja->data == NULL) { |
| 36 | ++ ja->len = 0; |
| 37 | ++ return SSL_CLIENT_HELLO_SUCCESS; |
| 38 | ++ } |
| 39 | ++ |
| 40 | ++ ja->len = SSL_client_hello_get_ja3_data(c->ssl->connection, ja->data, |
| 41 | ++ NGX_SSL_JA3_BUFFER_SIZE); |
| 42 | ++ if (ja->len == 0) { |
| 43 | ++ ngx_log_error(NGX_LOG_WARN, c->log, 0, "SSL_client_hello_get_ja3_data " |
| 44 | ++ "seems the buffer size is less that number of fileds; " |
| 45 | ++ "for this SSL connection can't get a ja3 hash string"); |
| 46 | ++ ja->data = NULL; |
| 47 | ++ } |
| 48 | ++ |
| 49 | ++ return SSL_CLIENT_HELLO_SUCCESS; |
| 50 | ++} |
| 51 | + |
| 52 | + ngx_int_t |
| 53 | + ngx_ssl_handshake(ngx_connection_t *c) |
| 54 | +@@ -1991,6 +2026,9 @@ ngx_ssl_handshake(ngx_connection_t *c) |
| 55 | + |
| 56 | + ngx_ssl_clear_error(c->log); |
| 57 | + |
| 58 | ++ (void) SSL_CTX_set_client_hello_cb(c->ssl->session_ctx, |
| 59 | ++ ngx_ssl_client_hello_ja3_cb, c); |
| 60 | ++ |
| 61 | + n = SSL_do_handshake(c->ssl->connection); |
| 62 | + |
| 63 | + ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_do_handshake: %d", n); |
| 64 | +diff --git a/src/event/ngx_event_openssl.h b/src/event/ngx_event_openssl.h |
| 65 | +index 42a01ce62..c4adc270d 100644 |
| 66 | +--- a/src/event/ngx_event_openssl.h |
| 67 | ++++ b/src/event/ngx_event_openssl.h |
| 68 | +@@ -129,6 +129,11 @@ struct ngx_ssl_connection_s { |
| 69 | + unsigned in_ocsp:1; |
| 70 | + unsigned early_preread:1; |
| 71 | + unsigned write_blocked:1; |
| 72 | ++ |
| 73 | ++ ngx_str_t fp_ja3_data; |
| 74 | ++ ngx_str_t fp_ja3_str; |
| 75 | ++ ngx_str_t fp_ja3_hash; |
| 76 | ++ uint16_t fp_tls_greased; |
| 77 | + }; |
| 78 | + |
| 79 | + |
| 80 | +diff --git a/src/http/v2/ngx_http_v2.c b/src/http/v2/ngx_http_v2.c |
| 81 | +index 0f5bd3de8..1b85e378d 100644 |
| 82 | +--- a/src/http/v2/ngx_http_v2.c |
| 83 | ++++ b/src/http/v2/ngx_http_v2.c |
| 84 | +@@ -301,6 +301,8 @@ ngx_http_v2_init(ngx_event_t *rev) |
| 85 | + ngx_add_timer(rev, cscf->client_header_timeout); |
| 86 | + } |
| 87 | + |
| 88 | ++ h2c->fp_fingerprinted = 0; |
| 89 | ++ |
| 90 | + c->idle = 1; |
| 91 | + ngx_reusable_connection(c, 0); |
| 92 | + |
| 93 | +@@ -1352,6 +1354,14 @@ ngx_http_v2_state_headers(ngx_http_v2_connection_t *h2c, u_char *pos, |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | ++ if (!h2c->fp_fingerprinted && h2c->fp_priorities.len < 32) { |
| 98 | ++ h2c->fp_priorities.data[h2c->fp_priorities.len] = (uint8_t)stream->node->id; |
| 99 | ++ h2c->fp_priorities.data[h2c->fp_priorities.len+1] = (uint8_t)excl; |
| 100 | ++ h2c->fp_priorities.data[h2c->fp_priorities.len+2] = (uint8_t)depend; |
| 101 | ++ h2c->fp_priorities.data[h2c->fp_priorities.len+3] = (uint8_t)(weight-1); |
| 102 | ++ h2c->fp_priorities.len += 4; |
| 103 | ++ } |
| 104 | ++ |
| 105 | + return ngx_http_v2_state_header_block(h2c, pos, end); |
| 106 | + |
| 107 | + rst_stream: |
| 108 | +@@ -1775,6 +1785,9 @@ ngx_http_v2_state_process_header(ngx_http_v2_connection_t *h2c, u_char *pos, |
| 109 | + } |
| 110 | + |
| 111 | + if (header->name.data[0] == ':') { |
| 112 | ++ if (!h2c->fp_fingerprinted && h2c->fp_pseudoheaders.len < 32 && header->name.len > 1) |
| 113 | ++ h2c->fp_pseudoheaders.data[h2c->fp_pseudoheaders.len++] = header->name.data[1]; |
| 114 | ++ |
| 115 | + rc = ngx_http_v2_pseudo_header(r, header); |
| 116 | + |
| 117 | + if (rc == NGX_OK) { |
| 118 | +@@ -2194,6 +2207,12 @@ ngx_http_v2_state_settings_params(ngx_http_v2_connection_t *h2c, u_char *pos, |
| 119 | + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0, |
| 120 | + "http2 setting %ui:%ui", id, value); |
| 121 | + |
| 122 | ++ if (!h2c->fp_fingerprinted && h2c->fp_settings.len < 32) { |
| 123 | ++ h2c->fp_settings.data[h2c->fp_settings.len] = (uint8_t)id; |
| 124 | ++ *(uint32_t*)(h2c->fp_settings.data + h2c->fp_settings.len + 1) = (uint32_t)value; |
| 125 | ++ h2c->fp_settings.len += 5; |
| 126 | ++ } |
| 127 | ++ |
| 128 | + switch (id) { |
| 129 | + |
| 130 | + case NGX_HTTP_V2_INIT_WINDOW_SIZE_SETTING: |
| 131 | +@@ -2478,6 +2497,9 @@ ngx_http_v2_state_window_update(ngx_http_v2_connection_t *h2c, u_char *pos, |
| 132 | + } |
| 133 | + |
| 134 | + h2c->send_window += window; |
| 135 | ++ if (!h2c->fp_fingerprinted) { |
| 136 | ++ h2c->fp_windowupdate = window; |
| 137 | ++ } |
| 138 | + |
| 139 | + while (!ngx_queue_empty(&h2c->waiting)) { |
| 140 | + q = ngx_queue_head(&h2c->waiting); |
| 141 | +diff --git a/src/http/v2/ngx_http_v2.h b/src/http/v2/ngx_http_v2.h |
| 142 | +index 6751b3026..60a68a0fd 100644 |
| 143 | +--- a/src/http/v2/ngx_http_v2.h |
| 144 | ++++ b/src/http/v2/ngx_http_v2.h |
| 145 | +@@ -17,6 +17,8 @@ |
| 146 | + |
| 147 | + #define NGX_HTTP_V2_STATE_BUFFER_SIZE 16 |
| 148 | + |
| 149 | ++#define NGX_FP_V2_BUFFER_SIZE 32 |
| 150 | ++ |
| 151 | + #define NGX_HTTP_V2_DEFAULT_FRAME_SIZE (1 << 14) |
| 152 | + #define NGX_HTTP_V2_MAX_FRAME_SIZE ((1 << 24) - 1) |
| 153 | + |
| 154 | +@@ -121,6 +123,12 @@ typedef struct { |
| 155 | + } ngx_http_v2_hpack_t; |
| 156 | + |
| 157 | + |
| 158 | ++typedef struct { |
| 159 | ++ u_char data[NGX_FP_V2_BUFFER_SIZE]; |
| 160 | ++ size_t len; |
| 161 | ++} ngx_http_v2_fp_fixed_str_t; |
| 162 | ++ |
| 163 | ++ |
| 164 | + struct ngx_http_v2_connection_s { |
| 165 | + ngx_connection_t *connection; |
| 166 | + ngx_http_connection_t *http_connection; |
| 167 | +@@ -168,6 +176,13 @@ struct ngx_http_v2_connection_s { |
| 168 | + unsigned table_update:1; |
| 169 | + unsigned blocked:1; |
| 170 | + unsigned goaway:1; |
| 171 | ++ |
| 172 | ++ unsigned fp_fingerprinted:1; |
| 173 | ++ ngx_http_v2_fp_fixed_str_t fp_settings, |
| 174 | ++ fp_priorities, |
| 175 | ++ fp_pseudoheaders; |
| 176 | ++ ngx_uint_t fp_windowupdate; |
| 177 | ++ ngx_str_t fp_str; |
| 178 | + }; |
| 179 | + |
| 180 | + |
0 commit comments