ssl_cookie.c 6.6 KB

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