Skip to content

Commit e2025a0

Browse files
Return more info when the remote certificate validation fails
1 parent f882cbe commit e2025a0

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/DotPulsar/Internal/Connector.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
namespace DotPulsar.Internal;
1616

17+
using System;
1718
using System.Net;
1819
using System.Net.Security;
1920
using System.Net.Sockets;
@@ -97,21 +98,31 @@ private static async Task<Stream> GetStream(string host, int port, CancellationT
9798
private async Task<Stream> EncryptStream(Stream stream, string host, CancellationToken _)
9899
{
99100
SslStream? sslStream = null;
101+
var policyErrors = SslPolicyErrors.None;
102+
103+
bool Validate(object sender, X509Certificate? certificate, X509Chain? chain, SslPolicyErrors sslPolicyErrors)
104+
{
105+
policyErrors = sslPolicyErrors;
106+
return ValidateServerCertificate(certificate, chain, sslPolicyErrors);
107+
}
100108

101109
try
102110
{
103-
sslStream = new SslStream(stream, false, ValidateServerCertificate, null);
111+
sslStream = new SslStream(stream, false, Validate, null);
104112
await sslStream.AuthenticateAsClientAsync(host, _clientCertificates, SslProtocols.None, _checkCertificateRevocation).ConfigureAwait(false);
105113
return sslStream;
106114
}
107-
catch
115+
catch (Exception exception)
108116
{
109117
if (sslStream is null)
110118
stream.Dispose();
111119
else
112120
sslStream.Dispose();
113121

114-
throw;
122+
if (policyErrors == SslPolicyErrors.None)
123+
throw;
124+
125+
throw new AuthenticationException($"The remote certificate validation failed with SSL policy errors '{policyErrors}'", exception);
115126
}
116127
}
117128
#else
@@ -123,7 +134,7 @@ private async Task<Stream> EncryptStream(Stream stream, string host, Cancellatio
123134
bool Validate(object sender, X509Certificate? certificate, X509Chain? chain, SslPolicyErrors sslPolicyErrors)
124135
{
125136
policyErrors = sslPolicyErrors;
126-
return ValidateServerCertificate(sender, certificate, chain, sslPolicyErrors);
137+
return ValidateServerCertificate(certificate, chain, sslPolicyErrors);
127138
}
128139

129140
try
@@ -146,15 +157,15 @@ bool Validate(object sender, X509Certificate? certificate, X509Chain? chain, Ssl
146157
else
147158
await sslStream.DisposeAsync().ConfigureAwait(false);
148159

149-
if (policyErrors != SslPolicyErrors.None)
150-
exception.Data.Add("SslPolicyErrors", policyErrors);
160+
if (policyErrors == SslPolicyErrors.None)
161+
throw;
151162

152-
throw;
163+
throw new AuthenticationException($"The remote certificate validation failed with SSL policy errors '{policyErrors}'", exception);
153164
}
154165
}
155166
#endif
156167

157-
private bool ValidateServerCertificate(object sender, X509Certificate? certificate, X509Chain? chain, SslPolicyErrors sslPolicyErrors)
168+
private bool ValidateServerCertificate(X509Certificate? certificate, X509Chain? chain, SslPolicyErrors sslPolicyErrors)
158169
{
159170
if (sslPolicyErrors == SslPolicyErrors.None)
160171
return true;

0 commit comments

Comments
 (0)