Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify a certificate chain using a CA certificate C#

I am trying to connect to a Mosquitto broker. The broker will have a ca.crt and a server.crt. My app will only have the ca.crt.

Upon connection the broker provides both ca.crt and server.crt (certificate chain). How can I validate both against the ca.crt which I already have? ca.crt and the one present on the client are the same.

like image 660
Yamamotooko Avatar asked Oct 20 '25 03:10

Yamamotooko


1 Answers

Use the X509Chain class and put the ca.crt, loaded as X509Certificate2, onto the ExtraStore property of the ChainPolicy property.

var caCert = new X509Certificate2(".\\ca.crt");
var serverCert = new X509Certificate2(".\\server.crt");

X509Chain ch = new X509Chain();
ch.ChainPolicy.RevocationMode = X509RevocationMode.Online;
ch.ChainPolicy.ExtraStore = new X509Certificate2Collection(caCert);
ch.Build (serverCert);
like image 188
Daniel Fisher lennybacon Avatar answered Oct 21 '25 17:10

Daniel Fisher lennybacon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!