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