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