test_suite_ssl.function 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* BEGIN_HEADER */
  2. #include <mbedtls/ssl.h>
  3. #include <mbedtls/ssl_internal.h>
  4. /* END_HEADER */
  5. /* BEGIN_DEPENDENCIES
  6. * depends_on:MBEDTLS_SSL_TLS_C
  7. * END_DEPENDENCIES
  8. */
  9. /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
  10. void ssl_dtls_replay( data_t * prevs, data_t * new, int ret )
  11. {
  12. uint32_t len = 0;
  13. mbedtls_ssl_context ssl;
  14. mbedtls_ssl_config conf;
  15. mbedtls_ssl_init( &ssl );
  16. mbedtls_ssl_config_init( &conf );
  17. TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
  18. MBEDTLS_SSL_IS_CLIENT,
  19. MBEDTLS_SSL_TRANSPORT_DATAGRAM,
  20. MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
  21. TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
  22. /* Read previous record numbers */
  23. for( len = 0; len < prevs->len; len += 6 )
  24. {
  25. memcpy( ssl.in_ctr + 2, prevs->x + len, 6 );
  26. mbedtls_ssl_dtls_replay_update( &ssl );
  27. }
  28. /* Check new number */
  29. memcpy( ssl.in_ctr + 2, new->x, 6 );
  30. TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret );
  31. mbedtls_ssl_free( &ssl );
  32. mbedtls_ssl_config_free( &conf );
  33. }
  34. /* END_CASE */
  35. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
  36. void ssl_set_hostname_twice( char *hostname0, char *hostname1 )
  37. {
  38. mbedtls_ssl_context ssl;
  39. mbedtls_ssl_init( &ssl );
  40. TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 );
  41. TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 );
  42. mbedtls_ssl_free( &ssl );
  43. }
  44. /* END_CASE */
  45. /* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
  46. void ssl_cf_hmac( int hash )
  47. {
  48. /*
  49. * Test the function mbedtls_ssl_cf_hmac() against a reference
  50. * implementation.
  51. */
  52. mbedtls_md_context_t ctx, ref_ctx;
  53. const mbedtls_md_info_t *md_info;
  54. size_t out_len, block_size;
  55. size_t min_in_len, in_len, max_in_len, i;
  56. /* TLS additional data is 13 bytes (hence the "lucky 13" name) */
  57. unsigned char add_data[13];
  58. unsigned char ref_out[MBEDTLS_MD_MAX_SIZE];
  59. unsigned char *data = NULL;
  60. unsigned char *out = NULL;
  61. unsigned char rec_num = 0;
  62. mbedtls_md_init( &ctx );
  63. mbedtls_md_init( &ref_ctx );
  64. md_info = mbedtls_md_info_from_type( hash );
  65. TEST_ASSERT( md_info != NULL );
  66. out_len = mbedtls_md_get_size( md_info );
  67. TEST_ASSERT( out_len != 0 );
  68. block_size = hash == MBEDTLS_MD_SHA384 ? 128 : 64;
  69. /* Use allocated out buffer to catch overwrites */
  70. out = mbedtls_calloc( 1, out_len );
  71. TEST_ASSERT( out != NULL );
  72. /* Set up contexts with the given hash and a dummy key */
  73. TEST_ASSERT( 0 == mbedtls_md_setup( &ctx, md_info, 1 ) );
  74. TEST_ASSERT( 0 == mbedtls_md_setup( &ref_ctx, md_info, 1 ) );
  75. memset( ref_out, 42, sizeof( ref_out ) );
  76. TEST_ASSERT( 0 == mbedtls_md_hmac_starts( &ctx, ref_out, out_len ) );
  77. TEST_ASSERT( 0 == mbedtls_md_hmac_starts( &ref_ctx, ref_out, out_len ) );
  78. memset( ref_out, 0, sizeof( ref_out ) );
  79. /*
  80. * Test all possible lengths up to a point. The difference between
  81. * max_in_len and min_in_len is at most 255, and make sure they both vary
  82. * by at least one block size.
  83. */
  84. for( max_in_len = 0; max_in_len <= 255 + block_size; max_in_len++ )
  85. {
  86. /* Use allocated in buffer to catch overreads */
  87. data = mbedtls_calloc( 1, max_in_len );
  88. TEST_ASSERT( data != NULL || max_in_len == 0 );
  89. min_in_len = max_in_len > 255 ? max_in_len - 255 : 0;
  90. for( in_len = min_in_len; in_len <= max_in_len; in_len++ )
  91. {
  92. /* Set up dummy data and add_data */
  93. rec_num++;
  94. memset( add_data, rec_num, sizeof( add_data ) );
  95. for( i = 0; i < in_len; i++ )
  96. data[i] = ( i & 0xff ) ^ rec_num;
  97. /* Get the function's result */
  98. TEST_CF_SECRET( &in_len, sizeof( in_len ) );
  99. TEST_ASSERT( 0 == mbedtls_ssl_cf_hmac( &ctx, add_data, sizeof( add_data ),
  100. data, in_len,
  101. min_in_len, max_in_len,
  102. out ) );
  103. TEST_CF_PUBLIC( &in_len, sizeof( in_len ) );
  104. TEST_CF_PUBLIC( out, out_len );
  105. /* Compute the reference result */
  106. TEST_ASSERT( 0 == mbedtls_md_hmac_update( &ref_ctx, add_data,
  107. sizeof( add_data ) ) );
  108. TEST_ASSERT( 0 == mbedtls_md_hmac_update( &ref_ctx, data, in_len ) );
  109. TEST_ASSERT( 0 == mbedtls_md_hmac_finish( &ref_ctx, ref_out ) );
  110. TEST_ASSERT( 0 == mbedtls_md_hmac_reset( &ref_ctx ) );
  111. /* Compare */
  112. TEST_ASSERT( 0 == memcmp( out, ref_out, out_len ) );
  113. }
  114. mbedtls_free( data );
  115. data = NULL;
  116. }
  117. exit:
  118. mbedtls_md_free( &ref_ctx );
  119. mbedtls_md_free( &ctx );
  120. mbedtls_free( data );
  121. mbedtls_free( out );
  122. }
  123. /* END_CASE */
  124. /* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
  125. void ssl_cf_memcpy_offset( int offset_min, int offset_max, int len )
  126. {
  127. unsigned char *dst = NULL;
  128. unsigned char *src = NULL;
  129. size_t src_len = offset_max + len;
  130. size_t secret;
  131. dst = mbedtls_calloc( 1, len );
  132. TEST_ASSERT( dst != NULL );
  133. src = mbedtls_calloc( 1, src_len );
  134. TEST_ASSERT( src != NULL );
  135. /* Fill src in a way that we can detect if we copied the right bytes */
  136. rnd_std_rand( NULL, src, src_len );
  137. for( secret = offset_min; secret <= (size_t) offset_max; secret++ )
  138. {
  139. TEST_CF_SECRET( &secret, sizeof( secret ) );
  140. mbedtls_ssl_cf_memcpy_offset( dst, src, secret,
  141. offset_min, offset_max, len );
  142. TEST_CF_PUBLIC( &secret, sizeof( secret ) );
  143. TEST_CF_PUBLIC( dst, len );
  144. TEST_ASSERT( memcmp( dst, src + secret, len ) == 0 );
  145. }
  146. exit:
  147. mbedtls_free( dst );
  148. mbedtls_free( src );
  149. }
  150. /* END_CASE */