param([string]$SigningDirectory = '') $ErrorActionPreference = 'Stop' $taskApp = Split-Path $PSScriptRoot -Parent $taskRepo = Split-Path $taskApp -Parent if (!$SigningDirectory) { $SigningDirectory = Join-Path $taskApp 'keys/production' } $taskCredentialPath = Join-Path $SigningDirectory 'credentials.dpapi' $taskKeyStore = Join-Path $SigningDirectory 'cmvr-production.p12' $taskCertificate = Join-Path $taskApp 'certs/prod.pem' function Save-TaskMachineRecovery([string]$taskPlainJson) { $taskProtected = [System.Security.Cryptography.ProtectedData]::Protect([Text.Encoding]::UTF8.GetBytes($taskPlainJson), $null, [System.Security.Cryptography.DataProtectionScope]::LocalMachine) [IO.File]::WriteAllBytes((Join-Path $SigningDirectory 'credentials.machine.dpapi'), $taskProtected) & icacls.exe $SigningDirectory '/grant' '*S-1-5-32-544:(OI)(CI)F' | Out-Null if ($LASTEXITCODE) { throw 'Cannot allow local administrators to back up signing material' } } if (Test-Path -LiteralPath $taskCredentialPath) { foreach ($taskRequiredFile in @($taskKeyStore, $taskCertificate, (Join-Path $SigningDirectory 'ota-private-key.pem'), (Join-Path $SigningDirectory 'ready.json'))) { if (!(Test-Path -LiteralPath $taskRequiredFile)) { throw 'Partial signing initialization found. Preserve existing keys; inspect before recovery.' } } if (!(Test-Path -LiteralPath (Join-Path $SigningDirectory 'credentials.machine.dpapi'))) { $taskExistingSecret = (Get-Content -LiteralPath $taskCredentialPath -Raw).Trim() | ConvertTo-SecureString $taskExistingPtr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($taskExistingSecret) try { Save-TaskMachineRecovery ([Runtime.InteropServices.Marshal]::PtrToStringBSTR($taskExistingPtr)) } finally { [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($taskExistingPtr) } } Write-Output 'Existing production identity preserved.' return } if ((Test-Path -LiteralPath $SigningDirectory) -or (Test-Path -LiteralPath $taskCertificate)) { throw 'Refusing to overwrite existing production signing material.' } if (!$env:JAVA_HOME) { throw 'JAVA_HOME required' } New-Item -ItemType Directory -Path $SigningDirectory | Out-Null # Restrict this directory to the current Windows identity; secrets are additionally encrypted. $taskIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name & icacls.exe $SigningDirectory '/inheritance:r' '/grant:r' "${taskIdentity}:(OI)(CI)F" | Out-Null if ($LASTEXITCODE) { throw 'Cannot restrict signing directory permissions' } $taskRandom = [System.Security.Cryptography.RandomNumberGenerator]::Create() function New-TaskSecret { $taskBytes = New-Object byte[] 48 $taskRandom.GetBytes($taskBytes) return [Convert]::ToBase64String($taskBytes) } $taskSecrets = @{ storePassword = New-TaskSecret; otaPassword = New-TaskSecret; alias = 'cmvr-production' } $taskJson = $taskSecrets | ConvertTo-Json -Compress $taskSecure = ConvertTo-SecureString -String $taskJson -AsPlainText -Force $taskSecure | ConvertFrom-SecureString | Set-Content -LiteralPath $taskCredentialPath -Encoding ASCII Save-TaskMachineRecovery $taskJson try { $env:CMVR_STORE_PASSWORD = $taskSecrets.storePassword $env:CMVR_OTA_PASSWORD = $taskSecrets.otaPassword & (Join-Path $env:JAVA_HOME 'bin/keytool.exe') '-genkeypair' '-keystore' $taskKeyStore '-storetype' 'PKCS12' '-alias' $taskSecrets.alias '-keyalg' 'RSA' '-keysize' '3072' '-sigalg' 'SHA256withRSA' '-validity' '10000' '-dname' 'CN=CMVR Production Android, OU=Mobile, O=CMVR, C=CN' '-storepass:env' 'CMVR_STORE_PASSWORD' '-keypass:env' 'CMVR_STORE_PASSWORD' if ($LASTEXITCODE) { throw 'Production Android key generation failed' } & node.exe (Join-Path $PSScriptRoot 'create-production-ota-key.mjs') $SigningDirectory if ($LASTEXITCODE) { throw 'Production OTA key generation failed' } & (Join-Path $env:JAVA_HOME 'bin/keytool.exe') '-exportcert' '-rfc' '-keystore' $taskKeyStore '-alias' $taskSecrets.alias '-storepass:env' 'CMVR_STORE_PASSWORD' '-file' (Join-Path $SigningDirectory 'android-certificate.pem') if ($LASTEXITCODE) { throw 'Android public certificate export failed' } @{ createdAt = [DateTime]::UtcNow.ToString('o'); alias = $taskSecrets.alias; passwordProtection = 'Windows DPAPI CurrentUser and machine-bound recovery'; package = 'cn.cmvr.wms' } | ConvertTo-Json | Set-Content -LiteralPath (Join-Path $SigningDirectory 'ready.json') -Encoding UTF8 Write-Output "Production signing identity created in $SigningDirectory. Preserve this directory and the Windows user profile." } finally { Remove-Item Env:CMVR_STORE_PASSWORD -ErrorAction SilentlyContinue Remove-Item Env:CMVR_OTA_PASSWORD -ErrorAction SilentlyContinue $taskJson = $null; $taskSecrets = $null; $taskRandom.Dispose() }