C ASN1\u时间\u打印功能不带BIO?
如本问题所述:,可以将ASN1时间写入BIO缓冲区,然后将其读回自定义缓冲区C ASN1\u时间\u打印功能不带BIO?,c,openssl,C,Openssl,如本问题所述:,可以将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,如何实现这一点?只有在未
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,如何实现这一点?只有在未定义
OPENSSL\u NO\u BIO
时,才会出现ASN1\u TIME\u print
功能。有没有办法将时间直接写入给定的缓冲区?我认为这应该是可能的,至少在将时间直接写入给定的缓冲区方面是可能的,但您仍然需要使用BIOs
理想情况下,BIO\u new\u mem\u buf
适合,因为它使用给定的缓冲区作为源创建内存中的BIO。不幸的是,该函数将给定的缓冲区视为只读,这不是我们想要的。但是,我们可以基于BIO_new_mem_buf
,创建自己的函数(我们称之为BIO_new_mem_buf
):
这与BIO\u new\u mem\u buf
类似,只是a)len
参数必须指示给定缓冲区的大小,b)BIO未标记为“readonly”
有了上述功能,您现在应该可以拨打:
ASN1_TIME_print(bio, tm)
并将时间显示在给定的缓冲区中
请注意,我没有测试上述代码,所以YMMV。希望这有帮助 您可以尝试下面的示例代码。它不使用BIO,但应该提供与OP示例相同的输出。如果不信任ASN1_时间字符串,则需要添加以下错误检查:
- notBefore->数据大于10个字符
- 每个字符值介于“0”和“9”之间
- 年、月、日、小时、分钟、秒的值
- 类型
ASN1_TIME_print(bio, tm)
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
}