Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ internal static SslPolicyErrors BuildChainAndVerifyProperties(X509Chain chain, X
if (!chainBuildResult // Build failed on handle or on policy.
&& chain.SafeHandle!.DangerousGetHandle() == IntPtr.Zero) // Build failed to generate a valid handle.
{
#if NETFRAMEWORK
throw new CryptographicException(Marshal.GetLastWin32Error());
#else
throw new CryptographicException(Marshal.GetLastPInvokeError());
#endif
}

if (checkCertName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ System.Net.Http.WinHttpHandler</PackageDescription>
Link="Common\System\Net\Security\CertificateHelper.cs" />
<Compile Include="$(CommonPath)\System\Net\Security\CertificateHelper.Windows.cs"
Link="Common\System\Net\Security\CertificateHelper.Windows.cs" />
<Compile Include="$(CommonPath)\System\Net\Security\CertificateValidation.Windows.cs"
Link="Common\System\Net\Security\CertificateValidation.Windows.cs" />
<Compile Include="$(CommonPath)\System\Runtime\ExceptionServices\ExceptionStackTrace.cs"
Link="Common\System\Runtime\ExceptionServices\ExceptionStackTrace.cs" />
<Compile Include="$(CommonPath)\System\Threading\Tasks\RendezvousAwaitable.cs"
Expand All @@ -80,7 +82,6 @@ System.Net.Http.WinHttpHandler</PackageDescription>
<Compile Include="System\Net\Http\NetEventSource.WinHttpHandler.cs" />
<Compile Include="System\Net\Http\NoWriteNoSeekStreamContent.cs" />
<Compile Include="System\Net\Http\WinHttpAuthHelper.cs" />
<Compile Include="System\Net\Http\WinHttpCertificateHelper.cs" />
<Compile Include="System\Net\Http\WinHttpChannelBinding.cs" />
<Compile Include="System\Net\Http\WinHttpChunkMode.cs" />
<Compile Include="System\Net\Http\WinHttpCookieContainerAdapter.cs" />
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using SafeWinHttpHandle = Interop.WinHttp.SafeWinHttpHandle;
Expand All @@ -21,6 +22,8 @@ namespace System.Net.Http
/// </summary>
internal static class WinHttpRequestCallback
{
private static readonly Oid ServerAuthOid = new Oid("1.3.6.1.5.5.7.3.1", "1.3.6.1.5.5.7.3.1");

public static Interop.WinHttp.WINHTTP_STATUS_CALLBACK StaticCallbackDelegate =
new Interop.WinHttp.WINHTTP_STATUS_CALLBACK(WinHttpCallback);

Expand Down Expand Up @@ -370,13 +373,34 @@ private static void OnRequestSendingRequest(WinHttpRequestState state)

try
{
WinHttpCertificateHelper.BuildChain(
// Create and configure the X509Chain
chain = new X509Chain();
chain.ChainPolicy.RevocationMode = state.CheckCertificateRevocationList ? X509RevocationMode.Online : X509RevocationMode.NoCheck;
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.ExcludeRoot;
// Authenticate the remote party: (e.g. when operating in client mode, authenticate the server).
chain.ChainPolicy.ApplicationPolicy.Add(ServerAuthOid);

if (remoteCertificateStore.Count > 0)
{
if (NetEventSource.Log.IsEnabled())
{
foreach (X509Certificate cert in remoteCertificateStore)
{
NetEventSource.Info(remoteCertificateStore, $"Adding cert to ExtraStore: {cert.Subject}");
}
}

chain.ChainPolicy.ExtraStore.AddRange(remoteCertificateStore);
}

// Call the shared BuildChainAndVerifyProperties method
// isServer=false because WinHttpHandler is a client validating a server certificate
sslPolicyErrors = System.Net.CertificateValidation.BuildChainAndVerifyProperties(
chain,
serverCertificate,
remoteCertificateStore,
state.RequestMessage.RequestUri.Host,
state.CheckCertificateRevocationList,
out chain,
out sslPolicyErrors);
checkCertName: true,
isServer: false,
hostName: state.RequestMessage.RequestUri.Host);

result = state.ServerCertificateValidationCallback(
state.RequestMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,13 @@
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

namespace System.Net.Http
namespace System.Net
{
internal static class WinHttpCertificateHelper
internal static partial class CertificateValidation
{
public static void BuildChain(
X509Certificate2 certificate,
X509Certificate2Collection remoteCertificateStore,
string hostName,
bool checkCertificateRevocationList,
out X509Chain chain,
out SslPolicyErrors sslPolicyErrors)
internal static SslPolicyErrors BuildChainAndVerifyProperties(X509Chain chain, X509Certificate2 remoteCertificate, bool checkCertName, bool isServer, string? hostName)
{
chain = null;
sslPolicyErrors = SslPolicyErrors.None;
return SslPolicyErrors.None;
}
}
}
Expand Down
Loading