Skip to content

Renegotiation with custom BIO buffer with less than 33 bytes space fails #2551

Closed

Description

@Scottmitch

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)

  1. Complete a handshake
  2. Server initiates a renegotiation (calls SSL_renegotiate, returns 1)
  3. 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.
  1. Server calls SSL_set_state(SSL_ST_ACCEPT)
  2. 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
  1. Server reads the 32 pending bytes from the BIO. The BIO now has 32 bytes of free space.
  2. 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.

Activity

  1. davidben commented on Feb 4, 2017

    @davidben
    Contributor

    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. :-)

  2. Scottmitch commented on Feb 5, 2017

    @Scottmitch
    Author

    @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.

  3. richsalz commented on Feb 5, 2017

    @richsalz
    Contributor

    There have been vulnerabilities around renegotiation. At least make sure you only support the secure version.

  4. davidben commented on Feb 5, 2017

    @davidben
    Contributor

    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.

  5. Scottmitch commented on Feb 5, 2017

    @Scottmitch
    Author

    For reference see the SECURE RENEGOTIATION section in the docs

  6. Scottmitch commented on Feb 5, 2017

    @Scottmitch
    Author

    Thanks @richsalz & @davidben ... I'll check out the 3SHAKE doc

  7. johnou commented on Feb 5, 2017

    @johnou

    @Scottmitch I guess some unsafe versions are disabled by default in Netty, I see this every now and then in our prod logs.

    Caused by: javax.net.ssl.SSLHandshakeException: error:1412E152:SSL routines:SSL_PARSE_CLIENTHELLO_TLSEXT:unsafe legacy renegotiation disabled
    	at io.netty.handler.ssl.ReferenceCountedOpenSslEngine.sslReadErrorResult(ReferenceCountedOpenSslEngine.java:953) 
    
  8. mattcaswell commented on Feb 6, 2017

    @mattcaswell
    Member

    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.

  9. Scottmitch commented on Feb 6, 2017

    @Scottmitch
    Author

    @mattcaswell - Thanks for following up! We will investigate more netty/netty#6320.

  10. davidben commented on Feb 6, 2017

    @davidben
    Contributor

    @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. :-)

  11. mattcaswell commented on Feb 6, 2017

    @mattcaswell
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions