Skip to content

Commit e069ae7

Browse files
authored
Merge pull request #520 from johanstokking/feature/websocket/client_ds_data
feat(websocket): Support DS peripheral for mutual TLS (IDFGH-12285)
2 parents a3c2bbe + 44d476f commit e069ae7

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

components/esp_websocket_client/esp_websocket_client.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ typedef struct {
9393
size_t client_cert_len;
9494
const char *client_key;
9595
size_t client_key_len;
96+
#if CONFIG_ESP_TLS_USE_DS_PERIPHERAL
97+
void *client_ds_data;
98+
#endif
9699
bool use_global_ca_store;
97100
bool skip_cert_common_name_check;
98101
const char *cert_common_name;
@@ -531,6 +534,10 @@ static esp_err_t esp_websocket_client_create_transport(esp_websocket_client_hand
531534
} else {
532535
esp_transport_ssl_set_client_key_data_der(ssl, client->config->client_key, client->config->client_key_len);
533536
}
537+
#if CONFIG_ESP_TLS_USE_DS_PERIPHERAL
538+
} else if (client->config->client_ds_data) {
539+
esp_transport_ssl_set_ds_data(ssl, client->config->client_ds_data);
540+
#endif
534541
}
535542
if (client->config->crt_bundle_attach) {
536543
#ifdef CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
@@ -696,6 +703,9 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
696703
client->config->client_cert_len = config->client_cert_len;
697704
client->config->client_key = config->client_key;
698705
client->config->client_key_len = config->client_key_len;
706+
#if CONFIG_ESP_TLS_USE_DS_PERIPHERAL
707+
client->config->client_ds_data = config->client_ds_data;
708+
#endif
699709
client->config->skip_cert_common_name_check = config->skip_cert_common_name_check;
700710
client->config->cert_common_name = config->cert_common_name;
701711
client->config->crt_bundle_attach = config->crt_bundle_attach;

components/esp_websocket_client/include/esp_websocket_client.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,13 @@ typedef struct {
108108
int buffer_size; /*!< Websocket buffer size */
109109
const char *cert_pem; /*!< Pointer to certificate data in PEM or DER format for server verify (with SSL), default is NULL, not required to verify the server. PEM-format must have a terminating NULL-character. DER-format requires the length to be passed in cert_len. */
110110
size_t cert_len; /*!< Length of the buffer pointed to by cert_pem. May be 0 for null-terminated pem */
111-
const char *client_cert; /*!< Pointer to certificate data in PEM or DER format for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed. If it is not NULL, also `client_key` has to be provided. PEM-format must have a terminating NULL-character. DER-format requires the length to be passed in client_cert_len. */
111+
const char *client_cert; /*!< Pointer to certificate data in PEM or DER format for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed. If it is not NULL, also `client_key` or `client_ds_data` (if supported) has to be provided. PEM-format must have a terminating NULL-character. DER-format requires the length to be passed in client_cert_len. */
112112
size_t client_cert_len; /*!< Length of the buffer pointed to by client_cert. May be 0 for null-terminated pem */
113-
const char *client_key; /*!< Pointer to private key data in PEM or DER format for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed. If it is not NULL, also `client_cert` has to be provided. PEM-format must have a terminating NULL-character. DER-format requires the length to be passed in client_key_len */
113+
const char *client_key; /*!< Pointer to private key data in PEM or DER format for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed. If it is not NULL, also `client_cert` has to be provided and `client_ds_data` (if supported) gets ignored. PEM-format must have a terminating NULL-character. DER-format requires the length to be passed in client_key_len */
114114
size_t client_key_len; /*!< Length of the buffer pointed to by client_key_pem. May be 0 for null-terminated pem */
115+
#if CONFIG_ESP_TLS_USE_DS_PERIPHERAL
116+
void *client_ds_data; /*!< Pointer to the encrypted private key data for SSL mutual authentication using the DS peripheral, default is NULL, not required if mutual authentication is not needed. If it is not NULL, also `client_cert` has to be provided. It is ignored if `client_key` is provided */
117+
#endif
115118
esp_websocket_transport_t transport; /*!< Websocket transport type, see `esp_websocket_transport_t */
116119
const char *subprotocol; /*!< Websocket subprotocol */
117120
const char *user_agent; /*!< Websocket user-agent */

docs/esp_websocket_client/en/index.rst

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,43 @@ Configuration:
6666
.. note:: If you want to verify the server, then you need to provide a certificate in PEM format, and provide to ``cert_pem`` in :cpp:type:`websocket_client_config_t`. If no certficate is provided then the TLS connection will default to not requiring verification.
6767

6868
PEM certificate for this example could be extracted from an openssl `s_client` command connecting to websocket.org.
69-
In case a host operating system has `openssl` and `sed` packages installed, one could execute the following command to download and save the root or intermediate root certificate to a file (Note for Windows users: Both Linux like environment or Windows native packages may be used).
70-
```
71-
echo "" | openssl s_client -showcerts -connect websocket.org:443 | sed -n "1,/Root/d; /BEGIN/,/END/p" | openssl x509 -outform PEM >websocket_org.pem
72-
```
69+
In case a host operating system has `openssl` and `sed` packages installed, one could execute the following command to download and save the root or intermediate root certificate to a file (Note for Windows users: Both Linux like environment or Windows native packages may be used). ::
70+
71+
echo "" | openssl s_client -showcerts -connect websocket.org:443 \
72+
| sed -n "1,/Root/d; /BEGIN/,/END/p" \
73+
| openssl x509 -outform PEM \
74+
> websocket_org.pem
7375

7476
This command will extract the second certificate in the chain and save it as a pem-file.
7577

78+
Mutual TLS with DS Peripheral
79+
"""""""""""""""""""""""""""""
80+
81+
To leverage the Digital Signature (DS) peripheral on supported targets, use `esp_secure_cert_mgr <https://github.com/espressif/esp_secure_cert_mgr/>`_ to flash an encrypted client certificate. In your project, add the dependency: ::
82+
83+
idf.py add-dependency esp_secure_cert_mgr
84+
85+
Set ``client_cert`` and ``client_ds_data`` in the config struct:
86+
87+
.. code:: c
88+
89+
char *client_cert = NULL;
90+
uint32_t client_cert_len = 0;
91+
esp_err_t err = esp_secure_cert_get_device_cert(&client_cert, &client_cert_len);
92+
assert(err == ESP_OK);
93+
94+
esp_ds_data_ctx_t *ds_data = esp_secure_cert_get_ds_ctx();
95+
assert(ds_data != NULL);
96+
97+
esp_websocket_client_config_t config = {
98+
.uri = "wss://echo.websocket.org",
99+
.cert_pem = (const char *)websocket_org_pem_start,
100+
.client_cert = client_cert,
101+
.client_ds_data = ds_data,
102+
};
103+
104+
.. note:: ``client_cert`` provided by `esp_secure_cert_mgr` is a null-terminated PEM; so ``client_cert_len`` (DER format) should not be set.
105+
76106
Subprotocol
77107
^^^^^^^^^^^
78108

@@ -91,14 +121,14 @@ For more options on :cpp:type:`esp_websocket_client_config_t`, please refer to A
91121

92122
Events
93123
------
94-
* `WEBSOCKET_EVENT_BEGIN': The client thread is running.
124+
* `WEBSOCKET_EVENT_BEGIN`: The client thread is running.
95125
* `WEBSOCKET_EVENT_BEFORE_CONNECT`: The client is about to connect.
96126
* `WEBSOCKET_EVENT_CONNECTED`: The client has successfully established a connection to the server. The client is now ready to send and receive data. Contains no event data.
97127
* `WEBSOCKET_EVENT_DATA`: The client has successfully received and parsed a WebSocket frame. The event data contains a pointer to the payload data, the length of the payload data as well as the opcode of the received frame. A message may be fragmented into multiple events if the length exceeds the buffer size. This event will also be posted for non-payload frames, e.g. pong or connection close frames.
98128
* `WEBSOCKET_EVENT_ERROR`: The client has experienced an error. Examples include transport write or read failures.
99129
* `WEBSOCKET_EVENT_DISCONNECTED`: The client has aborted the connection due to the transport layer failing to read data, e.g. because the server is unavailable. Contains no event data.
100130
* `WEBSOCKET_EVENT_CLOSED`: The connection has been closed cleanly.
101-
* `WEBSOCKET_EVENT_FINISH': The client thread is about to exit.
131+
* `WEBSOCKET_EVENT_FINISH`: The client thread is about to exit.
102132

103133
If the client handle is needed in the event handler it can be accessed through the pointer passed to the event handler:
104134

0 commit comments

Comments
 (0)