diff --git a/README.md b/README.md index 7ef43ed..7dd16df 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,8 @@ Mapped so far: get_ciphersuite_id - ssl_session: init, free; save and load return `MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE` +- timing: get_timer, set_delay, get_delay, get_final_delay, on a monotonic + clock. `MBEDTLS_TIMING_C` needs Windows or a Unix-like system. Known differences from Mbed TLS: @@ -230,7 +232,8 @@ make MBEDTLS_COMPAT=1 mbedtls-compat-test runs these against `libwolfpsa.a`. Each is also a target of its own. -- `mbedtls-compat-unit`: `test/mbedtls_compat/x509_crt_test.c`, +- `mbedtls-compat-unit`: `test/mbedtls_compat/timing_test.c` checks the + timers. `x509_crt_test.c`, `pk_test.c` and `pem_test.c` parse Mbed TLS's test certificates and keys. `ssl_test.c` checks return codes and verify results, client and server in one process. diff --git a/src/mbedtls/timing.c b/src/mbedtls/timing.c new file mode 100644 index 0000000..b0b577e --- /dev/null +++ b/src/mbedtls/timing.c @@ -0,0 +1,130 @@ +/* timing.c + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfPSA. + * + * wolfPSA is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfPSA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +/* Mbed TLS 4.x timers on a monotonic clock (MBEDTLS_COMPAT=1). */ + +/* clock_gettime() with -std=c99 */ +#if defined(WOLFPSA_MBEDTLS_COMPAT) && !defined(_POSIX_C_SOURCE) + #define _POSIX_C_SOURCE 200112L +#endif /* WOLFPSA_MBEDTLS_COMPAT && !_POSIX_C_SOURCE */ + +#ifdef HAVE_CONFIG_H + #include +#endif /* HAVE_CONFIG_H */ + +#include "psa_config.h" + +#if defined(WOLFSSL_PSA_ENGINE) && defined(WOLFPSA_MBEDTLS_COMPAT) + +#include +#include + +/* The generated config has it on Windows and Unix-like systems */ +#if defined(MBEDTLS_TIMING_C) != \ + (defined(USE_WINDOWS_API) || defined(__unix__) || defined(__APPLE__)) + #error "MBEDTLS_TIMING_C does not match the platform" +#endif /* MBEDTLS_TIMING_C != (USE_WINDOWS_API || __unix__ || __APPLE__) */ + +#if defined(MBEDTLS_TIMING_C) + +#ifdef USE_WINDOWS_API + #include +#else + #include +#endif /* USE_WINDOWS_API */ + +/* Milliseconds from a monotonic clock, else from time() as in Mbed TLS */ +static int64_t MbedTimingNow(void) +{ +#ifdef USE_WINDOWS_API + return (int64_t)GetTickCount64(); +#else + struct timespec ts; + + if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { + return (int64_t)time(NULL) * 1000; + } + return (int64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000; +#endif /* USE_WINDOWS_API */ +} + +unsigned long long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time* val, + int reset) +{ + int64_t now; + + WOLFSSL_ENTER("mbedtls_timing_get_timer"); + + if (val == NULL) { + return 0; + } + now = MbedTimingNow(); + if (reset) { + val->ms = now; + return 0; + } + return (unsigned long long)(now - val->ms); +} + +void mbedtls_timing_set_delay(void* data, uint32_t int_ms, uint32_t fin_ms) +{ + mbedtls_timing_delay_context* ctx = (mbedtls_timing_delay_context*)data; + + WOLFSSL_ENTER("mbedtls_timing_set_delay"); + + if (ctx != NULL) { + ctx->int_ms = int_ms; + ctx->fin_ms = fin_ms; + if (fin_ms != 0) { + (void)mbedtls_timing_get_timer(&ctx->timer, 1); + } + } +} + +/* Returns -1 when cancelled, else how many of the two delays have passed */ +int mbedtls_timing_get_delay(void* data) +{ + mbedtls_timing_delay_context* ctx = (mbedtls_timing_delay_context*)data; + unsigned long long elapsed; + + WOLFSSL_ENTER("mbedtls_timing_get_delay"); + + if (ctx == NULL || ctx->fin_ms == 0) { + return -1; + } + elapsed = mbedtls_timing_get_timer(&ctx->timer, 0); + if (elapsed >= ctx->fin_ms) { + return 2; + } + return (elapsed >= ctx->int_ms) ? 1 : 0; +} + +uint32_t mbedtls_timing_get_final_delay( + const mbedtls_timing_delay_context* data) +{ + WOLFSSL_ENTER("mbedtls_timing_get_final_delay"); + + return (data == NULL) ? 0 : data->fin_ms; +} + +#endif /* MBEDTLS_TIMING_C */ + +#endif /* WOLFSSL_PSA_ENGINE && WOLFPSA_MBEDTLS_COMPAT */ diff --git a/test/mbedtls_compat/Makefile b/test/mbedtls_compat/Makefile index 124f638..6ea9cd5 100644 --- a/test/mbedtls_compat/Makefile +++ b/test/mbedtls_compat/Makefile @@ -25,9 +25,9 @@ endif # Mbed TLS's test data DATA := $(MBEDTLS_PATH)/framework/data_files -# Certificate, key and PEM parsing, and TLS return codes and verify results -# with a client and a server in one process -UNIT := $(addprefix $(BUILD_DIR)/,x509_crt_test pk_test pem_test ssl_test) +# Parsing, TLS return codes and verify results in one process, and timers +UNIT := $(addprefix $(BUILD_DIR)/,x509_crt_test pk_test pem_test ssl_test \ + timing_test) # The peers are the wolfSSL example server and client, from a wolfSSL # configured out of tree from WOLFSSL_PATH, so they do not depend on wolfPSA's @@ -49,6 +49,7 @@ PROGRAMS := $(addprefix $(BUILD_DIR)/programs/ssl/,mini_client ssl_server \ ifeq ($(MBEDTLS_THREADING_C),1) PROGRAMS += $(BUILD_DIR)/programs/ssl/ssl_pthread_server endif +PROGRAMS += $(BUILD_DIR)/programs/x509/load_roots # Mbed TLS test certificates, data only. certs.c and the generated # test_certs.h build against the Mbed TLS checkout's own headers. CERTS := $(BUILD_DIR)/certs.o diff --git a/test/mbedtls_compat/programs.sh b/test/mbedtls_compat/programs.sh index dbeb9cc..3fe9fbf 100755 --- a/test/mbedtls_compat/programs.sh +++ b/test/mbedtls_compat/programs.sh @@ -47,6 +47,8 @@ TLS13="-p 11111 -d -v 4" TLS12="-p 11111 -d -v 3" GOT="tls_get() returned" P=programs/ssl +X=programs/x509 +ROOTS="$DATA/test-ca.crt $DATA/test-ca2.crt $DATA/test-ca-sha256.crt" # mini_client's built-in CA, test-ca2 from 2013, encodes its name as only the # older server5 certs do, and wolfSSL compares encoded names SERVER5="-p 4433 -d -c $DATA/server5-expired.crt -k $DATA/server5.key" @@ -66,7 +68,8 @@ while IFS='|' read -r name prog args sopts wrap expect want; do SKIPS=$((SKIPS + 1)) continue fi - start_server "$sopts" + srv= + [ -z "$sopts" ] || start_server "$sopts" out=$(cd "$WOLFSSL_PATH" && $wrap $WRAP "$BUILD_DIR/$prog" $args 2>&1) rc=$? if [ $rc -eq "$expect" ] && ! echo "$out" | grep -q "not defined" && @@ -76,8 +79,10 @@ while IFS='|' read -r name prog args sopts wrap expect want; do echo "FAIL: $name: exit $rc: $out" FAILS=$((FAILS + 1)) fi - kill $srv 2>/dev/null - wait $srv 2>/dev/null + if [ -n "$srv" ]; then + kill $srv 2>/dev/null + wait $srv 2>/dev/null + fi done < mini_client|$P/mini_client||$SERVER5|$CA2_DATE|0| mini_client, expired CA, ssl_handshake_failed|$P/mini_client||$SERVER5||7| CASES diff --git a/test/mbedtls_compat/timing_test.c b/test/mbedtls_compat/timing_test.c new file mode 100644 index 0000000..1c36a21 --- /dev/null +++ b/test/mbedtls_compat/timing_test.c @@ -0,0 +1,143 @@ +/* timing_test.c + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfPSA. + * + * wolfPSA is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfPSA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +/* Mbed TLS 4.x timers. The first two cases are from test_suite_timing. */ + +#include +#include +#include + +#include + +#define INT_MS 100 +#define FIN_MS 200 +/* Upper bound for a wait, in case a delay never passes */ +#define MAX_WAIT_MS 5000 + +/* Reset writes the time, which is not 0 */ +static int GetTimer(void) +{ + struct mbedtls_timing_hr_time t; + static const struct mbedtls_timing_hr_time zero; + + memset(&t, 0, sizeof(t)); + return mbedtls_timing_get_timer(&t, 1) == 0 && + memcmp(&t, &zero, sizeof(t)) != 0 && + mbedtls_timing_get_timer(&t, 0) < MAX_WAIT_MS; +} + +static int DelayZero(void) +{ + mbedtls_timing_delay_context ctx; + + mbedtls_timing_set_delay(&ctx, 0, 0); + return mbedtls_timing_get_delay(&ctx) == -1 && + mbedtls_timing_get_final_delay(&ctx) == 0; +} + +/* In ms: time() moves on once or twice in 1100 ms */ +static int FollowsTime(void) +{ + struct mbedtls_timing_hr_time t; + unsigned long long ms; + time_t start = time(NULL); + time_t now; + + (void)mbedtls_timing_get_timer(&t, 1); + do { + ms = mbedtls_timing_get_timer(&t, 0); + now = time(NULL); + } while (ms < 1100 && now - start < 3); + return ms >= 1100 && now - start >= 1 && now - start <= 2; +} + +/* 0, then 1 after INT_MS, then 2 after FIN_MS, by t started just before */ +static int Delays(void) +{ + mbedtls_timing_delay_context ctx; + struct mbedtls_timing_hr_time t; + unsigned long long elapsed; + int last; + int now; + int ok; + + (void)mbedtls_timing_get_timer(&t, 1); + mbedtls_timing_set_delay(&ctx, INT_MS, FIN_MS); + last = mbedtls_timing_get_delay(&ctx); + ok = last == 0 && mbedtls_timing_get_final_delay(&ctx) == FIN_MS; + while (ok && last != 2) { + now = mbedtls_timing_get_delay(&ctx); + elapsed = mbedtls_timing_get_timer(&t, 0); + ok = now >= last && elapsed < MAX_WAIT_MS && + (now < 1 || elapsed >= INT_MS) && (now < 2 || elapsed >= FIN_MS); + last = now; + } + return ok; +} + +/* A delay of 0 cancels the one running */ +static int Cancel(void) +{ + mbedtls_timing_delay_context ctx; + + mbedtls_timing_set_delay(&ctx, INT_MS, FIN_MS); + mbedtls_timing_set_delay(&ctx, INT_MS, 0); + return mbedtls_timing_get_delay(&ctx) == -1 && + mbedtls_timing_get_final_delay(&ctx) == 0; +} + +static int NullArgs(void) +{ + mbedtls_timing_set_delay(NULL, INT_MS, FIN_MS); + return mbedtls_timing_get_timer(NULL, 1) == 0 && + mbedtls_timing_get_timer(NULL, 0) == 0 && + mbedtls_timing_get_delay(NULL) == -1 && + mbedtls_timing_get_final_delay(NULL) == 0; +} + +static const struct { + const char* name; + int (*test)(void); +} cases[] = { + { "get timer", GetTimer }, + { "delay 0ms", DelayZero }, + { "milliseconds", FollowsTime }, + { "delays", Delays }, + { "cancel", Cancel }, + { "NULL arguments", NullArgs } +}; + +int main(void) +{ + size_t i; + int fails = 0; + + for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { + if (cases[i].test()) { + printf("PASS: %s\n", cases[i].name); + } + else { + printf("FAIL: %s\n", cases[i].name); + fails++; + } + } + return (fails == 0) ? 0 : 1; +} diff --git a/tools/psa_want_probe.c b/tools/psa_want_probe.c index a1abfbe..0ef4ab5 100644 --- a/tools/psa_want_probe.c +++ b/tools/psa_want_probe.c @@ -296,3 +296,8 @@ __WPSA_EMIT__MBEDTLS_SSL_PROTO_TLS1_3 #endif #endif #endif +/* A monotonic clock, on Windows and Unix-like systems */ +#if defined(WOLFPSA_MBEDTLS_COMPAT) && (defined(USE_WINDOWS_API) || \ + defined(__unix__) || defined(__APPLE__)) +__WPSA_EMIT__MBEDTLS_TIMING_C +#endif /* WOLFPSA_MBEDTLS_COMPAT && a monotonic clock */ diff --git a/wolfpsa.map b/wolfpsa.map index 1922648..cda6dd2 100644 --- a/wolfpsa.map +++ b/wolfpsa.map @@ -143,6 +143,7 @@ WOLFPSA_1.0 { mbedtls_net_*; mbedtls_pem_*; mbedtls_ssl_*; + mbedtls_timing_*; mbedtls_x509_*; /* The wolfCrypt objects a devId selects live inside this library, * so a shared-library user has to register the device against this diff --git a/wolfpsa/mbedtls_compat/mbedtls/timing.h b/wolfpsa/mbedtls_compat/mbedtls/timing.h index 5c21245..16492dd 100644 --- a/wolfpsa/mbedtls_compat/mbedtls/timing.h +++ b/wolfpsa/mbedtls_compat/mbedtls/timing.h @@ -19,11 +19,36 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA */ -/* No mbedtls_timing_* yet; DTLS needs them. */ - #ifndef MBEDTLS_TIMING_H #define MBEDTLS_TIMING_H #include "mbedtls/build_info.h" +#include + +#ifdef __cplusplus + extern "C" { +#endif /* __cplusplus */ + +struct mbedtls_timing_hr_time { + int64_t ms; +}; + +typedef struct mbedtls_timing_delay_context { + struct mbedtls_timing_hr_time timer; + uint32_t int_ms; + uint32_t fin_ms; +} mbedtls_timing_delay_context; + +unsigned long long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time* val, + int reset); +void mbedtls_timing_set_delay(void* data, uint32_t int_ms, uint32_t fin_ms); +int mbedtls_timing_get_delay(void* data); +uint32_t mbedtls_timing_get_final_delay( + const mbedtls_timing_delay_context* data); + +#ifdef __cplusplus + } /* extern "C" */ +#endif /* __cplusplus */ + #endif /* MBEDTLS_TIMING_H */