blob: 6ec4171a890e8ce03d42a8f8d3faeedf01b83273 [file] [log] [blame]
Gilles Peskine0d980b82021-01-05 23:34:27 +01001/*
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 Peskine504c1a32021-01-05 23:40:14 +010026
27#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Hanno Becker296fefe2021-06-21 09:32:27 +010028void 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 Peskine504c1a32021-01-05 23:40:14 +010035{
36 eap_tls_keys *keys = (eap_tls_keys *)p_expkey;
37
Hanno Beckerc4c38ca2021-05-24 10:57:07 +010038 /* We're only interested in the TLS 1.2 master secret */
39 if( secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET )
Hanno Becker296fefe2021-06-21 09:32:27 +010040 return;
Hanno Beckerc4c38ca2021-05-24 10:57:07 +010041 if( secret_len != sizeof( keys->master_secret ) )
Hanno Becker296fefe2021-06-21 09:32:27 +010042 return;
Hanno Beckerc4c38ca2021-05-24 10:57:07 +010043
44 memcpy( keys->master_secret, secret, sizeof( keys->master_secret ) );
Gilles Peskine504c1a32021-01-05 23:40:14 +010045 memcpy( keys->randbytes, client_random, 32 );
46 memcpy( keys->randbytes + 32, server_random, 32 );
47 keys->tls_prf_type = tls_prf_type;
Gilles Peskine504c1a32021-01-05 23:40:14 +010048}
49
Hanno Becker296fefe2021-06-21 09:32:27 +010050void 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 Peskine504c1a32021-01-05 23:40:14 +010057{
58 char nss_keylog_line[ 200 ];
59 size_t const client_random_len = 32;
Gilles Peskine504c1a32021-01-05 23:40:14 +010060 size_t len = 0;
61 size_t j;
Gilles Peskine504c1a32021-01-05 23:40:14 +010062
Hanno Beckerc4c38ca2021-05-24 10:57:07 +010063 /* We're only interested in the TLS 1.2 master secret */
64 if( secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET )
Hanno Becker296fefe2021-06-21 09:32:27 +010065 return;
Hanno Beckerc4c38ca2021-05-24 10:57:07 +010066
Gilles Peskine504c1a32021-01-05 23:40:14 +010067 ((void) p_expkey);
Gilles Peskine504c1a32021-01-05 23:40:14 +010068 ((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 Beckerc4c38ca2021-05-24 10:57:07 +010082 for( j = 0; j < secret_len; j++ )
Gilles Peskine504c1a32021-01-05 23:40:14 +010083 {
84 len += sprintf( nss_keylog_line + len,
Hanno Beckerc4c38ca2021-05-24 10:57:07 +010085 "%02x", secret[j] );
Gilles Peskine504c1a32021-01-05 23:40:14 +010086 }
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 Peskine504c1a32021-01-05 23:40:14 +0100102 goto exit;
103 }
104
105 if( fwrite( nss_keylog_line, 1, len, f ) != len )
106 {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100107 fclose( f );
108 goto exit;
109 }
110
111 fclose( f );
112 }
113
114exit:
115 mbedtls_platform_zeroize( nss_keylog_line,
116 sizeof( nss_keylog_line ) );
Gilles Peskine504c1a32021-01-05 23:40:14 +0100117}
118
119#if defined( MBEDTLS_SSL_DTLS_SRTP )
Hanno Becker296fefe2021-06-21 09:32:27 +0100120void 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 Peskine504c1a32021-01-05 23:40:14 +0100127{
128 dtls_srtp_keys *keys = (dtls_srtp_keys *)p_expkey;
129
Hanno Beckerc4c38ca2021-05-24 10:57:07 +0100130 /* We're only interested in the TLS 1.2 master secret */
131 if( secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET )
Hanno Becker296fefe2021-06-21 09:32:27 +0100132 return;
Hanno Beckerc4c38ca2021-05-24 10:57:07 +0100133 if( secret_len != sizeof( keys->master_secret ) )
Hanno Becker296fefe2021-06-21 09:32:27 +0100134 return;
Hanno Beckerc4c38ca2021-05-24 10:57:07 +0100135
136 memcpy( keys->master_secret, secret, sizeof( keys->master_secret ) );
Gilles Peskine504c1a32021-01-05 23:40:14 +0100137 memcpy( keys->randbytes, client_random, 32 );
138 memcpy( keys->randbytes + 32, server_random, 32 );
139 keys->tls_prf_type = tls_prf_type;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100140}
141#endif /* MBEDTLS_SSL_DTLS_SRTP */
142
143#endif /* MBEDTLS_SSL_EXPORT_KEYS */
144
Gilles Peskine504c1a32021-01-05 23:40:14 +0100145int ssl_check_record( mbedtls_ssl_context const *ssl,
146 unsigned char const *buf, size_t len )
147{
Manuel Pégourié-Gonnarde5306f62021-07-07 10:48:26 +0200148 int my_ret = 0, ret_cr1, ret_cr2;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100149 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é-Gonnarde5306f62021-07-07 10:48:26 +0200158 ret_cr1 = mbedtls_ssl_check_record( ssl, tmp_buf, len );
159 if( ret_cr1 != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE )
Gilles Peskine504c1a32021-01-05 23:40:14 +0100160 {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100161 /* 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é-Gonnarde5306f62021-07-07 10:48:26 +0200164 ret_cr2 = mbedtls_ssl_check_record( ssl, tmp_buf, len );
165 if( ret_cr2 != ret_cr1 )
Gilles Peskine504c1a32021-01-05 23:40:14 +0100166 {
167 mbedtls_printf( "mbedtls_ssl_check_record() returned inconsistent results.\n" );
Manuel Pégourié-Gonnarde5306f62021-07-07 10:48:26 +0200168 my_ret = -1;
Manuel Pégourié-Gonnard69c10a42021-07-06 12:05:23 +0200169 goto cleanup;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100170 }
171
Manuel Pégourié-Gonnarde5306f62021-07-07 10:48:26 +0200172 switch( ret_cr1 )
Gilles Peskine504c1a32021-01-05 23:40:14 +0100173 {
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é-Gonnarde5306f62021-07-07 10:48:26 +0200193 mbedtls_printf( "mbedtls_ssl_check_record() failed fatally with -%#04x.\n", (unsigned int) -ret_cr1 );
194 my_ret = -1;
Manuel Pégourié-Gonnard69c10a42021-07-06 12:05:23 +0200195 goto cleanup;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100196 }
197
198 /* Regardless of the outcome, forward the record to the stack. */
199 }
200
Manuel Pégourié-Gonnard69c10a42021-07-06 12:05:23 +0200201cleanup:
Gilles Peskine504c1a32021-01-05 23:40:14 +0100202 mbedtls_free( tmp_buf );
203
Manuel Pégourié-Gonnarde5306f62021-07-07 10:48:26 +0200204 return( my_ret );
Gilles Peskine504c1a32021-01-05 23:40:14 +0100205}
Gilles Peskine504c1a32021-01-05 23:40:14 +0100206
207int 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 Peskine504c1a32021-01-05 23:40:14 +0100226 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
227 return( -1 );
Gilles Peskine504c1a32021-01-05 23:40:14 +0100228 }
229
230 return( (int) recv_len );
231}
232
233int 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 Peskine504c1a32021-01-05 23:40:14 +0100250 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
251 return( -1 );
Gilles Peskine504c1a32021-01-05 23:40:14 +0100252 }
253
254 return( (int) recv_len );
255}
256
257int 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)
268int ssl_sig_hashes_for_test[] = {
269#if defined(MBEDTLS_SHA512_C)
270 MBEDTLS_MD_SHA512,
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200271#endif
272#if defined(MBEDTLS_SHA384_C)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100273 MBEDTLS_MD_SHA384,
274#endif
275#if defined(MBEDTLS_SHA256_C)
276 MBEDTLS_MD_SHA256,
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200277#endif
278#if defined(MBEDTLS_SHA224_C)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100279 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 Jonese383fa62021-04-27 14:50:43 +0100288
289#if defined(MBEDTLS_X509_CRT_PARSE_C)
Chris Jonese383fa62021-04-27 14:50:43 +0100290/** Functionally equivalent to mbedtls_x509_crt_verify_info, see that function
291 * for more info.
292 */
293int x509_crt_verify_info( char *buf, size_t size, const char *prefix,
294 uint32_t flags )
295{
Chris Jonesfa1f9042021-04-28 10:04:05 +0100296#if !defined(MBEDTLS_X509_REMOVE_INFO)
Chris Jonese383fa62021-04-27 14:50:43 +0100297 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 */