Skip to content

Commit 5d5b865

Browse files
author
Kimmo Vaisanen
committed
Update offloaded TLSSocket to TLSSocket design
1 parent 8c673ba commit 5d5b865

File tree

1 file changed

+102
-27
lines changed

1 file changed

+102
-27
lines changed

docs/design-documents/features/connectivity/TLSSocket - Design document.md

Lines changed: 102 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
11
# TLSSocket - Design document
22
## Overview and Background
33

4-
This document describes design of TLSSocket class that provides simple interface for Mbed OS
5-
user to create TLS connections over TCP socket.
4+
This document describes design of TLSSocket class that provides simple interface for Mbed OS user to create TLS connections over TCP socket.
5+
6+
Mbed OS provides 2 different implementations for TLS socket; Mbed TLS based (default) or offloaded TLS socket when used network stack (e.g. cellular modem target driver) supports it.
67

78
This class greatly simplifies the usage of TLS but limits itself to only one use case.
8-
This design limitation is accepted as other users can continue using Mbed TLS API
9-
directly. TLSSocket also exposes internal Mbed TLS structures allowing use of Mbed TLS API to configure the underlying library.
9+
This design limitation is accepted as other users can continue using Mbed TLS API directly. Mbed TLS based TLSSocket also exposes internal Mbed TLS structures allowing use of Mbed TLS API to configure the underlying library.
1010

1111
High level goal is to demonstrate Mbed OS users that secure connections are not hard to do.
1212

1313
## Design Limitations
1414

15-
Following design limitations exist in the current design of TLSSocket
15+
Following design limitations exist in the current design of TLSSocket:
1616

17+
Mbed TLS based TLSSocket:
1718
* `TLSSocket::connect()` is always blocking
1819
* Can only use server and client certificates through `set_root_ca_cert()` and `set_client_cert_key()` methods. For other use cases, internal Mbed TLS structures are exposed.
1920
* No PSK mode
2021

22+
Offloaded vs. Mbed TLS based TLSSocket:
23+
* For offloaded TLS socket `set_root_ca_cert()` and `set_client_cert_key()` must be called after `TLSSocket::open()` and before `TLSSocket::connect()`.
24+
* Offloaded TLS socket API does not support all Mbed TLS based TLSSocket methods, but common (i.e. `open()`, `connect()`, `close()` and setting certficates) use the same API.
25+
2126
# System Architecture and High-Level Design
2227

23-
Secure socket consist of two classes:
28+
Mbed TLS based secure socket consist of two classes:
2429

25-
* `TLSocketWrapper` <br>
30+
* `TLSSocketWrapper` <br>
2631
Which handles initialization of TLS library and does TLS handsake. Takes any Socket as a
2732
trasport.
2833
* `TLSSocket` <br>
29-
Which inherits TLSocketWrapper and has TCP socket as a transport.
34+
Which inherits TLSSocketWrapper and has TCP socket as a transport.
3035
Adds `connect(char *hostname)`
3136
for initiating the TCP and TLS handshakes at one call.
3237

@@ -41,16 +46,16 @@ Secure socket consist of two classes:
4146
`--------------'
4247
|
4348
|
44-
,----------------------------.
45-
|TLSocketWrapper |
46-
|----------------------------|
47-
|Socket *transport; |
48-
|----------------------------|
49-
|TLSocketWrapper(*transport);|
50-
|int set_root_ca_cert(...); |
51-
|void set_hostname(...); |
52-
|int do_handshake(); |
53-
`----------------------------'
49+
,-----------------------------.
50+
|TLSSocketWrapper |
51+
|-----------------------------|
52+
|Socket *transport; |
53+
|-----------------------------|
54+
|TLSSocketWrapper(*transport);|
55+
|int set_root_ca_cert(...); |
56+
|void set_hostname(...); |
57+
|int do_handshake(); |
58+
`-----------------------------'
5459
|
5560
,----------------------------.
5661
|TLSSocket |
@@ -61,9 +66,35 @@ Secure socket consist of two classes:
6166
`----------------------------'
6267
```
6368

69+
Offloaded TLS socket:
70+
71+
* `TLSSocket` <br>
72+
Extends TCP socket with certicate and key setter methods and internally handles TLS socket control to network stack using offload TLS socket setsockopt() settings.
73+
74+
```
75+
,--------------.
76+
|TCPSocket |
77+
|--------------|
78+
|--------------|
79+
|int connect();|
80+
|int recv(); |
81+
|int send(); |
82+
`--------------'
83+
|
84+
|
85+
,----------------------------. ,-----------------------------.
86+
|TLSSocket | |NetworkStack implementation |
87+
|----------------------------| |-----------------------------|
88+
|----------------------------| -----> |-----------------------------|
89+
|int set_root_ca_cert(...); | |nsapi_error_t setsockopt(...)|
90+
`----------------------------' `-----------------------------'
91+
```
92+
6493
## High Level Design Goal 1: Abstract socket API
6594

66-
Secure socket both uses `Socket` interface as its transport layer and implements it. This makes it transport independent and there is no direct dependency to IP stack.
95+
Mbed TLS based secure socket both uses `Socket` interface as its transport layer and implements it. This makes it transport independent and there is no direct dependency to IP stack.
96+
97+
Offloaded secure socket shares the same API as Mbed TLS based TLSSocket as much as possible. This API has no dependency to Mbed TLS APIs.
6798

6899
When TLSSocket implements Socket API it is able to be used instead of TCP connection in
69100
any Mbed OS library. For example MQTT library is made secure without any code changes:
@@ -75,13 +106,12 @@ Aim is to first support only certificate based authentication, so we implement o
75106

76107
## High Level Design Goal 3: Support both blocking and non-blocking operations
77108

78-
As the Mbed TLS is already work with both socket types, we are able to `TLSocketWrapper`
79-
that can handle both types as well.
109+
As the Mbed TLS already worsk with both socket types, we are able to create `TLSSocketWrapper` that can handle both types as well.
80110

81111
Functions `set_blocking()` and `set_timeout()` just pass the information for underlying
82112
transport socket. Extra handling on the TLS state machine is not required.
83113

84-
## High Level Design Goal 4: Expose Mbed TLS structures
114+
## High Level Design Goal 4: Expose Mbed TLS structures (Mbed TLS secure socket only)
85115

86116
Exposing Mbed TLS configuration structures allows user to configure the underlying TLS instance using Mbed TLS API. This allows greater usability as TLSSocket is not limited to only one use case.
87117

@@ -99,14 +129,22 @@ Mbed OS [Socket interface](https://github.com/ARMmbed/mbed-os/blob/master/featur
99129

100130
### Receiving and sending data from Mbed TLS
101131

132+
Mbed TLS based secure socket:
133+
102134
`TLSSocketWrapper` contains static wrappers `ssl_recv()` and `ssl_send()` functions which are
103135
registered to Mbed TLS library in `mbedtls_ssl_set_bio()` call.
104136

105-
There functions then call the transport socket's `Socket::send()` and `Socket::recv()` calls
137+
These functions then call the transport socket's `Socket::send()` and `Socket::recv()` calls
106138
respectively. Error coded are passed through, except `NSAPI_ERROR_WOULD_BLOCK` which is translated to `MBEDTLS_ERR_SSL_WANT_WRITE` or `MBEDTLS_ERR_SSL_WANT_READ`.
107139

140+
Offloaded secure socket:
141+
142+
When socket has been marked as secure socket in network stack by `TLSSocket::connect()`, network stack must handle `socket_send()` and `socket_recv()` respectively, i.e. in external modem case these methods send secure socket specific send/receive commands to modem.
143+
108144
### Providing Socket API
109145

146+
Mbed TLS based secure socket:
147+
110148
```
111149
virtual nsapi_error_t close();
112150
```
@@ -155,7 +193,7 @@ These are returning `NSAPI_ERROR_UNSUPPORTED` as TLS socket cannot be set to lis
155193

156194
## Detailed Design for certificate based authentication
157195

158-
`TLSSocketWrapper` provides following API to set server certificate. You can use either BASE64 formatted PEM certificate, or binary DER certificates. Later form just assumes `root_ca_pem` or `client_cert_pem` to be standard C string and counts its lenght and passes to method which takes just `void*` and `len`.
196+
TLSSocket API provides following API to set server certificate. For Mbed TLS secure socket, this API is implemented by `TLSSocketWrapper` class. You can use either BASE64 formatted PEM certificate, or binary DER certificates. Later form just assumes `root_ca_pem` or `client_cert_pem` to be standard C string and counts its lenght and passes to method which takes just `void*` and `len`.
159197

160198
```
161199
/** Sets the certification of Root CA.
@@ -193,7 +231,7 @@ nsapi_error_t TLSSocketWrapper::set_client_cert_key(const char *client_cert_pem,
193231

194232
Certificate is then passed unmodified to `mbedtls_x509_crt_parse()` function.
195233

196-
## Detailed Design for Support both blocking and non-blocking operations
234+
## Detailed Design for Support both blocking and non-blocking operations (Mbed TLS secure socket only)
197235

198236
`send()` and `receive()` methods do not need to know whether underlying socket is in
199237
blocking mode as Mbed OS return values are enough to tell that.
@@ -214,7 +252,7 @@ When this `NSAPI_ERROR_WOULD_BLOCK` is returned:
214252

215253
When transport socket is in blocking mode, it never returns `NSAPI_ERROR_WOULD_BLOCK` and therefore `mbedtls_ssl_write()` never gets `MBEDTLS_ERR_SSL_WANT_WRITE`, so any translation does not happen, but code path stays unchanged.
216254

217-
## Detailed Design for exposing Mbed TLS structures
255+
## Detailed Design for exposing Mbed TLS structures (Mbed TLS secure socket only)
218256

219257
TLSSocket exposes following API to provide access to internal Mbed TLS data structures:
220258
```
@@ -231,10 +269,30 @@ This allows sockets to share same configuration and allow user to fine tune TLS
231269
```
232270
TLSSocket a;
233271
TLSSocket b;
272+
a.open(<interface>);
273+
b.open(<interface>);
234274
a.set_root_ca_cert(<cert>);
235275
b.set_ssl_config(a.get_ssl_config());
236276
```
237277

278+
## Detailed Design for communication between TLSSocket and network stack (Offloaded secure socket only)
279+
280+
All TLSSocket control between TLSSocket and network stack is handled via socket options. Network stack must implement at least following options:
281+
282+
```
283+
typedef enum nsapi_tlssocket_level {
284+
NSAPI_TLSSOCKET_LEVEL = 7099, /*!< TLSSocket option level - see nsapi_tlssocket_option_t for options*/
285+
} nsapi_tlssocket_level_t;
286+
287+
typedef enum nsapi_tlssocket_option {
288+
NSAPI_TLSSOCKET_SET_HOSTNAME, /*!< Set host name */
289+
NSAPI_TLSSOCKET_SET_CACERT, /*!< Set server CA certificate */
290+
NSAPI_TLSSOCKET_SET_CLCERT, /*!< Set client certificate */
291+
NSAPI_TLSSOCKET_SET_CLKEY, /*!< Set client key */
292+
NSAPI_TLSSOCKET_ENABLE /*!< Enable TLSSocket */
293+
} nsapi_tlssocket_option_t;
294+
```
295+
238296
# Usage Scenarios and Examples
239297

240298
## Scenario 1: Connecting to secure server:
@@ -259,17 +317,34 @@ No tool changes required
259317

260318
# Other Information
261319

320+
## Selecting Mbed TLS secure socket or offloaded TLS socket
321+
322+
By default system is configured to use Mbed TLS based secure socket as only few network stacks support offloaded TLS socket.
323+
Offloaded TLS socket can be enabled in application json configuration file by setting: `"nsapi.offload-tlssocket": true`
324+
262325
## Reusability
263326

327+
Mbed TLS secure socket:
264328
Parts of the state machine are probably relevant when implementing DTLS socket.
265329

266330
TLSSocketWrapper is entirely reusable when doing TLS handshake using any socket type.
267331
It does not have tight bindings to TCP.
268332

333+
Offloaded secure socket:
334+
Current implementation is for TLS only and does not support DTLS.
335+
269336
## Assumptions
270337

271-
We are assuming that server certificate is given from the application to `TLSSocket::set_root_ca_cert()` interface in a format that is understood by Mbed TLS.
338+
We are assuming that server certificate is given from the application to `TLSSocket::set_root_ca_cert()` interface in a format that is understood by Mbed TLS or, in offloaded TLS socket case, the implementation of TLS socket in network stack.
272339

273340
## Deprecations
274341

275342
No deprecations
343+
344+
## Security
345+
346+
Offloaded TLS socket has one major security difference compared to Mbed TLS based secure socket. In Mbed TLS secure socket data is encrypted already by application processor and therefore all user data between MCU and network interface HW is encrypted. But with offloaded TLS socket, encryption happens in network stack, which can be implemented in network HW resulting in data between MCU and network HW being unencrypted (e.g. in cellular case, UART traffic between MCU and cellular modem).
347+
348+
## Memory consumption
349+
350+
Maybe one of the biggest reason selecting offloaded TLSSocket is memory consumption. In offloaded TLSSocket TLS implementation can be in modem firmware resulting in a significantly smaller application size and RAM consumption if application does not need Mbed TLS functionality for other reasons.

0 commit comments

Comments
 (0)