ssl_cookie.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * DTLS cookie callbacks implementation
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. /*
  47. * These session callbacks use a simple chained list
  48. * to store and retrieve the session information.
  49. */
  50. #if !defined(MBEDTLS_CONFIG_FILE)
  51. #include "mbedtls/config.h"
  52. #else
  53. #include MBEDTLS_CONFIG_FILE
  54. #endif
  55. #if defined(MBEDTLS_SSL_COOKIE_C)
  56. #if defined(MBEDTLS_PLATFORM_C)
  57. #include "mbedtls/platform.h"
  58. #else
  59. #define mbedtls_calloc calloc
  60. #define mbedtls_free free
  61. #endif
  62. #include "mbedtls/ssl_cookie.h"
  63. #include "mbedtls/ssl_internal.h"
  64. #include "mbedtls/platform_util.h"
  65. #include <string.h>
  66. /*
  67. * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-512 is
  68. * available. Try SHA-256 first, 512 wastes resources since we need to stay
  69. * with max 32 bytes of cookie for DTLS 1.0
  70. */
  71. #if defined(MBEDTLS_SHA256_C)
  72. #define COOKIE_MD MBEDTLS_MD_SHA224
  73. #define COOKIE_MD_OUTLEN 32
  74. #define COOKIE_HMAC_LEN 28
  75. #elif defined(MBEDTLS_SHA512_C)
  76. #define COOKIE_MD MBEDTLS_MD_SHA384
  77. #define COOKIE_MD_OUTLEN 48
  78. #define COOKIE_HMAC_LEN 28
  79. #elif defined(MBEDTLS_SHA1_C)
  80. #define COOKIE_MD MBEDTLS_MD_SHA1
  81. #define COOKIE_MD_OUTLEN 20
  82. #define COOKIE_HMAC_LEN 20
  83. #else
  84. #error "DTLS hello verify needs SHA-1 or SHA-2"
  85. #endif
  86. /*
  87. * Cookies are formed of a 4-bytes timestamp (or serial number) and
  88. * an HMAC of timestemp and client ID.
  89. */
  90. #define COOKIE_LEN ( 4 + COOKIE_HMAC_LEN )
  91. void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx )
  92. {
  93. mbedtls_md_init( &ctx->hmac_ctx );
  94. #if !defined(MBEDTLS_HAVE_TIME)
  95. ctx->serial = 0;
  96. #endif
  97. ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
  98. #if defined(MBEDTLS_THREADING_C)
  99. mbedtls_mutex_init( &ctx->mutex );
  100. #endif
  101. }
  102. void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay )
  103. {
  104. ctx->timeout = delay;
  105. }
  106. void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx )
  107. {
  108. mbedtls_md_free( &ctx->hmac_ctx );
  109. #if defined(MBEDTLS_THREADING_C)
  110. mbedtls_mutex_free( &ctx->mutex );
  111. #endif
  112. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) );
  113. }
  114. int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
  115. int (*f_rng)(void *, unsigned char *, size_t),
  116. void *p_rng )
  117. {
  118. int ret;
  119. unsigned char key[COOKIE_MD_OUTLEN];
  120. if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
  121. return( ret );
  122. ret = mbedtls_md_setup( &ctx->hmac_ctx, mbedtls_md_info_from_type( COOKIE_MD ), 1 );
  123. if( ret != 0 )
  124. return( ret );
  125. ret = mbedtls_md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) );
  126. if( ret != 0 )
  127. return( ret );
  128. mbedtls_platform_zeroize( key, sizeof( key ) );
  129. return( 0 );
  130. }
  131. /*
  132. * Generate the HMAC part of a cookie
  133. */
  134. static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx,
  135. const unsigned char time[4],
  136. unsigned char **p, unsigned char *end,
  137. const unsigned char *cli_id, size_t cli_id_len )
  138. {
  139. unsigned char hmac_out[COOKIE_MD_OUTLEN];
  140. MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_HMAC_LEN );
  141. if( mbedtls_md_hmac_reset( hmac_ctx ) != 0 ||
  142. mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 ||
  143. mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 ||
  144. mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 )
  145. {
  146. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  147. }
  148. memcpy( *p, hmac_out, COOKIE_HMAC_LEN );
  149. *p += COOKIE_HMAC_LEN;
  150. return( 0 );
  151. }
  152. /*
  153. * Generate cookie for DTLS ClientHello verification
  154. */
  155. int mbedtls_ssl_cookie_write( void *p_ctx,
  156. unsigned char **p, unsigned char *end,
  157. const unsigned char *cli_id, size_t cli_id_len )
  158. {
  159. int ret;
  160. mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
  161. unsigned long t;
  162. if( ctx == NULL || cli_id == NULL )
  163. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  164. MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_LEN );
  165. #if defined(MBEDTLS_HAVE_TIME)
  166. t = (unsigned long) mbedtls_time( NULL );
  167. #else
  168. t = ctx->serial++;
  169. #endif
  170. (*p)[0] = (unsigned char)( t >> 24 );
  171. (*p)[1] = (unsigned char)( t >> 16 );
  172. (*p)[2] = (unsigned char)( t >> 8 );
  173. (*p)[3] = (unsigned char)( t );
  174. *p += 4;
  175. #if defined(MBEDTLS_THREADING_C)
  176. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  177. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret );
  178. #endif
  179. ret = ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4,
  180. p, end, cli_id, cli_id_len );
  181. #if defined(MBEDTLS_THREADING_C)
  182. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  183. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
  184. MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  185. #endif
  186. return( ret );
  187. }
  188. /*
  189. * Check a cookie
  190. */
  191. int mbedtls_ssl_cookie_check( void *p_ctx,
  192. const unsigned char *cookie, size_t cookie_len,
  193. const unsigned char *cli_id, size_t cli_id_len )
  194. {
  195. unsigned char ref_hmac[COOKIE_HMAC_LEN];
  196. int ret = 0;
  197. unsigned char *p = ref_hmac;
  198. mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
  199. unsigned long cur_time, cookie_time;
  200. if( ctx == NULL || cli_id == NULL )
  201. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  202. if( cookie_len != COOKIE_LEN )
  203. return( -1 );
  204. #if defined(MBEDTLS_THREADING_C)
  205. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  206. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret );
  207. #endif
  208. if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie,
  209. &p, p + sizeof( ref_hmac ),
  210. cli_id, cli_id_len ) != 0 )
  211. ret = -1;
  212. #if defined(MBEDTLS_THREADING_C)
  213. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  214. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
  215. MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  216. #endif
  217. if( ret != 0 )
  218. return( ret );
  219. if( mbedtls_ssl_safer_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 )
  220. return( -1 );
  221. #if defined(MBEDTLS_HAVE_TIME)
  222. cur_time = (unsigned long) mbedtls_time( NULL );
  223. #else
  224. cur_time = ctx->serial;
  225. #endif
  226. cookie_time = ( (unsigned long) cookie[0] << 24 ) |
  227. ( (unsigned long) cookie[1] << 16 ) |
  228. ( (unsigned long) cookie[2] << 8 ) |
  229. ( (unsigned long) cookie[3] );
  230. if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout )
  231. return( -1 );
  232. return( 0 );
  233. }
  234. #endif /* MBEDTLS_SSL_COOKIE_C */