ssl_cookie.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * DTLS cookie callbacks implementation
  3. *
  4. * Copyright The Mbed TLS Contributors
  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.
  18. */
  19. /*
  20. * These session callbacks use a simple chained list
  21. * to store and retrieve the session information.
  22. */
  23. #include "common.h"
  24. #if defined(MBEDTLS_SSL_COOKIE_C)
  25. #if defined(MBEDTLS_PLATFORM_C)
  26. #include "mbedtls/platform.h"
  27. #else
  28. #define mbedtls_calloc calloc
  29. #define mbedtls_free free
  30. #endif
  31. #include "mbedtls/ssl_cookie.h"
  32. #include "ssl_misc.h"
  33. #include "mbedtls/error.h"
  34. #include "mbedtls/platform_util.h"
  35. #include "mbedtls/constant_time.h"
  36. #include <string.h>
  37. /*
  38. * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-512 is
  39. * available. Try SHA-256 first, 512 wastes resources
  40. */
  41. #if defined(MBEDTLS_SHA224_C)
  42. #define COOKIE_MD MBEDTLS_MD_SHA224
  43. #define COOKIE_MD_OUTLEN 32
  44. #define COOKIE_HMAC_LEN 28
  45. #elif defined(MBEDTLS_SHA384_C)
  46. #define COOKIE_MD MBEDTLS_MD_SHA384
  47. #define COOKIE_MD_OUTLEN 48
  48. #define COOKIE_HMAC_LEN 28
  49. #elif defined(MBEDTLS_SHA1_C)
  50. #define COOKIE_MD MBEDTLS_MD_SHA1
  51. #define COOKIE_MD_OUTLEN 20
  52. #define COOKIE_HMAC_LEN 20
  53. #else
  54. #error "DTLS hello verify needs SHA-1 or SHA-2"
  55. #endif
  56. /*
  57. * Cookies are formed of a 4-bytes timestamp (or serial number) and
  58. * an HMAC of timestemp and client ID.
  59. */
  60. #define COOKIE_LEN ( 4 + COOKIE_HMAC_LEN )
  61. void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx )
  62. {
  63. mbedtls_md_init( &ctx->hmac_ctx );
  64. #if !defined(MBEDTLS_HAVE_TIME)
  65. ctx->serial = 0;
  66. #endif
  67. ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
  68. #if defined(MBEDTLS_THREADING_C)
  69. mbedtls_mutex_init( &ctx->mutex );
  70. #endif
  71. }
  72. void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay )
  73. {
  74. ctx->timeout = delay;
  75. }
  76. void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx )
  77. {
  78. mbedtls_md_free( &ctx->hmac_ctx );
  79. #if defined(MBEDTLS_THREADING_C)
  80. mbedtls_mutex_free( &ctx->mutex );
  81. #endif
  82. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) );
  83. }
  84. int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
  85. int (*f_rng)(void *, unsigned char *, size_t),
  86. void *p_rng )
  87. {
  88. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  89. unsigned char key[COOKIE_MD_OUTLEN];
  90. if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
  91. return( ret );
  92. ret = mbedtls_md_setup( &ctx->hmac_ctx, mbedtls_md_info_from_type( COOKIE_MD ), 1 );
  93. if( ret != 0 )
  94. return( ret );
  95. ret = mbedtls_md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) );
  96. if( ret != 0 )
  97. return( ret );
  98. mbedtls_platform_zeroize( key, sizeof( key ) );
  99. return( 0 );
  100. }
  101. /*
  102. * Generate the HMAC part of a cookie
  103. */
  104. static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx,
  105. const unsigned char time[4],
  106. unsigned char **p, unsigned char *end,
  107. const unsigned char *cli_id, size_t cli_id_len )
  108. {
  109. unsigned char hmac_out[COOKIE_MD_OUTLEN];
  110. MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_HMAC_LEN );
  111. if( mbedtls_md_hmac_reset( hmac_ctx ) != 0 ||
  112. mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 ||
  113. mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 ||
  114. mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 )
  115. {
  116. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  117. }
  118. memcpy( *p, hmac_out, COOKIE_HMAC_LEN );
  119. *p += COOKIE_HMAC_LEN;
  120. return( 0 );
  121. }
  122. /*
  123. * Generate cookie for DTLS ClientHello verification
  124. */
  125. int mbedtls_ssl_cookie_write( void *p_ctx,
  126. unsigned char **p, unsigned char *end,
  127. const unsigned char *cli_id, size_t cli_id_len )
  128. {
  129. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  130. mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
  131. unsigned long t;
  132. if( ctx == NULL || cli_id == NULL )
  133. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  134. MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_LEN );
  135. #if defined(MBEDTLS_HAVE_TIME)
  136. t = (unsigned long) mbedtls_time( NULL );
  137. #else
  138. t = ctx->serial++;
  139. #endif
  140. MBEDTLS_PUT_UINT32_BE(t, *p, 0);
  141. *p += 4;
  142. #if defined(MBEDTLS_THREADING_C)
  143. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  144. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret ) );
  145. #endif
  146. ret = ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4,
  147. p, end, cli_id, cli_id_len );
  148. #if defined(MBEDTLS_THREADING_C)
  149. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  150. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR,
  151. MBEDTLS_ERR_THREADING_MUTEX_ERROR ) );
  152. #endif
  153. return( ret );
  154. }
  155. /*
  156. * Check a cookie
  157. */
  158. int mbedtls_ssl_cookie_check( void *p_ctx,
  159. const unsigned char *cookie, size_t cookie_len,
  160. const unsigned char *cli_id, size_t cli_id_len )
  161. {
  162. unsigned char ref_hmac[COOKIE_HMAC_LEN];
  163. int ret = 0;
  164. unsigned char *p = ref_hmac;
  165. mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
  166. unsigned long cur_time, cookie_time;
  167. if( ctx == NULL || cli_id == NULL )
  168. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  169. if( cookie_len != COOKIE_LEN )
  170. return( -1 );
  171. #if defined(MBEDTLS_THREADING_C)
  172. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  173. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret ) );
  174. #endif
  175. if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie,
  176. &p, p + sizeof( ref_hmac ),
  177. cli_id, cli_id_len ) != 0 )
  178. ret = -1;
  179. #if defined(MBEDTLS_THREADING_C)
  180. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  181. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR,
  182. MBEDTLS_ERR_THREADING_MUTEX_ERROR ) );
  183. #endif
  184. if( ret != 0 )
  185. return( ret );
  186. if( mbedtls_ct_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 )
  187. return( -1 );
  188. #if defined(MBEDTLS_HAVE_TIME)
  189. cur_time = (unsigned long) mbedtls_time( NULL );
  190. #else
  191. cur_time = ctx->serial;
  192. #endif
  193. cookie_time = ( (unsigned long) cookie[0] << 24 ) |
  194. ( (unsigned long) cookie[1] << 16 ) |
  195. ( (unsigned long) cookie[2] << 8 ) |
  196. ( (unsigned long) cookie[3] );
  197. if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout )
  198. return( -1 );
  199. return( 0 );
  200. }
  201. #endif /* MBEDTLS_SSL_COOKIE_C */