Merge pull request #4048 from kumpera/iface_casting_cleanup
[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 #if SECURITY_DEP                
55                 readonly IMonoTlsProvider provider;
56                 readonly NetworkStream networkStream;           
57 #endif
58                 readonly HttpWebRequest request;
59
60                 IMonoSslStream sslStream;
61                 WebExceptionStatus status;
62
63                 internal HttpWebRequest Request {
64                         get { return request; }
65                 }
66
67                 internal IMonoSslStream SslStream {
68                         get { return sslStream; }
69                 }
70
71                 internal WebExceptionStatus ExceptionStatus {
72                         get { return status; }
73                 }
74
75                 internal bool CertificateValidationFailed {
76                         get; set;
77                 }
78
79 #if SECURITY_DEP
80 //              readonly ChainValidationHelper validationHelper;
81                 readonly MonoTlsSettings settings;
82
83                 public MonoTlsStream (HttpWebRequest request, NetworkStream networkStream)
84                 {
85                         this.request = request;
86                         this.networkStream = networkStream;
87
88                         settings = request.TlsSettings;
89                         provider = request.TlsProvider ?? MonoTlsProviderFactory.GetProviderInternal ();
90                         status = WebExceptionStatus.SecureChannelFailure;
91
92                         /*validationHelper =*/ ChainValidationHelper.Create (provider.Provider, ref settings, this);
93                 }
94
95                 internal Stream CreateStream (byte[] buffer)
96                 {
97                         sslStream = provider.CreateSslStream (networkStream, false, settings);
98
99                         try {
100                                 sslStream.AuthenticateAsClient (
101                                         request.Host, request.ClientCertificates,
102                                         (SslProtocols)ServicePointManager.SecurityProtocol,
103                                         ServicePointManager.CheckCertificateRevocationList);
104
105                                 status = WebExceptionStatus.Success;
106                         } catch (Exception) {
107                                 status = WebExceptionStatus.SecureChannelFailure;
108                                 throw;
109                         } finally {
110                                 if (CertificateValidationFailed)
111                                         status = WebExceptionStatus.TrustFailure;
112
113                                 if (status == WebExceptionStatus.Success)
114                                         request.ServicePoint.UpdateClientCertificate (sslStream.InternalLocalCertificate);
115                                 else {
116                                         request.ServicePoint.UpdateClientCertificate (null);
117                                         sslStream = null;
118                                 }
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 }