blob: 6da0ba4a503e5dc3bc75eebe3003460556a7d14d [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)
28int eap_tls_key_derivation( void *p_expkey,
Hanno Beckerc4c38ca2021-05-24 10:57:07 +010029 mbedtls_ssl_key_export_type secret_type,
30 const unsigned char *secret,
31 size_t secret_len,
Gilles Peskine504c1a32021-01-05 23:40:14 +010032 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 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 )
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 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;
48
Gilles Peskine504c1a32021-01-05 23:40:14 +010049 return( 0 );
50}
51
52int nss_keylog_export( void *p_expkey,
Hanno Beckerc4c38ca2021-05-24 10:57:07 +010053 mbedtls_ssl_key_export_type secret_type,
54 const unsigned char *secret,
55 size_t secret_len,
Gilles Peskine504c1a32021-01-05 23:40:14 +010056 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 Peskine504c1a32021-01-05 23:40:14 +010062 size_t len = 0;
63 size_t j;
64 int ret = 0;
65
Hanno Beckerc4c38ca2021-05-24 10:57:07 +010066 /* 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 Peskine504c1a32021-01-05 23:40:14 +010070 ((void) p_expkey);
Gilles Peskine504c1a32021-01-05 23:40:14 +010071 ((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 Beckerc4c38ca2021-05-24 10:57:07 +010085 for( j = 0; j < secret_len; j++ )
Gilles Peskine504c1a32021-01-05 23:40:14 +010086 {
87 len += sprintf( nss_keylog_line + len,
Hanno Beckerc4c38ca2021-05-24 10:57:07 +010088 "%02x", secret[j] );
Gilles Peskine504c1a32021-01-05 23:40:14 +010089 }
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
119exit:
120 mbedtls_platform_zeroize( nss_keylog_line,
121 sizeof( nss_keylog_line ) );
122 return( ret );
123}
124
125#if defined( MBEDTLS_SSL_DTLS_SRTP )
126int dtls_srtp_key_derivation( void *p_expkey,
Hanno Beckerc4c38ca2021-05-24 10:57:07 +0100127 mbedtls_ssl_key_export_type secret_type,
128 const unsigned char *secret,
129 size_t secret_len,
Gilles Peskine504c1a32021-01-05 23:40:14 +0100130 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 Beckerc4c38ca2021-05-24 10:57:07 +0100136 /* 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 Peskine504c1a32021-01-05 23:40:14 +0100143 memcpy( keys->randbytes, client_random, 32 );
144 memcpy( keys->randbytes + 32, server_random, 32 );
145 keys->tls_prf_type = tls_prf_type;
146
Gilles Peskine504c1a32021-01-05 23:40:14 +0100147 return( 0 );
148}
149#endif /* MBEDTLS_SSL_DTLS_SRTP */
150
151#endif /* MBEDTLS_SSL_EXPORT_KEYS */
152
Gilles Peskine504c1a32021-01-05 23:40:14 +0100153int 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 Peskine504c1a32021-01-05 23:40:14 +0100213
214int 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 Peskine504c1a32021-01-05 23:40:14 +0100233 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
234 return( -1 );
Gilles Peskine504c1a32021-01-05 23:40:14 +0100235 }
236
237 return( (int) recv_len );
238}
239
240int 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 Peskine504c1a32021-01-05 23:40:14 +0100257 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
258 return( -1 );
Gilles Peskine504c1a32021-01-05 23:40:14 +0100259 }
260
261 return( (int) recv_len );
262}
263
264int 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)
275int ssl_sig_hashes_for_test[] = {
276#if defined(MBEDTLS_SHA512_C)
277 MBEDTLS_MD_SHA512,
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200278#endif
279#if defined(MBEDTLS_SHA384_C)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100280 MBEDTLS_MD_SHA384,
281#endif
282#if defined(MBEDTLS_SHA256_C)
283 MBEDTLS_MD_SHA256,
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200284#endif
285#if defined(MBEDTLS_SHA224_C)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100286 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 Jonese383fa62021-04-27 14:50:43 +0100295
296#if defined(MBEDTLS_X509_CRT_PARSE_C)
Chris Jonese383fa62021-04-27 14:50:43 +0100297/** Functionally equivalent to mbedtls_x509_crt_verify_info, see that function
298 * for more info.
299 */
300int x509_crt_verify_info( char *buf, size_t size, const char *prefix,
301 uint32_t flags )
302{
Chris Jonesfa1f9042021-04-28 10:04:05 +0100303#if !defined(MBEDTLS_X509_REMOVE_INFO)
Chris Jonese383fa62021-04-27 14:50:43 +0100304 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 */