1- using Amqp ;
1+ using System . Net . Security ;
2+ using System . Security . Authentication ;
3+ using System . Security . Cryptography . X509Certificates ;
4+ using Amqp ;
25
36namespace RabbitMQ . AMQP . Client . Impl ;
47
@@ -95,21 +98,35 @@ public class ConnectionSettings : IConnectionSettings
9598 private readonly Address _address ;
9699 private readonly string _connectionName = "" ;
97100 private readonly string _virtualHost = "/" ;
101+ private readonly ITlsSettings ? _tlsSettings ;
98102
99- public ConnectionSettings ( string address )
103+ public ConnectionSettings ( string address , ITlsSettings ? tlsSettings = null )
100104 {
101105 _address = new Address ( address ) ;
106+ _tlsSettings = tlsSettings ;
107+
108+ if ( _address . UseSsl && _tlsSettings == null )
109+ {
110+ _tlsSettings = new TlsSettings ( ) ;
111+ }
102112 }
103113
104114 public ConnectionSettings ( string host , int port ,
105115 string user , string password ,
106- string virtualHost , string scheme , string connectionName )
116+ string virtualHost , string scheme , string connectionName ,
117+ ITlsSettings ? tlsSettings = null )
107118 {
108119 _address = new Address ( host : host , port : port ,
109120 user : user , password : password ,
110121 path : "/" , scheme : scheme ) ;
111122 _connectionName = connectionName ;
112123 _virtualHost = virtualHost ;
124+ _tlsSettings = tlsSettings ;
125+
126+ if ( _address . UseSsl && _tlsSettings == null )
127+ {
128+ _tlsSettings = new TlsSettings ( ) ;
129+ }
113130 }
114131
115132 public string Host => _address . Host ;
@@ -122,6 +139,8 @@ public ConnectionSettings(string host, int port,
122139 public string Path => _address . Path ;
123140 public bool UseSsl => _address . UseSsl ;
124141
142+ public ITlsSettings ? TlsSettings => _tlsSettings ;
143+
125144 public override string ToString ( )
126145 {
127146 return
@@ -307,3 +326,44 @@ public override string ToString()
307326 return $ "BackOffDelayPolicy{{ Attempt={ _attempt } , TotalAttempt={ _totalAttempt } , IsActive={ IsActive } }}";
308327 }
309328}
329+
330+ public class TlsSettings : ITlsSettings
331+ {
332+ internal const SslProtocols DefaultSslProtocols = SslProtocols . None ;
333+
334+ private readonly SslProtocols _protocols ;
335+ private readonly X509CertificateCollection _clientCertificates ;
336+ private readonly bool _checkCertificateRevocation = false ;
337+ private readonly RemoteCertificateValidationCallback ? _remoteCertificateValidationCallback ;
338+ private readonly LocalCertificateSelectionCallback ? _localCertificateSelectionCallback ;
339+
340+ public TlsSettings ( ) : this ( DefaultSslProtocols )
341+ {
342+ }
343+
344+ public TlsSettings ( SslProtocols protocols )
345+ {
346+ _protocols = protocols ;
347+ _clientCertificates = new X509CertificateCollection ( ) ;
348+ _remoteCertificateValidationCallback = trustEverythingCertValidationCallback ;
349+ _localCertificateSelectionCallback = null ;
350+ }
351+
352+ public SslProtocols Protocols => _protocols ;
353+
354+ public X509CertificateCollection ClientCertificates => _clientCertificates ;
355+
356+ public bool CheckCertificateRevocation => _checkCertificateRevocation ;
357+
358+ public RemoteCertificateValidationCallback ? RemoteCertificateValidationCallback
359+ => _remoteCertificateValidationCallback ;
360+
361+ public LocalCertificateSelectionCallback ? LocalCertificateSelectionCallback
362+ => _localCertificateSelectionCallback ;
363+
364+ private static bool trustEverythingCertValidationCallback ( object sender , X509Certificate ? certificate ,
365+ X509Chain ? chain , SslPolicyErrors sslPolicyErrors )
366+ {
367+ return true ;
368+ }
369+ }
0 commit comments