Skip to content

Commit 6efd7e6

Browse files
committed
Add certificate with friendly name and additional WSL checks
1 parent d653ab0 commit 6efd7e6

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/Shared/CertificateGeneration/UnixCertificateManager.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ internal sealed partial class UnixCertificateManager : CertificateManager
3535
private const string PowerShellCommand = "powershell.exe";
3636
private const string WslInteropPath = "/proc/sys/fs/binfmt_misc/WSLInterop";
3737
private const string WslInteropLatePath = "/proc/sys/fs/binfmt_misc/WSLInterop-late";
38+
private const string WslFriendlyName = AspNetHttpsOidFriendlyName + " (WSL)";
3839

3940
private const string OpenSslCommand = "openssl";
4041
private const string CertUtilCommand = "certutil";
@@ -370,7 +371,7 @@ protected override TrustLevel TrustCertificateCore(X509Certificate2 certificate)
370371
}
371372

372373
// Check to see if we're running in WSL; if so, use powershell.exe to add the certificate to the Windows trust store as well
373-
if (IsRunningOnWsl())
374+
if (IsRunningOnWslWithInterop())
374375
{
375376
if (TryTrustCertificateInWindowsStore(certPath))
376377
{
@@ -584,14 +585,26 @@ private static string GetCertificateNickname(X509Certificate2 certificate)
584585
}
585586

586587
/// <summary>
587-
/// Detects if the current environment is Windows Subsystem for Linux (WSL).
588+
/// Detects if the current environment is Windows Subsystem for Linux (WSL) with interop enabled.
588589
/// </summary>
589-
/// <returns>True if running on WSL; otherwise, false.</returns>
590-
private static bool IsRunningOnWsl()
590+
/// <returns>True if running on WSL with interop; otherwise, false.</returns>
591+
private static bool IsRunningOnWslWithInterop()
591592
{
592593
// WSL exposes special files that indicate WSL interop is enabled.
593594
// Either WSLInterop or WSLInterop-late may be present depending on the WSL version and configuration.
594-
return File.Exists(WslInteropPath) || File.Exists(WslInteropLatePath);
595+
if (File.Exists(WslInteropPath) || File.Exists(WslInteropLatePath))
596+
{
597+
return true;
598+
}
599+
600+
// Additionally check for standard WSL environment variables as a fallback.
601+
// WSL_INTEROP is set to the path of the interop socket.
602+
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WSL_INTEROP")))
603+
{
604+
return true;
605+
}
606+
607+
return false;
595608
}
596609

597610
/// <summary>
@@ -605,8 +618,10 @@ private static bool TryTrustCertificateInWindowsStore(string certificatePath)
605618
// We use Import-Certificate which can handle PEM files on modern Windows.
606619
// The -CertStoreLocation parameter specifies the store location.
607620
var escapedPath = certificatePath.Replace("'", "''");
621+
var escapedFriendlyName = WslFriendlyName.Replace("'", "''");
608622
var powershellScript = $@"
609623
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2('{escapedPath}')
624+
$cert.FriendlyName = '{escapedFriendlyName}'
610625
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store('Root', 'CurrentUser')
611626
$store.Open('ReadWrite')
612627
$store.Add($cert)

0 commit comments

Comments
 (0)