blob: 23a5bddac259a624d500c034b9b7b9c3592b5730 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Janos Follath23bdca02016-10-07 14:47:14 +010053#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020055#endif
56
Hanno Becker2a43f6f2018-08-10 11:12:52 +010057static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
58
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010059/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010061{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020063 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010064 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010065#else
66 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010067#endif
68 return( 0 );
69}
70
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020071/*
72 * Start a timer.
73 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020076{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020077 if( ssl->f_set_timer == NULL )
78 return;
79
80 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
81 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020082}
83
84/*
85 * Return -1 is timer is expired, 0 if it isn't.
86 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020088{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020089 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020090 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020091
92 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020093 {
94 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020095 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020096 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020097
98 return( 0 );
99}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200100
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100101static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
102 mbedtls_ssl_transform *transform );
103static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
104 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100105
106#define SSL_DONT_FORCE_FLUSH 0
107#define SSL_FORCE_FLUSH 1
108
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200109#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100110
111static uint16_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
112{
113 uint16_t mtu = ssl->conf->mtu;
114
115 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
116 return( (int) mtu );
117
118 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
119}
120
Hanno Becker67bc7c32018-08-06 11:33:50 +0100121static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
122{
123 size_t const bytes_written = ssl->out_left;
124 uint16_t const mtu = ssl_get_maximum_datagram_size( ssl );
125
126 /* Double-check that the write-index hasn't gone
127 * past what we can transmit in a single datagram. */
128 if( bytes_written > (size_t) mtu )
129 {
130 /* Should never happen... */
131 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
132 }
133
134 return( (int) ( mtu - bytes_written ) );
135}
136
137static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
138{
139 int ret;
140 size_t remaining, expansion;
141 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
142
143#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
144 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
145
146 if( max_len > mfl )
147 max_len = mfl;
148#endif
149
150 ret = ssl_get_remaining_space_in_datagram( ssl );
151 if( ret < 0 )
152 return( ret );
153 remaining = (size_t) ret;
154
155 ret = mbedtls_ssl_get_record_expansion( ssl );
156 if( ret < 0 )
157 return( ret );
158 expansion = (size_t) ret;
159
160 if( remaining <= expansion )
161 return( 0 );
162
163 remaining -= expansion;
164 if( remaining >= max_len )
165 remaining = max_len;
166
167 return( (int) remaining );
168}
169
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200170/*
171 * Double the retransmit timeout value, within the allowed range,
172 * returning -1 if the maximum value has already been reached.
173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200175{
176 uint32_t new_timeout;
177
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200178 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200179 return( -1 );
180
181 new_timeout = 2 * ssl->handshake->retransmit_timeout;
182
183 /* Avoid arithmetic overflow and range overflow */
184 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200185 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200186 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200187 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200188 }
189
190 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200192 ssl->handshake->retransmit_timeout ) );
193
194 return( 0 );
195}
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200198{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200199 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200201 ssl->handshake->retransmit_timeout ) );
202}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200206/*
207 * Convert max_fragment_length codes to length.
208 * RFC 6066 says:
209 * enum{
210 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
211 * } MaxFragmentLength;
212 * and we add 0 -> extension unused
213 */
Angus Grattond8213d02016-05-25 20:56:48 +1000214static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200215{
Angus Grattond8213d02016-05-25 20:56:48 +1000216 switch( mfl )
217 {
218 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
219 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
220 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
221 return 512;
222 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
223 return 1024;
224 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
225 return 2048;
226 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
227 return 4096;
228 default:
229 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
230 }
231}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200233
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200234#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200236{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_ssl_session_free( dst );
238 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200241 if( src->peer_cert != NULL )
242 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200243 int ret;
244
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200245 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200246 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200247 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200252 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200255 dst->peer_cert = NULL;
256 return( ret );
257 }
258 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200260
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200261#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200262 if( src->ticket != NULL )
263 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200264 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200265 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200266 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200267
268 memcpy( dst->ticket, src->ticket, src->ticket_len );
269 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200270#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200271
272 return( 0 );
273}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200274#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
277int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200278 const unsigned char *key_enc, const unsigned char *key_dec,
279 size_t keylen,
280 const unsigned char *iv_enc, const unsigned char *iv_dec,
281 size_t ivlen,
282 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200283 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
285int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
286int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
287int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
288int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
289#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000290
Paul Bakker5121ce52009-01-03 21:22:43 +0000291/*
292 * Key material generation
293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200295static int ssl3_prf( const unsigned char *secret, size_t slen,
296 const char *label,
297 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000298 unsigned char *dstbuf, size_t dlen )
299{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100300 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000301 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200302 mbedtls_md5_context md5;
303 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000304 unsigned char padding[16];
305 unsigned char sha1sum[20];
306 ((void)label);
307
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200308 mbedtls_md5_init( &md5 );
309 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200310
Paul Bakker5f70b252012-09-13 14:23:06 +0000311 /*
312 * SSLv3:
313 * block =
314 * MD5( secret + SHA1( 'A' + secret + random ) ) +
315 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
316 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
317 * ...
318 */
319 for( i = 0; i < dlen / 16; i++ )
320 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200321 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000322
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100323 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100324 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100325 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100326 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100327 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100328 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100329 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100330 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100331 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100332 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000333
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100334 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100335 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100336 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100337 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100338 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100339 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100340 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100341 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000342 }
343
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100344exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200345 mbedtls_md5_free( &md5 );
346 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000347
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500348 mbedtls_platform_zeroize( padding, sizeof( padding ) );
349 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000350
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100351 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000352}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200356static int tls1_prf( const unsigned char *secret, size_t slen,
357 const char *label,
358 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000359 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000360{
Paul Bakker23986e52011-04-24 08:57:21 +0000361 size_t nb, hs;
362 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200363 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000364 unsigned char tmp[128];
365 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 const mbedtls_md_info_t *md_info;
367 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100368 int ret;
369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000371
372 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000374
375 hs = ( slen + 1 ) / 2;
376 S1 = secret;
377 S2 = secret + slen - hs;
378
379 nb = strlen( label );
380 memcpy( tmp + 20, label, nb );
381 memcpy( tmp + 20 + nb, random, rlen );
382 nb += rlen;
383
384 /*
385 * First compute P_md5(secret,label+random)[0..dlen]
386 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
388 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100391 return( ret );
392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
394 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
395 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000396
397 for( i = 0; i < dlen; i += 16 )
398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_md_hmac_reset ( &md_ctx );
400 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
401 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_md_hmac_reset ( &md_ctx );
404 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
405 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000406
407 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
408
409 for( j = 0; j < k; j++ )
410 dstbuf[i + j] = h_i[j];
411 }
412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100414
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 /*
416 * XOR out with P_sha1(secret,label+random)[0..dlen]
417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
419 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100422 return( ret );
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
425 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
426 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000427
428 for( i = 0; i < dlen; i += 20 )
429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_md_hmac_reset ( &md_ctx );
431 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
432 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_md_hmac_reset ( &md_ctx );
435 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
436 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
438 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
439
440 for( j = 0; j < k; j++ )
441 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
442 }
443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100445
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500446 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
447 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000448
449 return( 0 );
450}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
454static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100455 const unsigned char *secret, size_t slen,
456 const char *label,
457 const unsigned char *random, size_t rlen,
458 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000459{
460 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100461 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000462 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
464 const mbedtls_md_info_t *md_info;
465 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100466 int ret;
467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
471 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100474
475 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000477
478 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100479 memcpy( tmp + md_len, label, nb );
480 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000481 nb += rlen;
482
483 /*
484 * Compute P_<hash>(secret, label + random)[0..dlen]
485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100487 return( ret );
488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
490 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
491 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100492
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100493 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_md_hmac_reset ( &md_ctx );
496 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
497 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_md_hmac_reset ( &md_ctx );
500 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
501 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000502
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100503 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000504
505 for( j = 0; j < k; j++ )
506 dstbuf[i + j] = h_i[j];
507 }
508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100510
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500511 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
512 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000513
514 return( 0 );
515}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100518static int tls_prf_sha256( const unsigned char *secret, size_t slen,
519 const char *label,
520 const unsigned char *random, size_t rlen,
521 unsigned char *dstbuf, size_t dlen )
522{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100524 label, random, rlen, dstbuf, dlen ) );
525}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200529static int tls_prf_sha384( const unsigned char *secret, size_t slen,
530 const char *label,
531 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000532 unsigned char *dstbuf, size_t dlen )
533{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100535 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000536}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537#endif /* MBEDTLS_SHA512_C */
538#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
543 defined(MBEDTLS_SSL_PROTO_TLS1_1)
544static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200545#endif
Paul Bakker380da532012-04-18 16:10:25 +0000546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547#if defined(MBEDTLS_SSL_PROTO_SSL3)
548static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
549static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200550#endif
551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
553static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
554static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200555#endif
556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
558#if defined(MBEDTLS_SHA256_C)
559static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
560static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
561static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200562#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564#if defined(MBEDTLS_SHA512_C)
565static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
566static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
567static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100568#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000572{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200573 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 unsigned char keyblk[256];
576 unsigned char *key1;
577 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100578 unsigned char *mac_enc;
579 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000580 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200581 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 const mbedtls_cipher_info_t *cipher_info;
583 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_ssl_session *session = ssl->session_negotiate;
586 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
587 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100592 if( cipher_info == NULL )
593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100595 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100597 }
598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100600 if( md_info == NULL )
601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100603 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100605 }
606
Paul Bakker5121ce52009-01-03 21:22:43 +0000607 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000608 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000609 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610#if defined(MBEDTLS_SSL_PROTO_SSL3)
611 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000612 {
Paul Bakker48916f92012-09-16 19:57:18 +0000613 handshake->tls_prf = ssl3_prf;
614 handshake->calc_verify = ssl_calc_verify_ssl;
615 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000616 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200617 else
618#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
620 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000621 {
Paul Bakker48916f92012-09-16 19:57:18 +0000622 handshake->tls_prf = tls1_prf;
623 handshake->calc_verify = ssl_calc_verify_tls;
624 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000625 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200626 else
627#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
629#if defined(MBEDTLS_SHA512_C)
630 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
631 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000632 {
Paul Bakker48916f92012-09-16 19:57:18 +0000633 handshake->tls_prf = tls_prf_sha384;
634 handshake->calc_verify = ssl_calc_verify_tls_sha384;
635 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000636 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000637 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200638#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639#if defined(MBEDTLS_SHA256_C)
640 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000641 {
Paul Bakker48916f92012-09-16 19:57:18 +0000642 handshake->tls_prf = tls_prf_sha256;
643 handshake->calc_verify = ssl_calc_verify_tls_sha256;
644 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000645 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200646 else
647#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
651 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200652 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000653
654 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 * SSLv3:
656 * master =
657 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
658 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
659 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200660 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200661 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000662 * master = PRF( premaster, "master secret", randbytes )[0..47]
663 */
Paul Bakker0a597072012-09-25 21:55:46 +0000664 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000667 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
670 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200671 {
672 unsigned char session_hash[48];
673 size_t hash_len;
674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200676
677 ssl->handshake->calc_verify( ssl, session_hash );
678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
680 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200683 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200685 {
686 hash_len = 48;
687 }
688 else
689#endif
690 hash_len = 32;
691 }
692 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200694 hash_len = 36;
695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200697
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100698 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
699 "extended master secret",
700 session_hash, hash_len,
701 session->master, 48 );
702 if( ret != 0 )
703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100705 return( ret );
706 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200707
708 }
709 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200710#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100711 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
712 "master secret",
713 handshake->randbytes, 64,
714 session->master, 48 );
715 if( ret != 0 )
716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100718 return( ret );
719 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200720
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500721 mbedtls_platform_zeroize( handshake->premaster,
722 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000723 }
724 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000726
727 /*
728 * Swap the client and server random values.
729 */
Paul Bakker48916f92012-09-16 19:57:18 +0000730 memcpy( tmp, handshake->randbytes, 64 );
731 memcpy( handshake->randbytes, tmp + 32, 32 );
732 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500733 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
735 /*
736 * SSLv3:
737 * key block =
738 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
739 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
740 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
741 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
742 * ...
743 *
744 * TLSv1:
745 * key block = PRF( master, "key expansion", randbytes )
746 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100747 ret = handshake->tls_prf( session->master, 48, "key expansion",
748 handshake->randbytes, 64, keyblk, 256 );
749 if( ret != 0 )
750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100752 return( ret );
753 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
756 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
757 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
758 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
759 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000760
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500761 mbedtls_platform_zeroize( handshake->randbytes,
762 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000763
764 /*
765 * Determine the appropriate key, IV and MAC length.
766 */
Paul Bakker68884e32013-01-07 18:20:04 +0100767
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200768 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200771 cipher_info->mode == MBEDTLS_MODE_CCM ||
772 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000773 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200774 size_t taglen, explicit_ivlen;
775
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200776 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000777 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200778
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200779 /* All modes haves 96-bit IVs;
780 * GCM and CCM has 4 implicit and 8 explicit bytes
781 * ChachaPoly has all 12 bytes implicit
782 */
Paul Bakker68884e32013-01-07 18:20:04 +0100783 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200784 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
785 transform->fixed_ivlen = 12;
786 else
787 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200788
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200789 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
790 taglen = transform->ciphersuite_info->flags &
791 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
792
793
794 /* Minimum length of encrypted record */
795 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
796 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100797 }
798 else
799 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200800 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
802 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200805 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100806 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000807
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200808 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000809 mac_key_len = mbedtls_md_get_size( md_info );
810 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200813 /*
814 * If HMAC is to be truncated, we shall keep the leftmost bytes,
815 * (rfc 6066 page 13 or rfc 2104 section 4),
816 * so we only need to adjust the length here.
817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000821
822#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
823 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000824 * HMAC implementation which also truncates the key
825 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000826 mac_key_len = transform->maclen;
827#endif
828 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200830
831 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100832 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000833
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200834 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200836 transform->minlen = transform->maclen;
837 else
Paul Bakker68884e32013-01-07 18:20:04 +0100838 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200839 /*
840 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100841 * 1. if EtM is in use: one block plus MAC
842 * otherwise: * first multiple of blocklen greater than maclen
843 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200844 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
846 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100847 {
848 transform->minlen = transform->maclen
849 + cipher_info->block_size;
850 }
851 else
852#endif
853 {
854 transform->minlen = transform->maclen
855 + cipher_info->block_size
856 - transform->maclen % cipher_info->block_size;
857 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
860 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
861 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200862 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100863 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200864#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
866 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
867 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200868 {
869 transform->minlen += transform->ivlen;
870 }
871 else
872#endif
873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
875 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200876 }
Paul Bakker68884e32013-01-07 18:20:04 +0100877 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000878 }
879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000881 transform->keylen, transform->minlen, transform->ivlen,
882 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000883
884 /*
885 * Finally setup the cipher contexts, IVs and MAC secrets.
886 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200888 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000889 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000890 key1 = keyblk + mac_key_len * 2;
891 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000892
Paul Bakker68884e32013-01-07 18:20:04 +0100893 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000894 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000895
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000896 /*
897 * This is not used in TLS v1.1.
898 */
Paul Bakker48916f92012-09-16 19:57:18 +0000899 iv_copy_len = ( transform->fixed_ivlen ) ?
900 transform->fixed_ivlen : transform->ivlen;
901 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
902 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000903 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000904 }
905 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906#endif /* MBEDTLS_SSL_CLI_C */
907#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200908 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000909 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000910 key1 = keyblk + mac_key_len * 2 + transform->keylen;
911 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000912
Hanno Becker81c7b182017-11-09 18:39:33 +0000913 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100914 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000915
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000916 /*
917 * This is not used in TLS v1.1.
918 */
Paul Bakker48916f92012-09-16 19:57:18 +0000919 iv_copy_len = ( transform->fixed_ivlen ) ?
920 transform->fixed_ivlen : transform->ivlen;
921 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
922 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000923 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000924 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100925 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
929 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100930 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932#if defined(MBEDTLS_SSL_PROTO_SSL3)
933 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100934 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000935 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
938 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100939 }
940
Hanno Becker81c7b182017-11-09 18:39:33 +0000941 memcpy( transform->mac_enc, mac_enc, mac_key_len );
942 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100943 }
944 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945#endif /* MBEDTLS_SSL_PROTO_SSL3 */
946#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
947 defined(MBEDTLS_SSL_PROTO_TLS1_2)
948 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100949 {
Gilles Peskine039fd122018-03-19 19:06:08 +0100950 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
951 For AEAD-based ciphersuites, there is nothing to do here. */
952 if( mac_key_len != 0 )
953 {
954 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
955 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
956 }
Paul Bakker68884e32013-01-07 18:20:04 +0100957 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200958 else
959#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
962 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200963 }
Paul Bakker68884e32013-01-07 18:20:04 +0100964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
966 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000967 {
968 int ret = 0;
969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +0000971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100973 transform->iv_enc, transform->iv_dec,
974 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100975 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +0000976 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
979 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000980 }
981 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000983
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200984#if defined(MBEDTLS_SSL_EXPORT_KEYS)
985 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100986 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200987 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
988 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +0000989 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100990 iv_copy_len );
991 }
992#endif
993
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200994 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200995 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000996 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200997 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200998 return( ret );
999 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001000
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001001 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001002 cipher_info ) ) != 0 )
1003 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001004 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001005 return( ret );
1006 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001009 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001013 return( ret );
1014 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001017 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001021 return( ret );
1022 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024#if defined(MBEDTLS_CIPHER_MODE_CBC)
1025 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1028 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001031 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001032 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1035 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001038 return( ret );
1039 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001040 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001042
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001043 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001046 // Initialize compression
1047 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001049 {
Paul Bakker16770332013-10-11 09:59:44 +02001050 if( ssl->compress_buf == NULL )
1051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001053 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001054 if( ssl->compress_buf == NULL )
1055 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001057 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001058 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001059 }
1060 }
1061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001063
Paul Bakker48916f92012-09-16 19:57:18 +00001064 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1065 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001066
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001067 if( deflateInit( &transform->ctx_deflate,
1068 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001069 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1072 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001073 }
1074 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001078
1079 return( 0 );
1080}
1081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082#if defined(MBEDTLS_SSL_PROTO_SSL3)
1083void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001084{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001085 mbedtls_md5_context md5;
1086 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001087 unsigned char pad_1[48];
1088 unsigned char pad_2[48];
1089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001091
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001092 mbedtls_md5_init( &md5 );
1093 mbedtls_sha1_init( &sha1 );
1094
1095 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1096 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001097
Paul Bakker380da532012-04-18 16:10:25 +00001098 memset( pad_1, 0x36, 48 );
1099 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001100
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001101 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1102 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1103 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001104
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001105 mbedtls_md5_starts_ret( &md5 );
1106 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1107 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1108 mbedtls_md5_update_ret( &md5, hash, 16 );
1109 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001110
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001111 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1112 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1113 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001114
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001115 mbedtls_sha1_starts_ret( &sha1 );
1116 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1117 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1118 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1119 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001123
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001124 mbedtls_md5_free( &md5 );
1125 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001126
Paul Bakker380da532012-04-18 16:10:25 +00001127 return;
1128}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1132void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001133{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001134 mbedtls_md5_context md5;
1135 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001138
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001139 mbedtls_md5_init( &md5 );
1140 mbedtls_sha1_init( &sha1 );
1141
1142 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1143 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001144
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001145 mbedtls_md5_finish_ret( &md5, hash );
1146 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001150
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001151 mbedtls_md5_free( &md5 );
1152 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001153
Paul Bakker380da532012-04-18 16:10:25 +00001154 return;
1155}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1159#if defined(MBEDTLS_SHA256_C)
1160void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001161{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001162 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001163
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001164 mbedtls_sha256_init( &sha256 );
1165
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001167
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001168 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001169 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001173
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001174 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001175
Paul Bakker380da532012-04-18 16:10:25 +00001176 return;
1177}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180#if defined(MBEDTLS_SHA512_C)
1181void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001182{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001183 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001184
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001185 mbedtls_sha512_init( &sha512 );
1186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001188
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001189 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001190 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001194
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001195 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001196
Paul Bakker5121ce52009-01-03 21:22:43 +00001197 return;
1198}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199#endif /* MBEDTLS_SHA512_C */
1200#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1203int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001204{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001205 unsigned char *p = ssl->handshake->premaster;
1206 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001207 const unsigned char *psk = ssl->conf->psk;
1208 size_t psk_len = ssl->conf->psk_len;
1209
1210 /* If the psk callback was called, use its result */
1211 if( ssl->handshake->psk != NULL )
1212 {
1213 psk = ssl->handshake->psk;
1214 psk_len = ssl->handshake->psk_len;
1215 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001216
1217 /*
1218 * PMS = struct {
1219 * opaque other_secret<0..2^16-1>;
1220 * opaque psk<0..2^16-1>;
1221 * };
1222 * with "other_secret" depending on the particular key exchange
1223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1225 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001226 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001227 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001229
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001230 *(p++) = (unsigned char)( psk_len >> 8 );
1231 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001232
1233 if( end < p || (size_t)( end - p ) < psk_len )
1234 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1235
1236 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001237 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001238 }
1239 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1241#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1242 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001243 {
1244 /*
1245 * other_secret already set by the ClientKeyExchange message,
1246 * and is 48 bytes long
1247 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001248 if( end - p < 2 )
1249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1250
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001251 *p++ = 0;
1252 *p++ = 48;
1253 p += 48;
1254 }
1255 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1257#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1258 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001259 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001260 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001261 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001262
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001263 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001265 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001266 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001269 return( ret );
1270 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001271 *(p++) = (unsigned char)( len >> 8 );
1272 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001273 p += len;
1274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001276 }
1277 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1279#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1280 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001281 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001282 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001283 size_t zlen;
1284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001286 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001287 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001290 return( ret );
1291 }
1292
1293 *(p++) = (unsigned char)( zlen >> 8 );
1294 *(p++) = (unsigned char)( zlen );
1295 p += zlen;
1296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001298 }
1299 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1303 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001304 }
1305
1306 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001307 if( end - p < 2 )
1308 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001309
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001310 *(p++) = (unsigned char)( psk_len >> 8 );
1311 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001312
1313 if( end < p || (size_t)( end - p ) < psk_len )
1314 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1315
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001316 memcpy( p, psk, psk_len );
1317 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001318
1319 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1320
1321 return( 0 );
1322}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001326/*
1327 * SSLv3.0 MAC functions
1328 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001329#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001330static void ssl_mac( mbedtls_md_context_t *md_ctx,
1331 const unsigned char *secret,
1332 const unsigned char *buf, size_t len,
1333 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001334 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001335{
1336 unsigned char header[11];
1337 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001338 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1340 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001341
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001342 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001344 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001345 else
Paul Bakker68884e32013-01-07 18:20:04 +01001346 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001347
1348 memcpy( header, ctr, 8 );
1349 header[ 8] = (unsigned char) type;
1350 header[ 9] = (unsigned char)( len >> 8 );
1351 header[10] = (unsigned char)( len );
1352
Paul Bakker68884e32013-01-07 18:20:04 +01001353 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 mbedtls_md_starts( md_ctx );
1355 mbedtls_md_update( md_ctx, secret, md_size );
1356 mbedtls_md_update( md_ctx, padding, padlen );
1357 mbedtls_md_update( md_ctx, header, 11 );
1358 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001359 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001360
Paul Bakker68884e32013-01-07 18:20:04 +01001361 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 mbedtls_md_starts( md_ctx );
1363 mbedtls_md_update( md_ctx, secret, md_size );
1364 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001365 mbedtls_md_update( md_ctx, out, md_size );
1366 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001367}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1371 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001372 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001373#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001374#endif
1375
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001376/* The function below is only used in the Lucky 13 counter-measure in
1377 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1378#if defined(SSL_SOME_MODES_USE_MAC) && \
1379 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1380 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1381 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1382/* This function makes sure every byte in the memory region is accessed
1383 * (in ascending addresses order) */
1384static void ssl_read_memory( unsigned char *p, size_t len )
1385{
1386 unsigned char acc = 0;
1387 volatile unsigned char force;
1388
1389 for( ; len != 0; p++, len-- )
1390 acc ^= *p;
1391
1392 force = acc;
1393 (void) force;
1394}
1395#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1396
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001397/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001398 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001399 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001401{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001403 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001407 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1410 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001411 }
1412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001416 ssl->out_msg, ssl->out_msglen );
1417
Paul Bakker5121ce52009-01-03 21:22:43 +00001418 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001419 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001420 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001421#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 if( mode == MBEDTLS_MODE_STREAM ||
1423 ( mode == MBEDTLS_MODE_CBC
1424#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1425 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001426#endif
1427 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#if defined(MBEDTLS_SSL_PROTO_SSL3)
1430 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001431 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001432 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001433
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001434 ssl_mac( &ssl->transform_out->md_ctx_enc,
1435 ssl->transform_out->mac_enc,
1436 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001437 ssl->out_ctr, ssl->out_msgtype,
1438 mac );
1439
1440 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001441 }
1442 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001443#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1445 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1446 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001447 {
Hanno Becker992b6872017-11-09 18:57:39 +00001448 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1451 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1452 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1453 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001454 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001455 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001457
1458 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001459 }
1460 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001461#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1464 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001465 }
1466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001468 ssl->out_msg + ssl->out_msglen,
1469 ssl->transform_out->maclen );
1470
1471 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001472 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001473 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001474#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001475
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001476 /*
1477 * Encrypt
1478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1480 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001481 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001482 int ret;
1483 size_t olen = 0;
1484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001486 "including %d bytes of padding",
1487 ssl->out_msglen, 0 ) );
1488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001490 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001491 ssl->transform_out->ivlen,
1492 ssl->out_msg, ssl->out_msglen,
1493 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001496 return( ret );
1497 }
1498
1499 if( ssl->out_msglen != olen )
1500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1502 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001503 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001504 }
Paul Bakker68884e32013-01-07 18:20:04 +01001505 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001507#if defined(MBEDTLS_GCM_C) || \
1508 defined(MBEDTLS_CCM_C) || \
1509 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001511 mode == MBEDTLS_MODE_CCM ||
1512 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001513 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001514 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001515 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001516 unsigned char *enc_msg;
1517 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001518 unsigned char iv[12];
1519 mbedtls_ssl_transform *transform = ssl->transform_out;
1520 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001522 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001523
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001524 /*
1525 * Prepare additional authenticated data
1526 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001527 memcpy( add_data, ssl->out_ctr, 8 );
1528 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001530 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001531 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1532 add_data[12] = ssl->out_msglen & 0xFF;
1533
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001534 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001535
Paul Bakker68884e32013-01-07 18:20:04 +01001536 /*
1537 * Generate IV
1538 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001539 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1540 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001541 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001542 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1543 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1544 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1545
1546 }
1547 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1548 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001549 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001550 unsigned char i;
1551
1552 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1553
1554 for( i = 0; i < 8; i++ )
1555 iv[i+4] ^= ssl->out_ctr[i];
1556 }
1557 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001558 {
1559 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1561 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001562 }
1563
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001564 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1565 iv, transform->ivlen );
1566 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1567 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001568
Paul Bakker68884e32013-01-07 18:20:04 +01001569 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001570 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001571 */
1572 enc_msg = ssl->out_msg;
1573 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001574 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001577 "including 0 bytes of padding",
1578 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001579
Paul Bakker68884e32013-01-07 18:20:04 +01001580 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001581 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001582 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001583 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1584 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001585 add_data, 13,
1586 enc_msg, enc_msglen,
1587 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001588 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001591 return( ret );
1592 }
1593
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001594 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1597 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001598 }
1599
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001600 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001601 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001604 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001605 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1607#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001608 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001610 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001611 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001612 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001613 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001614
Paul Bakker48916f92012-09-16 19:57:18 +00001615 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1616 ssl->transform_out->ivlen;
1617 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001618 padlen = 0;
1619
1620 for( i = 0; i <= padlen; i++ )
1621 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1622
1623 ssl->out_msglen += padlen + 1;
1624
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001625 enc_msglen = ssl->out_msglen;
1626 enc_msg = ssl->out_msg;
1627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001629 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001630 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1631 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001634 {
1635 /*
1636 * Generate IV
1637 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001638 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001639 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001640 if( ret != 0 )
1641 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001642
Paul Bakker92be97b2013-01-02 17:30:03 +01001643 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001644 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001645
1646 /*
1647 * Fix pointer positions and message length with added IV
1648 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001649 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001650 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001651 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001652 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001656 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001657 ssl->out_msglen, ssl->transform_out->ivlen,
1658 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001661 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001662 ssl->transform_out->ivlen,
1663 enc_msg, enc_msglen,
1664 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001667 return( ret );
1668 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001669
Paul Bakkercca5b812013-08-31 17:40:26 +02001670 if( enc_msglen != olen )
1671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1673 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001674 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1677 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001678 {
1679 /*
1680 * Save IV in SSL3 and TLS1
1681 */
1682 memcpy( ssl->transform_out->iv_enc,
1683 ssl->transform_out->cipher_ctx_enc.iv,
1684 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001685 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001686#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001689 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001690 {
1691 /*
1692 * MAC(MAC_write_key, seq_num +
1693 * TLSCipherText.type +
1694 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001695 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001696 * IV + // except for TLS 1.0
1697 * ENC(content + padding + padding_length));
1698 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001699 unsigned char pseudo_hdr[13];
1700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001702
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001703 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1704 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001705 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1706 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1711 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001712 ssl->out_iv, ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001714 ssl->out_iv + ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001716
1717 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001718 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001719 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001721 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001722 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001724 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1727 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001728 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001729
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001730 /* Make extra sure authentication was performed, exactly once */
1731 if( auth_done != 1 )
1732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1734 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001735 }
1736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001738
1739 return( 0 );
1740}
1741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001743{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001745 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001746#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001747 size_t padlen = 0, correct = 1;
1748#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001751
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001752 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1755 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001756 }
1757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001759
Paul Bakker48916f92012-09-16 19:57:18 +00001760 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001763 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001765 }
1766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1768 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001769 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001770 int ret;
1771 size_t olen = 0;
1772
Paul Bakker68884e32013-01-07 18:20:04 +01001773 padlen = 0;
1774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001776 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001777 ssl->transform_in->ivlen,
1778 ssl->in_msg, ssl->in_msglen,
1779 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001782 return( ret );
1783 }
1784
1785 if( ssl->in_msglen != olen )
1786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1788 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001789 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001790 }
Paul Bakker68884e32013-01-07 18:20:04 +01001791 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001793#if defined(MBEDTLS_GCM_C) || \
1794 defined(MBEDTLS_CCM_C) || \
1795 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001797 mode == MBEDTLS_MODE_CCM ||
1798 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001799 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001800 int ret;
1801 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001802 unsigned char *dec_msg;
1803 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001804 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001805 unsigned char iv[12];
1806 mbedtls_ssl_transform *transform = ssl->transform_in;
1807 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001809 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001810
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001811 /*
1812 * Compute and update sizes
1813 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001814 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001817 "+ taglen (%d)", ssl->in_msglen,
1818 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001820 }
1821 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1822
Paul Bakker68884e32013-01-07 18:20:04 +01001823 dec_msg = ssl->in_msg;
1824 dec_msg_result = ssl->in_msg;
1825 ssl->in_msglen = dec_msglen;
1826
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001827 /*
1828 * Prepare additional authenticated data
1829 */
Paul Bakker68884e32013-01-07 18:20:04 +01001830 memcpy( add_data, ssl->in_ctr, 8 );
1831 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001833 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001834 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1835 add_data[12] = ssl->in_msglen & 0xFF;
1836
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001837 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001838
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001839 /*
1840 * Prepare IV
1841 */
1842 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1843 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001844 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001845 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1846 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001847
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001848 }
1849 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1850 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001851 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001852 unsigned char i;
1853
1854 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1855
1856 for( i = 0; i < 8; i++ )
1857 iv[i+4] ^= ssl->in_ctr[i];
1858 }
1859 else
1860 {
1861 /* Reminder if we ever add an AEAD mode with a different size */
1862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1863 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1864 }
1865
1866 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001868
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001869 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001870 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001871 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001873 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001874 add_data, 13,
1875 dec_msg, dec_msglen,
1876 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001877 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1882 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001883
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001884 return( ret );
1885 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001886 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001887
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001888 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1891 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001892 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001893 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001894 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1896#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001897 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001899 {
Paul Bakker45829992013-01-03 14:52:21 +01001900 /*
1901 * Decrypt and check the padding
1902 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001903 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001904 unsigned char *dec_msg;
1905 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001906 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001907 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001908 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001909
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 /*
Paul Bakker45829992013-01-03 14:52:21 +01001911 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001912 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1914 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001915 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001916#endif
Paul Bakker45829992013-01-03 14:52:21 +01001917
1918 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1919 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001922 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1923 ssl->transform_in->ivlen,
1924 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001926 }
1927
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001928 dec_msglen = ssl->in_msglen;
1929 dec_msg = ssl->in_msg;
1930 dec_msg_result = ssl->in_msg;
1931
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001932 /*
1933 * Authenticate before decrypt if enabled
1934 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1936 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001937 {
Hanno Becker992b6872017-11-09 18:57:39 +00001938 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001939 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001942
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001943 dec_msglen -= ssl->transform_in->maclen;
1944 ssl->in_msglen -= ssl->transform_in->maclen;
1945
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001946 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1947 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1948 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1949 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1954 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001955 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001956 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001960 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001961 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001962 ssl->transform_in->maclen );
1963
Hanno Becker992b6872017-11-09 18:57:39 +00001964 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1965 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001970 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001971 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001972 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001974
1975 /*
1976 * Check length sanity
1977 */
1978 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001981 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001983 }
1984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001986 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001987 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001988 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001990 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001991 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00001992 dec_msglen -= ssl->transform_in->ivlen;
1993 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001994
Paul Bakker48916f92012-09-16 19:57:18 +00001995 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001996 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001997 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002001 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002002 ssl->transform_in->ivlen,
2003 dec_msg, dec_msglen,
2004 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002007 return( ret );
2008 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002009
Paul Bakkercca5b812013-08-31 17:40:26 +02002010 if( dec_msglen != olen )
2011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2013 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002014 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2017 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002018 {
2019 /*
2020 * Save IV in SSL3 and TLS1
2021 */
2022 memcpy( ssl->transform_in->iv_dec,
2023 ssl->transform_in->cipher_ctx_dec.iv,
2024 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002025 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002027
2028 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002029
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002030 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002031 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033#if defined(MBEDTLS_SSL_DEBUG_ALL)
2034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002035 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002036#endif
Paul Bakker45829992013-01-03 14:52:21 +01002037 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002038 correct = 0;
2039 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041#if defined(MBEDTLS_SSL_PROTO_SSL3)
2042 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002043 {
Paul Bakker48916f92012-09-16 19:57:18 +00002044 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046#if defined(MBEDTLS_SSL_DEBUG_ALL)
2047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002048 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002049 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002050#endif
Paul Bakker45829992013-01-03 14:52:21 +01002051 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002052 }
2053 }
2054 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2056#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2057 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2058 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 {
2060 /*
Paul Bakker45829992013-01-03 14:52:21 +01002061 * TLSv1+: always check the padding up to the first failure
2062 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002063 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002064 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002065 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002066 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002067
Paul Bakker956c9e02013-12-19 14:42:28 +01002068 /*
2069 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002070 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002071 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002072 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002073 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002074 *
2075 * In both cases we reset padding_idx to a safe value (0) to
2076 * prevent out-of-buffer reads.
2077 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002078 correct &= ( padlen <= ssl->in_msglen );
2079 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002080 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002081
2082 padding_idx *= correct;
2083
Angus Grattonb512bc12018-06-19 15:57:50 +10002084 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002085 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002086 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002087 pad_count += real_count *
2088 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2089 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002090
2091 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002094 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002096#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002097 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002098 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002099 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2101 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2104 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002105 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002106
2107 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002109 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002111 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2114 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002115 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002116
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002117#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002119 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002120#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002121
2122 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002123 * Authenticate if not done yet.
2124 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002126#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002127 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002128 {
Hanno Becker992b6872017-11-09 18:57:39 +00002129 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002130
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002131 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002132
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002133 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2134 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136#if defined(MBEDTLS_SSL_PROTO_SSL3)
2137 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002138 {
2139 ssl_mac( &ssl->transform_in->md_ctx_dec,
2140 ssl->transform_in->mac_dec,
2141 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002142 ssl->in_ctr, ssl->in_msgtype,
2143 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002144 }
2145 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2147#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2148 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2149 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002150 {
2151 /*
2152 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002153 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002154 *
2155 * Known timing attacks:
2156 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2157 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002158 * To compensate for different timings for the MAC calculation
2159 * depending on how much padding was removed (which is determined
2160 * by padlen), process extra_run more blocks through the hash
2161 * function.
2162 *
2163 * The formula in the paper is
2164 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2165 * where L1 is the size of the header plus the decrypted message
2166 * plus CBC padding and L2 is the size of the header plus the
2167 * decrypted message. This is for an underlying hash function
2168 * with 64-byte blocks.
2169 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2170 * correctly. We round down instead of up, so -56 is the correct
2171 * value for our calculations instead of -55.
2172 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002173 * Repeat the formula rather than defining a block_size variable.
2174 * This avoids requiring division by a variable at runtime
2175 * (which would be marginally less efficient and would require
2176 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002177 */
2178 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002179
2180 /*
2181 * The next two sizes are the minimum and maximum values of
2182 * in_msglen over all padlen values.
2183 *
2184 * They're independent of padlen, since we previously did
2185 * in_msglen -= padlen.
2186 *
2187 * Note that max_len + maclen is never more than the buffer
2188 * length, as we previously did in_msglen -= maclen too.
2189 */
2190 const size_t max_len = ssl->in_msglen + padlen;
2191 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2192
Gilles Peskine20b44082018-05-29 14:06:49 +02002193 switch( ssl->transform_in->ciphersuite_info->mac )
2194 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002195#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2196 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002197 case MBEDTLS_MD_MD5:
2198 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002199 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002200 /* 8 bytes of message size, 64-byte compression blocks */
2201 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2202 ( 13 + ssl->in_msglen + 8 ) / 64;
2203 break;
2204#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002205#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002206 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002207 /* 16 bytes of message size, 128-byte compression blocks */
2208 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2209 ( 13 + ssl->in_msglen + 16 ) / 128;
2210 break;
2211#endif
2212 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002214 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2215 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002216
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002217 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2220 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2221 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2222 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002223 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002224 /* Make sure we access everything even when padlen > 0. This
2225 * makes the synchronisation requirements for just-in-time
2226 * Prime+Probe attacks much tighter and hopefully impractical. */
2227 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002228 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002229
2230 /* Call mbedtls_md_process at least once due to cache attacks
2231 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002232 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002236
2237 /* Make sure we access all the memory that could contain the MAC,
2238 * before we check it in the next code block. This makes the
2239 * synchronisation requirements for just-in-time Prime+Probe
2240 * attacks much tighter and hopefully impractical. */
2241 ssl_read_memory( ssl->in_msg + min_len,
2242 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002243 }
2244 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2246 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2249 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002250 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002251
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002252#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002253 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2254 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2255 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002256#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002257
Hanno Becker992b6872017-11-09 18:57:39 +00002258 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2259 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261#if defined(MBEDTLS_SSL_DEBUG_ALL)
2262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002263#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002264 correct = 0;
2265 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002266 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00002267
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002268 /*
2269 * Finally check the correct flag
2270 */
2271 if( correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002273 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002274#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002275
2276 /* Make extra sure authentication was performed, exactly once */
2277 if( auth_done != 1 )
2278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2280 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002281 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002282
2283 if( ssl->in_msglen == 0 )
2284 {
Angus Gratton34817922018-06-19 15:58:22 +10002285#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2286 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2287 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2288 {
2289 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2291 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2292 }
2293#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2294
Paul Bakker5121ce52009-01-03 21:22:43 +00002295 ssl->nb_zero++;
2296
2297 /*
2298 * Three or more empty messages may be a DoS attack
2299 * (excessive CPU consumption).
2300 */
2301 if( ssl->nb_zero > 3 )
2302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002304 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002306 }
2307 }
2308 else
2309 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002312 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002313 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002314 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002315 }
2316 else
2317#endif
2318 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002319 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002320 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2321 if( ++ssl->in_ctr[i - 1] != 0 )
2322 break;
2323
2324 /* The loop goes to its end iff the counter is wrapping */
2325 if( i == ssl_ep_len( ssl ) )
2326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2328 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002329 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002330 }
2331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002333
2334 return( 0 );
2335}
2336
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002337#undef MAC_NONE
2338#undef MAC_PLAINTEXT
2339#undef MAC_CIPHERTEXT
2340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002342/*
2343 * Compression/decompression functions
2344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002346{
2347 int ret;
2348 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002349 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002350 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002351 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002354
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002355 if( len_pre == 0 )
2356 return( 0 );
2357
Paul Bakker2770fbd2012-07-03 13:30:23 +00002358 memcpy( msg_pre, ssl->out_msg, len_pre );
2359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002361 ssl->out_msglen ) );
2362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002364 ssl->out_msg, ssl->out_msglen );
2365
Paul Bakker48916f92012-09-16 19:57:18 +00002366 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2367 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2368 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002369 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002370
Paul Bakker48916f92012-09-16 19:57:18 +00002371 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002372 if( ret != Z_OK )
2373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2375 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002376 }
2377
Angus Grattond8213d02016-05-25 20:56:48 +10002378 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002379 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002382 ssl->out_msglen ) );
2383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002385 ssl->out_msg, ssl->out_msglen );
2386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002388
2389 return( 0 );
2390}
2391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002393{
2394 int ret;
2395 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002396 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002397 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002398 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002401
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002402 if( len_pre == 0 )
2403 return( 0 );
2404
Paul Bakker2770fbd2012-07-03 13:30:23 +00002405 memcpy( msg_pre, ssl->in_msg, len_pre );
2406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002408 ssl->in_msglen ) );
2409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002411 ssl->in_msg, ssl->in_msglen );
2412
Paul Bakker48916f92012-09-16 19:57:18 +00002413 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2414 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2415 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002416 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002417 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002418
Paul Bakker48916f92012-09-16 19:57:18 +00002419 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002420 if( ret != Z_OK )
2421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2423 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002424 }
2425
Angus Grattond8213d02016-05-25 20:56:48 +10002426 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002427 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002430 ssl->in_msglen ) );
2431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002433 ssl->in_msg, ssl->in_msglen );
2434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002436
2437 return( 0 );
2438}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2442static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444#if defined(MBEDTLS_SSL_PROTO_DTLS)
2445static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002446{
2447 /* If renegotiation is not enforced, retransmit until we would reach max
2448 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002449 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002450 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002451 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002452 unsigned char doublings = 1;
2453
2454 while( ratio != 0 )
2455 {
2456 ++doublings;
2457 ratio >>= 1;
2458 }
2459
2460 if( ++ssl->renego_records_seen > doublings )
2461 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002463 return( 0 );
2464 }
2465 }
2466
2467 return( ssl_write_hello_request( ssl ) );
2468}
2469#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002471
Paul Bakker5121ce52009-01-03 21:22:43 +00002472/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002473 * Fill the input message buffer by appending data to it.
2474 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002475 *
2476 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2477 * available (from this read and/or a previous one). Otherwise, an error code
2478 * is returned (possibly EOF or WANT_READ).
2479 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002480 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2481 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2482 * since we always read a whole datagram at once.
2483 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002484 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002485 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002488{
Paul Bakker23986e52011-04-24 08:57:21 +00002489 int ret;
2490 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002493
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002494 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002497 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002499 }
2500
Angus Grattond8213d02016-05-25 20:56:48 +10002501 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2504 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002505 }
2506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002508 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002509 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002510 uint32_t timeout;
2511
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002512 /* Just to be sure */
2513 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2514 {
2515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2516 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2517 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2518 }
2519
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002520 /*
2521 * The point is, we need to always read a full datagram at once, so we
2522 * sometimes read more then requested, and handle the additional data.
2523 * It could be the rest of the current record (while fetching the
2524 * header) and/or some other records in the same datagram.
2525 */
2526
2527 /*
2528 * Move to the next record in the already read datagram if applicable
2529 */
2530 if( ssl->next_record_offset != 0 )
2531 {
2532 if( ssl->in_left < ssl->next_record_offset )
2533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2535 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002536 }
2537
2538 ssl->in_left -= ssl->next_record_offset;
2539
2540 if( ssl->in_left != 0 )
2541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002543 ssl->next_record_offset ) );
2544 memmove( ssl->in_hdr,
2545 ssl->in_hdr + ssl->next_record_offset,
2546 ssl->in_left );
2547 }
2548
2549 ssl->next_record_offset = 0;
2550 }
2551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002553 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002554
2555 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002556 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002557 */
2558 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002561 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002562 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002563
2564 /*
2565 * A record can't be split accross datagrams. If we need to read but
2566 * are not at the beginning of a new record, the caller did something
2567 * wrong.
2568 */
2569 if( ssl->in_left != 0 )
2570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002573 }
2574
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002575 /*
2576 * Don't even try to read if time's out already.
2577 * This avoids by-passing the timer when repeatedly receiving messages
2578 * that will end up being dropped.
2579 */
2580 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002581 {
2582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002583 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002584 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002585 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002586 {
Angus Grattond8213d02016-05-25 20:56:48 +10002587 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002590 timeout = ssl->handshake->retransmit_timeout;
2591 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002592 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002595
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002596 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002597 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2598 timeout );
2599 else
2600 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002603
2604 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002606 }
2607
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002608 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002611 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002614 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002615 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002618 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002619 }
2620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002624 return( ret );
2625 }
2626
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002627 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002628 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002630 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002632 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002633 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002636 return( ret );
2637 }
2638
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002639 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002640 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002642 }
2643
Paul Bakker5121ce52009-01-03 21:22:43 +00002644 if( ret < 0 )
2645 return( ret );
2646
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002647 ssl->in_left = ret;
2648 }
2649 else
2650#endif
2651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002653 ssl->in_left, nb_want ) );
2654
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002655 while( ssl->in_left < nb_want )
2656 {
2657 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002658
2659 if( ssl_check_timer( ssl ) != 0 )
2660 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2661 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002662 {
2663 if( ssl->f_recv_timeout != NULL )
2664 {
2665 ret = ssl->f_recv_timeout( ssl->p_bio,
2666 ssl->in_hdr + ssl->in_left, len,
2667 ssl->conf->read_timeout );
2668 }
2669 else
2670 {
2671 ret = ssl->f_recv( ssl->p_bio,
2672 ssl->in_hdr + ssl->in_left, len );
2673 }
2674 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002677 ssl->in_left, nb_want ) );
2678 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002679
2680 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002682
2683 if( ret < 0 )
2684 return( ret );
2685
mohammad160352aecb92018-03-28 23:41:40 -07002686 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002687 {
Darryl Green11999bb2018-03-13 15:22:58 +00002688 MBEDTLS_SSL_DEBUG_MSG( 1,
2689 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002690 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002691 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2692 }
2693
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002694 ssl->in_left += ret;
2695 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 }
2697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
2700 return( 0 );
2701}
2702
2703/*
2704 * Flush any data not yet written
2705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002707{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002708 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002709 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002712
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002713 if( ssl->f_send == NULL )
2714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002716 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002718 }
2719
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002720 /* Avoid incrementing counter if data is flushed */
2721 if( ssl->out_left == 0 )
2722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002724 return( 0 );
2725 }
2726
Paul Bakker5121ce52009-01-03 21:22:43 +00002727 while( ssl->out_left > 0 )
2728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2730 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002731
Hanno Becker2b1e3542018-08-06 11:19:13 +01002732 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002733 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002736
2737 if( ret <= 0 )
2738 return( ret );
2739
mohammad160352aecb92018-03-28 23:41:40 -07002740 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002741 {
Darryl Green11999bb2018-03-13 15:22:58 +00002742 MBEDTLS_SSL_DEBUG_MSG( 1,
2743 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002744 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002745 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2746 }
2747
Paul Bakker5121ce52009-01-03 21:22:43 +00002748 ssl->out_left -= ret;
2749 }
2750
Hanno Becker2b1e3542018-08-06 11:19:13 +01002751#if defined(MBEDTLS_SSL_PROTO_DTLS)
2752 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2753 {
2754 ssl->out_hdr = ssl->out_buf;
2755 }
2756 else
2757#endif
2758 {
2759 ssl->out_hdr = ssl->out_buf + 8;
2760 }
2761 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002764
2765 return( 0 );
2766}
2767
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002768/*
2769 * Functions to handle the DTLS retransmission state machine
2770 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002772/*
2773 * Append current handshake message to current outgoing flight
2774 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002776{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2779 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2780 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002781
2782 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002783 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002784 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002787 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002788 }
2789
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002790 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002791 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002794 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002795 }
2796
2797 /* Copy current handshake message with headers */
2798 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2799 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002800 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002801 msg->next = NULL;
2802
2803 /* Append to the current flight */
2804 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002805 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002806 else
2807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002809 while( cur->next != NULL )
2810 cur = cur->next;
2811 cur->next = msg;
2812 }
2813
Hanno Becker3b235902018-08-06 09:54:53 +01002814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002815 return( 0 );
2816}
2817
2818/*
2819 * Free the current flight of handshake messages
2820 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002822{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 mbedtls_ssl_flight_item *cur = flight;
2824 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002825
2826 while( cur != NULL )
2827 {
2828 next = cur->next;
2829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830 mbedtls_free( cur->p );
2831 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002832
2833 cur = next;
2834 }
2835}
2836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2838static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002839#endif
2840
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002841/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002842 * Swap transform_out and out_ctr with the alternative ones
2843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002845{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002847 unsigned char tmp_out_ctr[8];
2848
2849 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002852 return;
2853 }
2854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002856
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002857 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002858 tmp_transform = ssl->transform_out;
2859 ssl->transform_out = ssl->handshake->alt_transform_out;
2860 ssl->handshake->alt_transform_out = tmp_transform;
2861
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002862 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002863 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2864 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002865 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002866
2867 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01002868 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2871 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2876 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002877 }
2878 }
2879#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002880}
2881
2882/*
2883 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002884 */
2885int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2886{
2887 int ret = 0;
2888
2889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2890
2891 ret = mbedtls_ssl_flight_transmit( ssl );
2892
2893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2894
2895 return( ret );
2896}
2897
2898/*
2899 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002900 *
2901 * Need to remember the current message in case flush_output returns
2902 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002903 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002904 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002905int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002906{
Hanno Becker67bc7c32018-08-06 11:33:50 +01002907 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002908 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002911 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002913
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002914 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002915 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002916 ssl_swap_epochs( ssl );
2917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002919 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002920
2921 while( ssl->handshake->cur_msg != NULL )
2922 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002923 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002924 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002925
Hanno Beckere1dcb032018-08-17 16:47:58 +01002926 int const is_finished =
2927 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2928 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2929
Hanno Becker04da1892018-08-14 13:22:10 +01002930 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2931 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2932
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002933 /* Swap epochs before sending Finished: we can't do it after
2934 * sending ChangeCipherSpec, in case write returns WANT_READ.
2935 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002936 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002937 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002939 ssl_swap_epochs( ssl );
2940 }
2941
Hanno Becker67bc7c32018-08-06 11:33:50 +01002942 ret = ssl_get_remaining_payload_in_datagram( ssl );
2943 if( ret < 0 )
2944 return( ret );
2945 max_frag_len = (size_t) ret;
2946
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002947 /* CCS is copied as is, while HS messages may need fragmentation */
2948 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2949 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002950 if( max_frag_len == 0 )
2951 {
2952 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2953 return( ret );
2954
2955 continue;
2956 }
2957
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002958 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002959 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002960 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002961
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002962 /* Update position inside current message */
2963 ssl->handshake->cur_msg_p += cur->len;
2964 }
2965 else
2966 {
2967 const unsigned char * const p = ssl->handshake->cur_msg_p;
2968 const size_t hs_len = cur->len - 12;
2969 const size_t frag_off = p - ( cur->p + 12 );
2970 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002971 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002972
Hanno Beckere1dcb032018-08-17 16:47:58 +01002973 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002974 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002975 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002976 ssl_swap_epochs( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002977
2978 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2979 return( ret );
2980
2981 continue;
2982 }
2983 max_hs_frag_len = max_frag_len - 12;
2984
2985 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2986 max_hs_frag_len : rem_len;
2987
2988 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002989 {
2990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01002991 (unsigned) cur_hs_frag_len,
2992 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002993 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002994
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002995 /* Messages are stored with handshake headers as if not fragmented,
2996 * copy beginning of headers then fill fragmentation fields.
2997 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
2998 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002999
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003000 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3001 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3002 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3003
Hanno Becker67bc7c32018-08-06 11:33:50 +01003004 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3005 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3006 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003007
3008 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3009
Hanno Becker67bc7c32018-08-06 11:33:50 +01003010 /* Copy the handshame message content and set records fields */
3011 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3012 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003013 ssl->out_msgtype = cur->type;
3014
3015 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003016 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003017 }
3018
3019 /* If done with the current message move to the next one if any */
3020 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3021 {
3022 if( cur->next != NULL )
3023 {
3024 ssl->handshake->cur_msg = cur->next;
3025 ssl->handshake->cur_msg_p = cur->next->p + 12;
3026 }
3027 else
3028 {
3029 ssl->handshake->cur_msg = NULL;
3030 ssl->handshake->cur_msg_p = NULL;
3031 }
3032 }
3033
3034 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003035 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003038 return( ret );
3039 }
3040 }
3041
Hanno Becker67bc7c32018-08-06 11:33:50 +01003042 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3043 return( ret );
3044
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003045 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3047 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003048 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003050 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003051 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3052 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003053
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003055
3056 return( 0 );
3057}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003058
3059/*
3060 * To be called when the last message of an incoming flight is received.
3061 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003063{
3064 /* We won't need to resend that one any more */
3065 ssl_flight_free( ssl->handshake->flight );
3066 ssl->handshake->flight = NULL;
3067 ssl->handshake->cur_msg = NULL;
3068
3069 /* The next incoming flight will start with this msg_seq */
3070 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3071
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003072 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003073 ssl_set_timer( ssl, 0 );
3074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3076 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003079 }
3080 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003082}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003083
3084/*
3085 * To be called when the last message of an outgoing flight is send.
3086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003088{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003089 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003090 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3093 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003096 }
3097 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003099}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003100#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003101
Paul Bakker5121ce52009-01-03 21:22:43 +00003102/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003103 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003104 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003105
3106/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003107 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003108 *
3109 * - fill in handshake headers
3110 * - update handshake checksum
3111 * - DTLS: save message for resending
3112 * - then pass to the record layer
3113 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003114 * DTLS: except for HelloRequest, messages are only queued, and will only be
3115 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003116 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003117 * Inputs:
3118 * - ssl->out_msglen: 4 + actual handshake message len
3119 * (4 is the size of handshake headers for TLS)
3120 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3121 * - ssl->out_msg + 4: the handshake message body
3122 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003123 * Ouputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003124 * - ssl->out_msglen: the length of the record contents
3125 * (including handshake headers but excluding record headers)
3126 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003127 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003128int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003129{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003130 int ret;
3131 const size_t hs_len = ssl->out_msglen - 4;
3132 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003133
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003134 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3135
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003136 /*
3137 * Sanity checks
3138 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003139 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3140 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3141 {
3142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3143 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3144 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003145
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003146 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3147 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3148 ssl->handshake == NULL )
3149 {
3150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3151 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3152 }
3153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003155 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003156 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003158 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3160 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003161 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003162#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003163
Hanno Beckerb50a2532018-08-06 11:52:54 +01003164 /* Double-check that we did not exceed the bounds
3165 * of the outgoing record buffer.
3166 * This should never fail as the various message
3167 * writing functions must obey the bounds of the
3168 * outgoing record buffer, but better be safe.
3169 *
3170 * Note: We deliberately do not check for the MTU or MFL here.
3171 */
3172 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3173 {
3174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3175 "size %u, maximum %u",
3176 (unsigned) ssl->out_msglen,
3177 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3178 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3179 }
3180
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003181 /*
3182 * Fill handshake headers
3183 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003185 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003186 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3187 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3188 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003189
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003190 /*
3191 * DTLS has additional fields in the Handshake layer,
3192 * between the length field and the actual payload:
3193 * uint16 message_seq;
3194 * uint24 fragment_offset;
3195 * uint24 fragment_length;
3196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003198 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003199 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003200 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003201 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003202 {
3203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3204 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003205 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003206 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003207 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3208 }
3209
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003210 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003211 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003212
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003213 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003214 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003215 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003216 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3217 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3218 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003219 }
3220 else
3221 {
3222 ssl->out_msg[4] = 0;
3223 ssl->out_msg[5] = 0;
3224 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003225
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003226 /* Handshake hashes are computed without fragmentation,
3227 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003228 memset( ssl->out_msg + 6, 0x00, 3 );
3229 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003230 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003232
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003233 /* Update running hashes of hanshake messages seen */
3234 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3235 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003236 }
3237
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003238 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003240 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003241 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003242 {
3243 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003246 return( ret );
3247 }
3248 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003249 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003250#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003251 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003252 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003253 {
3254 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3255 return( ret );
3256 }
3257 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003258
3259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3260
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003261 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003262}
3263
3264/*
3265 * Record layer functions
3266 */
3267
3268/*
3269 * Write current record.
3270 *
3271 * Uses:
3272 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3273 * - ssl->out_msglen: length of the record content (excl headers)
3274 * - ssl->out_msg: record content
3275 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003276int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003277{
3278 int ret, done = 0;
3279 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003280 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003281
3282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
3283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003285 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003286 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003287 {
3288 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003291 return( ret );
3292 }
3293
3294 len = ssl->out_msglen;
3295 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003298#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3299 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303 ret = mbedtls_ssl_hw_record_write( ssl );
3304 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3307 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003308 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003309
3310 if( ret == 0 )
3311 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003312 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003314 if( !done )
3315 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003316 unsigned i;
3317 size_t protected_record_size;
3318
Paul Bakker05ef8352012-05-08 09:17:57 +00003319 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003321 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003322
Hanno Becker19859472018-08-06 09:40:20 +01003323 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003324 ssl->out_len[0] = (unsigned char)( len >> 8 );
3325 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003326
Paul Bakker48916f92012-09-16 19:57:18 +00003327 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003328 {
3329 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003332 return( ret );
3333 }
3334
3335 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003336 ssl->out_len[0] = (unsigned char)( len >> 8 );
3337 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003338 }
3339
Hanno Becker2b1e3542018-08-06 11:19:13 +01003340 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3341
3342#if defined(MBEDTLS_SSL_PROTO_DTLS)
3343 /* In case of DTLS, double-check that we don't exceed
3344 * the remaining space in the datagram. */
3345 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3346 {
3347 ret = ssl_get_maximum_datagram_size( ssl );
3348 if( ret < 0 )
3349 return( ret );
3350
3351 if( protected_record_size > (size_t) ret )
3352 {
3353 /* Should never happen */
3354 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3355 }
3356 }
3357#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Becker2b1e3542018-08-06 11:19:13 +01003360 "version = [%d:%d], msglen = %d",
3361 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2], len ) );
3362
Paul Bakker05ef8352012-05-08 09:17:57 +00003363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Becker2b1e3542018-08-06 11:19:13 +01003365 ssl->out_hdr, protected_record_size );
3366
3367 ssl->out_left += protected_record_size;
3368 ssl->out_hdr += protected_record_size;
3369 ssl_update_out_pointers( ssl, ssl->transform_out );
3370
Hanno Becker04484622018-08-06 09:49:38 +01003371 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3372 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3373 break;
3374
3375 /* The loop goes to its end iff the counter is wrapping */
3376 if( i == ssl_ep_len( ssl ) )
3377 {
3378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3379 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3380 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003381 }
3382
Hanno Becker67bc7c32018-08-06 11:33:50 +01003383#if defined(MBEDTLS_SSL_PROTO_DTLS)
3384 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3385 {
3386 size_t remaining = ssl_get_remaining_payload_in_datagram( ssl );
3387 if( remaining == 0 )
3388 flush = SSL_FORCE_FLUSH;
3389 else
3390 {
3391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Stil %u bytes available in current datagram", (unsigned) remaining ) );
3392 }
3393 }
3394#endif /* MBEDTLS_SSL_PROTO_DTLS */
3395
3396 if( ( flush == SSL_FORCE_FLUSH ) &&
3397 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003400 return( ret );
3401 }
3402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003404
3405 return( 0 );
3406}
3407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003409/*
3410 * Mark bits in bitmask (used for DTLS HS reassembly)
3411 */
3412static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3413{
3414 unsigned int start_bits, end_bits;
3415
3416 start_bits = 8 - ( offset % 8 );
3417 if( start_bits != 8 )
3418 {
3419 size_t first_byte_idx = offset / 8;
3420
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003421 /* Special case */
3422 if( len <= start_bits )
3423 {
3424 for( ; len != 0; len-- )
3425 mask[first_byte_idx] |= 1 << ( start_bits - len );
3426
3427 /* Avoid potential issues with offset or len becoming invalid */
3428 return;
3429 }
3430
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003431 offset += start_bits; /* Now offset % 8 == 0 */
3432 len -= start_bits;
3433
3434 for( ; start_bits != 0; start_bits-- )
3435 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3436 }
3437
3438 end_bits = len % 8;
3439 if( end_bits != 0 )
3440 {
3441 size_t last_byte_idx = ( offset + len ) / 8;
3442
3443 len -= end_bits; /* Now len % 8 == 0 */
3444
3445 for( ; end_bits != 0; end_bits-- )
3446 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3447 }
3448
3449 memset( mask + offset / 8, 0xFF, len / 8 );
3450}
3451
3452/*
3453 * Check that bitmask is full
3454 */
3455static int ssl_bitmask_check( unsigned char *mask, size_t len )
3456{
3457 size_t i;
3458
3459 for( i = 0; i < len / 8; i++ )
3460 if( mask[i] != 0xFF )
3461 return( -1 );
3462
3463 for( i = 0; i < len % 8; i++ )
3464 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3465 return( -1 );
3466
3467 return( 0 );
3468}
3469
3470/*
3471 * Reassemble fragmented DTLS handshake messages.
3472 *
3473 * Use a temporary buffer for reassembly, divided in two parts:
3474 * - the first holds the reassembled message (including handshake header),
3475 * - the second holds a bitmask indicating which parts of the message
3476 * (excluding headers) have been received so far.
3477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003479{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003480 unsigned char *msg, *bitmask;
3481 size_t frag_len, frag_off;
3482 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
3483
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003484 if( ssl->handshake == NULL )
3485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
3487 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003488 }
3489
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003490 /*
3491 * For first fragment, check size and allocate buffer
3492 */
3493 if( ssl->handshake->hs_msg == NULL )
3494 {
3495 size_t alloc_len;
3496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003498 msg_len ) );
3499
Angus Grattond8213d02016-05-25 20:56:48 +10003500 if( ssl->in_hslen > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3503 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003504 }
3505
3506 /* The bitmask needs one bit per byte of message excluding header */
3507 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
3508
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003509 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003510 if( ssl->handshake->hs_msg == NULL )
3511 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003513 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003514 }
3515
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003516 /* Prepare final header: copy msg_type, length and message_seq,
3517 * then add standardised fragment_offset and fragment_length */
3518 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
3519 memset( ssl->handshake->hs_msg + 6, 0, 3 );
3520 memcpy( ssl->handshake->hs_msg + 9,
3521 ssl->handshake->hs_msg + 1, 3 );
3522 }
3523 else
3524 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003525 /* Make sure msg_type and length are consistent */
3526 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
3529 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003530 }
3531 }
3532
3533 msg = ssl->handshake->hs_msg + 12;
3534 bitmask = msg + msg_len;
3535
3536 /*
3537 * Check and copy current fragment
3538 */
3539 frag_off = ( ssl->in_msg[6] << 16 ) |
3540 ( ssl->in_msg[7] << 8 ) |
3541 ssl->in_msg[8];
3542 frag_len = ( ssl->in_msg[9] << 16 ) |
3543 ( ssl->in_msg[10] << 8 ) |
3544 ssl->in_msg[11];
3545
3546 if( frag_off + frag_len > msg_len )
3547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003549 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003550 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003551 }
3552
3553 if( frag_len + 12 > ssl->in_msglen )
3554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003556 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003558 }
3559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003561 frag_off, frag_len ) );
3562
3563 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3564 ssl_bitmask_set( bitmask, frag_off, frag_len );
3565
3566 /*
3567 * Do we have the complete message by now?
3568 * If yes, finalize it, else ask to read the next record.
3569 */
3570 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01003573 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003574 }
3575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003577
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003578 if( frag_len + 12 < ssl->in_msglen )
3579 {
3580 /*
3581 * We'got more handshake messages in the same record.
3582 * This case is not handled now because no know implementation does
3583 * that and it's hard to test, so we prefer to fail cleanly for now.
3584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3586 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003587 }
3588
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003589 if( ssl->in_left > ssl->next_record_offset )
3590 {
3591 /*
3592 * We've got more data in the buffer after the current record,
3593 * that we don't want to overwrite. Move it before writing the
3594 * reassembled message, and adjust in_left and next_record_offset.
3595 */
3596 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3597 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3598 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3599
3600 /* First compute and check new lengths */
3601 ssl->next_record_offset = new_remain - ssl->in_hdr;
3602 ssl->in_left = ssl->next_record_offset + remain_len;
3603
Angus Grattond8213d02016-05-25 20:56:48 +10003604 if( ssl->in_left > MBEDTLS_SSL_IN_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003605 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3608 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003609 }
3610
3611 memmove( new_remain, cur_remain, remain_len );
3612 }
3613
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003614 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003617 ssl->handshake->hs_msg = NULL;
3618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003620 ssl->in_msg, ssl->in_hslen );
3621
3622 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003623}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003625
Simon Butcher99000142016-10-13 17:21:01 +01003626int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003627{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003631 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003633 }
3634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003636 ( ssl->in_msg[1] << 16 ) |
3637 ( ssl->in_msg[2] << 8 ) |
3638 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003641 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003642 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003645 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003646 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003647 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003648 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003649
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003650 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003651 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3652 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3653 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3654 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003655 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003656 /* Retransmit only on last message from previous flight, to avoid
3657 * too many retransmissions.
3658 * Besides, No sane server ever retransmits HelloVerifyRequest */
3659 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003663 "message_seq = %d, start_of_flight = %d",
3664 recv_msg_seq,
3665 ssl->handshake->in_flight_start_seq ) );
3666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003670 return( ret );
3671 }
3672 }
3673 else
3674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003676 "message_seq = %d, expected = %d",
3677 recv_msg_seq,
3678 ssl->handshake->in_msg_seq ) );
3679 }
3680
Hanno Becker90333da2017-10-10 11:27:13 +01003681 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003682 }
3683 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003684
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003685 /* Reassemble if current message is fragmented or reassembly is
3686 * already in progress */
3687 if( ssl->in_msglen < ssl->in_hslen ||
3688 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3689 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3690 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003693
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003694 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003697 return( ret );
3698 }
3699 }
3700 }
3701 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003702#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003703 /* With TLS we don't handle fragmentation (for now) */
3704 if( ssl->in_msglen < ssl->in_hslen )
3705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3707 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003708 }
3709
Simon Butcher99000142016-10-13 17:21:01 +01003710 return( 0 );
3711}
3712
3713void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3714{
3715
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003716 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3717 ssl->handshake != NULL )
3718 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003719 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003720 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003721
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003722 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003723#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003724 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003725 ssl->handshake != NULL )
3726 {
3727 ssl->handshake->in_msg_seq++;
3728 }
3729#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003730}
3731
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003732/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003733 * DTLS anti-replay: RFC 6347 4.1.2.6
3734 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003735 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3736 * Bit n is set iff record number in_window_top - n has been seen.
3737 *
3738 * Usually, in_window_top is the last record number seen and the lsb of
3739 * in_window is set. The only exception is the initial state (record number 0
3740 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003741 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003742#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3743static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003744{
3745 ssl->in_window_top = 0;
3746 ssl->in_window = 0;
3747}
3748
3749static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3750{
3751 return( ( (uint64_t) buf[0] << 40 ) |
3752 ( (uint64_t) buf[1] << 32 ) |
3753 ( (uint64_t) buf[2] << 24 ) |
3754 ( (uint64_t) buf[3] << 16 ) |
3755 ( (uint64_t) buf[4] << 8 ) |
3756 ( (uint64_t) buf[5] ) );
3757}
3758
3759/*
3760 * Return 0 if sequence number is acceptable, -1 otherwise
3761 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003762int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003763{
3764 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3765 uint64_t bit;
3766
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003767 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003768 return( 0 );
3769
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003770 if( rec_seqnum > ssl->in_window_top )
3771 return( 0 );
3772
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003773 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003774
3775 if( bit >= 64 )
3776 return( -1 );
3777
3778 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3779 return( -1 );
3780
3781 return( 0 );
3782}
3783
3784/*
3785 * Update replay window on new validated record
3786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003787void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003788{
3789 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3790
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003791 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003792 return;
3793
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003794 if( rec_seqnum > ssl->in_window_top )
3795 {
3796 /* Update window_top and the contents of the window */
3797 uint64_t shift = rec_seqnum - ssl->in_window_top;
3798
3799 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003800 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003801 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003802 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003803 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003804 ssl->in_window |= 1;
3805 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003806
3807 ssl->in_window_top = rec_seqnum;
3808 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003809 else
3810 {
3811 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003812 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003813
3814 if( bit < 64 ) /* Always true, but be extra sure */
3815 ssl->in_window |= (uint64_t) 1 << bit;
3816 }
3817}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003818#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003819
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003820#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003821/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003822static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3823
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003824/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003825 * Without any SSL context, check if a datagram looks like a ClientHello with
3826 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003827 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003828 *
3829 * - if cookie is valid, return 0
3830 * - if ClientHello looks superficially valid but cookie is not,
3831 * fill obuf and set olen, then
3832 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3833 * - otherwise return a specific error code
3834 */
3835static int ssl_check_dtls_clihlo_cookie(
3836 mbedtls_ssl_cookie_write_t *f_cookie_write,
3837 mbedtls_ssl_cookie_check_t *f_cookie_check,
3838 void *p_cookie,
3839 const unsigned char *cli_id, size_t cli_id_len,
3840 const unsigned char *in, size_t in_len,
3841 unsigned char *obuf, size_t buf_len, size_t *olen )
3842{
3843 size_t sid_len, cookie_len;
3844 unsigned char *p;
3845
3846 if( f_cookie_write == NULL || f_cookie_check == NULL )
3847 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3848
3849 /*
3850 * Structure of ClientHello with record and handshake headers,
3851 * and expected values. We don't need to check a lot, more checks will be
3852 * done when actually parsing the ClientHello - skipping those checks
3853 * avoids code duplication and does not make cookie forging any easier.
3854 *
3855 * 0-0 ContentType type; copied, must be handshake
3856 * 1-2 ProtocolVersion version; copied
3857 * 3-4 uint16 epoch; copied, must be 0
3858 * 5-10 uint48 sequence_number; copied
3859 * 11-12 uint16 length; (ignored)
3860 *
3861 * 13-13 HandshakeType msg_type; (ignored)
3862 * 14-16 uint24 length; (ignored)
3863 * 17-18 uint16 message_seq; copied
3864 * 19-21 uint24 fragment_offset; copied, must be 0
3865 * 22-24 uint24 fragment_length; (ignored)
3866 *
3867 * 25-26 ProtocolVersion client_version; (ignored)
3868 * 27-58 Random random; (ignored)
3869 * 59-xx SessionID session_id; 1 byte len + sid_len content
3870 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3871 * ...
3872 *
3873 * Minimum length is 61 bytes.
3874 */
3875 if( in_len < 61 ||
3876 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3877 in[3] != 0 || in[4] != 0 ||
3878 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3879 {
3880 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3881 }
3882
3883 sid_len = in[59];
3884 if( sid_len > in_len - 61 )
3885 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3886
3887 cookie_len = in[60 + sid_len];
3888 if( cookie_len > in_len - 60 )
3889 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3890
3891 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3892 cli_id, cli_id_len ) == 0 )
3893 {
3894 /* Valid cookie */
3895 return( 0 );
3896 }
3897
3898 /*
3899 * If we get here, we've got an invalid cookie, let's prepare HVR.
3900 *
3901 * 0-0 ContentType type; copied
3902 * 1-2 ProtocolVersion version; copied
3903 * 3-4 uint16 epoch; copied
3904 * 5-10 uint48 sequence_number; copied
3905 * 11-12 uint16 length; olen - 13
3906 *
3907 * 13-13 HandshakeType msg_type; hello_verify_request
3908 * 14-16 uint24 length; olen - 25
3909 * 17-18 uint16 message_seq; copied
3910 * 19-21 uint24 fragment_offset; copied
3911 * 22-24 uint24 fragment_length; olen - 25
3912 *
3913 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3914 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3915 *
3916 * Minimum length is 28.
3917 */
3918 if( buf_len < 28 )
3919 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3920
3921 /* Copy most fields and adapt others */
3922 memcpy( obuf, in, 25 );
3923 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3924 obuf[25] = 0xfe;
3925 obuf[26] = 0xff;
3926
3927 /* Generate and write actual cookie */
3928 p = obuf + 28;
3929 if( f_cookie_write( p_cookie,
3930 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3931 {
3932 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3933 }
3934
3935 *olen = p - obuf;
3936
3937 /* Go back and fill length fields */
3938 obuf[27] = (unsigned char)( *olen - 28 );
3939
3940 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3941 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3942 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3943
3944 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3945 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3946
3947 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3948}
3949
3950/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003951 * Handle possible client reconnect with the same UDP quadruplet
3952 * (RFC 6347 Section 4.2.8).
3953 *
3954 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3955 * that looks like a ClientHello.
3956 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003957 * - if the input looks like a ClientHello without cookies,
3958 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003959 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003960 * - if the input looks like a ClientHello with a valid cookie,
3961 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003962 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003963 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003964 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003965 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003966 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3967 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003968 */
3969static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3970{
3971 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003972 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003973
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003974 ret = ssl_check_dtls_clihlo_cookie(
3975 ssl->conf->f_cookie_write,
3976 ssl->conf->f_cookie_check,
3977 ssl->conf->p_cookie,
3978 ssl->cli_id, ssl->cli_id_len,
3979 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003980 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003981
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003982 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3983
3984 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003985 {
Brian J Murray1903fb32016-11-06 04:45:15 -08003986 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003987 * If the error is permanent we'll catch it later,
3988 * if it's not, then hopefully it'll work next time. */
3989 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3990
3991 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003992 }
3993
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003994 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003995 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003996 /* Got a valid cookie, partially reset context */
3997 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3998 {
3999 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4000 return( ret );
4001 }
4002
4003 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004004 }
4005
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004006 return( ret );
4007}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004008#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004009
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004010/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004011 * ContentType type;
4012 * ProtocolVersion version;
4013 * uint16 epoch; // DTLS only
4014 * uint48 sequence_number; // DTLS only
4015 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004016 *
4017 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004018 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004019 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4020 *
4021 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004022 * 1. proceed with the record if this function returns 0
4023 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4024 * 3. return CLIENT_RECONNECT if this function return that value
4025 * 4. drop the whole datagram if this function returns anything else.
4026 * Point 2 is needed when the peer is resending, and we have already received
4027 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004028 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004029static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004030{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004031 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004033 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004034
Paul Bakker5121ce52009-01-03 21:22:43 +00004035 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004036 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004037 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004040 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004041 ssl->in_msgtype,
4042 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004043
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004044 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004045 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4046 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4047 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4048 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004051
4052#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004053 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4054 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004055 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4056#endif /* MBEDTLS_SSL_PROTO_DTLS */
4057 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4058 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004060 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004061 }
4062
4063 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004064 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4067 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004068 }
4069
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004070 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4073 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004074 }
4075
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004076 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004077 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004078 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4081 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004082 }
4083
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004084 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004085 * DTLS-related tests.
4086 * Check epoch before checking length constraint because
4087 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4088 * message gets duplicated before the corresponding Finished message,
4089 * the second ChangeCipherSpec should be discarded because it belongs
4090 * to an old epoch, but not because its length is shorter than
4091 * the minimum record length for packets using the new record transform.
4092 * Note that these two kinds of failures are handled differently,
4093 * as an unexpected record is silently skipped but an invalid
4094 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004095 */
4096#if defined(MBEDTLS_SSL_PROTO_DTLS)
4097 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4098 {
4099 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4100
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004101 /* Check epoch (and sequence number) with DTLS */
4102 if( rec_epoch != ssl->in_epoch )
4103 {
4104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4105 "expected %d, received %d",
4106 ssl->in_epoch, rec_epoch ) );
4107
4108#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4109 /*
4110 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4111 * access the first byte of record content (handshake type), as we
4112 * have an active transform (possibly iv_len != 0), so use the
4113 * fact that the record header len is 13 instead.
4114 */
4115 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4116 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4117 rec_epoch == 0 &&
4118 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4119 ssl->in_left > 13 &&
4120 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4121 {
4122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4123 "from the same port" ) );
4124 return( ssl_handle_possible_reconnect( ssl ) );
4125 }
4126 else
4127#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
4128 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4129 }
4130
4131#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4132 /* Replay detection only works for the current epoch */
4133 if( rec_epoch == ssl->in_epoch &&
4134 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4135 {
4136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4137 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4138 }
4139#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004140
4141 /* Drop unexpected ChangeCipherSpec messages */
4142 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4143 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4144 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4145 {
4146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
4147 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4148 }
4149
4150 /* Drop unexpected ApplicationData records,
4151 * except at the beginning of renegotiations */
4152 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4153 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4154#if defined(MBEDTLS_SSL_RENEGOTIATION)
4155 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4156 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4157#endif
4158 )
4159 {
4160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4161 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4162 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004163 }
4164#endif /* MBEDTLS_SSL_PROTO_DTLS */
4165
Hanno Becker52c6dc62017-05-26 16:07:36 +01004166
4167 /* Check length against bounds of the current transform and version */
4168 if( ssl->transform_in == NULL )
4169 {
4170 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004171 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004172 {
4173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4174 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4175 }
4176 }
4177 else
4178 {
4179 if( ssl->in_msglen < ssl->transform_in->minlen )
4180 {
4181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4182 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4183 }
4184
4185#if defined(MBEDTLS_SSL_PROTO_SSL3)
4186 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004187 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004188 {
4189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4190 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4191 }
4192#endif
4193#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4194 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4195 /*
4196 * TLS encrypted messages can have up to 256 bytes of padding
4197 */
4198 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4199 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004200 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004201 {
4202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4203 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4204 }
4205#endif
4206 }
4207
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004208 return( 0 );
4209}
Paul Bakker5121ce52009-01-03 21:22:43 +00004210
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004211/*
4212 * If applicable, decrypt (and decompress) record content
4213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004214static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004215{
4216 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004218 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4219 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004221#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4222 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004226 ret = mbedtls_ssl_hw_record_read( ssl );
4227 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004229 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4230 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004231 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004232
4233 if( ret == 0 )
4234 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004235 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004236#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004237 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004238 {
4239 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004241 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004242 return( ret );
4243 }
4244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004245 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004246 ssl->in_msg, ssl->in_msglen );
4247
Angus Grattond8213d02016-05-25 20:56:48 +10004248 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4251 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004252 }
4253 }
4254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004255#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004256 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004257 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004258 {
4259 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004261 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004262 return( ret );
4263 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004264 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004265#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004267#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004268 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004270 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004271 }
4272#endif
4273
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004274 return( 0 );
4275}
4276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004277static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004278
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004279/*
4280 * Read a record.
4281 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004282 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4283 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4284 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004285 */
Hanno Becker1097b342018-08-15 14:09:41 +01004286
4287/* Helper functions for mbedtls_ssl_read_record(). */
4288static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004289static int ssl_read_record_layer( mbedtls_ssl_context *ssl );
4290
Hanno Becker327c93b2018-08-15 13:56:18 +01004291int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
4292 unsigned update_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004293{
4294 int ret;
4295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004297
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004298 if( ssl->keep_current_message == 0 )
4299 {
4300 do {
Simon Butcher99000142016-10-13 17:21:01 +01004301
Hanno Becker4162b112018-08-15 14:05:04 +01004302 ret = ssl_read_record_layer( ssl );
Hanno Beckera4b143a2018-08-15 14:01:34 +01004303 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4304 continue;
Hanno Becker90333da2017-10-10 11:27:13 +01004305
4306 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004307 {
4308 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4309 return( ret );
4310 }
4311
4312 ret = mbedtls_ssl_handle_message_type( ssl );
4313
Hanno Becker90333da2017-10-10 11:27:13 +01004314 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4315 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004316
4317 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004318 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004319 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004320 return( ret );
4321 }
4322
Hanno Becker327c93b2018-08-15 13:56:18 +01004323 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4324 update_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004325 {
4326 mbedtls_ssl_update_handshake_status( ssl );
4327 }
Simon Butcher99000142016-10-13 17:21:01 +01004328 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004329 else
Simon Butcher99000142016-10-13 17:21:01 +01004330 {
Hanno Becker02f59072018-08-15 14:00:24 +01004331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004332 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004333 }
4334
4335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4336
4337 return( 0 );
4338}
4339
Hanno Becker1097b342018-08-15 14:09:41 +01004340static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004341{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004342 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004343 * Consume last content-layer message and potentially
4344 * update in_msglen which keeps track of the contents'
4345 * consumption state.
4346 *
4347 * (1) Handshake messages:
4348 * Remove last handshake message, move content
4349 * and adapt in_msglen.
4350 *
4351 * (2) Alert messages:
4352 * Consume whole record content, in_msglen = 0.
4353 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004354 * (3) Change cipher spec:
4355 * Consume whole record content, in_msglen = 0.
4356 *
4357 * (4) Application data:
4358 * Don't do anything - the record layer provides
4359 * the application data as a stream transport
4360 * and consumes through mbedtls_ssl_read only.
4361 *
4362 */
4363
4364 /* Case (1): Handshake messages */
4365 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004366 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004367 /* Hard assertion to be sure that no application data
4368 * is in flight, as corrupting ssl->in_msglen during
4369 * ssl->in_offt != NULL is fatal. */
4370 if( ssl->in_offt != NULL )
4371 {
4372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4373 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4374 }
4375
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004376 /*
4377 * Get next Handshake message in the current record
4378 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004379
Hanno Becker4a810fb2017-05-24 16:27:30 +01004380 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004381 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004382 * current handshake content: If DTLS handshake
4383 * fragmentation is used, that's the fragment
4384 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004385 * size here is faulty and should be changed at
4386 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004387 * (2) While it doesn't seem to cause problems, one
4388 * has to be very careful not to assume that in_hslen
4389 * is always <= in_msglen in a sensible communication.
4390 * Again, it's wrong for DTLS handshake fragmentation.
4391 * The following check is therefore mandatory, and
4392 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004393 * Additionally, ssl->in_hslen might be arbitrarily out of
4394 * bounds after handling a DTLS message with an unexpected
4395 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004396 */
4397 if( ssl->in_hslen < ssl->in_msglen )
4398 {
4399 ssl->in_msglen -= ssl->in_hslen;
4400 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4401 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004402
Hanno Becker4a810fb2017-05-24 16:27:30 +01004403 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4404 ssl->in_msg, ssl->in_msglen );
4405 }
4406 else
4407 {
4408 ssl->in_msglen = 0;
4409 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004410
Hanno Becker4a810fb2017-05-24 16:27:30 +01004411 ssl->in_hslen = 0;
4412 }
4413 /* Case (4): Application data */
4414 else if( ssl->in_offt != NULL )
4415 {
4416 return( 0 );
4417 }
4418 /* Everything else (CCS & Alerts) */
4419 else
4420 {
4421 ssl->in_msglen = 0;
4422 }
4423
Hanno Becker1097b342018-08-15 14:09:41 +01004424 return( 0 );
4425}
4426
4427static int ssl_read_record_layer( mbedtls_ssl_context *ssl )
4428{
4429 int ret;
4430
4431 /*
4432 * Step A
4433 *
4434 * Consume last content-layer message and potentially
4435 * update in_msglen which keeps track of the contents'
4436 * consumption state.
4437 */
4438
4439 ret = ssl_consume_current_message( ssl );
4440 if( ret != 0 )
4441 return( ret );
4442
Hanno Becker4a810fb2017-05-24 16:27:30 +01004443 /*
4444 * Step B
4445 *
4446 * Fetch and decode new record if current one is fully consumed.
4447 *
4448 */
4449
4450 if( ssl->in_msglen > 0 )
4451 {
4452 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004453 return( 0 );
4454 }
4455
Hanno Becker4a810fb2017-05-24 16:27:30 +01004456 /* Current record either fully processed or to be discarded. */
4457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004458 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004460 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004461 return( ret );
4462 }
4463
4464 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004466#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004467 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4468 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004469 {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004470 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4471 {
4472 /* Skip unexpected record (but not whole datagram) */
4473 ssl->next_record_offset = ssl->in_msglen
4474 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004475
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4477 "(header)" ) );
4478 }
4479 else
4480 {
4481 /* Skip invalid record and the rest of the datagram */
4482 ssl->next_record_offset = 0;
4483 ssl->in_left = 0;
4484
4485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4486 "(header)" ) );
4487 }
4488
4489 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004490 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004491 }
4492#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004493 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004494 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004495
4496 /*
4497 * Read and optionally decrypt the message contents
4498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004499 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4500 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004502 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004503 return( ret );
4504 }
4505
4506 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004507#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004508 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004510 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01004511 if( ssl->next_record_offset < ssl->in_left )
4512 {
4513 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4514 }
4515 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004516 else
4517#endif
4518 ssl->in_left = 0;
4519
4520 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004522#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004523 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004524 {
4525 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004526 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4527 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004528 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004529 /* Except when waiting for Finished as a bad mac here
4530 * probably means something went wrong in the handshake
4531 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4532 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4533 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4534 {
4535#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4536 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4537 {
4538 mbedtls_ssl_send_alert_message( ssl,
4539 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4540 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4541 }
4542#endif
4543 return( ret );
4544 }
4545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004546#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004547 if( ssl->conf->badmac_limit != 0 &&
4548 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4551 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004552 }
4553#endif
4554
Hanno Becker4a810fb2017-05-24 16:27:30 +01004555 /* As above, invalid records cause
4556 * dismissal of the whole datagram. */
4557
4558 ssl->next_record_offset = 0;
4559 ssl->in_left = 0;
4560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004562 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004563 }
4564
4565 return( ret );
4566 }
4567 else
4568#endif
4569 {
4570 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004571#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4572 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004574 mbedtls_ssl_send_alert_message( ssl,
4575 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4576 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004577 }
4578#endif
4579 return( ret );
4580 }
4581 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004582
Simon Butcher99000142016-10-13 17:21:01 +01004583 return( 0 );
4584}
4585
4586int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4587{
4588 int ret;
4589
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004590 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004591 * Handle particular types of records
4592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004593 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004594 {
Simon Butcher99000142016-10-13 17:21:01 +01004595 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4596 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004597 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004598 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004599 }
4600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004601 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004602 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004603 if( ssl->in_msglen != 2 )
4604 {
4605 /* Note: Standard allows for more than one 2 byte alert
4606 to be packed in a single message, but Mbed TLS doesn't
4607 currently support this. */
4608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4609 ssl->in_msglen ) );
4610 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4611 }
4612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004614 ssl->in_msg[0], ssl->in_msg[1] ) );
4615
4616 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004617 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004619 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004622 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004623 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004624 }
4625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004626 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4627 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4630 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004631 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004632
4633#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4634 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4635 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4636 {
Hanno Becker90333da2017-10-10 11:27:13 +01004637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004638 /* Will be handled when trying to parse ServerHello */
4639 return( 0 );
4640 }
4641#endif
4642
4643#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4644 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4645 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4646 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4647 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4648 {
4649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4650 /* Will be handled in mbedtls_ssl_parse_certificate() */
4651 return( 0 );
4652 }
4653#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4654
4655 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004656 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004657 }
4658
Hanno Beckerc76c6192017-06-06 10:03:17 +01004659#if defined(MBEDTLS_SSL_PROTO_DTLS)
4660 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4661 ssl->handshake != NULL &&
4662 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4663 {
4664 ssl_handshake_wrapup_free_hs_transform( ssl );
4665 }
4666#endif
4667
Paul Bakker5121ce52009-01-03 21:22:43 +00004668 return( 0 );
4669}
4670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004671int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004672{
4673 int ret;
4674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004675 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4676 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4677 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004678 {
4679 return( ret );
4680 }
4681
4682 return( 0 );
4683}
4684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004685int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004686 unsigned char level,
4687 unsigned char message )
4688{
4689 int ret;
4690
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004691 if( ssl == NULL || ssl->conf == NULL )
4692 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004695 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004697 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004698 ssl->out_msglen = 2;
4699 ssl->out_msg[0] = level;
4700 ssl->out_msg[1] = message;
4701
Hanno Becker67bc7c32018-08-06 11:33:50 +01004702 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004704 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004705 return( ret );
4706 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004708
4709 return( 0 );
4710}
4711
Paul Bakker5121ce52009-01-03 21:22:43 +00004712/*
4713 * Handshake functions
4714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004715#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4716 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4717 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4718 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4719 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4720 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4721 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02004722/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004723int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004724{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004725 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004729 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4730 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004731 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4732 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004735 ssl->state++;
4736 return( 0 );
4737 }
4738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4740 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004741}
4742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004743int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004744{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004745 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004749 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4750 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004751 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4752 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004755 ssl->state++;
4756 return( 0 );
4757 }
4758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4760 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004761}
Gilles Peskinef9828522017-05-03 12:28:43 +02004762
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004763#else
Gilles Peskinef9828522017-05-03 12:28:43 +02004764/* Some certificate support -> implement write and parse */
4765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004766int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004767{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004768 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004769 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004770 const mbedtls_x509_crt *crt;
4771 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004775 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4776 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004777 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4778 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004781 ssl->state++;
4782 return( 0 );
4783 }
4784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004785#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004786 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004787 {
4788 if( ssl->client_auth == 0 )
4789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004791 ssl->state++;
4792 return( 0 );
4793 }
4794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004795#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004796 /*
4797 * If using SSLv3 and got no cert, send an Alert message
4798 * (otherwise an empty Certificate message will be sent).
4799 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004800 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4801 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004802 {
4803 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004804 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4805 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4806 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004809 goto write_msg;
4810 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004811#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004812 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004813#endif /* MBEDTLS_SSL_CLI_C */
4814#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004815 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004817 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4820 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004821 }
4822 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004823#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004825 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004826
4827 /*
4828 * 0 . 0 handshake type
4829 * 1 . 3 handshake length
4830 * 4 . 6 length of all certs
4831 * 7 . 9 length of cert. 1
4832 * 10 . n-1 peer certificate
4833 * n . n+2 length of cert. 2
4834 * n+3 . ... upper level cert, etc.
4835 */
4836 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004837 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004838
Paul Bakker29087132010-03-21 21:03:34 +00004839 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004840 {
4841 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10004842 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10004845 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004846 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004847 }
4848
4849 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4850 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4851 ssl->out_msg[i + 2] = (unsigned char)( n );
4852
4853 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4854 i += n; crt = crt->next;
4855 }
4856
4857 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4858 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4859 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4860
4861 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004862 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4863 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004864
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004865#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004866write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004867#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004868
4869 ssl->state++;
4870
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004871 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004872 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004873 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004874 return( ret );
4875 }
4876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004878
Paul Bakkered27a042013-04-18 22:46:23 +02004879 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004880}
4881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004882int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004883{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004884 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004885 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004886 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004887 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02004888 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00004889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004892 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4893 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004894 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4895 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004898 ssl->state++;
4899 return( 0 );
4900 }
4901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004902#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004903 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004904 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4905 {
4906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4907 ssl->state++;
4908 return( 0 );
4909 }
4910
4911#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4912 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4913 authmode = ssl->handshake->sni_authmode;
4914#endif
4915
4916 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4917 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004918 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004919 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004921 ssl->state++;
4922 return( 0 );
4923 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004924#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004925
Hanno Becker327c93b2018-08-15 13:56:18 +01004926 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004927 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004928 /* mbedtls_ssl_read_record may have sent an alert already. We
4929 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004930 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004931 return( ret );
4932 }
4933
4934 ssl->state++;
4935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004936#if defined(MBEDTLS_SSL_SRV_C)
4937#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004938 /*
4939 * Check if the client sent an empty certificate
4940 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004941 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004943 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004944 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004945 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4946 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4947 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004950
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004951 /* The client was asked for a certificate but didn't send
4952 one. The client should know what's going on, so we
4953 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004954 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004955 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004956 return( 0 );
4957 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004958 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004959 }
4960 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004961#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004963#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4964 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004965 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004966 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004968 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4969 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4970 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4971 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004974
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004975 /* The client was asked for a certificate but didn't send
4976 one. The client should know what's going on, so we
4977 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004978 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004979 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004980 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004981 else
4982 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004983 }
4984 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004985#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4986 MBEDTLS_SSL_PROTO_TLS1_2 */
4987#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004989 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004992 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4993 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004994 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004995 }
4996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004997 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
4998 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005001 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5002 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005003 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005004 }
5005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005006 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005007
Paul Bakker5121ce52009-01-03 21:22:43 +00005008 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005009 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005010 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005011 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005012
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005013 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005014 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005017 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5018 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005019 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005020 }
5021
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005022 /* In case we tried to reuse a session but it failed */
5023 if( ssl->session_negotiate->peer_cert != NULL )
5024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005025 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5026 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005027 }
5028
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005029 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005030 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005031 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005033 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005034 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5035 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005036 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005037 }
5038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005039 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005040
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005041 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005042
5043 while( i < ssl->in_hslen )
5044 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005045 if ( i + 3 > ssl->in_hslen ) {
5046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5047 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5048 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5049 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5050 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005051 if( ssl->in_msg[i] != 0 )
5052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005054 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5055 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005056 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005057 }
5058
5059 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5060 | (unsigned int) ssl->in_msg[i + 2];
5061 i += 3;
5062
5063 if( n < 128 || i + n > ssl->in_hslen )
5064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005066 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5067 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005068 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005069 }
5070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005071 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005072 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005073 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005074 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005075 case 0: /*ok*/
5076 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5077 /* Ignore certificate with an unknown algorithm: maybe a
5078 prior certificate was already trusted. */
5079 break;
5080
5081 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5082 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5083 goto crt_parse_der_failed;
5084
5085 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5086 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5087 goto crt_parse_der_failed;
5088
5089 default:
5090 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5091 crt_parse_der_failed:
5092 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005093 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005094 return( ret );
5095 }
5096
5097 i += n;
5098 }
5099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005100 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005101
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005102 /*
5103 * On client, make sure the server cert doesn't change during renego to
5104 * avoid "triple handshake" attack: https://secure-resumption.com/
5105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005106#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005107 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005108 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005109 {
5110 if( ssl->session->peer_cert == NULL )
5111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005113 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5114 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005115 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005116 }
5117
5118 if( ssl->session->peer_cert->raw.len !=
5119 ssl->session_negotiate->peer_cert->raw.len ||
5120 memcmp( ssl->session->peer_cert->raw.p,
5121 ssl->session_negotiate->peer_cert->raw.p,
5122 ssl->session->peer_cert->raw.len ) != 0 )
5123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005125 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5126 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005127 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005128 }
5129 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005130#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005131
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005132 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005133 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005134 mbedtls_x509_crt *ca_chain;
5135 mbedtls_x509_crl *ca_crl;
5136
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005137#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005138 if( ssl->handshake->sni_ca_chain != NULL )
5139 {
5140 ca_chain = ssl->handshake->sni_ca_chain;
5141 ca_crl = ssl->handshake->sni_ca_crl;
5142 }
5143 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005144#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005145 {
5146 ca_chain = ssl->conf->ca_chain;
5147 ca_crl = ssl->conf->ca_crl;
5148 }
5149
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005150 /*
5151 * Main check: verify certificate
5152 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005153 ret = mbedtls_x509_crt_verify_with_profile(
5154 ssl->session_negotiate->peer_cert,
5155 ca_chain, ca_crl,
5156 ssl->conf->cert_profile,
5157 ssl->hostname,
5158 &ssl->session_negotiate->verify_result,
5159 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00005160
5161 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005163 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005164 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005165
5166 /*
5167 * Secondary checks: always done, but change 'ret' only if it was 0
5168 */
5169
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005170#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005172 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005173
5174 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005175 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005176 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005177 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005178 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005181 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005182 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005183 }
5184 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005185#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005187 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005188 ciphersuite_info,
5189 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005190 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005193 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005194 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005195 }
5196
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005197 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5198 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5199 * with details encoded in the verification flags. All other kinds
5200 * of error codes, including those from the user provided f_vrfy
5201 * functions, are treated as fatal and lead to a failure of
5202 * ssl_parse_certificate even if verification was optional. */
5203 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5204 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5205 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5206 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005207 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005208 }
5209
5210 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5211 {
5212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5213 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5214 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005215
5216 if( ret != 0 )
5217 {
5218 /* The certificate may have been rejected for several reasons.
5219 Pick one and send the corresponding alert. Which alert to send
5220 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005221 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5222 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5223 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5224 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5225 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5226 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5227 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5228 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5229 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5230 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5231 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5232 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5233 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5234 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5235 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5236 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5237 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5238 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5239 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5240 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005241 else
5242 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005243 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5244 alert );
5245 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005246
Hanno Beckere6706e62017-05-15 16:05:15 +01005247#if defined(MBEDTLS_DEBUG_C)
5248 if( ssl->session_negotiate->verify_result != 0 )
5249 {
5250 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5251 ssl->session_negotiate->verify_result ) );
5252 }
5253 else
5254 {
5255 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5256 }
5257#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005258 }
5259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005261
5262 return( ret );
5263}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005264#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5265 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5266 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5267 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5268 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5269 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5270 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005272int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005273{
5274 int ret;
5275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005278 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005279 ssl->out_msglen = 1;
5280 ssl->out_msg[0] = 1;
5281
Paul Bakker5121ce52009-01-03 21:22:43 +00005282 ssl->state++;
5283
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005284 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005285 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005286 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005287 return( ret );
5288 }
5289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005291
5292 return( 0 );
5293}
5294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005295int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005296{
5297 int ret;
5298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005300
Hanno Becker327c93b2018-08-15 13:56:18 +01005301 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005303 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005304 return( ret );
5305 }
5306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005307 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005310 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5311 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005312 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005313 }
5314
5315 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
5316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005318 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5319 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005320 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00005321 }
5322
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005323 /*
5324 * Switch to our negotiated transform and session parameters for inbound
5325 * data.
5326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005327 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005328 ssl->transform_in = ssl->transform_negotiate;
5329 ssl->session_in = ssl->session_negotiate;
5330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005331#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005332 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005334#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005335 ssl_dtls_replay_reset( ssl );
5336#endif
5337
5338 /* Increment epoch */
5339 if( ++ssl->in_epoch == 0 )
5340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005342 /* This is highly unlikely to happen for legitimate reasons, so
5343 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005344 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005345 }
5346 }
5347 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005348#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005349 memset( ssl->in_ctr, 0, 8 );
5350
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005351 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5354 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005356 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005358 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005359 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5360 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005361 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005362 }
5363 }
5364#endif
5365
Paul Bakker5121ce52009-01-03 21:22:43 +00005366 ssl->state++;
5367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005369
5370 return( 0 );
5371}
5372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005373void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5374 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005375{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005376 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005378#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5379 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5380 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005381 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005382 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005383#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005384#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5385#if defined(MBEDTLS_SHA512_C)
5386 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005387 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5388 else
5389#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390#if defined(MBEDTLS_SHA256_C)
5391 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005392 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005393 else
5394#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005395#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005398 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005399 }
Paul Bakker380da532012-04-18 16:10:25 +00005400}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005403{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005404#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5405 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005406 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5407 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005408#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005409#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5410#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005411 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005412#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005413#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005414 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005415#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005417}
5418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005419static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005420 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005421{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005422#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5423 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005424 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5425 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005426#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005427#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5428#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005429 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005430#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005431#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005432 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005433#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005434#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005435}
5436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005437#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5438 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5439static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005440 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005441{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005442 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5443 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005444}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005445#endif
Paul Bakker380da532012-04-18 16:10:25 +00005446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005447#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5448#if defined(MBEDTLS_SHA256_C)
5449static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005450 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005451{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005452 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005453}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005454#endif
Paul Bakker380da532012-04-18 16:10:25 +00005455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005456#if defined(MBEDTLS_SHA512_C)
5457static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005458 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005459{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005460 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005461}
Paul Bakker769075d2012-11-24 11:26:46 +01005462#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005463#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005465#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005466static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005468{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005469 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005470 mbedtls_md5_context md5;
5471 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005472
Paul Bakker5121ce52009-01-03 21:22:43 +00005473 unsigned char padbuf[48];
5474 unsigned char md5sum[16];
5475 unsigned char sha1sum[20];
5476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005477 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005478 if( !session )
5479 session = ssl->session;
5480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005482
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005483 mbedtls_md5_init( &md5 );
5484 mbedtls_sha1_init( &sha1 );
5485
5486 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5487 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005488
5489 /*
5490 * SSLv3:
5491 * hash =
5492 * MD5( master + pad2 +
5493 * MD5( handshake + sender + master + pad1 ) )
5494 * + SHA1( master + pad2 +
5495 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005496 */
5497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005498#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005499 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5500 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005501#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005503#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005504 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5505 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005506#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005508 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005509 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005510
Paul Bakker1ef83d62012-04-11 12:09:53 +00005511 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005512
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005513 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5514 mbedtls_md5_update_ret( &md5, session->master, 48 );
5515 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5516 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005517
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005518 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5519 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5520 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5521 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005522
Paul Bakker1ef83d62012-04-11 12:09:53 +00005523 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005524
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005525 mbedtls_md5_starts_ret( &md5 );
5526 mbedtls_md5_update_ret( &md5, session->master, 48 );
5527 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5528 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5529 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005530
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005531 mbedtls_sha1_starts_ret( &sha1 );
5532 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5533 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5534 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5535 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005537 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005538
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005539 mbedtls_md5_free( &md5 );
5540 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005541
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005542 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
5543 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
5544 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005547}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005548#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005550#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005551static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005552 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005553{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005554 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005555 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005556 mbedtls_md5_context md5;
5557 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005558 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005560 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005561 if( !session )
5562 session = ssl->session;
5563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005565
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005566 mbedtls_md5_init( &md5 );
5567 mbedtls_sha1_init( &sha1 );
5568
5569 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5570 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005571
Paul Bakker1ef83d62012-04-11 12:09:53 +00005572 /*
5573 * TLSv1:
5574 * hash = PRF( master, finished_label,
5575 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5576 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005578#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005579 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5580 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005581#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005583#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005584 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5585 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005586#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005588 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005589 ? "client finished"
5590 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005591
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005592 mbedtls_md5_finish_ret( &md5, padbuf );
5593 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005594
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005595 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005596 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005599
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005600 mbedtls_md5_free( &md5 );
5601 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005602
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005603 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005606}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005607#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005609#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5610#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005611static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005612 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005613{
5614 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005615 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005616 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005617 unsigned char padbuf[32];
5618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005619 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005620 if( !session )
5621 session = ssl->session;
5622
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005623 mbedtls_sha256_init( &sha256 );
5624
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005626
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005627 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005628
5629 /*
5630 * TLSv1.2:
5631 * hash = PRF( master, finished_label,
5632 * Hash( handshake ) )[0.11]
5633 */
5634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005635#if !defined(MBEDTLS_SHA256_ALT)
5636 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005637 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005638#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005641 ? "client finished"
5642 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005643
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005644 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005645
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005646 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005647 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005649 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005650
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005651 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005652
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005653 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005656}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005657#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005659#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005660static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005662{
5663 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005664 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005665 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005666 unsigned char padbuf[48];
5667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005668 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005669 if( !session )
5670 session = ssl->session;
5671
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005672 mbedtls_sha512_init( &sha512 );
5673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005675
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005676 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005677
5678 /*
5679 * TLSv1.2:
5680 * hash = PRF( master, finished_label,
5681 * Hash( handshake ) )[0.11]
5682 */
5683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005684#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005685 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5686 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005687#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005689 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005690 ? "client finished"
5691 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005692
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005693 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005694
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005695 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005696 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005698 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005699
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005700 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005701
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005702 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005705}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005706#endif /* MBEDTLS_SHA512_C */
5707#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005709static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005710{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005711 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005712
5713 /*
5714 * Free our handshake params
5715 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02005716 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005717 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005718 ssl->handshake = NULL;
5719
5720 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005721 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005722 */
5723 if( ssl->transform )
5724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725 mbedtls_ssl_transform_free( ssl->transform );
5726 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005727 }
5728 ssl->transform = ssl->transform_negotiate;
5729 ssl->transform_negotiate = NULL;
5730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005732}
5733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005734void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005735{
5736 int resume = ssl->handshake->resume;
5737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005738 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005740#if defined(MBEDTLS_SSL_RENEGOTIATION)
5741 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005743 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005744 ssl->renego_records_seen = 0;
5745 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005746#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005747
5748 /*
5749 * Free the previous session and switch in the current one
5750 */
Paul Bakker0a597072012-09-25 21:55:46 +00005751 if( ssl->session )
5752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005753#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005754 /* RFC 7366 3.1: keep the EtM state */
5755 ssl->session_negotiate->encrypt_then_mac =
5756 ssl->session->encrypt_then_mac;
5757#endif
5758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759 mbedtls_ssl_session_free( ssl->session );
5760 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005761 }
5762 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005763 ssl->session_negotiate = NULL;
5764
Paul Bakker0a597072012-09-25 21:55:46 +00005765 /*
5766 * Add cache entry
5767 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005768 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005769 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005770 resume == 0 )
5771 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005772 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005774 }
Paul Bakker0a597072012-09-25 21:55:46 +00005775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005776#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005777 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005778 ssl->handshake->flight != NULL )
5779 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005780 /* Cancel handshake timer */
5781 ssl_set_timer( ssl, 0 );
5782
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005783 /* Keep last flight around in case we need to resend it:
5784 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005785 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005786 }
5787 else
5788#endif
5789 ssl_handshake_wrapup_free_hs_transform( ssl );
5790
Paul Bakker48916f92012-09-16 19:57:18 +00005791 ssl->state++;
5792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005793 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005794}
5795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005796int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005797{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005798 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005801
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005802 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01005803
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005804 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005805
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01005806 /*
5807 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
5808 * may define some other value. Currently (early 2016), no defined
5809 * ciphersuite does this (and this is unlikely to change as activity has
5810 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
5811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005812 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005814#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005815 ssl->verify_data_len = hash_len;
5816 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005817#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005818
Paul Bakker5121ce52009-01-03 21:22:43 +00005819 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005820 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5821 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005822
5823 /*
5824 * In case of session resuming, invert the client and server
5825 * ChangeCipherSpec messages order.
5826 */
Paul Bakker0a597072012-09-25 21:55:46 +00005827 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005829#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005830 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005831 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005832#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005833#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005834 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005835 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005836#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005837 }
5838 else
5839 ssl->state++;
5840
Paul Bakker48916f92012-09-16 19:57:18 +00005841 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005842 * Switch to our negotiated transform and session parameters for outbound
5843 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005844 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005845 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005847#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005848 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005849 {
5850 unsigned char i;
5851
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005852 /* Remember current epoch settings for resending */
5853 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01005854 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005855
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005856 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01005857 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005858
5859 /* Increment epoch */
5860 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01005861 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005862 break;
5863
5864 /* The loop goes to its end iff the counter is wrapping */
5865 if( i == 0 )
5866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5868 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005869 }
5870 }
5871 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01005873 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005874
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005875 ssl->transform_out = ssl->transform_negotiate;
5876 ssl->session_out = ssl->session_negotiate;
5877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5879 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005881 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005883 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5884 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005885 }
5886 }
5887#endif
5888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005889#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005890 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005891 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005892#endif
5893
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005894 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005895 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005896 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005897 return( ret );
5898 }
5899
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005900#if defined(MBEDTLS_SSL_PROTO_DTLS)
5901 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5902 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
5903 {
5904 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
5905 return( ret );
5906 }
5907#endif
5908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005910
5911 return( 0 );
5912}
5913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005914#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005915#define SSL_MAX_HASH_LEN 36
5916#else
5917#define SSL_MAX_HASH_LEN 12
5918#endif
5919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005920int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005921{
Paul Bakker23986e52011-04-24 08:57:21 +00005922 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005923 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005924 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005927
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005928 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005929
Hanno Becker327c93b2018-08-15 13:56:18 +01005930 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005933 return( ret );
5934 }
5935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005936 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005939 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5940 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005941 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005942 }
5943
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005944 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005945#if defined(MBEDTLS_SSL_PROTO_SSL3)
5946 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005947 hash_len = 36;
5948 else
5949#endif
5950 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005952 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5953 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005956 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5957 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005958 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005959 }
5960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005961 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005962 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005965 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5966 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005967 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005968 }
5969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005970#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005971 ssl->verify_data_len = hash_len;
5972 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005973#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005974
Paul Bakker0a597072012-09-25 21:55:46 +00005975 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005977#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005978 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005979 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005980#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005981#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005982 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005983 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005984#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005985 }
5986 else
5987 ssl->state++;
5988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005989#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005990 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005991 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005992#endif
5993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005995
5996 return( 0 );
5997}
5998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005999static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006000{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006001 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006003#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6004 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6005 mbedtls_md5_init( &handshake->fin_md5 );
6006 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006007 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6008 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006009#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006010#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6011#if defined(MBEDTLS_SHA256_C)
6012 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006013 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006014#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006015#if defined(MBEDTLS_SHA512_C)
6016 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006017 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006018#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006019#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006020
6021 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006022
6023#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6024 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6025 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6026#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028#if defined(MBEDTLS_DHM_C)
6029 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006030#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031#if defined(MBEDTLS_ECDH_C)
6032 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006033#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006034#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006035 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006036#if defined(MBEDTLS_SSL_CLI_C)
6037 handshake->ecjpake_cache = NULL;
6038 handshake->ecjpake_cache_len = 0;
6039#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006040#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006041
6042#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6043 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6044#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006045}
6046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006047static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006048{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006049 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006051 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6052 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054 mbedtls_md_init( &transform->md_ctx_enc );
6055 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006056}
6057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006058void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006059{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006061}
6062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006064{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006065 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006066 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006068 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006069 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006070 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006071 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006072
6073 /*
6074 * Either the pointers are now NULL or cleared properly and can be freed.
6075 * Now allocate missing structures.
6076 */
6077 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006078 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006079 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006080 }
Paul Bakker48916f92012-09-16 19:57:18 +00006081
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006082 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006083 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006084 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006085 }
Paul Bakker48916f92012-09-16 19:57:18 +00006086
Paul Bakker82788fb2014-10-20 13:59:19 +02006087 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006088 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006089 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006090 }
Paul Bakker48916f92012-09-16 19:57:18 +00006091
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006092 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006093 if( ssl->handshake == NULL ||
6094 ssl->transform_negotiate == NULL ||
6095 ssl->session_negotiate == NULL )
6096 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006099 mbedtls_free( ssl->handshake );
6100 mbedtls_free( ssl->transform_negotiate );
6101 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006102
6103 ssl->handshake = NULL;
6104 ssl->transform_negotiate = NULL;
6105 ssl->session_negotiate = NULL;
6106
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006107 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006108 }
6109
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006110 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006111 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006112 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006113 ssl_handshake_params_init( ssl->handshake );
6114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006115#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006116 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6117 {
6118 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006119
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006120 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6121 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6122 else
6123 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006124
6125 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006126 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006127#endif
6128
Paul Bakker48916f92012-09-16 19:57:18 +00006129 return( 0 );
6130}
6131
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006132#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006133/* Dummy cookie callbacks for defaults */
6134static int ssl_cookie_write_dummy( void *ctx,
6135 unsigned char **p, unsigned char *end,
6136 const unsigned char *cli_id, size_t cli_id_len )
6137{
6138 ((void) ctx);
6139 ((void) p);
6140 ((void) end);
6141 ((void) cli_id);
6142 ((void) cli_id_len);
6143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006144 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006145}
6146
6147static int ssl_cookie_check_dummy( void *ctx,
6148 const unsigned char *cookie, size_t cookie_len,
6149 const unsigned char *cli_id, size_t cli_id_len )
6150{
6151 ((void) ctx);
6152 ((void) cookie);
6153 ((void) cookie_len);
6154 ((void) cli_id);
6155 ((void) cli_id_len);
6156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006157 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006158}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006159#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006160
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006161/* Once ssl->out_hdr as the address of the beginning of the
6162 * next outgoing record is set, deduce the other pointers.
6163 *
6164 * Note: For TLS, we save the implicit record sequence number
6165 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6166 * and the caller has to make sure there's space for this.
6167 */
6168
6169static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6170 mbedtls_ssl_transform *transform )
6171{
6172#if defined(MBEDTLS_SSL_PROTO_DTLS)
6173 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6174 {
6175 ssl->out_ctr = ssl->out_hdr + 3;
6176 ssl->out_len = ssl->out_hdr + 11;
6177 ssl->out_iv = ssl->out_hdr + 13;
6178 }
6179 else
6180#endif
6181 {
6182 ssl->out_ctr = ssl->out_hdr - 8;
6183 ssl->out_len = ssl->out_hdr + 3;
6184 ssl->out_iv = ssl->out_hdr + 5;
6185 }
6186
6187 /* Adjust out_msg to make space for explicit IV, if used. */
6188 if( transform != NULL &&
6189 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6190 {
6191 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6192 }
6193 else
6194 ssl->out_msg = ssl->out_iv;
6195}
6196
6197/* Once ssl->in_hdr as the address of the beginning of the
6198 * next incoming record is set, deduce the other pointers.
6199 *
6200 * Note: For TLS, we save the implicit record sequence number
6201 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6202 * and the caller has to make sure there's space for this.
6203 */
6204
6205static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6206 mbedtls_ssl_transform *transform )
6207{
6208#if defined(MBEDTLS_SSL_PROTO_DTLS)
6209 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6210 {
6211 ssl->in_ctr = ssl->in_hdr + 3;
6212 ssl->in_len = ssl->in_hdr + 11;
6213 ssl->in_iv = ssl->in_hdr + 13;
6214 }
6215 else
6216#endif
6217 {
6218 ssl->in_ctr = ssl->in_hdr - 8;
6219 ssl->in_len = ssl->in_hdr + 3;
6220 ssl->in_iv = ssl->in_hdr + 5;
6221 }
6222
6223 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6224 if( transform != NULL &&
6225 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6226 {
6227 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6228 }
6229 else
6230 ssl->in_msg = ssl->in_iv;
6231}
6232
Paul Bakker5121ce52009-01-03 21:22:43 +00006233/*
6234 * Initialize an SSL context
6235 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006236void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6237{
6238 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6239}
6240
6241/*
6242 * Setup an SSL context
6243 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006244
6245static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6246{
6247 /* Set the incoming and outgoing record pointers. */
6248#if defined(MBEDTLS_SSL_PROTO_DTLS)
6249 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6250 {
6251 ssl->out_hdr = ssl->out_buf;
6252 ssl->in_hdr = ssl->in_buf;
6253 }
6254 else
6255#endif /* MBEDTLS_SSL_PROTO_DTLS */
6256 {
6257 ssl->out_hdr = ssl->out_buf + 8;
6258 ssl->in_hdr = ssl->in_buf + 8;
6259 }
6260
6261 /* Derive other internal pointers. */
6262 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6263 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6264}
6265
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006266int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006267 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006268{
Paul Bakker48916f92012-09-16 19:57:18 +00006269 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006270
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006271 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006272
6273 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006274 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006275 */
Angus Grattond8213d02016-05-25 20:56:48 +10006276 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6277 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006278 {
Angus Grattond8213d02016-05-25 20:56:48 +10006279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6280 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6281 }
6282
6283 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6284 if( ssl->out_buf == NULL )
6285 {
6286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006287 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006288 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006289 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006290 }
6291
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006292 ssl_reset_in_out_pointers( ssl );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006293
Paul Bakker48916f92012-09-16 19:57:18 +00006294 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6295 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006296
6297 return( 0 );
6298}
6299
6300/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006301 * Reset an initialized and used SSL context for re-use while retaining
6302 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006303 *
6304 * If partial is non-zero, keep data in the input buffer and client ID.
6305 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006306 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006307static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006308{
Paul Bakker48916f92012-09-16 19:57:18 +00006309 int ret;
6310
Hanno Becker7e772132018-08-10 12:38:21 +01006311#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6312 !defined(MBEDTLS_SSL_SRV_C)
6313 ((void) partial);
6314#endif
6315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006316 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006317
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006318 /* Cancel any possibly running timer */
6319 ssl_set_timer( ssl, 0 );
6320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006321#if defined(MBEDTLS_SSL_RENEGOTIATION)
6322 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006323 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006324
6325 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006326 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6327 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006328#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006329 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006330
Paul Bakker7eb013f2011-10-06 12:37:39 +00006331 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01006332 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006333
6334 ssl->in_msgtype = 0;
6335 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006336#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006337 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006338 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006339#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006340#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006341 ssl_dtls_replay_reset( ssl );
6342#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006343
6344 ssl->in_hslen = 0;
6345 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006346
6347 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006348
6349 ssl->out_msgtype = 0;
6350 ssl->out_msglen = 0;
6351 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006352#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6353 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006354 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006355#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006356
Hanno Becker19859472018-08-06 09:40:20 +01006357 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6358
Paul Bakker48916f92012-09-16 19:57:18 +00006359 ssl->transform_in = NULL;
6360 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006361
Hanno Becker78640902018-08-13 16:35:15 +01006362 ssl->session_in = NULL;
6363 ssl->session_out = NULL;
6364
Angus Grattond8213d02016-05-25 20:56:48 +10006365 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006366
6367#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006368 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006369#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
6370 {
6371 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10006372 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006373 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006375#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6376 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6379 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6382 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006383 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006384 }
6385#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006386
Paul Bakker48916f92012-09-16 19:57:18 +00006387 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006389 mbedtls_ssl_transform_free( ssl->transform );
6390 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006391 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006392 }
Paul Bakker48916f92012-09-16 19:57:18 +00006393
Paul Bakkerc0463502013-02-14 11:19:38 +01006394 if( ssl->session )
6395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006396 mbedtls_ssl_session_free( ssl->session );
6397 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006398 ssl->session = NULL;
6399 }
6400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006401#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006402 ssl->alpn_chosen = NULL;
6403#endif
6404
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006405#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01006406#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006407 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006408#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006409 {
6410 mbedtls_free( ssl->cli_id );
6411 ssl->cli_id = NULL;
6412 ssl->cli_id_len = 0;
6413 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006414#endif
6415
Paul Bakker48916f92012-09-16 19:57:18 +00006416 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6417 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006418
6419 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006420}
6421
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006422/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006423 * Reset an initialized and used SSL context for re-use while retaining
6424 * all application-set variables, function pointers and data.
6425 */
6426int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6427{
6428 return( ssl_session_reset_int( ssl, 0 ) );
6429}
6430
6431/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006432 * SSL set accessors
6433 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006434void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006435{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006436 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006437}
6438
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006439void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006440{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006441 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006442}
6443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006444#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006445void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006446{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006447 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006448}
6449#endif
6450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006451#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006452void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006453{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006454 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006455}
6456#endif
6457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006458#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01006459
6460void mbedtls_ssl_conf_datagram_packing( mbedtls_ssl_context *ssl,
6461 unsigned allow_packing )
6462{
6463 ssl->disable_datagram_packing = !allow_packing;
6464}
6465
6466void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
6467 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006468{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006469 conf->hs_timeout_min = min;
6470 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006471}
6472#endif
6473
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006474void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006475{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006476 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006477}
6478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006479#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006480void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006481 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006482 void *p_vrfy )
6483{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006484 conf->f_vrfy = f_vrfy;
6485 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006486}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006488
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006489void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006490 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006491 void *p_rng )
6492{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006493 conf->f_rng = f_rng;
6494 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006495}
6496
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006497void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006498 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006499 void *p_dbg )
6500{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006501 conf->f_dbg = f_dbg;
6502 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006503}
6504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006505void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006506 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006507 mbedtls_ssl_send_t *f_send,
6508 mbedtls_ssl_recv_t *f_recv,
6509 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006510{
6511 ssl->p_bio = p_bio;
6512 ssl->f_send = f_send;
6513 ssl->f_recv = f_recv;
6514 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006515}
6516
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006517void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006518{
6519 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006520}
6521
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006522void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6523 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006524 mbedtls_ssl_set_timer_t *f_set_timer,
6525 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006526{
6527 ssl->p_timer = p_timer;
6528 ssl->f_set_timer = f_set_timer;
6529 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006530
6531 /* Make sure we start with no timer running */
6532 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006533}
6534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006535#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006536void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006537 void *p_cache,
6538 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6539 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006540{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006541 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006542 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006543 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006544}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006545#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547#if defined(MBEDTLS_SSL_CLI_C)
6548int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006549{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006550 int ret;
6551
6552 if( ssl == NULL ||
6553 session == NULL ||
6554 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006555 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006557 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006558 }
6559
6560 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6561 return( ret );
6562
Paul Bakker0a597072012-09-25 21:55:46 +00006563 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006564
6565 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006566}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006567#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006568
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006569void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006570 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006571{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006572 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6573 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6574 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6575 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006576}
6577
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006578void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006579 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006580 int major, int minor )
6581{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006582 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006583 return;
6584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006585 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006586 return;
6587
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006588 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00006589}
6590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006591#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006592void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01006593 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006594{
6595 conf->cert_profile = profile;
6596}
6597
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006598/* Append a new keycert entry to a (possibly empty) list */
6599static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
6600 mbedtls_x509_crt *cert,
6601 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006602{
niisato8ee24222018-06-25 19:05:48 +09006603 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006604
niisato8ee24222018-06-25 19:05:48 +09006605 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
6606 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006607 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006608
niisato8ee24222018-06-25 19:05:48 +09006609 new_cert->cert = cert;
6610 new_cert->key = key;
6611 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006612
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006613 /* Update head is the list was null, else add to the end */
6614 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006615 {
niisato8ee24222018-06-25 19:05:48 +09006616 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006617 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006618 else
6619 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006620 mbedtls_ssl_key_cert *cur = *head;
6621 while( cur->next != NULL )
6622 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09006623 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006624 }
6625
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006626 return( 0 );
6627}
6628
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006629int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006630 mbedtls_x509_crt *own_cert,
6631 mbedtls_pk_context *pk_key )
6632{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006633 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006634}
6635
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006636void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006637 mbedtls_x509_crt *ca_chain,
6638 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006639{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006640 conf->ca_chain = ca_chain;
6641 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006642}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006644
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006645#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6646int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6647 mbedtls_x509_crt *own_cert,
6648 mbedtls_pk_context *pk_key )
6649{
6650 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6651 own_cert, pk_key ) );
6652}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006653
6654void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6655 mbedtls_x509_crt *ca_chain,
6656 mbedtls_x509_crl *ca_crl )
6657{
6658 ssl->handshake->sni_ca_chain = ca_chain;
6659 ssl->handshake->sni_ca_crl = ca_crl;
6660}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006661
6662void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6663 int authmode )
6664{
6665 ssl->handshake->sni_authmode = authmode;
6666}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006667#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6668
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006669#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006670/*
6671 * Set EC J-PAKE password for current handshake
6672 */
6673int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
6674 const unsigned char *pw,
6675 size_t pw_len )
6676{
6677 mbedtls_ecjpake_role role;
6678
Janos Follath8eb64132016-06-03 15:40:57 +01006679 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006680 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6681
6682 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6683 role = MBEDTLS_ECJPAKE_SERVER;
6684 else
6685 role = MBEDTLS_ECJPAKE_CLIENT;
6686
6687 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
6688 role,
6689 MBEDTLS_MD_SHA256,
6690 MBEDTLS_ECP_DP_SECP256R1,
6691 pw, pw_len ) );
6692}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006693#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006695#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006696int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006697 const unsigned char *psk, size_t psk_len,
6698 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006699{
Paul Bakker6db455e2013-09-18 17:29:31 +02006700 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006701 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006703 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6704 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006705
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006706 /* Identity len will be encoded on two bytes */
6707 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10006708 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006709 {
6710 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6711 }
6712
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006713 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006714 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006715 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006716
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006717 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006718 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006719 conf->psk_len = 0;
6720 }
6721 if( conf->psk_identity != NULL )
6722 {
6723 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006724 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006725 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006726 }
6727
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006728 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6729 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006730 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006731 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006732 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006733 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006734 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006735 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006736 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006737
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006738 conf->psk_len = psk_len;
6739 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006740
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006741 memcpy( conf->psk, psk, conf->psk_len );
6742 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006743
6744 return( 0 );
6745}
6746
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006747int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6748 const unsigned char *psk, size_t psk_len )
6749{
6750 if( psk == NULL || ssl->handshake == NULL )
6751 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6752
6753 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6754 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6755
6756 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006757 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006758 mbedtls_platform_zeroize( ssl->handshake->psk,
6759 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006760 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006761 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006762 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006763
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006764 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006765 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006766
6767 ssl->handshake->psk_len = psk_len;
6768 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6769
6770 return( 0 );
6771}
6772
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006773void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006775 size_t),
6776 void *p_psk )
6777{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006778 conf->f_psk = f_psk;
6779 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006780}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006781#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006782
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006783#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01006784
6785#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006786int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006787{
6788 int ret;
6789
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006790 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6791 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6792 {
6793 mbedtls_mpi_free( &conf->dhm_P );
6794 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006795 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006796 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006797
6798 return( 0 );
6799}
Hanno Becker470a8c42017-10-04 15:28:46 +01006800#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006801
Hanno Beckera90658f2017-10-04 15:29:08 +01006802int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
6803 const unsigned char *dhm_P, size_t P_len,
6804 const unsigned char *dhm_G, size_t G_len )
6805{
6806 int ret;
6807
6808 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
6809 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
6810 {
6811 mbedtls_mpi_free( &conf->dhm_P );
6812 mbedtls_mpi_free( &conf->dhm_G );
6813 return( ret );
6814 }
6815
6816 return( 0 );
6817}
Paul Bakker5121ce52009-01-03 21:22:43 +00006818
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006819int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006820{
6821 int ret;
6822
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006823 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6824 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6825 {
6826 mbedtls_mpi_free( &conf->dhm_P );
6827 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006828 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006829 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006830
6831 return( 0 );
6832}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006833#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006834
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006835#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6836/*
6837 * Set the minimum length for Diffie-Hellman parameters
6838 */
6839void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6840 unsigned int bitlen )
6841{
6842 conf->dhm_min_bitlen = bitlen;
6843}
6844#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6845
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02006846#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006847/*
6848 * Set allowed/preferred hashes for handshake signatures
6849 */
6850void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6851 const int *hashes )
6852{
6853 conf->sig_hashes = hashes;
6854}
Hanno Becker947194e2017-04-07 13:25:49 +01006855#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006856
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006857#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006858/*
6859 * Set the allowed elliptic curves
6860 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006861void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006862 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006863{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006864 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006865}
Hanno Becker947194e2017-04-07 13:25:49 +01006866#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006867
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006868#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006869int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006870{
Hanno Becker947194e2017-04-07 13:25:49 +01006871 /* Initialize to suppress unnecessary compiler warning */
6872 size_t hostname_len = 0;
6873
6874 /* Check if new hostname is valid before
6875 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01006876 if( hostname != NULL )
6877 {
6878 hostname_len = strlen( hostname );
6879
6880 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6881 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6882 }
6883
6884 /* Now it's clear that we will overwrite the old hostname,
6885 * so we can free it safely */
6886
6887 if( ssl->hostname != NULL )
6888 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006889 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01006890 mbedtls_free( ssl->hostname );
6891 }
6892
6893 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006894
Paul Bakker5121ce52009-01-03 21:22:43 +00006895 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01006896 {
6897 ssl->hostname = NULL;
6898 }
6899 else
6900 {
6901 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01006902 if( ssl->hostname == NULL )
6903 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006904
Hanno Becker947194e2017-04-07 13:25:49 +01006905 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006906
Hanno Becker947194e2017-04-07 13:25:49 +01006907 ssl->hostname[hostname_len] = '\0';
6908 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006909
6910 return( 0 );
6911}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01006912#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006913
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006914#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006915void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006916 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006917 const unsigned char *, size_t),
6918 void *p_sni )
6919{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006920 conf->f_sni = f_sni;
6921 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006922}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006925#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006926int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006927{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006928 size_t cur_len, tot_len;
6929 const char **p;
6930
6931 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08006932 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6933 * MUST NOT be truncated."
6934 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006935 */
6936 tot_len = 0;
6937 for( p = protos; *p != NULL; p++ )
6938 {
6939 cur_len = strlen( *p );
6940 tot_len += cur_len;
6941
6942 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006943 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006944 }
6945
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006946 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006947
6948 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006949}
6950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006951const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006952{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006953 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006954}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006956
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006957void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006958{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006959 conf->max_major_ver = major;
6960 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006961}
6962
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006963void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006964{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006965 conf->min_major_ver = major;
6966 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006967}
6968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006969#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006970void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006971{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006972 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006973}
6974#endif
6975
Janos Follath088ce432017-04-10 12:42:31 +01006976#if defined(MBEDTLS_SSL_SRV_C)
6977void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
6978 char cert_req_ca_list )
6979{
6980 conf->cert_req_ca_list = cert_req_ca_list;
6981}
6982#endif
6983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006984#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006985void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006986{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006987 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006988}
6989#endif
6990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006991#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006992void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006993{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006994 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006995}
6996#endif
6997
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006998#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006999void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007000{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007001 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007002}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007003#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007004
Manuel Pégourié-Gonnard0b1d9b22017-09-21 13:15:27 +02007005#if defined(MBEDTLS_SSL_PROTO_DTLS)
7006void mbedtls_ssl_conf_mtu( mbedtls_ssl_config *conf, uint16_t mtu )
7007{
7008 conf->mtu = mtu;
7009}
7010#endif
7011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007012#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007013int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007014{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007015 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007016 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007018 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007019 }
7020
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007021 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007022
7023 return( 0 );
7024}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007025#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007027#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007028void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007029{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007030 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007031}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007032#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007034#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007035void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007036{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007037 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007038}
7039#endif
7040
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007041void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007042{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007043 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007044}
7045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007046#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007047void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007048{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007049 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007050}
7051
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007052void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007053{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007054 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007055}
7056
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007057void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007058 const unsigned char period[8] )
7059{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007060 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007061}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007062#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007064#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007065#if defined(MBEDTLS_SSL_CLI_C)
7066void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007067{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007068 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007069}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007070#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007071
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007072#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007073void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7074 mbedtls_ssl_ticket_write_t *f_ticket_write,
7075 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7076 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007077{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007078 conf->f_ticket_write = f_ticket_write;
7079 conf->f_ticket_parse = f_ticket_parse;
7080 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007081}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007082#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007083#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007084
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007085#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7086void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7087 mbedtls_ssl_export_keys_t *f_export_keys,
7088 void *p_export_keys )
7089{
7090 conf->f_export_keys = f_export_keys;
7091 conf->p_export_keys = p_export_keys;
7092}
7093#endif
7094
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007095#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007096void mbedtls_ssl_conf_async_private_cb(
7097 mbedtls_ssl_config *conf,
7098 mbedtls_ssl_async_sign_t *f_async_sign,
7099 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7100 mbedtls_ssl_async_resume_t *f_async_resume,
7101 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007102 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007103{
7104 conf->f_async_sign_start = f_async_sign;
7105 conf->f_async_decrypt_start = f_async_decrypt;
7106 conf->f_async_resume = f_async_resume;
7107 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007108 conf->p_async_config_data = async_config_data;
7109}
7110
Gilles Peskine8f97af72018-04-26 11:46:10 +02007111void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7112{
7113 return( conf->p_async_config_data );
7114}
7115
Gilles Peskine1febfef2018-04-30 11:54:39 +02007116void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007117{
7118 if( ssl->handshake == NULL )
7119 return( NULL );
7120 else
7121 return( ssl->handshake->user_async_ctx );
7122}
7123
Gilles Peskine1febfef2018-04-30 11:54:39 +02007124void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007125 void *ctx )
7126{
7127 if( ssl->handshake != NULL )
7128 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007129}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007130#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007131
Paul Bakker5121ce52009-01-03 21:22:43 +00007132/*
7133 * SSL get accessors
7134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007135size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007136{
7137 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7138}
7139
Hanno Becker8b170a02017-10-10 11:51:19 +01007140int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7141{
7142 /*
7143 * Case A: We're currently holding back
7144 * a message for further processing.
7145 */
7146
7147 if( ssl->keep_current_message == 1 )
7148 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007149 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007150 return( 1 );
7151 }
7152
7153 /*
7154 * Case B: Further records are pending in the current datagram.
7155 */
7156
7157#if defined(MBEDTLS_SSL_PROTO_DTLS)
7158 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7159 ssl->in_left > ssl->next_record_offset )
7160 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007161 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007162 return( 1 );
7163 }
7164#endif /* MBEDTLS_SSL_PROTO_DTLS */
7165
7166 /*
7167 * Case C: A handshake message is being processed.
7168 */
7169
Hanno Becker8b170a02017-10-10 11:51:19 +01007170 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7171 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007172 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007173 return( 1 );
7174 }
7175
7176 /*
7177 * Case D: An application data message is being processed
7178 */
7179 if( ssl->in_offt != NULL )
7180 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007181 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007182 return( 1 );
7183 }
7184
7185 /*
7186 * In all other cases, the rest of the message can be dropped.
7187 * As in ssl_read_record_layer, this needs to be adapted if
7188 * we implement support for multiple alerts in single records.
7189 */
7190
7191 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7192 return( 0 );
7193}
7194
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007195uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007196{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007197 if( ssl->session != NULL )
7198 return( ssl->session->verify_result );
7199
7200 if( ssl->session_negotiate != NULL )
7201 return( ssl->session_negotiate->verify_result );
7202
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007203 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007204}
7205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007206const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007207{
Paul Bakker926c8e42013-03-06 10:23:34 +01007208 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007209 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007211 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007212}
7213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007214const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007215{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007216#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007217 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007218 {
7219 switch( ssl->minor_ver )
7220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007222 return( "DTLSv1.0" );
7223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007224 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007225 return( "DTLSv1.2" );
7226
7227 default:
7228 return( "unknown (DTLS)" );
7229 }
7230 }
7231#endif
7232
Paul Bakker43ca69c2011-01-15 17:35:19 +00007233 switch( ssl->minor_ver )
7234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007236 return( "SSLv3.0" );
7237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007239 return( "TLSv1.0" );
7240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007242 return( "TLSv1.1" );
7243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007245 return( "TLSv1.2" );
7246
Paul Bakker43ca69c2011-01-15 17:35:19 +00007247 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007248 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007249 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007250}
7251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007253{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007254 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007256 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007257
Hanno Becker78640902018-08-13 16:35:15 +01007258 if( transform == NULL )
7259 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007261#if defined(MBEDTLS_ZLIB_SUPPORT)
7262 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7263 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007264 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007265#endif
7266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007269 case MBEDTLS_MODE_GCM:
7270 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007271 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007272 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007273 transform_expansion = transform->minlen;
7274 break;
7275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007277
7278 block_size = mbedtls_cipher_get_block_size(
7279 &transform->cipher_ctx_enc );
7280
7281#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7282 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7283 {
7284 /* Expansion due to addition of
7285 * - MAC
7286 * - CBC padding (theoretically up to 256 bytes, but
7287 * we never use more than block_size)
7288 * - explicit IV
7289 */
7290 transform_expansion = transform->maclen + 2 * block_size;
7291 }
7292 else
7293#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7294 {
7295 /* No explicit IV prior to TLS 1.1. */
7296 transform_expansion = transform->maclen + block_size;
7297 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007298 break;
7299
7300 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007303 }
7304
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007305 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007306}
7307
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007308#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7309size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7310{
7311 size_t max_len;
7312
7313 /*
7314 * Assume mfl_code is correct since it was checked when set
7315 */
Angus Grattond8213d02016-05-25 20:56:48 +10007316 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007317
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007318 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007319 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007320 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007321 {
Angus Grattond8213d02016-05-25 20:56:48 +10007322 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007323 }
7324
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007325 /* During a handshake, use the value being negotiated */
7326 if( ssl->session_negotiate != NULL &&
7327 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7328 {
7329 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7330 }
7331
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007332 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007333}
7334#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7335
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007336int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7337{
7338 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7339
7340#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7341 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7342
7343 if( max_len > mfl )
7344 max_len = mfl;
7345#endif
7346
7347#if defined(MBEDTLS_SSL_PROTO_DTLS)
7348 if( ssl->conf->mtu != 0 )
7349 {
7350 const size_t mtu = ssl->conf->mtu;
7351 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7352 const size_t overhead = (size_t) ret;
7353
7354 if( ret < 0 )
7355 return( ret );
7356
7357 if( mtu <= overhead )
7358 {
7359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7360 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7361 }
7362
7363 if( max_len > mtu - overhead )
7364 max_len = mtu - overhead;
7365 }
7366#endif
7367
Hanno Becker0defedb2018-08-10 12:35:02 +01007368#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7369 !defined(MBEDTLS_SSL_PROTO_DTLS)
7370 ((void) ssl);
7371#endif
7372
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007373 return( (int) max_len );
7374}
7375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007376#if defined(MBEDTLS_X509_CRT_PARSE_C)
7377const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007378{
7379 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007380 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007381
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007382 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007383}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386#if defined(MBEDTLS_SSL_CLI_C)
7387int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007388{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007389 if( ssl == NULL ||
7390 dst == NULL ||
7391 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007392 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007394 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007395 }
7396
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007397 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007398}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007399#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007400
Paul Bakker5121ce52009-01-03 21:22:43 +00007401/*
Paul Bakker1961b702013-01-25 14:49:24 +01007402 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007403 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007405{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007406 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007407
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007408 if( ssl == NULL || ssl->conf == NULL )
7409 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007412 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007413 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007414#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007416 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007417 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007418#endif
7419
Paul Bakker1961b702013-01-25 14:49:24 +01007420 return( ret );
7421}
7422
7423/*
7424 * Perform the SSL handshake
7425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007426int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007427{
7428 int ret = 0;
7429
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007430 if( ssl == NULL || ssl->conf == NULL )
7431 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007437 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01007438
7439 if( ret != 0 )
7440 break;
7441 }
7442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007444
7445 return( ret );
7446}
7447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007448#if defined(MBEDTLS_SSL_RENEGOTIATION)
7449#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007450/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007451 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00007452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007453static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007454{
7455 int ret;
7456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007458
7459 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007460 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7461 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007462
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007463 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007464 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007465 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007466 return( ret );
7467 }
7468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007470
7471 return( 0 );
7472}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007473#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007474
7475/*
7476 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007477 * - any side: calling mbedtls_ssl_renegotiate(),
7478 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7479 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007480 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007481 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007482 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007485{
7486 int ret;
7487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007489
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007490 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7491 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007492
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007493 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7494 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007496 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007497 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007498 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007499 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007500 ssl->handshake->out_msg_seq = 1;
7501 else
7502 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007503 }
7504#endif
7505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7507 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007509 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007512 return( ret );
7513 }
7514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007516
7517 return( 0 );
7518}
7519
7520/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007521 * Renegotiate current connection on client,
7522 * or request renegotiation on server
7523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007525{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007527
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007528 if( ssl == NULL || ssl->conf == NULL )
7529 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007531#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007532 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007533 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007535 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7536 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007538 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007539
7540 /* Did we already try/start sending HelloRequest? */
7541 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007542 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007543
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007544 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007545 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007548#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007549 /*
7550 * On client, either start the renegotiation process or,
7551 * if already in progress, continue the handshake
7552 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007555 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7556 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007557
7558 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007560 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007561 return( ret );
7562 }
7563 }
7564 else
7565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007566 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007568 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007569 return( ret );
7570 }
7571 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007572#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007573
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007574 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007575}
7576
7577/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007578 * Check record counters and renegotiate if they're above the limit.
7579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007580static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007581{
Andres AG2196c7f2016-12-15 17:01:16 +00007582 size_t ep_len = ssl_ep_len( ssl );
7583 int in_ctr_cmp;
7584 int out_ctr_cmp;
7585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
7587 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007588 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007589 {
7590 return( 0 );
7591 }
7592
Andres AG2196c7f2016-12-15 17:01:16 +00007593 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
7594 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01007595 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00007596 ssl->conf->renego_period + ep_len, 8 - ep_len );
7597
7598 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007599 {
7600 return( 0 );
7601 }
7602
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007605}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007607
7608/*
7609 * Receive application data decrypted from the SSL layer
7610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007611int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007612{
Hanno Becker4a810fb2017-05-24 16:27:30 +01007613 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00007614 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00007615
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007616 if( ssl == NULL || ssl->conf == NULL )
7617 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007622 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007624 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007625 return( ret );
7626
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007627 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007629 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007630 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007631 return( ret );
7632 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007633 }
7634#endif
7635
Hanno Becker4a810fb2017-05-24 16:27:30 +01007636 /*
7637 * Check if renegotiation is necessary and/or handshake is
7638 * in process. If yes, perform/continue, and fall through
7639 * if an unexpected packet is received while the client
7640 * is waiting for the ServerHello.
7641 *
7642 * (There is no equivalent to the last condition on
7643 * the server-side as it is not treated as within
7644 * a handshake while waiting for the ClientHello
7645 * after a renegotiation request.)
7646 */
7647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007649 ret = ssl_check_ctr_renegotiate( ssl );
7650 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7651 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007653 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007654 return( ret );
7655 }
7656#endif
7657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007658 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007660 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01007661 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7662 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007664 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007665 return( ret );
7666 }
7667 }
7668
Hanno Beckere41158b2017-10-23 13:30:32 +01007669 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01007670 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007671 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007672 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007673 if( ssl->f_get_timer != NULL &&
7674 ssl->f_get_timer( ssl->p_timer ) == -1 )
7675 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007676 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007677 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007678
Hanno Becker327c93b2018-08-15 13:56:18 +01007679 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007680 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007681 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
7682 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00007683
Hanno Becker4a810fb2017-05-24 16:27:30 +01007684 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7685 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007686 }
7687
7688 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007690 {
7691 /*
7692 * OpenSSL sends empty messages to randomize the IV
7693 */
Hanno Becker327c93b2018-08-15 13:56:18 +01007694 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007696 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00007697 return( 0 );
7698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007699 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007700 return( ret );
7701 }
7702 }
7703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007704 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00007705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007707
Hanno Becker4a810fb2017-05-24 16:27:30 +01007708 /*
7709 * - For client-side, expect SERVER_HELLO_REQUEST.
7710 * - For server-side, expect CLIENT_HELLO.
7711 * - Fail (TLS) or silently drop record (DTLS) in other cases.
7712 */
7713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007714#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007715 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007716 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01007717 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00007718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007720
7721 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007723 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007724 {
7725 continue;
7726 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007727#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007728 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007729 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007730#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007731
Hanno Becker4a810fb2017-05-24 16:27:30 +01007732#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007733 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007734 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007737
7738 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007739#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007740 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007741 {
7742 continue;
7743 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007744#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007745 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00007746 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007747#endif /* MBEDTLS_SSL_SRV_C */
7748
Hanno Becker21df7f92017-10-17 11:03:26 +01007749#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007750 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007751 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
7752 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
7753 ssl->conf->allow_legacy_renegotiation ==
7754 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
7755 {
7756 /*
7757 * Accept renegotiation request
7758 */
Paul Bakker48916f92012-09-16 19:57:18 +00007759
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007760 /* DTLS clients need to know renego is server-initiated */
7761#if defined(MBEDTLS_SSL_PROTO_DTLS)
7762 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7763 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7764 {
7765 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
7766 }
7767#endif
7768 ret = ssl_start_renegotiation( ssl );
7769 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7770 ret != 0 )
7771 {
7772 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
7773 return( ret );
7774 }
7775 }
7776 else
Hanno Becker21df7f92017-10-17 11:03:26 +01007777#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00007778 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007779 /*
7780 * Refuse renegotiation
7781 */
7782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007783 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007785#if defined(MBEDTLS_SSL_PROTO_SSL3)
7786 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007787 {
Gilles Peskine92e44262017-05-10 17:27:49 +02007788 /* SSLv3 does not have a "no_renegotiation" warning, so
7789 we send a fatal alert and abort the connection. */
7790 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7791 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7792 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007793 }
7794 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007795#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7796#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7797 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7798 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007800 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7801 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7802 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007803 {
7804 return( ret );
7805 }
Paul Bakker48916f92012-09-16 19:57:18 +00007806 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007807 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007808#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
7809 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02007810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7812 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02007813 }
Paul Bakker48916f92012-09-16 19:57:18 +00007814 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007815
Hanno Becker90333da2017-10-10 11:27:13 +01007816 /* At this point, we don't know whether the renegotiation has been
7817 * completed or not. The cases to consider are the following:
7818 * 1) The renegotiation is complete. In this case, no new record
7819 * has been read yet.
7820 * 2) The renegotiation is incomplete because the client received
7821 * an application data record while awaiting the ServerHello.
7822 * 3) The renegotiation is incomplete because the client received
7823 * a non-handshake, non-application data message while awaiting
7824 * the ServerHello.
7825 * In each of these case, looping will be the proper action:
7826 * - For 1), the next iteration will read a new record and check
7827 * if it's application data.
7828 * - For 2), the loop condition isn't satisfied as application data
7829 * is present, hence continue is the same as break
7830 * - For 3), the loop condition is satisfied and read_record
7831 * will re-deliver the message that was held back by the client
7832 * when expecting the ServerHello.
7833 */
7834 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00007835 }
Hanno Becker21df7f92017-10-17 11:03:26 +01007836#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007837 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007838 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007839 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007840 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007841 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007844 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007845 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007846 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007847 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007848 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007851 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
7852 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007855 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007856 }
7857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
7861 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007862 }
7863
7864 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007865
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007866 /* We're going to return something now, cancel timer,
7867 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007869 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007870
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007871#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007872 /* If we requested renego but received AppData, resend HelloRequest.
7873 * Do it now, after setting in_offt, to avoid taking this branch
7874 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007876 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007878 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007879 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007882 return( ret );
7883 }
7884 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01007886#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00007887 }
7888
7889 n = ( len < ssl->in_msglen )
7890 ? len : ssl->in_msglen;
7891
7892 memcpy( buf, ssl->in_offt, n );
7893 ssl->in_msglen -= n;
7894
7895 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01007896 {
7897 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00007898 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01007899 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007900 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007901 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01007902 {
Paul Bakker5121ce52009-01-03 21:22:43 +00007903 /* more data available */
7904 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007905 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007908
Paul Bakker23986e52011-04-24 08:57:21 +00007909 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007910}
7911
7912/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007913 * Send application data to be encrypted by the SSL layer, taking care of max
7914 * fragment length and buffer size.
7915 *
7916 * According to RFC 5246 Section 6.2.1:
7917 *
7918 * Zero-length fragments of Application data MAY be sent as they are
7919 * potentially useful as a traffic analysis countermeasure.
7920 *
7921 * Therefore, it is possible that the input message length is 0 and the
7922 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007923 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007924static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007925 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007926{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007927 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
7928 const size_t max_len = (size_t) ret;
7929
7930 if( ret < 0 )
7931 {
7932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
7933 return( ret );
7934 }
7935
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007936 if( len > max_len )
7937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007939 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007942 "maximum fragment length: %d > %d",
7943 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007944 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007945 }
7946 else
7947#endif
7948 len = max_len;
7949 }
Paul Bakker887bd502011-06-08 13:10:54 +00007950
Paul Bakker5121ce52009-01-03 21:22:43 +00007951 if( ssl->out_left != 0 )
7952 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007953 /*
7954 * The user has previously tried to send the data and
7955 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7956 * written. In this case, we expect the high-level write function
7957 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7958 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007962 return( ret );
7963 }
7964 }
Paul Bakker887bd502011-06-08 13:10:54 +00007965 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00007966 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007967 /*
7968 * The user is trying to send a message the first time, so we need to
7969 * copy the data into the internal buffers and setup the data structure
7970 * to keep track of partial writes
7971 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007972 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007974 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00007975
Hanno Becker67bc7c32018-08-06 11:33:50 +01007976 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00007977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007978 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00007979 return( ret );
7980 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007981 }
7982
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007983 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007984}
7985
7986/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007987 * Write application data, doing 1/n-1 splitting if necessary.
7988 *
7989 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007990 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01007991 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007992 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007994static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007995 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007996{
7997 int ret;
7998
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007999 if( ssl->conf->cbc_record_splitting ==
8000 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008001 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008002 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8003 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8004 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008005 {
8006 return( ssl_write_real( ssl, buf, len ) );
8007 }
8008
8009 if( ssl->split_done == 0 )
8010 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008011 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008012 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008013 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008014 }
8015
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008016 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8017 return( ret );
8018 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008019
8020 return( ret + 1 );
8021}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008023
8024/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008025 * Write application data (public-facing wrapper)
8026 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008027int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008028{
8029 int ret;
8030
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008032
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008033 if( ssl == NULL || ssl->conf == NULL )
8034 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8035
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008036#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008037 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8038 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008039 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008040 return( ret );
8041 }
8042#endif
8043
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008044 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008045 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008046 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008047 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008048 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008049 return( ret );
8050 }
8051 }
8052
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008053#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008054 ret = ssl_write_split( ssl, buf, len );
8055#else
8056 ret = ssl_write_real( ssl, buf, len );
8057#endif
8058
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008060
8061 return( ret );
8062}
8063
8064/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008065 * Notify the peer that the connection is being closed
8066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008067int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008068{
8069 int ret;
8070
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008071 if( ssl == NULL || ssl->conf == NULL )
8072 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008075
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008076 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008077 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008079 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8082 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8083 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008085 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008086 return( ret );
8087 }
8088 }
8089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008091
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008092 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008093}
8094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008095void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008096{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008097 if( transform == NULL )
8098 return;
8099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008100#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008101 deflateEnd( &transform->ctx_deflate );
8102 inflateEnd( &transform->ctx_inflate );
8103#endif
8104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008105 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8106 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008108 mbedtls_md_free( &transform->md_ctx_enc );
8109 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008110
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008111 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008112}
8113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008114#if defined(MBEDTLS_X509_CRT_PARSE_C)
8115static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008116{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008117 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008118
8119 while( cur != NULL )
8120 {
8121 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008122 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008123 cur = next;
8124 }
8125}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008126#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008127
Gilles Peskine9b562d52018-04-25 20:32:43 +02008128void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008129{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008130 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8131
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008132 if( handshake == NULL )
8133 return;
8134
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008135#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8136 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8137 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008138 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008139 handshake->async_in_progress = 0;
8140 }
8141#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8142
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008143#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8144 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8145 mbedtls_md5_free( &handshake->fin_md5 );
8146 mbedtls_sha1_free( &handshake->fin_sha1 );
8147#endif
8148#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8149#if defined(MBEDTLS_SHA256_C)
8150 mbedtls_sha256_free( &handshake->fin_sha256 );
8151#endif
8152#if defined(MBEDTLS_SHA512_C)
8153 mbedtls_sha512_free( &handshake->fin_sha512 );
8154#endif
8155#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008157#if defined(MBEDTLS_DHM_C)
8158 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008159#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008160#if defined(MBEDTLS_ECDH_C)
8161 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008162#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008163#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008164 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008165#if defined(MBEDTLS_SSL_CLI_C)
8166 mbedtls_free( handshake->ecjpake_cache );
8167 handshake->ecjpake_cache = NULL;
8168 handshake->ecjpake_cache_len = 0;
8169#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008170#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008171
Janos Follath4ae5c292016-02-10 11:27:43 +00008172#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8173 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008174 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008175 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008176#endif
8177
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008178#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8179 if( handshake->psk != NULL )
8180 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008181 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008182 mbedtls_free( handshake->psk );
8183 }
8184#endif
8185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008186#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8187 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008188 /*
8189 * Free only the linked list wrapper, not the keys themselves
8190 * since the belong to the SNI callback
8191 */
8192 if( handshake->sni_key_cert != NULL )
8193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008195
8196 while( cur != NULL )
8197 {
8198 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008199 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008200 cur = next;
8201 }
8202 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008203#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008205#if defined(MBEDTLS_SSL_PROTO_DTLS)
8206 mbedtls_free( handshake->verify_cookie );
8207 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008208 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008209#endif
8210
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008211 mbedtls_platform_zeroize( handshake,
8212 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008213}
8214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008215void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008216{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008217 if( session == NULL )
8218 return;
8219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008220#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008221 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008223 mbedtls_x509_crt_free( session->peer_cert );
8224 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008225 }
Paul Bakkered27a042013-04-18 22:46:23 +02008226#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008227
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008228#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008229 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008230#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008231
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008232 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008233}
8234
Paul Bakker5121ce52009-01-03 21:22:43 +00008235/*
8236 * Free an SSL context
8237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008238void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008239{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008240 if( ssl == NULL )
8241 return;
8242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008244
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008245 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008246 {
Angus Grattond8213d02016-05-25 20:56:48 +10008247 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008248 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008249 }
8250
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008251 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008252 {
Angus Grattond8213d02016-05-25 20:56:48 +10008253 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008254 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008255 }
8256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008257#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008258 if( ssl->compress_buf != NULL )
8259 {
Angus Grattond8213d02016-05-25 20:56:48 +10008260 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008261 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008262 }
8263#endif
8264
Paul Bakker48916f92012-09-16 19:57:18 +00008265 if( ssl->transform )
8266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008267 mbedtls_ssl_transform_free( ssl->transform );
8268 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008269 }
8270
8271 if( ssl->handshake )
8272 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008273 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008274 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8275 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008277 mbedtls_free( ssl->handshake );
8278 mbedtls_free( ssl->transform_negotiate );
8279 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008280 }
8281
Paul Bakkerc0463502013-02-14 11:19:38 +01008282 if( ssl->session )
8283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008284 mbedtls_ssl_session_free( ssl->session );
8285 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008286 }
8287
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008288#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008289 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008290 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008291 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008292 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008293 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008294#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008296#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8297 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8300 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008301 }
8302#endif
8303
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008304#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008305 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008306#endif
8307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008309
Paul Bakker86f04f42013-02-14 11:20:09 +01008310 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008311 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008312}
8313
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008314/*
8315 * Initialze mbedtls_ssl_config
8316 */
8317void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8318{
8319 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8320}
8321
Simon Butcherc97b6972015-12-27 23:48:17 +00008322#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008323static int ssl_preset_default_hashes[] = {
8324#if defined(MBEDTLS_SHA512_C)
8325 MBEDTLS_MD_SHA512,
8326 MBEDTLS_MD_SHA384,
8327#endif
8328#if defined(MBEDTLS_SHA256_C)
8329 MBEDTLS_MD_SHA256,
8330 MBEDTLS_MD_SHA224,
8331#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008332#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008333 MBEDTLS_MD_SHA1,
8334#endif
8335 MBEDTLS_MD_NONE
8336};
Simon Butcherc97b6972015-12-27 23:48:17 +00008337#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008338
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008339static int ssl_preset_suiteb_ciphersuites[] = {
8340 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8341 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8342 0
8343};
8344
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008345#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008346static int ssl_preset_suiteb_hashes[] = {
8347 MBEDTLS_MD_SHA256,
8348 MBEDTLS_MD_SHA384,
8349 MBEDTLS_MD_NONE
8350};
8351#endif
8352
8353#if defined(MBEDTLS_ECP_C)
8354static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8355 MBEDTLS_ECP_DP_SECP256R1,
8356 MBEDTLS_ECP_DP_SECP384R1,
8357 MBEDTLS_ECP_DP_NONE
8358};
8359#endif
8360
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008361/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008362 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008363 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008364int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008365 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008366{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008367#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008368 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008369#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008370
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008371 /* Use the functions here so that they are covered in tests,
8372 * but otherwise access member directly for efficiency */
8373 mbedtls_ssl_conf_endpoint( conf, endpoint );
8374 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008375
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008376 /*
8377 * Things that are common to all presets
8378 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008379#if defined(MBEDTLS_SSL_CLI_C)
8380 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8381 {
8382 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8383#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8384 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8385#endif
8386 }
8387#endif
8388
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008389#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008390 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008391#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008392
8393#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8394 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8395#endif
8396
8397#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8398 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8399#endif
8400
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008401#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8402 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
8403#endif
8404
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008405#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008406 conf->f_cookie_write = ssl_cookie_write_dummy;
8407 conf->f_cookie_check = ssl_cookie_check_dummy;
8408#endif
8409
8410#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
8411 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
8412#endif
8413
Janos Follath088ce432017-04-10 12:42:31 +01008414#if defined(MBEDTLS_SSL_SRV_C)
8415 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
8416#endif
8417
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008418#if defined(MBEDTLS_SSL_PROTO_DTLS)
8419 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
8420 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
8421#endif
8422
8423#if defined(MBEDTLS_SSL_RENEGOTIATION)
8424 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00008425 memset( conf->renego_period, 0x00, 2 );
8426 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008427#endif
8428
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008429#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
8430 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8431 {
Hanno Becker00d0a682017-10-04 13:14:29 +01008432 const unsigned char dhm_p[] =
8433 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
8434 const unsigned char dhm_g[] =
8435 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
8436
Hanno Beckera90658f2017-10-04 15:29:08 +01008437 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
8438 dhm_p, sizeof( dhm_p ),
8439 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008440 {
8441 return( ret );
8442 }
8443 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008444#endif
8445
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008446 /*
8447 * Preset-specific defaults
8448 */
8449 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008450 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008451 /*
8452 * NSA Suite B
8453 */
8454 case MBEDTLS_SSL_PRESET_SUITEB:
8455 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
8456 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
8457 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8458 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8459
8460 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8461 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8462 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8463 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8464 ssl_preset_suiteb_ciphersuites;
8465
8466#if defined(MBEDTLS_X509_CRT_PARSE_C)
8467 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008468#endif
8469
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008470#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008471 conf->sig_hashes = ssl_preset_suiteb_hashes;
8472#endif
8473
8474#if defined(MBEDTLS_ECP_C)
8475 conf->curve_list = ssl_preset_suiteb_curves;
8476#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02008477 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008478
8479 /*
8480 * Default
8481 */
8482 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03008483 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
8484 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
8485 MBEDTLS_SSL_MIN_MAJOR_VERSION :
8486 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
8487 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
8488 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
8489 MBEDTLS_SSL_MIN_MINOR_VERSION :
8490 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008491 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8492 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8493
8494#if defined(MBEDTLS_SSL_PROTO_DTLS)
8495 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8496 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
8497#endif
8498
8499 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8500 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8501 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8502 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8503 mbedtls_ssl_list_ciphersuites();
8504
8505#if defined(MBEDTLS_X509_CRT_PARSE_C)
8506 conf->cert_profile = &mbedtls_x509_crt_profile_default;
8507#endif
8508
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008509#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008510 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008511#endif
8512
8513#if defined(MBEDTLS_ECP_C)
8514 conf->curve_list = mbedtls_ecp_grp_id_list();
8515#endif
8516
8517#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8518 conf->dhm_min_bitlen = 1024;
8519#endif
8520 }
8521
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008522 return( 0 );
8523}
8524
8525/*
8526 * Free mbedtls_ssl_config
8527 */
8528void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
8529{
8530#if defined(MBEDTLS_DHM_C)
8531 mbedtls_mpi_free( &conf->dhm_P );
8532 mbedtls_mpi_free( &conf->dhm_G );
8533#endif
8534
8535#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8536 if( conf->psk != NULL )
8537 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008538 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008539 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00008540 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008541 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09008542 }
8543
8544 if( conf->psk_identity != NULL )
8545 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008546 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09008547 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00008548 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008549 conf->psk_identity_len = 0;
8550 }
8551#endif
8552
8553#if defined(MBEDTLS_X509_CRT_PARSE_C)
8554 ssl_key_cert_free( conf->key_cert );
8555#endif
8556
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008557 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008558}
8559
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008560#if defined(MBEDTLS_PK_C) && \
8561 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008562/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008563 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008565unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008566{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008567#if defined(MBEDTLS_RSA_C)
8568 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
8569 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008570#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008571#if defined(MBEDTLS_ECDSA_C)
8572 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
8573 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008574#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008575 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008576}
8577
Hanno Becker7e5437a2017-04-28 17:15:26 +01008578unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
8579{
8580 switch( type ) {
8581 case MBEDTLS_PK_RSA:
8582 return( MBEDTLS_SSL_SIG_RSA );
8583 case MBEDTLS_PK_ECDSA:
8584 case MBEDTLS_PK_ECKEY:
8585 return( MBEDTLS_SSL_SIG_ECDSA );
8586 default:
8587 return( MBEDTLS_SSL_SIG_ANON );
8588 }
8589}
8590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008591mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008592{
8593 switch( sig )
8594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008595#if defined(MBEDTLS_RSA_C)
8596 case MBEDTLS_SSL_SIG_RSA:
8597 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008598#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008599#if defined(MBEDTLS_ECDSA_C)
8600 case MBEDTLS_SSL_SIG_ECDSA:
8601 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008602#endif
8603 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008604 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008605 }
8606}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008607#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008608
Hanno Becker7e5437a2017-04-28 17:15:26 +01008609#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8610 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8611
8612/* Find an entry in a signature-hash set matching a given hash algorithm. */
8613mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
8614 mbedtls_pk_type_t sig_alg )
8615{
8616 switch( sig_alg )
8617 {
8618 case MBEDTLS_PK_RSA:
8619 return( set->rsa );
8620 case MBEDTLS_PK_ECDSA:
8621 return( set->ecdsa );
8622 default:
8623 return( MBEDTLS_MD_NONE );
8624 }
8625}
8626
8627/* Add a signature-hash-pair to a signature-hash set */
8628void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
8629 mbedtls_pk_type_t sig_alg,
8630 mbedtls_md_type_t md_alg )
8631{
8632 switch( sig_alg )
8633 {
8634 case MBEDTLS_PK_RSA:
8635 if( set->rsa == MBEDTLS_MD_NONE )
8636 set->rsa = md_alg;
8637 break;
8638
8639 case MBEDTLS_PK_ECDSA:
8640 if( set->ecdsa == MBEDTLS_MD_NONE )
8641 set->ecdsa = md_alg;
8642 break;
8643
8644 default:
8645 break;
8646 }
8647}
8648
8649/* Allow exactly one hash algorithm for each signature. */
8650void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
8651 mbedtls_md_type_t md_alg )
8652{
8653 set->rsa = md_alg;
8654 set->ecdsa = md_alg;
8655}
8656
8657#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
8658 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
8659
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008660/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008661 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008662 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008663mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008664{
8665 switch( hash )
8666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008667#if defined(MBEDTLS_MD5_C)
8668 case MBEDTLS_SSL_HASH_MD5:
8669 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008670#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008671#if defined(MBEDTLS_SHA1_C)
8672 case MBEDTLS_SSL_HASH_SHA1:
8673 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008674#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008675#if defined(MBEDTLS_SHA256_C)
8676 case MBEDTLS_SSL_HASH_SHA224:
8677 return( MBEDTLS_MD_SHA224 );
8678 case MBEDTLS_SSL_HASH_SHA256:
8679 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008680#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008681#if defined(MBEDTLS_SHA512_C)
8682 case MBEDTLS_SSL_HASH_SHA384:
8683 return( MBEDTLS_MD_SHA384 );
8684 case MBEDTLS_SSL_HASH_SHA512:
8685 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008686#endif
8687 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008688 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008689 }
8690}
8691
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008692/*
8693 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
8694 */
8695unsigned char mbedtls_ssl_hash_from_md_alg( int md )
8696{
8697 switch( md )
8698 {
8699#if defined(MBEDTLS_MD5_C)
8700 case MBEDTLS_MD_MD5:
8701 return( MBEDTLS_SSL_HASH_MD5 );
8702#endif
8703#if defined(MBEDTLS_SHA1_C)
8704 case MBEDTLS_MD_SHA1:
8705 return( MBEDTLS_SSL_HASH_SHA1 );
8706#endif
8707#if defined(MBEDTLS_SHA256_C)
8708 case MBEDTLS_MD_SHA224:
8709 return( MBEDTLS_SSL_HASH_SHA224 );
8710 case MBEDTLS_MD_SHA256:
8711 return( MBEDTLS_SSL_HASH_SHA256 );
8712#endif
8713#if defined(MBEDTLS_SHA512_C)
8714 case MBEDTLS_MD_SHA384:
8715 return( MBEDTLS_SSL_HASH_SHA384 );
8716 case MBEDTLS_MD_SHA512:
8717 return( MBEDTLS_SSL_HASH_SHA512 );
8718#endif
8719 default:
8720 return( MBEDTLS_SSL_HASH_NONE );
8721 }
8722}
8723
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008724#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008725/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008726 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008727 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008728 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008729int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008730{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008731 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008732
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008733 if( ssl->conf->curve_list == NULL )
8734 return( -1 );
8735
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008736 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008737 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008738 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008739
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008740 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008741}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008742#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008743
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008744#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008745/*
8746 * Check if a hash proposed by the peer is in our list.
8747 * Return 0 if we're willing to use it, -1 otherwise.
8748 */
8749int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
8750 mbedtls_md_type_t md )
8751{
8752 const int *cur;
8753
8754 if( ssl->conf->sig_hashes == NULL )
8755 return( -1 );
8756
8757 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
8758 if( *cur == (int) md )
8759 return( 0 );
8760
8761 return( -1 );
8762}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008763#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008765#if defined(MBEDTLS_X509_CRT_PARSE_C)
8766int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
8767 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008768 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008769 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008770{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008771 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008772#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008773 int usage = 0;
8774#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008775#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008776 const char *ext_oid;
8777 size_t ext_len;
8778#endif
8779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008780#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
8781 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008782 ((void) cert);
8783 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008784 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008785#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008787#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
8788 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008789 {
8790 /* Server part of the key exchange */
8791 switch( ciphersuite->key_exchange )
8792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008793 case MBEDTLS_KEY_EXCHANGE_RSA:
8794 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008795 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008796 break;
8797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008798 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
8799 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
8800 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
8801 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008802 break;
8803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008804 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
8805 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008806 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008807 break;
8808
8809 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008810 case MBEDTLS_KEY_EXCHANGE_NONE:
8811 case MBEDTLS_KEY_EXCHANGE_PSK:
8812 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
8813 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02008814 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008815 usage = 0;
8816 }
8817 }
8818 else
8819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008820 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
8821 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008822 }
8823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008824 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008825 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008826 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008827 ret = -1;
8828 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008829#else
8830 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008831#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008833#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
8834 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008836 ext_oid = MBEDTLS_OID_SERVER_AUTH;
8837 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008838 }
8839 else
8840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008841 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
8842 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008843 }
8844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008845 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008846 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008847 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008848 ret = -1;
8849 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008850#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008851
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008852 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008853}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008854#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02008855
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008856/*
8857 * Convert version numbers to/from wire format
8858 * and, for DTLS, to/from TLS equivalent.
8859 *
8860 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08008861 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008862 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
8863 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
8864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008865void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008866 unsigned char ver[2] )
8867{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008868#if defined(MBEDTLS_SSL_PROTO_DTLS)
8869 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008871 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008872 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8873
8874 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
8875 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
8876 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008877 else
8878#else
8879 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008880#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008881 {
8882 ver[0] = (unsigned char) major;
8883 ver[1] = (unsigned char) minor;
8884 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008885}
8886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008887void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008888 const unsigned char ver[2] )
8889{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008890#if defined(MBEDTLS_SSL_PROTO_DTLS)
8891 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008892 {
8893 *major = 255 - ver[0] + 2;
8894 *minor = 255 - ver[1] + 1;
8895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008896 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008897 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8898 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008899 else
8900#else
8901 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008902#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008903 {
8904 *major = ver[0];
8905 *minor = ver[1];
8906 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008907}
8908
Simon Butcher99000142016-10-13 17:21:01 +01008909int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
8910{
8911#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8912 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
8913 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8914
8915 switch( md )
8916 {
8917#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
8918#if defined(MBEDTLS_MD5_C)
8919 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01008920 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01008921#endif
8922#if defined(MBEDTLS_SHA1_C)
8923 case MBEDTLS_SSL_HASH_SHA1:
8924 ssl->handshake->calc_verify = ssl_calc_verify_tls;
8925 break;
8926#endif
8927#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
8928#if defined(MBEDTLS_SHA512_C)
8929 case MBEDTLS_SSL_HASH_SHA384:
8930 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
8931 break;
8932#endif
8933#if defined(MBEDTLS_SHA256_C)
8934 case MBEDTLS_SSL_HASH_SHA256:
8935 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
8936 break;
8937#endif
8938 default:
8939 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8940 }
8941
8942 return 0;
8943#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
8944 (void) ssl;
8945 (void) md;
8946
8947 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8948#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8949}
8950
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008951#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8952 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8953int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
8954 unsigned char *output,
8955 unsigned char *data, size_t data_len )
8956{
8957 int ret = 0;
8958 mbedtls_md5_context mbedtls_md5;
8959 mbedtls_sha1_context mbedtls_sha1;
8960
8961 mbedtls_md5_init( &mbedtls_md5 );
8962 mbedtls_sha1_init( &mbedtls_sha1 );
8963
8964 /*
8965 * digitally-signed struct {
8966 * opaque md5_hash[16];
8967 * opaque sha_hash[20];
8968 * };
8969 *
8970 * md5_hash
8971 * MD5(ClientHello.random + ServerHello.random
8972 * + ServerParams);
8973 * sha_hash
8974 * SHA(ClientHello.random + ServerHello.random
8975 * + ServerParams);
8976 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008977 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008978 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008979 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008980 goto exit;
8981 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008982 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008983 ssl->handshake->randbytes, 64 ) ) != 0 )
8984 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008985 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008986 goto exit;
8987 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008988 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008989 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008990 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008991 goto exit;
8992 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008993 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008994 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008995 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008996 goto exit;
8997 }
8998
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008999 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009000 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009001 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009002 goto exit;
9003 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009004 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009005 ssl->handshake->randbytes, 64 ) ) != 0 )
9006 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009007 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009008 goto exit;
9009 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009010 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009011 data_len ) ) != 0 )
9012 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009013 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009014 goto exit;
9015 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009016 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009017 output + 16 ) ) != 0 )
9018 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009020 goto exit;
9021 }
9022
9023exit:
9024 mbedtls_md5_free( &mbedtls_md5 );
9025 mbedtls_sha1_free( &mbedtls_sha1 );
9026
9027 if( ret != 0 )
9028 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9029 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9030
9031 return( ret );
9032
9033}
9034#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9035 MBEDTLS_SSL_PROTO_TLS1_1 */
9036
9037#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9038 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9039int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009040 unsigned char *hash, size_t *hashlen,
9041 unsigned char *data, size_t data_len,
9042 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009043{
9044 int ret = 0;
9045 mbedtls_md_context_t ctx;
9046 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009047 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009048
9049 mbedtls_md_init( &ctx );
9050
9051 /*
9052 * digitally-signed struct {
9053 * opaque client_random[32];
9054 * opaque server_random[32];
9055 * ServerDHParams params;
9056 * };
9057 */
9058 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9059 {
9060 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9061 goto exit;
9062 }
9063 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9064 {
9065 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9066 goto exit;
9067 }
9068 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9069 {
9070 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9071 goto exit;
9072 }
9073 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9074 {
9075 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9076 goto exit;
9077 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009078 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009079 {
9080 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9081 goto exit;
9082 }
9083
9084exit:
9085 mbedtls_md_free( &ctx );
9086
9087 if( ret != 0 )
9088 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9089 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9090
9091 return( ret );
9092}
9093#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9094 MBEDTLS_SSL_PROTO_TLS1_2 */
9095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009096#endif /* MBEDTLS_SSL_TLS_C */