【问题标题】:How to get SSL certificates expiry dates from the SSLContext?如何从 SSLContext 获取 SSL 证书到期日期?
【发布时间】:2020-05-20 12:21:43
【问题描述】:

我目前正在从我的 Java 程序中的 pem 文件中读取证书和密钥,并使用它来构造一个 SSLContext 对象,就像这样 -

final SslContext _sslContext = SslContextBuilder.forClient().ciphers(ciphers)
      .sslProvider(sslProvider).trustManager(_trustedCerts).keyManager(_cert, _key, pwd).build();

其中 _cert、_key 是文件。

有什么方法可以使用此 SSLContext 对象来获取证书到期日期和 DN?

【问题讨论】:

  • 对您来说可能很明显,但您甚至没有指定您正在使用哪种编程语言!另外,您是否查阅过您正在使用的“SSL”库的文档?您已经拥有证书(因为您使用它来构建上下文)所以库中肯定有一些方法可以解析证书并提取您需要的信息。
  • @PatrickMevzek 很抱歉没有提供语言细节。你是对的,我有这些文件,所以我能够获得我需要使用的信息!感谢您的推动

标签: java ssl ssl-certificate


【解决方案1】:

我能够直接从 pem 文件中获取证书信息。这就是我在 Java 中以编程方式完成的方式 -

CertificateFactory fact = null;
try {
    fact = CertificateFactory.getInstance("X.509");
} catch (CertificateException e) {
    e.printStackTrace();
}
FileInputStream is = null;
try {
    is = new FileInputStream(_cert);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
X509Certificate cer = null;
try {
    cer = (X509Certificate) fact.generateCertificate(is);
    log.info("Cer Not Before - {} ", cer.getNotBefore());
    log.info("Cer Not After - {} ", cer.getNotAfter());
    log.info("Cer Issuer DN - {} ",cer.getIssuerDN());
} catch (CertificateException e) {
    e.printStackTrace();
}

这个 stackoverflow 答案帮助我解决了这个问题How to load public certificate from pem file?

【讨论】:

    【解决方案2】:

    您可以通过这种方式找到在握手期间发送给对等方的 SSL 证书 (X509Certificate) 的到期日期:

    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());
    }
    

    这将返回证书序列号和证书信息(颁发者可分辨名称和有效期结束日期)的映射:

    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;
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-13
      • 2012-03-29
      • 2015-04-07
      • 1970-01-01
      相关资源
      最近更新 更多