File: MobileTlsContext.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (306 lines) | stat: -rw-r--r-- 8,527 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//
// MobileTlsContext.cs
//
// Author:
//       Martin Baulig <martin.baulig@xamarin.com>
//
// Copyright (c) 2015 Xamarin, Inc.
//

#if SECURITY_DEP
#if MONO_SECURITY_ALIAS
extern alias MonoSecurity;
#endif

#if MONO_SECURITY_ALIAS
using MonoSecurity::Mono.Security.Interface;
#else
using Mono.Security.Interface;
#endif

using System;
using System.IO;
using SD = System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;

namespace Mono.Net.Security
{
	abstract class MobileTlsContext : IDisposable
	{
		ChainValidationHelper certificateValidator;

		protected MobileTlsContext (MobileAuthenticatedStream parent, MonoSslAuthenticationOptions options)
		{
			Parent = parent;
			Options = options;
			IsServer = options.ServerMode;
			EnabledProtocols = options.EnabledSslProtocols;

			if (options.ServerMode) {
				LocalServerCertificate = options.ServerCertificate;
				AskForClientCertificate = options.ClientCertificateRequired;
			} else {
				ClientCertificates = options.ClientCertificates;
				TargetHost = options.TargetHost;
				ServerName = options.TargetHost;
				if (!string.IsNullOrEmpty (ServerName)) {
					var pos = ServerName.IndexOf (':');
					if (pos > 0)
						ServerName = ServerName.Substring (0, pos);
				}
			}

			certificateValidator = ChainValidationHelper.GetInternalValidator (
				parent.SslStream, parent.Provider, parent.Settings);
		}

		internal MonoSslAuthenticationOptions Options {
			get;
		}

		internal MobileAuthenticatedStream Parent {
			get;
		}

		public MonoTlsSettings Settings => Parent.Settings;

		public MonoTlsProvider Provider => Parent.Provider;

		[SD.Conditional ("MONO_TLS_DEBUG")]
		protected void Debug (string message, params object[] args)
		{
			Parent.Debug ("{0}: {1}", GetType ().Name, string.Format (message, args));
		}

		public abstract bool HasContext {
			get;
		}

		public abstract bool IsAuthenticated {
			get;
		}

		public bool IsServer {
			get;
		}

		internal string TargetHost {
			get;
		}

		protected string ServerName {
			get;
		}

		protected bool AskForClientCertificate {
			get;
		}

		protected SslProtocols EnabledProtocols {
			get;
		}

		protected X509CertificateCollection ClientCertificates {
			get;
		}

		internal bool AllowRenegotiation {
			get { return false; }
		}

		protected void GetProtocolVersions (out TlsProtocolCode? min, out TlsProtocolCode? max)
		{
			if ((EnabledProtocols & SslProtocols.Tls) != 0)
				min = TlsProtocolCode.Tls10;
			else if ((EnabledProtocols & SslProtocols.Tls11) != 0)
				min = TlsProtocolCode.Tls11;
			else if ((EnabledProtocols & SslProtocols.Tls12) != 0)
				min = TlsProtocolCode.Tls12;
			else
				min = null;

			if ((EnabledProtocols & SslProtocols.Tls12) != 0)
				max = TlsProtocolCode.Tls12;
			else if ((EnabledProtocols & SslProtocols.Tls11) != 0)
				max = TlsProtocolCode.Tls11;
			else if ((EnabledProtocols & SslProtocols.Tls) != 0)
				max = TlsProtocolCode.Tls10;
			else
				max = null;
		}

		public abstract void StartHandshake ();

		public abstract bool ProcessHandshake ();

		public abstract void FinishHandshake ();

		public abstract MonoTlsConnectionInfo ConnectionInfo {
			get;
		}

		internal X509Certificate LocalServerCertificate {
			get;
			private set;
		}

		internal abstract bool IsRemoteCertificateAvailable {
			get;
		}

		internal abstract X509Certificate LocalClientCertificate {
			get;
		}

		public abstract X509Certificate2 RemoteCertificate {
			get;
		}

		public abstract TlsProtocols NegotiatedProtocol {
			get;
		}

		public abstract void Flush ();

		public abstract (int ret, bool wantMore) Read (byte[] buffer, int offset, int count);

		public abstract (int ret, bool wantMore) Write (byte[] buffer, int offset, int count);

		public abstract void Shutdown ();

		public abstract bool PendingRenegotiation ();

		protected bool ValidateCertificate (X509Certificate2 leaf, X509Chain chain)
		{
			var result = certificateValidator.ValidateCertificate (TargetHost, IsServer, leaf, chain);
			return result != null && result.Trusted && !result.UserDenied;
		}

		protected bool ValidateCertificate (X509Certificate2Collection certificates)
		{
			var result = certificateValidator.ValidateCertificate (TargetHost, IsServer, certificates);
			return result != null && result.Trusted && !result.UserDenied;
		}

		protected X509Certificate SelectServerCertificate (string serverIdentity)
		{
			// There are three options for selecting the server certificate. When
			// selecting which to use, we prioritize the new ServerCertSelectionDelegate
			// API. If the new API isn't used we call LocalCertSelectionCallback (for compat
			// with .NET Framework), and if neither is set we fall back to using ServerCertificate.

			if (Options.ServerCertSelectionDelegate != null) {
				LocalServerCertificate = Options.ServerCertSelectionDelegate (serverIdentity);

				if (LocalServerCertificate == null)
					throw new AuthenticationException (SR.net_ssl_io_no_server_cert);
			} else if (Settings.ClientCertificateSelectionCallback != null) {
				var tempCollection = new X509CertificateCollection ();
				tempCollection.Add (Options.ServerCertificate);
				// We pass string.Empty here to maintain strict compatability with .NET Framework.
				LocalServerCertificate = Settings.ClientCertificateSelectionCallback (string.Empty, tempCollection, null, Array.Empty<string>());
			} else {
				LocalServerCertificate = Options.ServerCertificate;
			}

			if (LocalServerCertificate == null)
				throw new NotSupportedException (SR.net_ssl_io_no_server_cert);

			return LocalServerCertificate;
		}

		protected X509Certificate SelectClientCertificate (string[] acceptableIssuers)
		{
			if (Settings.DisallowUnauthenticatedCertificateRequest && !IsAuthenticated)
				return null;

			if (RemoteCertificate == null)
				throw new TlsException (AlertDescription.InternalError, "Cannot request client certificate before receiving one from the server.");

			/*
			 * We need to pass null to the user selection callback during the initial handshake, to allow the callback to distinguish
			 * between an authenticated and unauthenticated session.
			 */
			X509Certificate certificate;
			var selected = certificateValidator.SelectClientCertificate (
				TargetHost, ClientCertificates, IsAuthenticated ? RemoteCertificate : null, acceptableIssuers, out certificate);
			if (selected)
				return certificate;

			if (ClientCertificates == null || ClientCertificates.Count == 0)
				return null;

			/*
			 * .NET actually scans the entire collection to ensure the selected certificate has a private key in it.
			 *
			 * However, since we do not support private key retrieval from the key store, we require all certificates
			 * to have a private key in them (explicitly or implicitly via OS X keychain lookup).
			 */
			if (acceptableIssuers == null || acceptableIssuers.Length == 0)
				return ClientCertificates [0];

			// Copied from the referencesource implementation in referencesource/System/net/System/Net/_SecureChannel.cs.
			for (int i = 0; i < ClientCertificates.Count; i++) {
				var certificate2 = ClientCertificates[i] as X509Certificate2;
				if (certificate2 == null)
					continue;

				X509Chain chain = null;
				try {
					chain = new X509Chain ();
					chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
					chain.ChainPolicy.VerificationFlags = X509VerificationFlags.IgnoreInvalidName;
					chain.Build (certificate2);

					//
					// We ignore any errors happened with chain.
					// Consider: try to locate the "best" client cert that has no errors and the lognest validity internal
					//
					if (chain.ChainElements.Count == 0)
						continue;
					for (int ii=0; ii< chain.ChainElements.Count; ++ii) {
						var issuer = chain.ChainElements[ii].Certificate.Issuer;
						if (Array.IndexOf (acceptableIssuers, issuer) != -1)
							return certificate2;
					}
				} catch {
					; // ignore errors
				} finally {
					if (chain != null)
						chain.Reset ();
				}
			}

			// No certificate matches.
			return null;
		}

		public abstract bool CanRenegotiate {
			get;
		}

		public abstract void Renegotiate ();

		public void Dispose ()
		{
			Dispose (true);
			GC.SuppressFinalize (this);
		}

		protected virtual void Dispose (bool disposing)
		{
		}

		~MobileTlsContext ()
		{
			Dispose (false);
		}
	}
}

#endif