ASN1_TIME_print功能,而BIO? [英] ASN1_TIME_print functionality without BIO?

查看:880
本文介绍了ASN1_TIME_print功能,而BIO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在本问题中所述: Openssl的C ++获取到期日,有可能写一个ASN1时间到BIO缓冲区,然后读回到自定义缓冲 BUF

As described in this question: Openssl C++ get expiry date, there is the possibility to write an ASN1 time into a BIO buffer and then read it back into a custom buffer 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 不是只定义了present。有没有直接写入时间到一个给定的缓冲区的方式?

How could this be achieved without using BIO at all? The ASN1_TIME_print function is only present when OPENSSL_NO_BIO is not defined. Is there a way to write the time directly into a given buffer?

推荐答案

我想这应该是可能的,至少在直接写入时间为一个给定的缓冲区而言 - 但你仍然需要使用BIOS

I think this should be possible, at least in terms of writing the time directly into a given buffer -- but you'll still need to use BIOs.

在理想情况下, BIO_new_mem_buf 将适合,因为它使用的是定的缓冲区作为源创建一个内存BIO。不幸的是,该函数把给定的缓冲区的只读的,这不是我们想要的。但是,我们可以创建自己的函数(我们称之为 BIO_new_mem_buf2 )的基础上, BIO_new_mem_buf 来源$ C ​​$ C

Ideally, BIO_new_mem_buf would suit, given that it creates an in-memory BIO using a given buffer as the source. Unfortunately, that function treats the given buffer as read-only, which is not what we want. However, we can create our own function (let's call it BIO_new_mem_buf2), based on the BIO_new_mem_buf source code:

BIO *BIO_new_mem_buf2(void *buf, int len)
{
    BIO *ret;
    BUF_MEM *b;
    size_t sz;

    if (!buf) {
        BIOerr(BIO_F_BIO_NEW_MEM_BUF, BIO_R_NULL_PARAMETER);
        return NULL;
    }
    sz = (size_t)len;
    if (!(ret = BIO_new(BIO_s_mem())))
        return NULL;
    b = (BUF_MEM *)ret->ptr;
    b->data = buf;
    b->length = sz;
    b->max = sz;
    return ret;
}

这就像 BIO_new_mem_buf 除了的是 A)的的 LEN 参数的必须表示给定的缓冲区的大小,和 b)的的BIO是的的标记为只读。

This is just like BIO_new_mem_buf, except that a) the len argument must indicate the size of the given buffer, and b) the BIO is not marked "readonly".

通过上面的,你现在应该能够调用:

With the above, you should now be able to call:

ASN1_TIME_print(bio, tm)

和有时间出现在你给出的缓冲区。

and have the time appear in your given buffer.

注意的,我没有测试过上述code,所以因人而异。希望这有助于!

Note that I have not tested the above code, so YMMV. Hope this helps!

这篇关于ASN1_TIME_print功能,而BIO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆