blob: b1bfb6760daba7af7e792db86496801d5398c881 [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
Andrzej Kurekc929a822019-01-14 03:51:11 -050057#if defined(MBEDTLS_USE_PSA_CRYPTO)
58#include "mbedtls/psa_util.h"
59#endif
60
Hanno Becker2a43f6f2018-08-10 11:12:52 +010061static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010062static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010063
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010064/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010066{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020068 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010070#else
71 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010072#endif
73 return( 0 );
74}
75
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020076/*
77 * Start a timer.
78 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020082 if( ssl->f_set_timer == NULL )
83 return;
84
85 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
86 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020087}
88
89/*
90 * Return -1 is timer is expired, 0 if it isn't.
91 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020093{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020094 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020095 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020096
97 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020098 {
99 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200100 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200101 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200102
103 return( 0 );
104}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200105
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100106static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
107 mbedtls_ssl_transform *transform );
108static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
109 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100110
111#define SSL_DONT_FORCE_FLUSH 0
112#define SSL_FORCE_FLUSH 1
113
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200114#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100115
Hanno Beckerd5847772018-08-28 10:09:23 +0100116/* Forward declarations for functions related to message buffering. */
117static void ssl_buffering_free( mbedtls_ssl_context *ssl );
118static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
119 uint8_t slot );
120static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
121static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
122static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
123static int ssl_buffer_message( mbedtls_ssl_context *ssl );
124static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100125static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100126
Hanno Beckera67dee22018-08-22 10:05:20 +0100127static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100128static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100129{
Hanno Becker11682cc2018-08-22 14:41:02 +0100130 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100131
132 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100133 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100134
135 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
136}
137
Hanno Becker67bc7c32018-08-06 11:33:50 +0100138static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
139{
Hanno Becker11682cc2018-08-22 14:41:02 +0100140 size_t const bytes_written = ssl->out_left;
141 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100142
143 /* Double-check that the write-index hasn't gone
144 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100145 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100146 {
147 /* Should never happen... */
148 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
149 }
150
151 return( (int) ( mtu - bytes_written ) );
152}
153
154static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
155{
156 int ret;
157 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400158 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100159
160#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
161 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
162
163 if( max_len > mfl )
164 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100165
166 /* By the standard (RFC 6066 Sect. 4), the MFL extension
167 * only limits the maximum record payload size, so in theory
168 * we would be allowed to pack multiple records of payload size
169 * MFL into a single datagram. However, this would mean that there's
170 * no way to explicitly communicate MTU restrictions to the peer.
171 *
172 * The following reduction of max_len makes sure that we never
173 * write datagrams larger than MFL + Record Expansion Overhead.
174 */
175 if( max_len <= ssl->out_left )
176 return( 0 );
177
178 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100179#endif
180
181 ret = ssl_get_remaining_space_in_datagram( ssl );
182 if( ret < 0 )
183 return( ret );
184 remaining = (size_t) ret;
185
186 ret = mbedtls_ssl_get_record_expansion( ssl );
187 if( ret < 0 )
188 return( ret );
189 expansion = (size_t) ret;
190
191 if( remaining <= expansion )
192 return( 0 );
193
194 remaining -= expansion;
195 if( remaining >= max_len )
196 remaining = max_len;
197
198 return( (int) remaining );
199}
200
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200201/*
202 * Double the retransmit timeout value, within the allowed range,
203 * returning -1 if the maximum value has already been reached.
204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200206{
207 uint32_t new_timeout;
208
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200209 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200210 return( -1 );
211
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200212 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
213 * in the following way: after the initial transmission and a first
214 * retransmission, back off to a temporary estimated MTU of 508 bytes.
215 * This value is guaranteed to be deliverable (if not guaranteed to be
216 * delivered) of any compliant IPv4 (and IPv6) network, and should work
217 * on most non-IP stacks too. */
218 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400219 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200220 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
222 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200223
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200224 new_timeout = 2 * ssl->handshake->retransmit_timeout;
225
226 /* Avoid arithmetic overflow and range overflow */
227 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200228 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200229 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200230 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200231 }
232
233 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200235 ssl->handshake->retransmit_timeout ) );
236
237 return( 0 );
238}
239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200241{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200242 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200244 ssl->handshake->retransmit_timeout ) );
245}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200249/*
250 * Convert max_fragment_length codes to length.
251 * RFC 6066 says:
252 * enum{
253 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
254 * } MaxFragmentLength;
255 * and we add 0 -> extension unused
256 */
Angus Grattond8213d02016-05-25 20:56:48 +1000257static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200258{
Angus Grattond8213d02016-05-25 20:56:48 +1000259 switch( mfl )
260 {
261 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
262 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
263 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
264 return 512;
265 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
266 return 1024;
267 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
268 return 2048;
269 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
270 return 4096;
271 default:
272 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
273 }
274}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200276
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200277#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200279{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_ssl_session_free( dst );
281 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200284 if( src->peer_cert != NULL )
285 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200286 int ret;
287
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200288 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200289 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200290 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200295 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200298 dst->peer_cert = NULL;
299 return( ret );
300 }
301 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200304#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200305 if( src->ticket != NULL )
306 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200307 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200308 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200309 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200310
311 memcpy( dst->ticket, src->ticket, src->ticket_len );
312 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200313#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200314
315 return( 0 );
316}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200317#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
320int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200321 const unsigned char *key_enc, const unsigned char *key_dec,
322 size_t keylen,
323 const unsigned char *iv_enc, const unsigned char *iv_dec,
324 size_t ivlen,
325 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200326 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
328int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
329int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
330int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
331int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
332#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000333
Paul Bakker5121ce52009-01-03 21:22:43 +0000334/*
335 * Key material generation
336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200338static int ssl3_prf( const unsigned char *secret, size_t slen,
339 const char *label,
340 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000341 unsigned char *dstbuf, size_t dlen )
342{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100343 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000344 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200345 mbedtls_md5_context md5;
346 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000347 unsigned char padding[16];
348 unsigned char sha1sum[20];
349 ((void)label);
350
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200351 mbedtls_md5_init( &md5 );
352 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200353
Paul Bakker5f70b252012-09-13 14:23:06 +0000354 /*
355 * SSLv3:
356 * block =
357 * MD5( secret + SHA1( 'A' + secret + random ) ) +
358 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
359 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
360 * ...
361 */
362 for( i = 0; i < dlen / 16; i++ )
363 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200364 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000365
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100366 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100367 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100368 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100369 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100370 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100371 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100372 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100373 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100374 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100375 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000376
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100377 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100378 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100379 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100380 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100381 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100382 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100383 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100384 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000385 }
386
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100387exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200388 mbedtls_md5_free( &md5 );
389 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000390
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500391 mbedtls_platform_zeroize( padding, sizeof( padding ) );
392 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000393
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100394 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000395}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200399static int tls1_prf( const unsigned char *secret, size_t slen,
400 const char *label,
401 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000402 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000403{
Paul Bakker23986e52011-04-24 08:57:21 +0000404 size_t nb, hs;
405 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200406 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000407 unsigned char tmp[128];
408 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 const mbedtls_md_info_t *md_info;
410 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100411 int ret;
412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000414
415 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000417
418 hs = ( slen + 1 ) / 2;
419 S1 = secret;
420 S2 = secret + slen - hs;
421
422 nb = strlen( label );
423 memcpy( tmp + 20, label, nb );
424 memcpy( tmp + 20 + nb, random, rlen );
425 nb += rlen;
426
427 /*
428 * First compute P_md5(secret,label+random)[0..dlen]
429 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
431 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100434 return( ret );
435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
437 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
438 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000439
440 for( i = 0; i < dlen; i += 16 )
441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_md_hmac_reset ( &md_ctx );
443 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
444 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_md_hmac_reset ( &md_ctx );
447 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
448 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000449
450 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
451
452 for( j = 0; j < k; j++ )
453 dstbuf[i + j] = h_i[j];
454 }
455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100457
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 /*
459 * XOR out with P_sha1(secret,label+random)[0..dlen]
460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
462 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100465 return( ret );
466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
468 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
469 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
471 for( i = 0; i < dlen; i += 20 )
472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 mbedtls_md_hmac_reset ( &md_ctx );
474 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
475 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_md_hmac_reset ( &md_ctx );
478 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
479 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000480
481 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
482
483 for( j = 0; j < k; j++ )
484 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
485 }
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100488
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500489 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
490 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 return( 0 );
493}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500497#if defined(MBEDTLS_USE_PSA_CRYPTO)
498static int tls_prf_generic( mbedtls_md_type_t md_type,
499 const unsigned char *secret, size_t slen,
500 const char *label,
501 const unsigned char *random, size_t rlen,
502 unsigned char *dstbuf, size_t dlen )
503{
504 psa_status_t status;
505 psa_algorithm_t alg;
506 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500507 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500508 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
509
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500510 if( ( status = psa_allocate_key( PSA_KEY_TYPE_DERIVE,
511 slen * 8, &master_slot ) ) != PSA_SUCCESS )
512 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500513 if( status != PSA_SUCCESS )
514 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
515 if( md_type == MBEDTLS_MD_SHA384 )
516 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
517 else
518 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
519
520 psa_key_policy_init( &policy );
521 psa_key_policy_set_usage( &policy,
522 PSA_KEY_USAGE_DERIVE,
523 alg );
524 status = psa_set_key_policy( master_slot, &policy );
525 if( status != PSA_SUCCESS )
526 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
527
528 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
529 if( status != PSA_SUCCESS )
530 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
531
532 status = psa_key_derivation( &generator,
533 master_slot, alg,
534 random, rlen,
535 (unsigned char const *) label,
536 (size_t) strlen( label ),
537 dlen );
538 if( status != PSA_SUCCESS )
539 {
540 psa_generator_abort( &generator );
541 psa_destroy_key( master_slot );
542 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
543 }
544
545 status = psa_generator_read( &generator, dstbuf, dlen );
546 if( status != PSA_SUCCESS )
547 {
548 psa_generator_abort( &generator );
549 psa_destroy_key( master_slot );
550 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
551 }
552
553 status = psa_generator_abort( &generator );
554 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500555 {
556 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500557 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500558 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500559
560 status = psa_destroy_key( master_slot );
561 if( status != PSA_SUCCESS )
562 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
563
Andrzej Kurek33171262019-01-15 03:25:18 -0500564 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500565}
566
567#else /* MBEDTLS_USE_PSA_CRYPTO */
568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100570 const unsigned char *secret, size_t slen,
571 const char *label,
572 const unsigned char *random, size_t rlen,
573 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000574{
575 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100576 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000577 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
579 const mbedtls_md_info_t *md_info;
580 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100581 int ret;
582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
586 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100589
590 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000592
593 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100594 memcpy( tmp + md_len, label, nb );
595 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000596 nb += rlen;
597
598 /*
599 * Compute P_<hash>(secret, label + random)[0..dlen]
600 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100602 return( ret );
603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
605 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
606 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100607
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100608 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 mbedtls_md_hmac_reset ( &md_ctx );
611 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
612 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 mbedtls_md_hmac_reset ( &md_ctx );
615 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
616 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000617
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100618 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000619
620 for( j = 0; j < k; j++ )
621 dstbuf[i + j] = h_i[j];
622 }
623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100625
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500626 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
627 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000628
629 return( 0 );
630}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500631#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100633static int tls_prf_sha256( const unsigned char *secret, size_t slen,
634 const char *label,
635 const unsigned char *random, size_t rlen,
636 unsigned char *dstbuf, size_t dlen )
637{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100639 label, random, rlen, dstbuf, dlen ) );
640}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200644static int tls_prf_sha384( const unsigned char *secret, size_t slen,
645 const char *label,
646 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000647 unsigned char *dstbuf, size_t dlen )
648{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100650 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000651}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#endif /* MBEDTLS_SHA512_C */
653#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
658 defined(MBEDTLS_SSL_PROTO_TLS1_1)
659static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200660#endif
Paul Bakker380da532012-04-18 16:10:25 +0000661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#if defined(MBEDTLS_SSL_PROTO_SSL3)
663static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
664static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200665#endif
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
668static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
669static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200670#endif
671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
673#if defined(MBEDTLS_SHA256_C)
674static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
675static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
676static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200677#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#if defined(MBEDTLS_SHA512_C)
680static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
681static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
682static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100683#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000685
Hanno Becker7d0a5692018-10-23 15:26:22 +0100686#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
687 defined(MBEDTLS_USE_PSA_CRYPTO)
688static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
689{
690 if( ssl->conf->f_psk != NULL )
691 {
692 /* If we've used a callback to select the PSK,
693 * the static configuration is irrelevant. */
694 if( ssl->handshake->psk_opaque != 0 )
695 return( 1 );
696
697 return( 0 );
698 }
699
700 if( ssl->conf->psk_opaque != 0 )
701 return( 1 );
702
703 return( 0 );
704}
705#endif /* MBEDTLS_USE_PSA_CRYPTO &&
706 MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000709{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200710 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000711#if defined(MBEDTLS_USE_PSA_CRYPTO)
712 int psa_fallthrough;
713#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000714 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000715 unsigned char keyblk[256];
716 unsigned char *key1;
717 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100718 unsigned char *mac_enc;
719 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000720 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200721 size_t iv_copy_len;
Hanno Beckerf704bef2018-11-16 15:21:18 +0000722 size_t taglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 const mbedtls_cipher_info_t *cipher_info;
724 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100725
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000726 /* cf. RFC 5246, Section 8.1:
727 * "The master secret is always exactly 48 bytes in length." */
728 size_t const master_secret_len = 48;
729
Hanno Becker35b23c72018-10-23 12:10:41 +0100730#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
731 unsigned char session_hash[48];
732#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_ssl_session *session = ssl->session_negotiate;
735 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
736 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100741 if( cipher_info == NULL )
742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100744 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100746 }
747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100749 if( md_info == NULL )
750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100752 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100754 }
755
Paul Bakker5121ce52009-01-03 21:22:43 +0000756 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000757 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#if defined(MBEDTLS_SSL_PROTO_SSL3)
760 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000761 {
Paul Bakker48916f92012-09-16 19:57:18 +0000762 handshake->tls_prf = ssl3_prf;
763 handshake->calc_verify = ssl_calc_verify_ssl;
764 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000765 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200766 else
767#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
769 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000770 {
Paul Bakker48916f92012-09-16 19:57:18 +0000771 handshake->tls_prf = tls1_prf;
772 handshake->calc_verify = ssl_calc_verify_tls;
773 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000774 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200775 else
776#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
778#if defined(MBEDTLS_SHA512_C)
779 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
780 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000781 {
Paul Bakker48916f92012-09-16 19:57:18 +0000782 handshake->tls_prf = tls_prf_sha384;
783 handshake->calc_verify = ssl_calc_verify_tls_sha384;
784 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000785 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000786 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200787#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788#if defined(MBEDTLS_SHA256_C)
789 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000790 {
Paul Bakker48916f92012-09-16 19:57:18 +0000791 handshake->tls_prf = tls_prf_sha256;
792 handshake->calc_verify = ssl_calc_verify_tls_sha256;
793 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000794 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200795 else
796#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
800 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200801 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000802
803 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000804 * SSLv3:
805 * master =
806 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
807 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
808 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200809 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200810 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000811 * master = PRF( premaster, "master secret", randbytes )[0..47]
812 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100813 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000814 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100815 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
816 }
817 else
818 {
819 /* The label for the KDF used for key expansion.
820 * This is either "master secret" or "extended master secret"
821 * depending on whether the Extended Master Secret extension
822 * is used. */
823 char const *lbl = "master secret";
824
825 /* The salt for the KDF used for key expansion.
826 * - If the Extended Master Secret extension is not used,
827 * this is ClientHello.Random + ServerHello.Random
828 * (see Sect. 8.1 in RFC 5246).
829 * - If the Extended Master Secret extension is used,
830 * this is the transcript of the handshake so far.
831 * (see Sect. 4 in RFC 7627). */
832 unsigned char const *salt = handshake->randbytes;
833 size_t salt_len = 64;
834
835#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
836 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
837 ssl->transform_negotiate->ciphersuite_info;
838 mbedtls_md_type_t const md_type = ciphersuite_info->mac;
839#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Paul Bakker5121ce52009-01-03 21:22:43 +0000840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
842 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200845
Hanno Becker35b23c72018-10-23 12:10:41 +0100846 lbl = "extended master secret";
847 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200848 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
850 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852#if defined(MBEDTLS_SHA512_C)
Hanno Becker35b23c72018-10-23 12:10:41 +0100853 if( md_type == MBEDTLS_MD_SHA384 )
854 salt_len = 48;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200855 else
Hanno Becker35b23c72018-10-23 12:10:41 +0100856#endif /* MBEDTLS_SHA512_C */
857 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200858 }
859 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100861 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200862
Hanno Becker35b23c72018-10-23 12:10:41 +0100863 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200864 }
Hanno Becker35b23c72018-10-23 12:10:41 +0100865#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
866
Hanno Becker7d0a5692018-10-23 15:26:22 +0100867#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
868 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
869 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
870 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
871 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100872 {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100873 /* Perform PSK-to-MS expansion in a single step. */
874 psa_status_t status;
875 psa_algorithm_t alg;
876 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500877 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100878
879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
880
881 psk = ssl->conf->psk_opaque;
882 if( ssl->handshake->psk_opaque != 0 )
883 psk = ssl->handshake->psk_opaque;
884
885 if( md_type == MBEDTLS_MD_SHA384 )
886 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
887 else
888 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
889
890 status = psa_key_derivation( &generator, psk, alg,
891 salt, salt_len,
892 (unsigned char const *) lbl,
893 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000894 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100895 if( status != PSA_SUCCESS )
896 {
897 psa_generator_abort( &generator );
898 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
899 }
900
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000901 status = psa_generator_read( &generator, session->master,
902 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100903 if( status != PSA_SUCCESS )
904 {
905 psa_generator_abort( &generator );
906 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
907 }
908
909 status = psa_generator_abort( &generator );
910 if( status != PSA_SUCCESS )
911 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100912 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100913 else
914#endif
915 {
916 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
917 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000918 session->master,
919 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100920 if( ret != 0 )
921 {
922 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
923 return( ret );
924 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200925
Hanno Becker7d0a5692018-10-23 15:26:22 +0100926 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
927 handshake->premaster,
928 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +0100929
Hanno Becker7d0a5692018-10-23 15:26:22 +0100930 mbedtls_platform_zeroize( handshake->premaster,
931 sizeof(handshake->premaster) );
932 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000933 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000934
935 /*
936 * Swap the client and server random values.
937 */
Paul Bakker48916f92012-09-16 19:57:18 +0000938 memcpy( tmp, handshake->randbytes, 64 );
939 memcpy( handshake->randbytes, tmp + 32, 32 );
940 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500941 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000942
943 /*
944 * SSLv3:
945 * key block =
946 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
947 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
948 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
949 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
950 * ...
951 *
952 * TLSv1:
953 * key block = PRF( master, "key expansion", randbytes )
954 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100955 ret = handshake->tls_prf( session->master, 48, "key expansion",
956 handshake->randbytes, 64, keyblk, 256 );
957 if( ret != 0 )
958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100960 return( ret );
961 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
964 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
965 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
966 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
967 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000968
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500969 mbedtls_platform_zeroize( handshake->randbytes,
970 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000971
972 /*
973 * Determine the appropriate key, IV and MAC length.
974 */
Paul Bakker68884e32013-01-07 18:20:04 +0100975
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200976 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200979 cipher_info->mode == MBEDTLS_MODE_CCM ||
980 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000981 {
Hanno Beckerf704bef2018-11-16 15:21:18 +0000982 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200983
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200984 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000985 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200986
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200987 /* All modes haves 96-bit IVs;
988 * GCM and CCM has 4 implicit and 8 explicit bytes
989 * ChachaPoly has all 12 bytes implicit
990 */
Paul Bakker68884e32013-01-07 18:20:04 +0100991 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200992 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
993 transform->fixed_ivlen = 12;
994 else
995 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200996
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200997 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
998 taglen = transform->ciphersuite_info->flags &
999 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
1000
1001
1002 /* Minimum length of encrypted record */
1003 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
1004 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001005 }
1006 else
1007 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001008 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1010 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001013 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001014 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001015
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001016 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001017 mac_key_len = mbedtls_md_get_size( md_info );
1018 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001021 /*
1022 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1023 * (rfc 6066 page 13 or rfc 2104 section 4),
1024 * so we only need to adjust the length here.
1025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001029
1030#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1031 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001032 * HMAC implementation which also truncates the key
1033 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001034 mac_key_len = transform->maclen;
1035#endif
1036 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001038
1039 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001040 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001042 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001044 transform->minlen = transform->maclen;
1045 else
Paul Bakker68884e32013-01-07 18:20:04 +01001046 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001047 /*
1048 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001049 * 1. if EtM is in use: one block plus MAC
1050 * otherwise: * first multiple of blocklen greater than maclen
1051 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001052 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1054 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001055 {
1056 transform->minlen = transform->maclen
1057 + cipher_info->block_size;
1058 }
1059 else
1060#endif
1061 {
1062 transform->minlen = transform->maclen
1063 + cipher_info->block_size
1064 - transform->maclen % cipher_info->block_size;
1065 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1068 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1069 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001070 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001071 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001072#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1074 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1075 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001076 {
1077 transform->minlen += transform->ivlen;
1078 }
1079 else
1080#endif
1081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1083 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001084 }
Paul Bakker68884e32013-01-07 18:20:04 +01001085 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001086 }
1087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001089 transform->keylen, transform->minlen, transform->ivlen,
1090 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001091
1092 /*
1093 * Finally setup the cipher contexts, IVs and MAC secrets.
1094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001096 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001097 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001098 key1 = keyblk + mac_key_len * 2;
1099 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001100
Paul Bakker68884e32013-01-07 18:20:04 +01001101 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001102 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001103
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001104 /*
1105 * This is not used in TLS v1.1.
1106 */
Paul Bakker48916f92012-09-16 19:57:18 +00001107 iv_copy_len = ( transform->fixed_ivlen ) ?
1108 transform->fixed_ivlen : transform->ivlen;
1109 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
1110 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001111 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 }
1113 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114#endif /* MBEDTLS_SSL_CLI_C */
1115#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001116 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001117 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001118 key1 = keyblk + mac_key_len * 2 + transform->keylen;
1119 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001120
Hanno Becker81c7b182017-11-09 18:39:33 +00001121 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001122 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001123
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001124 /*
1125 * This is not used in TLS v1.1.
1126 */
Paul Bakker48916f92012-09-16 19:57:18 +00001127 iv_copy_len = ( transform->fixed_ivlen ) ?
1128 transform->fixed_ivlen : transform->ivlen;
1129 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
1130 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001131 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001132 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001133 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1137 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001138 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140#if defined(MBEDTLS_SSL_PROTO_SSL3)
1141 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001142 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001143 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1146 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001147 }
1148
Hanno Becker81c7b182017-11-09 18:39:33 +00001149 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1150 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001151 }
1152 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1154#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1155 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1156 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001157 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001158 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1159 For AEAD-based ciphersuites, there is nothing to do here. */
1160 if( mac_key_len != 0 )
1161 {
1162 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1163 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1164 }
Paul Bakker68884e32013-01-07 18:20:04 +01001165 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001166 else
1167#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1170 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001171 }
Paul Bakker68884e32013-01-07 18:20:04 +01001172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1174 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001175 {
1176 int ret = 0;
1177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001181 transform->iv_enc, transform->iv_dec,
1182 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001183 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001184 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1187 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001188 }
1189 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001191
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001192#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1193 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001194 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001195 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1196 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001197 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001198 iv_copy_len );
1199 }
1200#endif
1201
Hanno Beckerf704bef2018-11-16 15:21:18 +00001202#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001203
1204 /* Only use PSA-based ciphers for TLS-1.2.
1205 * That's relevant at least for TLS-1.0, where
1206 * we assume that mbedtls_cipher_crypt() updates
1207 * the structure field for the IV, which the PSA-based
1208 * implementation currently doesn't. */
1209#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1210 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001211 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001212 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
1213 cipher_info, taglen );
1214 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1215 {
1216 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1217 return( ret );
1218 }
1219
1220 if( ret == 0 )
1221 {
1222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based encryption cipher context" ) );
1223 psa_fallthrough = 0;
1224 }
1225 else
1226 {
1227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1228 psa_fallthrough = 1;
1229 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001230 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001231 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001232 psa_fallthrough = 1;
1233#else
1234 psa_fallthrough = 1;
1235#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001236
Hanno Beckercb1cc802018-11-17 22:27:38 +00001237 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001238#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001239 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001240 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001241 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001242 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001243 return( ret );
1244 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001245
Hanno Beckerf704bef2018-11-16 15:21:18 +00001246#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001247 /* Only use PSA-based ciphers for TLS-1.2.
1248 * That's relevant at least for TLS-1.0, where
1249 * we assume that mbedtls_cipher_crypt() updates
1250 * the structure field for the IV, which the PSA-based
1251 * implementation currently doesn't. */
1252#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1253 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001254 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001255 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
1256 cipher_info, taglen );
1257 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1258 {
1259 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1260 return( ret );
1261 }
1262
1263 if( ret == 0 )
1264 {
1265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based decryption cipher context" ) );
1266 psa_fallthrough = 0;
1267 }
1268 else
1269 {
1270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1271 psa_fallthrough = 1;
1272 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001273 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001274 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001275 psa_fallthrough = 1;
1276#else
1277 psa_fallthrough = 1;
1278#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001279
Hanno Beckercb1cc802018-11-17 22:27:38 +00001280 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001281#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001282 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001283 cipher_info ) ) != 0 )
1284 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001285 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001286 return( ret );
1287 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001290 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001294 return( ret );
1295 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001298 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001302 return( ret );
1303 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305#if defined(MBEDTLS_CIPHER_MODE_CBC)
1306 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1309 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001312 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001313 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1316 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001319 return( ret );
1320 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001323
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001324 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001327 // Initialize compression
1328 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001330 {
Paul Bakker16770332013-10-11 09:59:44 +02001331 if( ssl->compress_buf == NULL )
1332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001334 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001335 if( ssl->compress_buf == NULL )
1336 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001338 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001339 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001340 }
1341 }
1342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001344
Paul Bakker48916f92012-09-16 19:57:18 +00001345 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1346 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001347
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001348 if( deflateInit( &transform->ctx_deflate,
1349 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001350 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1353 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001354 }
1355 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001359
1360 return( 0 );
1361}
1362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363#if defined(MBEDTLS_SSL_PROTO_SSL3)
1364void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001365{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001366 mbedtls_md5_context md5;
1367 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001368 unsigned char pad_1[48];
1369 unsigned char pad_2[48];
1370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001372
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001373 mbedtls_md5_init( &md5 );
1374 mbedtls_sha1_init( &sha1 );
1375
1376 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1377 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001378
Paul Bakker380da532012-04-18 16:10:25 +00001379 memset( pad_1, 0x36, 48 );
1380 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001381
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001382 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1383 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1384 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001385
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001386 mbedtls_md5_starts_ret( &md5 );
1387 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1388 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1389 mbedtls_md5_update_ret( &md5, hash, 16 );
1390 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001391
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001392 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1393 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1394 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001395
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001396 mbedtls_sha1_starts_ret( &sha1 );
1397 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1398 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1399 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1400 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001404
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001405 mbedtls_md5_free( &md5 );
1406 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001407
Paul Bakker380da532012-04-18 16:10:25 +00001408 return;
1409}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1413void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001414{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001415 mbedtls_md5_context md5;
1416 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001419
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001420 mbedtls_md5_init( &md5 );
1421 mbedtls_sha1_init( &sha1 );
1422
1423 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1424 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001425
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001426 mbedtls_md5_finish_ret( &md5, hash );
1427 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001431
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001432 mbedtls_md5_free( &md5 );
1433 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001434
Paul Bakker380da532012-04-18 16:10:25 +00001435 return;
1436}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1440#if defined(MBEDTLS_SHA256_C)
1441void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001442{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001443 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001444
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001445 mbedtls_sha256_init( &sha256 );
1446
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001448
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001449 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001450 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001454
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001455 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001456
Paul Bakker380da532012-04-18 16:10:25 +00001457 return;
1458}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461#if defined(MBEDTLS_SHA512_C)
1462void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001463{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001464 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001465
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001466 mbedtls_sha512_init( &sha512 );
1467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001469
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001470 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001471 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001475
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001476 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001477
Paul Bakker5121ce52009-01-03 21:22:43 +00001478 return;
1479}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480#endif /* MBEDTLS_SHA512_C */
1481#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1484int 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 +02001485{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001486 unsigned char *p = ssl->handshake->premaster;
1487 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001488 const unsigned char *psk = ssl->conf->psk;
1489 size_t psk_len = ssl->conf->psk_len;
1490
1491 /* If the psk callback was called, use its result */
1492 if( ssl->handshake->psk != NULL )
1493 {
1494 psk = ssl->handshake->psk;
1495 psk_len = ssl->handshake->psk_len;
1496 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001497
1498 /*
1499 * PMS = struct {
1500 * opaque other_secret<0..2^16-1>;
1501 * opaque psk<0..2^16-1>;
1502 * };
1503 * with "other_secret" depending on the particular key exchange
1504 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1506 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001507 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001508 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001510
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001511 *(p++) = (unsigned char)( psk_len >> 8 );
1512 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001513
1514 if( end < p || (size_t)( end - p ) < psk_len )
1515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1516
1517 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001518 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001519 }
1520 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1522#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1523 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001524 {
1525 /*
1526 * other_secret already set by the ClientKeyExchange message,
1527 * and is 48 bytes long
1528 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001529 if( end - p < 2 )
1530 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1531
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001532 *p++ = 0;
1533 *p++ = 48;
1534 p += 48;
1535 }
1536 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1538#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1539 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001540 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001541 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001542 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001543
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001544 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001546 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001547 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001550 return( ret );
1551 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001552 *(p++) = (unsigned char)( len >> 8 );
1553 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001554 p += len;
1555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001557 }
1558 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1560#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1561 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001562 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001563 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001564 size_t zlen;
1565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001567 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001568 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001571 return( ret );
1572 }
1573
1574 *(p++) = (unsigned char)( zlen >> 8 );
1575 *(p++) = (unsigned char)( zlen );
1576 p += zlen;
1577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001579 }
1580 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1584 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001585 }
1586
1587 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001588 if( end - p < 2 )
1589 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001590
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001591 *(p++) = (unsigned char)( psk_len >> 8 );
1592 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001593
1594 if( end < p || (size_t)( end - p ) < psk_len )
1595 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1596
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001597 memcpy( p, psk, psk_len );
1598 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001599
1600 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1601
1602 return( 0 );
1603}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001607/*
1608 * SSLv3.0 MAC functions
1609 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001610#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001611static void ssl_mac( mbedtls_md_context_t *md_ctx,
1612 const unsigned char *secret,
1613 const unsigned char *buf, size_t len,
1614 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001615 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001616{
1617 unsigned char header[11];
1618 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001619 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1621 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001622
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001623 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001625 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001626 else
Paul Bakker68884e32013-01-07 18:20:04 +01001627 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001628
1629 memcpy( header, ctr, 8 );
1630 header[ 8] = (unsigned char) type;
1631 header[ 9] = (unsigned char)( len >> 8 );
1632 header[10] = (unsigned char)( len );
1633
Paul Bakker68884e32013-01-07 18:20:04 +01001634 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635 mbedtls_md_starts( md_ctx );
1636 mbedtls_md_update( md_ctx, secret, md_size );
1637 mbedtls_md_update( md_ctx, padding, padlen );
1638 mbedtls_md_update( md_ctx, header, 11 );
1639 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001640 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001641
Paul Bakker68884e32013-01-07 18:20:04 +01001642 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 mbedtls_md_starts( md_ctx );
1644 mbedtls_md_update( md_ctx, secret, md_size );
1645 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001646 mbedtls_md_update( md_ctx, out, md_size );
1647 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001648}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1652 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001653 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001654#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001655#endif
1656
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001657/* The function below is only used in the Lucky 13 counter-measure in
1658 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1659#if defined(SSL_SOME_MODES_USE_MAC) && \
1660 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1661 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1662 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1663/* This function makes sure every byte in the memory region is accessed
1664 * (in ascending addresses order) */
1665static void ssl_read_memory( unsigned char *p, size_t len )
1666{
1667 unsigned char acc = 0;
1668 volatile unsigned char force;
1669
1670 for( ; len != 0; p++, len-- )
1671 acc ^= *p;
1672
1673 force = acc;
1674 (void) force;
1675}
1676#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1677
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001678/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001679 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001682{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001684 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001687
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001688 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1691 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001692 }
1693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001697 ssl->out_msg, ssl->out_msglen );
1698
Paul Bakker5121ce52009-01-03 21:22:43 +00001699 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001700 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001701 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001702#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703 if( mode == MBEDTLS_MODE_STREAM ||
1704 ( mode == MBEDTLS_MODE_CBC
1705#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1706 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001707#endif
1708 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710#if defined(MBEDTLS_SSL_PROTO_SSL3)
1711 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001712 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001713 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001714
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001715 ssl_mac( &ssl->transform_out->md_ctx_enc,
1716 ssl->transform_out->mac_enc,
1717 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001718 ssl->out_ctr, ssl->out_msgtype,
1719 mac );
1720
1721 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001722 }
1723 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001724#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1726 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1727 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001728 {
Hanno Becker992b6872017-11-09 18:57:39 +00001729 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1732 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1733 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1734 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001735 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001736 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001738
1739 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001740 }
1741 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001742#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1745 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001746 }
1747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001749 ssl->out_msg + ssl->out_msglen,
1750 ssl->transform_out->maclen );
1751
1752 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001753 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001754 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001755#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001756
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001757 /*
1758 * Encrypt
1759 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1761 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001762 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001763 int ret;
1764 size_t olen = 0;
1765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001767 "including %d bytes of padding",
1768 ssl->out_msglen, 0 ) );
1769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001771 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001772 ssl->transform_out->ivlen,
1773 ssl->out_msg, ssl->out_msglen,
1774 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001777 return( ret );
1778 }
1779
1780 if( ssl->out_msglen != olen )
1781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1783 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001784 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001785 }
Paul Bakker68884e32013-01-07 18:20:04 +01001786 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001788#if defined(MBEDTLS_GCM_C) || \
1789 defined(MBEDTLS_CCM_C) || \
1790 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001792 mode == MBEDTLS_MODE_CCM ||
1793 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001794 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001795 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001796 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001797 unsigned char *enc_msg;
1798 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001799 unsigned char iv[12];
1800 mbedtls_ssl_transform *transform = ssl->transform_out;
1801 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001803 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001804
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001805 /*
1806 * Prepare additional authenticated data
1807 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001808 memcpy( add_data, ssl->out_ctr, 8 );
1809 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001811 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001812 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1813 add_data[12] = ssl->out_msglen & 0xFF;
1814
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001815 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001816
Paul Bakker68884e32013-01-07 18:20:04 +01001817 /*
1818 * Generate IV
1819 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001820 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1821 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001822 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001823 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1824 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1825 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1826
1827 }
1828 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1829 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001830 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001831 unsigned char i;
1832
1833 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1834
1835 for( i = 0; i < 8; i++ )
1836 iv[i+4] ^= ssl->out_ctr[i];
1837 }
1838 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001839 {
1840 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1842 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001843 }
1844
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001845 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1846 iv, transform->ivlen );
1847 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1848 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001849
Paul Bakker68884e32013-01-07 18:20:04 +01001850 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001851 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001852 */
1853 enc_msg = ssl->out_msg;
1854 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001855 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001858 "including 0 bytes of padding",
1859 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001860
Paul Bakker68884e32013-01-07 18:20:04 +01001861 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001862 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001863 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001864 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1865 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001866 add_data, 13,
1867 enc_msg, enc_msglen,
1868 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001869 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001872 return( ret );
1873 }
1874
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001875 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1878 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001879 }
1880
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001881 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001882 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001885 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001886 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1888#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001889 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001891 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001892 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001893 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001894 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001895
Paul Bakker48916f92012-09-16 19:57:18 +00001896 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1897 ssl->transform_out->ivlen;
1898 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001899 padlen = 0;
1900
1901 for( i = 0; i <= padlen; i++ )
1902 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1903
1904 ssl->out_msglen += padlen + 1;
1905
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001906 enc_msglen = ssl->out_msglen;
1907 enc_msg = ssl->out_msg;
1908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001910 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001911 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1912 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001913 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001915 {
1916 /*
1917 * Generate IV
1918 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001919 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001920 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001921 if( ret != 0 )
1922 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001923
Paul Bakker92be97b2013-01-02 17:30:03 +01001924 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001925 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001926
1927 /*
1928 * Fix pointer positions and message length with added IV
1929 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001930 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001931 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001932 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001933 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001937 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001938 ssl->out_msglen, ssl->transform_out->ivlen,
1939 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001942 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001943 ssl->transform_out->ivlen,
1944 enc_msg, enc_msglen,
1945 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001948 return( ret );
1949 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001950
Paul Bakkercca5b812013-08-31 17:40:26 +02001951 if( enc_msglen != olen )
1952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1954 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001955 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1958 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001959 {
1960 /*
1961 * Save IV in SSL3 and TLS1
1962 */
1963 memcpy( ssl->transform_out->iv_enc,
1964 ssl->transform_out->cipher_ctx_enc.iv,
1965 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001966 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001967#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001970 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001971 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001972 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1973
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001974 /*
1975 * MAC(MAC_write_key, seq_num +
1976 * TLSCipherText.type +
1977 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001978 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001979 * IV + // except for TLS 1.0
1980 * ENC(content + padding + padding_length));
1981 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001982 unsigned char pseudo_hdr[13];
1983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001985
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001986 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1987 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001988 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1989 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1994 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001995 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00001996 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001998
Hanno Becker3d8c9072018-01-05 16:24:22 +00001999 memcpy( ssl->out_iv + ssl->out_msglen, mac,
2000 ssl->transform_out->maclen );
2001
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002002 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002003 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002004 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002006 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002007 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002009 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2012 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002013 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002014
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002015 /* Make extra sure authentication was performed, exactly once */
2016 if( auth_done != 1 )
2017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2019 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002020 }
2021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002023
2024 return( 0 );
2025}
2026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002028{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002030 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002031#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002032 size_t padlen = 0, correct = 1;
2033#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002036
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002037 if( ssl->session_in == NULL || ssl->transform_in == NULL )
2038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2040 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002041 }
2042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002044
Paul Bakker48916f92012-09-16 19:57:18 +00002045 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00002048 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002050 }
2051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2053 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002054 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002055 int ret;
2056 size_t olen = 0;
2057
Paul Bakker68884e32013-01-07 18:20:04 +01002058 padlen = 0;
2059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002061 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002062 ssl->transform_in->ivlen,
2063 ssl->in_msg, ssl->in_msglen,
2064 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002067 return( ret );
2068 }
2069
2070 if( ssl->in_msglen != olen )
2071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2073 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002074 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002075 }
Paul Bakker68884e32013-01-07 18:20:04 +01002076 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002078#if defined(MBEDTLS_GCM_C) || \
2079 defined(MBEDTLS_CCM_C) || \
2080 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002082 mode == MBEDTLS_MODE_CCM ||
2083 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002084 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002085 int ret;
2086 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002087 unsigned char *dec_msg;
2088 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002089 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002090 unsigned char iv[12];
2091 mbedtls_ssl_transform *transform = ssl->transform_in;
2092 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002094 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002095
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002096 /*
2097 * Compute and update sizes
2098 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002099 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002102 "+ taglen (%d)", ssl->in_msglen,
2103 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002105 }
2106 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
2107
Paul Bakker68884e32013-01-07 18:20:04 +01002108 dec_msg = ssl->in_msg;
2109 dec_msg_result = ssl->in_msg;
2110 ssl->in_msglen = dec_msglen;
2111
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002112 /*
2113 * Prepare additional authenticated data
2114 */
Paul Bakker68884e32013-01-07 18:20:04 +01002115 memcpy( add_data, ssl->in_ctr, 8 );
2116 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002118 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002119 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2120 add_data[12] = ssl->in_msglen & 0xFF;
2121
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002122 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002123
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002124 /*
2125 * Prepare IV
2126 */
2127 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2128 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002129 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002130 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2131 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002132
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002133 }
2134 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2135 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002136 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002137 unsigned char i;
2138
2139 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2140
2141 for( i = 0; i < 8; i++ )
2142 iv[i+4] ^= ssl->in_ctr[i];
2143 }
2144 else
2145 {
2146 /* Reminder if we ever add an AEAD mode with a different size */
2147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2148 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2149 }
2150
2151 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002153
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002154 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002155 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002158 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002159 add_data, 13,
2160 dec_msg, dec_msglen,
2161 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002162 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2167 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002168
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002169 return( ret );
2170 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002171 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002172
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002173 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2176 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002177 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002178 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002179 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2181#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002182 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002184 {
Paul Bakker45829992013-01-03 14:52:21 +01002185 /*
2186 * Decrypt and check the padding
2187 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002188 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002189 unsigned char *dec_msg;
2190 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002191 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002192 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002193 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002194
Paul Bakker5121ce52009-01-03 21:22:43 +00002195 /*
Paul Bakker45829992013-01-03 14:52:21 +01002196 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2199 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002200 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002201#endif
Paul Bakker45829992013-01-03 14:52:21 +01002202
2203 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2204 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002207 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2208 ssl->transform_in->ivlen,
2209 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002211 }
2212
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002213 dec_msglen = ssl->in_msglen;
2214 dec_msg = ssl->in_msg;
2215 dec_msg_result = ssl->in_msg;
2216
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002217 /*
2218 * Authenticate before decrypt if enabled
2219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2221 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002222 {
Hanno Becker992b6872017-11-09 18:57:39 +00002223 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002224 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002227
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002228 dec_msglen -= ssl->transform_in->maclen;
2229 ssl->in_msglen -= ssl->transform_in->maclen;
2230
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002231 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2232 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2233 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2234 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2239 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002240 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002241 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002245 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002246 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002247 ssl->transform_in->maclen );
2248
Hanno Becker992b6872017-11-09 18:57:39 +00002249 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2250 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002255 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002256 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002257 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002259
2260 /*
2261 * Check length sanity
2262 */
2263 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002266 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002268 }
2269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002271 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002272 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002275 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002276 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002277 dec_msglen -= ssl->transform_in->ivlen;
2278 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002279
Paul Bakker48916f92012-09-16 19:57:18 +00002280 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002281 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002282 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002286 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002287 ssl->transform_in->ivlen,
2288 dec_msg, dec_msglen,
2289 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002292 return( ret );
2293 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002294
Paul Bakkercca5b812013-08-31 17:40:26 +02002295 if( dec_msglen != olen )
2296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2298 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002299 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2302 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002303 {
2304 /*
2305 * Save IV in SSL3 and TLS1
2306 */
2307 memcpy( ssl->transform_in->iv_dec,
2308 ssl->transform_in->cipher_ctx_dec.iv,
2309 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002310 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002311#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002312
2313 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002314
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002315 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002316 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318#if defined(MBEDTLS_SSL_DEBUG_ALL)
2319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002320 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002321#endif
Paul Bakker45829992013-01-03 14:52:21 +01002322 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002323 correct = 0;
2324 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326#if defined(MBEDTLS_SSL_PROTO_SSL3)
2327 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 {
Paul Bakker48916f92012-09-16 19:57:18 +00002329 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331#if defined(MBEDTLS_SSL_DEBUG_ALL)
2332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002333 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002334 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002335#endif
Paul Bakker45829992013-01-03 14:52:21 +01002336 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002337 }
2338 }
2339 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2341#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2342 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2343 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002344 {
2345 /*
Paul Bakker45829992013-01-03 14:52:21 +01002346 * TLSv1+: always check the padding up to the first failure
2347 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002348 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002349 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002350 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002351 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002352
Paul Bakker956c9e02013-12-19 14:42:28 +01002353 /*
2354 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002355 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002356 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002357 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002358 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002359 *
2360 * In both cases we reset padding_idx to a safe value (0) to
2361 * prevent out-of-buffer reads.
2362 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002363 correct &= ( padlen <= ssl->in_msglen );
2364 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002365 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002366
2367 padding_idx *= correct;
2368
Angus Grattonb512bc12018-06-19 15:57:50 +10002369 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002370 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002371 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002372 pad_count += real_count *
2373 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2374 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002375
2376 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002379 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002381#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002382 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002383 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002384 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2386 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2389 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002390 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002391
2392 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002393 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002394 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002396 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2399 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002400 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002401
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002402#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002405#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002406
2407 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002408 * Authenticate if not done yet.
2409 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002411#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002412 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002413 {
Hanno Becker992b6872017-11-09 18:57:39 +00002414 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002415
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002416 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002417
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002418 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2419 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421#if defined(MBEDTLS_SSL_PROTO_SSL3)
2422 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002423 {
2424 ssl_mac( &ssl->transform_in->md_ctx_dec,
2425 ssl->transform_in->mac_dec,
2426 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002427 ssl->in_ctr, ssl->in_msgtype,
2428 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002429 }
2430 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2432#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2433 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2434 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002435 {
2436 /*
2437 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002438 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002439 *
2440 * Known timing attacks:
2441 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2442 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002443 * To compensate for different timings for the MAC calculation
2444 * depending on how much padding was removed (which is determined
2445 * by padlen), process extra_run more blocks through the hash
2446 * function.
2447 *
2448 * The formula in the paper is
2449 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2450 * where L1 is the size of the header plus the decrypted message
2451 * plus CBC padding and L2 is the size of the header plus the
2452 * decrypted message. This is for an underlying hash function
2453 * with 64-byte blocks.
2454 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2455 * correctly. We round down instead of up, so -56 is the correct
2456 * value for our calculations instead of -55.
2457 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002458 * Repeat the formula rather than defining a block_size variable.
2459 * This avoids requiring division by a variable at runtime
2460 * (which would be marginally less efficient and would require
2461 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002462 */
2463 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002464
2465 /*
2466 * The next two sizes are the minimum and maximum values of
2467 * in_msglen over all padlen values.
2468 *
2469 * They're independent of padlen, since we previously did
2470 * in_msglen -= padlen.
2471 *
2472 * Note that max_len + maclen is never more than the buffer
2473 * length, as we previously did in_msglen -= maclen too.
2474 */
2475 const size_t max_len = ssl->in_msglen + padlen;
2476 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2477
Gilles Peskine20b44082018-05-29 14:06:49 +02002478 switch( ssl->transform_in->ciphersuite_info->mac )
2479 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002480#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2481 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002482 case MBEDTLS_MD_MD5:
2483 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002484 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002485 /* 8 bytes of message size, 64-byte compression blocks */
2486 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2487 ( 13 + ssl->in_msglen + 8 ) / 64;
2488 break;
2489#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002490#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002491 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002492 /* 16 bytes of message size, 128-byte compression blocks */
2493 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2494 ( 13 + ssl->in_msglen + 16 ) / 128;
2495 break;
2496#endif
2497 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002499 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2500 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002501
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002502 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2505 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2506 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2507 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002508 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002509 /* Make sure we access everything even when padlen > 0. This
2510 * makes the synchronisation requirements for just-in-time
2511 * Prime+Probe attacks much tighter and hopefully impractical. */
2512 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002513 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002514
2515 /* Call mbedtls_md_process at least once due to cache attacks
2516 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002517 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002521
2522 /* Make sure we access all the memory that could contain the MAC,
2523 * before we check it in the next code block. This makes the
2524 * synchronisation requirements for just-in-time Prime+Probe
2525 * attacks much tighter and hopefully impractical. */
2526 ssl_read_memory( ssl->in_msg + min_len,
2527 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002528 }
2529 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2531 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2534 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002535 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002536
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002537#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002538 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2539 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2540 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002541#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
Hanno Becker992b6872017-11-09 18:57:39 +00002543 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2544 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546#if defined(MBEDTLS_SSL_DEBUG_ALL)
2547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002548#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002549 correct = 0;
2550 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002551 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002552 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002553
2554 /*
2555 * Finally check the correct flag
2556 */
2557 if( correct == 0 )
2558 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002559#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002560
2561 /* Make extra sure authentication was performed, exactly once */
2562 if( auth_done != 1 )
2563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2565 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002566 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002567
2568 if( ssl->in_msglen == 0 )
2569 {
Angus Gratton34817922018-06-19 15:58:22 +10002570#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2571 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2572 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2573 {
2574 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2576 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2577 }
2578#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2579
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 ssl->nb_zero++;
2581
2582 /*
2583 * Three or more empty messages may be a DoS attack
2584 * (excessive CPU consumption).
2585 */
2586 if( ssl->nb_zero > 3 )
2587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002589 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002591 }
2592 }
2593 else
2594 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002597 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002598 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002599 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002600 }
2601 else
2602#endif
2603 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002604 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002605 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2606 if( ++ssl->in_ctr[i - 1] != 0 )
2607 break;
2608
2609 /* The loop goes to its end iff the counter is wrapping */
2610 if( i == ssl_ep_len( ssl ) )
2611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2613 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002614 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002615 }
2616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002618
2619 return( 0 );
2620}
2621
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002622#undef MAC_NONE
2623#undef MAC_PLAINTEXT
2624#undef MAC_CIPHERTEXT
2625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002627/*
2628 * Compression/decompression functions
2629 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002631{
2632 int ret;
2633 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002634 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002635 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002636 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002639
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002640 if( len_pre == 0 )
2641 return( 0 );
2642
Paul Bakker2770fbd2012-07-03 13:30:23 +00002643 memcpy( msg_pre, ssl->out_msg, len_pre );
2644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002646 ssl->out_msglen ) );
2647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002649 ssl->out_msg, ssl->out_msglen );
2650
Paul Bakker48916f92012-09-16 19:57:18 +00002651 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2652 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2653 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002654 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002655
Paul Bakker48916f92012-09-16 19:57:18 +00002656 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002657 if( ret != Z_OK )
2658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2660 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002661 }
2662
Angus Grattond8213d02016-05-25 20:56:48 +10002663 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002664 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002667 ssl->out_msglen ) );
2668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002670 ssl->out_msg, ssl->out_msglen );
2671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002673
2674 return( 0 );
2675}
2676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002678{
2679 int ret;
2680 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002681 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002682 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002683 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002686
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002687 if( len_pre == 0 )
2688 return( 0 );
2689
Paul Bakker2770fbd2012-07-03 13:30:23 +00002690 memcpy( msg_pre, ssl->in_msg, len_pre );
2691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002693 ssl->in_msglen ) );
2694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002696 ssl->in_msg, ssl->in_msglen );
2697
Paul Bakker48916f92012-09-16 19:57:18 +00002698 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2699 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2700 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002701 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002702 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002703
Paul Bakker48916f92012-09-16 19:57:18 +00002704 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002705 if( ret != Z_OK )
2706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2708 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002709 }
2710
Angus Grattond8213d02016-05-25 20:56:48 +10002711 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002712 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002715 ssl->in_msglen ) );
2716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002718 ssl->in_msg, ssl->in_msglen );
2719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002721
2722 return( 0 );
2723}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2727static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729#if defined(MBEDTLS_SSL_PROTO_DTLS)
2730static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002731{
2732 /* If renegotiation is not enforced, retransmit until we would reach max
2733 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002734 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002735 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002736 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002737 unsigned char doublings = 1;
2738
2739 while( ratio != 0 )
2740 {
2741 ++doublings;
2742 ratio >>= 1;
2743 }
2744
2745 if( ++ssl->renego_records_seen > doublings )
2746 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002748 return( 0 );
2749 }
2750 }
2751
2752 return( ssl_write_hello_request( ssl ) );
2753}
2754#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002756
Paul Bakker5121ce52009-01-03 21:22:43 +00002757/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002758 * Fill the input message buffer by appending data to it.
2759 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002760 *
2761 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2762 * available (from this read and/or a previous one). Otherwise, an error code
2763 * is returned (possibly EOF or WANT_READ).
2764 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002765 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2766 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2767 * since we always read a whole datagram at once.
2768 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002769 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002770 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002771 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002773{
Paul Bakker23986e52011-04-24 08:57:21 +00002774 int ret;
2775 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002778
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002779 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002782 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002784 }
2785
Angus Grattond8213d02016-05-25 20:56:48 +10002786 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2789 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002790 }
2791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002793 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002794 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002795 uint32_t timeout;
2796
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002797 /* Just to be sure */
2798 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2799 {
2800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2801 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2803 }
2804
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002805 /*
2806 * The point is, we need to always read a full datagram at once, so we
2807 * sometimes read more then requested, and handle the additional data.
2808 * It could be the rest of the current record (while fetching the
2809 * header) and/or some other records in the same datagram.
2810 */
2811
2812 /*
2813 * Move to the next record in the already read datagram if applicable
2814 */
2815 if( ssl->next_record_offset != 0 )
2816 {
2817 if( ssl->in_left < ssl->next_record_offset )
2818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2820 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002821 }
2822
2823 ssl->in_left -= ssl->next_record_offset;
2824
2825 if( ssl->in_left != 0 )
2826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002828 ssl->next_record_offset ) );
2829 memmove( ssl->in_hdr,
2830 ssl->in_hdr + ssl->next_record_offset,
2831 ssl->in_left );
2832 }
2833
2834 ssl->next_record_offset = 0;
2835 }
2836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002838 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002839
2840 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002841 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002842 */
2843 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002846 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002847 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002848
2849 /*
2850 * A record can't be split accross datagrams. If we need to read but
2851 * are not at the beginning of a new record, the caller did something
2852 * wrong.
2853 */
2854 if( ssl->in_left != 0 )
2855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2857 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002858 }
2859
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002860 /*
2861 * Don't even try to read if time's out already.
2862 * This avoids by-passing the timer when repeatedly receiving messages
2863 * that will end up being dropped.
2864 */
2865 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002866 {
2867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002868 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002869 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002870 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002871 {
Angus Grattond8213d02016-05-25 20:56:48 +10002872 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002875 timeout = ssl->handshake->retransmit_timeout;
2876 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002877 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002880
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002881 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002882 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2883 timeout );
2884 else
2885 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002888
2889 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002891 }
2892
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002893 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002896 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002899 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002900 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002903 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002904 }
2905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002909 return( ret );
2910 }
2911
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002912 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002913 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002915 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002917 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002918 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002921 return( ret );
2922 }
2923
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002924 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002925 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002927 }
2928
Paul Bakker5121ce52009-01-03 21:22:43 +00002929 if( ret < 0 )
2930 return( ret );
2931
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002932 ssl->in_left = ret;
2933 }
2934 else
2935#endif
2936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002938 ssl->in_left, nb_want ) );
2939
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002940 while( ssl->in_left < nb_want )
2941 {
2942 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002943
2944 if( ssl_check_timer( ssl ) != 0 )
2945 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2946 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002947 {
2948 if( ssl->f_recv_timeout != NULL )
2949 {
2950 ret = ssl->f_recv_timeout( ssl->p_bio,
2951 ssl->in_hdr + ssl->in_left, len,
2952 ssl->conf->read_timeout );
2953 }
2954 else
2955 {
2956 ret = ssl->f_recv( ssl->p_bio,
2957 ssl->in_hdr + ssl->in_left, len );
2958 }
2959 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002962 ssl->in_left, nb_want ) );
2963 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002964
2965 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002967
2968 if( ret < 0 )
2969 return( ret );
2970
mohammad160352aecb92018-03-28 23:41:40 -07002971 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002972 {
Darryl Green11999bb2018-03-13 15:22:58 +00002973 MBEDTLS_SSL_DEBUG_MSG( 1,
2974 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002975 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002976 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2977 }
2978
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002979 ssl->in_left += ret;
2980 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002981 }
2982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002984
2985 return( 0 );
2986}
2987
2988/*
2989 * Flush any data not yet written
2990 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002992{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002993 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002994 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002997
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002998 if( ssl->f_send == NULL )
2999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003001 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003003 }
3004
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003005 /* Avoid incrementing counter if data is flushed */
3006 if( ssl->out_left == 0 )
3007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003009 return( 0 );
3010 }
3011
Paul Bakker5121ce52009-01-03 21:22:43 +00003012 while( ssl->out_left > 0 )
3013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3015 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003016
Hanno Becker2b1e3542018-08-06 11:19:13 +01003017 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003018 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003020 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003021
3022 if( ret <= 0 )
3023 return( ret );
3024
mohammad160352aecb92018-03-28 23:41:40 -07003025 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003026 {
Darryl Green11999bb2018-03-13 15:22:58 +00003027 MBEDTLS_SSL_DEBUG_MSG( 1,
3028 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003029 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003030 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3031 }
3032
Paul Bakker5121ce52009-01-03 21:22:43 +00003033 ssl->out_left -= ret;
3034 }
3035
Hanno Becker2b1e3542018-08-06 11:19:13 +01003036#if defined(MBEDTLS_SSL_PROTO_DTLS)
3037 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003038 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003039 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003040 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003041 else
3042#endif
3043 {
3044 ssl->out_hdr = ssl->out_buf + 8;
3045 }
3046 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003049
3050 return( 0 );
3051}
3052
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003053/*
3054 * Functions to handle the DTLS retransmission state machine
3055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003057/*
3058 * Append current handshake message to current outgoing flight
3059 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003061{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3064 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3065 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003066
3067 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003068 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003069 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003072 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003073 }
3074
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003075 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003076 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003079 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003080 }
3081
3082 /* Copy current handshake message with headers */
3083 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3084 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003085 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003086 msg->next = NULL;
3087
3088 /* Append to the current flight */
3089 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003090 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003091 else
3092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003094 while( cur->next != NULL )
3095 cur = cur->next;
3096 cur->next = msg;
3097 }
3098
Hanno Becker3b235902018-08-06 09:54:53 +01003099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003100 return( 0 );
3101}
3102
3103/*
3104 * Free the current flight of handshake messages
3105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003107{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003108 mbedtls_ssl_flight_item *cur = flight;
3109 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003110
3111 while( cur != NULL )
3112 {
3113 next = cur->next;
3114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003115 mbedtls_free( cur->p );
3116 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003117
3118 cur = next;
3119 }
3120}
3121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3123static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003124#endif
3125
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003126/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003127 * Swap transform_out and out_ctr with the alternative ones
3128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003130{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003131 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003132 unsigned char tmp_out_ctr[8];
3133
3134 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003137 return;
3138 }
3139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003141
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003142 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003143 tmp_transform = ssl->transform_out;
3144 ssl->transform_out = ssl->handshake->alt_transform_out;
3145 ssl->handshake->alt_transform_out = tmp_transform;
3146
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003147 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003148 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3149 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003150 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003151
3152 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003153 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3156 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3161 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003162 }
3163 }
3164#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003165}
3166
3167/*
3168 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003169 */
3170int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3171{
3172 int ret = 0;
3173
3174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3175
3176 ret = mbedtls_ssl_flight_transmit( ssl );
3177
3178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3179
3180 return( ret );
3181}
3182
3183/*
3184 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003185 *
3186 * Need to remember the current message in case flush_output returns
3187 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003188 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003189 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003190int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003191{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003192 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003196 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003198
3199 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003200 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003201 ssl_swap_epochs( ssl );
3202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003203 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003204 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003205
3206 while( ssl->handshake->cur_msg != NULL )
3207 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003208 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003209 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003210
Hanno Beckere1dcb032018-08-17 16:47:58 +01003211 int const is_finished =
3212 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3213 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3214
Hanno Becker04da1892018-08-14 13:22:10 +01003215 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3216 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3217
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003218 /* Swap epochs before sending Finished: we can't do it after
3219 * sending ChangeCipherSpec, in case write returns WANT_READ.
3220 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003221 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003222 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003223 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003224 ssl_swap_epochs( ssl );
3225 }
3226
Hanno Becker67bc7c32018-08-06 11:33:50 +01003227 ret = ssl_get_remaining_payload_in_datagram( ssl );
3228 if( ret < 0 )
3229 return( ret );
3230 max_frag_len = (size_t) ret;
3231
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003232 /* CCS is copied as is, while HS messages may need fragmentation */
3233 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3234 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003235 if( max_frag_len == 0 )
3236 {
3237 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3238 return( ret );
3239
3240 continue;
3241 }
3242
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003243 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003244 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003245 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003246
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003247 /* Update position inside current message */
3248 ssl->handshake->cur_msg_p += cur->len;
3249 }
3250 else
3251 {
3252 const unsigned char * const p = ssl->handshake->cur_msg_p;
3253 const size_t hs_len = cur->len - 12;
3254 const size_t frag_off = p - ( cur->p + 12 );
3255 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003256 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003257
Hanno Beckere1dcb032018-08-17 16:47:58 +01003258 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003259 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003260 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003261 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003262
Hanno Becker67bc7c32018-08-06 11:33:50 +01003263 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3264 return( ret );
3265
3266 continue;
3267 }
3268 max_hs_frag_len = max_frag_len - 12;
3269
3270 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3271 max_hs_frag_len : rem_len;
3272
3273 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003274 {
3275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003276 (unsigned) cur_hs_frag_len,
3277 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003278 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003279
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003280 /* Messages are stored with handshake headers as if not fragmented,
3281 * copy beginning of headers then fill fragmentation fields.
3282 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3283 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003284
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003285 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3286 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3287 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3288
Hanno Becker67bc7c32018-08-06 11:33:50 +01003289 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3290 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3291 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003292
3293 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3294
Hanno Becker3f7b9732018-08-28 09:53:25 +01003295 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003296 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3297 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003298 ssl->out_msgtype = cur->type;
3299
3300 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003301 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003302 }
3303
3304 /* If done with the current message move to the next one if any */
3305 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3306 {
3307 if( cur->next != NULL )
3308 {
3309 ssl->handshake->cur_msg = cur->next;
3310 ssl->handshake->cur_msg_p = cur->next->p + 12;
3311 }
3312 else
3313 {
3314 ssl->handshake->cur_msg = NULL;
3315 ssl->handshake->cur_msg_p = NULL;
3316 }
3317 }
3318
3319 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003320 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003322 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003323 return( ret );
3324 }
3325 }
3326
Hanno Becker67bc7c32018-08-06 11:33:50 +01003327 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3328 return( ret );
3329
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003330 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3332 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003333 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003336 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3337 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003338
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003340
3341 return( 0 );
3342}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003343
3344/*
3345 * To be called when the last message of an incoming flight is received.
3346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003348{
3349 /* We won't need to resend that one any more */
3350 ssl_flight_free( ssl->handshake->flight );
3351 ssl->handshake->flight = NULL;
3352 ssl->handshake->cur_msg = NULL;
3353
3354 /* The next incoming flight will start with this msg_seq */
3355 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3356
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003357 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003358 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003359
Hanno Becker0271f962018-08-16 13:23:47 +01003360 /* Clear future message buffering structure. */
3361 ssl_buffering_free( ssl );
3362
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003363 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003364 ssl_set_timer( ssl, 0 );
3365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003366 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3367 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003370 }
3371 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003373}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003374
3375/*
3376 * To be called when the last message of an outgoing flight is send.
3377 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003379{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003380 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003381 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003383 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3384 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003387 }
3388 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003389 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003390}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003392
Paul Bakker5121ce52009-01-03 21:22:43 +00003393/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003394 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003395 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003396
3397/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003398 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003399 *
3400 * - fill in handshake headers
3401 * - update handshake checksum
3402 * - DTLS: save message for resending
3403 * - then pass to the record layer
3404 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003405 * DTLS: except for HelloRequest, messages are only queued, and will only be
3406 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003407 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003408 * Inputs:
3409 * - ssl->out_msglen: 4 + actual handshake message len
3410 * (4 is the size of handshake headers for TLS)
3411 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3412 * - ssl->out_msg + 4: the handshake message body
3413 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003414 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003415 * - ssl->out_msglen: the length of the record contents
3416 * (including handshake headers but excluding record headers)
3417 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003418 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003419int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003420{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003421 int ret;
3422 const size_t hs_len = ssl->out_msglen - 4;
3423 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003424
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3426
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003427 /*
3428 * Sanity checks
3429 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003430 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003431 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3432 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003433 /* In SSLv3, the client might send a NoCertificate alert. */
3434#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3435 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3436 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3437 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3438#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3439 {
3440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3441 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3442 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003443 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003444
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003445 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3446 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3447 ssl->handshake == NULL )
3448 {
3449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3450 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3451 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003454 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003455 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003456 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003457 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3459 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003460 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003461#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003462
Hanno Beckerb50a2532018-08-06 11:52:54 +01003463 /* Double-check that we did not exceed the bounds
3464 * of the outgoing record buffer.
3465 * This should never fail as the various message
3466 * writing functions must obey the bounds of the
3467 * outgoing record buffer, but better be safe.
3468 *
3469 * Note: We deliberately do not check for the MTU or MFL here.
3470 */
3471 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3472 {
3473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3474 "size %u, maximum %u",
3475 (unsigned) ssl->out_msglen,
3476 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3477 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3478 }
3479
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003480 /*
3481 * Fill handshake headers
3482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003484 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003485 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3486 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3487 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003488
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003489 /*
3490 * DTLS has additional fields in the Handshake layer,
3491 * between the length field and the actual payload:
3492 * uint16 message_seq;
3493 * uint24 fragment_offset;
3494 * uint24 fragment_length;
3495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003496#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003497 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003498 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003499 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003500 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003501 {
3502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3503 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003504 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003505 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003506 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3507 }
3508
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003509 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003510 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003511
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003512 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003513 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003514 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003515 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3516 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3517 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003518 }
3519 else
3520 {
3521 ssl->out_msg[4] = 0;
3522 ssl->out_msg[5] = 0;
3523 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003524
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003525 /* Handshake hashes are computed without fragmentation,
3526 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003527 memset( ssl->out_msg + 6, 0x00, 3 );
3528 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003529 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003531
Hanno Becker0207e532018-08-28 10:28:28 +01003532 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003533 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3534 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003535 }
3536
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003537 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003539 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Becker081bd812018-08-22 16:07:59 +01003540 ( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
3541 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003542 {
3543 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003546 return( ret );
3547 }
3548 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003549 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003550#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003551 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003552 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003553 {
3554 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3555 return( ret );
3556 }
3557 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003558
3559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3560
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003561 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003562}
3563
3564/*
3565 * Record layer functions
3566 */
3567
3568/*
3569 * Write current record.
3570 *
3571 * Uses:
3572 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3573 * - ssl->out_msglen: length of the record content (excl headers)
3574 * - ssl->out_msg: record content
3575 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003576int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003577{
3578 int ret, done = 0;
3579 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003580 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003581
3582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003584#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003585 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003586 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003587 {
3588 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003590 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003591 return( ret );
3592 }
3593
3594 len = ssl->out_msglen;
3595 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003598#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3599 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603 ret = mbedtls_ssl_hw_record_write( ssl );
3604 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003606 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3607 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003608 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003609
3610 if( ret == 0 )
3611 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003612 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003614 if( !done )
3615 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003616 unsigned i;
3617 size_t protected_record_size;
3618
Paul Bakker05ef8352012-05-08 09:17:57 +00003619 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003621 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003622
Hanno Becker19859472018-08-06 09:40:20 +01003623 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003624 ssl->out_len[0] = (unsigned char)( len >> 8 );
3625 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003626
Paul Bakker48916f92012-09-16 19:57:18 +00003627 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003628 {
3629 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003631 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003632 return( ret );
3633 }
3634
3635 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003636 ssl->out_len[0] = (unsigned char)( len >> 8 );
3637 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003638 }
3639
Hanno Becker2b1e3542018-08-06 11:19:13 +01003640 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3641
3642#if defined(MBEDTLS_SSL_PROTO_DTLS)
3643 /* In case of DTLS, double-check that we don't exceed
3644 * the remaining space in the datagram. */
3645 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3646 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003647 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003648 if( ret < 0 )
3649 return( ret );
3650
3651 if( protected_record_size > (size_t) ret )
3652 {
3653 /* Should never happen */
3654 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3655 }
3656 }
3657#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003660 "version = [%d:%d], msglen = %d",
3661 ssl->out_hdr[0], ssl->out_hdr[1],
3662 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003665 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003666
3667 ssl->out_left += protected_record_size;
3668 ssl->out_hdr += protected_record_size;
3669 ssl_update_out_pointers( ssl, ssl->transform_out );
3670
Hanno Becker04484622018-08-06 09:49:38 +01003671 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3672 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3673 break;
3674
3675 /* The loop goes to its end iff the counter is wrapping */
3676 if( i == ssl_ep_len( ssl ) )
3677 {
3678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3679 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3680 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003681 }
3682
Hanno Becker67bc7c32018-08-06 11:33:50 +01003683#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003684 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3685 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003686 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003687 size_t remaining;
3688 ret = ssl_get_remaining_payload_in_datagram( ssl );
3689 if( ret < 0 )
3690 {
3691 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3692 ret );
3693 return( ret );
3694 }
3695
3696 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003697 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003698 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003699 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003700 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003701 else
3702 {
Hanno Becker513815a2018-08-20 11:56:09 +01003703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003704 }
3705 }
3706#endif /* MBEDTLS_SSL_PROTO_DTLS */
3707
3708 if( ( flush == SSL_FORCE_FLUSH ) &&
3709 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003712 return( ret );
3713 }
3714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003716
3717 return( 0 );
3718}
3719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003721
3722static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3723{
3724 if( ssl->in_msglen < ssl->in_hslen ||
3725 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3726 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3727 {
3728 return( 1 );
3729 }
3730 return( 0 );
3731}
Hanno Becker44650b72018-08-16 12:51:11 +01003732
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003733static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003734{
3735 return( ( ssl->in_msg[9] << 16 ) |
3736 ( ssl->in_msg[10] << 8 ) |
3737 ssl->in_msg[11] );
3738}
3739
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003740static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003741{
3742 return( ( ssl->in_msg[6] << 16 ) |
3743 ( ssl->in_msg[7] << 8 ) |
3744 ssl->in_msg[8] );
3745}
3746
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003747static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003748{
3749 uint32_t msg_len, frag_off, frag_len;
3750
3751 msg_len = ssl_get_hs_total_len( ssl );
3752 frag_off = ssl_get_hs_frag_off( ssl );
3753 frag_len = ssl_get_hs_frag_len( ssl );
3754
3755 if( frag_off > msg_len )
3756 return( -1 );
3757
3758 if( frag_len > msg_len - frag_off )
3759 return( -1 );
3760
3761 if( frag_len + 12 > ssl->in_msglen )
3762 return( -1 );
3763
3764 return( 0 );
3765}
3766
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003767/*
3768 * Mark bits in bitmask (used for DTLS HS reassembly)
3769 */
3770static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3771{
3772 unsigned int start_bits, end_bits;
3773
3774 start_bits = 8 - ( offset % 8 );
3775 if( start_bits != 8 )
3776 {
3777 size_t first_byte_idx = offset / 8;
3778
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003779 /* Special case */
3780 if( len <= start_bits )
3781 {
3782 for( ; len != 0; len-- )
3783 mask[first_byte_idx] |= 1 << ( start_bits - len );
3784
3785 /* Avoid potential issues with offset or len becoming invalid */
3786 return;
3787 }
3788
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003789 offset += start_bits; /* Now offset % 8 == 0 */
3790 len -= start_bits;
3791
3792 for( ; start_bits != 0; start_bits-- )
3793 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3794 }
3795
3796 end_bits = len % 8;
3797 if( end_bits != 0 )
3798 {
3799 size_t last_byte_idx = ( offset + len ) / 8;
3800
3801 len -= end_bits; /* Now len % 8 == 0 */
3802
3803 for( ; end_bits != 0; end_bits-- )
3804 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3805 }
3806
3807 memset( mask + offset / 8, 0xFF, len / 8 );
3808}
3809
3810/*
3811 * Check that bitmask is full
3812 */
3813static int ssl_bitmask_check( unsigned char *mask, size_t len )
3814{
3815 size_t i;
3816
3817 for( i = 0; i < len / 8; i++ )
3818 if( mask[i] != 0xFF )
3819 return( -1 );
3820
3821 for( i = 0; i < len % 8; i++ )
3822 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3823 return( -1 );
3824
3825 return( 0 );
3826}
3827
Hanno Becker56e205e2018-08-16 09:06:12 +01003828/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003829static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003830 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003831{
Hanno Becker56e205e2018-08-16 09:06:12 +01003832 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003833
Hanno Becker56e205e2018-08-16 09:06:12 +01003834 alloc_len = 12; /* Handshake header */
3835 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003836
Hanno Beckerd07df862018-08-16 09:14:58 +01003837 if( add_bitmap )
3838 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003839
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003840 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003841}
Hanno Becker56e205e2018-08-16 09:06:12 +01003842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003844
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003845static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003846{
3847 return( ( ssl->in_msg[1] << 16 ) |
3848 ( ssl->in_msg[2] << 8 ) |
3849 ssl->in_msg[3] );
3850}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003851
Simon Butcher99000142016-10-13 17:21:01 +01003852int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003853{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003857 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003859 }
3860
Hanno Becker12555c62018-08-16 12:47:53 +01003861 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003864 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003865 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003867#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003868 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003869 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003870 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003871 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003872
Hanno Becker44650b72018-08-16 12:51:11 +01003873 if( ssl_check_hs_header( ssl ) != 0 )
3874 {
3875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3876 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3877 }
3878
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003879 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003880 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3881 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3882 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3883 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003884 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003885 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3886 {
3887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3888 recv_msg_seq,
3889 ssl->handshake->in_msg_seq ) );
3890 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3891 }
3892
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003893 /* Retransmit only on last message from previous flight, to avoid
3894 * too many retransmissions.
3895 * Besides, No sane server ever retransmits HelloVerifyRequest */
3896 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003897 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003900 "message_seq = %d, start_of_flight = %d",
3901 recv_msg_seq,
3902 ssl->handshake->in_flight_start_seq ) );
3903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003904 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003906 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003907 return( ret );
3908 }
3909 }
3910 else
3911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003913 "message_seq = %d, expected = %d",
3914 recv_msg_seq,
3915 ssl->handshake->in_msg_seq ) );
3916 }
3917
Hanno Becker90333da2017-10-10 11:27:13 +01003918 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003919 }
3920 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003921
Hanno Becker6d97ef52018-08-16 13:09:04 +01003922 /* Message reassembly is handled alongside buffering of future
3923 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003924 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003925 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003926 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003929 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003930 }
3931 }
3932 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003933#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003934 /* With TLS we don't handle fragmentation (for now) */
3935 if( ssl->in_msglen < ssl->in_hslen )
3936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3938 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003939 }
3940
Simon Butcher99000142016-10-13 17:21:01 +01003941 return( 0 );
3942}
3943
3944void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3945{
Hanno Becker0271f962018-08-16 13:23:47 +01003946 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003947
Hanno Becker0271f962018-08-16 13:23:47 +01003948 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003949 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003950 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003951 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003952
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003953 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003954#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003955 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003956 ssl->handshake != NULL )
3957 {
Hanno Becker0271f962018-08-16 13:23:47 +01003958 unsigned offset;
3959 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003960
Hanno Becker0271f962018-08-16 13:23:47 +01003961 /* Increment handshake sequence number */
3962 hs->in_msg_seq++;
3963
3964 /*
3965 * Clear up handshake buffering and reassembly structure.
3966 */
3967
3968 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003969 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003970
3971 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003972 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3973 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003974 offset++, hs_buf++ )
3975 {
3976 *hs_buf = *(hs_buf + 1);
3977 }
3978
3979 /* Create a fresh last entry */
3980 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003981 }
3982#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003983}
3984
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003985/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003986 * DTLS anti-replay: RFC 6347 4.1.2.6
3987 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003988 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3989 * Bit n is set iff record number in_window_top - n has been seen.
3990 *
3991 * Usually, in_window_top is the last record number seen and the lsb of
3992 * in_window is set. The only exception is the initial state (record number 0
3993 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003994 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003995#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3996static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003997{
3998 ssl->in_window_top = 0;
3999 ssl->in_window = 0;
4000}
4001
4002static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4003{
4004 return( ( (uint64_t) buf[0] << 40 ) |
4005 ( (uint64_t) buf[1] << 32 ) |
4006 ( (uint64_t) buf[2] << 24 ) |
4007 ( (uint64_t) buf[3] << 16 ) |
4008 ( (uint64_t) buf[4] << 8 ) |
4009 ( (uint64_t) buf[5] ) );
4010}
4011
4012/*
4013 * Return 0 if sequence number is acceptable, -1 otherwise
4014 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004015int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004016{
4017 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4018 uint64_t bit;
4019
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004020 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004021 return( 0 );
4022
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004023 if( rec_seqnum > ssl->in_window_top )
4024 return( 0 );
4025
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004026 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004027
4028 if( bit >= 64 )
4029 return( -1 );
4030
4031 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4032 return( -1 );
4033
4034 return( 0 );
4035}
4036
4037/*
4038 * Update replay window on new validated record
4039 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004040void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004041{
4042 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4043
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004044 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004045 return;
4046
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004047 if( rec_seqnum > ssl->in_window_top )
4048 {
4049 /* Update window_top and the contents of the window */
4050 uint64_t shift = rec_seqnum - ssl->in_window_top;
4051
4052 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004053 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004054 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004055 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004056 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004057 ssl->in_window |= 1;
4058 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004059
4060 ssl->in_window_top = rec_seqnum;
4061 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004062 else
4063 {
4064 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004065 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004066
4067 if( bit < 64 ) /* Always true, but be extra sure */
4068 ssl->in_window |= (uint64_t) 1 << bit;
4069 }
4070}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004072
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004073#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004074/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004075static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4076
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004077/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004078 * Without any SSL context, check if a datagram looks like a ClientHello with
4079 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004080 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004081 *
4082 * - if cookie is valid, return 0
4083 * - if ClientHello looks superficially valid but cookie is not,
4084 * fill obuf and set olen, then
4085 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4086 * - otherwise return a specific error code
4087 */
4088static int ssl_check_dtls_clihlo_cookie(
4089 mbedtls_ssl_cookie_write_t *f_cookie_write,
4090 mbedtls_ssl_cookie_check_t *f_cookie_check,
4091 void *p_cookie,
4092 const unsigned char *cli_id, size_t cli_id_len,
4093 const unsigned char *in, size_t in_len,
4094 unsigned char *obuf, size_t buf_len, size_t *olen )
4095{
4096 size_t sid_len, cookie_len;
4097 unsigned char *p;
4098
4099 if( f_cookie_write == NULL || f_cookie_check == NULL )
4100 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4101
4102 /*
4103 * Structure of ClientHello with record and handshake headers,
4104 * and expected values. We don't need to check a lot, more checks will be
4105 * done when actually parsing the ClientHello - skipping those checks
4106 * avoids code duplication and does not make cookie forging any easier.
4107 *
4108 * 0-0 ContentType type; copied, must be handshake
4109 * 1-2 ProtocolVersion version; copied
4110 * 3-4 uint16 epoch; copied, must be 0
4111 * 5-10 uint48 sequence_number; copied
4112 * 11-12 uint16 length; (ignored)
4113 *
4114 * 13-13 HandshakeType msg_type; (ignored)
4115 * 14-16 uint24 length; (ignored)
4116 * 17-18 uint16 message_seq; copied
4117 * 19-21 uint24 fragment_offset; copied, must be 0
4118 * 22-24 uint24 fragment_length; (ignored)
4119 *
4120 * 25-26 ProtocolVersion client_version; (ignored)
4121 * 27-58 Random random; (ignored)
4122 * 59-xx SessionID session_id; 1 byte len + sid_len content
4123 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4124 * ...
4125 *
4126 * Minimum length is 61 bytes.
4127 */
4128 if( in_len < 61 ||
4129 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4130 in[3] != 0 || in[4] != 0 ||
4131 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4132 {
4133 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4134 }
4135
4136 sid_len = in[59];
4137 if( sid_len > in_len - 61 )
4138 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4139
4140 cookie_len = in[60 + sid_len];
4141 if( cookie_len > in_len - 60 )
4142 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4143
4144 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4145 cli_id, cli_id_len ) == 0 )
4146 {
4147 /* Valid cookie */
4148 return( 0 );
4149 }
4150
4151 /*
4152 * If we get here, we've got an invalid cookie, let's prepare HVR.
4153 *
4154 * 0-0 ContentType type; copied
4155 * 1-2 ProtocolVersion version; copied
4156 * 3-4 uint16 epoch; copied
4157 * 5-10 uint48 sequence_number; copied
4158 * 11-12 uint16 length; olen - 13
4159 *
4160 * 13-13 HandshakeType msg_type; hello_verify_request
4161 * 14-16 uint24 length; olen - 25
4162 * 17-18 uint16 message_seq; copied
4163 * 19-21 uint24 fragment_offset; copied
4164 * 22-24 uint24 fragment_length; olen - 25
4165 *
4166 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4167 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4168 *
4169 * Minimum length is 28.
4170 */
4171 if( buf_len < 28 )
4172 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4173
4174 /* Copy most fields and adapt others */
4175 memcpy( obuf, in, 25 );
4176 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4177 obuf[25] = 0xfe;
4178 obuf[26] = 0xff;
4179
4180 /* Generate and write actual cookie */
4181 p = obuf + 28;
4182 if( f_cookie_write( p_cookie,
4183 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4184 {
4185 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4186 }
4187
4188 *olen = p - obuf;
4189
4190 /* Go back and fill length fields */
4191 obuf[27] = (unsigned char)( *olen - 28 );
4192
4193 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4194 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4195 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4196
4197 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4198 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4199
4200 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4201}
4202
4203/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004204 * Handle possible client reconnect with the same UDP quadruplet
4205 * (RFC 6347 Section 4.2.8).
4206 *
4207 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4208 * that looks like a ClientHello.
4209 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004210 * - if the input looks like a ClientHello without cookies,
4211 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004212 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004213 * - if the input looks like a ClientHello with a valid cookie,
4214 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004215 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004216 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004217 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004218 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004219 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4220 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004221 */
4222static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4223{
4224 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004225 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004226
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004227 ret = ssl_check_dtls_clihlo_cookie(
4228 ssl->conf->f_cookie_write,
4229 ssl->conf->f_cookie_check,
4230 ssl->conf->p_cookie,
4231 ssl->cli_id, ssl->cli_id_len,
4232 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004233 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004234
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004235 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4236
4237 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004238 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004239 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004240 * If the error is permanent we'll catch it later,
4241 * if it's not, then hopefully it'll work next time. */
4242 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4243
4244 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004245 }
4246
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004247 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004248 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004249 /* Got a valid cookie, partially reset context */
4250 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4251 {
4252 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4253 return( ret );
4254 }
4255
4256 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004257 }
4258
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004259 return( ret );
4260}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004261#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004262
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004263/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004264 * ContentType type;
4265 * ProtocolVersion version;
4266 * uint16 epoch; // DTLS only
4267 * uint48 sequence_number; // DTLS only
4268 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004269 *
4270 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004271 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004272 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4273 *
4274 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004275 * 1. proceed with the record if this function returns 0
4276 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4277 * 3. return CLIENT_RECONNECT if this function return that value
4278 * 4. drop the whole datagram if this function returns anything else.
4279 * Point 2 is needed when the peer is resending, and we have already received
4280 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004282static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004283{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004284 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004286 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 +02004287
Paul Bakker5121ce52009-01-03 21:22:43 +00004288 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004289 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004290 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004292 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004293 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004294 ssl->in_msgtype,
4295 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004296
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004297 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004298 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4299 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4300 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4301 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004304
4305#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004306 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4307 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004308 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4309#endif /* MBEDTLS_SSL_PROTO_DTLS */
4310 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4311 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004313 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004314 }
4315
4316 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004317 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4320 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004321 }
4322
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004323 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4326 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004327 }
4328
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004329 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004330 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004331 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4334 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004335 }
4336
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004337 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004338 * DTLS-related tests.
4339 * Check epoch before checking length constraint because
4340 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4341 * message gets duplicated before the corresponding Finished message,
4342 * the second ChangeCipherSpec should be discarded because it belongs
4343 * to an old epoch, but not because its length is shorter than
4344 * the minimum record length for packets using the new record transform.
4345 * Note that these two kinds of failures are handled differently,
4346 * as an unexpected record is silently skipped but an invalid
4347 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004348 */
4349#if defined(MBEDTLS_SSL_PROTO_DTLS)
4350 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4351 {
4352 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4353
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004354 /* Check epoch (and sequence number) with DTLS */
4355 if( rec_epoch != ssl->in_epoch )
4356 {
4357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4358 "expected %d, received %d",
4359 ssl->in_epoch, rec_epoch ) );
4360
4361#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4362 /*
4363 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4364 * access the first byte of record content (handshake type), as we
4365 * have an active transform (possibly iv_len != 0), so use the
4366 * fact that the record header len is 13 instead.
4367 */
4368 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4369 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4370 rec_epoch == 0 &&
4371 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4372 ssl->in_left > 13 &&
4373 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4374 {
4375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4376 "from the same port" ) );
4377 return( ssl_handle_possible_reconnect( ssl ) );
4378 }
4379 else
4380#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004381 {
4382 /* Consider buffering the record. */
4383 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4384 {
4385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4386 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4387 }
4388
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004389 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004390 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004391 }
4392
4393#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4394 /* Replay detection only works for the current epoch */
4395 if( rec_epoch == ssl->in_epoch &&
4396 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4397 {
4398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4399 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4400 }
4401#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004402
Hanno Becker52c6dc62017-05-26 16:07:36 +01004403 /* Drop unexpected ApplicationData records,
4404 * except at the beginning of renegotiations */
4405 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4406 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4407#if defined(MBEDTLS_SSL_RENEGOTIATION)
4408 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4409 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4410#endif
4411 )
4412 {
4413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4414 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4415 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004416 }
4417#endif /* MBEDTLS_SSL_PROTO_DTLS */
4418
Hanno Becker52c6dc62017-05-26 16:07:36 +01004419
4420 /* Check length against bounds of the current transform and version */
4421 if( ssl->transform_in == NULL )
4422 {
4423 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004424 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004425 {
4426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4427 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4428 }
4429 }
4430 else
4431 {
4432 if( ssl->in_msglen < ssl->transform_in->minlen )
4433 {
4434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4435 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4436 }
4437
4438#if defined(MBEDTLS_SSL_PROTO_SSL3)
4439 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004440 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004441 {
4442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4443 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4444 }
4445#endif
4446#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4447 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4448 /*
4449 * TLS encrypted messages can have up to 256 bytes of padding
4450 */
4451 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4452 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004453 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004454 {
4455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4456 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4457 }
4458#endif
4459 }
4460
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004461 return( 0 );
4462}
Paul Bakker5121ce52009-01-03 21:22:43 +00004463
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004464/*
4465 * If applicable, decrypt (and decompress) record content
4466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004467static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004468{
4469 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004471 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4472 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004474#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4475 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004479 ret = mbedtls_ssl_hw_record_read( ssl );
4480 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004482 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4483 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004484 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004485
4486 if( ret == 0 )
4487 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004488 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004489#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004490 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004491 {
4492 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004494 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004495 return( ret );
4496 }
4497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004498 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004499 ssl->in_msg, ssl->in_msglen );
4500
Angus Grattond8213d02016-05-25 20:56:48 +10004501 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4504 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004505 }
4506 }
4507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004508#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004509 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004510 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004511 {
4512 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004514 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004515 return( ret );
4516 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004517 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004518#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004520#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004521 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004523 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004524 }
4525#endif
4526
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004527 return( 0 );
4528}
4529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004530static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004531
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004532/*
4533 * Read a record.
4534 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004535 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4536 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4537 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004538 */
Hanno Becker1097b342018-08-15 14:09:41 +01004539
4540/* Helper functions for mbedtls_ssl_read_record(). */
4541static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004542static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4543static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004544
Hanno Becker327c93b2018-08-15 13:56:18 +01004545int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004546 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004547{
4548 int ret;
4549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004551
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004552 if( ssl->keep_current_message == 0 )
4553 {
4554 do {
Simon Butcher99000142016-10-13 17:21:01 +01004555
Hanno Becker26994592018-08-15 14:14:59 +01004556 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004557 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004558 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004559
Hanno Beckere74d5562018-08-15 14:26:08 +01004560 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004561 {
Hanno Becker40f50842018-08-15 14:48:01 +01004562#if defined(MBEDTLS_SSL_PROTO_DTLS)
4563 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004564
Hanno Becker40f50842018-08-15 14:48:01 +01004565 /* We only check for buffered messages if the
4566 * current datagram is fully consumed. */
4567 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004568 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004569 {
Hanno Becker40f50842018-08-15 14:48:01 +01004570 if( ssl_load_buffered_message( ssl ) == 0 )
4571 have_buffered = 1;
4572 }
4573
4574 if( have_buffered == 0 )
4575#endif /* MBEDTLS_SSL_PROTO_DTLS */
4576 {
4577 ret = ssl_get_next_record( ssl );
4578 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4579 continue;
4580
4581 if( ret != 0 )
4582 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004583 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004584 return( ret );
4585 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004586 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004587 }
4588
4589 ret = mbedtls_ssl_handle_message_type( ssl );
4590
Hanno Becker40f50842018-08-15 14:48:01 +01004591#if defined(MBEDTLS_SSL_PROTO_DTLS)
4592 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4593 {
4594 /* Buffer future message */
4595 ret = ssl_buffer_message( ssl );
4596 if( ret != 0 )
4597 return( ret );
4598
4599 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4600 }
4601#endif /* MBEDTLS_SSL_PROTO_DTLS */
4602
Hanno Becker90333da2017-10-10 11:27:13 +01004603 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4604 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004605
4606 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004607 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004608 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004609 return( ret );
4610 }
4611
Hanno Becker327c93b2018-08-15 13:56:18 +01004612 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004613 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004614 {
4615 mbedtls_ssl_update_handshake_status( ssl );
4616 }
Simon Butcher99000142016-10-13 17:21:01 +01004617 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004618 else
Simon Butcher99000142016-10-13 17:21:01 +01004619 {
Hanno Becker02f59072018-08-15 14:00:24 +01004620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004621 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004622 }
4623
4624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4625
4626 return( 0 );
4627}
4628
Hanno Becker40f50842018-08-15 14:48:01 +01004629#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004630static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004631{
Hanno Becker40f50842018-08-15 14:48:01 +01004632 if( ssl->in_left > ssl->next_record_offset )
4633 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004634
Hanno Becker40f50842018-08-15 14:48:01 +01004635 return( 0 );
4636}
4637
4638static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4639{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004640 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004641 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004642 int ret = 0;
4643
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004644 if( hs == NULL )
4645 return( -1 );
4646
Hanno Beckere00ae372018-08-20 09:39:42 +01004647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4648
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004649 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4650 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4651 {
4652 /* Check if we have seen a ChangeCipherSpec before.
4653 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004654 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004655 {
4656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4657 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004658 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004659 }
4660
Hanno Becker39b8bc92018-08-28 17:17:13 +01004661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004662 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4663 ssl->in_msglen = 1;
4664 ssl->in_msg[0] = 1;
4665
4666 /* As long as they are equal, the exact value doesn't matter. */
4667 ssl->in_left = 0;
4668 ssl->next_record_offset = 0;
4669
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004670 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004671 goto exit;
4672 }
Hanno Becker37f95322018-08-16 13:55:32 +01004673
Hanno Beckerb8f50142018-08-28 10:01:34 +01004674#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004675 /* Debug only */
4676 {
4677 unsigned offset;
4678 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4679 {
4680 hs_buf = &hs->buffering.hs[offset];
4681 if( hs_buf->is_valid == 1 )
4682 {
4683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4684 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004685 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004686 }
4687 }
4688 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004689#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004690
4691 /* Check if we have buffered and/or fully reassembled the
4692 * next handshake message. */
4693 hs_buf = &hs->buffering.hs[0];
4694 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4695 {
4696 /* Synthesize a record containing the buffered HS message. */
4697 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4698 ( hs_buf->data[2] << 8 ) |
4699 hs_buf->data[3];
4700
4701 /* Double-check that we haven't accidentally buffered
4702 * a message that doesn't fit into the input buffer. */
4703 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4704 {
4705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4706 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4707 }
4708
4709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4710 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4711 hs_buf->data, msg_len + 12 );
4712
4713 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4714 ssl->in_hslen = msg_len + 12;
4715 ssl->in_msglen = msg_len + 12;
4716 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4717
4718 ret = 0;
4719 goto exit;
4720 }
4721 else
4722 {
4723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4724 hs->in_msg_seq ) );
4725 }
4726
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004727 ret = -1;
4728
4729exit:
4730
4731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4732 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004733}
4734
Hanno Beckera02b0b42018-08-21 17:20:27 +01004735static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4736 size_t desired )
4737{
4738 int offset;
4739 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4741 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004742
Hanno Becker01315ea2018-08-21 17:22:17 +01004743 /* Get rid of future records epoch first, if such exist. */
4744 ssl_free_buffered_record( ssl );
4745
4746 /* Check if we have enough space available now. */
4747 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4748 hs->buffering.total_bytes_buffered ) )
4749 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004751 return( 0 );
4752 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004753
Hanno Becker4f432ad2018-08-28 10:02:32 +01004754 /* We don't have enough space to buffer the next expected handshake
4755 * message. Remove buffers used for future messages to gain space,
4756 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004757 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4758 offset >= 0; offset-- )
4759 {
4760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4761 offset ) );
4762
Hanno Beckerb309b922018-08-23 13:18:05 +01004763 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004764
4765 /* Check if we have enough space available now. */
4766 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4767 hs->buffering.total_bytes_buffered ) )
4768 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004770 return( 0 );
4771 }
4772 }
4773
4774 return( -1 );
4775}
4776
Hanno Becker40f50842018-08-15 14:48:01 +01004777static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4778{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004779 int ret = 0;
4780 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4781
4782 if( hs == NULL )
4783 return( 0 );
4784
4785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4786
4787 switch( ssl->in_msgtype )
4788 {
4789 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004791
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004792 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004793 break;
4794
4795 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004796 {
4797 unsigned recv_msg_seq_offset;
4798 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4799 mbedtls_ssl_hs_buffer *hs_buf;
4800 size_t msg_len = ssl->in_hslen - 12;
4801
4802 /* We should never receive an old handshake
4803 * message - double-check nonetheless. */
4804 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4805 {
4806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4807 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4808 }
4809
4810 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4811 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4812 {
4813 /* Silently ignore -- message too far in the future */
4814 MBEDTLS_SSL_DEBUG_MSG( 2,
4815 ( "Ignore future HS message with sequence number %u, "
4816 "buffering window %u - %u",
4817 recv_msg_seq, ssl->handshake->in_msg_seq,
4818 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4819
4820 goto exit;
4821 }
4822
4823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4824 recv_msg_seq, recv_msg_seq_offset ) );
4825
4826 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4827
4828 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004829 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004830 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004831 size_t reassembly_buf_sz;
4832
Hanno Becker37f95322018-08-16 13:55:32 +01004833 hs_buf->is_fragmented =
4834 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4835
4836 /* We copy the message back into the input buffer
4837 * after reassembly, so check that it's not too large.
4838 * This is an implementation-specific limitation
4839 * and not one from the standard, hence it is not
4840 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004841 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004842 {
4843 /* Ignore message */
4844 goto exit;
4845 }
4846
Hanno Beckere0b150f2018-08-21 15:51:03 +01004847 /* Check if we have enough space to buffer the message. */
4848 if( hs->buffering.total_bytes_buffered >
4849 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4850 {
4851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4852 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4853 }
4854
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004855 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4856 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004857
4858 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4859 hs->buffering.total_bytes_buffered ) )
4860 {
4861 if( recv_msg_seq_offset > 0 )
4862 {
4863 /* If we can't buffer a future message because
4864 * of space limitations -- ignore. */
4865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
4866 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4867 (unsigned) hs->buffering.total_bytes_buffered ) );
4868 goto exit;
4869 }
Hanno Beckere1801392018-08-21 16:51:05 +01004870 else
4871 {
4872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- attempt to make space by freeing buffered future messages\n",
4873 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4874 (unsigned) hs->buffering.total_bytes_buffered ) );
4875 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004876
Hanno Beckera02b0b42018-08-21 17:20:27 +01004877 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004878 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %u (%u with bitmap) would exceed the compile-time limit %u (already %u bytes buffered) -- fail\n",
4880 (unsigned) msg_len,
4881 (unsigned) reassembly_buf_sz,
4882 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004883 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004884 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4885 goto exit;
4886 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004887 }
4888
4889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4890 msg_len ) );
4891
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004892 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4893 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004894 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004895 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004896 goto exit;
4897 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004898 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004899
4900 /* Prepare final header: copy msg_type, length and message_seq,
4901 * then add standardised fragment_offset and fragment_length */
4902 memcpy( hs_buf->data, ssl->in_msg, 6 );
4903 memset( hs_buf->data + 6, 0, 3 );
4904 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4905
4906 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004907
4908 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004909 }
4910 else
4911 {
4912 /* Make sure msg_type and length are consistent */
4913 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4914 {
4915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4916 /* Ignore */
4917 goto exit;
4918 }
4919 }
4920
Hanno Becker4422bbb2018-08-20 09:40:19 +01004921 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004922 {
4923 size_t frag_len, frag_off;
4924 unsigned char * const msg = hs_buf->data + 12;
4925
4926 /*
4927 * Check and copy current fragment
4928 */
4929
4930 /* Validation of header fields already done in
4931 * mbedtls_ssl_prepare_handshake_record(). */
4932 frag_off = ssl_get_hs_frag_off( ssl );
4933 frag_len = ssl_get_hs_frag_len( ssl );
4934
4935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4936 frag_off, frag_len ) );
4937 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4938
4939 if( hs_buf->is_fragmented )
4940 {
4941 unsigned char * const bitmask = msg + msg_len;
4942 ssl_bitmask_set( bitmask, frag_off, frag_len );
4943 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4944 msg_len ) == 0 );
4945 }
4946 else
4947 {
4948 hs_buf->is_complete = 1;
4949 }
4950
4951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4952 hs_buf->is_complete ? "" : "not yet " ) );
4953 }
4954
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004955 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004956 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004957
4958 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004959 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004960 break;
4961 }
4962
4963exit:
4964
4965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4966 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004967}
4968#endif /* MBEDTLS_SSL_PROTO_DTLS */
4969
Hanno Becker1097b342018-08-15 14:09:41 +01004970static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004971{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004972 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004973 * Consume last content-layer message and potentially
4974 * update in_msglen which keeps track of the contents'
4975 * consumption state.
4976 *
4977 * (1) Handshake messages:
4978 * Remove last handshake message, move content
4979 * and adapt in_msglen.
4980 *
4981 * (2) Alert messages:
4982 * Consume whole record content, in_msglen = 0.
4983 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004984 * (3) Change cipher spec:
4985 * Consume whole record content, in_msglen = 0.
4986 *
4987 * (4) Application data:
4988 * Don't do anything - the record layer provides
4989 * the application data as a stream transport
4990 * and consumes through mbedtls_ssl_read only.
4991 *
4992 */
4993
4994 /* Case (1): Handshake messages */
4995 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004996 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004997 /* Hard assertion to be sure that no application data
4998 * is in flight, as corrupting ssl->in_msglen during
4999 * ssl->in_offt != NULL is fatal. */
5000 if( ssl->in_offt != NULL )
5001 {
5002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5003 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5004 }
5005
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005006 /*
5007 * Get next Handshake message in the current record
5008 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005009
Hanno Becker4a810fb2017-05-24 16:27:30 +01005010 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005011 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005012 * current handshake content: If DTLS handshake
5013 * fragmentation is used, that's the fragment
5014 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005015 * size here is faulty and should be changed at
5016 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005017 * (2) While it doesn't seem to cause problems, one
5018 * has to be very careful not to assume that in_hslen
5019 * is always <= in_msglen in a sensible communication.
5020 * Again, it's wrong for DTLS handshake fragmentation.
5021 * The following check is therefore mandatory, and
5022 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005023 * Additionally, ssl->in_hslen might be arbitrarily out of
5024 * bounds after handling a DTLS message with an unexpected
5025 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005026 */
5027 if( ssl->in_hslen < ssl->in_msglen )
5028 {
5029 ssl->in_msglen -= ssl->in_hslen;
5030 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5031 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005032
Hanno Becker4a810fb2017-05-24 16:27:30 +01005033 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5034 ssl->in_msg, ssl->in_msglen );
5035 }
5036 else
5037 {
5038 ssl->in_msglen = 0;
5039 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005040
Hanno Becker4a810fb2017-05-24 16:27:30 +01005041 ssl->in_hslen = 0;
5042 }
5043 /* Case (4): Application data */
5044 else if( ssl->in_offt != NULL )
5045 {
5046 return( 0 );
5047 }
5048 /* Everything else (CCS & Alerts) */
5049 else
5050 {
5051 ssl->in_msglen = 0;
5052 }
5053
Hanno Becker1097b342018-08-15 14:09:41 +01005054 return( 0 );
5055}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005056
Hanno Beckere74d5562018-08-15 14:26:08 +01005057static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5058{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005059 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005060 return( 1 );
5061
5062 return( 0 );
5063}
5064
Hanno Becker5f066e72018-08-16 14:56:31 +01005065#if defined(MBEDTLS_SSL_PROTO_DTLS)
5066
5067static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5068{
5069 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5070 if( hs == NULL )
5071 return;
5072
Hanno Becker01315ea2018-08-21 17:22:17 +01005073 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005074 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005075 hs->buffering.total_bytes_buffered -=
5076 hs->buffering.future_record.len;
5077
5078 mbedtls_free( hs->buffering.future_record.data );
5079 hs->buffering.future_record.data = NULL;
5080 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005081}
5082
5083static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5084{
5085 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5086 unsigned char * rec;
5087 size_t rec_len;
5088 unsigned rec_epoch;
5089
5090 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5091 return( 0 );
5092
5093 if( hs == NULL )
5094 return( 0 );
5095
Hanno Becker5f066e72018-08-16 14:56:31 +01005096 rec = hs->buffering.future_record.data;
5097 rec_len = hs->buffering.future_record.len;
5098 rec_epoch = hs->buffering.future_record.epoch;
5099
5100 if( rec == NULL )
5101 return( 0 );
5102
Hanno Becker4cb782d2018-08-20 11:19:05 +01005103 /* Only consider loading future records if the
5104 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005105 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005106 return( 0 );
5107
Hanno Becker5f066e72018-08-16 14:56:31 +01005108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5109
5110 if( rec_epoch != ssl->in_epoch )
5111 {
5112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5113 goto exit;
5114 }
5115
5116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5117
5118 /* Double-check that the record is not too large */
5119 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5120 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5121 {
5122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5123 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5124 }
5125
5126 memcpy( ssl->in_hdr, rec, rec_len );
5127 ssl->in_left = rec_len;
5128 ssl->next_record_offset = 0;
5129
5130 ssl_free_buffered_record( ssl );
5131
5132exit:
5133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5134 return( 0 );
5135}
5136
5137static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5138{
5139 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5140 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005141 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005142
5143 /* Don't buffer future records outside handshakes. */
5144 if( hs == NULL )
5145 return( 0 );
5146
5147 /* Only buffer handshake records (we are only interested
5148 * in Finished messages). */
5149 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5150 return( 0 );
5151
5152 /* Don't buffer more than one future epoch record. */
5153 if( hs->buffering.future_record.data != NULL )
5154 return( 0 );
5155
Hanno Becker01315ea2018-08-21 17:22:17 +01005156 /* Don't buffer record if there's not enough buffering space remaining. */
5157 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5158 hs->buffering.total_bytes_buffered ) )
5159 {
5160 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
5161 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5162 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005163 return( 0 );
5164 }
5165
Hanno Becker5f066e72018-08-16 14:56:31 +01005166 /* Buffer record */
5167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5168 ssl->in_epoch + 1 ) );
5169 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5170 rec_hdr_len + ssl->in_msglen );
5171
5172 /* ssl_parse_record_header() only considers records
5173 * of the next epoch as candidates for buffering. */
5174 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005175 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005176
5177 hs->buffering.future_record.data =
5178 mbedtls_calloc( 1, hs->buffering.future_record.len );
5179 if( hs->buffering.future_record.data == NULL )
5180 {
5181 /* If we run out of RAM trying to buffer a
5182 * record from the next epoch, just ignore. */
5183 return( 0 );
5184 }
5185
Hanno Becker01315ea2018-08-21 17:22:17 +01005186 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005187
Hanno Becker01315ea2018-08-21 17:22:17 +01005188 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005189 return( 0 );
5190}
5191
5192#endif /* MBEDTLS_SSL_PROTO_DTLS */
5193
Hanno Beckere74d5562018-08-15 14:26:08 +01005194static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005195{
5196 int ret;
5197
Hanno Becker5f066e72018-08-16 14:56:31 +01005198#if defined(MBEDTLS_SSL_PROTO_DTLS)
5199 /* We might have buffered a future record; if so,
5200 * and if the epoch matches now, load it.
5201 * On success, this call will set ssl->in_left to
5202 * the length of the buffered record, so that
5203 * the calls to ssl_fetch_input() below will
5204 * essentially be no-ops. */
5205 ret = ssl_load_buffered_record( ssl );
5206 if( ret != 0 )
5207 return( ret );
5208#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005210 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005212 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005213 return( ret );
5214 }
5215
5216 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005218#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005219 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5220 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005221 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005222 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5223 {
5224 ret = ssl_buffer_future_record( ssl );
5225 if( ret != 0 )
5226 return( ret );
5227
5228 /* Fall through to handling of unexpected records */
5229 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5230 }
5231
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005232 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5233 {
5234 /* Skip unexpected record (but not whole datagram) */
5235 ssl->next_record_offset = ssl->in_msglen
5236 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005237
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5239 "(header)" ) );
5240 }
5241 else
5242 {
5243 /* Skip invalid record and the rest of the datagram */
5244 ssl->next_record_offset = 0;
5245 ssl->in_left = 0;
5246
5247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5248 "(header)" ) );
5249 }
5250
5251 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005252 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005253 }
5254#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005255 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005256 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005257
5258 /*
5259 * Read and optionally decrypt the message contents
5260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005261 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5262 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005264 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005265 return( ret );
5266 }
5267
5268 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005269#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005270 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005272 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005273 if( ssl->next_record_offset < ssl->in_left )
5274 {
5275 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5276 }
5277 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005278 else
5279#endif
5280 ssl->in_left = 0;
5281
5282 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005284#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005285 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005286 {
5287 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005288 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5289 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005290 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005291 /* Except when waiting for Finished as a bad mac here
5292 * probably means something went wrong in the handshake
5293 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5294 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5295 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5296 {
5297#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5298 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5299 {
5300 mbedtls_ssl_send_alert_message( ssl,
5301 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5302 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5303 }
5304#endif
5305 return( ret );
5306 }
5307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005308#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005309 if( ssl->conf->badmac_limit != 0 &&
5310 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5313 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005314 }
5315#endif
5316
Hanno Becker4a810fb2017-05-24 16:27:30 +01005317 /* As above, invalid records cause
5318 * dismissal of the whole datagram. */
5319
5320 ssl->next_record_offset = 0;
5321 ssl->in_left = 0;
5322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005324 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005325 }
5326
5327 return( ret );
5328 }
5329 else
5330#endif
5331 {
5332 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005333#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5334 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005336 mbedtls_ssl_send_alert_message( ssl,
5337 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5338 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005339 }
5340#endif
5341 return( ret );
5342 }
5343 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005344
Simon Butcher99000142016-10-13 17:21:01 +01005345 return( 0 );
5346}
5347
5348int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5349{
5350 int ret;
5351
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005352 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005353 * Handle particular types of records
5354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005355 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005356 {
Simon Butcher99000142016-10-13 17:21:01 +01005357 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5358 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005359 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005360 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005361 }
5362
Hanno Beckere678eaa2018-08-21 14:57:46 +01005363 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005364 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005365 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005366 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5368 ssl->in_msglen ) );
5369 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005370 }
5371
Hanno Beckere678eaa2018-08-21 14:57:46 +01005372 if( ssl->in_msg[0] != 1 )
5373 {
5374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5375 ssl->in_msg[0] ) );
5376 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5377 }
5378
5379#if defined(MBEDTLS_SSL_PROTO_DTLS)
5380 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5381 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5382 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5383 {
5384 if( ssl->handshake == NULL )
5385 {
5386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5387 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5388 }
5389
5390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5391 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5392 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005393#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005394 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005396 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005397 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005398 if( ssl->in_msglen != 2 )
5399 {
5400 /* Note: Standard allows for more than one 2 byte alert
5401 to be packed in a single message, but Mbed TLS doesn't
5402 currently support this. */
5403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5404 ssl->in_msglen ) );
5405 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5406 }
5407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005409 ssl->in_msg[0], ssl->in_msg[1] ) );
5410
5411 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005412 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005414 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005417 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005418 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005419 }
5420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005421 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5422 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5425 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005426 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005427
5428#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5429 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5430 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5431 {
Hanno Becker90333da2017-10-10 11:27:13 +01005432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005433 /* Will be handled when trying to parse ServerHello */
5434 return( 0 );
5435 }
5436#endif
5437
5438#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5439 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5440 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5441 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5442 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5443 {
5444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5445 /* Will be handled in mbedtls_ssl_parse_certificate() */
5446 return( 0 );
5447 }
5448#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5449
5450 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005451 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005452 }
5453
Hanno Beckerc76c6192017-06-06 10:03:17 +01005454#if defined(MBEDTLS_SSL_PROTO_DTLS)
5455 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5456 ssl->handshake != NULL &&
5457 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5458 {
5459 ssl_handshake_wrapup_free_hs_transform( ssl );
5460 }
5461#endif
5462
Paul Bakker5121ce52009-01-03 21:22:43 +00005463 return( 0 );
5464}
5465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005467{
5468 int ret;
5469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005470 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5471 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5472 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005473 {
5474 return( ret );
5475 }
5476
5477 return( 0 );
5478}
5479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005481 unsigned char level,
5482 unsigned char message )
5483{
5484 int ret;
5485
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005486 if( ssl == NULL || ssl->conf == NULL )
5487 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005490 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005492 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005493 ssl->out_msglen = 2;
5494 ssl->out_msg[0] = level;
5495 ssl->out_msg[1] = message;
5496
Hanno Becker67bc7c32018-08-06 11:33:50 +01005497 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005500 return( ret );
5501 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005503
5504 return( 0 );
5505}
5506
Paul Bakker5121ce52009-01-03 21:22:43 +00005507/*
5508 * Handshake functions
5509 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005510#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5511 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5512 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5513 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5514 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5515 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5516 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005517/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005518int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005519{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005520 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005524 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5525 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005526 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5527 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005530 ssl->state++;
5531 return( 0 );
5532 }
5533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5535 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005536}
5537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005538int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005539{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005540 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005544 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5545 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005546 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5547 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005550 ssl->state++;
5551 return( 0 );
5552 }
5553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5555 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005556}
Gilles Peskinef9828522017-05-03 12:28:43 +02005557
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005558#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005559/* Some certificate support -> implement write and parse */
5560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005561int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005562{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005564 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005565 const mbedtls_x509_crt *crt;
5566 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005570 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5571 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005572 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5573 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005575 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005576 ssl->state++;
5577 return( 0 );
5578 }
5579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005580#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005581 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005582 {
5583 if( ssl->client_auth == 0 )
5584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005586 ssl->state++;
5587 return( 0 );
5588 }
5589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005590#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005591 /*
5592 * If using SSLv3 and got no cert, send an Alert message
5593 * (otherwise an empty Certificate message will be sent).
5594 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005595 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5596 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005597 {
5598 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005599 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5600 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5601 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005604 goto write_msg;
5605 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005606#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005607 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608#endif /* MBEDTLS_SSL_CLI_C */
5609#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005610 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005612 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5615 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005616 }
5617 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005618#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005620 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005621
5622 /*
5623 * 0 . 0 handshake type
5624 * 1 . 3 handshake length
5625 * 4 . 6 length of all certs
5626 * 7 . 9 length of cert. 1
5627 * 10 . n-1 peer certificate
5628 * n . n+2 length of cert. 2
5629 * n+3 . ... upper level cert, etc.
5630 */
5631 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005632 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005633
Paul Bakker29087132010-03-21 21:03:34 +00005634 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005635 {
5636 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005637 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005640 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005641 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005642 }
5643
5644 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5645 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5646 ssl->out_msg[i + 2] = (unsigned char)( n );
5647
5648 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5649 i += n; crt = crt->next;
5650 }
5651
5652 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5653 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5654 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5655
5656 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005657 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5658 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005659
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005660#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005661write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005662#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005663
5664 ssl->state++;
5665
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005666 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005667 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005668 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005669 return( ret );
5670 }
5671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005673
Paul Bakkered27a042013-04-18 22:46:23 +02005674 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005675}
5676
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005677/*
5678 * Once the certificate message is read, parse it into a cert chain and
5679 * perform basic checks, but leave actual verification to the caller
5680 */
5681static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005682{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005683 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005684 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005685 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005687#if defined(MBEDTLS_SSL_SRV_C)
5688#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005689 /*
5690 * Check if the client sent an empty certificate
5691 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005692 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005693 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005694 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005695 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005696 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5697 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5698 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005701
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005702 /* The client was asked for a certificate but didn't send
5703 one. The client should know what's going on, so we
5704 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005705 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005706 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005707 }
5708 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005709#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005711#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5712 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005713 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005714 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5717 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5718 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5719 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005722
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005723 /* The client was asked for a certificate but didn't send
5724 one. The client should know what's going on, so we
5725 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005726 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005727 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005728 }
5729 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005730#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5731 MBEDTLS_SSL_PROTO_TLS1_2 */
5732#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005734 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005737 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5738 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005739 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005740 }
5741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005742 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5743 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005746 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5747 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005748 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005749 }
5750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005751 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005752
Paul Bakker5121ce52009-01-03 21:22:43 +00005753 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005754 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005755 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005756 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005757
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005758 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005762 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5763 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005764 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005765 }
5766
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005767 /* In case we tried to reuse a session but it failed */
5768 if( ssl->session_negotiate->peer_cert != NULL )
5769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005770 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5771 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005772 }
5773
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005774 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005775 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005776 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005778 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005779 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5780 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005781 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005782 }
5783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005784 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005785
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005786 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005787
5788 while( i < ssl->in_hslen )
5789 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005790 if ( i + 3 > ssl->in_hslen ) {
5791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5792 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5793 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5794 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5795 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005796 if( ssl->in_msg[i] != 0 )
5797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005799 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5800 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005801 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005802 }
5803
5804 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5805 | (unsigned int) ssl->in_msg[i + 2];
5806 i += 3;
5807
5808 if( n < 128 || i + n > ssl->in_hslen )
5809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005811 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5812 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005813 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005814 }
5815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005816 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005817 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005818 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005819 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005820 case 0: /*ok*/
5821 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5822 /* Ignore certificate with an unknown algorithm: maybe a
5823 prior certificate was already trusted. */
5824 break;
5825
5826 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5827 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5828 goto crt_parse_der_failed;
5829
5830 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5831 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5832 goto crt_parse_der_failed;
5833
5834 default:
5835 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5836 crt_parse_der_failed:
5837 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005838 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005839 return( ret );
5840 }
5841
5842 i += n;
5843 }
5844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005845 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005846
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005847 /*
5848 * On client, make sure the server cert doesn't change during renego to
5849 * avoid "triple handshake" attack: https://secure-resumption.com/
5850 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005851#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005852 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005853 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005854 {
5855 if( ssl->session->peer_cert == NULL )
5856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005858 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5859 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005860 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005861 }
5862
5863 if( ssl->session->peer_cert->raw.len !=
5864 ssl->session_negotiate->peer_cert->raw.len ||
5865 memcmp( ssl->session->peer_cert->raw.p,
5866 ssl->session_negotiate->peer_cert->raw.p,
5867 ssl->session->peer_cert->raw.len ) != 0 )
5868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005870 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5871 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005873 }
5874 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005875#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005876
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005877 return( 0 );
5878}
5879
5880int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5881{
5882 int ret;
5883 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5884 ssl->transform_negotiate->ciphersuite_info;
5885#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5886 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
5887 ? ssl->handshake->sni_authmode
5888 : ssl->conf->authmode;
5889#else
5890 const int authmode = ssl->conf->authmode;
5891#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005892 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005893
5894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5895
5896 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5897 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
5898 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5899 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5900 {
5901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5902 ssl->state++;
5903 return( 0 );
5904 }
5905
5906#if defined(MBEDTLS_SSL_SRV_C)
5907 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5908 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5909 {
5910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5911 ssl->state++;
5912 return( 0 );
5913 }
5914
5915 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5916 authmode == MBEDTLS_SSL_VERIFY_NONE )
5917 {
5918 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005920
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005921 ssl->state++;
5922 return( 0 );
5923 }
5924#endif
5925
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005926#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5927 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005928 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005929 {
5930 goto crt_verify;
5931 }
5932#endif
5933
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02005934 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005935 {
5936 /* mbedtls_ssl_read_record may have sent an alert already. We
5937 let it decide whether to alert. */
5938 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5939 return( ret );
5940 }
5941
5942 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
5943 {
5944#if defined(MBEDTLS_SSL_SRV_C)
5945 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
5946 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
5947 {
5948 ret = 0;
5949 }
5950#endif
5951
5952 ssl->state++;
5953 return( ret );
5954 }
5955
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005956#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5957 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005958 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005959
5960crt_verify:
5961 if( ssl->handshake->ecrs_enabled)
5962 rs_ctx = &ssl->handshake->ecrs_ctx;
5963#endif
5964
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005965 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005966 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005967 mbedtls_x509_crt *ca_chain;
5968 mbedtls_x509_crl *ca_crl;
5969
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005970#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005971 if( ssl->handshake->sni_ca_chain != NULL )
5972 {
5973 ca_chain = ssl->handshake->sni_ca_chain;
5974 ca_crl = ssl->handshake->sni_ca_crl;
5975 }
5976 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005977#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005978 {
5979 ca_chain = ssl->conf->ca_chain;
5980 ca_crl = ssl->conf->ca_crl;
5981 }
5982
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005983 /*
5984 * Main check: verify certificate
5985 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005986 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005987 ssl->session_negotiate->peer_cert,
5988 ca_chain, ca_crl,
5989 ssl->conf->cert_profile,
5990 ssl->hostname,
5991 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005992 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00005993
5994 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005996 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005997 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005998
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005999#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6000 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02006001 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006002#endif
6003
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006004 /*
6005 * Secondary checks: always done, but change 'ret' only if it was 0
6006 */
6007
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006008#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006010 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006011
6012 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006013 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006014 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006015 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006016 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006019 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006020 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006021 }
6022 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006023#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006026 ciphersuite_info,
6027 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006028 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006031 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006033 }
6034
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006035 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6036 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6037 * with details encoded in the verification flags. All other kinds
6038 * of error codes, including those from the user provided f_vrfy
6039 * functions, are treated as fatal and lead to a failure of
6040 * ssl_parse_certificate even if verification was optional. */
6041 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6042 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6043 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6044 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006045 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006046 }
6047
6048 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6049 {
6050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6051 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6052 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006053
6054 if( ret != 0 )
6055 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006056 uint8_t alert;
6057
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006058 /* The certificate may have been rejected for several reasons.
6059 Pick one and send the corresponding alert. Which alert to send
6060 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006061 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6062 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6063 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6064 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6065 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6066 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6067 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6068 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6069 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6070 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6071 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6072 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6073 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6074 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6075 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6076 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6077 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6078 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6079 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6080 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006081 else
6082 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006083 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6084 alert );
6085 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006086
Hanno Beckere6706e62017-05-15 16:05:15 +01006087#if defined(MBEDTLS_DEBUG_C)
6088 if( ssl->session_negotiate->verify_result != 0 )
6089 {
6090 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6091 ssl->session_negotiate->verify_result ) );
6092 }
6093 else
6094 {
6095 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6096 }
6097#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006098 }
6099
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006100 ssl->state++;
6101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006103
6104 return( ret );
6105}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006106#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6107 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6108 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6109 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6110 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6111 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6112 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006114int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006115{
6116 int ret;
6117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006120 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006121 ssl->out_msglen = 1;
6122 ssl->out_msg[0] = 1;
6123
Paul Bakker5121ce52009-01-03 21:22:43 +00006124 ssl->state++;
6125
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006126 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006127 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006128 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006129 return( ret );
6130 }
6131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006133
6134 return( 0 );
6135}
6136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006137int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006138{
6139 int ret;
6140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006142
Hanno Becker327c93b2018-08-15 13:56:18 +01006143 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006145 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006146 return( ret );
6147 }
6148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006149 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006152 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6153 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006154 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006155 }
6156
Hanno Beckere678eaa2018-08-21 14:57:46 +01006157 /* CCS records are only accepted if they have length 1 and content '1',
6158 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006159
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006160 /*
6161 * Switch to our negotiated transform and session parameters for inbound
6162 * data.
6163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006164 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006165 ssl->transform_in = ssl->transform_negotiate;
6166 ssl->session_in = ssl->session_negotiate;
6167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006168#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006169 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006171#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006172 ssl_dtls_replay_reset( ssl );
6173#endif
6174
6175 /* Increment epoch */
6176 if( ++ssl->in_epoch == 0 )
6177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006179 /* This is highly unlikely to happen for legitimate reasons, so
6180 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006181 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006182 }
6183 }
6184 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006185#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006186 memset( ssl->in_ctr, 0, 8 );
6187
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006188 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006190#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6191 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006195 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006196 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6197 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006198 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006199 }
6200 }
6201#endif
6202
Paul Bakker5121ce52009-01-03 21:22:43 +00006203 ssl->state++;
6204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006206
6207 return( 0 );
6208}
6209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006210void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6211 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006212{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006213 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6216 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6217 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006218 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006219 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006220#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006221#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6222#if defined(MBEDTLS_SHA512_C)
6223 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006224 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6225 else
6226#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006227#if defined(MBEDTLS_SHA256_C)
6228 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006229 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006230 else
6231#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006235 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006236 }
Paul Bakker380da532012-04-18 16:10:25 +00006237}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006240{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006241#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6242 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006243 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6244 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006245#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006246#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6247#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006248 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006249#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006250#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006251 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006252#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006253#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006254}
6255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006256static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006257 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006258{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6260 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006261 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6262 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006263#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006264#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6265#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006266 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006267#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006268#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006269 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006270#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006271#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006272}
6273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006274#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6275 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6276static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006277 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006278{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006279 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6280 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006281}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006282#endif
Paul Bakker380da532012-04-18 16:10:25 +00006283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006284#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6285#if defined(MBEDTLS_SHA256_C)
6286static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006287 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006288{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006289 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006290}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006291#endif
Paul Bakker380da532012-04-18 16:10:25 +00006292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293#if defined(MBEDTLS_SHA512_C)
6294static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006295 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006296{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006297 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006298}
Paul Bakker769075d2012-11-24 11:26:46 +01006299#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006300#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006302#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006303static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006304 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006305{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006306 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006307 mbedtls_md5_context md5;
6308 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006309
Paul Bakker5121ce52009-01-03 21:22:43 +00006310 unsigned char padbuf[48];
6311 unsigned char md5sum[16];
6312 unsigned char sha1sum[20];
6313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006314 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006315 if( !session )
6316 session = ssl->session;
6317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006319
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006320 mbedtls_md5_init( &md5 );
6321 mbedtls_sha1_init( &sha1 );
6322
6323 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6324 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006325
6326 /*
6327 * SSLv3:
6328 * hash =
6329 * MD5( master + pad2 +
6330 * MD5( handshake + sender + master + pad1 ) )
6331 * + SHA1( master + pad2 +
6332 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006333 */
6334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006335#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006336 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6337 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006338#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006340#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006341 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6342 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006343#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006345 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006346 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006347
Paul Bakker1ef83d62012-04-11 12:09:53 +00006348 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006349
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006350 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6351 mbedtls_md5_update_ret( &md5, session->master, 48 );
6352 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6353 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006354
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006355 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6356 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6357 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6358 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006359
Paul Bakker1ef83d62012-04-11 12:09:53 +00006360 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006361
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006362 mbedtls_md5_starts_ret( &md5 );
6363 mbedtls_md5_update_ret( &md5, session->master, 48 );
6364 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6365 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6366 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006367
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006368 mbedtls_sha1_starts_ret( &sha1 );
6369 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6370 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6371 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6372 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006374 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006375
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006376 mbedtls_md5_free( &md5 );
6377 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006378
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006379 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6380 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6381 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006384}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006385#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006387#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006388static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006389 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006390{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006391 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006392 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006393 mbedtls_md5_context md5;
6394 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006395 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006397 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006398 if( !session )
6399 session = ssl->session;
6400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006402
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006403 mbedtls_md5_init( &md5 );
6404 mbedtls_sha1_init( &sha1 );
6405
6406 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6407 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006408
Paul Bakker1ef83d62012-04-11 12:09:53 +00006409 /*
6410 * TLSv1:
6411 * hash = PRF( master, finished_label,
6412 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6413 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006415#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006416 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6417 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006418#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006421 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6422 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006423#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006425 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006426 ? "client finished"
6427 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006428
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006429 mbedtls_md5_finish_ret( &md5, padbuf );
6430 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006431
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006432 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006433 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006435 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006436
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006437 mbedtls_md5_free( &md5 );
6438 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006439
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006440 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006443}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006444#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006446#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6447#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006448static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006449 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006450{
6451 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006452 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006453 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006454 unsigned char padbuf[32];
6455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006456 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006457 if( !session )
6458 session = ssl->session;
6459
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006460 mbedtls_sha256_init( &sha256 );
6461
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006463
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006464 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006465
6466 /*
6467 * TLSv1.2:
6468 * hash = PRF( master, finished_label,
6469 * Hash( handshake ) )[0.11]
6470 */
6471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006472#if !defined(MBEDTLS_SHA256_ALT)
6473 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006474 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006475#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006477 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006478 ? "client finished"
6479 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006480
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006481 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006482
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006483 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006484 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006486 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006487
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006488 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006489
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006490 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006493}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006494#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006496#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006497static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006498 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006499{
6500 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006501 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006502 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006503 unsigned char padbuf[48];
6504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006505 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006506 if( !session )
6507 session = ssl->session;
6508
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006509 mbedtls_sha512_init( &sha512 );
6510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006512
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006513 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006514
6515 /*
6516 * TLSv1.2:
6517 * hash = PRF( master, finished_label,
6518 * Hash( handshake ) )[0.11]
6519 */
6520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006521#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006522 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6523 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006524#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006526 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006527 ? "client finished"
6528 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006529
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006530 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006531
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006532 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006533 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006535 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006536
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006537 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006538
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006539 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006542}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006543#endif /* MBEDTLS_SHA512_C */
6544#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006547{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006548 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006549
6550 /*
6551 * Free our handshake params
6552 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006553 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006554 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006555 ssl->handshake = NULL;
6556
6557 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006558 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006559 */
6560 if( ssl->transform )
6561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006562 mbedtls_ssl_transform_free( ssl->transform );
6563 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006564 }
6565 ssl->transform = ssl->transform_negotiate;
6566 ssl->transform_negotiate = NULL;
6567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006568 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006569}
6570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006571void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006572{
6573 int resume = ssl->handshake->resume;
6574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006575 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006577#if defined(MBEDTLS_SSL_RENEGOTIATION)
6578 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006580 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006581 ssl->renego_records_seen = 0;
6582 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006583#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006584
6585 /*
6586 * Free the previous session and switch in the current one
6587 */
Paul Bakker0a597072012-09-25 21:55:46 +00006588 if( ssl->session )
6589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006590#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006591 /* RFC 7366 3.1: keep the EtM state */
6592 ssl->session_negotiate->encrypt_then_mac =
6593 ssl->session->encrypt_then_mac;
6594#endif
6595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596 mbedtls_ssl_session_free( ssl->session );
6597 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006598 }
6599 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006600 ssl->session_negotiate = NULL;
6601
Paul Bakker0a597072012-09-25 21:55:46 +00006602 /*
6603 * Add cache entry
6604 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006605 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006606 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006607 resume == 0 )
6608 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006609 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006611 }
Paul Bakker0a597072012-09-25 21:55:46 +00006612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006614 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006615 ssl->handshake->flight != NULL )
6616 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006617 /* Cancel handshake timer */
6618 ssl_set_timer( ssl, 0 );
6619
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006620 /* Keep last flight around in case we need to resend it:
6621 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006622 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006623 }
6624 else
6625#endif
6626 ssl_handshake_wrapup_free_hs_transform( ssl );
6627
Paul Bakker48916f92012-09-16 19:57:18 +00006628 ssl->state++;
6629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006630 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006631}
6632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006633int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006634{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006635 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006638
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006639 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006640
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006641 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006642
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006643 /*
6644 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6645 * may define some other value. Currently (early 2016), no defined
6646 * ciphersuite does this (and this is unlikely to change as activity has
6647 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6648 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006649 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006651#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006652 ssl->verify_data_len = hash_len;
6653 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006654#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006655
Paul Bakker5121ce52009-01-03 21:22:43 +00006656 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006657 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6658 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006659
6660 /*
6661 * In case of session resuming, invert the client and server
6662 * ChangeCipherSpec messages order.
6663 */
Paul Bakker0a597072012-09-25 21:55:46 +00006664 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006666#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006667 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006668 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006669#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006670#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006671 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006673#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006674 }
6675 else
6676 ssl->state++;
6677
Paul Bakker48916f92012-09-16 19:57:18 +00006678 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006679 * Switch to our negotiated transform and session parameters for outbound
6680 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006681 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006684#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006685 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006686 {
6687 unsigned char i;
6688
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006689 /* Remember current epoch settings for resending */
6690 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006691 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006692
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006693 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006694 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006695
6696 /* Increment epoch */
6697 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006698 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006699 break;
6700
6701 /* The loop goes to its end iff the counter is wrapping */
6702 if( i == 0 )
6703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6705 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006706 }
6707 }
6708 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006709#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006710 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006711
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006712 ssl->transform_out = ssl->transform_negotiate;
6713 ssl->session_out = ssl->session_negotiate;
6714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6716 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006720 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6721 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006722 }
6723 }
6724#endif
6725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006726#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006727 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006728 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006729#endif
6730
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006731 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006732 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006733 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006734 return( ret );
6735 }
6736
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006737#if defined(MBEDTLS_SSL_PROTO_DTLS)
6738 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6739 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6740 {
6741 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6742 return( ret );
6743 }
6744#endif
6745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006747
6748 return( 0 );
6749}
6750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006751#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006752#define SSL_MAX_HASH_LEN 36
6753#else
6754#define SSL_MAX_HASH_LEN 12
6755#endif
6756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006757int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006758{
Paul Bakker23986e52011-04-24 08:57:21 +00006759 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006760 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006761 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006764
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006765 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006766
Hanno Becker327c93b2018-08-15 13:56:18 +01006767 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006769 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006770 return( ret );
6771 }
6772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006773 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006775 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006776 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6777 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006779 }
6780
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006781 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782#if defined(MBEDTLS_SSL_PROTO_SSL3)
6783 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006784 hash_len = 36;
6785 else
6786#endif
6787 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006789 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6790 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006793 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6794 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006796 }
6797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006798 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006799 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006802 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6803 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006804 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006805 }
6806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006807#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006808 ssl->verify_data_len = hash_len;
6809 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006810#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006811
Paul Bakker0a597072012-09-25 21:55:46 +00006812 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006814#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006815 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006816 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006817#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006818#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006819 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006820 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006821#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006822 }
6823 else
6824 ssl->state++;
6825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006827 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006829#endif
6830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006832
6833 return( 0 );
6834}
6835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006836static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006837{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006838 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006840#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6841 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6842 mbedtls_md5_init( &handshake->fin_md5 );
6843 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006844 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6845 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006846#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006847#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6848#if defined(MBEDTLS_SHA256_C)
6849 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006850 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006851#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852#if defined(MBEDTLS_SHA512_C)
6853 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006854 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006855#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006856#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006857
6858 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006859
6860#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6861 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6862 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6863#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006865#if defined(MBEDTLS_DHM_C)
6866 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006867#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006868#if defined(MBEDTLS_ECDH_C)
6869 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006870#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006871#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006872 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006873#if defined(MBEDTLS_SSL_CLI_C)
6874 handshake->ecjpake_cache = NULL;
6875 handshake->ecjpake_cache_len = 0;
6876#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006877#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006878
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006879#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02006880 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006881#endif
6882
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006883#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6884 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6885#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006886}
6887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006888static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006889{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006890 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006892 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6893 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006895 mbedtls_md_init( &transform->md_ctx_enc );
6896 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006897}
6898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006899void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006900{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006901 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006902}
6903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006904static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006905{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006906 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006907 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006909 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006911 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006912 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006913
6914 /*
6915 * Either the pointers are now NULL or cleared properly and can be freed.
6916 * Now allocate missing structures.
6917 */
6918 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006919 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006920 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006921 }
Paul Bakker48916f92012-09-16 19:57:18 +00006922
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006923 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006924 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006925 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006926 }
Paul Bakker48916f92012-09-16 19:57:18 +00006927
Paul Bakker82788fb2014-10-20 13:59:19 +02006928 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006929 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006930 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006931 }
Paul Bakker48916f92012-09-16 19:57:18 +00006932
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006933 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006934 if( ssl->handshake == NULL ||
6935 ssl->transform_negotiate == NULL ||
6936 ssl->session_negotiate == NULL )
6937 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006940 mbedtls_free( ssl->handshake );
6941 mbedtls_free( ssl->transform_negotiate );
6942 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006943
6944 ssl->handshake = NULL;
6945 ssl->transform_negotiate = NULL;
6946 ssl->session_negotiate = NULL;
6947
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006948 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006949 }
6950
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006951 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006953 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006954 ssl_handshake_params_init( ssl->handshake );
6955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006956#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006957 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6958 {
6959 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006960
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006961 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6962 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6963 else
6964 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006965
6966 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006967 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006968#endif
6969
Paul Bakker48916f92012-09-16 19:57:18 +00006970 return( 0 );
6971}
6972
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006973#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006974/* Dummy cookie callbacks for defaults */
6975static int ssl_cookie_write_dummy( void *ctx,
6976 unsigned char **p, unsigned char *end,
6977 const unsigned char *cli_id, size_t cli_id_len )
6978{
6979 ((void) ctx);
6980 ((void) p);
6981 ((void) end);
6982 ((void) cli_id);
6983 ((void) cli_id_len);
6984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006985 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006986}
6987
6988static int ssl_cookie_check_dummy( void *ctx,
6989 const unsigned char *cookie, size_t cookie_len,
6990 const unsigned char *cli_id, size_t cli_id_len )
6991{
6992 ((void) ctx);
6993 ((void) cookie);
6994 ((void) cookie_len);
6995 ((void) cli_id);
6996 ((void) cli_id_len);
6997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006998 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006999}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007000#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007001
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007002/* Once ssl->out_hdr as the address of the beginning of the
7003 * next outgoing record is set, deduce the other pointers.
7004 *
7005 * Note: For TLS, we save the implicit record sequence number
7006 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7007 * and the caller has to make sure there's space for this.
7008 */
7009
7010static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7011 mbedtls_ssl_transform *transform )
7012{
7013#if defined(MBEDTLS_SSL_PROTO_DTLS)
7014 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7015 {
7016 ssl->out_ctr = ssl->out_hdr + 3;
7017 ssl->out_len = ssl->out_hdr + 11;
7018 ssl->out_iv = ssl->out_hdr + 13;
7019 }
7020 else
7021#endif
7022 {
7023 ssl->out_ctr = ssl->out_hdr - 8;
7024 ssl->out_len = ssl->out_hdr + 3;
7025 ssl->out_iv = ssl->out_hdr + 5;
7026 }
7027
7028 /* Adjust out_msg to make space for explicit IV, if used. */
7029 if( transform != NULL &&
7030 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7031 {
7032 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7033 }
7034 else
7035 ssl->out_msg = ssl->out_iv;
7036}
7037
7038/* Once ssl->in_hdr as the address of the beginning of the
7039 * next incoming record is set, deduce the other pointers.
7040 *
7041 * Note: For TLS, we save the implicit record sequence number
7042 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7043 * and the caller has to make sure there's space for this.
7044 */
7045
7046static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7047 mbedtls_ssl_transform *transform )
7048{
7049#if defined(MBEDTLS_SSL_PROTO_DTLS)
7050 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7051 {
7052 ssl->in_ctr = ssl->in_hdr + 3;
7053 ssl->in_len = ssl->in_hdr + 11;
7054 ssl->in_iv = ssl->in_hdr + 13;
7055 }
7056 else
7057#endif
7058 {
7059 ssl->in_ctr = ssl->in_hdr - 8;
7060 ssl->in_len = ssl->in_hdr + 3;
7061 ssl->in_iv = ssl->in_hdr + 5;
7062 }
7063
7064 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7065 if( transform != NULL &&
7066 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7067 {
7068 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7069 }
7070 else
7071 ssl->in_msg = ssl->in_iv;
7072}
7073
Paul Bakker5121ce52009-01-03 21:22:43 +00007074/*
7075 * Initialize an SSL context
7076 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007077void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7078{
7079 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7080}
7081
7082/*
7083 * Setup an SSL context
7084 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007085
7086static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7087{
7088 /* Set the incoming and outgoing record pointers. */
7089#if defined(MBEDTLS_SSL_PROTO_DTLS)
7090 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7091 {
7092 ssl->out_hdr = ssl->out_buf;
7093 ssl->in_hdr = ssl->in_buf;
7094 }
7095 else
7096#endif /* MBEDTLS_SSL_PROTO_DTLS */
7097 {
7098 ssl->out_hdr = ssl->out_buf + 8;
7099 ssl->in_hdr = ssl->in_buf + 8;
7100 }
7101
7102 /* Derive other internal pointers. */
7103 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7104 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7105}
7106
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007107int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007108 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007109{
Paul Bakker48916f92012-09-16 19:57:18 +00007110 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007111
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007112 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007113
7114 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007115 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007116 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007117
7118 /* Set to NULL in case of an error condition */
7119 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007120
Angus Grattond8213d02016-05-25 20:56:48 +10007121 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7122 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007123 {
Angus Grattond8213d02016-05-25 20:56:48 +10007124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007125 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007126 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007127 }
7128
7129 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7130 if( ssl->out_buf == NULL )
7131 {
7132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007133 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007134 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007135 }
7136
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007137 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007138
Paul Bakker48916f92012-09-16 19:57:18 +00007139 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007140 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007141
7142 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007143
7144error:
7145 mbedtls_free( ssl->in_buf );
7146 mbedtls_free( ssl->out_buf );
7147
7148 ssl->conf = NULL;
7149
7150 ssl->in_buf = NULL;
7151 ssl->out_buf = NULL;
7152
7153 ssl->in_hdr = NULL;
7154 ssl->in_ctr = NULL;
7155 ssl->in_len = NULL;
7156 ssl->in_iv = NULL;
7157 ssl->in_msg = NULL;
7158
7159 ssl->out_hdr = NULL;
7160 ssl->out_ctr = NULL;
7161 ssl->out_len = NULL;
7162 ssl->out_iv = NULL;
7163 ssl->out_msg = NULL;
7164
k-stachowiak9f7798e2018-07-31 16:52:32 +02007165 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007166}
7167
7168/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007169 * Reset an initialized and used SSL context for re-use while retaining
7170 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007171 *
7172 * If partial is non-zero, keep data in the input buffer and client ID.
7173 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007174 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007175static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007176{
Paul Bakker48916f92012-09-16 19:57:18 +00007177 int ret;
7178
Hanno Becker7e772132018-08-10 12:38:21 +01007179#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7180 !defined(MBEDTLS_SSL_SRV_C)
7181 ((void) partial);
7182#endif
7183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007184 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007185
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007186 /* Cancel any possibly running timer */
7187 ssl_set_timer( ssl, 0 );
7188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189#if defined(MBEDTLS_SSL_RENEGOTIATION)
7190 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007191 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007192
7193 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007194 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7195 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007196#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007197 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007198
Paul Bakker7eb013f2011-10-06 12:37:39 +00007199 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007200 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007201
7202 ssl->in_msgtype = 0;
7203 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007205 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007206 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007207#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007209 ssl_dtls_replay_reset( ssl );
7210#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007211
7212 ssl->in_hslen = 0;
7213 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007214
7215 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007216
7217 ssl->out_msgtype = 0;
7218 ssl->out_msglen = 0;
7219 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7221 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007222 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007223#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007224
Hanno Becker19859472018-08-06 09:40:20 +01007225 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7226
Paul Bakker48916f92012-09-16 19:57:18 +00007227 ssl->transform_in = NULL;
7228 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007229
Hanno Becker78640902018-08-13 16:35:15 +01007230 ssl->session_in = NULL;
7231 ssl->session_out = NULL;
7232
Angus Grattond8213d02016-05-25 20:56:48 +10007233 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007234
7235#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007236 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007237#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7238 {
7239 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007240 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007241 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7244 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7247 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7250 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007251 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007252 }
7253#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007254
Paul Bakker48916f92012-09-16 19:57:18 +00007255 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007257 mbedtls_ssl_transform_free( ssl->transform );
7258 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007259 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007260 }
Paul Bakker48916f92012-09-16 19:57:18 +00007261
Paul Bakkerc0463502013-02-14 11:19:38 +01007262 if( ssl->session )
7263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264 mbedtls_ssl_session_free( ssl->session );
7265 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007266 ssl->session = NULL;
7267 }
7268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007269#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007270 ssl->alpn_chosen = NULL;
7271#endif
7272
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007273#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007274#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007275 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007276#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007277 {
7278 mbedtls_free( ssl->cli_id );
7279 ssl->cli_id = NULL;
7280 ssl->cli_id_len = 0;
7281 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007282#endif
7283
Paul Bakker48916f92012-09-16 19:57:18 +00007284 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7285 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007286
7287 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007288}
7289
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007290/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007291 * Reset an initialized and used SSL context for re-use while retaining
7292 * all application-set variables, function pointers and data.
7293 */
7294int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7295{
7296 return( ssl_session_reset_int( ssl, 0 ) );
7297}
7298
7299/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007300 * SSL set accessors
7301 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007302void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007303{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007304 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007305}
7306
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007307void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007308{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007309 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007310}
7311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007312#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007313void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007314{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007315 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007316}
7317#endif
7318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007320void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007321{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007322 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007323}
7324#endif
7325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007326#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007327
Hanno Becker1841b0a2018-08-24 11:13:57 +01007328void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7329 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007330{
7331 ssl->disable_datagram_packing = !allow_packing;
7332}
7333
7334void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7335 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007336{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007337 conf->hs_timeout_min = min;
7338 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007339}
7340#endif
7341
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007342void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007343{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007344 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007345}
7346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007347#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007348void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007349 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007350 void *p_vrfy )
7351{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007352 conf->f_vrfy = f_vrfy;
7353 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007355#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007356
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007357void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007358 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007359 void *p_rng )
7360{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007361 conf->f_rng = f_rng;
7362 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007363}
7364
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007365void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007366 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007367 void *p_dbg )
7368{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007369 conf->f_dbg = f_dbg;
7370 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007371}
7372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007373void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007374 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007375 mbedtls_ssl_send_t *f_send,
7376 mbedtls_ssl_recv_t *f_recv,
7377 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007378{
7379 ssl->p_bio = p_bio;
7380 ssl->f_send = f_send;
7381 ssl->f_recv = f_recv;
7382 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007383}
7384
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007385#if defined(MBEDTLS_SSL_PROTO_DTLS)
7386void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7387{
7388 ssl->mtu = mtu;
7389}
7390#endif
7391
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007392void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007393{
7394 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007395}
7396
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007397void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7398 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007399 mbedtls_ssl_set_timer_t *f_set_timer,
7400 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007401{
7402 ssl->p_timer = p_timer;
7403 ssl->f_set_timer = f_set_timer;
7404 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007405
7406 /* Make sure we start with no timer running */
7407 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007408}
7409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007410#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007411void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007412 void *p_cache,
7413 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7414 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007415{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007416 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007417 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007418 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007419}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422#if defined(MBEDTLS_SSL_CLI_C)
7423int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007424{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007425 int ret;
7426
7427 if( ssl == NULL ||
7428 session == NULL ||
7429 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007430 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007432 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007433 }
7434
7435 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7436 return( ret );
7437
Paul Bakker0a597072012-09-25 21:55:46 +00007438 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007439
7440 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007441}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007443
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007444void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007445 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007446{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007447 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7448 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7449 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7450 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007451}
7452
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007453void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007454 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007455 int major, int minor )
7456{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007458 return;
7459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007460 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007461 return;
7462
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007463 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007464}
7465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007466#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007467void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007468 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007469{
7470 conf->cert_profile = profile;
7471}
7472
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007473/* Append a new keycert entry to a (possibly empty) list */
7474static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7475 mbedtls_x509_crt *cert,
7476 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007477{
niisato8ee24222018-06-25 19:05:48 +09007478 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007479
niisato8ee24222018-06-25 19:05:48 +09007480 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7481 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007482 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007483
niisato8ee24222018-06-25 19:05:48 +09007484 new_cert->cert = cert;
7485 new_cert->key = key;
7486 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007487
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007488 /* Update head is the list was null, else add to the end */
7489 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007490 {
niisato8ee24222018-06-25 19:05:48 +09007491 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007492 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007493 else
7494 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007495 mbedtls_ssl_key_cert *cur = *head;
7496 while( cur->next != NULL )
7497 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007498 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007499 }
7500
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007501 return( 0 );
7502}
7503
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007504int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007505 mbedtls_x509_crt *own_cert,
7506 mbedtls_pk_context *pk_key )
7507{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007508 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007509}
7510
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007511void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007512 mbedtls_x509_crt *ca_chain,
7513 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007514{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007515 conf->ca_chain = ca_chain;
7516 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007517}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007518#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007519
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007520#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7521int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7522 mbedtls_x509_crt *own_cert,
7523 mbedtls_pk_context *pk_key )
7524{
7525 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7526 own_cert, pk_key ) );
7527}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007528
7529void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7530 mbedtls_x509_crt *ca_chain,
7531 mbedtls_x509_crl *ca_crl )
7532{
7533 ssl->handshake->sni_ca_chain = ca_chain;
7534 ssl->handshake->sni_ca_crl = ca_crl;
7535}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007536
7537void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7538 int authmode )
7539{
7540 ssl->handshake->sni_authmode = authmode;
7541}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007542#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7543
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007544#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007545/*
7546 * Set EC J-PAKE password for current handshake
7547 */
7548int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7549 const unsigned char *pw,
7550 size_t pw_len )
7551{
7552 mbedtls_ecjpake_role role;
7553
Janos Follath8eb64132016-06-03 15:40:57 +01007554 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007555 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7556
7557 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7558 role = MBEDTLS_ECJPAKE_SERVER;
7559 else
7560 role = MBEDTLS_ECJPAKE_CLIENT;
7561
7562 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7563 role,
7564 MBEDTLS_MD_SHA256,
7565 MBEDTLS_ECP_DP_SECP256R1,
7566 pw, pw_len ) );
7567}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007568#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007570#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007571
7572static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
7573{
7574 /* Remove reference to existing PSK, if any. */
7575#if defined(MBEDTLS_USE_PSA_CRYPTO)
7576 if( conf->psk_opaque != 0 )
7577 {
7578 /* The maintenance of the PSK key slot is the
7579 * user's responsibility. */
7580 conf->psk_opaque = 0;
7581 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00007582 /* This and the following branch should never
7583 * be taken simultaenously as we maintain the
7584 * invariant that raw and opaque PSKs are never
7585 * configured simultaneously. As a safeguard,
7586 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007587#endif /* MBEDTLS_USE_PSA_CRYPTO */
7588 if( conf->psk != NULL )
7589 {
7590 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
7591
7592 mbedtls_free( conf->psk );
7593 conf->psk = NULL;
7594 conf->psk_len = 0;
7595 }
7596
7597 /* Remove reference to PSK identity, if any. */
7598 if( conf->psk_identity != NULL )
7599 {
7600 mbedtls_free( conf->psk_identity );
7601 conf->psk_identity = NULL;
7602 conf->psk_identity_len = 0;
7603 }
7604}
7605
Hanno Becker7390c712018-11-15 13:33:04 +00007606/* This function assumes that PSK identity in the SSL config is unset.
7607 * It checks that the provided identity is well-formed and attempts
7608 * to make a copy of it in the SSL config.
7609 * On failure, the PSK identity in the config remains unset. */
7610static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
7611 unsigned char const *psk_identity,
7612 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007613{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007614 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00007615 if( psk_identity == NULL ||
7616 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007617 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007618 {
7619 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7620 }
7621
Hanno Becker7390c712018-11-15 13:33:04 +00007622 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
7623 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007624 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02007625
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007626 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007627 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02007628
7629 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02007630}
7631
Hanno Becker7390c712018-11-15 13:33:04 +00007632int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
7633 const unsigned char *psk, size_t psk_len,
7634 const unsigned char *psk_identity, size_t psk_identity_len )
7635{
7636 int ret;
7637 /* Remove opaque/raw PSK + PSK Identity */
7638 ssl_conf_remove_psk( conf );
7639
7640 /* Check and set raw PSK */
7641 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
7642 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7643 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
7644 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7645 conf->psk_len = psk_len;
7646 memcpy( conf->psk, psk, conf->psk_len );
7647
7648 /* Check and set PSK Identity */
7649 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
7650 if( ret != 0 )
7651 ssl_conf_remove_psk( conf );
7652
7653 return( ret );
7654}
7655
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007656static void ssl_remove_psk( mbedtls_ssl_context *ssl )
7657{
7658#if defined(MBEDTLS_USE_PSA_CRYPTO)
7659 if( ssl->handshake->psk_opaque != 0 )
7660 {
7661 ssl->handshake->psk_opaque = 0;
7662 }
7663 else
7664#endif /* MBEDTLS_USE_PSA_CRYPTO */
7665 if( ssl->handshake->psk != NULL )
7666 {
7667 mbedtls_platform_zeroize( ssl->handshake->psk,
7668 ssl->handshake->psk_len );
7669 mbedtls_free( ssl->handshake->psk );
7670 ssl->handshake->psk_len = 0;
7671 }
7672}
7673
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007674int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7675 const unsigned char *psk, size_t psk_len )
7676{
7677 if( psk == NULL || ssl->handshake == NULL )
7678 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7679
7680 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7681 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7682
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007683 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007684
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007685 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007686 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007687
7688 ssl->handshake->psk_len = psk_len;
7689 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7690
7691 return( 0 );
7692}
7693
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007694#if defined(MBEDTLS_USE_PSA_CRYPTO)
7695int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007696 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007697 const unsigned char *psk_identity,
7698 size_t psk_identity_len )
7699{
Hanno Becker7390c712018-11-15 13:33:04 +00007700 int ret;
7701 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007702 ssl_conf_remove_psk( conf );
7703
Hanno Becker7390c712018-11-15 13:33:04 +00007704 /* Check and set opaque PSK */
7705 if( psk_slot == 0 )
7706 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007707 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00007708
7709 /* Check and set PSK Identity */
7710 ret = ssl_conf_set_psk_identity( conf, psk_identity,
7711 psk_identity_len );
7712 if( ret != 0 )
7713 ssl_conf_remove_psk( conf );
7714
7715 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007716}
7717
7718int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007719 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007720{
7721 if( psk_slot == 0 || ssl->handshake == NULL )
7722 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7723
7724 ssl_remove_psk( ssl );
7725 ssl->handshake->psk_opaque = psk_slot;
7726 return( 0 );
7727}
7728#endif /* MBEDTLS_USE_PSA_CRYPTO */
7729
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007730void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007732 size_t),
7733 void *p_psk )
7734{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007735 conf->f_psk = f_psk;
7736 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007737}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007738#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007739
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007740#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007741
7742#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007743int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007744{
7745 int ret;
7746
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007747 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7748 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7749 {
7750 mbedtls_mpi_free( &conf->dhm_P );
7751 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007752 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007753 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007754
7755 return( 0 );
7756}
Hanno Becker470a8c42017-10-04 15:28:46 +01007757#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007758
Hanno Beckera90658f2017-10-04 15:29:08 +01007759int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7760 const unsigned char *dhm_P, size_t P_len,
7761 const unsigned char *dhm_G, size_t G_len )
7762{
7763 int ret;
7764
7765 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7766 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7767 {
7768 mbedtls_mpi_free( &conf->dhm_P );
7769 mbedtls_mpi_free( &conf->dhm_G );
7770 return( ret );
7771 }
7772
7773 return( 0 );
7774}
Paul Bakker5121ce52009-01-03 21:22:43 +00007775
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007776int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007777{
7778 int ret;
7779
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007780 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7781 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7782 {
7783 mbedtls_mpi_free( &conf->dhm_P );
7784 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007785 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007786 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007787
7788 return( 0 );
7789}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007790#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007791
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007792#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7793/*
7794 * Set the minimum length for Diffie-Hellman parameters
7795 */
7796void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7797 unsigned int bitlen )
7798{
7799 conf->dhm_min_bitlen = bitlen;
7800}
7801#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7802
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007803#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007804/*
7805 * Set allowed/preferred hashes for handshake signatures
7806 */
7807void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7808 const int *hashes )
7809{
7810 conf->sig_hashes = hashes;
7811}
Hanno Becker947194e2017-04-07 13:25:49 +01007812#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007813
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007814#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007815/*
7816 * Set the allowed elliptic curves
7817 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007818void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007819 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007820{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007821 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007822}
Hanno Becker947194e2017-04-07 13:25:49 +01007823#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007824
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007825#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007826int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007827{
Hanno Becker947194e2017-04-07 13:25:49 +01007828 /* Initialize to suppress unnecessary compiler warning */
7829 size_t hostname_len = 0;
7830
7831 /* Check if new hostname is valid before
7832 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007833 if( hostname != NULL )
7834 {
7835 hostname_len = strlen( hostname );
7836
7837 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7838 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7839 }
7840
7841 /* Now it's clear that we will overwrite the old hostname,
7842 * so we can free it safely */
7843
7844 if( ssl->hostname != NULL )
7845 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007846 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007847 mbedtls_free( ssl->hostname );
7848 }
7849
7850 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007851
Paul Bakker5121ce52009-01-03 21:22:43 +00007852 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007853 {
7854 ssl->hostname = NULL;
7855 }
7856 else
7857 {
7858 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007859 if( ssl->hostname == NULL )
7860 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007861
Hanno Becker947194e2017-04-07 13:25:49 +01007862 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007863
Hanno Becker947194e2017-04-07 13:25:49 +01007864 ssl->hostname[hostname_len] = '\0';
7865 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007866
7867 return( 0 );
7868}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007869#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007870
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007871#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007872void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007874 const unsigned char *, size_t),
7875 void *p_sni )
7876{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007877 conf->f_sni = f_sni;
7878 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007879}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007880#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007883int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007884{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007885 size_t cur_len, tot_len;
7886 const char **p;
7887
7888 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007889 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7890 * MUST NOT be truncated."
7891 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007892 */
7893 tot_len = 0;
7894 for( p = protos; *p != NULL; p++ )
7895 {
7896 cur_len = strlen( *p );
7897 tot_len += cur_len;
7898
7899 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007901 }
7902
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007903 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007904
7905 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007906}
7907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007909{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007910 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007911}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007912#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007913
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007914void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007915{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007916 conf->max_major_ver = major;
7917 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007918}
7919
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007920void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007921{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007922 conf->min_major_ver = major;
7923 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007924}
7925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007927void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007928{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007929 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007930}
7931#endif
7932
Janos Follath088ce432017-04-10 12:42:31 +01007933#if defined(MBEDTLS_SSL_SRV_C)
7934void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7935 char cert_req_ca_list )
7936{
7937 conf->cert_req_ca_list = cert_req_ca_list;
7938}
7939#endif
7940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007942void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007943{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007944 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007945}
7946#endif
7947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007948#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007949void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007950{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007951 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007952}
7953#endif
7954
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007955#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007956void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007957{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007958 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007959}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007960#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007962#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007963int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007964{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007966 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007968 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007969 }
7970
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007971 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007972
7973 return( 0 );
7974}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007977#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007978void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007979{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007980 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007981}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007984#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007985void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007986{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007987 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007988}
7989#endif
7990
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007991void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007992{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007993 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007994}
7995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007997void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007998{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007999 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008000}
8001
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008002void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008003{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008004 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008005}
8006
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008007void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008008 const unsigned char period[8] )
8009{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008010 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008011}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008014#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008015#if defined(MBEDTLS_SSL_CLI_C)
8016void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008017{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008018 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008019}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008020#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008021
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008022#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008023void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8024 mbedtls_ssl_ticket_write_t *f_ticket_write,
8025 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8026 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008027{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008028 conf->f_ticket_write = f_ticket_write;
8029 conf->f_ticket_parse = f_ticket_parse;
8030 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008031}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008032#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008033#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008034
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008035#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8036void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8037 mbedtls_ssl_export_keys_t *f_export_keys,
8038 void *p_export_keys )
8039{
8040 conf->f_export_keys = f_export_keys;
8041 conf->p_export_keys = p_export_keys;
8042}
8043#endif
8044
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008045#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008046void mbedtls_ssl_conf_async_private_cb(
8047 mbedtls_ssl_config *conf,
8048 mbedtls_ssl_async_sign_t *f_async_sign,
8049 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8050 mbedtls_ssl_async_resume_t *f_async_resume,
8051 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008052 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008053{
8054 conf->f_async_sign_start = f_async_sign;
8055 conf->f_async_decrypt_start = f_async_decrypt;
8056 conf->f_async_resume = f_async_resume;
8057 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008058 conf->p_async_config_data = async_config_data;
8059}
8060
Gilles Peskine8f97af72018-04-26 11:46:10 +02008061void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8062{
8063 return( conf->p_async_config_data );
8064}
8065
Gilles Peskine1febfef2018-04-30 11:54:39 +02008066void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008067{
8068 if( ssl->handshake == NULL )
8069 return( NULL );
8070 else
8071 return( ssl->handshake->user_async_ctx );
8072}
8073
Gilles Peskine1febfef2018-04-30 11:54:39 +02008074void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008075 void *ctx )
8076{
8077 if( ssl->handshake != NULL )
8078 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008079}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008080#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008081
Paul Bakker5121ce52009-01-03 21:22:43 +00008082/*
8083 * SSL get accessors
8084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008085size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008086{
8087 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8088}
8089
Hanno Becker8b170a02017-10-10 11:51:19 +01008090int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8091{
8092 /*
8093 * Case A: We're currently holding back
8094 * a message for further processing.
8095 */
8096
8097 if( ssl->keep_current_message == 1 )
8098 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008099 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008100 return( 1 );
8101 }
8102
8103 /*
8104 * Case B: Further records are pending in the current datagram.
8105 */
8106
8107#if defined(MBEDTLS_SSL_PROTO_DTLS)
8108 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8109 ssl->in_left > ssl->next_record_offset )
8110 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008111 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008112 return( 1 );
8113 }
8114#endif /* MBEDTLS_SSL_PROTO_DTLS */
8115
8116 /*
8117 * Case C: A handshake message is being processed.
8118 */
8119
Hanno Becker8b170a02017-10-10 11:51:19 +01008120 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8121 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008122 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008123 return( 1 );
8124 }
8125
8126 /*
8127 * Case D: An application data message is being processed
8128 */
8129 if( ssl->in_offt != NULL )
8130 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008131 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008132 return( 1 );
8133 }
8134
8135 /*
8136 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008137 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008138 * we implement support for multiple alerts in single records.
8139 */
8140
8141 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8142 return( 0 );
8143}
8144
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008145uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008146{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008147 if( ssl->session != NULL )
8148 return( ssl->session->verify_result );
8149
8150 if( ssl->session_negotiate != NULL )
8151 return( ssl->session_negotiate->verify_result );
8152
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008153 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008154}
8155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008156const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008157{
Paul Bakker926c8e42013-03-06 10:23:34 +01008158 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008159 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008161 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008162}
8163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008165{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008166#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008167 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008168 {
8169 switch( ssl->minor_ver )
8170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008171 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008172 return( "DTLSv1.0" );
8173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008174 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008175 return( "DTLSv1.2" );
8176
8177 default:
8178 return( "unknown (DTLS)" );
8179 }
8180 }
8181#endif
8182
Paul Bakker43ca69c2011-01-15 17:35:19 +00008183 switch( ssl->minor_ver )
8184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008185 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008186 return( "SSLv3.0" );
8187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008188 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008189 return( "TLSv1.0" );
8190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008191 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008192 return( "TLSv1.1" );
8193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008195 return( "TLSv1.2" );
8196
Paul Bakker43ca69c2011-01-15 17:35:19 +00008197 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008198 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008199 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008200}
8201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008202int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008203{
Hanno Becker3136ede2018-08-17 15:28:19 +01008204 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008205 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008206 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008207
Hanno Becker78640902018-08-13 16:35:15 +01008208 if( transform == NULL )
8209 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008211#if defined(MBEDTLS_ZLIB_SUPPORT)
8212 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8213 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008214#endif
8215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008216 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008218 case MBEDTLS_MODE_GCM:
8219 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008220 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008221 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008222 transform_expansion = transform->minlen;
8223 break;
8224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008225 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008226
8227 block_size = mbedtls_cipher_get_block_size(
8228 &transform->cipher_ctx_enc );
8229
Hanno Becker3136ede2018-08-17 15:28:19 +01008230 /* Expansion due to the addition of the MAC. */
8231 transform_expansion += transform->maclen;
8232
8233 /* Expansion due to the addition of CBC padding;
8234 * Theoretically up to 256 bytes, but we never use
8235 * more than the block size of the underlying cipher. */
8236 transform_expansion += block_size;
8237
8238 /* For TLS 1.1 or higher, an explicit IV is added
8239 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008240#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8241 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008242 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008243#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008244
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008245 break;
8246
8247 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008249 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008250 }
8251
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008252 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008253}
8254
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008255#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8256size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8257{
8258 size_t max_len;
8259
8260 /*
8261 * Assume mfl_code is correct since it was checked when set
8262 */
Angus Grattond8213d02016-05-25 20:56:48 +10008263 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008264
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008265 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008266 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008267 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008268 {
Angus Grattond8213d02016-05-25 20:56:48 +10008269 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008270 }
8271
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008272 /* During a handshake, use the value being negotiated */
8273 if( ssl->session_negotiate != NULL &&
8274 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8275 {
8276 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8277 }
8278
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008279 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008280}
8281#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8282
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008283#if defined(MBEDTLS_SSL_PROTO_DTLS)
8284static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8285{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008286 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8287 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8288 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8289 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8290 return ( 0 );
8291
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008292 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8293 return( ssl->mtu );
8294
8295 if( ssl->mtu == 0 )
8296 return( ssl->handshake->mtu );
8297
8298 return( ssl->mtu < ssl->handshake->mtu ?
8299 ssl->mtu : ssl->handshake->mtu );
8300}
8301#endif /* MBEDTLS_SSL_PROTO_DTLS */
8302
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008303int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8304{
8305 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8306
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008307#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8308 !defined(MBEDTLS_SSL_PROTO_DTLS)
8309 (void) ssl;
8310#endif
8311
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008312#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8313 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8314
8315 if( max_len > mfl )
8316 max_len = mfl;
8317#endif
8318
8319#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008320 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008321 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008322 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008323 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8324 const size_t overhead = (size_t) ret;
8325
8326 if( ret < 0 )
8327 return( ret );
8328
8329 if( mtu <= overhead )
8330 {
8331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8332 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8333 }
8334
8335 if( max_len > mtu - overhead )
8336 max_len = mtu - overhead;
8337 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008338#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008339
Hanno Becker0defedb2018-08-10 12:35:02 +01008340#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8341 !defined(MBEDTLS_SSL_PROTO_DTLS)
8342 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008343#endif
8344
8345 return( (int) max_len );
8346}
8347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008348#if defined(MBEDTLS_X509_CRT_PARSE_C)
8349const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008350{
8351 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008352 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008353
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008354 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008355}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008356#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008358#if defined(MBEDTLS_SSL_CLI_C)
8359int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008360{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008361 if( ssl == NULL ||
8362 dst == NULL ||
8363 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008364 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008366 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008367 }
8368
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008369 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008370}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008371#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008372
Paul Bakker5121ce52009-01-03 21:22:43 +00008373/*
Paul Bakker1961b702013-01-25 14:49:24 +01008374 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008376int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008377{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008378 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008379
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008380 if( ssl == NULL || ssl->conf == NULL )
8381 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008383#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008384 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008385 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008386#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008387#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008388 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008389 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008390#endif
8391
Paul Bakker1961b702013-01-25 14:49:24 +01008392 return( ret );
8393}
8394
8395/*
8396 * Perform the SSL handshake
8397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008398int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008399{
8400 int ret = 0;
8401
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008402 if( ssl == NULL || ssl->conf == NULL )
8403 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008407 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008409 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008410
8411 if( ret != 0 )
8412 break;
8413 }
8414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008416
8417 return( ret );
8418}
8419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008420#if defined(MBEDTLS_SSL_RENEGOTIATION)
8421#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008422/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008423 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008425static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008426{
8427 int ret;
8428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008430
8431 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008432 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8433 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008434
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008435 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008436 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008437 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008438 return( ret );
8439 }
8440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008442
8443 return( 0 );
8444}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008445#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008446
8447/*
8448 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008449 * - any side: calling mbedtls_ssl_renegotiate(),
8450 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8451 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008452 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008453 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008454 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008455 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008456static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008457{
8458 int ret;
8459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008461
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008462 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8463 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008464
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008465 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8466 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008467#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008468 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008469 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008470 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008471 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008472 ssl->handshake->out_msg_seq = 1;
8473 else
8474 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008475 }
8476#endif
8477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008478 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8479 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008481 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008483 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008484 return( ret );
8485 }
8486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008488
8489 return( 0 );
8490}
8491
8492/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008493 * Renegotiate current connection on client,
8494 * or request renegotiation on server
8495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008496int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008497{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008498 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008499
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008500 if( ssl == NULL || ssl->conf == NULL )
8501 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008503#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008504 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008505 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008507 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8508 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008510 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008511
8512 /* Did we already try/start sending HelloRequest? */
8513 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008514 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008515
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008516 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008517 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008518#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008520#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008521 /*
8522 * On client, either start the renegotiation process or,
8523 * if already in progress, continue the handshake
8524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008525 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008527 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008529
8530 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008532 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008533 return( ret );
8534 }
8535 }
8536 else
8537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008538 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008541 return( ret );
8542 }
8543 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008544#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008545
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008546 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008547}
8548
8549/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008550 * Check record counters and renegotiate if they're above the limit.
8551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008552static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008553{
Andres AG2196c7f2016-12-15 17:01:16 +00008554 size_t ep_len = ssl_ep_len( ssl );
8555 int in_ctr_cmp;
8556 int out_ctr_cmp;
8557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008558 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8559 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008560 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008561 {
8562 return( 0 );
8563 }
8564
Andres AG2196c7f2016-12-15 17:01:16 +00008565 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8566 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008567 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008568 ssl->conf->renego_period + ep_len, 8 - ep_len );
8569
8570 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008571 {
8572 return( 0 );
8573 }
8574
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008576 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008577}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008578#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008579
8580/*
8581 * Receive application data decrypted from the SSL layer
8582 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008583int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008584{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008585 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008586 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008587
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008588 if( ssl == NULL || ssl->conf == NULL )
8589 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008593#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008594 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008596 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008597 return( ret );
8598
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008599 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008600 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008601 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008602 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008603 return( ret );
8604 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008605 }
8606#endif
8607
Hanno Becker4a810fb2017-05-24 16:27:30 +01008608 /*
8609 * Check if renegotiation is necessary and/or handshake is
8610 * in process. If yes, perform/continue, and fall through
8611 * if an unexpected packet is received while the client
8612 * is waiting for the ServerHello.
8613 *
8614 * (There is no equivalent to the last condition on
8615 * the server-side as it is not treated as within
8616 * a handshake while waiting for the ClientHello
8617 * after a renegotiation request.)
8618 */
8619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008620#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008621 ret = ssl_check_ctr_renegotiate( ssl );
8622 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8623 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008625 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008626 return( ret );
8627 }
8628#endif
8629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008630 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008632 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008633 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8634 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008636 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008637 return( ret );
8638 }
8639 }
8640
Hanno Beckere41158b2017-10-23 13:30:32 +01008641 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008642 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008643 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008644 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008645 if( ssl->f_get_timer != NULL &&
8646 ssl->f_get_timer( ssl->p_timer ) == -1 )
8647 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008648 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008649 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008650
Hanno Becker327c93b2018-08-15 13:56:18 +01008651 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008652 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008653 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8654 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008655
Hanno Becker4a810fb2017-05-24 16:27:30 +01008656 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8657 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008658 }
8659
8660 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008661 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008662 {
8663 /*
8664 * OpenSSL sends empty messages to randomize the IV
8665 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008666 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008668 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008669 return( 0 );
8670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008671 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008672 return( ret );
8673 }
8674 }
8675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008676 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008679
Hanno Becker4a810fb2017-05-24 16:27:30 +01008680 /*
8681 * - For client-side, expect SERVER_HELLO_REQUEST.
8682 * - For server-side, expect CLIENT_HELLO.
8683 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8684 */
8685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008687 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008688 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008689 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008692
8693 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008694#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008695 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008696 {
8697 continue;
8698 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008699#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008700 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008701 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008702#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008703
Hanno Becker4a810fb2017-05-24 16:27:30 +01008704#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008705 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008706 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008709
8710 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008711#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008712 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008713 {
8714 continue;
8715 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008716#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008717 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008718 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008719#endif /* MBEDTLS_SSL_SRV_C */
8720
Hanno Becker21df7f92017-10-17 11:03:26 +01008721#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008722 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008723 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8724 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8725 ssl->conf->allow_legacy_renegotiation ==
8726 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8727 {
8728 /*
8729 * Accept renegotiation request
8730 */
Paul Bakker48916f92012-09-16 19:57:18 +00008731
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008732 /* DTLS clients need to know renego is server-initiated */
8733#if defined(MBEDTLS_SSL_PROTO_DTLS)
8734 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8735 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8736 {
8737 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8738 }
8739#endif
8740 ret = ssl_start_renegotiation( ssl );
8741 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8742 ret != 0 )
8743 {
8744 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8745 return( ret );
8746 }
8747 }
8748 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008749#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008750 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008751 /*
8752 * Refuse renegotiation
8753 */
8754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008755 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008757#if defined(MBEDTLS_SSL_PROTO_SSL3)
8758 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008759 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008760 /* SSLv3 does not have a "no_renegotiation" warning, so
8761 we send a fatal alert and abort the connection. */
8762 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8763 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8764 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008765 }
8766 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008767#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8768#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8769 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8770 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008772 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8773 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8774 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008775 {
8776 return( ret );
8777 }
Paul Bakker48916f92012-09-16 19:57:18 +00008778 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008779 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008780#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8781 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8784 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008785 }
Paul Bakker48916f92012-09-16 19:57:18 +00008786 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008787
Hanno Becker90333da2017-10-10 11:27:13 +01008788 /* At this point, we don't know whether the renegotiation has been
8789 * completed or not. The cases to consider are the following:
8790 * 1) The renegotiation is complete. In this case, no new record
8791 * has been read yet.
8792 * 2) The renegotiation is incomplete because the client received
8793 * an application data record while awaiting the ServerHello.
8794 * 3) The renegotiation is incomplete because the client received
8795 * a non-handshake, non-application data message while awaiting
8796 * the ServerHello.
8797 * In each of these case, looping will be the proper action:
8798 * - For 1), the next iteration will read a new record and check
8799 * if it's application data.
8800 * - For 2), the loop condition isn't satisfied as application data
8801 * is present, hence continue is the same as break
8802 * - For 3), the loop condition is satisfied and read_record
8803 * will re-deliver the message that was held back by the client
8804 * when expecting the ServerHello.
8805 */
8806 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008807 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008808#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008809 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008810 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008811 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008812 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008813 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008816 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008817 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008818 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008819 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008820 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008821#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008823 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8824 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008827 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008828 }
8829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008830 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8833 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008834 }
8835
8836 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008837
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008838 /* We're going to return something now, cancel timer,
8839 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008840 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008841 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008842
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008843#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008844 /* If we requested renego but received AppData, resend HelloRequest.
8845 * Do it now, after setting in_offt, to avoid taking this branch
8846 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008847#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008848 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008849 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008850 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008851 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008853 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008854 return( ret );
8855 }
8856 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008857#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008858#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008859 }
8860
8861 n = ( len < ssl->in_msglen )
8862 ? len : ssl->in_msglen;
8863
8864 memcpy( buf, ssl->in_offt, n );
8865 ssl->in_msglen -= n;
8866
8867 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008868 {
8869 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008870 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008871 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008872 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008873 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008874 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008875 /* more data available */
8876 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008877 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008880
Paul Bakker23986e52011-04-24 08:57:21 +00008881 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008882}
8883
8884/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008885 * Send application data to be encrypted by the SSL layer, taking care of max
8886 * fragment length and buffer size.
8887 *
8888 * According to RFC 5246 Section 6.2.1:
8889 *
8890 * Zero-length fragments of Application data MAY be sent as they are
8891 * potentially useful as a traffic analysis countermeasure.
8892 *
8893 * Therefore, it is possible that the input message length is 0 and the
8894 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008895 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008896static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008897 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008898{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008899 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8900 const size_t max_len = (size_t) ret;
8901
8902 if( ret < 0 )
8903 {
8904 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8905 return( ret );
8906 }
8907
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008908 if( len > max_len )
8909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008910#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008911 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008914 "maximum fragment length: %d > %d",
8915 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008916 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008917 }
8918 else
8919#endif
8920 len = max_len;
8921 }
Paul Bakker887bd502011-06-08 13:10:54 +00008922
Paul Bakker5121ce52009-01-03 21:22:43 +00008923 if( ssl->out_left != 0 )
8924 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008925 /*
8926 * The user has previously tried to send the data and
8927 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8928 * written. In this case, we expect the high-level write function
8929 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8930 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008931 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008933 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008934 return( ret );
8935 }
8936 }
Paul Bakker887bd502011-06-08 13:10:54 +00008937 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008938 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008939 /*
8940 * The user is trying to send a message the first time, so we need to
8941 * copy the data into the internal buffers and setup the data structure
8942 * to keep track of partial writes
8943 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008944 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008945 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008946 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008947
Hanno Becker67bc7c32018-08-06 11:33:50 +01008948 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008950 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008951 return( ret );
8952 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008953 }
8954
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008955 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008956}
8957
8958/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008959 * Write application data, doing 1/n-1 splitting if necessary.
8960 *
8961 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008962 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008963 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008964 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008965#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008966static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008967 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008968{
8969 int ret;
8970
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008971 if( ssl->conf->cbc_record_splitting ==
8972 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008973 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008974 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8975 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8976 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008977 {
8978 return( ssl_write_real( ssl, buf, len ) );
8979 }
8980
8981 if( ssl->split_done == 0 )
8982 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008983 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008984 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008985 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008986 }
8987
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008988 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8989 return( ret );
8990 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008991
8992 return( ret + 1 );
8993}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008994#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008995
8996/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008997 * Write application data (public-facing wrapper)
8998 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008999int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009000{
9001 int ret;
9002
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009004
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009005 if( ssl == NULL || ssl->conf == NULL )
9006 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9007
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009008#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009009 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9010 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009011 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009012 return( ret );
9013 }
9014#endif
9015
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009016 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009017 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009018 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009019 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009020 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009021 return( ret );
9022 }
9023 }
9024
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009025#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009026 ret = ssl_write_split( ssl, buf, len );
9027#else
9028 ret = ssl_write_real( ssl, buf, len );
9029#endif
9030
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009032
9033 return( ret );
9034}
9035
9036/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009037 * Notify the peer that the connection is being closed
9038 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009039int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009040{
9041 int ret;
9042
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009043 if( ssl == NULL || ssl->conf == NULL )
9044 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009047
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009048 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009049 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009051 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009053 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9054 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9055 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009057 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009058 return( ret );
9059 }
9060 }
9061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009063
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009064 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009065}
9066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009067void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009068{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009069 if( transform == NULL )
9070 return;
9071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009072#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009073 deflateEnd( &transform->ctx_deflate );
9074 inflateEnd( &transform->ctx_inflate );
9075#endif
9076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009077 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9078 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009080 mbedtls_md_free( &transform->md_ctx_enc );
9081 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009082
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009083 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009084}
9085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009086#if defined(MBEDTLS_X509_CRT_PARSE_C)
9087static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009089 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009090
9091 while( cur != NULL )
9092 {
9093 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009094 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009095 cur = next;
9096 }
9097}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009098#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009099
Hanno Becker0271f962018-08-16 13:23:47 +01009100#if defined(MBEDTLS_SSL_PROTO_DTLS)
9101
9102static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9103{
9104 unsigned offset;
9105 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9106
9107 if( hs == NULL )
9108 return;
9109
Hanno Becker283f5ef2018-08-24 09:34:47 +01009110 ssl_free_buffered_record( ssl );
9111
Hanno Becker0271f962018-08-16 13:23:47 +01009112 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009113 ssl_buffering_free_slot( ssl, offset );
9114}
9115
9116static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9117 uint8_t slot )
9118{
9119 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9120 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009121
9122 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9123 return;
9124
Hanno Beckere605b192018-08-21 15:59:07 +01009125 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009126 {
Hanno Beckere605b192018-08-21 15:59:07 +01009127 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009128 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009129 mbedtls_free( hs_buf->data );
9130 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009131 }
9132}
9133
9134#endif /* MBEDTLS_SSL_PROTO_DTLS */
9135
Gilles Peskine9b562d52018-04-25 20:32:43 +02009136void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009137{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009138 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9139
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009140 if( handshake == NULL )
9141 return;
9142
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009143#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9144 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9145 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009146 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009147 handshake->async_in_progress = 0;
9148 }
9149#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9150
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009151#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9152 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9153 mbedtls_md5_free( &handshake->fin_md5 );
9154 mbedtls_sha1_free( &handshake->fin_sha1 );
9155#endif
9156#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9157#if defined(MBEDTLS_SHA256_C)
9158 mbedtls_sha256_free( &handshake->fin_sha256 );
9159#endif
9160#if defined(MBEDTLS_SHA512_C)
9161 mbedtls_sha512_free( &handshake->fin_sha512 );
9162#endif
9163#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009165#if defined(MBEDTLS_DHM_C)
9166 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009167#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009168#if defined(MBEDTLS_ECDH_C)
9169 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009170#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009171#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009172 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009173#if defined(MBEDTLS_SSL_CLI_C)
9174 mbedtls_free( handshake->ecjpake_cache );
9175 handshake->ecjpake_cache = NULL;
9176 handshake->ecjpake_cache_len = 0;
9177#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009178#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009179
Janos Follath4ae5c292016-02-10 11:27:43 +00009180#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9181 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009182 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009183 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009184#endif
9185
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009186#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9187 if( handshake->psk != NULL )
9188 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009189 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009190 mbedtls_free( handshake->psk );
9191 }
9192#endif
9193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009194#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9195 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009196 /*
9197 * Free only the linked list wrapper, not the keys themselves
9198 * since the belong to the SNI callback
9199 */
9200 if( handshake->sni_key_cert != NULL )
9201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009202 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009203
9204 while( cur != NULL )
9205 {
9206 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009207 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009208 cur = next;
9209 }
9210 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009211#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009212
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009213#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009214 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009215#endif
9216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009217#if defined(MBEDTLS_SSL_PROTO_DTLS)
9218 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009219 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009220 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009221#endif
9222
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009223 mbedtls_platform_zeroize( handshake,
9224 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009225}
9226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009227void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009228{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009229 if( session == NULL )
9230 return;
9231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009232#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00009233 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00009234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009235 mbedtls_x509_crt_free( session->peer_cert );
9236 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00009237 }
Paul Bakkered27a042013-04-18 22:46:23 +02009238#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009239
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009240#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009241 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009242#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009243
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009244 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009245}
9246
Paul Bakker5121ce52009-01-03 21:22:43 +00009247/*
9248 * Free an SSL context
9249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009250void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009251{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009252 if( ssl == NULL )
9253 return;
9254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009256
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009257 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009258 {
Angus Grattond8213d02016-05-25 20:56:48 +10009259 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009260 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009261 }
9262
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009263 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009264 {
Angus Grattond8213d02016-05-25 20:56:48 +10009265 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009266 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009267 }
9268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009269#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009270 if( ssl->compress_buf != NULL )
9271 {
Angus Grattond8213d02016-05-25 20:56:48 +10009272 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009273 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009274 }
9275#endif
9276
Paul Bakker48916f92012-09-16 19:57:18 +00009277 if( ssl->transform )
9278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009279 mbedtls_ssl_transform_free( ssl->transform );
9280 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009281 }
9282
9283 if( ssl->handshake )
9284 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009285 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009286 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9287 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009289 mbedtls_free( ssl->handshake );
9290 mbedtls_free( ssl->transform_negotiate );
9291 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009292 }
9293
Paul Bakkerc0463502013-02-14 11:19:38 +01009294 if( ssl->session )
9295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009296 mbedtls_ssl_session_free( ssl->session );
9297 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009298 }
9299
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009300#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009301 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009302 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009303 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009304 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009305 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009306#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009308#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9309 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9312 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009313 }
9314#endif
9315
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009316#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009317 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009318#endif
9319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009321
Paul Bakker86f04f42013-02-14 11:20:09 +01009322 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009323 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009324}
9325
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009326/*
9327 * Initialze mbedtls_ssl_config
9328 */
9329void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9330{
9331 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9332}
9333
Simon Butcherc97b6972015-12-27 23:48:17 +00009334#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009335static int ssl_preset_default_hashes[] = {
9336#if defined(MBEDTLS_SHA512_C)
9337 MBEDTLS_MD_SHA512,
9338 MBEDTLS_MD_SHA384,
9339#endif
9340#if defined(MBEDTLS_SHA256_C)
9341 MBEDTLS_MD_SHA256,
9342 MBEDTLS_MD_SHA224,
9343#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009344#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009345 MBEDTLS_MD_SHA1,
9346#endif
9347 MBEDTLS_MD_NONE
9348};
Simon Butcherc97b6972015-12-27 23:48:17 +00009349#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009350
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009351static int ssl_preset_suiteb_ciphersuites[] = {
9352 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9353 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9354 0
9355};
9356
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009357#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009358static int ssl_preset_suiteb_hashes[] = {
9359 MBEDTLS_MD_SHA256,
9360 MBEDTLS_MD_SHA384,
9361 MBEDTLS_MD_NONE
9362};
9363#endif
9364
9365#if defined(MBEDTLS_ECP_C)
9366static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9367 MBEDTLS_ECP_DP_SECP256R1,
9368 MBEDTLS_ECP_DP_SECP384R1,
9369 MBEDTLS_ECP_DP_NONE
9370};
9371#endif
9372
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009373/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009374 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009375 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009376int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009377 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009378{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009379#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009380 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009381#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009382
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009383 /* Use the functions here so that they are covered in tests,
9384 * but otherwise access member directly for efficiency */
9385 mbedtls_ssl_conf_endpoint( conf, endpoint );
9386 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009387
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009388 /*
9389 * Things that are common to all presets
9390 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009391#if defined(MBEDTLS_SSL_CLI_C)
9392 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9393 {
9394 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9395#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9396 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9397#endif
9398 }
9399#endif
9400
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009401#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009402 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009403#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009404
9405#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9406 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9407#endif
9408
9409#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9410 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9411#endif
9412
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009413#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9414 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9415#endif
9416
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009417#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009418 conf->f_cookie_write = ssl_cookie_write_dummy;
9419 conf->f_cookie_check = ssl_cookie_check_dummy;
9420#endif
9421
9422#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9423 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9424#endif
9425
Janos Follath088ce432017-04-10 12:42:31 +01009426#if defined(MBEDTLS_SSL_SRV_C)
9427 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9428#endif
9429
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009430#if defined(MBEDTLS_SSL_PROTO_DTLS)
9431 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9432 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9433#endif
9434
9435#if defined(MBEDTLS_SSL_RENEGOTIATION)
9436 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009437 memset( conf->renego_period, 0x00, 2 );
9438 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009439#endif
9440
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009441#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9442 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9443 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009444 const unsigned char dhm_p[] =
9445 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9446 const unsigned char dhm_g[] =
9447 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9448
Hanno Beckera90658f2017-10-04 15:29:08 +01009449 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9450 dhm_p, sizeof( dhm_p ),
9451 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009452 {
9453 return( ret );
9454 }
9455 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009456#endif
9457
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009458 /*
9459 * Preset-specific defaults
9460 */
9461 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009462 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009463 /*
9464 * NSA Suite B
9465 */
9466 case MBEDTLS_SSL_PRESET_SUITEB:
9467 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9468 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9469 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9470 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9471
9472 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9473 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9474 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9475 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9476 ssl_preset_suiteb_ciphersuites;
9477
9478#if defined(MBEDTLS_X509_CRT_PARSE_C)
9479 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009480#endif
9481
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009482#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009483 conf->sig_hashes = ssl_preset_suiteb_hashes;
9484#endif
9485
9486#if defined(MBEDTLS_ECP_C)
9487 conf->curve_list = ssl_preset_suiteb_curves;
9488#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009489 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009490
9491 /*
9492 * Default
9493 */
9494 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009495 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9496 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9497 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9498 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9499 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9500 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9501 MBEDTLS_SSL_MIN_MINOR_VERSION :
9502 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009503 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9504 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9505
9506#if defined(MBEDTLS_SSL_PROTO_DTLS)
9507 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9508 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9509#endif
9510
9511 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9512 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9513 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9514 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9515 mbedtls_ssl_list_ciphersuites();
9516
9517#if defined(MBEDTLS_X509_CRT_PARSE_C)
9518 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9519#endif
9520
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009521#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009522 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009523#endif
9524
9525#if defined(MBEDTLS_ECP_C)
9526 conf->curve_list = mbedtls_ecp_grp_id_list();
9527#endif
9528
9529#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9530 conf->dhm_min_bitlen = 1024;
9531#endif
9532 }
9533
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009534 return( 0 );
9535}
9536
9537/*
9538 * Free mbedtls_ssl_config
9539 */
9540void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9541{
9542#if defined(MBEDTLS_DHM_C)
9543 mbedtls_mpi_free( &conf->dhm_P );
9544 mbedtls_mpi_free( &conf->dhm_G );
9545#endif
9546
9547#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9548 if( conf->psk != NULL )
9549 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009550 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009551 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009552 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009553 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009554 }
9555
9556 if( conf->psk_identity != NULL )
9557 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009558 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009559 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009560 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009561 conf->psk_identity_len = 0;
9562 }
9563#endif
9564
9565#if defined(MBEDTLS_X509_CRT_PARSE_C)
9566 ssl_key_cert_free( conf->key_cert );
9567#endif
9568
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009569 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009570}
9571
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009572#if defined(MBEDTLS_PK_C) && \
9573 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009574/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009575 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009576 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009577unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009578{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009579#if defined(MBEDTLS_RSA_C)
9580 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9581 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009582#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009583#if defined(MBEDTLS_ECDSA_C)
9584 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9585 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009586#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009587 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009588}
9589
Hanno Becker7e5437a2017-04-28 17:15:26 +01009590unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9591{
9592 switch( type ) {
9593 case MBEDTLS_PK_RSA:
9594 return( MBEDTLS_SSL_SIG_RSA );
9595 case MBEDTLS_PK_ECDSA:
9596 case MBEDTLS_PK_ECKEY:
9597 return( MBEDTLS_SSL_SIG_ECDSA );
9598 default:
9599 return( MBEDTLS_SSL_SIG_ANON );
9600 }
9601}
9602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009603mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009604{
9605 switch( sig )
9606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009607#if defined(MBEDTLS_RSA_C)
9608 case MBEDTLS_SSL_SIG_RSA:
9609 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009610#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009611#if defined(MBEDTLS_ECDSA_C)
9612 case MBEDTLS_SSL_SIG_ECDSA:
9613 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009614#endif
9615 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009616 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009617 }
9618}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009619#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009620
Hanno Becker7e5437a2017-04-28 17:15:26 +01009621#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9622 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9623
9624/* Find an entry in a signature-hash set matching a given hash algorithm. */
9625mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9626 mbedtls_pk_type_t sig_alg )
9627{
9628 switch( sig_alg )
9629 {
9630 case MBEDTLS_PK_RSA:
9631 return( set->rsa );
9632 case MBEDTLS_PK_ECDSA:
9633 return( set->ecdsa );
9634 default:
9635 return( MBEDTLS_MD_NONE );
9636 }
9637}
9638
9639/* Add a signature-hash-pair to a signature-hash set */
9640void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9641 mbedtls_pk_type_t sig_alg,
9642 mbedtls_md_type_t md_alg )
9643{
9644 switch( sig_alg )
9645 {
9646 case MBEDTLS_PK_RSA:
9647 if( set->rsa == MBEDTLS_MD_NONE )
9648 set->rsa = md_alg;
9649 break;
9650
9651 case MBEDTLS_PK_ECDSA:
9652 if( set->ecdsa == MBEDTLS_MD_NONE )
9653 set->ecdsa = md_alg;
9654 break;
9655
9656 default:
9657 break;
9658 }
9659}
9660
9661/* Allow exactly one hash algorithm for each signature. */
9662void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9663 mbedtls_md_type_t md_alg )
9664{
9665 set->rsa = md_alg;
9666 set->ecdsa = md_alg;
9667}
9668
9669#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9670 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9671
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009672/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009673 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009674 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009675mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009676{
9677 switch( hash )
9678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009679#if defined(MBEDTLS_MD5_C)
9680 case MBEDTLS_SSL_HASH_MD5:
9681 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009682#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009683#if defined(MBEDTLS_SHA1_C)
9684 case MBEDTLS_SSL_HASH_SHA1:
9685 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009686#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009687#if defined(MBEDTLS_SHA256_C)
9688 case MBEDTLS_SSL_HASH_SHA224:
9689 return( MBEDTLS_MD_SHA224 );
9690 case MBEDTLS_SSL_HASH_SHA256:
9691 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009692#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009693#if defined(MBEDTLS_SHA512_C)
9694 case MBEDTLS_SSL_HASH_SHA384:
9695 return( MBEDTLS_MD_SHA384 );
9696 case MBEDTLS_SSL_HASH_SHA512:
9697 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009698#endif
9699 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009700 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009701 }
9702}
9703
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009704/*
9705 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9706 */
9707unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9708{
9709 switch( md )
9710 {
9711#if defined(MBEDTLS_MD5_C)
9712 case MBEDTLS_MD_MD5:
9713 return( MBEDTLS_SSL_HASH_MD5 );
9714#endif
9715#if defined(MBEDTLS_SHA1_C)
9716 case MBEDTLS_MD_SHA1:
9717 return( MBEDTLS_SSL_HASH_SHA1 );
9718#endif
9719#if defined(MBEDTLS_SHA256_C)
9720 case MBEDTLS_MD_SHA224:
9721 return( MBEDTLS_SSL_HASH_SHA224 );
9722 case MBEDTLS_MD_SHA256:
9723 return( MBEDTLS_SSL_HASH_SHA256 );
9724#endif
9725#if defined(MBEDTLS_SHA512_C)
9726 case MBEDTLS_MD_SHA384:
9727 return( MBEDTLS_SSL_HASH_SHA384 );
9728 case MBEDTLS_MD_SHA512:
9729 return( MBEDTLS_SSL_HASH_SHA512 );
9730#endif
9731 default:
9732 return( MBEDTLS_SSL_HASH_NONE );
9733 }
9734}
9735
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009736#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009737/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009738 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009739 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009740 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009741int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009742{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009743 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009744
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009745 if( ssl->conf->curve_list == NULL )
9746 return( -1 );
9747
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009748 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009749 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009750 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009751
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009752 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009753}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009754#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009755
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009756#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009757/*
9758 * Check if a hash proposed by the peer is in our list.
9759 * Return 0 if we're willing to use it, -1 otherwise.
9760 */
9761int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9762 mbedtls_md_type_t md )
9763{
9764 const int *cur;
9765
9766 if( ssl->conf->sig_hashes == NULL )
9767 return( -1 );
9768
9769 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9770 if( *cur == (int) md )
9771 return( 0 );
9772
9773 return( -1 );
9774}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009775#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009777#if defined(MBEDTLS_X509_CRT_PARSE_C)
9778int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9779 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009780 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009781 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009782{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009783 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009784#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009785 int usage = 0;
9786#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009787#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009788 const char *ext_oid;
9789 size_t ext_len;
9790#endif
9791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009792#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9793 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009794 ((void) cert);
9795 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009796 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009797#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009799#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9800 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009801 {
9802 /* Server part of the key exchange */
9803 switch( ciphersuite->key_exchange )
9804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009805 case MBEDTLS_KEY_EXCHANGE_RSA:
9806 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009807 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009808 break;
9809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009810 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9811 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9812 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9813 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009814 break;
9815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009816 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9817 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009818 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009819 break;
9820
9821 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822 case MBEDTLS_KEY_EXCHANGE_NONE:
9823 case MBEDTLS_KEY_EXCHANGE_PSK:
9824 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9825 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009826 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009827 usage = 0;
9828 }
9829 }
9830 else
9831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009832 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9833 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009834 }
9835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009836 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009837 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009838 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009839 ret = -1;
9840 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009841#else
9842 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009845#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9846 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009848 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9849 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009850 }
9851 else
9852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009853 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9854 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009855 }
9856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009857 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009858 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009859 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009860 ret = -1;
9861 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009862#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009863
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009864 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009865}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009866#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009867
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009868/*
9869 * Convert version numbers to/from wire format
9870 * and, for DTLS, to/from TLS equivalent.
9871 *
9872 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009873 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009874 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9875 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9876 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009877void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009878 unsigned char ver[2] )
9879{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009880#if defined(MBEDTLS_SSL_PROTO_DTLS)
9881 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009883 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009884 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9885
9886 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9887 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9888 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009889 else
9890#else
9891 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009892#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009893 {
9894 ver[0] = (unsigned char) major;
9895 ver[1] = (unsigned char) minor;
9896 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009897}
9898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009899void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009900 const unsigned char ver[2] )
9901{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009902#if defined(MBEDTLS_SSL_PROTO_DTLS)
9903 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009904 {
9905 *major = 255 - ver[0] + 2;
9906 *minor = 255 - ver[1] + 1;
9907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009908 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009909 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9910 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009911 else
9912#else
9913 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009914#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009915 {
9916 *major = ver[0];
9917 *minor = ver[1];
9918 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009919}
9920
Simon Butcher99000142016-10-13 17:21:01 +01009921int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9922{
9923#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9924 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9925 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9926
9927 switch( md )
9928 {
9929#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9930#if defined(MBEDTLS_MD5_C)
9931 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009932 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009933#endif
9934#if defined(MBEDTLS_SHA1_C)
9935 case MBEDTLS_SSL_HASH_SHA1:
9936 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9937 break;
9938#endif
9939#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9940#if defined(MBEDTLS_SHA512_C)
9941 case MBEDTLS_SSL_HASH_SHA384:
9942 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9943 break;
9944#endif
9945#if defined(MBEDTLS_SHA256_C)
9946 case MBEDTLS_SSL_HASH_SHA256:
9947 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9948 break;
9949#endif
9950 default:
9951 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9952 }
9953
9954 return 0;
9955#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9956 (void) ssl;
9957 (void) md;
9958
9959 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9960#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9961}
9962
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009963#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9964 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9965int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9966 unsigned char *output,
9967 unsigned char *data, size_t data_len )
9968{
9969 int ret = 0;
9970 mbedtls_md5_context mbedtls_md5;
9971 mbedtls_sha1_context mbedtls_sha1;
9972
9973 mbedtls_md5_init( &mbedtls_md5 );
9974 mbedtls_sha1_init( &mbedtls_sha1 );
9975
9976 /*
9977 * digitally-signed struct {
9978 * opaque md5_hash[16];
9979 * opaque sha_hash[20];
9980 * };
9981 *
9982 * md5_hash
9983 * MD5(ClientHello.random + ServerHello.random
9984 * + ServerParams);
9985 * sha_hash
9986 * SHA(ClientHello.random + ServerHello.random
9987 * + ServerParams);
9988 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009989 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009990 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009991 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009992 goto exit;
9993 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009994 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009995 ssl->handshake->randbytes, 64 ) ) != 0 )
9996 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009997 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009998 goto exit;
9999 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010000 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010001 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010002 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010003 goto exit;
10004 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010005 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010006 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010007 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010008 goto exit;
10009 }
10010
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010011 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010012 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010013 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010014 goto exit;
10015 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010016 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010017 ssl->handshake->randbytes, 64 ) ) != 0 )
10018 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010020 goto exit;
10021 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010022 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010023 data_len ) ) != 0 )
10024 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010025 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010026 goto exit;
10027 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010028 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010029 output + 16 ) ) != 0 )
10030 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010031 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010032 goto exit;
10033 }
10034
10035exit:
10036 mbedtls_md5_free( &mbedtls_md5 );
10037 mbedtls_sha1_free( &mbedtls_sha1 );
10038
10039 if( ret != 0 )
10040 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10041 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10042
10043 return( ret );
10044
10045}
10046#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10047 MBEDTLS_SSL_PROTO_TLS1_1 */
10048
10049#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10050 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10051int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010052 unsigned char *hash, size_t *hashlen,
10053 unsigned char *data, size_t data_len,
10054 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010055{
10056 int ret = 0;
10057 mbedtls_md_context_t ctx;
10058 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010059 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010060
10061 mbedtls_md_init( &ctx );
10062
10063 /*
10064 * digitally-signed struct {
10065 * opaque client_random[32];
10066 * opaque server_random[32];
10067 * ServerDHParams params;
10068 * };
10069 */
10070 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10071 {
10072 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10073 goto exit;
10074 }
10075 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10076 {
10077 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10078 goto exit;
10079 }
10080 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10081 {
10082 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10083 goto exit;
10084 }
10085 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10086 {
10087 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10088 goto exit;
10089 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010090 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010091 {
10092 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10093 goto exit;
10094 }
10095
10096exit:
10097 mbedtls_md_free( &ctx );
10098
10099 if( ret != 0 )
10100 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10101 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10102
10103 return( ret );
10104}
10105#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10106 MBEDTLS_SSL_PROTO_TLS1_2 */
10107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010108#endif /* MBEDTLS_SSL_TLS_C */