Skip to content

Offloaded TLSSocket and BG96 support for it #11357

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 3 commits into from
Oct 3, 2019
Merged

Offloaded TLSSocket and BG96 support for it #11357

merged 3 commits into from
Oct 3, 2019

Conversation

kivaisan
Copy link
Contributor

@kivaisan kivaisan commented Aug 28, 2019

Description

Some external modems have an internal TLSSocket implementation which can be used instead of mbedtls based TLSSocket. Using offloaded TLSSocket can result in significantly reduce ROM usage.

Offloaded TLSSocket can be enabled by enabling "nsapi.offload-tlssocket" and the used network stack (e.g. cellular modem's CellularStack class) must support the setsockopt's defined in nsapi_types.h.

Compared to original mbedtls based TLSSocket, offloaded TLSSocket brings in one significant API limitation. Offloaded TLSSocket requires setting of certificates and keys after open() and before connect() calls, where mbedtls based TLSSocket allows setting these before open() call.

This PR also includes a reference implementation for BG96 cellular modem.

Pull request type

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

Reviewers

@AnttiKauppila @kjbracey-arm

Release Notes

Some external modems have an internal TLSSocket implementation which can be used instead of mbedtls based TLSSocket. Using offloaded TLSSocket can result in significantly reduce ROM usage.

Offloaded TLSSocket can be enabled by enabling "nsapi.offload-tlssocket" and the used network stack (e.g. cellular modem's CellularStack class) must support the setsockopt's defined in nsapi_types.h.

Compared to original mbedtls based TLSSocket, offloaded TLSSocket brings in one significant API limitation. Offloaded TLSSocket requires setting of certificates and keys after open() and before connect() calls, where mbedtls based TLSSocket allows setting these before open() call.

This PR also includes a reference implementation for BG96 cellular modem.

@ciarmcom ciarmcom requested review from AnttiKauppila, kjbracey and a team August 28, 2019 07:00
@ciarmcom
Copy link
Member

@kivaisan, thank you for your changes.
@AnttiKauppila @kjbracey-arm @ARMmbed/mbed-os-test @ARMmbed/mbed-os-ipcore @ARMmbed/mbed-os-wan @ARMmbed/mbed-os-maintainers please review.

@@ -118,6 +119,7 @@ class AT_CellularStack : public NetworkStack, public AT_CellularBase {
bool started; // socket has been opened on modem stack
bool tx_ready; // socket is ready for sending on modem stack
bool rx_avail; // socket has data for reading on modem stack
bool tls_socket;

Choose a reason for hiding this comment

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

Where is this used

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll add similar description as other fields. This is internally used by AT_CellularStack to define if socket is offloaded TLSSocket.

if ((_at.get_last_error() == NSAPI_ERROR_OK) && err) {
if (err == BG96_SOCKET_BIND_FAIL) {
socket->id = -1;
if (socket->tls_socket) {
Copy link

@AnttiKauppila AnttiKauppila Aug 28, 2019

Choose a reason for hiding this comment

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

What is the value of this when using MbedTLS for TLSSocket?

Copy link
Contributor Author

@kivaisan kivaisan Aug 28, 2019

Choose a reason for hiding this comment

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

It is set to false for mbedtls based TLSSocket because in that case socket looks like a normal TCP socket from CellularStack point of view.

@AnttiKauppila
Copy link

Will this work with MbedTLS anymore or should you encapsulate the BG96 SSL code inside MBED_CONF_NSAPI_OFFLOAD_TLSSOCKET macro?

@@ -48,6 +48,10 @@ static nsapi_error_t _tlssocket_connect_to_daytime_srv(TLSSocket &sock)
return err;
}

TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.set_root_ca_cert(tls_global::cert));

sock.set_timeout(10000); // Set timeout for case TLSSocket does not support peer closed indication
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't we also need this in other functions connecting to various servers, for example here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think there is some room for little refactoring in here to use common method for opening the server connection in different test cases, but I think that should be done as a separate task.

@@ -46,5 +48,71 @@ TLSSocket::~TLSSocket()
*/
close();
}
#endif
Copy link
Contributor

Choose a reason for hiding this comment

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

Please, add the // MBEDTLS_SSL_CLI_C for readability.

*
* @param root_ca Root CA Certificate in any Mbed TLS-supported format.
* @param len Length of certificate (including terminating 0 for PEM).
* @return 0 on success, negative error code on failure.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we stick to the NSAPI_ERROR_OK convention, rather than disclose its value 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good finding. I'll provide a fix.

@@ -65,6 +65,10 @@
"socket-stats-max-count": {
"help": "Maximum number of socket statistics cached",
"value": 10
},
"offload-tlssocket" : {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a thought here, please reject if "offload" is in fact a widely used name for what you are trying to achieve :)
To me it sounds like "offload" is a potential gain for the user, as they offload the memory and processor (and probably increase the load on the external modem?) but doesn't exactly indicate the main choice that user makes with this flag.
I wonder if something like "external" (from mbed-os perspective) or "embedded" would better suit the purpose?
Are there any reasons, different than offloading the main core, that would make users switch this on?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AnttiKauppila , @kjbracey-arm, @lauri-piikivi what do you think about the term "offloaded"? The same term was already used for DNS:

"offload-dns-queries" : {

@kivaisan
Copy link
Contributor Author

Will this work with MbedTLS anymore or should you encapsulate the BG96 SSL code inside MBED_CONF_NSAPI_OFFLOAD_TLSSOCKET macro?

If offloaded TLSSocket is enabled, mbedtls socket cannot be used. This was discussed and at the moment there is no requirement to support both at the same time. Supporting both at the same time would require API break, or a different class name for offloaded TLSSocket.

@0xc0170
Copy link
Contributor

0xc0170 commented Aug 29, 2019

@kivaisan Is this targeting 5.14 ? It has not been labeled. Please let us know asap.

If it is, we should have reviews done today to get into CI

@AnttiKauppila
Copy link

Will this work with MbedTLS anymore or should you encapsulate the BG96 SSL code inside MBED_CONF_NSAPI_OFFLOAD_TLSSOCKET macro?

If offloaded TLSSocket is enabled, mbedtls socket cannot be used. This was discussed and at the moment there is no requirement to support both at the same time. Supporting both at the same time would require API break, or a different class name for offloaded TLSSocket.

The question was if this still work with MbedTLS like previously (not simultaneously with offload)

@kivaisan
Copy link
Contributor Author

The question was if this still work with MbedTLS like previously (not simultaneously with offload)

Yes. Offloading is an optional feature and is disabled by default. In case of mbedtls based TLSSocket cellularstack handles the socket as a normal TCP socket.

@kivaisan
Copy link
Contributor Author

@kivaisan Is this targeting 5.14 ? It has not been labeled. Please let us know asap.

If it is, we should have reviews done today to get into CI

@AnttiKauppila , what do you think?

@AnttiKauppila
Copy link

This is not in hurry for 5.14

SKIP_IF_TCP_UNSUPPORTED();
TLSSocket sock;
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(NetworkInterface::get_default_instance()));
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.set_root_ca_cert(tls_global::cert));
TEST_ASSERT_EQUAL(NSAPI_ERROR_AUTH_FAILURE,
sock.connect("google.com", 443)); // 443 is https port.
TEST_ASSERT_EQUAL(NSAPI_ERROR_AUTH_FAILURE, sock.connect("www.mtvuutiset.fi", https_port));

Choose a reason for hiding this comment

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

Use a local/CI host.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I didn't understand your comment. Could you please describe more?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AriParkkila , was there something you requested for this?

Choose a reason for hiding this comment

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

How about using e.g. example.com here instead of www.mtvuutiset.fi.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Example.com does not work either. Probably uses some CA root already included in the BG96 modem. But I removed mtvuutiset.fi as well. I think those other handshake tests verifies the needed functionality.

@0xc0170
Copy link
Contributor

0xc0170 commented Sep 3, 2019

@kivaisan Please add Release notes section above for [X] Functionality change

@0xc0170
Copy link
Contributor

0xc0170 commented Sep 3, 2019

I would also suggest adding more details to the commits. There are one liners for functionality changes, provide more information there.

Copy link
Contributor

@0xc0170 0xc0170 left a comment

Choose a reason for hiding this comment

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

Commit msg update and release notes section provide

@kivaisan
Copy link
Contributor Author

I updated commit messages (and also squashed some) and also added some release note description.

@0xc0170
Copy link
Contributor

0xc0170 commented Sep 25, 2019

@AnttiKauppila Please approve this functionality change

@kivaisan
Copy link
Contributor Author

Add possibility to use network stack's internal TLS socket instead of mbedtls based TLS socket. This requires support from network stack (for example cellular modem driver) and reference BG96 implementation is provided.

how to use it? Is it via config or? Can we provide more details to the release notes?

I updated release notes and also description a bit about usage.

@SeppoTakalo
Copy link
Contributor

Please switch the content of your Release Notes and Description sections as it makes much more sense to have full explanation in public facing documentation instead of closed PRs.

Also, add some notes about security implications, because offloading is a potential security risk, if modem is connected by unprotected wires to main CPU. It allows man-in-the-middle attacks using just soldering iron and TTL-serial adapter.

@SeppoTakalo
Copy link
Contributor

Please update the design document as well: https://github.com/ARMmbed/mbed-os/blob/master/docs/design-documents/features/connectivity/TLSSocket%20-%20Design%20document.md

Copy link
Contributor

@0xc0170 0xc0170 left a comment

Choose a reason for hiding this comment

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

To answer @SeppoTakalo requests above

@kivaisan
Copy link
Contributor Author

kivaisan commented Sep 26, 2019

Please switch the content of your Release Notes and Description sections as it makes much more sense to have full explanation in public facing documentation instead of closed PRs.

I disagree. I think description should be more detailed info than release note entry.

Also, add some notes about security implications, because offloading is a potential security risk, if modem is connected by unprotected wires to main CPU. It allows man-in-the-middle attacks using just soldering iron and TTL-serial adapter.
Please update the design document as well: https://github.com/ARMmbed/mbed-os/blob/master/docs/design-documents/features/connectivity/TLSSocket%20-%20Design%20document.md

I think note about security concern can be part of this documentation, not commit description.

@0xc0170
Copy link
Contributor

0xc0170 commented Oct 1, 2019

@kivaisan We are improving the PR template, there will be more subsections. We should provide more details in the release notes as we have provided so far.

I'll schedule CI now

@0xc0170
Copy link
Contributor

0xc0170 commented Oct 1, 2019

CI started

@mbed-ci
Copy link

mbed-ci commented Oct 1, 2019

Test run: SUCCESS

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

@AnttiKauppila
Copy link

@adbridge Why is this marked now for 6.0? What is the reasoning and who demanded this?

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.

10 participants