ssl_test_common_source.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Common source code for SSL test programs. This file is included by
  3. * both ssl_client2.c and ssl_server2.c and is intended for source
  4. * code that is textually identical in both programs, but that cannot be
  5. * compiled separately because it refers to types or macros that are
  6. * different in the two programs, or because it would have an incomplete
  7. * type.
  8. *
  9. * This file is meant to be #include'd and cannot be compiled separately.
  10. *
  11. * Copyright The Mbed TLS Contributors
  12. * SPDX-License-Identifier: Apache-2.0
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  15. * not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  22. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. #if defined(MBEDTLS_SSL_EXPORT_KEYS)
  27. int eap_tls_key_derivation( void *p_expkey,
  28. const unsigned char *ms,
  29. const unsigned char *kb,
  30. size_t maclen,
  31. size_t keylen,
  32. size_t ivlen,
  33. const unsigned char client_random[32],
  34. const unsigned char server_random[32],
  35. mbedtls_tls_prf_types tls_prf_type )
  36. {
  37. eap_tls_keys *keys = (eap_tls_keys *)p_expkey;
  38. ( ( void ) kb );
  39. memcpy( keys->master_secret, ms, sizeof( keys->master_secret ) );
  40. memcpy( keys->randbytes, client_random, 32 );
  41. memcpy( keys->randbytes + 32, server_random, 32 );
  42. keys->tls_prf_type = tls_prf_type;
  43. if( opt.debug_level > 2 )
  44. {
  45. mbedtls_printf("exported maclen is %u\n", (unsigned)maclen);
  46. mbedtls_printf("exported keylen is %u\n", (unsigned)keylen);
  47. mbedtls_printf("exported ivlen is %u\n", (unsigned)ivlen);
  48. }
  49. return( 0 );
  50. }
  51. int nss_keylog_export( void *p_expkey,
  52. const unsigned char *ms,
  53. const unsigned char *kb,
  54. size_t maclen,
  55. size_t keylen,
  56. size_t ivlen,
  57. const unsigned char client_random[32],
  58. const unsigned char server_random[32],
  59. mbedtls_tls_prf_types tls_prf_type )
  60. {
  61. char nss_keylog_line[ 200 ];
  62. size_t const client_random_len = 32;
  63. size_t const master_secret_len = 48;
  64. size_t len = 0;
  65. size_t j;
  66. int ret = 0;
  67. ((void) p_expkey);
  68. ((void) kb);
  69. ((void) maclen);
  70. ((void) keylen);
  71. ((void) ivlen);
  72. ((void) server_random);
  73. ((void) tls_prf_type);
  74. len += sprintf( nss_keylog_line + len,
  75. "%s", "CLIENT_RANDOM " );
  76. for( j = 0; j < client_random_len; j++ )
  77. {
  78. len += sprintf( nss_keylog_line + len,
  79. "%02x", client_random[j] );
  80. }
  81. len += sprintf( nss_keylog_line + len, " " );
  82. for( j = 0; j < master_secret_len; j++ )
  83. {
  84. len += sprintf( nss_keylog_line + len,
  85. "%02x", ms[j] );
  86. }
  87. len += sprintf( nss_keylog_line + len, "\n" );
  88. nss_keylog_line[ len ] = '\0';
  89. mbedtls_printf( "\n" );
  90. mbedtls_printf( "---------------- NSS KEYLOG -----------------\n" );
  91. mbedtls_printf( "%s", nss_keylog_line );
  92. mbedtls_printf( "---------------------------------------------\n" );
  93. if( opt.nss_keylog_file != NULL )
  94. {
  95. FILE *f;
  96. if( ( f = fopen( opt.nss_keylog_file, "a" ) ) == NULL )
  97. {
  98. ret = -1;
  99. goto exit;
  100. }
  101. if( fwrite( nss_keylog_line, 1, len, f ) != len )
  102. {
  103. ret = -1;
  104. fclose( f );
  105. goto exit;
  106. }
  107. fclose( f );
  108. }
  109. exit:
  110. mbedtls_platform_zeroize( nss_keylog_line,
  111. sizeof( nss_keylog_line ) );
  112. return( ret );
  113. }
  114. #if defined( MBEDTLS_SSL_DTLS_SRTP )
  115. int dtls_srtp_key_derivation( void *p_expkey,
  116. const unsigned char *ms,
  117. const unsigned char *kb,
  118. size_t maclen,
  119. size_t keylen,
  120. size_t ivlen,
  121. const unsigned char client_random[32],
  122. const unsigned char server_random[32],
  123. mbedtls_tls_prf_types tls_prf_type )
  124. {
  125. dtls_srtp_keys *keys = (dtls_srtp_keys *)p_expkey;
  126. ( ( void ) kb );
  127. memcpy( keys->master_secret, ms, sizeof( keys->master_secret ) );
  128. memcpy( keys->randbytes, client_random, 32 );
  129. memcpy( keys->randbytes + 32, server_random, 32 );
  130. keys->tls_prf_type = tls_prf_type;
  131. if( opt.debug_level > 2 )
  132. {
  133. mbedtls_printf( "exported maclen is %u\n", (unsigned) maclen );
  134. mbedtls_printf( "exported keylen is %u\n", (unsigned) keylen );
  135. mbedtls_printf( "exported ivlen is %u\n", (unsigned) ivlen );
  136. }
  137. return( 0 );
  138. }
  139. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  140. #endif /* MBEDTLS_SSL_EXPORT_KEYS */
  141. #if defined(MBEDTLS_SSL_RECORD_CHECKING)
  142. int ssl_check_record( mbedtls_ssl_context const *ssl,
  143. unsigned char const *buf, size_t len )
  144. {
  145. int my_ret = 0, ret_cr1, ret_cr2;
  146. unsigned char *tmp_buf;
  147. /* Record checking may modify the input buffer,
  148. * so make a copy. */
  149. tmp_buf = mbedtls_calloc( 1, len );
  150. if( tmp_buf == NULL )
  151. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  152. memcpy( tmp_buf, buf, len );
  153. ret_cr1 = mbedtls_ssl_check_record( ssl, tmp_buf, len );
  154. if( ret_cr1 != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE )
  155. {
  156. /* Test-only: Make sure that mbedtls_ssl_check_record()
  157. * doesn't alter state. */
  158. memcpy( tmp_buf, buf, len ); /* Restore buffer */
  159. ret_cr2 = mbedtls_ssl_check_record( ssl, tmp_buf, len );
  160. if( ret_cr2 != ret_cr1 )
  161. {
  162. mbedtls_printf( "mbedtls_ssl_check_record() returned inconsistent results.\n" );
  163. my_ret = -1;
  164. goto cleanup;
  165. }
  166. switch( ret_cr1 )
  167. {
  168. case 0:
  169. break;
  170. case MBEDTLS_ERR_SSL_INVALID_RECORD:
  171. if( opt.debug_level > 1 )
  172. mbedtls_printf( "mbedtls_ssl_check_record() detected invalid record.\n" );
  173. break;
  174. case MBEDTLS_ERR_SSL_INVALID_MAC:
  175. if( opt.debug_level > 1 )
  176. mbedtls_printf( "mbedtls_ssl_check_record() detected unauthentic record.\n" );
  177. break;
  178. case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD:
  179. if( opt.debug_level > 1 )
  180. mbedtls_printf( "mbedtls_ssl_check_record() detected unexpected record.\n" );
  181. break;
  182. default:
  183. mbedtls_printf( "mbedtls_ssl_check_record() failed fatally with -%#04x.\n", (unsigned int) -ret_cr1 );
  184. my_ret = -1;
  185. goto cleanup;
  186. }
  187. /* Regardless of the outcome, forward the record to the stack. */
  188. }
  189. cleanup:
  190. mbedtls_free( tmp_buf );
  191. return( my_ret );
  192. }
  193. #endif /* MBEDTLS_SSL_RECORD_CHECKING */
  194. int recv_cb( void *ctx, unsigned char *buf, size_t len )
  195. {
  196. io_ctx_t *io_ctx = (io_ctx_t*) ctx;
  197. size_t recv_len;
  198. int ret;
  199. if( opt.nbio == 2 )
  200. ret = delayed_recv( io_ctx->net, buf, len );
  201. else
  202. ret = mbedtls_net_recv( io_ctx->net, buf, len );
  203. if( ret < 0 )
  204. return( ret );
  205. recv_len = (size_t) ret;
  206. if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  207. {
  208. /* Here's the place to do any datagram/record checking
  209. * in between receiving the packet from the underlying
  210. * transport and passing it on to the TLS stack. */
  211. #if defined(MBEDTLS_SSL_RECORD_CHECKING)
  212. if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
  213. return( -1 );
  214. #endif /* MBEDTLS_SSL_RECORD_CHECKING */
  215. }
  216. return( (int) recv_len );
  217. }
  218. int recv_timeout_cb( void *ctx, unsigned char *buf, size_t len,
  219. uint32_t timeout )
  220. {
  221. io_ctx_t *io_ctx = (io_ctx_t*) ctx;
  222. int ret;
  223. size_t recv_len;
  224. ret = mbedtls_net_recv_timeout( io_ctx->net, buf, len, timeout );
  225. if( ret < 0 )
  226. return( ret );
  227. recv_len = (size_t) ret;
  228. if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  229. {
  230. /* Here's the place to do any datagram/record checking
  231. * in between receiving the packet from the underlying
  232. * transport and passing it on to the TLS stack. */
  233. #if defined(MBEDTLS_SSL_RECORD_CHECKING)
  234. if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
  235. return( -1 );
  236. #endif /* MBEDTLS_SSL_RECORD_CHECKING */
  237. }
  238. return( (int) recv_len );
  239. }
  240. int send_cb( void *ctx, unsigned char const *buf, size_t len )
  241. {
  242. io_ctx_t *io_ctx = (io_ctx_t*) ctx;
  243. if( opt.nbio == 2 )
  244. return( delayed_send( io_ctx->net, buf, len ) );
  245. return( mbedtls_net_send( io_ctx->net, buf, len ) );
  246. }
  247. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  248. int ssl_sig_hashes_for_test[] = {
  249. #if defined(MBEDTLS_SHA512_C)
  250. MBEDTLS_MD_SHA512,
  251. MBEDTLS_MD_SHA384,
  252. #endif
  253. #if defined(MBEDTLS_SHA256_C)
  254. MBEDTLS_MD_SHA256,
  255. MBEDTLS_MD_SHA224,
  256. #endif
  257. #if defined(MBEDTLS_SHA1_C)
  258. /* Allow SHA-1 as we use it extensively in tests. */
  259. MBEDTLS_MD_SHA1,
  260. #endif
  261. MBEDTLS_MD_NONE
  262. };
  263. #endif /* MBEDTLS_X509_CRT_PARSE_C */