Skip to content

TLSSocket send/recv return WOULD_BLOCK error instead of NO_CONNECTION #9584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2019

Conversation

michalpasztamobica
Copy link
Contributor

@michalpasztamobica michalpasztamobica commented Feb 1, 2019

Description

In case mbedtls handshake advertising MBEDTLS_ERR_SSL_WANT_READ or MBEDTLS_ERR_SSL_WANT_WRITE is not really a critical error and should be recoverable. We improve the accurateness of the return value from send/recv function by returning NSAPI_ERROR_WOULD_BLOCK instead of NSAPI_ERROR_NO_CONNECTION in such case.

Pull request type

[x] Fix
[ ] Refactor
[ ] Target update
[ ] Functionality change
[ ] Docs update
[ ] Test update
[ ] Breaking change

Reviewers

@SeppoTakalo
@kjbracey-arm

@ciarmcom ciarmcom requested review from SeppoTakalo and a team February 1, 2019 16:00
@ciarmcom
Copy link
Member

ciarmcom commented Feb 1, 2019

@michalpasztamobica, thank you for your changes.
@SeppoTakalo @ARMmbed/mbed-os-ipcore @ARMmbed/mbed-os-maintainers please review.

Copy link
Contributor

@SeppoTakalo SeppoTakalo left a comment

Choose a reason for hiding this comment

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

I don't see why you are changing this?

When calling Socket::connect() in non blocking mode:

  1. First call returns NSAPI_ERROR_IN_PROGRESS which means that asyncronous operation started.
  2. All subsequent calls return NSAPI_ERROR_ALREADY until connection is reached.
  3. When connected, returns NSAPI_ERROR_IS_CONNECTED

There is no WOULD_BLOCK in Socket::connect()

What was the problem you are fixing?

@@ -197,9 +197,6 @@ nsapi_error_t TLSSocketWrapper::start_handshake(bool first_call)

ret = continue_handshake();
if (first_call) {
if (ret == NSAPI_ERROR_ALREADY) {
ret = NSAPI_ERROR_IN_PROGRESS; // If first call should return IN_PROGRESS
Copy link
Contributor

Choose a reason for hiding this comment

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

This sounds like you are changing the API.

@michalpasztamobica michalpasztamobica force-pushed the tlssocket_would_block branch 4 times, most recently from 4ead3ed to 9fb0bf6 Compare February 6, 2019 08:04
@michalpasztamobica
Copy link
Contributor Author

@SeppoTakalo , thank you for your alertness, I overinterpreted my task. There should be no API change, indeed. The PR was only meant to address the return type of continue_handshake. Now I see that connect's behavior should remain unchanged. I amended the PR, I hope it's ok now.

@michalpasztamobica michalpasztamobica force-pushed the tlssocket_would_block branch 2 times, most recently from eeb3ea3 to 7ece4e7 Compare February 6, 2019 14:33
@michalpasztamobica
Copy link
Contributor Author

With the current code the status is following:

  • continue_handshake returns NSAPI_ERROR_WOULD_BLOCK in case the WANT_READ or WANT_WRITE are returned from mbedtls_ssl_handshake().
  • In connect() call this will result in NSAPI_ERROR_IN_PROGRESS returned, as was the case so far
  • However the recv() and send() will now return NSAPI_ERROR_WOULD_BLOCK if they are in non-blocking mode, instead of NSAPI_ERROR_NO_CONNECTION. which was not exactly adequate in this context.

@SeppoTakalo , please confirm that we have the common understanding of the situation.
@kjbracey-arm , would you please share your thoughts on this?

@SeppoTakalo
Copy link
Contributor

Not exactly,

continue_handshake() returns:

  • NSAPI_ERROR_ALREADY if mbed TLS returned MBEDTLS_ERR_SSL_WANT_READ/WRITE
  • NSAPI_ERROR_AUTH_FAILURE on any failure
  • NSAPI_ERROR_IS_CONNECTED if connection was established.

This is called from connect(), which might translate calls, and return:

  • NSAPI_ERROR_IN_PROGRESS in case this was the first call to connect() and continue_handshake() returned NSAPI_ERROR_ALREADY
  • NSAPI_ERROR_OK in case this is first call, and continue_handshake() returned NSAPI_ERROR_IS_CONNECTED.

Same kind of translation might happen in send()/recv() calls, where they might call continue_handshake() in case TLS handshake was not completed. In those case it might return:

  • NSAPI_ERROR_NO_CONNECTION if continue_handshake() returns NSAPI_ERROR_ALREADY. (This can be changed, probably, not have not been tested).
  • NSAPI_ERROR_AUTH_FAILURE on any failure
    And in case of NSAPI_ERROR_IS_CONNECTED is returned, it continues to read/write.

In case mbedtls fails to execute handshake advertising
MBEDTLS_ERR_SSL_WANT_READ or MBEDTLS_ERR_SSL_WANT_WRITE,
TLSSocketWrapper::continue_handshake returns NSAPI_ERROR_WOULD_BLOCK.
@michalpasztamobica michalpasztamobica changed the title TLSSocket returns WOULD_BLOCK error instead of ALREADY TLSSocket send/recv return WOULD_BLOCK error instead of NO_CONNECTION Feb 7, 2019
@michalpasztamobica
Copy link
Contributor Author

I have run an internal CI job against the mbed-client-testapp and all tests passed fine. @SeppoTakalo , do you have more ideas on what we could check before merging this into master?
It looks safe and reasonable to me :)

Copy link
Contributor

@SeppoTakalo SeppoTakalo left a comment

Choose a reason for hiding this comment

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

Think this looks OK now

@michalpasztamobica
Copy link
Contributor Author

@0xc0170 , this PR is already approved. Could you change the label, please?

@0xc0170
Copy link
Contributor

0xc0170 commented Feb 11, 2019

CI started

@mbed-ci
Copy link

mbed-ci commented Feb 11, 2019

Test run: SUCCESS

Summary: 12 of 12 test jobs passed
Build number : 1
Build artifacts

@cmonr cmonr merged commit 92e1464 into ARMmbed:master Feb 12, 2019
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.

6 participants