Merge pull request #3753 from henricm/enable-windowsbase-tests-on-windows
[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.Tasks;
45 using System.Security.Authentication;
46 using System.Security.Cryptography.X509Certificates;
47 using System.Security.Principal;
48 using System.Security.Cryptography;
49
50 namespace Mono.Net.Security
51 {
52         class MonoTlsStream
53         {
54                 readonly IMonoTlsProvider provider;
55                 readonly HttpWebRequest request;
56                 readonly NetworkStream networkStream;
57
58                 IMonoSslStream sslStream;
59                 WebExceptionStatus status;
60
61                 internal HttpWebRequest Request {
62                         get { return request; }
63                 }
64
65                 internal IMonoSslStream SslStream {
66                         get { return sslStream; }
67                 }
68
69                 internal WebExceptionStatus ExceptionStatus {
70                         get { return status; }
71                 }
72
73                 internal bool CertificateValidationFailed {
74                         get; set;
75                 }
76
77 #if SECURITY_DEP
78 //              readonly ChainValidationHelper validationHelper;
79                 readonly MonoTlsSettings settings;
80
81                 public MonoTlsStream (HttpWebRequest request, NetworkStream networkStream)
82                 {
83                         this.request = request;
84                         this.networkStream = networkStream;
85
86                         settings = request.TlsSettings;
87                         provider = request.TlsProvider ?? MonoTlsProviderFactory.GetProviderInternal ();
88                         status = WebExceptionStatus.SecureChannelFailure;
89
90                         /*validationHelper =*/ ChainValidationHelper.Create (provider.Provider, ref settings, this);
91                 }
92
93                 internal Stream CreateStream (byte[] buffer)
94                 {
95                         sslStream = provider.CreateSslStream (networkStream, false, settings);
96
97                         try {
98                                 sslStream.AuthenticateAsClient (
99                                         request.Host, request.ClientCertificates,
100                                         (SslProtocols)ServicePointManager.SecurityProtocol,
101                                         ServicePointManager.CheckCertificateRevocationList);
102
103                                 status = WebExceptionStatus.Success;
104                         } catch (Exception ex) {
105                                 status = WebExceptionStatus.SecureChannelFailure;
106                                 throw;
107                         } finally {
108                                 if (CertificateValidationFailed)
109                                         status = WebExceptionStatus.TrustFailure;
110
111                                 if (status == WebExceptionStatus.Success)
112                                         request.ServicePoint.UpdateClientCertificate (sslStream.InternalLocalCertificate);
113                                 else {
114                                         request.ServicePoint.UpdateClientCertificate (null);
115                                         sslStream = null;
116                                 }
117                         }
118
119                         try {
120                                 if (buffer != null)
121                                         sslStream.Write (buffer, 0, buffer.Length);
122                         } catch {
123                                 status = WebExceptionStatus.SendFailure;
124                                 sslStream = null;
125                                 throw;
126                         }
127
128                         return sslStream.AuthenticatedStream;
129                 }
130 #endif
131         }
132 }