comparison src/http/ngx_http_request.c @ 7732:59e1c73fe02b

SSL: ssl_reject_handshake directive (ticket #195). In some cases it might be needed to reject SSL handshake based on SNI server name provided, for example, to make sure an invalid certificate is not returned to clients trying to contact a name-based virtual server without SSL configured. Previously, a "ssl_ciphers aNULL;" was used for this. This workaround, however, is not compatible with TLSv1.3, in particular, when using BoringSSL, where it is not possible to configure TLSv1.3 ciphers at all. With this change, the ssl_reject_handshake directive is introduced, which instructs nginx to reject SSL handshakes with an "unrecognized_name" alert in a particular server block. For example, to reject handshake with names other than example.com, one can use the following configuration: server { listen 443 ssl; ssl_reject_handshake on; } server { listen 443 ssl; server_name example.com; ssl_certificate example.com.crt; ssl_certificate_key example.com.key; } The following configuration can be used to reject all SSL handshakes without SNI server name provided: server { listen 443 ssl; ssl_reject_handshake on; } server { listen 443 ssl; server_name ~^; ssl_certificate example.crt; ssl_certificate_key example.key; } Additionally, the ssl_reject_handshake directive makes configuring certificates for the default server block optional. If no certificates are configured in the default server for a given listening socket, certificates must be defined in all non-default server blocks with the listening socket in question.
author Maxim Dounin <mdounin@mdounin.ru>
date Thu, 22 Oct 2020 18:02:28 +0300
parents eb940fe579cf
children ed17a2a95c8d 71b7453fb11f
comparison
equal deleted inserted replaced
7731:fd0b2226919b 7732:59e1c73fe02b
869 if (c->ssl->handshaked) { 869 if (c->ssl->handshaked) {
870 *ad = SSL_AD_NO_RENEGOTIATION; 870 *ad = SSL_AD_NO_RENEGOTIATION;
871 return SSL_TLSEXT_ERR_ALERT_FATAL; 871 return SSL_TLSEXT_ERR_ALERT_FATAL;
872 } 872 }
873 873
874 hc = c->data;
875
874 servername = SSL_get_servername(ssl_conn, TLSEXT_NAMETYPE_host_name); 876 servername = SSL_get_servername(ssl_conn, TLSEXT_NAMETYPE_host_name);
875 877
876 if (servername == NULL) { 878 if (servername == NULL) {
877 return SSL_TLSEXT_ERR_OK; 879 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
880 "SSL server name: null");
881 goto done;
878 } 882 }
879 883
880 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, 884 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
881 "SSL server name: \"%s\"", servername); 885 "SSL server name: \"%s\"", servername);
882 886
883 host.len = ngx_strlen(servername); 887 host.len = ngx_strlen(servername);
884 888
885 if (host.len == 0) { 889 if (host.len == 0) {
886 return SSL_TLSEXT_ERR_OK; 890 goto done;
887 } 891 }
888 892
889 host.data = (u_char *) servername; 893 host.data = (u_char *) servername;
890 894
891 rc = ngx_http_validate_host(&host, c->pool, 1); 895 rc = ngx_http_validate_host(&host, c->pool, 1);
892 896
893 if (rc == NGX_ERROR) { 897 if (rc == NGX_ERROR) {
894 *ad = SSL_AD_INTERNAL_ERROR; 898 goto error;
895 return SSL_TLSEXT_ERR_ALERT_FATAL;
896 } 899 }
897 900
898 if (rc == NGX_DECLINED) { 901 if (rc == NGX_DECLINED) {
899 return SSL_TLSEXT_ERR_OK; 902 goto done;
900 } 903 }
901
902 hc = c->data;
903 904
904 rc = ngx_http_find_virtual_server(c, hc->addr_conf->virtual_names, &host, 905 rc = ngx_http_find_virtual_server(c, hc->addr_conf->virtual_names, &host,
905 NULL, &cscf); 906 NULL, &cscf);
906 907
907 if (rc == NGX_ERROR) { 908 if (rc == NGX_ERROR) {
908 *ad = SSL_AD_INTERNAL_ERROR; 909 goto error;
909 return SSL_TLSEXT_ERR_ALERT_FATAL;
910 } 910 }
911 911
912 if (rc == NGX_DECLINED) { 912 if (rc == NGX_DECLINED) {
913 return SSL_TLSEXT_ERR_OK; 913 goto done;
914 } 914 }
915 915
916 hc->ssl_servername = ngx_palloc(c->pool, sizeof(ngx_str_t)); 916 hc->ssl_servername = ngx_palloc(c->pool, sizeof(ngx_str_t));
917 if (hc->ssl_servername == NULL) { 917 if (hc->ssl_servername == NULL) {
918 *ad = SSL_AD_INTERNAL_ERROR; 918 goto error;
919 return SSL_TLSEXT_ERR_ALERT_FATAL;
920 } 919 }
921 920
922 *hc->ssl_servername = host; 921 *hc->ssl_servername = host;
923 922
924 hc->conf_ctx = cscf->ctx; 923 hc->conf_ctx = cscf->ctx;
931 930
932 c->ssl->buffer_size = sscf->buffer_size; 931 c->ssl->buffer_size = sscf->buffer_size;
933 932
934 if (sscf->ssl.ctx) { 933 if (sscf->ssl.ctx) {
935 if (SSL_set_SSL_CTX(ssl_conn, sscf->ssl.ctx) == NULL) { 934 if (SSL_set_SSL_CTX(ssl_conn, sscf->ssl.ctx) == NULL) {
936 *ad = SSL_AD_INTERNAL_ERROR; 935 goto error;
937 return SSL_TLSEXT_ERR_ALERT_FATAL;
938 } 936 }
939 937
940 /* 938 /*
941 * SSL_set_SSL_CTX() only changes certs as of 1.0.0d 939 * SSL_set_SSL_CTX() only changes certs as of 1.0.0d
942 * adjust other things we care about 940 * adjust other things we care about
958 #ifdef SSL_OP_NO_RENEGOTIATION 956 #ifdef SSL_OP_NO_RENEGOTIATION
959 SSL_set_options(ssl_conn, SSL_OP_NO_RENEGOTIATION); 957 SSL_set_options(ssl_conn, SSL_OP_NO_RENEGOTIATION);
960 #endif 958 #endif
961 } 959 }
962 960
961 done:
962
963 sscf = ngx_http_get_module_srv_conf(hc->conf_ctx, ngx_http_ssl_module);
964
965 if (sscf->reject_handshake) {
966 c->ssl->handshake_rejected = 1;
967 *ad = SSL_AD_UNRECOGNIZED_NAME;
968 return SSL_TLSEXT_ERR_ALERT_FATAL;
969 }
970
963 return SSL_TLSEXT_ERR_OK; 971 return SSL_TLSEXT_ERR_OK;
972
973 error:
974
975 *ad = SSL_AD_INTERNAL_ERROR;
976 return SSL_TLSEXT_ERR_ALERT_FATAL;
964 } 977 }
965 978
966 #endif 979 #endif
967 980
968 981