I'm building a small wrapper java class around some rest webservices we use internally. we use a secured HTTPS connection. Normally, only trusted certificates are allowed by SSL. I would like the user of the wrapper class to be able to give a X509Certificate object as input to the wrapper constructor. This certificate is typically unsecured, but that's the whole point - we want to test internally without hacing a secured one.
public ServicesWrapper(String serviceURL, X509Certificate additionalCertificate)
{
//Add the certificate to the trustmanager of the sslcontext.
}
I've seen the "trust all certificates" TrustManager hack multiple times (like here), but this is not what I need. I would really like to specify that the given (untrusted) certificate should now be considered trusted. Other untrusted certificates are not good. So I implemented my own TrustManager, but I'm stuck at the checkTrusted step:
public class NaiveTrustManager implements X509TrustManager
{
//The extra certificate that should be trusted.
private X509Certificate additionalCertificate;
//Constructor
public NaiveTrustManager(X509Certificate _additionalCertificate)
{
additionalCertificate = _additionalCertificate;
}
...other methods here...
public void checkServerTrusted(java.security.cert.X509Certificate[] certificateChain, String authType)
{
for(X509Certificate c : certificateChain)
{
**?????????????????**
pseudocode:
if (c is equal to additionalcertificate)
stop execution, we trust it!
if (nothing matches)
throw certificatEexception
}
}
}
what is the correct syntax for checking a certificate against a chain of certificates? I checked some attributes but certificates don't seem to have a name or id or anything to match for equality or compare them.