gmtime_r_alt.c 源码
2026/7/20大约 1 分钟附录源码附录MBED TLS时间
gmtime_r_alt.c — MBED TLS 时间函数适配
路径: components/security/mbedtls/port/src/gmtime_r_alt.c
功能: 实现 MBED TLS 的时间转换函数适配,将 mbedtls_platform_gmtime_r 映射到 POSIX 标准的 gmtime_r。用于 X.509 证书有效期验证过程中将时间戳转换为 UTC 时间结构。
引用章节: 7.3.1 MBED TLS 加密组件
完整源码
/**
***********************************************************************************************************************
* Copyright (c) 2020, China Mobile Communications Group Co.,Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @file gmtime_rl_alt.c
*
* @brief os related alt time functions
*
* @revision
* Date Author Notes
* 2020-11-25 OneOS Team First Version
***********************************************************************************************************************
*/
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#ifdef MBEDTLS_PLATFORM_GMTIME_R_ALT
#include <sys/time.h>
#include "mbedtls/platform_time.h"
struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt,
struct tm *tm_buf )
{
return gmtime_r(tt, tm_buf);
}
#endif关键说明
| 模块 | 说明 |
|---|---|
| gmtime_r | 线程安全版本的 gmtime,将 time_t 时间戳转换为 UTC 时间的 struct tm 结构 |
| 使用场景 | X.509 证书验证时,需要检查证书的 notBefore 和 notAfter 有效期,必须将时间戳转换为可读的日期时间 |
| 平台依赖 | 依赖 POSIX sys/time.h,需要 OneOS 提供 gmtime_r 实现或启用 MBEDTLS_HAVE_TIME_DATE |
| 适配宏 | 通过 MBEDTLS_PLATFORM_GMTIME_R_ALT 启用,让 MBED TLS 使用平台提供的 gmtime_r 替代内置实现 |
| 线程安全 | 使用 _r 后缀的可重入版本,调用者提供 tm_buf 缓冲区,避免静态变量导致的线程安全问题 |