5 // Martin Baulig <martin.baulig@xamarin.com>
7 // Copyright (c) 2015 Xamarin, Inc.
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
28 #if MONO_SECURITY_ALIAS
29 extern alias MonoSecurity;
32 #if MONO_SECURITY_ALIAS
33 using MonoSecurity::Mono.Security.Interface;
35 using Mono.Security.Interface;
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;
50 namespace Mono.Net.Security
55 readonly IMonoTlsProvider provider;
56 readonly NetworkStream networkStream;
58 readonly HttpWebRequest request;
60 IMonoSslStream sslStream;
61 WebExceptionStatus status;
63 internal HttpWebRequest Request {
64 get { return request; }
67 internal IMonoSslStream SslStream {
68 get { return sslStream; }
71 internal WebExceptionStatus ExceptionStatus {
72 get { return status; }
75 internal bool CertificateValidationFailed {
80 // readonly ChainValidationHelper validationHelper;
81 readonly MonoTlsSettings settings;
83 public MonoTlsStream (HttpWebRequest request, NetworkStream networkStream)
85 this.request = request;
86 this.networkStream = networkStream;
88 settings = request.TlsSettings;
89 provider = request.TlsProvider ?? MonoTlsProviderFactory.GetProviderInternal ();
90 status = WebExceptionStatus.SecureChannelFailure;
92 /*validationHelper =*/ ChainValidationHelper.Create (provider.Provider, ref settings, this);
95 internal Stream CreateStream (byte[] buffer)
97 sslStream = provider.CreateSslStream (networkStream, false, settings);
100 sslStream.AuthenticateAsClient (
101 request.Host, request.ClientCertificates,
102 (SslProtocols)ServicePointManager.SecurityProtocol,
103 ServicePointManager.CheckCertificateRevocationList);
105 status = WebExceptionStatus.Success;
106 } catch (Exception) {
107 status = WebExceptionStatus.SecureChannelFailure;
110 if (CertificateValidationFailed)
111 status = WebExceptionStatus.TrustFailure;
113 if (status == WebExceptionStatus.Success)
114 request.ServicePoint.UpdateClientCertificate (sslStream.InternalLocalCertificate);
116 request.ServicePoint.UpdateClientCertificate (null);
123 sslStream.Write (buffer, 0, buffer.Length);
125 status = WebExceptionStatus.SendFailure;
130 return sslStream.AuthenticatedStream;