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