New run-jenkins tags to allow a checked build job and fix partial checked build suppo...
[mono.git] / mcs / class / System / ReferenceSources / SslStream.cs
1 //
2 // Mono-specific additions to Microsoft's SslStream.cs
3 //
4 #if MONO_FEATURE_NEW_TLS && SECURITY_DEP
5 #if MONO_SECURITY_ALIAS
6 extern alias MonoSecurity;
7 using MonoSecurity::Mono.Security.Interface;
8 #else
9 using Mono.Security.Interface;
10 #endif
11 using System.Threading;
12 using System.Security.Cryptography.X509Certificates;
13 using MNS = Mono.Net.Security;
14
15 namespace System.Net.Security
16 {
17         using System.Net.Sockets;
18         using System.IO;
19
20         partial class SslStream : IMonoTlsEventSink
21         {
22                 #if SECURITY_DEP
23                 SSPIConfiguration _Configuration;
24
25                 internal SslStream (Stream innerStream, bool leaveInnerStreamOpen, EncryptionPolicy encryptionPolicy, MonoTlsProvider provider, MonoTlsSettings settings)
26                         : base (innerStream, leaveInnerStreamOpen)
27                 {
28                         if (encryptionPolicy != EncryptionPolicy.RequireEncryption && encryptionPolicy != EncryptionPolicy.AllowNoEncryption && encryptionPolicy != EncryptionPolicy.NoEncryption)
29                                 throw new ArgumentException (SR.GetString (SR.net_invalid_enum, "EncryptionPolicy"), "encryptionPolicy");
30
31                         var validationHelper = MNS.ChainValidationHelper.CloneWithCallbackWrapper (provider, ref settings, myUserCertValidationCallbackWrapper);
32
33                         LocalCertSelectionCallback selectionCallback = null;
34                         if (validationHelper.HasCertificateSelectionCallback)
35                                 selectionCallback = validationHelper.SelectClientCertificate;
36
37                         var internalProvider = new MNS.Private.MonoTlsProviderWrapper (provider);
38                         _Configuration = new MyConfiguration (internalProvider, settings, this);
39                         _SslState = new SslState (innerStream, null, selectionCallback, encryptionPolicy, _Configuration);
40                 }
41
42                 /*
43                  * Mono-specific version of 'userCertValidationCallbackWrapper'; we're called from ChainValidationHelper.ValidateChain() here.
44                  *
45                  * Since we're built without the PrebuiltSystem alias, we can't use 'SslPolicyErrors' here.  This prevents us from creating a subclass of 'ChainValidationHelper'
46                  * as well as providing a custom 'ServerCertValidationCallback'.
47                  */
48                 bool myUserCertValidationCallbackWrapper (ServerCertValidationCallback callback, X509Certificate certificate, X509Chain chain, MonoSslPolicyErrors sslPolicyErrors)
49                 {
50                         m_RemoteCertificateOrBytes = certificate == null ? null : certificate.GetRawCertData ();
51                         if (callback == null) {
52                                 if (!_SslState.RemoteCertRequired)
53                                         sslPolicyErrors &= ~MonoSslPolicyErrors.RemoteCertificateNotAvailable;
54
55                                 return (sslPolicyErrors == MonoSslPolicyErrors.None);
56                         }
57
58                         return MNS.ChainValidationHelper.InvokeCallback (callback, this, certificate, chain, sslPolicyErrors);
59                 }
60
61                 class MyConfiguration : SSPIConfiguration
62                 {
63                         MNS.IMonoTlsProvider provider;
64                         MonoTlsSettings settings;
65                         IMonoTlsEventSink eventSink;
66
67                         public MyConfiguration (MNS.IMonoTlsProvider provider, MonoTlsSettings settings, IMonoTlsEventSink eventSink)
68                         {
69                                 this.provider = provider;
70                                 this.settings = settings;
71                                 this.eventSink = eventSink;
72                         }
73
74                         public MNS.IMonoTlsProvider Provider {
75                                 get { return provider; }
76                         }
77
78                         public MonoTlsSettings Settings {
79                                 get { return settings; }
80                         }
81
82                         public IMonoTlsEventSink EventSink {
83                                 get { return eventSink; }
84                         }
85                 }
86                 #endif
87
88                 internal bool IsClosed {
89                         get { return _SslState.IsClosed; }
90                 }
91
92                 internal Exception LastError {
93                         get { return lastError; }
94                 }
95
96                 #region IMonoTlsEventSink
97
98                 Exception lastError;
99
100                 void IMonoTlsEventSink.Error (Exception exception)
101                 {
102                         Interlocked.CompareExchange<Exception> (ref lastError, exception, null);
103                 }
104
105                 void IMonoTlsEventSink.ReceivedCloseNotify ()
106                 {
107                 }
108
109                 #endregion
110
111                 internal IAsyncResult BeginShutdown (AsyncCallback asyncCallback, object asyncState)
112                 {
113                         return _SslState.BeginShutdown (asyncCallback, asyncState);
114                 }
115
116                 internal void EndShutdown (IAsyncResult asyncResult)
117                 {
118                         _SslState.EndShutdown (asyncResult);
119                 }
120
121                 internal IAsyncResult BeginRenegotiate (AsyncCallback asyncCallback, object asyncState)
122                 {
123                         return _SslState.BeginRenegotiate (asyncCallback, asyncState);
124                 }
125
126                 internal void EndRenegotiate (IAsyncResult asyncResult)
127                 {
128                         _SslState.EndRenegotiate (asyncResult);
129                 }
130
131                 internal X509Certificate InternalLocalCertificate {
132                         get { return _SslState.InternalLocalCertificate; }
133                 }
134
135                 internal MonoTlsConnectionInfo GetMonoConnectionInfo ()
136                 {
137                         return _SslState.GetMonoConnectionInfo ();
138                 }
139         }
140 }
141 #endif