RFC 7301 and ALPN: Choosing the HTTP protocol at a TLS gateway
The ALPN standard from 2014 helps a TLS gateway distinguish HTTP/2 and HTTP/1.1 connections. Three local handshakes record the client offer, the server’s choice and the result when no protocol is negotiated.
Archive retrospective · Topic year 2014
In this article
Two application protocols at an HTTPS gateway
A reporting service accepts connections through a TLS gateway. Some clients can use HTTP/2, while older clients speak HTTP/1.1. They can reach the same port, although their first application bytes follow different rules. HTTP/1.1 requests are text lines. HTTP/2 begins with a different preface and uses binary frames. Before handing the connection to a request parser, the gateway needs to know which rules to apply. RFC 7301, published in July 2014, defined the Application-Layer Protocol Negotiation extension for making that decision during the TLS handshake. The client offers a list of protocols. The server communicates one choice from that list. Both client generations can therefore connect to a single listener and hand their application data to the appropriate handler.
Certificate validation and protocol selection
The server’s identity and the application protocol for the connection are separate pieces of information. In the example, the client connects using localhost as the server name. The certificate includes localhost as a subject alternative name, and the client trusts only the certificate created for the lab. The operating system’s trust store is untouched. The server’s list puts h2 ahead of http/1.1. When the client offers the two in reverse order, this server selects h2. That result reflects the configured server preference. RFC 7301 describes a selection from the intersection of the two sides’ lists. A log that treats the client’s first entry as the result would misreport this handshake. Reading the negotiated value from the socket records what the server actually chose.
Running three loopback connections
The example creates a one-day certificate with an installed OpenSSL command-line tool. It puts the key and certificate in a temporary directory and removes both when the run finishes. The Node server binds to 127.0.0.1 and lets the operating system choose a free port. The first client offers http/1.1 and h2, in that order. The second offers http/1.1 alone. The third sends no ALPN extension. For each connection the program records certificate validation, the TLS version and the selected application protocol. It sends no HTTP request or response. To reproduce the lab, run the example with a current Node release and OpenSSL in your PATH. Alternatively, use YG_ALPN_OPENSSL to give the executable’s local path.
import assert from 'node:assert/strict';
import { execFileSync } from 'node:child_process';
import { once } from 'node:events';
import { mkdtemp, readFile, rmdir, unlink } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { connect, createServer } from 'node:tls';
const folder = await mkdtemp(join(tmpdir(), 'alpn-lab-'));
const keyFile = join(folder, 'loopback-key.pem');
const certFile = join(folder, 'loopback-cert.pem');
let server;
try {
const openssl = process.env.YG_ALPN_OPENSSL ?? 'openssl';
execFileSync(openssl, ['req', '-x509', '-newkey', 'rsa:2048', '-noenc',
'-days', '1', '-subj', '/CN=localhost', '-addext', 'subjectAltName=DNS:localhost',
'-keyout', keyFile, '-out', certFile], { stdio: 'ignore' });
const [key, cert] = await Promise.all([readFile(keyFile), readFile(certFile)]);
server = createServer({
key, cert, ALPNProtocols: ['h2', 'http/1.1'],
minVersion: 'TLSv1.3', maxVersion: 'TLSv1.3',
}, socket => socket.end());
server.listen(0, '127.0.0.1');
await once(server, 'listening');
async function observe(offered) {
const socket = connect({
host: '127.0.0.1', servername: 'localhost',
port: server.address().port, ca: cert,
...(offered ? { ALPNProtocols: offered } : {}),
});
const closed = once(socket, 'close');
await once(socket, 'secureConnect');
const result = {
offered: offered ?? [], verified: socket.authorized, tls: socket.getProtocol(),
negotiated: socket.alpnProtocol || null,
};
socket.end();
await closed;
return result;
}
const results = [];
for (const offer of [['http/1.1', 'h2'], ['http/1.1'], undefined]) {
results.push(await observe(offer));
}
assert.deepEqual(results.map(row => row.negotiated), ['h2', 'http/1.1', null]);
assert(results.every(row => row.verified && row.tls === 'TLSv1.3'));
console.log(JSON.stringify({ node: process.version, openssl: process.versions.openssl, results }, null, 2));
} finally {
if (server?.listening) {
const closed = once(server, 'close');
server.close();
await closed;
}
for (const path of [keyFile, certFile]) {
await unlink(path).catch(error => { if (error.code !== 'ENOENT') throw error; });
}
await rmdir(folder);
}
Results and what they measure
On Windows, all three connections negotiated TLS 1.3 and passed certificate verification with Node v24.19.0, linked against OpenSSL 3.5.7. The application protocol results were h2, http/1.1 and null, respectively. In the third case Node returned no negotiated ALPN protocol, which the program represented as null. A successful TLS connection in that case leaves the application default to the surrounding service policy. In the first case the selection of h2 also says nothing about whether an HTTP/2 request would receive a valid response. An end-to-end test would send a real request over the selected protocol and check that both client and server parse its frames correctly. The program here measures one phase only, namely the handshake.
What to record in a gateway log
A gateway can capture the TLS version, selected ALPN protocol and application status as separate fields. If the proportion of h2 connections suddenly falls, compare what clients offered with what the server selected. If those values are conflated, changes in the client population and changes in server preference can look identical. Also measure any second connection from the gateway to the origin service. A gateway can accept h2 from the client and deliberately speak HTTP/1.1 to the origin. Certificate validation, negotiated protocol and application response each describe a distinct part of the path. The three observations in this lab provide a small, reproducible example of recording those parts separately.
Reading the 2014 RFC alongside a modern trace
RFC 7301 predates TLS 1.3. It explains ALPN in the context of ClientHello and ServerHello. In TLS 1.3, the client still offers the extension in ClientHello, while the server sends its selection in EncryptedExtensions. RFC 8446 documents the placement in its extension table and its discussion of EncryptedExtensions. The lab forces TLS 1.3 so its execution environment is explicit. RFC 9113 and the IANA registry document the h2 identifier for HTTP/2 separately. Across the historical and modern handshakes, the application still receives a selected protocol identifier before it tries to parse a request. Independently checking request results then establishes whether that protocol worked beyond negotiation.
Reader discussion
Which data would you verify before applying this development in your field?
No comments yet. Be the first to contribute.
Write a comment