Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java code to display expiration date of certificates in a Java KeyStore

Tags:

java

Looking for java code to display expiration date of certificates in a given keystore.

What i am expecting is below output after running the java code:

CerticateName: CertificateExpirationDate: NumberOfDaysLeft:

Update

I have come up with the code below, Which print certificate alias, I am interested in Expriation date

import java.io.FileInputStream;
import java.security.KeyStore;
import java.util.Enumeration;

public class sslcertslist {
  public static void main(String[] argv) throws Exception {
    FileInputStream is = new FileInputStream("MyKeystore.jks");
    KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    String password = "MyPassword";
    keystore.load(is, password.toCharArray());

    Enumeration e = keystore.aliases();
    for (; e.hasMoreElements();) {
      String alias = (String) e.nextElement();

      boolean b = keystore.isKeyEntry(alias);

      b = keystore.isCertificateEntry(alias);
      System.out.println(alias);
    }
    is.close();
  }
}
like image 343
Sunil Nagavelli Avatar asked Jun 30 '17 03:06

Sunil Nagavelli


People also ask

How do I set the expiry date in Java?

getTime() + milliseconds_in_half_year; Date newDate = new Date(expirationDate);


2 Answers

import java.io.FileInputStream;
import java.security.KeyStore;
import java.util.Enumeration;
import java.io.IOException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableEntryException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.*;

public class GetSslcertsExpires {

public static void main(String[] argv) throws Exception {

 try {
        KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
        keystore.load(new FileInputStream("/DemoTrust.jks"), "DemoTrustKeyStorePassPhrase".toCharArray());
        Enumeration aliases = keystore.aliases();
        for(; aliases.hasMoreElements();) {
            String alias = (String) aliases.nextElement();
            Date certExpiryDate = ((X509Certificate) keystore.getCertificate(alias)).getNotAfter();
            SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
            //Tue Oct 17 06:02:22 AEST 2006
            Date today = new Date();
            long dateDiff = certExpiryDate.getTime() - today.getTime();
            long expiresIn = dateDiff / (24 * 60 * 60 * 1000);
            System.out.println("Certifiate: " + alias + "\tExpires On: " + certExpiryDate + "\tFormated Date: " + ft.format(certExpiryDate) + "\tToday's Date: " + ft.format(today) + "\tExpires In: "+ expiresIn);
        }
    }

catch (Exception e)
     {
       e.printStackTrace();
    }
  }
}
  • Sample Output enter image description here
like image 64
Sunil Nagavelli Avatar answered Nov 15 '22 03:11

Sunil Nagavelli


Just to add another solution

public static Date getSimpleCertExpiryDate(String pfxFileName, String pfxPassword) throws KeyStoreException,
        NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException {

    KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    keystore.load(new FileInputStream(pfxFileName), pfxPassword.toCharArray());
    Enumeration<?> aliases = keystore.aliases();
    Date expiryDate = null;

    for (; aliases.hasMoreElements();) {
        String alias = (String) aliases.nextElement();
        expiryDate = ((X509Certificate) keystore.getCertificate(alias)).getNotAfter();
    }
    return expiryDate;
}

Output be Expiry Date (e.g.) as Tue Mar 16 11:03:25 IST 2021 You can use this to check against today's date

like image 28
Anand Avatar answered Nov 15 '22 03:11

Anand