diff --git a/core/sysdep/core_adapter.c b/core/sysdep/core_adapter.c index b1fbc6d..e826d7a 100644 --- a/core/sysdep/core_adapter.c +++ b/core/sysdep/core_adapter.c @@ -165,6 +165,18 @@ static void _core_mbedtls_debug(void *ctx, int32_t level, const char *file, int3 } +static const mbedtls_x509_crt *g_verified_root_ca = NULL; +static int g_verified_max_depth = -1; + +static int _core_mbedtls_verify_cb(void *p_vrfy, mbedtls_x509_crt *crt, int depth, uint32_t *flags) +{ + if (depth > g_verified_max_depth) { + g_verified_max_depth = depth; + g_verified_root_ca = crt; + } + return 0; +} + static int32_t _core_mbedtls_net_send(void *ctx, const uint8_t *buf, size_t len) { int32_t ret = g_origin_portfile->core_sysdep_network_send(ctx, (uint8_t *)buf, len, 5000, NULL); @@ -358,6 +370,10 @@ int32_t _tls_network_establish(void *handle) return STATE_PORT_TLS_INVALID_CRED_OPTION; } + g_verified_root_ca = NULL; + g_verified_max_depth = -1; + mbedtls_ssl_conf_verify(&adapter_handle->mbedtls.ssl_config, _core_mbedtls_verify_cb, NULL); + res = mbedtls_ssl_setup(&adapter_handle->mbedtls.ssl_ctx, &adapter_handle->mbedtls.ssl_config); if (res < 0) { core_log1(g_origin_portfile, STATE_ADAPTER_COMMON, "mbedtls_ssl_setup error, res: %x\r\n", &res); @@ -394,6 +410,23 @@ int32_t _tls_network_establish(void *handle) return res; } + { + if (g_verified_root_ca != NULL) { + char dn_buf[256]; + char cert_buf[384]; + int dn_len = mbedtls_x509_dn_gets(dn_buf, sizeof(dn_buf), &g_verified_root_ca->subject); + if (dn_len < 0) { + dn_buf[0] = '\0'; + } + snprintf(cert_buf, sizeof(cert_buf), + "Verified root CA: %s, not_after: %04d-%02d-%02d %02d:%02d:%02d\r\n", + dn_buf, + g_verified_root_ca->valid_to.year, g_verified_root_ca->valid_to.mon, g_verified_root_ca->valid_to.day, + g_verified_root_ca->valid_to.hour, g_verified_root_ca->valid_to.min, g_verified_root_ca->valid_to.sec); + core_log(g_origin_portfile, STATE_ADAPTER_COMMON, cert_buf); + } + } + core_log2(g_origin_portfile, STATE_ADAPTER_COMMON, "success to establish mbedtls connection, (cost %d bytes in total, max used %d bytes)\r\n", &g_mbedtls_total_mem_used, &g_mbedtls_max_mem_used);