ssl_cookie.c 7.4 KB

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