You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
6
7
7
8
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.
10
10
11
11
High level goal is to demonstrate Mbed OS users that secure connections are not hard to do.
12
12
13
13
## Design Limitations
14
14
15
-
Following design limitations exist in the current design of TLSSocket
15
+
Following design limitations exist in the current design of TLSSocket:
16
16
17
+
Mbed TLS based TLSSocket:
17
18
*`TLSSocket::connect()` is always blocking
18
19
* 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.
19
20
* No PSK mode
20
21
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
+
21
26
# System Architecture and High-Level Design
22
27
23
-
Secure socket consist of two classes:
28
+
Mbed TLS based secure socket consist of two classes:
24
29
25
-
*`TLSocketWrapper` <br>
30
+
*`TLSSocketWrapper` <br>
26
31
Which handles initialization of TLS library and does TLS handsake. Takes any Socket as a
27
32
trasport.
28
33
*`TLSSocket` <br>
29
-
Which inherits TLSocketWrapper and has TCP socket as a transport.
34
+
Which inherits TLSSocketWrapper and has TCP socket as a transport.
30
35
Adds `connect(char *hostname)`
31
36
for initiating the TCP and TLS handshakes at one call.
32
37
@@ -41,16 +46,16 @@ Secure socket consist of two classes:
41
46
`--------------'
42
47
|
43
48
|
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
+
`-----------------------------'
54
59
|
55
60
,----------------------------.
56
61
|TLSSocket |
@@ -61,9 +66,35 @@ Secure socket consist of two classes:
61
66
`----------------------------'
62
67
```
63
68
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.
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.
67
98
68
99
When TLSSocket implements Socket API it is able to be used instead of TCP connection in
69
100
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
75
106
76
107
## High Level Design Goal 3: Support both blocking and non-blocking operations
77
108
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.
80
110
81
111
Functions `set_blocking()` and `set_timeout()` just pass the information for underlying
82
112
transport socket. Extra handling on the TLS state machine is not required.
83
113
84
-
## High Level Design Goal 4: Expose Mbed TLS structures
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.
87
117
@@ -99,14 +129,22 @@ Mbed OS [Socket interface](https://github.com/ARMmbed/mbed-os/blob/master/featur
99
129
100
130
### Receiving and sending data from Mbed TLS
101
131
132
+
Mbed TLS based secure socket:
133
+
102
134
`TLSSocketWrapper` contains static wrappers `ssl_recv()` and `ssl_send()` functions which are
103
135
registered to Mbed TLS library in `mbedtls_ssl_set_bio()` call.
104
136
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
106
138
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`.
107
139
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
+
108
144
### Providing Socket API
109
145
146
+
Mbed TLS based secure socket:
147
+
110
148
```
111
149
virtual nsapi_error_t close();
112
150
```
@@ -155,7 +193,7 @@ These are returning `NSAPI_ERROR_UNSUPPORTED` as TLS socket cannot be set to lis
155
193
156
194
## Detailed Design for certificate based authentication
157
195
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`.
Certificate is then passed unmodified to `mbedtls_x509_crt_parse()` function.
195
233
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)
197
235
198
236
`send()` and `receive()` methods do not need to know whether underlying socket is in
199
237
blocking mode as Mbed OS return values are enough to tell that.
@@ -214,7 +252,7 @@ When this `NSAPI_ERROR_WOULD_BLOCK` is returned:
214
252
215
253
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.
216
254
217
-
## Detailed Design for exposing Mbed TLS structures
TLSSocket exposes following API to provide access to internal Mbed TLS data structures:
220
258
```
@@ -231,10 +269,30 @@ This allows sockets to share same configuration and allow user to fine tune TLS
231
269
```
232
270
TLSSocket a;
233
271
TLSSocket b;
272
+
a.open(<interface>);
273
+
b.open(<interface>);
234
274
a.set_root_ca_cert(<cert>);
235
275
b.set_ssl_config(a.get_ssl_config());
236
276
```
237
277
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
+
238
296
# Usage Scenarios and Examples
239
297
240
298
## Scenario 1: Connecting to secure server:
@@ -259,17 +317,34 @@ No tool changes required
259
317
260
318
# Other Information
261
319
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
+
262
325
## Reusability
263
326
327
+
Mbed TLS secure socket:
264
328
Parts of the state machine are probably relevant when implementing DTLS socket.
265
329
266
330
TLSSocketWrapper is entirely reusable when doing TLS handshake using any socket type.
267
331
It does not have tight bindings to TCP.
268
332
333
+
Offloaded secure socket:
334
+
Current implementation is for TLS only and does not support DTLS.
335
+
269
336
## Assumptions
270
337
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.
272
339
273
340
## Deprecations
274
341
275
342
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