OpenSSL: accept a TLS connection and then transfer another process

We have a (Linux) server on which two processes are running: A and B. Currently, clients establish a connection to process A, which then passes the received socket file descriptor to handle B, allowing process B to use the existing fd / socket for unhindered communication with the client. The client and process B then handshake the TLS and continue talking about the received TLS connection.

(I leave a lot of details here, but yes, there are good reasons when process A acts as an intermediary, and not just connects directly to process B)

Now, because of the <long complicated story involving new client applications and websockets> it looks like we might have to handshake the TLS in process A, and then transfer the established TLS connection to process B.

Is it possible? The main socket file descriptor can be copied (we are already doing this), and, at least theoretically, the internal TLS state data can also be copied and used to restore the TLS connection in process B, which uses the connection efficiently.

But does OpenSSL reveal any means? I found the d2i_SSL_SESSION function, which seems to do something similar for the OpenSSL session object, but being completely new to OpenSSL, I'm not sure if that is enough. There are sessions, context, BIO and many other complex sounding terms. How much would you need to serialize and transfer to process B for this? And how will this be done in practice?

Switching should be 100% transparent for the client: it should simply perform SSL acknowledgment on this IP port, and then continue talking on the resulting socket, not knowing that one process accepts the connection and performs TLS acknowledgment, and the other processes all subsequent messages .

+4
source share
5 answers

I have not tried this in practice, but, as far as I remember, after the connection is created at the socket level, it is initialized by openssl, and then you read / write using SSL_read and SSL_write. They take the socket parameter fd as a parameter. The connection itself (from the SSL side) is represented by SSL_CTX SSL structures.

So in theory this sounds possible, but, as I said, I have never tried it in the real world.

+1
source

Exchange of SSL context between processes is really possible. However, then the context of the SSL session would have to be in the shared memory location accessible for both processes (because for unknown reasons, we want the actual handshake to be performed in process A and input / output data in process B.

The first step is to register callbacks for SSL_CTX_sess_set_new_cb (ctx, shared_ctx_new_cb); SSL_CTX_sess_set_get_cb (ctx, shared_ctx_get_cb); SSL_CTX_sess_set_remove_cb (ctx, shared_ctx_remove_cb);

Ensure that the appropriate SSL session context is always created in shared memory (or at least returns a serialized and ready-to-use address pointer to SSL_SESSION

To (de) serialize the SSL_SESSION 'C' structure, use the available API d2i_SSL_SESSION (...) and i2d_SSL_SESSION (...)

A working project using this approach is sample code at https://github.com/varnish/hitch/blob/master/src/shctx.c

+1
source

I do not think this is possible, since part of the initial handshake is the exchange of keys, and the keys are necessary for constant communication. Process B must know the key used by the remote end and process A.

0
source

sounds like more problems worth ... consider other projects, for example. proxy connection through A on behalf of B, using the loopback interface, for example.

0
source

A recent kernel patch can make this possible by setting a normal fd for the TLS connection. See the “ TLS in the Jake Edge Core December 2, 2015 ” page. I also went to another question fooobar.com/questions/1434212 / ....

0
source

Source: https://habr.com/ru/post/1434210/


All Articles