Skip to content
Open
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
5 changes: 5 additions & 0 deletions changes-entries/h3_ssl_var_lookup.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*) mod_http3: HTTPS and the SSL_PROTOCOL, SSL_CIPHER, SSL_CIPHER_* and
SSL_SESSION_RESUMED variables are now set over HTTP/3 for mod_rewrite
%{SSL:...}, mod_headers %{...}s and the other modules that read SSL
variables the same way. HTTPS used to be "off" and the SSL_ variables
empty. [Arturo Bernal]
62 changes: 56 additions & 6 deletions modules/http3/h3_hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,58 @@ int h3_hook_ssl_conn_is_ssl(conn_rec* c)
return IS_H3_CONN(c) ? OK : DECLINED;
}

/* The SSL_ variables HTTP/3 knows, named as mod_ssl names them. */
static const struct
{
const char* name;
apr_size_t offset;
} h3_tls_vars[] = {
{"SSL_PROTOCOL", APR_OFFSETOF(h3_tls_env, protocol)},
{"SSL_CIPHER", APR_OFFSETOF(h3_tls_env, cipher)},
{"SSL_CIPHER_USEKEYSIZE", APR_OFFSETOF(h3_tls_env, cipher_usekeysize)},
{"SSL_CIPHER_ALGKEYSIZE", APR_OFFSETOF(h3_tls_env, cipher_algkeysize)},
{"SSL_CIPHER_EXPORT", APR_OFFSETOF(h3_tls_env, cipher_export)},
{"SSL_SESSION_RESUMED", APR_OFFSETOF(h3_tls_env, session_resumed)},
};

#define H3_TLS_VAR(tls, i) (*(const char* const*)((const char*)(tls) + h3_tls_vars[i].offset))

const char* h3_hook_ssl_var_lookup(apr_pool_t* p H3_UNUSED, server_rec* s H3_UNUSED, conn_rec* c, request_rec* r, const char* name)
{
if (!c && r)
{
c = r->connection;
}
if (!c || !IS_H3_CONN(c))
{
return NULL;
}

/* mod_ssl does not manage this connection: without an answer here it
* reports HTTPS as "off" and no SSL_ variable. Names are matched without
* case, as mod_ssl does. */
if (!ap_cstr_casecmp(name, "HTTPS"))
{
return "on";
}

conn_rec* mc = c->master ? c->master : c;
const h3_session* session = ap_get_module_config(mc->conn_config, &http3_module);
const h3_tls_env* tls = session ? &session->tls_env : NULL;
if (!tls || !tls->protocol)
{
return NULL;
}
for (apr_size_t i = 0; i < OSSL_NELEM(h3_tls_vars); i++)
{
if (!ap_cstr_casecmp(name, h3_tls_vars[i].name))
{
return H3_TLS_VAR(tls, i);
}
}
return NULL;
}

int h3_hook_fixups(request_rec* r)
{
if (!ap_is_initial_req(r))
Expand All @@ -71,12 +123,10 @@ int h3_hook_fixups(request_rec* r)
const h3_tls_env* tls = (ctx && ctx->stream && ctx->stream->session) ? &ctx->stream->session->tls_env : NULL;
if (tls && tls->protocol)
{
apr_table_setn(r->subprocess_env, "SSL_PROTOCOL", tls->protocol);
apr_table_setn(r->subprocess_env, "SSL_CIPHER", tls->cipher);
apr_table_setn(r->subprocess_env, "SSL_CIPHER_USEKEYSIZE", tls->cipher_usekeysize);
apr_table_setn(r->subprocess_env, "SSL_CIPHER_ALGKEYSIZE", tls->cipher_algkeysize);
apr_table_setn(r->subprocess_env, "SSL_CIPHER_EXPORT", tls->cipher_export);
apr_table_setn(r->subprocess_env, "SSL_SESSION_RESUMED", tls->session_resumed);
for (apr_size_t i = 0; i < OSSL_NELEM(h3_tls_vars); i++)
{
apr_table_setn(r->subprocess_env, h3_tls_vars[i].name, H3_TLS_VAR(tls, i));
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions modules/http3/h3_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ conn_rec* h3_synth_conn(h3_session* session)
ap_set_module_config(c->conn_config, h3_logio_module, apr_pcalloc(cpool, sizeof(h3_logio_config_t)));
}

/* Stream connections reach the session through c->master; they use their
* own slot for the EOR brigade. */
ap_set_module_config(c->conn_config, &http3_module, session);
session->c = c;
return c;
}
Expand Down
12 changes: 12 additions & 0 deletions modules/http3/include/h3_hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ apr_port_t h3_hook_default_port(const request_rec* r);
*/
int h3_hook_ssl_conn_is_ssl(conn_rec* c);

/**
* ap_hook_ssl_var_lookup. Answers HTTPS and the SSL_ variables HTTP/3 knows
* for an H3 connection, which mod_ssl does not manage.
* @param p Pool for the result (unused, values live in the session).
* @param s The server (unused).
* @param c The connection, may be NULL when r is given.
* @param r The request, may be NULL.
* @param name The variable name.
* @return The value, or NULL to let other modules answer.
*/
const char* h3_hook_ssl_var_lookup(apr_pool_t* p, server_rec* s, conn_rec* c, request_rec* r, const char* name);

/**
* ap_hook_post_read_request. No-op for H3; reserved for future
* per-request initialisation. Always returns OK.
Expand Down
1 change: 1 addition & 0 deletions modules/http3/mod_http3.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ static void register_hooks(apr_pool_t* p H3_UNUSED)
ap_hook_http_scheme(h3_hook_http_scheme, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_default_port(h3_hook_default_port, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_ssl_conn_is_ssl(h3_hook_ssl_conn_is_ssl, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_ssl_var_lookup(h3_hook_ssl_var_lookup, NULL, NULL, APR_HOOK_MIDDLE);

h3_net_out_filter_handle = ap_register_output_filter("H3_NET_OUT", h3_filter_out, NULL, AP_FTYPE_NETWORK);
h3_net_in_filter_handle = ap_register_input_filter("H3_NET_IN", h3_filter_in, NULL, AP_FTYPE_NETWORK);
Expand Down
41 changes: 41 additions & 0 deletions test/modules/http3/test_102_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,44 @@ def test_h3_102_10_no_tickets(self, env):
client = env.h3_client()
assert cgi_env(client)["SSL_SESSION_RESUMED"] == "Initial"
assert client.received_tickets == []


class TestSslVarLookup:
"""ap_ssl_var_lookup() callers see the TLS state of an HTTP/3 request."""

@pytest.fixture(autouse=True, scope="class")
@classmethod
def _class_scope(cls, env):
H3Conf(env).add_h3_vhost(directives=[
'Header always set X-SSL-Protocol "%{SSL_PROTOCOL}s"',
'Header always set X-SSL-Cipher "%{SSL_CIPHER}s"',
'Header always set X-HTTPS "%{HTTPS}s"',
'Header always set X-Lower-HTTPS "%{https}s"',
'Header always set X-SSL-Interface "%{SSL_VERSION_INTERFACE}s"',
"RewriteEngine on",
r"RewriteCond %{SSL:ssl_protocol} ^TLSv1\.3$",
"RewriteCond %{SSL:HTTPS} =on",
"RewriteRule ^/sslvar$ /sslvar-tls13 [R=302,L]",
]).install()
assert env.apache_restart() == 0

def test_h3_102_20_headers(self, env):
url = f"https://{env.h3_domain}:{env.https_port}/"
h1 = env.curl_get(url, options=["--http1.1", "--tlsv1.3"])
assert h1.response["header"]["x-ssl-protocol"] == "TLSv1.3"
client = env.h3_client()
r = client.get("/")
assert r.header("x-https") == "on", f"{r}"
assert r.header("x-lower-https") == "on", f"{r}"
assert r.header("x-ssl-protocol") == "TLSv1.3", f"{r}"
assert r.header("x-ssl-cipher") == cgi_env(client)["SSL_CIPHER"], \
f"{r}"
# names mod_http3 does not know still reach mod_ssl
assert r.header("x-ssl-interface") == \
h1.response["header"]["x-ssl-interface"], f"{r}"
assert r.header("x-ssl-interface").startswith("mod_ssl/"), f"{r}"

def test_h3_102_21_rewrite(self, env):
r = env.h3_client().get("/sslvar")
assert r.status == 302, f"{r}"
assert r.header("location").endswith("/sslvar-tls13"), f"{r}"
Loading