#requires -Version 5.1 #requires -RunAsAdministrator # Publish the next trusted-OS profile from actual passive service observations. [CmdletBinding()] param([Parameter(Mandatory)][ValidateSet('Prepare','Publish','Read','FinishStarts')][string]$Action) $ErrorActionPreference='Stop' $commonHash='c54e36b32a2fcccb072f2cca52a38b6c4da31755d86205599016c5d180f5cec4' $common=Join-Path $PSScriptRoot 'Set-PassiveJobPair.ps1' if ((Get-FileHash -LiteralPath $common -Algorithm SHA256).Hash -ne $commonHash) { throw 'The reviewed passive-pair controller must be next to this script' } $text=[Text.Encoding]::UTF8.GetString([IO.File]::ReadAllBytes($common)) $first="`n`$originalCase = " $last="`nif (`$Mode -eq 'Read') {" $i=$text.IndexOf($first,[StringComparison]::Ordinal) $j=$text.IndexOf($last,[StringComparison]::Ordinal) if ($i -lt 0 -or $j -le $i -or $i -ne $text.LastIndexOf($first,[StringComparison]::Ordinal) -or $j -ne $text.LastIndexOf($last,[StringComparison]::Ordinal)) { throw 'Passive-pair definition boundary' } # Fixed declarations and functions only. Its Prepare, Install and Restore bodies are excluded. . ([scriptblock]::Create($text.Substring($i,$j-$i))) CheckControl $work $true $activation=(Marker 'profile-publish-intent') $ready=(Marker 'profile-prepared') $success=(Marker 'profile-published') function RequireNewPassivePair { if ((Hash $config) -cne $configHash -or (Hash $helperBinary) -cne $helperHash -or (Test-Path -LiteralPath (Marker 'restore-intent'))) { throw 'Passive successor pair required' } VerifyFiles (CurrentFiles) $newHashes } function RoleDigests($platformHash,$installedHash,$independentHash,$previous) { $out=@{} foreach ($name in @('agent','system_helper')) { $bound=$installed.agent if ($name -ceq 'system_helper') { $bound=$installed.helper } $old=@($previous.trusted_os_active_guest.qualifications | Where-Object { $_.role -ceq $name }) if ($old.Count -ne 1) { throw 'Expected one predecessor witness per role' } $archive=Join-Path $oldFolder ($name+'-qualification.json') CheckControl $archive $false if ((Hash $archive) -cne $old[0].qualification_sha256) { throw 'Original role provenance changed' } $archived=Get-Content -LiteralPath $archive -Raw | ConvertFrom-Json $build='a003dfa8cc2397a99933aceeb2d8dab51f883ab9cdf27876fbfc94a78873044c' if ($name -ceq 'system_helper') { # The old pair receipt linked the later agent and referenced the # separately built helper. Identify the actual helper link itself. $build='04ed34e6e850aca77c1fd4677ddb271f7ccd9492a97f5d4e00ead4c1f032f29d' } Record ('profile-role-'+$name) ([ordered]@{ profile='rmm.windows.trusted-os.product-role-prerequisite.v1';role=$name binary_sha256=$bound.image_file.sha256; service=$bound.service; kernel=$bound.kernel image_file=$bound.image_file; predecessor_qualification_sha256=$old[0].qualification_sha256 pair_case_id=$id; pair_result_sha256=$installedHash; independent_pair_readback_sha256=$independentHash product_build_receipt_sha256=$build; platform_provenance_sha256=$platformHash raw_capture=$archived.raw_capture; signed_runtime_verified=$false; physical_calibration_claimed=$false }) $out[$name]=Hash (Marker ('profile-role-'+$name)) } return $out } function BuildSuccessor { $installedHash=Hash (Marker 'installed') $unit=Join-Path $boot 'profile-unit-fcec400-result.json' if ((Hash $unit) -cne '48de474d842ebc87550ba2e27b66086e6aa573973f252f4b8e19ac7b12b75a63') { throw 'Windows unit evidence changed' } $independent=[ordered]@{agent=(LiveRole 'agent' $newHashes.agent);helper=(LiveRole 'helper' $helperHash);files=(CurrentFiles);device=(PreservedDevice)} foreach ($field in $independent.Keys) { if (!(JsonEqual $installed.$field $independent.$field)) { throw "Independent native pair read differs: $field" } } $independent.utc=[DateTime]::UtcNow.ToString('o') $independent.pair_case=$id Record 'profile-independent-native-read' $independent $independentHash=Hash (Marker 'profile-independent-native-read') $old=Get-Content -LiteralPath $target -Raw | ConvertFrom-Json if ($old.schema_version -ne 2 -or $old.key_generation -ne 1 -or $old.server_instance -cne 'c6f9657a-446f-49af-8fb8-bd8932939568' -or $old.trusted_os_active_guest.operating_assumption -cne 'active_guest_no_pause_save_restore_snapshot_migration_host_suspend' -or $old.trusted_os_active_guest.platform -cne 'kvm_qemu_hypervclock' -or @($old.trusted_os_active_guest.qualifications).Count -ne 2) { throw 'Unexpected original V2 contract' } $platform=Join-Path $oldFolder 'platform-provenance.json' CheckControl $platform $false $oldPlatformHash=Hash $platform if ($oldPlatformHash -cne $old.trusted_os_active_guest.platform_provenance_sha256) { throw 'Original platform provenance changed' } Record 'profile-platform' ([ordered]@{ profile='rmm.windows.trusted-os.platform-provenance.v1';operating_assumption=$old.trusted_os_active_guest.operating_assumption platform=$old.trusted_os_active_guest.platform;previous_platform_provenance_sha256=$oldPlatformHash predecessor_profile_sha256=$profileHash;pair_case_id=$id;pair_result_sha256=$installedHash independent_pair_readback_sha256=$independentHash;independent_physical_calibration_claimed=$false agent_build_receipt_sha256='a003dfa8cc2397a99933aceeb2d8dab51f883ab9cdf27876fbfc94a78873044c' linux_profile_unit_sha256='ee3a2c99651491a2fd876f1a3463919dc32d9509d9f9ef4eb73a36aa848bb56d' windows_profile_unit_sha256=(Hash $unit) }) $platformHash=Hash (Marker 'profile-platform') $roles=RoleDigests $platformHash $installedHash $independentHash $old $profileText=[Text.Encoding]::UTF8.GetString([IO.File]::ReadAllBytes($target)) $changes=[ordered]@{} $changes[$oldHashes.agent]=$newHashes.agent $changes[$oldPlatformHash]=$platformHash foreach ($r in $old.trusted_os_active_guest.qualifications) { $changes[$r.qualification_sha256]=$roles[$r.role] } if ($changes.Count -ne 4) { throw 'Expected four independent substitutions' } foreach ($oldValue in $changes.Keys) { $pos=$profileText.IndexOf($oldValue,[StringComparison]::Ordinal) if ($pos -lt 0 -or $pos -ne $profileText.LastIndexOf($oldValue,[StringComparison]::Ordinal)) { throw 'Cannot uniquely bind successor' } $profileText=$profileText.Replace($oldValue,$changes[$oldValue]) } $updated=$profileText | ConvertFrom-Json if ($updated.key_generation -ne $old.key_generation -or $updated.server_instance -cne $old.server_instance -or !(JsonEqual $old.binding $updated.binding) -or !(JsonEqual $old.public_keys_hex $updated.public_keys_hex)) { throw 'Unexpected trust-root changes' } $draft=Join-Path $work 'new-time-profile' WriteBytes $draft ([Text.UTF8Encoding]::new($false).GetBytes($profileText)) CheckControl $draft $false $oldCopy=Join-Path $work 'old-time-profile' WriteBytes $oldCopy ([IO.File]::ReadAllBytes($target)) if ((Hash $oldCopy) -cne $profileHash) { throw 'Time profile predecessor changed' } Record 'profile-prepared' ([ordered]@{ id=$id;utc=[DateTime]::UtcNow.ToString('o');predecessor=(FileIdentity $target);successor_sha256=(Hash $draft) source_sha256=$installedHash;independent_sha256=$independentHash;platform_sha256=$platformHash;role_sha256=$roles original_publish_result_sha256='62a04be9c7d08d3687b51eacd6adbe290b91e16ba934d6b5bb531e8764a56cf5' predecessor_revision=1;successor_revision=2;signing_key_generation=$old.key_generation;clock_qualified=$false }) } function CheckPrepared { if ($prepared.id -cne $id -or $prepared.predecessor.sha256 -cne $profileHash -or $prepared.successor_revision -ne 2 -or $prepared.predecessor_revision -ne 1 -or $prepared.signing_key_generation -ne 1 -or $prepared.source_sha256 -cne (Hash (Marker 'installed')) -or $prepared.independent_sha256 -cne (Hash (Marker 'profile-independent-native-read')) -or $prepared.platform_sha256 -cne (Hash (Marker 'profile-platform')) -or $prepared.successor_sha256 -cne (Hash (Join-Path $work 'new-time-profile')) -or $prepared.successor_sha256 -ceq $profileHash -or (Hash (Join-Path $work 'old-time-profile')) -cne $profileHash) { throw 'Successor preparation inconsistent' } foreach ($name in @('agent','system_helper')) { if ($prepared.role_sha256.$name -cne (Hash (Marker ('profile-role-'+$name)))) { throw 'Role proof changed' } } } function FinishPublication { $intent=LoadRecord 'profile-publish-intent' $actual=FileIdentity $target if ($intent.id -cne $id -or $intent.prepared_sha256 -cne (Hash $ready) -or $actual.sha256 -cne $prepared.successor_sha256 -or $actual.acl_sha256 -cne $prepared.predecessor.acl_sha256) { throw 'Successor readback failed' } if (Test-Path -LiteralPath (Marker 'time-installed-time')) { if (!(JsonEqual (LoadRecord 'time-installed-time') $actual)) { throw 'Installed profile identity changed' } } else { # An interrupted Replace can finish before the final journal write. # Recognize the recorded candidate and native backup without another Replace. StopCheck $attempt=LoadRecord 'time-replace-time' $previous=FileIdentity (Join-Path $work 'time-replaced-time') if (!(JsonEqual $attempt.previous $prepared.predecessor) -or $previous.sha256 -cne $prepared.predecessor.sha256 -or $previous.file_id -cne $prepared.predecessor.file_id -or $actual.file_id -cne $attempt.candidate.file_id -or $actual.volume_serial -cne $attempt.candidate.volume_serial) { throw 'Unconfirmed profile replacement: inspect current state' } Record 'time-installed-time' $actual } if (Test-Path -LiteralPath $success) { throw 'Already published: use Read' } foreach ($role in @('helper','agent')) { $hash=$newHashes.agent;$name=$agentName if ($role -eq 'helper') { $hash=$helperHash;$name=$helperName } $s=Service $role if ($s.state -ceq 'Stopped') { if (Test-Path -LiteralPath (Marker ('time-start-'+$role))) { throw "Existing unconfirmed $role start; read current state" } StartKnownRole $role $name $hash 'time' } elseif ($s.state -ceq 'Running') { if (!(Test-Path -LiteralPath (Marker ('time-start-'+$role)))) { throw "Unexpected independently started $role" } $null=LiveRole $role $hash } else { throw "Unexpected state $role" } } Record 'profile-published' ([ordered]@{ id=$id;utc=[DateTime]::UtcNow.ToString('o');configuration=(FileIdentity $target) predecessor=$prepared.predecessor; successor_revision=2; signing_key_generation=1 role_proofs=$prepared.role_sha256; publication_since=$intent.utc agent=(LiveRole 'agent' $newHashes.agent); helper=(LiveRole 'helper' $helperHash) signed_runtime_verified=$false; job_mode=$false;privileged_effects=$false }) Write-Output "Published successor profile $id" } if ($Action -ceq 'Read') { $o=[ordered]@{id=$id;read_only=$true;configuration=(FileIdentity $target);agent=(Service 'agent');helper=(Service 'helper');marks=@()} $o.marks=@(Get-ChildItem -LiteralPath $work -Filter 'profile-*.json' -File | Select-Object -ExpandProperty Name) if (Test-Path -LiteralPath $success) { $o.published=LoadRecord 'profile-published' } $o|ConvertTo-Json -Depth 15 -Compress return } RequireNewPassivePair $installed=LoadRecord 'installed' if ($installed.id -cne $id -or $installed.job_mode -ne $false -or $installed.clock_qualified -ne $false) { throw 'Expected passive installation' } if ($Action -ceq 'Prepare') { VerifyPassiveContext if (Test-Path -LiteralPath $activation) { throw 'Already began publication' } BuildSuccessor Write-Output "Prepared successor profile from independent passive service proofs $id" return } $prepared=LoadRecord 'profile-prepared' CheckPrepared if ($Action -ceq 'FinishStarts') { FinishPublication;return } VerifyPassiveContext $held=LoadRecord 'profile-independent-native-read' if (!(JsonEqual $held.agent (LiveRole 'agent' $newHashes.agent)) -or !(JsonEqual $held.helper (LiveRole 'helper' $helperHash)) -or !(JsonEqual $held.device (PreservedDevice)) -or !(JsonEqual $prepared.predecessor (FileIdentity $target))) { throw 'Positive, unchanged passive pair and original time profile required before publication' } Record 'profile-publish-intent' ([ordered]@{id=$id;utc=[DateTime]::UtcNow.ToString('o');prepared_sha256=(Hash $ready)}) StopKnownRole 'agent' $agentName $held.agent 'time' StopKnownRole 'helper' $helperName $held.helper 'time' $locations.Add('time',$target) SwapFile 'time' (Join-Path $work 'new-time-profile') $prepared.predecessor $prepared.successor_sha256 'time' $locations.Remove('time') FinishPublication