Imported Upstream version 5.16.0.100

Former-commit-id: 38faa55fb9669e35e7d8448b15c25dc447f25767
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2018-08-07 15:19:03 +00:00
parent 0a9828183b
commit 7d7f676260
4419 changed files with 170950 additions and 90273 deletions

View File

@ -56,9 +56,10 @@ using MNS = Mono.Net.Security;
namespace System.Net.Security
{
public delegate X509Certificate ServerCertificateSelectionCallback (object sender, string hostName);
/*
* These two are defined by the referencesource; add them heere to make
* it easy to switch between the two implementations.
* Internal delegates from the referencesource / corefx.
*/
internal delegate bool RemoteCertValidationCallback (
@ -73,10 +74,16 @@ namespace System.Net.Security
X509Certificate remoteCertificate,
string[] acceptableIssuers);
internal delegate X509Certificate ServerCertSelectionCallback (string hostName);
public class SslStream : AuthenticatedStream
{
#if SECURITY_DEP
MonoTlsProvider provider;
MonoTlsSettings settings;
RemoteCertificateValidationCallback validationCallback;
LocalCertificateSelectionCallback selectionCallback;
bool explicitSettings;
IMonoSslStream impl;
internal IMonoSslStream Impl {
@ -86,6 +93,8 @@ namespace System.Net.Security
}
}
IMonoSslStream2 Impl2 => (IMonoSslStream2)Impl;
internal MonoTlsProvider Provider {
get {
CheckDisposed ();
@ -107,7 +116,8 @@ namespace System.Net.Security
: base (innerStream, leaveInnerStreamOpen)
{
provider = GetProvider ();
impl = provider.CreateSslStreamInternal (this, innerStream, leaveInnerStreamOpen, null);
settings = MonoTlsSettings.CopyDefaultSettings ();
impl = provider.CreateSslStreamInternal (this, innerStream, leaveInnerStreamOpen, settings);
}
public SslStream (Stream innerStream, bool leaveInnerStreamOpen, RemoteCertificateValidationCallback userCertificateValidationCallback)
@ -119,15 +129,15 @@ namespace System.Net.Security
: base (innerStream, leaveInnerStreamOpen)
{
provider = GetProvider ();
var settings = MonoTlsSettings.CopyDefaultSettings ();
settings.RemoteCertificateValidationCallback = MNS.Private.CallbackHelpers.PublicToMono (userCertificateValidationCallback);
settings.ClientCertificateSelectionCallback = MNS.Private.CallbackHelpers.PublicToMono (userCertificateSelectionCallback);
settings = MonoTlsSettings.CopyDefaultSettings ();
SetAndVerifyValidationCallback (userCertificateValidationCallback);
SetAndVerifySelectionCallback (userCertificateSelectionCallback);
impl = provider.CreateSslStream (innerStream, leaveInnerStreamOpen, settings);
}
[MonoLimitation ("encryptionPolicy is ignored")]
public SslStream (Stream innerStream, bool leaveInnerStreamOpen, RemoteCertificateValidationCallback userCertificateValidationCallback, LocalCertificateSelectionCallback userCertificateSelectionCallback, EncryptionPolicy encryptionPolicy)
: this (innerStream, leaveInnerStreamOpen, userCertificateValidationCallback, userCertificateSelectionCallback)
: this (innerStream, leaveInnerStreamOpen, userCertificateValidationCallback, userCertificateSelectionCallback)
{
}
@ -135,6 +145,8 @@ namespace System.Net.Security
: base (innerStream, leaveInnerStreamOpen)
{
this.provider = provider;
this.settings = settings.Clone ();
explicitSettings = true;
impl = provider.CreateSslStreamInternal (this, innerStream, leaveInnerStreamOpen, settings);
}
@ -144,23 +156,54 @@ namespace System.Net.Security
return sslStream.Impl;
}
void SetAndVerifyValidationCallback (RemoteCertificateValidationCallback callback)
{
if (validationCallback == null) {
validationCallback = callback;
settings.RemoteCertificateValidationCallback = MNS.Private.CallbackHelpers.PublicToMono (callback);
} else if ((callback != null && validationCallback != callback) || (explicitSettings & settings.RemoteCertificateValidationCallback != null)) {
throw new InvalidOperationException (SR.Format (SR.net_conflicting_options, nameof (RemoteCertificateValidationCallback)));
}
}
void SetAndVerifySelectionCallback (LocalCertificateSelectionCallback callback)
{
if (selectionCallback == null) {
selectionCallback = callback;
if (callback == null)
settings.ClientCertificateSelectionCallback = null;
else
settings.ClientCertificateSelectionCallback = (t, lc, rc, ai) => callback (this, t, lc, rc, ai);
} else if ((callback != null && selectionCallback != callback) || (explicitSettings && settings.ClientCertificateSelectionCallback != null)) {
throw new InvalidOperationException (SR.Format (SR.net_conflicting_options, nameof (LocalCertificateSelectionCallback)));
}
}
public virtual void AuthenticateAsClient (string targetHost)
{
Impl.AuthenticateAsClient (targetHost);
}
public virtual void AuthenticateAsClient (string targetHost, X509CertificateCollection clientCertificates, bool checkCertificateRevocation)
{
Impl.AuthenticateAsClient (targetHost, clientCertificates, SecurityProtocol.SystemDefaultSecurityProtocols, checkCertificateRevocation);
}
public virtual void AuthenticateAsClient (string targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
{
Impl.AuthenticateAsClient (targetHost, clientCertificates, enabledSslProtocols, checkCertificateRevocation);
}
// [HostProtection (ExternalThreading=true)]
public virtual IAsyncResult BeginAuthenticateAsClient (string targetHost, AsyncCallback asyncCallback, object asyncState)
{
return Impl.BeginAuthenticateAsClient (targetHost, asyncCallback, asyncState);
}
// [HostProtection (ExternalThreading=true)]
public virtual IAsyncResult BeginAuthenticateAsClient (string targetHost, X509CertificateCollection clientCertificates, bool checkCertificateRevocation, AsyncCallback asyncCallback, object asyncState)
{
return Impl.BeginAuthenticateAsClient (targetHost, clientCertificates, SecurityProtocol.SystemDefaultSecurityProtocols, checkCertificateRevocation, asyncCallback, asyncState);
}
public virtual IAsyncResult BeginAuthenticateAsClient (string targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation, AsyncCallback asyncCallback, object asyncState)
{
return Impl.BeginAuthenticateAsClient (targetHost, clientCertificates, enabledSslProtocols, checkCertificateRevocation, asyncCallback, asyncState);
@ -176,17 +219,26 @@ namespace System.Net.Security
Impl.AuthenticateAsServer (serverCertificate);
}
public virtual void AuthenticateAsServer (X509Certificate serverCertificate, bool clientCertificateRequired, bool checkCertificateRevocation)
{
Impl.AuthenticateAsServer (serverCertificate, clientCertificateRequired, SecurityProtocol.SystemDefaultSecurityProtocols, checkCertificateRevocation);
}
public virtual void AuthenticateAsServer (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
{
Impl.AuthenticateAsServer (serverCertificate, clientCertificateRequired, enabledSslProtocols, checkCertificateRevocation);
}
// [HostProtection (ExternalThreading=true)]
public virtual IAsyncResult BeginAuthenticateAsServer (X509Certificate serverCertificate, AsyncCallback asyncCallback, object asyncState)
{
return Impl.BeginAuthenticateAsServer (serverCertificate, asyncCallback, asyncState);
}
public virtual IAsyncResult BeginAuthenticateAsServer (X509Certificate serverCertificate, bool clientCertificateRequired, bool checkCertificateRevocation, AsyncCallback asyncCallback, object asyncState)
{
return Impl.BeginAuthenticateAsServer (serverCertificate, clientCertificateRequired, SecurityProtocol.SystemDefaultSecurityProtocols, checkCertificateRevocation, asyncCallback, asyncState);
}
public virtual IAsyncResult BeginAuthenticateAsServer (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation, AsyncCallback asyncCallback, object asyncState)
{
return Impl.BeginAuthenticateAsServer (serverCertificate, clientCertificateRequired, enabledSslProtocols, checkCertificateRevocation, asyncCallback, asyncState);
@ -197,33 +249,50 @@ namespace System.Net.Security
Impl.EndAuthenticateAsServer (asyncResult);
}
public TransportContext TransportContext {
get {
throw new NotSupportedException();
}
}
public TransportContext TransportContext => null;
// [HostProtection (ExternalThreading=true)]
public virtual Task AuthenticateAsClientAsync (string targetHost)
{
return Impl.AuthenticateAsClientAsync (targetHost);
}
public virtual Task AuthenticateAsClientAsync (string targetHost, X509CertificateCollection clientCertificates, bool checkCertificateRevocation)
{
return Impl.AuthenticateAsClientAsync (targetHost, clientCertificates, SecurityProtocol.SystemDefaultSecurityProtocols, checkCertificateRevocation);
}
public virtual Task AuthenticateAsClientAsync (string targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
{
return Impl.AuthenticateAsClientAsync (targetHost, clientCertificates, enabledSslProtocols, checkCertificateRevocation);
}
public Task AuthenticateAsClientAsync (SslClientAuthenticationOptions sslClientAuthenticationOptions, CancellationToken cancellationToken)
{
SetAndVerifyValidationCallback (sslClientAuthenticationOptions.RemoteCertificateValidationCallback);
SetAndVerifySelectionCallback (sslClientAuthenticationOptions.LocalCertificateSelectionCallback);
return Impl2.AuthenticateAsClientAsync (new MNS.MonoSslClientAuthenticationOptions (sslClientAuthenticationOptions), cancellationToken);
}
public virtual Task AuthenticateAsServerAsync (X509Certificate serverCertificate)
{
return Impl.AuthenticateAsServerAsync (serverCertificate);
}
public virtual Task AuthenticateAsServerAsync (X509Certificate serverCertificate, bool clientCertificateRequired, bool checkCertificateRevocation)
{
return Impl.AuthenticateAsServerAsync (serverCertificate, clientCertificateRequired, SecurityProtocol.SystemDefaultSecurityProtocols, checkCertificateRevocation);
}
public virtual Task AuthenticateAsServerAsync (X509Certificate serverCertificate, bool clientCertificateRequired, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
{
return Impl.AuthenticateAsServerAsync (serverCertificate, clientCertificateRequired, enabledSslProtocols, checkCertificateRevocation);
}
public Task AuthenticateAsServerAsync (SslServerAuthenticationOptions sslServerAuthenticationOptions, CancellationToken cancellationToken)
{
return Impl2.AuthenticateAsServerAsync (new MNS.MonoSslServerAuthenticationOptions (sslServerAuthenticationOptions), cancellationToken);
}
public virtual Task ShutdownAsync ()
{
return Impl.ShutdownAsync ();