使用C测试X509证书到期日期

 手机用户2502905627_315 发布于 2023-01-18 09:15

如果X509?*证书过期,我该如何编程测试?他们是直接加密api?或者我必须得到not_after时间并在我的代码中手动检查它?

1 个回答
  • 你没有说出哪种语言,所以我总结了它们:

    PHP

    $data = openssl_x509_parse(file_get_contents('/path/to/cert.crt'));
    
    $validFrom = date('Y-m-d H:i:s', $data['validFrom_time_t']);
    $validTo = date('Y-m-d H:i:s', $data['validTo_time_t']);
    

    Java X509Certificate

    InputStream inStream = null;
    
     try (InputStream inStream = new FileInputStream("fileName-of-cert")) {
         CertificateFactory cf = CertificateFactory.getInstance("X.509");
         X509Certificate cert = (X509Certificate)cf.generateCertificate(inStream);
    
         // check if valid on specific date (now set for today)
         cert.checkValidity(new Date());
         // Date nBefore = cert.getNotBefore();
         // Date nAfter = cert.getNotAfter();
     }
    

    C++

    using namespace System;
    using namespace System::Security::Cryptography::X509Certificates;
    int main()
    {
    
       // The path to the certificate.
       String^ Certificate = "Certificate.cer";
    
       // Load the certificate into an X509Certificate object.
       X509Certificate^ cert = X509Certificate::CreateFromCertFile( Certificate );
    
       // Get the value.
       String^ results = cert->GetExpirationDateString();
    
       // Display the value to the console.
       Console::WriteLine( results );
    }
    

    C

    X509 *x
    time_t *ptime;
    i=X509_cmp_time(X509_get_notBefore(x), ptime);
    i=X509_cmp_time(X509_get_notAfter(x), ptime);
    

    2023-01-18 09:18 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有