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 | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/platform.h" |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 30 | #else |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 31 | #define mbedtls_calloc calloc |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 32 | #define mbedtls_free free |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 33 | #endif |
| 34 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 35 | #include "mbedtls/ssl_cookie.h" |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 36 | #include "ssl_misc.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 37 | #include "mbedtls/error.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 38 | #include "mbedtls/platform_util.h" |
Gabor Mezei | 765862c | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 39 | #include "mbedtls/constant_time.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 40 | |
Manuel Pégourié-Gonnard | d901d17 | 2015-02-16 18:37:53 +0000 | [diff] [blame] | 41 | #include <string.h> |
| 42 | |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 43 | /* |
| 44 | * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-512 is |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 45 | * available. Try SHA-256 first, 512 wastes resources |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 46 | */ |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 47 | #if defined(MBEDTLS_SHA224_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 48 | #define COOKIE_MD MBEDTLS_MD_SHA224 |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 49 | #define COOKIE_MD_OUTLEN 32 |
| 50 | #define COOKIE_HMAC_LEN 28 |
Mateusz Starzyk | 6326a8d | 2021-05-10 13:51:53 +0200 | [diff] [blame] | 51 | #elif defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | #define COOKIE_MD MBEDTLS_MD_SHA384 |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 53 | #define COOKIE_MD_OUTLEN 48 |
| 54 | #define COOKIE_HMAC_LEN 28 |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 55 | #elif defined(MBEDTLS_SHA1_C) |
| 56 | #define COOKIE_MD MBEDTLS_MD_SHA1 |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 57 | #define COOKIE_MD_OUTLEN 20 |
| 58 | #define COOKIE_HMAC_LEN 20 |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 59 | #else |
| 60 | #error "DTLS hello verify needs SHA-1 or SHA-2" |
| 61 | #endif |
| 62 | |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 63 | /* |
| 64 | * Cookies are formed of a 4-bytes timestamp (or serial number) and |
| 65 | * an HMAC of timestemp and client ID. |
| 66 | */ |
| 67 | #define COOKIE_LEN ( 4 + COOKIE_HMAC_LEN ) |
| 68 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 69 | void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx ) |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 70 | { |
Neil Armstrong | bca99ee | 2022-03-04 10:20:20 +0100 | [diff] [blame] | 71 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 72 | ctx->psa_hmac = MBEDTLS_SVC_KEY_ID_INIT; |
| 73 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 74 | mbedtls_md_init( &ctx->hmac_ctx ); |
| 75 | #if !defined(MBEDTLS_HAVE_TIME) |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 76 | ctx->serial = 0; |
| 77 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 78 | ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT; |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 79 | |
| 80 | #if defined(MBEDTLS_THREADING_C) |
| 81 | mbedtls_mutex_init( &ctx->mutex ); |
| 82 | #endif |
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) |
| 93 | psa_destroy_key( ctx->psa_hmac ); |
| 94 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
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 |
| 100 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 101 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) ); |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 104 | int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx, |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 105 | int (*f_rng)(void *, unsigned char *, size_t), |
| 106 | void *p_rng ) |
| 107 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 108 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 109 | unsigned char key[COOKIE_MD_OUTLEN]; |
Neil Armstrong | d633201 | 2022-03-04 10:26:16 +0100 | [diff] [blame] | 110 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 111 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 112 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 113 | psa_algorithm_t alg; |
| 114 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 115 | |
| 116 | if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 ) |
| 117 | return( ret ); |
| 118 | |
Neil Armstrong | d633201 | 2022-03-04 10:26:16 +0100 | [diff] [blame] | 119 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 120 | alg = mbedtls_psa_translate_md( COOKIE_MD ); |
| 121 | if( alg == 0 ) |
| 122 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 123 | |
| 124 | ctx->psa_hmac_alg = PSA_ALG_HMAC( alg ); |
| 125 | |
| 126 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 127 | psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( alg ) ); |
| 128 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 129 | |
| 130 | if( ( status = psa_import_key( &attributes, |
| 131 | key, sizeof( key ), |
| 132 | &ctx->psa_hmac ) ) != PSA_SUCCESS ) |
| 133 | { |
| 134 | return psa_ssl_status_to_mbedtls( status ); |
| 135 | } |
Neil Armstrong | 23d34ce | 2022-03-04 10:32:26 +0100 | [diff] [blame^] | 136 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 137 | 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] | 138 | if( ret != 0 ) |
| 139 | return( ret ); |
| 140 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 141 | 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] | 142 | if( ret != 0 ) |
| 143 | return( ret ); |
Neil Armstrong | 23d34ce | 2022-03-04 10:32:26 +0100 | [diff] [blame^] | 144 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 145 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 146 | mbedtls_platform_zeroize( key, sizeof( key ) ); |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 147 | |
| 148 | return( 0 ); |
| 149 | } |
| 150 | |
| 151 | /* |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 152 | * Generate the HMAC part of a cookie |
| 153 | */ |
Neil Armstrong | 23d34ce | 2022-03-04 10:32:26 +0100 | [diff] [blame^] | 154 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 155 | static int ssl_cookie_hmac( mbedtls_ssl_cookie_ctx *ctx, |
| 156 | const unsigned char time[4], |
| 157 | unsigned char **p, unsigned char *end, |
| 158 | const unsigned char *cli_id, size_t cli_id_len ) |
| 159 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 160 | 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] | 161 | const unsigned char time[4], |
| 162 | unsigned char **p, unsigned char *end, |
| 163 | const unsigned char *cli_id, size_t cli_id_len ) |
Neil Armstrong | 23d34ce | 2022-03-04 10:32:26 +0100 | [diff] [blame^] | 164 | #endif |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 165 | { |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 166 | unsigned char hmac_out[COOKIE_MD_OUTLEN]; |
Neil Armstrong | 23d34ce | 2022-03-04 10:32:26 +0100 | [diff] [blame^] | 167 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 168 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 169 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 170 | size_t sign_mac_length = 0; |
| 171 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 172 | |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 173 | MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_HMAC_LEN ); |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 174 | |
Neil Armstrong | 23d34ce | 2022-03-04 10:32:26 +0100 | [diff] [blame^] | 175 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 176 | if( psa_mac_sign_setup( &operation, ctx->psa_hmac, |
| 177 | ctx->psa_hmac_alg ) != PSA_SUCCESS ) { |
| 178 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 179 | goto cookie_hmac_exit; |
| 180 | } |
| 181 | |
| 182 | if( psa_mac_update( &operation, time, 4 ) != PSA_SUCCESS ) { |
| 183 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 184 | goto cookie_hmac_exit; |
| 185 | } |
| 186 | |
| 187 | if( psa_mac_update( &operation, cli_id, |
| 188 | cli_id_len ) != PSA_SUCCESS ) { |
| 189 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 190 | goto cookie_hmac_exit; |
| 191 | } |
| 192 | |
| 193 | if( psa_mac_sign_finish( &operation, hmac_out, COOKIE_MD_OUTLEN, |
| 194 | &sign_mac_length ) != PSA_SUCCESS ) { |
| 195 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 196 | goto cookie_hmac_exit; |
| 197 | } |
| 198 | |
| 199 | ret = 0; |
| 200 | #else |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 201 | if( mbedtls_md_hmac_reset( hmac_ctx ) != 0 || |
| 202 | mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 || |
| 203 | mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 || |
| 204 | mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 ) |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 205 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 207 | } |
Neil Armstrong | 23d34ce | 2022-03-04 10:32:26 +0100 | [diff] [blame^] | 208 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 209 | |
| 210 | memcpy( *p, hmac_out, COOKIE_HMAC_LEN ); |
| 211 | *p += COOKIE_HMAC_LEN; |
| 212 | |
Neil Armstrong | 23d34ce | 2022-03-04 10:32:26 +0100 | [diff] [blame^] | 213 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 214 | cookie_hmac_exit: |
| 215 | if( psa_mac_abort( &operation ) != PSA_SUCCESS ) |
| 216 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 217 | |
| 218 | return ret; |
| 219 | #else |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 220 | return( 0 ); |
Neil Armstrong | 23d34ce | 2022-03-04 10:32:26 +0100 | [diff] [blame^] | 221 | #endif |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /* |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 225 | * Generate cookie for DTLS ClientHello verification |
| 226 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | int mbedtls_ssl_cookie_write( void *p_ctx, |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 228 | unsigned char **p, unsigned char *end, |
| 229 | const unsigned char *cli_id, size_t cli_id_len ) |
| 230 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 231 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 232 | 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] | 233 | unsigned long t; |
Manuel Pégourié-Gonnard | e4de061 | 2014-07-23 17:26:48 +0200 | [diff] [blame] | 234 | |
Manuel Pégourié-Gonnard | 29ad7e8 | 2014-07-23 19:12:15 +0200 | [diff] [blame] | 235 | if( ctx == NULL || cli_id == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 237 | |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 238 | MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_LEN ); |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 239 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 240 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 241 | t = (unsigned long) mbedtls_time( NULL ); |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 242 | #else |
| 243 | t = ctx->serial++; |
| 244 | #endif |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 245 | |
Joe Subbiani | b6511b0 | 2021-07-16 15:02:55 +0100 | [diff] [blame] | 246 | MBEDTLS_PUT_UINT32_BE(t, *p, 0); |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 247 | *p += 4; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 248 | |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 249 | #if defined(MBEDTLS_THREADING_C) |
| 250 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 251 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret ) ); |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 252 | #endif |
| 253 | |
Neil Armstrong | 23d34ce | 2022-03-04 10:32:26 +0100 | [diff] [blame^] | 254 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 255 | ret = ssl_cookie_hmac( ctx, *p - 4, |
| 256 | #else |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 257 | ret = ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4, |
Neil Armstrong | 23d34ce | 2022-03-04 10:32:26 +0100 | [diff] [blame^] | 258 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 259 | p, end, cli_id, cli_id_len ); |
| 260 | |
| 261 | #if defined(MBEDTLS_THREADING_C) |
| 262 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 263 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, |
| 264 | MBEDTLS_ERR_THREADING_MUTEX_ERROR ) ); |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 265 | #endif |
| 266 | |
| 267 | return( ret ); |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* |
| 271 | * Check a cookie |
| 272 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 273 | int mbedtls_ssl_cookie_check( void *p_ctx, |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 274 | const unsigned char *cookie, size_t cookie_len, |
| 275 | const unsigned char *cli_id, size_t cli_id_len ) |
| 276 | { |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 277 | unsigned char ref_hmac[COOKIE_HMAC_LEN]; |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 278 | int ret = 0; |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 279 | unsigned char *p = ref_hmac; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 280 | 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] | 281 | unsigned long cur_time, cookie_time; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 282 | |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 283 | if( ctx == NULL || cli_id == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 284 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 285 | |
| 286 | if( cookie_len != COOKIE_LEN ) |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 287 | return( -1 ); |
| 288 | |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 289 | #if defined(MBEDTLS_THREADING_C) |
| 290 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 291 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret ) ); |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 292 | #endif |
| 293 | |
Neil Armstrong | 23d34ce | 2022-03-04 10:32:26 +0100 | [diff] [blame^] | 294 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 295 | if( ssl_cookie_hmac( ctx, cookie, |
| 296 | #else |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 297 | if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie, |
Neil Armstrong | 23d34ce | 2022-03-04 10:32:26 +0100 | [diff] [blame^] | 298 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 299 | &p, p + sizeof( ref_hmac ), |
| 300 | cli_id, cli_id_len ) != 0 ) |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 301 | ret = -1; |
| 302 | |
| 303 | #if defined(MBEDTLS_THREADING_C) |
| 304 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
Gilles Peskine | c2f7b75 | 2021-12-13 12:35:08 +0100 | [diff] [blame] | 305 | { |
| 306 | ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, |
| 307 | MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
| 308 | } |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 309 | #endif |
| 310 | |
| 311 | if( ret != 0 ) |
Gilles Peskine | c2f7b75 | 2021-12-13 12:35:08 +0100 | [diff] [blame] | 312 | goto exit; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 313 | |
Gabor Mezei | 90437e3 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 314 | if( mbedtls_ct_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 ) |
Gilles Peskine | c2f7b75 | 2021-12-13 12:35:08 +0100 | [diff] [blame] | 315 | { |
| 316 | ret = -1; |
| 317 | goto exit; |
| 318 | } |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 319 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 320 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 321 | cur_time = (unsigned long) mbedtls_time( NULL ); |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 322 | #else |
| 323 | cur_time = ctx->serial; |
| 324 | #endif |
| 325 | |
| 326 | cookie_time = ( (unsigned long) cookie[0] << 24 ) | |
| 327 | ( (unsigned long) cookie[1] << 16 ) | |
| 328 | ( (unsigned long) cookie[2] << 8 ) | |
| 329 | ( (unsigned long) cookie[3] ); |
| 330 | |
Manuel Pégourié-Gonnard | bef8f09 | 2014-07-23 23:40:29 +0200 | [diff] [blame] | 331 | if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout ) |
Gilles Peskine | c2f7b75 | 2021-12-13 12:35:08 +0100 | [diff] [blame] | 332 | { |
| 333 | ret = -1; |
| 334 | goto exit; |
| 335 | } |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 336 | |
Gilles Peskine | c2f7b75 | 2021-12-13 12:35:08 +0100 | [diff] [blame] | 337 | exit: |
| 338 | mbedtls_platform_zeroize( ref_hmac, sizeof( ref_hmac ) ); |
| 339 | return( ret ); |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 340 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 341 | #endif /* MBEDTLS_SSL_COOKIE_C */ |