ssl_cookie.c 7.0 KB

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