Java by API/java.security.cert/CertificateFactory
CertificateFactory: generateCertificate(InputStream inStream) throws CertificateException
import java.io.FileInputStream;
import java.security.Principal;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
public class Main {
public static void main(String args[]) throws Exception {
Certificate[] certpath = new Certificate[args.length];
CertificateFactory cf = CertificateFactory.getInstance("X.509");
for (int i = 0; i < args.length; i++) {
FileInputStream in = new FileInputStream(args[i]);
certpath[i] = cf.generateCertificate(in);
}
for (int i = 0; i < certpath.length - 1; i++) {
Principal issuer = ((X509Certificate) certpath[i]).getIssuerDN();
Principal subject = ((X509Certificate) certpath[i + 1]).getSubjectDN();
if (!issuer.equals(subject)) {
System.out.println("in " + i + " issuer is " + issuer);
System.out.println("But in " + (i + 1));
System.out.println("subject is " + subject);
break;
}
}
}
}
CertificateFactory: generateCertPath(List<? extends Certificate> certificates) throws CertificateException
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.security.cert.CertPath;
import java.security.cert.CertificateFactory;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public class Main {
public static void main(String args[]) throws Exception {
SSLSocketFactory factory = HttpsURLConnection.getDefaultSSLSocketFactory();
SSLSocket socket = (SSLSocket) factory.createSocket("127.0.0.1", 9999);
socket.startHandshake();
SSLSession session = socket.getSession();
java.security.cert.Certificate[] servercerts = session.getPeerCertificates();
List mylist = new ArrayList();
for (int i = 0; i < servercerts.length; i++) {
mylist.add(servercerts[i]);
}
CertificateFactory cf = CertificateFactory.getInstance("X.509");
CertPath cp = cf.generateCertPath(mylist);
FileOutputStream f = new FileOutputStream("CertPath.dat");
ObjectOutputStream b = new ObjectOutputStream(f);
b.writeObject(cp);
}
}
CertificateFactory: getInstance(String type) throws CertificateException
import java.io.FileInputStream;
import java.security.Principal;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
public class Main {
public static void main(String args[]) throws Exception {
Certificate[] certpath = new Certificate[args.length];
CertificateFactory cf = CertificateFactory.getInstance("X.509");
for (int i = 0; i < args.length; i++) {
FileInputStream in = new FileInputStream(args[i]);
certpath[i] = cf.generateCertificate(in);
}
for (int i = 0; i < certpath.length - 1; i++) {
Principal issuer = ((X509Certificate) certpath[i]).getIssuerDN();
Principal subject = ((X509Certificate) certpath[i + 1]).getSubjectDN();
if (!issuer.equals(subject)) {
System.out.println("in " + i + " issuer is " + issuer);
System.out.println("But in " + (i + 1));
System.out.println("subject is " + subject);
break;
}
}
}
}