1 /*
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 */
26
27 #if defined(MBEDTLS_SSL_EXPORT_KEYS)
eap_tls_key_derivation(void * p_expkey,const unsigned char * ms,const unsigned char * kb,size_t maclen,size_t keylen,size_t ivlen,const unsigned char client_random[32],const unsigned char server_random[32],mbedtls_tls_prf_types tls_prf_type)28 int 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
nss_keylog_export(void * p_expkey,const unsigned char * ms,const unsigned char * kb,size_t maclen,size_t keylen,size_t ivlen,const unsigned char client_random[32],const unsigned char server_random[32],mbedtls_tls_prf_types tls_prf_type)55 int 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
125 exit:
126 mbedtls_platform_zeroize( nss_keylog_line,
127 sizeof( nss_keylog_line ) );
128 return( ret );
129 }
130
131 #if defined( MBEDTLS_SSL_DTLS_SRTP )
dtls_srtp_key_derivation(void * p_expkey,const unsigned char * ms,const unsigned char * kb,size_t maclen,size_t keylen,size_t ivlen,const unsigned char client_random[32],const unsigned char server_random[32],mbedtls_tls_prf_types tls_prf_type)132 int 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)
ssl_check_record(mbedtls_ssl_context const * ssl,unsigned char const * buf,size_t len)163 int ssl_check_record( mbedtls_ssl_context const *ssl,
164 unsigned char const *buf, size_t len )
165 {
166 int my_ret = 0, ret_cr1, ret_cr2;
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_cr1 = mbedtls_ssl_check_record( ssl, tmp_buf, len );
177 if( ret_cr1 != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE )
178 {
179 /* Test-only: Make sure that mbedtls_ssl_check_record()
180 * doesn't alter state. */
181 memcpy( tmp_buf, buf, len ); /* Restore buffer */
182 ret_cr2 = mbedtls_ssl_check_record( ssl, tmp_buf, len );
183 if( ret_cr2 != ret_cr1 )
184 {
185 mbedtls_printf( "mbedtls_ssl_check_record() returned inconsistent results.\n" );
186 my_ret = -1;
187 goto cleanup;
188 }
189
190 switch( ret_cr1 )
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_cr1 );
212 my_ret = -1;
213 goto cleanup;
214 }
215
216 /* Regardless of the outcome, forward the record to the stack. */
217 }
218
219 cleanup:
220 mbedtls_free( tmp_buf );
221
222 return( my_ret );
223 }
224 #endif /* MBEDTLS_SSL_RECORD_CHECKING */
225
recv_cb(void * ctx,unsigned char * buf,size_t len)226 int recv_cb( void *ctx, unsigned char *buf, size_t len )
227 {
228 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
229 size_t recv_len;
230 int ret;
231
232 if( opt.nbio == 2 )
233 ret = delayed_recv( io_ctx->net, buf, len );
234 else
235 ret = mbedtls_net_recv( io_ctx->net, buf, len );
236 if( ret < 0 )
237 return( ret );
238 recv_len = (size_t) ret;
239
240 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
241 {
242 /* Here's the place to do any datagram/record checking
243 * in between receiving the packet from the underlying
244 * transport and passing it on to the TLS stack. */
245 #if defined(MBEDTLS_SSL_RECORD_CHECKING)
246 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
247 return( -1 );
248 #endif /* MBEDTLS_SSL_RECORD_CHECKING */
249 }
250
251 return( (int) recv_len );
252 }
253
recv_timeout_cb(void * ctx,unsigned char * buf,size_t len,uint32_t timeout)254 int recv_timeout_cb( void *ctx, unsigned char *buf, size_t len,
255 uint32_t timeout )
256 {
257 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
258 int ret;
259 size_t recv_len;
260
261 ret = mbedtls_net_recv_timeout( io_ctx->net, buf, len, timeout );
262 if( ret < 0 )
263 return( ret );
264 recv_len = (size_t) ret;
265
266 if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
267 {
268 /* Here's the place to do any datagram/record checking
269 * in between receiving the packet from the underlying
270 * transport and passing it on to the TLS stack. */
271 #if defined(MBEDTLS_SSL_RECORD_CHECKING)
272 if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 )
273 return( -1 );
274 #endif /* MBEDTLS_SSL_RECORD_CHECKING */
275 }
276
277 return( (int) recv_len );
278 }
279
send_cb(void * ctx,unsigned char const * buf,size_t len)280 int send_cb( void *ctx, unsigned char const *buf, size_t len )
281 {
282 io_ctx_t *io_ctx = (io_ctx_t*) ctx;
283
284 if( opt.nbio == 2 )
285 return( delayed_send( io_ctx->net, buf, len ) );
286
287 return( mbedtls_net_send( io_ctx->net, buf, len ) );
288 }
289
290 #if defined(MBEDTLS_X509_CRT_PARSE_C)
291 int ssl_sig_hashes_for_test[] = {
292 #if defined(MBEDTLS_SHA512_C)
293 MBEDTLS_MD_SHA512,
294 MBEDTLS_MD_SHA384,
295 #endif
296 #if defined(MBEDTLS_SHA256_C)
297 MBEDTLS_MD_SHA256,
298 MBEDTLS_MD_SHA224,
299 #endif
300 #if defined(MBEDTLS_SHA1_C)
301 /* Allow SHA-1 as we use it extensively in tests. */
302 MBEDTLS_MD_SHA1,
303 #endif
304 MBEDTLS_MD_NONE
305 };
306 #endif /* MBEDTLS_X509_CRT_PARSE_C */
307