Fixes https://github.com/mono/mono/issues/15805 at least in terms of what may be...
[mono-project.git] / mcs / class / System / Mono.Net.Security / MonoTlsStream.cs
blob935df6acaf078aa1277d408485d3a4f440a84fdf
1 //
2 // MonoTlsStream.cs
3 //
4 // Author:
5 // Martin Baulig <martin.baulig@xamarin.com>
6 //
7 // Copyright (c) 2015 Xamarin, Inc.
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
27 #if SECURITY_DEP
28 #if MONO_SECURITY_ALIAS
29 extern alias MonoSecurity;
30 #endif
32 #if MONO_SECURITY_ALIAS
33 using MonoSecurity::Mono.Security.Interface;
34 #else
35 using Mono.Security.Interface;
36 #endif
37 #endif
39 using System;
40 using System.IO;
41 using System.Net;
42 using System.Net.Sockets;
43 using System.Net.Security;
44 using System.Threading;
45 using System.Threading.Tasks;
46 using System.Security.Authentication;
47 using System.Security.Cryptography.X509Certificates;
48 using System.Security.Principal;
49 using System.Security.Cryptography;
51 namespace Mono.Net.Security
53 class MonoTlsStream : IDisposable
55 #if SECURITY_DEP
56 readonly MobileTlsProvider provider;
57 readonly NetworkStream networkStream;
58 readonly HttpWebRequest request;
60 readonly MonoTlsSettings settings;
62 internal HttpWebRequest Request {
63 get { return request; }
66 SslStream sslStream;
68 internal SslStream SslStream {
69 get { return sslStream; }
71 #else
72 const string EXCEPTION_MESSAGE = "System.Net.Security.SslStream is not supported on the current platform.";
73 #endif
75 WebExceptionStatus status;
77 internal WebExceptionStatus ExceptionStatus {
78 get { return status; }
81 internal bool CertificateValidationFailed {
82 get; set;
85 public MonoTlsStream (HttpWebRequest request, NetworkStream networkStream)
87 #if SECURITY_DEP
88 this.request = request;
89 this.networkStream = networkStream;
91 settings = request.TlsSettings;
92 provider = request.TlsProvider ?? MonoTlsProviderFactory.GetProviderInternal ();
93 status = WebExceptionStatus.SecureChannelFailure;
95 ChainValidationHelper.Create (provider, ref settings, this);
96 #else
97 status = WebExceptionStatus.SecureChannelFailure;
98 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
99 #endif
102 internal async Task<Stream> CreateStream (WebConnectionTunnel tunnel, CancellationToken cancellationToken)
104 #if SECURITY_DEP
105 var socket = networkStream.InternalSocket;
106 WebConnection.Debug ($"MONO TLS STREAM CREATE STREAM: {socket.ID}");
107 sslStream = new SslStream (networkStream, false, provider, settings);
109 try {
110 var host = request.Host;
111 if (!string.IsNullOrEmpty (host)) {
112 var pos = host.IndexOf (':');
113 if (pos > 0)
114 host = host.Substring (0, pos);
117 await sslStream.AuthenticateAsClientAsync (
118 host, request.ClientCertificates,
119 (SslProtocols)ServicePointManager.SecurityProtocol,
120 ServicePointManager.CheckCertificateRevocationList).ConfigureAwait (false);
122 status = WebExceptionStatus.Success;
124 request.ServicePoint.UpdateClientCertificate (sslStream.LocalCertificate);
125 } catch (Exception ex) {
126 WebConnection.Debug ($"MONO TLS STREAM ERROR: {socket.ID} {socket.CleanedUp} {ex.Message}");
127 if (socket.CleanedUp)
128 status = WebExceptionStatus.RequestCanceled;
129 else if (CertificateValidationFailed)
130 status = WebExceptionStatus.TrustFailure;
131 else
132 status = WebExceptionStatus.SecureChannelFailure;
134 request.ServicePoint.UpdateClientCertificate (null);
135 CloseSslStream ();
136 throw;
139 try {
140 if (tunnel?.Data != null)
141 await sslStream.WriteAsync (tunnel.Data, 0, tunnel.Data.Length, cancellationToken).ConfigureAwait (false);
142 } catch {
143 status = WebExceptionStatus.SendFailure;
144 CloseSslStream ();
145 throw;
148 return sslStream;
149 #else
150 throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
151 #endif
154 public void Dispose ()
156 CloseSslStream ();
159 void CloseSslStream () {
160 if (sslStream != null) {
161 sslStream.Dispose ();
162 sslStream = null;