Skip to content

Commit 39425d5

Browse files
committed
#50 - Adds support for OpenSSL 3
1 parent 320466b commit 39425d5

11 files changed

+196
-68
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
lib
2+
nginx
3+
openssl
4+
t/lib

.travis.yml

-51
This file was deleted.

CHANGES

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Changes nginx-ssl-ja3 v0.0.3 14 Set 2024
2+
3+
- Support for OpenSSL 3
4+
15
Changes nginx-ssl-ja3 v0.0.2 3 Jun 2019
26

37
nginx-ssl-ja3: nginx variables for ja3 fingerprint

README.md

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# nginx-ssl-ja3 [![Build Status](https://app.travis-ci.com/fooinha/nginx-ssl-ja3.svg?branch=master)](https://app.travis-ci.com/github/fooinha/nginx-ssl-ja3)
1+
# nginx-ssl-ja3
22

33
nginx module for SSL/TLS ja3 fingerprint.
44

@@ -69,7 +69,7 @@ stream {
6969

7070
### Dependencies
7171

72-
* [OpenSSL](https://github.com/openssl) - 1.1.1 (branch OpenSSL_1_1_1-stable)
72+
* [OpenSSL](https://github.com/openssl) - 3.3.2 (branch openssl-3.3.2)
7373

7474
The master version OpenSSL is required because this module fetches the
7575
extensions types declared at SSL/TLS Client Hello by using the new early
@@ -87,7 +87,7 @@ the patch is already applied. Check the Dockerfile of the dev image.
8787
### Patches
8888

8989
- [nginx - save client hello extensions](patches/nginx.latest.patch)
90-
- [openssl - more tls extensions](patches/openssl.extensions.patch)
90+
- [openssl - more tls extensions](patches/openssl-3.extensions.patch)
9191

9292

9393
### Compilation and installation
@@ -98,21 +98,22 @@ Build as a common nginx module.
9898

9999
# Hack/patch openssl - to include more common extensions
100100

101-
$ patch -p1 < /build/nginx-ssl-ja3/patches/openssl.extensions.patch
101+
$ patch -p1 < /build/nginx-ssl-ja3/patches/openssl-3.extensions.patch
102102

103103
patching file include/openssl/tls1.h
104+
...
104105
patching file ssl/statem/extensions.c
106+
...
105107

106108

107109
# Hack/patch nginx
108110

109111
$ patch -p1 < /build/ngx_ssl_ja3/patches/nginx.latest.patch
110112

111113
patching file src/event/ngx_event_openssl.c
112-
Hunk #1 succeeded at 1358 (offset 137 lines).
113-
Hunk #2 succeeded at 1426 (offset 143 lines).
114+
...
114115
patching file src/event/ngx_event_openssl.h
115-
Hunk #1 succeeded at 99 (offset 1 line).
116+
...
116117

117118
# Configure
118119

@@ -149,10 +150,6 @@ Creating nginx-ssl-ja3
149150

150151
@**fooinha** - author
151152

152-
@**Sessa93**
153-
154-
@**bartebor**
155-
156153
## Fair Warning
157154

158155
**THIS IS NOT PRODUCTION** ready.

docker/debian-nginx-ssl-ja3/Dockerfile

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ RUN git clone https://github.com/nginx/nginx-tests
6161

6262

6363
# Build and install openssl
64-
RUN git clone -v https://github.com/openssl/openssl -b 'OpenSSL_1_1_1-stable'
65-
COPY patches/openssl.extensions.patch /build/openssl
64+
RUN git clone -v https://github.com/openssl/openssl -b 'openssl-3.3.2'
65+
COPY patches/openssl-3.extensions.patch /build/openssl
6666

6767
WORKDIR /build/openssl
68-
RUN patch -p1 < openssl.extensions.patch
68+
RUN patch -p1 < openssl-3.extensions.patch
6969
RUN ./config -d
7070
RUN make
7171
RUN make install

docker/docker-compose.yml

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: '3'
2-
31
services:
42

53
nginx-dev:
+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
diff -r 2e63d59c342d src/event/ngx_event_openssl.c
2+
--- a/src/event/ngx_event_openssl.c Tue Sep 10 16:48:11 2024 +0400
3+
+++ b/src/event/ngx_event_openssl.c Sat Sep 14 18:00:11 2024 +0000
4+
@@ -1742,6 +1742,7 @@
5+
#ifdef SSL_OP_NO_RENEGOTIATION
6+
SSL_set_options(sc->connection, SSL_OP_NO_RENEGOTIATION);
7+
#endif
8+
+ SSL_set_options(sc->connection, SSL_OP_NO_TICKET);
9+
}
10+
11+
if (SSL_set_ex_data(sc->connection, ngx_ssl_connection_index, c) == 0) {
12+
@@ -1793,6 +1794,116 @@
13+
return NGX_OK;
14+
}
15+
16+
+/* ----- JA3 HACK START -----------------------------------------------------*/
17+
+
18+
+void
19+
+ngx_SSL_client_features(ngx_connection_t *c) {
20+
+
21+
+ unsigned short *ciphers_out = NULL;
22+
+ int *curves_out = NULL;
23+
+ int *point_formats_out = NULL;
24+
+ size_t i = 0;
25+
+ size_t len = 0;
26+
+ SSL *s = NULL;
27+
+
28+
+ if (c == NULL) {
29+
+ return;
30+
+ }
31+
+ s = c->ssl->connection;
32+
+
33+
+ /* Cipher suites */
34+
+ c->ssl->ciphers = NULL;
35+
+ c->ssl->ciphers_sz = SSL_get0_raw_cipherlist(s, &ciphers_out);
36+
+ c->ssl->ciphers_sz /= 2;
37+
+
38+
+ if (c->ssl->ciphers_sz && ciphers_out) {
39+
+ len = c->ssl->ciphers_sz * sizeof(unsigned short);
40+
+ c->ssl->ciphers = ngx_pnalloc(c->pool, len);
41+
+ ngx_memcpy(c->ssl->ciphers, ciphers_out, len);
42+
+ }
43+
+
44+
+ /* Elliptic curve points */
45+
+
46+
+ c->ssl->curves_sz = SSL_get1_curves(s, NULL);
47+
+ if (c->ssl->curves_sz) {
48+
+ len = c->ssl->curves_sz * sizeof(int);
49+
+ curves_out = OPENSSL_malloc(len);
50+
+ if (curves_out != NULL) {
51+
+ memset(curves_out, 0, len);
52+
+ SSL_get1_curves(s, curves_out);
53+
+ len = c->ssl->curves_sz * sizeof(unsigned short);
54+
+ c->ssl->curves = ngx_pnalloc(c->pool, len);
55+
+ if (c->ssl->curves != NULL) {
56+
+ for (i = 0; i < c->ssl->curves_sz; i++) {
57+
+ c->ssl->curves[i] = (unsigned short) curves_out[i];
58+
+ }
59+
+ }
60+
+ OPENSSL_free(curves_out);
61+
+ }
62+
+ }
63+
+
64+
+ /* Elliptic curve point formats */
65+
+ c->ssl->point_formats_sz = SSL_get0_ec_point_formats(s, &point_formats_out);
66+
+ if (c->ssl->point_formats_sz && point_formats_out != NULL) {
67+
+ len = c->ssl->point_formats_sz * sizeof(unsigned char);
68+
+ c->ssl->point_formats = ngx_pnalloc(c->pool, len);
69+
+ if (c->ssl->point_formats != NULL) {
70+
+ ngx_memcpy(c->ssl->point_formats, point_formats_out, len);
71+
+ }
72+
+ }
73+
+}
74+
+
75+
+/* should *ALWAYS return 1
76+
+ * # define SSL_CLIENT_HELLO_SUCCESS 1
77+
+ *
78+
+ * otherwise
79+
+ * A failure in the ClientHello callback terminates the connection.
80+
+ */
81+
+int
82+
+ngx_SSL_early_cb_fn(SSL *s, int *al, void *arg) {
83+
+
84+
+ int got_extensions;
85+
+ int *ext_out;
86+
+ size_t ext_len;
87+
+ ngx_connection_t *c;
88+
+
89+
+ c = arg;
90+
+
91+
+ if (c == NULL) {
92+
+ return 1;
93+
+ }
94+
+
95+
+ if (c->ssl == NULL) {
96+
+ return 1;
97+
+ }
98+
+
99+
+ c->ssl->extensions_size = 0;
100+
+ c->ssl->extensions = NULL;
101+
+ got_extensions = SSL_client_hello_get1_extensions_present(
102+
+ s,
103+
+ &ext_out,
104+
+ &ext_len);
105+
+ if (!got_extensions) {
106+
+ return 1;
107+
+ }
108+
+ if (!ext_out) {
109+
+ return 1;
110+
+ }
111+
+ if (!ext_len) {
112+
+ return 1;
113+
+ }
114+
+
115+
+ c->ssl->extensions = ngx_palloc(c->pool, sizeof(int) * ext_len);
116+
+ if (c->ssl->extensions != NULL) {
117+
+ c->ssl->extensions_size = ext_len;
118+
+ ngx_memcpy(c->ssl->extensions, ext_out, sizeof(int) * ext_len);
119+
+ }
120+
+
121+
+ OPENSSL_free(ext_out);
122+
+
123+
+ return 1;
124+
+}
125+
+/* ----- JA3 HACK END -------------------------------------------------------*/
126+
127+
ngx_int_t
128+
ngx_ssl_handshake(ngx_connection_t *c)
129+
@@ -1813,6 +1924,10 @@
130+
131+
ngx_ssl_clear_error(c->log);
132+
133+
+/* ----- JA3 HACK START -----------------------------------------------------*/
134+
+ SSL_CTX_set_client_hello_cb(c->ssl->session_ctx, ngx_SSL_early_cb_fn, c);
135+
+/* ----- JA3 HACK END -------------------------------------------------------*/
136+
+
137+
n = SSL_do_handshake(c->ssl->connection);
138+
139+
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_do_handshake: %d", n);
140+
@@ -1831,6 +1946,10 @@
141+
ngx_ssl_handshake_log(c);
142+
#endif
143+
144+
+/* ----- JA3 HACK START -----------------------------------------------------*/
145+
+ ngx_SSL_client_features(c);
146+
+/* ----- JA3 HACK END -------------------------------------------------------*/
147+
+
148+
c->recv = ngx_ssl_recv;
149+
c->send = ngx_ssl_write;
150+
c->recv_chain = ngx_ssl_recv_chain;
151+
diff -r 2e63d59c342d src/event/ngx_event_openssl.h
152+
--- a/src/event/ngx_event_openssl.h Tue Sep 10 16:48:11 2024 +0400
153+
+++ b/src/event/ngx_event_openssl.h Sat Sep 14 18:00:11 2024 +0000
154+
@@ -128,6 +128,20 @@
155+
unsigned in_ocsp:1;
156+
unsigned early_preread:1;
157+
unsigned write_blocked:1;
158+
+
159+
+/* ----- JA3 HACK START -----------------------------------------------------*/
160+
+ size_t ciphers_sz;
161+
+ unsigned short *ciphers;
162+
+
163+
+ size_t extensions_size;
164+
+ int *extensions;
165+
+
166+
+ size_t curves_sz;
167+
+ unsigned short *curves;
168+
+
169+
+ size_t point_formats_sz;
170+
+ unsigned char *point_formats;
171+
+/* ----- JA3 HACK END -------------------------------------------------------*/
172+
};
173+
174+

patches/nginx.latest.patch

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nginx.1.23.1.ssl.extensions.patch
1+
nginx.1.27.2.ssl.extensions.patch

patches/openssl-3.extensions.patch

Whitespace-only changes.

src/ngx_ssl_ja3.c

+2
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ ngx_ssj_ja3_num_digits(int n)
138138
return c;
139139
}
140140

141+
#ifdef JA3_SORT_EXT
141142
static void
142143
ngx_sort_ext(unsigned short *ext, int size)
143144
{
@@ -154,6 +155,7 @@ ngx_sort_ext(unsigned short *ext, int size)
154155
}
155156
}
156157
}
158+
#endif
157159

158160
#if (NGX_DEBUG)
159161
static void

0 commit comments

Comments
 (0)