33using System . Net . Http ;
44using System . Net . Sockets ;
55using System . Runtime . InteropServices ;
6+ using System . Security . Cryptography ;
67using System . Security . Cryptography . X509Certificates ;
78using System . Text ;
89using System . Text . Json ;
@@ -1179,16 +1180,124 @@ private static void ValidateConfValue(Dictionary<string, Dictionary<string, stri
11791180// ═══════════════════════════════════════════════════════════════════
11801181
11811182/// <summary>
1182- /// Exports Windows trusted root/intermediate CA certificates into the WSL
1183+ /// Exports Windows trusted root CA certificates into the WSL
11831184/// distro's system trust store. This resolves curl exit-60 failures that
11841185/// occur on corporate networks where a TLS-intercepting proxy injects a
11851186/// self-signed certificate that the WSL Ubuntu instance does not trust.
11861187/// The step is non-fatal: if the Windows store cannot be read or the WSL
1187- /// filesystem cannot be reached , it logs a warning and continues so that
1188+ /// trust store cannot be updated , it logs a warning and continues so that
11881189/// setups on non-proxied networks are not blocked.
11891190/// </summary>
11901191public sealed class SyncWindowsCaCertsStep : SetupStep
11911192{
1193+ private const string ManagedCertDirectory = "/usr/local/share/ca-certificates/openclaw-windows" ;
1194+ private const string InstallScript = """
1195+ set -euo pipefail
1196+
1197+ target=/usr/local/share/ca-certificates/openclaw-windows
1198+ parent=/usr/local/share/ca-certificates
1199+ backup_parent=/var/lib/openclaw-setup
1200+ backup="$backup_parent/windows-ca-certificates.backup"
1201+ install -d -m 0700 -o root -g root "$backup_parent"
1202+ staging="$(mktemp -d "$parent/.openclaw-windows.XXXXXX")"
1203+ chmod 0755 "$staging"
1204+ old_moved=0
1205+ new_installed=0
1206+
1207+ cleanup() {
1208+ rm -rf -- "$staging"
1209+ if [ "$new_installed" -eq 1 ]; then
1210+ rm -rf -- "$target"
1211+ fi
1212+ if [ "$old_moved" -eq 1 ] && [ -d "$backup" ]; then
1213+ mv -- "$backup" "$target"
1214+ fi
1215+ rm -rf -- "$backup"
1216+ if [ "$old_moved" -eq 1 ] || [ "$new_installed" -eq 1 ]; then
1217+ update-ca-certificates --fresh >/dev/null 2>&1 || true
1218+ fi
1219+ }
1220+ trap cleanup EXIT
1221+
1222+ if [ -d "$backup" ]; then
1223+ rm -rf -- "$target"
1224+ mv -- "$backup" "$target"
1225+ update-ca-certificates --fresh
1226+ fi
1227+
1228+ count=0
1229+ while IFS=$'\t' read -r fingerprint payload; do
1230+ if [ -z "$fingerprint" ] && [ -z "$payload" ]; then
1231+ continue
1232+ fi
1233+ case "$fingerprint" in
1234+ ''|*[!0-9A-Fa-f]*) echo "Invalid certificate fingerprint" >&2; exit 64 ;;
1235+ esac
1236+ if [ "${#fingerprint}" -ne 64 ]; then
1237+ echo "Invalid certificate fingerprint length" >&2
1238+ exit 64
1239+ fi
1240+ case "$payload" in
1241+ ''|*[!A-Za-z0-9+/=]*) echo "Invalid certificate payload" >&2; exit 64 ;;
1242+ esac
1243+
1244+ fingerprint="${fingerprint,,}"
1245+ cert_path="$staging/$fingerprint.crt"
1246+ {
1247+ printf '%s\n' '-----BEGIN CERTIFICATE-----'
1248+ printf '%s\n' "$payload" | fold -w 64
1249+ printf '%s\n' '-----END CERTIFICATE-----'
1250+ } > "$cert_path"
1251+ chmod 0644 "$cert_path"
1252+
1253+ if ! openssl x509 -in "$cert_path" -noout >/dev/null 2>&1; then
1254+ echo "Invalid X.509 certificate for $fingerprint" >&2
1255+ exit 65
1256+ fi
1257+ actual="$(openssl x509 -in "$cert_path" -outform DER | sha256sum | cut -d ' ' -f1)"
1258+ if [ "$actual" != "$fingerprint" ]; then
1259+ echo "Certificate fingerprint mismatch for $fingerprint" >&2
1260+ exit 65
1261+ fi
1262+ count=$((count + 1))
1263+ done
1264+
1265+ if [ "$count" -eq 0 ]; then
1266+ echo "No certificates received" >&2
1267+ exit 64
1268+ fi
1269+
1270+ if [ -d "$target" ]; then
1271+ mv -- "$target" "$backup"
1272+ old_moved=1
1273+ fi
1274+ mv -- "$staging" "$target"
1275+ new_installed=1
1276+ update-ca-certificates --fresh
1277+
1278+ rm -rf -- "$backup"
1279+ old_moved=0
1280+ new_installed=0
1281+ trap - EXIT
1282+ """ ;
1283+ private const string RollbackScript = """
1284+ set -e
1285+ target=/usr/local/share/ca-certificates/openclaw-windows
1286+ backup=/var/lib/openclaw-setup/windows-ca-certificates.backup
1287+ if [ -d "$target" ]; then
1288+ rm -rf -- "$target"
1289+ update-ca-certificates --fresh
1290+ fi
1291+ rm -rf -- "$backup"
1292+ """ ;
1293+
1294+ private readonly Func < WindowsCaCertificateExport > _exportCertificates ;
1295+
1296+ public SyncWindowsCaCertsStep ( ) : this ( ExportWindowsTrustedRoots ) { }
1297+
1298+ internal SyncWindowsCaCertsStep ( Func < WindowsCaCertificateExport > exportCertificates )
1299+ => _exportCertificates = exportCertificates ;
1300+
11921301 public override string Id => "sync-ca-certs" ;
11931302 public override string DisplayName => "Sync Windows CA certificates to WSL" ;
11941303 public override bool CanRetry => false ;
@@ -1197,89 +1306,112 @@ public override async Task<StepResult> ExecuteAsync(SetupContext ctx, Cancellati
11971306 {
11981307 var distro = ctx . DistroName ! ;
11991308
1200- string pemBundle ;
1309+ WindowsCaCertificateExport export ;
12011310 try
12021311 {
1203- pemBundle = ExportWindowsCaCerts ( ) ;
1312+ export = _exportCertificates ( ) ;
12041313 }
12051314 catch ( Exception ex )
12061315 {
12071316 ctx . Logger . Warn ( $ "Could not read Windows CA store: { ex . Message } — skipping CA sync") ;
12081317 return StepResult . Ok ( "Skipped: could not read Windows CA store" ) ;
12091318 }
12101319
1211- if ( string . IsNullOrWhiteSpace ( pemBundle ) )
1320+ foreach ( var warning in export . Warnings )
1321+ ctx . Logger . Warn ( warning ) ;
1322+
1323+ if ( export . CertificateCount == 0 )
12121324 {
12131325 ctx . Logger . Warn ( "Windows CA store returned no certificates — skipping CA sync" ) ;
12141326 return StepResult . Ok ( "Skipped: Windows CA store is empty" ) ;
12151327 }
12161328
1217- // Write directly to the WSL filesystem via the UNC share WSL exposes on Windows.
1218- // This avoids needing stdin piping through RunInWslAsync.
1219- var wslCertDir = $@ "\\wsl$\{ distro } \usr\local\share\ca-certificates";
1220- try
1221- {
1222- Directory . CreateDirectory ( wslCertDir ) ;
1223- File . WriteAllText ( Path . Combine ( wslCertDir , "windows-ca-bundle.crt" ) , pemBundle ) ;
1224- }
1225- catch ( Exception ex )
1226- {
1227- ctx . Logger . Warn ( $ "Could not write CA bundle to WSL filesystem: { ex . Message } — skipping CA sync") ;
1228- return StepResult . Ok ( "Skipped: could not write to WSL filesystem" ) ;
1229- }
1230-
1231- var result = await ctx . Commands . RunInWslAsync (
1232- distro ,
1233- "update-ca-certificates" ,
1234- TimeSpan . FromSeconds ( 30 ) ,
1235- ct : ct ,
1236- user : "root" ) ;
1329+ var result = await ctx . Commands . RunAsync (
1330+ WslConstants . WslExePath ,
1331+ [ "-d" , distro , "-u" , "root" , "--" , "bash" , "-c" , NormalizeShellScript ( InstallScript ) ] ,
1332+ TimeSpan . FromSeconds ( 60 ) ,
1333+ workingDirectory : WslConstants . SafeWindowsWorkingDirectory ,
1334+ stdinInput : export . Manifest ,
1335+ ct : ct ) ;
12371336
12381337 if ( result . ExitCode != 0 )
12391338 {
12401339 ctx . Logger . Warn ( $ "update-ca-certificates exited { result . ExitCode } : { result . Stderr . Trim ( ) } ") ;
1241- // Non-fatal: the bundle is written; curl may still work depending on the error.
1242- return StepResult . Ok ( $ "CA bundle written but update-ca-certificates warned (exit { result . ExitCode } )") ;
1340+ return StepResult . Ok ( $ "Skipped: could not update WSL CA certificates (exit { result . ExitCode } )") ;
12431341 }
12441342
1245- ctx . Logger . Info ( " Windows CA certificates synced to WSL trust store") ;
1246- return StepResult . Ok ( " Windows CA certificates synced to WSL") ;
1343+ ctx . Logger . Info ( $ "Synced { export . CertificateCount } Windows CA certificates to WSL trust store") ;
1344+ return StepResult . Ok ( $ "Synced { export . CertificateCount } Windows CA certificates to WSL") ;
12471345 }
12481346
1249- public override Task RollbackAsync ( SetupContext ctx , CancellationToken ct )
1347+ public override async Task RollbackAsync ( SetupContext ctx , CancellationToken ct )
12501348 {
12511349 var distro = ctx . DistroName ! ;
1252- var wslCertPath = $@ "\\wsl$\{ distro } \usr\local\share\ca-certificates\windows-ca-bundle.crt";
1253- if ( File . Exists ( wslCertPath ) )
1350+ var result = await ctx . Commands . RunAsync (
1351+ WslConstants . WslExePath ,
1352+ [ "-d" , distro , "-u" , "root" , "--" , "bash" , "-c" , NormalizeShellScript ( RollbackScript ) ] ,
1353+ TimeSpan . FromSeconds ( 30 ) ,
1354+ workingDirectory : WslConstants . SafeWindowsWorkingDirectory ,
1355+ ct : ct ) ;
1356+ if ( result . ExitCode == 0 )
12541357 {
1255- File . Delete ( wslCertPath ) ;
1256- ctx . Logger . Info ( "[Rollback] Removed windows-ca-bundle.crt from WSL" ) ;
1358+ ctx . Logger . Info ( $ "[Rollback] Removed { ManagedCertDirectory } from WSL trust store" ) ;
1359+ return ;
12571360 }
1258- return Task . CompletedTask ;
1361+
1362+ throw new InvalidOperationException (
1363+ $ "Could not remove synced CA certificates (exit { result . ExitCode } ): { result . Stderr . Trim ( ) } ") ;
12591364 }
12601365
1261- private static string ExportWindowsCaCerts ( )
1366+ internal static WindowsCaCertificateExport BuildCertificateManifest (
1367+ IEnumerable < byte [ ] > rawCertificates ,
1368+ IReadOnlyList < string > ? warnings = null )
12621369 {
1263- var sb = new StringBuilder ( ) ;
1264- var storeNames = new [ ] { StoreName . Root , StoreName . CertificateAuthority } ;
1265-
1266- foreach ( var storeName in storeNames )
1370+ var certificates = new SortedDictionary < string , string > ( StringComparer . Ordinal ) ;
1371+ foreach ( var rawCertificate in rawCertificates )
12671372 {
1268- using var store = new X509Store ( storeName , StoreLocation . LocalMachine ) ;
1269- store . Open ( OpenFlags . ReadOnly ) ;
1373+ var fingerprint = Convert . ToHexString ( SHA256 . HashData ( rawCertificate ) ) . ToLowerInvariant ( ) ;
1374+ certificates . TryAdd ( fingerprint , Convert . ToBase64String ( rawCertificate ) ) ;
1375+ }
12701376
1271- foreach ( var cert in store . Certificates )
1377+ var manifest = new StringBuilder ( ) ;
1378+ foreach ( var ( fingerprint , payload ) in certificates )
1379+ manifest . Append ( fingerprint ) . Append ( '\t ' ) . Append ( payload ) . Append ( '\n ' ) ;
1380+
1381+ return new WindowsCaCertificateExport ( manifest . ToString ( ) , certificates . Count , warnings ?? [ ] ) ;
1382+ }
1383+
1384+ private static string NormalizeShellScript ( string script )
1385+ => script . Replace ( "\r " , "" , StringComparison . Ordinal ) ;
1386+
1387+ private static WindowsCaCertificateExport ExportWindowsTrustedRoots ( )
1388+ {
1389+ var rawCertificates = new List < byte [ ] > ( ) ;
1390+ var warnings = new List < string > ( ) ;
1391+ foreach ( var location in new [ ] { StoreLocation . LocalMachine , StoreLocation . CurrentUser } )
1392+ {
1393+ try
1394+ {
1395+ using var store = new X509Store ( StoreName . Root , location ) ;
1396+ store . Open ( OpenFlags . ReadOnly ) ;
1397+ foreach ( var certificate in store . Certificates )
1398+ rawCertificates . Add ( certificate . RawData ) ;
1399+ }
1400+ catch ( Exception ex ) when ( ex is CryptographicException or UnauthorizedAccessException )
12721401 {
1273- sb . AppendLine ( "-----BEGIN CERTIFICATE-----" ) ;
1274- sb . AppendLine ( Convert . ToBase64String ( cert . RawData , Base64FormattingOptions . InsertLineBreaks ) ) ;
1275- sb . AppendLine ( "-----END CERTIFICATE-----" ) ;
1402+ warnings . Add ( $ "Could not read Windows { location } Root store: { ex . Message } ") ;
12761403 }
12771404 }
12781405
1279- return sb . ToString ( ) ;
1406+ return BuildCertificateManifest ( rawCertificates , warnings ) ;
12801407 }
12811408}
12821409
1410+ internal sealed record WindowsCaCertificateExport (
1411+ string Manifest ,
1412+ int CertificateCount ,
1413+ IReadOnlyList < string > Warnings ) ;
1414+
12831415public sealed class InstallCliStep : SetupStep
12841416{
12851417 public override string Id => "install-cli" ;
0 commit comments