Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

event based openssl bio

Tags:

c

openssl

I found example code at https://gist.github.com/darrenjs/4645f115d10aa4b5cebf57483ec82eca that uses openssl BIO to implement "nonblocking" socket IO.

But in main function poll only listens stdin and socket fd. poll never listens rbio and wbio.

Is it means function BIO_write and BIO_read are blocking? If they are blocking it makes that code blocking right? If they are not blocking why poll function doesn't listen that BIOs?

I tried to use BIO_get_fd function but it always returns 0. I think it not supposed to create fd as default. I tried to init BIO with *bio = BIO_new(BIO_s_fd()) but still BIO_get_fd returns 0.

I created two pipe int pipes[2][2] used function BIO_set_fd and gave pipes[0][1] to rbio and gave pipes[1][1] to wbio. BIO_write works with that way. But SSL_read function fails with error SSL_ERROR_SYSCALL I think it means SSL_read tried to read pipes[0][1]. I know pipes as one way use like; pipe[0] will be used for read pipe[1] will use for write. But I didn't find way to set pipe[0] pipe[1] to same BIO.

Is it possible to implement pipes to openssl BIOs?

If it's not possible how i can say openssl to create their BIO fd so I can use that fd in poll/epoll/etc?

Note; my main target is doing event based BIO without using socket fd in SSL/BIO functions. I'll handle data transfer between socket fd and BIO fd.

like image 804
yaxok52636 Avatar asked Oct 20 '20 10:10

yaxok52636


1 Answers

poll only needs to listen to stdin and socket.fd, because that is where rbio gets its data from and where wbio writes to.

BIO_write and BIO_read are as blocking as their underlying medium. In case of network they can be considered blocking. But in case of your example they are both BIO_s_mem, pure memory-base BIO-s. As such you can consider them non-blocking.

Skipping ahead, it is possible to create BIO on pretty much anything. If you have a file descriptor (FD) then you don't have to do anything, just use BIO_s_fd. If for some reason you don't like it you can always implement your own BIO. It's not too hard and there are several examples out there.

like image 182
TCvD Avatar answered Nov 02 '22 07:11

TCvD