I trying to implement X509TrustManager
to check server certificate with the certificate that I have in my application (say in classes folder).
I am hitting and https
web site so when checkServerTrusted()
method get called the method is passed an X.509 certificate array.
Can I safely assume that the certificate of the server that is called is the first in the array?
Just to clarify, in the checkServerTrusted() method, I have to validate the server's certificate.
Here is code look like :
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException{
InputStream inStream;
try {
inStream = new FileInputStream("E:\\Desktop\\cert\\domain.crt");
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate Mycert = (X509Certificate)cf.generateCertificate(inStream);
inStream.close();
if (certs == null || certs.length == 0 || authType == null
|| authType.length() == 0) {
throw new IllegalArgumentException("null or zero-length parameter");
}
certs[0].verify(Mycert.getPublicKey());
} catch (Exception e) {
// TODO Auto-generated catch block
throw new CertificateException("error in validating certificate" , e);
}
}
So is it safe to verify with first certificate only?