You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Renegotiation with custom BIO buffer with less than 33 bytes space fails #2551
OpenSSL Version: OpenSSL 1.0.2j
OS: macOS Sierra 10.12.3
When a renegotiation is initiated a 33 byte payload is generated by OpenSSL. If the BIO doesn't have enough space to hold 33 bytes then OpenSSL will not attempt to re-write the remaining bytes after space becomes available in the BIO. I am developing a custom BIO implementation and documentation is sparse so it is very possible I'm using the OpenSSL APIs incorrectly.
Steps to Reproduce (Use a BIO which has 32 bytes free space)
Complete a handshake
Server initiates a renegotiation (calls SSL_renegotiate, returns 1)
Server calls SSL_do_handshake (attempts to write 33 bytes into the BIO but only is able to write 32 bytes, returns -1, SSL_get_error returns SSL_ERROR_WANT_WRITE)
OpenSSL attempts to write the 1 remaining byte which wouldn't fit and my custom BIO calls BIO_set_retry_write(bio) and return -1.
Server calls SSL_set_state(SSL_ST_ACCEPT)
Server calls SSL_do_handshake (returns -1, SSL_get_error returns SSL_ERROR_WANT_READ)
OpenSSL attempts to read from the BIO, but my custom BIO has nothing to read so calls BIO_set_retry_read(bio) and returns -1
Server reads the 32 pending bytes from the BIO. The BIO now has 32 bytes of free space.
Server calls SSL_do_handshake (returns -1, SSL_get_error returns SSL_ERROR_WANT_READ)
OpenSSL tries to read from the BIO. I would expect OpenSSL to try to write the 1 byte which has not been written.
I also have a code reproducer, but the code involves Java & JNI (netty/netty#6201). Let me know if you are interested and I can list the steps required to run the code.
It's been a while since I'd looked at the dance to do a renegotiation, but I believe you need to drive SSL_do_handshake (3) to completion before calling SSL_set_state (4). The first SSL_do_handshake, once driven to completion, is just sending the HelloRequest as a "handshake". Then SSL_set_state is a slightly screwy hack which causes OpenSSL to require the client initiate a renegotiation. At that point, the second SSL_do_handshake (5) does the actual renegotiation handshake.
(In principle, TLS only has client-initiated renegotiations and HelloRequest is merely a request for the client to initiate one. In practice, client-initiated renegotiations don't exist and if a server sends a HelloRequest, it will refuse to progress a connection unless the client renegotiates, so HelloRequest is effectively a server-initiated renegotiation.)
PS: Don't do renegotiations. They're terrible. :-)
@davidben - You nailed it. Operator error. Thanks a bunch! Also thanks for the additional details regarding HelloRequest ... makes sense.
PS: Don't do renegotiations. They're terrible. :-)
That seems to be the common sentiment. Unfortunately Netty already supports it so I don't think we can remove support unless there is a compelling security vulnerability.
3SHAKE also has ramifications for renegotiation. Not to mention general application bugs when parameters change midway through a connection or what things mean during a renegotiation.
It's been a while since I'd looked at the dance to do a renegotiation, but I believe you need to drive SSL_do_handshake (3) to completion before calling SSL_set_state (4).
Don't call SSL_set_state() at all during renegotiation (or in fact ever)!!! I suspect this way of doing things originated from some code in s_server that did it this way for a while (although AFAICT that code never actually made it as a far a released version). I removed it in commit 075c879. The original code even had a big comment next to it labelled "EVIL HACK" - which it certainly was and was the reason I removed it.
After sending the HelloRequest, as @davidben points out, you must make sure you drive the handshake to completion. Following that the client may or may not respond with a ClientHello to initiate renegotiation. It is under no obligation to do so. By forcing the state into SSL_ST_ACCEPT, you are fiddling with the internal state machine set up to force it to expect a ClientHello next - which might not happen. There be dragons. This approach almost certainly doesn't work with the new state machine code in 1.1.0 anyway.
@mattcaswell That too. There's a reason we ripped out almost all the renegotiation code in BoringSSL. :-) You may need to think about a replacement API though. The SSL_set_state nonsense allowed you to forbid the client from sending application data at this point, which isn't quite what 075c879 does. This is commonly used in the renegotiate + client auth hack that some HTTP/1.1 servers do. Apache httpd, for instance, does it.
Or you could declare, as we do, that this hack is illegitimate and not supported on the server by your library. But you might have a harder time doing that than we do. (I think we have stub SSL_renegotiate and SSL_set_state functions to avoid breaking some compiles, but they do not do anything.)
@Scottmitch No, seriously, don't allow renegotiations. :-)
The SSL_set_state nonsense allowed you to forbid the client from sending application data at this point, which isn't quite what 075c879 does.
Well you can't really forbid the client from sending app data. The client is under no obligation to honour the renegotiation request. Of course the server app can always abort the connection if gets data when it doesn't want it - so I see no reason why OpenSSL needs to do anything special here.
So far no one using 1.1.0 has asked us to "fix" SSL_set_state() to allow them to do this.
Activity
davidben commentedon Feb 4, 2017
It's been a while since I'd looked at the dance to do a renegotiation, but I believe you need to drive
SSL_do_handshake(3) to completion before callingSSL_set_state(4). The firstSSL_do_handshake, once driven to completion, is just sending the HelloRequest as a "handshake". ThenSSL_set_stateis a slightly screwy hack which causes OpenSSL to require the client initiate a renegotiation. At that point, the secondSSL_do_handshake(5) does the actual renegotiation handshake.(In principle, TLS only has client-initiated renegotiations and HelloRequest is merely a request for the client to initiate one. In practice, client-initiated renegotiations don't exist and if a server sends a HelloRequest, it will refuse to progress a connection unless the client renegotiates, so HelloRequest is effectively a server-initiated renegotiation.)
PS: Don't do renegotiations. They're terrible. :-)
Scottmitch commentedon Feb 5, 2017
@davidben - You nailed it. Operator error. Thanks a bunch! Also thanks for the additional details regarding
HelloRequest... makes sense.That seems to be the common sentiment. Unfortunately Netty already supports it so I don't think we can remove support unless there is a compelling security vulnerability.
richsalz commentedon Feb 5, 2017
There have been vulnerabilities around renegotiation. At least make sure you only support the secure version.
davidben commentedon Feb 5, 2017
3SHAKE also has ramifications for renegotiation. Not to mention general application bugs when parameters change midway through a connection or what things mean during a renegotiation.
Scottmitch commentedon Feb 5, 2017
For reference see the SECURE RENEGOTIATION section in the docs
Scottmitch commentedon Feb 5, 2017
Thanks @richsalz & @davidben ... I'll check out the 3SHAKE doc
johnou commentedon Feb 5, 2017
@Scottmitch I guess some unsafe versions are disabled by default in Netty, I see this every now and then in our prod logs.
mattcaswell commentedon Feb 6, 2017
Don't call
SSL_set_state()at all during renegotiation (or in fact ever)!!! I suspect this way of doing things originated from some code in s_server that did it this way for a while (although AFAICT that code never actually made it as a far a released version). I removed it in commit 075c879. The original code even had a big comment next to it labelled "EVIL HACK" - which it certainly was and was the reason I removed it.After sending the HelloRequest, as @davidben points out, you must make sure you drive the handshake to completion. Following that the client may or may not respond with a ClientHello to initiate renegotiation. It is under no obligation to do so. By forcing the state into SSL_ST_ACCEPT, you are fiddling with the internal state machine set up to force it to expect a ClientHello next - which might not happen. There be dragons. This approach almost certainly doesn't work with the new state machine code in 1.1.0 anyway.
Scottmitch commentedon Feb 6, 2017
@mattcaswell - Thanks for following up! We will investigate more netty/netty#6320.
davidben commentedon Feb 6, 2017
@mattcaswell That too. There's a reason we ripped out almost all the renegotiation code in BoringSSL. :-) You may need to think about a replacement API though. The
SSL_set_statenonsense allowed you to forbid the client from sending application data at this point, which isn't quite what 075c879 does. This is commonly used in the renegotiate + client auth hack that some HTTP/1.1 servers do. Apache httpd, for instance, does it.Or you could declare, as we do, that this hack is illegitimate and not supported on the server by your library. But you might have a harder time doing that than we do. (I think we have stub
SSL_renegotiateandSSL_set_statefunctions to avoid breaking some compiles, but they do not do anything.)@Scottmitch No, seriously, don't allow renegotiations. :-)
mattcaswell commentedon Feb 6, 2017
Well you can't really forbid the client from sending app data. The client is under no obligation to honour the renegotiation request. Of course the server app can always abort the connection if gets data when it doesn't want it - so I see no reason why OpenSSL needs to do anything special here.
So far no one using 1.1.0 has asked us to "fix"
SSL_set_state()to allow them to do this.