src/http/modules/ngx_http_ssl_module.c - nginx source code

Global variables defined

Data types defined

Functions defined

Macros defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_http.h>

  8. #if (NGX_QUIC_OPENSSL_COMPAT)
  9. #include <ngx_event_quic_openssl_compat.h>
  10. #endif


  11. typedef ngx_int_t (*ngx_ssl_variable_handler_pt)(ngx_connection_t *c,
  12.     ngx_pool_t *pool, ngx_str_t *s);


  13. #define NGX_DEFAULT_CIPHERS     "HIGH:!aNULL:!MD5"
  14. #define NGX_DEFAULT_ECDH_CURVE  "auto"

  15. #define NGX_HTTP_ALPN_PROTOS    "\x08http/1.1\x08http/1.0\x08http/0.9"


  16. #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
  17. static int ngx_http_ssl_alpn_select(ngx_ssl_conn_t *ssl_conn,
  18.     const unsigned char **out, unsigned char *outlen,
  19.     const unsigned char *in, unsigned int inlen, void *arg);
  20. #endif

  21. static ngx_int_t ngx_http_ssl_static_variable(ngx_http_request_t *r,
  22.     ngx_http_variable_value_t *v, uintptr_t data);
  23. static ngx_int_t ngx_http_ssl_variable(ngx_http_request_t *r,
  24.     ngx_http_variable_value_t *v, uintptr_t data);

  25. static ngx_int_t ngx_http_ssl_add_variables(ngx_conf_t *cf);
  26. static void *ngx_http_ssl_create_srv_conf(ngx_conf_t *cf);
  27. static char *ngx_http_ssl_merge_srv_conf(ngx_conf_t *cf,
  28.     void *parent, void *child);

  29. static ngx_int_t ngx_http_ssl_compile_certificates(ngx_conf_t *cf,
  30.     ngx_http_ssl_srv_conf_t *conf);

  31. static char *ngx_http_ssl_password_file(ngx_conf_t *cf, ngx_command_t *cmd,
  32.     void *conf);
  33. static char *ngx_http_ssl_session_cache(ngx_conf_t *cf, ngx_command_t *cmd,
  34.     void *conf);
  35. static char *ngx_http_ssl_ocsp_cache(ngx_conf_t *cf, ngx_command_t *cmd,
  36.     void *conf);

  37. static char *ngx_http_ssl_conf_command_check(ngx_conf_t *cf, void *post,
  38.     void *data);

  39. static ngx_int_t ngx_http_ssl_init(ngx_conf_t *cf);
  40. #if (NGX_QUIC_OPENSSL_COMPAT)
  41. static ngx_int_t ngx_http_ssl_quic_compat_init(ngx_conf_t *cf,
  42.     ngx_http_conf_addr_t *addr);
  43. #endif


  44. static ngx_conf_bitmask_t  ngx_http_ssl_protocols[] = {
  45.     { ngx_string("SSLv2"), NGX_SSL_SSLv2 },
  46.     { ngx_string("SSLv3"), NGX_SSL_SSLv3 },
  47.     { ngx_string("TLSv1"), NGX_SSL_TLSv1 },
  48.     { ngx_string("TLSv1.1"), NGX_SSL_TLSv1_1 },
  49.     { ngx_string("TLSv1.2"), NGX_SSL_TLSv1_2 },
  50.     { ngx_string("TLSv1.3"), NGX_SSL_TLSv1_3 },
  51.     { ngx_null_string, 0 }
  52. };


  53. static ngx_conf_enum_t  ngx_http_ssl_verify[] = {
  54.     { ngx_string("off"), 0 },
  55.     { ngx_string("on"), 1 },
  56.     { ngx_string("optional"), 2 },
  57.     { ngx_string("optional_no_ca"), 3 },
  58.     { ngx_null_string, 0 }
  59. };


  60. static ngx_conf_enum_t  ngx_http_ssl_ocsp[] = {
  61.     { ngx_string("off"), 0 },
  62.     { ngx_string("on"), 1 },
  63.     { ngx_string("leaf"), 2 },
  64.     { ngx_null_string, 0 }
  65. };


  66. static ngx_conf_post_t  ngx_http_ssl_conf_command_post =
  67.     { ngx_http_ssl_conf_command_check };


  68. static ngx_command_t  ngx_http_ssl_commands[] = {

  69.     { ngx_string("ssl_certificate"),
  70.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  71.       ngx_conf_set_str_array_slot,
  72.       NGX_HTTP_SRV_CONF_OFFSET,
  73.       offsetof(ngx_http_ssl_srv_conf_t, certificates),
  74.       NULL },

  75.     { ngx_string("ssl_certificate_key"),
  76.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  77.       ngx_conf_set_str_array_slot,
  78.       NGX_HTTP_SRV_CONF_OFFSET,
  79.       offsetof(ngx_http_ssl_srv_conf_t, certificate_keys),
  80.       NULL },

  81.     { ngx_string("ssl_password_file"),
  82.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  83.       ngx_http_ssl_password_file,
  84.       NGX_HTTP_SRV_CONF_OFFSET,
  85.       0,
  86.       NULL },

  87.     { ngx_string("ssl_dhparam"),
  88.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  89.       ngx_conf_set_str_slot,
  90.       NGX_HTTP_SRV_CONF_OFFSET,
  91.       offsetof(ngx_http_ssl_srv_conf_t, dhparam),
  92.       NULL },

  93.     { ngx_string("ssl_ecdh_curve"),
  94.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  95.       ngx_conf_set_str_slot,
  96.       NGX_HTTP_SRV_CONF_OFFSET,
  97.       offsetof(ngx_http_ssl_srv_conf_t, ecdh_curve),
  98.       NULL },

  99.     { ngx_string("ssl_protocols"),
  100.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_1MORE,
  101.       ngx_conf_set_bitmask_slot,
  102.       NGX_HTTP_SRV_CONF_OFFSET,
  103.       offsetof(ngx_http_ssl_srv_conf_t, protocols),
  104.       &ngx_http_ssl_protocols },

  105.     { ngx_string("ssl_ciphers"),
  106.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  107.       ngx_conf_set_str_slot,
  108.       NGX_HTTP_SRV_CONF_OFFSET,
  109.       offsetof(ngx_http_ssl_srv_conf_t, ciphers),
  110.       NULL },

  111.     { ngx_string("ssl_buffer_size"),
  112.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  113.       ngx_conf_set_size_slot,
  114.       NGX_HTTP_SRV_CONF_OFFSET,
  115.       offsetof(ngx_http_ssl_srv_conf_t, buffer_size),
  116.       NULL },

  117.     { ngx_string("ssl_verify_client"),
  118.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  119.       ngx_conf_set_enum_slot,
  120.       NGX_HTTP_SRV_CONF_OFFSET,
  121.       offsetof(ngx_http_ssl_srv_conf_t, verify),
  122.       &ngx_http_ssl_verify },

  123.     { ngx_string("ssl_verify_depth"),
  124.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  125.       ngx_conf_set_num_slot,
  126.       NGX_HTTP_SRV_CONF_OFFSET,
  127.       offsetof(ngx_http_ssl_srv_conf_t, verify_depth),
  128.       NULL },

  129.     { ngx_string("ssl_client_certificate"),
  130.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  131.       ngx_conf_set_str_slot,
  132.       NGX_HTTP_SRV_CONF_OFFSET,
  133.       offsetof(ngx_http_ssl_srv_conf_t, client_certificate),
  134.       NULL },

  135.     { ngx_string("ssl_trusted_certificate"),
  136.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  137.       ngx_conf_set_str_slot,
  138.       NGX_HTTP_SRV_CONF_OFFSET,
  139.       offsetof(ngx_http_ssl_srv_conf_t, trusted_certificate),
  140.       NULL },

  141.     { ngx_string("ssl_prefer_server_ciphers"),
  142.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
  143.       ngx_conf_set_flag_slot,
  144.       NGX_HTTP_SRV_CONF_OFFSET,
  145.       offsetof(ngx_http_ssl_srv_conf_t, prefer_server_ciphers),
  146.       NULL },

  147.     { ngx_string("ssl_session_cache"),
  148.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE12,
  149.       ngx_http_ssl_session_cache,
  150.       NGX_HTTP_SRV_CONF_OFFSET,
  151.       0,
  152.       NULL },

  153.     { ngx_string("ssl_session_tickets"),
  154.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
  155.       ngx_conf_set_flag_slot,
  156.       NGX_HTTP_SRV_CONF_OFFSET,
  157.       offsetof(ngx_http_ssl_srv_conf_t, session_tickets),
  158.       NULL },

  159.     { ngx_string("ssl_session_ticket_key"),
  160.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  161.       ngx_conf_set_str_array_slot,
  162.       NGX_HTTP_SRV_CONF_OFFSET,
  163.       offsetof(ngx_http_ssl_srv_conf_t, session_ticket_keys),
  164.       NULL },

  165.     { ngx_string("ssl_session_timeout"),
  166.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  167.       ngx_conf_set_sec_slot,
  168.       NGX_HTTP_SRV_CONF_OFFSET,
  169.       offsetof(ngx_http_ssl_srv_conf_t, session_timeout),
  170.       NULL },

  171.     { ngx_string("ssl_crl"),
  172.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  173.       ngx_conf_set_str_slot,
  174.       NGX_HTTP_SRV_CONF_OFFSET,
  175.       offsetof(ngx_http_ssl_srv_conf_t, crl),
  176.       NULL },

  177.     { ngx_string("ssl_ocsp"),
  178.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
  179.       ngx_conf_set_enum_slot,
  180.       NGX_HTTP_SRV_CONF_OFFSET,
  181.       offsetof(ngx_http_ssl_srv_conf_t, ocsp),
  182.       &ngx_http_ssl_ocsp },

  183.     { ngx_string("ssl_ocsp_responder"),
  184.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  185.       ngx_conf_set_str_slot,
  186.       NGX_HTTP_SRV_CONF_OFFSET,
  187.       offsetof(ngx_http_ssl_srv_conf_t, ocsp_responder),
  188.       NULL },

  189.     { ngx_string("ssl_ocsp_cache"),
  190.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  191.       ngx_http_ssl_ocsp_cache,
  192.       NGX_HTTP_SRV_CONF_OFFSET,
  193.       0,
  194.       NULL },

  195.     { ngx_string("ssl_stapling"),
  196.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
  197.       ngx_conf_set_flag_slot,
  198.       NGX_HTTP_SRV_CONF_OFFSET,
  199.       offsetof(ngx_http_ssl_srv_conf_t, stapling),
  200.       NULL },

  201.     { ngx_string("ssl_stapling_file"),
  202.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  203.       ngx_conf_set_str_slot,
  204.       NGX_HTTP_SRV_CONF_OFFSET,
  205.       offsetof(ngx_http_ssl_srv_conf_t, stapling_file),
  206.       NULL },

  207.     { ngx_string("ssl_stapling_responder"),
  208.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  209.       ngx_conf_set_str_slot,
  210.       NGX_HTTP_SRV_CONF_OFFSET,
  211.       offsetof(ngx_http_ssl_srv_conf_t, stapling_responder),
  212.       NULL },

  213.     { ngx_string("ssl_stapling_verify"),
  214.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
  215.       ngx_conf_set_flag_slot,
  216.       NGX_HTTP_SRV_CONF_OFFSET,
  217.       offsetof(ngx_http_ssl_srv_conf_t, stapling_verify),
  218.       NULL },

  219.     { ngx_string("ssl_early_data"),
  220.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
  221.       ngx_conf_set_flag_slot,
  222.       NGX_HTTP_SRV_CONF_OFFSET,
  223.       offsetof(ngx_http_ssl_srv_conf_t, early_data),
  224.       NULL },

  225.     { ngx_string("ssl_conf_command"),
  226.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE2,
  227.       ngx_conf_set_keyval_slot,
  228.       NGX_HTTP_SRV_CONF_OFFSET,
  229.       offsetof(ngx_http_ssl_srv_conf_t, conf_commands),
  230.       &ngx_http_ssl_conf_command_post },

  231.     { ngx_string("ssl_reject_handshake"),
  232.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
  233.       ngx_conf_set_flag_slot,
  234.       NGX_HTTP_SRV_CONF_OFFSET,
  235.       offsetof(ngx_http_ssl_srv_conf_t, reject_handshake),
  236.       NULL },

  237.       ngx_null_command
  238. };


  239. static ngx_http_module_t  ngx_http_ssl_module_ctx = {
  240.     ngx_http_ssl_add_variables,            /* preconfiguration */
  241.     ngx_http_ssl_init,                     /* postconfiguration */

  242.     NULL,                                  /* create main configuration */
  243.     NULL,                                  /* init main configuration */

  244.     ngx_http_ssl_create_srv_conf,          /* create server configuration */
  245.     ngx_http_ssl_merge_srv_conf,           /* merge server configuration */

  246.     NULL,                                  /* create location configuration */
  247.     NULL                                   /* merge location configuration */
  248. };


  249. ngx_module_t  ngx_http_ssl_module = {
  250.     NGX_MODULE_V1,
  251.     &ngx_http_ssl_module_ctx,              /* module context */
  252.     ngx_http_ssl_commands,                 /* module directives */
  253.     NGX_HTTP_MODULE,                       /* module type */
  254.     NULL,                                  /* init master */
  255.     NULL,                                  /* init module */
  256.     NULL,                                  /* init process */
  257.     NULL,                                  /* init thread */
  258.     NULL,                                  /* exit thread */
  259.     NULL,                                  /* exit process */
  260.     NULL,                                  /* exit master */
  261.     NGX_MODULE_V1_PADDING
  262. };


  263. static ngx_http_variable_t  ngx_http_ssl_vars[] = {

  264.     { ngx_string("ssl_protocol"), NULL, ngx_http_ssl_static_variable,
  265.       (uintptr_t) ngx_ssl_get_protocol, NGX_HTTP_VAR_CHANGEABLE, 0 },

  266.     { ngx_string("ssl_cipher"), NULL, ngx_http_ssl_static_variable,
  267.       (uintptr_t) ngx_ssl_get_cipher_name, NGX_HTTP_VAR_CHANGEABLE, 0 },

  268.     { ngx_string("ssl_ciphers"), NULL, ngx_http_ssl_variable,
  269.       (uintptr_t) ngx_ssl_get_ciphers, NGX_HTTP_VAR_CHANGEABLE, 0 },

  270.     { ngx_string("ssl_curve"), NULL, ngx_http_ssl_variable,
  271.       (uintptr_t) ngx_ssl_get_curve, NGX_HTTP_VAR_CHANGEABLE, 0 },

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

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

  276.     { ngx_string("ssl_session_reused"), NULL, ngx_http_ssl_variable,
  277.       (uintptr_t) ngx_ssl_get_session_reused, NGX_HTTP_VAR_CHANGEABLE, 0 },

  278.     { ngx_string("ssl_early_data"), NULL, ngx_http_ssl_variable,
  279.       (uintptr_t) ngx_ssl_get_early_data,
  280.       NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE, 0 },

  281.     { ngx_string("ssl_server_name"), NULL, ngx_http_ssl_variable,
  282.       (uintptr_t) ngx_ssl_get_server_name, NGX_HTTP_VAR_CHANGEABLE, 0 },

  283.     { ngx_string("ssl_alpn_protocol"), NULL, ngx_http_ssl_variable,
  284.       (uintptr_t) ngx_ssl_get_alpn_protocol, NGX_HTTP_VAR_CHANGEABLE, 0 },

  285.     { ngx_string("ssl_client_cert"), NULL, ngx_http_ssl_variable,
  286.       (uintptr_t) ngx_ssl_get_certificate, NGX_HTTP_VAR_CHANGEABLE, 0 },

  287.     { ngx_string("ssl_client_raw_cert"), NULL, ngx_http_ssl_variable,
  288.       (uintptr_t) ngx_ssl_get_raw_certificate,
  289.       NGX_HTTP_VAR_CHANGEABLE, 0 },

  290.     { ngx_string("ssl_client_escaped_cert"), NULL, ngx_http_ssl_variable,
  291.       (uintptr_t) ngx_ssl_get_escaped_certificate,
  292.       NGX_HTTP_VAR_CHANGEABLE, 0 },

  293.     { ngx_string("ssl_client_s_dn"), NULL, ngx_http_ssl_variable,
  294.       (uintptr_t) ngx_ssl_get_subject_dn, NGX_HTTP_VAR_CHANGEABLE, 0 },

  295.     { ngx_string("ssl_client_i_dn"), NULL, ngx_http_ssl_variable,
  296.       (uintptr_t) ngx_ssl_get_issuer_dn, NGX_HTTP_VAR_CHANGEABLE, 0 },

  297.     { ngx_string("ssl_client_s_dn_legacy"), NULL, ngx_http_ssl_variable,
  298.       (uintptr_t) ngx_ssl_get_subject_dn_legacy, NGX_HTTP_VAR_CHANGEABLE, 0 },

  299.     { ngx_string("ssl_client_i_dn_legacy"), NULL, ngx_http_ssl_variable,
  300.       (uintptr_t) ngx_ssl_get_issuer_dn_legacy, NGX_HTTP_VAR_CHANGEABLE, 0 },

  301.     { ngx_string("ssl_client_serial"), NULL, ngx_http_ssl_variable,
  302.       (uintptr_t) ngx_ssl_get_serial_number, NGX_HTTP_VAR_CHANGEABLE, 0 },

  303.     { ngx_string("ssl_client_fingerprint"), NULL, ngx_http_ssl_variable,
  304.       (uintptr_t) ngx_ssl_get_fingerprint, NGX_HTTP_VAR_CHANGEABLE, 0 },

  305.     { ngx_string("ssl_client_verify"), NULL, ngx_http_ssl_variable,
  306.       (uintptr_t) ngx_ssl_get_client_verify, NGX_HTTP_VAR_CHANGEABLE, 0 },

  307.     { ngx_string("ssl_client_v_start"), NULL, ngx_http_ssl_variable,
  308.       (uintptr_t) ngx_ssl_get_client_v_start, NGX_HTTP_VAR_CHANGEABLE, 0 },

  309.     { ngx_string("ssl_client_v_end"), NULL, ngx_http_ssl_variable,
  310.       (uintptr_t) ngx_ssl_get_client_v_end, NGX_HTTP_VAR_CHANGEABLE, 0 },

  311.     { ngx_string("ssl_client_v_remain"), NULL, ngx_http_ssl_variable,
  312.       (uintptr_t) ngx_ssl_get_client_v_remain, NGX_HTTP_VAR_CHANGEABLE, 0 },

  313.       ngx_http_null_variable
  314. };


  315. static ngx_str_t ngx_http_ssl_sess_id_ctx = ngx_string("HTTP");


  316. #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation

  317. static int
  318. ngx_http_ssl_alpn_select(ngx_ssl_conn_t *ssl_conn, const unsigned char **out,
  319.     unsigned char *outlen, const unsigned char *in, unsigned int inlen,
  320.     void *arg)
  321. {
  322.     unsigned int             srvlen;
  323.     unsigned char           *srv;
  324. #if (NGX_DEBUG)
  325.     unsigned int             i;
  326. #endif
  327. #if (NGX_HTTP_V2 || NGX_HTTP_V3)
  328.     ngx_http_connection_t   *hc;
  329. #endif
  330. #if (NGX_HTTP_V2)
  331.     ngx_http_v2_srv_conf_t  *h2scf;
  332. #endif
  333. #if (NGX_HTTP_V3)
  334.     ngx_http_v3_srv_conf_t  *h3scf;
  335. #endif
  336. #if (NGX_HTTP_V2 || NGX_HTTP_V3 || NGX_DEBUG)
  337.     ngx_connection_t        *c;

  338.     c = ngx_ssl_get_connection(ssl_conn);
  339. #endif

  340. #if (NGX_DEBUG)
  341.     for (i = 0; i < inlen; i += in[i] + 1) {
  342.         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
  343.                        "SSL ALPN supported by client: %*s",
  344.                        (size_t) in[i], &in[i + 1]);
  345.     }
  346. #endif

  347. #if (NGX_HTTP_V2 || NGX_HTTP_V3)
  348.     hc = c->data;
  349. #endif

  350. #if (NGX_HTTP_V3)
  351.     if (hc->addr_conf->quic) {

  352.         h3scf = ngx_http_get_module_srv_conf(hc->conf_ctx, ngx_http_v3_module);

  353.         if (h3scf->enable && h3scf->enable_hq) {
  354.             srv = (unsigned char *) NGX_HTTP_V3_ALPN_PROTO
  355.                                     NGX_HTTP_V3_HQ_ALPN_PROTO;
  356.             srvlen = sizeof(NGX_HTTP_V3_ALPN_PROTO NGX_HTTP_V3_HQ_ALPN_PROTO)
  357.                      - 1;

  358.         } else if (h3scf->enable_hq) {
  359.             srv = (unsigned char *) NGX_HTTP_V3_HQ_ALPN_PROTO;
  360.             srvlen = sizeof(NGX_HTTP_V3_HQ_ALPN_PROTO) - 1;

  361.         } else if (h3scf->enable) {
  362.             srv = (unsigned char *) NGX_HTTP_V3_ALPN_PROTO;
  363.             srvlen = sizeof(NGX_HTTP_V3_ALPN_PROTO) - 1;

  364.         } else {
  365.             return SSL_TLSEXT_ERR_ALERT_FATAL;
  366.         }

  367.     } else
  368. #endif
  369.     {
  370. #if (NGX_HTTP_V2)
  371.         h2scf = ngx_http_get_module_srv_conf(hc->conf_ctx, ngx_http_v2_module);

  372.         if (h2scf->enable || hc->addr_conf->http2) {
  373.             srv = (unsigned char *) NGX_HTTP_V2_ALPN_PROTO NGX_HTTP_ALPN_PROTOS;
  374.             srvlen = sizeof(NGX_HTTP_V2_ALPN_PROTO NGX_HTTP_ALPN_PROTOS) - 1;

  375.         } else
  376. #endif
  377.         {
  378.             srv = (unsigned char *) NGX_HTTP_ALPN_PROTOS;
  379.             srvlen = sizeof(NGX_HTTP_ALPN_PROTOS) - 1;
  380.         }
  381.     }

  382.     if (SSL_select_next_proto((unsigned char **) out, outlen, srv, srvlen,
  383.                               in, inlen)
  384.         != OPENSSL_NPN_NEGOTIATED)
  385.     {
  386.         return SSL_TLSEXT_ERR_ALERT_FATAL;
  387.     }

  388.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
  389.                    "SSL ALPN selected: %*s", (size_t) *outlen, *out);

  390.     return SSL_TLSEXT_ERR_OK;
  391. }

  392. #endif


  393. static ngx_int_t
  394. ngx_http_ssl_static_variable(ngx_http_request_t *r,
  395.     ngx_http_variable_value_t *v, uintptr_t data)
  396. {
  397.     ngx_ssl_variable_handler_pt  handler = (ngx_ssl_variable_handler_pt) data;

  398.     size_t     len;
  399.     ngx_str_t  s;

  400.     if (r->connection->ssl) {

  401.         (void) handler(r->connection, NULL, &s);

  402.         v->data = s.data;

  403.         for (len = 0; v->data[len]; len++) { /* void */ }

  404.         v->len = len;
  405.         v->valid = 1;
  406.         v->no_cacheable = 0;
  407.         v->not_found = 0;

  408.         return NGX_OK;
  409.     }

  410.     v->not_found = 1;

  411.     return NGX_OK;
  412. }


  413. static ngx_int_t
  414. ngx_http_ssl_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
  415.     uintptr_t data)
  416. {
  417.     ngx_ssl_variable_handler_pt  handler = (ngx_ssl_variable_handler_pt) data;

  418.     ngx_str_t  s;

  419.     if (r->connection->ssl) {

  420.         if (handler(r->connection, r->pool, &s) != NGX_OK) {
  421.             return NGX_ERROR;
  422.         }

  423.         v->len = s.len;
  424.         v->data = s.data;

  425.         if (v->len) {
  426.             v->valid = 1;
  427.             v->no_cacheable = 0;
  428.             v->not_found = 0;

  429.             return NGX_OK;
  430.         }
  431.     }

  432.     v->not_found = 1;

  433.     return NGX_OK;
  434. }


  435. static ngx_int_t
  436. ngx_http_ssl_add_variables(ngx_conf_t *cf)
  437. {
  438.     ngx_http_variable_t  *var, *v;

  439.     for (v = ngx_http_ssl_vars; v->name.len; v++) {
  440.         var = ngx_http_add_variable(cf, &v->name, v->flags);
  441.         if (var == NULL) {
  442.             return NGX_ERROR;
  443.         }

  444.         var->get_handler = v->get_handler;
  445.         var->data = v->data;
  446.     }

  447.     return NGX_OK;
  448. }


  449. static void *
  450. ngx_http_ssl_create_srv_conf(ngx_conf_t *cf)
  451. {
  452.     ngx_http_ssl_srv_conf_t  *sscf;

  453.     sscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ssl_srv_conf_t));
  454.     if (sscf == NULL) {
  455.         return NULL;
  456.     }

  457.     /*
  458.      * set by ngx_pcalloc():
  459.      *
  460.      *     sscf->protocols = 0;
  461.      *     sscf->certificate_values = NULL;
  462.      *     sscf->dhparam = { 0, NULL };
  463.      *     sscf->ecdh_curve = { 0, NULL };
  464.      *     sscf->client_certificate = { 0, NULL };
  465.      *     sscf->trusted_certificate = { 0, NULL };
  466.      *     sscf->crl = { 0, NULL };
  467.      *     sscf->ciphers = { 0, NULL };
  468.      *     sscf->shm_zone = NULL;
  469.      *     sscf->ocsp_responder = { 0, NULL };
  470.      *     sscf->stapling_file = { 0, NULL };
  471.      *     sscf->stapling_responder = { 0, NULL };
  472.      */

  473.     sscf->prefer_server_ciphers = NGX_CONF_UNSET;
  474.     sscf->early_data = NGX_CONF_UNSET;
  475.     sscf->reject_handshake = NGX_CONF_UNSET;
  476.     sscf->buffer_size = NGX_CONF_UNSET_SIZE;
  477.     sscf->verify = NGX_CONF_UNSET_UINT;
  478.     sscf->verify_depth = NGX_CONF_UNSET_UINT;
  479.     sscf->certificates = NGX_CONF_UNSET_PTR;
  480.     sscf->certificate_keys = NGX_CONF_UNSET_PTR;
  481.     sscf->passwords = NGX_CONF_UNSET_PTR;
  482.     sscf->conf_commands = NGX_CONF_UNSET_PTR;
  483.     sscf->builtin_session_cache = NGX_CONF_UNSET;
  484.     sscf->session_timeout = NGX_CONF_UNSET;
  485.     sscf->session_tickets = NGX_CONF_UNSET;
  486.     sscf->session_ticket_keys = NGX_CONF_UNSET_PTR;
  487.     sscf->ocsp = NGX_CONF_UNSET_UINT;
  488.     sscf->ocsp_cache_zone = NGX_CONF_UNSET_PTR;
  489.     sscf->stapling = NGX_CONF_UNSET;
  490.     sscf->stapling_verify = NGX_CONF_UNSET;

  491.     return sscf;
  492. }


  493. static char *
  494. ngx_http_ssl_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
  495. {
  496.     ngx_http_ssl_srv_conf_t *prev = parent;
  497.     ngx_http_ssl_srv_conf_t *conf = child;

  498.     ngx_pool_cleanup_t  *cln;

  499.     ngx_conf_merge_value(conf->session_timeout,
  500.                          prev->session_timeout, 300);

  501.     ngx_conf_merge_value(conf->prefer_server_ciphers,
  502.                          prev->prefer_server_ciphers, 0);

  503.     ngx_conf_merge_value(conf->early_data, prev->early_data, 0);
  504.     ngx_conf_merge_value(conf->reject_handshake, prev->reject_handshake, 0);

  505.     ngx_conf_merge_bitmask_value(conf->protocols, prev->protocols,
  506.                          (NGX_CONF_BITMASK_SET|NGX_SSL_DEFAULT_PROTOCOLS));

  507.     ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size,
  508.                          NGX_SSL_BUFSIZE);

  509.     ngx_conf_merge_uint_value(conf->verify, prev->verify, 0);
  510.     ngx_conf_merge_uint_value(conf->verify_depth, prev->verify_depth, 1);

  511.     ngx_conf_merge_ptr_value(conf->certificates, prev->certificates, NULL);
  512.     ngx_conf_merge_ptr_value(conf->certificate_keys, prev->certificate_keys,
  513.                          NULL);

  514.     ngx_conf_merge_ptr_value(conf->passwords, prev->passwords, NULL);

  515.     ngx_conf_merge_str_value(conf->dhparam, prev->dhparam, "");

  516.     ngx_conf_merge_str_value(conf->client_certificate, prev->client_certificate,
  517.                          "");
  518.     ngx_conf_merge_str_value(conf->trusted_certificate,
  519.                          prev->trusted_certificate, "");
  520.     ngx_conf_merge_str_value(conf->crl, prev->crl, "");

  521.     ngx_conf_merge_str_value(conf->ecdh_curve, prev->ecdh_curve,
  522.                          NGX_DEFAULT_ECDH_CURVE);

  523.     ngx_conf_merge_str_value(conf->ciphers, prev->ciphers, NGX_DEFAULT_CIPHERS);

  524.     ngx_conf_merge_ptr_value(conf->conf_commands, prev->conf_commands, NULL);

  525.     ngx_conf_merge_uint_value(conf->ocsp, prev->ocsp, 0);
  526.     ngx_conf_merge_str_value(conf->ocsp_responder, prev->ocsp_responder, "");
  527.     ngx_conf_merge_ptr_value(conf->ocsp_cache_zone,
  528.                          prev->ocsp_cache_zone, NULL);

  529.     ngx_conf_merge_value(conf->stapling, prev->stapling, 0);
  530.     ngx_conf_merge_value(conf->stapling_verify, prev->stapling_verify, 0);
  531.     ngx_conf_merge_str_value(conf->stapling_file, prev->stapling_file, "");
  532.     ngx_conf_merge_str_value(conf->stapling_responder,
  533.                          prev->stapling_responder, "");

  534.     conf->ssl.log = cf->log;

  535.     if (conf->certificates) {

  536.         if (conf->certificate_keys == NULL
  537.             || conf->certificate_keys->nelts < conf->certificates->nelts)
  538.         {
  539.             ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
  540.                           "no \"ssl_certificate_key\" is defined "
  541.                           "for certificate \"%V\"",
  542.                           ((ngx_str_t *) conf->certificates->elts)
  543.                           + conf->certificates->nelts - 1);
  544.             return NGX_CONF_ERROR;
  545.         }

  546.     } else if (!conf->reject_handshake) {
  547.         return NGX_CONF_OK;
  548.     }

  549.     if (ngx_ssl_create(&conf->ssl, conf->protocols, conf) != NGX_OK) {
  550.         return NGX_CONF_ERROR;
  551.     }

  552.     cln = ngx_pool_cleanup_add(cf->pool, 0);
  553.     if (cln == NULL) {
  554.         ngx_ssl_cleanup_ctx(&conf->ssl);
  555.         return NGX_CONF_ERROR;
  556.     }

  557.     cln->handler = ngx_ssl_cleanup_ctx;
  558.     cln->data = &conf->ssl;

  559. #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME

  560.     if (SSL_CTX_set_tlsext_servername_callback(conf->ssl.ctx,
  561.                                                ngx_http_ssl_servername)
  562.         == 0)
  563.     {
  564.         ngx_log_error(NGX_LOG_WARN, cf->log, 0,
  565.             "nginx was built with SNI support, however, now it is linked "
  566.             "dynamically to an OpenSSL library which has no tlsext support, "
  567.             "therefore SNI is not available");
  568.     }

  569. #endif

  570. #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
  571.     SSL_CTX_set_alpn_select_cb(conf->ssl.ctx, ngx_http_ssl_alpn_select, NULL);
  572. #endif

  573.     if (ngx_ssl_ciphers(cf, &conf->ssl, &conf->ciphers,
  574.                         conf->prefer_server_ciphers)
  575.         != NGX_OK)
  576.     {
  577.         return NGX_CONF_ERROR;
  578.     }

  579.     if (ngx_http_ssl_compile_certificates(cf, conf) != NGX_OK) {
  580.         return NGX_CONF_ERROR;
  581.     }

  582.     if (conf->certificate_values) {

  583. #ifdef SSL_R_CERT_CB_ERROR

  584.         /* install callback to lookup certificates */

  585.         SSL_CTX_set_cert_cb(conf->ssl.ctx, ngx_http_ssl_certificate, conf);

  586. #else
  587.         ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
  588.                       "variables in "
  589.                       "\"ssl_certificate\" and \"ssl_certificate_key\" "
  590.                       "directives are not supported on this platform");
  591.         return NGX_CONF_ERROR;
  592. #endif

  593.     } else if (conf->certificates) {

  594.         /* configure certificates */

  595.         if (ngx_ssl_certificates(cf, &conf->ssl, conf->certificates,
  596.                                  conf->certificate_keys, conf->passwords)
  597.             != NGX_OK)
  598.         {
  599.             return NGX_CONF_ERROR;
  600.         }
  601.     }

  602.     conf->ssl.buffer_size = conf->buffer_size;

  603.     if (conf->verify) {

  604.         if (conf->verify != 3
  605.             && conf->client_certificate.len == 0
  606.             && conf->trusted_certificate.len == 0)
  607.         {
  608.             ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
  609.                           "no ssl_client_certificate or "
  610.                           "ssl_trusted_certificate for ssl_verify_client");
  611.             return NGX_CONF_ERROR;
  612.         }

  613.         if (ngx_ssl_client_certificate(cf, &conf->ssl,
  614.                                        &conf->client_certificate,
  615.                                        conf->verify_depth)
  616.             != NGX_OK)
  617.         {
  618.             return NGX_CONF_ERROR;
  619.         }
  620.     }

  621.     if (ngx_ssl_trusted_certificate(cf, &conf->ssl,
  622.                                     &conf->trusted_certificate,
  623.                                     conf->verify_depth)
  624.         != NGX_OK)
  625.     {
  626.         return NGX_CONF_ERROR;
  627.     }

  628.     if (ngx_ssl_crl(cf, &conf->ssl, &conf->crl) != NGX_OK) {
  629.         return NGX_CONF_ERROR;
  630.     }

  631.     if (conf->ocsp) {

  632.         if (conf->verify == 3) {
  633.             ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
  634.                           "\"ssl_ocsp\" is incompatible with "
  635.                           "\"ssl_verify_client optional_no_ca\"");
  636.             return NGX_CONF_ERROR;
  637.         }

  638.         if (ngx_ssl_ocsp(cf, &conf->ssl, &conf->ocsp_responder, conf->ocsp,
  639.                          conf->ocsp_cache_zone)
  640.             != NGX_OK)
  641.         {
  642.             return NGX_CONF_ERROR;
  643.         }
  644.     }

  645.     if (ngx_ssl_dhparam(cf, &conf->ssl, &conf->dhparam) != NGX_OK) {
  646.         return NGX_CONF_ERROR;
  647.     }

  648.     if (ngx_ssl_ecdh_curve(cf, &conf->ssl, &conf->ecdh_curve) != NGX_OK) {
  649.         return NGX_CONF_ERROR;
  650.     }

  651.     ngx_conf_merge_value(conf->builtin_session_cache,
  652.                          prev->builtin_session_cache, NGX_SSL_NONE_SCACHE);

  653.     if (conf->shm_zone == NULL) {
  654.         conf->shm_zone = prev->shm_zone;
  655.     }

  656.     if (ngx_ssl_session_cache(&conf->ssl, &ngx_http_ssl_sess_id_ctx,
  657.                               conf->certificates, conf->builtin_session_cache,
  658.                               conf->shm_zone, conf->session_timeout)
  659.         != NGX_OK)
  660.     {
  661.         return NGX_CONF_ERROR;
  662.     }

  663.     ngx_conf_merge_value(conf->session_tickets, prev->session_tickets, 1);

  664. #ifdef SSL_OP_NO_TICKET
  665.     if (!conf->session_tickets) {
  666.         SSL_CTX_set_options(conf->ssl.ctx, SSL_OP_NO_TICKET);
  667.     }
  668. #endif

  669.     ngx_conf_merge_ptr_value(conf->session_ticket_keys,
  670.                          prev->session_ticket_keys, NULL);

  671.     if (ngx_ssl_session_ticket_keys(cf, &conf->ssl, conf->session_ticket_keys)
  672.         != NGX_OK)
  673.     {
  674.         return NGX_CONF_ERROR;
  675.     }

  676.     if (conf->stapling) {

  677.         if (ngx_ssl_stapling(cf, &conf->ssl, &conf->stapling_file,
  678.                              &conf->stapling_responder, conf->stapling_verify)
  679.             != NGX_OK)
  680.         {
  681.             return NGX_CONF_ERROR;
  682.         }

  683.     }

  684.     if (ngx_ssl_early_data(cf, &conf->ssl, conf->early_data) != NGX_OK) {
  685.         return NGX_CONF_ERROR;
  686.     }

  687.     if (ngx_ssl_conf_commands(cf, &conf->ssl, conf->conf_commands) != NGX_OK) {
  688.         return NGX_CONF_ERROR;
  689.     }

  690.     return NGX_CONF_OK;
  691. }


  692. static ngx_int_t
  693. ngx_http_ssl_compile_certificates(ngx_conf_t *cf,
  694.     ngx_http_ssl_srv_conf_t *conf)
  695. {
  696.     ngx_str_t                         *cert, *key;
  697.     ngx_uint_t                         i, nelts;
  698.     ngx_http_complex_value_t          *cv;
  699.     ngx_http_compile_complex_value_t   ccv;

  700.     if (conf->certificates == NULL) {
  701.         return NGX_OK;
  702.     }

  703.     cert = conf->certificates->elts;
  704.     key = conf->certificate_keys->elts;
  705.     nelts = conf->certificates->nelts;

  706.     for (i = 0; i < nelts; i++) {

  707.         if (ngx_http_script_variables_count(&cert[i])) {
  708.             goto found;
  709.         }

  710.         if (ngx_http_script_variables_count(&key[i])) {
  711.             goto found;
  712.         }
  713.     }

  714.     return NGX_OK;

  715. found:

  716.     conf->certificate_values = ngx_array_create(cf->pool, nelts,
  717.                                              sizeof(ngx_http_complex_value_t));
  718.     if (conf->certificate_values == NULL) {
  719.         return NGX_ERROR;
  720.     }

  721.     conf->certificate_key_values = ngx_array_create(cf->pool, nelts,
  722.                                              sizeof(ngx_http_complex_value_t));
  723.     if (conf->certificate_key_values == NULL) {
  724.         return NGX_ERROR;
  725.     }

  726.     for (i = 0; i < nelts; i++) {

  727.         cv = ngx_array_push(conf->certificate_values);
  728.         if (cv == NULL) {
  729.             return NGX_ERROR;
  730.         }

  731.         ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));

  732.         ccv.cf = cf;
  733.         ccv.value = &cert[i];
  734.         ccv.complex_value = cv;
  735.         ccv.zero = 1;

  736.         if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  737.             return NGX_ERROR;
  738.         }

  739.         cv = ngx_array_push(conf->certificate_key_values);
  740.         if (cv == NULL) {
  741.             return NGX_ERROR;
  742.         }

  743.         ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));

  744.         ccv.cf = cf;
  745.         ccv.value = &key[i];
  746.         ccv.complex_value = cv;
  747.         ccv.zero = 1;

  748.         if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  749.             return NGX_ERROR;
  750.         }
  751.     }

  752.     conf->passwords = ngx_ssl_preserve_passwords(cf, conf->passwords);
  753.     if (conf->passwords == NULL) {
  754.         return NGX_ERROR;
  755.     }

  756.     return NGX_OK;
  757. }


  758. static char *
  759. ngx_http_ssl_password_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  760. {
  761.     ngx_http_ssl_srv_conf_t *sscf = conf;

  762.     ngx_str_t  *value;

  763.     if (sscf->passwords != NGX_CONF_UNSET_PTR) {
  764.         return "is duplicate";
  765.     }

  766.     value = cf->args->elts;

  767.     sscf->passwords = ngx_ssl_read_password_file(cf, &value[1]);

  768.     if (sscf->passwords == NULL) {
  769.         return NGX_CONF_ERROR;
  770.     }

  771.     return NGX_CONF_OK;
  772. }


  773. static char *
  774. ngx_http_ssl_session_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  775. {
  776.     ngx_http_ssl_srv_conf_t *sscf = conf;

  777.     size_t       len;
  778.     ngx_str_t   *value, name, size;
  779.     ngx_int_t    n;
  780.     ngx_uint_t   i, j;

  781.     value = cf->args->elts;

  782.     for (i = 1; i < cf->args->nelts; i++) {

  783.         if (ngx_strcmp(value[i].data, "off") == 0) {
  784.             sscf->builtin_session_cache = NGX_SSL_NO_SCACHE;
  785.             continue;
  786.         }

  787.         if (ngx_strcmp(value[i].data, "none") == 0) {
  788.             sscf->builtin_session_cache = NGX_SSL_NONE_SCACHE;
  789.             continue;
  790.         }

  791.         if (ngx_strcmp(value[i].data, "builtin") == 0) {
  792.             sscf->builtin_session_cache = NGX_SSL_DFLT_BUILTIN_SCACHE;
  793.             continue;
  794.         }

  795.         if (value[i].len > sizeof("builtin:") - 1
  796.             && ngx_strncmp(value[i].data, "builtin:", sizeof("builtin:") - 1)
  797.                == 0)
  798.         {
  799.             n = ngx_atoi(value[i].data + sizeof("builtin:") - 1,
  800.                          value[i].len - (sizeof("builtin:") - 1));

  801.             if (n == NGX_ERROR) {
  802.                 goto invalid;
  803.             }

  804.             sscf->builtin_session_cache = n;

  805.             continue;
  806.         }

  807.         if (value[i].len > sizeof("shared:") - 1
  808.             && ngx_strncmp(value[i].data, "shared:", sizeof("shared:") - 1)
  809.                == 0)
  810.         {
  811.             len = 0;

  812.             for (j = sizeof("shared:") - 1; j < value[i].len; j++) {
  813.                 if (value[i].data[j] == ':') {
  814.                     break;
  815.                 }

  816.                 len++;
  817.             }

  818.             if (len == 0 || j == value[i].len) {
  819.                 goto invalid;
  820.             }

  821.             name.len = len;
  822.             name.data = value[i].data + sizeof("shared:") - 1;

  823.             size.len = value[i].len - j - 1;
  824.             size.data = name.data + len + 1;

  825.             n = ngx_parse_size(&size);

  826.             if (n == NGX_ERROR) {
  827.                 goto invalid;
  828.             }

  829.             if (n < (ngx_int_t) (8 * ngx_pagesize)) {
  830.                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  831.                                    "session cache \"%V\" is too small",
  832.                                    &value[i]);

  833.                 return NGX_CONF_ERROR;
  834.             }

  835.             sscf->shm_zone = ngx_shared_memory_add(cf, &name, n,
  836.                                                    &ngx_http_ssl_module);
  837.             if (sscf->shm_zone == NULL) {
  838.                 return NGX_CONF_ERROR;
  839.             }

  840.             sscf->shm_zone->init = ngx_ssl_session_cache_init;

  841.             continue;
  842.         }

  843.         goto invalid;
  844.     }

  845.     if (sscf->shm_zone && sscf->builtin_session_cache == NGX_CONF_UNSET) {
  846.         sscf->builtin_session_cache = NGX_SSL_NO_BUILTIN_SCACHE;
  847.     }

  848.     return NGX_CONF_OK;

  849. invalid:

  850.     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  851.                        "invalid session cache \"%V\"", &value[i]);

  852.     return NGX_CONF_ERROR;
  853. }


  854. static char *
  855. ngx_http_ssl_ocsp_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  856. {
  857.     ngx_http_ssl_srv_conf_t *sscf = conf;

  858.     size_t       len;
  859.     ngx_int_t    n;
  860.     ngx_str_t   *value, name, size;
  861.     ngx_uint_t   j;

  862.     if (sscf->ocsp_cache_zone != NGX_CONF_UNSET_PTR) {
  863.         return "is duplicate";
  864.     }

  865.     value = cf->args->elts;

  866.     if (ngx_strcmp(value[1].data, "off") == 0) {
  867.         sscf->ocsp_cache_zone = NULL;
  868.         return NGX_CONF_OK;
  869.     }

  870.     if (value[1].len <= sizeof("shared:") - 1
  871.         || ngx_strncmp(value[1].data, "shared:", sizeof("shared:") - 1) != 0)
  872.     {
  873.         goto invalid;
  874.     }

  875.     len = 0;

  876.     for (j = sizeof("shared:") - 1; j < value[1].len; j++) {
  877.         if (value[1].data[j] == ':') {
  878.             break;
  879.         }

  880.         len++;
  881.     }

  882.     if (len == 0 || j == value[1].len) {
  883.         goto invalid;
  884.     }

  885.     name.len = len;
  886.     name.data = value[1].data + sizeof("shared:") - 1;

  887.     size.len = value[1].len - j - 1;
  888.     size.data = name.data + len + 1;

  889.     n = ngx_parse_size(&size);

  890.     if (n == NGX_ERROR) {
  891.         goto invalid;
  892.     }

  893.     if (n < (ngx_int_t) (8 * ngx_pagesize)) {
  894.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  895.                            "OCSP cache \"%V\" is too small", &value[1]);

  896.         return NGX_CONF_ERROR;
  897.     }

  898.     sscf->ocsp_cache_zone = ngx_shared_memory_add(cf, &name, n,
  899.                                                   &ngx_http_ssl_module_ctx);
  900.     if (sscf->ocsp_cache_zone == NULL) {
  901.         return NGX_CONF_ERROR;
  902.     }

  903.     sscf->ocsp_cache_zone->init = ngx_ssl_ocsp_cache_init;

  904.     return NGX_CONF_OK;

  905. invalid:

  906.     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  907.                        "invalid OCSP cache \"%V\"", &value[1]);

  908.     return NGX_CONF_ERROR;
  909. }


  910. static char *
  911. ngx_http_ssl_conf_command_check(ngx_conf_t *cf, void *post, void *data)
  912. {
  913. #ifndef SSL_CONF_FLAG_FILE
  914.     return "is not supported on this platform";
  915. #else
  916.     return NGX_CONF_OK;
  917. #endif
  918. }


  919. static ngx_int_t
  920. ngx_http_ssl_init(ngx_conf_t *cf)
  921. {
  922.     ngx_uint_t                   a, p, s;
  923.     const char                  *name;
  924.     ngx_http_conf_addr_t        *addr;
  925.     ngx_http_conf_port_t        *port;
  926.     ngx_http_ssl_srv_conf_t     *sscf;
  927.     ngx_http_core_loc_conf_t    *clcf;
  928.     ngx_http_core_srv_conf_t   **cscfp, *cscf;
  929.     ngx_http_core_main_conf_t   *cmcf;

  930.     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
  931.     cscfp = cmcf->servers.elts;

  932.     for (s = 0; s < cmcf->servers.nelts; s++) {

  933.         sscf = cscfp[s]->ctx->srv_conf[ngx_http_ssl_module.ctx_index];

  934.         if (sscf->ssl.ctx == NULL) {
  935.             continue;
  936.         }

  937.         clcf = cscfp[s]->ctx->loc_conf[ngx_http_core_module.ctx_index];

  938.         if (sscf->stapling) {
  939.             if (ngx_ssl_stapling_resolver(cf, &sscf->ssl, clcf->resolver,
  940.                                           clcf->resolver_timeout)
  941.                 != NGX_OK)
  942.             {
  943.                 return NGX_ERROR;
  944.             }
  945.         }

  946.         if (sscf->ocsp) {
  947.             if (ngx_ssl_ocsp_resolver(cf, &sscf->ssl, clcf->resolver,
  948.                                       clcf->resolver_timeout)
  949.                 != NGX_OK)
  950.             {
  951.                 return NGX_ERROR;
  952.             }
  953.         }
  954.     }

  955.     if (cmcf->ports == NULL) {
  956.         return NGX_OK;
  957.     }

  958.     port = cmcf->ports->elts;
  959.     for (p = 0; p < cmcf->ports->nelts; p++) {

  960.         addr = port[p].addrs.elts;
  961.         for (a = 0; a < port[p].addrs.nelts; a++) {

  962.             if (!addr[a].opt.ssl && !addr[a].opt.quic) {
  963.                 continue;
  964.             }

  965.             if (addr[a].opt.quic) {
  966.                 name = "quic";

  967. #if (NGX_QUIC_OPENSSL_COMPAT)
  968.                 if (ngx_http_ssl_quic_compat_init(cf, &addr[a]) != NGX_OK) {
  969.                     return NGX_ERROR;
  970.                 }
  971. #endif

  972.             } else {
  973.                 name = "ssl";
  974.             }

  975.             cscf = addr[a].default_server;
  976.             sscf = cscf->ctx->srv_conf[ngx_http_ssl_module.ctx_index];

  977.             if (sscf->certificates) {

  978.                 if (addr[a].opt.quic && !(sscf->protocols & NGX_SSL_TLSv1_3)) {
  979.                     ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
  980.                                   "\"ssl_protocols\" must enable TLSv1.3 for "
  981.                                   "the \"listen ... %s\" directive in %s:%ui",
  982.                                   name, cscf->file_name, cscf->line);
  983.                     return NGX_ERROR;
  984.                 }

  985.                 continue;
  986.             }

  987.             if (!sscf->reject_handshake) {
  988.                 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
  989.                               "no \"ssl_certificate\" is defined for "
  990.                               "the \"listen ... %s\" directive in %s:%ui",
  991.                               name, cscf->file_name, cscf->line);
  992.                 return NGX_ERROR;
  993.             }

  994.             /*
  995.              * if no certificates are defined in the default server,
  996.              * check all non-default server blocks
  997.              */

  998.             cscfp = addr[a].servers.elts;
  999.             for (s = 0; s < addr[a].servers.nelts; s++) {

  1000.                 cscf = cscfp[s];
  1001.                 sscf = cscf->ctx->srv_conf[ngx_http_ssl_module.ctx_index];

  1002.                 if (sscf->certificates || sscf->reject_handshake) {
  1003.                     continue;
  1004.                 }

  1005.                 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
  1006.                               "no \"ssl_certificate\" is defined for "
  1007.                               "the \"listen ... %s\" directive in %s:%ui",
  1008.                               name, cscf->file_name, cscf->line);
  1009.                 return NGX_ERROR;
  1010.             }
  1011.         }
  1012.     }

  1013.     return NGX_OK;
  1014. }


  1015. #if (NGX_QUIC_OPENSSL_COMPAT)

  1016. static ngx_int_t
  1017. ngx_http_ssl_quic_compat_init(ngx_conf_t *cf, ngx_http_conf_addr_t *addr)
  1018. {
  1019.     ngx_uint_t                  s;
  1020.     ngx_http_ssl_srv_conf_t    *sscf;
  1021.     ngx_http_core_srv_conf_t  **cscfp, *cscf;

  1022.     cscfp = addr->servers.elts;
  1023.     for (s = 0; s < addr->servers.nelts; s++) {

  1024.         cscf = cscfp[s];
  1025.         sscf = cscf->ctx->srv_conf[ngx_http_ssl_module.ctx_index];

  1026.         if (sscf->certificates || sscf->reject_handshake) {
  1027.             if (ngx_quic_compat_init(cf, sscf->ssl.ctx) != NGX_OK) {
  1028.                 return NGX_ERROR;
  1029.             }
  1030.         }
  1031.     }

  1032.     return NGX_OK;
  1033. }

  1034. #endif