Thursday 15 October 2015

Java: Reading pkcs12 certificate information

Following program used to get information about pkcs12 certificate and to check whether certificate is expired or not.

import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.cert.CertificateExpiredException;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
import java.util.Objects;

import javax.security.auth.x500.X500Principal;

public class Pkcs12Util {
 private File certificate;
 private String password;
 private KeyStore keyStore;

 public Pkcs12Util(String certPath, String password) {
  this(new File(certPath), password);
 }

 public Pkcs12Util(File certificate, String password) {
  Objects.nonNull(certificate);
  Objects.nonNull(password);

  this.certificate = certificate;
  this.password = password;
  init();
 }

 private void init() {
  try {
   keyStore = KeyStore.getInstance("pkcs12");
   keyStore.load(new FileInputStream(certificate),
     password.toCharArray());
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 /**
  * @return true if the certificate is valid, false if certificate expired.
  */
 public boolean isValidCert() {
  try {
   Enumeration<String> e = keyStore.aliases();
   while (e.hasMoreElements()) {
    String alias = e.nextElement();
    X509Certificate certificate = (X509Certificate) keyStore
      .getCertificate(alias);
    try {
     certificate.checkValidity();
     return true;
    } catch (CertificateExpiredException ex) {
     return false;
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return true;
 }

 /**
  * @return Issuer name
  */
 public String getIssuerName() {
  try {
   Enumeration<String> e = keyStore.aliases();
   while (e.hasMoreElements()) {
    String alias = e.nextElement();
    X509Certificate certificate = (X509Certificate) keyStore
      .getCertificate(alias);
    X500Principal issuer = certificate.getIssuerX500Principal();

    return issuer.getName();
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return "";
 }

 public String getDetails() {
  try {
   Enumeration<String> e = keyStore.aliases();
   while (e.hasMoreElements()) {
    String alias = e.nextElement();
    X509Certificate certificate = (X509Certificate) keyStore
      .getCertificate(alias);

    return certificate.toString();
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return "";
 }
}

import java.io.File;

public class Main {
 public static void main(String[] args) throws Exception {
  File file1 = new File("/Users/harikrishna_gurram/shared/cert.p12");

  Pkcs12Util util = new Pkcs12Util(file1, "password1");

  System.out.println("Is certificate valid : " + util.isValidCert());
  System.out.println("Issuer details : " + util.getIssuerName());
  System.out.println("Full details : " + util.getDetails());
 }
}



No comments:

Post a Comment