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 extern alias PrebuiltSystem;
35 #if MONO_SECURITY_ALIAS
36 using MonoSecurity::Mono.Security.Interface;
38 using Mono.Security.Interface;
41 using XX509CertificateCollection = PrebuiltSystem::System.Security.Cryptography.X509Certificates.X509CertificateCollection;
43 using XX509CertificateCollection = System.Security.Cryptography.X509Certificates.X509CertificateCollection;
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;
58 namespace Mono.Net.Security
62 readonly IMonoTlsProvider provider;
63 readonly HttpWebRequest request;
64 readonly NetworkStream networkStream;
66 IMonoSslStream sslStream;
67 WebExceptionStatus status;
69 internal HttpWebRequest Request {
70 get { return request; }
73 internal IMonoSslStream SslStream {
74 get { return sslStream; }
77 internal WebExceptionStatus ExceptionStatus {
78 get { return status; }
81 internal bool CertificateValidationFailed {
86 readonly ChainValidationHelper validationHelper;
87 readonly MonoTlsSettings settings;
89 public MonoTlsStream (HttpWebRequest request, NetworkStream networkStream)
91 this.request = request;
92 this.networkStream = networkStream;
94 settings = request.TlsSettings;
95 provider = request.TlsProvider ?? MonoTlsProviderFactory.GetProviderInternal ();
96 status = WebExceptionStatus.SecureChannelFailure;
98 validationHelper = ChainValidationHelper.Create (provider.Provider, ref settings, this);
101 internal Stream CreateStream (byte[] buffer)
103 sslStream = provider.CreateSslStream (networkStream, false, settings);
106 sslStream.AuthenticateAsClient (
107 request.Address.Host, (XX509CertificateCollection)(object)request.ClientCertificates,
108 (SslProtocols)ServicePointManager.SecurityProtocol,
109 ServicePointManager.CheckCertificateRevocationList);
111 status = WebExceptionStatus.Success;
113 if (CertificateValidationFailed)
114 status = WebExceptionStatus.TrustFailure;
116 request.ServicePoint.SetClientCertificate (sslStream.InternalLocalCertificate);
117 if (status != WebExceptionStatus.Success)
123 sslStream.Write (buffer, 0, buffer.Length);
125 status = WebExceptionStatus.SendFailure;
130 return sslStream.AuthenticatedStream;