diff --git a/.github/workflows/fips.yml b/.github/workflows/fips.yml index 31981af..924dbc1 100644 --- a/.github/workflows/fips.yml +++ b/.github/workflows/fips.yml @@ -67,8 +67,12 @@ jobs: os: [ubuntu-24.04, ubuntu-24.04-arm] provider: ["3.5.9", "3.1.2"] include: - - { provider: "3.5.9", cmvp: "5586", mldsa: "offered" } - - { provider: "3.1.2", cmvp: "4985", mldsa: "refused" } + # `pq`: under 3.5.9 (ML-KEM approved) the CNSA 2.0 post-quantum TLS tests must run + # (SecP384r1MLKEM1024, ML-KEM-1024; X25519 is not approved, so X25519MLKEM768 is not + # offered in FIPS mode). Under 3.1.2 OTP still lists the ML-KEM groups but the handshake + # exits; the tests ask by trying and do not run there. + - { provider: "3.5.9", cmvp: "5586", mldsa: "offered", pq: "cnsa" } + - { provider: "3.1.2", cmvp: "4985", mldsa: "refused", pq: "" } timeout-minutes: 45 env: # Fixed absolute paths: the rpath, OpenSSL's MODULESDIR and OTP's ROOTDIR are baked in. @@ -177,9 +181,19 @@ jobs: mix deps.get MIX_ENV=test mix compile + # OpenSSL's own client, for the post-quantum TLS tests' independent check: this job's 3.5.9 + # command, through a wrapper so its library path reaches only it. + - name: the OpenSSL command the post-quantum TLS tests use + run: | + printf '#!/bin/sh\nLD_LIBRARY_PATH=%s/lib exec %s/bin/openssl "$@"\n' "$P" "$P" > "$RUNNER_TEMP/openssl" + chmod +x "$RUNNER_TEMP/openssl" + "$RUNNER_TEMP/openssl" version + echo "OPENSSL_CLI=$RUNNER_TEMP/openssl" >> "$GITHUB_ENV" + - name: the suite, in FIPS mode, asserted in the same VM before any test runs env: ELIXIR_ERL_OPTIONS: "-crypto fips_mode true" + PQ_TLS_REQUIRED: ${{ matrix.pq }} run: > elixir -e 'Application.load(:crypto)' diff --git a/.github/workflows/flake-hunt.yml b/.github/workflows/flake-hunt.yml index 15485ae..41100d7 100644 --- a/.github/workflows/flake-hunt.yml +++ b/.github/workflows/flake-hunt.yml @@ -40,14 +40,16 @@ jobs: fail-fast: false matrix: include: - - { leg: floor, otp: "27", elixir: "1.17", os: ubuntu-24.04, version-type: loose } - - { leg: pinned, otp: "28", elixir: "1.18", os: ubuntu-24.04, version-type: loose } - - { leg: head, otp: "29", elixir: "1.20", os: ubuntu-24.04, version-type: loose } - - { leg: box, otp: "28.5.0.9", elixir: "1.20.4", os: ubuntu-26.04-arm, version-type: strict } + # The gate's four pairs, `pq` included (gate.yml says what it requires of each leg). + - { leg: floor, otp: "27", elixir: "1.17", os: ubuntu-24.04, version-type: loose, pq: "" } + - { leg: pinned, otp: "28", elixir: "1.18", os: ubuntu-24.04, version-type: loose, pq: "" } + - { leg: head, otp: "29", elixir: "1.20", os: ubuntu-24.04, version-type: loose, pq: "" } + - { leg: box, otp: "28.5.0.9", elixir: "1.20.4", os: ubuntu-26.04-arm, version-type: strict, pq: "all" } env: # Through the environment, never inline in a script: the input is the dispatcher's text, # and tools/flake_hunt.sh refuses anything but a positive integer. RUNS: ${{ inputs.runs || '10' }} + PQ_TLS_REQUIRED: ${{ matrix.pq }} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - uses: erlef/setup-beam@54075bcc5e249e4758d363f27d099f55d843f124 # v1.24.1 diff --git a/.github/workflows/gate.yml b/.github/workflows/gate.yml index f45d61d..a858ddc 100644 --- a/.github/workflows/gate.yml +++ b/.github/workflows/gate.yml @@ -52,10 +52,14 @@ jobs: fail-fast: false matrix: include: - - { leg: floor, otp: "27", elixir: "1.17", os: ubuntu-24.04, version-type: loose } - - { leg: pinned, otp: "28", elixir: "1.18", os: ubuntu-24.04, version-type: loose } - - { leg: head, otp: "29", elixir: "1.20", os: ubuntu-24.04, version-type: loose } - - { leg: box, otp: "28.5.0.9", elixir: "1.20.4", os: ubuntu-26.04-arm, version-type: strict } + # `pq`: the post-quantum TLS tests this leg must run (test_helper.exs, PQ_TLS_REQUIRED). + # The 24.04 legs' OTP links OpenSSL 3.0.13, which has no ML-KEM; the box leg's links the + # image's OpenSSL 3.5, so it must complete X25519MLKEM768, SecP384r1MLKEM1024 and + # ML-KEM-1024 handshakes and have OpenSSL's own client name each group. + - { leg: floor, otp: "27", elixir: "1.17", os: ubuntu-24.04, version-type: loose, pq: "" } + - { leg: pinned, otp: "28", elixir: "1.18", os: ubuntu-24.04, version-type: loose, pq: "" } + - { leg: head, otp: "29", elixir: "1.20", os: ubuntu-24.04, version-type: loose, pq: "" } + - { leg: box, otp: "28.5.0.9", elixir: "1.20.4", os: ubuntu-26.04-arm, version-type: strict, pq: "all" } steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: @@ -91,7 +95,7 @@ jobs: # local thresholds, never enforced -- the draw overlaps the ceiling there (tools/gate.sh). # GATE_AUDIT=require: the dependency audit must reach the registry here; an answer from # hex's cache is a failure in CI, where a stale pass would be taken for a fresh one. - - run: GATE_LEG=${{ matrix.leg }} GATE_BENCH=record GATE_AUDIT=require ./tools/gate.sh + - run: PQ_TLS_REQUIRED=${{ matrix.pq }} GATE_LEG=${{ matrix.leg }} GATE_BENCH=record GATE_AUDIT=require ./tools/gate.sh # The pull-request body, read from the event and held to the same terms as the commit # messages (tools/text_terms.sh) -- the one public surface a person writes last, which no diff --git a/CHANGELOG.md b/CHANGELOG.md index dee8e2c..b0379ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,19 @@ All notable changes to this project are documented here. The format follows ## [Unreleased] +### Added: post-quantum TLS, measured (tests and pages; no public entry) + +- `test/beam_mcp/transport/pq_tls_test.exs` serves the HTTP transport from `Bandit` over HTTPS and + completes TLS 1.3 handshakes in which both sides allow only one post-quantum key exchange: + X25519MLKEM768, SecP384r1MLKEM1024 and ML-KEM-1024. It answers an MCP request over each, has a + classical-only server refuse each, and has OpenSSL's `s_client` name the negotiated group. The + groups are found by a real handshake, not `ssl:groups/0`, which in FIPS mode under provider + 3.1.2 lists ML-KEM groups whose handshake then exits. CI's box leg must run all of them; the + FIPS leg's 3.5.9 rows must run the two ML-KEM-1024 groups (in FIPS mode X25519MLKEM768 is not + offered). The README's "Post-quantum TLS, measured" says what a host configures. +- `docs/crypto-posture.md` says the post-quantum signature exists (`beam_mcp_signer` `0.3.0`, + KMS-held keys in `0.4.0`); `docs/fips.md` gains the FIPS-mode measurement. + ## [0.12.0] - 2026-11-30 ### Documented: running on a platform the provider's certificate does not name, and a No-CUI notice diff --git a/README.md b/README.md index c8fa0c4..5354caa 100644 --- a/README.md +++ b/README.md @@ -480,6 +480,39 @@ because HTTP is the carrier that stamps every request modern; stdio's dual-era r and connection limits (your HTTP server's settings, not this Plug's), and authentication: `authorize/1` is where you put it, and it is required precisely so the decision is yours. +**Post-quantum TLS, measured.** TLS is your listener's, through OTP's `ssl`, but this repository +measures what it gives. `test/beam_mcp/transport/pq_tls_test.exs` serves this Plug from `Bandit` +over HTTPS and completes real TLS 1.3 handshakes in which client and server each allow only a +post-quantum key exchange, then answers an MCP request over the connection: + +- **X25519MLKEM768**, the hybrid most clients offer, on OTP 28 linked to OpenSSL 3.5 or later, + outside FIPS mode. OTP's default groups accept it. +- **SecP384r1MLKEM1024 and ML-KEM-1024**, the ML-KEM parameter set CNSA 2.0 names, outside FIPS + mode and in FIPS mode under OpenSSL FIPS provider 3.5.9 (CMVP #5586). In FIPS mode + X25519MLKEM768 is not offered: X25519 is not an approved algorithm. + +Each is held from both sides. A server that allows only a classical group refuses the same +client, and OpenSSL's own `s_client` reports `Negotiated TLS1.3 group:` with the group's name. CI +runs them on the box leg (aarch64, the image's OpenSSL 3.5) and in the FIPS leg under 3.5.9, and +those legs fail rather than skip if they do not run. An OTP linked to OpenSSL 3.0 has no ML-KEM +group. To require post-quantum key exchange, list only those groups in your listener's options: + +```elixir +{Bandit, + plug: {BeamMCP.Transport.HTTP, opts}, + scheme: :https, + thousand_island_options: [ + transport_options: [ + versions: [:"tlsv1.3"], + supported_groups: [:secp384r1mlkem1024, :mlkem1024], + certfile: "...", keyfile: "..." + ] + ]} +``` + +Under FIPS provider 3.1.2 (CMVP #4985, no ML-KEM), OTP still lists the ML-KEM groups, and a +handshake with one exits instead of alerting. Do not configure them there. + ## What it speaks Newline-delimited JSON-RPC over stdio, **dual-era**: it serves both the current revision and diff --git a/docs/crypto-posture.md b/docs/crypto-posture.md index 8160d51..c1d944a 100644 --- a/docs/crypto-posture.md +++ b/docs/crypto-posture.md @@ -64,9 +64,11 @@ the one site that calls it, over the bytes `encode/2` produces, returning the si them and moving no byte. The one implementation here, `BeamMCP.Signer.None`, signs nothing; the reference implementation that does, Ed25519 through OTP's `:crypto` with a key the host hands in under `opts[:private_key]`, is the separate package `beam_mcp_signer`, which a host -attaches; it is never a dependency of this one. A -post-quantum signature (ML-DSA) would be another module behind the same callback and change -nothing here, since the envelope already names its digest. +attaches; it is never a dependency of this one. The post-quantum signature is another module +behind the same callback, as this page said it would be, and changed nothing here: ML-DSA-87 +(FIPS 204, RFC 9964's COSE form) since `beam_mcp_signer` `0.3.0`, with ECDSA P-384 and keys held +in AWS KMS since `0.4.0`. Transport encryption is the host's listener's; what its TLS gives, +post-quantum key exchange included, is measured in the README's "Post-quantum TLS, measured". **It performs no cryptography on a request.** The HTTP transport's `:authorize_body` hook hands the host the body exactly as received so the host can verify a signature over it; the diff --git a/docs/fips.md b/docs/fips.md index e474766..46cd49b 100644 --- a/docs/fips.md +++ b/docs/fips.md @@ -161,3 +161,9 @@ A host under CNSA 2.0 (US National Security Systems) asks for `algorithm: :sha38 `:sha512`: CNSA 2.0 specifies SHA-384 or SHA-512, and the package's default is `:sha256`, which is unchanged. Signatures, their algorithm and where their keys are held belong to the signer behind `BeamMCP.Signer`, not to this package. + +A host's TLS in FIPS mode under provider 3.5.9 completes TLS 1.3 handshakes whose only key +exchange is SecP384r1MLKEM1024 or ML-KEM-1024 (CNSA 2.0's ML-KEM parameter set), measured with +this package's HTTP transport behind `Bandit` (`test/beam_mcp/transport/pq_tls_test.exs`, run by +the FIPS leg). Under 3.1.2 OTP lists those groups too, and a handshake with one exits: configure +them only under a provider that serves ML-KEM. diff --git a/test/beam_mcp/transport/pq_tls_test.exs b/test/beam_mcp/transport/pq_tls_test.exs new file mode 100644 index 0000000..aae71e7 --- /dev/null +++ b/test/beam_mcp/transport/pq_tls_test.exs @@ -0,0 +1,170 @@ +# SPDX-FileCopyrightText: 2026 Sudo Apt Holdings LLC +# SPDX-License-Identifier: Apache-2.0 + +defmodule BeamMCP.Transport.PQTLSTest do + @moduledoc """ + The transport over TLS 1.3 with a post-quantum key exchange, on a real listener: Bandit + serving `BeamMCP.Transport.HTTP` over HTTPS, and a real handshake to it. + + This package does no TLS itself; the host's listener does, through OTP's `ssl`. These tests + measure what that gives, for each key-exchange group the runtime really completes a handshake + with (test_helper.exs asks by trying one): + + * `:pq_tls`: X25519MLKEM768, the hybrid most clients offer (OTP 28 linked to OpenSSL 3.5 or + later, outside FIPS mode); + * `:pq_tls_cnsa`: SecP384r1MLKEM1024 and ML-KEM-1024, the ML-KEM parameter set CNSA 2.0 + names, which also work in FIPS mode under OpenSSL FIPS provider 3.5.9. + + For each group: + + * a TLS 1.3 handshake in which client and server each allow ONLY that group completes, and + an MCP request is answered over it. OTP's `ssl` does not report the negotiated TLS 1.3 + group, so the proof is by construction: the key share is drawn from the groups both sides + allow, and that set is the one group; + * the negative control: the same client against a server that allows only a classical group + (X25519, or P-384 for the CNSA groups) is refused, so the first test cannot pass by a + handshake that ignores the groups; + * an independent client, OpenSSL's `s_client` (an OpenSSL 3.5 or later command; tagged + `:openssl_cli`), reports "Negotiated TLS1.3 group: " against a server that + allows that group. + """ + use ExUnit.Case, async: false + + alias BeamMCP.Test.PQTLS + alias BeamMCP.Transport.HTTP + + @modern "2026-07-28" + + defmodule Catalog do + @behaviour BeamMCP.Catalog + @impl true + def capabilities do + %{ + tools: [ + %BeamMCP.ToolSpec{ + name: :echo, + command_class: :observe, + mode: :read_only, + description: "Echo.", + input_schema: %{ + "type" => "object", + "properties" => %{}, + "additionalProperties" => true + } + } + ], + resources: [], + prompts: [] + } + end + end + + setup_all do + {:ok, _} = Application.ensure_all_started(:ssl) + PQTLS.certs() + end + + # A Bandit HTTPS listener on the loopback, TLS 1.3 only, allowing `groups`. + defp listen(server_tls, groups) do + transport = [versions: [:"tlsv1.3"], supported_groups: groups] ++ server_tls + + plug_opts = [ + catalog: Catalog, + dispatch: fn _n, a, _o -> {:ok, a} end, + authorize: fn _conn -> :ok end, + allowed_origins: :any + ] + + spec = + Supervisor.child_spec( + {Bandit, + plug: {HTTP, plug_opts}, + scheme: :https, + port: 0, + ip: :loopback, + startup_log: false, + thousand_island_options: [transport_options: transport]}, + id: {Bandit, make_ref()} + ) + + pid = start_supervised!(spec) + {:ok, {_address, port}} = ThousandIsland.listener_info(pid) + port + end + + defp connect(port, cacerts, groups), + do: :ssl.connect(~c"127.0.0.1", port, PQTLS.client_opts(cacerts, groups), 5_000) + + # One MCP request over the TLS socket, HTTP/1.1, and the response read to its close. + defp tools_list(socket) do + body = + ~s({"jsonrpc":"2.0","id":1,"method":"tools/list","params":{"_meta":) <> + ~s({"io.modelcontextprotocol/protocolVersion":"#{@modern}",) <> + ~s("io.modelcontextprotocol/clientCapabilities":{}}}}) + + request = + "POST / HTTP/1.1\r\nhost: 127.0.0.1\r\ncontent-type: application/json\r\n" <> + "accept: application/json, text/event-stream\r\nmcp-protocol-version: #{@modern}\r\n" <> + "mcp-method: tools/list\r\nconnection: close\r\ncontent-length: #{byte_size(body)}\r\n\r\n" <> + body + + :ok = :ssl.send(socket, request) + read_all(socket, "") + end + + defp read_all(socket, acc) do + case :ssl.recv(socket, 0, 5_000) do + {:ok, data} -> read_all(socket, acc <> data) + {:error, :closed} -> acc + end + end + + # {tag, the group, OpenSSL's name for it, a classical group for the negative control} + for {tag, group, openssl_name, classical} <- [ + {:pq_tls, :x25519mlkem768, "X25519MLKEM768", :x25519}, + {:pq_tls_cnsa, :secp384r1mlkem1024, "SecP384r1MLKEM1024", :secp384r1}, + {:pq_tls_cnsa, :mlkem1024, "MLKEM1024", :secp384r1} + ] do + describe "#{openssl_name}" do + @describetag tag + + test "both sides allow only #{openssl_name}: TLS 1.3 completes, and MCP is served over it", + %{server: server, cacerts: cacerts} do + port = listen(server, [unquote(group)]) + assert {:ok, socket} = connect(port, cacerts, [unquote(group)]) + assert {:ok, [protocol: :"tlsv1.3"]} = :ssl.connection_information(socket, [:protocol]) + + response = tools_list(socket) + assert response =~ ~r{\AHTTP/1\.1 200 }, response + assert response =~ ~s("name":"echo") + end + + test "the negative control: a server that allows only #{classical} refuses a client offering only #{openssl_name}", + %{server: server, cacerts: cacerts} do + port = listen(server, [unquote(classical)]) + assert {:error, _alert} = connect(port, cacerts, [unquote(group)]) + end + + @tag :openssl_cli + test "an independent client, OpenSSL's s_client, negotiates #{openssl_name}", + %{server: server} do + port = listen(server, [unquote(group)]) + openssl = System.get_env("OPENSSL_CLI") || System.find_executable("openssl") + + # stdin from /dev/null, so s_client completes the handshake, prints it, and exits. + {out, _status} = + System.cmd( + "sh", + [ + "-c", + ~s("$0" s_client -connect 127.0.0.1:#{port} -tls1_3 -groups #{unquote(openssl_name)} -brief + result = + try do + {:ok, transport} = :ssl.transport_accept(listen, 5_000) + :ssl.handshake(transport, 5_000) + catch + kind, reason -> {kind, reason} + end + + send(parent, {:pq_server, result}) + end) + + client = + try do + :ssl.connect(~c"127.0.0.1", port, client_opts(cacerts, groups), 5_000) + catch + kind, reason -> {kind, reason} + end + + server_result = + receive do + {:pq_server, r} -> r + after + 6_000 -> :timeout + end + + Process.exit(acceptor, :kill) + :ssl.close(listen) + match?({:ok, _}, client) and match?({:ok, _}, server_result) + catch + _kind, _reason -> false + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs index 45a85db..4033ea1 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -6,7 +6,56 @@ # once the reload tests landed). Under cover those tests are excluded -- the gate runs the # plain suite, where they run -- so the coverage record keeps the modules they reload. cover? = Code.ensure_loaded?(:cover) and :cover.modules() != [] -ExUnit.start(exclude: if(cover?, do: [:hot_reload], else: [])) +# Post-quantum TLS (test/beam_mcp/transport/pq_tls_test.exs) runs for each key-exchange group this +# runtime really completes a TLS 1.3 handshake with, asked by trying one on the loopback: not by +# `:ssl.groups/0`, which in FIPS mode under a provider without ML-KEM (3.1.2) still lists the ML-KEM +# groups, and the handshake then exits (measured 2026-11-30). `:pq_tls` is X25519MLKEM768 (OTP 28 +# linked to OpenSSL 3.5 or later, outside FIPS mode: X25519 is not an approved algorithm); +# `:pq_tls_cnsa` is SecP384r1MLKEM1024 and ML-KEM-1024, CNSA 2.0's ML-KEM parameter set, which +# also work in FIPS mode under provider 3.5.9. With PQ_TLS_REQUIRED=cnsa (the FIPS leg's 3.5.9 +# rows) or =all (CI's box leg: both, and the OpenSSL command too), a runtime without them is +# refused here, so those legs cannot pass by excluding the tests. +{:ok, _} = Application.ensure_all_started(:ssl) +pq_works = &BeamMCP.Test.PQTLS.handshake?/1 +pq_tls? = pq_works.([:x25519mlkem768]) +pq_tls_cnsa? = pq_works.([:secp384r1mlkem1024]) and pq_works.([:mlkem1024]) + +pq_required = System.get_env("PQ_TLS_REQUIRED", "") + +if pq_required in ["cnsa", "all"] and not pq_tls_cnsa?, + do: + raise( + "PQ_TLS_REQUIRED=#{pq_required} and ML-KEM-1024 does not complete a handshake here: refused" + ) + +if pq_required == "all" and not pq_tls?, + do: raise("PQ_TLS_REQUIRED=all and X25519MLKEM768 does not complete a handshake here: refused") + +# The independent check through OpenSSL's own client needs an `openssl` command of 3.5 or later +# (OPENSSL_CLI, or the first `openssl` on the path). +openssl_cli? = + with path when is_binary(path) <- + System.get_env("OPENSSL_CLI") || System.find_executable("openssl"), + {"OpenSSL " <> version, 0} <- System.cmd(path, ["version"], stderr_to_stdout: true), + [major, minor | _] <- + version |> String.split([".", " "]) |> Enum.take(2) |> Enum.map(&Integer.parse/1), + {ma, _} <- major, + {mi, _} <- minor do + ma > 3 or (ma == 3 and mi >= 5) + else + _ -> false + end + +if pq_required == "all" and not openssl_cli?, + do: raise("PQ_TLS_REQUIRED=all and no OpenSSL 3.5 or later command is found: refused") + +ExUnit.start( + exclude: + if(cover?, do: [:hot_reload], else: []) ++ + if(pq_tls?, do: [], else: [:pq_tls]) ++ + if(pq_tls_cnsa?, do: [], else: [:pq_tls_cnsa]) ++ + if(openssl_cli?, do: [], else: [:openssl_cli]) +) # The gate's `properties` step runs the property tests alone at a generation count it sets # through PROPERTY_RUNS (tools/gate.sh); the plain suite keeps StreamData's default of 100.