Reputation: 12670
I'm trying to implement a custom X509TrustManager
which tells the user about the certificate and gives them the ability to continue to use the server despite the problem.
I have hooked almost everything together and started testing against various certificates on badssl.com.
When I visit expired.badssl.com in Safari, I see three certificates:
* COMODO RSA Certification Authority
* COMODO RSA Domain Validation Secure Server CA
* *.badssl.com
And my application gets the full chain.
When I visit wrong.host.badssl.com, though, Safari still shows three certificates, but my application only sees two:
* DigiCert Global Root CA (Not in the array passed to the method!)
* DigiCert SHA2 Secure Server CA
* *.badssl.com
I assume I can use the "Issuer" name from the "DigiCert SHA2 Secure Server CA" certificate to find the root certificate somehow, but where do I find it?
Upvotes: 0
Views: 2020
Reputation: 38771
The root certificate used to validate the server's chain (or in general any received chain) should be found in the local truststore. That's exactly what the default TrustManager
does -- it looks in the local truststore, or more exactly it instantiates a CertPathValidator
which (normally) defaults to PKIXValidator
that looks in a truststore (a KeyStore containing cert entries) normally initialized from a local file defaulting to JRE/lib/security/cacerts
, and then executes that validator which does validation by looking up the root from said truststore using a HashMap
by subject name.
This is stated, albeit briefly, in RFC 5246 for TLS 1.2 (unchanged from 4346 for 1.1 and 2346 for 1.0).
If you look more closely you'll find that the cases aren't actually as different as you think.
wrong.host.badssl.com
serves a cert chain consisting of:
*.badssl.com
fails for wrong.host.badssl.com
because certificate wildcard-name matching only does one (leftmost) DNS label not more (this would need two).expired.badssl.com
serves a cert chain consisting of:
(PS for checklisters: I use SHA1 fingerprints because they're easier to cut&paste and are still good enough -- SHA1 collisions have been found for some data, but not full certs which are much harder because of the signature correlation, and anyway only second-preimage would actually be a problem and AFAIK no one has made any progress on that at all.)
The question in your title, 'given only the leaf cert', should never occur in TLS because of the RFCs referenced above. But if it does, this is nearly a duplicate of OpenSSL generate certificate chain and the same logic applies: follow CAIssuers, or (with luck) chain on the CT log(s) -- as I did manually for the above!
Upvotes: 5