blob: 7b85c7633f804ee3045eb857cfd0b75520b6b88a [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{
148 int ret;
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
158 ret = mbedtls_ssl_check_record( ssl, tmp_buf, len );
159 if( ret != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE )
160 {
161 int ret_repeated;
162
163 /* Test-only: Make sure that mbedtls_ssl_check_record()
164 * doesn't alter state. */
165 memcpy( tmp_buf, buf, len ); /* Restore buffer */
166 ret_repeated = mbedtls_ssl_check_record( ssl, tmp_buf, len );
167 if( ret != ret_repeated )
168 {
169 mbedtls_printf( "mbedtls_ssl_check_record() returned inconsistent results.\n" );
Manuel Pégourié-Gonnard69c10a42021-07-06 12:05:23 +0200170 ret = -1;
171 goto cleanup;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100172 }
173
174 switch( ret )
175 {
176 case 0:
177 break;
178
179 case MBEDTLS_ERR_SSL_INVALID_RECORD:
180 if( opt.debug_level > 1 )
181 mbedtls_printf( "mbedtls_ssl_check_record() detected invalid record.\n" );
Manuel Pégourié-Gonnard69c10a42021-07-06 12:05:23 +0200182 ret = 0;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100183 break;
184
185 case MBEDTLS_ERR_SSL_INVALID_MAC:
186 if( opt.debug_level > 1 )
187 mbedtls_printf( "mbedtls_ssl_check_record() detected unauthentic record.\n" );
Manuel Pégourié-Gonnard69c10a42021-07-06 12:05:23 +0200188 ret = 0;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100189 break;
190
191 case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD:
192 if( opt.debug_level > 1 )
193 mbedtls_printf( "mbedtls_ssl_check_record() detected unexpected record.\n" );
Manuel Pégourié-Gonnard69c10a42021-07-06 12:05:23 +0200194 ret = 0;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100195 break;
196
197 default:
198 mbedtls_printf( "mbedtls_ssl_check_record() failed fatally with -%#04x.\n", (unsigned int) -ret );
Manuel Pégourié-Gonnard69c10a42021-07-06 12:05:23 +0200199 ret = -1;
200 goto cleanup;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100201 }
202
203 /* Regardless of the outcome, forward the record to the stack. */
204 }
205
Manuel Pégourié-Gonnard69c10a42021-07-06 12:05:23 +0200206cleanup:
Gilles Peskine504c1a32021-01-05 23:40:14 +0100207 mbedtls_free( tmp_buf );
208
Manuel Pégourié-Gonnard69c10a42021-07-06 12:05:23 +0200209 return( ret );
Gilles Peskine504c1a32021-01-05 23:40:14 +0100210}
Gilles Peskine504c1a32021-01-05 23:40:14 +0100211
212int recv_cb( void *ctx, unsigned char *buf, size_t len )
213{
214 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
215 size_t recv_len;
216 int ret;
217
218 if( opt.nbio == 2 )
219 ret = delayed_recv( io_ctx->net, buf, len );
220 else
221 ret = mbedtls_net_recv( io_ctx->net, buf, len );
222 if( ret < 0 )
223 return( ret );
224 recv_len = (size_t) ret;
225
226 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
227 {
228 /* Here's the place to do any datagram/record checking
229 * in between receiving the packet from the underlying
230 * transport and passing it on to the TLS stack. */
Gilles Peskine504c1a32021-01-05 23:40:14 +0100231 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
232 return( -1 );
Gilles Peskine504c1a32021-01-05 23:40:14 +0100233 }
234
235 return( (int) recv_len );
236}
237
238int recv_timeout_cb( void *ctx, unsigned char *buf, size_t len,
239 uint32_t timeout )
240{
241 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
242 int ret;
243 size_t recv_len;
244
245 ret = mbedtls_net_recv_timeout( io_ctx->net, buf, len, timeout );
246 if( ret < 0 )
247 return( ret );
248 recv_len = (size_t) ret;
249
250 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
251 {
252 /* Here's the place to do any datagram/record checking
253 * in between receiving the packet from the underlying
254 * transport and passing it on to the TLS stack. */
Gilles Peskine504c1a32021-01-05 23:40:14 +0100255 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
256 return( -1 );
Gilles Peskine504c1a32021-01-05 23:40:14 +0100257 }
258
259 return( (int) recv_len );
260}
261
262int send_cb( void *ctx, unsigned char const *buf, size_t len )
263{
264 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
265
266 if( opt.nbio == 2 )
267 return( delayed_send( io_ctx->net, buf, len ) );
268
269 return( mbedtls_net_send( io_ctx->net, buf, len ) );
270}
271
272#if defined(MBEDTLS_X509_CRT_PARSE_C)
273int ssl_sig_hashes_for_test[] = {
274#if defined(MBEDTLS_SHA512_C)
275 MBEDTLS_MD_SHA512,
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200276#endif
277#if defined(MBEDTLS_SHA384_C)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100278 MBEDTLS_MD_SHA384,
279#endif
280#if defined(MBEDTLS_SHA256_C)
281 MBEDTLS_MD_SHA256,
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200282#endif
283#if defined(MBEDTLS_SHA224_C)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100284 MBEDTLS_MD_SHA224,
285#endif
286#if defined(MBEDTLS_SHA1_C)
287 /* Allow SHA-1 as we use it extensively in tests. */
288 MBEDTLS_MD_SHA1,
289#endif
290 MBEDTLS_MD_NONE
291};
292#endif /* MBEDTLS_X509_CRT_PARSE_C */
Chris Jonese383fa62021-04-27 14:50:43 +0100293
294#if defined(MBEDTLS_X509_CRT_PARSE_C)
Chris Jonese383fa62021-04-27 14:50:43 +0100295/** Functionally equivalent to mbedtls_x509_crt_verify_info, see that function
296 * for more info.
297 */
298int x509_crt_verify_info( char *buf, size_t size, const char *prefix,
299 uint32_t flags )
300{
Chris Jonesfa1f9042021-04-28 10:04:05 +0100301#if !defined(MBEDTLS_X509_REMOVE_INFO)
Chris Jonese383fa62021-04-27 14:50:43 +0100302 return( mbedtls_x509_crt_verify_info( buf, size, prefix, flags ) );
303
304#else /* !MBEDTLS_X509_REMOVE_INFO */
305 int ret;
306 char *p = buf;
307 size_t n = size;
308
309#define X509_CRT_ERROR_INFO( err, err_str, info ) \
310 if( ( flags & err ) != 0 ) \
311 { \
312 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, info ); \
313 MBEDTLS_X509_SAFE_SNPRINTF; \
314 flags ^= err; \
315 }
316
317 MBEDTLS_X509_CRT_ERROR_INFO_LIST
318#undef X509_CRT_ERROR_INFO
319
320 if( flags != 0 )
321 {
322 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
323 "(this should not happen)\n", prefix );
324 MBEDTLS_X509_SAFE_SNPRINTF;
325 }
326
327 return( (int) ( size - n ) );
328#endif /* MBEDTLS_X509_REMOVE_INFO */
329}
330#endif /* MBEDTLS_X509_CRT_PARSE_C */