Skip to content

Commit

Permalink
SSL: $ssl_curves (ticket #1088).
Browse files Browse the repository at this point in the history
The variable contains a list of curves as supported by the client.
Known curves are listed by their names, unknown ones are shown
in hex, e.g., "0x001d:prime256v1:secp521r1:secp384r1".

Note that OpenSSL uses session data for SSL_get1_curves(), and
it doesn't store full list of curves supported by the client when
serializing a session.  As a result $ssl_curves is only available
for new sessions (and will be empty for reused ones).

The variable is only meaningful when using OpenSSL 1.0.2 and above.
With older versions the variable is empty.
  • Loading branch information
mdounin committed Dec 5, 2016
1 parent 2daf788 commit 5510919
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/event/ngx_event_openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3377,6 +3377,74 @@ ngx_ssl_get_ciphers(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
}


ngx_int_t
ngx_ssl_get_curves(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
{
#ifdef SSL_CTRL_GET_CURVES

int *curves, n, i, nid;
u_char *p;
size_t len;

n = SSL_get1_curves(c->ssl->connection, NULL);

if (n <= 0) {
s->len = 0;
return NGX_OK;
}

curves = ngx_palloc(pool, n * sizeof(int));

n = SSL_get1_curves(c->ssl->connection, curves);
len = 0;

for (i = 0; i < n; i++) {
nid = curves[i];

if (nid & TLSEXT_nid_unknown) {
len += sizeof("0x0000") - 1;

} else {
len += ngx_strlen(OBJ_nid2sn(nid));
}

len += sizeof(":") - 1;
}

s->data = ngx_pnalloc(pool, len);
if (s->data == NULL) {
return NGX_ERROR;
}

p = s->data;

for (i = 0; i < n; i++) {
nid = curves[i];

if (nid & TLSEXT_nid_unknown) {
p = ngx_sprintf(p, "0x%04xd", nid & 0xffff);

} else {
p = ngx_sprintf(p, "%s", OBJ_nid2sn(nid));
}

*p++ = ':';
}

p--;

s->len = p - s->data;

#else

s->len = 0;

#endif

return NGX_OK;
}


ngx_int_t
ngx_ssl_get_session_id(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
{
Expand Down
2 changes: 2 additions & 0 deletions src/event/ngx_event_openssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ ngx_int_t ngx_ssl_get_cipher_name(ngx_connection_t *c, ngx_pool_t *pool,
ngx_str_t *s);
ngx_int_t ngx_ssl_get_ciphers(ngx_connection_t *c, ngx_pool_t *pool,
ngx_str_t *s);
ngx_int_t ngx_ssl_get_curves(ngx_connection_t *c, ngx_pool_t *pool,
ngx_str_t *s);
ngx_int_t ngx_ssl_get_session_id(ngx_connection_t *c, ngx_pool_t *pool,
ngx_str_t *s);
ngx_int_t ngx_ssl_get_session_reused(ngx_connection_t *c, ngx_pool_t *pool,
Expand Down
3 changes: 3 additions & 0 deletions src/http/modules/ngx_http_ssl_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ static ngx_http_variable_t ngx_http_ssl_vars[] = {
{ ngx_string("ssl_ciphers"), NULL, ngx_http_ssl_variable,
(uintptr_t) ngx_ssl_get_ciphers, NGX_HTTP_VAR_CHANGEABLE, 0 },

{ ngx_string("ssl_curves"), NULL, ngx_http_ssl_variable,
(uintptr_t) ngx_ssl_get_curves, NGX_HTTP_VAR_CHANGEABLE, 0 },

{ ngx_string("ssl_session_id"), NULL, ngx_http_ssl_variable,
(uintptr_t) ngx_ssl_get_session_id, NGX_HTTP_VAR_CHANGEABLE, 0 },

Expand Down
3 changes: 3 additions & 0 deletions src/stream/ngx_stream_ssl_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ static ngx_stream_variable_t ngx_stream_ssl_vars[] = {
{ ngx_string("ssl_ciphers"), NULL, ngx_stream_ssl_variable,
(uintptr_t) ngx_ssl_get_ciphers, NGX_STREAM_VAR_CHANGEABLE, 0 },

{ ngx_string("ssl_curves"), NULL, ngx_stream_ssl_variable,
(uintptr_t) ngx_ssl_get_curves, NGX_STREAM_VAR_CHANGEABLE, 0 },

{ ngx_string("ssl_session_id"), NULL, ngx_stream_ssl_variable,
(uintptr_t) ngx_ssl_get_session_id, NGX_STREAM_VAR_CHANGEABLE, 0 },

Expand Down

0 comments on commit 5510919

Please sign in to comment.