@@ -51,54 +51,59 @@ public async Task<HttpResponseMessage> SendRequestAsync(HttpRequestMessage httpR
5151 {
5252 try
5353 {
54- _logger . LogInformation ( $ "Sending HttpRequest to mesh: { httpRequestMessage . RequestUri } ") ;
54+ _logger . LogInformation ( $ "Sending HttpRequest to mesh: { httpRequestMessage . RequestUri } ") ;
5555 MailboxConfiguration mailboxConfiguration = _mailboxConfigurationResolver . GetMailboxConfiguration ( mailboxId ) ;
56- var authHeader = MeshAuthorizationHelper . GenerateAuthHeaderValue ( mailboxId , mailboxConfiguration . Password ! , mailboxConfiguration . SharedKey ! ) ;
56+ var authHeader = MeshAuthorizationHelper . GenerateAuthHeaderValue ( mailboxId , mailboxConfiguration . Password ! , mailboxConfiguration . SharedKey ! ) ;
5757 httpRequestMessage . Headers . Add ( "authorization" , authHeader ) ;
58- var result = await SendHttpRequest ( httpRequestMessage , mailboxConfiguration ) ;
58+ var result = await SendHttpRequest ( httpRequestMessage , mailboxConfiguration ) ;
5959 return result ;
6060 }
61- catch ( Exception ex )
61+ catch ( Exception ex )
6262 {
63- _logger . LogCritical ( ex , "Exception encountered while calling MESH API" ) ;
63+ _logger . LogCritical ( ex , "Exception encountered while calling MESH API" ) ;
6464 throw ;
6565 }
6666 }
6767
6868
69- private async Task < HttpResponseMessage > SendHttpRequest ( HttpRequestMessage httpRequestMessage , MailboxConfiguration mailboxConfiguration )
69+ private async Task < HttpResponseMessage > SendHttpRequest ( HttpRequestMessage httpRequestMessage , MailboxConfiguration mailboxConfiguration )
7070 {
7171
7272 using var handler = new HttpClientHandler ( ) ;
7373 httpRequestMessage = AddHeaders ( httpRequestMessage ) ;
7474 var timeInSeconds = _meshConnectConfiguration . TimeoutInSeconds ;
7575
76- HttpClient httpClient ;
7776
78- if ( mailboxConfiguration . Cert != null )
77+ if ( mailboxConfiguration . Cert != null )
7978 {
80- _logger . LogInformation ( "Adding Certificate to HTTP Call" ) ;
81- handler . ClientCertificateOptions = ClientCertificateOption . Manual ;
82- handler . ClientCertificates . Add ( mailboxConfiguration . Cert ) ;
83- handler . SslProtocols = SslProtocols . Tls12 ;
84- handler . ServerCertificateCustomValidationCallback =
85- ( httpRequestMessage , cert , cetChain , policyErrors ) =>
79+ _logger . LogInformation ( "Adding Certificate to HTTP Call" ) ;
80+ handler . ClientCertificateOptions = ClientCertificateOption . Manual ;
81+ handler . ClientCertificates . Add ( mailboxConfiguration . Cert ) ; // this is the pfx file built from the private key and client cert
82+ handler . SslProtocols = SslProtocols . Tls12 ;
83+ handler . ServerCertificateCustomValidationCallback = ( httpRequestMessage , cert , chain , sslPolicyErrors ) =>
84+ {
85+ if ( sslPolicyErrors == System . Net . Security . SslPolicyErrors . None )
8686 {
87- // It is possible to inspect the certificate provided by the server.
88- _logger . LogInformation ( $ "Requested URI: { httpRequestMessage . RequestUri } ") ;
89- _logger . LogInformation ( $ "Effective date: { cert ? . GetEffectiveDateString ( ) } ") ;
90- _logger . LogInformation ( $ "Exp date: { cert ? . GetExpirationDateString ( ) } ") ;
91- _logger . LogInformation ( $ "Issuer: { cert ? . Issuer } ") ;
92- _logger . LogInformation ( $ "Subject: { cert ? . Subject } ") ;
93-
94- // Based on the custom logic it is possible to decide whether the client considers certificate valid or not
95- _logger . LogInformation ( $ "Errors: { policyErrors } ") ;
96- _logger . LogWarning ( "Bypassing Server certificate Validation Check" ) ;
97- return true ;
98- } ;
87+ return true ; // Everything is fine
88+ }
89+
90+ chain . ChainPolicy . TrustMode = X509ChainTrustMode . CustomRootTrust ;
91+
92+ // Manually add the CA certificates to the chain
93+ foreach ( var caCert in mailboxConfiguration . serverSideCertCollection )
94+ {
95+ chain . ChainPolicy . CustomTrustStore . Add ( caCert ) ;
96+ }
97+ if ( cert != null )
98+ {
99+ // Rebuild the chain with added certs
100+ return chain . Build ( cert ) ;
101+ }
102+ return false ;
103+ } ;
99104 }
100105
101- httpClient = new HttpClient ( handler )
106+ var httpClient = new HttpClient ( handler )
102107 {
103108 Timeout = TimeSpan . FromSeconds ( timeInSeconds )
104109 } ;
0 commit comments