From 34d134198c6e6de302a7d47e3d4e5173f3e5c917 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 1 Dec 2026 22:37:00 -0600 Subject: [PATCH 1/7] keywrap: an empty TLS key passphrase counts as none given load_checked_cert_chain treated password="" as a given passphrase, so an encrypted key with an empty passphrase (a Secret or env var set to "") went past the key-wrap refusal into OpenSSL and failed with a raw SSL error. It now normalises "" and b"" to None before the check, so that key gets the clean "no passphrase is configured" refusal, and an unencrypted key with "" still loads. BACKLOG #6182 (code limb). Verified-by: tests --- messagefoundry/keywrap.py | 8 ++++++- tests/test_keywrap.py | 44 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/messagefoundry/keywrap.py b/messagefoundry/keywrap.py index 4aadef090c..907e9de172 100644 --- a/messagefoundry/keywrap.py +++ b/messagefoundry/keywrap.py @@ -1374,7 +1374,13 @@ def load_checked_cert_chain( The check and the load are one call, so a site cannot keep one and drop the other. OpenSSL reads the file a second time, and a file swapped between the two reads is not caught by the check; the empty-passphrase callback passed for ``password=None`` is what keeps a swapped-in - encrypted key off a terminal prompt then.""" + encrypted key off a terminal prompt then. + + An empty ``password`` counts as none given (BACKLOG #6182). A Kubernetes Secret or an env var + set to ``""`` means "no passphrase", and passing ``""`` on would let an encrypted key past the + refusal above to fail inside OpenSSL with a raw SSL error instead of the clean refusal.""" + if not password: + password = None refuse_weak_cert_chain_key( certfile, keyfile, diff --git a/tests/test_keywrap.py b/tests/test_keywrap.py index 42a7df98b8..aa96bf7a9b 100644 --- a/tests/test_keywrap.py +++ b/tests/test_keywrap.py @@ -331,6 +331,50 @@ def test_an_encrypted_key_with_no_passphrase_never_reaches_openssl( assert spy_load_cert_chain == [] +@pytest.mark.parametrize("empty", ["", b""]) +def test_an_empty_passphrase_counts_as_none_given( + tmp_path: Path, material: Material, empty: str | bytes, spy_load_cert_chain: list[object] +) -> None: + # BACKLOG #6182: a Secret or env var set to "" means no passphrase. Before the fix "" counted + # as given, so an encrypted key went past the refusal into OpenSSL. + combined = _write(tmp_path, "both.pem", material.cert_pem + material.wraps["approved"]) + with pytest.raises(KeyWrapRefused, match="no passphrase is configured"): + keywrap.load_checked_cert_chain( + ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER), + combined, + None, + empty, + cert_setting="cert", + key_setting="key", + unlock_setting="key password", + ) + assert spy_load_cert_chain == [] + + +@pytest.mark.parametrize("site", sorted(_SSL_WITH_PASSPHRASE)) +def test_an_ssl_loader_refuses_an_encrypted_key_with_an_empty_passphrase( + tmp_path: Path, material: Material, site: str, spy_load_cert_chain: list[object] +) -> None: + combined = _write(tmp_path, "both.pem", material.cert_pem + material.wraps["approved"]) + with pytest.raises(KeyWrapRefused, match="no passphrase is configured"): + _SSL_WITH_PASSPHRASE[site](combined, None, "") + assert spy_load_cert_chain == [] + + +@pytest.mark.parametrize("site", sorted(_SSL_WITH_PASSPHRASE)) +def test_an_ssl_loader_still_loads_an_unencrypted_key_with_an_empty_passphrase( + tmp_path: Path, material: Material, site: str +) -> None: + plain = material.key.private_bytes( + serialization.Encoding.PEM, + serialization.PrivateFormat.PKCS8, + serialization.NoEncryption(), + ) + cert = _write(tmp_path, "cert.pem", material.cert_pem) + key = _write(tmp_path, "key.pem", plain) + assert _SSL_WITH_PASSPHRASE[site](cert, key, "") is not None + + @pytest.mark.parametrize("site", sorted(_SSL_WITHOUT_PASSPHRASE)) def test_a_passphrase_less_loader_refuses_a_weak_wrap( tmp_path: Path, material: Material, site: str, spy_load_cert_chain: list[object] From c0eed3139003bb9cd9f62be434f781fecc795f12 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 1 Dec 2026 22:44:30 -0600 Subject: [PATCH 2/7] k8s secret example: load the store key and passwords from files The example kubectl command put the store encryption key and the database password on its command line with --from-literal, where they sit in the process list while kubectl runs and in shell history, and it passed an empty api-tls-key-password literal. It now reads each secret with --from-file from a file the operator writes in a private directory (read -rs plus the printf builtin, gen-key piped through tr), drops the empty passphrase, and says to delete the files afterwards. Only the non-secret tls-revocation-attested stays a literal. CLOUD-DEPLOYMENT.md step 3 points at the same rule, and the example-key test now pins --from-file for every secret key. BACKLOG #6182 (doc limb). Verified-by: tests --- docker/k8s/secret.example.yaml | 27 ++++++++++++++++++---- docs/CLOUD-DEPLOYMENT.md | 4 +++- tests/test_b532w3_k8s_attestation_check.py | 9 +++++++- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/docker/k8s/secret.example.yaml b/docker/k8s/secret.example.yaml index 5bd561650a..469ef78f12 100644 --- a/docker/k8s/secret.example.yaml +++ b/docker/k8s/secret.example.yaml @@ -6,21 +6,38 @@ # cert/key from local files (never inline). Notes on the literals (kept OUT of the command so it is # copy-paste-safe — every continued line ends in a backslash with NO trailing inline comment, so nothing # is silently dropped): -# - api-tls-key-password: leave "" (or omit the flag) if the API key is unencrypted. +# - api-tls-key-password: omit the flag if the API key is unencrypted. Never pass an empty value. # - store-password: REQUIRED ONLY for the HA manifest (external Postgres / SQL Server); omit it for # the single-node SQLite statefulset.yaml. # - tls-revocation-attested: your API revocation attestation (docs/SECURITY.md, API revocation gate). # Both manifests require the key; without it the kubelet refuses the # pod (CreateContainerConfigError, at the first container reading it). Set 1 only if your PKI -# enforces revocation. +# enforces revocation. It is not a secret, so it is the one --from-literal. +# +# The store key and the passwords go in through files you write first, never as --from-literal: a +# value on a command line is readable by other local users in the process list while kubectl runs, +# and stays in your shell history. Write the files in a private directory, with no trailing +# newline (--from-file keeps every byte, so a trailing newline would become part of the value), +# and delete them once the Secret exists. In bash: +# +# umask 077 && mkdir -p secrets +# messagefoundry gen-key | tr -d '\n' > secrets/store-encryption-key +# read -rs -p 'store password: ' v && printf '%s' "$v" > secrets/store-password; unset v; echo +# read -rs -p 'API key passphrase: ' v && printf '%s' "$v" > secrets/api-tls-key-password; unset v; echo +# +# (read -rs keeps the value off the screen and out of history, and printf is a shell builtin, so +# the value never appears in a process's arguments.) Skip the store-password line for the +# single-node manifest, and the passphrase line if the API key is unencrypted, and drop the +# matching --from-file flag below too. # # kubectl create secret generic mefor-secrets \ # --from-file=api.crt=tls/api.crt \ # --from-file=api.key=tls/api.key \ -# --from-literal=store-encryption-key="$(messagefoundry gen-key)" \ -# --from-literal=api-tls-key-password="" \ -# --from-literal=store-password="..." \ +# --from-file=store-encryption-key=secrets/store-encryption-key \ +# --from-file=api-tls-key-password=secrets/api-tls-key-password \ +# --from-file=store-password=secrets/store-password \ # --from-literal=tls-revocation-attested="..." +# rm -f secrets/store-encryption-key secrets/store-password secrets/api-tls-key-password # # The store key is base64 of 32 random bytes (mint with `messagefoundry gen-key`). The MLLP key must be # UNENCRYPTED unless the MLLP() connection sets tls_key_password (env-sourced). diff --git a/docs/CLOUD-DEPLOYMENT.md b/docs/CLOUD-DEPLOYMENT.md index 7f6de189a9..4526fe7c16 100644 --- a/docs/CLOUD-DEPLOYMENT.md +++ b/docs/CLOUD-DEPLOYMENT.md @@ -371,7 +371,9 @@ and the engine's at-rest encryption does not cover them ([`PHI.md`](PHI.md) §2, *Residuals / gotchas* in [`docker/README.md`](../docker/README.md#residuals--gotchas). 3. **Create the Secret** ([`docker/k8s/secret.example.yaml`](../docker/k8s/secret.example.yaml)) with `store-encryption-key`, `store-password`, `tls-revocation-attested`, and the API TLS cert/key (and - optional `api-tls-key-password`). Both manifests, `ha-postgres.yaml` and `statefulset.yaml`, read + optional `api-tls-key-password`). Load the store key and the passwords with `--from-file`, never + `--from-literal`, which would put them in the process list and your shell history; the example's + header shows how to write those files. Both manifests, `ha-postgres.yaml` and `statefulset.yaml`, read `MEFOR_TLS_REVOCATION_ATTESTED` from the `tls-revocation-attested` key, with no default and not optional, so without it the kubelet refuses each pod with `CreateContainerConfigError`, at the first container that reads it. The attestation is yours diff --git a/tests/test_b532w3_k8s_attestation_check.py b/tests/test_b532w3_k8s_attestation_check.py index 7750cf4e44..f18f31adc1 100644 --- a/tests/test_b532w3_k8s_attestation_check.py +++ b/tests/test_b532w3_k8s_attestation_check.py @@ -141,5 +141,12 @@ def test_the_example_secret_still_names_every_key() -> None: if key in {"api.crt", "api.key"}: assert f"--from-file={key}=" in text, key continue - assert f"--from-literal={key}=" in text, key + if key == "tls-revocation-attested": + # Not a secret, so the one literal the example command keeps. + assert f"--from-literal={key}=" in text, key + else: + # BACKLOG #6182: a secret value on a command line sits in the process list and in + # shell history, so the example reads it from a file the operator writes. + assert f"--from-file={key}=" in text, key + assert f"--from-literal={key}=" not in text, key assert f' # {key}: "REPLACE' in text, f"no commented REPLACE entry for {key}" From 430dbe975a6a4e01d825048a0c1f325508cccd70 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 1 Dec 2026 23:10:54 -0600 Subject: [PATCH 3/7] keywrap: an empty passphrase refusal says the setting is empty Review round 1 on the previous commit: a key wrapped under an empty passphrase used to load with "" and is now refused, and the refusal only said "Set ", which the operator had done. When the passphrase given was "", the no-passphrase refusal now adds that the setting is empty and counts as none, and to give the real passphrase or re-wrap the key under a non-empty one. A test pins that key's flip from load to refusal, with a control that OpenSSL does open it with "". The check uses bool(password) rather than reassigning the argument, and the docstring no longer claims "" always failed in OpenSSL. This is a fail-closed tightening: a key wrapped under an empty passphrase, which loaded before, is refused. BACKLOG #6182 (code limb, review repair). Verified-by: tests --- messagefoundry/keywrap.py | 50 ++++++++++++++++++++++++++------------- tests/test_keywrap.py | 40 +++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 16 deletions(-) diff --git a/messagefoundry/keywrap.py b/messagefoundry/keywrap.py index 907e9de172..aacb8b9a9e 100644 --- a/messagefoundry/keywrap.py +++ b/messagefoundry/keywrap.py @@ -1275,12 +1275,25 @@ def key_wrap_refusal( if encrypted and not passphrase_given: fix = _DECRYPT if unlock_setting is None else f"Set {unlock_setting}" return ( - f"{setting}: the private key is encrypted and no passphrase is configured for it. It " - f"is refused before any library can try to open it. {fix}" + f"{setting}: {_NO_PASSPHRASE_REFUSAL}. It is refused before any library can try to " + f"open it. {fix}" ) return None +#: The heart of :func:`key_wrap_refusal`'s text for an encrypted key given no passphrase. +_NO_PASSPHRASE_REFUSAL: Final = ( + "the private key is encrypted and no passphrase is configured for it" +) + +#: What :func:`load_checked_cert_chain` adds to that refusal when the passphrase it was given was +#: empty, so an operator who did set the setting, to ``""``, is not told only to set it. +_EMPTY_PASSPHRASE_NOTE: Final = ( + "The passphrase setting is empty, and an empty passphrase counts as none: give the real " + "passphrase, or re-wrap a key encrypted under an empty passphrase under a non-empty one" +) + + def refuse_weak_key_wrap( material: bytes, *, @@ -1376,20 +1389,25 @@ def load_checked_cert_chain( check; the empty-passphrase callback passed for ``password=None`` is what keeps a swapped-in encrypted key off a terminal prompt then. - An empty ``password`` counts as none given (BACKLOG #6182). A Kubernetes Secret or an env var - set to ``""`` means "no passphrase", and passing ``""`` on would let an encrypted key past the - refusal above to fail inside OpenSSL with a raw SSL error instead of the clean refusal.""" - if not password: - password = None - refuse_weak_cert_chain_key( - certfile, - keyfile, - cert_setting=cert_setting, - key_setting=key_setting, - unlock_setting=unlock_setting, - passphrase_given=password is not None, - ) - ctx.load_cert_chain(certfile, keyfile, password if password is not None else _no_passphrase) + An empty ``password`` counts as none given (BACKLOG #6182): a Kubernetes Secret or an env var + set to ``""`` reads as "no passphrase". So an encrypted key with ``""`` gets the wrap check's + no-passphrase refusal rather than a raw OpenSSL error on a wrong passphrase. A key wrapped + under an empty passphrase, which OpenSSL would load with ``""``, is refused the same way, and + the refusal says so: its wrap protects nothing.""" + try: + refuse_weak_cert_chain_key( + certfile, + keyfile, + cert_setting=cert_setting, + key_setting=key_setting, + unlock_setting=unlock_setting, + passphrase_given=bool(password), + ) + except KeyWrapRefused as exc: + if password is None or password or _NO_PASSPHRASE_REFUSAL not in str(exc): + raise + raise KeyWrapRefused(f"{exc}. {_EMPTY_PASSPHRASE_NOTE}") from None + ctx.load_cert_chain(certfile, keyfile, password or _no_passphrase) def load_connection_cert_chain( diff --git a/tests/test_keywrap.py b/tests/test_keywrap.py index aa96bf7a9b..b0c4767a21 100644 --- a/tests/test_keywrap.py +++ b/tests/test_keywrap.py @@ -351,6 +351,46 @@ def test_an_empty_passphrase_counts_as_none_given( assert spy_load_cert_chain == [] +def test_a_key_wrapped_under_an_empty_passphrase_is_refused_and_says_why( + tmp_path: Path, material: Material +) -> None: + # The one input whose outcome #6182 flips: OpenSSL loads this key with "", and before the fix + # so did load_checked_cert_chain. Its wrap protects nothing, so it is now refused, and the text + # says the setting is empty rather than only "Set it". + wrapped = pkcs8_pem(material.key, "", prf="sha256", iterations=600_000) + combined = _write(tmp_path, "both.pem", material.cert_pem + wrapped) + # Control: the fixture is a real encrypted key OpenSSL opens with the empty passphrase. + ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER).load_cert_chain(combined, None, "") + with pytest.raises(KeyWrapRefused, match="no passphrase is configured") as caught: + keywrap.load_checked_cert_chain( + ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER), + combined, + None, + "", + cert_setting="cert", + key_setting="key", + unlock_setting="key password", + ) + assert "The passphrase setting is empty" in str(caught.value) + + +def test_a_missing_passphrase_refusal_carries_no_empty_setting_note( + tmp_path: Path, material: Material, spy_load_cert_chain: list[object] +) -> None: + combined = _write(tmp_path, "both.pem", material.cert_pem + material.wraps["approved"]) + with pytest.raises(KeyWrapRefused, match="no passphrase is configured") as caught: + keywrap.load_checked_cert_chain( + ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER), + combined, + None, + None, + cert_setting="cert", + key_setting="key", + unlock_setting="key password", + ) + assert "The passphrase setting is empty" not in str(caught.value) + + @pytest.mark.parametrize("site", sorted(_SSL_WITH_PASSPHRASE)) def test_an_ssl_loader_refuses_an_encrypted_key_with_an_empty_passphrase( tmp_path: Path, material: Material, site: str, spy_load_cert_chain: list[object] From 826cf6162c73e52d79ea5a386e3f7c6c269d59be Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 1 Dec 2026 23:15:27 -0600 Subject: [PATCH 4/7] k8s secret example: harden the file recipe; document the empty passphrase Review round 1 on the doc limb. The recipe now runs in a subshell, so the umask and shell options end with it, with set -euo pipefail and a test -s so a failed gen-key stops it rather than leaving an empty key file; chmod 700 tightens a secrets directory left from an earlier run; tr strips the carriage return Python writes to a pipe on Windows; IFS= keeps leading and trailing spaces in a password; and the files are removed only if kubectl succeeds. The api-tls-key-password note says an empty value counts as no passphrase, matching docker/secrets.env.example and the code. CONNECTIONS.md's wrap-floor list says the TLS loaders treat an empty passphrase setting as none. BACKLOG #6182 (doc limb, review repair). Proposed PR title: k8s secret example loads secrets from files; an empty TLS key passphrase counts as none (BACKLOG #6182) Proposed vault banner: BUILT on branch b20056-6182, awaiting PR. The example kubectl command reads the store key and passwords with --from-file from operator-written files; keywrap.load_checked_cert_chain treats an empty passphrase as none given, so an encrypted key with "" gets the clean key-wrap refusal (a key wrapped under an empty passphrase, which loaded before, is now refused: a fail-closed tightening). Verified-by: tests --- docker/k8s/secret.example.yaml | 30 ++++++++++++++++++------------ docs/CONNECTIONS.md | 5 ++++- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/docker/k8s/secret.example.yaml b/docker/k8s/secret.example.yaml index 469ef78f12..d99bad5612 100644 --- a/docker/k8s/secret.example.yaml +++ b/docker/k8s/secret.example.yaml @@ -6,7 +6,8 @@ # cert/key from local files (never inline). Notes on the literals (kept OUT of the command so it is # copy-paste-safe — every continued line ends in a backslash with NO trailing inline comment, so nothing # is silently dropped): -# - api-tls-key-password: omit the flag if the API key is unencrypted. Never pass an empty value. +# - api-tls-key-password: omit the flag if the API key is unencrypted. An empty value counts as +# no passphrase, so it adds nothing. # - store-password: REQUIRED ONLY for the HA manifest (external Postgres / SQL Server); omit it for # the single-node SQLite statefulset.yaml. # - tls-revocation-attested: your API revocation attestation (docs/SECURITY.md, API revocation gate). @@ -18,17 +19,22 @@ # value on a command line is readable by other local users in the process list while kubectl runs, # and stays in your shell history. Write the files in a private directory, with no trailing # newline (--from-file keeps every byte, so a trailing newline would become part of the value), -# and delete them once the Secret exists. In bash: +# and delete them once the Secret exists. In bash, in a subshell so the umask and the shell options +# end with it: # -# umask 077 && mkdir -p secrets -# messagefoundry gen-key | tr -d '\n' > secrets/store-encryption-key -# read -rs -p 'store password: ' v && printf '%s' "$v" > secrets/store-password; unset v; echo -# read -rs -p 'API key passphrase: ' v && printf '%s' "$v" > secrets/api-tls-key-password; unset v; echo +# ( +# set -euo pipefail; umask 077; mkdir -p secrets; chmod 700 secrets +# messagefoundry gen-key | tr -d '\r\n' > secrets/store-encryption-key +# test -s secrets/store-encryption-key +# IFS= read -rs -p 'store password: ' v; echo; printf '%s' "$v" > secrets/store-password +# IFS= read -rs -p 'API key passphrase: ' v; echo; printf '%s' "$v" > secrets/api-tls-key-password +# ) # -# (read -rs keeps the value off the screen and out of history, and printf is a shell builtin, so -# the value never appears in a process's arguments.) Skip the store-password line for the -# single-node manifest, and the passphrase line if the API key is unencrypted, and drop the -# matching --from-file flag below too. +# (IFS= read -rs keeps the value whole, off the screen and out of history; printf is a shell +# builtin, so the value never appears in a process's arguments; tr also strips the carriage return +# Windows adds; pipefail and test -s stop on a gen-key that failed.) Skip the store-password line +# for the single-node manifest, and the passphrase line if the API key is unencrypted, and drop the +# matching --from-file flag and rm path below too. The files are deleted only if kubectl succeeds. # # kubectl create secret generic mefor-secrets \ # --from-file=api.crt=tls/api.crt \ @@ -36,8 +42,8 @@ # --from-file=store-encryption-key=secrets/store-encryption-key \ # --from-file=api-tls-key-password=secrets/api-tls-key-password \ # --from-file=store-password=secrets/store-password \ -# --from-literal=tls-revocation-attested="..." -# rm -f secrets/store-encryption-key secrets/store-password secrets/api-tls-key-password +# --from-literal=tls-revocation-attested="..." \ +# && rm -f secrets/store-encryption-key secrets/store-password secrets/api-tls-key-password # # The store key is base64 of 32 random bytes (mint with `messagefoundry gen-key`). The MLLP key must be # UNENCRYPTED unless the MLLP() connection sets tls_key_password (env-sourced). diff --git a/docs/CONNECTIONS.md b/docs/CONNECTIONS.md index c6e2f540d7..86203bcda9 100644 --- a/docs/CONNECTIONS.md +++ b/docs/CONNECTIONS.md @@ -4346,7 +4346,10 @@ scrypt passes at its Appendix C floor. - PBKDF2 under its floor. **The common tools write 2048 iterations by default**: `openssl req` without `-nodes`, `openssl genpkey -aes256`, and `cryptography`'s `BestAvailableEncryption`. A key made any of those ways is refused until you re-wrap it; -- an encrypted key where the loader has no passphrase to give, before any library can prompt. +- an encrypted key where the loader has no passphrase to give, before any library can prompt. At + the TLS loaders an empty passphrase setting (`""`) counts as none, so an encrypted key with an + empty `tls_key_password` or `MEFOR_API_TLS_KEY_PASSWORD` is refused here too, including one + wrapped under an empty passphrase. **Re-wrap a PEM or PKCS#8 key** at the floor, then point the setting at the new file: From d8a1e22843a23addb19942198e38961277a30716 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Wed, 2 Dec 2026 00:16:12 -0600 Subject: [PATCH 5/7] keywrap: build the empty-passphrase advice where the refusal is made Review round 2. The previous commit caught its own refusal, matched its text and re-raised it from None, which the from-None gate refuses and which was fragile. key_wrap_refusal and the three refuse_weak_* helpers now take empty_passphrase, and the refusal names " is set but empty" itself: on the no-passphrase branch instead of "Set ", and after the re-wrap advice on a weak wrap, so following that advice does not lead to a second refusal. The docstring now says the empty-passphrase callback is passed for "" too. Tests pin the note at every site loader and on the weak-wrap path. BACKLOG #6182 (code limb, review repair round 2). Verified-by: tests --- messagefoundry/keywrap.py | 74 ++++++++++++++++++++++----------------- tests/test_keywrap.py | 26 ++++++++++++-- 2 files changed, 64 insertions(+), 36 deletions(-) diff --git a/messagefoundry/keywrap.py b/messagefoundry/keywrap.py index aacb8b9a9e..3c374bfc26 100644 --- a/messagefoundry/keywrap.py +++ b/messagefoundry/keywrap.py @@ -1248,14 +1248,17 @@ def key_wrap_refusal( setting: str, unlock_setting: str | None, passphrase_given: bool, + empty_passphrase: bool = False, ) -> str | None: """The refusal text for ``material``, or ``None`` when every key in it may be loaded. ``setting`` names where the key came from, for the message. ``unlock_setting`` names what carries its passphrase, in the words an operator would use to set it, or is ``None`` for a loader that takes no passphrase at all. ``passphrase_given`` says whether the loader is about to - pass one. The text holds no key bytes, passphrase or path, so a caller may raise it as its own - error type.""" + pass one. ``empty_passphrase`` says the loader was given an empty one, which counts as none, + so the advice names the empty setting rather than only telling the operator to set it + (BACKLOG #6182). The text holds no key bytes, passphrase or path, so a caller may raise it as + its own error type.""" if _too_large(material): return ( f"{setting}: the key file is over {_MAX_KEY_FILE_BYTES} bytes, too large to check its " @@ -1271,27 +1274,30 @@ def key_wrap_refusal( fix = _REENCODE else: fix = _REWRAP + if empty_passphrase: + fix = f"{fix}. {_empty_setting_note(unlock_setting)}" return f"{setting}: the private key {problem}. It is refused. {fix}" if encrypted and not passphrase_given: - fix = _DECRYPT if unlock_setting is None else f"Set {unlock_setting}" + if unlock_setting is None: + fix = _DECRYPT + elif empty_passphrase: + fix = _empty_setting_note(unlock_setting) + else: + fix = f"Set {unlock_setting}" return ( - f"{setting}: {_NO_PASSPHRASE_REFUSAL}. It is refused before any library can try to " - f"open it. {fix}" + f"{setting}: the private key is encrypted and no passphrase is configured for it. It " + f"is refused before any library can try to open it. {fix}" ) return None -#: The heart of :func:`key_wrap_refusal`'s text for an encrypted key given no passphrase. -_NO_PASSPHRASE_REFUSAL: Final = ( - "the private key is encrypted and no passphrase is configured for it" -) - -#: What :func:`load_checked_cert_chain` adds to that refusal when the passphrase it was given was -#: empty, so an operator who did set the setting, to ``""``, is not told only to set it. -_EMPTY_PASSPHRASE_NOTE: Final = ( - "The passphrase setting is empty, and an empty passphrase counts as none: give the real " - "passphrase, or re-wrap a key encrypted under an empty passphrase under a non-empty one" -) +def _empty_setting_note(unlock_setting: str) -> str: + """The advice for a key whose passphrase setting is set to ``""``, which counts as none.""" + return ( + f"{unlock_setting} is set but empty, and an empty passphrase counts as none: set it to the " + "key's passphrase, and wrap a key that has none, or one encrypted under an empty " + "passphrase, under a non-empty one" + ) def refuse_weak_key_wrap( @@ -1300,6 +1306,7 @@ def refuse_weak_key_wrap( setting: str, unlock_setting: str | None, passphrase_given: bool, + empty_passphrase: bool = False, ) -> None: """Raise :class:`KeyWrapRefused` when :func:`key_wrap_refusal` refuses ``material``.""" reason = key_wrap_refusal( @@ -1307,6 +1314,7 @@ def refuse_weak_key_wrap( setting=setting, unlock_setting=unlock_setting, passphrase_given=passphrase_given, + empty_passphrase=empty_passphrase, ) if reason is not None: raise KeyWrapRefused(reason) @@ -1318,6 +1326,7 @@ def refuse_weak_key_file( setting: str, unlock_setting: str | None, passphrase_given: bool, + empty_passphrase: bool = False, ) -> None: """Read the key file at ``path`` and refuse it as :func:`refuse_weak_key_wrap` does. @@ -1343,6 +1352,7 @@ def refuse_weak_key_file( setting=setting, unlock_setting=unlock_setting, passphrase_given=passphrase_given, + empty_passphrase=empty_passphrase, ) @@ -1354,6 +1364,7 @@ def refuse_weak_cert_chain_key( key_setting: str, unlock_setting: str | None, passphrase_given: bool, + empty_passphrase: bool = False, ) -> None: """Check the key an ``ssl.SSLContext.load_cert_chain(certfile, keyfile, ...)`` call would load. @@ -1364,6 +1375,7 @@ def refuse_weak_cert_chain_key( setting=key_setting if keyfile else cert_setting, unlock_setting=unlock_setting, passphrase_given=passphrase_given, + empty_passphrase=empty_passphrase, ) @@ -1386,27 +1398,23 @@ def load_checked_cert_chain( The check and the load are one call, so a site cannot keep one and drop the other. OpenSSL reads the file a second time, and a file swapped between the two reads is not caught by the - check; the empty-passphrase callback passed for ``password=None`` is what keeps a swapped-in - encrypted key off a terminal prompt then. + check; the empty-passphrase callback, passed when ``password`` is ``None`` or empty, is what + keeps a swapped-in encrypted key off a terminal prompt then. An empty ``password`` counts as none given (BACKLOG #6182): a Kubernetes Secret or an env var set to ``""`` reads as "no passphrase". So an encrypted key with ``""`` gets the wrap check's no-passphrase refusal rather than a raw OpenSSL error on a wrong passphrase. A key wrapped - under an empty passphrase, which OpenSSL would load with ``""``, is refused the same way, and - the refusal says so: its wrap protects nothing.""" - try: - refuse_weak_cert_chain_key( - certfile, - keyfile, - cert_setting=cert_setting, - key_setting=key_setting, - unlock_setting=unlock_setting, - passphrase_given=bool(password), - ) - except KeyWrapRefused as exc: - if password is None or password or _NO_PASSPHRASE_REFUSAL not in str(exc): - raise - raise KeyWrapRefused(f"{exc}. {_EMPTY_PASSPHRASE_NOTE}") from None + under an empty passphrase, which OpenSSL would load with ``""``, is refused the same way: its + wrap protects nothing. Either refusal names the empty setting.""" + refuse_weak_cert_chain_key( + certfile, + keyfile, + cert_setting=cert_setting, + key_setting=key_setting, + unlock_setting=unlock_setting, + passphrase_given=bool(password), + empty_passphrase=password is not None and not password, + ) ctx.load_cert_chain(certfile, keyfile, password or _no_passphrase) diff --git a/tests/test_keywrap.py b/tests/test_keywrap.py index b0c4767a21..f830cfd45a 100644 --- a/tests/test_keywrap.py +++ b/tests/test_keywrap.py @@ -371,7 +371,7 @@ def test_a_key_wrapped_under_an_empty_passphrase_is_refused_and_says_why( key_setting="key", unlock_setting="key password", ) - assert "The passphrase setting is empty" in str(caught.value) + assert "is set but empty" in str(caught.value) def test_a_missing_passphrase_refusal_carries_no_empty_setting_note( @@ -388,7 +388,7 @@ def test_a_missing_passphrase_refusal_carries_no_empty_setting_note( key_setting="key", unlock_setting="key password", ) - assert "The passphrase setting is empty" not in str(caught.value) + assert "is set but empty" not in str(caught.value) @pytest.mark.parametrize("site", sorted(_SSL_WITH_PASSPHRASE)) @@ -396,9 +396,29 @@ def test_an_ssl_loader_refuses_an_encrypted_key_with_an_empty_passphrase( tmp_path: Path, material: Material, site: str, spy_load_cert_chain: list[object] ) -> None: combined = _write(tmp_path, "both.pem", material.cert_pem + material.wraps["approved"]) - with pytest.raises(KeyWrapRefused, match="no passphrase is configured"): + with pytest.raises(KeyWrapRefused, match="no passphrase is configured") as caught: _SSL_WITH_PASSPHRASE[site](combined, None, "") assert spy_load_cert_chain == [] + assert "is set but empty" in str(caught.value), site + + +def test_a_weak_wrap_with_an_empty_passphrase_names_the_empty_setting( + tmp_path: Path, material: Material, spy_load_cert_chain: list[object] +) -> None: + # Re-wrap advice alone would send the operator to a second refusal for the empty setting. + combined = _write(tmp_path, "both.pem", material.cert_pem + _legacy_pem(material.key)) + with pytest.raises(KeyWrapRefused, match="legacy OpenSSL PEM encryption") as caught: + keywrap.load_checked_cert_chain( + ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER), + combined, + None, + "", + cert_setting="cert", + key_setting="key", + unlock_setting="key password", + ) + assert "key password is set but empty" in str(caught.value) + assert spy_load_cert_chain == [] @pytest.mark.parametrize("site", sorted(_SSL_WITH_PASSPHRASE)) From b4ebf0e6b07644d6bd883397966e585422d3b629 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Wed, 2 Dec 2026 00:17:14 -0600 Subject: [PATCH 6/7] docs: empty TLS key passphrase remedy, changelog, secret recipe notes Review round 2 on the doc limb. CONNECTIONS.md gives the remedy for a key refused over an empty passphrase setting (the real passphrase, a re-wrap under a non-empty one, or an unencrypted key) and says why it differs from the PKCS#12 rule. A changelog.d fragment records the tightening and the example Secret change. The example says the recipe is for bash on Linux or macOS, or WSL (Git Bash does not set Windows file permissions, and zsh reads read -p differently), to escrow the store key before the files go, and what to do if kubectl fails. CLOUD-DEPLOYMENT.md links to the example's reason rather than restating it. BACKLOG #6182 (doc limb, review repair round 2). Proposed PR title: k8s secret example loads secrets from files; an empty TLS key passphrase counts as none (BACKLOG #6182) Proposed vault banner: BUILT on branch b20056-6182, awaiting PR. The example kubectl command reads the store key and passwords with --from-file from operator-written files; the TLS key loaders treat an empty passphrase as none given, so an encrypted key with "" gets the key-wrap refusal naming the empty setting. A key wrapped under an empty passphrase, which loaded before, is now refused: a fail-closed tightening, with its remedy in CONNECTIONS.md and the changelog. Verified-by: tests --- .../6182-empty-tls-key-passphrase.security.md | 8 ++++++++ docker/k8s/secret.example.yaml | 19 +++++++++++++------ docs/CLOUD-DEPLOYMENT.md | 3 +-- docs/CONNECTIONS.md | 6 +++++- 4 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 changelog.d/6182-empty-tls-key-passphrase.security.md diff --git a/changelog.d/6182-empty-tls-key-passphrase.security.md b/changelog.d/6182-empty-tls-key-passphrase.security.md new file mode 100644 index 0000000000..a14e8cc6e6 --- /dev/null +++ b/changelog.d/6182-empty-tls-key-passphrase.security.md @@ -0,0 +1,8 @@ +- **An empty TLS key passphrase counts as none.** At the TLS key loaders (the API listener, MLLP, + the HTTP listener, DICOM, FTPS and SOAP), a `tls_key_password` or `MEFOR_API_TLS_KEY_PASSWORD` + set to the empty string is now treated as no passphrase. An encrypted key with it gets the + key-wrap refusal, which names the empty setting, rather than a raw OpenSSL error. A key wrapped + under an empty passphrase, which loaded before, is now refused: set the real passphrase, re-wrap + the key under a non-empty one, or supply it unencrypted. The Kubernetes example Secret now reads + the store key and passwords from files the operator writes, with `--from-file`, rather than + putting them on a `kubectl` command line. (`BACKLOG #6182`) diff --git a/docker/k8s/secret.example.yaml b/docker/k8s/secret.example.yaml index d99bad5612..490302b1c3 100644 --- a/docker/k8s/secret.example.yaml +++ b/docker/k8s/secret.example.yaml @@ -19,8 +19,10 @@ # value on a command line is readable by other local users in the process list while kubectl runs, # and stays in your shell history. Write the files in a private directory, with no trailing # newline (--from-file keeps every byte, so a trailing newline would become part of the value), -# and delete them once the Secret exists. In bash, in a subshell so the umask and the shell options -# end with it: +# and delete them once the Secret exists. Run it in bash on Linux or macOS (start `bash` first if +# your shell is zsh, where read -p means something else), or in WSL on Windows: Git Bash's chmod +# and umask do not set Windows file permissions, so the directory there would not be private. It +# runs in a subshell so the umask and the shell options end with it: # # ( # set -euo pipefail; umask 077; mkdir -p secrets; chmod 700 secrets @@ -31,10 +33,15 @@ # ) # # (IFS= read -rs keeps the value whole, off the screen and out of history; printf is a shell -# builtin, so the value never appears in a process's arguments; tr also strips the carriage return -# Windows adds; pipefail and test -s stop on a gen-key that failed.) Skip the store-password line -# for the single-node manifest, and the passphrase line if the API key is unencrypted, and drop the -# matching --from-file flag and rm path below too. The files are deleted only if kubectl succeeds. +# builtin, so the value never appears in a process's arguments; tr also strips a carriage return; +# pipefail and test -s stop on a gen-key that failed.) Skip the store-password line for the +# single-node manifest, and the passphrase line if the API key is unencrypted, and drop the +# matching --from-file flag and rm path below too. +# +# Before the files go, put a copy of the store key in your key escrow: once they are deleted the +# Secret would be its only copy, and losing the key loses every stored message encrypted under it. +# The files are deleted only if kubectl succeeds. If it fails, fix the cause and run it again, or +# delete the files and the secrets directory by hand. # # kubectl create secret generic mefor-secrets \ # --from-file=api.crt=tls/api.crt \ diff --git a/docs/CLOUD-DEPLOYMENT.md b/docs/CLOUD-DEPLOYMENT.md index 4526fe7c16..98f48cbff5 100644 --- a/docs/CLOUD-DEPLOYMENT.md +++ b/docs/CLOUD-DEPLOYMENT.md @@ -372,8 +372,7 @@ and the engine's at-rest encryption does not cover them ([`PHI.md`](PHI.md) §2, 3. **Create the Secret** ([`docker/k8s/secret.example.yaml`](../docker/k8s/secret.example.yaml)) with `store-encryption-key`, `store-password`, `tls-revocation-attested`, and the API TLS cert/key (and optional `api-tls-key-password`). Load the store key and the passwords with `--from-file`, never - `--from-literal`, which would put them in the process list and your shell history; the example's - header shows how to write those files. Both manifests, `ha-postgres.yaml` and `statefulset.yaml`, read + `--from-literal`; the example's header says why, and how to write those files. Both manifests, `ha-postgres.yaml` and `statefulset.yaml`, read `MEFOR_TLS_REVOCATION_ATTESTED` from the `tls-revocation-attested` key, with no default and not optional, so without it the kubelet refuses each pod with `CreateContainerConfigError`, at the first container that reads it. The attestation is yours diff --git a/docs/CONNECTIONS.md b/docs/CONNECTIONS.md index 86203bcda9..b69b7c44eb 100644 --- a/docs/CONNECTIONS.md +++ b/docs/CONNECTIONS.md @@ -4349,7 +4349,11 @@ scrypt passes at its Appendix C floor. - an encrypted key where the loader has no passphrase to give, before any library can prompt. At the TLS loaders an empty passphrase setting (`""`) counts as none, so an encrypted key with an empty `tls_key_password` or `MEFOR_API_TLS_KEY_PASSWORD` is refused here too, including one - wrapped under an empty passphrase. + wrapped under an empty passphrase. The remedy is to set the setting to the key's passphrase, or + to re-wrap the key with the command below, giving a **non-empty** passphrase at its prompt, or to + supply the key unencrypted with the setting removed. This differs on purpose from the PKCS#12 + rule further down, where an empty `MEFOR_PFX_PASSWORD` is a real empty passphrase: a TLS key's + passphrase setting is usually filled from a Secret or env var whose empty value means "none". **Re-wrap a PEM or PKCS#8 key** at the floor, then point the setting at the new file: From b5a51bef868db4cb259ad69d9cf4f1e3f30c549d Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Wed, 2 Dec 2026 08:10:20 -0600 Subject: [PATCH 7/7] k8s secret example: WSL keeps the directory private only off /mnt The WSL advice now says to work from the Linux file system, since a Windows drive under /mnt may ignore chmod and umask. Verified-by: tests --- docker/k8s/secret.example.yaml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docker/k8s/secret.example.yaml b/docker/k8s/secret.example.yaml index 490302b1c3..3c28ff4ef7 100644 --- a/docker/k8s/secret.example.yaml +++ b/docker/k8s/secret.example.yaml @@ -20,9 +20,11 @@ # and stays in your shell history. Write the files in a private directory, with no trailing # newline (--from-file keeps every byte, so a trailing newline would become part of the value), # and delete them once the Secret exists. Run it in bash on Linux or macOS (start `bash` first if -# your shell is zsh, where read -p means something else), or in WSL on Windows: Git Bash's chmod -# and umask do not set Windows file permissions, so the directory there would not be private. It -# runs in a subshell so the umask and the shell options end with it: +# your shell is zsh, where read -p means something else), or in WSL on Windows from a directory in +# the Linux file system (for example under ~), not under /mnt: Git Bash's chmod and umask do not +# set Windows file permissions, and a Windows drive under /mnt may ignore them too, so the +# directory there would not be private. It runs in a subshell so the umask and the shell options +# end with it: # # ( # set -euo pipefail; umask 077; mkdir -p secrets; chmod 700 secrets