Skip to content

Commit 509a4e8

Browse files
dedokphuslu
authored andcommitted
Specified variable behavior after typical functional tests. Also off preread module from nginx bootstrap, else error appear
1 parent 6000f93 commit 509a4e8

File tree

3 files changed

+76
-45
lines changed

3 files changed

+76
-45
lines changed

config

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@
66
ngx_addon_name=ngx_ssl_fingerprint_module
77

88
CORE_LIBS="$CORE_LIBS"
9-
109
CORE_INCS="$CORE_INCS $ngx_addon_dir/src"
1110

12-
STREAM_MODULES="ngx_stream_ssl_fingerprint_preread_module $STREAM_MODULES"
13-
1411
HTTP_MODULES="$HTTP_MODULES ngx_http_ssl_fingerprint_module"
1512

16-
NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
17-
$ngx_addon_dir/src/nginx_ssl_fingerprint.c \
18-
$ngx_addon_dir/src/ngx_stream_ssl_fingerprint_preread_module.c \
19-
$ngx_addon_dir/src/ngx_http_ssl_fingerprint_module.c
20-
"
13+
stream_module=""
2114

22-
CFLAGS="$CFLAGS -I$ngx_addon_dir"
15+
if [ $STREAM_SSL_PREREAD = YES ]; then
16+
STREAM_MODULES="ngx_stream_ssl_fingerprint_preread_module $STREAM_MODULES"
17+
stream_module="$ngx_addon_dir/src/ngx_stream_ssl_fingerprint_preread_module.c"
18+
fi
19+
20+
NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
21+
$ngx_addon_dir/src/nginx_ssl_fingerprint.c \
22+
$ngx_addon_dir/src/ngx_http_ssl_fingerprint_module.c \
23+
$stream_module"
2324

2425
have=NGX_JA3_FINGERPRING_MODULE . auto/have
2526

src/nginx_ssl_fingerprint.c

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -188,19 +188,33 @@ unsigned char *append_uint32(unsigned char* dst, uint32_t n)
188188
return dst;
189189
}
190190

191+
192+
/**
193+
* Params:
194+
* c and c->ssl should be valid pointers
195+
*
196+
* Returns:
197+
* NGX_OK - c->ssl->fp_ja3_str is already set
198+
* NGX_ERROR - something went wrong
199+
*/
191200
int ngx_ssl_ja3(ngx_connection_t *c)
192201
{
193202
u_char *ptr = NULL, *data = NULL;
194203
size_t num = 0, i;
195204
uint16_t n, greased = 0;
196205

197-
if (c == NULL || c->ssl == NULL) {
198-
return NGX_DECLINED;
199-
}
200-
201206
data = c->ssl->fp_ja3_data.data;
202207
if (data == NULL) {
203-
return NGX_DECLINED;
208+
/**
209+
* NOTE:
210+
* If we can't set it in OpenSSL,
211+
* then something defenetly something went wrong.
212+
* Typical production configuration has log level set to error,
213+
* this would help to debug this case, if it happened.
214+
*/
215+
ngx_log_error(NGX_LOG_WARN, c->log, 0,
216+
"ngx_ssl_ja3: fp_ja_data == NULL");
217+
return NGX_ERROR;
204218
}
205219

206220
if (c->ssl->fp_ja3_str.data != NULL) {
@@ -212,7 +226,7 @@ int ngx_ssl_ja3(ngx_connection_t *c)
212226
if (c->ssl->fp_ja3_str.data == NULL) {
213227
/** Else we break a data stream */
214228
c->ssl->fp_ja3_str.len = 0;
215-
return NGX_DECLINED /** NGX_ERROR? */;
229+
return NGX_ERROR;
216230
}
217231

218232
ngx_log_debug(NGX_LOG_DEBUG_EVENT, c->log, 0, "ngx_ssl_ja3: alloc bytes: [%d]\n", c->ssl->fp_ja3_str.len);
@@ -296,28 +310,33 @@ int ngx_ssl_ja3(ngx_connection_t *c)
296310
return NGX_OK;
297311
}
298312

313+
/**
314+
* Params:
315+
* c and c->ssl should be valid pointers and tested before.
316+
*
317+
* Returns:
318+
* NGX_OK - c->ssl->fp_ja3_hash is alread set
319+
* NGX_ERROR - something went wrong
320+
*/
299321
int ngx_ssl_ja3_hash(ngx_connection_t *c)
300322
{
301323
ngx_md5_t ctx;
302324
u_char hash_buf[16];
303325

304-
if (c == NULL
305-
|| c->ssl == NULL
306-
|| c->ssl->fp_ja3_hash.len > 0)
307-
{
308-
return NGX_DECLINED;
326+
if (c->ssl->fp_ja3_hash.len > 0) {
327+
return NGX_OK;
309328
}
310329

311-
if (ngx_ssl_ja3(c) == NGX_DECLINED) {
312-
return NGX_DECLINED;
330+
if (ngx_ssl_ja3(c) != NGX_OK) {
331+
return NGX_ERROR;
313332
}
314333

315334
c->ssl->fp_ja3_hash.len = 32;
316335
c->ssl->fp_ja3_hash.data = ngx_pnalloc(c->pool, c->ssl->fp_ja3_hash.len);
317336
if (c->ssl->fp_ja3_hash.data == NULL) {
318-
/** Else we break a stream */
337+
/** Else we can break a stream */
319338
c->ssl->fp_ja3_hash.len = 0;
320-
return NGX_DECLINED;
339+
return NGX_ERROR;
321340
}
322341

323342
ngx_log_debug(NGX_LOG_DEBUG_EVENT, c->log, 0, "ngx_ssl_ja3_hash: alloc bytes: [%d]\n", c->ssl->fp_ja3_hash.len);
@@ -330,25 +349,32 @@ int ngx_ssl_ja3_hash(ngx_connection_t *c)
330349
return NGX_OK;
331350
}
332351

352+
/**
353+
* Params:
354+
* c and h2c should be a valid pointers
355+
*
356+
* Returns:
357+
* NGX_OK -- h2c->fp_str is set
358+
* NGX_ERROR -- something went wrong
359+
*/
333360
int ngx_http2_fingerprint(ngx_connection_t *c, ngx_http_v2_connection_t *h2c)
334361
{
335362
unsigned char *pstr = NULL;
336363
unsigned short n = 0;
337364
size_t i;
338365

339-
if (c == NULL || h2c == NULL) {
340-
return NGX_DECLINED;
341-
}
342-
343366
if (h2c->fp_str.len > 0) {
344367
return NGX_OK;
345368
}
346369

347-
n = 4 + h2c->fp_settings.len * 3 + 10 + h2c->fp_priorities.len * 2 + h2c->fp_pseudoheaders.len * 2;
370+
n = 4 + h2c->fp_settings.len * 3
371+
+ 10 + h2c->fp_priorities.len * 2
372+
+ h2c->fp_pseudoheaders.len * 2;
373+
348374
h2c->fp_str.data = ngx_pnalloc(c->pool, n);
349375
if (h2c->fp_str.data == NULL) {
350376
/** Else we break a stream */
351-
return NGX_DECLINED;
377+
return NGX_ERROR;
352378
}
353379
pstr = h2c->fp_str.data;
354380

src/ngx_http_ssl_fingerprint_module.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,20 @@ static ngx_int_t
5656
ngx_http_ssl_greased(ngx_http_request_t *r,
5757
ngx_http_variable_value_t *v, uintptr_t data)
5858
{
59-
/* For access.log's map $http2_fingerpring {}:
59+
/* For access.log's map $http2_VAR {}:
6060
* if it's not found, then user could add a defined string */
6161
v->not_found = 1;
6262

63-
if (ngx_ssl_ja3(r->connection) != NGX_OK) {
63+
if (r->connection->ssl == NULL) {
6464
return NGX_OK;
6565
}
6666

67+
if (ngx_ssl_ja3(r->connection) != NGX_OK) {
68+
return NGX_ERROR;
69+
}
70+
6771
v->len = 1;
6872
v->data = (u_char*) (r->connection->ssl->fp_tls_greased ? "1" : "0");
69-
v->valid = 1;
70-
v->no_cacheable = 1;
7173
v->not_found = 0;
7274

7375
return NGX_OK;
@@ -77,19 +79,21 @@ static ngx_int_t
7779
ngx_http_ssl_fingerprint(ngx_http_request_t *r,
7880
ngx_http_variable_value_t *v, uintptr_t data)
7981
{
80-
/* For access.log's map $http2_fingerpring {}:
82+
/* For access.log's map $VAR {}:
8183
* if it's not found, then user could add a defined string */
8284
v->not_found = 1;
8385

84-
if (ngx_ssl_ja3(r->connection) != NGX_OK) {
86+
if (r->connection->ssl == NULL) {
8587
return NGX_OK;
8688
}
8789

90+
if (ngx_ssl_ja3(r->connection) != NGX_OK) {
91+
return NGX_ERROR;
92+
}
93+
8894
v->data = r->connection->ssl->fp_ja3_str.data;
8995
v->len = r->connection->ssl->fp_ja3_str.len;
90-
v->no_cacheable = 1;
9196
v->not_found = 0;
92-
v->valid = 1;
9397

9498
return NGX_OK;
9599
}
@@ -98,19 +102,21 @@ static ngx_int_t
98102
ngx_http_ssl_fingerprint_hash(ngx_http_request_t *r,
99103
ngx_http_variable_value_t *v, uintptr_t data)
100104
{
101-
/* For access.log's map $http2_fingerpring {}:
105+
/* For access.log's map $VAR {}:
102106
* if it's not found, then user could add a defined string */
103107
v->not_found = 1;
104108

109+
if (r->connection->ssl == NULL) {
110+
return NGX_OK;
111+
}
112+
105113
if (ngx_ssl_ja3_hash(r->connection) != NGX_OK) {
106114
return NGX_OK;
107115
}
108116

109117
v->data = r->connection->ssl->fp_ja3_hash.data;
110118
v->len = r->connection->ssl->fp_ja3_hash.len;
111-
v->no_cacheable = 1;
112119
v->not_found = 0;
113-
v->valid = 1;
114120

115121
return NGX_OK;
116122
}
@@ -119,7 +125,7 @@ static ngx_int_t
119125
ngx_http_http2_fingerprint(ngx_http_request_t *r,
120126
ngx_http_variable_value_t *v, uintptr_t data)
121127
{
122-
/* For access.log's map $http2_fingerpring {}:
128+
/* For access.log's map $VAR {}:
123129
* if it's not found, then user could add a defined string */
124130
v->not_found = 1;
125131

@@ -130,14 +136,12 @@ ngx_http_http2_fingerprint(ngx_http_request_t *r,
130136
if (ngx_http2_fingerprint(r->connection, r->stream->connection)
131137
!= NGX_OK)
132138
{
133-
return NGX_OK;
139+
return NGX_ERROR;
134140
}
135141

136142
v->data = r->stream->connection->fp_str.data;
137143
v->len = r->stream->connection->fp_str.len;
138-
v->valid = 1;
139144
v->not_found = 0;
140-
v->no_cacheable = 1;
141145

142146
return NGX_OK;
143147
}

0 commit comments

Comments
 (0)