14
14
15
15
namespace DotPulsar . Internal ;
16
16
17
+ using System ;
17
18
using System . Net ;
18
19
using System . Net . Security ;
19
20
using System . Net . Sockets ;
@@ -97,21 +98,31 @@ private static async Task<Stream> GetStream(string host, int port, CancellationT
97
98
private async Task < Stream > EncryptStream ( Stream stream , string host , CancellationToken _ )
98
99
{
99
100
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
+ }
100
108
101
109
try
102
110
{
103
- sslStream = new SslStream ( stream , false , ValidateServerCertificate , null ) ;
111
+ sslStream = new SslStream ( stream , false , Validate , null ) ;
104
112
await sslStream . AuthenticateAsClientAsync ( host , _clientCertificates , SslProtocols . None , _checkCertificateRevocation ) . ConfigureAwait ( false ) ;
105
113
return sslStream ;
106
114
}
107
- catch
115
+ catch ( Exception exception )
108
116
{
109
117
if ( sslStream is null )
110
118
stream . Dispose ( ) ;
111
119
else
112
120
sslStream . Dispose ( ) ;
113
121
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 ) ;
115
126
}
116
127
}
117
128
#else
@@ -123,7 +134,7 @@ private async Task<Stream> EncryptStream(Stream stream, string host, Cancellatio
123
134
bool Validate ( object sender , X509Certificate ? certificate , X509Chain ? chain , SslPolicyErrors sslPolicyErrors )
124
135
{
125
136
policyErrors = sslPolicyErrors ;
126
- return ValidateServerCertificate ( sender , certificate , chain , sslPolicyErrors ) ;
137
+ return ValidateServerCertificate ( certificate , chain , sslPolicyErrors ) ;
127
138
}
128
139
129
140
try
@@ -146,15 +157,15 @@ bool Validate(object sender, X509Certificate? certificate, X509Chain? chain, Ssl
146
157
else
147
158
await sslStream . DisposeAsync ( ) . ConfigureAwait ( false ) ;
148
159
149
- if ( policyErrors ! = SslPolicyErrors . None )
150
- exception . Data . Add ( "SslPolicyErrors" , policyErrors ) ;
160
+ if ( policyErrors = = SslPolicyErrors . None )
161
+ throw ;
151
162
152
- throw ;
163
+ throw new AuthenticationException ( $ "The remote certificate validation failed with SSL policy errors ' { policyErrors } '" , exception ) ;
153
164
}
154
165
}
155
166
#endif
156
167
157
- private bool ValidateServerCertificate ( object sender , X509Certificate ? certificate , X509Chain ? chain , SslPolicyErrors sslPolicyErrors )
168
+ private bool ValidateServerCertificate ( X509Certificate ? certificate , X509Chain ? chain , SslPolicyErrors sslPolicyErrors )
158
169
{
159
170
if ( sslPolicyErrors == SslPolicyErrors . None )
160
171
return true ;
0 commit comments