Skip to content

[skip ci] feature: ngx_http_lua_ffi_ssl_get_client_hello_ciphers() #2418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 33 additions & 16 deletions src/ngx_http_lua_ssl_client_helloby.c
Original file line number Diff line number Diff line change
Expand Up @@ -662,15 +662,14 @@ ngx_http_lua_ffi_ssl_get_client_hello_ext(ngx_http_request_t *r,
}


int
ngx_http_lua_ffi_ssl_get_client_hello_ext_present(ngx_http_request_t *r,
int **extensions, size_t *extensions_len, char **err)
int ngx_http_lua_ffi_ssl_get_client_hello_ciphers(ngx_http_request_t *r,
unsigned short **ciphers, size_t *ciphers_cnt, char **err)
{
ngx_ssl_conn_t *ssl_conn;
int got_extensions;
size_t ext_len;
int *ext_out;
/* OPENSSL will allocate memory for us and make the ext_out point to it */
size_t ciphersuites_bytes;
const unsigned char *ciphers_raw;
unsigned short *ciphers_ja3;
ngx_connection_t *c;


if (r->connection == NULL || r->connection->ssl == NULL) {
Expand All @@ -684,21 +683,39 @@ ngx_http_lua_ffi_ssl_get_client_hello_ext_present(ngx_http_request_t *r,
return NGX_ERROR;
}

c = ngx_ssl_get_connection(ssl_conn);
if (c == NULL) {
*err = "couldn't get real ngx_connection_t pointer";
return NGX_ERROR;
}

#ifdef SSL_ERROR_WANT_CLIENT_HELLO_CB
got_extensions = SSL_client_hello_get1_extensions_present(ssl_conn,
&ext_out, &ext_len);
if (!got_extensions || !ext_out || !ext_len) {
*err = "failed SSL_client_hello_get1_extensions_present()";
ciphersuites_bytes = SSL_client_hello_get0_ciphers(ssl_conn, &ciphers_raw);

if (!ciphersuites_bytes) {
*err = "failed SSL_client_hello_get0_ciphers()";
return NGX_DECLINED;
}

if (ciphersuites_bytes % 2 != 0) {
*err = "SSL_client_hello_get0_ciphers() odd ciphersuites_bytes";
return NGX_DECLINED;
}

*extensions = ngx_palloc(r->connection->pool, sizeof(int) * ext_len);
if (*extensions != NULL) {
ngx_memcpy(*extensions, ext_out, sizeof(int) * ext_len);
*extensions_len = ext_len;
*ciphers_cnt = ciphersuites_bytes / 2;

*ciphers = ngx_palloc(c->pool, sizeof(short) * (*ciphers_cnt));
if (*ciphers == NULL) {
*err = "ngx_palloc() failed for the ciphers array";
return NGX_ERROR;
}

OPENSSL_free(ext_out);
for (int i = 0 ; i < *ciphers_cnt ; i++) {
uint16_t cipher = (ciphers_raw[i*2] << 8) | ciphers_raw[i*2 + 1];
(*ciphers)[i] = cipher; /* like ntohs but more portable, supposedly */
}


return NGX_OK;
#else
*err = "OpenSSL too old to support this function";
Expand Down
Loading