Merge pull request #5179 from alexrp/profiler-thread-name
[mono.git] / mcs / class / System / Mono.Net.Security / MonoTlsStream.cs
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:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
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.
26
27 #if SECURITY_DEP
28 #if MONO_SECURITY_ALIAS
29 extern alias MonoSecurity;
30 #endif
31
32 #if MONO_SECURITY_ALIAS
33 using MonoSecurity::Mono.Security.Interface;
34 #else
35 using Mono.Security.Interface;
36 #endif
37 #endif
38
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;
50
51 namespace Mono.Net.Security
52 {
53         class MonoTlsStream
54         {
55 #if SECURITY_DEP                
56                 readonly MonoTlsProvider provider;
57                 readonly NetworkStream networkStream;           
58                 readonly HttpWebRequest request;
59
60                 readonly MonoTlsSettings settings;
61
62                 internal HttpWebRequest Request {
63                         get { return request; }
64                 }
65
66                 IMonoSslStream sslStream;
67
68                 internal IMonoSslStream SslStream {
69                         get { return sslStream; }
70                 }
71 #else
72                 const string EXCEPTION_MESSAGE = "System.Net.Security.SslStream is not supported on the current platform.";
73 #endif
74
75                 WebExceptionStatus status;
76
77                 internal WebExceptionStatus ExceptionStatus {
78                         get { return status; }
79                 }
80
81                 internal bool CertificateValidationFailed {
82                         get; set;
83                 }
84
85                 public MonoTlsStream (HttpWebRequest request, NetworkStream networkStream)
86                 {
87 #if SECURITY_DEP
88                         this.request = request;
89                         this.networkStream = networkStream;
90
91                         settings = request.TlsSettings;
92                         provider = request.TlsProvider ?? MonoTlsProviderFactory.GetProviderInternal ();
93                         status = WebExceptionStatus.SecureChannelFailure;
94
95                         ChainValidationHelper.Create (provider, ref settings, this);
96 #else
97                         throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
98 #endif
99                 }
100
101                 internal Stream CreateStream (byte[] buffer)
102                 {
103 #if SECURITY_DEP
104                         sslStream = provider.CreateSslStream (networkStream, false, settings);
105
106                         try {
107                                 var host = request.Host;
108                                 if (!string.IsNullOrEmpty (host)) {
109                                         var pos = host.IndexOf (':');
110                                         if (pos > 0)
111                                                 host = host.Substring (0, pos);
112                                 }
113
114                                 sslStream.AuthenticateAsClient (
115                                         host, request.ClientCertificates,
116                                         (SslProtocols)ServicePointManager.SecurityProtocol,
117                                         ServicePointManager.CheckCertificateRevocationList);
118
119                                 status = WebExceptionStatus.Success;
120                         } catch {
121                                 status = WebExceptionStatus.SecureChannelFailure;
122                                 throw;
123                         } finally {
124                                 if (CertificateValidationFailed)
125                                         status = WebExceptionStatus.TrustFailure;
126
127                                 if (status == WebExceptionStatus.Success)
128                                         request.ServicePoint.UpdateClientCertificate (sslStream.InternalLocalCertificate);
129                                 else {
130                                         request.ServicePoint.UpdateClientCertificate (null);
131                                         sslStream = null;
132                                 }
133                         }
134
135                         try {
136                                 if (buffer != null)
137                                         sslStream.Write (buffer, 0, buffer.Length);
138                         } catch {
139                                 status = WebExceptionStatus.SendFailure;
140                                 sslStream = null;
141                                 throw;
142                         }
143
144                         return sslStream.AuthenticatedStream;
145 #else
146                         throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);
147 #endif
148                 }
149         }
150 }