Skip to content

feature: ngx_http_lua_ffi_ssl_ciphers #2424

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
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
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
75 changes: 75 additions & 0 deletions src/ngx_http_lua_ssl_certby.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static u_char *ngx_http_lua_log_ssl_cert_error(ngx_log_t *log, u_char *buf,
size_t len);
static ngx_int_t ngx_http_lua_ssl_cert_by_chunk(lua_State *L,
ngx_http_request_t *r);
static int ngx_http_lua_is_grease_cipher(uint16_t cipher_id);


ngx_int_t
Expand Down Expand Up @@ -450,6 +451,16 @@ ngx_http_lua_log_ssl_cert_error(ngx_log_t *log, u_char *buf, size_t len)
}


static int
ngx_http_lua_is_grease_cipher(uint16_t cipher_id)
{
/* GREASE values follow pattern: 0x?A?A where ? can be any hex digit */
/* and both ? must be the same */
/* Check if both bytes follow ?A pattern and high nibbles match */
return (cipher_id & 0x0F0F) == 0x0A0A;
}


static ngx_int_t
ngx_http_lua_ssl_cert_by_chunk(lua_State *L, ngx_http_request_t *r)
{
Expand Down Expand Up @@ -837,6 +848,70 @@ ngx_http_lua_ffi_ssl_raw_server_addr(ngx_http_request_t *r, char **addr,
}


int
ngx_http_lua_ffi_req_shared_ssl_ciphers(ngx_http_request_t *r,
uint16_t *ciphers, uint16_t *nciphers, int filter_grease, char **err)
{
#ifdef OPENSSL_IS_BORINGSSL

*err = "BoringSSL is not supported for SSL cipher operations";
return NGX_ERROR;

#else
ngx_ssl_conn_t *ssl_conn;
STACK_OF(SSL_CIPHER) *sk, *ck;
int sn, cn, i, n;
uint16_t cipher;

if (r == NULL || r->connection == NULL || r->connection->ssl == NULL) {
*err = "bad request";
return NGX_ERROR;
}

ssl_conn = r->connection->ssl->connection;
if (ssl_conn == NULL) {
*err = "bad ssl conn";
return NGX_ERROR;
}

sk = SSL_get1_supported_ciphers(ssl_conn);
ck = SSL_get_client_ciphers(ssl_conn);
sn = sk_SSL_CIPHER_num(sk);
cn = sk_SSL_CIPHER_num(ck);

if (sn > *nciphers) {
*err = "buffer too small";
*nciphers = 0;
sk_SSL_CIPHER_free(sk);
return NGX_ERROR;
}

for (*nciphers = 0, i = 0; i < sn; i++) {
cipher = SSL_CIPHER_get_protocol_id(sk_SSL_CIPHER_value(sk, i));

/* Skip GREASE ciphers if filtering is enabled */
if (filter_grease && ngx_http_lua_is_grease_cipher(cipher)) {
continue;
}

for (n = 0; n < cn; n++) {
if (SSL_CIPHER_get_protocol_id(sk_SSL_CIPHER_value(ck, n))
== cipher)
{
ciphers[(*nciphers)++] = cipher;
break;
}
}
}

sk_SSL_CIPHER_free(sk);

return NGX_OK;
#endif

}


int
ngx_http_lua_ffi_ssl_server_name(ngx_http_request_t *r, char **name,
size_t *namelen, char **err)
Expand Down
Loading