CarotDav_decompile/Rei.Fs/WebRequestCertificateValidationManager.cs

137 lines
4.9 KiB
C#

// Decompiled with JetBrains decompiler
// Type: Rei.Fs.WebRequestCertificateValidationManager
// Assembly: Rei.Fs, Version=1.13.2.8796, Culture=neutral, PublicKeyToken=null
// MVID: D8B08A8B-697C-4439-9CFF-1BE4EE46F7B0
// Assembly location: F:\Eigene Dateien\Dropbox\portable Collection\Progs\CarotDAV\Rei.Fs.dll
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
namespace Rei.Fs
{
public sealed class WebRequestCertificateValidationManager
{
private static Dictionary<WebRequest, WebRequestCertificateValidationManager.RequestStore> p_ReqToClient = new Dictionary<WebRequest, WebRequestCertificateValidationManager.RequestStore>();
static WebRequestCertificateValidationManager()
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(WebRequestCertificateValidationManager.InnerCertificateValidationCallback);
IEnumerator registeredModules = AuthenticationManager.RegisteredModules;
List<IAuthenticationModule> authenticationModuleList = new List<IAuthenticationModule>();
while (registeredModules.MoveNext())
authenticationModuleList.Add((IAuthenticationModule) registeredModules.Current);
AuthenticationManager.Register((IAuthenticationModule) new WebRequestCertificateValidationManager.FookAuthClient());
List<IAuthenticationModule>.Enumerator enumerator;
try
{
enumerator = authenticationModuleList.GetEnumerator();
while (enumerator.MoveNext())
AuthenticationManager.Register(enumerator.Current);
}
finally
{
enumerator.Dispose();
}
}
private static bool InnerCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
int certErrorIgnores;
lock (WebRequestCertificateValidationManager.p_ReqToClient)
{
WebRequest key = sender as WebRequest;
if (!WebRequestCertificateValidationManager.p_ReqToClient.ContainsKey(key))
return false;
ConnectionSettingBase connectionSetting = WebRequestCertificateValidationManager.p_ReqToClient[key].ConnectionSetting;
certErrorIgnores = (int) connectionSetting.CertErrorIgnores;
connectionSetting.LastCertError = sslPolicyErrors;
}
return (sslPolicyErrors & (SslPolicyErrors) ~certErrorIgnores) == SslPolicyErrors.None;
}
public static void Register(WebRequest req, ConnectionSettingBase setting)
{
lock (WebRequestCertificateValidationManager.p_ReqToClient)
WebRequestCertificateValidationManager.p_ReqToClient.Add(req, new WebRequestCertificateValidationManager.RequestStore(setting));
}
public static void UnRegister(WebRequest req)
{
lock (WebRequestCertificateValidationManager.p_ReqToClient)
WebRequestCertificateValidationManager.p_ReqToClient.Remove(req);
}
public static string[] GetChallenges(WebRequest req)
{
lock (WebRequestCertificateValidationManager.p_ReqToClient)
{
if (!WebRequestCertificateValidationManager.p_ReqToClient.ContainsKey(req))
return new string[0];
return WebRequestCertificateValidationManager.p_ReqToClient[req].ChallengeList.ToArray();
}
}
public static ConnectionSettingBase GetConnectionSetting(WebRequest req)
{
lock (WebRequestCertificateValidationManager.p_ReqToClient)
{
if (!WebRequestCertificateValidationManager.p_ReqToClient.ContainsKey(req))
return (ConnectionSettingBase) null;
return WebRequestCertificateValidationManager.p_ReqToClient[req].ConnectionSetting;
}
}
private class FookAuthClient : IAuthenticationModule
{
public Authorization Authenticate(string challenge, WebRequest request, ICredentials credentials)
{
lock (WebRequestCertificateValidationManager.p_ReqToClient)
{
if (WebRequestCertificateValidationManager.p_ReqToClient.ContainsKey(request))
WebRequestCertificateValidationManager.p_ReqToClient[request].ChallengeList.Add(challenge);
}
return (Authorization) null;
}
public string AuthenticationType
{
get
{
return ":fook";
}
}
public bool CanPreAuthenticate
{
get
{
return false;
}
}
public Authorization PreAuthenticate(WebRequest request, ICredentials credentials)
{
return (Authorization) null;
}
}
private class RequestStore
{
public ConnectionSettingBase ConnectionSetting;
public List<string> ChallengeList;
public SslPolicyErrors LastCertError;
public RequestStore(ConnectionSettingBase c)
{
this.ChallengeList = new List<string>();
this.ConnectionSetting = c;
}
}
}
}