[nginx] SSL: added $ssl_alpn_protocol variable.

Vladimir Homutov vl at nginx.com
Wed Oct 20 17:27:30 UTC 2021


details:   https://hg.nginx.org/nginx/rev/eb6c77e6d55d
branches:  
changeset: 7935:eb6c77e6d55d
user:      Vladimir Homutov <vl at nginx.com>
date:      Thu Oct 14 11:46:23 2021 +0300
description:
SSL: added $ssl_alpn_protocol variable.

The variable contains protocol selected by ALPN during handshake and
is empty otherwise.

diffstat:

 src/event/ngx_event_openssl.c          |  30 ++++++++++++++++++++++++++++++
 src/event/ngx_event_openssl.h          |   2 ++
 src/http/modules/ngx_http_ssl_module.c |   3 +++
 src/stream/ngx_stream_ssl_module.c     |   3 +++
 4 files changed, 38 insertions(+), 0 deletions(-)

diffs (78 lines):

diff -r 61abb35bb8cf -r eb6c77e6d55d src/event/ngx_event_openssl.c
--- a/src/event/ngx_event_openssl.c	Fri Oct 15 10:02:15 2021 +0300
+++ b/src/event/ngx_event_openssl.c	Thu Oct 14 11:46:23 2021 +0300
@@ -4699,6 +4699,36 @@ ngx_ssl_get_server_name(ngx_connection_t
 
 
 ngx_int_t
+ngx_ssl_get_alpn_protocol(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
+{
+#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
+
+    unsigned int          len;
+    const unsigned char  *data;
+
+    SSL_get0_alpn_selected(c->ssl->connection, &data, &len);
+
+    if (len > 0) {
+
+        s->data = ngx_pnalloc(pool, len);
+        if (s->data == NULL) {
+            return NGX_ERROR;
+        }
+
+        ngx_memcpy(s->data, data, len);
+        s->len = len;
+
+        return NGX_OK;
+    }
+
+#endif
+
+    s->len = 0;
+    return NGX_OK;
+}
+
+
+ngx_int_t
 ngx_ssl_get_raw_certificate(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
 {
     size_t   len;
diff -r 61abb35bb8cf -r eb6c77e6d55d src/event/ngx_event_openssl.h
--- a/src/event/ngx_event_openssl.h	Fri Oct 15 10:02:15 2021 +0300
+++ b/src/event/ngx_event_openssl.h	Thu Oct 14 11:46:23 2021 +0300
@@ -265,6 +265,8 @@ ngx_int_t ngx_ssl_get_early_data(ngx_con
     ngx_str_t *s);
 ngx_int_t ngx_ssl_get_server_name(ngx_connection_t *c, ngx_pool_t *pool,
     ngx_str_t *s);
+ngx_int_t ngx_ssl_get_alpn_protocol(ngx_connection_t *c, ngx_pool_t *pool,
+    ngx_str_t *s);
 ngx_int_t ngx_ssl_get_raw_certificate(ngx_connection_t *c, ngx_pool_t *pool,
     ngx_str_t *s);
 ngx_int_t ngx_ssl_get_certificate(ngx_connection_t *c, ngx_pool_t *pool,
diff -r 61abb35bb8cf -r eb6c77e6d55d src/http/modules/ngx_http_ssl_module.c
--- a/src/http/modules/ngx_http_ssl_module.c	Fri Oct 15 10:02:15 2021 +0300
+++ b/src/http/modules/ngx_http_ssl_module.c	Thu Oct 14 11:46:23 2021 +0300
@@ -358,6 +358,9 @@ static ngx_http_variable_t  ngx_http_ssl
     { ngx_string("ssl_server_name"), NULL, ngx_http_ssl_variable,
       (uintptr_t) ngx_ssl_get_server_name, NGX_HTTP_VAR_CHANGEABLE, 0 },
 
+    { ngx_string("ssl_alpn_protocol"), NULL, ngx_http_ssl_variable,
+      (uintptr_t) ngx_ssl_get_alpn_protocol, NGX_HTTP_VAR_CHANGEABLE, 0 },
+
     { ngx_string("ssl_client_cert"), NULL, ngx_http_ssl_variable,
       (uintptr_t) ngx_ssl_get_certificate, NGX_HTTP_VAR_CHANGEABLE, 0 },
 
diff -r 61abb35bb8cf -r eb6c77e6d55d src/stream/ngx_stream_ssl_module.c
--- a/src/stream/ngx_stream_ssl_module.c	Fri Oct 15 10:02:15 2021 +0300
+++ b/src/stream/ngx_stream_ssl_module.c	Thu Oct 14 11:46:23 2021 +0300
@@ -266,6 +266,9 @@ static ngx_stream_variable_t  ngx_stream
     { ngx_string("ssl_server_name"), NULL, ngx_stream_ssl_variable,
       (uintptr_t) ngx_ssl_get_server_name, NGX_STREAM_VAR_CHANGEABLE, 0 },
 
+    { ngx_string("ssl_alpn_protocol"), NULL, ngx_stream_ssl_variable,
+      (uintptr_t) ngx_ssl_get_alpn_protocol, NGX_STREAM_VAR_CHANGEABLE, 0 },
+
     { ngx_string("ssl_client_cert"), NULL, ngx_stream_ssl_variable,
       (uintptr_t) ngx_ssl_get_certificate, NGX_STREAM_VAR_CHANGEABLE, 0 },
 


More information about the nginx-devel mailing list