Skip to content

OpenSSL ByteBuffer BIO - #6201

Closed
Scottmitch wants to merge 1 commit into
netty:4.1from
Scottmitch:openssl_bytebuffer_bio
Closed

Scottmitch wants to merge 1 commit into
netty:4.1from
Scottmitch:openssl_bytebuffer_bio

Conversation

@Scottmitch

Copy link
Copy Markdown
Member

Motivation:
Currently Netty utilizes BIO_new_bio_pair so we can control all FD lifetime and event notification but delegates to OpenSSL for encryption/decryption. The current implementation sets up a pair of BIO buffers to read/write encrypted/plaintext data. This approach requires copying of data from Java ByteBuffers to native memory BIO buffers, and also requires both BIO buffers to be sufficiently large to hold application data. If direct ByteBuffers are used we can avoid coyping to/from the intermediate BIO buffer and just read/write directly from the direct ByteBuffer memory. We still need an internal buffer because OpenSSL may generate write data as a result of read calls (e.g. handshake, alerts, renegotiation, etc..), but this buffer doesn't have to be be large enough to hold application data.

Modifications:

  • Take advantage of the new ByteBuffer based BIO provided by netty-tcnative instead of using BIO_read and BIO_write.

Result:
Less copying and lower memory footprint requirement per TLS connection.

@Scottmitch Scottmitch self-assigned this Jan 10, 2017
@Scottmitch

Scottmitch commented Jan 10, 2017 •

Copy link
Copy Markdown
Member Author

- only the last commit is part of this PR

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@normanmaurer - this is meant to be a discussion point.

@Scottmitch
Scottmitch force-pushed the openssl_bytebuffer_bio branch from e7aaa16 to 8661327 Compare February 3, 2017 09:15
Comment thread pom.xml Outdated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this must change

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@normanmaurer will update this before the next release

@Scottmitch
Scottmitch force-pushed the openssl_bytebuffer_bio branch from c2bbaab to affc6ac Compare February 3, 2017 09:36

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Scottmitch can we ensure we also release when the assert fails. Just in case :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is necessary. The assert signifies a programatic error and was added to aid development and unit tests. Do you have a suggestion which doesn't introduce runtime checks or additional try/catch?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah... its ok

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Scottmitch I think we can remove this via a constructor option ? Like strict and non-strict mode ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@normanmaurer - great! I noticed this was thrashing a lot during my testing. let me break this out into another PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Scottmitch can you open an issue and link it here ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

false alarm ... issue was pre-existing but has been fixed in this PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Scottmitch like discussed before I think we should factor this out in a separate PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Scottmitch Scottmitch Feb 6, 2017 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The OpenSSL team has advised against doing this. The current approach involving SSL.SSL_ST_ACCEPT had issues so I fixed them in this PR and will investigate removal in of SSL.SSL_ST_ACCEPT in #6320.

Motivation:
Currently Netty utilizes BIO_new_bio_pair so we can control all FD lifetime and event notification but delegates to OpenSSL for encryption/decryption. The current implementation sets up a pair of BIO buffers to read/write encrypted/plaintext data. This approach requires copying of data from Java ByteBuffers to native memory BIO buffers, and also requires both BIO buffers to be sufficiently large to hold application data. If direct ByteBuffers are used we can avoid coyping to/from the intermediate BIO buffer and just read/write directly from the direct ByteBuffer memory. We still need an internal buffer because OpenSSL may generate write data as a result of read calls (e.g. handshake, alerts, renegotiation, etc..), but this buffer doesn't have to be be large enough to hold application data.

Modifications:
- Take advantage of the new ByteBuffer based BIO provided by netty-tcnative instead of using BIO_read and BIO_write.

Result:
Less copying and lower memory footprint requirement per TLS connection.
@Scottmitch
Scottmitch force-pushed the openssl_bytebuffer_bio branch from c6ab13f to 40d8b45 Compare February 7, 2017 05:39
AccessController.doPrivileged(new PrivilegedAction<Integer>() {
@Override
public Integer run() {
return Math.max(1,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why 1 and not 0 (which will use the default then) ?

@Scottmitch Scottmitch Feb 7, 2017 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed support for the default at the tcnative level, and disallowed setting 0. Users should explicitly set the size they want. Default of 16k is likely way to big anyways.

Also not having a buffer is not an option because I don't know of a way to ask SSL how much data it has pending (SSL_pending didn't work) ... so we can't distinguish between NEED_WRAP and NEED_UNWRAP when SSL wants to write some non-application data.

@Scottmitch

Copy link
Copy Markdown
Member Author

4.1 (d06990f) 4.0 (00cd6d2)

@Scottmitch Scottmitch closed this Feb 9, 2017
@Scottmitch
Scottmitch deleted the openssl_bytebuffer_bio branch February 9, 2017 17:51
@Scottmitch Scottmitch added this to the 4.0.45.Final milestone Feb 9, 2017
@normanmaurer

Copy link
Copy Markdown
Member

LGTM... Thanks!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants