esp_crt_bundle.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <string.h>
  14. #include <esp_system.h>
  15. #include "esp_crt_bundle.h"
  16. #include "esp_log.h"
  17. #include "esp_err.h"
  18. #define BUNDLE_HEADER_OFFSET 2
  19. #define CRT_HEADER_OFFSET 4
  20. static const char *TAG = "esp-x509-crt-bundle";
  21. /* a dummy certificate so that
  22. * cacert_ptr passes non-NULL check during handshake */
  23. static mbedtls_x509_crt s_dummy_crt;
  24. extern const uint8_t x509_crt_imported_bundle_bin_start[] asm("_binary_x509_crt_bundle_start");
  25. extern const uint8_t x509_crt_imported_bundle_bin_end[] asm("_binary_x509_crt_bundle_end");
  26. typedef struct crt_bundle_t {
  27. const uint8_t **crts;
  28. uint16_t num_certs;
  29. size_t x509_crt_bundle_len;
  30. } crt_bundle_t;
  31. static crt_bundle_t s_crt_bundle;
  32. static int esp_crt_check_signature(mbedtls_x509_crt *child, const uint8_t *pub_key_buf, size_t pub_key_len);
  33. static int esp_crt_check_signature(mbedtls_x509_crt *child, const uint8_t *pub_key_buf, size_t pub_key_len)
  34. {
  35. int ret = 0;
  36. mbedtls_x509_crt parent;
  37. const mbedtls_md_info_t *md_info;
  38. unsigned char hash[MBEDTLS_MD_MAX_SIZE];
  39. mbedtls_x509_crt_init(&parent);
  40. if ( (ret = mbedtls_pk_parse_public_key(&parent.pk, pub_key_buf, pub_key_len) ) != 0) {
  41. ESP_LOGE(TAG, "PK parse failed with error %X", ret);
  42. goto cleanup;
  43. }
  44. // Fast check to avoid expensive computations when not necessary
  45. if (!mbedtls_pk_can_do(&parent.pk, child->sig_pk)) {
  46. ESP_LOGE(TAG, "Simple compare failed");
  47. ret = -1;
  48. goto cleanup;
  49. }
  50. md_info = mbedtls_md_info_from_type(child->sig_md);
  51. if ( (ret = mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash )) != 0 ) {
  52. ESP_LOGE(TAG, "Internal mbedTLS error %X", ret);
  53. goto cleanup;
  54. }
  55. if ( (ret = mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent.pk,
  56. child->sig_md, hash, mbedtls_md_get_size( md_info ),
  57. child->sig.p, child->sig.len )) != 0 ) {
  58. ESP_LOGE(TAG, "PK verify failed with error %X", ret);
  59. goto cleanup;
  60. }
  61. cleanup:
  62. mbedtls_x509_crt_free(&parent);
  63. return ret;
  64. }
  65. /* This callback is called for every certificate in the chain. If the chain
  66. * is proper each intermediate certificate is validated through its parent
  67. * in the x509_crt_verify_chain() function. So this callback should
  68. * only verify the first untrusted link in the chain is signed by the
  69. * root certificate in the trusted bundle
  70. */
  71. int esp_crt_verify_callback(void *buf, mbedtls_x509_crt *crt, int depth, uint32_t *flags)
  72. {
  73. mbedtls_x509_crt *child = crt;
  74. /* It's OK for a trusted cert to have a weak signature hash alg.
  75. as we already trust this certificate */
  76. uint32_t flags_filtered = *flags & ~(MBEDTLS_X509_BADCERT_BAD_MD);
  77. if (flags_filtered != MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
  78. return 0;
  79. }
  80. if (s_crt_bundle.crts == NULL) {
  81. ESP_LOGE(TAG, "No certificates in bundle");
  82. return MBEDTLS_ERR_X509_FATAL_ERROR;
  83. }
  84. ESP_LOGD(TAG, "%d certificates in bundle", s_crt_bundle.num_certs);
  85. size_t name_len = 0;
  86. const uint8_t *crt_name;
  87. bool crt_found = false;
  88. int start = 0;
  89. int end = s_crt_bundle.num_certs - 1;
  90. int middle = (end - start) / 2;
  91. /* Look for the certificate using binary search on subject name */
  92. while (start <= end) {
  93. name_len = s_crt_bundle.crts[middle][0] << 8 | s_crt_bundle.crts[middle][1];
  94. crt_name = s_crt_bundle.crts[middle] + CRT_HEADER_OFFSET;
  95. int cmp_res = memcmp(child->issuer_raw.p, crt_name, name_len );
  96. if (cmp_res == 0) {
  97. crt_found = true;
  98. break;
  99. } else if (cmp_res < 0) {
  100. end = middle - 1;
  101. } else {
  102. start = middle + 1;
  103. }
  104. middle = (start + end) / 2;
  105. }
  106. int ret = MBEDTLS_ERR_X509_FATAL_ERROR;
  107. if (crt_found) {
  108. size_t key_len = s_crt_bundle.crts[middle][2] << 8 | s_crt_bundle.crts[middle][3];
  109. ret = esp_crt_check_signature(child, s_crt_bundle.crts[middle] + CRT_HEADER_OFFSET + name_len, key_len);
  110. }
  111. if (ret == 0) {
  112. ESP_LOGI(TAG, "Certificate validated");
  113. *flags = 0;
  114. return 0;
  115. }
  116. ESP_LOGE(TAG, "Failed to verify certificate");
  117. return MBEDTLS_ERR_X509_FATAL_ERROR;
  118. }
  119. /* Initialize the bundle into an array so we can do binary search for certs,
  120. the bundle generated by the python utility is already presorted by subject name
  121. */
  122. static esp_err_t esp_crt_bundle_init(const uint8_t *x509_bundle)
  123. {
  124. s_crt_bundle.num_certs = (x509_bundle[0] << 8) | x509_bundle[1];
  125. s_crt_bundle.crts = calloc(s_crt_bundle.num_certs, sizeof(x509_bundle));
  126. if (s_crt_bundle.crts == NULL) {
  127. ESP_LOGE(TAG, "Unable to allocate memory for bundle");
  128. return ESP_ERR_NO_MEM;
  129. }
  130. const uint8_t *cur_crt;
  131. cur_crt = x509_bundle + BUNDLE_HEADER_OFFSET;
  132. for (int i = 0; i < s_crt_bundle.num_certs; i++) {
  133. s_crt_bundle.crts[i] = cur_crt;
  134. size_t name_len = cur_crt[0] << 8 | cur_crt[1];
  135. size_t key_len = cur_crt[2] << 8 | cur_crt[3];
  136. cur_crt = cur_crt + CRT_HEADER_OFFSET + name_len + key_len;
  137. }
  138. return ESP_OK;
  139. }
  140. esp_err_t esp_crt_bundle_attach(void *conf)
  141. {
  142. esp_err_t ret = ESP_OK;
  143. // If no bundle has been set by the user then use the bundle embedded in the binary
  144. if (s_crt_bundle.crts == NULL) {
  145. ret = esp_crt_bundle_init(x509_crt_imported_bundle_bin_start);
  146. }
  147. if (ret != ESP_OK) {
  148. ESP_LOGE(TAG, "Failed to attach bundle");
  149. return ret;
  150. }
  151. if (conf) {
  152. /* point to a dummy certificate
  153. * This is only required so that the
  154. * cacert_ptr passes non-NULL check during handshake
  155. */
  156. mbedtls_ssl_config *ssl_conf = (mbedtls_ssl_config *)conf;
  157. mbedtls_x509_crt_init(&s_dummy_crt);
  158. mbedtls_ssl_conf_ca_chain(ssl_conf, &s_dummy_crt, NULL);
  159. mbedtls_ssl_conf_verify(ssl_conf, esp_crt_verify_callback, NULL);
  160. }
  161. return ret;
  162. }
  163. void esp_crt_bundle_detach(mbedtls_ssl_config *conf)
  164. {
  165. free(s_crt_bundle.crts);
  166. s_crt_bundle.crts = NULL;
  167. if (conf) {
  168. mbedtls_ssl_conf_verify(conf, NULL, NULL);
  169. }
  170. }
  171. void esp_crt_bundle_set(const uint8_t *x509_bundle)
  172. {
  173. // Free any previously used bundle
  174. free(s_crt_bundle.crts);
  175. esp_crt_bundle_init(x509_bundle);
  176. }