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 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 28 | # if defined(MBEDTLS_PLATFORM_C) |
| 29 | # include "mbedtls/platform.h" |
| 30 | # else |
| 31 | # define mbedtls_calloc calloc |
| 32 | # define mbedtls_free free |
| 33 | # endif |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 34 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 35 | # include "mbedtls/ssl_cookie.h" |
| 36 | # include "ssl_misc.h" |
| 37 | # include "mbedtls/error.h" |
| 38 | # include "mbedtls/platform_util.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 39 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 40 | # include <string.h> |
Manuel Pégourié-Gonnard | d901d17 | 2015-02-16 18:37:53 +0000 | [diff] [blame] | 41 | |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 42 | /* |
| 43 | * 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] | 44 | * available. Try SHA-256 first, 512 wastes resources |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 45 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 46 | # if defined(MBEDTLS_SHA224_C) |
| 47 | # define COOKIE_MD MBEDTLS_MD_SHA224 |
| 48 | # define COOKIE_MD_OUTLEN 32 |
| 49 | # define COOKIE_HMAC_LEN 28 |
| 50 | # elif defined(MBEDTLS_SHA384_C) |
| 51 | # define COOKIE_MD MBEDTLS_MD_SHA384 |
| 52 | # define COOKIE_MD_OUTLEN 48 |
| 53 | # define COOKIE_HMAC_LEN 28 |
| 54 | # elif defined(MBEDTLS_SHA1_C) |
| 55 | # define COOKIE_MD MBEDTLS_MD_SHA1 |
| 56 | # define COOKIE_MD_OUTLEN 20 |
| 57 | # define COOKIE_HMAC_LEN 20 |
| 58 | # else |
| 59 | # error "DTLS hello verify needs SHA-1 or SHA-2" |
| 60 | # endif |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 61 | |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 62 | /* |
| 63 | * Cookies are formed of a 4-bytes timestamp (or serial number) and |
| 64 | * an HMAC of timestemp and client ID. |
| 65 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 66 | # define COOKIE_LEN (4 + COOKIE_HMAC_LEN) |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 67 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 68 | void mbedtls_ssl_cookie_init(mbedtls_ssl_cookie_ctx *ctx) |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 69 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 70 | mbedtls_md_init(&ctx->hmac_ctx); |
| 71 | # if !defined(MBEDTLS_HAVE_TIME) |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 72 | ctx->serial = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 73 | # endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 74 | ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT; |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 75 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 76 | # if defined(MBEDTLS_THREADING_C) |
| 77 | mbedtls_mutex_init(&ctx->mutex); |
| 78 | # endif |
Manuel Pégourié-Gonnard | bef8f09 | 2014-07-23 23:40:29 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 81 | void mbedtls_ssl_cookie_set_timeout(mbedtls_ssl_cookie_ctx *ctx, |
| 82 | unsigned long delay) |
Manuel Pégourié-Gonnard | bef8f09 | 2014-07-23 23:40:29 +0200 | [diff] [blame] | 83 | { |
| 84 | ctx->timeout = delay; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 87 | void mbedtls_ssl_cookie_free(mbedtls_ssl_cookie_ctx *ctx) |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 88 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 89 | mbedtls_md_free(&ctx->hmac_ctx); |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 90 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 91 | # if defined(MBEDTLS_THREADING_C) |
| 92 | mbedtls_mutex_free(&ctx->mutex); |
| 93 | # endif |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 94 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 95 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ssl_cookie_ctx)); |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 98 | int mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx *ctx, |
| 99 | int (*f_rng)(void *, unsigned char *, size_t), |
| 100 | void *p_rng) |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 101 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 102 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 103 | unsigned char key[COOKIE_MD_OUTLEN]; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 104 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 105 | if ((ret = f_rng(p_rng, key, sizeof(key))) != 0) |
| 106 | return ret; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 107 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 108 | ret = mbedtls_md_setup(&ctx->hmac_ctx, mbedtls_md_info_from_type(COOKIE_MD), |
| 109 | 1); |
| 110 | if (ret != 0) |
| 111 | return ret; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 112 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 113 | ret = mbedtls_md_hmac_starts(&ctx->hmac_ctx, key, sizeof(key)); |
| 114 | if (ret != 0) |
| 115 | return ret; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 116 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 117 | mbedtls_platform_zeroize(key, sizeof(key)); |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 118 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 119 | return 0; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 123 | * Generate the HMAC part of a cookie |
| 124 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 125 | static int ssl_cookie_hmac(mbedtls_md_context_t *hmac_ctx, |
| 126 | const unsigned char time[4], |
| 127 | unsigned char **p, |
| 128 | unsigned char *end, |
| 129 | const unsigned char *cli_id, |
| 130 | size_t cli_id_len) |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 131 | { |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 132 | unsigned char hmac_out[COOKIE_MD_OUTLEN]; |
| 133 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 134 | MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_HMAC_LEN); |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 135 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 136 | if (mbedtls_md_hmac_reset(hmac_ctx) != 0 || |
| 137 | mbedtls_md_hmac_update(hmac_ctx, time, 4) != 0 || |
| 138 | mbedtls_md_hmac_update(hmac_ctx, cli_id, cli_id_len) != 0 || |
| 139 | mbedtls_md_hmac_finish(hmac_ctx, hmac_out) != 0) { |
| 140 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 143 | memcpy(*p, hmac_out, COOKIE_HMAC_LEN); |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 144 | *p += COOKIE_HMAC_LEN; |
| 145 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 146 | return 0; |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 150 | * Generate cookie for DTLS ClientHello verification |
| 151 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 152 | int mbedtls_ssl_cookie_write(void *p_ctx, |
| 153 | unsigned char **p, |
| 154 | unsigned char *end, |
| 155 | const unsigned char *cli_id, |
| 156 | size_t cli_id_len) |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 157 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 158 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 159 | 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] | 160 | unsigned long t; |
Manuel Pégourié-Gonnard | e4de061 | 2014-07-23 17:26:48 +0200 | [diff] [blame] | 161 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 162 | if (ctx == NULL || cli_id == NULL) |
| 163 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 164 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 165 | MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_LEN); |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 166 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 167 | # if defined(MBEDTLS_HAVE_TIME) |
| 168 | t = (unsigned long)mbedtls_time(NULL); |
| 169 | # else |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 170 | t = ctx->serial++; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 171 | # endif |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 172 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 173 | (*p)[0] = (unsigned char)(t >> 24); |
| 174 | (*p)[1] = (unsigned char)(t >> 16); |
| 175 | (*p)[2] = (unsigned char)(t >> 8); |
| 176 | (*p)[3] = (unsigned char)(t); |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 177 | *p += 4; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 178 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 179 | # if defined(MBEDTLS_THREADING_C) |
| 180 | if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) |
| 181 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret); |
| 182 | # endif |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 183 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 184 | ret = ssl_cookie_hmac(&ctx->hmac_ctx, *p - 4, p, end, cli_id, cli_id_len); |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 185 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 186 | # if defined(MBEDTLS_THREADING_C) |
| 187 | if (mbedtls_mutex_unlock(&ctx->mutex) != 0) |
| 188 | return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, |
| 189 | MBEDTLS_ERR_THREADING_MUTEX_ERROR)); |
| 190 | # endif |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 191 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 192 | return ret; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Check a cookie |
| 197 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 198 | int mbedtls_ssl_cookie_check(void *p_ctx, |
| 199 | const unsigned char *cookie, |
| 200 | size_t cookie_len, |
| 201 | const unsigned char *cli_id, |
| 202 | size_t cli_id_len) |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 203 | { |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 204 | unsigned char ref_hmac[COOKIE_HMAC_LEN]; |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 205 | int ret = 0; |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 206 | unsigned char *p = ref_hmac; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 207 | 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] | 208 | unsigned long cur_time, cookie_time; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 209 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 210 | if (ctx == NULL || cli_id == NULL) |
| 211 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 212 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 213 | if (cookie_len != COOKIE_LEN) |
| 214 | return -1; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 215 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 216 | # if defined(MBEDTLS_THREADING_C) |
| 217 | if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) |
| 218 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret); |
| 219 | # endif |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 220 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 221 | if (ssl_cookie_hmac(&ctx->hmac_ctx, cookie, &p, p + sizeof(ref_hmac), |
| 222 | cli_id, cli_id_len) != 0) |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 223 | ret = -1; |
| 224 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 225 | # if defined(MBEDTLS_THREADING_C) |
| 226 | if (mbedtls_mutex_unlock(&ctx->mutex) != 0) |
| 227 | return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, |
| 228 | MBEDTLS_ERR_THREADING_MUTEX_ERROR)); |
| 229 | # endif |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 230 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 231 | if (ret != 0) |
| 232 | return ret; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 233 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 234 | if (mbedtls_ssl_safer_memcmp(cookie + 4, ref_hmac, sizeof(ref_hmac)) != 0) |
| 235 | return -1; |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 236 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 237 | # if defined(MBEDTLS_HAVE_TIME) |
| 238 | cur_time = (unsigned long)mbedtls_time(NULL); |
| 239 | # else |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 240 | cur_time = ctx->serial; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 241 | # endif |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 242 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 243 | cookie_time = ((unsigned long)cookie[0] << 24) | |
| 244 | ((unsigned long)cookie[1] << 16) | |
| 245 | ((unsigned long)cookie[2] << 8) | ((unsigned long)cookie[3]); |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 246 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 247 | if (ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout) |
| 248 | return -1; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 249 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 250 | return 0; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 251 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 252 | #endif /* MBEDTLS_SSL_COOKIE_C */ |