blob: d85f658900cf6632a5a8222f48300d8a57909b90 [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
162#if defined(MBEDTLS_SSL_RECORD_CHECKING)
163int ssl_check_record( mbedtls_ssl_context const *ssl,
164 unsigned char const *buf, size_t len )
165{
166 int ret;
167 unsigned char *tmp_buf;
168
169 /* Record checking may modify the input buffer,
170 * so make a copy. */
171 tmp_buf = mbedtls_calloc( 1, len );
172 if( tmp_buf == NULL )
173 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
174 memcpy( tmp_buf, buf, len );
175
176 ret = mbedtls_ssl_check_record( ssl, tmp_buf, len );
177 if( ret != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE )
178 {
179 int ret_repeated;
180
181 /* Test-only: Make sure that mbedtls_ssl_check_record()
182 * doesn't alter state. */
183 memcpy( tmp_buf, buf, len ); /* Restore buffer */
184 ret_repeated = mbedtls_ssl_check_record( ssl, tmp_buf, len );
185 if( ret != ret_repeated )
186 {
187 mbedtls_printf( "mbedtls_ssl_check_record() returned inconsistent results.\n" );
188 return( -1 );
189 }
190
191 switch( ret )
192 {
193 case 0:
194 break;
195
196 case MBEDTLS_ERR_SSL_INVALID_RECORD:
197 if( opt.debug_level > 1 )
198 mbedtls_printf( "mbedtls_ssl_check_record() detected invalid record.\n" );
199 break;
200
201 case MBEDTLS_ERR_SSL_INVALID_MAC:
202 if( opt.debug_level > 1 )
203 mbedtls_printf( "mbedtls_ssl_check_record() detected unauthentic record.\n" );
204 break;
205
206 case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD:
207 if( opt.debug_level > 1 )
208 mbedtls_printf( "mbedtls_ssl_check_record() detected unexpected record.\n" );
209 break;
210
211 default:
212 mbedtls_printf( "mbedtls_ssl_check_record() failed fatally with -%#04x.\n", (unsigned int) -ret );
213 return( -1 );
214 }
215
216 /* Regardless of the outcome, forward the record to the stack. */
217 }
218
219 mbedtls_free( tmp_buf );
220
221 return( 0 );
222}
223#endif /* MBEDTLS_SSL_RECORD_CHECKING */
224
225int recv_cb( void *ctx, unsigned char *buf, size_t len )
226{
227 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
228 size_t recv_len;
229 int ret;
230
231 if( opt.nbio == 2 )
232 ret = delayed_recv( io_ctx->net, buf, len );
233 else
234 ret = mbedtls_net_recv( io_ctx->net, buf, len );
235 if( ret < 0 )
236 return( ret );
237 recv_len = (size_t) ret;
238
239 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
240 {
241 /* Here's the place to do any datagram/record checking
242 * in between receiving the packet from the underlying
243 * transport and passing it on to the TLS stack. */
244#if defined(MBEDTLS_SSL_RECORD_CHECKING)
245 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
246 return( -1 );
247#endif /* MBEDTLS_SSL_RECORD_CHECKING */
248 }
249
250 return( (int) recv_len );
251}
252
253int recv_timeout_cb( void *ctx, unsigned char *buf, size_t len,
254 uint32_t timeout )
255{
256 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
257 int ret;
258 size_t recv_len;
259
260 ret = mbedtls_net_recv_timeout( io_ctx->net, buf, len, timeout );
261 if( ret < 0 )
262 return( ret );
263 recv_len = (size_t) ret;
264
265 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
266 {
267 /* Here's the place to do any datagram/record checking
268 * in between receiving the packet from the underlying
269 * transport and passing it on to the TLS stack. */
270#if defined(MBEDTLS_SSL_RECORD_CHECKING)
271 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
272 return( -1 );
273#endif /* MBEDTLS_SSL_RECORD_CHECKING */
274 }
275
276 return( (int) recv_len );
277}
278
279int send_cb( void *ctx, unsigned char const *buf, size_t len )
280{
281 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
282
283 if( opt.nbio == 2 )
284 return( delayed_send( io_ctx->net, buf, len ) );
285
286 return( mbedtls_net_send( io_ctx->net, buf, len ) );
287}
288
289#if defined(MBEDTLS_X509_CRT_PARSE_C)
290int ssl_sig_hashes_for_test[] = {
291#if defined(MBEDTLS_SHA512_C)
292 MBEDTLS_MD_SHA512,
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200293#endif
294#if defined(MBEDTLS_SHA384_C)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100295 MBEDTLS_MD_SHA384,
296#endif
297#if defined(MBEDTLS_SHA256_C)
298 MBEDTLS_MD_SHA256,
299 MBEDTLS_MD_SHA224,
300#endif
301#if defined(MBEDTLS_SHA1_C)
302 /* Allow SHA-1 as we use it extensively in tests. */
303 MBEDTLS_MD_SHA1,
304#endif
305 MBEDTLS_MD_NONE
306};
307#endif /* MBEDTLS_X509_CRT_PARSE_C */