You can find the expiry dates of SSL certificates (X509Certificate) that were sent to the peer during handshaking this way:
public Map<BigInteger, CertificateInfo> getCertificatesExpiryDatesAndDistinguishedNames(SslContext context) {
SSLSessionContext sessionContext = context.sessionContext();
return Collections.list(sessionContext.getIds()).stream()
.map(sessionContext::getSession)
.map(SSLSession::getLocalCertificates) // certificate(s) that were sent to the peer during handshaking
.map(Stream::of)
.map(streamOfCertificates -> streamOfCertificates.map(X509Certificate.class::cast))
.flatMap(Function.identity())
.collect(toMap(X509Certificate::getSerialNumber, this::convertToCertificateInfo));
}
private CertificateInfo convertToCertificateInfo(final X509Certificate certificate) {
return new CertificateInfo(certificate.getIssuerX500Principal(), certificate.getNotAfter());
}
This returns a map of certificates serial numbers and certificate info (issuer distinguished name and end date of the validity period):
public class CertificateInfo {
private final X500Principal x500Principal;
private final Date endDateOfValidityPeriod;
public CertificateInfo(X500Principal x500Principal, Date endDateOfValidityPeriod) {
this.x500Principal = x500Principal;
this.endDateOfValidityPeriod = endDateOfValidityPeriod;
}
public X500Principal getX500Principal() {
return x500Principal;
}
public Date getEndDateOfValidityPeriod() {
return endDateOfValidityPeriod;
}
}