|
| 1 | +## Secure Socket |
| 2 | + |
| 3 | +Mbed OS provides easy interface for creating secure connections in form of TLS stream. |
| 4 | +`TLSSocket` class provides the ability to secure any stream based socket connection, for example TCP stream. This allows existing protocol libraries to be used through secure connections. |
| 5 | + |
| 6 | +`TLSSocket` is inheriting the `Socket` class, which allows any application that uses `Socket` to use `TLSSocket` instead. |
| 7 | +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. For example, we can use HTTP library and give `TLSSocket` for it, to covert it to HTTPS. |
| 8 | + |
| 9 | +### Usage example |
| 10 | + |
| 11 | +TLSSocket API follows Socket API so it is easy to use after setting up: |
| 12 | + |
| 13 | +``` |
| 14 | +TLSSocket *socket = new TLSSocket(); |
| 15 | +socket->open(network) |
| 16 | +socket->set_root_ca_cert(certificate); |
| 17 | +
|
| 18 | +socket->connect(HOST_NAME, PORT) |
| 19 | +
|
| 20 | +socket->send(data, size); |
| 21 | +``` |
| 22 | + |
| 23 | +Please note that internal TLS structures require over 1 kB of RAM, so each TLSSocket should be allocated from heap using `new` command, instead of using stack or statically allocating it. |
| 24 | + |
| 25 | + |
| 26 | +### Design |
| 27 | + |
| 28 | +Internally `TLSSocket` consist of two classes `TLSSocketWrapper` and `TLSSocket` as shown in the following diagram: |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +The `TLSSocketWrapper` is able to use any `Socket` as its transport. `TLSSocket` is a helper that uses directly `TCPSocket` for its transport, making it easy to adopt existing TCP based applications to TLS. |
| 33 | + |
| 34 | +One use case of `TLSSocketWrapper` is that existing TCP socket can be upgraded to TLS, by wrapping it like this: |
| 35 | + |
| 36 | +``` |
| 37 | +TCPSocket connection(net); |
| 38 | +connection.connect(SERVER, PORT); |
| 39 | +
|
| 40 | +// First talk with the server without encryption |
| 41 | +connection.send("STARTTLS\r\n", 10); |
| 42 | +
|
| 43 | +// Wrap the TCP into TLS object |
| 44 | +TLSSocketWrapper tls = new TLSSocketWrapper(connection, SERVER, TLSSocketWrapper::TRANSPORT_CLOSE); |
| 45 | +
|
| 46 | +// Initiate TLS handshake |
| 47 | +tls.connect(); |
| 48 | +
|
| 49 | +// Now the secure connection can be used like regular socket |
| 50 | +tls.send("HELLO", 5); |
| 51 | +``` |
| 52 | + |
| 53 | +### API |
| 54 | + |
| 55 | +`TLSSocketWrapper` implements Mbed OS Socket API and extends it with functions that allow configuring security certificates. Please note that for most of the use cases, you are using these methods through `TLSSocket` class. |
| 56 | + |
| 57 | +#### Configuring certificates |
| 58 | + |
| 59 | +`TLSSocketWrapper` provides following API to set server certificate. You can use either BASE64 formatted PEM certificate, or binary DER certificates. Later form of these functions 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`. |
| 60 | + |
| 61 | +``` |
| 62 | +/** Sets the certification of Root CA. |
| 63 | + * |
| 64 | + * @param root_ca Root CA Certificate in any mbed-TLS supported format. |
| 65 | + * @param len Length of certificate (including terminating 0 for PEM). |
| 66 | + */ |
| 67 | +nsapi_error_t TLSSocketWrapper::set_root_ca_cert(const void *root_ca, size_t len); |
| 68 | +
|
| 69 | +/** Sets the certification of Root CA. |
| 70 | + * |
| 71 | + * @param root_ca_pem Root CA Certificate in PEM format |
| 72 | + */ |
| 73 | +nsapi_error_t TLSSocketWrapper::set_root_ca_cert(const char *root_ca_pem); |
| 74 | +``` |
| 75 | + |
| 76 | +If client authentication is required, following API allows you to set the client certificate and private key: |
| 77 | + |
| 78 | +``` |
| 79 | +/** Sets client certificate, and client private key. |
| 80 | + * |
| 81 | + * @param client_cert Client certification in any mbed-TLS supported format. |
| 82 | + * @param client_private_key Client private key in PEM format. |
| 83 | + */ |
| 84 | +nsapi_error_t TLSSocketWrapper::set_client_cert_key(const void *client_cert_pem, size_t client_cert_len, |
| 85 | + const void *client_private_key_pem, size_t client_private_key_len); |
| 86 | +
|
| 87 | +/** Sets client certificate, and client private key. |
| 88 | + * |
| 89 | + * @param client_cert_pem Client certification in PEM format. |
| 90 | + * @param client_private_key Client private key in PEM format. |
| 91 | + */ |
| 92 | +nsapi_error_t TLSSocketWrapper::set_client_cert_key(const char *client_cert_pem, const char *client_private_key_pem); |
| 93 | +``` |
| 94 | + |
| 95 | +#### Socket API |
| 96 | + |
| 97 | +`TLSSocketWrapper` implements [Mbed OS Socket API](https://os.mbed.com/docs/v5.10/apis/network-socket.html) as follows. |
| 98 | + |
| 99 | +``` |
| 100 | +virtual nsapi_error_t close(); |
| 101 | +``` |
| 102 | + |
| 103 | +Destroys the memory allocated by TLS library. |
| 104 | +Also closes the transport socket, unless [transport mode](#transport-modes) is set to `TRANSPORT_KEEP` or `TRANSPORT_CONNECT`. |
| 105 | + |
| 106 | + |
| 107 | +``` |
| 108 | +virtual nsapi_error_t connect(const SocketAddress &address); |
| 109 | +``` |
| 110 | + |
| 111 | +Initiates the TCP connection and continues to TLS hanshake. If [transport mode](#transport-modes) is either `TRANSPORT_KEEP` or `TRANSPORT_CLOSE`, TCP is assumed to be open and state directly goes into TLS handshake. |
| 112 | +This is currently forced to blocking mode. After succesfully connecting, you can set it to non-blockin mode. |
| 113 | + |
| 114 | +``` |
| 115 | +virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size); |
| 116 | +virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size); |
| 117 | +virtual nsapi_size_or_error_t sendto(const SocketAddress &address, const void *data, nsapi_size_t size); |
| 118 | +virtual nsapi_size_or_error_t recvfrom(SocketAddress *address, void *data, nsapi_size_t size); |
| 119 | +``` |
| 120 | +These work as expected, but `SocketAddress` parameters are ignored. TLS connection cannot |
| 121 | +change the peer. Also `recvfrom()` call does not set the peer address. |
| 122 | + |
| 123 | +Mbed TLS error codes `MBEDTLS_ERR_SSL_WANT_READ` and `MBEDTLS_ERR_SSL_WANT_WRITE` are |
| 124 | +translated to `NSAPI_ERROR_WOULD_BLOCK` before passing to user. |
| 125 | + |
| 126 | +`MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY` is ignored and zero is returned to user (connection closed). Other error codes are passed through. |
| 127 | + |
| 128 | +``` |
| 129 | +virtual nsapi_error_t bind(const SocketAddress &address); |
| 130 | +virtual void set_blocking(bool blocking); |
| 131 | +virtual void set_timeout(int timeout); |
| 132 | +virtual void sigio(mbed::Callback<void()> func); |
| 133 | +virtual nsapi_error_t setsockopt(int level, int optname, const void *optval, unsigned optlen); |
| 134 | +virtual nsapi_error_t getsockopt(int level, int optname, void *optval, unsigned *optlen); |
| 135 | +``` |
| 136 | +These are passed through to transport socket. |
| 137 | + |
| 138 | + |
| 139 | +``` |
| 140 | +virtual Socket *accept(nsapi_error_t *error = NULL); |
| 141 | +virtual nsapi_error_t listen(int backlog = 1); |
| 142 | +``` |
| 143 | +These are returning `NSAPI_ERROR_UNSUPPORTED` as TLS socket cannot be set to listening mode. |
| 144 | + |
| 145 | +#### Transport modes |
| 146 | + |
| 147 | +`TLSSocketWrapper` has four modes that are given in the constructor and affect how the transport Socket is used in connection and closing phases. |
| 148 | + |
| 149 | +|Mode|Behaviour on trasport socket| |
| 150 | +|----|----------------------------| |
| 151 | +|TRANSPORT_KEEP | Keep the transport as it is. Does not call `connect()` or `close()` methods. | |
| 152 | +|TRANSPORT_CONNECT_AND_CLOSE | Both `connect()` and `close()` are called. (default) | |
| 153 | +|TRANSPORT_CONNECT | Call `connect()` but do not close the connection when finished. | |
| 154 | +|TRANSPORT_CLOSE | Call `close()` when connection is finished. | |
| 155 | + |
| 156 | +Default mode is `TRANSPORT_CONNECT_AND_CLOSE`. |
| 157 | + |
| 158 | + |
| 159 | +#### Advanced usage: using internal Mbed TLS structures |
| 160 | + |
| 161 | +User may choose to use internal Mbed TLS structures to configure the TLS instance. This is supported by exposing some Mbed TLS structures like this: |
| 162 | + |
| 163 | +``` |
| 164 | +mbedtls_x509_crt *get_own_cert(); |
| 165 | +int set_own_cert(mbedtls_x509_crt *); |
| 166 | +mbedtls_x509_crt *get_ca_chain(); |
| 167 | +void set_ca_chain(mbedtls_x509_crt *); |
| 168 | +mbedtls_ssl_config *get_ssl_config(); |
| 169 | +void set_ssl_config(mbedtls_ssl_config *); |
| 170 | +``` |
| 171 | + |
| 172 | +For guidance of how to use these, please refer to Mbed TLS documentation. |
0 commit comments