ssl_cookie.c 7.3 KB

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