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