test_esp_crt_bundle.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /* SSL server using plain mbedTLS sockets
  2. *
  3. * Adapted from the ssl_server example in mbedtls.
  4. *
  5. * Original Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  6. * Additions Copyright (C) Copyright 2019 Espressif Systems (Shanghai) PTE LTD, Apache 2.0 License.
  7. *
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. #include "esp_err.h"
  22. #include "esp_log.h"
  23. #include "freertos/FreeRTOS.h"
  24. #include "freertos/task.h"
  25. #include "freertos/semphr.h"
  26. #include "mbedtls/entropy.h"
  27. #include "mbedtls/ctr_drbg.h"
  28. #include "mbedtls/certs.h"
  29. #include "mbedtls/x509.h"
  30. #include "mbedtls/ssl.h"
  31. #include "mbedtls/net_sockets.h"
  32. #include "mbedtls/error.h"
  33. #include "mbedtls/debug.h"
  34. #include "esp_crt_bundle.h"
  35. #include "unity.h"
  36. #include "test_utils.h"
  37. #define SERVER_ADDRESS "localhost"
  38. #define SERVER_PORT "4433"
  39. extern const uint8_t server_cert_chain_pem_start[] asm("_binary_server_cert_chain_pem_start");
  40. extern const uint8_t server_cert_chain_pem_end[] asm("_binary_server_cert_chain_pem_end");
  41. extern const uint8_t server_pk_start[] asm("_binary_prvtkey_pem_start");
  42. extern const uint8_t server_pk_end[] asm("_binary_prvtkey_pem_end");
  43. extern const uint8_t server_cert_bundle_start[] asm("_binary_server_cert_bundle_start");
  44. extern const uint8_t server_cert_bundle_end[] asm("_binary_server_cert_bundle_end");
  45. extern const uint8_t bad_md_crt_pem_start[] asm("_binary_bad_md_crt_pem_start");
  46. extern const uint8_t bad_md_crt_pem_end[] asm("_binary_bad_md_crt_pem_end");
  47. extern const uint8_t wrong_sig_crt_pem_start[] asm("_binary_wrong_sig_crt_esp32_com_pem_start");
  48. extern const uint8_t wrong_sig_crt_pem_end[] asm("_binary_wrong_sig_crt_esp32_com_pem_end");
  49. extern const uint8_t correct_sig_crt_pem_start[] asm("_binary_correct_sig_crt_esp32_com_pem_start");
  50. extern const uint8_t correct_sig_crt_pem_end[] asm("_binary_correct_sig_crt_esp32_com_pem_end");
  51. typedef struct {
  52. mbedtls_ssl_context ssl;
  53. mbedtls_net_context listen_fd;
  54. mbedtls_net_context client_fd;
  55. mbedtls_entropy_context entropy;
  56. mbedtls_ctr_drbg_context ctr_drbg;
  57. mbedtls_ssl_config conf;
  58. mbedtls_x509_crt cert;
  59. mbedtls_pk_context pkey;
  60. } mbedtls_endpoint_t;
  61. typedef enum {
  62. ESP_CRT_VALIDATE_UNKNOWN,
  63. ESP_CRT_VALIDATE_OK,
  64. ESP_CRT_VALIDATE_FAIL,
  65. }esp_crt_validate_res_t;
  66. int esp_crt_verify_callback(void *buf, mbedtls_x509_crt *crt, int data, uint32_t *flags);
  67. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3)
  68. // TODO ESP32-S3 IDF-1878
  69. static const char *TAG = "cert_bundle_test";
  70. static volatile bool exit_flag;
  71. esp_err_t endpoint_teardown(mbedtls_endpoint_t *endpoint);
  72. esp_err_t server_setup(mbedtls_endpoint_t *server)
  73. {
  74. int ret;
  75. mbedtls_ssl_config_init( &server->conf );
  76. mbedtls_net_init( &server->listen_fd );
  77. mbedtls_net_init( &server->client_fd );
  78. mbedtls_ssl_init( &server->ssl );
  79. mbedtls_x509_crt_init( &server->cert );
  80. mbedtls_pk_init( &server->pkey );
  81. mbedtls_entropy_init( &server->entropy );
  82. mbedtls_ctr_drbg_init( &server->ctr_drbg );
  83. ESP_LOGI(TAG, "Loading the server cert and key");
  84. ret = mbedtls_x509_crt_parse( &server->cert, server_cert_chain_pem_start,
  85. server_cert_chain_pem_end - server_cert_chain_pem_start);
  86. if ( ret != 0 ) {
  87. ESP_LOGE(TAG, "mbedtls_x509_crt_parse returned %d", ret );
  88. return ESP_FAIL;
  89. }
  90. ret = mbedtls_pk_parse_key( &server->pkey, (const unsigned char *)server_pk_start,
  91. server_pk_end - server_pk_start, NULL, 0 );
  92. if ( ret != 0 ) {
  93. ESP_LOGE(TAG, "mbedtls_pk_parse_key returned %d", ret );
  94. return ESP_FAIL;
  95. }
  96. ESP_LOGI(TAG, "Bind on https://%s:%s/", SERVER_ADDRESS, SERVER_PORT );
  97. if ( ( ret = mbedtls_net_bind( &server->listen_fd, NULL, SERVER_PORT, MBEDTLS_NET_PROTO_TCP ) ) != 0 ) {
  98. ESP_LOGE(TAG, "mbedtls_net_bind returned %d", ret );
  99. return ESP_FAIL;
  100. }
  101. mbedtls_net_set_nonblock(&server->listen_fd);
  102. ESP_LOGI(TAG, "Seeding the random number generator");
  103. if ( ( ret = mbedtls_ctr_drbg_seed( &server->ctr_drbg, mbedtls_entropy_func, &server->entropy,
  104. NULL, 0) ) != 0 ) {
  105. ESP_LOGE(TAG, "mbedtls_ctr_drbg_seed returned %d", ret );
  106. return ESP_FAIL;
  107. }
  108. ESP_LOGI(TAG, "Setting up the SSL data");
  109. if ( ( ret = mbedtls_ssl_config_defaults( &server->conf,
  110. MBEDTLS_SSL_IS_SERVER,
  111. MBEDTLS_SSL_TRANSPORT_STREAM,
  112. MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 ) {
  113. ESP_LOGE(TAG, "mbedtls_ssl_config_defaults returned %d", ret );
  114. return ESP_FAIL;
  115. }
  116. mbedtls_ssl_conf_rng( &server->conf, mbedtls_ctr_drbg_random, &server->ctr_drbg );
  117. if (( ret = mbedtls_ssl_conf_own_cert( &server->conf, &server->cert, &server->pkey ) ) != 0 ) {
  118. ESP_LOGE(TAG, "mbedtls_ssl_conf_own_cert returned %d", ret );
  119. return ESP_FAIL;
  120. }
  121. if (( ret = mbedtls_ssl_setup( &server->ssl, &server->conf ) ) != 0 ) {
  122. ESP_LOGE(TAG, "mbedtls_ssl_setup returned %d", ret );
  123. return ESP_FAIL;
  124. }
  125. return ESP_OK;
  126. }
  127. void server_task(void *pvParameters)
  128. {
  129. int ret;
  130. mbedtls_endpoint_t server;
  131. xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters;
  132. if (server_setup(&server) != ESP_OK) {
  133. ESP_LOGE(TAG, "SSL server setup failed");
  134. goto exit;
  135. }
  136. /* Signal that server is up and hence client task can start now */
  137. xSemaphoreGive(*sema);
  138. bool connected = false;
  139. while (!exit_flag) {
  140. ret = mbedtls_net_accept( &server.listen_fd, &server.client_fd, NULL, 0, NULL );
  141. if (ret == 0) {
  142. connected = true;
  143. }
  144. if (connected) {
  145. mbedtls_ssl_set_bio( &server.ssl, &server.client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
  146. ret = mbedtls_ssl_handshake( &server.ssl );
  147. mbedtls_ssl_session_reset(&server.ssl);
  148. connected = false;
  149. }
  150. vTaskDelay(20 / portTICK_PERIOD_MS);
  151. }
  152. ESP_LOGE(TAG, "Server shutdown");
  153. exit:
  154. endpoint_teardown(&server);
  155. xSemaphoreGive(*sema);
  156. vTaskDelete(NULL);
  157. }
  158. esp_err_t endpoint_teardown(mbedtls_endpoint_t *endpoint)
  159. {
  160. mbedtls_net_free( &endpoint->client_fd );
  161. mbedtls_net_free( &endpoint->listen_fd );
  162. mbedtls_x509_crt_free( &endpoint->cert );
  163. mbedtls_pk_free( &endpoint->pkey );
  164. mbedtls_ssl_free( &endpoint->ssl );
  165. mbedtls_ssl_config_free( &endpoint->conf );
  166. mbedtls_ctr_drbg_free( &endpoint->ctr_drbg );
  167. mbedtls_entropy_free( &endpoint->entropy );
  168. return ESP_OK;
  169. }
  170. esp_err_t client_setup(mbedtls_endpoint_t *client)
  171. {
  172. int ret;
  173. mbedtls_ssl_config_init( &client->conf );
  174. mbedtls_net_init( &client->client_fd );
  175. mbedtls_ssl_init( &client->ssl );
  176. mbedtls_x509_crt_init( &client->cert );
  177. mbedtls_pk_init( &client->pkey );
  178. mbedtls_entropy_init( &client->entropy );
  179. mbedtls_ctr_drbg_init( &client->ctr_drbg );
  180. ESP_LOGI(TAG, "Seeding the random number generator");
  181. if ((ret = mbedtls_ctr_drbg_seed(&client->ctr_drbg, mbedtls_entropy_func, &client->entropy,
  182. NULL, 0)) != 0) {
  183. ESP_LOGE(TAG, "mbedtls_ctr_drbg_seed returned %d", ret);
  184. return ESP_FAIL;
  185. }
  186. ESP_LOGI(TAG, "Setting hostname for TLS session...");
  187. /* Hostname set here should match CN in server certificate */
  188. if ((ret = mbedtls_ssl_set_hostname(&client->ssl, SERVER_ADDRESS)) != 0) {
  189. ESP_LOGE(TAG, "mbedtls_ssl_set_hostname returned -0x%x", -ret);
  190. return ESP_FAIL;
  191. }
  192. ESP_LOGI(TAG, "Setting up the SSL/TLS structure...");
  193. if ((ret = mbedtls_ssl_config_defaults(&client->conf,
  194. MBEDTLS_SSL_IS_CLIENT,
  195. MBEDTLS_SSL_TRANSPORT_STREAM,
  196. MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
  197. ESP_LOGE(TAG, "mbedtls_ssl_config_defaults returned %d", ret);
  198. return ESP_FAIL;
  199. }
  200. mbedtls_ssl_conf_rng(&client->conf, mbedtls_ctr_drbg_random, &client->ctr_drbg);
  201. if ((ret = mbedtls_ssl_setup(&client->ssl, &client->conf)) != 0) {
  202. ESP_LOGE(TAG, "mbedtls_ssl_setup returned -0x%x\n\n", -ret);
  203. return ESP_FAIL;
  204. }
  205. return ESP_OK;
  206. }
  207. int client_task(const uint8_t *bundle, esp_crt_validate_res_t *res)
  208. {
  209. int ret = ESP_FAIL;
  210. mbedtls_endpoint_t client;
  211. *res = ESP_CRT_VALIDATE_UNKNOWN;
  212. if (client_setup(&client) != ESP_OK) {
  213. ESP_LOGE(TAG, "SSL client setup failed");
  214. goto exit;
  215. }
  216. esp_crt_bundle_attach(&client.conf);
  217. if (bundle) {
  218. /* Set a bundle different from the menuconfig bundle */
  219. esp_crt_bundle_set(bundle);
  220. }
  221. ESP_LOGI(TAG, "Connecting to %s:%s...", SERVER_ADDRESS, SERVER_PORT);
  222. if ((ret = mbedtls_net_connect(&client.client_fd, SERVER_ADDRESS, SERVER_PORT, MBEDTLS_NET_PROTO_TCP)) != 0) {
  223. ESP_LOGE(TAG, "mbedtls_net_connect returned -%x", -ret);
  224. goto exit;
  225. }
  226. ESP_LOGI(TAG, "Connected.");
  227. mbedtls_ssl_set_bio(&client.ssl, &client.client_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
  228. ESP_LOGI(TAG, "Performing the SSL/TLS handshake with bundle that is missing the server root certificate");
  229. while ( ( ret = mbedtls_ssl_handshake( &client.ssl ) ) != 0 ) {
  230. if ( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE ) {
  231. printf( "mbedtls_ssl_handshake failed with -0x%x\n", -ret );
  232. break;
  233. }
  234. }
  235. ESP_LOGI(TAG, "Verifying peer X.509 certificate for bundle ...");
  236. ret = mbedtls_ssl_get_verify_result(&client.ssl);
  237. *res = (ret == 0) ? ESP_CRT_VALIDATE_OK : ESP_CRT_VALIDATE_FAIL;
  238. // Reset session before new connection
  239. mbedtls_ssl_close_notify(&client.ssl);
  240. mbedtls_ssl_session_reset(&client.ssl);
  241. mbedtls_net_free( &client.client_fd);
  242. exit:
  243. mbedtls_ssl_close_notify(&client.ssl);
  244. mbedtls_ssl_session_reset(&client.ssl);
  245. esp_crt_bundle_detach(&client.conf);
  246. endpoint_teardown(&client);
  247. return ret;
  248. }
  249. TEST_CASE("custom certificate bundle", "[mbedtls]")
  250. {
  251. esp_crt_validate_res_t validate_res;
  252. test_case_uses_tcpip();
  253. xSemaphoreHandle signal_sem = xSemaphoreCreateBinary();
  254. TEST_ASSERT_NOT_NULL(signal_sem);
  255. exit_flag = false;
  256. xTaskCreate(server_task, "server task", 8192, &signal_sem, 10, NULL);
  257. // Wait for the server to start up
  258. if (!xSemaphoreTake(signal_sem, 10000 / portTICK_PERIOD_MS)) {
  259. TEST_FAIL_MESSAGE("signal_sem not released, server start failed");
  260. }
  261. /* Test with default crt bundle that doesnt contain the ca crt */
  262. client_task(NULL, &validate_res);
  263. TEST_ASSERT(validate_res == ESP_CRT_VALIDATE_FAIL);
  264. /* Test with bundle that does contain the CA crt */
  265. client_task(server_cert_bundle_start, &validate_res);
  266. TEST_ASSERT(validate_res == ESP_CRT_VALIDATE_OK);
  267. exit_flag = true;
  268. if (!xSemaphoreTake(signal_sem, 10000 / portTICK_PERIOD_MS)) {
  269. TEST_FAIL_MESSAGE("signal_sem not released, server exit failed");
  270. }
  271. vSemaphoreDelete(signal_sem);
  272. }
  273. #endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3)
  274. TEST_CASE("custom certificate bundle - weak hash", "[mbedtls]")
  275. {
  276. /* A weak signature hash on the trusted certificate should not stop
  277. us from verifying the chain, since we already trust it a weak signature hash is
  278. not a security issue */
  279. mbedtls_x509_crt crt;
  280. uint32_t flags = 0;
  281. esp_crt_bundle_attach(NULL);
  282. mbedtls_x509_crt_init( &crt );
  283. mbedtls_x509_crt_parse(&crt, bad_md_crt_pem_start, bad_md_crt_pem_end - bad_md_crt_pem_start);
  284. TEST_ASSERT(mbedtls_x509_crt_verify(&crt, NULL, NULL, NULL, &flags, esp_crt_verify_callback, NULL) == 0);
  285. mbedtls_x509_crt_free(&crt);
  286. esp_crt_bundle_detach(NULL);
  287. }
  288. TEST_CASE("custom certificate bundle - wrong signature", "[mbedtls]")
  289. {
  290. /* Check that the bundle will not verify a valid certificate from trusted root where the signature is wrong */
  291. mbedtls_x509_crt crt;
  292. uint32_t flags = 0;
  293. esp_crt_bundle_attach(NULL);
  294. mbedtls_x509_crt_init( &crt );
  295. /* esp32.com cert chain where 1 byte in the signature is changed */
  296. printf("Testing certificate with wrong signature\n");
  297. mbedtls_x509_crt_parse(&crt, wrong_sig_crt_pem_start, wrong_sig_crt_pem_end - wrong_sig_crt_pem_start);
  298. TEST_ASSERT(mbedtls_x509_crt_verify(&crt, NULL, NULL, NULL, &flags, esp_crt_verify_callback, NULL) != 0);
  299. mbedtls_x509_crt_free(&crt);
  300. mbedtls_x509_crt_init( &crt );
  301. /* the correct esp32.com cert chain*/
  302. printf("Testing certificate with correct signature\n");
  303. mbedtls_x509_crt_parse(&crt, correct_sig_crt_pem_start, correct_sig_crt_pem_end - correct_sig_crt_pem_start);
  304. TEST_ASSERT(mbedtls_x509_crt_verify(&crt, NULL, NULL, NULL, &flags, esp_crt_verify_callback, NULL) == 0);
  305. mbedtls_x509_crt_free(&crt);
  306. esp_crt_bundle_detach(NULL);
  307. }