Gilles Peskine | 0d980b8 | 2021-01-05 23:34:27 +0100 | [diff] [blame] | 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 | */ |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 26 | |
| 27 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 28 | void eap_tls_key_derivation( void *p_expkey, |
| 29 | mbedtls_ssl_key_export_type secret_type, |
| 30 | const unsigned char *secret, |
| 31 | size_t secret_len, |
| 32 | const unsigned char client_random[32], |
| 33 | const unsigned char server_random[32], |
| 34 | mbedtls_tls_prf_types tls_prf_type ) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 35 | { |
| 36 | eap_tls_keys *keys = (eap_tls_keys *)p_expkey; |
| 37 | |
Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 38 | /* We're only interested in the TLS 1.2 master secret */ |
| 39 | if( secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET ) |
Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 40 | return; |
Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 41 | if( secret_len != sizeof( keys->master_secret ) ) |
Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 42 | return; |
Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 43 | |
| 44 | memcpy( keys->master_secret, secret, sizeof( keys->master_secret ) ); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 45 | memcpy( keys->randbytes, client_random, 32 ); |
| 46 | memcpy( keys->randbytes + 32, server_random, 32 ); |
| 47 | keys->tls_prf_type = tls_prf_type; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 48 | } |
| 49 | |
Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 50 | void nss_keylog_export( void *p_expkey, |
| 51 | mbedtls_ssl_key_export_type secret_type, |
| 52 | const unsigned char *secret, |
| 53 | size_t secret_len, |
| 54 | const unsigned char client_random[32], |
| 55 | const unsigned char server_random[32], |
| 56 | mbedtls_tls_prf_types tls_prf_type ) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 57 | { |
| 58 | char nss_keylog_line[ 200 ]; |
| 59 | size_t const client_random_len = 32; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 60 | size_t len = 0; |
| 61 | size_t j; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 62 | |
Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 63 | /* We're only interested in the TLS 1.2 master secret */ |
| 64 | if( secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET ) |
Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 65 | return; |
Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 66 | |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 67 | ((void) p_expkey); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 68 | ((void) server_random); |
| 69 | ((void) tls_prf_type); |
| 70 | |
| 71 | len += sprintf( nss_keylog_line + len, |
| 72 | "%s", "CLIENT_RANDOM " ); |
| 73 | |
| 74 | for( j = 0; j < client_random_len; j++ ) |
| 75 | { |
| 76 | len += sprintf( nss_keylog_line + len, |
| 77 | "%02x", client_random[j] ); |
| 78 | } |
| 79 | |
| 80 | len += sprintf( nss_keylog_line + len, " " ); |
| 81 | |
Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 82 | for( j = 0; j < secret_len; j++ ) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 83 | { |
| 84 | len += sprintf( nss_keylog_line + len, |
Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 85 | "%02x", secret[j] ); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | len += sprintf( nss_keylog_line + len, "\n" ); |
| 89 | nss_keylog_line[ len ] = '\0'; |
| 90 | |
| 91 | mbedtls_printf( "\n" ); |
| 92 | mbedtls_printf( "---------------- NSS KEYLOG -----------------\n" ); |
| 93 | mbedtls_printf( "%s", nss_keylog_line ); |
| 94 | mbedtls_printf( "---------------------------------------------\n" ); |
| 95 | |
| 96 | if( opt.nss_keylog_file != NULL ) |
| 97 | { |
| 98 | FILE *f; |
| 99 | |
| 100 | if( ( f = fopen( opt.nss_keylog_file, "a" ) ) == NULL ) |
| 101 | { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 102 | goto exit; |
| 103 | } |
| 104 | |
| 105 | if( fwrite( nss_keylog_line, 1, len, f ) != len ) |
| 106 | { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 107 | fclose( f ); |
| 108 | goto exit; |
| 109 | } |
| 110 | |
| 111 | fclose( f ); |
| 112 | } |
| 113 | |
| 114 | exit: |
| 115 | mbedtls_platform_zeroize( nss_keylog_line, |
| 116 | sizeof( nss_keylog_line ) ); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | #if defined( MBEDTLS_SSL_DTLS_SRTP ) |
Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 120 | void dtls_srtp_key_derivation( void *p_expkey, |
| 121 | mbedtls_ssl_key_export_type secret_type, |
| 122 | const unsigned char *secret, |
| 123 | size_t secret_len, |
| 124 | const unsigned char client_random[32], |
| 125 | const unsigned char server_random[32], |
| 126 | mbedtls_tls_prf_types tls_prf_type ) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 127 | { |
| 128 | dtls_srtp_keys *keys = (dtls_srtp_keys *)p_expkey; |
| 129 | |
Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 130 | /* We're only interested in the TLS 1.2 master secret */ |
| 131 | if( secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET ) |
Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 132 | return; |
Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 133 | if( secret_len != sizeof( keys->master_secret ) ) |
Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 134 | return; |
Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 135 | |
| 136 | memcpy( keys->master_secret, secret, sizeof( keys->master_secret ) ); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 137 | memcpy( keys->randbytes, client_random, 32 ); |
| 138 | memcpy( keys->randbytes + 32, server_random, 32 ); |
| 139 | keys->tls_prf_type = tls_prf_type; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 140 | } |
| 141 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 142 | |
| 143 | #endif /* MBEDTLS_SSL_EXPORT_KEYS */ |
| 144 | |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 145 | int ssl_check_record( mbedtls_ssl_context const *ssl, |
| 146 | unsigned char const *buf, size_t len ) |
| 147 | { |
Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame^] | 148 | int my_ret = 0, ret_cr1, ret_cr2; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 149 | unsigned char *tmp_buf; |
| 150 | |
| 151 | /* Record checking may modify the input buffer, |
| 152 | * so make a copy. */ |
| 153 | tmp_buf = mbedtls_calloc( 1, len ); |
| 154 | if( tmp_buf == NULL ) |
| 155 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 156 | memcpy( tmp_buf, buf, len ); |
| 157 | |
Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame^] | 158 | ret_cr1 = mbedtls_ssl_check_record( ssl, tmp_buf, len ); |
| 159 | if( ret_cr1 != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 160 | { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 161 | /* Test-only: Make sure that mbedtls_ssl_check_record() |
| 162 | * doesn't alter state. */ |
| 163 | memcpy( tmp_buf, buf, len ); /* Restore buffer */ |
Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame^] | 164 | ret_cr2 = mbedtls_ssl_check_record( ssl, tmp_buf, len ); |
| 165 | if( ret_cr2 != ret_cr1 ) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 166 | { |
| 167 | mbedtls_printf( "mbedtls_ssl_check_record() returned inconsistent results.\n" ); |
Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame^] | 168 | my_ret = -1; |
Manuel Pégourié-Gonnard | 69c10a4 | 2021-07-06 12:05:23 +0200 | [diff] [blame] | 169 | goto cleanup; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 170 | } |
| 171 | |
Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame^] | 172 | switch( ret_cr1 ) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 173 | { |
| 174 | case 0: |
| 175 | break; |
| 176 | |
| 177 | case MBEDTLS_ERR_SSL_INVALID_RECORD: |
| 178 | if( opt.debug_level > 1 ) |
| 179 | mbedtls_printf( "mbedtls_ssl_check_record() detected invalid record.\n" ); |
| 180 | break; |
| 181 | |
| 182 | case MBEDTLS_ERR_SSL_INVALID_MAC: |
| 183 | if( opt.debug_level > 1 ) |
| 184 | mbedtls_printf( "mbedtls_ssl_check_record() detected unauthentic record.\n" ); |
| 185 | break; |
| 186 | |
| 187 | case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD: |
| 188 | if( opt.debug_level > 1 ) |
| 189 | mbedtls_printf( "mbedtls_ssl_check_record() detected unexpected record.\n" ); |
| 190 | break; |
| 191 | |
| 192 | default: |
Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame^] | 193 | mbedtls_printf( "mbedtls_ssl_check_record() failed fatally with -%#04x.\n", (unsigned int) -ret_cr1 ); |
| 194 | my_ret = -1; |
Manuel Pégourié-Gonnard | 69c10a4 | 2021-07-06 12:05:23 +0200 | [diff] [blame] | 195 | goto cleanup; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* Regardless of the outcome, forward the record to the stack. */ |
| 199 | } |
| 200 | |
Manuel Pégourié-Gonnard | 69c10a4 | 2021-07-06 12:05:23 +0200 | [diff] [blame] | 201 | cleanup: |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 202 | mbedtls_free( tmp_buf ); |
| 203 | |
Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame^] | 204 | return( my_ret ); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 205 | } |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 206 | |
| 207 | int recv_cb( void *ctx, unsigned char *buf, size_t len ) |
| 208 | { |
| 209 | io_ctx_t *io_ctx = (io_ctx_t*) ctx; |
| 210 | size_t recv_len; |
| 211 | int ret; |
| 212 | |
| 213 | if( opt.nbio == 2 ) |
| 214 | ret = delayed_recv( io_ctx->net, buf, len ); |
| 215 | else |
| 216 | ret = mbedtls_net_recv( io_ctx->net, buf, len ); |
| 217 | if( ret < 0 ) |
| 218 | return( ret ); |
| 219 | recv_len = (size_t) ret; |
| 220 | |
| 221 | if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 222 | { |
| 223 | /* Here's the place to do any datagram/record checking |
| 224 | * in between receiving the packet from the underlying |
| 225 | * transport and passing it on to the TLS stack. */ |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 226 | if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 ) |
| 227 | return( -1 ); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | return( (int) recv_len ); |
| 231 | } |
| 232 | |
| 233 | int recv_timeout_cb( void *ctx, unsigned char *buf, size_t len, |
| 234 | uint32_t timeout ) |
| 235 | { |
| 236 | io_ctx_t *io_ctx = (io_ctx_t*) ctx; |
| 237 | int ret; |
| 238 | size_t recv_len; |
| 239 | |
| 240 | ret = mbedtls_net_recv_timeout( io_ctx->net, buf, len, timeout ); |
| 241 | if( ret < 0 ) |
| 242 | return( ret ); |
| 243 | recv_len = (size_t) ret; |
| 244 | |
| 245 | if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 246 | { |
| 247 | /* Here's the place to do any datagram/record checking |
| 248 | * in between receiving the packet from the underlying |
| 249 | * transport and passing it on to the TLS stack. */ |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 250 | if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 ) |
| 251 | return( -1 ); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | return( (int) recv_len ); |
| 255 | } |
| 256 | |
| 257 | int send_cb( void *ctx, unsigned char const *buf, size_t len ) |
| 258 | { |
| 259 | io_ctx_t *io_ctx = (io_ctx_t*) ctx; |
| 260 | |
| 261 | if( opt.nbio == 2 ) |
| 262 | return( delayed_send( io_ctx->net, buf, len ) ); |
| 263 | |
| 264 | return( mbedtls_net_send( io_ctx->net, buf, len ) ); |
| 265 | } |
| 266 | |
| 267 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 268 | int ssl_sig_hashes_for_test[] = { |
| 269 | #if defined(MBEDTLS_SHA512_C) |
| 270 | MBEDTLS_MD_SHA512, |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 271 | #endif |
| 272 | #if defined(MBEDTLS_SHA384_C) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 273 | MBEDTLS_MD_SHA384, |
| 274 | #endif |
| 275 | #if defined(MBEDTLS_SHA256_C) |
| 276 | MBEDTLS_MD_SHA256, |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 277 | #endif |
| 278 | #if defined(MBEDTLS_SHA224_C) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 279 | MBEDTLS_MD_SHA224, |
| 280 | #endif |
| 281 | #if defined(MBEDTLS_SHA1_C) |
| 282 | /* Allow SHA-1 as we use it extensively in tests. */ |
| 283 | MBEDTLS_MD_SHA1, |
| 284 | #endif |
| 285 | MBEDTLS_MD_NONE |
| 286 | }; |
| 287 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Chris Jones | e383fa6 | 2021-04-27 14:50:43 +0100 | [diff] [blame] | 288 | |
| 289 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Chris Jones | e383fa6 | 2021-04-27 14:50:43 +0100 | [diff] [blame] | 290 | /** Functionally equivalent to mbedtls_x509_crt_verify_info, see that function |
| 291 | * for more info. |
| 292 | */ |
| 293 | int x509_crt_verify_info( char *buf, size_t size, const char *prefix, |
| 294 | uint32_t flags ) |
| 295 | { |
Chris Jones | fa1f904 | 2021-04-28 10:04:05 +0100 | [diff] [blame] | 296 | #if !defined(MBEDTLS_X509_REMOVE_INFO) |
Chris Jones | e383fa6 | 2021-04-27 14:50:43 +0100 | [diff] [blame] | 297 | return( mbedtls_x509_crt_verify_info( buf, size, prefix, flags ) ); |
| 298 | |
| 299 | #else /* !MBEDTLS_X509_REMOVE_INFO */ |
| 300 | int ret; |
| 301 | char *p = buf; |
| 302 | size_t n = size; |
| 303 | |
| 304 | #define X509_CRT_ERROR_INFO( err, err_str, info ) \ |
| 305 | if( ( flags & err ) != 0 ) \ |
| 306 | { \ |
| 307 | ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, info ); \ |
| 308 | MBEDTLS_X509_SAFE_SNPRINTF; \ |
| 309 | flags ^= err; \ |
| 310 | } |
| 311 | |
| 312 | MBEDTLS_X509_CRT_ERROR_INFO_LIST |
| 313 | #undef X509_CRT_ERROR_INFO |
| 314 | |
| 315 | if( flags != 0 ) |
| 316 | { |
| 317 | ret = mbedtls_snprintf( p, n, "%sUnknown reason " |
| 318 | "(this should not happen)\n", prefix ); |
| 319 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 320 | } |
| 321 | |
| 322 | return( (int) ( size - n ) ); |
| 323 | #endif /* MBEDTLS_X509_REMOVE_INFO */ |
| 324 | } |
| 325 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |