C 中的 SSL 包装器流

发布于 2024-08-13 13:32:53 字数 714 浏览 3 评论 0原文

我有一个简单的 C 语言 stream_t 类型,具有基本的读/写操作,并支持使用函数指针的多个底层实现。因此,流可以由文件、字符缓冲区等支持。

一种流类型是标准 POSIX 套接字,我想编写一个包装器流,它将向现有流添加 SSL 支持,类似于 .NET 的 SslStream。所以我可以写这样的东西:

stream_t *socket = something();

// wrap existing stream and perform handshake as client
stream_t *ssl_stream = ssl_stream_create(socket);
ssl_stream_authenticate_as_user(ssl_stream);

// now all read/writes are encrypted and passed through to the wrapped stream

在使用 OpenSSL 的 BIO_new_connect(...) 等之前我已经编写了一些 SSL 套接字代码,但这是比我需要的更高级别的 API。 OpenSSL 是否公开了我手动执行握手和加密所需的功能?或者还有其他我可以使用的库吗?

I have a simple stream_t type in C with your basic read/write operations, and support for multiple underlying implementations using function pointers. So a stream could be backed by a file, a char buffer, etc.

One stream type is a standard POSIX socket, and I would like to code a wrapper stream that will add SSL support to an existing stream, similar to .NET's SslStream. So I could write something like this:

stream_t *socket = something();

// wrap existing stream and perform handshake as client
stream_t *ssl_stream = ssl_stream_create(socket);
ssl_stream_authenticate_as_user(ssl_stream);

// now all read/writes are encrypted and passed through to the wrapped stream

I have written some SSL socket code before using OpenSSL's BIO_new_connect(...) etc. but this is a higher level API than what I need. Does OpenSSL expose the functions I would need to manually perform the handshake and encryption? Or is there some other library I can use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睫毛溺水了 2024-08-20 13:32:53

我不知道您可以使用任何库,但您可以找到大量示例。大多数 C 语言应用程序都必须对其 TCP 代码执行相同的操作,因此 SSL 和原始套接字版本不会有太大差异。

例如,查看 Pine IMAP 中的 ssl_unix.c,

https://svn.cac.washington.edu/public/alpine/snapshots/imap/src/osdep/unix/

它的作用正是您使用 OpenSSL 所描述的。

I don't know any libraries you can use but you can find plenty of samples. Most applications in C would have to do the same for their TCP code so SSL and raw socket versions don't differ too much.

For example, check out ssl_unix.c from Pine IMAP,

https://svn.cac.washington.edu/public/alpine/snapshots/imap/src/osdep/unix/

It does exactly what you are describing with OpenSSL.

幸福不弃 2024-08-20 13:32:53

我使用了 BIO_s_mem 作为 SSL 套接字操作的掩护。我会自己读取和写入套接字(而不是将句柄交给 OpenSSL 并让它执行读取/写入操作)。当您调用 SSL_accept(在服务器端)或 SSL_connect(在客户端)时,握手就会完成。除此之外,只需调用 SSL_readSSL_write 即可进行读取和写入。

I have used a BIO_s_mem as a cover for SSL socket operations. I would read from and write to the socket myself (rather than giving the handle to OpenSSL and having it do the reading/writing). The handshake is done when you call SSL_accept (on the server side) or SSL_connect (on the client side). Other than that, just call SSL_read and SSL_write to do the reading and writing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文