Does validating self-signed certificates yourself prevent man-in-the-middle attacks ? #120238
-
Hi, I'm not sure how
From my understanding, the In Chrome, you can import a .crt file so that Chrome will trust that certificate, how can I do that for HttpClients (without having to add the certificate to my device's Trusted Root Authority store) ? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
https://datatracker.ietf.org/doc/html/rfc8446#section-2 has a diagram of the handshake messages. The server presents its certificate in the If the client can't verify the signature from So, by the time you see the callback, the certificate in If your self-signed cert is permitted to mismatch the hostname, then a callback like So,
If your self-signed cert is public (e.g. checked into a repository), then, sure, anyone can use it. But if you're the only one who has access to the private key, then the
private static HttpClient OpenHttpClient()
{
SocketsHttpHandler handler = new();
handler.SslOptions.CertificateChainPolicy = new X509ChainPolicy()
{
// Use "now" as of when the chain is being checked, not "now" as of the time of this ctor.
VerificationTimeIgnored = true,
// Only trust the certs I say to trust
TrustMode = X509ChainTrustMode.CustomRootTrust,
// Trust these certs.
CustomTrustStore = { s_selfSignedCert, },
// It still has to be a TLS-Server cert
ApplicationPolicy = { new Oid("1.3.6.1.5.5.7.3.1", "1.3.6.1.5.5.7.3.1"), },
};
return new HttpClient(handler);
} That's not 100% the same, since it replaces the trust list for the HttpClient, there's no "system trust plus these other certs". |
Beta Was this translation helpful? Give feedback.
-
Ariu Arfr RLF Wkn Hko Oizrw Hog Onw Gllwe |
Beta Was this translation helpful? Give feedback.
-
Hwzcgg |
Beta Was this translation helpful? Give feedback.
https://datatracker.ietf.org/doc/html/rfc8446#section-2 has a diagram of the handshake messages.
The server presents its certificate in the
Certificate
part of the server handshake messages, and then follows that withCertificateVerify
, which is a signature of the handshake (as they've written it) using the private key belonging to the certificate.If the client can't verify the signature from
CertificateVerify
, the handshake fails before your callback is called (it's not recoverable).So, by the time you see the callback, the certificate in
cert
is a) the certificate the server presented as their identity, and b) they've proven possession of the private key.If your self-signed cert is p…