Skip to content

Commit baa4f2f

Browse files
committed
ngx_http_lua_ffi_ssl_get_client_hello_ciphers()
Partially inspired by: https://github.com/naofumi0628/haproxy/blob/fefb9e37714bd2e3ad2adc3a321e165fc1dafae2/src/ssl_sock.c#L2252 Relevant: fooinha/nginx-ssl-ja3#64 openssl/openssl#27580 And especially: https://github.com/openresty/lua-nginx-module#:~:text=after%20SSL%20handshake%2C-,the%20ngx.ctx%20created,-in%20ssl_certificate_by_lua* It might be pointless for me to pull all this data into Lua-land if I don't find a way to store those values. I need some kind of ngx.ctx but related not a request but to a ngx_ssl_connection_t, instead of a request.
1 parent edd1b6a commit baa4f2f

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/ngx_http_lua_ssl_client_helloby.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,60 @@ ngx_http_lua_ffi_ssl_get_client_hello_ext_present(ngx_http_request_t *r,
707707
}
708708

709709

710+
int ngx_http_lua_ffi_ssl_get_client_hello_ciphers(ngx_http_request_t *r,
711+
int **ciphers, size_t *cipherslen, char **err)
712+
{
713+
ngx_ssl_conn_t *ssl_conn;
714+
size_t ciphersuites_length;
715+
const unsigned char *ciphers_raw;
716+
717+
718+
if (r->connection == NULL || r->connection->ssl == NULL) {
719+
*err = "bad request";
720+
return NGX_ERROR;
721+
}
722+
723+
ssl_conn = r->connection->ssl->connection;
724+
if (ssl_conn == NULL) {
725+
*err = "bad ssl conn";
726+
return NGX_ERROR;
727+
}
728+
729+
#ifdef SSL_ERROR_WANT_CLIENT_HELLO_CB
730+
ciphersuites_length = SSL_client_hello_get0_ciphers(ssl_conn, &ciphers_raw);
731+
732+
if (!ciphersuites_length) {
733+
*err = "failed SSL_client_hello_get0_ciphers()";
734+
return NGX_DECLINED;
735+
}
736+
737+
if (ciphersuites_length %2 != 0) {
738+
*err = "SSL_client_hello_get0_ciphers() odd ciphersuites_length";
739+
return NGX_DECLINED;
740+
}
741+
742+
*cipherslen = ciphersuites_length / 2;
743+
744+
*ciphers = ngx_palloc(r->connection->pool, sizeof(int) * (*cipherslen));
745+
if (*ciphers == NULL) {
746+
*err = "failed to ngx_palloc() for the ciphers' array";
747+
return NGX_ERROR;
748+
}
749+
750+
for (int i = 0 ; i < *cipherslen ; i++) {
751+
uint16_t cipher = (ciphers_raw[i*2] << 8) | ciphers_raw[i*2 + 1];
752+
753+
(*ciphers)[i] = cipher;
754+
}
755+
756+
return NGX_OK;
757+
#else
758+
*err = "OpenSSL too old to support this function";
759+
return NGX_ERROR;
760+
#endif
761+
}
762+
763+
710764
int
711765
ngx_http_lua_ffi_ssl_set_protocols(ngx_http_request_t *r,
712766
int protocols, char **err)

0 commit comments

Comments
 (0)