c - 没有 BIO 的 ASN1_TIME_print 功能?

标签 c openssl

如本问题所述:Openssl C++ get expiry date ,可以将 ASN1 时间写入 BIO 缓冲区,然后将其读回自定义缓冲区 buf:

BIO *bio;
int write = 0;
bio = BIO_new(BIO_s_mem());
if (bio) {
  if (ASN1_TIME_print(bio, tm))
    write = BIO_read(bio, buf, len-1);
  BIO_free(bio);
}
buf[write]='\0';
return write;

如果完全不使用 BIO,如何实现这一目标? ASN1_TIME_print 函数仅在 OPENSSL_NO_BIO 未定义时出现。有没有办法将时间直接写入给定的缓冲区?

最佳答案

您可以尝试下面的示例代码。它不使用 BIO,但应该为您提供与 OP 示例相同的输出。如果您不信任 ASN1_TIME 字符串,您需要添加一些错误检查:

  • notBefore->数据大于 10 个字符
  • 每个字符值都在'0'和'9'之间
  • 年、月、日、时、分、秒的值
  • 输入

如果您需要多种类型,您应该测试类型(即 UTC)。

您还应该测试日期/时间是否为 GMT,如果您希望输出与使用 BIO 时完全匹配,则将其添加到字符串中。看: openssl/crypto/asn1/t_x509.c - ASN1_UTCTIME_print 或 ASN1_GENERALIZEDTIME_print


ASN1_TIME* notBefore = NULL;
int len = 32;
char buf[len];
struct tm tm_time;

notBefore = X509_get_notBefore(x509_cert);

// Format ASN1_TIME  with type UTC into a tm struct
if(notBefore->type == V_ASN1_UTCTIME){
    strptime((const char*)notBefore->data, "%y%m%d%H%M%SZ" , &tm_time);
    strftime(buf, sizeof(char) * len, "%h %d %H:%M:%S %Y", &tm_time);
}

// Format ASN1_TIME with type "Generalized" into a tm struct
if(notBefore->type == V_ASN1_GENERALIZEDTIME){
     // I didn't look this format up, but it shouldn't be too difficult
}

关于c - 没有 BIO 的 ASN1_TIME_print 功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17235368/

相关文章:

c - 与体系结构无关的 C 代码

c++ - 循环终止时无法获得输入

node.js - Nodejs createDecipher,可以用两个不同的 key 解密相同的密文

php - 无法识别本地发行人

python - 在 Linux 上开发 HDMI 端口

c - C程序预处理后生成哪个文件?

c - 处理从库中的 OS API 返回的错误的最佳方法是什么?

openssl - 为什么在 MacOS Catalina 上使用 Cro 时 Raku 会崩溃并显示 'Abort trap: 6'?

c# - RSACryptoServiceProvider 和 openSSL 之间的互操作性

python - 验证用 python 签名的文件的问题