From 81368a39ed0b11700d58a47a43b7eceba66ee810 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 01:50:02 -0600 Subject: [PATCH 01/19] measure-store-access.ps1 counts only the ADR 0167 refusal as a refused start Wait-ForGateRefusal returned on any log line carrying "no enabled Administrator". The skipped-gate warning and the warn-mode line carry that clause too, and an engine that logs either goes on to serve, so the StartFirst arm could report a refusal for an engine that started. Test-GateRefusalLine now requires the refusal's own words on the same line, and the StartFirstEndToEnd arm uses it with its narrower clause. New test lifts the function from the script's AST and runs it under pwsh and Windows PowerShell 5.1 on lines worded as app.py words them, and pins that app.py still words them so. BACKLOG #5658 --- scripts/service/measure-store-access.ps1 | 26 ++++- .../test_measure_store_access_gate_refusal.py | 108 ++++++++++++++++++ tests/test_tooling_partition.py | 4 + 3 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 tests/test_measure_store_access_gate_refusal.py diff --git a/scripts/service/measure-store-access.ps1 b/scripts/service/measure-store-access.ps1 index ce42685976..8e19f6ffb9 100644 --- a/scripts/service/measure-store-access.ps1 +++ b/scripts/service/measure-store-access.ps1 @@ -123,7 +123,8 @@ $GateRefusal = "no enabled Administrator" $NoAdminRefusal = "no enabled Administrator exists" $ProvisionHint = "provision-admin --username" # The words that make it a REFUSAL. The skipped-gate WARNING and the warn-mode line carry the two -# phrases above too, so without this an engine that started and served would pass step 1. +# phrases above too, so without this an engine that started and served would pass step 1 of every +# arm; Test-GateRefusalLine requires both on one line. $StartRefused = "refusing to start" $ProvisionDecline = "already has an enabled Administrator" $OpenErrorPattern = "unable to open database file|Access is denied|PermissionError|OperationalError" @@ -630,16 +631,29 @@ function Start-TheService([string]$Phase) { } } +function Test-GateRefusalLine([string]$Line, [string]$Clause = $GateRefusal) { + <# + Whether one log line is the ADR 0167 REFUSAL: it carries the gate's clause ($Clause, or a + narrower one such as $NoAdminRefusal) and the refusal's own words. The skipped-gate warning and + the warn-mode line carry the clause alone, and an engine that logs either goes on to start and + serve, so neither may count as a refusal. + #> + return ($Line -match [regex]::Escape($Clause)) -and ($Line -match [regex]::Escape($StartRefused)) +} + function Wait-ForGateRefusal([string]$Phase) { <# - Wait until the service logs the ADR 0167 refusal, which it reaches only after opening the store - and reading its users. Samples process owners throughout, since NSSM restarts the refused engine - and each restart is a new process. Returns $true on the refusal, $false on timeout. + Wait until the service logs the ADR 0167 refusal (Test-GateRefusalLine), which it reaches only + after opening the store and reading its users. Samples process owners throughout, since NSSM + restarts the refused engine and each restart is a new process. Returns $true on the refusal, + $false on timeout. #> $deadline = (Get-Date).AddSeconds($WaitSeconds) while ((Get-Date) -lt $deadline) { Update-OwnerSample "during $Phase" - if ((Get-LogMatches ([regex]::Escape($GateRefusal))).Count -gt 0) { return $true } + $refused = @(Get-LogMatches ([regex]::Escape($GateRefusal)) | + Where-Object { Test-GateRefusalLine $_.Line }) + if ($refused.Count -gt 0) { return $true } Start-Sleep -Seconds 1 } return $false @@ -963,7 +977,7 @@ try { "Wave 2 no path writes a bootstrap credential file (AC-13)") } $noAdmin = @(Get-LogMatches ([regex]::Escape($NoAdminRefusal)) | - Where-Object { $_.Line -match [regex]::Escape($StartRefused) }) + Where-Object { Test-GateRefusalLine $_.Line $NoAdminRefusal }) if (-not $opened) { $what = if (Test-Path -LiteralPath $DbPath) { "created $DbPath but never reached the account gate, so its open is unproven" } else { "never created $DbPath" } diff --git a/tests/test_measure_store_access_gate_refusal.py b/tests/test_measure_store_access_gate_refusal.py new file mode 100644 index 0000000000..96707b8ec4 --- /dev/null +++ b/tests/test_measure_store_access_gate_refusal.py @@ -0,0 +1,108 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# Copyright (C) 2026 MessageFoundry Foundation, LLC and contributors +"""``measure-store-access.ps1`` counts only the ADR 0167 REFUSAL as a refused start. + +``Wait-ForGateRefusal`` used to return on any log line carrying the gate's clause, "no enabled +Administrator". The skipped-gate warning and the warn-mode line carry that clause too, and an engine +that logs either goes on to start and serve, so the StartFirst arm could report a refusal for an +engine that served. Now a line must also carry the refusal's own words, through +``Test-GateRefusalLine``. + +NOTHING HERE RUNS THE SCRIPT: it installs a service and must run elevated, which only the hosted +windows-service-smoke leg does. The test lifts the two phrase assignments and +``Test-GateRefusalLine`` out of the script's AST and calls the function on lines worded as the +engine words them, and pins that the engine still words them so. It needs ``pwsh`` and skips where +there is none. +""" + +from __future__ import annotations + +import json +import shutil +import uuid +from pathlib import Path + +import pytest + +from tests._spawn_lock import run_single + +_ROOT = Path(__file__).resolve().parent.parent +_SCRIPT = _ROOT / "scripts" / "service" / "measure-store-access.ps1" +_APP = _ROOT / "messagefoundry" / "api" / "app.py" + +#: The prefix each of the gate's three lines opens with in ``app.py``, and a line worded so. +_REFUSAL = ( + "refusing to start a PHI instance: ", + "RuntimeError: refusing to start a PHI instance: no enabled Administrator exists in this store," + " so no enabled account holds the role that manages users and roles", +) +_SKIPPED = ( + "the engine is starting with the notice gate skipped: ", + "WARNING messagefoundry.api.app: the engine is starting with the notice gate skipped: no enabled" + " Administrator exists in this store, so no enabled account holds the role", +) +_WARN_MODE = ( + "PHI instance with no deliverable security-notice recipient: ", + "WARNING messagefoundry.api.app: PHI instance with no deliverable security-notice recipient: no" + " enabled Administrator has a notification address, so every out-of-band security notice", +) + + +def _psq(value: str) -> str: + return "'" + value.replace("'", "''") + "'" + + +def _judge(host: str, tmp_path: Path, lines: list[str]) -> dict[str, object]: + if shutil.which(host) is None: + pytest.skip(f"SKIP (nothing run): {host} not on PATH") + script = f""" +$ErrorActionPreference = 'Stop' +$errs = $null +$ast = [System.Management.Automation.Language.Parser]::ParseFile({_psq(str(_SCRIPT))}, [ref]$null, [ref]$errs) +if (@($errs).Count) {{ throw "the script does not parse: $($errs[0])" }} +foreach ($var in @('GateRefusal', 'StartRefused')) {{ + $set = $ast.Find({{ $args[0] -is [System.Management.Automation.Language.AssignmentStatementAst] -and + $args[0].Left.Extent.Text -eq ('$' + $var) }}, $false) + if (-not $set) {{ throw "missing assignment `${{var}}" }} + . ([scriptblock]::Create($set.Extent.Text)) +}} +$def = $ast.Find({{ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] -and + $args[0].Name -eq 'Test-GateRefusalLine' }}, $true) +if (-not $def) {{ throw 'missing function Test-GateRefusalLine' }} +. ([scriptblock]::Create($def.Extent.Text)) +$lines = {_psq(json.dumps(lines))} | ConvertFrom-Json +ConvertTo-Json -Compress -InputObject @{{ + refusal = @($lines | ForEach-Object {{ [bool](Test-GateRefusalLine $_) }}) + clause = @($lines | ForEach-Object {{ $_ -match [regex]::Escape($GateRefusal) }}) }} +""" + f = tmp_path / f"gate-{uuid.uuid4().hex}.ps1" + # With a BOM, which Windows PowerShell 5.1 needs to read a non-ASCII path as UTF-8. + f.write_text(script, encoding="utf-8-sig") + # Windows PowerShell's default policy can refuse an unsigned -File; pwsh's does not. + policy = ["-ExecutionPolicy", "Bypass"] if host == "powershell" else [] + proc = run_single( + [host, "-NoProfile", "-NonInteractive", *policy, "-File", str(f)], + capture_output=True, + text=True, + timeout=120, + check=False, + ) + assert proc.returncode == 0, f"harness failed: {proc.stdout}\n{proc.stderr}" + result: dict[str, object] = json.loads(proc.stdout.strip().splitlines()[-1]) + return result + + +def test_the_engine_still_words_the_three_gate_lines_so() -> None: + app = _APP.read_text(encoding="utf-8") + for prefix, line in (_REFUSAL, _SKIPPED, _WARN_MODE): + assert prefix in app, prefix + assert prefix in line + + +@pytest.mark.parametrize("host", ["pwsh", "powershell"]) +def test_only_the_refusal_counts_as_a_refused_start(tmp_path: Path, host: str) -> None: + got = _judge(host, tmp_path, [_REFUSAL[1], _SKIPPED[1], _WARN_MODE[1], "unrelated line"]) + assert got["refusal"] == [True, False, False, False], got + # The control: the clause the wait used to match alone is in both warning lines, so the old + # criterion took an engine that went on to serve for a refused one. + assert got["clause"] == [True, True, True, False], got diff --git a/tests/test_tooling_partition.py b/tests/test_tooling_partition.py index 289e68bdf6..0f70fdf1fa 100644 --- a/tests/test_tooling_partition.py +++ b/tests/test_tooling_partition.py @@ -76,6 +76,10 @@ # the change that breaks it. Its other half reads docs/SECURITY.md, which is what makes it a # doc-drift guard, but the doc half is not the reason it stays. "test_adaptive_attributes_doc_drift.py", + # Reads messagefoundry/api/app.py to pin the ADR 0167 gate's three log-line prefixes that + # scripts/service/measure-store-access.ps1 tells apart. An engine diff that rewords them must + # meet this test, and the tooling job's path gate would not run it then. + "test_measure_store_access_gate_refusal.py", # Reads tests/conftest.py and packaging/messagefoundry-webconsole/tests/{conftest, # _ui_clients}.py off disk, the two copies of the in-process browser stand-in that every # /ui write test in both suites runs through (vault BACKLOG #3316). No messagefoundry/** From daa80aae1665ed1f7642cb8585b3cfcc1590e7ea Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 01:52:47 -0600 Subject: [PATCH 02/19] import-db-ca.ps1 checks each CA chains to a root in the file and refuses one outside its validity The script refused a file only when no certificate in it was a self-signed root. Now each certificate that passes the CA checks must also chain to a self-signed root through the file's own certificates, every link's signature verified (Test-SignedBy, Get-ChainRefusal), and a certificate outside its validity period is refused. Other changes: - roots are added first, each store opened once; - dates print in UTC, and a refused certificate is listed with its reason, not a store; - each refusal names thumbprint, Subject and reason, worded the same in the listing and the error, which names the first five and counts the rest; - bare base64 behind a byte-order mark is read. DEPLOY-SERVER-DB.md (the rotation step removes every certificate the old file added), AOAG-DEPLOYMENT.md and INPUT-SURFACES.md point to the help. Tests read the terminating error's own message, not pwsh's error view, and cover the chain, validity, BOM, bounded-error, elevation and PFX-in-PEM cases. BACKLOG #5561 Proposed PR title: Service scripts: import-db-ca.ps1 checks the chain and validity of each CA; measure-store-access.ps1 counts only the gate refusal (BACKLOG #5561, #5658) Proposed banner #5561: PARTIAL -- each CA must chain to a self-signed root in the file and be within its validity period; roots go first; refusals name the Subject; several items kept in one line each. Proposed banner #5658: SHIPPED -- Wait-ForGateRefusal counts a line only when it carries the refusal's own words, with a test against app.py's wording. --- docs/AOAG-DEPLOYMENT.md | 5 +- docs/DEPLOY-SERVER-DB.md | 23 ++-- docs/INPUT-SURFACES.md | 2 +- scripts/service/import-db-ca.ps1 | 175 +++++++++++++++++-------- tests/test_import_db_ca_bundle.py | 205 +++++++++++++++++++++++++----- 5 files changed, 310 insertions(+), 100 deletions(-) diff --git a/docs/AOAG-DEPLOYMENT.md b/docs/AOAG-DEPLOYMENT.md index bc175edff1..daebf16201 100644 --- a/docs/AOAG-DEPLOYMENT.md +++ b/docs/AOAG-DEPLOYMENT.md @@ -472,8 +472,9 @@ The engine connects with `Encrypt=yes` and **verifies** the server certificate. posture; `MEFOR_ALLOW_INSECURE_TLS` does not lift it (owner ruling 2026-11-14, vault BACKLOG #3533 item 2). This has three consequences for the AG: -- ODBC Driver 18 validates against the **Windows machine trust store** (`LocalMachine\Root`), and - there is no CA-file keyword. Import the DB CA on **every engine VM in both DCs** +- ODBC Driver 18 validates against the **Windows machine stores** (the root in `LocalMachine\Root`; + [`DEPLOY-SERVER-DB.md`](DEPLOY-SERVER-DB.md) §5.2 says where each CA goes), and there is no + CA-file keyword. Import the DB CA on **every engine VM in both DCs** ([`DEPLOY-SERVER-DB.md`](DEPLOY-SERVER-DB.md) §5, `scripts/service/import-db-ca.ps1`). `[store].ssl_root_cert` is no substitute here: on SQL Server it pins one exact certificate and switches the store to `Encrypt=strict`, so after a failover to a replica that presents a different diff --git a/docs/DEPLOY-SERVER-DB.md b/docs/DEPLOY-SERVER-DB.md index 527cd4a6ae..a88b8da9e2 100644 --- a/docs/DEPLOY-SERVER-DB.md +++ b/docs/DEPLOY-SERVER-DB.md @@ -150,7 +150,7 @@ server = "sql01.corp.example.com" database = "MessageFoundry" auth = "integrated" # Trusted_Connection=yes — the gMSA's identity authenticates encrypt = true # default; TLS to the DB -trust_server_certificate = false # default; verify the DB cert (import its CA into LocalMachine\Root, §5.2) +trust_server_certificate = false # default; verify the DB cert (import its CA into the machine stores, §5.2) require_managed_identity = true # refuse a static SQL login on production PHI (ASVS 13.2.1) # schema_management = "external" # the server-DB default; step 4 owns the DDL (BACKLOG #305) ``` @@ -552,9 +552,8 @@ From an **elevated (Administrator)** PowerShell, use the helper: .\scripts\service\import-db-ca.ps1 -CaPath C:\certs\internal-root-ca.crt ``` -The helper shows every certificate in the file, and refuses the whole file in at least these cases: -any one is not a CA or carries a private key, the file holds no self-signed root, or a DER value in -it does not end where its bytes end. The helper's own help lists the rest. +The helper shows every certificate in the file, and refuses the whole file, importing nothing, on +any of the refusals its own help lists. **Where each CA goes.** This paragraph and the helper's own help are the statement of it; other documents point here. A self-signed root, whose signature verifies under its own key, goes into @@ -565,10 +564,10 @@ Server hop still checks the chain up to the root. It **is** usable as a trust an engine's Python TLS on Windows: CPython's `ssl.create_default_context()`, which the PostgreSQL system-trust path calls, loads the `CA` and `ROOT` system stores, and Python 3.13 and later set partial-chain verification, so a chain that reaches a certificate in `LocalMachine\CA` validates -there without its root. The helper refuses a file unless at least one certificate in it is a -self-signed root. It does not check that each other CA in the file chains to that root, and with -`-Confirm` you may still decline the root and accept another CA, so give it the root of the CA you -mean to trust, alone or in that CA's own `.p7b` chain, and accept every certificate it offers. +there without its root. The helper refuses a file unless it holds a self-signed root and every +other CA in it chains to a self-signed root through the file's own certificates. With `-Confirm` you +may still decline the root and accept another CA, so give it the root of the CA you mean to trust, +alone or in that CA's own `.p7b` chain, and accept every certificate it offers. The built-ins below check none of that, so use them only for a file holding one self-signed root CA you have checked yourself: @@ -601,9 +600,11 @@ connection fails validation** (NIST SP 800-52r2: rotate certificates before expi 2. **Roll the DB server certificate** to one issued by the new CA (a DB-administrator action). Because both CAs are trusted, connections keep validating across the swap. 3. **Remove the old CA** once every host trusts the new one and the server cert has rolled: - - **SQL Server:** delete the old CA from whichever machine store holds it, `LocalMachine\Root` for - a root or `LocalMachine\CA` for an issuing CA (e.g. - `Get-ChildItem Cert:\LocalMachine\Root, Cert:\LocalMachine\CA | Where-Object Thumbprint -eq | Remove-Item`). + - **SQL Server:** delete every certificate the old CA's file added, the root from + `LocalMachine\Root` and any issuing CA from `LocalMachine\CA`, by the thumbprints the helper + listed when it imported that file. An issuing CA left in `LocalMachine\CA` stays a trust anchor + for the engine's Python TLS (§5.2). For example + `Get-ChildItem Cert:\LocalMachine\Root, Cert:\LocalMachine\CA | Where-Object Thumbprint -in @('', '') | Remove-Item`. - **PostgreSQL:** drop the old CA from the PEM bundle and reload. **Rotating only the server leaf cert** (same CA): no trust-store change is needed — the new leaf still diff --git a/docs/INPUT-SURFACES.md b/docs/INPUT-SURFACES.md index f131743811..862d73836c 100644 --- a/docs/INPUT-SURFACES.md +++ b/docs/INPUT-SURFACES.md @@ -523,7 +523,7 @@ help. Each rule is in the script's `param()` block or its body. | `uninstall-net-helper.ps1` | `-ServiceName` | The `-ServiceName` rule above | | `measure-store-access.ps1` | `-Order` | Required. One of `ProvisionFirst`, `StartFirst`, `StartFirstEndToEnd` | | | `-DataDir` | Required | -| `import-db-ca.ps1` | `-CaPath` | Required. A certificate file holding a self-signed root, in which every certificate is a CA with no private key. The help block at the top of the script, which `Get-Help` does not print, as the paragraph above says, lists the shapes it reads and, with "at least", what it refuses | +| `import-db-ca.ps1` | `-CaPath` | Required. A certificate file holding a self-signed root. The help block at the top of the script, which `Get-Help` does not print, as the paragraph above says, lists the shapes it reads and what it refuses | `install-service.ps1` also relates parameters to each other. `-EngineWheel` refuses `-AppExe` and `-PythonExe`. `-EngineWheelSha256`, `-RequirementsLock`, `-RequirementsLockSha256` and diff --git a/scripts/service/import-db-ca.ps1 b/scripts/service/import-db-ca.ps1 index b4729f2a82..e6e4d475af 100644 --- a/scripts/service/import-db-ca.ps1 +++ b/scripts/service/import-db-ca.ps1 @@ -29,21 +29,26 @@ ssl.create_default_context(), which the PostgreSQL system-trust path calls, loads the CA and ROOT system stores, and Python 3.13 and later set partial-chain verification, so a chain that reaches a certificate in LocalMachine\CA validates there without its root. A file is refused - unless at least one certificate in it is a self-signed root. That is the whole check: it does - not check that each other CA in the file chains to that root, and with -Confirm the operator - may still decline the root and accept another CA. + unless it holds a self-signed root and every other CA in it chains to a self-signed root + through the file's own certificates, each link's signature verified. No revocation, name + constraint or path length is checked, and with -Confirm the operator may still decline the + root and accept another CA. PostgreSQL can alternatively pin a CA by file via [store].ssl_root_cert (no machine import needed). This machine-store import is the path SQL Server REQUIRES; Postgres can use either. The file is read once. Every certificate it holds is shown, checked, and only then added, so - nothing reaches a store that the operator was not shown. The whole file is refused, and - nothing is imported, when any certificate in it is not marked as a CA by its basic - constraints, has a key usage that cannot sign certificates, or carries a private key, when it - holds no self-signed root, when a DER value in it (the whole file, a PEM block, or decoded - base64) does not end where its bytes end, or when a control character other than tab, CR or - LF sits outside its PEM blocks. Other text outside PEM blocks, bare base64 among them, is - ignored, not refused. At least a .pfx, and + nothing reaches a store that the operator was not shown. Roots are added first. The whole + file is refused, and nothing is imported, when any certificate in it is not marked as a CA by + its basic constraints, has a key usage that cannot sign certificates, carries a private key, + or is outside its validity period, when it holds no self-signed root, when a CA in it does not + chain to a self-signed root in the file, when a DER value in it (the whole file, a PEM block, + or decoded base64) does not end where its bytes end, or when a control character other than + tab, CR or LF sits outside its PEM blocks. A BER value with an indefinite length, which some + tools write for a .p7b, is refused by that DER rule; re-encode it as DER first. Only RSA + PKCS#1 v1.5 and ECDSA signatures with SHA-1 or SHA-2 are verified, so a root or a chain link + signed any other way (RSA-PSS, for one) is refused. Other text outside PEM blocks, bare base64 + among them, is ignored, not refused. At least a .pfx, and a well-formed PEM block with any label but CERTIFICATE, X509 CERTIFICATE or PKCS7 (such as a PRIVATE KEY block), are refused before anything is parsed. Key material outside well-formed PEM armour is not looked for; no private key is ever loaded either way. @@ -208,8 +213,11 @@ function Read-CaBundle { $encoding = [System.Text.Encoding]::Unicode } elseif ($Bytes.Length -ge 2 -and $Bytes[0] -eq 0xFE -and $Bytes[1] -eq 0xFF) { $encoding = [System.Text.Encoding]::BigEndianUnicode + } elseif ($Bytes.Length -ge 3 -and $Bytes[0] -eq 0xEF -and $Bytes[1] -eq 0xBB -and $Bytes[2] -eq 0xBF) { + $encoding = [System.Text.Encoding]::UTF8 } - $text = $encoding.GetString($Bytes) + # GetString keeps a byte-order mark as U+FEFF, which bare base64 would otherwise fail to match. + $text = $encoding.GetString($Bytes).TrimStart([char]0xFEFF) # Every piece of DER, the whole file or one PEM block, must be a certificate or a PKCS#7 chain, # so a .pfx is refused wherever it sits, before it is parsed. $importDer = { @@ -299,13 +307,15 @@ function Read-CaBundle { return , $unique } -# Whether the certificate is self-signed: its subject and issuer are the same name AND its signature -# verifies under its own public key. A self-issued certificate another key signed, such as a CA -# re-key link certificate, is not. RSA PKCS#1 v1.5 and ECDSA with SHA-1 or SHA-2 are checked; any -# other signature algorithm (RSA-PSS, for one) reads as not self-signed, which fails closed. -function Test-SelfSigned { - param([Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate) - if ([Convert]::ToBase64String($Certificate.SubjectName.RawData) -cne +# Whether $Signer issued the certificate: the certificate's issuer is $Signer's subject, byte for +# byte, AND its signature verifies under $Signer's public key. RSA PKCS#1 v1.5 and ECDSA with SHA-1 +# or SHA-2 are checked; any other signature algorithm (RSA-PSS, SHA-224, Ed25519 and DSA among them) +# reads as not signed, which fails closed: such a certificate is never a root, and never a link. +function Test-SignedBy { + param( + [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate, + [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Signer) + if ([Convert]::ToBase64String($Signer.SubjectName.RawData) -cne [Convert]::ToBase64String($Certificate.IssuerName.RawData)) { return $false } $hashes = @{ '1.2.840.113549.1.1.5' = 'SHA1'; '1.2.840.113549.1.1.11' = 'SHA256' @@ -332,12 +342,12 @@ function Test-SelfSigned { [Array]::Copy($raw, $bits.Start + 1, $signature, 0, $signature.Length) try { if ($oid.StartsWith('1.2.840.113549.')) { - $rsa = [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPublicKey($Certificate) + $rsa = [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPublicKey($Signer) if (-not $rsa) { return $false } return [bool]$rsa.VerifyData($signed, $signature, $hash, [System.Security.Cryptography.RSASignaturePadding]::Pkcs1) } - $ecdsa = [System.Security.Cryptography.X509Certificates.ECDsaCertificateExtensions]::GetECDsaPublicKey($Certificate) + $ecdsa = [System.Security.Cryptography.X509Certificates.ECDsaCertificateExtensions]::GetECDsaPublicKey($Signer) if (-not $ecdsa) { return $false } # X.509 carries an ECDSA signature as a DER SEQUENCE of r and s; .NET Framework's VerifyData # reads only r and s side by side, each padded to the key's width. @@ -362,6 +372,42 @@ function Test-SelfSigned { } } +# Whether the certificate is self-signed: it is its own signer by Test-SignedBy, so its subject and +# issuer are the same name AND its signature verifies under its own public key. A self-issued +# certificate another key signed, such as a CA re-key link certificate, is not. +function Test-SelfSigned { + param([Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate) + return (Test-SignedBy -Certificate $Certificate -Signer $Certificate) +} + +# Why the certificate does not chain to a self-signed root in the file, or $null when it does. The +# walk follows issuer links through the file only, never a machine store: each step needs a +# certificate in $Bundle whose subject is the issuer's name and whose key verifies the signature +# (Test-SignedBy). Every such issuer is followed, not only the first, so a cross-certificate that +# shares a root's name and key cannot hide the root behind it; each certificate is visited once, so +# a loop of links ends. Nothing else about the chain is checked here; Get-CaRefusal checks each +# certificate on its own, and no revocation or name constraint is read. +function Get-ChainRefusal { + param( + [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate, + [Parameter(Mandatory = $true)][AllowEmptyCollection()][System.Security.Cryptography.X509Certificates.X509Certificate2Collection]$Bundle) + $visited = @{ $Certificate.Thumbprint = $true } + $pending = [System.Collections.Generic.Queue[object]]::new() + $pending.Enqueue($Certificate) + while ($pending.Count -gt 0) { + $current = $pending.Dequeue() + if (Test-SelfSigned -Certificate $current) { return $null } + foreach ($candidate in $Bundle) { + if (-not $visited.ContainsKey($candidate.Thumbprint) -and + (Test-SignedBy -Certificate $current -Signer $candidate)) { + $visited[$candidate.Thumbprint] = $true + $pending.Enqueue($candidate) + } + } + } + return 'it does not chain to a self-signed root in the file' +} + # The store a checked CA belongs in: Root for a self-signed one, CertificateAuthority (the # intermediate store) for any other. The help's "Where each CA goes" says what each store means. function Get-CaStoreName { @@ -375,11 +421,17 @@ function Get-CaStoreName { # extension counts as not a CA here. RFC 5280 (the Basic Constraints section) requires the extension # of a version 3 CA certificate; a version 1 root, which cannot carry it, is refused too, by this # script's choice rather than the RFC's. A key usage extension that leaves out certificate signing -# also counts as not a CA (RFC 5280, the Key Usage section). +# also counts as not a CA (RFC 5280, the Key Usage section). A certificate outside its validity +# period at $NowUtc is refused as well: nothing that chains through it validates, so importing it +# is only ever a mistake, and an expired root must not stand in for the one a CA chains to. function Get-CaRefusal { - param([Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate) + param( + [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate, + [datetime]$NowUtc = [datetime]::UtcNow) # Read-CaBundle already refuses every container that can carry a key; this holds if it ever stops. if ($Certificate.HasPrivateKey) { return 'it carries a private key' } + if ($Certificate.NotAfter.ToUniversalTime() -lt $NowUtc) { return 'it has expired' } + if ($Certificate.NotBefore.ToUniversalTime() -gt $NowUtc) { return 'it is not valid yet' } $constraints = @($Certificate.Extensions | Where-Object { $_ -is [System.Security.Cryptography.X509Certificates.X509BasicConstraintsExtension] }) if ($constraints.Count -eq 0) { return 'it has no basic constraints extension, so it is not marked as a CA' } @@ -413,43 +465,56 @@ try { throw "Could not read '$CaPath' as X.509 CA certificates: $($_.Exception.Message)" } +$nowUtc = [datetime]::UtcNow +# Each refused certificate as one line: its thumbprint, its Subject escaped for the console, and why. +# The host listing and the terminating error both print these same lines. +$refusals = [System.Collections.Generic.List[string]]::new() +# Each certificate is checked before it is shown, so a refused one is listed with its reason and +# never with a store it would not go to. The store shown is the one it is offered for and written to. +# A certificate that passes on its own must also reach a root through this file's own certificates, +# so a root that signed none of them cannot carry an unrelated issuing CA in beside it. Write-Host "The file holds $($bundle.Count) certificate(s):" -$now = [datetime]::Now -$refusals = [System.Collections.Generic.List[object]]::new() -# The store each certificate is shown with is the one it is offered for and written to. $entries = @(foreach ($cert in $bundle) { $target = $stores[(Get-CaStoreName -Certificate $cert)] + $why = Get-CaRefusal -Certificate $cert -NowUtc $nowUtc + if (-not $why) { $why = Get-ChainRefusal -Certificate $cert -Bundle $bundle } Write-Host "" Write-Host " Subject : $(ConvertTo-PrintableText $cert.Subject)" Write-Host " Issuer : $(ConvertTo-PrintableText $cert.Issuer)" Write-Host " Thumbprint : $($cert.Thumbprint)" - Write-Host " Not after : $($cert.NotAfter.ToString('u'))" - Write-Host " Store : $($target.Label)" - if ($cert.NotAfter -lt $now -or $cert.NotBefore -gt $now) { - Write-Warning "$($cert.Thumbprint) is outside its validity period; a chain through it will not validate." + Write-Host " Not before : $($cert.NotBefore.ToUniversalTime().ToString('u'))" + Write-Host " Not after : $($cert.NotAfter.ToUniversalTime().ToString('u'))" + if ($why) { + Write-Host " Refused : $why" + $refusals.Add("$($cert.Thumbprint) ($(ConvertTo-PrintableText $cert.Subject)): $why") + } else { + Write-Host " Store : $($target.Label)" } - $why = Get-CaRefusal -Certificate $cert - if ($why) { $refusals.Add([pscustomobject]@{ Thumbprint = $cert.Thumbprint; Why = $why }) } @{ Certificate = $cert; Target = $target } }) -if ($refusals.Count -gt 0) { - # Printed one per line for the operator. pwsh 7's default error view rewraps a message at the - # console width, which can split a thumbprint from its reason. The error repeats each pair on - # one line, for a caller that catches it or keeps only the error stream. - Write-Host "" - Write-Host "Refused:" - foreach ($refusal in $refusals) { Write-Host " $($refusal.Thumbprint): $($refusal.Why)" } - throw ("Nothing was imported. Every certificate in the file must be a CA that may sign " + - "certificates and carries no private key. Refused: " + - (($refusals | ForEach-Object { "$($_.Thumbprint) ($($_.Why))" }) -join '; ') + ".") -} if (-not ($entries | Where-Object { $_.Target -eq $stores['Root'] })) { # Without its root an issuing CA validates nothing over SChannel, and over the engine's Python TLS # it would be trusted with no root's revocation or constraints over it (the help's "Where each - # CA goes"), so a rootless file is refused rather than imported. + # CA goes"), so a rootless file is refused rather than imported. Checked first, since every CA + # in such a file is also refused for reaching no root, and this message says why in one line. throw ("Nothing was imported. The file holds no self-signed root. Give this script the root " + "the CA chains to, alone or in the same .p7b chain.") } +if ($refusals.Count -gt 0) { + # Printed one per line for the operator: pwsh 7's default error view rewraps a message at the + # console width, which can split a line. The error carries the same lines, joined into its one + # message, for a caller that catches it or keeps only the error stream; past the first few it + # gives the count instead. + Write-Host "" + Write-Host "Refused:" + foreach ($line in $refusals) { Write-Host " $line" } + $shown = 5 + $named = ($refusals | Select-Object -First $shown) -join '; ' + if ($refusals.Count -gt $shown) { $named += "; and $($refusals.Count - $shown) more, listed above" } + throw ("Nothing was imported. Every certificate in the file must be a CA, within its validity " + + "period, that may sign certificates, carries no private key, and chains to a self-signed " + + "root in the file. Refused: $named.") +} # --- import ------------------------------------------------------------------ @@ -461,20 +526,27 @@ $approved = @(foreach ($entry in $entries) { }) if ($approved.Count -gt 0) { - # Each store keys on thumbprint, so re-importing the same CA is a no-op (idempotent). + # Each store keys on thumbprint, so re-importing the same CA is a no-op (idempotent). Roots go + # first, so a failure part way never leaves an approved issuing CA added and an approved root not. + # Nothing is rolled back; the error says how many were added. $added = 0 try { - foreach ($entry in $approved) { - $entry.Target.Store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite) + foreach ($name in 'Root', 'CertificateAuthority') { + $batch = @($approved | Where-Object { $_.Target -eq $stores[$name] }) + if ($batch.Count -eq 0) { continue } + $store = $stores[$name].Store + $store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite) try { - $entry.Target.Store.Add($entry.Certificate) + foreach ($entry in $batch) { + $store.Add($entry.Certificate) + $added++ + } } finally { - $entry.Target.Store.Close() + $store.Close() } - $added++ } } catch { - throw ("Added $added of $($approved.Count) certificate(s), in the order shown, before this " + + throw ("Added $added of $($approved.Count) certificate(s), roots first, before this " + "failed: $($_.Exception.Message)") } Write-Host "" @@ -489,5 +561,6 @@ if ($approved.Count -gt 0) { } Write-Host "" Write-Host "Rotation: when the CA is replaced, import the NEW CA first (this leaves both trusted)," - Write-Host "roll the server cert, then remove the OLD CA -- see docs/DEPLOY-SERVER-DB.md." + Write-Host "roll the server cert, then remove every certificate the OLD file added, by the" + Write-Host "thumbprints this script listed for it -- see docs/DEPLOY-SERVER-DB.md." } diff --git a/tests/test_import_db_ca_bundle.py b/tests/test_import_db_ca_bundle.py index 3c753f55ef..cc4376afff 100644 --- a/tests/test_import_db_ca_bundle.py +++ b/tests/test_import_db_ca_bundle.py @@ -66,9 +66,12 @@ def _cert( issuer: tuple[x509.Certificate, _Key] | None = None, *, cert_sign: bool | None = None, + valid_from_days: int = -1, + valid_to_days: int = 30, ) -> tuple[x509.Certificate, _Key]: """A synthetic certificate. ``ca`` None leaves the basic constraints extension out, and - ``cert_sign`` None leaves the key usage extension out.""" + ``cert_sign`` None leaves the key usage extension out. The validity period is given in days + from now.""" key = ec.generate_private_key(ec.SECP256R1()) name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, cn)]) now = datetime.datetime.now(datetime.UTC) @@ -78,8 +81,8 @@ def _cert( .issuer_name(issuer[0].subject if issuer else name) .public_key(key.public_key()) .serial_number(x509.random_serial_number()) - .not_valid_before(now - datetime.timedelta(days=1)) - .not_valid_after(now + datetime.timedelta(days=30)) + .not_valid_before(now + datetime.timedelta(days=valid_from_days)) + .not_valid_after(now + datetime.timedelta(days=valid_to_days)) ) if ca is not None: builder = builder.add_extension(x509.BasicConstraints(ca=ca, path_length=None), True) @@ -113,10 +116,21 @@ def _pem(*certs: x509.Certificate) -> bytes: @pytest.fixture(scope="module") def chain() -> dict[str, tuple[x509.Certificate, _Key]]: root = _cert("Synthetic Test Root CA", True) + other_root = _cert("Synthetic Other Root CA", True) return { "root": root, "intermediate": _cert("Synthetic Test Issuing CA", True, root), "leaf": _cert("db.test.invalid", False, root), + # Issued by a root the file does not hold, and signed under the same issuer NAME as the + # file's root by another key: neither chains to the file's root. + "foreign_intermediate": _cert("Synthetic Foreign Issuing CA", True, other_root), + "name_only_intermediate": _cert( + "Synthetic Name-Only Issuing CA", True, (root[0], other_root[1]) + ), + "expired_root": _cert( + "Synthetic Expired Root CA", True, valid_from_days=-30, valid_to_days=-1 + ), + "future_root": _cert("Synthetic Future Root CA", True, valid_from_days=2), "unconstrained": _cert("Synthetic No Constraints", None), "no_cert_sign": _cert("Synthetic CA Without CertSign", True, cert_sign=False), "cert_sign": _cert("Synthetic CA With CertSign", True, cert_sign=True), @@ -214,6 +228,24 @@ def files( "der_concat": root.public_bytes(der) + inter.public_bytes(der), "der_then_pem": root.public_bytes(der) + _pem(inter), "b64_trailing": base64.encodebytes(root.public_bytes(der) + b"\x00\x01"), + # A root beside a CA that does not chain to it: a foreign issuer, or the root's name alone. + "pem_foreign_intermediate": _pem(root, chain["foreign_intermediate"][0]), + "pem_name_only_intermediate": _pem(root, chain["name_only_intermediate"][0]), + # Outside the validity period: an expired root beside a valid one, and a root not valid yet. + "pem_expired_root": _pem(root, chain["expired_root"][0]), + "pem_future_root": _pem(chain["future_root"][0]), + # Bare base64 behind a byte-order mark, UTF-8 and UTF-16. + "b64_utf8_bom": b"\xef\xbb\xbf" + base64.encodebytes(root.public_bytes(der)), + "b64_utf16_bom": b"\xff\xfe" + + base64.encodebytes(root.public_bytes(der)).decode("ascii").encode("utf-16-le"), + # A .pfx carried under a CERTIFICATE header: refused by its content, not its label. + "pfx_in_certificate_block": b"-----BEGIN CERTIFICATE-----\n" + + base64.encodebytes( + pkcs12.serialize_key_and_certificates( + b"x", root_key, root, None, serialization.NoEncryption() + ) + ) + + b"-----END CERTIFICATE-----\n", "empty": b"", "base64_junk": b"not a certificate at all\n", "garbage": b"not a certificate!\n", @@ -231,14 +263,15 @@ def _psq(value: str) -> str: def _read_all(tmp_path: Path, paths: dict[str, Path]) -> dict[str, dict[str, object]]: - """Each file through ``Read-CaBundle``, then ``Get-CaRefusal`` and ``Get-CaStoreName``, as the - script does.""" + """Each file through ``Read-CaBundle``, then ``Get-CaRefusal``, ``Get-CaStoreName`` and + ``Get-ChainRefusal``, as the script does.""" script = f""" $ErrorActionPreference = 'Stop' $ast = [System.Management.Automation.Language.Parser]::ParseFile( {_psq(str(_SCRIPT))}, [ref]$null, [ref]$null) foreach ($name in @('Test-EscapedForConsole', 'ConvertTo-PrintableText', 'Read-DerHeader', - 'Read-CaBundle', 'Get-CaRefusal', 'Test-SelfSigned', 'Get-CaStoreName')) {{ + 'Read-CaBundle', 'Get-CaRefusal', 'Test-SignedBy', 'Test-SelfSigned', + 'Get-ChainRefusal', 'Get-CaStoreName')) {{ $def = $ast.Find({{ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $args[0].Name -eq $name }}, $true) if (-not $def) {{ throw "missing function $name" }} @@ -251,7 +284,8 @@ def _read_all(tmp_path: Path, paths: dict[str, Path]) -> dict[str, dict[str, obj $bundle = Read-CaBundle -Bytes ([System.IO.File]::ReadAllBytes($paths[$k])) $out[$k] = @{{ certs = @(foreach ($c in $bundle) {{ @{{ thumb = $c.Thumbprint; refusal = (Get-CaRefusal -Certificate $c); - store = (Get-CaStoreName -Certificate $c) }} }}) }} + store = (Get-CaStoreName -Certificate $c); + chain = (Get-ChainRefusal -Certificate $c -Bundle $bundle) }} }}) }} }} catch {{ $out[$k] = @{{ error = $_.Exception.Message }} }} @@ -332,6 +366,37 @@ def certs(name: str) -> list[tuple[str, str | None]]: ) assert usage[_thumb(chain["cert_sign"][0])] is None + # Bare base64 behind a byte-order mark reads as the root it holds. + assert certs("b64_utf8_bom") == [(root, None)] + assert certs("b64_utf16_bom") == [(root, None)] + + # Outside its validity period, a root is refused on its own, whatever else the file holds. + expired = dict(certs("pem_expired_root")) + assert expired[_thumb(chain["expired_root"][0])] == "it has expired" + assert expired[root] is None + assert certs("pem_future_root") == [(_thumb(chain["future_root"][0]), "it is not valid yet")] + + # Each CA must chain to a self-signed root through the file's own certificates: an issuing CA + # and a re-key certificate that do pass, and one whose issuer is not in the file, or only + # carries the root's name under another key, does not. + def chained(name: str) -> dict[str, object]: + entry = got[name] + assert "error" not in entry, f"{name}: {entry}" + listed = entry["certs"] + assert isinstance(listed, list) + return {c["thumb"]: c["chain"] for c in listed} + + assert chained("p7b_der") == {root: None, inter: None} + assert chained("root_and_rekey") == {root: None, rekey: None} + unchained = "it does not chain to a self-signed root in the file" + for name, key in ( + ("pem_foreign_intermediate", "foreign_intermediate"), + ("pem_name_only_intermediate", "name_only_intermediate"), + ): + assert chained(name) == {root: None, _thumb(chain[key][0]): unchained}, name + # Each is a CA in its own right, so only the chain check catches it. + assert dict(certs(name))[_thumb(chain[key][0])] is None, name + # Refused before anything is parsed: a private key, in either container, and what is not a # certificate file at all. def error(name: str) -> str: @@ -340,6 +405,7 @@ def error(name: str) -> str: return str(entry["error"]) assert "'Pfx' content" in error("pfx") + assert "'Pfx' content" in error("pfx_in_certificate_block") assert "labelled 'PRIVATE KEY'" in error("pem_with_key") assert "labelled 'EC PRIVATE KEY'" in error("pem_zero_first_with_key") assert "not well formed" in error("pem_unterminated") @@ -353,20 +419,30 @@ def error(name: str) -> str: assert "control character outside its PEM blocks" in error("der_then_pem") -def _run_whatif(ca_path: Path) -> subprocess.CompletedProcess[str]: +_ERROR_MARK = "TERMINATING-ERROR-MESSAGE: " + + +def _run_script(ca_path: Path, *, whatif: bool = True) -> subprocess.CompletedProcess[str]: + """Run the script unelevated. Its terminating error's own message, not pwsh's rendered error + view, is printed to stdout on one line after ``_ERROR_MARK``: the view rewraps at the console + width and quotes the throwing source line, so text read there can come from the code.""" if _elevated(): pytest.skip("SKIP (nothing run): elevated session; the script is run only unelevated") + wrapper = Path(ca_path).parent / f"run-{uuid.uuid4().hex}.ps1" + wrapper.write_text( + f""" +$ErrorActionPreference = 'Stop' +try {{ + & {_psq(str(_SCRIPT))} -CaPath {_psq(str(ca_path))}{" -WhatIf" if whatif else ""} +}} catch {{ + [Console]::Out.WriteLine({_psq(_ERROR_MARK)} + ($_.Exception.Message -replace '\\s+', ' ')) + exit 1 +}} +""", + encoding="utf-8", + ) return subprocess.run( - [ - _pwsh(), - "-NoProfile", - "-NonInteractive", - "-File", - str(_SCRIPT), - "-CaPath", - str(ca_path), - "-WhatIf", - ], + [_pwsh(), "-NoProfile", "-NonInteractive", "-File", str(wrapper)], capture_output=True, text=True, encoding="utf-8", @@ -374,10 +450,18 @@ def _run_whatif(ca_path: Path) -> subprocess.CompletedProcess[str]: ) +def _error_message(proc: subprocess.CompletedProcess[str]) -> str: + found = [ + ln[len(_ERROR_MARK) :] for ln in proc.stdout.splitlines() if ln.startswith(_ERROR_MARK) + ] + assert len(found) == 1, proc.stdout + proc.stderr + return found[0] + + def test_whatif_shows_and_offers_every_certificate_of_an_unsigned_p7b( chain: dict[str, tuple[x509.Certificate, _Key]], files: dict[str, Path] ) -> None: - proc = _run_whatif(files["p7b_der"]) + proc = _run_script(files["p7b_der"]) out = proc.stdout + proc.stderr assert proc.returncode == 0, out assert "The file holds 2 certificate(s)" in out @@ -399,15 +483,22 @@ def test_a_file_without_a_self_signed_root_imports_nothing( name: str, files: dict[str, Path] ) -> None: # An issuing CA alone, or a self-issued certificate another key signed: no root, so refused. - proc = _run_whatif(files[name]) + proc = _run_script(files[name]) out = proc.stdout + proc.stderr assert proc.returncode != 0, out - assert "Nothing was imported. The file holds no self-signed root." in out + assert _error_message(proc).startswith( + "Nothing was imported. The file holds no self-signed root." + ), out assert "What if" not in out _NOT_A_CA = "its basic constraints say it is not a CA" _NO_CERT_SIGN = "its key usage does not allow it to sign certificates" +_UNCHAINED = "it does not chain to a self-signed root in the file" + + +def _pair(cert: x509.Certificate, why: str) -> str: + return f"{_thumb(cert)} ({cert.subject.rfc4514_string()}): {why}" @pytest.mark.parametrize( @@ -415,20 +506,22 @@ def test_a_file_without_a_self_signed_root_imports_nothing( [ ("p7b_with_leaf", [("leaf", _NOT_A_CA)]), ("pem_two_refused", [("leaf", _NOT_A_CA), ("no_cert_sign", _NO_CERT_SIGN)]), + ("pem_expired_root", [("expired_root", "it has expired")]), + ("pem_foreign_intermediate", [("foreign_intermediate", _UNCHAINED)]), + ("pem_name_only_intermediate", [("name_only_intermediate", _UNCHAINED)]), ], ) -def test_a_bundle_holding_a_non_ca_imports_nothing( +def test_a_bundle_holding_a_refused_certificate_imports_nothing( name: str, refused: list[tuple[str, str]], chain: dict[str, tuple[x509.Certificate, _Key]], files: dict[str, Path], ) -> None: - proc = _run_whatif(files[name]) + proc = _run_script(files[name]) out = proc.stdout + proc.stderr assert proc.returncode != 0, out - assert "Nothing was imported" in out - thumbs = [_thumb(chain[key][0]) for key, _why in refused] - # The printed list holds each reason as a whole line, in file order, and nothing else: pwsh's + pairs = [_pair(chain[key][0], why) for key, why in refused] + # The printed list holds each refusal as a whole line, in file order, and nothing else: pwsh's # error view rewraps at the console width, and at a Linux runner's 80 columns that split a # thumbprint from its reason. lines = proc.stdout.splitlines() @@ -436,18 +529,59 @@ def test_a_bundle_holding_a_non_ca_imports_nothing( listed = itertools.takewhile( lambda ln: ln.startswith(" "), lines[lines.index("Refused:") + 1 :] ) - assert list(listed) == [ - f" {t}: {why}" for t, (_key, why) in zip(thumbs, refused, strict=True) - ], out - # The error names every refused thumbprint, in order, and not the root CA beside them. The - # error view's source-line excerpt shows code, not a thumbprint, so this reads the message. - at = [proc.stderr.find(t) for t in thumbs] - assert -1 not in at and at == sorted(at), proc.stderr - assert _thumb(chain["root"][0]) not in proc.stderr, proc.stderr + assert list(listed) == [f" {p}" for p in pairs], out + # The error's own message carries every pair, worded as the list words it, in order, and not + # the root CA beside them. + message = _error_message(proc) + assert message.startswith("Nothing was imported."), message + at = [message.find(p) for p in pairs] + assert -1 not in at and at == sorted(at), message + assert _thumb(chain["root"][0]) not in message, message # Refused before a single import is offered, even the CA beside it. assert "What if" not in out +def test_the_error_names_the_first_five_refusals_and_counts_the_rest( + tmp_path: Path, chain: dict[str, tuple[x509.Certificate, _Key]] +) -> None: + leaves = [_cert(f"db{i}.test.invalid", False, chain["root"])[0] for i in range(7)] + path = tmp_path / "seven_leaves.pem" + path.write_bytes(_pem(chain["root"][0], *leaves)) + proc = _run_script(path) + assert proc.returncode != 0, proc.stdout + proc.stderr + message = _error_message(proc) + for leaf in leaves[:5]: + assert _pair(leaf, _NOT_A_CA) in message, message + for leaf in leaves[5:]: + assert _thumb(leaf) not in message, message + # Still listed for the operator. + assert f" {_pair(leaf, _NOT_A_CA)}" in proc.stdout.splitlines(), proc.stdout + assert message.endswith("; and 2 more, listed above."), message + + +def test_a_listed_refused_certificate_names_no_store( + chain: dict[str, tuple[x509.Certificate, _Key]], files: dict[str, Path] +) -> None: + proc = _run_script(files["p7b_with_leaf"]) + lines = proc.stdout.splitlines() + at = lines.index(f" Thumbprint : {_thumb(chain['leaf'][0])}") + block = list(itertools.takewhile(lambda ln: ln.startswith(" "), lines[at:])) + assert f" Refused : {_NOT_A_CA}" in block, block + assert not any(ln.startswith(" Store") for ln in block), block + # The validity period is printed in UTC, so its 'Z' suffix is true. + not_after = chain["leaf"][0].not_valid_after_utc.strftime("%Y-%m-%d %H:%M:%SZ") + assert f" Not after : {not_after}" in block, block + + +def test_an_unelevated_import_is_refused_before_the_file_is_read(files: dict[str, Path]) -> None: + if sys.platform != "win32": + pytest.skip("SKIP (nothing run): the elevation check reads a Windows token") + proc = _run_script(files["der"], whatif=False) + assert proc.returncode != 0, proc.stdout + proc.stderr + assert "requires an elevated (Administrator) PowerShell" in _error_message(proc) + assert "The file holds" not in proc.stdout + + def test_the_script_adds_through_the_store_and_never_by_path() -> None: code = "\n".join( ln @@ -463,4 +597,5 @@ def test_the_script_adds_through_the_store_and_never_by_path() -> None: # single import is offered. assert body.index("IsInRole(") < body.index("ReadAllBytes(") assert body.index("Get-CaRefusal -Certificate") < body.index("ShouldProcess(") - assert body.index("ShouldProcess(") < body.index(".Store.Add(") + assert body.index("Get-ChainRefusal -Certificate") < body.index("ShouldProcess(") + assert body.index("ShouldProcess(") < body.index("$store.Add(") From 48562cfade96efc9dbe3eb724ab2a1c492650ba8 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 02:46:03 -0600 Subject: [PATCH 03/19] Service scripts: review round 1 fixes for import-db-ca.ps1 and measure-store-access.ps1 import-db-ca.ps1: - an approved CA must reach an approved root, so declining a root under -Confirm no longer imports a rootless issuing CA; - a signature the script does not verify (RSA-PSS, SHA-224) is refused by name instead of reading as a missing root; - a certificate not valid yet is listed with a note, not refused, so a new CA can be staged; expiry is checked after the CA checks; - a rootless file's error carries the per-certificate refusals too; - help, DEPLOY-SERVER-DB.md, INPUT-SURFACES.md and the crypto inventory text say what is checked; the rotation step keeps a certificate the store held before the import. measure-store-access.ps1: Wait-ForGateRefusal returns at once, with a reading, when the gate logs its clause without refusing, and the failure texts say "logged no ADR 0167 refusal". BACKLOG #5561, #5658 Proposed PR title: Service scripts: import-db-ca.ps1 checks the chain and expiry of each CA; measure-store-access.ps1 counts only the gate refusal (BACKLOG #5561, #5658) Proposed banner #5561: PARTIAL -- each CA must chain to a self-signed root in the file, an approved CA to an approved root, and an expired or unverifiable certificate is refused; roots go first; refusals name the Subject. Items kept in one line each are listed in the PR body. Proposed banner #5658: SHIPPED -- Wait-ForGateRefusal counts a line only when it carries the refusal's own words, with a test against app.py's wording. --- docs/DEPLOY-SERVER-DB.md | 19 +-- docs/INPUT-SURFACES.md | 2 +- scripts/security/crypto_inventory_check.py | 7 +- scripts/service/import-db-ca.ps1 | 134 +++++++++++------- scripts/service/measure-store-access.ps1 | 30 ++-- tests/test_import_db_ca_bundle.py | 47 +++++- .../test_measure_store_access_gate_refusal.py | 16 ++- 7 files changed, 167 insertions(+), 88 deletions(-) diff --git a/docs/DEPLOY-SERVER-DB.md b/docs/DEPLOY-SERVER-DB.md index a88b8da9e2..cdf8d3364f 100644 --- a/docs/DEPLOY-SERVER-DB.md +++ b/docs/DEPLOY-SERVER-DB.md @@ -552,8 +552,8 @@ From an **elevated (Administrator)** PowerShell, use the helper: .\scripts\service\import-db-ca.ps1 -CaPath C:\certs\internal-root-ca.crt ``` -The helper shows every certificate in the file, and refuses the whole file, importing nothing, on -any of the refusals its own help lists. +The helper shows every certificate in the file, and refuses the whole file, importing nothing, on at +least the refusals its own help lists. **Where each CA goes.** This paragraph and the helper's own help are the statement of it; other documents point here. A self-signed root, whose signature verifies under its own key, goes into @@ -565,9 +565,9 @@ engine's Python TLS on Windows: CPython's `ssl.create_default_context()`, which system-trust path calls, loads the `CA` and `ROOT` system stores, and Python 3.13 and later set partial-chain verification, so a chain that reaches a certificate in `LocalMachine\CA` validates there without its root. The helper refuses a file unless it holds a self-signed root and every -other CA in it chains to a self-signed root through the file's own certificates. With `-Confirm` you -may still decline the root and accept another CA, so give it the root of the CA you mean to trust, -alone or in that CA's own `.p7b` chain, and accept every certificate it offers. +other CA in it chains to a self-signed root through the file's own certificates. With `-Confirm`, +declining a root that an accepted CA chains through imports nothing, so give it the root of the CA +you mean to trust, alone or in that CA's own `.p7b` chain, and accept every certificate it offers. The built-ins below check none of that, so use them only for a file holding one self-signed root CA you have checked yourself: @@ -600,10 +600,11 @@ connection fails validation** (NIST SP 800-52r2: rotate certificates before expi 2. **Roll the DB server certificate** to one issued by the new CA (a DB-administrator action). Because both CAs are trusted, connections keep validating across the swap. 3. **Remove the old CA** once every host trusts the new one and the server cert has rolled: - - **SQL Server:** delete every certificate the old CA's file added, the root from - `LocalMachine\Root` and any issuing CA from `LocalMachine\CA`, by the thumbprints the helper - listed when it imported that file. An issuing CA left in `LocalMachine\CA` stays a trust anchor - for the engine's Python TLS (§5.2). For example + - **SQL Server:** delete the old CA's certificates, the root from `LocalMachine\Root` and any + issuing CA from `LocalMachine\CA`, by the thumbprints the helper listed when it imported that + file. An issuing CA left in `LocalMachine\CA` stays a trust anchor for the engine's Python TLS + (§5.2). The helper lists a certificate the store already held too, so keep any that something + else installed or still needs (Group Policy, say). For example `Get-ChildItem Cert:\LocalMachine\Root, Cert:\LocalMachine\CA | Where-Object Thumbprint -in @('', '') | Remove-Item`. - **PostgreSQL:** drop the old CA from the PEM bundle and reload. diff --git a/docs/INPUT-SURFACES.md b/docs/INPUT-SURFACES.md index 862d73836c..28ed0118a2 100644 --- a/docs/INPUT-SURFACES.md +++ b/docs/INPUT-SURFACES.md @@ -523,7 +523,7 @@ help. Each rule is in the script's `param()` block or its body. | `uninstall-net-helper.ps1` | `-ServiceName` | The `-ServiceName` rule above | | `measure-store-access.ps1` | `-Order` | Required. One of `ProvisionFirst`, `StartFirst`, `StartFirstEndToEnd` | | | `-DataDir` | Required | -| `import-db-ca.ps1` | `-CaPath` | Required. A certificate file holding a self-signed root. The help block at the top of the script, which `Get-Help` does not print, as the paragraph above says, lists the shapes it reads and what it refuses | +| `import-db-ca.ps1` | `-CaPath` | Required. A certificate file holding a self-signed root. The help block at the top of the script, which `Get-Help` does not print, as the paragraph above says, lists the shapes it reads and, with "at least", what it refuses | `install-service.ps1` also relates parameters to each other. `-EngineWheel` refuses `-AppExe` and `-PythonExe`. `-EngineWheelSha256`, `-RequirementsLock`, `-RequirementsLockSha256` and diff --git a/scripts/security/crypto_inventory_check.py b/scripts/security/crypto_inventory_check.py index 092ae36505..604e68cf3a 100644 --- a/scripts/security/crypto_inventory_check.py +++ b/scripts/security/crypto_inventory_check.py @@ -2928,9 +2928,10 @@ def check_non_python_operations(repo: Path) -> tuple[list[str], int, dict[str, f POWERSHELL_OPERATION_INVENTORY: dict[str, frozenset[str]] = { # --- The operator deployment path (docs/SERVICE.md). These two are the reason this arm exists. --- # Parses the operator's CA certificate file into a collection (so a malformed file fails before - # anything is touched and the operator sees every certificate they are about to trust), checks - # whether each one's signature verifies under its own key (to tell a self-signed root from an - # issuing CA), then INSTALLS each one through X509Store into a machine store (docs/DEPLOY-SERVER-DB.md + # anything is touched and the operator sees every certificate they are about to trust), verifies + # each one's signature under its own key (to tell a self-signed root from an issuing CA) and + # under the keys of the file's other certificates (to check that each CA chains to a root in the + # file; RSA PKCS#1 v1.5 and ECDSA with SHA-1, SHA-256, SHA-384 or SHA-512), then INSTALLS each one through X509Store into a machine store (docs/DEPLOY-SERVER-DB.md # section 5.2 says which). A trust-anchor decision for the whole host, not only the engine. "scripts/service/import-db-ca.ps1": frozenset( {"key_cert:X509", "key_cert:X509Store", "sign_verify:SignData"} diff --git a/scripts/service/import-db-ca.ps1 b/scripts/service/import-db-ca.ps1 index e6e4d475af..cdae34ae56 100644 --- a/scripts/service/import-db-ca.ps1 +++ b/scripts/service/import-db-ca.ps1 @@ -31,8 +31,8 @@ reaches a certificate in LocalMachine\CA validates there without its root. A file is refused unless it holds a self-signed root and every other CA in it chains to a self-signed root through the file's own certificates, each link's signature verified. No revocation, name - constraint or path length is checked, and with -Confirm the operator may still decline the - root and accept another CA. + constraint or path length is checked. With -Confirm the operator may decline a root, but then + nothing is imported if an approved CA chains only through it. PostgreSQL can alternatively pin a CA by file via [store].ssl_root_cert (no machine import needed). This machine-store import is the path SQL Server REQUIRES; Postgres can use either. @@ -41,14 +41,15 @@ nothing reaches a store that the operator was not shown. Roots are added first. The whole file is refused, and nothing is imported, when any certificate in it is not marked as a CA by its basic constraints, has a key usage that cannot sign certificates, carries a private key, - or is outside its validity period, when it holds no self-signed root, when a CA in it does not - chain to a self-signed root in the file, when a DER value in it (the whole file, a PEM block, - or decoded base64) does not end where its bytes end, or when a control character other than - tab, CR or LF sits outside its PEM blocks. A BER value with an indefinite length, which some - tools write for a .p7b, is refused by that DER rule; re-encode it as DER first. Only RSA - PKCS#1 v1.5 and ECDSA signatures with SHA-1 or SHA-2 are verified, so a root or a chain link - signed any other way (RSA-PSS, for one) is refused. Other text outside PEM blocks, bare base64 - among them, is ignored, not refused. At least a .pfx, and + is signed in a way this script does not verify, or has expired, when it holds no self-signed + root, when a CA in it does not chain to a self-signed root in the file, when a DER value in it + (the whole file, a PEM block, or decoded base64) does not end where its bytes end, or when a + control character other than tab, CR or LF sits outside its PEM blocks. Only RSA PKCS#1 v1.5 + and ECDSA signatures with SHA-1, SHA-256, SHA-384 or SHA-512 are verified; RSA-PSS and + SHA-224, for two, are not. A BER value with an indefinite length, which some tools write for a + .p7b, is refused by the DER rule; re-encode it as DER first. A certificate not valid yet is + listed with a note, not refused, so a new CA can be staged ahead of its start. Other text + outside PEM blocks, bare base64 among them, is ignored, not refused. At least a .pfx, and a well-formed PEM block with any label but CERTIFICATE, X509 CERTIFICATE or PKCS7 (such as a PRIVATE KEY block), are refused before anything is parsed. Key material outside well-formed PEM armour is not looked for; no private key is ever loaded either way. @@ -307,25 +308,28 @@ function Read-CaBundle { return , $unique } +# The hash of each signature algorithm this script can verify, by OID: RSA PKCS#1 v1.5 and ECDSA, +# each with SHA-1, SHA-256, SHA-384 or SHA-512. Any other (RSA-PSS, SHA-224, Ed25519 and DSA among +# them) is not verified, and Get-CaRefusal refuses a certificate signed with one. +$SignatureHashes = @{ + '1.2.840.113549.1.1.5' = 'SHA1'; '1.2.840.113549.1.1.11' = 'SHA256' + '1.2.840.113549.1.1.12' = 'SHA384'; '1.2.840.113549.1.1.13' = 'SHA512' + '1.2.840.10045.4.1' = 'SHA1'; '1.2.840.10045.4.3.2' = 'SHA256' + '1.2.840.10045.4.3.3' = 'SHA384'; '1.2.840.10045.4.3.4' = 'SHA512' +} + # Whether $Signer issued the certificate: the certificate's issuer is $Signer's subject, byte for -# byte, AND its signature verifies under $Signer's public key. RSA PKCS#1 v1.5 and ECDSA with SHA-1 -# or SHA-2 are checked; any other signature algorithm (RSA-PSS, SHA-224, Ed25519 and DSA among them) -# reads as not signed, which fails closed: such a certificate is never a root, and never a link. +# byte, AND its signature verifies under $Signer's public key. A signature algorithm outside +# $SignatureHashes reads as not signed, which fails closed. function Test-SignedBy { param( [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate, [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Signer) if ([Convert]::ToBase64String($Signer.SubjectName.RawData) -cne [Convert]::ToBase64String($Certificate.IssuerName.RawData)) { return $false } - $hashes = @{ - '1.2.840.113549.1.1.5' = 'SHA1'; '1.2.840.113549.1.1.11' = 'SHA256' - '1.2.840.113549.1.1.12' = 'SHA384'; '1.2.840.113549.1.1.13' = 'SHA512' - '1.2.840.10045.4.1' = 'SHA1'; '1.2.840.10045.4.3.2' = 'SHA256' - '1.2.840.10045.4.3.3' = 'SHA384'; '1.2.840.10045.4.3.4' = 'SHA512' - } $oid = $Certificate.SignatureAlgorithm.Value - if (-not $hashes.ContainsKey($oid)) { return $false } - $hash = [System.Security.Cryptography.HashAlgorithmName]::new($hashes[$oid]) + if (-not $SignatureHashes.ContainsKey($oid)) { return $false } + $hash = [System.Security.Cryptography.HashAlgorithmName]::new($SignatureHashes[$oid]) # Certificate ::= SEQUENCE { tbsCertificate, signatureAlgorithm, signatureValue BIT STRING }. $raw = $Certificate.RawData $outer = Read-DerHeader -Bytes $raw @@ -421,17 +425,18 @@ function Get-CaStoreName { # extension counts as not a CA here. RFC 5280 (the Basic Constraints section) requires the extension # of a version 3 CA certificate; a version 1 root, which cannot carry it, is refused too, by this # script's choice rather than the RFC's. A key usage extension that leaves out certificate signing -# also counts as not a CA (RFC 5280, the Key Usage section). A certificate outside its validity -# period at $NowUtc is refused as well: nothing that chains through it validates, so importing it -# is only ever a mistake, and an expired root must not stand in for the one a CA chains to. +# also counts as not a CA (RFC 5280, the Key Usage section). A signature this script cannot verify +# is refused by name, so a root signed that way is not reported as missing. A certificate that has +# expired at $NowUtc is refused as well: nothing that chains through it validates, so importing it +# is only ever a mistake, and an expired root must not stand in for the one a CA chains to. A +# certificate not valid yet is not refused here; the listing warns of it, since a new CA may be +# staged ahead of its start for a make-before-break rotation. function Get-CaRefusal { param( [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate, [datetime]$NowUtc = [datetime]::UtcNow) # Read-CaBundle already refuses every container that can carry a key; this holds if it ever stops. if ($Certificate.HasPrivateKey) { return 'it carries a private key' } - if ($Certificate.NotAfter.ToUniversalTime() -lt $NowUtc) { return 'it has expired' } - if ($Certificate.NotBefore.ToUniversalTime() -gt $NowUtc) { return 'it is not valid yet' } $constraints = @($Certificate.Extensions | Where-Object { $_ -is [System.Security.Cryptography.X509Certificates.X509BasicConstraintsExtension] }) if ($constraints.Count -eq 0) { return 'it has no basic constraints extension, so it is not marked as a CA' } @@ -445,6 +450,11 @@ function Get-CaRefusal { return 'its key usage does not allow it to sign certificates' } } + $algorithm = $Certificate.SignatureAlgorithm + if (-not $SignatureHashes.ContainsKey($algorithm.Value)) { + return "it is signed with $($algorithm.FriendlyName) ($($algorithm.Value)), which this script does not verify" + } + if ($Certificate.NotAfter.ToUniversalTime() -lt $NowUtc) { return 'it has expired' } return $null } @@ -484,6 +494,9 @@ $entries = @(foreach ($cert in $bundle) { Write-Host " Thumbprint : $($cert.Thumbprint)" Write-Host " Not before : $($cert.NotBefore.ToUniversalTime().ToString('u'))" Write-Host " Not after : $($cert.NotAfter.ToUniversalTime().ToString('u'))" + if ($cert.NotBefore.ToUniversalTime() -gt $nowUtc) { + Write-Host " Not valid yet: nothing that chains through it validates before its start." + } if ($why) { Write-Host " Refused : $why" $refusals.Add("$($cert.Thumbprint) ($(ConvertTo-PrintableText $cert.Subject)): $why") @@ -492,28 +505,33 @@ $entries = @(foreach ($cert in $bundle) { } @{ Certificate = $cert; Target = $target } }) -if (-not ($entries | Where-Object { $_.Target -eq $stores['Root'] })) { - # Without its root an issuing CA validates nothing over SChannel, and over the engine's Python TLS - # it would be trusted with no root's revocation or constraints over it (the help's "Where each - # CA goes"), so a rootless file is refused rather than imported. Checked first, since every CA - # in such a file is also refused for reaching no root, and this message says why in one line. - throw ("Nothing was imported. The file holds no self-signed root. Give this script the root " + - "the CA chains to, alone or in the same .p7b chain.") -} -if ($refusals.Count -gt 0) { +# Without its root an issuing CA validates nothing over SChannel, and over the engine's Python TLS +# it would be trusted with no root's revocation or constraints over it (the help's "Where each CA +# goes"), so a rootless file is refused rather than imported. Every CA in such a file is also refused +# for reaching no root, so the error leads with the one cause that explains them. +$rootless = -not ($entries | Where-Object { $_.Target -eq $stores['Root'] }) +if ($rootless -or $refusals.Count -gt 0) { # Printed one per line for the operator: pwsh 7's default error view rewraps a message at the # console width, which can split a line. The error carries the same lines, joined into its one # message, for a caller that catches it or keeps only the error stream; past the first few it # gives the count instead. - Write-Host "" - Write-Host "Refused:" - foreach ($line in $refusals) { Write-Host " $line" } - $shown = 5 - $named = ($refusals | Select-Object -First $shown) -join '; ' - if ($refusals.Count -gt $shown) { $named += "; and $($refusals.Count - $shown) more, listed above" } - throw ("Nothing was imported. Every certificate in the file must be a CA, within its validity " + - "period, that may sign certificates, carries no private key, and chains to a self-signed " + - "root in the file. Refused: $named.") + $named = '' + if ($refusals.Count -gt 0) { + Write-Host "" + Write-Host "Refused:" + foreach ($line in $refusals) { Write-Host " $line" } + $shown = 5 + $named = " Refused: " + (($refusals | Select-Object -First $shown) -join '; ') + if ($refusals.Count -gt $shown) { $named += "; and $($refusals.Count - $shown) more, listed above" } + $named += "." + } + if ($rootless) { + throw ("Nothing was imported. The file holds no self-signed root. Give this script the root " + + "the CA chains to, alone or in the same .p7b chain.$named") + } + throw ("Nothing was imported. Every certificate in the file must be a CA that may sign " + + "certificates, carries no private key, is signed in a way this script verifies, has not " + + "expired, and chains to a self-signed root in the file.$named") } # --- import ------------------------------------------------------------------ @@ -524,6 +542,19 @@ $approved = @(foreach ($entry in $entries) { $entry } }) +# Under -Confirm the operator may decline a root. An approved CA must still reach a root through the +# approved certificates alone, or it would land in LocalMachine\CA with no root over it. +$approvedSet = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection +foreach ($entry in $approved) { [void]$approvedSet.Add($entry.Certificate) } +$orphans = @(foreach ($entry in $approved) { + if (Get-ChainRefusal -Certificate $entry.Certificate -Bundle $approvedSet) { + "$($entry.Certificate.Thumbprint) ($(ConvertTo-PrintableText $entry.Certificate.Subject))" + } +}) +if ($orphans.Count -gt 0) { + throw ("Nothing was imported. A root was declined that these approved CAs chain to, and a CA " + + "is not imported without its root: $($orphans -join '; ').") +} if ($approved.Count -gt 0) { # Each store keys on thumbprint, so re-importing the same CA is a no-op (idempotent). Roots go @@ -551,16 +582,11 @@ if ($approved.Count -gt 0) { } Write-Host "" Write-Host "Imported $added CA certificate(s)." - if ($approved | Where-Object { $_.Target -eq $stores['Root'] }) { - Write-Host "A DB server cert chaining to the imported root now validates with" -ForegroundColor Green - Write-Host "trust_server_certificate = false (no TrustServerCertificate=true needed)." -ForegroundColor Green - } else { - Write-Warning ("No root was imported. Over SChannel and ODBC a DB server cert validates only if " + - "its root is already in $StoreLocation; the engine's Python TLS treats LocalMachine\CA " + - "differently (see this script's help, 'Where each CA goes').") - } + # Every approved CA reached an approved root above, so a root is among what was imported. + Write-Host "A DB server cert chaining to the imported root now validates with" -ForegroundColor Green + Write-Host "trust_server_certificate = false (no TrustServerCertificate=true needed)." -ForegroundColor Green Write-Host "" Write-Host "Rotation: when the CA is replaced, import the NEW CA first (this leaves both trusted)," - Write-Host "roll the server cert, then remove every certificate the OLD file added, by the" - Write-Host "thumbprints this script listed for it -- see docs/DEPLOY-SERVER-DB.md." + Write-Host "roll the server cert, then remove the OLD file's certificates by the thumbprints listed" + Write-Host "above, keeping any that was in the store before -- see docs/DEPLOY-SERVER-DB.md." } diff --git a/scripts/service/measure-store-access.ps1 b/scripts/service/measure-store-access.ps1 index 8e19f6ffb9..74eeec7dc7 100644 --- a/scripts/service/measure-store-access.ps1 +++ b/scripts/service/measure-store-access.ps1 @@ -646,14 +646,19 @@ function Wait-ForGateRefusal([string]$Phase) { Wait until the service logs the ADR 0167 refusal (Test-GateRefusalLine), which it reaches only after opening the store and reading its users. Samples process owners throughout, since NSSM restarts the refused engine and each restart is a new process. Returns $true on the refusal, - $false on timeout. + $false on timeout, and $false at once, with a reading quoting it, when the gate's clause is + logged without the refusal: the engine read its users and the gate let the start go on. #> $deadline = (Get-Date).AddSeconds($WaitSeconds) while ((Get-Date) -lt $deadline) { Update-OwnerSample "during $Phase" - $refused = @(Get-LogMatches ([regex]::Escape($GateRefusal)) | - Where-Object { Test-GateRefusalLine $_.Line }) - if ($refused.Count -gt 0) { return $true } + $gate = @(Get-LogMatches ([regex]::Escape($GateRefusal))) + if (@($gate | Where-Object { Test-GateRefusalLine $_.Line }).Count -gt 0) { return $true } + if ($gate.Count -gt 0) { + Add-Reading ("during $Phase the service reached the ADR 0167 gate, which did not refuse " + + "the start: $(Get-Excerpt $gate[0].Line 300)") + return $false + } Start-Sleep -Seconds 1 } return $false @@ -979,18 +984,19 @@ try { $noAdmin = @(Get-LogMatches ([regex]::Escape($NoAdminRefusal)) | Where-Object { Test-GateRefusalLine $_.Line $NoAdminRefusal }) if (-not $opened) { - $what = if (Test-Path -LiteralPath $DbPath) { "created $DbPath but never reached the account gate, so its open is unproven" } + $what = if (Test-Path -LiteralPath $DbPath) { "created $DbPath but logged no ADR 0167 refusal (a reading above says if the gate let it start)" } else { "never created $DbPath" } Add-Failure ("$ServiceIdentity $what within $WaitSeconds s on a fresh store. " + (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) Show-LogTail } elseif ($noAdmin.Count -eq 0) { - $line = @(Get-LogMatches ([regex]::Escape($GateRefusal)) | Select-Object -First 1 | + $line = @(Get-LogMatches ([regex]::Escape($GateRefusal)) | + Where-Object { Test-GateRefusalLine $_.Line } | Select-Object -First 1 | ForEach-Object { Get-Excerpt $_.Line 300 }) - Add-Failure ("$ServiceIdentity reached the ADR 0167 gate on a fresh store, but no line said " + - "both '$StartRefused' and '$NoAdminRefusal'. Either the start was not refused, or it " + - "was refused for a missing address, which means the first start created an account no " + - "operator named. Gate line: $($line -join ' ')") + Add-Failure ("$ServiceIdentity was refused by the ADR 0167 gate on a fresh store, but not " + + "in the words '$NoAdminRefusal'. A refusal for a missing address would mean the first " + + "start created an account no operator named; the refusal line says which. " + + "Refusal line: $($line -join ' ')") Show-LogTail } elseif (@($noAdmin | Where-Object { $_.Line -match [regex]::Escape($ProvisionHint) }).Count -eq 0) { Add-Failure ("$ServiceIdentity was refused for want of an Administrator, but the refusal " + @@ -1032,7 +1038,7 @@ try { Stop-TheService Show-Trio "service first start" if (-not $opened) { - $what = if (Test-Path -LiteralPath $DbPath) { "created $DbPath but never reached the account gate, so its open is unproven" } + $what = if (Test-Path -LiteralPath $DbPath) { "created $DbPath but logged no ADR 0167 refusal (a reading above says if the gate let it start)" } else { "never created $DbPath" } Add-Failure ("$ServiceIdentity $what within $WaitSeconds s on a fresh store. " + (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) @@ -1084,7 +1090,7 @@ try { Stop-TheService if (-not $reopened) { Add-Failure ("$ServiceIdentity could not open $DbPath after $Operator's open " + - "re-secured it: the restart never reached the account gate in $WaitSeconds s. " + + "re-secured it: the restart logged no ADR 0167 refusal in $WaitSeconds s. " + (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) Show-LogTail } else { diff --git a/tests/test_import_db_ca_bundle.py b/tests/test_import_db_ca_bundle.py index cc4376afff..93fb9b346b 100644 --- a/tests/test_import_db_ca_bundle.py +++ b/tests/test_import_db_ca_bundle.py @@ -12,11 +12,12 @@ NOTHING HERE WRITES A CERTIFICATE STORE. The fixtures are generated in a pytest temporary directory. The function-level tests lift ``Read-CaBundle``, ``Get-CaRefusal`` and ``Get-CaStoreName`` (and the console helpers they call) out of the script's AST and call them, as -``tests/test_import_db_ca_console_text.py`` does. The whole-script tests run it with ``-WhatIf`` -only, where ``ShouldProcess`` declines every import, so the store is never opened; they are also -skipped in an elevated session, so even a regression that ignored ``-WhatIf`` would meet the -script's own elevation refusal rather than the machine Root store. Both halves need ``pwsh`` and -skip where there is none. +``tests/test_import_db_ca_console_text.py`` does. The whole-script tests are skipped in an elevated +session. All but one run it with ``-WhatIf``, where ``ShouldProcess`` declines every import, so the +store is never opened, and even a regression that ignored ``-WhatIf`` would meet the script's own +elevation refusal rather than the machine Root store. The one without ``-WhatIf`` exists to meet +that refusal, on Windows only; since hosted Windows runners are elevated, it runs only on an +unelevated developer box. Both halves need ``pwsh`` and skip where there is none. """ from __future__ import annotations @@ -35,7 +36,7 @@ import pytest from cryptography import x509 from cryptography.hazmat.primitives import hashes, serialization -from cryptography.hazmat.primitives.asymmetric import ec, rsa +from cryptography.hazmat.primitives.asymmetric import ec, padding, rsa from cryptography.hazmat.primitives.serialization import pkcs7, pkcs12 from cryptography.x509.oid import NameOID @@ -162,9 +163,29 @@ def build(name: x509.Name, public: object, signer: object) -> x509.Certificate: rsa_key = rsa.generate_private_key(public_exponent=65537, key_size=2048) rsa_name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Synthetic RSA Root CA")]) new_key = ec.generate_private_key(ec.SECP384R1()) + pss_name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Synthetic RSA-PSS Root CA")]) + pss_root = ( + x509.CertificateBuilder() + .subject_name(pss_name) + .issuer_name(pss_name) + .public_key(rsa_key.public_key()) + .serial_number(x509.random_serial_number()) + .not_valid_before(now - datetime.timedelta(days=1)) + .not_valid_after(now + datetime.timedelta(days=30)) + .add_extension(x509.BasicConstraints(ca=True, path_length=None), True) + .sign( + rsa_key, + hashes.SHA256(), + rsa_padding=padding.PSS( + mgf=padding.MGF1(hashes.SHA256()), salt_length=padding.PSS.DIGEST_LENGTH + ), + ) + ) return { "rsa_root": build(rsa_name, rsa_key.public_key(), rsa_key), "rekey": build(root.subject, new_key.public_key(), root_key), + # Self-signed, but with a signature the script does not verify. + "pss_root": pss_root, } @@ -234,6 +255,7 @@ def files( # Outside the validity period: an expired root beside a valid one, and a root not valid yet. "pem_expired_root": _pem(root, chain["expired_root"][0]), "pem_future_root": _pem(chain["future_root"][0]), + "pem_pss_root": _pem(extra["pss_root"]), # Bare base64 behind a byte-order mark, UTF-8 and UTF-16. "b64_utf8_bom": b"\xef\xbb\xbf" + base64.encodebytes(root.public_bytes(der)), "b64_utf16_bom": b"\xff\xfe" @@ -269,6 +291,10 @@ def _read_all(tmp_path: Path, paths: dict[str, Path]) -> dict[str, dict[str, obj $ErrorActionPreference = 'Stop' $ast = [System.Management.Automation.Language.Parser]::ParseFile( {_psq(str(_SCRIPT))}, [ref]$null, [ref]$null) +$set = $ast.Find({{ $args[0] -is [System.Management.Automation.Language.AssignmentStatementAst] -and + $args[0].Left.Extent.Text -eq '$SignatureHashes' }}, $false) +if (-not $set) {{ throw 'missing assignment $SignatureHashes' }} +. ([scriptblock]::Create($set.Extent.Text)) foreach ($name in @('Test-EscapedForConsole', 'ConvertTo-PrintableText', 'Read-DerHeader', 'Read-CaBundle', 'Get-CaRefusal', 'Test-SignedBy', 'Test-SelfSigned', 'Get-ChainRefusal', 'Get-CaStoreName')) {{ @@ -374,7 +400,14 @@ def certs(name: str) -> list[tuple[str, str | None]]: expired = dict(certs("pem_expired_root")) assert expired[_thumb(chain["expired_root"][0])] == "it has expired" assert expired[root] is None - assert certs("pem_future_root") == [(_thumb(chain["future_root"][0]), "it is not valid yet")] + # Not valid yet is not refused: a new CA may be staged ahead of its start. + assert certs("pem_future_root") == [(_thumb(chain["future_root"][0]), None)] + # A signature the script cannot verify is refused by name, not read as a missing root. + pss = certs("pem_pss_root") + assert len(pss) == 1 and pss[0][0] == _thumb(extra["pss_root"]), pss + assert str(pss[0][1]).startswith("it is signed with ") and "1.2.840.113549.1.1.10" in str( + pss[0][1] + ), pss # Each CA must chain to a self-signed root through the file's own certificates: an issuing CA # and a re-key certificate that do pass, and one whose issuer is not in the file, or only diff --git a/tests/test_measure_store_access_gate_refusal.py b/tests/test_measure_store_access_gate_refusal.py index 96707b8ec4..f433b718f6 100644 --- a/tests/test_measure_store_access_gate_refusal.py +++ b/tests/test_measure_store_access_gate_refusal.py @@ -11,7 +11,8 @@ NOTHING HERE RUNS THE SCRIPT: it installs a service and must run elevated, which only the hosted windows-service-smoke leg does. The test lifts the two phrase assignments and ``Test-GateRefusalLine`` out of the script's AST and calls the function on lines worded as the -engine words them, and pins that the engine still words them so. It needs ``pwsh`` and skips where +engine words them. It pins that ``app.py`` still holds each line's prefix and the clause text after +it, as literals; it does not render the lines from ``app.py``. It needs ``pwsh`` and skips where there is none. """ @@ -60,7 +61,7 @@ def _judge(host: str, tmp_path: Path, lines: list[str]) -> dict[str, object]: $errs = $null $ast = [System.Management.Automation.Language.Parser]::ParseFile({_psq(str(_SCRIPT))}, [ref]$null, [ref]$errs) if (@($errs).Count) {{ throw "the script does not parse: $($errs[0])" }} -foreach ($var in @('GateRefusal', 'StartRefused')) {{ +foreach ($var in @('GateRefusal', 'NoAdminRefusal', 'StartRefused')) {{ $set = $ast.Find({{ $args[0] -is [System.Management.Automation.Language.AssignmentStatementAst] -and $args[0].Left.Extent.Text -eq ('$' + $var) }}, $false) if (-not $set) {{ throw "missing assignment `${{var}}" }} @@ -73,6 +74,7 @@ def _judge(host: str, tmp_path: Path, lines: list[str]) -> dict[str, object]: $lines = {_psq(json.dumps(lines))} | ConvertFrom-Json ConvertTo-Json -Compress -InputObject @{{ refusal = @($lines | ForEach-Object {{ [bool](Test-GateRefusalLine $_) }}) + no_admin = @($lines | ForEach-Object {{ [bool](Test-GateRefusalLine $_ $NoAdminRefusal) }}) clause = @($lines | ForEach-Object {{ $_ -match [regex]::Escape($GateRefusal) }}) }} """ f = tmp_path / f"gate-{uuid.uuid4().hex}.ps1" @@ -97,12 +99,22 @@ def test_the_engine_still_words_the_three_gate_lines_so() -> None: for prefix, line in (_REFUSAL, _SKIPPED, _WARN_MODE): assert prefix in app, prefix assert prefix in line + # The clauses the script matches on, as app.py spells the detail text after each prefix. The + # no-Administrator detail is split across an f-string, so its two halves are pinned apart. + assert '"no enabled Administrator{' in app + assert '"exists in this store, so no enabled account holds the role "' in app + assert ( + '"no enabled Administrator has a notification address, so every out-of-band security "' + in app + ) @pytest.mark.parametrize("host", ["pwsh", "powershell"]) def test_only_the_refusal_counts_as_a_refused_start(tmp_path: Path, host: str) -> None: got = _judge(host, tmp_path, [_REFUSAL[1], _SKIPPED[1], _WARN_MODE[1], "unrelated line"]) assert got["refusal"] == [True, False, False, False], got + # The narrower clause the StartFirstEndToEnd arm passes: the no-Administrator refusal only. + assert got["no_admin"] == [True, False, False, False], got # The control: the clause the wait used to match alone is in both warning lines, so the old # criterion took an engine that went on to serve for a refused one. assert got["clause"] == [True, True, True, False], got From 80ed1a9ae043702c69944dcb6c34fa7f170eaa38 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 03:20:11 -0600 Subject: [PATCH 04/19] Service scripts: review round 2 fixes for import-db-ca.ps1 and measure-store-access.ps1 import-db-ca.ps1: - a self-issued root refused for an unverifiable signature no longer reads as "no self-signed root"; - a certificate the store already held is named "Already in" and counted apart from those added; - the -Confirm refusal names a declined root or intermediate; - the rotation hint says to keep what the new chain or anything else needs; - a not-valid-yet certificate warns on the warning stream again; - the help's refusal list reads "at least". measure-store-access.ps1: only the gate's own two let-it-start lines end the wait early; another line carrying the clause does not. BACKLOG #5561, #5658 Proposed PR title: Service scripts: import-db-ca.ps1 checks the chain and expiry of each CA; measure-store-access.ps1 counts only the gate refusal (BACKLOG #5561, #5658) Proposed banner #5561: PARTIAL -- each CA must chain to a self-signed root in the file, an approved CA to an approved root, and an expired or unverifiable certificate is refused; roots go first; refusals name the Subject. Items kept in one line each are listed in the PR body. Proposed banner #5658: SHIPPED -- Wait-ForGateRefusal counts a line only when it carries the refusal's own words, with a test against app.py's wording. --- docs/DEPLOY-SERVER-DB.md | 4 +- scripts/service/import-db-ca.ps1 | 53 ++++++++++++++----- scripts/service/measure-store-access.ps1 | 10 +++- tests/test_import_db_ca_bundle.py | 10 ++++ .../test_measure_store_access_gate_refusal.py | 20 +++++-- 5 files changed, 74 insertions(+), 23 deletions(-) diff --git a/docs/DEPLOY-SERVER-DB.md b/docs/DEPLOY-SERVER-DB.md index cdf8d3364f..5e8942370d 100644 --- a/docs/DEPLOY-SERVER-DB.md +++ b/docs/DEPLOY-SERVER-DB.md @@ -603,8 +603,8 @@ connection fails validation** (NIST SP 800-52r2: rotate certificates before expi - **SQL Server:** delete the old CA's certificates, the root from `LocalMachine\Root` and any issuing CA from `LocalMachine\CA`, by the thumbprints the helper listed when it imported that file. An issuing CA left in `LocalMachine\CA` stays a trust anchor for the engine's Python TLS - (§5.2). The helper lists a certificate the store already held too, so keep any that something - else installed or still needs (Group Policy, say). For example + (§5.2). Keep any certificate the new chain, or something else (Group Policy, say), still needs; + the helper's import names each one the store already held as "Already in". For example `Get-ChildItem Cert:\LocalMachine\Root, Cert:\LocalMachine\CA | Where-Object Thumbprint -in @('', '') | Remove-Item`. - **PostgreSQL:** drop the old CA from the PEM bundle and reload. diff --git a/scripts/service/import-db-ca.ps1 b/scripts/service/import-db-ca.ps1 index cdae34ae56..1b96fdd267 100644 --- a/scripts/service/import-db-ca.ps1 +++ b/scripts/service/import-db-ca.ps1 @@ -39,7 +39,7 @@ The file is read once. Every certificate it holds is shown, checked, and only then added, so nothing reaches a store that the operator was not shown. Roots are added first. The whole - file is refused, and nothing is imported, when any certificate in it is not marked as a CA by + file is refused, and nothing is imported, in at least these cases: when any certificate in it is not marked as a CA by its basic constraints, has a key usage that cannot sign certificates, carries a private key, is signed in a way this script does not verify, or has expired, when it holds no self-signed root, when a CA in it does not chain to a self-signed root in the file, when a DER value in it @@ -384,6 +384,13 @@ function Test-SelfSigned { return (Test-SignedBy -Certificate $Certificate -Signer $Certificate) } +# Whether the certificate's subject and issuer are the same name, byte for byte. Nothing is verified. +function Test-SelfIssued { + param([Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate) + return ([Convert]::ToBase64String($Certificate.SubjectName.RawData) -ceq + [Convert]::ToBase64String($Certificate.IssuerName.RawData)) +} + # Why the certificate does not chain to a self-signed root in the file, or $null when it does. The # walk follows issuer links through the file only, never a machine store: each step needs a # certificate in $Bundle whose subject is the issuer's name and whose key verifies the signature @@ -484,6 +491,7 @@ $refusals = [System.Collections.Generic.List[string]]::new() # A certificate that passes on its own must also reach a root through this file's own certificates, # so a root that signed none of them cannot carry an unrelated issuing CA in beside it. Write-Host "The file holds $($bundle.Count) certificate(s):" +$unverifiedRoot = $false $entries = @(foreach ($cert in $bundle) { $target = $stores[(Get-CaStoreName -Certificate $cert)] $why = Get-CaRefusal -Certificate $cert -NowUtc $nowUtc @@ -495,7 +503,13 @@ $entries = @(foreach ($cert in $bundle) { Write-Host " Not before : $($cert.NotBefore.ToUniversalTime().ToString('u'))" Write-Host " Not after : $($cert.NotAfter.ToUniversalTime().ToString('u'))" if ($cert.NotBefore.ToUniversalTime() -gt $nowUtc) { - Write-Host " Not valid yet: nothing that chains through it validates before its start." + Write-Warning "$($cert.Thumbprint) is not valid yet; nothing that chains through it validates before its start." + } + # A self-issued certificate refused for a signature this script cannot verify may be the file's + # root, so the error must not say the file holds none. + if ($why -and -not $target.Equals($stores['Root']) -and (Test-SelfIssued -Certificate $cert) -and + -not $SignatureHashes.ContainsKey($cert.SignatureAlgorithm.Value)) { + $unverifiedRoot = $true } if ($why) { Write-Host " Refused : $why" @@ -509,7 +523,7 @@ $entries = @(foreach ($cert in $bundle) { # it would be trusted with no root's revocation or constraints over it (the help's "Where each CA # goes"), so a rootless file is refused rather than imported. Every CA in such a file is also refused # for reaching no root, so the error leads with the one cause that explains them. -$rootless = -not ($entries | Where-Object { $_.Target -eq $stores['Root'] }) +$rootless = -not $unverifiedRoot -and -not ($entries | Where-Object { $_.Target -eq $stores['Root'] }) if ($rootless -or $refusals.Count -gt 0) { # Printed one per line for the operator: pwsh 7's default error view rewraps a message at the # console width, which can split a line. The error carries the same lines, joined into its one @@ -542,8 +556,9 @@ $approved = @(foreach ($entry in $entries) { $entry } }) -# Under -Confirm the operator may decline a root. An approved CA must still reach a root through the -# approved certificates alone, or it would land in LocalMachine\CA with no root over it. +# Under -Confirm the operator may decline a root or an intermediate CA. An approved CA must still +# reach a root through the approved certificates alone, or it would land in LocalMachine\CA with no +# root over it. $approvedSet = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection foreach ($entry in $approved) { [void]$approvedSet.Add($entry.Certificate) } $orphans = @(foreach ($entry in $approved) { @@ -552,15 +567,17 @@ $orphans = @(foreach ($entry in $approved) { } }) if ($orphans.Count -gt 0) { - throw ("Nothing was imported. A root was declined that these approved CAs chain to, and a CA " + - "is not imported without its root: $($orphans -join '; ').") + throw ("Nothing was imported. A root or intermediate CA was declined that these approved CAs " + + "chain through, and a CA is not imported without the path to its root: $($orphans -join '; ').") } if ($approved.Count -gt 0) { - # Each store keys on thumbprint, so re-importing the same CA is a no-op (idempotent). Roots go - # first, so a failure part way never leaves an approved issuing CA added and an approved root not. - # Nothing is rolled back; the error says how many were added. + # Each store keys on thumbprint, so re-importing the same CA is a no-op (idempotent). One the + # store already held is named as such, since a later rotation must not remove it as the file's. + # Roots go first, so a failure part way never leaves an approved issuing CA added and an approved + # root not. Nothing is rolled back; the error says how many were added. $added = 0 + $present = 0 try { foreach ($name in 'Root', 'CertificateAuthority') { $batch = @($approved | Where-Object { $_.Target -eq $stores[$name] }) @@ -569,6 +586,14 @@ if ($approved.Count -gt 0) { $store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite) try { foreach ($entry in $batch) { + $thumb = $entry.Certificate.Thumbprint + if ($store.Certificates.Find( + [System.Security.Cryptography.X509Certificates.X509FindType]::FindByThumbprint, + $thumb, $false).Count -gt 0) { + Write-Host "Already in $($stores[$name].Label), left as it was: $thumb" + $present++ + continue + } $store.Add($entry.Certificate) $added++ } @@ -581,12 +606,12 @@ if ($approved.Count -gt 0) { "failed: $($_.Exception.Message)") } Write-Host "" - Write-Host "Imported $added CA certificate(s)." - # Every approved CA reached an approved root above, so a root is among what was imported. + Write-Host "Imported $added CA certificate(s); $present were already in their store." + # Every approved CA reached an approved root above, so a root is now in the Root store. Write-Host "A DB server cert chaining to the imported root now validates with" -ForegroundColor Green Write-Host "trust_server_certificate = false (no TrustServerCertificate=true needed)." -ForegroundColor Green Write-Host "" Write-Host "Rotation: when the CA is replaced, import the NEW CA first (this leaves both trusted)," - Write-Host "roll the server cert, then remove the OLD file's certificates by the thumbprints listed" - Write-Host "above, keeping any that was in the store before -- see docs/DEPLOY-SERVER-DB.md." + Write-Host "roll the server cert, then remove only the old certificates that the new chain and" + Write-Host "nothing else still needs -- see docs/DEPLOY-SERVER-DB.md section 5.3." } diff --git a/scripts/service/measure-store-access.ps1 b/scripts/service/measure-store-access.ps1 index 74eeec7dc7..45e7a2fa3e 100644 --- a/scripts/service/measure-store-access.ps1 +++ b/scripts/service/measure-store-access.ps1 @@ -126,6 +126,9 @@ $ProvisionHint = "provision-admin --username" # phrases above too, so without this an engine that started and served would pass step 1 of every # arm; Test-GateRefusalLine requires both on one line. $StartRefused = "refusing to start" +# The openings of the gate's two lines that let a start go on: the skipped-gate warning and the +# warn-mode line (app.py). A regex, matched as is. +$GateLetStart = "the engine is starting with the notice gate skipped: |PHI instance with no deliverable security-notice recipient: " $ProvisionDecline = "already has an enabled Administrator" $OpenErrorPattern = "unable to open database file|Access is denied|PermissionError|OperationalError" # store.py logs these and carries on; each means that open did not restrict the trio as it tried to @@ -654,9 +657,12 @@ function Wait-ForGateRefusal([string]$Phase) { Update-OwnerSample "during $Phase" $gate = @(Get-LogMatches ([regex]::Escape($GateRefusal))) if (@($gate | Where-Object { Test-GateRefusalLine $_.Line }).Count -gt 0) { return $true } - if ($gate.Count -gt 0) { + # Only the gate's own two let-it-start lines end the wait: other engine lines carry the + # clause too, and say nothing about this gate. + $letStart = @($gate | Where-Object { $_.Line -match $GateLetStart }) + if ($letStart.Count -gt 0) { Add-Reading ("during $Phase the service reached the ADR 0167 gate, which did not refuse " + - "the start: $(Get-Excerpt $gate[0].Line 300)") + "the start: $(Get-Excerpt $letStart[0].Line 300)") return $false } Start-Sleep -Seconds 1 diff --git a/tests/test_import_db_ca_bundle.py b/tests/test_import_db_ca_bundle.py index 93fb9b346b..972c54de5a 100644 --- a/tests/test_import_db_ca_bundle.py +++ b/tests/test_import_db_ca_bundle.py @@ -574,6 +574,16 @@ def test_a_bundle_holding_a_refused_certificate_imports_nothing( assert "What if" not in out +def test_a_root_signed_in_a_way_the_script_cannot_verify_is_not_reported_missing( + extra: dict[str, x509.Certificate], files: dict[str, Path] +) -> None: + proc = _run_script(files["pem_pss_root"]) + assert proc.returncode != 0, proc.stdout + proc.stderr + message = _error_message(proc) + assert "holds no self-signed root" not in message, message + assert f"{_thumb(extra['pss_root'])} (" in message and "it is signed with " in message, message + + def test_the_error_names_the_first_five_refusals_and_counts_the_rest( tmp_path: Path, chain: dict[str, tuple[x509.Certificate, _Key]] ) -> None: diff --git a/tests/test_measure_store_access_gate_refusal.py b/tests/test_measure_store_access_gate_refusal.py index f433b718f6..b7a97a0ad3 100644 --- a/tests/test_measure_store_access_gate_refusal.py +++ b/tests/test_measure_store_access_gate_refusal.py @@ -61,7 +61,7 @@ def _judge(host: str, tmp_path: Path, lines: list[str]) -> dict[str, object]: $errs = $null $ast = [System.Management.Automation.Language.Parser]::ParseFile({_psq(str(_SCRIPT))}, [ref]$null, [ref]$errs) if (@($errs).Count) {{ throw "the script does not parse: $($errs[0])" }} -foreach ($var in @('GateRefusal', 'NoAdminRefusal', 'StartRefused')) {{ +foreach ($var in @('GateRefusal', 'NoAdminRefusal', 'StartRefused', 'GateLetStart')) {{ $set = $ast.Find({{ $args[0] -is [System.Management.Automation.Language.AssignmentStatementAst] -and $args[0].Left.Extent.Text -eq ('$' + $var) }}, $false) if (-not $set) {{ throw "missing assignment `${{var}}" }} @@ -75,6 +75,7 @@ def _judge(host: str, tmp_path: Path, lines: list[str]) -> dict[str, object]: ConvertTo-Json -Compress -InputObject @{{ refusal = @($lines | ForEach-Object {{ [bool](Test-GateRefusalLine $_) }}) no_admin = @($lines | ForEach-Object {{ [bool](Test-GateRefusalLine $_ $NoAdminRefusal) }}) + let_start = @($lines | ForEach-Object {{ $_ -match $GateLetStart }}) clause = @($lines | ForEach-Object {{ $_ -match [regex]::Escape($GateRefusal) }}) }} """ f = tmp_path / f"gate-{uuid.uuid4().hex}.ps1" @@ -111,10 +112,19 @@ def test_the_engine_still_words_the_three_gate_lines_so() -> None: @pytest.mark.parametrize("host", ["pwsh", "powershell"]) def test_only_the_refusal_counts_as_a_refused_start(tmp_path: Path, host: str) -> None: - got = _judge(host, tmp_path, [_REFUSAL[1], _SKIPPED[1], _WARN_MODE[1], "unrelated line"]) - assert got["refusal"] == [True, False, False, False], got + # Another engine line that carries the clause and is not the gate's. + census = ( + "WARNING messagefoundry.auth.service: no enabled Administrator can sign in without an" + " outside identity service" + ) + got = _judge( + host, tmp_path, [_REFUSAL[1], _SKIPPED[1], _WARN_MODE[1], census, "unrelated line"] + ) + assert got["refusal"] == [True, False, False, False, False], got # The narrower clause the StartFirstEndToEnd arm passes: the no-Administrator refusal only. - assert got["no_admin"] == [True, False, False, False], got + assert got["no_admin"] == [True, False, False, False, False], got + # Only the gate's two let-it-start lines end the wait early, not the census line. + assert got["let_start"] == [False, True, True, False, False], got # The control: the clause the wait used to match alone is in both warning lines, so the old # criterion took an engine that went on to serve for a refused one. - assert got["clause"] == [True, True, True, False], got + assert got["clause"] == [True, True, True, True, False], got From bc0d9ec995d66b9b587044f86f7108f608f4debd Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 04:11:23 -0600 Subject: [PATCH 05/19] Service scripts: correct four false texts left by review round 3 - import-db-ca.ps1's rotation hint said to remove the old certificates the new chain still needs; it now says to remove only those neither the new chain nor anything else needs. - A comment implied the chain check keeps unrelated roots out. It states that any self-signed CA in the file is its own anchor and is installed in Root, as on main. Behaviour unchanged. - measure-store-access.ps1's control-reopen and restart failures no longer name a file lockout when the gate let the start go on. - test_import_db_ca_console_text.py's docstring no longer says the bundle test runs -WhatIf only. BACKLOG #5561, #5658 Proposed PR title: Service scripts: import-db-ca.ps1 checks the chain and expiry of each CA; measure-store-access.ps1 counts only the gate refusal (BACKLOG #5561, #5658) Proposed banner #5561: PARTIAL -- each CA must chain to a self-signed root in the file, an approved CA to an approved root, and an expired or unverifiable certificate is refused; roots go first; refusals name the Subject. Items kept in one line each are listed in the PR body. Proposed banner #5658: SHIPPED -- Wait-ForGateRefusal counts a line only when it carries the refusal's own words, with a test against app.py's wording. --- scripts/service/import-db-ca.ps1 | 9 +++++---- scripts/service/measure-store-access.ps1 | 15 ++++++++++++++- tests/test_import_db_ca_console_text.py | 4 ++-- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/scripts/service/import-db-ca.ps1 b/scripts/service/import-db-ca.ps1 index 1b96fdd267..fd32552d3a 100644 --- a/scripts/service/import-db-ca.ps1 +++ b/scripts/service/import-db-ca.ps1 @@ -488,8 +488,9 @@ $nowUtc = [datetime]::UtcNow $refusals = [System.Collections.Generic.List[string]]::new() # Each certificate is checked before it is shown, so a refused one is listed with its reason and # never with a store it would not go to. The store shown is the one it is offered for and written to. -# A certificate that passes on its own must also reach a root through this file's own certificates, -# so a root that signed none of them cannot carry an unrelated issuing CA in beside it. +# A certificate that passes on its own must also reach a self-signed root through this file's own +# certificates, so an issuing CA whose root is not in the file is refused. Any self-signed CA in the +# file is its own anchor and passes, so an unrelated root in the file is installed in Root too. Write-Host "The file holds $($bundle.Count) certificate(s):" $unverifiedRoot = $false $entries = @(foreach ($cert in $bundle) { @@ -612,6 +613,6 @@ if ($approved.Count -gt 0) { Write-Host "trust_server_certificate = false (no TrustServerCertificate=true needed)." -ForegroundColor Green Write-Host "" Write-Host "Rotation: when the CA is replaced, import the NEW CA first (this leaves both trusted)," - Write-Host "roll the server cert, then remove only the old certificates that the new chain and" - Write-Host "nothing else still needs -- see docs/DEPLOY-SERVER-DB.md section 5.3." + Write-Host "roll the server cert, then remove only the old certificates that neither the new chain" + Write-Host "nor anything else still needs -- see docs/DEPLOY-SERVER-DB.md section 5.3." } diff --git a/scripts/service/measure-store-access.ps1 b/scripts/service/measure-store-access.ps1 index 45e7a2fa3e..fa3e7b72a4 100644 --- a/scripts/service/measure-store-access.ps1 +++ b/scripts/service/measure-store-access.ps1 @@ -652,6 +652,8 @@ function Wait-ForGateRefusal([string]$Phase) { $false on timeout, and $false at once, with a reading quoting it, when the gate's clause is logged without the refusal: the engine read its users and the gate let the start go on. #> + # Read by callers after a $false, so a failure text never names a lockout the gate ruled out. + $script:GateLetStartSeen = $false $deadline = (Get-Date).AddSeconds($WaitSeconds) while ((Get-Date) -lt $deadline) { Update-OwnerSample "during $Phase" @@ -663,6 +665,7 @@ function Wait-ForGateRefusal([string]$Phase) { if ($letStart.Count -gt 0) { Add-Reading ("during $Phase the service reached the ADR 0167 gate, which did not refuse " + "the start: $(Get-Excerpt $letStart[0].Line 300)") + $script:GateLetStartSeen = $true return $false } Start-Sleep -Seconds 1 @@ -1070,6 +1073,11 @@ try { Stop-TheService if ($own) { Add-Reading "control: $ServiceIdentity reopened the store it created, so the store is restricted to the service and not to nobody" + } elseif ($script:GateLetStartSeen) { + Add-Failure ("control: $ServiceIdentity reopened the store it created and reached " + + "the ADR 0167 gate, which let the start go on (the reading above), so this " + + "control expected a refusal it did not get; the store's access is not in question.") + Show-LogTail } else { Add-Failure ("control: $ServiceIdentity could not reopen even the store it created " + "itself, which nobody else touched, so its own first open locked out every " + @@ -1094,7 +1102,12 @@ try { } else { $reopened = Wait-ForGateRefusal "the restart" Stop-TheService - if (-not $reopened) { + if (-not $reopened -and $script:GateLetStartSeen) { + Add-Failure ("$ServiceIdentity opened $DbPath after $Operator's open and reached " + + "the ADR 0167 gate, which let the start go on (the reading above), so the " + + "restart expected a refusal it did not get; the store's access is not in question.") + Show-LogTail + } elseif (-not $reopened) { Add-Failure ("$ServiceIdentity could not open $DbPath after $Operator's open " + "re-secured it: the restart logged no ADR 0167 refusal in $WaitSeconds s. " + (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) diff --git a/tests/test_import_db_ca_console_text.py b/tests/test_import_db_ca_console_text.py index 33907cbedd..366c87f34c 100644 --- a/tests/test_import_db_ca_console_text.py +++ b/tests/test_import_db_ca_console_text.py @@ -8,8 +8,8 @@ before ``Write-Host`` or ``ShouldProcess`` shows it. NOTHING HERE RUNS THE SCRIPT ITSELF: an import needs an elevated Windows session and writes the -machine trust store. (``tests/test_import_db_ca_bundle.py`` runs it with ``-WhatIf`` only, which -writes nothing.) So the static half pins that every console line naming the certificate's Subject or +machine trust store. (``tests/test_import_db_ca_bundle.py`` runs it unelevated only: with +``-WhatIf``, which writes nothing, and once without it, to meet the script's elevation refusal.) So the static half pins that every console line naming the certificate's Subject or Issuer goes through the helper, and the behavioural half lifts the helper's two function definitions out of the script's own AST and calls them, never running the script. The behavioural half needs ``pwsh`` and skips where there is none, as ``tests/test_net_helper_install_scripts.py`` From feaae1142f6e64a5884f77ece4dced3bbc395feb Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 06:14:50 -0600 Subject: [PATCH 06/19] Service scripts: repair round on PR 3025 review findings import-db-ca.ps1: - issuer and signer names match as raw DER or as decoded text ignoring case (Test-SameName), so a PrintableString and a UTF8String of one name link; - an expired certificate is listed as skipped and never installed; only a CA that reaches its root solely through an expired one is refused, and a file whose only root expired says so; - the success message no longer says "now validates" when an imported certificate is not valid yet; - "Already in" now reads the machine's registry store, not the logical store that also shows Group Policy copies; the partial-failure error counts them; - the help says "at least" for what is not checked, and that any self-signed CA in the file goes into Root. measure-store-access.ps1: the first-start and restart texts say "did not log the expected refusal line", which holds for every refusal wording, and the Wait-ForGateRefusal help names the two lines that end it early. BACKLOG #5561, #5658 Proposed PR title: Service scripts: import-db-ca.ps1 checks the chain and expiry of each CA; measure-store-access.ps1 counts only the gate refusal (BACKLOG #5561, #5658) Proposed banner #5561: PARTIAL -- each CA must chain to a self-signed root in the file through unexpired certificates, an approved CA to an approved root, and an unverifiable certificate is refused; an expired one is skipped; roots go first; refusals name the Subject. Items kept in one line each are listed in the PR body. Proposed banner #5658: SHIPPED -- Wait-ForGateRefusal counts a line only when it carries the refusal's own words, with a test against app.py's wording. --- docs/DEPLOY-SERVER-DB.md | 5 +- scripts/service/import-db-ca.ps1 | 134 +++++++++++++++-------- scripts/service/measure-store-access.ps1 | 12 +- tests/test_import_db_ca_bundle.py | 98 +++++++++++++++-- 4 files changed, 184 insertions(+), 65 deletions(-) diff --git a/docs/DEPLOY-SERVER-DB.md b/docs/DEPLOY-SERVER-DB.md index 5e8942370d..de77a3bd8f 100644 --- a/docs/DEPLOY-SERVER-DB.md +++ b/docs/DEPLOY-SERVER-DB.md @@ -564,8 +564,9 @@ Server hop still checks the chain up to the root. It **is** usable as a trust an engine's Python TLS on Windows: CPython's `ssl.create_default_context()`, which the PostgreSQL system-trust path calls, loads the `CA` and `ROOT` system stores, and Python 3.13 and later set partial-chain verification, so a chain that reaches a certificate in `LocalMachine\CA` validates -there without its root. The helper refuses a file unless it holds a self-signed root and every -other CA in it chains to a self-signed root through the file's own certificates. With `-Confirm`, +there without its root. The helper refuses a file whose CAs do not chain to a self-signed root in +it; its help says how it checks that, and that it installs any self-signed CA in the file into +`LocalMachine\Root`. With `-Confirm`, declining a root that an accepted CA chains through imports nothing, so give it the root of the CA you mean to trust, alone or in that CA's own `.p7b` chain, and accept every certificate it offers. diff --git a/scripts/service/import-db-ca.ps1 b/scripts/service/import-db-ca.ps1 index fd32552d3a..1491ad325d 100644 --- a/scripts/service/import-db-ca.ps1 +++ b/scripts/service/import-db-ca.ps1 @@ -30,19 +30,23 @@ ROOT system stores, and Python 3.13 and later set partial-chain verification, so a chain that reaches a certificate in LocalMachine\CA validates there without its root. A file is refused unless it holds a self-signed root and every other CA in it chains to a self-signed root - through the file's own certificates, each link's signature verified. No revocation, name - constraint or path length is checked. With -Confirm the operator may decline a root, but then - nothing is imported if an approved CA chains only through it. + through the file's own certificates, each link's signature verified and its names compared as + decoded text, not as raw bytes. Not checked, at least: revocation, name constraints, path + length and extended key usage. Any self-signed CA in the file is installed in LocalMachine\Root, + whether or not another certificate in the file chains to it. With -Confirm the operator may + decline a root, but then nothing is imported if an approved CA chains only through it. PostgreSQL can alternatively pin a CA by file via [store].ssl_root_cert (no machine import needed). This machine-store import is the path SQL Server REQUIRES; Postgres can use either. The file is read once. Every certificate it holds is shown, checked, and only then added, so - nothing reaches a store that the operator was not shown. Roots are added first. The whole - file is refused, and nothing is imported, in at least these cases: when any certificate in it is not marked as a CA by - its basic constraints, has a key usage that cannot sign certificates, carries a private key, - is signed in a way this script does not verify, or has expired, when it holds no self-signed - root, when a CA in it does not chain to a self-signed root in the file, when a DER value in it + nothing reaches a store that the operator was not shown. Roots are added first. An expired + certificate is listed and skipped, never installed. The whole file is refused, and nothing is + imported, in at least these cases: when any certificate in it is not marked as a CA by its + basic constraints, has a key usage that cannot sign certificates, carries a private key, or + is signed in a way this script does not verify, when it holds no unexpired self-signed root, + when an unexpired CA in it does not chain to an unexpired self-signed root through the file's + unexpired certificates, when a DER value in it (the whole file, a PEM block, or decoded base64) does not end where its bytes end, or when a control character other than tab, CR or LF sits outside its PEM blocks. Only RSA PKCS#1 v1.5 and ECDSA signatures with SHA-1, SHA-256, SHA-384 or SHA-512 are verified; RSA-PSS and @@ -55,7 +59,9 @@ PEM armour is not looked for; no private key is ever loaded either way. Run from an elevated (Administrator) PowerShell prompt; -WhatIf needs no elevation and changes - nothing. Idempotent: re-importing the same CA is a no-op (each store keys on thumbprint). + nothing. Idempotent: a CA already in the machine's own Root or CA registry store is named and + left as it is. A copy seen only through Group Policy or the enterprise store does not count, + so that CA is added too. .PARAMETER CaPath Path to the CA certificate(s) to trust: a DER, base64 or PEM certificate (.cer/.crt/.pem, one @@ -318,15 +324,27 @@ $SignatureHashes = @{ '1.2.840.10045.4.3.3' = 'SHA384'; '1.2.840.10045.4.3.4' = 'SHA512' } -# Whether $Signer issued the certificate: the certificate's issuer is $Signer's subject, byte for -# byte, AND its signature verifies under $Signer's public key. A signature algorithm outside -# $SignatureHashes reads as not signed, which fails closed. +# Whether two distinguished names are the same name: equal as raw DER, or equal as decoded text +# ignoring case. The decoded form lets a name encoded as a PrintableString in one certificate match +# the same text encoded as a UTF8String in another, which RFC 5280 name matching treats as equal. +# It is only a filter for candidate issuers: the signature check in Test-SignedBy is what decides. +function Test-SameName { + param( + [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X500DistinguishedName]$A, + [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X500DistinguishedName]$B) + if ([Convert]::ToBase64String($A.RawData) -ceq [Convert]::ToBase64String($B.RawData)) { return $true } + $flags = [System.Security.Cryptography.X509Certificates.X500DistinguishedNameFlags]::None + return ($A.Decode($flags) -eq $B.Decode($flags)) +} + +# Whether $Signer issued the certificate: the certificate's issuer is $Signer's subject +# (Test-SameName) AND its signature verifies under $Signer's public key. A signature algorithm +# outside $SignatureHashes reads as not signed, which fails closed. function Test-SignedBy { param( [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate, [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Signer) - if ([Convert]::ToBase64String($Signer.SubjectName.RawData) -cne - [Convert]::ToBase64String($Certificate.IssuerName.RawData)) { return $false } + if (-not (Test-SameName $Signer.SubjectName $Certificate.IssuerName)) { return $false } $oid = $Certificate.SignatureAlgorithm.Value if (-not $SignatureHashes.ContainsKey($oid)) { return $false } $hash = [System.Security.Cryptography.HashAlgorithmName]::new($SignatureHashes[$oid]) @@ -384,11 +402,10 @@ function Test-SelfSigned { return (Test-SignedBy -Certificate $Certificate -Signer $Certificate) } -# Whether the certificate's subject and issuer are the same name, byte for byte. Nothing is verified. +# Whether the certificate's subject and issuer are the same name (Test-SameName). Nothing is verified. function Test-SelfIssued { param([Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate) - return ([Convert]::ToBase64String($Certificate.SubjectName.RawData) -ceq - [Convert]::ToBase64String($Certificate.IssuerName.RawData)) + return (Test-SameName $Certificate.SubjectName $Certificate.IssuerName) } # Why the certificate does not chain to a self-signed root in the file, or $null when it does. The @@ -397,7 +414,7 @@ function Test-SelfIssued { # (Test-SignedBy). Every such issuer is followed, not only the first, so a cross-certificate that # shares a root's name and key cannot hide the root behind it; each certificate is visited once, so # a loop of links ends. Nothing else about the chain is checked here; Get-CaRefusal checks each -# certificate on its own, and no revocation or name constraint is read. +# certificate on its own, and the help says what neither checks. function Get-ChainRefusal { param( [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate, @@ -433,15 +450,10 @@ function Get-CaStoreName { # of a version 3 CA certificate; a version 1 root, which cannot carry it, is refused too, by this # script's choice rather than the RFC's. A key usage extension that leaves out certificate signing # also counts as not a CA (RFC 5280, the Key Usage section). A signature this script cannot verify -# is refused by name, so a root signed that way is not reported as missing. A certificate that has -# expired at $NowUtc is refused as well: nothing that chains through it validates, so importing it -# is only ever a mistake, and an expired root must not stand in for the one a CA chains to. A -# certificate not valid yet is not refused here; the listing warns of it, since a new CA may be -# staged ahead of its start for a make-before-break rotation. +# is refused by name, so a root signed that way is not reported as missing. Validity is not checked +# here: the caller skips an expired certificate and warns of one not valid yet. function Get-CaRefusal { - param( - [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate, - [datetime]$NowUtc = [datetime]::UtcNow) + param([Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate) # Read-CaBundle already refuses every container that can carry a key; this holds if it ever stops. if ($Certificate.HasPrivateKey) { return 'it carries a private key' } $constraints = @($Certificate.Extensions | @@ -461,7 +473,6 @@ function Get-CaRefusal { if (-not $SignatureHashes.ContainsKey($algorithm.Value)) { return "it is signed with $($algorithm.FriendlyName) ($($algorithm.Value)), which this script does not verify" } - if ($Certificate.NotAfter.ToUniversalTime() -lt $NowUtc) { return 'it has expired' } return $null } @@ -489,14 +500,27 @@ $refusals = [System.Collections.Generic.List[string]]::new() # Each certificate is checked before it is shown, so a refused one is listed with its reason and # never with a store it would not go to. The store shown is the one it is offered for and written to. # A certificate that passes on its own must also reach a self-signed root through this file's own -# certificates, so an issuing CA whose root is not in the file is refused. Any self-signed CA in the -# file is its own anchor and passes, so an unrelated root in the file is installed in Root too. +# unexpired certificates, so an issuing CA whose root is not in the file is refused. Any self-signed +# CA in the file is its own anchor and passes, so an unrelated root in the file is installed in Root +# too. An expired certificate that passes on its own is skipped, never installed; only a CA that +# reaches its root through it is refused. +$live = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection +foreach ($cert in $bundle) { + if ($cert.NotAfter.ToUniversalTime() -ge $nowUtc) { [void]$live.Add($cert) } +} Write-Host "The file holds $($bundle.Count) certificate(s):" $unverifiedRoot = $false +$expiredRoot = $false $entries = @(foreach ($cert in $bundle) { $target = $stores[(Get-CaStoreName -Certificate $cert)] - $why = Get-CaRefusal -Certificate $cert -NowUtc $nowUtc - if (-not $why) { $why = Get-ChainRefusal -Certificate $cert -Bundle $bundle } + $expired = $cert.NotAfter.ToUniversalTime() -lt $nowUtc + $why = Get-CaRefusal -Certificate $cert + if (-not $why -and -not $expired) { + $why = Get-ChainRefusal -Certificate $cert -Bundle $live + if ($why -and -not (Get-ChainRefusal -Certificate $cert -Bundle $bundle)) { + $why = 'it chains to a self-signed root in the file only through an expired certificate' + } + } Write-Host "" Write-Host " Subject : $(ConvertTo-PrintableText $cert.Subject)" Write-Host " Issuer : $(ConvertTo-PrintableText $cert.Issuer)" @@ -508,13 +532,16 @@ $entries = @(foreach ($cert in $bundle) { } # A self-issued certificate refused for a signature this script cannot verify may be the file's # root, so the error must not say the file holds none. - if ($why -and -not $target.Equals($stores['Root']) -and (Test-SelfIssued -Certificate $cert) -and - -not $SignatureHashes.ContainsKey($cert.SignatureAlgorithm.Value)) { + if ((Test-SelfIssued -Certificate $cert) -and -not $SignatureHashes.ContainsKey($cert.SignatureAlgorithm.Value)) { $unverifiedRoot = $true } if ($why) { Write-Host " Refused : $why" $refusals.Add("$($cert.Thumbprint) ($(ConvertTo-PrintableText $cert.Subject)): $why") + } elseif ($expired) { + Write-Host " Skipped : it has expired, so it is not installed" + if ($target.Equals($stores['Root'])) { $expiredRoot = $true } + continue } else { Write-Host " Store : $($target.Label)" } @@ -541,12 +568,13 @@ if ($rootless -or $refusals.Count -gt 0) { $named += "." } if ($rootless) { - throw ("Nothing was imported. The file holds no self-signed root. Give this script the root " + + $which = if ($expiredRoot) { "no self-signed root that has not expired" } else { "no self-signed root" } + throw ("Nothing was imported. The file holds $which. Give this script the root " + "the CA chains to, alone or in the same .p7b chain.$named") } throw ("Nothing was imported. Every certificate in the file must be a CA that may sign " + - "certificates, carries no private key, is signed in a way this script verifies, has not " + - "expired, and chains to a self-signed root in the file.$named") + "certificates, carries no private key, and is signed in a way this script verifies, and " + + "every unexpired one must chain to an unexpired self-signed root in the file.$named") } # --- import ------------------------------------------------------------------ @@ -573,10 +601,12 @@ if ($orphans.Count -gt 0) { } if ($approved.Count -gt 0) { - # Each store keys on thumbprint, so re-importing the same CA is a no-op (idempotent). One the - # store already held is named as such, since a later rotation must not remove it as the file's. - # Roots go first, so a failure part way never leaves an approved issuing CA added and an approved - # root not. Nothing is rolled back; the error says how many were added. + # A CA already in the machine's own store is named as such and left alone, since a later rotation + # must not remove it as the file's. That store is the machine's registry store. The logical + # store X509Store reads also shows Group Policy and enterprise copies, and skipping on one of + # those would leave the import resting on that policy. Roots go first, so a failure part way never leaves an approved issuing CA added + # and an approved root not. Nothing is rolled back; the error says how many were added. + $registryName = @{ Root = 'ROOT'; CertificateAuthority = 'CA' } $added = 0 $present = 0 try { @@ -588,9 +618,8 @@ if ($approved.Count -gt 0) { try { foreach ($entry in $batch) { $thumb = $entry.Certificate.Thumbprint - if ($store.Certificates.Find( - [System.Security.Cryptography.X509Certificates.X509FindType]::FindByThumbprint, - $thumb, $false).Count -gt 0) { + $own = "HKLM:\SOFTWARE\Microsoft\SystemCertificates\$($registryName[$name])\Certificates\$thumb" + if (Test-Path -LiteralPath $own) { Write-Host "Already in $($stores[$name].Label), left as it was: $thumb" $present++ continue @@ -603,14 +632,23 @@ if ($approved.Count -gt 0) { } } } catch { - throw ("Added $added of $($approved.Count) certificate(s), roots first, before this " + - "failed: $($_.Exception.Message)") + throw ("Added $added of $($approved.Count) certificate(s), roots first, with $present " + + "already present, before this failed: $($_.Exception.Message)") } Write-Host "" Write-Host "Imported $added CA certificate(s); $present were already in their store." - # Every approved CA reached an approved root above, so a root is now in the Root store. - Write-Host "A DB server cert chaining to the imported root now validates with" -ForegroundColor Green - Write-Host "trust_server_certificate = false (no TrustServerCertificate=true needed)." -ForegroundColor Green + $notYet = @($approved | Where-Object { $_.Certificate.NotBefore.ToUniversalTime() -gt $nowUtc }) + if ($notYet.Count -gt 0) { + # Staged ahead of its start: say when it will validate, not that it does. + $from = ($notYet | ForEach-Object { $_.Certificate.NotBefore.ToUniversalTime() } | + Sort-Object -Descending | Select-Object -First 1).ToString('u') + Write-Warning ("$($notYet.Count) imported certificate(s) are not valid yet. A chain through " + + "them does not validate before $from; do not roll the DB server cert onto it before then.") + } else { + # Every approved CA reached an approved root above, so a root is now in the Root store. + Write-Host "A DB server cert chaining to the imported root now validates with" -ForegroundColor Green + Write-Host "trust_server_certificate = false (no TrustServerCertificate=true needed)." -ForegroundColor Green + } Write-Host "" Write-Host "Rotation: when the CA is replaced, import the NEW CA first (this leaves both trusted)," Write-Host "roll the server cert, then remove only the old certificates that neither the new chain" diff --git a/scripts/service/measure-store-access.ps1 b/scripts/service/measure-store-access.ps1 index fa3e7b72a4..c43cbb1a92 100644 --- a/scripts/service/measure-store-access.ps1 +++ b/scripts/service/measure-store-access.ps1 @@ -649,8 +649,8 @@ function Wait-ForGateRefusal([string]$Phase) { Wait until the service logs the ADR 0167 refusal (Test-GateRefusalLine), which it reaches only after opening the store and reading its users. Samples process owners throughout, since NSSM restarts the refused engine and each restart is a new process. Returns $true on the refusal, - $false on timeout, and $false at once, with a reading quoting it, when the gate's clause is - logged without the refusal: the engine read its users and the gate let the start go on. + $false on timeout, and $false at once, with a reading quoting it, when one of the gate's two + let-it-start lines ($GateLetStart) is logged. Any other line is waited past. #> # Read by callers after a $false, so a failure text never names a lockout the gate ruled out. $script:GateLetStartSeen = $false @@ -993,7 +993,7 @@ try { $noAdmin = @(Get-LogMatches ([regex]::Escape($NoAdminRefusal)) | Where-Object { Test-GateRefusalLine $_.Line $NoAdminRefusal }) if (-not $opened) { - $what = if (Test-Path -LiteralPath $DbPath) { "created $DbPath but logged no ADR 0167 refusal (a reading above says if the gate let it start)" } + $what = if (Test-Path -LiteralPath $DbPath) { "created $DbPath but did not log the expected refusal line" } else { "never created $DbPath" } Add-Failure ("$ServiceIdentity $what within $WaitSeconds s on a fresh store. " + (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) @@ -1047,7 +1047,7 @@ try { Stop-TheService Show-Trio "service first start" if (-not $opened) { - $what = if (Test-Path -LiteralPath $DbPath) { "created $DbPath but logged no ADR 0167 refusal (a reading above says if the gate let it start)" } + $what = if (Test-Path -LiteralPath $DbPath) { "created $DbPath but did not log the expected refusal line" } else { "never created $DbPath" } Add-Failure ("$ServiceIdentity $what within $WaitSeconds s on a fresh store. " + (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) @@ -1108,8 +1108,8 @@ try { "restart expected a refusal it did not get; the store's access is not in question.") Show-LogTail } elseif (-not $reopened) { - Add-Failure ("$ServiceIdentity could not open $DbPath after $Operator's open " + - "re-secured it: the restart logged no ADR 0167 refusal in $WaitSeconds s. " + + Add-Failure ("$ServiceIdentity, restarted on $DbPath after $Operator's open " + + "re-secured it, did not log the expected refusal line in $WaitSeconds s. " + (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) Show-LogTail } else { diff --git a/tests/test_import_db_ca_bundle.py b/tests/test_import_db_ca_bundle.py index 972c54de5a..b4aebec61c 100644 --- a/tests/test_import_db_ca_bundle.py +++ b/tests/test_import_db_ca_bundle.py @@ -10,8 +10,9 @@ root goes into the machine Root store and any other CA into the intermediate store. NOTHING HERE WRITES A CERTIFICATE STORE. The fixtures are generated in a pytest temporary -directory. The function-level tests lift ``Read-CaBundle``, ``Get-CaRefusal`` and -``Get-CaStoreName`` (and the console helpers they call) out of the script's AST and call them, as +directory. The function-level tests lift ``Read-CaBundle``, ``Get-CaRefusal``, +``Get-CaStoreName`` and ``Get-ChainRefusal`` (with the ``$SignatureHashes`` table and the helpers +they call) out of the script's AST and call them, as ``tests/test_import_db_ca_console_text.py`` does. The whole-script tests are skipped in an elevated session. All but one run it with ``-WhatIf``, where ``ShouldProcess`` declines every import, so the store is never opened, and even a regression that ignored ``-WhatIf`` would meet the script's own @@ -38,6 +39,7 @@ from cryptography.hazmat.primitives import hashes, serialization from cryptography.hazmat.primitives.asymmetric import ec, padding, rsa from cryptography.hazmat.primitives.serialization import pkcs7, pkcs12 +from cryptography.x509.name import _ASN1Type from cryptography.x509.oid import NameOID _SCRIPT = Path(__file__).resolve().parent.parent / "scripts" / "service" / "import-db-ca.ps1" @@ -118,6 +120,7 @@ def _pem(*certs: x509.Certificate) -> bytes: def chain() -> dict[str, tuple[x509.Certificate, _Key]]: root = _cert("Synthetic Test Root CA", True) other_root = _cert("Synthetic Other Root CA", True) + expired_root = _cert("Synthetic Expired Root CA", True, valid_from_days=-30, valid_to_days=-1) return { "root": root, "intermediate": _cert("Synthetic Test Issuing CA", True, root), @@ -128,10 +131,10 @@ def chain() -> dict[str, tuple[x509.Certificate, _Key]]: "name_only_intermediate": _cert( "Synthetic Name-Only Issuing CA", True, (root[0], other_root[1]) ), - "expired_root": _cert( - "Synthetic Expired Root CA", True, valid_from_days=-30, valid_to_days=-1 - ), + "expired_root": expired_root, "future_root": _cert("Synthetic Future Root CA", True, valid_from_days=2), + # Valid itself, but its only root in the file has expired. + "under_expired": _cert("Synthetic Issuing CA Under Expired Root", True, expired_root), "unconstrained": _cert("Synthetic No Constraints", None), "no_cert_sign": _cert("Synthetic CA Without CertSign", True, cert_sign=False), "cert_sign": _cert("Synthetic CA With CertSign", True, cert_sign=True), @@ -181,7 +184,34 @@ def build(name: x509.Name, public: object, signer: object) -> x509.Certificate: ), ) ) + # The root names itself with a PrintableString; the issuing CA names that issuer with a + # UTF8String of the same text. RFC 5280 name matching treats the two as one name. + enc_key = ec.generate_private_key(ec.SECP256R1()) + enc_text = "Synthetic Encoded Root CA" + enc_root = build( + x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, enc_text, _ASN1Type.PrintableString)]), + enc_key.public_key(), + enc_key, + ) + enc_issuing = ( + x509.CertificateBuilder() + .subject_name( + x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Synthetic Encoded Issuing CA")]) + ) + .issuer_name( + x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, enc_text, _ASN1Type.UTF8String)]) + ) + .public_key(ec.generate_private_key(ec.SECP256R1()).public_key()) + .serial_number(x509.random_serial_number()) + .not_valid_before(now - datetime.timedelta(days=1)) + .not_valid_after(now + datetime.timedelta(days=30)) + .add_extension(x509.BasicConstraints(ca=True, path_length=None), True) + .sign(enc_key, hashes.SHA256()) + ) + assert enc_root.subject.public_bytes() != enc_issuing.issuer.public_bytes() return { + "enc_root": enc_root, + "enc_issuing": enc_issuing, "rsa_root": build(rsa_name, rsa_key.public_key(), rsa_key), "rekey": build(root.subject, new_key.public_key(), root_key), # Self-signed, but with a signature the script does not verify. @@ -254,6 +284,9 @@ def files( "pem_name_only_intermediate": _pem(root, chain["name_only_intermediate"][0]), # Outside the validity period: an expired root beside a valid one, and a root not valid yet. "pem_expired_root": _pem(root, chain["expired_root"][0]), + "pem_only_expired_root": _pem(chain["expired_root"][0]), + "pem_under_expired": _pem(root, chain["expired_root"][0], chain["under_expired"][0]), + "pem_encoded_names": _pem(extra["enc_root"], extra["enc_issuing"]), "pem_future_root": _pem(chain["future_root"][0]), "pem_pss_root": _pem(extra["pss_root"]), # Bare base64 behind a byte-order mark, UTF-8 and UTF-16. @@ -297,7 +330,7 @@ def _read_all(tmp_path: Path, paths: dict[str, Path]) -> dict[str, dict[str, obj . ([scriptblock]::Create($set.Extent.Text)) foreach ($name in @('Test-EscapedForConsole', 'ConvertTo-PrintableText', 'Read-DerHeader', 'Read-CaBundle', 'Get-CaRefusal', 'Test-SignedBy', 'Test-SelfSigned', - 'Get-ChainRefusal', 'Get-CaStoreName')) {{ + 'Get-ChainRefusal', 'Get-CaStoreName', 'Test-SameName')) {{ $def = $ast.Find({{ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $args[0].Name -eq $name }}, $true) if (-not $def) {{ throw "missing function $name" }} @@ -396,9 +429,9 @@ def certs(name: str) -> list[tuple[str, str | None]]: assert certs("b64_utf8_bom") == [(root, None)] assert certs("b64_utf16_bom") == [(root, None)] - # Outside its validity period, a root is refused on its own, whatever else the file holds. + # Validity is the caller's to judge, not Get-CaRefusal's. expired = dict(certs("pem_expired_root")) - assert expired[_thumb(chain["expired_root"][0])] == "it has expired" + assert expired[_thumb(chain["expired_root"][0])] is None assert expired[root] is None # Not valid yet is not refused: a new CA may be staged ahead of its start. assert certs("pem_future_root") == [(_thumb(chain["future_root"][0]), None)] @@ -421,6 +454,11 @@ def chained(name: str) -> dict[str, object]: assert chained("p7b_der") == {root: None, inter: None} assert chained("root_and_rekey") == {root: None, rekey: None} + # Names that differ only in string encoding still link a CA to its root. + assert chained("pem_encoded_names") == { + _thumb(extra["enc_root"]): None, + _thumb(extra["enc_issuing"]): None, + } unchained = "it does not chain to a self-signed root in the file" for name, key in ( ("pem_foreign_intermediate", "foreign_intermediate"), @@ -539,7 +577,16 @@ def _pair(cert: x509.Certificate, why: str) -> str: [ ("p7b_with_leaf", [("leaf", _NOT_A_CA)]), ("pem_two_refused", [("leaf", _NOT_A_CA), ("no_cert_sign", _NO_CERT_SIGN)]), - ("pem_expired_root", [("expired_root", "it has expired")]), + ( + "pem_under_expired", + [ + ( + "under_expired", + "it chains to a self-signed root in the file only through an expired" + " certificate", + ) + ], + ), ("pem_foreign_intermediate", [("foreign_intermediate", _UNCHAINED)]), ("pem_name_only_intermediate", [("name_only_intermediate", _UNCHAINED)]), ], @@ -574,6 +621,39 @@ def test_a_bundle_holding_a_refused_certificate_imports_nothing( assert "What if" not in out +def test_an_expired_certificate_off_every_import_path_is_skipped( + chain: dict[str, tuple[x509.Certificate, _Key]], files: dict[str, Path] +) -> None: + proc = _run_script(files["pem_expired_root"]) + out = proc.stdout + proc.stderr + assert proc.returncode == 0, out + lines = proc.stdout.splitlines() + at = lines.index(f" Thumbprint : {_thumb(chain['expired_root'][0])}") + block = list(itertools.takewhile(lambda ln: ln.startswith(" "), lines[at:])) + assert " Skipped : it has expired, so it is not installed" in block, block + offered = [ln for ln in out.splitlines() if "What if" in ln] + assert len(offered) == 1 and _thumb(chain["root"][0]) in offered[0], offered + + +def test_a_file_whose_only_root_has_expired_imports_nothing(files: dict[str, Path]) -> None: + proc = _run_script(files["pem_only_expired_root"]) + assert proc.returncode != 0, proc.stdout + proc.stderr + assert _error_message(proc).startswith( + "Nothing was imported. The file holds no self-signed root that has not expired." + ), proc.stdout + + +def test_a_chain_whose_names_differ_only_in_encoding_is_offered( + extra: dict[str, x509.Certificate], files: dict[str, Path] +) -> None: + proc = _run_script(files["pem_encoded_names"]) + out = proc.stdout + proc.stderr + assert proc.returncode == 0, out + offered = [ln for ln in out.splitlines() if "What if" in ln] + assert len(offered) == 2, offered + assert any(_thumb(extra["enc_issuing"]) in ln and "LocalMachine\\CA" in ln for ln in offered) + + def test_a_root_signed_in_a_way_the_script_cannot_verify_is_not_reported_missing( extra: dict[str, x509.Certificate], files: dict[str, Path] ) -> None: From 2f439b0ee4105b7d914be1c7600f9e0e14693331 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 06:56:40 -0600 Subject: [PATCH 07/19] Service scripts: correct two statements the repair round made false - Test-SameName's comment said decoded names compare ignoring case, but PowerShell -eq also drops ignorable code points; it now compares OrdinalIgnoreCase, as the comment says. - measure-store-access.ps1's first-start failures now say the gate let the start go on when it did, instead of reading as a timeout. BACKLOG #5561, #5658 Proposed PR title: Service scripts: import-db-ca.ps1 checks the chain and expiry of each CA; measure-store-access.ps1 counts only the gate refusal (BACKLOG #5561, #5658) Proposed banner #5561: PARTIAL -- each CA must chain to a self-signed root in the file through unexpired certificates, an approved CA to an approved root, and an unverifiable certificate is refused; an expired one is skipped; roots go first; refusals name the Subject. Items kept in one line each are listed in the PR body. Proposed banner #5658: SHIPPED -- Wait-ForGateRefusal counts a line only when it carries the refusal's own words, with a test against app.py's wording. --- scripts/service/import-db-ca.ps1 | 4 +++- scripts/service/measure-store-access.ps1 | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/scripts/service/import-db-ca.ps1 b/scripts/service/import-db-ca.ps1 index 1491ad325d..feb8701f3b 100644 --- a/scripts/service/import-db-ca.ps1 +++ b/scripts/service/import-db-ca.ps1 @@ -334,7 +334,9 @@ function Test-SameName { [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X500DistinguishedName]$B) if ([Convert]::ToBase64String($A.RawData) -ceq [Convert]::ToBase64String($B.RawData)) { return $true } $flags = [System.Security.Cryptography.X509Certificates.X500DistinguishedNameFlags]::None - return ($A.Decode($flags) -eq $B.Decode($flags)) + # Ordinal, so only letter case is ignored; a culture-aware -eq would also drop ignorable code + # points such as a zero-width space. + return [string]::Equals($A.Decode($flags), $B.Decode($flags), [System.StringComparison]::OrdinalIgnoreCase) } # Whether $Signer issued the certificate: the certificate's issuer is $Signer's subject diff --git a/scripts/service/measure-store-access.ps1 b/scripts/service/measure-store-access.ps1 index c43cbb1a92..336ccd1fde 100644 --- a/scripts/service/measure-store-access.ps1 +++ b/scripts/service/measure-store-access.ps1 @@ -993,7 +993,8 @@ try { $noAdmin = @(Get-LogMatches ([regex]::Escape($NoAdminRefusal)) | Where-Object { Test-GateRefusalLine $_.Line $NoAdminRefusal }) if (-not $opened) { - $what = if (Test-Path -LiteralPath $DbPath) { "created $DbPath but did not log the expected refusal line" } + $what = if ($script:GateLetStartSeen) { "created $DbPath and reached the ADR 0167 gate, which let the start go on (the READING line quotes it) rather than refusing it," } + elseif (Test-Path -LiteralPath $DbPath) { "created $DbPath but did not log the expected refusal line" } else { "never created $DbPath" } Add-Failure ("$ServiceIdentity $what within $WaitSeconds s on a fresh store. " + (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) @@ -1047,7 +1048,8 @@ try { Stop-TheService Show-Trio "service first start" if (-not $opened) { - $what = if (Test-Path -LiteralPath $DbPath) { "created $DbPath but did not log the expected refusal line" } + $what = if ($script:GateLetStartSeen) { "created $DbPath and reached the ADR 0167 gate, which let the start go on (the READING line quotes it) rather than refusing it," } + elseif (Test-Path -LiteralPath $DbPath) { "created $DbPath but did not log the expected refusal line" } else { "never created $DbPath" } Add-Failure ("$ServiceIdentity $what within $WaitSeconds s on a fresh store. " + (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) From 3720400f9227fa3c4157ad2664fc4ecb17507c95 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 08:25:08 -0600 Subject: [PATCH 08/19] Service scripts: CA import links names as Windows does and skips expired certs first Repair round on PR 3025 covering review round-013ab02b, findings 1, 3-8, 12 (BACKLOG #5561, #5658). - import-db-ca.ps1 links an issuer to its signer only when the name bytes are equal, as the Windows chain engine does; a file linked only by names that differ in string type or case is refused (X509Chain.Build stops at PartialChain on both, measured). X509Chain itself was not adopted: the script runs on Windows PowerShell 5.1, which lacks CustomRootTrust. - An expired certificate is skipped before any check, so it cannot refuse the file. - A declined root the machine's own Root store holds anchors the CAs under it. - Presence: a root counts in Root only; any other CA in CA or Root. - The green "now validates" line prints only after an add; an all-present or all-declined run says so. - An unknown signature OID is named by its value. - measure-store-access.ps1: a first start the gate let go on gets no store-access attribution, as the control and restart branches. - DANGEROUS-FUNCTIONALITY.md row and DEPLOY-SERVER-DB.md 5.2 -Confirm sentence match the script. Proposed PR title: Service scripts: import-db-ca.ps1 reads every certificate and trusts only CAs; measure-store-access counts only the gate refusal (BACKLOG #5561, #5658) Proposed banner: #5561, #5658 PARTIAL -- engine PR 3025; review findings 2, 9, 10, 11, 13, 14 of round-013ab02b remain open. --- docs/DANGEROUS-FUNCTIONALITY.md | 2 +- docs/DEPLOY-SERVER-DB.md | 3 +- scripts/service/import-db-ca.ps1 | 167 ++++++++++++------ scripts/service/measure-store-access.ps1 | 14 +- tests/test_import_db_ca_bundle.py | 158 +++++++++++++++-- .../test_measure_store_access_gate_refusal.py | 13 ++ 6 files changed, 284 insertions(+), 73 deletions(-) diff --git a/docs/DANGEROUS-FUNCTIONALITY.md b/docs/DANGEROUS-FUNCTIONALITY.md index 97f8349477..0b9de85b1e 100644 --- a/docs/DANGEROUS-FUNCTIONALITY.md +++ b/docs/DANGEROUS-FUNCTIONALITY.md @@ -912,7 +912,7 @@ administrator rights. What each one grants or changes: | `uninstall-service.ps1` | Removes the service. With `-RemoveLogonRight`, rewrites local security policy with `secedit` again, to take "Log on as a service" back, working in a new administrator-only folder under the Windows folder's `Temp`. With `-RemoveAccountAces`, removes the account's permission entries from the data and config directories. | | `install-net-helper.ps1` | Registers `mefor-net-helper` (ADR 0056) as a service running as LocalSystem. It listens on the named pipe `\\.\pipe\mefor-net-helper` and adds or removes one floating IP address by running `netsh`. | | `uninstall-net-helper.ps1` | Removes the helper service. With `-ReleaseAddress`, first asks the helper to remove the floating address from this machine. | -| `import-db-ca.ps1` | Adds the CA certificates in one file to the machine stores `Cert:\LocalMachine\Root` and `Cert:\LocalMachine\CA`. Which CA goes where, and what each store is a trust anchor for, is in [`DEPLOY-SERVER-DB.md`](DEPLOY-SERVER-DB.md) section 5.2. Refuses the whole file if, at least, any certificate in it is not a CA or carries a private key, or the file holds no self-signed root. With `-WhatIf` it only shows what it would add, and runs without administrator rights. | +| `import-db-ca.ps1` | Adds the CA certificates in one file to the machine stores `Cert:\LocalMachine\Root` and `Cert:\LocalMachine\CA`. Which CA goes where, and what each store is a trust anchor for, is in [`DEPLOY-SERVER-DB.md`](DEPLOY-SERVER-DB.md) section 5.2; a self-signed CA in the file goes into `Cert:\LocalMachine\Root`. An expired certificate is listed and skipped, never added. A CA the machine's own store already holds is named and left as it is. Refuses the whole file if, at least, any unexpired certificate in it is not a CA or carries a private key, the file holds no unexpired self-signed root, or an unexpired CA in it does not chain to one through the file's unexpired certificates, its issuer name equal to its signer's subject byte for byte. With `-WhatIf` it only shows what it would add, and runs without administrator rights. | | `measure-store-access.ps1` | A measurement script, in the assessment's operator-run scope. [`ASVS-ASSESSMENT-METHOD.md`](ASVS-ASSESSMENT-METHOD.md) section 2 puts this folder in by its scope sentence, and names this script in by owner ruling of 2026-10-05. It measures whether the service account and the operator can both open one store; it is not a deployment step. It installs and uninstalls the service, and deletes the `-DataDir` it is given before it starts. Run it only on a disposable host: with its default `-ServiceName`, it would take over and then remove an engine service installed under that name. It also runs `python` from `PATH` while elevated. Unless `-AppExe` names another, it also takes the `messagefoundry` command from `PATH` (`Get-Command messagefoundry`) and runs it while elevated, at least for `gen-key`, which mints the run's store key, and `protect-key`, which writes the service's key file. The `python` probe starts import the engine's own code from whatever install that `python` finds, whatever `-AppExe` says. In the CI job `windows-service-smoke`, whose install step installs the engine editable, both are the checkout's. The installer's check of the engine install ([`SERVICE.md`](SERVICE.md), *Install the engine where only administrators can write*) never covers the `python` starts, and covers `protect-key` only when `-AppExe` names the launcher it checks; `gen-key` runs before the installer does. So whoever could write a folder on `PATH`, or the install those commands load, would run code as administrator and would see the store key. The `protect-key` start is handed that store key in its environment, as `MEFOR_STORE_ENCRYPTION_KEY`, which is cleared after it exits. So are at least the `python` probe starts that screen the password and provision the administrator. Those starts and the probe start that signs in are also handed, as `MEFOR_W0_ADMIN_PASSWORD`, the synthetic administrator password, or for the screen each candidate it tries. Without `-AppExe`, the service itself runs the engine in the installer's default install root, not the `PATH` `messagefoundry`. It runs NSSM only by full path, from the `-NssmDir` it hands the installer, so it runs the copy the installer checked. The CI job `windows-service-smoke`, which runs this script, runs NSSM the same way. | **The run-as account is the setting to look at hardest.** `install-service.ps1` defaults to a diff --git a/docs/DEPLOY-SERVER-DB.md b/docs/DEPLOY-SERVER-DB.md index de77a3bd8f..d9a0a548f1 100644 --- a/docs/DEPLOY-SERVER-DB.md +++ b/docs/DEPLOY-SERVER-DB.md @@ -567,7 +567,8 @@ partial-chain verification, so a chain that reaches a certificate in `LocalMachi there without its root. The helper refuses a file whose CAs do not chain to a self-signed root in it; its help says how it checks that, and that it installs any self-signed CA in the file into `LocalMachine\Root`. With `-Confirm`, -declining a root that an accepted CA chains through imports nothing, so give it the root of the CA +declining a root that an accepted CA chains through imports nothing, unless the machine's own +`LocalMachine\Root` store already holds that root, so give it the root of the CA you mean to trust, alone or in that CA's own `.p7b` chain, and accept every certificate it offers. The built-ins below check none of that, so use them only for a file holding one self-signed root diff --git a/scripts/service/import-db-ca.ps1 b/scripts/service/import-db-ca.ps1 index feb8701f3b..378a600cbc 100644 --- a/scripts/service/import-db-ca.ps1 +++ b/scripts/service/import-db-ca.ps1 @@ -29,21 +29,24 @@ ssl.create_default_context(), which the PostgreSQL system-trust path calls, loads the CA and ROOT system stores, and Python 3.13 and later set partial-chain verification, so a chain that reaches a certificate in LocalMachine\CA validates there without its root. A file is refused - unless it holds a self-signed root and every other CA in it chains to a self-signed root - through the file's own certificates, each link's signature verified and its names compared as - decoded text, not as raw bytes. Not checked, at least: revocation, name constraints, path - length and extended key usage. Any self-signed CA in the file is installed in LocalMachine\Root, - whether or not another certificate in the file chains to it. With -Confirm the operator may - decline a root, but then nothing is imported if an approved CA chains only through it. + unless it holds a self-signed root and every other unexpired CA in it chains to a self-signed + root through the file's own unexpired certificates. Each link's signature is verified, and its + issuer name must equal its signer's subject byte for byte, as the Windows chain engine requires: + names that differ only in string type or letter case do not link there, so a file linked only + by such names is refused. Not checked, at least: revocation, name constraints, path length and + extended key usage. Any self-signed CA in the file is installed in LocalMachine\Root, whether + or not another certificate in the file chains to it. With -Confirm the operator may decline a + root; then nothing is imported if an approved CA chains only through it, unless that root is + already in the machine's own LocalMachine\Root store. PostgreSQL can alternatively pin a CA by file via [store].ssl_root_cert (no machine import needed). This machine-store import is the path SQL Server REQUIRES; Postgres can use either. The file is read once. Every certificate it holds is shown, checked, and only then added, so nothing reaches a store that the operator was not shown. Roots are added first. An expired - certificate is listed and skipped, never installed. The whole file is refused, and nothing is - imported, in at least these cases: when any certificate in it is not marked as a CA by its - basic constraints, has a key usage that cannot sign certificates, carries a private key, or + certificate is listed and skipped, never installed, and is not checked as a CA. The whole file + is refused, and nothing is imported, in at least these cases: when any unexpired certificate in + it is not marked as a CA by its basic constraints, has a key usage that cannot sign certificates, carries a private key, or is signed in a way this script does not verify, when it holds no unexpired self-signed root, when an unexpired CA in it does not chain to an unexpired self-signed root through the file's unexpired certificates, when a DER value in it @@ -59,9 +62,10 @@ PEM armour is not looked for; no private key is ever loaded either way. Run from an elevated (Administrator) PowerShell prompt; -WhatIf needs no elevation and changes - nothing. Idempotent: a CA already in the machine's own Root or CA registry store is named and - left as it is. A copy seen only through Group Policy or the enterprise store does not count, - so that CA is added too. + nothing. Idempotent: a root already in the machine's own Root registry store, or any other CA + already in its own CA or Root registry store, is named and left as it is. A copy seen only + through Group Policy or the enterprise store does not count, so that CA is added too. A root + found only in the CA store is added to Root, since the CA store anchors nothing for SChannel. .PARAMETER CaPath Path to the CA certificate(s) to trust: a DER, base64 or PEM certificate (.cer/.crt/.pem, one @@ -324,19 +328,17 @@ $SignatureHashes = @{ '1.2.840.10045.4.3.3' = 'SHA384'; '1.2.840.10045.4.3.4' = 'SHA512' } -# Whether two distinguished names are the same name: equal as raw DER, or equal as decoded text -# ignoring case. The decoded form lets a name encoded as a PrintableString in one certificate match -# the same text encoded as a UTF8String in another, which RFC 5280 name matching treats as equal. -# It is only a filter for candidate issuers: the signature check in Test-SignedBy is what decides. +# Whether two distinguished names are the same name as the Windows chain engine links them: equal +# as raw DER bytes. A name spelt as a PrintableString in one certificate and a UTF8String in another, +# or in another letter case, does not link there (X509Chain.Build stops at PartialChain), so it does +# not link here either, and a file that relies on such a link is refused rather than imported as a +# chain SChannel cannot build. It is only a filter for candidate issuers: the signature check in +# Test-SignedBy decides. function Test-SameName { param( [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X500DistinguishedName]$A, [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X500DistinguishedName]$B) - if ([Convert]::ToBase64String($A.RawData) -ceq [Convert]::ToBase64String($B.RawData)) { return $true } - $flags = [System.Security.Cryptography.X509Certificates.X500DistinguishedNameFlags]::None - # Ordinal, so only letter case is ignored; a culture-aware -eq would also drop ignorable code - # points such as a zero-width space. - return [string]::Equals($A.Decode($flags), $B.Decode($flags), [System.StringComparison]::OrdinalIgnoreCase) + return ([Convert]::ToBase64String($A.RawData) -ceq [Convert]::ToBase64String($B.RawData)) } # Whether $Signer issued the certificate: the certificate's issuer is $Signer's subject @@ -473,11 +475,57 @@ function Get-CaRefusal { } $algorithm = $Certificate.SignatureAlgorithm if (-not $SignatureHashes.ContainsKey($algorithm.Value)) { - return "it is signed with $($algorithm.FriendlyName) ($($algorithm.Value)), which this script does not verify" + # .NET has no friendly name for an OID it does not know, so the OID alone names it then. + $named = if ($algorithm.FriendlyName) { "$($algorithm.FriendlyName) ($($algorithm.Value))" } else { $algorithm.Value } + return "it is signed with $named, which this script does not verify" } return $null } +# Which of the machine's own registry stores already holds the certificate where it counts, as a key +# of $stores, or $null. A root counts only in Root, since the CA store anchors nothing for SChannel; +# any other CA counts in CA or in Root, where it is trusted at least as far. The logical store +# X509Store reads also shows Group Policy and enterprise copies; those are not read here, so a CA +# held only there is added, and the import does not rest on that policy. $RegistryRoot is a +# parameter only so a test can point it at a directory. +function Get-PresentStoreName { + param( + [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate, + [Parameter(Mandatory = $true)][string]$StoreName, + [string]$RegistryRoot = 'HKLM:\SOFTWARE\Microsoft\SystemCertificates') + $where = if ($StoreName -eq 'Root') { @('Root') } else { @('CertificateAuthority', 'Root') } + $registryName = @{ Root = 'ROOT'; CertificateAuthority = 'CA' } + foreach ($name in $where) { + # Path.Combine, not Join-Path, which throws for a drive this host lacks (HKLM: off Windows). + $own = [System.IO.Path]::Combine($RegistryRoot, $registryName[$name], 'Certificates', $Certificate.Thumbprint) + if (Test-Path -LiteralPath $own) { return $name } + } + return $null +} + +# The certificates an approved CA may chain through: every approved one, and every declined one the +# machine's own store already holds where it counts (Get-PresentStoreName), so declining a root the +# machine already trusts does not leave the CAs under it without their root. +function Get-AnchorSet { + param( + [Parameter(Mandatory = $true)][AllowEmptyCollection()][object[]]$Entries, + [Parameter(Mandatory = $true)][AllowEmptyCollection()][object[]]$Approved, + [string]$RegistryRoot = 'HKLM:\SOFTWARE\Microsoft\SystemCertificates') + $set = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection + $taken = @{} + foreach ($entry in $Approved) { + $taken[$entry.Certificate.Thumbprint] = $true + [void]$set.Add($entry.Certificate) + } + foreach ($entry in $Entries) { + if (-not $taken.ContainsKey($entry.Certificate.Thumbprint) -and + (Get-PresentStoreName -Certificate $entry.Certificate -StoreName $entry.StoreName -RegistryRoot $RegistryRoot)) { + [void]$set.Add($entry.Certificate) + } + } + return , $set +} + # --- read and check -------------------------------------------------------------- if (-not (Test-Path -LiteralPath $CaPath -PathType Leaf)) { @@ -501,11 +549,12 @@ $nowUtc = [datetime]::UtcNow $refusals = [System.Collections.Generic.List[string]]::new() # Each certificate is checked before it is shown, so a refused one is listed with its reason and # never with a store it would not go to. The store shown is the one it is offered for and written to. -# A certificate that passes on its own must also reach a self-signed root through this file's own +# An expired certificate is skipped before any check, never installed, so nothing else about it can +# refuse the file; only an unexpired CA that reaches its root through it is refused. An unexpired +# certificate that passes on its own must also reach a self-signed root through this file's own # unexpired certificates, so an issuing CA whose root is not in the file is refused. Any self-signed # CA in the file is its own anchor and passes, so an unrelated root in the file is installed in Root -# too. An expired certificate that passes on its own is skipped, never installed; only a CA that -# reaches its root through it is refused. +# too. $live = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection foreach ($cert in $bundle) { if ($cert.NotAfter.ToUniversalTime() -ge $nowUtc) { [void]$live.Add($cert) } @@ -514,13 +563,17 @@ Write-Host "The file holds $($bundle.Count) certificate(s):" $unverifiedRoot = $false $expiredRoot = $false $entries = @(foreach ($cert in $bundle) { - $target = $stores[(Get-CaStoreName -Certificate $cert)] + $storeName = Get-CaStoreName -Certificate $cert + $target = $stores[$storeName] $expired = $cert.NotAfter.ToUniversalTime() -lt $nowUtc - $why = Get-CaRefusal -Certificate $cert - if (-not $why -and -not $expired) { - $why = Get-ChainRefusal -Certificate $cert -Bundle $live - if ($why -and -not (Get-ChainRefusal -Certificate $cert -Bundle $bundle)) { - $why = 'it chains to a self-signed root in the file only through an expired certificate' + $why = $null + if (-not $expired) { + $why = Get-CaRefusal -Certificate $cert + if (-not $why) { + $why = Get-ChainRefusal -Certificate $cert -Bundle $live + if ($why -and -not (Get-ChainRefusal -Certificate $cert -Bundle $bundle)) { + $why = 'it chains to a self-signed root in the file only through an expired certificate' + } } } Write-Host "" @@ -534,7 +587,8 @@ $entries = @(foreach ($cert in $bundle) { } # A self-issued certificate refused for a signature this script cannot verify may be the file's # root, so the error must not say the file holds none. - if ((Test-SelfIssued -Certificate $cert) -and -not $SignatureHashes.ContainsKey($cert.SignatureAlgorithm.Value)) { + if (-not $expired -and (Test-SelfIssued -Certificate $cert) -and + -not $SignatureHashes.ContainsKey($cert.SignatureAlgorithm.Value)) { $unverifiedRoot = $true } if ($why) { @@ -547,7 +601,7 @@ $entries = @(foreach ($cert in $bundle) { } else { Write-Host " Store : $($target.Label)" } - @{ Certificate = $cert; Target = $target } + @{ Certificate = $cert; Target = $target; StoreName = $storeName } }) # Without its root an issuing CA validates nothing over SChannel, and over the engine's Python TLS # it would be trusted with no root's revocation or constraints over it (the help's "Where each CA @@ -588,27 +642,29 @@ $approved = @(foreach ($entry in $entries) { } }) # Under -Confirm the operator may decline a root or an intermediate CA. An approved CA must still -# reach a root through the approved certificates alone, or it would land in LocalMachine\CA with no -# root over it. -$approvedSet = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection -foreach ($entry in $approved) { [void]$approvedSet.Add($entry.Certificate) } +# reach a root through the approved certificates and the declined ones the machine already holds +# (Get-AnchorSet), or it would land in LocalMachine\CA with no root over it. +$anchorSet = Get-AnchorSet -Entries $entries -Approved $approved $orphans = @(foreach ($entry in $approved) { - if (Get-ChainRefusal -Certificate $entry.Certificate -Bundle $approvedSet) { + if (Get-ChainRefusal -Certificate $entry.Certificate -Bundle $anchorSet) { "$($entry.Certificate.Thumbprint) ($(ConvertTo-PrintableText $entry.Certificate.Subject))" } }) if ($orphans.Count -gt 0) { throw ("Nothing was imported. A root or intermediate CA was declined that these approved CAs " + - "chain through, and a CA is not imported without the path to its root: $($orphans -join '; ').") + "chain through, and the machine's own store does not hold it, so a CA is not imported " + + "without the path to its root: $($orphans -join '; ').") } +if ($approved.Count -eq 0 -and -not $WhatIfPreference) { + Write-Host "" + Write-Host "Nothing was imported: every certificate was declined." +} if ($approved.Count -gt 0) { - # A CA already in the machine's own store is named as such and left alone, since a later rotation - # must not remove it as the file's. That store is the machine's registry store. The logical - # store X509Store reads also shows Group Policy and enterprise copies, and skipping on one of - # those would leave the import resting on that policy. Roots go first, so a failure part way never leaves an approved issuing CA added - # and an approved root not. Nothing is rolled back; the error says how many were added. - $registryName = @{ Root = 'ROOT'; CertificateAuthority = 'CA' } + # A CA the machine's own store already holds (Get-PresentStoreName) is named as such and left + # alone, since a later rotation must not remove it as the file's. Roots go first, so a failure + # part way never leaves an approved issuing CA added and an approved root not. Nothing is + # rolled back; the error says how many were added. $added = 0 $present = 0 try { @@ -619,10 +675,9 @@ if ($approved.Count -gt 0) { $store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite) try { foreach ($entry in $batch) { - $thumb = $entry.Certificate.Thumbprint - $own = "HKLM:\SOFTWARE\Microsoft\SystemCertificates\$($registryName[$name])\Certificates\$thumb" - if (Test-Path -LiteralPath $own) { - Write-Host "Already in $($stores[$name].Label), left as it was: $thumb" + $found = Get-PresentStoreName -Certificate $entry.Certificate -StoreName $name + if ($found) { + Write-Host "Already in $($stores[$found].Label), left as it was: $($entry.Certificate.Thumbprint)" $present++ continue } @@ -638,17 +693,21 @@ if ($approved.Count -gt 0) { "already present, before this failed: $($_.Exception.Message)") } Write-Host "" - Write-Host "Imported $added CA certificate(s); $present were already in their store." + if ($added -eq 0) { + Write-Host "Nothing was added: all $present approved certificate(s) were already in the machine's own store." + } else { + Write-Host "Added $added CA certificate(s); $present were already in the machine's own store." + } $notYet = @($approved | Where-Object { $_.Certificate.NotBefore.ToUniversalTime() -gt $nowUtc }) if ($notYet.Count -gt 0) { # Staged ahead of its start: say when it will validate, not that it does. $from = ($notYet | ForEach-Object { $_.Certificate.NotBefore.ToUniversalTime() } | Sort-Object -Descending | Select-Object -First 1).ToString('u') - Write-Warning ("$($notYet.Count) imported certificate(s) are not valid yet. A chain through " + + Write-Warning ("$($notYet.Count) approved certificate(s) are not valid yet. A chain through " + "them does not validate before $from; do not roll the DB server cert onto it before then.") - } else { - # Every approved CA reached an approved root above, so a root is now in the Root store. - Write-Host "A DB server cert chaining to the imported root now validates with" -ForegroundColor Green + } elseif ($added -gt 0) { + # Only after an add: every approved CA reached a root above, approved or already in Root. + Write-Host "A DB server cert chaining to these CAs now validates with" -ForegroundColor Green Write-Host "trust_server_certificate = false (no TrustServerCertificate=true needed)." -ForegroundColor Green } Write-Host "" diff --git a/scripts/service/measure-store-access.ps1 b/scripts/service/measure-store-access.ps1 index 336ccd1fde..0e4a2d48ec 100644 --- a/scripts/service/measure-store-access.ps1 +++ b/scripts/service/measure-store-access.ps1 @@ -996,8 +996,11 @@ try { $what = if ($script:GateLetStartSeen) { "created $DbPath and reached the ADR 0167 gate, which let the start go on (the READING line quotes it) rather than refusing it," } elseif (Test-Path -LiteralPath $DbPath) { "created $DbPath but did not log the expected refusal line" } else { "never created $DbPath" } - Add-Failure ("$ServiceIdentity $what within $WaitSeconds s on a fresh store. " + - (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) + # As in the control and restart branches: a gate that let the start go on rules out a + # lockout, so only a start it did not reach gets the store-access attribution. + $cause = if ($script:GateLetStartSeen) { "The store's access is not in question." } + else { Get-Attribution $ServiceIdentity (Get-ServiceSids) "" } + Add-Failure ("$ServiceIdentity $what within $WaitSeconds s on a fresh store. " + $cause) Show-LogTail } elseif ($noAdmin.Count -eq 0) { $line = @(Get-LogMatches ([regex]::Escape($GateRefusal)) | @@ -1051,8 +1054,11 @@ try { $what = if ($script:GateLetStartSeen) { "created $DbPath and reached the ADR 0167 gate, which let the start go on (the READING line quotes it) rather than refusing it," } elseif (Test-Path -LiteralPath $DbPath) { "created $DbPath but did not log the expected refusal line" } else { "never created $DbPath" } - Add-Failure ("$ServiceIdentity $what within $WaitSeconds s on a fresh store. " + - (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) + # As in the control and restart branches: a gate that let the start go on rules out a + # lockout, so only a start it did not reach gets the store-access attribution. + $cause = if ($script:GateLetStartSeen) { "The store's access is not in question." } + else { Get-Attribution $ServiceIdentity (Get-ServiceSids) "" } + Add-Failure ("$ServiceIdentity $what within $WaitSeconds s on a fresh store. " + $cause) Show-LogTail } else { Add-Reading "$ServiceIdentity created and opened $DbPath, then the ADR 0167 gate refused it (expected)" diff --git a/tests/test_import_db_ca_bundle.py b/tests/test_import_db_ca_bundle.py index b4aebec61c..b759270bff 100644 --- a/tests/test_import_db_ca_bundle.py +++ b/tests/test_import_db_ca_bundle.py @@ -10,9 +10,9 @@ root goes into the machine Root store and any other CA into the intermediate store. NOTHING HERE WRITES A CERTIFICATE STORE. The fixtures are generated in a pytest temporary -directory. The function-level tests lift ``Read-CaBundle``, ``Get-CaRefusal``, -``Get-CaStoreName`` and ``Get-ChainRefusal`` (with the ``$SignatureHashes`` table and the helpers -they call) out of the script's AST and call them, as +directory. The function-level tests lift at least ``Read-CaBundle``, ``Get-CaRefusal``, +``Get-CaStoreName``, ``Get-ChainRefusal``, ``Get-PresentStoreName`` and ``Get-AnchorSet`` (with the +``$SignatureHashes`` table and the helpers they call) out of the script's AST and call them, as ``tests/test_import_db_ca_console_text.py`` does. The whole-script tests are skipped in an elevated session. All but one run it with ``-WhatIf``, where ``ShouldProcess`` declines every import, so the store is never opened, and even a regression that ignored ``-WhatIf`` would meet the script's own @@ -135,6 +135,10 @@ def chain() -> dict[str, tuple[x509.Certificate, _Key]]: "future_root": _cert("Synthetic Future Root CA", True, valid_from_days=2), # Valid itself, but its only root in the file has expired. "under_expired": _cert("Synthetic Issuing CA Under Expired Root", True, expired_root), + # Expired, and not a CA either: skipped for its expiry before anything refuses it. + "expired_leaf": _cert( + "expired.db.test.invalid", False, root, valid_from_days=-30, valid_to_days=-1 + ), "unconstrained": _cert("Synthetic No Constraints", None), "no_cert_sign": _cert("Synthetic CA Without CertSign", True, cert_sign=False), "cert_sign": _cert("Synthetic CA With CertSign", True, cert_sign=True), @@ -209,7 +213,29 @@ def build(name: x509.Name, public: object, signer: object) -> x509.Certificate: .sign(enc_key, hashes.SHA256()) ) assert enc_root.subject.public_bytes() != enc_issuing.issuer.public_bytes() + # The same text in another letter case: the Windows chain engine does not link it either. + case_key = ec.generate_private_key(ec.SECP256R1()) + case_root = build( + x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Synthetic Case Root CA")]), + case_key.public_key(), + case_key, + ) + case_issuing = ( + x509.CertificateBuilder() + .subject_name( + x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Synthetic Case Issuing CA")]) + ) + .issuer_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "SYNTHETIC CASE ROOT CA")])) + .public_key(ec.generate_private_key(ec.SECP256R1()).public_key()) + .serial_number(x509.random_serial_number()) + .not_valid_before(now - datetime.timedelta(days=1)) + .not_valid_after(now + datetime.timedelta(days=30)) + .add_extension(x509.BasicConstraints(ca=True, path_length=None), True) + .sign(case_key, hashes.SHA256()) + ) return { + "case_root": case_root, + "case_issuing": case_issuing, "enc_root": enc_root, "enc_issuing": enc_issuing, "rsa_root": build(rsa_name, rsa_key.public_key(), rsa_key), @@ -287,6 +313,8 @@ def files( "pem_only_expired_root": _pem(chain["expired_root"][0]), "pem_under_expired": _pem(root, chain["expired_root"][0], chain["under_expired"][0]), "pem_encoded_names": _pem(extra["enc_root"], extra["enc_issuing"]), + "pem_case_names": _pem(extra["case_root"], extra["case_issuing"]), + "pem_expired_leaf": _pem(root, chain["expired_leaf"][0]), "pem_future_root": _pem(chain["future_root"][0]), "pem_pss_root": _pem(extra["pss_root"]), # Bare base64 behind a byte-order mark, UTF-8 and UTF-16. @@ -454,12 +482,17 @@ def chained(name: str) -> dict[str, object]: assert chained("p7b_der") == {root: None, inter: None} assert chained("root_and_rekey") == {root: None, rekey: None} - # Names that differ only in string encoding still link a CA to its root. - assert chained("pem_encoded_names") == { - _thumb(extra["enc_root"]): None, - _thumb(extra["enc_issuing"]): None, - } unchained = "it does not chain to a self-signed root in the file" + # Names that differ only in string type or letter case do not link a CA to its root, as the + # Windows chain engine does not link them. + for name, root_key, issuing_key in ( + ("pem_encoded_names", "enc_root", "enc_issuing"), + ("pem_case_names", "case_root", "case_issuing"), + ): + assert chained(name) == { + _thumb(extra[root_key]): None, + _thumb(extra[issuing_key]): unchained, + }, name for name, key in ( ("pem_foreign_intermediate", "foreign_intermediate"), ("pem_name_only_intermediate", "name_only_intermediate"), @@ -643,15 +676,114 @@ def test_a_file_whose_only_root_has_expired_imports_nothing(files: dict[str, Pat ), proc.stdout -def test_a_chain_whose_names_differ_only_in_encoding_is_offered( - extra: dict[str, x509.Certificate], files: dict[str, Path] +@pytest.mark.parametrize( + ("name", "issuing"), + [("pem_encoded_names", "enc_issuing"), ("pem_case_names", "case_issuing")], +) +def test_a_chain_whose_names_differ_only_in_encoding_or_case_is_refused( + name: str, issuing: str, extra: dict[str, x509.Certificate], files: dict[str, Path] ) -> None: - proc = _run_script(files["pem_encoded_names"]) + # The Windows chain engine links an issuer only by the exact bytes of its name, so SChannel + # could not build this chain: the file is refused, not imported. + proc = _run_script(files[name]) + out = proc.stdout + proc.stderr + assert proc.returncode != 0, out + message = _error_message(proc) + assert message.startswith("Nothing was imported."), message + assert _pair(extra[issuing], _UNCHAINED) in message, message + assert "What if" not in out + + +def test_an_expired_certificate_is_skipped_before_anything_refuses_it( + chain: dict[str, tuple[x509.Certificate, _Key]], files: dict[str, Path] +) -> None: + # Expired and not a CA: listed and skipped, so the root beside it is still offered. + proc = _run_script(files["pem_expired_leaf"]) out = proc.stdout + proc.stderr assert proc.returncode == 0, out + lines = proc.stdout.splitlines() + at = lines.index(f" Thumbprint : {_thumb(chain['expired_leaf'][0])}") + block = list(itertools.takewhile(lambda ln: ln.startswith(" "), lines[at:])) + assert " Skipped : it has expired, so it is not installed" in block, block + assert not any(ln.startswith(" Refused") for ln in block), block offered = [ln for ln in out.splitlines() if "What if" in ln] - assert len(offered) == 2, offered - assert any(_thumb(extra["enc_issuing"]) in ln and "LocalMachine\\CA" in ln for ln in offered) + assert len(offered) == 1 and _thumb(chain["root"][0]) in offered[0], offered + + +def test_a_ca_already_held_counts_where_it_anchors_and_a_declined_held_root_anchors( + tmp_path: Path, chain: dict[str, tuple[x509.Certificate, _Key]], files: dict[str, Path] +) -> None: + """``Get-PresentStoreName`` and ``Get-AnchorSet`` lifted out of the script, with a directory + standing in for the machine's registry stores, so nothing reads or writes the real ones.""" + root = _thumb(chain["root"][0]) + inter = _thumb(chain["intermediate"][0]) + # Each case: the registry store names holding the root, then those holding the issuing CA. + cases = { + "none": ([], []), + "root_in_root": (["ROOT"], []), + "root_in_ca": (["CA"], []), + "inter_in_ca": ([], ["CA"]), + "inter_in_root": ([], ["ROOT"]), + } + for case, (root_in, inter_in) in cases.items(): + for store, thumb in [(s, root) for s in root_in] + [(s, inter) for s in inter_in]: + held = tmp_path / case / store / "Certificates" + held.mkdir(parents=True, exist_ok=True) + (held / thumb).write_bytes(b"") + script = f""" +$ErrorActionPreference = 'Stop' +$ast = [System.Management.Automation.Language.Parser]::ParseFile( + {_psq(str(_SCRIPT))}, [ref]$null, [ref]$null) +$set = $ast.Find({{ $args[0] -is [System.Management.Automation.Language.AssignmentStatementAst] -and + $args[0].Left.Extent.Text -eq '$SignatureHashes' }}, $false) +. ([scriptblock]::Create($set.Extent.Text)) +foreach ($name in @('Test-EscapedForConsole', 'ConvertTo-PrintableText', 'Read-DerHeader', + 'Read-CaBundle', 'Test-SignedBy', 'Test-SelfSigned', 'Test-SameName', + 'Get-ChainRefusal', 'Get-CaStoreName', 'Get-PresentStoreName', + 'Get-AnchorSet')) {{ + $def = $ast.Find({{ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] -and + $args[0].Name -eq $name }}, $true) + if (-not $def) {{ throw "missing function $name" }} + . ([scriptblock]::Create($def.Extent.Text)) +}} +$bundle = Read-CaBundle -Bytes ([System.IO.File]::ReadAllBytes({_psq(str(files["p7b_der"]))})) +$entries = @(foreach ($c in $bundle) {{ @{{ Certificate = $c; StoreName = (Get-CaStoreName -Certificate $c) }} }}) +$out = @{{}} +foreach ($case in {_psq(",".join(cases))}.Split(',')) {{ + $dir = Join-Path {_psq(str(tmp_path))} $case + $present = @{{}} + foreach ($e in $entries) {{ + $present[$e.Certificate.Thumbprint] = Get-PresentStoreName -Certificate $e.Certificate -StoreName $e.StoreName -RegistryRoot $dir + }} + # The root declined, the issuing CA approved. + $approved = @($entries | Where-Object {{ $_.StoreName -ne 'Root' }}) + $anchors = Get-AnchorSet -Entries $entries -Approved $approved -RegistryRoot $dir + $out[$case] = @{{ present = $present; orphan = (Get-ChainRefusal -Certificate $approved[0].Certificate -Bundle $anchors) }} +}} +$out | ConvertTo-Json -Depth 5 -Compress +""" + f = tmp_path / f"dbca-{uuid.uuid4().hex}.ps1" + f.write_text(script, encoding="utf-8") + proc = subprocess.run( + [_pwsh(), "-NoProfile", "-NonInteractive", "-File", str(f)], + capture_output=True, + text=True, + encoding="utf-8", + timeout=120, + ) + assert proc.returncode == 0, f"harness failed: {proc.stdout}\n{proc.stderr}" + got = json.loads(proc.stdout.strip().splitlines()[-1]) + # A root counts only in Root; an issuing CA counts in CA or in Root. + assert got["none"]["present"] == {root: None, inter: None}, got + assert got["root_in_root"]["present"] == {root: "Root", inter: None}, got + assert got["root_in_ca"]["present"] == {root: None, inter: None}, got + assert got["inter_in_ca"]["present"] == {root: None, inter: "CertificateAuthority"}, got + assert got["inter_in_root"]["present"] == {root: None, inter: "Root"}, got + # Declining a root the machine's Root store already holds leaves the issuing CA anchored; a root + # held nowhere, or only in the CA store, does not. + assert got["root_in_root"]["orphan"] is None, got + for case in ("none", "root_in_ca", "inter_in_ca", "inter_in_root"): + assert got[case]["orphan"] == _UNCHAINED, (case, got) def test_a_root_signed_in_a_way_the_script_cannot_verify_is_not_reported_missing( diff --git a/tests/test_measure_store_access_gate_refusal.py b/tests/test_measure_store_access_gate_refusal.py index b7a97a0ad3..c827200505 100644 --- a/tests/test_measure_store_access_gate_refusal.py +++ b/tests/test_measure_store_access_gate_refusal.py @@ -128,3 +128,16 @@ def test_only_the_refusal_counts_as_a_refused_start(tmp_path: Path, host: str) - # The control: the clause the wait used to match alone is in both warning lines, so the old # criterion took an engine that went on to serve for a refused one. assert got["clause"] == [True, True, True, True, False], got + + +def test_a_first_start_the_gate_let_go_on_is_not_attributed_to_store_access() -> None: + # Both first-start branches name the cause only as the control and restart branches do: the + # store-access attribution only when the gate did not let the start go on. + text = _SCRIPT.read_text(encoding="utf-8") + failure = 'Add-Failure ("$ServiceIdentity $what within $WaitSeconds s on a fresh store. "' + assert text.count(failure) == 2 + for at in [i for i in range(len(text)) if text.startswith(failure, i)]: + before = text[text.rindex("$cause = if", 0, at) : at] + assert before.startswith("$cause = if ($script:GateLetStartSeen) {"), before + assert "Get-Attribution" in before.split("else", 1)[1], before + assert "Get-Attribution" not in text[at : text.index("\n", at)] From 9650211c86bb3dc8f54801c59d4a9a70f036e05d Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 08:57:43 -0600 Subject: [PATCH 09/19] Service scripts: state where byte-equal name linking is stricter than SChannel Review round-80536edf (xhigh) found two statements the previous commit made false (BACKLOG #5561, #5658): - The Windows chain engine can link an issuer by key identifier even when the names differ in case or string type, so byte-equal name linking is stricter than SChannel there, not equal to it. The help, the Test-SameName comment, the DANGEROUS-FUNCTIONALITY.md row and the test comments now say so. - "The machine's own Root store" read as the logical store the operator sees. The help and DEPLOY-SERVER-DB.md 5.2 now say the ROOT registry store, and that a Group Policy, enterprise or AuthRoot copy does not count. Proposed PR title: Service scripts: import-db-ca.ps1 reads every certificate and trusts only CAs; measure-store-access counts only the gate refusal (BACKLOG #5561, #5658) Proposed banner: #5561, #5658 PARTIAL -- engine PR 3025; review findings 2, 9, 10, 11, 13, 14 of round-013ab02b and the open findings of round-80536edf remain open. --- docs/DANGEROUS-FUNCTIONALITY.md | 2 +- docs/DEPLOY-SERVER-DB.md | 3 ++- scripts/service/import-db-ca.ps1 | 27 +++++++++++++++------------ tests/test_import_db_ca_bundle.py | 10 ++++++---- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/docs/DANGEROUS-FUNCTIONALITY.md b/docs/DANGEROUS-FUNCTIONALITY.md index 0b9de85b1e..b9a0743f64 100644 --- a/docs/DANGEROUS-FUNCTIONALITY.md +++ b/docs/DANGEROUS-FUNCTIONALITY.md @@ -912,7 +912,7 @@ administrator rights. What each one grants or changes: | `uninstall-service.ps1` | Removes the service. With `-RemoveLogonRight`, rewrites local security policy with `secedit` again, to take "Log on as a service" back, working in a new administrator-only folder under the Windows folder's `Temp`. With `-RemoveAccountAces`, removes the account's permission entries from the data and config directories. | | `install-net-helper.ps1` | Registers `mefor-net-helper` (ADR 0056) as a service running as LocalSystem. It listens on the named pipe `\\.\pipe\mefor-net-helper` and adds or removes one floating IP address by running `netsh`. | | `uninstall-net-helper.ps1` | Removes the helper service. With `-ReleaseAddress`, first asks the helper to remove the floating address from this machine. | -| `import-db-ca.ps1` | Adds the CA certificates in one file to the machine stores `Cert:\LocalMachine\Root` and `Cert:\LocalMachine\CA`. Which CA goes where, and what each store is a trust anchor for, is in [`DEPLOY-SERVER-DB.md`](DEPLOY-SERVER-DB.md) section 5.2; a self-signed CA in the file goes into `Cert:\LocalMachine\Root`. An expired certificate is listed and skipped, never added. A CA the machine's own store already holds is named and left as it is. Refuses the whole file if, at least, any unexpired certificate in it is not a CA or carries a private key, the file holds no unexpired self-signed root, or an unexpired CA in it does not chain to one through the file's unexpired certificates, its issuer name equal to its signer's subject byte for byte. With `-WhatIf` it only shows what it would add, and runs without administrator rights. | +| `import-db-ca.ps1` | Adds the CA certificates in one file to the machine stores `Cert:\LocalMachine\Root` and `Cert:\LocalMachine\CA`. Which CA goes where, and what each store is a trust anchor for, is in [`DEPLOY-SERVER-DB.md`](DEPLOY-SERVER-DB.md) section 5.2; a self-signed CA in the file goes into `Cert:\LocalMachine\Root`. An expired certificate is listed and skipped, never added. A CA the machine's own store already holds is named and left as it is. Refuses the whole file if, at least, any unexpired certificate in it is not a CA or carries a private key, the file holds no unexpired self-signed root, or an unexpired CA in it does not chain to one through the file's unexpired certificates, its issuer name equal to its signer's subject byte for byte (stricter than the Windows chain engine, which can also link by key identifier). With `-WhatIf` it only shows what it would add, and runs without administrator rights. | | `measure-store-access.ps1` | A measurement script, in the assessment's operator-run scope. [`ASVS-ASSESSMENT-METHOD.md`](ASVS-ASSESSMENT-METHOD.md) section 2 puts this folder in by its scope sentence, and names this script in by owner ruling of 2026-10-05. It measures whether the service account and the operator can both open one store; it is not a deployment step. It installs and uninstalls the service, and deletes the `-DataDir` it is given before it starts. Run it only on a disposable host: with its default `-ServiceName`, it would take over and then remove an engine service installed under that name. It also runs `python` from `PATH` while elevated. Unless `-AppExe` names another, it also takes the `messagefoundry` command from `PATH` (`Get-Command messagefoundry`) and runs it while elevated, at least for `gen-key`, which mints the run's store key, and `protect-key`, which writes the service's key file. The `python` probe starts import the engine's own code from whatever install that `python` finds, whatever `-AppExe` says. In the CI job `windows-service-smoke`, whose install step installs the engine editable, both are the checkout's. The installer's check of the engine install ([`SERVICE.md`](SERVICE.md), *Install the engine where only administrators can write*) never covers the `python` starts, and covers `protect-key` only when `-AppExe` names the launcher it checks; `gen-key` runs before the installer does. So whoever could write a folder on `PATH`, or the install those commands load, would run code as administrator and would see the store key. The `protect-key` start is handed that store key in its environment, as `MEFOR_STORE_ENCRYPTION_KEY`, which is cleared after it exits. So are at least the `python` probe starts that screen the password and provision the administrator. Those starts and the probe start that signs in are also handed, as `MEFOR_W0_ADMIN_PASSWORD`, the synthetic administrator password, or for the screen each candidate it tries. Without `-AppExe`, the service itself runs the engine in the installer's default install root, not the `PATH` `messagefoundry`. It runs NSSM only by full path, from the `-NssmDir` it hands the installer, so it runs the copy the installer checked. The CI job `windows-service-smoke`, which runs this script, runs NSSM the same way. | **The run-as account is the setting to look at hardest.** `install-service.ps1` defaults to a diff --git a/docs/DEPLOY-SERVER-DB.md b/docs/DEPLOY-SERVER-DB.md index d9a0a548f1..2fe3bfbae3 100644 --- a/docs/DEPLOY-SERVER-DB.md +++ b/docs/DEPLOY-SERVER-DB.md @@ -568,7 +568,8 @@ there without its root. The helper refuses a file whose CAs do not chain to a se it; its help says how it checks that, and that it installs any self-signed CA in the file into `LocalMachine\Root`. With `-Confirm`, declining a root that an accepted CA chains through imports nothing, unless the machine's own -`LocalMachine\Root` store already holds that root, so give it the root of the CA +`ROOT` registry store already holds that root (a Group Policy, enterprise or AuthRoot copy shown +under `Cert:\LocalMachine\Root` does not count), so give it the root of the CA you mean to trust, alone or in that CA's own `.p7b` chain, and accept every certificate it offers. The built-ins below check none of that, so use them only for a file holding one self-signed root diff --git a/scripts/service/import-db-ca.ps1 b/scripts/service/import-db-ca.ps1 index 378a600cbc..3082b30cfd 100644 --- a/scripts/service/import-db-ca.ps1 +++ b/scripts/service/import-db-ca.ps1 @@ -31,13 +31,14 @@ reaches a certificate in LocalMachine\CA validates there without its root. A file is refused unless it holds a self-signed root and every other unexpired CA in it chains to a self-signed root through the file's own unexpired certificates. Each link's signature is verified, and its - issuer name must equal its signer's subject byte for byte, as the Windows chain engine requires: - names that differ only in string type or letter case do not link there, so a file linked only - by such names is refused. Not checked, at least: revocation, name constraints, path length and + issuer name must equal its signer's subject byte for byte, so a file linked only by names that + differ in string type or letter case is refused. Without key identifiers the Windows chain + engine does not link such names either; with them it can, so this check is stricter than + SChannel there and refuses some chains SChannel would build. Not checked, at least: revocation, name constraints, path length and extended key usage. Any self-signed CA in the file is installed in LocalMachine\Root, whether or not another certificate in the file chains to it. With -Confirm the operator may decline a root; then nothing is imported if an approved CA chains only through it, unless that root is - already in the machine's own LocalMachine\Root store. + already in the machine's own Root registry store (see Idempotent below). PostgreSQL can alternatively pin a CA by file via [store].ssl_root_cert (no machine import needed). This machine-store import is the path SQL Server REQUIRES; Postgres can use either. @@ -64,7 +65,8 @@ Run from an elevated (Administrator) PowerShell prompt; -WhatIf needs no elevation and changes nothing. Idempotent: a root already in the machine's own Root registry store, or any other CA already in its own CA or Root registry store, is named and left as it is. A copy seen only - through Group Policy or the enterprise store does not count, so that CA is added too. A root + through at least Group Policy, the enterprise store or AuthRoot does not count, so that CA is + added too, and a root declined under -Confirm counts as held only in that registry store. A root found only in the CA store is added to Root, since the CA store anchors nothing for SChannel. .PARAMETER CaPath @@ -328,12 +330,12 @@ $SignatureHashes = @{ '1.2.840.10045.4.3.3' = 'SHA384'; '1.2.840.10045.4.3.4' = 'SHA512' } -# Whether two distinguished names are the same name as the Windows chain engine links them: equal -# as raw DER bytes. A name spelt as a PrintableString in one certificate and a UTF8String in another, -# or in another letter case, does not link there (X509Chain.Build stops at PartialChain), so it does -# not link here either, and a file that relies on such a link is refused rather than imported as a -# chain SChannel cannot build. It is only a filter for candidate issuers: the signature check in -# Test-SignedBy decides. +# Whether two distinguished names are the same name: equal as raw DER bytes. A name spelt as a +# PrintableString in one certificate and a UTF8String in another, or in another letter case, does +# not link here. Without key identifiers the Windows chain engine does not link it either +# (X509Chain.Build stops at PartialChain); with matching key identifiers it can, so this refuses +# some chains SChannel would build rather than accept one it could not. It is only a filter for +# candidate issuers: the signature check in Test-SignedBy decides. function Test-SameName { param( [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X500DistinguishedName]$A, @@ -485,7 +487,8 @@ function Get-CaRefusal { # Which of the machine's own registry stores already holds the certificate where it counts, as a key # of $stores, or $null. A root counts only in Root, since the CA store anchors nothing for SChannel; # any other CA counts in CA or in Root, where it is trusted at least as far. The logical store -# X509Store reads also shows Group Policy and enterprise copies; those are not read here, so a CA +# X509Store reads also shows at least Group Policy, enterprise and AuthRoot copies; those are not +# read here, so a CA # held only there is added, and the import does not rest on that policy. $RegistryRoot is a # parameter only so a test can point it at a directory. function Get-PresentStoreName { diff --git a/tests/test_import_db_ca_bundle.py b/tests/test_import_db_ca_bundle.py index b759270bff..9e3c547d4b 100644 --- a/tests/test_import_db_ca_bundle.py +++ b/tests/test_import_db_ca_bundle.py @@ -213,7 +213,8 @@ def build(name: x509.Name, public: object, signer: object) -> x509.Certificate: .sign(enc_key, hashes.SHA256()) ) assert enc_root.subject.public_bytes() != enc_issuing.issuer.public_bytes() - # The same text in another letter case: the Windows chain engine does not link it either. + # The same text in another letter case. With no key identifiers, as here, the Windows chain + # engine does not link it either. case_key = ec.generate_private_key(ec.SECP256R1()) case_root = build( x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Synthetic Case Root CA")]), @@ -484,7 +485,7 @@ def chained(name: str) -> dict[str, object]: assert chained("root_and_rekey") == {root: None, rekey: None} unchained = "it does not chain to a self-signed root in the file" # Names that differ only in string type or letter case do not link a CA to its root, as the - # Windows chain engine does not link them. + # Windows chain engine does not link them between certificates with no key identifiers. for name, root_key, issuing_key in ( ("pem_encoded_names", "enc_root", "enc_issuing"), ("pem_case_names", "case_root", "case_issuing"), @@ -683,8 +684,9 @@ def test_a_file_whose_only_root_has_expired_imports_nothing(files: dict[str, Pat def test_a_chain_whose_names_differ_only_in_encoding_or_case_is_refused( name: str, issuing: str, extra: dict[str, x509.Certificate], files: dict[str, Path] ) -> None: - # The Windows chain engine links an issuer only by the exact bytes of its name, so SChannel - # could not build this chain: the file is refused, not imported. + # These certificates carry no key identifiers, and without them the Windows chain engine links + # an issuer only by the exact bytes of its name, so SChannel could not build this chain: the + # file is refused, not imported. proc = _run_script(files[name]) out = proc.stdout + proc.stderr assert proc.returncode != 0, out From 2cdb5fa35d16fc08af5dab9d1b66feb73c75c307 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 10:43:56 -0600 Subject: [PATCH 10/19] measure-store-access.ps1 recognises the gate's could-not-run warning; PR narrowed to it Narrows this branch to BACKLOG #5658. The import-db-ca.ps1 chain work for BACKLOG #5561 is withdrawn: that script, its tests and every doc passage this branch changed about it are back to their merge-base text, and #5561 goes back to a later brief built on the Windows chain engine. Wait-ForGateRefusal searches the log for the gate's let-start lines on their own, at least three of them, now including app.py's "security-notice gate could not run" warning, which does not carry the clause the other lines share. The two first-start failure blocks are one Add-FirstStartFailure. Proposed PR title: measure-store-access.ps1 counts only the ADR 0167 refusal as a refused start (BACKLOG #5658) Proposed banners: #5658 BUILT, in this PR. #5561 OPEN; this PR's history holds a withdrawn hand-built chain check. --- changelog.d/5658.fixed.md | 5 + docs/AOAG-DEPLOYMENT.md | 5 +- docs/DANGEROUS-FUNCTIONALITY.md | 2 +- docs/DEPLOY-SERVER-DB.md | 27 +- docs/INPUT-SURFACES.md | 2 +- scripts/security/crypto_inventory_check.py | 7 +- scripts/service/import-db-ca.ps1 | 375 +++----------- scripts/service/measure-store-access.ps1 | 52 +- tests/test_import_db_ca_bundle.py | 478 ++---------------- tests/test_import_db_ca_console_text.py | 4 +- .../test_measure_store_access_gate_refusal.py | 167 ++++-- 11 files changed, 291 insertions(+), 833 deletions(-) create mode 100644 changelog.d/5658.fixed.md diff --git a/changelog.d/5658.fixed.md b/changelog.d/5658.fixed.md new file mode 100644 index 0000000000..9b816dbfc1 --- /dev/null +++ b/changelog.d/5658.fixed.md @@ -0,0 +1,5 @@ +- **`measure-store-access.ps1` counts a start as refused only on the ADR 0167 gate's refusal + line.** It used to count any line naming "no enabled Administrator", which the gate's warnings + also carry when they let the start go on. A start the gate let go on now fails the arm without + naming store access as the cause. The script's `$GateLetStart` comment names the lines it + recognises. (`BACKLOG #5658`) diff --git a/docs/AOAG-DEPLOYMENT.md b/docs/AOAG-DEPLOYMENT.md index daebf16201..bc175edff1 100644 --- a/docs/AOAG-DEPLOYMENT.md +++ b/docs/AOAG-DEPLOYMENT.md @@ -472,9 +472,8 @@ The engine connects with `Encrypt=yes` and **verifies** the server certificate. posture; `MEFOR_ALLOW_INSECURE_TLS` does not lift it (owner ruling 2026-11-14, vault BACKLOG #3533 item 2). This has three consequences for the AG: -- ODBC Driver 18 validates against the **Windows machine stores** (the root in `LocalMachine\Root`; - [`DEPLOY-SERVER-DB.md`](DEPLOY-SERVER-DB.md) §5.2 says where each CA goes), and there is no - CA-file keyword. Import the DB CA on **every engine VM in both DCs** +- ODBC Driver 18 validates against the **Windows machine trust store** (`LocalMachine\Root`), and + there is no CA-file keyword. Import the DB CA on **every engine VM in both DCs** ([`DEPLOY-SERVER-DB.md`](DEPLOY-SERVER-DB.md) §5, `scripts/service/import-db-ca.ps1`). `[store].ssl_root_cert` is no substitute here: on SQL Server it pins one exact certificate and switches the store to `Encrypt=strict`, so after a failover to a replica that presents a different diff --git a/docs/DANGEROUS-FUNCTIONALITY.md b/docs/DANGEROUS-FUNCTIONALITY.md index b9a0743f64..97f8349477 100644 --- a/docs/DANGEROUS-FUNCTIONALITY.md +++ b/docs/DANGEROUS-FUNCTIONALITY.md @@ -912,7 +912,7 @@ administrator rights. What each one grants or changes: | `uninstall-service.ps1` | Removes the service. With `-RemoveLogonRight`, rewrites local security policy with `secedit` again, to take "Log on as a service" back, working in a new administrator-only folder under the Windows folder's `Temp`. With `-RemoveAccountAces`, removes the account's permission entries from the data and config directories. | | `install-net-helper.ps1` | Registers `mefor-net-helper` (ADR 0056) as a service running as LocalSystem. It listens on the named pipe `\\.\pipe\mefor-net-helper` and adds or removes one floating IP address by running `netsh`. | | `uninstall-net-helper.ps1` | Removes the helper service. With `-ReleaseAddress`, first asks the helper to remove the floating address from this machine. | -| `import-db-ca.ps1` | Adds the CA certificates in one file to the machine stores `Cert:\LocalMachine\Root` and `Cert:\LocalMachine\CA`. Which CA goes where, and what each store is a trust anchor for, is in [`DEPLOY-SERVER-DB.md`](DEPLOY-SERVER-DB.md) section 5.2; a self-signed CA in the file goes into `Cert:\LocalMachine\Root`. An expired certificate is listed and skipped, never added. A CA the machine's own store already holds is named and left as it is. Refuses the whole file if, at least, any unexpired certificate in it is not a CA or carries a private key, the file holds no unexpired self-signed root, or an unexpired CA in it does not chain to one through the file's unexpired certificates, its issuer name equal to its signer's subject byte for byte (stricter than the Windows chain engine, which can also link by key identifier). With `-WhatIf` it only shows what it would add, and runs without administrator rights. | +| `import-db-ca.ps1` | Adds the CA certificates in one file to the machine stores `Cert:\LocalMachine\Root` and `Cert:\LocalMachine\CA`. Which CA goes where, and what each store is a trust anchor for, is in [`DEPLOY-SERVER-DB.md`](DEPLOY-SERVER-DB.md) section 5.2. Refuses the whole file if, at least, any certificate in it is not a CA or carries a private key, or the file holds no self-signed root. With `-WhatIf` it only shows what it would add, and runs without administrator rights. | | `measure-store-access.ps1` | A measurement script, in the assessment's operator-run scope. [`ASVS-ASSESSMENT-METHOD.md`](ASVS-ASSESSMENT-METHOD.md) section 2 puts this folder in by its scope sentence, and names this script in by owner ruling of 2026-10-05. It measures whether the service account and the operator can both open one store; it is not a deployment step. It installs and uninstalls the service, and deletes the `-DataDir` it is given before it starts. Run it only on a disposable host: with its default `-ServiceName`, it would take over and then remove an engine service installed under that name. It also runs `python` from `PATH` while elevated. Unless `-AppExe` names another, it also takes the `messagefoundry` command from `PATH` (`Get-Command messagefoundry`) and runs it while elevated, at least for `gen-key`, which mints the run's store key, and `protect-key`, which writes the service's key file. The `python` probe starts import the engine's own code from whatever install that `python` finds, whatever `-AppExe` says. In the CI job `windows-service-smoke`, whose install step installs the engine editable, both are the checkout's. The installer's check of the engine install ([`SERVICE.md`](SERVICE.md), *Install the engine where only administrators can write*) never covers the `python` starts, and covers `protect-key` only when `-AppExe` names the launcher it checks; `gen-key` runs before the installer does. So whoever could write a folder on `PATH`, or the install those commands load, would run code as administrator and would see the store key. The `protect-key` start is handed that store key in its environment, as `MEFOR_STORE_ENCRYPTION_KEY`, which is cleared after it exits. So are at least the `python` probe starts that screen the password and provision the administrator. Those starts and the probe start that signs in are also handed, as `MEFOR_W0_ADMIN_PASSWORD`, the synthetic administrator password, or for the screen each candidate it tries. Without `-AppExe`, the service itself runs the engine in the installer's default install root, not the `PATH` `messagefoundry`. It runs NSSM only by full path, from the `-NssmDir` it hands the installer, so it runs the copy the installer checked. The CI job `windows-service-smoke`, which runs this script, runs NSSM the same way. | **The run-as account is the setting to look at hardest.** `install-service.ps1` defaults to a diff --git a/docs/DEPLOY-SERVER-DB.md b/docs/DEPLOY-SERVER-DB.md index 2fe3bfbae3..527cd4a6ae 100644 --- a/docs/DEPLOY-SERVER-DB.md +++ b/docs/DEPLOY-SERVER-DB.md @@ -150,7 +150,7 @@ server = "sql01.corp.example.com" database = "MessageFoundry" auth = "integrated" # Trusted_Connection=yes — the gMSA's identity authenticates encrypt = true # default; TLS to the DB -trust_server_certificate = false # default; verify the DB cert (import its CA into the machine stores, §5.2) +trust_server_certificate = false # default; verify the DB cert (import its CA into LocalMachine\Root, §5.2) require_managed_identity = true # refuse a static SQL login on production PHI (ASVS 13.2.1) # schema_management = "external" # the server-DB default; step 4 owns the DDL (BACKLOG #305) ``` @@ -552,8 +552,9 @@ From an **elevated (Administrator)** PowerShell, use the helper: .\scripts\service\import-db-ca.ps1 -CaPath C:\certs\internal-root-ca.crt ``` -The helper shows every certificate in the file, and refuses the whole file, importing nothing, on at -least the refusals its own help lists. +The helper shows every certificate in the file, and refuses the whole file in at least these cases: +any one is not a CA or carries a private key, the file holds no self-signed root, or a DER value in +it does not end where its bytes end. The helper's own help lists the rest. **Where each CA goes.** This paragraph and the helper's own help are the statement of it; other documents point here. A self-signed root, whose signature verifies under its own key, goes into @@ -564,13 +565,10 @@ Server hop still checks the chain up to the root. It **is** usable as a trust an engine's Python TLS on Windows: CPython's `ssl.create_default_context()`, which the PostgreSQL system-trust path calls, loads the `CA` and `ROOT` system stores, and Python 3.13 and later set partial-chain verification, so a chain that reaches a certificate in `LocalMachine\CA` validates -there without its root. The helper refuses a file whose CAs do not chain to a self-signed root in -it; its help says how it checks that, and that it installs any self-signed CA in the file into -`LocalMachine\Root`. With `-Confirm`, -declining a root that an accepted CA chains through imports nothing, unless the machine's own -`ROOT` registry store already holds that root (a Group Policy, enterprise or AuthRoot copy shown -under `Cert:\LocalMachine\Root` does not count), so give it the root of the CA -you mean to trust, alone or in that CA's own `.p7b` chain, and accept every certificate it offers. +there without its root. The helper refuses a file unless at least one certificate in it is a +self-signed root. It does not check that each other CA in the file chains to that root, and with +`-Confirm` you may still decline the root and accept another CA, so give it the root of the CA you +mean to trust, alone or in that CA's own `.p7b` chain, and accept every certificate it offers. The built-ins below check none of that, so use them only for a file holding one self-signed root CA you have checked yourself: @@ -603,12 +601,9 @@ connection fails validation** (NIST SP 800-52r2: rotate certificates before expi 2. **Roll the DB server certificate** to one issued by the new CA (a DB-administrator action). Because both CAs are trusted, connections keep validating across the swap. 3. **Remove the old CA** once every host trusts the new one and the server cert has rolled: - - **SQL Server:** delete the old CA's certificates, the root from `LocalMachine\Root` and any - issuing CA from `LocalMachine\CA`, by the thumbprints the helper listed when it imported that - file. An issuing CA left in `LocalMachine\CA` stays a trust anchor for the engine's Python TLS - (§5.2). Keep any certificate the new chain, or something else (Group Policy, say), still needs; - the helper's import names each one the store already held as "Already in". For example - `Get-ChildItem Cert:\LocalMachine\Root, Cert:\LocalMachine\CA | Where-Object Thumbprint -in @('', '') | Remove-Item`. + - **SQL Server:** delete the old CA from whichever machine store holds it, `LocalMachine\Root` for + a root or `LocalMachine\CA` for an issuing CA (e.g. + `Get-ChildItem Cert:\LocalMachine\Root, Cert:\LocalMachine\CA | Where-Object Thumbprint -eq | Remove-Item`). - **PostgreSQL:** drop the old CA from the PEM bundle and reload. **Rotating only the server leaf cert** (same CA): no trust-store change is needed — the new leaf still diff --git a/docs/INPUT-SURFACES.md b/docs/INPUT-SURFACES.md index 28ed0118a2..f131743811 100644 --- a/docs/INPUT-SURFACES.md +++ b/docs/INPUT-SURFACES.md @@ -523,7 +523,7 @@ help. Each rule is in the script's `param()` block or its body. | `uninstall-net-helper.ps1` | `-ServiceName` | The `-ServiceName` rule above | | `measure-store-access.ps1` | `-Order` | Required. One of `ProvisionFirst`, `StartFirst`, `StartFirstEndToEnd` | | | `-DataDir` | Required | -| `import-db-ca.ps1` | `-CaPath` | Required. A certificate file holding a self-signed root. The help block at the top of the script, which `Get-Help` does not print, as the paragraph above says, lists the shapes it reads and, with "at least", what it refuses | +| `import-db-ca.ps1` | `-CaPath` | Required. A certificate file holding a self-signed root, in which every certificate is a CA with no private key. The help block at the top of the script, which `Get-Help` does not print, as the paragraph above says, lists the shapes it reads and, with "at least", what it refuses | `install-service.ps1` also relates parameters to each other. `-EngineWheel` refuses `-AppExe` and `-PythonExe`. `-EngineWheelSha256`, `-RequirementsLock`, `-RequirementsLockSha256` and diff --git a/scripts/security/crypto_inventory_check.py b/scripts/security/crypto_inventory_check.py index 604e68cf3a..092ae36505 100644 --- a/scripts/security/crypto_inventory_check.py +++ b/scripts/security/crypto_inventory_check.py @@ -2928,10 +2928,9 @@ def check_non_python_operations(repo: Path) -> tuple[list[str], int, dict[str, f POWERSHELL_OPERATION_INVENTORY: dict[str, frozenset[str]] = { # --- The operator deployment path (docs/SERVICE.md). These two are the reason this arm exists. --- # Parses the operator's CA certificate file into a collection (so a malformed file fails before - # anything is touched and the operator sees every certificate they are about to trust), verifies - # each one's signature under its own key (to tell a self-signed root from an issuing CA) and - # under the keys of the file's other certificates (to check that each CA chains to a root in the - # file; RSA PKCS#1 v1.5 and ECDSA with SHA-1, SHA-256, SHA-384 or SHA-512), then INSTALLS each one through X509Store into a machine store (docs/DEPLOY-SERVER-DB.md + # anything is touched and the operator sees every certificate they are about to trust), checks + # whether each one's signature verifies under its own key (to tell a self-signed root from an + # issuing CA), then INSTALLS each one through X509Store into a machine store (docs/DEPLOY-SERVER-DB.md # section 5.2 says which). A trust-anchor decision for the whole host, not only the engine. "scripts/service/import-db-ca.ps1": frozenset( {"key_cert:X509", "key_cert:X509Store", "sign_verify:SignData"} diff --git a/scripts/service/import-db-ca.ps1 b/scripts/service/import-db-ca.ps1 index 3082b30cfd..b4729f2a82 100644 --- a/scripts/service/import-db-ca.ps1 +++ b/scripts/service/import-db-ca.ps1 @@ -29,45 +29,27 @@ ssl.create_default_context(), which the PostgreSQL system-trust path calls, loads the CA and ROOT system stores, and Python 3.13 and later set partial-chain verification, so a chain that reaches a certificate in LocalMachine\CA validates there without its root. A file is refused - unless it holds a self-signed root and every other unexpired CA in it chains to a self-signed - root through the file's own unexpired certificates. Each link's signature is verified, and its - issuer name must equal its signer's subject byte for byte, so a file linked only by names that - differ in string type or letter case is refused. Without key identifiers the Windows chain - engine does not link such names either; with them it can, so this check is stricter than - SChannel there and refuses some chains SChannel would build. Not checked, at least: revocation, name constraints, path length and - extended key usage. Any self-signed CA in the file is installed in LocalMachine\Root, whether - or not another certificate in the file chains to it. With -Confirm the operator may decline a - root; then nothing is imported if an approved CA chains only through it, unless that root is - already in the machine's own Root registry store (see Idempotent below). + unless at least one certificate in it is a self-signed root. That is the whole check: it does + not check that each other CA in the file chains to that root, and with -Confirm the operator + may still decline the root and accept another CA. PostgreSQL can alternatively pin a CA by file via [store].ssl_root_cert (no machine import needed). This machine-store import is the path SQL Server REQUIRES; Postgres can use either. The file is read once. Every certificate it holds is shown, checked, and only then added, so - nothing reaches a store that the operator was not shown. Roots are added first. An expired - certificate is listed and skipped, never installed, and is not checked as a CA. The whole file - is refused, and nothing is imported, in at least these cases: when any unexpired certificate in - it is not marked as a CA by its basic constraints, has a key usage that cannot sign certificates, carries a private key, or - is signed in a way this script does not verify, when it holds no unexpired self-signed root, - when an unexpired CA in it does not chain to an unexpired self-signed root through the file's - unexpired certificates, when a DER value in it - (the whole file, a PEM block, or decoded base64) does not end where its bytes end, or when a - control character other than tab, CR or LF sits outside its PEM blocks. Only RSA PKCS#1 v1.5 - and ECDSA signatures with SHA-1, SHA-256, SHA-384 or SHA-512 are verified; RSA-PSS and - SHA-224, for two, are not. A BER value with an indefinite length, which some tools write for a - .p7b, is refused by the DER rule; re-encode it as DER first. A certificate not valid yet is - listed with a note, not refused, so a new CA can be staged ahead of its start. Other text - outside PEM blocks, bare base64 among them, is ignored, not refused. At least a .pfx, and + nothing reaches a store that the operator was not shown. The whole file is refused, and + nothing is imported, when any certificate in it is not marked as a CA by its basic + constraints, has a key usage that cannot sign certificates, or carries a private key, when it + holds no self-signed root, when a DER value in it (the whole file, a PEM block, or decoded + base64) does not end where its bytes end, or when a control character other than tab, CR or + LF sits outside its PEM blocks. Other text outside PEM blocks, bare base64 among them, is + ignored, not refused. At least a .pfx, and a well-formed PEM block with any label but CERTIFICATE, X509 CERTIFICATE or PKCS7 (such as a PRIVATE KEY block), are refused before anything is parsed. Key material outside well-formed PEM armour is not looked for; no private key is ever loaded either way. Run from an elevated (Administrator) PowerShell prompt; -WhatIf needs no elevation and changes - nothing. Idempotent: a root already in the machine's own Root registry store, or any other CA - already in its own CA or Root registry store, is named and left as it is. A copy seen only - through at least Group Policy, the enterprise store or AuthRoot does not count, so that CA is - added too, and a root declined under -Confirm counts as held only in that registry store. A root - found only in the CA store is added to Root, since the CA store anchors nothing for SChannel. + nothing. Idempotent: re-importing the same CA is a no-op (each store keys on thumbprint). .PARAMETER CaPath Path to the CA certificate(s) to trust: a DER, base64 or PEM certificate (.cer/.crt/.pem, one @@ -226,11 +208,8 @@ function Read-CaBundle { $encoding = [System.Text.Encoding]::Unicode } elseif ($Bytes.Length -ge 2 -and $Bytes[0] -eq 0xFE -and $Bytes[1] -eq 0xFF) { $encoding = [System.Text.Encoding]::BigEndianUnicode - } elseif ($Bytes.Length -ge 3 -and $Bytes[0] -eq 0xEF -and $Bytes[1] -eq 0xBB -and $Bytes[2] -eq 0xBF) { - $encoding = [System.Text.Encoding]::UTF8 } - # GetString keeps a byte-order mark as U+FEFF, which bare base64 would otherwise fail to match. - $text = $encoding.GetString($Bytes).TrimStart([char]0xFEFF) + $text = $encoding.GetString($Bytes) # Every piece of DER, the whole file or one PEM block, must be a certificate or a PKCS#7 chain, # so a .pfx is refused wherever it sits, before it is parsed. $importDer = { @@ -320,40 +299,23 @@ function Read-CaBundle { return , $unique } -# The hash of each signature algorithm this script can verify, by OID: RSA PKCS#1 v1.5 and ECDSA, -# each with SHA-1, SHA-256, SHA-384 or SHA-512. Any other (RSA-PSS, SHA-224, Ed25519 and DSA among -# them) is not verified, and Get-CaRefusal refuses a certificate signed with one. -$SignatureHashes = @{ - '1.2.840.113549.1.1.5' = 'SHA1'; '1.2.840.113549.1.1.11' = 'SHA256' - '1.2.840.113549.1.1.12' = 'SHA384'; '1.2.840.113549.1.1.13' = 'SHA512' - '1.2.840.10045.4.1' = 'SHA1'; '1.2.840.10045.4.3.2' = 'SHA256' - '1.2.840.10045.4.3.3' = 'SHA384'; '1.2.840.10045.4.3.4' = 'SHA512' -} - -# Whether two distinguished names are the same name: equal as raw DER bytes. A name spelt as a -# PrintableString in one certificate and a UTF8String in another, or in another letter case, does -# not link here. Without key identifiers the Windows chain engine does not link it either -# (X509Chain.Build stops at PartialChain); with matching key identifiers it can, so this refuses -# some chains SChannel would build rather than accept one it could not. It is only a filter for -# candidate issuers: the signature check in Test-SignedBy decides. -function Test-SameName { - param( - [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X500DistinguishedName]$A, - [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X500DistinguishedName]$B) - return ([Convert]::ToBase64String($A.RawData) -ceq [Convert]::ToBase64String($B.RawData)) -} - -# Whether $Signer issued the certificate: the certificate's issuer is $Signer's subject -# (Test-SameName) AND its signature verifies under $Signer's public key. A signature algorithm -# outside $SignatureHashes reads as not signed, which fails closed. -function Test-SignedBy { - param( - [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate, - [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Signer) - if (-not (Test-SameName $Signer.SubjectName $Certificate.IssuerName)) { return $false } +# Whether the certificate is self-signed: its subject and issuer are the same name AND its signature +# verifies under its own public key. A self-issued certificate another key signed, such as a CA +# re-key link certificate, is not. RSA PKCS#1 v1.5 and ECDSA with SHA-1 or SHA-2 are checked; any +# other signature algorithm (RSA-PSS, for one) reads as not self-signed, which fails closed. +function Test-SelfSigned { + param([Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate) + if ([Convert]::ToBase64String($Certificate.SubjectName.RawData) -cne + [Convert]::ToBase64String($Certificate.IssuerName.RawData)) { return $false } + $hashes = @{ + '1.2.840.113549.1.1.5' = 'SHA1'; '1.2.840.113549.1.1.11' = 'SHA256' + '1.2.840.113549.1.1.12' = 'SHA384'; '1.2.840.113549.1.1.13' = 'SHA512' + '1.2.840.10045.4.1' = 'SHA1'; '1.2.840.10045.4.3.2' = 'SHA256' + '1.2.840.10045.4.3.3' = 'SHA384'; '1.2.840.10045.4.3.4' = 'SHA512' + } $oid = $Certificate.SignatureAlgorithm.Value - if (-not $SignatureHashes.ContainsKey($oid)) { return $false } - $hash = [System.Security.Cryptography.HashAlgorithmName]::new($SignatureHashes[$oid]) + if (-not $hashes.ContainsKey($oid)) { return $false } + $hash = [System.Security.Cryptography.HashAlgorithmName]::new($hashes[$oid]) # Certificate ::= SEQUENCE { tbsCertificate, signatureAlgorithm, signatureValue BIT STRING }. $raw = $Certificate.RawData $outer = Read-DerHeader -Bytes $raw @@ -370,12 +332,12 @@ function Test-SignedBy { [Array]::Copy($raw, $bits.Start + 1, $signature, 0, $signature.Length) try { if ($oid.StartsWith('1.2.840.113549.')) { - $rsa = [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPublicKey($Signer) + $rsa = [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPublicKey($Certificate) if (-not $rsa) { return $false } return [bool]$rsa.VerifyData($signed, $signature, $hash, [System.Security.Cryptography.RSASignaturePadding]::Pkcs1) } - $ecdsa = [System.Security.Cryptography.X509Certificates.ECDsaCertificateExtensions]::GetECDsaPublicKey($Signer) + $ecdsa = [System.Security.Cryptography.X509Certificates.ECDsaCertificateExtensions]::GetECDsaPublicKey($Certificate) if (-not $ecdsa) { return $false } # X.509 carries an ECDSA signature as a DER SEQUENCE of r and s; .NET Framework's VerifyData # reads only r and s side by side, each padded to the key's width. @@ -400,48 +362,6 @@ function Test-SignedBy { } } -# Whether the certificate is self-signed: it is its own signer by Test-SignedBy, so its subject and -# issuer are the same name AND its signature verifies under its own public key. A self-issued -# certificate another key signed, such as a CA re-key link certificate, is not. -function Test-SelfSigned { - param([Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate) - return (Test-SignedBy -Certificate $Certificate -Signer $Certificate) -} - -# Whether the certificate's subject and issuer are the same name (Test-SameName). Nothing is verified. -function Test-SelfIssued { - param([Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate) - return (Test-SameName $Certificate.SubjectName $Certificate.IssuerName) -} - -# Why the certificate does not chain to a self-signed root in the file, or $null when it does. The -# walk follows issuer links through the file only, never a machine store: each step needs a -# certificate in $Bundle whose subject is the issuer's name and whose key verifies the signature -# (Test-SignedBy). Every such issuer is followed, not only the first, so a cross-certificate that -# shares a root's name and key cannot hide the root behind it; each certificate is visited once, so -# a loop of links ends. Nothing else about the chain is checked here; Get-CaRefusal checks each -# certificate on its own, and the help says what neither checks. -function Get-ChainRefusal { - param( - [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate, - [Parameter(Mandatory = $true)][AllowEmptyCollection()][System.Security.Cryptography.X509Certificates.X509Certificate2Collection]$Bundle) - $visited = @{ $Certificate.Thumbprint = $true } - $pending = [System.Collections.Generic.Queue[object]]::new() - $pending.Enqueue($Certificate) - while ($pending.Count -gt 0) { - $current = $pending.Dequeue() - if (Test-SelfSigned -Certificate $current) { return $null } - foreach ($candidate in $Bundle) { - if (-not $visited.ContainsKey($candidate.Thumbprint) -and - (Test-SignedBy -Certificate $current -Signer $candidate)) { - $visited[$candidate.Thumbprint] = $true - $pending.Enqueue($candidate) - } - } - } - return 'it does not chain to a self-signed root in the file' -} - # The store a checked CA belongs in: Root for a self-signed one, CertificateAuthority (the # intermediate store) for any other. The help's "Where each CA goes" says what each store means. function Get-CaStoreName { @@ -455,9 +375,7 @@ function Get-CaStoreName { # extension counts as not a CA here. RFC 5280 (the Basic Constraints section) requires the extension # of a version 3 CA certificate; a version 1 root, which cannot carry it, is refused too, by this # script's choice rather than the RFC's. A key usage extension that leaves out certificate signing -# also counts as not a CA (RFC 5280, the Key Usage section). A signature this script cannot verify -# is refused by name, so a root signed that way is not reported as missing. Validity is not checked -# here: the caller skips an expired certificate and warns of one not valid yet. +# also counts as not a CA (RFC 5280, the Key Usage section). function Get-CaRefusal { param([Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate) # Read-CaBundle already refuses every container that can carry a key; this holds if it ever stops. @@ -475,60 +393,9 @@ function Get-CaRefusal { return 'its key usage does not allow it to sign certificates' } } - $algorithm = $Certificate.SignatureAlgorithm - if (-not $SignatureHashes.ContainsKey($algorithm.Value)) { - # .NET has no friendly name for an OID it does not know, so the OID alone names it then. - $named = if ($algorithm.FriendlyName) { "$($algorithm.FriendlyName) ($($algorithm.Value))" } else { $algorithm.Value } - return "it is signed with $named, which this script does not verify" - } - return $null -} - -# Which of the machine's own registry stores already holds the certificate where it counts, as a key -# of $stores, or $null. A root counts only in Root, since the CA store anchors nothing for SChannel; -# any other CA counts in CA or in Root, where it is trusted at least as far. The logical store -# X509Store reads also shows at least Group Policy, enterprise and AuthRoot copies; those are not -# read here, so a CA -# held only there is added, and the import does not rest on that policy. $RegistryRoot is a -# parameter only so a test can point it at a directory. -function Get-PresentStoreName { - param( - [Parameter(Mandatory = $true)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate, - [Parameter(Mandatory = $true)][string]$StoreName, - [string]$RegistryRoot = 'HKLM:\SOFTWARE\Microsoft\SystemCertificates') - $where = if ($StoreName -eq 'Root') { @('Root') } else { @('CertificateAuthority', 'Root') } - $registryName = @{ Root = 'ROOT'; CertificateAuthority = 'CA' } - foreach ($name in $where) { - # Path.Combine, not Join-Path, which throws for a drive this host lacks (HKLM: off Windows). - $own = [System.IO.Path]::Combine($RegistryRoot, $registryName[$name], 'Certificates', $Certificate.Thumbprint) - if (Test-Path -LiteralPath $own) { return $name } - } return $null } -# The certificates an approved CA may chain through: every approved one, and every declined one the -# machine's own store already holds where it counts (Get-PresentStoreName), so declining a root the -# machine already trusts does not leave the CAs under it without their root. -function Get-AnchorSet { - param( - [Parameter(Mandatory = $true)][AllowEmptyCollection()][object[]]$Entries, - [Parameter(Mandatory = $true)][AllowEmptyCollection()][object[]]$Approved, - [string]$RegistryRoot = 'HKLM:\SOFTWARE\Microsoft\SystemCertificates') - $set = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection - $taken = @{} - foreach ($entry in $Approved) { - $taken[$entry.Certificate.Thumbprint] = $true - [void]$set.Add($entry.Certificate) - } - foreach ($entry in $Entries) { - if (-not $taken.ContainsKey($entry.Certificate.Thumbprint) -and - (Get-PresentStoreName -Certificate $entry.Certificate -StoreName $entry.StoreName -RegistryRoot $RegistryRoot)) { - [void]$set.Add($entry.Certificate) - } - } - return , $set -} - # --- read and check -------------------------------------------------------------- if (-not (Test-Path -LiteralPath $CaPath -PathType Leaf)) { @@ -546,94 +413,42 @@ try { throw "Could not read '$CaPath' as X.509 CA certificates: $($_.Exception.Message)" } -$nowUtc = [datetime]::UtcNow -# Each refused certificate as one line: its thumbprint, its Subject escaped for the console, and why. -# The host listing and the terminating error both print these same lines. -$refusals = [System.Collections.Generic.List[string]]::new() -# Each certificate is checked before it is shown, so a refused one is listed with its reason and -# never with a store it would not go to. The store shown is the one it is offered for and written to. -# An expired certificate is skipped before any check, never installed, so nothing else about it can -# refuse the file; only an unexpired CA that reaches its root through it is refused. An unexpired -# certificate that passes on its own must also reach a self-signed root through this file's own -# unexpired certificates, so an issuing CA whose root is not in the file is refused. Any self-signed -# CA in the file is its own anchor and passes, so an unrelated root in the file is installed in Root -# too. -$live = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection -foreach ($cert in $bundle) { - if ($cert.NotAfter.ToUniversalTime() -ge $nowUtc) { [void]$live.Add($cert) } -} Write-Host "The file holds $($bundle.Count) certificate(s):" -$unverifiedRoot = $false -$expiredRoot = $false +$now = [datetime]::Now +$refusals = [System.Collections.Generic.List[object]]::new() +# The store each certificate is shown with is the one it is offered for and written to. $entries = @(foreach ($cert in $bundle) { - $storeName = Get-CaStoreName -Certificate $cert - $target = $stores[$storeName] - $expired = $cert.NotAfter.ToUniversalTime() -lt $nowUtc - $why = $null - if (-not $expired) { - $why = Get-CaRefusal -Certificate $cert - if (-not $why) { - $why = Get-ChainRefusal -Certificate $cert -Bundle $live - if ($why -and -not (Get-ChainRefusal -Certificate $cert -Bundle $bundle)) { - $why = 'it chains to a self-signed root in the file only through an expired certificate' - } - } - } + $target = $stores[(Get-CaStoreName -Certificate $cert)] Write-Host "" Write-Host " Subject : $(ConvertTo-PrintableText $cert.Subject)" Write-Host " Issuer : $(ConvertTo-PrintableText $cert.Issuer)" Write-Host " Thumbprint : $($cert.Thumbprint)" - Write-Host " Not before : $($cert.NotBefore.ToUniversalTime().ToString('u'))" - Write-Host " Not after : $($cert.NotAfter.ToUniversalTime().ToString('u'))" - if ($cert.NotBefore.ToUniversalTime() -gt $nowUtc) { - Write-Warning "$($cert.Thumbprint) is not valid yet; nothing that chains through it validates before its start." - } - # A self-issued certificate refused for a signature this script cannot verify may be the file's - # root, so the error must not say the file holds none. - if (-not $expired -and (Test-SelfIssued -Certificate $cert) -and - -not $SignatureHashes.ContainsKey($cert.SignatureAlgorithm.Value)) { - $unverifiedRoot = $true - } - if ($why) { - Write-Host " Refused : $why" - $refusals.Add("$($cert.Thumbprint) ($(ConvertTo-PrintableText $cert.Subject)): $why") - } elseif ($expired) { - Write-Host " Skipped : it has expired, so it is not installed" - if ($target.Equals($stores['Root'])) { $expiredRoot = $true } - continue - } else { - Write-Host " Store : $($target.Label)" + Write-Host " Not after : $($cert.NotAfter.ToString('u'))" + Write-Host " Store : $($target.Label)" + if ($cert.NotAfter -lt $now -or $cert.NotBefore -gt $now) { + Write-Warning "$($cert.Thumbprint) is outside its validity period; a chain through it will not validate." } - @{ Certificate = $cert; Target = $target; StoreName = $storeName } + $why = Get-CaRefusal -Certificate $cert + if ($why) { $refusals.Add([pscustomobject]@{ Thumbprint = $cert.Thumbprint; Why = $why }) } + @{ Certificate = $cert; Target = $target } }) -# Without its root an issuing CA validates nothing over SChannel, and over the engine's Python TLS -# it would be trusted with no root's revocation or constraints over it (the help's "Where each CA -# goes"), so a rootless file is refused rather than imported. Every CA in such a file is also refused -# for reaching no root, so the error leads with the one cause that explains them. -$rootless = -not $unverifiedRoot -and -not ($entries | Where-Object { $_.Target -eq $stores['Root'] }) -if ($rootless -or $refusals.Count -gt 0) { - # Printed one per line for the operator: pwsh 7's default error view rewraps a message at the - # console width, which can split a line. The error carries the same lines, joined into its one - # message, for a caller that catches it or keeps only the error stream; past the first few it - # gives the count instead. - $named = '' - if ($refusals.Count -gt 0) { - Write-Host "" - Write-Host "Refused:" - foreach ($line in $refusals) { Write-Host " $line" } - $shown = 5 - $named = " Refused: " + (($refusals | Select-Object -First $shown) -join '; ') - if ($refusals.Count -gt $shown) { $named += "; and $($refusals.Count - $shown) more, listed above" } - $named += "." - } - if ($rootless) { - $which = if ($expiredRoot) { "no self-signed root that has not expired" } else { "no self-signed root" } - throw ("Nothing was imported. The file holds $which. Give this script the root " + - "the CA chains to, alone or in the same .p7b chain.$named") - } +if ($refusals.Count -gt 0) { + # Printed one per line for the operator. pwsh 7's default error view rewraps a message at the + # console width, which can split a thumbprint from its reason. The error repeats each pair on + # one line, for a caller that catches it or keeps only the error stream. + Write-Host "" + Write-Host "Refused:" + foreach ($refusal in $refusals) { Write-Host " $($refusal.Thumbprint): $($refusal.Why)" } throw ("Nothing was imported. Every certificate in the file must be a CA that may sign " + - "certificates, carries no private key, and is signed in a way this script verifies, and " + - "every unexpired one must chain to an unexpired self-signed root in the file.$named") + "certificates and carries no private key. Refused: " + + (($refusals | ForEach-Object { "$($_.Thumbprint) ($($_.Why))" }) -join '; ') + ".") +} +if (-not ($entries | Where-Object { $_.Target -eq $stores['Root'] })) { + # Without its root an issuing CA validates nothing over SChannel, and over the engine's Python TLS + # it would be trusted with no root's revocation or constraints over it (the help's "Where each + # CA goes"), so a rootless file is refused rather than imported. + throw ("Nothing was imported. The file holds no self-signed root. Give this script the root " + + "the CA chains to, alone or in the same .p7b chain.") } # --- import ------------------------------------------------------------------ @@ -644,77 +459,35 @@ $approved = @(foreach ($entry in $entries) { $entry } }) -# Under -Confirm the operator may decline a root or an intermediate CA. An approved CA must still -# reach a root through the approved certificates and the declined ones the machine already holds -# (Get-AnchorSet), or it would land in LocalMachine\CA with no root over it. -$anchorSet = Get-AnchorSet -Entries $entries -Approved $approved -$orphans = @(foreach ($entry in $approved) { - if (Get-ChainRefusal -Certificate $entry.Certificate -Bundle $anchorSet) { - "$($entry.Certificate.Thumbprint) ($(ConvertTo-PrintableText $entry.Certificate.Subject))" - } -}) -if ($orphans.Count -gt 0) { - throw ("Nothing was imported. A root or intermediate CA was declined that these approved CAs " + - "chain through, and the machine's own store does not hold it, so a CA is not imported " + - "without the path to its root: $($orphans -join '; ').") -} -if ($approved.Count -eq 0 -and -not $WhatIfPreference) { - Write-Host "" - Write-Host "Nothing was imported: every certificate was declined." -} if ($approved.Count -gt 0) { - # A CA the machine's own store already holds (Get-PresentStoreName) is named as such and left - # alone, since a later rotation must not remove it as the file's. Roots go first, so a failure - # part way never leaves an approved issuing CA added and an approved root not. Nothing is - # rolled back; the error says how many were added. + # Each store keys on thumbprint, so re-importing the same CA is a no-op (idempotent). $added = 0 - $present = 0 try { - foreach ($name in 'Root', 'CertificateAuthority') { - $batch = @($approved | Where-Object { $_.Target -eq $stores[$name] }) - if ($batch.Count -eq 0) { continue } - $store = $stores[$name].Store - $store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite) + foreach ($entry in $approved) { + $entry.Target.Store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite) try { - foreach ($entry in $batch) { - $found = Get-PresentStoreName -Certificate $entry.Certificate -StoreName $name - if ($found) { - Write-Host "Already in $($stores[$found].Label), left as it was: $($entry.Certificate.Thumbprint)" - $present++ - continue - } - $store.Add($entry.Certificate) - $added++ - } + $entry.Target.Store.Add($entry.Certificate) } finally { - $store.Close() + $entry.Target.Store.Close() } + $added++ } } catch { - throw ("Added $added of $($approved.Count) certificate(s), roots first, with $present " + - "already present, before this failed: $($_.Exception.Message)") + throw ("Added $added of $($approved.Count) certificate(s), in the order shown, before this " + + "failed: $($_.Exception.Message)") } Write-Host "" - if ($added -eq 0) { - Write-Host "Nothing was added: all $present approved certificate(s) were already in the machine's own store." - } else { - Write-Host "Added $added CA certificate(s); $present were already in the machine's own store." - } - $notYet = @($approved | Where-Object { $_.Certificate.NotBefore.ToUniversalTime() -gt $nowUtc }) - if ($notYet.Count -gt 0) { - # Staged ahead of its start: say when it will validate, not that it does. - $from = ($notYet | ForEach-Object { $_.Certificate.NotBefore.ToUniversalTime() } | - Sort-Object -Descending | Select-Object -First 1).ToString('u') - Write-Warning ("$($notYet.Count) approved certificate(s) are not valid yet. A chain through " + - "them does not validate before $from; do not roll the DB server cert onto it before then.") - } elseif ($added -gt 0) { - # Only after an add: every approved CA reached a root above, approved or already in Root. - Write-Host "A DB server cert chaining to these CAs now validates with" -ForegroundColor Green + Write-Host "Imported $added CA certificate(s)." + if ($approved | Where-Object { $_.Target -eq $stores['Root'] }) { + Write-Host "A DB server cert chaining to the imported root now validates with" -ForegroundColor Green Write-Host "trust_server_certificate = false (no TrustServerCertificate=true needed)." -ForegroundColor Green + } else { + Write-Warning ("No root was imported. Over SChannel and ODBC a DB server cert validates only if " + + "its root is already in $StoreLocation; the engine's Python TLS treats LocalMachine\CA " + + "differently (see this script's help, 'Where each CA goes').") } Write-Host "" Write-Host "Rotation: when the CA is replaced, import the NEW CA first (this leaves both trusted)," - Write-Host "roll the server cert, then remove only the old certificates that neither the new chain" - Write-Host "nor anything else still needs -- see docs/DEPLOY-SERVER-DB.md section 5.3." + Write-Host "roll the server cert, then remove the OLD CA -- see docs/DEPLOY-SERVER-DB.md." } diff --git a/scripts/service/measure-store-access.ps1 b/scripts/service/measure-store-access.ps1 index 0e4a2d48ec..0e3460cd40 100644 --- a/scripts/service/measure-store-access.ps1 +++ b/scripts/service/measure-store-access.ps1 @@ -126,9 +126,11 @@ $ProvisionHint = "provision-admin --username" # phrases above too, so without this an engine that started and served would pass step 1 of every # arm; Test-GateRefusalLine requires both on one line. $StartRefused = "refusing to start" -# The openings of the gate's two lines that let a start go on: the skipped-gate warning and the -# warn-mode line (app.py). A regex, matched as is. -$GateLetStart = "the engine is starting with the notice gate skipped: |PHI instance with no deliverable security-notice recipient: " +# The openings of the gate's lines that let a start go on, at least these three in app.py: the +# skipped-gate warning, the warn-mode line, and the warning that the gate could not run (a Vault +# transit outage while the gate is not enforcing). The third does not carry $GateRefusal, so this is +# searched for on its own. A regex, matched as is. +$GateLetStart = "the engine is starting with the notice gate skipped: |PHI instance with no deliverable security-notice recipient: |the security-notice gate could not run: " $ProvisionDecline = "already has an enabled Administrator" $OpenErrorPattern = "unable to open database file|Access is denied|PermissionError|OperationalError" # store.py logs these and carries on; each means that open did not restrict the trio as it tried to @@ -649,7 +651,7 @@ function Wait-ForGateRefusal([string]$Phase) { Wait until the service logs the ADR 0167 refusal (Test-GateRefusalLine), which it reaches only after opening the store and reading its users. Samples process owners throughout, since NSSM restarts the refused engine and each restart is a new process. Returns $true on the refusal, - $false on timeout, and $false at once, with a reading quoting it, when one of the gate's two + $false on timeout, and $false at once, with a reading quoting it, when one of the gate's let-it-start lines ($GateLetStart) is logged. Any other line is waited past. #> # Read by callers after a $false, so a failure text never names a lockout the gate ruled out. @@ -659,9 +661,10 @@ function Wait-ForGateRefusal([string]$Phase) { Update-OwnerSample "during $Phase" $gate = @(Get-LogMatches ([regex]::Escape($GateRefusal))) if (@($gate | Where-Object { Test-GateRefusalLine $_.Line }).Count -gt 0) { return $true } - # Only the gate's own two let-it-start lines end the wait: other engine lines carry the - # clause too, and say nothing about this gate. - $letStart = @($gate | Where-Object { $_.Line -match $GateLetStart }) + # Only the gate's own let-it-start lines end the wait: other engine lines carry the clause + # too, and say nothing about this gate. Searched on their own, since the could-not-run + # warning does not carry the clause. + $letStart = @(Get-LogMatches $GateLetStart) if ($letStart.Count -gt 0) { Add-Reading ("during $Phase the service reached the ADR 0167 gate, which did not refuse " + "the start: $(Get-Excerpt $letStart[0].Line 300)") @@ -673,6 +676,21 @@ function Wait-ForGateRefusal([string]$Phase) { return $false } +function Add-FirstStartFailure { + <# + The failure for a first start on a fresh store that Wait-ForGateRefusal did not see refused. + As in the control and restart branches: a gate that let the start go on rules out a lockout, + so only a start it did not reach gets the store-access attribution. + #> + $what = if ($script:GateLetStartSeen) { "created $DbPath and reached the ADR 0167 gate, which let the start go on (the READING line quotes it) rather than refusing it," } + elseif (Test-Path -LiteralPath $DbPath) { "created $DbPath but did not log the expected refusal line" } + else { "never created $DbPath" } + $cause = if ($script:GateLetStartSeen) { "The store's access is not in question." } + else { Get-Attribution $ServiceIdentity (Get-ServiceSids) "" } + Add-Failure ("$ServiceIdentity $what within $WaitSeconds s on a fresh store. " + $cause) + Show-LogTail +} + function Test-ServiceServes([string]$Phase, [string]$StoreOrigin) { <# The engine is up on this store and a session can be had: /health over https pinned to the @@ -993,15 +1011,7 @@ try { $noAdmin = @(Get-LogMatches ([regex]::Escape($NoAdminRefusal)) | Where-Object { Test-GateRefusalLine $_.Line $NoAdminRefusal }) if (-not $opened) { - $what = if ($script:GateLetStartSeen) { "created $DbPath and reached the ADR 0167 gate, which let the start go on (the READING line quotes it) rather than refusing it," } - elseif (Test-Path -LiteralPath $DbPath) { "created $DbPath but did not log the expected refusal line" } - else { "never created $DbPath" } - # As in the control and restart branches: a gate that let the start go on rules out a - # lockout, so only a start it did not reach gets the store-access attribution. - $cause = if ($script:GateLetStartSeen) { "The store's access is not in question." } - else { Get-Attribution $ServiceIdentity (Get-ServiceSids) "" } - Add-Failure ("$ServiceIdentity $what within $WaitSeconds s on a fresh store. " + $cause) - Show-LogTail + Add-FirstStartFailure } elseif ($noAdmin.Count -eq 0) { $line = @(Get-LogMatches ([regex]::Escape($GateRefusal)) | Where-Object { Test-GateRefusalLine $_.Line } | Select-Object -First 1 | @@ -1051,15 +1061,7 @@ try { Stop-TheService Show-Trio "service first start" if (-not $opened) { - $what = if ($script:GateLetStartSeen) { "created $DbPath and reached the ADR 0167 gate, which let the start go on (the READING line quotes it) rather than refusing it," } - elseif (Test-Path -LiteralPath $DbPath) { "created $DbPath but did not log the expected refusal line" } - else { "never created $DbPath" } - # As in the control and restart branches: a gate that let the start go on rules out a - # lockout, so only a start it did not reach gets the store-access attribution. - $cause = if ($script:GateLetStartSeen) { "The store's access is not in question." } - else { Get-Attribution $ServiceIdentity (Get-ServiceSids) "" } - Add-Failure ("$ServiceIdentity $what within $WaitSeconds s on a fresh store. " + $cause) - Show-LogTail + Add-FirstStartFailure } else { Add-Reading "$ServiceIdentity created and opened $DbPath, then the ADR 0167 gate refused it (expected)" Test-Resecured -Identity $ServiceIdentity -FromServiceLog diff --git a/tests/test_import_db_ca_bundle.py b/tests/test_import_db_ca_bundle.py index 9e3c547d4b..3c753f55ef 100644 --- a/tests/test_import_db_ca_bundle.py +++ b/tests/test_import_db_ca_bundle.py @@ -10,15 +10,13 @@ root goes into the machine Root store and any other CA into the intermediate store. NOTHING HERE WRITES A CERTIFICATE STORE. The fixtures are generated in a pytest temporary -directory. The function-level tests lift at least ``Read-CaBundle``, ``Get-CaRefusal``, -``Get-CaStoreName``, ``Get-ChainRefusal``, ``Get-PresentStoreName`` and ``Get-AnchorSet`` (with the -``$SignatureHashes`` table and the helpers they call) out of the script's AST and call them, as -``tests/test_import_db_ca_console_text.py`` does. The whole-script tests are skipped in an elevated -session. All but one run it with ``-WhatIf``, where ``ShouldProcess`` declines every import, so the -store is never opened, and even a regression that ignored ``-WhatIf`` would meet the script's own -elevation refusal rather than the machine Root store. The one without ``-WhatIf`` exists to meet -that refusal, on Windows only; since hosted Windows runners are elevated, it runs only on an -unelevated developer box. Both halves need ``pwsh`` and skip where there is none. +directory. The function-level tests lift ``Read-CaBundle``, ``Get-CaRefusal`` and +``Get-CaStoreName`` (and the console helpers they call) out of the script's AST and call them, as +``tests/test_import_db_ca_console_text.py`` does. The whole-script tests run it with ``-WhatIf`` +only, where ``ShouldProcess`` declines every import, so the store is never opened; they are also +skipped in an elevated session, so even a regression that ignored ``-WhatIf`` would meet the +script's own elevation refusal rather than the machine Root store. Both halves need ``pwsh`` and +skip where there is none. """ from __future__ import annotations @@ -37,9 +35,8 @@ import pytest from cryptography import x509 from cryptography.hazmat.primitives import hashes, serialization -from cryptography.hazmat.primitives.asymmetric import ec, padding, rsa +from cryptography.hazmat.primitives.asymmetric import ec, rsa from cryptography.hazmat.primitives.serialization import pkcs7, pkcs12 -from cryptography.x509.name import _ASN1Type from cryptography.x509.oid import NameOID _SCRIPT = Path(__file__).resolve().parent.parent / "scripts" / "service" / "import-db-ca.ps1" @@ -69,12 +66,9 @@ def _cert( issuer: tuple[x509.Certificate, _Key] | None = None, *, cert_sign: bool | None = None, - valid_from_days: int = -1, - valid_to_days: int = 30, ) -> tuple[x509.Certificate, _Key]: """A synthetic certificate. ``ca`` None leaves the basic constraints extension out, and - ``cert_sign`` None leaves the key usage extension out. The validity period is given in days - from now.""" + ``cert_sign`` None leaves the key usage extension out.""" key = ec.generate_private_key(ec.SECP256R1()) name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, cn)]) now = datetime.datetime.now(datetime.UTC) @@ -84,8 +78,8 @@ def _cert( .issuer_name(issuer[0].subject if issuer else name) .public_key(key.public_key()) .serial_number(x509.random_serial_number()) - .not_valid_before(now + datetime.timedelta(days=valid_from_days)) - .not_valid_after(now + datetime.timedelta(days=valid_to_days)) + .not_valid_before(now - datetime.timedelta(days=1)) + .not_valid_after(now + datetime.timedelta(days=30)) ) if ca is not None: builder = builder.add_extension(x509.BasicConstraints(ca=ca, path_length=None), True) @@ -119,26 +113,10 @@ def _pem(*certs: x509.Certificate) -> bytes: @pytest.fixture(scope="module") def chain() -> dict[str, tuple[x509.Certificate, _Key]]: root = _cert("Synthetic Test Root CA", True) - other_root = _cert("Synthetic Other Root CA", True) - expired_root = _cert("Synthetic Expired Root CA", True, valid_from_days=-30, valid_to_days=-1) return { "root": root, "intermediate": _cert("Synthetic Test Issuing CA", True, root), "leaf": _cert("db.test.invalid", False, root), - # Issued by a root the file does not hold, and signed under the same issuer NAME as the - # file's root by another key: neither chains to the file's root. - "foreign_intermediate": _cert("Synthetic Foreign Issuing CA", True, other_root), - "name_only_intermediate": _cert( - "Synthetic Name-Only Issuing CA", True, (root[0], other_root[1]) - ), - "expired_root": expired_root, - "future_root": _cert("Synthetic Future Root CA", True, valid_from_days=2), - # Valid itself, but its only root in the file has expired. - "under_expired": _cert("Synthetic Issuing CA Under Expired Root", True, expired_root), - # Expired, and not a CA either: skipped for its expiry before anything refuses it. - "expired_leaf": _cert( - "expired.db.test.invalid", False, root, valid_from_days=-30, valid_to_days=-1 - ), "unconstrained": _cert("Synthetic No Constraints", None), "no_cert_sign": _cert("Synthetic CA Without CertSign", True, cert_sign=False), "cert_sign": _cert("Synthetic CA With CertSign", True, cert_sign=True), @@ -170,79 +148,9 @@ def build(name: x509.Name, public: object, signer: object) -> x509.Certificate: rsa_key = rsa.generate_private_key(public_exponent=65537, key_size=2048) rsa_name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Synthetic RSA Root CA")]) new_key = ec.generate_private_key(ec.SECP384R1()) - pss_name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Synthetic RSA-PSS Root CA")]) - pss_root = ( - x509.CertificateBuilder() - .subject_name(pss_name) - .issuer_name(pss_name) - .public_key(rsa_key.public_key()) - .serial_number(x509.random_serial_number()) - .not_valid_before(now - datetime.timedelta(days=1)) - .not_valid_after(now + datetime.timedelta(days=30)) - .add_extension(x509.BasicConstraints(ca=True, path_length=None), True) - .sign( - rsa_key, - hashes.SHA256(), - rsa_padding=padding.PSS( - mgf=padding.MGF1(hashes.SHA256()), salt_length=padding.PSS.DIGEST_LENGTH - ), - ) - ) - # The root names itself with a PrintableString; the issuing CA names that issuer with a - # UTF8String of the same text. RFC 5280 name matching treats the two as one name. - enc_key = ec.generate_private_key(ec.SECP256R1()) - enc_text = "Synthetic Encoded Root CA" - enc_root = build( - x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, enc_text, _ASN1Type.PrintableString)]), - enc_key.public_key(), - enc_key, - ) - enc_issuing = ( - x509.CertificateBuilder() - .subject_name( - x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Synthetic Encoded Issuing CA")]) - ) - .issuer_name( - x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, enc_text, _ASN1Type.UTF8String)]) - ) - .public_key(ec.generate_private_key(ec.SECP256R1()).public_key()) - .serial_number(x509.random_serial_number()) - .not_valid_before(now - datetime.timedelta(days=1)) - .not_valid_after(now + datetime.timedelta(days=30)) - .add_extension(x509.BasicConstraints(ca=True, path_length=None), True) - .sign(enc_key, hashes.SHA256()) - ) - assert enc_root.subject.public_bytes() != enc_issuing.issuer.public_bytes() - # The same text in another letter case. With no key identifiers, as here, the Windows chain - # engine does not link it either. - case_key = ec.generate_private_key(ec.SECP256R1()) - case_root = build( - x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Synthetic Case Root CA")]), - case_key.public_key(), - case_key, - ) - case_issuing = ( - x509.CertificateBuilder() - .subject_name( - x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Synthetic Case Issuing CA")]) - ) - .issuer_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "SYNTHETIC CASE ROOT CA")])) - .public_key(ec.generate_private_key(ec.SECP256R1()).public_key()) - .serial_number(x509.random_serial_number()) - .not_valid_before(now - datetime.timedelta(days=1)) - .not_valid_after(now + datetime.timedelta(days=30)) - .add_extension(x509.BasicConstraints(ca=True, path_length=None), True) - .sign(case_key, hashes.SHA256()) - ) return { - "case_root": case_root, - "case_issuing": case_issuing, - "enc_root": enc_root, - "enc_issuing": enc_issuing, "rsa_root": build(rsa_name, rsa_key.public_key(), rsa_key), "rekey": build(root.subject, new_key.public_key(), root_key), - # Self-signed, but with a signature the script does not verify. - "pss_root": pss_root, } @@ -306,30 +214,6 @@ def files( "der_concat": root.public_bytes(der) + inter.public_bytes(der), "der_then_pem": root.public_bytes(der) + _pem(inter), "b64_trailing": base64.encodebytes(root.public_bytes(der) + b"\x00\x01"), - # A root beside a CA that does not chain to it: a foreign issuer, or the root's name alone. - "pem_foreign_intermediate": _pem(root, chain["foreign_intermediate"][0]), - "pem_name_only_intermediate": _pem(root, chain["name_only_intermediate"][0]), - # Outside the validity period: an expired root beside a valid one, and a root not valid yet. - "pem_expired_root": _pem(root, chain["expired_root"][0]), - "pem_only_expired_root": _pem(chain["expired_root"][0]), - "pem_under_expired": _pem(root, chain["expired_root"][0], chain["under_expired"][0]), - "pem_encoded_names": _pem(extra["enc_root"], extra["enc_issuing"]), - "pem_case_names": _pem(extra["case_root"], extra["case_issuing"]), - "pem_expired_leaf": _pem(root, chain["expired_leaf"][0]), - "pem_future_root": _pem(chain["future_root"][0]), - "pem_pss_root": _pem(extra["pss_root"]), - # Bare base64 behind a byte-order mark, UTF-8 and UTF-16. - "b64_utf8_bom": b"\xef\xbb\xbf" + base64.encodebytes(root.public_bytes(der)), - "b64_utf16_bom": b"\xff\xfe" - + base64.encodebytes(root.public_bytes(der)).decode("ascii").encode("utf-16-le"), - # A .pfx carried under a CERTIFICATE header: refused by its content, not its label. - "pfx_in_certificate_block": b"-----BEGIN CERTIFICATE-----\n" - + base64.encodebytes( - pkcs12.serialize_key_and_certificates( - b"x", root_key, root, None, serialization.NoEncryption() - ) - ) - + b"-----END CERTIFICATE-----\n", "empty": b"", "base64_junk": b"not a certificate at all\n", "garbage": b"not a certificate!\n", @@ -347,19 +231,14 @@ def _psq(value: str) -> str: def _read_all(tmp_path: Path, paths: dict[str, Path]) -> dict[str, dict[str, object]]: - """Each file through ``Read-CaBundle``, then ``Get-CaRefusal``, ``Get-CaStoreName`` and - ``Get-ChainRefusal``, as the script does.""" + """Each file through ``Read-CaBundle``, then ``Get-CaRefusal`` and ``Get-CaStoreName``, as the + script does.""" script = f""" $ErrorActionPreference = 'Stop' $ast = [System.Management.Automation.Language.Parser]::ParseFile( {_psq(str(_SCRIPT))}, [ref]$null, [ref]$null) -$set = $ast.Find({{ $args[0] -is [System.Management.Automation.Language.AssignmentStatementAst] -and - $args[0].Left.Extent.Text -eq '$SignatureHashes' }}, $false) -if (-not $set) {{ throw 'missing assignment $SignatureHashes' }} -. ([scriptblock]::Create($set.Extent.Text)) foreach ($name in @('Test-EscapedForConsole', 'ConvertTo-PrintableText', 'Read-DerHeader', - 'Read-CaBundle', 'Get-CaRefusal', 'Test-SignedBy', 'Test-SelfSigned', - 'Get-ChainRefusal', 'Get-CaStoreName', 'Test-SameName')) {{ + 'Read-CaBundle', 'Get-CaRefusal', 'Test-SelfSigned', 'Get-CaStoreName')) {{ $def = $ast.Find({{ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $args[0].Name -eq $name }}, $true) if (-not $def) {{ throw "missing function $name" }} @@ -372,8 +251,7 @@ def _read_all(tmp_path: Path, paths: dict[str, Path]) -> dict[str, dict[str, obj $bundle = Read-CaBundle -Bytes ([System.IO.File]::ReadAllBytes($paths[$k])) $out[$k] = @{{ certs = @(foreach ($c in $bundle) {{ @{{ thumb = $c.Thumbprint; refusal = (Get-CaRefusal -Certificate $c); - store = (Get-CaStoreName -Certificate $c); - chain = (Get-ChainRefusal -Certificate $c -Bundle $bundle) }} }}) }} + store = (Get-CaStoreName -Certificate $c) }} }}) }} }} catch {{ $out[$k] = @{{ error = $_.Exception.Message }} }} @@ -454,54 +332,6 @@ def certs(name: str) -> list[tuple[str, str | None]]: ) assert usage[_thumb(chain["cert_sign"][0])] is None - # Bare base64 behind a byte-order mark reads as the root it holds. - assert certs("b64_utf8_bom") == [(root, None)] - assert certs("b64_utf16_bom") == [(root, None)] - - # Validity is the caller's to judge, not Get-CaRefusal's. - expired = dict(certs("pem_expired_root")) - assert expired[_thumb(chain["expired_root"][0])] is None - assert expired[root] is None - # Not valid yet is not refused: a new CA may be staged ahead of its start. - assert certs("pem_future_root") == [(_thumb(chain["future_root"][0]), None)] - # A signature the script cannot verify is refused by name, not read as a missing root. - pss = certs("pem_pss_root") - assert len(pss) == 1 and pss[0][0] == _thumb(extra["pss_root"]), pss - assert str(pss[0][1]).startswith("it is signed with ") and "1.2.840.113549.1.1.10" in str( - pss[0][1] - ), pss - - # Each CA must chain to a self-signed root through the file's own certificates: an issuing CA - # and a re-key certificate that do pass, and one whose issuer is not in the file, or only - # carries the root's name under another key, does not. - def chained(name: str) -> dict[str, object]: - entry = got[name] - assert "error" not in entry, f"{name}: {entry}" - listed = entry["certs"] - assert isinstance(listed, list) - return {c["thumb"]: c["chain"] for c in listed} - - assert chained("p7b_der") == {root: None, inter: None} - assert chained("root_and_rekey") == {root: None, rekey: None} - unchained = "it does not chain to a self-signed root in the file" - # Names that differ only in string type or letter case do not link a CA to its root, as the - # Windows chain engine does not link them between certificates with no key identifiers. - for name, root_key, issuing_key in ( - ("pem_encoded_names", "enc_root", "enc_issuing"), - ("pem_case_names", "case_root", "case_issuing"), - ): - assert chained(name) == { - _thumb(extra[root_key]): None, - _thumb(extra[issuing_key]): unchained, - }, name - for name, key in ( - ("pem_foreign_intermediate", "foreign_intermediate"), - ("pem_name_only_intermediate", "name_only_intermediate"), - ): - assert chained(name) == {root: None, _thumb(chain[key][0]): unchained}, name - # Each is a CA in its own right, so only the chain check catches it. - assert dict(certs(name))[_thumb(chain[key][0])] is None, name - # Refused before anything is parsed: a private key, in either container, and what is not a # certificate file at all. def error(name: str) -> str: @@ -510,7 +340,6 @@ def error(name: str) -> str: return str(entry["error"]) assert "'Pfx' content" in error("pfx") - assert "'Pfx' content" in error("pfx_in_certificate_block") assert "labelled 'PRIVATE KEY'" in error("pem_with_key") assert "labelled 'EC PRIVATE KEY'" in error("pem_zero_first_with_key") assert "not well formed" in error("pem_unterminated") @@ -524,30 +353,20 @@ def error(name: str) -> str: assert "control character outside its PEM blocks" in error("der_then_pem") -_ERROR_MARK = "TERMINATING-ERROR-MESSAGE: " - - -def _run_script(ca_path: Path, *, whatif: bool = True) -> subprocess.CompletedProcess[str]: - """Run the script unelevated. Its terminating error's own message, not pwsh's rendered error - view, is printed to stdout on one line after ``_ERROR_MARK``: the view rewraps at the console - width and quotes the throwing source line, so text read there can come from the code.""" +def _run_whatif(ca_path: Path) -> subprocess.CompletedProcess[str]: if _elevated(): pytest.skip("SKIP (nothing run): elevated session; the script is run only unelevated") - wrapper = Path(ca_path).parent / f"run-{uuid.uuid4().hex}.ps1" - wrapper.write_text( - f""" -$ErrorActionPreference = 'Stop' -try {{ - & {_psq(str(_SCRIPT))} -CaPath {_psq(str(ca_path))}{" -WhatIf" if whatif else ""} -}} catch {{ - [Console]::Out.WriteLine({_psq(_ERROR_MARK)} + ($_.Exception.Message -replace '\\s+', ' ')) - exit 1 -}} -""", - encoding="utf-8", - ) return subprocess.run( - [_pwsh(), "-NoProfile", "-NonInteractive", "-File", str(wrapper)], + [ + _pwsh(), + "-NoProfile", + "-NonInteractive", + "-File", + str(_SCRIPT), + "-CaPath", + str(ca_path), + "-WhatIf", + ], capture_output=True, text=True, encoding="utf-8", @@ -555,18 +374,10 @@ def _run_script(ca_path: Path, *, whatif: bool = True) -> subprocess.CompletedPr ) -def _error_message(proc: subprocess.CompletedProcess[str]) -> str: - found = [ - ln[len(_ERROR_MARK) :] for ln in proc.stdout.splitlines() if ln.startswith(_ERROR_MARK) - ] - assert len(found) == 1, proc.stdout + proc.stderr - return found[0] - - def test_whatif_shows_and_offers_every_certificate_of_an_unsigned_p7b( chain: dict[str, tuple[x509.Certificate, _Key]], files: dict[str, Path] ) -> None: - proc = _run_script(files["p7b_der"]) + proc = _run_whatif(files["p7b_der"]) out = proc.stdout + proc.stderr assert proc.returncode == 0, out assert "The file holds 2 certificate(s)" in out @@ -588,22 +399,15 @@ def test_a_file_without_a_self_signed_root_imports_nothing( name: str, files: dict[str, Path] ) -> None: # An issuing CA alone, or a self-issued certificate another key signed: no root, so refused. - proc = _run_script(files[name]) + proc = _run_whatif(files[name]) out = proc.stdout + proc.stderr assert proc.returncode != 0, out - assert _error_message(proc).startswith( - "Nothing was imported. The file holds no self-signed root." - ), out + assert "Nothing was imported. The file holds no self-signed root." in out assert "What if" not in out _NOT_A_CA = "its basic constraints say it is not a CA" _NO_CERT_SIGN = "its key usage does not allow it to sign certificates" -_UNCHAINED = "it does not chain to a self-signed root in the file" - - -def _pair(cert: x509.Certificate, why: str) -> str: - return f"{_thumb(cert)} ({cert.subject.rfc4514_string()}): {why}" @pytest.mark.parametrize( @@ -611,31 +415,20 @@ def _pair(cert: x509.Certificate, why: str) -> str: [ ("p7b_with_leaf", [("leaf", _NOT_A_CA)]), ("pem_two_refused", [("leaf", _NOT_A_CA), ("no_cert_sign", _NO_CERT_SIGN)]), - ( - "pem_under_expired", - [ - ( - "under_expired", - "it chains to a self-signed root in the file only through an expired" - " certificate", - ) - ], - ), - ("pem_foreign_intermediate", [("foreign_intermediate", _UNCHAINED)]), - ("pem_name_only_intermediate", [("name_only_intermediate", _UNCHAINED)]), ], ) -def test_a_bundle_holding_a_refused_certificate_imports_nothing( +def test_a_bundle_holding_a_non_ca_imports_nothing( name: str, refused: list[tuple[str, str]], chain: dict[str, tuple[x509.Certificate, _Key]], files: dict[str, Path], ) -> None: - proc = _run_script(files[name]) + proc = _run_whatif(files[name]) out = proc.stdout + proc.stderr assert proc.returncode != 0, out - pairs = [_pair(chain[key][0], why) for key, why in refused] - # The printed list holds each refusal as a whole line, in file order, and nothing else: pwsh's + assert "Nothing was imported" in out + thumbs = [_thumb(chain[key][0]) for key, _why in refused] + # The printed list holds each reason as a whole line, in file order, and nothing else: pwsh's # error view rewraps at the console width, and at a Linux runner's 80 columns that split a # thumbprint from its reason. lines = proc.stdout.splitlines() @@ -643,202 +436,18 @@ def test_a_bundle_holding_a_refused_certificate_imports_nothing( listed = itertools.takewhile( lambda ln: ln.startswith(" "), lines[lines.index("Refused:") + 1 :] ) - assert list(listed) == [f" {p}" for p in pairs], out - # The error's own message carries every pair, worded as the list words it, in order, and not - # the root CA beside them. - message = _error_message(proc) - assert message.startswith("Nothing was imported."), message - at = [message.find(p) for p in pairs] - assert -1 not in at and at == sorted(at), message - assert _thumb(chain["root"][0]) not in message, message + assert list(listed) == [ + f" {t}: {why}" for t, (_key, why) in zip(thumbs, refused, strict=True) + ], out + # The error names every refused thumbprint, in order, and not the root CA beside them. The + # error view's source-line excerpt shows code, not a thumbprint, so this reads the message. + at = [proc.stderr.find(t) for t in thumbs] + assert -1 not in at and at == sorted(at), proc.stderr + assert _thumb(chain["root"][0]) not in proc.stderr, proc.stderr # Refused before a single import is offered, even the CA beside it. assert "What if" not in out -def test_an_expired_certificate_off_every_import_path_is_skipped( - chain: dict[str, tuple[x509.Certificate, _Key]], files: dict[str, Path] -) -> None: - proc = _run_script(files["pem_expired_root"]) - out = proc.stdout + proc.stderr - assert proc.returncode == 0, out - lines = proc.stdout.splitlines() - at = lines.index(f" Thumbprint : {_thumb(chain['expired_root'][0])}") - block = list(itertools.takewhile(lambda ln: ln.startswith(" "), lines[at:])) - assert " Skipped : it has expired, so it is not installed" in block, block - offered = [ln for ln in out.splitlines() if "What if" in ln] - assert len(offered) == 1 and _thumb(chain["root"][0]) in offered[0], offered - - -def test_a_file_whose_only_root_has_expired_imports_nothing(files: dict[str, Path]) -> None: - proc = _run_script(files["pem_only_expired_root"]) - assert proc.returncode != 0, proc.stdout + proc.stderr - assert _error_message(proc).startswith( - "Nothing was imported. The file holds no self-signed root that has not expired." - ), proc.stdout - - -@pytest.mark.parametrize( - ("name", "issuing"), - [("pem_encoded_names", "enc_issuing"), ("pem_case_names", "case_issuing")], -) -def test_a_chain_whose_names_differ_only_in_encoding_or_case_is_refused( - name: str, issuing: str, extra: dict[str, x509.Certificate], files: dict[str, Path] -) -> None: - # These certificates carry no key identifiers, and without them the Windows chain engine links - # an issuer only by the exact bytes of its name, so SChannel could not build this chain: the - # file is refused, not imported. - proc = _run_script(files[name]) - out = proc.stdout + proc.stderr - assert proc.returncode != 0, out - message = _error_message(proc) - assert message.startswith("Nothing was imported."), message - assert _pair(extra[issuing], _UNCHAINED) in message, message - assert "What if" not in out - - -def test_an_expired_certificate_is_skipped_before_anything_refuses_it( - chain: dict[str, tuple[x509.Certificate, _Key]], files: dict[str, Path] -) -> None: - # Expired and not a CA: listed and skipped, so the root beside it is still offered. - proc = _run_script(files["pem_expired_leaf"]) - out = proc.stdout + proc.stderr - assert proc.returncode == 0, out - lines = proc.stdout.splitlines() - at = lines.index(f" Thumbprint : {_thumb(chain['expired_leaf'][0])}") - block = list(itertools.takewhile(lambda ln: ln.startswith(" "), lines[at:])) - assert " Skipped : it has expired, so it is not installed" in block, block - assert not any(ln.startswith(" Refused") for ln in block), block - offered = [ln for ln in out.splitlines() if "What if" in ln] - assert len(offered) == 1 and _thumb(chain["root"][0]) in offered[0], offered - - -def test_a_ca_already_held_counts_where_it_anchors_and_a_declined_held_root_anchors( - tmp_path: Path, chain: dict[str, tuple[x509.Certificate, _Key]], files: dict[str, Path] -) -> None: - """``Get-PresentStoreName`` and ``Get-AnchorSet`` lifted out of the script, with a directory - standing in for the machine's registry stores, so nothing reads or writes the real ones.""" - root = _thumb(chain["root"][0]) - inter = _thumb(chain["intermediate"][0]) - # Each case: the registry store names holding the root, then those holding the issuing CA. - cases = { - "none": ([], []), - "root_in_root": (["ROOT"], []), - "root_in_ca": (["CA"], []), - "inter_in_ca": ([], ["CA"]), - "inter_in_root": ([], ["ROOT"]), - } - for case, (root_in, inter_in) in cases.items(): - for store, thumb in [(s, root) for s in root_in] + [(s, inter) for s in inter_in]: - held = tmp_path / case / store / "Certificates" - held.mkdir(parents=True, exist_ok=True) - (held / thumb).write_bytes(b"") - script = f""" -$ErrorActionPreference = 'Stop' -$ast = [System.Management.Automation.Language.Parser]::ParseFile( - {_psq(str(_SCRIPT))}, [ref]$null, [ref]$null) -$set = $ast.Find({{ $args[0] -is [System.Management.Automation.Language.AssignmentStatementAst] -and - $args[0].Left.Extent.Text -eq '$SignatureHashes' }}, $false) -. ([scriptblock]::Create($set.Extent.Text)) -foreach ($name in @('Test-EscapedForConsole', 'ConvertTo-PrintableText', 'Read-DerHeader', - 'Read-CaBundle', 'Test-SignedBy', 'Test-SelfSigned', 'Test-SameName', - 'Get-ChainRefusal', 'Get-CaStoreName', 'Get-PresentStoreName', - 'Get-AnchorSet')) {{ - $def = $ast.Find({{ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] -and - $args[0].Name -eq $name }}, $true) - if (-not $def) {{ throw "missing function $name" }} - . ([scriptblock]::Create($def.Extent.Text)) -}} -$bundle = Read-CaBundle -Bytes ([System.IO.File]::ReadAllBytes({_psq(str(files["p7b_der"]))})) -$entries = @(foreach ($c in $bundle) {{ @{{ Certificate = $c; StoreName = (Get-CaStoreName -Certificate $c) }} }}) -$out = @{{}} -foreach ($case in {_psq(",".join(cases))}.Split(',')) {{ - $dir = Join-Path {_psq(str(tmp_path))} $case - $present = @{{}} - foreach ($e in $entries) {{ - $present[$e.Certificate.Thumbprint] = Get-PresentStoreName -Certificate $e.Certificate -StoreName $e.StoreName -RegistryRoot $dir - }} - # The root declined, the issuing CA approved. - $approved = @($entries | Where-Object {{ $_.StoreName -ne 'Root' }}) - $anchors = Get-AnchorSet -Entries $entries -Approved $approved -RegistryRoot $dir - $out[$case] = @{{ present = $present; orphan = (Get-ChainRefusal -Certificate $approved[0].Certificate -Bundle $anchors) }} -}} -$out | ConvertTo-Json -Depth 5 -Compress -""" - f = tmp_path / f"dbca-{uuid.uuid4().hex}.ps1" - f.write_text(script, encoding="utf-8") - proc = subprocess.run( - [_pwsh(), "-NoProfile", "-NonInteractive", "-File", str(f)], - capture_output=True, - text=True, - encoding="utf-8", - timeout=120, - ) - assert proc.returncode == 0, f"harness failed: {proc.stdout}\n{proc.stderr}" - got = json.loads(proc.stdout.strip().splitlines()[-1]) - # A root counts only in Root; an issuing CA counts in CA or in Root. - assert got["none"]["present"] == {root: None, inter: None}, got - assert got["root_in_root"]["present"] == {root: "Root", inter: None}, got - assert got["root_in_ca"]["present"] == {root: None, inter: None}, got - assert got["inter_in_ca"]["present"] == {root: None, inter: "CertificateAuthority"}, got - assert got["inter_in_root"]["present"] == {root: None, inter: "Root"}, got - # Declining a root the machine's Root store already holds leaves the issuing CA anchored; a root - # held nowhere, or only in the CA store, does not. - assert got["root_in_root"]["orphan"] is None, got - for case in ("none", "root_in_ca", "inter_in_ca", "inter_in_root"): - assert got[case]["orphan"] == _UNCHAINED, (case, got) - - -def test_a_root_signed_in_a_way_the_script_cannot_verify_is_not_reported_missing( - extra: dict[str, x509.Certificate], files: dict[str, Path] -) -> None: - proc = _run_script(files["pem_pss_root"]) - assert proc.returncode != 0, proc.stdout + proc.stderr - message = _error_message(proc) - assert "holds no self-signed root" not in message, message - assert f"{_thumb(extra['pss_root'])} (" in message and "it is signed with " in message, message - - -def test_the_error_names_the_first_five_refusals_and_counts_the_rest( - tmp_path: Path, chain: dict[str, tuple[x509.Certificate, _Key]] -) -> None: - leaves = [_cert(f"db{i}.test.invalid", False, chain["root"])[0] for i in range(7)] - path = tmp_path / "seven_leaves.pem" - path.write_bytes(_pem(chain["root"][0], *leaves)) - proc = _run_script(path) - assert proc.returncode != 0, proc.stdout + proc.stderr - message = _error_message(proc) - for leaf in leaves[:5]: - assert _pair(leaf, _NOT_A_CA) in message, message - for leaf in leaves[5:]: - assert _thumb(leaf) not in message, message - # Still listed for the operator. - assert f" {_pair(leaf, _NOT_A_CA)}" in proc.stdout.splitlines(), proc.stdout - assert message.endswith("; and 2 more, listed above."), message - - -def test_a_listed_refused_certificate_names_no_store( - chain: dict[str, tuple[x509.Certificate, _Key]], files: dict[str, Path] -) -> None: - proc = _run_script(files["p7b_with_leaf"]) - lines = proc.stdout.splitlines() - at = lines.index(f" Thumbprint : {_thumb(chain['leaf'][0])}") - block = list(itertools.takewhile(lambda ln: ln.startswith(" "), lines[at:])) - assert f" Refused : {_NOT_A_CA}" in block, block - assert not any(ln.startswith(" Store") for ln in block), block - # The validity period is printed in UTC, so its 'Z' suffix is true. - not_after = chain["leaf"][0].not_valid_after_utc.strftime("%Y-%m-%d %H:%M:%SZ") - assert f" Not after : {not_after}" in block, block - - -def test_an_unelevated_import_is_refused_before_the_file_is_read(files: dict[str, Path]) -> None: - if sys.platform != "win32": - pytest.skip("SKIP (nothing run): the elevation check reads a Windows token") - proc = _run_script(files["der"], whatif=False) - assert proc.returncode != 0, proc.stdout + proc.stderr - assert "requires an elevated (Administrator) PowerShell" in _error_message(proc) - assert "The file holds" not in proc.stdout - - def test_the_script_adds_through_the_store_and_never_by_path() -> None: code = "\n".join( ln @@ -854,5 +463,4 @@ def test_the_script_adds_through_the_store_and_never_by_path() -> None: # single import is offered. assert body.index("IsInRole(") < body.index("ReadAllBytes(") assert body.index("Get-CaRefusal -Certificate") < body.index("ShouldProcess(") - assert body.index("Get-ChainRefusal -Certificate") < body.index("ShouldProcess(") - assert body.index("ShouldProcess(") < body.index("$store.Add(") + assert body.index("ShouldProcess(") < body.index(".Store.Add(") diff --git a/tests/test_import_db_ca_console_text.py b/tests/test_import_db_ca_console_text.py index 366c87f34c..33907cbedd 100644 --- a/tests/test_import_db_ca_console_text.py +++ b/tests/test_import_db_ca_console_text.py @@ -8,8 +8,8 @@ before ``Write-Host`` or ``ShouldProcess`` shows it. NOTHING HERE RUNS THE SCRIPT ITSELF: an import needs an elevated Windows session and writes the -machine trust store. (``tests/test_import_db_ca_bundle.py`` runs it unelevated only: with -``-WhatIf``, which writes nothing, and once without it, to meet the script's elevation refusal.) So the static half pins that every console line naming the certificate's Subject or +machine trust store. (``tests/test_import_db_ca_bundle.py`` runs it with ``-WhatIf`` only, which +writes nothing.) So the static half pins that every console line naming the certificate's Subject or Issuer goes through the helper, and the behavioural half lifts the helper's two function definitions out of the script's own AST and calls them, never running the script. The behavioural half needs ``pwsh`` and skips where there is none, as ``tests/test_net_helper_install_scripts.py`` diff --git a/tests/test_measure_store_access_gate_refusal.py b/tests/test_measure_store_access_gate_refusal.py index c827200505..74add1c731 100644 --- a/tests/test_measure_store_access_gate_refusal.py +++ b/tests/test_measure_store_access_gate_refusal.py @@ -9,11 +9,11 @@ ``Test-GateRefusalLine``. NOTHING HERE RUNS THE SCRIPT: it installs a service and must run elevated, which only the hosted -windows-service-smoke leg does. The test lifts the two phrase assignments and -``Test-GateRefusalLine`` out of the script's AST and calls the function on lines worded as the -engine words them. It pins that ``app.py`` still holds each line's prefix and the clause text after -it, as literals; it does not render the lines from ``app.py``. It needs ``pwsh`` and skips where -there is none. +windows-service-smoke leg does. The tests lift the phrase assignments and functions they need out +of the script's AST: they call ``Test-GateRefusalLine`` on lines worded as the engine words them, +and run ``Wait-ForGateRefusal`` against a stand-in log reader. They pin that ``app.py`` still holds +each line's prefix and the clause text after it, as literals; they do not render the lines from +``app.py``. They need ``pwsh`` or ``powershell`` and skip where it is missing. """ from __future__ import annotations @@ -31,7 +31,7 @@ _SCRIPT = _ROOT / "scripts" / "service" / "measure-store-access.ps1" _APP = _ROOT / "messagefoundry" / "api" / "app.py" -#: The prefix each of the gate's three lines opens with in ``app.py``, and a line worded so. +#: The prefix each of the gate's lines opens with in ``app.py``, and a line worded so. _REFUSAL = ( "refusing to start a PHI instance: ", "RuntimeError: refusing to start a PHI instance: no enabled Administrator exists in this store," @@ -47,30 +47,68 @@ "WARNING messagefoundry.api.app: PHI instance with no deliverable security-notice recipient: no" " enabled Administrator has a notification address, so every out-of-band security notice", ) +#: The gate's could-not-run warning (a Vault transit outage while the gate is not enforcing). It lets +#: the start go on, and it does not carry the clause the lines above share. +_COULD_NOT_RUN = ( + "the security-notice gate could not run: ", + "WARNING messagefoundry.api.app: the security-notice gate could not run: the accounts could not" + " be checked against their integrity seal because Vault transit did not answer", +) def _psq(value: str) -> str: return "'" + value.replace("'", "''") + "'" -def _judge(host: str, tmp_path: Path, lines: list[str]) -> dict[str, object]: +def _run(host: str, tmp_path: Path, script: str) -> dict[str, object]: if shutil.which(host) is None: pytest.skip(f"SKIP (nothing run): {host} not on PATH") - script = f""" + f = tmp_path / f"gate-{uuid.uuid4().hex}.ps1" + # With a BOM, which Windows PowerShell 5.1 needs to read a non-ASCII path as UTF-8. + f.write_text(script, encoding="utf-8-sig") + # Windows PowerShell's default policy can refuse an unsigned -File; pwsh's does not. + policy = ["-ExecutionPolicy", "Bypass"] if host == "powershell" else [] + proc = run_single( + [host, "-NoProfile", "-NonInteractive", *policy, "-File", str(f)], + capture_output=True, + text=True, + timeout=120, + check=False, + ) + assert proc.returncode == 0, f"harness failed: {proc.stdout}\n{proc.stderr}" + result: dict[str, object] = json.loads(proc.stdout.strip().splitlines()[-1]) + return result + + +def _lift(variables: list[str], functions: list[str]) -> str: + """PowerShell that dot-sources the named assignments and functions out of the script's AST.""" + names = ", ".join(_psq(v) for v in variables) + funcs = ", ".join(_psq(f) for f in functions) + return f""" $ErrorActionPreference = 'Stop' $errs = $null $ast = [System.Management.Automation.Language.Parser]::ParseFile({_psq(str(_SCRIPT))}, [ref]$null, [ref]$errs) if (@($errs).Count) {{ throw "the script does not parse: $($errs[0])" }} -foreach ($var in @('GateRefusal', 'NoAdminRefusal', 'StartRefused', 'GateLetStart')) {{ +foreach ($var in @({names})) {{ $set = $ast.Find({{ $args[0] -is [System.Management.Automation.Language.AssignmentStatementAst] -and $args[0].Left.Extent.Text -eq ('$' + $var) }}, $false) if (-not $set) {{ throw "missing assignment `${{var}}" }} . ([scriptblock]::Create($set.Extent.Text)) }} -$def = $ast.Find({{ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] -and - $args[0].Name -eq 'Test-GateRefusalLine' }}, $true) -if (-not $def) {{ throw 'missing function Test-GateRefusalLine' }} -. ([scriptblock]::Create($def.Extent.Text)) +foreach ($name in @({funcs})) {{ + $def = $ast.Find({{ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] -and + $args[0].Name -eq $name }}, $true) + if (-not $def) {{ throw "missing function $name" }} + . ([scriptblock]::Create($def.Extent.Text)) +}} +""" + + +def _judge(host: str, tmp_path: Path, lines: list[str]) -> dict[str, object]: + script = _lift( + ["GateRefusal", "NoAdminRefusal", "StartRefused", "GateLetStart"], ["Test-GateRefusalLine"] + ) + ( + f""" $lines = {_psq(json.dumps(lines))} | ConvertFrom-Json ConvertTo-Json -Compress -InputObject @{{ refusal = @($lines | ForEach-Object {{ [bool](Test-GateRefusalLine $_) }}) @@ -78,26 +116,35 @@ def _judge(host: str, tmp_path: Path, lines: list[str]) -> dict[str, object]: let_start = @($lines | ForEach-Object {{ $_ -match $GateLetStart }}) clause = @($lines | ForEach-Object {{ $_ -match [regex]::Escape($GateRefusal) }}) }} """ - f = tmp_path / f"gate-{uuid.uuid4().hex}.ps1" - # With a BOM, which Windows PowerShell 5.1 needs to read a non-ASCII path as UTF-8. - f.write_text(script, encoding="utf-8-sig") - # Windows PowerShell's default policy can refuse an unsigned -File; pwsh's does not. - policy = ["-ExecutionPolicy", "Bypass"] if host == "powershell" else [] - proc = run_single( - [host, "-NoProfile", "-NonInteractive", *policy, "-File", str(f)], - capture_output=True, - text=True, - timeout=120, - check=False, ) - assert proc.returncode == 0, f"harness failed: {proc.stdout}\n{proc.stderr}" - result: dict[str, object] = json.loads(proc.stdout.strip().splitlines()[-1]) - return result + return _run(host, tmp_path, script) + + +def _wait(host: str, tmp_path: Path, lines: list[str]) -> dict[str, object]: + """Run the script's own ``Wait-ForGateRefusal`` with ``lines`` as the whole service log.""" + script = _lift( + ["GateRefusal", "StartRefused", "GateLetStart"], + ["Test-GateRefusalLine", "Wait-ForGateRefusal", "Get-Excerpt"], + ) + ( + f""" +$script:LogLines = @({_psq(json.dumps(lines))} | ConvertFrom-Json) +$script:ReadingsSeen = New-Object System.Collections.ArrayList +function Update-OwnerSample([string]$Phase) {{ }} +function Get-LogMatches {{ param([string]$Pattern) @($script:LogLines | Select-String -Pattern $Pattern) }} +function Add-Reading([string]$Text) {{ [void]$script:ReadingsSeen.Add($Text) }} +$WaitSeconds = 2 +$refused = [bool](Wait-ForGateRefusal 'the first start') +ConvertTo-Json -Compress -InputObject @{{ + refused = $refused; let_start_seen = [bool]$script:GateLetStartSeen + readings = @($script:ReadingsSeen).Count }} +""" + ) + return _run(host, tmp_path, script) -def test_the_engine_still_words_the_three_gate_lines_so() -> None: +def test_the_engine_still_words_the_gate_lines_so() -> None: app = _APP.read_text(encoding="utf-8") - for prefix, line in (_REFUSAL, _SKIPPED, _WARN_MODE): + for prefix, line in (_REFUSAL, _SKIPPED, _WARN_MODE, _COULD_NOT_RUN): assert prefix in app, prefix assert prefix in line # The clauses the script matches on, as app.py spells the detail text after each prefix. The @@ -118,26 +165,56 @@ def test_only_the_refusal_counts_as_a_refused_start(tmp_path: Path, host: str) - " outside identity service" ) got = _judge( - host, tmp_path, [_REFUSAL[1], _SKIPPED[1], _WARN_MODE[1], census, "unrelated line"] + host, + tmp_path, + [_REFUSAL[1], _SKIPPED[1], _WARN_MODE[1], _COULD_NOT_RUN[1], census, "unrelated line"], ) - assert got["refusal"] == [True, False, False, False, False], got + assert got["refusal"] == [True, False, False, False, False, False], got # The narrower clause the StartFirstEndToEnd arm passes: the no-Administrator refusal only. - assert got["no_admin"] == [True, False, False, False, False], got - # Only the gate's two let-it-start lines end the wait early, not the census line. - assert got["let_start"] == [False, True, True, False, False], got - # The control: the clause the wait used to match alone is in both warning lines, so the old - # criterion took an engine that went on to serve for a refused one. - assert got["clause"] == [True, True, True, True, False], got + assert got["no_admin"] == [True, False, False, False, False, False], got + # Only the gate's let-it-start lines end the wait early, not the census line. + assert got["let_start"] == [False, True, True, True, False, False], got + # The control: the clause the wait used to match alone is in the first two warning lines, so the + # old criterion took an engine that went on to serve for a refused one. The could-not-run line + # does not carry it. + assert got["clause"] == [True, True, True, False, True, False], got + + +@pytest.mark.parametrize("host", ["pwsh", "powershell"]) +@pytest.mark.parametrize( + ("line", "refused", "let_start"), + [ + (_REFUSAL[1], True, False), + (_SKIPPED[1], False, True), + (_WARN_MODE[1], False, True), + # Without the clause, a let-start search confined to clause lines never sees this one: the + # wait ran to its timeout and the failure named store access. + (_COULD_NOT_RUN[1], False, True), + ("unrelated line", False, False), + ], +) +def test_the_wait_ends_on_each_gate_line( + tmp_path: Path, host: str, line: str, refused: bool, let_start: bool +) -> None: + got = _wait(host, tmp_path, [line]) + assert got["refused"] is refused, got + assert got["let_start_seen"] is let_start, got + # A let-start line is quoted in a reading, which the failure text points at. + assert got["readings"] == (1 if let_start else 0), got def test_a_first_start_the_gate_let_go_on_is_not_attributed_to_store_access() -> None: - # Both first-start branches name the cause only as the control and restart branches do: the - # store-access attribution only when the gate did not let the start go on. + # Both first-start branches go through Add-FirstStartFailure, which names the cause only as the + # control and restart branches do: the store-access attribution only when the gate did not let + # the start go on. text = _SCRIPT.read_text(encoding="utf-8") + calls = [ln.strip() for ln in text.splitlines()] + assert calls.count("Add-FirstStartFailure") == 2 failure = 'Add-Failure ("$ServiceIdentity $what within $WaitSeconds s on a fresh store. "' - assert text.count(failure) == 2 - for at in [i for i in range(len(text)) if text.startswith(failure, i)]: - before = text[text.rindex("$cause = if", 0, at) : at] - assert before.startswith("$cause = if ($script:GateLetStartSeen) {"), before - assert "Get-Attribution" in before.split("else", 1)[1], before - assert "Get-Attribution" not in text[at : text.index("\n", at)] + assert text.count(failure) == 1 + at = text.index(failure) + assert text.rindex("function Add-FirstStartFailure", 0, at) > 0 + before = text[text.rindex("$cause = if", 0, at) : at] + assert before.startswith("$cause = if ($script:GateLetStartSeen) {"), before + assert "Get-Attribution" in before.split("else", 1)[1], before + assert "Get-Attribution" not in text[at : text.index("\n", at)] From d8a31a68874b79eef888f4b9a84544f949d58ebb Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 10:49:40 -0600 Subject: [PATCH 11/19] Tooling partition comment: the gate's prefixes are not three any more BACKLOG #5658. Proposed PR title: measure-store-access.ps1 counts only the ADR 0167 refusal as a refused start (BACKLOG #5658) Proposed banners: #5658 BUILT, in this PR. #5561 OPEN; this PR's history holds a withdrawn hand-built chain check. --- tests/test_tooling_partition.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_tooling_partition.py b/tests/test_tooling_partition.py index 0f70fdf1fa..24c05e457c 100644 --- a/tests/test_tooling_partition.py +++ b/tests/test_tooling_partition.py @@ -76,7 +76,7 @@ # the change that breaks it. Its other half reads docs/SECURITY.md, which is what makes it a # doc-drift guard, but the doc half is not the reason it stays. "test_adaptive_attributes_doc_drift.py", - # Reads messagefoundry/api/app.py to pin the ADR 0167 gate's three log-line prefixes that + # Reads messagefoundry/api/app.py to pin the ADR 0167 gate's log-line prefixes that # scripts/service/measure-store-access.ps1 tells apart. An engine diff that rewords them must # meet this test, and the tooling job's path gate would not run it then. "test_measure_store_access_gate_refusal.py", From cfe808812b70033b7c0db3b27eac4a150d80fa46 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 11:34:18 -0600 Subject: [PATCH 12/19] measure-store-access.ps1: correct three statements review round 51fbd95e found false The changelog fragment, the Add-FirstStartFailure docstring and the test module docstring each claimed more than the code does. Each now says only what the code checks. BACKLOG #5658. Proposed PR title: measure-store-access.ps1 counts only the ADR 0167 refusal as a refused start (BACKLOG #5658) Proposed banners: #5658 BUILT, in this PR. #5561 OPEN; this PR's history holds a withdrawn hand-built chain check. --- changelog.d/5658.fixed.md | 10 +++++----- scripts/service/measure-store-access.ps1 | 4 ++-- tests/test_measure_store_access_gate_refusal.py | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/changelog.d/5658.fixed.md b/changelog.d/5658.fixed.md index 9b816dbfc1..a22fce1600 100644 --- a/changelog.d/5658.fixed.md +++ b/changelog.d/5658.fixed.md @@ -1,5 +1,5 @@ -- **`measure-store-access.ps1` counts a start as refused only on the ADR 0167 gate's refusal - line.** It used to count any line naming "no enabled Administrator", which the gate's warnings - also carry when they let the start go on. A start the gate let go on now fails the arm without - naming store access as the cause. The script's `$GateLetStart` comment names the lines it - recognises. (`BACKLOG #5658`) +- **`measure-store-access.ps1` counts a start as refused only on a line that carries both + "refusing to start" and "no enabled Administrator".** It used to count any line naming "no + enabled Administrator", and the gate's skipped-gate and warn-mode warnings carry that too while + the start goes on. When the gate logs one of the let-start warnings the script's `$GateLetStart` + names, the arm now fails without naming store access as the cause. (`BACKLOG #5658`) diff --git a/scripts/service/measure-store-access.ps1 b/scripts/service/measure-store-access.ps1 index 0e3460cd40..04acd8c9cb 100644 --- a/scripts/service/measure-store-access.ps1 +++ b/scripts/service/measure-store-access.ps1 @@ -679,8 +679,8 @@ function Wait-ForGateRefusal([string]$Phase) { function Add-FirstStartFailure { <# The failure for a first start on a fresh store that Wait-ForGateRefusal did not see refused. - As in the control and restart branches: a gate that let the start go on rules out a lockout, - so only a start it did not reach gets the store-access attribution. + As in the control and restart branches, it gives the store-access attribution unless the wait + saw one of the gate's let-start lines ($GateLetStart). #> $what = if ($script:GateLetStartSeen) { "created $DbPath and reached the ADR 0167 gate, which let the start go on (the READING line quotes it) rather than refusing it," } elseif (Test-Path -LiteralPath $DbPath) { "created $DbPath but did not log the expected refusal line" } diff --git a/tests/test_measure_store_access_gate_refusal.py b/tests/test_measure_store_access_gate_refusal.py index 74add1c731..9b71507f09 100644 --- a/tests/test_measure_store_access_gate_refusal.py +++ b/tests/test_measure_store_access_gate_refusal.py @@ -11,9 +11,9 @@ NOTHING HERE RUNS THE SCRIPT: it installs a service and must run elevated, which only the hosted windows-service-smoke leg does. The tests lift the phrase assignments and functions they need out of the script's AST: they call ``Test-GateRefusalLine`` on lines worded as the engine words them, -and run ``Wait-ForGateRefusal`` against a stand-in log reader. They pin that ``app.py`` still holds -each line's prefix and the clause text after it, as literals; they do not render the lines from -``app.py``. They need ``pwsh`` or ``powershell`` and skip where it is missing. +and run ``Wait-ForGateRefusal`` against a stand-in log reader. They pin, as literals, that ``app.py`` +still holds each line's prefix and the clause texts the script matches on; they do not render the +lines from ``app.py``. They need ``pwsh`` or ``powershell`` and skip where it is missing. """ from __future__ import annotations From bc906340f15fffb477885bba82f0761c207ce75a Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 11:52:47 -0600 Subject: [PATCH 13/19] measure-store-access.ps1: two more statements review round 51fbd95e found false The control branch's failure said the store's access was not in question right after a failure that the operator could not open the store; it now says the service's access. The $StartRefused comment said step 1 of every arm would pass; ProvisionFirst has no refusal step, so it now names the steps that expect a refusal. BACKLOG #5658. Proposed PR title: measure-store-access.ps1 counts only the ADR 0167 refusal as a refused start (BACKLOG #5658) Proposed banners: #5658 BUILT, in this PR. #5561 OPEN; this PR's history holds a withdrawn hand-built chain check. --- scripts/service/measure-store-access.ps1 | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/scripts/service/measure-store-access.ps1 b/scripts/service/measure-store-access.ps1 index 04acd8c9cb..0a87c5a854 100644 --- a/scripts/service/measure-store-access.ps1 +++ b/scripts/service/measure-store-access.ps1 @@ -123,8 +123,9 @@ $GateRefusal = "no enabled Administrator" $NoAdminRefusal = "no enabled Administrator exists" $ProvisionHint = "provision-admin --username" # The words that make it a REFUSAL. The skipped-gate WARNING and the warn-mode line carry the two -# phrases above too, so without this an engine that started and served would pass step 1 of every -# arm; Test-GateRefusalLine requires both on one line. +# phrases above too, so without this an engine that started and served would pass every step that +# expects a refusal (each Wait-ForGateRefusal call below; ProvisionFirst has none); +# Test-GateRefusalLine requires both on one line. $StartRefused = "refusing to start" # The openings of the gate's lines that let a start go on, at least these three in app.py: the # skipped-gate warning, the warn-mode line, and the warning that the gate could not run (a Vault @@ -1086,7 +1087,7 @@ try { } elseif ($script:GateLetStartSeen) { Add-Failure ("control: $ServiceIdentity reopened the store it created and reached " + "the ADR 0167 gate, which let the start go on (the reading above), so this " + - "control expected a refusal it did not get; the store's access is not in question.") + "control expected a refusal it did not get; the service's access is not in question.") Show-LogTail } else { Add-Failure ("control: $ServiceIdentity could not reopen even the store it created " + From 4831341505861b169235364ebfd870fc1dfa0a59 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 13:09:03 -0600 Subject: [PATCH 14/19] measure-store-access.ps1: make six statements review round 6235709f7a found false true Prose and program output only; what the script decides is unchanged. - Wait-ForGateRefusal's comment no longer says a failure text never names a lockout the gate ruled out: it names the Vault-transit integrity-seal refusal as unrecognised, and says the wait times out and the caller prints the store-access attribution on that path. - The control branch's timeout red no longer states as fact that the first open locked out every identity: it says the expected refusal line was not logged, and states the lockout as a condition, as the restart and first-start texts do. - The let-start reds say "the service's access is not in question" (first start and restart used to say "the store's"). - The header's WHAT A RED MEANS says "at least" and names the three let-start reds as the exception; CONFINEMENT names which opens it checks and that the control reopen and red steps are not checked. - The $StartRefused comment no longer counts "two phrases above"; the $GateRefusal comment no longer says the gate has two branches. - The gate-refusal test's docstring says what it pins (not $ProvisionHint or $ProvisionDecline), and its could-not-run comment says the old failure carried the store-access attribution. BACKLOG #5658 Proposed PR title: measure-store-access.ps1 counts only the ADR 0167 refusal as a refused start (BACKLOG #5658) Proposed ledger banner: BUILT -- a start counts as refused only on a line with both the refusal words and the gate clause; the gate's let-start lines fail the arm without blaming store access. --- scripts/service/measure-store-access.ps1 | 64 ++++++++++++------- .../test_measure_store_access_gate_refusal.py | 7 +- 2 files changed, 44 insertions(+), 27 deletions(-) diff --git a/scripts/service/measure-store-access.ps1 b/scripts/service/measure-store-access.ps1 index 0a87c5a854..3cc8aefdc0 100644 --- a/scripts/service/measure-store-access.ps1 +++ b/scripts/service/measure-store-access.ps1 @@ -53,17 +53,24 @@ NT SERVICE\, and every process sampled under the service must run as it. Where the engine is up (provision first), the sample must include the engine process itself. - WHAT A RED MEANS. Every red names the identity that could not open the store, and lists for each - file of the trio the entries it carries and whether any grants that identity read and write. That - list is a READING of the DACL, not an access check: it names the file, and the behaviour above - says the open failed. A red is tagged [STORE ACCESS] only when the service log or the probe - output shows an open failure; otherwise it is tagged [NOT SHOWN TO BE STORE ACCESS], because a - refused start gate or a held port also stops the engine from serving. - - CONFINEMENT IS CHECKED AFTER EVERY OPEN. Opening is half of the question; the other half is the - property ADR 0163's restriction exists for. After each identity's open, every trio file may grant - only SYSTEM, BUILTIN\Administrators, the service account and the operator, block inheritance, and - be owned by one of them, or the arm goes RED. + WHAT A RED MEANS. At least every red that says an identity did not open, provision or serve + within the wait, apart from the three let-start reds below, names that identity and lists for + each file of the trio the entries it carries and whether any grants that identity read and + write. That list is a READING of the DACL, not an access check: it names the file, and the + behaviour above says the open failed. Such a red is tagged [STORE ACCESS] only when the + service log or the probe output shows an open failure; otherwise it is tagged + [NOT SHOWN TO BE STORE ACCESS], because a refused start gate or a held port also stops the + engine from serving. + The three let-start reds are for a start the ADR 0167 gate let go on where a refusal was + expected (the first start, the StartFirst control and the StartFirst restart). They say so, + point at the READING that quotes the gate's line, and say the service's access is not in + question, with no trio list and no tag. Other reds say what they checked. + + CONFINEMENT IS CHECKED AFTER EACH OPEN A STEP PROVES. Opening is half of the question; the other + half is the property ADR 0163's restriction exists for. After each such open, every trio file + may grant only SYSTEM, BUILTIN\Administrators, the service account and the operator, block + inheritance, and be owned by one of them, or the arm goes RED. The control reopen is not + checked, and neither is an open whose step went red, the three let-start reds included. A GREEN CAN BE CONDITIONAL, and says so: store.py only logs when it cannot restrict a file, so a restriction warning in the service log or the CLI output prints GREEN (CONDITIONAL) with a warning. @@ -116,16 +123,19 @@ $Email = "w0-admin@example.invalid" # The ADR 0167 gate's own words (messagefoundry/api/app.py, _assert_security_notice_is_deliverable), # and the words provision_first_administrator declines with (messagefoundry/auth/service.py). If # either is reworded, the arm goes RED on a missing proof rather than passing on a stale one. -# $GateRefusal is the clause BOTH of the gate's branches open with, so the file-access arms prove the -# service read the user table whichever branch fired. $NoAdminRefusal and $ProvisionHint are the -# no-Administrator branch alone, which StartFirstEndToEnd requires (ADR 0183 Wave 2, AC-11). +# $GateRefusal is the clause the gate's no-Administrator and no-address branches both open with, so +# the file-access arms prove the service read the user table whichever of the two fired. The gate's +# Vault-transit refusal carries no phrase here (see Wait-ForGateRefusal). $NoAdminRefusal and +# $ProvisionHint are the no-Administrator branch alone, which StartFirstEndToEnd requires (ADR 0183 +# Wave 2, AC-11); $NoAdminRefusal is in it only when no account record failed its integrity seal. $GateRefusal = "no enabled Administrator" $NoAdminRefusal = "no enabled Administrator exists" $ProvisionHint = "provision-admin --username" -# The words that make it a REFUSAL. The skipped-gate WARNING and the warn-mode line carry the two -# phrases above too, so without this an engine that started and served would pass every step that -# expects a refusal (each Wait-ForGateRefusal call below; ProvisionFirst has none); -# Test-GateRefusalLine requires both on one line. +# The words that make it a REFUSAL. The skipped-gate WARNING and the warn-mode line carry +# $GateRefusal too, and may carry the other two phrases above, so without this an engine that +# started and served would pass every step that expects a refusal (each Wait-ForGateRefusal call +# below; ProvisionFirst has none); Test-GateRefusalLine requires $StartRefused and the clause on one +# line. $StartRefused = "refusing to start" # The openings of the gate's lines that let a start go on, at least these three in app.py: the # skipped-gate warning, the warn-mode line, and the warning that the gate could not run (a Vault @@ -655,7 +665,12 @@ function Wait-ForGateRefusal([string]$Phase) { $false on timeout, and $false at once, with a reading quoting it, when one of the gate's let-it-start lines ($GateLetStart) is logged. Any other line is waited past. #> - # Read by callers after a $false, so a failure text never names a lockout the gate ruled out. + # Read by callers after a $false, so a failure text does not name a lockout when the gate logged + # a let-start line. One gate path is NOT recognised: the Vault-transit integrity-seal refusal + # ("refusing to start a PHI instance: the accounts could not be checked against their integrity + # seal ...") carries $StartRefused but not $GateRefusal, and opens with no $GateLetStart prefix. + # On that path the wait times out, and the caller prints its timeout text and the store-access + # attribution (Get-Attribution) rather than naming the gate. $script:GateLetStartSeen = $false $deadline = (Get-Date).AddSeconds($WaitSeconds) while ((Get-Date) -lt $deadline) { @@ -686,7 +701,7 @@ function Add-FirstStartFailure { $what = if ($script:GateLetStartSeen) { "created $DbPath and reached the ADR 0167 gate, which let the start go on (the READING line quotes it) rather than refusing it," } elseif (Test-Path -LiteralPath $DbPath) { "created $DbPath but did not log the expected refusal line" } else { "never created $DbPath" } - $cause = if ($script:GateLetStartSeen) { "The store's access is not in question." } + $cause = if ($script:GateLetStartSeen) { "The service's access is not in question." } else { Get-Attribution $ServiceIdentity (Get-ServiceSids) "" } Add-Failure ("$ServiceIdentity $what within $WaitSeconds s on a fresh store. " + $cause) Show-LogTail @@ -1090,9 +1105,10 @@ try { "control expected a refusal it did not get; the service's access is not in question.") Show-LogTail } else { - Add-Failure ("control: $ServiceIdentity could not reopen even the store it created " + - "itself, which nobody else touched, so its own first open locked out every " + - "identity. " + (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) + Add-Failure ("control: $ServiceIdentity, restarted on the store it created itself, " + + "which nobody else touched, did not log the expected refusal line in " + + "$WaitSeconds s. If the cause is store access, its own first open locked out " + + "every identity. " + (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) Show-LogTail } } else { @@ -1116,7 +1132,7 @@ try { if (-not $reopened -and $script:GateLetStartSeen) { Add-Failure ("$ServiceIdentity opened $DbPath after $Operator's open and reached " + "the ADR 0167 gate, which let the start go on (the reading above), so the " + - "restart expected a refusal it did not get; the store's access is not in question.") + "restart expected a refusal it did not get; the service's access is not in question.") Show-LogTail } elseif (-not $reopened) { Add-Failure ("$ServiceIdentity, restarted on $DbPath after $Operator's open " + diff --git a/tests/test_measure_store_access_gate_refusal.py b/tests/test_measure_store_access_gate_refusal.py index 9b71507f09..6ef28911d9 100644 --- a/tests/test_measure_store_access_gate_refusal.py +++ b/tests/test_measure_store_access_gate_refusal.py @@ -12,8 +12,9 @@ windows-service-smoke leg does. The tests lift the phrase assignments and functions they need out of the script's AST: they call ``Test-GateRefusalLine`` on lines worded as the engine words them, and run ``Wait-ForGateRefusal`` against a stand-in log reader. They pin, as literals, that ``app.py`` -still holds each line's prefix and the clause texts the script matches on; they do not render the -lines from ``app.py``. They need ``pwsh`` or ``powershell`` and skip where it is missing. +still holds each line's prefix and the no-Administrator and no-address detail texts; they do not pin +``$ProvisionHint`` or ``$ProvisionDecline``, and they do not render the lines from ``app.py``. They +need ``pwsh`` or ``powershell`` and skip where it is missing. """ from __future__ import annotations @@ -188,7 +189,7 @@ def test_only_the_refusal_counts_as_a_refused_start(tmp_path: Path, host: str) - (_SKIPPED[1], False, True), (_WARN_MODE[1], False, True), # Without the clause, a let-start search confined to clause lines never sees this one: the - # wait ran to its timeout and the failure named store access. + # wait ran to its timeout and the caller's failure carried the store-access attribution. (_COULD_NOT_RUN[1], False, True), ("unrelated line", False, False), ], From 747c25d043c61a4a5626d833ce503451ca4e5caf Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 13:37:07 -0600 Subject: [PATCH 15/19] measure-store-access.ps1: five more statements review round cbc8f90d found false Prose and program output only; what the script decides is unchanged. - The header's CONFINEMENT paragraph no longer says an open whose step went red is unchecked: it names the opens whose proof did not arrive. - The constants comment says the Vault-transit refusal carries none of the three phrases above, not "no phrase here" ($StartRefused is in it). - The control red and its comment no longer say nobody else touched the store: the operator's failed run may have opened and re-secured it, so the lockout is stated on that condition. - The changelog says only a step that waits for the refusal fails on a let-start warning. - The gate-refusal test's docstring says each PowerShell case needs its own host and two tests need neither. BACKLOG #5658 Proposed PR title: measure-store-access.ps1 counts only the ADR 0167 refusal as a refused start (BACKLOG #5658) Proposed ledger banner: BUILT -- a start counts as refused only on a line with both the refusal words and the gate clause; a step waiting for the refusal fails on the gate's let-start lines without blaming store access. --- changelog.d/5658.fixed.md | 5 +++-- scripts/service/measure-store-access.ps1 | 19 +++++++++++-------- .../test_measure_store_access_gate_refusal.py | 5 +++-- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/changelog.d/5658.fixed.md b/changelog.d/5658.fixed.md index a22fce1600..e72a690669 100644 --- a/changelog.d/5658.fixed.md +++ b/changelog.d/5658.fixed.md @@ -1,5 +1,6 @@ - **`measure-store-access.ps1` counts a start as refused only on a line that carries both "refusing to start" and "no enabled Administrator".** It used to count any line naming "no enabled Administrator", and the gate's skipped-gate and warn-mode warnings carry that too while - the start goes on. When the gate logs one of the let-start warnings the script's `$GateLetStart` - names, the arm now fails without naming store access as the cause. (`BACKLOG #5658`) + the start goes on. When a step that waits for the refusal sees one of the let-start warnings the + script's `$GateLetStart` names, the arm now fails without naming store access as the cause. + (`BACKLOG #5658`) diff --git a/scripts/service/measure-store-access.ps1 b/scripts/service/measure-store-access.ps1 index 3cc8aefdc0..6d416868fa 100644 --- a/scripts/service/measure-store-access.ps1 +++ b/scripts/service/measure-store-access.ps1 @@ -70,7 +70,8 @@ half is the property ADR 0163's restriction exists for. After each such open, every trio file may grant only SYSTEM, BUILTIN\Administrators, the service account and the operator, block inheritance, and be owned by one of them, or the arm goes RED. The control reopen is not - checked, and neither is an open whose step went red, the three let-start reds included. + checked, and neither is an open whose proof (the expected refusal, the operator's provision or + decline, or /health and sign-in) did not arrive, the three let-start reds included. A GREEN CAN BE CONDITIONAL, and says so: store.py only logs when it cannot restrict a file, so a restriction warning in the service log or the CLI output prints GREEN (CONDITIONAL) with a warning. @@ -125,7 +126,7 @@ $Email = "w0-admin@example.invalid" # either is reworded, the arm goes RED on a missing proof rather than passing on a stale one. # $GateRefusal is the clause the gate's no-Administrator and no-address branches both open with, so # the file-access arms prove the service read the user table whichever of the two fired. The gate's -# Vault-transit refusal carries no phrase here (see Wait-ForGateRefusal). $NoAdminRefusal and +# Vault-transit refusal carries none of these three (see Wait-ForGateRefusal). $NoAdminRefusal and # $ProvisionHint are the no-Administrator branch alone, which StartFirstEndToEnd requires (ADR 0183 # Wave 2, AC-11); $NoAdminRefusal is in it only when no account record failed its integrity seal. $GateRefusal = "no enabled Administrator" @@ -1091,9 +1092,10 @@ try { Add-Failure ("$Operator could not open $DbPath, which $ServiceIdentity created and " + "secured, through provision-admin's open path (exit $($r.Code)). " + (Get-Attribution $Operator (Get-OperatorSids) $r.Text)) - # CONTROL: nothing re-secured the store, so a service that cannot reopen even its own - # store means the first open locked out EVERY identity, not only the operator. That - # separates "restricted to the service" from "restricted to nobody usable". + # CONTROL: if the operator's failed run did not open and re-secure the store, a service + # that cannot reopen even its own store means the first open locked out EVERY identity, + # not only the operator. That separates "restricted to the service" from "restricted + # to nobody usable". Start-TheService "reopen of its own store (control)" $own = Wait-ForGateRefusal "the control reopen" Stop-TheService @@ -1106,9 +1108,10 @@ try { Show-LogTail } else { Add-Failure ("control: $ServiceIdentity, restarted on the store it created itself, " + - "which nobody else touched, did not log the expected refusal line in " + - "$WaitSeconds s. If the cause is store access, its own first open locked out " + - "every identity. " + (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) + "did not log the expected refusal line in $WaitSeconds s. If the cause is store " + + "access, and the operator's failed run did not open and re-secure the store, " + + "its own first open locked out every identity. " + + (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) Show-LogTail } } else { diff --git a/tests/test_measure_store_access_gate_refusal.py b/tests/test_measure_store_access_gate_refusal.py index 6ef28911d9..f29d5035a1 100644 --- a/tests/test_measure_store_access_gate_refusal.py +++ b/tests/test_measure_store_access_gate_refusal.py @@ -13,8 +13,9 @@ of the script's AST: they call ``Test-GateRefusalLine`` on lines worded as the engine words them, and run ``Wait-ForGateRefusal`` against a stand-in log reader. They pin, as literals, that ``app.py`` still holds each line's prefix and the no-Administrator and no-address detail texts; they do not pin -``$ProvisionHint`` or ``$ProvisionDecline``, and they do not render the lines from ``app.py``. They -need ``pwsh`` or ``powershell`` and skip where it is missing. +``$ProvisionHint`` or ``$ProvisionDecline``, and they do not render the lines from ``app.py``. Each +case that runs PowerShell needs its own host, ``pwsh`` or ``powershell``, and skips where that host +is missing; the ``app.py`` pin and the source-text test need neither. """ from __future__ import annotations From 6577b06ec5a75ab5d73f54c20b33d87ab0eb79da Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 14:23:16 -0600 Subject: [PATCH 16/19] measure-store-access: shrink red and header prose to what the run observed Prose and program output only; what the script decides is unchanged. Round 1aca1c50 findings 1-10 on PR 3025, shrunk rather than qualified: the header's StartFirst step 3, WHAT A RED MEANS and CONFINEMENT now state what Get-Attribution and Test-Resecured do, and point at the script for which reds and opens they cover; the operator, control and restart reds say what the run saw (a non-zero exit, a refusal line or its absence, a let-start line); the Get-Attribution comment and the changelog say no more than the code does; the three phrase names are spelled out. The GREEN line and the Test-Resecured docstring are shrunk the same way. BACKLOG #5658 Proposed PR title: measure-store-access counts only the ADR 0167 refusal as a refused start (BACKLOG #5658) Proposed ledger banner: SHIPPED -- the wait counts a line only with both the refusal words and the clause; a let-start line ends it with a reading and no store-access attribution. --- changelog.d/5658.fixed.md | 6 +- scripts/service/measure-store-access.ps1 | 115 +++++++++++------------ 2 files changed, 56 insertions(+), 65 deletions(-) diff --git a/changelog.d/5658.fixed.md b/changelog.d/5658.fixed.md index e72a690669..84559b4e63 100644 --- a/changelog.d/5658.fixed.md +++ b/changelog.d/5658.fixed.md @@ -1,6 +1,6 @@ - **`measure-store-access.ps1` counts a start as refused only on a line that carries both "refusing to start" and "no enabled Administrator".** It used to count any line naming "no enabled Administrator", and the gate's skipped-gate and warn-mode warnings carry that too while - the start goes on. When a step that waits for the refusal sees one of the let-start warnings the - script's `$GateLetStart` names, the arm now fails without naming store access as the cause. - (`BACKLOG #5658`) + the start goes on. When a step that waits for the refusal sees one of the gate's let-start lines + (`$GateLetStart`), the run logs a reading quoting it, and that step's red carries no store-access + attribution. (`BACKLOG #5658`) diff --git a/scripts/service/measure-store-access.ps1 b/scripts/service/measure-store-access.ps1 index 6d416868fa..744dac26f4 100644 --- a/scripts/service/measure-store-access.ps1 +++ b/scripts/service/measure-store-access.ps1 @@ -32,10 +32,10 @@ provisions. Either outcome is reached only after the store opened, so it is the proof that the operator opened it. 3. The service is started again and must reach the same refusal, which proves it can still - open the store that the operator's open re-secured. If step 2 instead PROVISIONED (no - bootstrap account existed), the gate would pass, so the proof is /health and sign-in. If - step 2 FAILED, the service is restarted anyway as a control: reopening the store it - created itself tells "restricted to the service" apart from "restricted to nobody usable". + open the store after the operator's open. If step 2 instead PROVISIONED (no bootstrap + account existed), the gate would pass, so the proof is /health and sign-in. If step 2 + FAILED, the service is restarted anyway as a control, and the run reports whether that + restart logged the refusal. -Order StartFirstEndToEnd (ADR 0183 Amendment A, Wave 2: AC-11 and AC-13 across two identities) The start-first flow an operator now meets, with no outcome left open. @@ -53,25 +53,19 @@ NT SERVICE\, and every process sampled under the service must run as it. Where the engine is up (provision first), the sample must include the engine process itself. - WHAT A RED MEANS. At least every red that says an identity did not open, provision or serve - within the wait, apart from the three let-start reds below, names that identity and lists for - each file of the trio the entries it carries and whether any grants that identity read and - write. That list is a READING of the DACL, not an access check: it names the file, and the - behaviour above says the open failed. Such a red is tagged [STORE ACCESS] only when the - service log or the probe output shows an open failure; otherwise it is tagged - [NOT SHOWN TO BE STORE ACCESS], because a refused start gate or a held port also stops the - engine from serving. - The three let-start reds are for a start the ADR 0167 gate let go on where a refusal was - expected (the first start, the StartFirst control and the StartFirst restart). They say so, - point at the READING that quotes the gate's line, and say the service's access is not in - question, with no trio list and no tag. Other reds say what they checked. - - CONFINEMENT IS CHECKED AFTER EACH OPEN A STEP PROVES. Opening is half of the question; the other - half is the property ADR 0163's restriction exists for. After each such open, every trio file - may grant only SYSTEM, BUILTIN\Administrators, the service account and the operator, block - inheritance, and be owned by one of them, or the arm goes RED. The control reopen is not - checked, and neither is an open whose proof (the expected refusal, the operator's provision or - decline, or /health and sign-in) did not arrive, the three let-start reds included. + WHAT A RED MEANS. A red that carries the store-access attribution (Get-Attribution) names an + identity and lists, for each file of the trio, its entries and whether any grants that identity + read and write. That list is a READING of the DACL, not an access check. It is tagged + [STORE ACCESS] when the service log or the probe output has a line matching $OpenErrorPattern, + and [NOT SHOWN TO BE STORE ACCESS] otherwise. Which reds carry it is in the script; at least a + red for a start the ADR 0167 gate let go on, where a refusal was expected, does not. + + CONFINEMENT. Opening is half of the question; the other half is the property ADR 0163's + restriction exists for. Test-Resecured reads each trio file present and goes RED on one that + inherits, is owned outside SYSTEM, BUILTIN\Administrators, the service account and the + operator, or grants anyone outside them. It runs only where the script calls it, which is not + after every open: at least the control reopen, and an open whose step logged no proof, are not + checked. A GREEN CAN BE CONDITIONAL, and says so: store.py only logs when it cannot restrict a file, so a restriction warning in the service log or the CLI output prints GREEN (CONDITIONAL) with a warning. @@ -126,17 +120,15 @@ $Email = "w0-admin@example.invalid" # either is reworded, the arm goes RED on a missing proof rather than passing on a stale one. # $GateRefusal is the clause the gate's no-Administrator and no-address branches both open with, so # the file-access arms prove the service read the user table whichever of the two fired. The gate's -# Vault-transit refusal carries none of these three (see Wait-ForGateRefusal). $NoAdminRefusal and -# $ProvisionHint are the no-Administrator branch alone, which StartFirstEndToEnd requires (ADR 0183 -# Wave 2, AC-11); $NoAdminRefusal is in it only when no account record failed its integrity seal. +# Vault-transit refusal carries none of $GateRefusal, $NoAdminRefusal and $ProvisionHint (see +# Wait-ForGateRefusal). $NoAdminRefusal and $ProvisionHint are the no-Administrator branch alone, +# which StartFirstEndToEnd requires (ADR 0183 Wave 2, AC-11); $NoAdminRefusal is in it only when no account record failed its integrity seal. $GateRefusal = "no enabled Administrator" $NoAdminRefusal = "no enabled Administrator exists" $ProvisionHint = "provision-admin --username" # The words that make it a REFUSAL. The skipped-gate WARNING and the warn-mode line carry -# $GateRefusal too, and may carry the other two phrases above, so without this an engine that -# started and served would pass every step that expects a refusal (each Wait-ForGateRefusal call -# below; ProvisionFirst has none); Test-GateRefusalLine requires $StartRefused and the clause on one -# line. +# $GateRefusal too, and the engine goes on to start after either. Test-GateRefusalLine requires +# $StartRefused and the clause on one line. $StartRefused = "refusing to start" # The openings of the gate's lines that let a start go on, at least these three in app.py: the # skipped-gate warning, the warn-mode line, and the warning that the gate could not run (a Vault @@ -331,9 +323,8 @@ function Get-LogMatches { function Get-Attribution { <# - One sentence for a red: which trio files carry no read+write grant for $Identity, whether the - log shows an open failure, and the trio itself. Stated as readings so a red caused by something - other than file access (a start gate, a port) is not dressed up as one. + The attribution text a red appends: which trio files carry no read+write grant for $Identity, + whether the log or $Output has an open-failure line, and the trio itself. #> param([string]$Identity, [string[]]$IdentitySids, [string]$Output) $trio = Get-TrioReport -Identity $Identity -IdentitySids $IdentitySids @@ -341,8 +332,8 @@ function Get-Attribution { else { "Every present trio file grants $Identity read+write, so the cause is probably NOT file access; read the output below." } $hits = @(Get-LogMatches $OpenErrorPattern | Select-Object -First 3 | ForEach-Object { Get-Excerpt $_.Line 240 }) if ($Output -and $Output -match $OpenErrorPattern) { $hits += "probe output: " + (Get-Excerpt $Output 240) } - # The KIND leads the message, so a red that is not shown to be file access cannot be read as the - # lockout Wave 0 is looking for: a refused start gate or a held port also fails to serve. + # The KIND opens this attribution text, which the caller may put after its own words. Without + # an open-failure line the tag says so: a refused start gate or a held port also fails to serve. $kind = if ($hits.Count -gt 0) { "[STORE ACCESS]" } else { "[NOT SHOWN TO BE STORE ACCESS]" } $log = if ($hits.Count -gt 0) { "Open-failure evidence: $($hits -join ' || ')." } else { "No open-failure line was found in the service log or the probe output." } @@ -361,10 +352,11 @@ function Show-Trio([string]$Label) { function Test-Resecured { <# After an identity's open, is the store still CONFINED -- the property ADR 0163's restriction - exists for? Every present trio file must block inheritance, be OWNED by, and grant only, SYSTEM, - BUILTIN\Administrators, the service account or the operator. Anything else is a RED: an - outside entry makes the store readable by that principal; an outside owner keeps WRITE_DAC and - can grant itself access; an inheriting file takes whatever the directory is later given. + exists for? A present trio file whose DACL it reads is a RED if it inherits, is OWNED outside, + or grants an Allow entry outside, SYSTEM, BUILTIN\Administrators, the service account and the + operator: an outside entry makes the store readable by that principal; an outside owner keeps + WRITE_DAC and can grant itself access; an inheriting file takes whatever the directory is later + given. A DACL it cannot read is a CONDITION. This replaced a check on the MECHANISM ("the .db still inherits", "the DACL did not change"), which encoded the pre-0b rewrite-to-the-opener design and would have flagged Wave 0b's @@ -699,10 +691,10 @@ function Add-FirstStartFailure { As in the control and restart branches, it gives the store-access attribution unless the wait saw one of the gate's let-start lines ($GateLetStart). #> - $what = if ($script:GateLetStartSeen) { "created $DbPath and reached the ADR 0167 gate, which let the start go on (the READING line quotes it) rather than refusing it," } + $what = if ($script:GateLetStartSeen) { "logged a line of the ADR 0167 gate letting the start go on (the READING line quotes it), where a refusal was expected," } elseif (Test-Path -LiteralPath $DbPath) { "created $DbPath but did not log the expected refusal line" } else { "never created $DbPath" } - $cause = if ($script:GateLetStartSeen) { "The service's access is not in question." } + $cause = if ($script:GateLetStartSeen) { "No store-access attribution is given for a start the gate let go on." } else { Get-Attribution $ServiceIdentity (Get-ServiceSids) "" } Add-Failure ("$ServiceIdentity $what within $WaitSeconds s on a fresh store. " + $cause) Show-LogTail @@ -1089,28 +1081,26 @@ try { $provisioned = ($r.Code -eq 0) $declined = ($r.Text -match [regex]::Escape($ProvisionDecline)) if (-not ($provisioned -or $declined)) { - Add-Failure ("$Operator could not open $DbPath, which $ServiceIdentity created and " + - "secured, through provision-admin's open path (exit $($r.Code)). " + + Add-Failure ("$Operator ran provision-admin on $DbPath, which $ServiceIdentity created, " + + "and it exited $($r.Code) without provisioning or declining, so the operator's " + + "open is unproven. " + (Get-Attribution $Operator (Get-OperatorSids) $r.Text)) - # CONTROL: if the operator's failed run did not open and re-secure the store, a service - # that cannot reopen even its own store means the first open locked out EVERY identity, - # not only the operator. That separates "restricted to the service" from "restricted - # to nobody usable". + # CONTROL: restart the service on the store it created and report whether it logs the + # refusal again. The trio listing above shows what the operator's failed run left. Start-TheService "reopen of its own store (control)" $own = Wait-ForGateRefusal "the control reopen" Stop-TheService if ($own) { - Add-Reading "control: $ServiceIdentity reopened the store it created, so the store is restricted to the service and not to nobody" + Add-Reading "control: $ServiceIdentity, restarted on the store it created, logged the expected refusal, so it reopened that store" } elseif ($script:GateLetStartSeen) { - Add-Failure ("control: $ServiceIdentity reopened the store it created and reached " + - "the ADR 0167 gate, which let the start go on (the reading above), so this " + - "control expected a refusal it did not get; the service's access is not in question.") + Add-Failure ("control: $ServiceIdentity, restarted on the store it created, logged a " + + "line of the ADR 0167 gate letting the start go on (the reading above), where " + + "this control expected a refusal. No store-access attribution is given for it.") Show-LogTail } else { Add-Failure ("control: $ServiceIdentity, restarted on the store it created itself, " + - "did not log the expected refusal line in $WaitSeconds s. If the cause is store " + - "access, and the operator's failed run did not open and re-secure the store, " + - "its own first open locked out every identity. " + + "did not log the expected refusal line in $WaitSeconds s. This run cannot tell " + + "whether the service's first open or the operator's failed run left the store so. " + (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) Show-LogTail } @@ -1119,12 +1109,12 @@ try { else { "and provision-admin declined on an existing Administrator (the pre-Wave 2 bootstrap shape)" } Add-Reading "$Operator opened $DbPath $how" Test-Resecured -Identity $Operator -Output $r.Text - # 3. The service starts again on the store the operator's open re-secured. + # 3. The service starts again on the store after the operator's open. Start-TheService "restart" if ($provisioned) { # An addressed Administrator now exists, so the gate passes: prove the open by serving. $failed = $Failures.Count - Test-ServiceServes "the restart" "$Operator's open re-secured" + Test-ServiceServes "the restart" "$Operator provisioned" Stop-TheService if ($Failures.Count -eq $failed) { Test-Resecured -Identity $ServiceIdentity -FromServiceLog @@ -1133,13 +1123,14 @@ try { $reopened = Wait-ForGateRefusal "the restart" Stop-TheService if (-not $reopened -and $script:GateLetStartSeen) { - Add-Failure ("$ServiceIdentity opened $DbPath after $Operator's open and reached " + - "the ADR 0167 gate, which let the start go on (the reading above), so the " + - "restart expected a refusal it did not get; the service's access is not in question.") + Add-Failure ("$ServiceIdentity, restarted on $DbPath after $Operator's open, " + + "logged a line of the ADR 0167 gate letting the start go on (the reading " + + "above), where the restart expected a refusal. No store-access attribution " + + "is given for it.") Show-LogTail } elseif (-not $reopened) { - Add-Failure ("$ServiceIdentity, restarted on $DbPath after $Operator's open " + - "re-secured it, did not log the expected refusal line in $WaitSeconds s. " + + Add-Failure ("$ServiceIdentity, restarted on $DbPath after $Operator's open, " + + "did not log the expected refusal line in $WaitSeconds s. " + (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) Show-LogTail } else { @@ -1183,7 +1174,7 @@ if ($Conditions.Count -gt 0) { Write-Host "::warning title=ADR 0183 Wave 0 ($Order) GREEN (CONDITIONAL)::$(Format-Annotation $why)" Write-Host "GREEN (CONDITIONAL) [$Order]: $why" } else { - Write-Host "GREEN [$Order]: both identities opened the store in this order, and it stayed confined after each open." + Write-Host "GREEN [$Order]: both identities opened the store in this order, and no confinement check went red." } # The Actions wrapper exits with $LASTEXITCODE, which the last native command (nssm, icacls) set. exit 0 From 755fd492251acc49b78c4b53ad6032019b666341 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 14:59:54 -0600 Subject: [PATCH 17/19] measure-store-access: shrink the round 3e0ecfeb prose findings Prose and program output only; what the script decides is unchanged. The control timeout red no longer names two causes for a timeout that has others. Wait-ForGateRefusal's comment says at least one gate outcome is unrecognised, not exactly one. The test docstring says the app.py pin is a substring check on prefixes and source fragments. The ci.yml comment points at the helper's header instead of claiming every red carries a trio reading and every open a confinement check. BACKLOG #5658 Proposed PR title: measure-store-access counts only the ADR 0167 refusal as a refused start (BACKLOG #5658) Proposed ledger banner: SHIPPED -- the wait counts a line only with both the refusal words and the clause; a let-start line ends it with a reading and no store-access attribution. --- .github/workflows/ci.yml | 10 ++++------ scripts/service/measure-store-access.ps1 | 11 +++++------ tests/test_measure_store_access_gate_refusal.py | 7 ++++--- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 156dc71024..9befc183d9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6293,12 +6293,10 @@ jobs: # Each arm installs the service under the DEFAULT virtual account (NT SERVICE\MessageFoundry), # with sign-in required, on its own fresh data directory, and uninstalls it afterwards. The helper # asserts the SCM run-as account and the owner of each sampled service process, so an arm that - # silently ran as some other account goes red instead of measuring the wrong thing. Every red - # names the identity and the trio file(s) granting it no read+write, and is tagged [STORE ACCESS] - # only when a log shows an open failure -- [NOT SHOWN TO BE STORE ACCESS] otherwise, so a refused - # start gate or a held port is not read as the lockout. After every open each trio file may grant only - # SYSTEM, Administrators, the service account and the operator, or the arm goes red. A restriction - # the engine logged as failed prints GREEN (CONDITIONAL). The helper's header says how each arm reds. + # silently ran as some other account goes red instead of measuring the wrong thing. Which reds + # carry a trio reading and a [STORE ACCESS] tag, and which opens have their confinement checked, + # is in the helper's header (WHAT A RED MEANS, CONFINEMENT); not every red, and not every open. + # A restriction the engine logged as failed prints GREEN (CONDITIONAL). # # `!cancelled()` rather than the default `success()`: both readings are wanted on every run, so # the start-first arm still runs when the provision-first arm or the service smoke went red. diff --git a/scripts/service/measure-store-access.ps1 b/scripts/service/measure-store-access.ps1 index 744dac26f4..b3a052d319 100644 --- a/scripts/service/measure-store-access.ps1 +++ b/scripts/service/measure-store-access.ps1 @@ -659,10 +659,10 @@ function Wait-ForGateRefusal([string]$Phase) { let-it-start lines ($GateLetStart) is logged. Any other line is waited past. #> # Read by callers after a $false, so a failure text does not name a lockout when the gate logged - # a let-start line. One gate path is NOT recognised: the Vault-transit integrity-seal refusal - # ("refusing to start a PHI instance: the accounts could not be checked against their integrity - # seal ...") carries $StartRefused but not $GateRefusal, and opens with no $GateLetStart prefix. - # On that path the wait times out, and the caller prints its timeout text and the store-access + # a let-start line. Not every gate outcome is recognised. At least the Vault-transit + # integrity-seal refusal ("refusing to start a PHI instance: the accounts could not be checked + # against their integrity seal ...") carries $StartRefused but not $GateRefusal, and opens with no + # $GateLetStart prefix. On such a path the wait times out, and the caller prints its timeout text and the store-access # attribution (Get-Attribution) rather than naming the gate. $script:GateLetStartSeen = $false $deadline = (Get-Date).AddSeconds($WaitSeconds) @@ -1099,8 +1099,7 @@ try { Show-LogTail } else { Add-Failure ("control: $ServiceIdentity, restarted on the store it created itself, " + - "did not log the expected refusal line in $WaitSeconds s. This run cannot tell " + - "whether the service's first open or the operator's failed run left the store so. " + + "did not log the expected refusal line in $WaitSeconds s. " + (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) Show-LogTail } diff --git a/tests/test_measure_store_access_gate_refusal.py b/tests/test_measure_store_access_gate_refusal.py index f29d5035a1..2321f15877 100644 --- a/tests/test_measure_store_access_gate_refusal.py +++ b/tests/test_measure_store_access_gate_refusal.py @@ -11,9 +11,10 @@ NOTHING HERE RUNS THE SCRIPT: it installs a service and must run elevated, which only the hosted windows-service-smoke leg does. The tests lift the phrase assignments and functions they need out of the script's AST: they call ``Test-GateRefusalLine`` on lines worded as the engine words them, -and run ``Wait-ForGateRefusal`` against a stand-in log reader. They pin, as literals, that ``app.py`` -still holds each line's prefix and the no-Administrator and no-address detail texts; they do not pin -``$ProvisionHint`` or ``$ProvisionDecline``, and they do not render the lines from ``app.py``. Each +and run ``Wait-ForGateRefusal`` against a stand-in log reader. A substring check finds each line's +prefix, and source fragments of the no-Administrator and no-address detail texts, somewhere in +``app.py``; it does not render the lines, so it does not prove the refusal still puts the clause and +"refusing to start" on one line. Each case that runs PowerShell needs its own host, ``pwsh`` or ``powershell``, and skips where that host is missing; the ``app.py`` pin and the source-text test need neither. """ From f87f576f4d80131d3be39e1f0624ff5deadce540 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 15:31:18 -0600 Subject: [PATCH 18/19] measure-store-access: cut the round bfed771b prose findings Prose and program output only; what the script decides is unchanged. The ci.yml comment no longer describes reds, confinement or GREEN (CONDITIONAL); one line names the helper's header. The test docstring says again that nothing pins $ProvisionHint or $ProvisionDecline. The Wait-ForGateRefusal comment says the seal refusal contains no $GateLetStart prefix, since the match is anywhere in the line. The control timeout red says its trio reading was taken after the control reopen. BACKLOG #5658 Proposed PR title: measure-store-access counts only the ADR 0167 refusal as a refused start (BACKLOG #5658) Proposed ledger banner: SHIPPED -- the wait counts a line only with both the refusal words and the clause; a let-start line ends it with a reading and no store-access attribution. --- .github/workflows/ci.yml | 6 ++---- scripts/service/measure-store-access.ps1 | 5 +++-- tests/test_measure_store_access_gate_refusal.py | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9befc183d9..2a8f9f2c42 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6293,10 +6293,8 @@ jobs: # Each arm installs the service under the DEFAULT virtual account (NT SERVICE\MessageFoundry), # with sign-in required, on its own fresh data directory, and uninstalls it afterwards. The helper # asserts the SCM run-as account and the owner of each sampled service process, so an arm that - # silently ran as some other account goes red instead of measuring the wrong thing. Which reds - # carry a trio reading and a [STORE ACCESS] tag, and which opens have their confinement checked, - # is in the helper's header (WHAT A RED MEANS, CONFINEMENT); not every red, and not every open. - # A restriction the engine logged as failed prints GREEN (CONDITIONAL). + # silently ran as some other account goes red instead of measuring the wrong thing. + # What a red or a GREEN means: the helper's header. # # `!cancelled()` rather than the default `success()`: both readings are wanted on every run, so # the start-first arm still runs when the provision-first arm or the service smoke went red. diff --git a/scripts/service/measure-store-access.ps1 b/scripts/service/measure-store-access.ps1 index b3a052d319..206037435c 100644 --- a/scripts/service/measure-store-access.ps1 +++ b/scripts/service/measure-store-access.ps1 @@ -661,7 +661,7 @@ function Wait-ForGateRefusal([string]$Phase) { # Read by callers after a $false, so a failure text does not name a lockout when the gate logged # a let-start line. Not every gate outcome is recognised. At least the Vault-transit # integrity-seal refusal ("refusing to start a PHI instance: the accounts could not be checked - # against their integrity seal ...") carries $StartRefused but not $GateRefusal, and opens with no + # against their integrity seal ...") carries $StartRefused but not $GateRefusal, and contains no # $GateLetStart prefix. On such a path the wait times out, and the caller prints its timeout text and the store-access # attribution (Get-Attribution) rather than naming the gate. $script:GateLetStartSeen = $false @@ -1099,7 +1099,8 @@ try { Show-LogTail } else { Add-Failure ("control: $ServiceIdentity, restarted on the store it created itself, " + - "did not log the expected refusal line in $WaitSeconds s. " + + "did not log the expected refusal line in $WaitSeconds s. The trio reading " + + "that follows was taken after the control reopen. " + (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) Show-LogTail } diff --git a/tests/test_measure_store_access_gate_refusal.py b/tests/test_measure_store_access_gate_refusal.py index 2321f15877..f62c3d0337 100644 --- a/tests/test_measure_store_access_gate_refusal.py +++ b/tests/test_measure_store_access_gate_refusal.py @@ -14,7 +14,7 @@ and run ``Wait-ForGateRefusal`` against a stand-in log reader. A substring check finds each line's prefix, and source fragments of the no-Administrator and no-address detail texts, somewhere in ``app.py``; it does not render the lines, so it does not prove the refusal still puts the clause and -"refusing to start" on one line. Each +"refusing to start" on one line. Nothing here pins ``$ProvisionHint`` or ``$ProvisionDecline``. Each case that runs PowerShell needs its own host, ``pwsh`` or ``powershell``, and skips where that host is missing; the ``app.py`` pin and the source-text test need neither. """ From 539578290c84d95c6407775d8fa543b3aa132885 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 15:54:31 -0600 Subject: [PATCH 19/19] measure-store-access: delete three prose claims the review found Deletions only, plus "at least" in one docstring; what the script decides is unchanged. The control timeout red no longer says when its trio reading was taken. The ci.yml pointer says only that the helper's header says what a red means. The test docstring says "at least" before the variables nothing pins. BACKLOG #5658 Proposed PR title: measure-store-access counts only the ADR 0167 refusal as a refused start (BACKLOG #5658) Proposed ledger banner: SHIPPED -- the wait counts a line only with both the refusal words and the clause; a let-start line ends it with a reading and no store-access attribution. --- .github/workflows/ci.yml | 2 +- scripts/service/measure-store-access.ps1 | 3 +-- tests/test_measure_store_access_gate_refusal.py | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2a8f9f2c42..31054e4140 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6294,7 +6294,7 @@ jobs: # with sign-in required, on its own fresh data directory, and uninstalls it afterwards. The helper # asserts the SCM run-as account and the owner of each sampled service process, so an arm that # silently ran as some other account goes red instead of measuring the wrong thing. - # What a red or a GREEN means: the helper's header. + # What a red means: the helper's header. # # `!cancelled()` rather than the default `success()`: both readings are wanted on every run, so # the start-first arm still runs when the provision-first arm or the service smoke went red. diff --git a/scripts/service/measure-store-access.ps1 b/scripts/service/measure-store-access.ps1 index 206037435c..1aaca00992 100644 --- a/scripts/service/measure-store-access.ps1 +++ b/scripts/service/measure-store-access.ps1 @@ -1099,8 +1099,7 @@ try { Show-LogTail } else { Add-Failure ("control: $ServiceIdentity, restarted on the store it created itself, " + - "did not log the expected refusal line in $WaitSeconds s. The trio reading " + - "that follows was taken after the control reopen. " + + "did not log the expected refusal line in $WaitSeconds s. " + (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) Show-LogTail } diff --git a/tests/test_measure_store_access_gate_refusal.py b/tests/test_measure_store_access_gate_refusal.py index f62c3d0337..9ef05be433 100644 --- a/tests/test_measure_store_access_gate_refusal.py +++ b/tests/test_measure_store_access_gate_refusal.py @@ -14,7 +14,7 @@ and run ``Wait-ForGateRefusal`` against a stand-in log reader. A substring check finds each line's prefix, and source fragments of the no-Administrator and no-address detail texts, somewhere in ``app.py``; it does not render the lines, so it does not prove the refusal still puts the clause and -"refusing to start" on one line. Nothing here pins ``$ProvisionHint`` or ``$ProvisionDecline``. Each +"refusing to start" on one line. Nothing here pins at least ``$ProvisionHint`` or ``$ProvisionDecline``. Each case that runs PowerShell needs its own host, ``pwsh`` or ``powershell``, and skips where that host is missing; the ``app.py`` pin and the source-text test need neither. """