指示 OpenSSL 在设置新 BIO 时不要释放 BIO 对象
在此示例代码中:
BIO *bio1 = BIO_new(BIO_s_mem());
BIO *bio2 = BIO_new(BIO_s_mem());
SSL_set_bio(ssl, bio1, bio1);
SSL_set_bio(ssl, bio2, bio2);
对 SSL_set_bio 的最后一次调用会自动调用 BIO_free(bio1)。 有没有办法告诉 OpenSSL 不要这样做?
我知道在使用 BIO_new(BIO_s_mem()) 创建内存 bio 后,我可以告诉 OpenSSL 不要使用 BIO_set_close(bio, BIO_NOCLOSE) 释放它的内存缓冲区。我的情况有类似的情况吗?
In this sample code:
BIO *bio1 = BIO_new(BIO_s_mem());
BIO *bio2 = BIO_new(BIO_s_mem());
SSL_set_bio(ssl, bio1, bio1);
SSL_set_bio(ssl, bio2, bio2);
the last call to SSL_set_bio automatically calls BIO_free(bio1).
Is there anyway to tell OpenSSL not to do so?
I know that upon creating a memory bio with BIO_new(BIO_s_mem()) I can tell OpenSSL not to free it's memory buffer with BIO_set_close(bio, BIO_NOCLOSE). Is there anything similar for my case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无法阻止
SSL_set_bio
释放公共 API 中的当前 BIO。你可以在源代码中看到它只是检查每个bio是否不为空,然后释放它。主要思想是,在调用
SSL_set_bio
之后,OpenSSL 拥有 BIO 并对其负责。如果我有正当理由在生产代码中保留生物缓冲区,我会编写自己的生物并使用它。这并不像听起来那么难。只需复制
/crypto/bio/bss_mem.c
,重命名函数和mem_method
表,然后替换mem_free()< 的行为/代码>。然后,传递
BIO_custom_mem_bio
或任何您为 Bio 命名的访问器函数,而不是BIO_s_mem
。如果我需要它用于调试目的而不是生产代码,我可能会深入到
ssl_st
结构 (SSL *
) 的内部,并在之前将所有 BIOS 设为 NULL调用SSL_set_bio
。但我不会在生产代码中这样做,因为未来的 SSL 版本可能会破坏该代码。There's no way to prevent
SSL_set_bio
from freeing the current BIO in the public API. You can see in the source code that it simply checks whether each bio is not null and then frees it.The main idea is that after you call
SSL_set_bio
, OpenSSL owns the BIO and is responsible for it.If I had a legitimate reason to keep the bio buffer around in production code, I would write my own bio and use that. It's not as hard as it sounds. Just copy
<openssl source>/crypto/bio/bss_mem.c
, rename the functions andmem_method
table, and then replace the behavior ofmem_free()
. Then instead ofBIO_s_mem
, passBIO_custom_mem_bio
or whatever you name the accessor function for your bio.If I needed it for debugging purposes and not production code, I'd probably just grovel into the internals of the
ssl_st
struct (SSL *
) and make all the bios NULL before callingSSL_set_bio
. But I wouldn't do that in production code because future SSL versions may break that code.您可以使用
BIO_up_ref()
来增加引用计数。BIO_free()
会减少计数,但不会释放它。You can use
BIO_up_ref()
to increase the reference count.BIO_free()
would decrease the count, but not free it.