C# how to trust certificate

0

I´ve got a signed certificate and its root certificate, I´ve installed both ROOT~> Vertrauenswürdige Stammzertifizierungsstellen (Trusted Root Certification Authorities) CERTIFICATE ~> Vetrauenswürdige Herausgeber (Trusted Publisher)

Also my SOAP Client has this code snippet before sending the request:

using (cert = new X509Certificate2(certFile))
 { 
     // set certificate to SOAP Client
     client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine,
     StoreName.TrustedPublisher, X509FindType.FindByThumbprint, cert.Thumbprint);

    if (!client.ClientCredentials.ClientCertificate.Certificate.Verify()) 
    {
        // Log
        throw new Exception("Client Certificate Verification failed");
    }
 }

Ther Verify Method returns false and if I run the code any way, this exception is thrown:

System.ServiceModel.Security.SecurityNegotiationException: Could not establish trust relationship for the SSL/TLS secure channel with authority 'MyCertNoTheROOTCert'.
 ---> System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
 ---> System.Security.Authentication.AuthenticationException: Authentication failed because the remote party sent a TLS alert: 'HandshakeFailure'.
 ---> System.ComponentModel.Win32Exception (0x80090326): Das Format der empfangenen Nachricht war unerwartet oder fehlerhaft.
   --- End of inner exception stack trace ---
   at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](TIOAdapter adapter, Boolean receiveFirst, Byte[] reAuthenticationData, Boolean isApm)
   at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Boolean async, Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Boolean async, Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.AuthenticationHelper.SendWithAuthAsync(HttpRequestMessage request, Uri authUri, Boolean async, ICredentials credentials, Boolean preAuthenticate, Boolean isProxyAuth, Boolean doRequestAuth, HttpConnectionPool pool, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.SendAsyncCore(HttpRequestMessage request, HttpCompletionOption completionOption, Boolean async, Boolean emitTelemetryStartStop, CancellationToken cancellationToken)
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpClientRequestChannel.HttpClientChannelAsyncRequest.SendRequestAsync(Message message, TimeoutHelper timeoutHelper)
   --- End of inner exception stack trace ---
   at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.<>c__DisplayClass1_0.<CreateGenericTask>b__0(IAsyncResult asyncResult)

EDIT:

Thanks to @bartonjs I´ve found the X509Chain but with the following code the Build Method does return false:

using (cert = new X509Certificate2(certFile)
        using (var certChain = new X509Chain())
        { 
            // build up cert from chain
            certChain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
            var buildValid = certChain.Build(cert);
            if(!buildValid)
                throw new Exception("Damn fuu thing");
        }
c#
ssl
https
certificate
root-certificate
asked on Stack Overflow Apr 23, 2021 by Max Mustermann • edited Apr 25, 2021 by Max Mustermann

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0