2016-02-22 11:00:01 -05:00
|
|
|
//
|
|
|
|
// System.Net.ServicePointManager
|
|
|
|
//
|
|
|
|
// Authors:
|
|
|
|
// Lawrence Pit (loz@cable.a2000.nl)
|
|
|
|
// Gonzalo Paniagua Javier (gonzalo@novell.com)
|
|
|
|
//
|
|
|
|
// Copyright (c) 2003-2010 Novell, Inc (http://www.novell.com)
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
// a copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
// permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
// the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be
|
|
|
|
// included in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
|
|
|
|
#if SECURITY_DEP
|
|
|
|
|
|
|
|
#if MONO_SECURITY_ALIAS
|
|
|
|
extern alias MonoSecurity;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MONO_SECURITY_ALIAS
|
|
|
|
using MonoSecurity::Mono.Security.Interface;
|
|
|
|
using MSX = MonoSecurity::Mono.Security.X509;
|
|
|
|
using MonoSecurity::Mono.Security.X509.Extensions;
|
|
|
|
#else
|
|
|
|
using Mono.Security.Interface;
|
|
|
|
using MSX = Mono.Security.X509;
|
|
|
|
using Mono.Security.X509.Extensions;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Net;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
using System.Configuration;
|
|
|
|
using System.Net.Configuration;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
|
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Net.Security;
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
namespace Mono.Net.Security
|
|
|
|
{
|
|
|
|
internal delegate bool ServerCertValidationCallbackWrapper (ServerCertValidationCallback callback, X509Certificate certificate, X509Chain chain, MonoSslPolicyErrors sslPolicyErrors);
|
|
|
|
|
2016-04-12 13:19:31 -04:00
|
|
|
internal class ChainValidationHelper : ICertificateValidator2
|
2016-02-22 11:00:01 -05:00
|
|
|
{
|
|
|
|
readonly object sender;
|
|
|
|
readonly MonoTlsSettings settings;
|
|
|
|
readonly MonoTlsProvider provider;
|
|
|
|
readonly ServerCertValidationCallback certValidationCallback;
|
|
|
|
readonly LocalCertSelectionCallback certSelectionCallback;
|
|
|
|
readonly ServerCertValidationCallbackWrapper callbackWrapper;
|
|
|
|
readonly MonoTlsStream tlsStream;
|
|
|
|
readonly HttpWebRequest request;
|
|
|
|
|
2017-04-10 11:41:01 +00:00
|
|
|
#pragma warning disable 618
|
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
internal static ICertificateValidator GetInternalValidator (MonoTlsProvider provider, MonoTlsSettings settings)
|
2016-02-22 11:00:01 -05:00
|
|
|
{
|
|
|
|
if (settings == null)
|
|
|
|
return new ChainValidationHelper (provider, null, false, null, null);
|
|
|
|
if (settings.CertificateValidator != null)
|
|
|
|
return settings.CertificateValidator;
|
|
|
|
return new ChainValidationHelper (provider, settings, false, null, null);
|
|
|
|
}
|
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
internal static ICertificateValidator GetDefaultValidator (MonoTlsSettings settings)
|
|
|
|
{
|
|
|
|
var provider = MonoTlsProviderFactory.GetProvider ();
|
|
|
|
if (settings == null)
|
|
|
|
return new ChainValidationHelper (provider, null, false, null, null);
|
|
|
|
if (settings.CertificateValidator != null)
|
|
|
|
throw new NotSupportedException ();
|
|
|
|
return new ChainValidationHelper (provider, settings, false, null, null);
|
|
|
|
}
|
|
|
|
|
2016-02-22 11:00:01 -05:00
|
|
|
#region SslStream support
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a hack which is used in SslStream - see ReferenceSources/SslStream.cs for details.
|
|
|
|
*/
|
|
|
|
internal static ChainValidationHelper CloneWithCallbackWrapper (MonoTlsProvider provider, ref MonoTlsSettings settings, ServerCertValidationCallbackWrapper wrapper)
|
|
|
|
{
|
|
|
|
var helper = (ChainValidationHelper)settings.CertificateValidator;
|
|
|
|
if (helper == null)
|
|
|
|
helper = new ChainValidationHelper (provider, settings, true, null, wrapper);
|
|
|
|
else
|
|
|
|
helper = new ChainValidationHelper (helper, provider, settings, wrapper);
|
|
|
|
settings = helper.settings;
|
|
|
|
return helper;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static bool InvokeCallback (ServerCertValidationCallback callback, object sender, X509Certificate certificate, X509Chain chain, MonoSslPolicyErrors sslPolicyErrors)
|
|
|
|
{
|
|
|
|
return callback.Invoke (sender, certificate, chain, (SslPolicyErrors)sslPolicyErrors);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
ChainValidationHelper (ChainValidationHelper other, MonoTlsProvider provider, MonoTlsSettings settings, ServerCertValidationCallbackWrapper callbackWrapper = null)
|
|
|
|
{
|
|
|
|
sender = other.sender;
|
|
|
|
certValidationCallback = other.certValidationCallback;
|
|
|
|
certSelectionCallback = other.certSelectionCallback;
|
|
|
|
tlsStream = other.tlsStream;
|
|
|
|
request = other.request;
|
|
|
|
|
|
|
|
if (settings == null)
|
|
|
|
settings = MonoTlsSettings.DefaultSettings;
|
|
|
|
|
|
|
|
this.provider = provider;
|
|
|
|
this.settings = settings.CloneWithValidator (this);
|
|
|
|
this.callbackWrapper = callbackWrapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static ChainValidationHelper Create (MonoTlsProvider provider, ref MonoTlsSettings settings, MonoTlsStream stream)
|
|
|
|
{
|
|
|
|
var helper = new ChainValidationHelper (provider, settings, true, stream, null);
|
|
|
|
settings = helper.settings;
|
|
|
|
return helper;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChainValidationHelper (MonoTlsProvider provider, MonoTlsSettings settings, bool cloneSettings, MonoTlsStream stream, ServerCertValidationCallbackWrapper callbackWrapper)
|
|
|
|
{
|
|
|
|
if (settings == null)
|
|
|
|
settings = MonoTlsSettings.CopyDefaultSettings ();
|
|
|
|
if (cloneSettings)
|
|
|
|
settings = settings.CloneWithValidator (this);
|
2016-11-10 13:04:39 +00:00
|
|
|
if (provider == null)
|
|
|
|
provider = MonoTlsProviderFactory.GetProvider ();
|
2016-02-22 11:00:01 -05:00
|
|
|
|
|
|
|
this.provider = provider;
|
|
|
|
this.settings = settings;
|
|
|
|
this.tlsStream = stream;
|
|
|
|
this.callbackWrapper = callbackWrapper;
|
|
|
|
|
|
|
|
var fallbackToSPM = false;
|
|
|
|
|
|
|
|
if (settings != null) {
|
|
|
|
if (settings.RemoteCertificateValidationCallback != null) {
|
|
|
|
var callback = Private.CallbackHelpers.MonoToPublic (settings.RemoteCertificateValidationCallback);
|
|
|
|
certValidationCallback = new ServerCertValidationCallback (callback);
|
|
|
|
}
|
|
|
|
certSelectionCallback = Private.CallbackHelpers.MonoToInternal (settings.ClientCertificateSelectionCallback);
|
2016-03-16 12:38:19 -04:00
|
|
|
fallbackToSPM = settings.UseServicePointManagerCallback ?? stream != null;
|
2016-02-22 11:00:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (stream != null) {
|
|
|
|
this.request = stream.Request;
|
|
|
|
this.sender = request;
|
|
|
|
|
|
|
|
if (certValidationCallback == null)
|
|
|
|
certValidationCallback = request.ServerCertValidationCallback;
|
|
|
|
if (certSelectionCallback == null)
|
|
|
|
certSelectionCallback = new LocalCertSelectionCallback (DefaultSelectionCallback);
|
|
|
|
|
|
|
|
if (settings == null)
|
|
|
|
fallbackToSPM = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fallbackToSPM && certValidationCallback == null)
|
|
|
|
certValidationCallback = ServicePointManager.ServerCertValidationCallback;
|
|
|
|
}
|
|
|
|
|
2017-04-10 11:41:01 +00:00
|
|
|
#pragma warning restore 618
|
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
static X509Certificate DefaultSelectionCallback (string targetHost, X509CertificateCollection localCertificates, X509Certificate remoteCertificate, string[] acceptableIssuers)
|
2016-02-22 11:00:01 -05:00
|
|
|
{
|
|
|
|
X509Certificate clientCertificate;
|
|
|
|
if (localCertificates == null || localCertificates.Count == 0)
|
|
|
|
clientCertificate = null;
|
|
|
|
else
|
|
|
|
clientCertificate = localCertificates [0];
|
|
|
|
return clientCertificate;
|
|
|
|
}
|
|
|
|
|
|
|
|
public MonoTlsProvider Provider {
|
|
|
|
get { return provider; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public MonoTlsSettings Settings {
|
|
|
|
get { return settings; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool HasCertificateSelectionCallback {
|
|
|
|
get { return certSelectionCallback != null; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool SelectClientCertificate (
|
2016-11-10 13:04:39 +00:00
|
|
|
string targetHost, X509CertificateCollection localCertificates, X509Certificate remoteCertificate,
|
2016-02-22 11:00:01 -05:00
|
|
|
string[] acceptableIssuers, out X509Certificate clientCertificate)
|
|
|
|
{
|
|
|
|
if (certSelectionCallback == null) {
|
|
|
|
clientCertificate = null;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
clientCertificate = certSelectionCallback (targetHost, localCertificates, remoteCertificate, acceptableIssuers);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal X509Certificate SelectClientCertificate (
|
2016-11-10 13:04:39 +00:00
|
|
|
string targetHost, X509CertificateCollection localCertificates, X509Certificate remoteCertificate,
|
2016-02-22 11:00:01 -05:00
|
|
|
string[] acceptableIssuers)
|
|
|
|
{
|
|
|
|
if (certSelectionCallback == null)
|
|
|
|
return null;
|
|
|
|
return certSelectionCallback (targetHost, localCertificates, remoteCertificate, acceptableIssuers);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal bool ValidateClientCertificate (X509Certificate certificate, MonoSslPolicyErrors errors)
|
|
|
|
{
|
2016-11-10 13:04:39 +00:00
|
|
|
var certs = new X509CertificateCollection ();
|
2016-02-22 11:00:01 -05:00
|
|
|
certs.Add (new X509Certificate2 (certificate.GetRawCertData ()));
|
|
|
|
|
2016-04-12 13:19:31 -04:00
|
|
|
var result = ValidateChain (string.Empty, true, certificate, null, certs, (SslPolicyErrors)errors);
|
2016-02-22 11:00:01 -05:00
|
|
|
if (result == null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return result.Trusted && !result.UserDenied;
|
|
|
|
}
|
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
public ValidationResult ValidateCertificate (string host, bool serverMode, X509CertificateCollection certs)
|
2016-02-22 11:00:01 -05:00
|
|
|
{
|
|
|
|
try {
|
2016-04-12 13:19:31 -04:00
|
|
|
X509Certificate leaf;
|
|
|
|
if (certs != null && certs.Count != 0)
|
|
|
|
leaf = certs [0];
|
|
|
|
else
|
|
|
|
leaf = null;
|
|
|
|
var result = ValidateChain (host, serverMode, leaf, null, certs, 0);
|
2016-02-22 11:00:01 -05:00
|
|
|
if (tlsStream != null)
|
|
|
|
tlsStream.CertificateValidationFailed = result == null || !result.Trusted || result.UserDenied;
|
|
|
|
return result;
|
|
|
|
} catch {
|
|
|
|
if (tlsStream != null)
|
|
|
|
tlsStream.CertificateValidationFailed = true;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
public ValidationResult ValidateCertificate (string host, bool serverMode, X509Certificate leaf, X509Chain chain)
|
2016-04-12 13:19:31 -04:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
var result = ValidateChain (host, serverMode, leaf, chain, null, 0);
|
|
|
|
if (tlsStream != null)
|
|
|
|
tlsStream.CertificateValidationFailed = result == null || !result.Trusted || result.UserDenied;
|
|
|
|
return result;
|
|
|
|
} catch {
|
|
|
|
if (tlsStream != null)
|
|
|
|
tlsStream.CertificateValidationFailed = true;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ValidationResult ValidateChain (string host, bool server, X509Certificate leaf,
|
2016-11-10 13:04:39 +00:00
|
|
|
X509Chain chain, X509CertificateCollection certs,
|
2016-04-12 13:19:31 -04:00
|
|
|
SslPolicyErrors errors)
|
|
|
|
{
|
|
|
|
var oldChain = chain;
|
|
|
|
var ownsChain = chain == null;
|
|
|
|
try {
|
|
|
|
var result = ValidateChain (host, server, leaf, ref chain, certs, errors);
|
|
|
|
if (chain != oldChain)
|
|
|
|
ownsChain = true;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
} finally {
|
|
|
|
// If ValidateChain() changed the chain, then we need to free it.
|
|
|
|
if (ownsChain && chain != null)
|
|
|
|
chain.Dispose ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ValidationResult ValidateChain (string host, bool server, X509Certificate leaf,
|
2016-11-10 13:04:39 +00:00
|
|
|
ref X509Chain chain, X509CertificateCollection certs,
|
2016-04-12 13:19:31 -04:00
|
|
|
SslPolicyErrors errors)
|
2016-02-22 11:00:01 -05:00
|
|
|
{
|
|
|
|
// user_denied is true if the user callback is called and returns false
|
|
|
|
bool user_denied = false;
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
var hasCallback = certValidationCallback != null || callbackWrapper != null;
|
|
|
|
|
|
|
|
if (tlsStream != null)
|
2016-03-16 12:38:19 -04:00
|
|
|
request.ServicePoint.UpdateServerCertificate (leaf);
|
2016-02-22 11:00:01 -05:00
|
|
|
|
|
|
|
if (leaf == null) {
|
|
|
|
errors |= SslPolicyErrors.RemoteCertificateNotAvailable;
|
|
|
|
if (hasCallback) {
|
|
|
|
if (callbackWrapper != null)
|
|
|
|
result = callbackWrapper.Invoke (certValidationCallback, leaf, null, (MonoSslPolicyErrors)errors);
|
|
|
|
else
|
|
|
|
result = certValidationCallback.Invoke (sender, leaf, null, errors);
|
|
|
|
user_denied = !result;
|
|
|
|
}
|
|
|
|
return new ValidationResult (result, user_denied, 0, (MonoSslPolicyErrors)errors);
|
|
|
|
}
|
|
|
|
|
2016-10-12 08:30:39 +00:00
|
|
|
// Ignore port number when validating certificates.
|
|
|
|
if (!string.IsNullOrEmpty (host)) {
|
|
|
|
var pos = host.IndexOf (':');
|
|
|
|
if (pos > 0)
|
|
|
|
host = host.Substring (0, pos);
|
|
|
|
}
|
|
|
|
|
2016-02-22 11:00:01 -05:00
|
|
|
ICertificatePolicy policy = ServicePointManager.GetLegacyCertificatePolicy ();
|
|
|
|
|
|
|
|
int status11 = 0; // Error code passed to the obsolete ICertificatePolicy callback
|
|
|
|
|
|
|
|
bool wantsChain = SystemCertificateValidator.NeedsChain (settings);
|
|
|
|
if (!wantsChain && hasCallback) {
|
|
|
|
if (settings == null || settings.CallbackNeedsCertificateChain)
|
|
|
|
wantsChain = true;
|
|
|
|
}
|
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
var xerrors = (MonoSslPolicyErrors)errors;
|
|
|
|
result = provider.ValidateCertificate (this, host, server, certs, wantsChain, ref chain, ref xerrors, ref status11);
|
|
|
|
errors = (SslPolicyErrors)xerrors;
|
2016-02-22 11:00:01 -05:00
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
if (status11 == 0 && errors != 0) {
|
|
|
|
// TRUST_E_FAIL
|
|
|
|
status11 = unchecked ((int)0x800B010B);
|
|
|
|
}
|
2016-02-22 11:00:01 -05:00
|
|
|
|
|
|
|
if (policy != null && (!(policy is DefaultCertificatePolicy) || certValidationCallback == null)) {
|
|
|
|
ServicePoint sp = null;
|
|
|
|
if (request != null)
|
|
|
|
sp = request.ServicePointNoLock;
|
|
|
|
|
|
|
|
// pre 2.0 callback
|
|
|
|
result = policy.CheckValidationResult (sp, leaf, request, status11);
|
|
|
|
user_denied = !result && !(policy is DefaultCertificatePolicy);
|
|
|
|
}
|
|
|
|
// If there's a 2.0 callback, it takes precedence
|
|
|
|
if (hasCallback) {
|
|
|
|
if (callbackWrapper != null)
|
|
|
|
result = callbackWrapper.Invoke (certValidationCallback, leaf, chain, (MonoSslPolicyErrors)errors);
|
|
|
|
else
|
|
|
|
result = certValidationCallback.Invoke (sender, leaf, chain, errors);
|
|
|
|
user_denied = !result;
|
|
|
|
}
|
|
|
|
return new ValidationResult (result, user_denied, status11, (MonoSslPolicyErrors)errors);
|
|
|
|
}
|
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
bool InvokeSystemValidator (string targetHost, bool serverMode, X509CertificateCollection certificates, X509Chain chain, ref MonoSslPolicyErrors xerrors, ref int status11)
|
2016-02-22 11:00:01 -05:00
|
|
|
{
|
|
|
|
var errors = (SslPolicyErrors)xerrors;
|
|
|
|
var result = SystemCertificateValidator.Evaluate (settings, targetHost, certificates, chain, ref errors, ref status11);
|
|
|
|
xerrors = (MonoSslPolicyErrors)errors;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|