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