【问题标题】:Testing X509 Certificate Expiry date with C用 C 测试 X509 证书到期日期
【发布时间】:2014-05-01 12:09:36
【问题描述】:

如何以编程方式测试 X509 ?* 证书是否已过期? 他们是直接加密 API 吗? 或者我必须得到 not_after 时间并在我的代码中手动检查?

【问题讨论】:

  • 你想用什么语言进行编程测试?
  • C ,我正在搜索看起来我现在必须做 time_t ; X509_cmp_time(cert, &now)
  • 我猜你可能想检查 openssl verify 命令的代码,它确实检查了最终证书的有效性。

标签: c cryptography openssl


【解决方案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);

【讨论】:

  • 有没有可能把 C++ sn-p 中的 ^ 误认为是 &
  • 另外,sn-p 依赖于 .NET 类。提起来也无妨。
  • ^ 相当于 c++/CLI 中的 *。引用指针的句柄
【解决方案2】:

你应该可以使用:

result=X509_cmp_time(X509_get_notBefore(cert), ptime);

result=X509_cmp_time(X509_get_notAfter(cert), ptime);

使用 OpenSSL。我不确定你是否能得到比这更少的“手动”。

【讨论】:

  • 这里的 ptime 是什么?它是一个openssl变量吗? time_t
  • 是的,time_t 代表当前时间(或者至少是您想要验证的时间)。
  • time_t now;time(&now); 我现在应该在 X509 cmp API 中传递指向 time_t 的指针,我猜只有 notAfter 可以检查它是否过期了?
  • 有效性是使用notBeforenotAfter 来衡量的,过期是使用notAfter 来衡量的,是的。当然,您还应该在可用的情况下使用 CLR 或 OCSP。
  • CLR -> CRL 用于上面评论中的证书吊销列表,当然。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
  • 2012-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多