| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 1 | /* | 
|  | 2 | *  DTLS cookie callbacks implementation | 
|  | 3 | * | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | *  Copyright The Mbed TLS Contributors | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 6 | * | 
|  | 7 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 8 | *  not use this file except in compliance with the License. | 
|  | 9 | *  You may obtain a copy of the License at | 
|  | 10 | * | 
|  | 11 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 12 | * | 
|  | 13 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 14 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 15 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 16 | *  See the License for the specific language governing permissions and | 
|  | 17 | *  limitations under the License. | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 18 | */ | 
|  | 19 | /* | 
|  | 20 | * These session callbacks use a simple chained list | 
|  | 21 | * to store and retrieve the session information. | 
|  | 22 | */ | 
|  | 23 |  | 
| Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 24 | #include "common.h" | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 25 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if defined(MBEDTLS_SSL_COOKIE_C) | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 27 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 28 | #include "mbedtls/platform.h" | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 29 |  | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 30 | #include "mbedtls/ssl_cookie.h" | 
| Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 31 | #include "ssl_misc.h" | 
| Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 32 | #include "mbedtls/error.h" | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 33 | #include "mbedtls/platform_util.h" | 
| Gabor Mezei | 765862c | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 34 | #include "mbedtls/constant_time.h" | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 35 |  | 
| Manuel Pégourié-Gonnard | 07018f9 | 2022-09-15 11:29:35 +0200 | [diff] [blame] | 36 | #include "mbedtls/legacy_or_psa.h" | 
| Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 37 |  | 
| Manuel Pégourié-Gonnard | d901d17 | 2015-02-16 18:37:53 +0000 | [diff] [blame] | 38 | #include <string.h> | 
|  | 39 |  | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 40 | /* | 
| Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 41 | * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-384 is | 
|  | 42 | * available. Try SHA-256 first, 384 wastes resources | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 43 | */ | 
| Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 44 | #if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_LOWLEVEL_OR_PSA) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 45 | #define COOKIE_MD           MBEDTLS_MD_SHA224 | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 46 | #define COOKIE_MD_OUTLEN    32 | 
|  | 47 | #define COOKIE_HMAC_LEN     28 | 
| Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 48 | #elif defined(MBEDTLS_HAS_ALG_SHA_384_VIA_LOWLEVEL_OR_PSA) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | #define COOKIE_MD           MBEDTLS_MD_SHA384 | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 50 | #define COOKIE_MD_OUTLEN    48 | 
|  | 51 | #define COOKIE_HMAC_LEN     28 | 
| Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 52 | #elif defined(MBEDTLS_HAS_ALG_SHA_1_VIA_LOWLEVEL_OR_PSA) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | #define COOKIE_MD           MBEDTLS_MD_SHA1 | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 54 | #define COOKIE_MD_OUTLEN    20 | 
|  | 55 | #define COOKIE_HMAC_LEN     20 | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 56 | #else | 
|  | 57 | #error "DTLS hello verify needs SHA-1 or SHA-2" | 
|  | 58 | #endif | 
|  | 59 |  | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 60 | /* | 
|  | 61 | * Cookies are formed of a 4-bytes timestamp (or serial number) and | 
| Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 62 | * an HMAC of timestamp and client ID. | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 63 | */ | 
|  | 64 | #define COOKIE_LEN      ( 4 + COOKIE_HMAC_LEN ) | 
|  | 65 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 66 | void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx ) | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 67 | { | 
| Neil Armstrong | bca99ee | 2022-03-04 10:20:20 +0100 | [diff] [blame] | 68 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Neil Armstrong | 488a40e | 2022-03-22 10:41:38 +0100 | [diff] [blame] | 69 | ctx->psa_hmac_key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Neil Armstrong | 77b69ab | 2022-03-04 14:35:13 +0100 | [diff] [blame] | 70 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 71 | mbedtls_md_init( &ctx->hmac_ctx ); | 
| Neil Armstrong | 77b69ab | 2022-03-04 14:35:13 +0100 | [diff] [blame] | 72 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 73 | #if !defined(MBEDTLS_HAVE_TIME) | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 74 | ctx->serial = 0; | 
|  | 75 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 76 | ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT; | 
| Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 77 |  | 
| Neil Armstrong | 7cd0270 | 2022-03-04 15:08:43 +0100 | [diff] [blame] | 78 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 79 | #if defined(MBEDTLS_THREADING_C) | 
|  | 80 | mbedtls_mutex_init( &ctx->mutex ); | 
|  | 81 | #endif | 
| Neil Armstrong | 7cd0270 | 2022-03-04 15:08:43 +0100 | [diff] [blame] | 82 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ | 
| Manuel Pégourié-Gonnard | bef8f09 | 2014-07-23 23:40:29 +0200 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 85 | void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay ) | 
| Manuel Pégourié-Gonnard | bef8f09 | 2014-07-23 23:40:29 +0200 | [diff] [blame] | 86 | { | 
|  | 87 | ctx->timeout = delay; | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 88 | } | 
|  | 89 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 90 | void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx ) | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 91 | { | 
| Neil Armstrong | bca99ee | 2022-03-04 10:20:20 +0100 | [diff] [blame] | 92 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Neil Armstrong | 488a40e | 2022-03-22 10:41:38 +0100 | [diff] [blame] | 93 | psa_destroy_key( ctx->psa_hmac_key ); | 
| Neil Armstrong | 77b69ab | 2022-03-04 14:35:13 +0100 | [diff] [blame] | 94 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 95 | mbedtls_md_free( &ctx->hmac_ctx ); | 
| Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 96 |  | 
|  | 97 | #if defined(MBEDTLS_THREADING_C) | 
| Ron Eldor | 04965ed | 2017-01-29 18:51:35 +0200 | [diff] [blame] | 98 | mbedtls_mutex_free( &ctx->mutex ); | 
| Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 99 | #endif | 
| Neil Armstrong | 7cd0270 | 2022-03-04 15:08:43 +0100 | [diff] [blame] | 100 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 101 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 102 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) ); | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 103 | } | 
|  | 104 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 105 | int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx, | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 106 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 107 | void *p_rng ) | 
|  | 108 | { | 
| Neil Armstrong | d633201 | 2022-03-04 10:26:16 +0100 | [diff] [blame] | 109 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 110 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 111 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 112 | psa_algorithm_t alg; | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 113 |  | 
| Neil Armstrong | 2217d6f | 2022-03-04 15:00:22 +0100 | [diff] [blame] | 114 | (void)f_rng; | 
|  | 115 | (void)p_rng; | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 116 |  | 
| Manuel Pégourié-Gonnard | abac037 | 2022-07-18 13:41:11 +0200 | [diff] [blame] | 117 | alg = mbedtls_hash_info_psa_from_md( COOKIE_MD ); | 
| Neil Armstrong | d633201 | 2022-03-04 10:26:16 +0100 | [diff] [blame] | 118 | if( alg == 0 ) | 
|  | 119 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 120 |  | 
| Neil Armstrong | be52f50 | 2022-03-07 14:17:26 +0100 | [diff] [blame] | 121 | ctx->psa_hmac_alg = PSA_ALG_TRUNCATED_MAC( PSA_ALG_HMAC( alg ), | 
|  | 122 | COOKIE_HMAC_LEN ); | 
| Neil Armstrong | d633201 | 2022-03-04 10:26:16 +0100 | [diff] [blame] | 123 |  | 
| Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 124 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE | | 
|  | 125 | PSA_KEY_USAGE_SIGN_MESSAGE ); | 
| Neil Armstrong | be52f50 | 2022-03-07 14:17:26 +0100 | [diff] [blame] | 126 | psa_set_key_algorithm( &attributes, ctx->psa_hmac_alg ); | 
| Neil Armstrong | d633201 | 2022-03-04 10:26:16 +0100 | [diff] [blame] | 127 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); | 
| Neil Armstrong | 2217d6f | 2022-03-04 15:00:22 +0100 | [diff] [blame] | 128 | psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( COOKIE_MD_OUTLEN ) ); | 
| Neil Armstrong | d633201 | 2022-03-04 10:26:16 +0100 | [diff] [blame] | 129 |  | 
| Neil Armstrong | 2217d6f | 2022-03-04 15:00:22 +0100 | [diff] [blame] | 130 | if( ( status = psa_generate_key( &attributes, | 
| Neil Armstrong | 488a40e | 2022-03-22 10:41:38 +0100 | [diff] [blame] | 131 | &ctx->psa_hmac_key ) ) != PSA_SUCCESS ) | 
| Neil Armstrong | d633201 | 2022-03-04 10:26:16 +0100 | [diff] [blame] | 132 | { | 
|  | 133 | return psa_ssl_status_to_mbedtls( status ); | 
|  | 134 | } | 
| Neil Armstrong | 23d34ce | 2022-03-04 10:32:26 +0100 | [diff] [blame] | 135 | #else | 
| Neil Armstrong | 2217d6f | 2022-03-04 15:00:22 +0100 | [diff] [blame] | 136 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 137 | unsigned char key[COOKIE_MD_OUTLEN]; | 
|  | 138 |  | 
|  | 139 | if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 ) | 
|  | 140 | return( ret ); | 
|  | 141 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 142 | ret = mbedtls_md_setup( &ctx->hmac_ctx, mbedtls_md_info_from_type( COOKIE_MD ), 1 ); | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 143 | if( ret != 0 ) | 
|  | 144 | return( ret ); | 
|  | 145 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 146 | ret = mbedtls_md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) ); | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 147 | if( ret != 0 ) | 
|  | 148 | return( ret ); | 
|  | 149 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 150 | mbedtls_platform_zeroize( key, sizeof( key ) ); | 
| Neil Armstrong | 2217d6f | 2022-03-04 15:00:22 +0100 | [diff] [blame] | 151 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 152 |  | 
|  | 153 | return( 0 ); | 
|  | 154 | } | 
|  | 155 |  | 
| Neil Armstrong | 2d5e343 | 2022-03-21 11:39:52 +0100 | [diff] [blame] | 156 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 157 | /* | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 158 | * Generate the HMAC part of a cookie | 
|  | 159 | */ | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 160 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx, | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 162 | const unsigned char time[4], | 
|  | 163 | unsigned char **p, unsigned char *end, | 
|  | 164 | const unsigned char *cli_id, size_t cli_id_len ) | 
|  | 165 | { | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 166 | unsigned char hmac_out[COOKIE_MD_OUTLEN]; | 
|  | 167 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 168 | MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_HMAC_LEN ); | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 169 |  | 
| Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 170 | if( mbedtls_md_hmac_reset(  hmac_ctx ) != 0 || | 
|  | 171 | mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 || | 
|  | 172 | mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 || | 
|  | 173 | mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 ) | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 174 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 175 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 176 | } | 
|  | 177 |  | 
|  | 178 | memcpy( *p, hmac_out, COOKIE_HMAC_LEN ); | 
|  | 179 | *p += COOKIE_HMAC_LEN; | 
|  | 180 |  | 
|  | 181 | return( 0 ); | 
|  | 182 | } | 
| Neil Armstrong | 2d5e343 | 2022-03-21 11:39:52 +0100 | [diff] [blame] | 183 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 184 |  | 
|  | 185 | /* | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 186 | * Generate cookie for DTLS ClientHello verification | 
|  | 187 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | int mbedtls_ssl_cookie_write( void *p_ctx, | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 189 | unsigned char **p, unsigned char *end, | 
|  | 190 | const unsigned char *cli_id, size_t cli_id_len ) | 
|  | 191 | { | 
| Neil Armstrong | 2d5e343 | 2022-03-21 11:39:52 +0100 | [diff] [blame] | 192 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 193 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; | 
| Neil Armstrong | 79daea2 | 2022-03-21 12:05:51 +0100 | [diff] [blame] | 194 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Neil Armstrong | 2d5e343 | 2022-03-21 11:39:52 +0100 | [diff] [blame] | 195 | size_t sign_mac_length = 0; | 
|  | 196 | #endif | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 197 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 198 | mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx; | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 199 | unsigned long t; | 
| Manuel Pégourié-Gonnard | e4de061 | 2014-07-23 17:26:48 +0200 | [diff] [blame] | 200 |  | 
| Manuel Pégourié-Gonnard | 29ad7e8 | 2014-07-23 19:12:15 +0200 | [diff] [blame] | 201 | if( ctx == NULL || cli_id == NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 202 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 203 |  | 
| Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 204 | MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_LEN ); | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 205 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | #if defined(MBEDTLS_HAVE_TIME) | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 207 | t = (unsigned long) mbedtls_time( NULL ); | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 208 | #else | 
|  | 209 | t = ctx->serial++; | 
|  | 210 | #endif | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 211 |  | 
| Joe Subbiani | b6511b0 | 2021-07-16 15:02:55 +0100 | [diff] [blame] | 212 | MBEDTLS_PUT_UINT32_BE(t, *p, 0); | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 213 | *p += 4; | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 214 |  | 
| Neil Armstrong | 7cd0270 | 2022-03-04 15:08:43 +0100 | [diff] [blame] | 215 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Neil Armstrong | 488a40e | 2022-03-22 10:41:38 +0100 | [diff] [blame] | 216 | status = psa_mac_sign_setup( &operation, ctx->psa_hmac_key, | 
| Neil Armstrong | 79daea2 | 2022-03-21 12:05:51 +0100 | [diff] [blame] | 217 | ctx->psa_hmac_alg ); | 
|  | 218 | if( status != PSA_SUCCESS ) | 
|  | 219 | { | 
|  | 220 | ret = psa_ssl_status_to_mbedtls( status ); | 
| Neil Armstrong | 2d5e343 | 2022-03-21 11:39:52 +0100 | [diff] [blame] | 221 | goto exit; | 
|  | 222 | } | 
|  | 223 |  | 
| Neil Armstrong | 79daea2 | 2022-03-21 12:05:51 +0100 | [diff] [blame] | 224 | status = psa_mac_update( &operation, *p - 4, 4 ); | 
|  | 225 | if( status != PSA_SUCCESS ) | 
|  | 226 | { | 
|  | 227 | ret = psa_ssl_status_to_mbedtls( status ); | 
| Neil Armstrong | 2d5e343 | 2022-03-21 11:39:52 +0100 | [diff] [blame] | 228 | goto exit; | 
|  | 229 | } | 
|  | 230 |  | 
| Neil Armstrong | 79daea2 | 2022-03-21 12:05:51 +0100 | [diff] [blame] | 231 | status = psa_mac_update( &operation, cli_id, cli_id_len ); | 
|  | 232 | if( status != PSA_SUCCESS ) | 
|  | 233 | { | 
|  | 234 | ret = psa_ssl_status_to_mbedtls( status ); | 
| Neil Armstrong | 2d5e343 | 2022-03-21 11:39:52 +0100 | [diff] [blame] | 235 | goto exit; | 
|  | 236 | } | 
|  | 237 |  | 
| Neil Armstrong | 79daea2 | 2022-03-21 12:05:51 +0100 | [diff] [blame] | 238 | status = psa_mac_sign_finish( &operation, *p, COOKIE_MD_OUTLEN, | 
|  | 239 | &sign_mac_length ); | 
|  | 240 | if( status != PSA_SUCCESS ) | 
|  | 241 | { | 
|  | 242 | ret = psa_ssl_status_to_mbedtls( status ); | 
| Neil Armstrong | 2d5e343 | 2022-03-21 11:39:52 +0100 | [diff] [blame] | 243 | goto exit; | 
|  | 244 | } | 
|  | 245 |  | 
|  | 246 | *p += COOKIE_HMAC_LEN; | 
|  | 247 |  | 
|  | 248 | ret = 0; | 
| Neil Armstrong | 7cd0270 | 2022-03-04 15:08:43 +0100 | [diff] [blame] | 249 | #else | 
| Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 250 | #if defined(MBEDTLS_THREADING_C) | 
|  | 251 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) | 
| Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 252 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret ) ); | 
| Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 253 | #endif | 
|  | 254 |  | 
|  | 255 | ret = ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4, | 
|  | 256 | p, end, cli_id, cli_id_len ); | 
|  | 257 |  | 
|  | 258 | #if defined(MBEDTLS_THREADING_C) | 
|  | 259 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) | 
| Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 260 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, | 
|  | 261 | MBEDTLS_ERR_THREADING_MUTEX_ERROR ) ); | 
| Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 262 | #endif | 
| Neil Armstrong | 7cd0270 | 2022-03-04 15:08:43 +0100 | [diff] [blame] | 263 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 264 |  | 
| Neil Armstrong | 2d5e343 | 2022-03-21 11:39:52 +0100 | [diff] [blame] | 265 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 266 | exit: | 
| Neil Armstrong | 79daea2 | 2022-03-21 12:05:51 +0100 | [diff] [blame] | 267 | status = psa_mac_abort( &operation ); | 
|  | 268 | if( status != PSA_SUCCESS ) | 
|  | 269 | ret = psa_ssl_status_to_mbedtls( status ); | 
| Neil Armstrong | 2d5e343 | 2022-03-21 11:39:52 +0100 | [diff] [blame] | 270 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 271 | return( ret ); | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 272 | } | 
|  | 273 |  | 
|  | 274 | /* | 
|  | 275 | * Check a cookie | 
|  | 276 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 277 | int mbedtls_ssl_cookie_check( void *p_ctx, | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 278 | const unsigned char *cookie, size_t cookie_len, | 
|  | 279 | const unsigned char *cli_id, size_t cli_id_len ) | 
|  | 280 | { | 
| Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 281 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 282 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; | 
| Neil Armstrong | 79daea2 | 2022-03-21 12:05:51 +0100 | [diff] [blame] | 283 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 284 | #else | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 285 | unsigned char ref_hmac[COOKIE_HMAC_LEN]; | 
|  | 286 | unsigned char *p = ref_hmac; | 
| Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 287 | #endif | 
|  | 288 | int ret = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 289 | mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx; | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 290 | unsigned long cur_time, cookie_time; | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 291 |  | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 292 | if( ctx == NULL || cli_id == NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 293 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 294 |  | 
|  | 295 | if( cookie_len != COOKIE_LEN ) | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 296 | return( -1 ); | 
|  | 297 |  | 
| Neil Armstrong | 7cd0270 | 2022-03-04 15:08:43 +0100 | [diff] [blame] | 298 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Neil Armstrong | 488a40e | 2022-03-22 10:41:38 +0100 | [diff] [blame] | 299 | status = psa_mac_verify_setup( &operation, ctx->psa_hmac_key, | 
| Neil Armstrong | 79daea2 | 2022-03-21 12:05:51 +0100 | [diff] [blame] | 300 | ctx->psa_hmac_alg ); | 
|  | 301 | if( status != PSA_SUCCESS ) | 
|  | 302 | { | 
|  | 303 | ret = psa_ssl_status_to_mbedtls( status ); | 
| Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 304 | goto exit; | 
|  | 305 | } | 
| Neil Armstrong | 7cd0270 | 2022-03-04 15:08:43 +0100 | [diff] [blame] | 306 |  | 
| Neil Armstrong | 79daea2 | 2022-03-21 12:05:51 +0100 | [diff] [blame] | 307 | status = psa_mac_update( &operation, cookie, 4 ); | 
|  | 308 | if( status != PSA_SUCCESS ) | 
|  | 309 | { | 
|  | 310 | ret = psa_ssl_status_to_mbedtls( status ); | 
| Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 311 | goto exit; | 
|  | 312 | } | 
|  | 313 |  | 
| Neil Armstrong | 79daea2 | 2022-03-21 12:05:51 +0100 | [diff] [blame] | 314 | status = psa_mac_update( &operation, cli_id, | 
|  | 315 | cli_id_len ); | 
|  | 316 | if( status != PSA_SUCCESS ) | 
|  | 317 | { | 
|  | 318 | ret = psa_ssl_status_to_mbedtls( status ); | 
| Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 319 | goto exit; | 
|  | 320 | } | 
|  | 321 |  | 
| Neil Armstrong | 79daea2 | 2022-03-21 12:05:51 +0100 | [diff] [blame] | 322 | status = psa_mac_verify_finish( &operation, cookie + 4, | 
|  | 323 | COOKIE_HMAC_LEN ); | 
|  | 324 | if( status != PSA_SUCCESS ) | 
|  | 325 | { | 
|  | 326 | ret = psa_ssl_status_to_mbedtls( status ); | 
| Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 327 | goto exit; | 
|  | 328 | } | 
| Neil Armstrong | 79daea2 | 2022-03-21 12:05:51 +0100 | [diff] [blame] | 329 |  | 
|  | 330 | ret = 0; | 
| Neil Armstrong | 7cd0270 | 2022-03-04 15:08:43 +0100 | [diff] [blame] | 331 | #else | 
| Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 332 | #if defined(MBEDTLS_THREADING_C) | 
|  | 333 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) | 
| Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 334 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret ) ); | 
| Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 335 | #endif | 
|  | 336 |  | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 337 | if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie, | 
|  | 338 | &p, p + sizeof( ref_hmac ), | 
|  | 339 | cli_id, cli_id_len ) != 0 ) | 
| Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 340 | ret = -1; | 
|  | 341 |  | 
|  | 342 | #if defined(MBEDTLS_THREADING_C) | 
|  | 343 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) | 
| Gilles Peskine | c2f7b75 | 2021-12-13 12:35:08 +0100 | [diff] [blame] | 344 | { | 
|  | 345 | ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, | 
|  | 346 | MBEDTLS_ERR_THREADING_MUTEX_ERROR ); | 
|  | 347 | } | 
| Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 348 | #endif | 
|  | 349 |  | 
|  | 350 | if( ret != 0 ) | 
| Gilles Peskine | c2f7b75 | 2021-12-13 12:35:08 +0100 | [diff] [blame] | 351 | goto exit; | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 352 |  | 
| Gabor Mezei | 90437e3 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 353 | if( mbedtls_ct_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 ) | 
| Gilles Peskine | c2f7b75 | 2021-12-13 12:35:08 +0100 | [diff] [blame] | 354 | { | 
|  | 355 | ret = -1; | 
|  | 356 | goto exit; | 
|  | 357 | } | 
| Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 358 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 359 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 360 | #if defined(MBEDTLS_HAVE_TIME) | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 361 | cur_time = (unsigned long) mbedtls_time( NULL ); | 
| Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 362 | #else | 
|  | 363 | cur_time = ctx->serial; | 
|  | 364 | #endif | 
|  | 365 |  | 
|  | 366 | cookie_time = ( (unsigned long) cookie[0] << 24 ) | | 
|  | 367 | ( (unsigned long) cookie[1] << 16 ) | | 
|  | 368 | ( (unsigned long) cookie[2] <<  8 ) | | 
|  | 369 | ( (unsigned long) cookie[3]       ); | 
|  | 370 |  | 
| Manuel Pégourié-Gonnard | bef8f09 | 2014-07-23 23:40:29 +0200 | [diff] [blame] | 371 | if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout ) | 
| Gilles Peskine | c2f7b75 | 2021-12-13 12:35:08 +0100 | [diff] [blame] | 372 | { | 
|  | 373 | ret = -1; | 
|  | 374 | goto exit; | 
|  | 375 | } | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 376 |  | 
| Gilles Peskine | c2f7b75 | 2021-12-13 12:35:08 +0100 | [diff] [blame] | 377 | exit: | 
| Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 378 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Neil Armstrong | 79daea2 | 2022-03-21 12:05:51 +0100 | [diff] [blame] | 379 | status = psa_mac_abort( &operation ); | 
|  | 380 | if( status != PSA_SUCCESS ) | 
|  | 381 | ret = psa_ssl_status_to_mbedtls( status ); | 
| Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 382 | #else | 
| Gilles Peskine | c2f7b75 | 2021-12-13 12:35:08 +0100 | [diff] [blame] | 383 | mbedtls_platform_zeroize( ref_hmac, sizeof( ref_hmac ) ); | 
| Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 384 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Gilles Peskine | c2f7b75 | 2021-12-13 12:35:08 +0100 | [diff] [blame] | 385 | return( ret ); | 
| Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 386 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 387 | #endif /* MBEDTLS_SSL_COOKIE_C */ |