blob: d30c36ea56f45c52a56edcae0b77f1a6a6e483d0 [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" );
170 return( -1 );
171 }
172
173 switch( ret )
174 {
175 case 0:
176 break;
177
178 case MBEDTLS_ERR_SSL_INVALID_RECORD:
179 if( opt.debug_level > 1 )
180 mbedtls_printf( "mbedtls_ssl_check_record() detected invalid record.\n" );
181 break;
182
183 case MBEDTLS_ERR_SSL_INVALID_MAC:
184 if( opt.debug_level > 1 )
185 mbedtls_printf( "mbedtls_ssl_check_record() detected unauthentic record.\n" );
186 break;
187
188 case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD:
189 if( opt.debug_level > 1 )
190 mbedtls_printf( "mbedtls_ssl_check_record() detected unexpected record.\n" );
191 break;
192
193 default:
194 mbedtls_printf( "mbedtls_ssl_check_record() failed fatally with -%#04x.\n", (unsigned int) -ret );
195 return( -1 );
196 }
197
198 /* Regardless of the outcome, forward the record to the stack. */
199 }
200
201 mbedtls_free( tmp_buf );
202
203 return( 0 );
204}
Gilles Peskine504c1a32021-01-05 23:40:14 +0100205
206int recv_cb( void *ctx, unsigned char *buf, size_t len )
207{
208 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
209 size_t recv_len;
210 int ret;
211
212 if( opt.nbio == 2 )
213 ret = delayed_recv( io_ctx->net, buf, len );
214 else
215 ret = mbedtls_net_recv( io_ctx->net, buf, len );
216 if( ret < 0 )
217 return( ret );
218 recv_len = (size_t) ret;
219
220 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
221 {
222 /* Here's the place to do any datagram/record checking
223 * in between receiving the packet from the underlying
224 * transport and passing it on to the TLS stack. */
Gilles Peskine504c1a32021-01-05 23:40:14 +0100225 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
226 return( -1 );
Gilles Peskine504c1a32021-01-05 23:40:14 +0100227 }
228
229 return( (int) recv_len );
230}
231
232int recv_timeout_cb( void *ctx, unsigned char *buf, size_t len,
233 uint32_t timeout )
234{
235 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
236 int ret;
237 size_t recv_len;
238
239 ret = mbedtls_net_recv_timeout( io_ctx->net, buf, len, timeout );
240 if( ret < 0 )
241 return( ret );
242 recv_len = (size_t) ret;
243
244 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
245 {
246 /* Here's the place to do any datagram/record checking
247 * in between receiving the packet from the underlying
248 * transport and passing it on to the TLS stack. */
Gilles Peskine504c1a32021-01-05 23:40:14 +0100249 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
250 return( -1 );
Gilles Peskine504c1a32021-01-05 23:40:14 +0100251 }
252
253 return( (int) recv_len );
254}
255
256int send_cb( void *ctx, unsigned char const *buf, size_t len )
257{
258 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
259
260 if( opt.nbio == 2 )
261 return( delayed_send( io_ctx->net, buf, len ) );
262
263 return( mbedtls_net_send( io_ctx->net, buf, len ) );
264}
265
266#if defined(MBEDTLS_X509_CRT_PARSE_C)
267int ssl_sig_hashes_for_test[] = {
268#if defined(MBEDTLS_SHA512_C)
269 MBEDTLS_MD_SHA512,
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200270#endif
271#if defined(MBEDTLS_SHA384_C)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100272 MBEDTLS_MD_SHA384,
273#endif
274#if defined(MBEDTLS_SHA256_C)
275 MBEDTLS_MD_SHA256,
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200276#endif
277#if defined(MBEDTLS_SHA224_C)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100278 MBEDTLS_MD_SHA224,
279#endif
280#if defined(MBEDTLS_SHA1_C)
281 /* Allow SHA-1 as we use it extensively in tests. */
282 MBEDTLS_MD_SHA1,
283#endif
284 MBEDTLS_MD_NONE
285};
286#endif /* MBEDTLS_X509_CRT_PARSE_C */
Chris Jonese383fa62021-04-27 14:50:43 +0100287
288#if defined(MBEDTLS_X509_CRT_PARSE_C)
Chris Jonese383fa62021-04-27 14:50:43 +0100289/** Functionally equivalent to mbedtls_x509_crt_verify_info, see that function
290 * for more info.
291 */
292int x509_crt_verify_info( char *buf, size_t size, const char *prefix,
293 uint32_t flags )
294{
Chris Jonesfa1f9042021-04-28 10:04:05 +0100295#if !defined(MBEDTLS_X509_REMOVE_INFO)
Chris Jonese383fa62021-04-27 14:50:43 +0100296 return( mbedtls_x509_crt_verify_info( buf, size, prefix, flags ) );
297
298#else /* !MBEDTLS_X509_REMOVE_INFO */
299 int ret;
300 char *p = buf;
301 size_t n = size;
302
303#define X509_CRT_ERROR_INFO( err, err_str, info ) \
304 if( ( flags & err ) != 0 ) \
305 { \
306 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, info ); \
307 MBEDTLS_X509_SAFE_SNPRINTF; \
308 flags ^= err; \
309 }
310
311 MBEDTLS_X509_CRT_ERROR_INFO_LIST
312#undef X509_CRT_ERROR_INFO
313
314 if( flags != 0 )
315 {
316 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
317 "(this should not happen)\n", prefix );
318 MBEDTLS_X509_SAFE_SNPRINTF;
319 }
320
321 return( (int) ( size - n ) );
322#endif /* MBEDTLS_X509_REMOVE_INFO */
323}
324#endif /* MBEDTLS_X509_CRT_PARSE_C */