blob: fca03fc833664049172b4c802ef050d91e1eecb8 [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 Kurek2f760752019-01-28 08:08:15 -0500510 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500511 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500512
Andrzej Kurekc929a822019-01-14 03:51:11 -0500513 if( md_type == MBEDTLS_MD_SHA384 )
514 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
515 else
516 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
517
Andrzej Kurek2f760752019-01-28 08:08:15 -0500518 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500519 psa_key_policy_set_usage( &policy,
520 PSA_KEY_USAGE_DERIVE,
521 alg );
522 status = psa_set_key_policy( master_slot, &policy );
523 if( status != PSA_SUCCESS )
524 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
525
526 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
527 if( status != PSA_SUCCESS )
528 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
529
530 status = psa_key_derivation( &generator,
531 master_slot, alg,
532 random, rlen,
533 (unsigned char const *) label,
534 (size_t) strlen( label ),
535 dlen );
536 if( status != PSA_SUCCESS )
537 {
538 psa_generator_abort( &generator );
539 psa_destroy_key( master_slot );
540 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
541 }
542
543 status = psa_generator_read( &generator, dstbuf, dlen );
544 if( status != PSA_SUCCESS )
545 {
546 psa_generator_abort( &generator );
547 psa_destroy_key( master_slot );
548 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
549 }
550
551 status = psa_generator_abort( &generator );
552 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500553 {
554 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500555 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500556 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500557
558 status = psa_destroy_key( master_slot );
559 if( status != PSA_SUCCESS )
560 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
561
Andrzej Kurek33171262019-01-15 03:25:18 -0500562 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500563}
564
565#else /* MBEDTLS_USE_PSA_CRYPTO */
566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100568 const unsigned char *secret, size_t slen,
569 const char *label,
570 const unsigned char *random, size_t rlen,
571 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000572{
573 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100574 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000575 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
577 const mbedtls_md_info_t *md_info;
578 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100579 int ret;
580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
584 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100587
588 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000590
591 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100592 memcpy( tmp + md_len, label, nb );
593 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000594 nb += rlen;
595
596 /*
597 * Compute P_<hash>(secret, label + random)[0..dlen]
598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100600 return( ret );
601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
603 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
604 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100605
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100606 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_md_hmac_reset ( &md_ctx );
609 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
610 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 mbedtls_md_hmac_reset ( &md_ctx );
613 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
614 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000615
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100616 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000617
618 for( j = 0; j < k; j++ )
619 dstbuf[i + j] = h_i[j];
620 }
621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100623
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500624 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
625 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000626
627 return( 0 );
628}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500629#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100631static int tls_prf_sha256( const unsigned char *secret, size_t slen,
632 const char *label,
633 const unsigned char *random, size_t rlen,
634 unsigned char *dstbuf, size_t dlen )
635{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100637 label, random, rlen, dstbuf, dlen ) );
638}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200642static int tls_prf_sha384( const unsigned char *secret, size_t slen,
643 const char *label,
644 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000645 unsigned char *dstbuf, size_t dlen )
646{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100648 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000649}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#endif /* MBEDTLS_SHA512_C */
651#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
656 defined(MBEDTLS_SSL_PROTO_TLS1_1)
657static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200658#endif
Paul Bakker380da532012-04-18 16:10:25 +0000659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#if defined(MBEDTLS_SSL_PROTO_SSL3)
661static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
662static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200663#endif
664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
666static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
667static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200668#endif
669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
671#if defined(MBEDTLS_SHA256_C)
672static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
673static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
674static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200675#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677#if defined(MBEDTLS_SHA512_C)
678static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
679static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
680static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100681#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000683
Hanno Becker7d0a5692018-10-23 15:26:22 +0100684#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
685 defined(MBEDTLS_USE_PSA_CRYPTO)
686static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
687{
688 if( ssl->conf->f_psk != NULL )
689 {
690 /* If we've used a callback to select the PSK,
691 * the static configuration is irrelevant. */
692 if( ssl->handshake->psk_opaque != 0 )
693 return( 1 );
694
695 return( 0 );
696 }
697
698 if( ssl->conf->psk_opaque != 0 )
699 return( 1 );
700
701 return( 0 );
702}
703#endif /* MBEDTLS_USE_PSA_CRYPTO &&
704 MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000707{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200708 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000709#if defined(MBEDTLS_USE_PSA_CRYPTO)
710 int psa_fallthrough;
711#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000713 unsigned char keyblk[256];
714 unsigned char *key1;
715 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100716 unsigned char *mac_enc;
717 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000718 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200719 size_t iv_copy_len;
Hanno Beckerf704bef2018-11-16 15:21:18 +0000720 size_t taglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 const mbedtls_cipher_info_t *cipher_info;
722 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100723
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000724 /* cf. RFC 5246, Section 8.1:
725 * "The master secret is always exactly 48 bytes in length." */
726 size_t const master_secret_len = 48;
727
Hanno Becker35b23c72018-10-23 12:10:41 +0100728#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
729 unsigned char session_hash[48];
730#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_ssl_session *session = ssl->session_negotiate;
733 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
734 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100739 if( cipher_info == NULL )
740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100742 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100744 }
745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100747 if( md_info == NULL )
748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100750 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100752 }
753
Paul Bakker5121ce52009-01-03 21:22:43 +0000754 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000755 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000756 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757#if defined(MBEDTLS_SSL_PROTO_SSL3)
758 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000759 {
Paul Bakker48916f92012-09-16 19:57:18 +0000760 handshake->tls_prf = ssl3_prf;
761 handshake->calc_verify = ssl_calc_verify_ssl;
762 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000763 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200764 else
765#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
767 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000768 {
Paul Bakker48916f92012-09-16 19:57:18 +0000769 handshake->tls_prf = tls1_prf;
770 handshake->calc_verify = ssl_calc_verify_tls;
771 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000772 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200773 else
774#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
776#if defined(MBEDTLS_SHA512_C)
777 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
778 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000779 {
Paul Bakker48916f92012-09-16 19:57:18 +0000780 handshake->tls_prf = tls_prf_sha384;
781 handshake->calc_verify = ssl_calc_verify_tls_sha384;
782 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000783 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000784 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200785#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786#if defined(MBEDTLS_SHA256_C)
787 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000788 {
Paul Bakker48916f92012-09-16 19:57:18 +0000789 handshake->tls_prf = tls_prf_sha256;
790 handshake->calc_verify = ssl_calc_verify_tls_sha256;
791 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000792 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200793 else
794#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
798 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200799 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000800
801 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000802 * SSLv3:
803 * master =
804 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
805 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
806 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200807 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200808 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000809 * master = PRF( premaster, "master secret", randbytes )[0..47]
810 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100811 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000812 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100813 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
814 }
815 else
816 {
817 /* The label for the KDF used for key expansion.
818 * This is either "master secret" or "extended master secret"
819 * depending on whether the Extended Master Secret extension
820 * is used. */
821 char const *lbl = "master secret";
822
823 /* The salt for the KDF used for key expansion.
824 * - If the Extended Master Secret extension is not used,
825 * this is ClientHello.Random + ServerHello.Random
826 * (see Sect. 8.1 in RFC 5246).
827 * - If the Extended Master Secret extension is used,
828 * this is the transcript of the handshake so far.
829 * (see Sect. 4 in RFC 7627). */
830 unsigned char const *salt = handshake->randbytes;
831 size_t salt_len = 64;
832
833#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
834 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
835 ssl->transform_negotiate->ciphersuite_info;
836 mbedtls_md_type_t const md_type = ciphersuite_info->mac;
837#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Paul Bakker5121ce52009-01-03 21:22:43 +0000838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
840 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200843
Hanno Becker35b23c72018-10-23 12:10:41 +0100844 lbl = "extended master secret";
845 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200846 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
848 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850#if defined(MBEDTLS_SHA512_C)
Hanno Becker35b23c72018-10-23 12:10:41 +0100851 if( md_type == MBEDTLS_MD_SHA384 )
852 salt_len = 48;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200853 else
Hanno Becker35b23c72018-10-23 12:10:41 +0100854#endif /* MBEDTLS_SHA512_C */
855 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200856 }
857 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100859 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200860
Hanno Becker35b23c72018-10-23 12:10:41 +0100861 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200862 }
Hanno Becker35b23c72018-10-23 12:10:41 +0100863#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
864
Hanno Becker7d0a5692018-10-23 15:26:22 +0100865#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
866 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
867 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
868 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
869 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100870 {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100871 /* Perform PSK-to-MS expansion in a single step. */
872 psa_status_t status;
873 psa_algorithm_t alg;
874 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500875 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100876
877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
878
879 psk = ssl->conf->psk_opaque;
880 if( ssl->handshake->psk_opaque != 0 )
881 psk = ssl->handshake->psk_opaque;
882
883 if( md_type == MBEDTLS_MD_SHA384 )
884 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
885 else
886 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
887
888 status = psa_key_derivation( &generator, psk, alg,
889 salt, salt_len,
890 (unsigned char const *) lbl,
891 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000892 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100893 if( status != PSA_SUCCESS )
894 {
895 psa_generator_abort( &generator );
896 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
897 }
898
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000899 status = psa_generator_read( &generator, session->master,
900 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100901 if( status != PSA_SUCCESS )
902 {
903 psa_generator_abort( &generator );
904 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
905 }
906
907 status = psa_generator_abort( &generator );
908 if( status != PSA_SUCCESS )
909 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100910 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100911 else
912#endif
913 {
914 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
915 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000916 session->master,
917 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100918 if( ret != 0 )
919 {
920 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
921 return( ret );
922 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200923
Hanno Becker7d0a5692018-10-23 15:26:22 +0100924 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
925 handshake->premaster,
926 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +0100927
Hanno Becker7d0a5692018-10-23 15:26:22 +0100928 mbedtls_platform_zeroize( handshake->premaster,
929 sizeof(handshake->premaster) );
930 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000931 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000932
933 /*
934 * Swap the client and server random values.
935 */
Paul Bakker48916f92012-09-16 19:57:18 +0000936 memcpy( tmp, handshake->randbytes, 64 );
937 memcpy( handshake->randbytes, tmp + 32, 32 );
938 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500939 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000940
941 /*
942 * SSLv3:
943 * key block =
944 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
945 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
946 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
947 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
948 * ...
949 *
950 * TLSv1:
951 * key block = PRF( master, "key expansion", randbytes )
952 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100953 ret = handshake->tls_prf( session->master, 48, "key expansion",
954 handshake->randbytes, 64, keyblk, 256 );
955 if( ret != 0 )
956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100958 return( ret );
959 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
962 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
963 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
964 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
965 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000966
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500967 mbedtls_platform_zeroize( handshake->randbytes,
968 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000969
970 /*
971 * Determine the appropriate key, IV and MAC length.
972 */
Paul Bakker68884e32013-01-07 18:20:04 +0100973
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200974 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200977 cipher_info->mode == MBEDTLS_MODE_CCM ||
978 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000979 {
Hanno Beckerf704bef2018-11-16 15:21:18 +0000980 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200981
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200982 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000983 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200984
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200985 /* All modes haves 96-bit IVs;
986 * GCM and CCM has 4 implicit and 8 explicit bytes
987 * ChachaPoly has all 12 bytes implicit
988 */
Paul Bakker68884e32013-01-07 18:20:04 +0100989 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200990 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
991 transform->fixed_ivlen = 12;
992 else
993 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200994
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200995 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
996 taglen = transform->ciphersuite_info->flags &
997 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
998
999
1000 /* Minimum length of encrypted record */
1001 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
1002 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001003 }
1004 else
1005 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001006 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1008 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001011 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001012 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001013
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001014 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001015 mac_key_len = mbedtls_md_get_size( md_info );
1016 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001019 /*
1020 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1021 * (rfc 6066 page 13 or rfc 2104 section 4),
1022 * so we only need to adjust the length here.
1023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001027
1028#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1029 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001030 * HMAC implementation which also truncates the key
1031 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001032 mac_key_len = transform->maclen;
1033#endif
1034 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001036
1037 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001038 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001039
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001040 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001042 transform->minlen = transform->maclen;
1043 else
Paul Bakker68884e32013-01-07 18:20:04 +01001044 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001045 /*
1046 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001047 * 1. if EtM is in use: one block plus MAC
1048 * otherwise: * first multiple of blocklen greater than maclen
1049 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1052 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001053 {
1054 transform->minlen = transform->maclen
1055 + cipher_info->block_size;
1056 }
1057 else
1058#endif
1059 {
1060 transform->minlen = transform->maclen
1061 + cipher_info->block_size
1062 - transform->maclen % cipher_info->block_size;
1063 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1066 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1067 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001068 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001069 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001070#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1072 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1073 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001074 {
1075 transform->minlen += transform->ivlen;
1076 }
1077 else
1078#endif
1079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1081 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001082 }
Paul Bakker68884e32013-01-07 18:20:04 +01001083 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001084 }
1085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001087 transform->keylen, transform->minlen, transform->ivlen,
1088 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001089
1090 /*
1091 * Finally setup the cipher contexts, IVs and MAC secrets.
1092 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001094 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001095 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001096 key1 = keyblk + mac_key_len * 2;
1097 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001098
Paul Bakker68884e32013-01-07 18:20:04 +01001099 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001100 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001101
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001102 /*
1103 * This is not used in TLS v1.1.
1104 */
Paul Bakker48916f92012-09-16 19:57:18 +00001105 iv_copy_len = ( transform->fixed_ivlen ) ?
1106 transform->fixed_ivlen : transform->ivlen;
1107 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
1108 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001109 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 }
1111 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112#endif /* MBEDTLS_SSL_CLI_C */
1113#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001114 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001115 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001116 key1 = keyblk + mac_key_len * 2 + transform->keylen;
1117 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001118
Hanno Becker81c7b182017-11-09 18:39:33 +00001119 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001120 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001121
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001122 /*
1123 * This is not used in TLS v1.1.
1124 */
Paul Bakker48916f92012-09-16 19:57:18 +00001125 iv_copy_len = ( transform->fixed_ivlen ) ?
1126 transform->fixed_ivlen : transform->ivlen;
1127 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
1128 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001129 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001131 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1135 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001136 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138#if defined(MBEDTLS_SSL_PROTO_SSL3)
1139 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001140 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001141 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1144 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001145 }
1146
Hanno Becker81c7b182017-11-09 18:39:33 +00001147 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1148 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001149 }
1150 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1152#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1153 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1154 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001155 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001156 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1157 For AEAD-based ciphersuites, there is nothing to do here. */
1158 if( mac_key_len != 0 )
1159 {
1160 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1161 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1162 }
Paul Bakker68884e32013-01-07 18:20:04 +01001163 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001164 else
1165#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1168 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001169 }
Paul Bakker68884e32013-01-07 18:20:04 +01001170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1172 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001173 {
1174 int ret = 0;
1175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001179 transform->iv_enc, transform->iv_dec,
1180 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001181 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001182 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1185 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001186 }
1187 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001189
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001190#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1191 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001192 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001193 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1194 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001195 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001196 iv_copy_len );
1197 }
1198#endif
1199
Hanno Beckerf704bef2018-11-16 15:21:18 +00001200#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001201
1202 /* Only use PSA-based ciphers for TLS-1.2.
1203 * That's relevant at least for TLS-1.0, where
1204 * we assume that mbedtls_cipher_crypt() updates
1205 * the structure field for the IV, which the PSA-based
1206 * implementation currently doesn't. */
1207#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1208 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001209 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001210 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
1211 cipher_info, taglen );
1212 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1213 {
1214 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1215 return( ret );
1216 }
1217
1218 if( ret == 0 )
1219 {
1220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based encryption cipher context" ) );
1221 psa_fallthrough = 0;
1222 }
1223 else
1224 {
1225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1226 psa_fallthrough = 1;
1227 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001228 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001229 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001230 psa_fallthrough = 1;
1231#else
1232 psa_fallthrough = 1;
1233#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001234
Hanno Beckercb1cc802018-11-17 22:27:38 +00001235 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001236#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001237 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001238 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001239 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001240 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001241 return( ret );
1242 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001243
Hanno Beckerf704bef2018-11-16 15:21:18 +00001244#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001245 /* Only use PSA-based ciphers for TLS-1.2.
1246 * That's relevant at least for TLS-1.0, where
1247 * we assume that mbedtls_cipher_crypt() updates
1248 * the structure field for the IV, which the PSA-based
1249 * implementation currently doesn't. */
1250#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1251 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001252 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001253 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
1254 cipher_info, taglen );
1255 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1256 {
1257 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1258 return( ret );
1259 }
1260
1261 if( ret == 0 )
1262 {
1263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based decryption cipher context" ) );
1264 psa_fallthrough = 0;
1265 }
1266 else
1267 {
1268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1269 psa_fallthrough = 1;
1270 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001271 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001272 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001273 psa_fallthrough = 1;
1274#else
1275 psa_fallthrough = 1;
1276#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001277
Hanno Beckercb1cc802018-11-17 22:27:38 +00001278 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001279#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001280 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001281 cipher_info ) ) != 0 )
1282 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001283 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001284 return( ret );
1285 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001288 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001292 return( ret );
1293 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001296 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001300 return( ret );
1301 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303#if defined(MBEDTLS_CIPHER_MODE_CBC)
1304 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1307 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001310 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001311 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1314 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001317 return( ret );
1318 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001321
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001322 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001325 // Initialize compression
1326 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001328 {
Paul Bakker16770332013-10-11 09:59:44 +02001329 if( ssl->compress_buf == NULL )
1330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001332 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001333 if( ssl->compress_buf == NULL )
1334 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001336 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001337 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001338 }
1339 }
1340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001342
Paul Bakker48916f92012-09-16 19:57:18 +00001343 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1344 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001345
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001346 if( deflateInit( &transform->ctx_deflate,
1347 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001348 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1351 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001352 }
1353 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001357
1358 return( 0 );
1359}
1360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#if defined(MBEDTLS_SSL_PROTO_SSL3)
1362void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001363{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001364 mbedtls_md5_context md5;
1365 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 unsigned char pad_1[48];
1367 unsigned char pad_2[48];
1368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001371 mbedtls_md5_init( &md5 );
1372 mbedtls_sha1_init( &sha1 );
1373
1374 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1375 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376
Paul Bakker380da532012-04-18 16:10:25 +00001377 memset( pad_1, 0x36, 48 );
1378 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001379
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001380 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1381 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1382 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001383
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001384 mbedtls_md5_starts_ret( &md5 );
1385 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1386 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1387 mbedtls_md5_update_ret( &md5, hash, 16 );
1388 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001389
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001390 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1391 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1392 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001393
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001394 mbedtls_sha1_starts_ret( &sha1 );
1395 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1396 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1397 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1398 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001402
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001403 mbedtls_md5_free( &md5 );
1404 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001405
Paul Bakker380da532012-04-18 16:10:25 +00001406 return;
1407}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1411void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001412{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001413 mbedtls_md5_context md5;
1414 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001417
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001418 mbedtls_md5_init( &md5 );
1419 mbedtls_sha1_init( &sha1 );
1420
1421 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1422 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001423
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001424 mbedtls_md5_finish_ret( &md5, hash );
1425 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001429
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001430 mbedtls_md5_free( &md5 );
1431 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001432
Paul Bakker380da532012-04-18 16:10:25 +00001433 return;
1434}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1438#if defined(MBEDTLS_SHA256_C)
1439void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001440{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001441 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001442
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001443 mbedtls_sha256_init( &sha256 );
1444
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001446
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001447 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001448 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001452
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001453 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001454
Paul Bakker380da532012-04-18 16:10:25 +00001455 return;
1456}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#if defined(MBEDTLS_SHA512_C)
1460void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001461{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001462 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001463
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001464 mbedtls_sha512_init( &sha512 );
1465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001467
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001468 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001469 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001473
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001474 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001475
Paul Bakker5121ce52009-01-03 21:22:43 +00001476 return;
1477}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478#endif /* MBEDTLS_SHA512_C */
1479#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1482int 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 +02001483{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001484 unsigned char *p = ssl->handshake->premaster;
1485 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001486 const unsigned char *psk = ssl->conf->psk;
1487 size_t psk_len = ssl->conf->psk_len;
1488
1489 /* If the psk callback was called, use its result */
1490 if( ssl->handshake->psk != NULL )
1491 {
1492 psk = ssl->handshake->psk;
1493 psk_len = ssl->handshake->psk_len;
1494 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001495
1496 /*
1497 * PMS = struct {
1498 * opaque other_secret<0..2^16-1>;
1499 * opaque psk<0..2^16-1>;
1500 * };
1501 * with "other_secret" depending on the particular key exchange
1502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1504 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001505 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001506 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001508
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001509 *(p++) = (unsigned char)( psk_len >> 8 );
1510 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001511
1512 if( end < p || (size_t)( end - p ) < psk_len )
1513 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1514
1515 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001516 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001517 }
1518 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1520#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1521 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001522 {
1523 /*
1524 * other_secret already set by the ClientKeyExchange message,
1525 * and is 48 bytes long
1526 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001527 if( end - p < 2 )
1528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1529
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001530 *p++ = 0;
1531 *p++ = 48;
1532 p += 48;
1533 }
1534 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1536#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1537 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001538 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001539 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001540 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001541
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001542 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001544 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001545 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001548 return( ret );
1549 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001550 *(p++) = (unsigned char)( len >> 8 );
1551 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001552 p += len;
1553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001555 }
1556 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1558#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1559 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001560 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001561 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001562 size_t zlen;
1563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001565 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001566 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001569 return( ret );
1570 }
1571
1572 *(p++) = (unsigned char)( zlen >> 8 );
1573 *(p++) = (unsigned char)( zlen );
1574 p += zlen;
1575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001577 }
1578 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1582 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001583 }
1584
1585 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001586 if( end - p < 2 )
1587 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001588
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001589 *(p++) = (unsigned char)( psk_len >> 8 );
1590 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001591
1592 if( end < p || (size_t)( end - p ) < psk_len )
1593 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1594
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001595 memcpy( p, psk, psk_len );
1596 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001597
1598 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1599
1600 return( 0 );
1601}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001605/*
1606 * SSLv3.0 MAC functions
1607 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001608#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001609static void ssl_mac( mbedtls_md_context_t *md_ctx,
1610 const unsigned char *secret,
1611 const unsigned char *buf, size_t len,
1612 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001613 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001614{
1615 unsigned char header[11];
1616 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001617 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1619 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001620
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001621 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001623 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001624 else
Paul Bakker68884e32013-01-07 18:20:04 +01001625 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001626
1627 memcpy( header, ctr, 8 );
1628 header[ 8] = (unsigned char) type;
1629 header[ 9] = (unsigned char)( len >> 8 );
1630 header[10] = (unsigned char)( len );
1631
Paul Bakker68884e32013-01-07 18:20:04 +01001632 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 mbedtls_md_starts( md_ctx );
1634 mbedtls_md_update( md_ctx, secret, md_size );
1635 mbedtls_md_update( md_ctx, padding, padlen );
1636 mbedtls_md_update( md_ctx, header, 11 );
1637 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001638 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001639
Paul Bakker68884e32013-01-07 18:20:04 +01001640 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 mbedtls_md_starts( md_ctx );
1642 mbedtls_md_update( md_ctx, secret, md_size );
1643 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001644 mbedtls_md_update( md_ctx, out, md_size );
1645 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001646}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1650 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001651 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001652#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001653#endif
1654
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001655/* The function below is only used in the Lucky 13 counter-measure in
1656 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1657#if defined(SSL_SOME_MODES_USE_MAC) && \
1658 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1659 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1660 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1661/* This function makes sure every byte in the memory region is accessed
1662 * (in ascending addresses order) */
1663static void ssl_read_memory( unsigned char *p, size_t len )
1664{
1665 unsigned char acc = 0;
1666 volatile unsigned char force;
1667
1668 for( ; len != 0; p++, len-- )
1669 acc ^= *p;
1670
1671 force = acc;
1672 (void) force;
1673}
1674#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1675
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001676/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001677 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001680{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001682 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001685
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001686 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1689 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001690 }
1691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001695 ssl->out_msg, ssl->out_msglen );
1696
Paul Bakker5121ce52009-01-03 21:22:43 +00001697 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001698 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001699 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001700#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 if( mode == MBEDTLS_MODE_STREAM ||
1702 ( mode == MBEDTLS_MODE_CBC
1703#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1704 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001705#endif
1706 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708#if defined(MBEDTLS_SSL_PROTO_SSL3)
1709 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001710 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001711 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001712
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001713 ssl_mac( &ssl->transform_out->md_ctx_enc,
1714 ssl->transform_out->mac_enc,
1715 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001716 ssl->out_ctr, ssl->out_msgtype,
1717 mac );
1718
1719 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001720 }
1721 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001722#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1724 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1725 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001726 {
Hanno Becker992b6872017-11-09 18:57:39 +00001727 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1730 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1731 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1732 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001733 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001734 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001736
1737 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001738 }
1739 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001740#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1743 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001744 }
1745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001747 ssl->out_msg + ssl->out_msglen,
1748 ssl->transform_out->maclen );
1749
1750 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001751 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001752 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001753#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001754
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001755 /*
1756 * Encrypt
1757 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1759 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001760 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001761 int ret;
1762 size_t olen = 0;
1763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001765 "including %d bytes of padding",
1766 ssl->out_msglen, 0 ) );
1767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001769 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001770 ssl->transform_out->ivlen,
1771 ssl->out_msg, ssl->out_msglen,
1772 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001775 return( ret );
1776 }
1777
1778 if( ssl->out_msglen != olen )
1779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1781 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001782 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001783 }
Paul Bakker68884e32013-01-07 18:20:04 +01001784 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001786#if defined(MBEDTLS_GCM_C) || \
1787 defined(MBEDTLS_CCM_C) || \
1788 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001790 mode == MBEDTLS_MODE_CCM ||
1791 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001792 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001793 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001794 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001795 unsigned char *enc_msg;
1796 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001797 unsigned char iv[12];
1798 mbedtls_ssl_transform *transform = ssl->transform_out;
1799 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001801 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001802
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001803 /*
1804 * Prepare additional authenticated data
1805 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001806 memcpy( add_data, ssl->out_ctr, 8 );
1807 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001809 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001810 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1811 add_data[12] = ssl->out_msglen & 0xFF;
1812
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001813 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001814
Paul Bakker68884e32013-01-07 18:20:04 +01001815 /*
1816 * Generate IV
1817 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001818 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1819 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001820 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001821 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1822 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1823 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1824
1825 }
1826 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1827 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001828 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001829 unsigned char i;
1830
1831 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1832
1833 for( i = 0; i < 8; i++ )
1834 iv[i+4] ^= ssl->out_ctr[i];
1835 }
1836 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001837 {
1838 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1840 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001841 }
1842
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001843 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1844 iv, transform->ivlen );
1845 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1846 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001847
Paul Bakker68884e32013-01-07 18:20:04 +01001848 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001849 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001850 */
1851 enc_msg = ssl->out_msg;
1852 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001853 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001856 "including 0 bytes of padding",
1857 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001858
Paul Bakker68884e32013-01-07 18:20:04 +01001859 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001860 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001861 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001862 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1863 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001864 add_data, 13,
1865 enc_msg, enc_msglen,
1866 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001867 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001870 return( ret );
1871 }
1872
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001873 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1876 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001877 }
1878
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001879 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001880 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001883 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001884 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1886#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001887 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001889 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001890 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001891 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001892 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001893
Paul Bakker48916f92012-09-16 19:57:18 +00001894 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1895 ssl->transform_out->ivlen;
1896 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001897 padlen = 0;
1898
1899 for( i = 0; i <= padlen; i++ )
1900 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1901
1902 ssl->out_msglen += padlen + 1;
1903
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001904 enc_msglen = ssl->out_msglen;
1905 enc_msg = ssl->out_msg;
1906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001908 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001909 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1910 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001911 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001913 {
1914 /*
1915 * Generate IV
1916 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001917 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001918 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001919 if( ret != 0 )
1920 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001921
Paul Bakker92be97b2013-01-02 17:30:03 +01001922 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001923 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001924
1925 /*
1926 * Fix pointer positions and message length with added IV
1927 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001928 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001929 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001930 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001931 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001935 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001936 ssl->out_msglen, ssl->transform_out->ivlen,
1937 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001940 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001941 ssl->transform_out->ivlen,
1942 enc_msg, enc_msglen,
1943 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001946 return( ret );
1947 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001948
Paul Bakkercca5b812013-08-31 17:40:26 +02001949 if( enc_msglen != olen )
1950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1952 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001953 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1956 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001957 {
1958 /*
1959 * Save IV in SSL3 and TLS1
1960 */
1961 memcpy( ssl->transform_out->iv_enc,
1962 ssl->transform_out->cipher_ctx_enc.iv,
1963 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001964 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001965#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001968 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001969 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001970 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1971
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001972 /*
1973 * MAC(MAC_write_key, seq_num +
1974 * TLSCipherText.type +
1975 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001976 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001977 * IV + // except for TLS 1.0
1978 * ENC(content + padding + padding_length));
1979 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001980 unsigned char pseudo_hdr[13];
1981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001983
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001984 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1985 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001986 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1987 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1992 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001993 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00001994 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001996
Hanno Becker3d8c9072018-01-05 16:24:22 +00001997 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1998 ssl->transform_out->maclen );
1999
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002000 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002001 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002002 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002004 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002005 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002007 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2010 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002011 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002012
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002013 /* Make extra sure authentication was performed, exactly once */
2014 if( auth_done != 1 )
2015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2017 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002018 }
2019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002021
2022 return( 0 );
2023}
2024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002026{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002028 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002029#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002030 size_t padlen = 0, correct = 1;
2031#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002034
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002035 if( ssl->session_in == NULL || ssl->transform_in == NULL )
2036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2038 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002039 }
2040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002042
Paul Bakker48916f92012-09-16 19:57:18 +00002043 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00002046 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002048 }
2049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2051 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002052 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002053 int ret;
2054 size_t olen = 0;
2055
Paul Bakker68884e32013-01-07 18:20:04 +01002056 padlen = 0;
2057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002059 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002060 ssl->transform_in->ivlen,
2061 ssl->in_msg, ssl->in_msglen,
2062 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002065 return( ret );
2066 }
2067
2068 if( ssl->in_msglen != olen )
2069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2071 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002072 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002073 }
Paul Bakker68884e32013-01-07 18:20:04 +01002074 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002076#if defined(MBEDTLS_GCM_C) || \
2077 defined(MBEDTLS_CCM_C) || \
2078 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002080 mode == MBEDTLS_MODE_CCM ||
2081 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002082 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002083 int ret;
2084 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002085 unsigned char *dec_msg;
2086 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002087 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002088 unsigned char iv[12];
2089 mbedtls_ssl_transform *transform = ssl->transform_in;
2090 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002092 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002093
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002094 /*
2095 * Compute and update sizes
2096 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002097 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002100 "+ taglen (%d)", ssl->in_msglen,
2101 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002103 }
2104 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
2105
Paul Bakker68884e32013-01-07 18:20:04 +01002106 dec_msg = ssl->in_msg;
2107 dec_msg_result = ssl->in_msg;
2108 ssl->in_msglen = dec_msglen;
2109
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002110 /*
2111 * Prepare additional authenticated data
2112 */
Paul Bakker68884e32013-01-07 18:20:04 +01002113 memcpy( add_data, ssl->in_ctr, 8 );
2114 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002116 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002117 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2118 add_data[12] = ssl->in_msglen & 0xFF;
2119
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002120 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002121
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002122 /*
2123 * Prepare IV
2124 */
2125 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2126 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002127 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002128 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2129 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002130
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002131 }
2132 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2133 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002134 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002135 unsigned char i;
2136
2137 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2138
2139 for( i = 0; i < 8; i++ )
2140 iv[i+4] ^= ssl->in_ctr[i];
2141 }
2142 else
2143 {
2144 /* Reminder if we ever add an AEAD mode with a different size */
2145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2146 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2147 }
2148
2149 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002151
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002152 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002153 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002156 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002157 add_data, 13,
2158 dec_msg, dec_msglen,
2159 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002160 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2165 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002166
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002167 return( ret );
2168 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002169 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002170
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002171 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2174 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002175 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002176 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002177 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2179#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002180 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002182 {
Paul Bakker45829992013-01-03 14:52:21 +01002183 /*
2184 * Decrypt and check the padding
2185 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002186 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002187 unsigned char *dec_msg;
2188 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002189 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002190 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002191 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002192
Paul Bakker5121ce52009-01-03 21:22:43 +00002193 /*
Paul Bakker45829992013-01-03 14:52:21 +01002194 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002195 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2197 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002198 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002199#endif
Paul Bakker45829992013-01-03 14:52:21 +01002200
2201 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2202 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002205 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2206 ssl->transform_in->ivlen,
2207 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002209 }
2210
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002211 dec_msglen = ssl->in_msglen;
2212 dec_msg = ssl->in_msg;
2213 dec_msg_result = ssl->in_msg;
2214
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002215 /*
2216 * Authenticate before decrypt if enabled
2217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2219 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002220 {
Hanno Becker992b6872017-11-09 18:57:39 +00002221 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002222 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002225
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002226 dec_msglen -= ssl->transform_in->maclen;
2227 ssl->in_msglen -= ssl->transform_in->maclen;
2228
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002229 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2230 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2231 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2232 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2237 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002238 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002239 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002243 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002244 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002245 ssl->transform_in->maclen );
2246
Hanno Becker992b6872017-11-09 18:57:39 +00002247 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2248 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002253 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002254 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002255 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002257
2258 /*
2259 * Check length sanity
2260 */
2261 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002264 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002266 }
2267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002269 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002270 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002273 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002274 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002275 dec_msglen -= ssl->transform_in->ivlen;
2276 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002277
Paul Bakker48916f92012-09-16 19:57:18 +00002278 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002279 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002280 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002284 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002285 ssl->transform_in->ivlen,
2286 dec_msg, dec_msglen,
2287 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002290 return( ret );
2291 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002292
Paul Bakkercca5b812013-08-31 17:40:26 +02002293 if( dec_msglen != olen )
2294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2296 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002297 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2300 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002301 {
2302 /*
2303 * Save IV in SSL3 and TLS1
2304 */
2305 memcpy( ssl->transform_in->iv_dec,
2306 ssl->transform_in->cipher_ctx_dec.iv,
2307 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002308 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002309#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002310
2311 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002312
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002313 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002314 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316#if defined(MBEDTLS_SSL_DEBUG_ALL)
2317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002318 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002319#endif
Paul Bakker45829992013-01-03 14:52:21 +01002320 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002321 correct = 0;
2322 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324#if defined(MBEDTLS_SSL_PROTO_SSL3)
2325 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002326 {
Paul Bakker48916f92012-09-16 19:57:18 +00002327 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329#if defined(MBEDTLS_SSL_DEBUG_ALL)
2330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002331 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002332 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002333#endif
Paul Bakker45829992013-01-03 14:52:21 +01002334 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002335 }
2336 }
2337 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2339#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2340 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2341 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002342 {
2343 /*
Paul Bakker45829992013-01-03 14:52:21 +01002344 * TLSv1+: always check the padding up to the first failure
2345 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002346 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002347 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002348 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002349 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002350
Paul Bakker956c9e02013-12-19 14:42:28 +01002351 /*
2352 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002353 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002354 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002355 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002356 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002357 *
2358 * In both cases we reset padding_idx to a safe value (0) to
2359 * prevent out-of-buffer reads.
2360 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002361 correct &= ( padlen <= ssl->in_msglen );
2362 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002363 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002364
2365 padding_idx *= correct;
2366
Angus Grattonb512bc12018-06-19 15:57:50 +10002367 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002368 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002369 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002370 pad_count += real_count *
2371 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2372 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002373
2374 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002377 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002379#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002380 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002381 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002382 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2384 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2387 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002388 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002389
2390 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002392 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002394 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2397 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002398 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002399
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002400#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002402 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002403#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002404
2405 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002406 * Authenticate if not done yet.
2407 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002409#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002410 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002411 {
Hanno Becker992b6872017-11-09 18:57:39 +00002412 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002413
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002414 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002415
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002416 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2417 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419#if defined(MBEDTLS_SSL_PROTO_SSL3)
2420 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002421 {
2422 ssl_mac( &ssl->transform_in->md_ctx_dec,
2423 ssl->transform_in->mac_dec,
2424 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002425 ssl->in_ctr, ssl->in_msgtype,
2426 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002427 }
2428 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2430#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2431 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2432 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002433 {
2434 /*
2435 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002436 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002437 *
2438 * Known timing attacks:
2439 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2440 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002441 * To compensate for different timings for the MAC calculation
2442 * depending on how much padding was removed (which is determined
2443 * by padlen), process extra_run more blocks through the hash
2444 * function.
2445 *
2446 * The formula in the paper is
2447 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2448 * where L1 is the size of the header plus the decrypted message
2449 * plus CBC padding and L2 is the size of the header plus the
2450 * decrypted message. This is for an underlying hash function
2451 * with 64-byte blocks.
2452 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2453 * correctly. We round down instead of up, so -56 is the correct
2454 * value for our calculations instead of -55.
2455 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002456 * Repeat the formula rather than defining a block_size variable.
2457 * This avoids requiring division by a variable at runtime
2458 * (which would be marginally less efficient and would require
2459 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002460 */
2461 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002462
2463 /*
2464 * The next two sizes are the minimum and maximum values of
2465 * in_msglen over all padlen values.
2466 *
2467 * They're independent of padlen, since we previously did
2468 * in_msglen -= padlen.
2469 *
2470 * Note that max_len + maclen is never more than the buffer
2471 * length, as we previously did in_msglen -= maclen too.
2472 */
2473 const size_t max_len = ssl->in_msglen + padlen;
2474 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2475
Gilles Peskine20b44082018-05-29 14:06:49 +02002476 switch( ssl->transform_in->ciphersuite_info->mac )
2477 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002478#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2479 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002480 case MBEDTLS_MD_MD5:
2481 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002482 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002483 /* 8 bytes of message size, 64-byte compression blocks */
2484 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2485 ( 13 + ssl->in_msglen + 8 ) / 64;
2486 break;
2487#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002488#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002489 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002490 /* 16 bytes of message size, 128-byte compression blocks */
2491 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2492 ( 13 + ssl->in_msglen + 16 ) / 128;
2493 break;
2494#endif
2495 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002497 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2498 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002499
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002500 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2503 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2504 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2505 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002506 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002507 /* Make sure we access everything even when padlen > 0. This
2508 * makes the synchronisation requirements for just-in-time
2509 * Prime+Probe attacks much tighter and hopefully impractical. */
2510 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002511 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002512
2513 /* Call mbedtls_md_process at least once due to cache attacks
2514 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002515 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002519
2520 /* Make sure we access all the memory that could contain the MAC,
2521 * before we check it in the next code block. This makes the
2522 * synchronisation requirements for just-in-time Prime+Probe
2523 * attacks much tighter and hopefully impractical. */
2524 ssl_read_memory( ssl->in_msg + min_len,
2525 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002526 }
2527 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2529 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2532 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002533 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002534
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002535#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002536 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2537 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2538 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002539#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002540
Hanno Becker992b6872017-11-09 18:57:39 +00002541 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2542 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544#if defined(MBEDTLS_SSL_DEBUG_ALL)
2545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002546#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002547 correct = 0;
2548 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002549 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002550 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002551
2552 /*
2553 * Finally check the correct flag
2554 */
2555 if( correct == 0 )
2556 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002557#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002558
2559 /* Make extra sure authentication was performed, exactly once */
2560 if( auth_done != 1 )
2561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002564 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002565
2566 if( ssl->in_msglen == 0 )
2567 {
Angus Gratton34817922018-06-19 15:58:22 +10002568#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2569 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2570 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2571 {
2572 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2574 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2575 }
2576#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2577
Paul Bakker5121ce52009-01-03 21:22:43 +00002578 ssl->nb_zero++;
2579
2580 /*
2581 * Three or more empty messages may be a DoS attack
2582 * (excessive CPU consumption).
2583 */
2584 if( ssl->nb_zero > 3 )
2585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002586 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002589 }
2590 }
2591 else
2592 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002595 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002596 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002597 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002598 }
2599 else
2600#endif
2601 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002602 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002603 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2604 if( ++ssl->in_ctr[i - 1] != 0 )
2605 break;
2606
2607 /* The loop goes to its end iff the counter is wrapping */
2608 if( i == ssl_ep_len( ssl ) )
2609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2611 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002612 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002613 }
2614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002616
2617 return( 0 );
2618}
2619
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002620#undef MAC_NONE
2621#undef MAC_PLAINTEXT
2622#undef MAC_CIPHERTEXT
2623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002625/*
2626 * Compression/decompression functions
2627 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002629{
2630 int ret;
2631 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002632 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002633 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002634 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002637
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002638 if( len_pre == 0 )
2639 return( 0 );
2640
Paul Bakker2770fbd2012-07-03 13:30:23 +00002641 memcpy( msg_pre, ssl->out_msg, len_pre );
2642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002644 ssl->out_msglen ) );
2645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002647 ssl->out_msg, ssl->out_msglen );
2648
Paul Bakker48916f92012-09-16 19:57:18 +00002649 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2650 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2651 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002652 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002653
Paul Bakker48916f92012-09-16 19:57:18 +00002654 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002655 if( ret != Z_OK )
2656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2658 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002659 }
2660
Angus Grattond8213d02016-05-25 20:56:48 +10002661 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002662 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002665 ssl->out_msglen ) );
2666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002668 ssl->out_msg, ssl->out_msglen );
2669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002671
2672 return( 0 );
2673}
2674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002676{
2677 int ret;
2678 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002679 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002680 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002681 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002684
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002685 if( len_pre == 0 )
2686 return( 0 );
2687
Paul Bakker2770fbd2012-07-03 13:30:23 +00002688 memcpy( msg_pre, ssl->in_msg, len_pre );
2689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002691 ssl->in_msglen ) );
2692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002694 ssl->in_msg, ssl->in_msglen );
2695
Paul Bakker48916f92012-09-16 19:57:18 +00002696 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2697 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2698 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002699 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002700 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002701
Paul Bakker48916f92012-09-16 19:57:18 +00002702 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002703 if( ret != Z_OK )
2704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2706 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002707 }
2708
Angus Grattond8213d02016-05-25 20:56:48 +10002709 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002710 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002713 ssl->in_msglen ) );
2714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002716 ssl->in_msg, ssl->in_msglen );
2717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002719
2720 return( 0 );
2721}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2725static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727#if defined(MBEDTLS_SSL_PROTO_DTLS)
2728static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002729{
2730 /* If renegotiation is not enforced, retransmit until we would reach max
2731 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002732 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002733 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002734 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002735 unsigned char doublings = 1;
2736
2737 while( ratio != 0 )
2738 {
2739 ++doublings;
2740 ratio >>= 1;
2741 }
2742
2743 if( ++ssl->renego_records_seen > doublings )
2744 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002746 return( 0 );
2747 }
2748 }
2749
2750 return( ssl_write_hello_request( ssl ) );
2751}
2752#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002754
Paul Bakker5121ce52009-01-03 21:22:43 +00002755/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002756 * Fill the input message buffer by appending data to it.
2757 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002758 *
2759 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2760 * available (from this read and/or a previous one). Otherwise, an error code
2761 * is returned (possibly EOF or WANT_READ).
2762 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002763 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2764 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2765 * since we always read a whole datagram at once.
2766 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002767 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002768 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002769 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002771{
Paul Bakker23986e52011-04-24 08:57:21 +00002772 int ret;
2773 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002776
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002777 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002780 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002782 }
2783
Angus Grattond8213d02016-05-25 20:56:48 +10002784 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2787 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002788 }
2789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002791 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002792 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002793 uint32_t timeout;
2794
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002795 /* Just to be sure */
2796 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2797 {
2798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2799 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2800 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2801 }
2802
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002803 /*
2804 * The point is, we need to always read a full datagram at once, so we
2805 * sometimes read more then requested, and handle the additional data.
2806 * It could be the rest of the current record (while fetching the
2807 * header) and/or some other records in the same datagram.
2808 */
2809
2810 /*
2811 * Move to the next record in the already read datagram if applicable
2812 */
2813 if( ssl->next_record_offset != 0 )
2814 {
2815 if( ssl->in_left < ssl->next_record_offset )
2816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2818 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002819 }
2820
2821 ssl->in_left -= ssl->next_record_offset;
2822
2823 if( ssl->in_left != 0 )
2824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002826 ssl->next_record_offset ) );
2827 memmove( ssl->in_hdr,
2828 ssl->in_hdr + ssl->next_record_offset,
2829 ssl->in_left );
2830 }
2831
2832 ssl->next_record_offset = 0;
2833 }
2834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002836 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002837
2838 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002839 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002840 */
2841 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002844 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002845 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002846
2847 /*
2848 * A record can't be split accross datagrams. If we need to read but
2849 * are not at the beginning of a new record, the caller did something
2850 * wrong.
2851 */
2852 if( ssl->in_left != 0 )
2853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2855 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002856 }
2857
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002858 /*
2859 * Don't even try to read if time's out already.
2860 * This avoids by-passing the timer when repeatedly receiving messages
2861 * that will end up being dropped.
2862 */
2863 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002864 {
2865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002866 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002867 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002868 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002869 {
Angus Grattond8213d02016-05-25 20:56:48 +10002870 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002872 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002873 timeout = ssl->handshake->retransmit_timeout;
2874 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002875 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002878
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002879 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002880 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2881 timeout );
2882 else
2883 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002886
2887 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002889 }
2890
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002891 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002894 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002897 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002898 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002901 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002902 }
2903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002907 return( ret );
2908 }
2909
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002910 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002911 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002913 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002915 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002916 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002919 return( ret );
2920 }
2921
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002922 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002923 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002925 }
2926
Paul Bakker5121ce52009-01-03 21:22:43 +00002927 if( ret < 0 )
2928 return( ret );
2929
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002930 ssl->in_left = ret;
2931 }
2932 else
2933#endif
2934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002936 ssl->in_left, nb_want ) );
2937
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002938 while( ssl->in_left < nb_want )
2939 {
2940 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002941
2942 if( ssl_check_timer( ssl ) != 0 )
2943 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2944 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002945 {
2946 if( ssl->f_recv_timeout != NULL )
2947 {
2948 ret = ssl->f_recv_timeout( ssl->p_bio,
2949 ssl->in_hdr + ssl->in_left, len,
2950 ssl->conf->read_timeout );
2951 }
2952 else
2953 {
2954 ret = ssl->f_recv( ssl->p_bio,
2955 ssl->in_hdr + ssl->in_left, len );
2956 }
2957 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002960 ssl->in_left, nb_want ) );
2961 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002962
2963 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002965
2966 if( ret < 0 )
2967 return( ret );
2968
mohammad160352aecb92018-03-28 23:41:40 -07002969 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002970 {
Darryl Green11999bb2018-03-13 15:22:58 +00002971 MBEDTLS_SSL_DEBUG_MSG( 1,
2972 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002973 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002974 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2975 }
2976
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002977 ssl->in_left += ret;
2978 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002979 }
2980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002982
2983 return( 0 );
2984}
2985
2986/*
2987 * Flush any data not yet written
2988 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002990{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002991 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002992 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002995
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002996 if( ssl->f_send == NULL )
2997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002999 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003001 }
3002
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003003 /* Avoid incrementing counter if data is flushed */
3004 if( ssl->out_left == 0 )
3005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003007 return( 0 );
3008 }
3009
Paul Bakker5121ce52009-01-03 21:22:43 +00003010 while( ssl->out_left > 0 )
3011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3013 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003014
Hanno Becker2b1e3542018-08-06 11:19:13 +01003015 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003016 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003019
3020 if( ret <= 0 )
3021 return( ret );
3022
mohammad160352aecb92018-03-28 23:41:40 -07003023 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003024 {
Darryl Green11999bb2018-03-13 15:22:58 +00003025 MBEDTLS_SSL_DEBUG_MSG( 1,
3026 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003027 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003028 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3029 }
3030
Paul Bakker5121ce52009-01-03 21:22:43 +00003031 ssl->out_left -= ret;
3032 }
3033
Hanno Becker2b1e3542018-08-06 11:19:13 +01003034#if defined(MBEDTLS_SSL_PROTO_DTLS)
3035 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003036 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003037 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003038 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003039 else
3040#endif
3041 {
3042 ssl->out_hdr = ssl->out_buf + 8;
3043 }
3044 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003047
3048 return( 0 );
3049}
3050
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003051/*
3052 * Functions to handle the DTLS retransmission state machine
3053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003055/*
3056 * Append current handshake message to current outgoing flight
3057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003058static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003059{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3062 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3063 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003064
3065 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003066 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003067 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003069 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003070 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003071 }
3072
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003073 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003074 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003077 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003078 }
3079
3080 /* Copy current handshake message with headers */
3081 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3082 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003083 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003084 msg->next = NULL;
3085
3086 /* Append to the current flight */
3087 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003088 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003089 else
3090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003092 while( cur->next != NULL )
3093 cur = cur->next;
3094 cur->next = msg;
3095 }
3096
Hanno Becker3b235902018-08-06 09:54:53 +01003097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003098 return( 0 );
3099}
3100
3101/*
3102 * Free the current flight of handshake messages
3103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003105{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106 mbedtls_ssl_flight_item *cur = flight;
3107 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003108
3109 while( cur != NULL )
3110 {
3111 next = cur->next;
3112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003113 mbedtls_free( cur->p );
3114 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003115
3116 cur = next;
3117 }
3118}
3119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3121static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003122#endif
3123
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003124/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003125 * Swap transform_out and out_ctr with the alternative ones
3126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003128{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003130 unsigned char tmp_out_ctr[8];
3131
3132 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003135 return;
3136 }
3137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003139
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003140 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003141 tmp_transform = ssl->transform_out;
3142 ssl->transform_out = ssl->handshake->alt_transform_out;
3143 ssl->handshake->alt_transform_out = tmp_transform;
3144
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003145 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003146 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3147 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003148 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003149
3150 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003151 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3154 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3159 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003160 }
3161 }
3162#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003163}
3164
3165/*
3166 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003167 */
3168int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3169{
3170 int ret = 0;
3171
3172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3173
3174 ret = mbedtls_ssl_flight_transmit( ssl );
3175
3176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3177
3178 return( ret );
3179}
3180
3181/*
3182 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003183 *
3184 * Need to remember the current message in case flush_output returns
3185 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003186 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003187 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003188int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003189{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003190 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003194 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003196
3197 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003198 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003199 ssl_swap_epochs( ssl );
3200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003202 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003203
3204 while( ssl->handshake->cur_msg != NULL )
3205 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003206 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003207 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003208
Hanno Beckere1dcb032018-08-17 16:47:58 +01003209 int const is_finished =
3210 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3211 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3212
Hanno Becker04da1892018-08-14 13:22:10 +01003213 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3214 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3215
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003216 /* Swap epochs before sending Finished: we can't do it after
3217 * sending ChangeCipherSpec, in case write returns WANT_READ.
3218 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003219 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003220 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003222 ssl_swap_epochs( ssl );
3223 }
3224
Hanno Becker67bc7c32018-08-06 11:33:50 +01003225 ret = ssl_get_remaining_payload_in_datagram( ssl );
3226 if( ret < 0 )
3227 return( ret );
3228 max_frag_len = (size_t) ret;
3229
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003230 /* CCS is copied as is, while HS messages may need fragmentation */
3231 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3232 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003233 if( max_frag_len == 0 )
3234 {
3235 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3236 return( ret );
3237
3238 continue;
3239 }
3240
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003241 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003242 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003243 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003244
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003245 /* Update position inside current message */
3246 ssl->handshake->cur_msg_p += cur->len;
3247 }
3248 else
3249 {
3250 const unsigned char * const p = ssl->handshake->cur_msg_p;
3251 const size_t hs_len = cur->len - 12;
3252 const size_t frag_off = p - ( cur->p + 12 );
3253 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003254 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003255
Hanno Beckere1dcb032018-08-17 16:47:58 +01003256 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003257 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003258 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003259 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003260
Hanno Becker67bc7c32018-08-06 11:33:50 +01003261 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3262 return( ret );
3263
3264 continue;
3265 }
3266 max_hs_frag_len = max_frag_len - 12;
3267
3268 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3269 max_hs_frag_len : rem_len;
3270
3271 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003272 {
3273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003274 (unsigned) cur_hs_frag_len,
3275 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003276 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003277
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003278 /* Messages are stored with handshake headers as if not fragmented,
3279 * copy beginning of headers then fill fragmentation fields.
3280 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3281 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003282
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003283 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3284 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3285 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3286
Hanno Becker67bc7c32018-08-06 11:33:50 +01003287 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3288 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3289 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003290
3291 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3292
Hanno Becker3f7b9732018-08-28 09:53:25 +01003293 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003294 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3295 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003296 ssl->out_msgtype = cur->type;
3297
3298 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003299 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003300 }
3301
3302 /* If done with the current message move to the next one if any */
3303 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3304 {
3305 if( cur->next != NULL )
3306 {
3307 ssl->handshake->cur_msg = cur->next;
3308 ssl->handshake->cur_msg_p = cur->next->p + 12;
3309 }
3310 else
3311 {
3312 ssl->handshake->cur_msg = NULL;
3313 ssl->handshake->cur_msg_p = NULL;
3314 }
3315 }
3316
3317 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003318 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003321 return( ret );
3322 }
3323 }
3324
Hanno Becker67bc7c32018-08-06 11:33:50 +01003325 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3326 return( ret );
3327
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003328 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3330 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003331 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003333 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003334 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3335 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003336
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003338
3339 return( 0 );
3340}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003341
3342/*
3343 * To be called when the last message of an incoming flight is received.
3344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003346{
3347 /* We won't need to resend that one any more */
3348 ssl_flight_free( ssl->handshake->flight );
3349 ssl->handshake->flight = NULL;
3350 ssl->handshake->cur_msg = NULL;
3351
3352 /* The next incoming flight will start with this msg_seq */
3353 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3354
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003355 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003356 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003357
Hanno Becker0271f962018-08-16 13:23:47 +01003358 /* Clear future message buffering structure. */
3359 ssl_buffering_free( ssl );
3360
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003361 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003362 ssl_set_timer( ssl, 0 );
3363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3365 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003367 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003368 }
3369 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003371}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003372
3373/*
3374 * To be called when the last message of an outgoing flight is send.
3375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003377{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003378 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003379 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3382 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003384 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003385 }
3386 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003388}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003389#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003390
Paul Bakker5121ce52009-01-03 21:22:43 +00003391/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003392 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003393 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003394
3395/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003396 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003397 *
3398 * - fill in handshake headers
3399 * - update handshake checksum
3400 * - DTLS: save message for resending
3401 * - then pass to the record layer
3402 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003403 * DTLS: except for HelloRequest, messages are only queued, and will only be
3404 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003405 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003406 * Inputs:
3407 * - ssl->out_msglen: 4 + actual handshake message len
3408 * (4 is the size of handshake headers for TLS)
3409 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3410 * - ssl->out_msg + 4: the handshake message body
3411 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003412 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003413 * - ssl->out_msglen: the length of the record contents
3414 * (including handshake headers but excluding record headers)
3415 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003416 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003417int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003418{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003419 int ret;
3420 const size_t hs_len = ssl->out_msglen - 4;
3421 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003422
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3424
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003425 /*
3426 * Sanity checks
3427 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003428 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003429 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3430 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003431 /* In SSLv3, the client might send a NoCertificate alert. */
3432#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3433 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3434 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3435 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3436#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3437 {
3438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3439 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3440 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003441 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003442
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003443 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3444 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3445 ssl->handshake == NULL )
3446 {
3447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3448 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3449 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003452 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003453 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003455 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3457 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003458 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003459#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003460
Hanno Beckerb50a2532018-08-06 11:52:54 +01003461 /* Double-check that we did not exceed the bounds
3462 * of the outgoing record buffer.
3463 * This should never fail as the various message
3464 * writing functions must obey the bounds of the
3465 * outgoing record buffer, but better be safe.
3466 *
3467 * Note: We deliberately do not check for the MTU or MFL here.
3468 */
3469 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3470 {
3471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3472 "size %u, maximum %u",
3473 (unsigned) ssl->out_msglen,
3474 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3475 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3476 }
3477
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003478 /*
3479 * Fill handshake headers
3480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003481 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003482 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003483 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3484 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3485 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003486
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003487 /*
3488 * DTLS has additional fields in the Handshake layer,
3489 * between the length field and the actual payload:
3490 * uint16 message_seq;
3491 * uint24 fragment_offset;
3492 * uint24 fragment_length;
3493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003495 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003496 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003497 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003498 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003499 {
3500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3501 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003502 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003503 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003504 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3505 }
3506
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003507 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003508 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003509
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003510 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003511 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003512 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003513 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3514 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3515 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003516 }
3517 else
3518 {
3519 ssl->out_msg[4] = 0;
3520 ssl->out_msg[5] = 0;
3521 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003522
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003523 /* Handshake hashes are computed without fragmentation,
3524 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003525 memset( ssl->out_msg + 6, 0x00, 3 );
3526 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003527 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003529
Hanno Becker0207e532018-08-28 10:28:28 +01003530 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003531 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3532 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003533 }
3534
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003535 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003537 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Becker081bd812018-08-22 16:07:59 +01003538 ( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
3539 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003540 {
3541 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003543 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003544 return( ret );
3545 }
3546 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003547 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003548#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003549 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003550 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003551 {
3552 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3553 return( ret );
3554 }
3555 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003556
3557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3558
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003559 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003560}
3561
3562/*
3563 * Record layer functions
3564 */
3565
3566/*
3567 * Write current record.
3568 *
3569 * Uses:
3570 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3571 * - ssl->out_msglen: length of the record content (excl headers)
3572 * - ssl->out_msg: record content
3573 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003574int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003575{
3576 int ret, done = 0;
3577 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003578 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003579
3580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003583 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003584 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003585 {
3586 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003589 return( ret );
3590 }
3591
3592 len = ssl->out_msglen;
3593 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3597 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 ret = mbedtls_ssl_hw_record_write( ssl );
3602 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3605 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003606 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003607
3608 if( ret == 0 )
3609 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003610 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003612 if( !done )
3613 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003614 unsigned i;
3615 size_t protected_record_size;
3616
Paul Bakker05ef8352012-05-08 09:17:57 +00003617 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003619 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003620
Hanno Becker19859472018-08-06 09:40:20 +01003621 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003622 ssl->out_len[0] = (unsigned char)( len >> 8 );
3623 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003624
Paul Bakker48916f92012-09-16 19:57:18 +00003625 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003626 {
3627 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003630 return( ret );
3631 }
3632
3633 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003634 ssl->out_len[0] = (unsigned char)( len >> 8 );
3635 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003636 }
3637
Hanno Becker2b1e3542018-08-06 11:19:13 +01003638 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3639
3640#if defined(MBEDTLS_SSL_PROTO_DTLS)
3641 /* In case of DTLS, double-check that we don't exceed
3642 * the remaining space in the datagram. */
3643 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3644 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003645 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003646 if( ret < 0 )
3647 return( ret );
3648
3649 if( protected_record_size > (size_t) ret )
3650 {
3651 /* Should never happen */
3652 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3653 }
3654 }
3655#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003658 "version = [%d:%d], msglen = %d",
3659 ssl->out_hdr[0], ssl->out_hdr[1],
3660 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003663 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003664
3665 ssl->out_left += protected_record_size;
3666 ssl->out_hdr += protected_record_size;
3667 ssl_update_out_pointers( ssl, ssl->transform_out );
3668
Hanno Becker04484622018-08-06 09:49:38 +01003669 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3670 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3671 break;
3672
3673 /* The loop goes to its end iff the counter is wrapping */
3674 if( i == ssl_ep_len( ssl ) )
3675 {
3676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3677 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3678 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003679 }
3680
Hanno Becker67bc7c32018-08-06 11:33:50 +01003681#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003682 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3683 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003684 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003685 size_t remaining;
3686 ret = ssl_get_remaining_payload_in_datagram( ssl );
3687 if( ret < 0 )
3688 {
3689 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3690 ret );
3691 return( ret );
3692 }
3693
3694 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003695 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003696 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003697 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003698 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003699 else
3700 {
Hanno Becker513815a2018-08-20 11:56:09 +01003701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003702 }
3703 }
3704#endif /* MBEDTLS_SSL_PROTO_DTLS */
3705
3706 if( ( flush == SSL_FORCE_FLUSH ) &&
3707 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003709 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003710 return( ret );
3711 }
3712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003714
3715 return( 0 );
3716}
3717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003719
3720static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3721{
3722 if( ssl->in_msglen < ssl->in_hslen ||
3723 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3724 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3725 {
3726 return( 1 );
3727 }
3728 return( 0 );
3729}
Hanno Becker44650b72018-08-16 12:51:11 +01003730
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003731static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003732{
3733 return( ( ssl->in_msg[9] << 16 ) |
3734 ( ssl->in_msg[10] << 8 ) |
3735 ssl->in_msg[11] );
3736}
3737
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003738static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003739{
3740 return( ( ssl->in_msg[6] << 16 ) |
3741 ( ssl->in_msg[7] << 8 ) |
3742 ssl->in_msg[8] );
3743}
3744
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003745static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003746{
3747 uint32_t msg_len, frag_off, frag_len;
3748
3749 msg_len = ssl_get_hs_total_len( ssl );
3750 frag_off = ssl_get_hs_frag_off( ssl );
3751 frag_len = ssl_get_hs_frag_len( ssl );
3752
3753 if( frag_off > msg_len )
3754 return( -1 );
3755
3756 if( frag_len > msg_len - frag_off )
3757 return( -1 );
3758
3759 if( frag_len + 12 > ssl->in_msglen )
3760 return( -1 );
3761
3762 return( 0 );
3763}
3764
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003765/*
3766 * Mark bits in bitmask (used for DTLS HS reassembly)
3767 */
3768static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3769{
3770 unsigned int start_bits, end_bits;
3771
3772 start_bits = 8 - ( offset % 8 );
3773 if( start_bits != 8 )
3774 {
3775 size_t first_byte_idx = offset / 8;
3776
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003777 /* Special case */
3778 if( len <= start_bits )
3779 {
3780 for( ; len != 0; len-- )
3781 mask[first_byte_idx] |= 1 << ( start_bits - len );
3782
3783 /* Avoid potential issues with offset or len becoming invalid */
3784 return;
3785 }
3786
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003787 offset += start_bits; /* Now offset % 8 == 0 */
3788 len -= start_bits;
3789
3790 for( ; start_bits != 0; start_bits-- )
3791 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3792 }
3793
3794 end_bits = len % 8;
3795 if( end_bits != 0 )
3796 {
3797 size_t last_byte_idx = ( offset + len ) / 8;
3798
3799 len -= end_bits; /* Now len % 8 == 0 */
3800
3801 for( ; end_bits != 0; end_bits-- )
3802 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3803 }
3804
3805 memset( mask + offset / 8, 0xFF, len / 8 );
3806}
3807
3808/*
3809 * Check that bitmask is full
3810 */
3811static int ssl_bitmask_check( unsigned char *mask, size_t len )
3812{
3813 size_t i;
3814
3815 for( i = 0; i < len / 8; i++ )
3816 if( mask[i] != 0xFF )
3817 return( -1 );
3818
3819 for( i = 0; i < len % 8; i++ )
3820 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3821 return( -1 );
3822
3823 return( 0 );
3824}
3825
Hanno Becker56e205e2018-08-16 09:06:12 +01003826/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003827static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003828 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003829{
Hanno Becker56e205e2018-08-16 09:06:12 +01003830 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003831
Hanno Becker56e205e2018-08-16 09:06:12 +01003832 alloc_len = 12; /* Handshake header */
3833 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003834
Hanno Beckerd07df862018-08-16 09:14:58 +01003835 if( add_bitmap )
3836 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003837
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003838 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003839}
Hanno Becker56e205e2018-08-16 09:06:12 +01003840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003842
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003843static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003844{
3845 return( ( ssl->in_msg[1] << 16 ) |
3846 ( ssl->in_msg[2] << 8 ) |
3847 ssl->in_msg[3] );
3848}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003849
Simon Butcher99000142016-10-13 17:21:01 +01003850int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003851{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003855 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003857 }
3858
Hanno Becker12555c62018-08-16 12:47:53 +01003859 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003862 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003863 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003866 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003867 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003868 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003869 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003870
Hanno Becker44650b72018-08-16 12:51:11 +01003871 if( ssl_check_hs_header( ssl ) != 0 )
3872 {
3873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3874 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3875 }
3876
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003877 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003878 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3879 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3880 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3881 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003882 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003883 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3884 {
3885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3886 recv_msg_seq,
3887 ssl->handshake->in_msg_seq ) );
3888 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3889 }
3890
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003891 /* Retransmit only on last message from previous flight, to avoid
3892 * too many retransmissions.
3893 * Besides, No sane server ever retransmits HelloVerifyRequest */
3894 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003895 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003898 "message_seq = %d, start_of_flight = %d",
3899 recv_msg_seq,
3900 ssl->handshake->in_flight_start_seq ) );
3901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003902 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003904 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003905 return( ret );
3906 }
3907 }
3908 else
3909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003911 "message_seq = %d, expected = %d",
3912 recv_msg_seq,
3913 ssl->handshake->in_msg_seq ) );
3914 }
3915
Hanno Becker90333da2017-10-10 11:27:13 +01003916 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003917 }
3918 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003919
Hanno Becker6d97ef52018-08-16 13:09:04 +01003920 /* Message reassembly is handled alongside buffering of future
3921 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003922 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003923 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003924 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003927 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003928 }
3929 }
3930 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003931#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003932 /* With TLS we don't handle fragmentation (for now) */
3933 if( ssl->in_msglen < ssl->in_hslen )
3934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3936 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003937 }
3938
Simon Butcher99000142016-10-13 17:21:01 +01003939 return( 0 );
3940}
3941
3942void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3943{
Hanno Becker0271f962018-08-16 13:23:47 +01003944 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003945
Hanno Becker0271f962018-08-16 13:23:47 +01003946 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003947 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003948 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003949 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003950
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003951 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003952#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003953 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003954 ssl->handshake != NULL )
3955 {
Hanno Becker0271f962018-08-16 13:23:47 +01003956 unsigned offset;
3957 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003958
Hanno Becker0271f962018-08-16 13:23:47 +01003959 /* Increment handshake sequence number */
3960 hs->in_msg_seq++;
3961
3962 /*
3963 * Clear up handshake buffering and reassembly structure.
3964 */
3965
3966 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003967 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003968
3969 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003970 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3971 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003972 offset++, hs_buf++ )
3973 {
3974 *hs_buf = *(hs_buf + 1);
3975 }
3976
3977 /* Create a fresh last entry */
3978 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003979 }
3980#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003981}
3982
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003983/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003984 * DTLS anti-replay: RFC 6347 4.1.2.6
3985 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003986 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3987 * Bit n is set iff record number in_window_top - n has been seen.
3988 *
3989 * Usually, in_window_top is the last record number seen and the lsb of
3990 * in_window is set. The only exception is the initial state (record number 0
3991 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003992 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003993#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3994static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003995{
3996 ssl->in_window_top = 0;
3997 ssl->in_window = 0;
3998}
3999
4000static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4001{
4002 return( ( (uint64_t) buf[0] << 40 ) |
4003 ( (uint64_t) buf[1] << 32 ) |
4004 ( (uint64_t) buf[2] << 24 ) |
4005 ( (uint64_t) buf[3] << 16 ) |
4006 ( (uint64_t) buf[4] << 8 ) |
4007 ( (uint64_t) buf[5] ) );
4008}
4009
4010/*
4011 * Return 0 if sequence number is acceptable, -1 otherwise
4012 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004013int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004014{
4015 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4016 uint64_t bit;
4017
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004018 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004019 return( 0 );
4020
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004021 if( rec_seqnum > ssl->in_window_top )
4022 return( 0 );
4023
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004024 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004025
4026 if( bit >= 64 )
4027 return( -1 );
4028
4029 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4030 return( -1 );
4031
4032 return( 0 );
4033}
4034
4035/*
4036 * Update replay window on new validated record
4037 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004038void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004039{
4040 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4041
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004042 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004043 return;
4044
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004045 if( rec_seqnum > ssl->in_window_top )
4046 {
4047 /* Update window_top and the contents of the window */
4048 uint64_t shift = rec_seqnum - ssl->in_window_top;
4049
4050 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004051 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004052 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004053 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004054 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004055 ssl->in_window |= 1;
4056 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004057
4058 ssl->in_window_top = rec_seqnum;
4059 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004060 else
4061 {
4062 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004063 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004064
4065 if( bit < 64 ) /* Always true, but be extra sure */
4066 ssl->in_window |= (uint64_t) 1 << bit;
4067 }
4068}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004069#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004070
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004071#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004072/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004073static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4074
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004075/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004076 * Without any SSL context, check if a datagram looks like a ClientHello with
4077 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004078 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004079 *
4080 * - if cookie is valid, return 0
4081 * - if ClientHello looks superficially valid but cookie is not,
4082 * fill obuf and set olen, then
4083 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4084 * - otherwise return a specific error code
4085 */
4086static int ssl_check_dtls_clihlo_cookie(
4087 mbedtls_ssl_cookie_write_t *f_cookie_write,
4088 mbedtls_ssl_cookie_check_t *f_cookie_check,
4089 void *p_cookie,
4090 const unsigned char *cli_id, size_t cli_id_len,
4091 const unsigned char *in, size_t in_len,
4092 unsigned char *obuf, size_t buf_len, size_t *olen )
4093{
4094 size_t sid_len, cookie_len;
4095 unsigned char *p;
4096
4097 if( f_cookie_write == NULL || f_cookie_check == NULL )
4098 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4099
4100 /*
4101 * Structure of ClientHello with record and handshake headers,
4102 * and expected values. We don't need to check a lot, more checks will be
4103 * done when actually parsing the ClientHello - skipping those checks
4104 * avoids code duplication and does not make cookie forging any easier.
4105 *
4106 * 0-0 ContentType type; copied, must be handshake
4107 * 1-2 ProtocolVersion version; copied
4108 * 3-4 uint16 epoch; copied, must be 0
4109 * 5-10 uint48 sequence_number; copied
4110 * 11-12 uint16 length; (ignored)
4111 *
4112 * 13-13 HandshakeType msg_type; (ignored)
4113 * 14-16 uint24 length; (ignored)
4114 * 17-18 uint16 message_seq; copied
4115 * 19-21 uint24 fragment_offset; copied, must be 0
4116 * 22-24 uint24 fragment_length; (ignored)
4117 *
4118 * 25-26 ProtocolVersion client_version; (ignored)
4119 * 27-58 Random random; (ignored)
4120 * 59-xx SessionID session_id; 1 byte len + sid_len content
4121 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4122 * ...
4123 *
4124 * Minimum length is 61 bytes.
4125 */
4126 if( in_len < 61 ||
4127 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4128 in[3] != 0 || in[4] != 0 ||
4129 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4130 {
4131 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4132 }
4133
4134 sid_len = in[59];
4135 if( sid_len > in_len - 61 )
4136 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4137
4138 cookie_len = in[60 + sid_len];
4139 if( cookie_len > in_len - 60 )
4140 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4141
4142 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4143 cli_id, cli_id_len ) == 0 )
4144 {
4145 /* Valid cookie */
4146 return( 0 );
4147 }
4148
4149 /*
4150 * If we get here, we've got an invalid cookie, let's prepare HVR.
4151 *
4152 * 0-0 ContentType type; copied
4153 * 1-2 ProtocolVersion version; copied
4154 * 3-4 uint16 epoch; copied
4155 * 5-10 uint48 sequence_number; copied
4156 * 11-12 uint16 length; olen - 13
4157 *
4158 * 13-13 HandshakeType msg_type; hello_verify_request
4159 * 14-16 uint24 length; olen - 25
4160 * 17-18 uint16 message_seq; copied
4161 * 19-21 uint24 fragment_offset; copied
4162 * 22-24 uint24 fragment_length; olen - 25
4163 *
4164 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4165 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4166 *
4167 * Minimum length is 28.
4168 */
4169 if( buf_len < 28 )
4170 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4171
4172 /* Copy most fields and adapt others */
4173 memcpy( obuf, in, 25 );
4174 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4175 obuf[25] = 0xfe;
4176 obuf[26] = 0xff;
4177
4178 /* Generate and write actual cookie */
4179 p = obuf + 28;
4180 if( f_cookie_write( p_cookie,
4181 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4182 {
4183 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4184 }
4185
4186 *olen = p - obuf;
4187
4188 /* Go back and fill length fields */
4189 obuf[27] = (unsigned char)( *olen - 28 );
4190
4191 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4192 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4193 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4194
4195 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4196 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4197
4198 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4199}
4200
4201/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004202 * Handle possible client reconnect with the same UDP quadruplet
4203 * (RFC 6347 Section 4.2.8).
4204 *
4205 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4206 * that looks like a ClientHello.
4207 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004208 * - if the input looks like a ClientHello without cookies,
4209 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004210 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004211 * - if the input looks like a ClientHello with a valid cookie,
4212 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004213 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004214 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004215 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004216 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004217 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4218 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004219 */
4220static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4221{
4222 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004223 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004224
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004225 ret = ssl_check_dtls_clihlo_cookie(
4226 ssl->conf->f_cookie_write,
4227 ssl->conf->f_cookie_check,
4228 ssl->conf->p_cookie,
4229 ssl->cli_id, ssl->cli_id_len,
4230 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004231 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004232
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004233 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4234
4235 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004236 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004237 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004238 * If the error is permanent we'll catch it later,
4239 * if it's not, then hopefully it'll work next time. */
4240 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4241
4242 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004243 }
4244
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004245 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004246 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004247 /* Got a valid cookie, partially reset context */
4248 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4249 {
4250 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4251 return( ret );
4252 }
4253
4254 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004255 }
4256
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004257 return( ret );
4258}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004259#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004260
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004261/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004262 * ContentType type;
4263 * ProtocolVersion version;
4264 * uint16 epoch; // DTLS only
4265 * uint48 sequence_number; // DTLS only
4266 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004267 *
4268 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004269 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004270 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4271 *
4272 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004273 * 1. proceed with the record if this function returns 0
4274 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4275 * 3. return CLIENT_RECONNECT if this function return that value
4276 * 4. drop the whole datagram if this function returns anything else.
4277 * Point 2 is needed when the peer is resending, and we have already received
4278 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004280static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004281{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004282 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284 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 +02004285
Paul Bakker5121ce52009-01-03 21:22:43 +00004286 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004287 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004288 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004290 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004291 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004292 ssl->in_msgtype,
4293 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004294
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004295 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004296 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4297 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4298 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4299 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004302
4303#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004304 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4305 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004306 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4307#endif /* MBEDTLS_SSL_PROTO_DTLS */
4308 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4309 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004311 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004312 }
4313
4314 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004315 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4318 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004319 }
4320
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004321 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4324 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004325 }
4326
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004327 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004328 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004329 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4332 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004333 }
4334
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004335 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004336 * DTLS-related tests.
4337 * Check epoch before checking length constraint because
4338 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4339 * message gets duplicated before the corresponding Finished message,
4340 * the second ChangeCipherSpec should be discarded because it belongs
4341 * to an old epoch, but not because its length is shorter than
4342 * the minimum record length for packets using the new record transform.
4343 * Note that these two kinds of failures are handled differently,
4344 * as an unexpected record is silently skipped but an invalid
4345 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004346 */
4347#if defined(MBEDTLS_SSL_PROTO_DTLS)
4348 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4349 {
4350 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4351
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004352 /* Check epoch (and sequence number) with DTLS */
4353 if( rec_epoch != ssl->in_epoch )
4354 {
4355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4356 "expected %d, received %d",
4357 ssl->in_epoch, rec_epoch ) );
4358
4359#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4360 /*
4361 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4362 * access the first byte of record content (handshake type), as we
4363 * have an active transform (possibly iv_len != 0), so use the
4364 * fact that the record header len is 13 instead.
4365 */
4366 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4367 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4368 rec_epoch == 0 &&
4369 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4370 ssl->in_left > 13 &&
4371 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4372 {
4373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4374 "from the same port" ) );
4375 return( ssl_handle_possible_reconnect( ssl ) );
4376 }
4377 else
4378#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004379 {
4380 /* Consider buffering the record. */
4381 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4382 {
4383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4384 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4385 }
4386
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004387 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004388 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004389 }
4390
4391#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4392 /* Replay detection only works for the current epoch */
4393 if( rec_epoch == ssl->in_epoch &&
4394 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4395 {
4396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4397 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4398 }
4399#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004400
Hanno Becker52c6dc62017-05-26 16:07:36 +01004401 /* Drop unexpected ApplicationData records,
4402 * except at the beginning of renegotiations */
4403 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4404 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4405#if defined(MBEDTLS_SSL_RENEGOTIATION)
4406 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4407 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4408#endif
4409 )
4410 {
4411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4412 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4413 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004414 }
4415#endif /* MBEDTLS_SSL_PROTO_DTLS */
4416
Hanno Becker52c6dc62017-05-26 16:07:36 +01004417
4418 /* Check length against bounds of the current transform and version */
4419 if( ssl->transform_in == NULL )
4420 {
4421 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004422 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004423 {
4424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4425 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4426 }
4427 }
4428 else
4429 {
4430 if( ssl->in_msglen < ssl->transform_in->minlen )
4431 {
4432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4433 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4434 }
4435
4436#if defined(MBEDTLS_SSL_PROTO_SSL3)
4437 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004438 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004439 {
4440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4441 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4442 }
4443#endif
4444#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4445 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4446 /*
4447 * TLS encrypted messages can have up to 256 bytes of padding
4448 */
4449 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4450 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004451 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004452 {
4453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4454 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4455 }
4456#endif
4457 }
4458
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004459 return( 0 );
4460}
Paul Bakker5121ce52009-01-03 21:22:43 +00004461
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004462/*
4463 * If applicable, decrypt (and decompress) record content
4464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004465static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004466{
4467 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004469 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4470 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004472#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4473 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004475 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004477 ret = mbedtls_ssl_hw_record_read( ssl );
4478 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004480 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4481 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004482 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004483
4484 if( ret == 0 )
4485 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004486 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004487#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004488 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004489 {
4490 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004492 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004493 return( ret );
4494 }
4495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004496 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004497 ssl->in_msg, ssl->in_msglen );
4498
Angus Grattond8213d02016-05-25 20:56:48 +10004499 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4502 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004503 }
4504 }
4505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004506#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004507 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004508 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004509 {
4510 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004512 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004513 return( ret );
4514 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004515 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004516#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004518#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004519 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004521 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004522 }
4523#endif
4524
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004525 return( 0 );
4526}
4527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004528static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004529
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004530/*
4531 * Read a record.
4532 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004533 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4534 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4535 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004536 */
Hanno Becker1097b342018-08-15 14:09:41 +01004537
4538/* Helper functions for mbedtls_ssl_read_record(). */
4539static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004540static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4541static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004542
Hanno Becker327c93b2018-08-15 13:56:18 +01004543int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004544 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004545{
4546 int ret;
4547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004549
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004550 if( ssl->keep_current_message == 0 )
4551 {
4552 do {
Simon Butcher99000142016-10-13 17:21:01 +01004553
Hanno Becker26994592018-08-15 14:14:59 +01004554 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004555 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004556 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004557
Hanno Beckere74d5562018-08-15 14:26:08 +01004558 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004559 {
Hanno Becker40f50842018-08-15 14:48:01 +01004560#if defined(MBEDTLS_SSL_PROTO_DTLS)
4561 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004562
Hanno Becker40f50842018-08-15 14:48:01 +01004563 /* We only check for buffered messages if the
4564 * current datagram is fully consumed. */
4565 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004566 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004567 {
Hanno Becker40f50842018-08-15 14:48:01 +01004568 if( ssl_load_buffered_message( ssl ) == 0 )
4569 have_buffered = 1;
4570 }
4571
4572 if( have_buffered == 0 )
4573#endif /* MBEDTLS_SSL_PROTO_DTLS */
4574 {
4575 ret = ssl_get_next_record( ssl );
4576 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4577 continue;
4578
4579 if( ret != 0 )
4580 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004581 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004582 return( ret );
4583 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004584 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004585 }
4586
4587 ret = mbedtls_ssl_handle_message_type( ssl );
4588
Hanno Becker40f50842018-08-15 14:48:01 +01004589#if defined(MBEDTLS_SSL_PROTO_DTLS)
4590 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4591 {
4592 /* Buffer future message */
4593 ret = ssl_buffer_message( ssl );
4594 if( ret != 0 )
4595 return( ret );
4596
4597 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4598 }
4599#endif /* MBEDTLS_SSL_PROTO_DTLS */
4600
Hanno Becker90333da2017-10-10 11:27:13 +01004601 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4602 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004603
4604 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004605 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004606 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004607 return( ret );
4608 }
4609
Hanno Becker327c93b2018-08-15 13:56:18 +01004610 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004611 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004612 {
4613 mbedtls_ssl_update_handshake_status( ssl );
4614 }
Simon Butcher99000142016-10-13 17:21:01 +01004615 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004616 else
Simon Butcher99000142016-10-13 17:21:01 +01004617 {
Hanno Becker02f59072018-08-15 14:00:24 +01004618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004619 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004620 }
4621
4622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4623
4624 return( 0 );
4625}
4626
Hanno Becker40f50842018-08-15 14:48:01 +01004627#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004628static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004629{
Hanno Becker40f50842018-08-15 14:48:01 +01004630 if( ssl->in_left > ssl->next_record_offset )
4631 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004632
Hanno Becker40f50842018-08-15 14:48:01 +01004633 return( 0 );
4634}
4635
4636static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4637{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004638 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004639 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004640 int ret = 0;
4641
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004642 if( hs == NULL )
4643 return( -1 );
4644
Hanno Beckere00ae372018-08-20 09:39:42 +01004645 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4646
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004647 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4648 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4649 {
4650 /* Check if we have seen a ChangeCipherSpec before.
4651 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004652 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004653 {
4654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4655 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004656 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004657 }
4658
Hanno Becker39b8bc92018-08-28 17:17:13 +01004659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004660 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4661 ssl->in_msglen = 1;
4662 ssl->in_msg[0] = 1;
4663
4664 /* As long as they are equal, the exact value doesn't matter. */
4665 ssl->in_left = 0;
4666 ssl->next_record_offset = 0;
4667
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004668 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004669 goto exit;
4670 }
Hanno Becker37f95322018-08-16 13:55:32 +01004671
Hanno Beckerb8f50142018-08-28 10:01:34 +01004672#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004673 /* Debug only */
4674 {
4675 unsigned offset;
4676 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4677 {
4678 hs_buf = &hs->buffering.hs[offset];
4679 if( hs_buf->is_valid == 1 )
4680 {
4681 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4682 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004683 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004684 }
4685 }
4686 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004687#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004688
4689 /* Check if we have buffered and/or fully reassembled the
4690 * next handshake message. */
4691 hs_buf = &hs->buffering.hs[0];
4692 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4693 {
4694 /* Synthesize a record containing the buffered HS message. */
4695 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4696 ( hs_buf->data[2] << 8 ) |
4697 hs_buf->data[3];
4698
4699 /* Double-check that we haven't accidentally buffered
4700 * a message that doesn't fit into the input buffer. */
4701 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4702 {
4703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4704 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4705 }
4706
4707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4708 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4709 hs_buf->data, msg_len + 12 );
4710
4711 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4712 ssl->in_hslen = msg_len + 12;
4713 ssl->in_msglen = msg_len + 12;
4714 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4715
4716 ret = 0;
4717 goto exit;
4718 }
4719 else
4720 {
4721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4722 hs->in_msg_seq ) );
4723 }
4724
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004725 ret = -1;
4726
4727exit:
4728
4729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4730 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004731}
4732
Hanno Beckera02b0b42018-08-21 17:20:27 +01004733static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4734 size_t desired )
4735{
4736 int offset;
4737 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4739 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004740
Hanno Becker01315ea2018-08-21 17:22:17 +01004741 /* Get rid of future records epoch first, if such exist. */
4742 ssl_free_buffered_record( ssl );
4743
4744 /* Check if we have enough space available now. */
4745 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4746 hs->buffering.total_bytes_buffered ) )
4747 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004749 return( 0 );
4750 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004751
Hanno Becker4f432ad2018-08-28 10:02:32 +01004752 /* We don't have enough space to buffer the next expected handshake
4753 * message. Remove buffers used for future messages to gain space,
4754 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004755 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4756 offset >= 0; offset-- )
4757 {
4758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4759 offset ) );
4760
Hanno Beckerb309b922018-08-23 13:18:05 +01004761 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004762
4763 /* Check if we have enough space available now. */
4764 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4765 hs->buffering.total_bytes_buffered ) )
4766 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004768 return( 0 );
4769 }
4770 }
4771
4772 return( -1 );
4773}
4774
Hanno Becker40f50842018-08-15 14:48:01 +01004775static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4776{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004777 int ret = 0;
4778 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4779
4780 if( hs == NULL )
4781 return( 0 );
4782
4783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4784
4785 switch( ssl->in_msgtype )
4786 {
4787 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004789
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004790 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004791 break;
4792
4793 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004794 {
4795 unsigned recv_msg_seq_offset;
4796 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4797 mbedtls_ssl_hs_buffer *hs_buf;
4798 size_t msg_len = ssl->in_hslen - 12;
4799
4800 /* We should never receive an old handshake
4801 * message - double-check nonetheless. */
4802 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4803 {
4804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4805 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4806 }
4807
4808 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4809 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4810 {
4811 /* Silently ignore -- message too far in the future */
4812 MBEDTLS_SSL_DEBUG_MSG( 2,
4813 ( "Ignore future HS message with sequence number %u, "
4814 "buffering window %u - %u",
4815 recv_msg_seq, ssl->handshake->in_msg_seq,
4816 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4817
4818 goto exit;
4819 }
4820
4821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4822 recv_msg_seq, recv_msg_seq_offset ) );
4823
4824 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4825
4826 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004827 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004828 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004829 size_t reassembly_buf_sz;
4830
Hanno Becker37f95322018-08-16 13:55:32 +01004831 hs_buf->is_fragmented =
4832 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4833
4834 /* We copy the message back into the input buffer
4835 * after reassembly, so check that it's not too large.
4836 * This is an implementation-specific limitation
4837 * and not one from the standard, hence it is not
4838 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004839 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004840 {
4841 /* Ignore message */
4842 goto exit;
4843 }
4844
Hanno Beckere0b150f2018-08-21 15:51:03 +01004845 /* Check if we have enough space to buffer the message. */
4846 if( hs->buffering.total_bytes_buffered >
4847 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4848 {
4849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4850 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4851 }
4852
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004853 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4854 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004855
4856 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4857 hs->buffering.total_bytes_buffered ) )
4858 {
4859 if( recv_msg_seq_offset > 0 )
4860 {
4861 /* If we can't buffer a future message because
4862 * of space limitations -- ignore. */
4863 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",
4864 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4865 (unsigned) hs->buffering.total_bytes_buffered ) );
4866 goto exit;
4867 }
Hanno Beckere1801392018-08-21 16:51:05 +01004868 else
4869 {
4870 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",
4871 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4872 (unsigned) hs->buffering.total_bytes_buffered ) );
4873 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004874
Hanno Beckera02b0b42018-08-21 17:20:27 +01004875 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004876 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004877 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",
4878 (unsigned) msg_len,
4879 (unsigned) reassembly_buf_sz,
4880 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004881 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004882 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4883 goto exit;
4884 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004885 }
4886
4887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4888 msg_len ) );
4889
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004890 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4891 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004892 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004893 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004894 goto exit;
4895 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004896 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004897
4898 /* Prepare final header: copy msg_type, length and message_seq,
4899 * then add standardised fragment_offset and fragment_length */
4900 memcpy( hs_buf->data, ssl->in_msg, 6 );
4901 memset( hs_buf->data + 6, 0, 3 );
4902 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4903
4904 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004905
4906 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004907 }
4908 else
4909 {
4910 /* Make sure msg_type and length are consistent */
4911 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4912 {
4913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4914 /* Ignore */
4915 goto exit;
4916 }
4917 }
4918
Hanno Becker4422bbb2018-08-20 09:40:19 +01004919 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004920 {
4921 size_t frag_len, frag_off;
4922 unsigned char * const msg = hs_buf->data + 12;
4923
4924 /*
4925 * Check and copy current fragment
4926 */
4927
4928 /* Validation of header fields already done in
4929 * mbedtls_ssl_prepare_handshake_record(). */
4930 frag_off = ssl_get_hs_frag_off( ssl );
4931 frag_len = ssl_get_hs_frag_len( ssl );
4932
4933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4934 frag_off, frag_len ) );
4935 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4936
4937 if( hs_buf->is_fragmented )
4938 {
4939 unsigned char * const bitmask = msg + msg_len;
4940 ssl_bitmask_set( bitmask, frag_off, frag_len );
4941 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4942 msg_len ) == 0 );
4943 }
4944 else
4945 {
4946 hs_buf->is_complete = 1;
4947 }
4948
4949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4950 hs_buf->is_complete ? "" : "not yet " ) );
4951 }
4952
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004953 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004954 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004955
4956 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004957 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004958 break;
4959 }
4960
4961exit:
4962
4963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4964 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004965}
4966#endif /* MBEDTLS_SSL_PROTO_DTLS */
4967
Hanno Becker1097b342018-08-15 14:09:41 +01004968static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004969{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004970 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004971 * Consume last content-layer message and potentially
4972 * update in_msglen which keeps track of the contents'
4973 * consumption state.
4974 *
4975 * (1) Handshake messages:
4976 * Remove last handshake message, move content
4977 * and adapt in_msglen.
4978 *
4979 * (2) Alert messages:
4980 * Consume whole record content, in_msglen = 0.
4981 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004982 * (3) Change cipher spec:
4983 * Consume whole record content, in_msglen = 0.
4984 *
4985 * (4) Application data:
4986 * Don't do anything - the record layer provides
4987 * the application data as a stream transport
4988 * and consumes through mbedtls_ssl_read only.
4989 *
4990 */
4991
4992 /* Case (1): Handshake messages */
4993 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004994 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004995 /* Hard assertion to be sure that no application data
4996 * is in flight, as corrupting ssl->in_msglen during
4997 * ssl->in_offt != NULL is fatal. */
4998 if( ssl->in_offt != NULL )
4999 {
5000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5001 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5002 }
5003
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005004 /*
5005 * Get next Handshake message in the current record
5006 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005007
Hanno Becker4a810fb2017-05-24 16:27:30 +01005008 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005009 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005010 * current handshake content: If DTLS handshake
5011 * fragmentation is used, that's the fragment
5012 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005013 * size here is faulty and should be changed at
5014 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005015 * (2) While it doesn't seem to cause problems, one
5016 * has to be very careful not to assume that in_hslen
5017 * is always <= in_msglen in a sensible communication.
5018 * Again, it's wrong for DTLS handshake fragmentation.
5019 * The following check is therefore mandatory, and
5020 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005021 * Additionally, ssl->in_hslen might be arbitrarily out of
5022 * bounds after handling a DTLS message with an unexpected
5023 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005024 */
5025 if( ssl->in_hslen < ssl->in_msglen )
5026 {
5027 ssl->in_msglen -= ssl->in_hslen;
5028 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5029 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005030
Hanno Becker4a810fb2017-05-24 16:27:30 +01005031 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5032 ssl->in_msg, ssl->in_msglen );
5033 }
5034 else
5035 {
5036 ssl->in_msglen = 0;
5037 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005038
Hanno Becker4a810fb2017-05-24 16:27:30 +01005039 ssl->in_hslen = 0;
5040 }
5041 /* Case (4): Application data */
5042 else if( ssl->in_offt != NULL )
5043 {
5044 return( 0 );
5045 }
5046 /* Everything else (CCS & Alerts) */
5047 else
5048 {
5049 ssl->in_msglen = 0;
5050 }
5051
Hanno Becker1097b342018-08-15 14:09:41 +01005052 return( 0 );
5053}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005054
Hanno Beckere74d5562018-08-15 14:26:08 +01005055static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5056{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005057 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005058 return( 1 );
5059
5060 return( 0 );
5061}
5062
Hanno Becker5f066e72018-08-16 14:56:31 +01005063#if defined(MBEDTLS_SSL_PROTO_DTLS)
5064
5065static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5066{
5067 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5068 if( hs == NULL )
5069 return;
5070
Hanno Becker01315ea2018-08-21 17:22:17 +01005071 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005072 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005073 hs->buffering.total_bytes_buffered -=
5074 hs->buffering.future_record.len;
5075
5076 mbedtls_free( hs->buffering.future_record.data );
5077 hs->buffering.future_record.data = NULL;
5078 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005079}
5080
5081static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5082{
5083 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5084 unsigned char * rec;
5085 size_t rec_len;
5086 unsigned rec_epoch;
5087
5088 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5089 return( 0 );
5090
5091 if( hs == NULL )
5092 return( 0 );
5093
Hanno Becker5f066e72018-08-16 14:56:31 +01005094 rec = hs->buffering.future_record.data;
5095 rec_len = hs->buffering.future_record.len;
5096 rec_epoch = hs->buffering.future_record.epoch;
5097
5098 if( rec == NULL )
5099 return( 0 );
5100
Hanno Becker4cb782d2018-08-20 11:19:05 +01005101 /* Only consider loading future records if the
5102 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005103 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005104 return( 0 );
5105
Hanno Becker5f066e72018-08-16 14:56:31 +01005106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5107
5108 if( rec_epoch != ssl->in_epoch )
5109 {
5110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5111 goto exit;
5112 }
5113
5114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5115
5116 /* Double-check that the record is not too large */
5117 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5118 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5119 {
5120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5121 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5122 }
5123
5124 memcpy( ssl->in_hdr, rec, rec_len );
5125 ssl->in_left = rec_len;
5126 ssl->next_record_offset = 0;
5127
5128 ssl_free_buffered_record( ssl );
5129
5130exit:
5131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5132 return( 0 );
5133}
5134
5135static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5136{
5137 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5138 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005139 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005140
5141 /* Don't buffer future records outside handshakes. */
5142 if( hs == NULL )
5143 return( 0 );
5144
5145 /* Only buffer handshake records (we are only interested
5146 * in Finished messages). */
5147 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5148 return( 0 );
5149
5150 /* Don't buffer more than one future epoch record. */
5151 if( hs->buffering.future_record.data != NULL )
5152 return( 0 );
5153
Hanno Becker01315ea2018-08-21 17:22:17 +01005154 /* Don't buffer record if there's not enough buffering space remaining. */
5155 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5156 hs->buffering.total_bytes_buffered ) )
5157 {
5158 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",
5159 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5160 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005161 return( 0 );
5162 }
5163
Hanno Becker5f066e72018-08-16 14:56:31 +01005164 /* Buffer record */
5165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5166 ssl->in_epoch + 1 ) );
5167 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5168 rec_hdr_len + ssl->in_msglen );
5169
5170 /* ssl_parse_record_header() only considers records
5171 * of the next epoch as candidates for buffering. */
5172 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005173 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005174
5175 hs->buffering.future_record.data =
5176 mbedtls_calloc( 1, hs->buffering.future_record.len );
5177 if( hs->buffering.future_record.data == NULL )
5178 {
5179 /* If we run out of RAM trying to buffer a
5180 * record from the next epoch, just ignore. */
5181 return( 0 );
5182 }
5183
Hanno Becker01315ea2018-08-21 17:22:17 +01005184 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005185
Hanno Becker01315ea2018-08-21 17:22:17 +01005186 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005187 return( 0 );
5188}
5189
5190#endif /* MBEDTLS_SSL_PROTO_DTLS */
5191
Hanno Beckere74d5562018-08-15 14:26:08 +01005192static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005193{
5194 int ret;
5195
Hanno Becker5f066e72018-08-16 14:56:31 +01005196#if defined(MBEDTLS_SSL_PROTO_DTLS)
5197 /* We might have buffered a future record; if so,
5198 * and if the epoch matches now, load it.
5199 * On success, this call will set ssl->in_left to
5200 * the length of the buffered record, so that
5201 * the calls to ssl_fetch_input() below will
5202 * essentially be no-ops. */
5203 ret = ssl_load_buffered_record( ssl );
5204 if( ret != 0 )
5205 return( ret );
5206#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005208 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005210 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005211 return( ret );
5212 }
5213
5214 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005216#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005217 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5218 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005219 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005220 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5221 {
5222 ret = ssl_buffer_future_record( ssl );
5223 if( ret != 0 )
5224 return( ret );
5225
5226 /* Fall through to handling of unexpected records */
5227 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5228 }
5229
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005230 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5231 {
5232 /* Skip unexpected record (but not whole datagram) */
5233 ssl->next_record_offset = ssl->in_msglen
5234 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005235
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5237 "(header)" ) );
5238 }
5239 else
5240 {
5241 /* Skip invalid record and the rest of the datagram */
5242 ssl->next_record_offset = 0;
5243 ssl->in_left = 0;
5244
5245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5246 "(header)" ) );
5247 }
5248
5249 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005250 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005251 }
5252#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005253 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005254 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005255
5256 /*
5257 * Read and optionally decrypt the message contents
5258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005259 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5260 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005262 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005263 return( ret );
5264 }
5265
5266 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005267#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005268 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005270 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005271 if( ssl->next_record_offset < ssl->in_left )
5272 {
5273 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5274 }
5275 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005276 else
5277#endif
5278 ssl->in_left = 0;
5279
5280 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005282#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005283 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005284 {
5285 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005286 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5287 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005288 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005289 /* Except when waiting for Finished as a bad mac here
5290 * probably means something went wrong in the handshake
5291 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5292 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5293 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5294 {
5295#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5296 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5297 {
5298 mbedtls_ssl_send_alert_message( ssl,
5299 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5300 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5301 }
5302#endif
5303 return( ret );
5304 }
5305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005306#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005307 if( ssl->conf->badmac_limit != 0 &&
5308 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5311 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005312 }
5313#endif
5314
Hanno Becker4a810fb2017-05-24 16:27:30 +01005315 /* As above, invalid records cause
5316 * dismissal of the whole datagram. */
5317
5318 ssl->next_record_offset = 0;
5319 ssl->in_left = 0;
5320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005322 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005323 }
5324
5325 return( ret );
5326 }
5327 else
5328#endif
5329 {
5330 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005331#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5332 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005334 mbedtls_ssl_send_alert_message( ssl,
5335 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5336 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005337 }
5338#endif
5339 return( ret );
5340 }
5341 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005342
Simon Butcher99000142016-10-13 17:21:01 +01005343 return( 0 );
5344}
5345
5346int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5347{
5348 int ret;
5349
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005350 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005351 * Handle particular types of records
5352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005354 {
Simon Butcher99000142016-10-13 17:21:01 +01005355 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5356 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005357 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005358 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005359 }
5360
Hanno Beckere678eaa2018-08-21 14:57:46 +01005361 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005362 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005363 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005364 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5366 ssl->in_msglen ) );
5367 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005368 }
5369
Hanno Beckere678eaa2018-08-21 14:57:46 +01005370 if( ssl->in_msg[0] != 1 )
5371 {
5372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5373 ssl->in_msg[0] ) );
5374 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5375 }
5376
5377#if defined(MBEDTLS_SSL_PROTO_DTLS)
5378 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5379 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5380 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5381 {
5382 if( ssl->handshake == NULL )
5383 {
5384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5385 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5386 }
5387
5388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5389 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5390 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005391#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005392 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005394 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005395 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005396 if( ssl->in_msglen != 2 )
5397 {
5398 /* Note: Standard allows for more than one 2 byte alert
5399 to be packed in a single message, but Mbed TLS doesn't
5400 currently support this. */
5401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5402 ssl->in_msglen ) );
5403 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5404 }
5405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005407 ssl->in_msg[0], ssl->in_msg[1] ) );
5408
5409 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005410 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005412 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005415 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005417 }
5418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005419 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5420 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5423 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005424 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005425
5426#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5427 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5428 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5429 {
Hanno Becker90333da2017-10-10 11:27:13 +01005430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005431 /* Will be handled when trying to parse ServerHello */
5432 return( 0 );
5433 }
5434#endif
5435
5436#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5437 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5438 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5439 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5440 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5441 {
5442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5443 /* Will be handled in mbedtls_ssl_parse_certificate() */
5444 return( 0 );
5445 }
5446#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5447
5448 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005449 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005450 }
5451
Hanno Beckerc76c6192017-06-06 10:03:17 +01005452#if defined(MBEDTLS_SSL_PROTO_DTLS)
5453 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5454 ssl->handshake != NULL &&
5455 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5456 {
5457 ssl_handshake_wrapup_free_hs_transform( ssl );
5458 }
5459#endif
5460
Paul Bakker5121ce52009-01-03 21:22:43 +00005461 return( 0 );
5462}
5463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005465{
5466 int ret;
5467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5469 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5470 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005471 {
5472 return( ret );
5473 }
5474
5475 return( 0 );
5476}
5477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005478int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005479 unsigned char level,
5480 unsigned char message )
5481{
5482 int ret;
5483
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005484 if( ssl == NULL || ssl->conf == NULL )
5485 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005488 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005491 ssl->out_msglen = 2;
5492 ssl->out_msg[0] = level;
5493 ssl->out_msg[1] = message;
5494
Hanno Becker67bc7c32018-08-06 11:33:50 +01005495 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005498 return( ret );
5499 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005501
5502 return( 0 );
5503}
5504
Paul Bakker5121ce52009-01-03 21:22:43 +00005505/*
5506 * Handshake functions
5507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005508#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5509 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5510 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5511 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5512 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5513 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5514 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005515/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005516int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005518 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005522 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5523 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005524 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5525 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005528 ssl->state++;
5529 return( 0 );
5530 }
5531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5533 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005534}
5535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005536int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005537{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005538 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005542 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5543 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005544 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5545 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005548 ssl->state++;
5549 return( 0 );
5550 }
5551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5553 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005554}
Gilles Peskinef9828522017-05-03 12:28:43 +02005555
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005556#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005557/* Some certificate support -> implement write and parse */
5558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005559int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005560{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005561 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005562 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563 const mbedtls_x509_crt *crt;
5564 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005568 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5569 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005570 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5571 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005574 ssl->state++;
5575 return( 0 );
5576 }
5577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005578#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005579 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005580 {
5581 if( ssl->client_auth == 0 )
5582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005584 ssl->state++;
5585 return( 0 );
5586 }
5587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005588#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005589 /*
5590 * If using SSLv3 and got no cert, send an Alert message
5591 * (otherwise an empty Certificate message will be sent).
5592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005593 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5594 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005595 {
5596 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005597 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5598 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5599 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005602 goto write_msg;
5603 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005605 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005606#endif /* MBEDTLS_SSL_CLI_C */
5607#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005608 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005610 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5613 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005614 }
5615 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005616#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005618 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005619
5620 /*
5621 * 0 . 0 handshake type
5622 * 1 . 3 handshake length
5623 * 4 . 6 length of all certs
5624 * 7 . 9 length of cert. 1
5625 * 10 . n-1 peer certificate
5626 * n . n+2 length of cert. 2
5627 * n+3 . ... upper level cert, etc.
5628 */
5629 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005630 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005631
Paul Bakker29087132010-03-21 21:03:34 +00005632 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005633 {
5634 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005635 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005638 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005639 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005640 }
5641
5642 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5643 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5644 ssl->out_msg[i + 2] = (unsigned char)( n );
5645
5646 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5647 i += n; crt = crt->next;
5648 }
5649
5650 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5651 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5652 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5653
5654 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005655 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5656 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005657
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005658#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005659write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005660#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005661
5662 ssl->state++;
5663
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005664 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005665 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005666 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005667 return( ret );
5668 }
5669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005671
Paul Bakkered27a042013-04-18 22:46:23 +02005672 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005673}
5674
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005675/*
5676 * Once the certificate message is read, parse it into a cert chain and
5677 * perform basic checks, but leave actual verification to the caller
5678 */
5679static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005680{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005681 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005682 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005683 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005685#if defined(MBEDTLS_SSL_SRV_C)
5686#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005687 /*
5688 * Check if the client sent an empty certificate
5689 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005690 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005691 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005692 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005693 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005694 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5695 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5696 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005699
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005700 /* The client was asked for a certificate but didn't send
5701 one. The client should know what's going on, so we
5702 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005703 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005704 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005705 }
5706 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005709#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5710 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005711 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005714 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5715 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5716 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5717 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005720
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005721 /* The client was asked for a certificate but didn't send
5722 one. The client should know what's going on, so we
5723 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005724 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005725 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005726 }
5727 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5729 MBEDTLS_SSL_PROTO_TLS1_2 */
5730#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005732 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005735 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5736 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005737 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005738 }
5739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005740 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5741 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005744 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5745 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005746 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005747 }
5748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005749 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005750
Paul Bakker5121ce52009-01-03 21:22:43 +00005751 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005752 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005753 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005754 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005755
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005756 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005757 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005760 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5761 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005762 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005763 }
5764
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005765 /* In case we tried to reuse a session but it failed */
5766 if( ssl->session_negotiate->peer_cert != NULL )
5767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005768 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5769 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005770 }
5771
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005772 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005773 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005774 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005775 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005776 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005777 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5778 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005779 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005780 }
5781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005783
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005784 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005785
5786 while( i < ssl->in_hslen )
5787 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005788 if ( i + 3 > ssl->in_hslen ) {
5789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5790 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5791 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5792 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5793 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005794 if( ssl->in_msg[i] != 0 )
5795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005797 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5798 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005799 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005800 }
5801
5802 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5803 | (unsigned int) ssl->in_msg[i + 2];
5804 i += 3;
5805
5806 if( n < 128 || i + n > ssl->in_hslen )
5807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005809 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5810 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005811 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005812 }
5813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005814 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005815 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005816 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005817 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005818 case 0: /*ok*/
5819 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5820 /* Ignore certificate with an unknown algorithm: maybe a
5821 prior certificate was already trusted. */
5822 break;
5823
5824 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5825 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5826 goto crt_parse_der_failed;
5827
5828 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5829 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5830 goto crt_parse_der_failed;
5831
5832 default:
5833 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5834 crt_parse_der_failed:
5835 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005836 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005837 return( ret );
5838 }
5839
5840 i += n;
5841 }
5842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005843 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005844
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005845 /*
5846 * On client, make sure the server cert doesn't change during renego to
5847 * avoid "triple handshake" attack: https://secure-resumption.com/
5848 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005849#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005850 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005851 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005852 {
5853 if( ssl->session->peer_cert == NULL )
5854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005856 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5857 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005858 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005859 }
5860
5861 if( ssl->session->peer_cert->raw.len !=
5862 ssl->session_negotiate->peer_cert->raw.len ||
5863 memcmp( ssl->session->peer_cert->raw.p,
5864 ssl->session_negotiate->peer_cert->raw.p,
5865 ssl->session->peer_cert->raw.len ) != 0 )
5866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005868 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5869 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005870 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005871 }
5872 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005873#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005874
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005875 return( 0 );
5876}
5877
5878int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5879{
5880 int ret;
5881 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5882 ssl->transform_negotiate->ciphersuite_info;
5883#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5884 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
5885 ? ssl->handshake->sni_authmode
5886 : ssl->conf->authmode;
5887#else
5888 const int authmode = ssl->conf->authmode;
5889#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005890 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005891
5892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5893
5894 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5895 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
5896 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5897 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5898 {
5899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5900 ssl->state++;
5901 return( 0 );
5902 }
5903
5904#if defined(MBEDTLS_SSL_SRV_C)
5905 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5906 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5907 {
5908 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5909 ssl->state++;
5910 return( 0 );
5911 }
5912
5913 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5914 authmode == MBEDTLS_SSL_VERIFY_NONE )
5915 {
5916 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5917 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005918
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005919 ssl->state++;
5920 return( 0 );
5921 }
5922#endif
5923
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005924#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5925 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005926 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005927 {
5928 goto crt_verify;
5929 }
5930#endif
5931
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02005932 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005933 {
5934 /* mbedtls_ssl_read_record may have sent an alert already. We
5935 let it decide whether to alert. */
5936 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5937 return( ret );
5938 }
5939
5940 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
5941 {
5942#if defined(MBEDTLS_SSL_SRV_C)
5943 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
5944 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
5945 {
5946 ret = 0;
5947 }
5948#endif
5949
5950 ssl->state++;
5951 return( ret );
5952 }
5953
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005954#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5955 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005956 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005957
5958crt_verify:
5959 if( ssl->handshake->ecrs_enabled)
5960 rs_ctx = &ssl->handshake->ecrs_ctx;
5961#endif
5962
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005963 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005964 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005965 mbedtls_x509_crt *ca_chain;
5966 mbedtls_x509_crl *ca_crl;
5967
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005968#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005969 if( ssl->handshake->sni_ca_chain != NULL )
5970 {
5971 ca_chain = ssl->handshake->sni_ca_chain;
5972 ca_crl = ssl->handshake->sni_ca_crl;
5973 }
5974 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005975#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005976 {
5977 ca_chain = ssl->conf->ca_chain;
5978 ca_crl = ssl->conf->ca_crl;
5979 }
5980
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005981 /*
5982 * Main check: verify certificate
5983 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005984 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005985 ssl->session_negotiate->peer_cert,
5986 ca_chain, ca_crl,
5987 ssl->conf->cert_profile,
5988 ssl->hostname,
5989 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005990 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00005991
5992 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005994 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005995 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005996
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005997#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5998 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02005999 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006000#endif
6001
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006002 /*
6003 * Secondary checks: always done, but change 'ret' only if it was 0
6004 */
6005
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006006#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006009
6010 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006011 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006012 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006013 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006014 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006017 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006018 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006019 }
6020 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006021#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006023 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006024 ciphersuite_info,
6025 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006026 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006029 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006031 }
6032
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006033 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6034 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6035 * with details encoded in the verification flags. All other kinds
6036 * of error codes, including those from the user provided f_vrfy
6037 * functions, are treated as fatal and lead to a failure of
6038 * ssl_parse_certificate even if verification was optional. */
6039 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6040 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6041 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6042 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006043 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006044 }
6045
6046 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6047 {
6048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6049 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6050 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006051
6052 if( ret != 0 )
6053 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006054 uint8_t alert;
6055
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006056 /* The certificate may have been rejected for several reasons.
6057 Pick one and send the corresponding alert. Which alert to send
6058 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006059 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6060 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6061 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6062 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6063 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6064 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6065 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6066 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6067 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6068 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6069 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6070 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6071 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6072 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6073 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6074 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6075 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6076 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6077 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6078 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006079 else
6080 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006081 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6082 alert );
6083 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006084
Hanno Beckere6706e62017-05-15 16:05:15 +01006085#if defined(MBEDTLS_DEBUG_C)
6086 if( ssl->session_negotiate->verify_result != 0 )
6087 {
6088 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6089 ssl->session_negotiate->verify_result ) );
6090 }
6091 else
6092 {
6093 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6094 }
6095#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006096 }
6097
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006098 ssl->state++;
6099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006100 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006101
6102 return( ret );
6103}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006104#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6105 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6106 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6107 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6108 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6109 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6110 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006112int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006113{
6114 int ret;
6115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006118 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006119 ssl->out_msglen = 1;
6120 ssl->out_msg[0] = 1;
6121
Paul Bakker5121ce52009-01-03 21:22:43 +00006122 ssl->state++;
6123
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006124 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006125 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006126 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006127 return( ret );
6128 }
6129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006131
6132 return( 0 );
6133}
6134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006135int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006136{
6137 int ret;
6138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006140
Hanno Becker327c93b2018-08-15 13:56:18 +01006141 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006143 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006144 return( ret );
6145 }
6146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006147 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006150 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6151 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006152 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006153 }
6154
Hanno Beckere678eaa2018-08-21 14:57:46 +01006155 /* CCS records are only accepted if they have length 1 and content '1',
6156 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006157
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006158 /*
6159 * Switch to our negotiated transform and session parameters for inbound
6160 * data.
6161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006162 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006163 ssl->transform_in = ssl->transform_negotiate;
6164 ssl->session_in = ssl->session_negotiate;
6165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006166#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006167 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006169#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006170 ssl_dtls_replay_reset( ssl );
6171#endif
6172
6173 /* Increment epoch */
6174 if( ++ssl->in_epoch == 0 )
6175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006177 /* This is highly unlikely to happen for legitimate reasons, so
6178 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006179 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006180 }
6181 }
6182 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006183#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006184 memset( ssl->in_ctr, 0, 8 );
6185
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006186 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006188#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6189 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006191 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006194 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6195 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006196 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006197 }
6198 }
6199#endif
6200
Paul Bakker5121ce52009-01-03 21:22:43 +00006201 ssl->state++;
6202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006204
6205 return( 0 );
6206}
6207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006208void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6209 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006210{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006211 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006213#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6214 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6215 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006216 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006217 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006218#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006219#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6220#if defined(MBEDTLS_SHA512_C)
6221 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006222 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6223 else
6224#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006225#if defined(MBEDTLS_SHA256_C)
6226 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006227 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006228 else
6229#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006233 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006234 }
Paul Bakker380da532012-04-18 16:10:25 +00006235}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006237void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006238{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6240 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006241 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6242 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006243#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006244#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6245#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006246 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006247#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006248#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006249 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006250#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006252}
6253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006255 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006256{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006257#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6258 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006259 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6260 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006261#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006262#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6263#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006264 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006265#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006266#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006267 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006268#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006269#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006270}
6271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006272#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6273 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6274static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006275 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006276{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006277 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6278 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006279}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006280#endif
Paul Bakker380da532012-04-18 16:10:25 +00006281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006282#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6283#if defined(MBEDTLS_SHA256_C)
6284static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006285 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006286{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006287 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006288}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006289#endif
Paul Bakker380da532012-04-18 16:10:25 +00006290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006291#if defined(MBEDTLS_SHA512_C)
6292static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006293 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006294{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006295 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006296}
Paul Bakker769075d2012-11-24 11:26:46 +01006297#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006298#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006300#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006301static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006302 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006303{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006304 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006305 mbedtls_md5_context md5;
6306 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006307
Paul Bakker5121ce52009-01-03 21:22:43 +00006308 unsigned char padbuf[48];
6309 unsigned char md5sum[16];
6310 unsigned char sha1sum[20];
6311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006312 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006313 if( !session )
6314 session = ssl->session;
6315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006317
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006318 mbedtls_md5_init( &md5 );
6319 mbedtls_sha1_init( &sha1 );
6320
6321 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6322 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006323
6324 /*
6325 * SSLv3:
6326 * hash =
6327 * MD5( master + pad2 +
6328 * MD5( handshake + sender + master + pad1 ) )
6329 * + SHA1( master + pad2 +
6330 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006331 */
6332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006334 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6335 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006336#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006338#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006339 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6340 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006341#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006343 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006344 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006345
Paul Bakker1ef83d62012-04-11 12:09:53 +00006346 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006347
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006348 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6349 mbedtls_md5_update_ret( &md5, session->master, 48 );
6350 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6351 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006352
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006353 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6354 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6355 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6356 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006357
Paul Bakker1ef83d62012-04-11 12:09:53 +00006358 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006359
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006360 mbedtls_md5_starts_ret( &md5 );
6361 mbedtls_md5_update_ret( &md5, session->master, 48 );
6362 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6363 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6364 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006365
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006366 mbedtls_sha1_starts_ret( &sha1 );
6367 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6368 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6369 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6370 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006372 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006373
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006374 mbedtls_md5_free( &md5 );
6375 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006376
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006377 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6378 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6379 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006382}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006383#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006385#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006386static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006387 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006388{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006389 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006390 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006391 mbedtls_md5_context md5;
6392 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006393 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006395 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006396 if( !session )
6397 session = ssl->session;
6398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006400
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006401 mbedtls_md5_init( &md5 );
6402 mbedtls_sha1_init( &sha1 );
6403
6404 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6405 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006406
Paul Bakker1ef83d62012-04-11 12:09:53 +00006407 /*
6408 * TLSv1:
6409 * hash = PRF( master, finished_label,
6410 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6411 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006413#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006414 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6415 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006416#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006418#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006419 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6420 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006421#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006423 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006424 ? "client finished"
6425 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006426
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006427 mbedtls_md5_finish_ret( &md5, padbuf );
6428 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006429
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006430 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006431 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006433 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006434
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006435 mbedtls_md5_free( &md5 );
6436 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006437
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006438 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006441}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006444#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6445#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006446static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006447 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006448{
6449 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006450 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006451 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006452 unsigned char padbuf[32];
6453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006454 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006455 if( !session )
6456 session = ssl->session;
6457
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006458 mbedtls_sha256_init( &sha256 );
6459
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006461
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006462 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006463
6464 /*
6465 * TLSv1.2:
6466 * hash = PRF( master, finished_label,
6467 * Hash( handshake ) )[0.11]
6468 */
6469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006470#if !defined(MBEDTLS_SHA256_ALT)
6471 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006472 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006473#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006475 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006476 ? "client finished"
6477 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006478
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006479 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006480
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006481 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006482 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006485
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006486 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006487
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006488 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006491}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006492#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006494#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006495static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006496 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006497{
6498 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006499 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006500 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006501 unsigned char padbuf[48];
6502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006504 if( !session )
6505 session = ssl->session;
6506
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006507 mbedtls_sha512_init( &sha512 );
6508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006510
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006511 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006512
6513 /*
6514 * TLSv1.2:
6515 * hash = PRF( master, finished_label,
6516 * Hash( handshake ) )[0.11]
6517 */
6518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006520 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6521 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006522#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006524 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006525 ? "client finished"
6526 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006527
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006528 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006529
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006530 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006531 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006533 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006534
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006535 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006536
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006537 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006540}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006541#endif /* MBEDTLS_SHA512_C */
6542#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006544static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006545{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006547
6548 /*
6549 * Free our handshake params
6550 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006551 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006552 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006553 ssl->handshake = NULL;
6554
6555 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006556 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006557 */
6558 if( ssl->transform )
6559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006560 mbedtls_ssl_transform_free( ssl->transform );
6561 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006562 }
6563 ssl->transform = ssl->transform_negotiate;
6564 ssl->transform_negotiate = NULL;
6565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006567}
6568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006569void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006570{
6571 int resume = ssl->handshake->resume;
6572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006575#if defined(MBEDTLS_SSL_RENEGOTIATION)
6576 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006578 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006579 ssl->renego_records_seen = 0;
6580 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006581#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006582
6583 /*
6584 * Free the previous session and switch in the current one
6585 */
Paul Bakker0a597072012-09-25 21:55:46 +00006586 if( ssl->session )
6587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006588#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006589 /* RFC 7366 3.1: keep the EtM state */
6590 ssl->session_negotiate->encrypt_then_mac =
6591 ssl->session->encrypt_then_mac;
6592#endif
6593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594 mbedtls_ssl_session_free( ssl->session );
6595 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006596 }
6597 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006598 ssl->session_negotiate = NULL;
6599
Paul Bakker0a597072012-09-25 21:55:46 +00006600 /*
6601 * Add cache entry
6602 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006603 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006604 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006605 resume == 0 )
6606 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006607 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006609 }
Paul Bakker0a597072012-09-25 21:55:46 +00006610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006611#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006612 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006613 ssl->handshake->flight != NULL )
6614 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006615 /* Cancel handshake timer */
6616 ssl_set_timer( ssl, 0 );
6617
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006618 /* Keep last flight around in case we need to resend it:
6619 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006620 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006621 }
6622 else
6623#endif
6624 ssl_handshake_wrapup_free_hs_transform( ssl );
6625
Paul Bakker48916f92012-09-16 19:57:18 +00006626 ssl->state++;
6627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006628 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006629}
6630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006631int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006632{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006633 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006636
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006637 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006638
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006639 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006640
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006641 /*
6642 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6643 * may define some other value. Currently (early 2016), no defined
6644 * ciphersuite does this (and this is unlikely to change as activity has
6645 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006647 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006649#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006650 ssl->verify_data_len = hash_len;
6651 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006652#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006653
Paul Bakker5121ce52009-01-03 21:22:43 +00006654 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6656 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006657
6658 /*
6659 * In case of session resuming, invert the client and server
6660 * ChangeCipherSpec messages order.
6661 */
Paul Bakker0a597072012-09-25 21:55:46 +00006662 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006664#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006665 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006666 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006667#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006668#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006669 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006670 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006671#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006672 }
6673 else
6674 ssl->state++;
6675
Paul Bakker48916f92012-09-16 19:57:18 +00006676 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006677 * Switch to our negotiated transform and session parameters for outbound
6678 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006679 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006680 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006683 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006684 {
6685 unsigned char i;
6686
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006687 /* Remember current epoch settings for resending */
6688 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006689 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006690
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006691 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006692 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006693
6694 /* Increment epoch */
6695 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006696 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006697 break;
6698
6699 /* The loop goes to its end iff the counter is wrapping */
6700 if( i == 0 )
6701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6703 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006704 }
6705 }
6706 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006707#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006708 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006709
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006710 ssl->transform_out = ssl->transform_negotiate;
6711 ssl->session_out = ssl->session_negotiate;
6712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6714 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006716 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6719 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006720 }
6721 }
6722#endif
6723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006724#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006725 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006726 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006727#endif
6728
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006729 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006730 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006731 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006732 return( ret );
6733 }
6734
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006735#if defined(MBEDTLS_SSL_PROTO_DTLS)
6736 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6737 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6738 {
6739 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6740 return( ret );
6741 }
6742#endif
6743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006745
6746 return( 0 );
6747}
6748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006749#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006750#define SSL_MAX_HASH_LEN 36
6751#else
6752#define SSL_MAX_HASH_LEN 12
6753#endif
6754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006756{
Paul Bakker23986e52011-04-24 08:57:21 +00006757 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006758 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006759 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006762
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006763 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006764
Hanno Becker327c93b2018-08-15 13:56:18 +01006765 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006767 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006768 return( ret );
6769 }
6770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006771 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006774 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6775 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006777 }
6778
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006779 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006780#if defined(MBEDTLS_SSL_PROTO_SSL3)
6781 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006782 hash_len = 36;
6783 else
6784#endif
6785 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006787 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6788 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006791 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6792 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006793 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006794 }
6795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006797 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006800 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6801 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006802 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006803 }
6804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006805#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006806 ssl->verify_data_len = hash_len;
6807 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006808#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006809
Paul Bakker0a597072012-09-25 21:55:46 +00006810 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006812#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006813 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006814 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006815#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006816#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006817 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006818 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006819#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006820 }
6821 else
6822 ssl->state++;
6823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006824#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006825 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006827#endif
6828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006830
6831 return( 0 );
6832}
6833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006835{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006836 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006838#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6839 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6840 mbedtls_md5_init( &handshake->fin_md5 );
6841 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006842 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6843 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006844#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006845#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6846#if defined(MBEDTLS_SHA256_C)
6847 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006848 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006849#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850#if defined(MBEDTLS_SHA512_C)
6851 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006852 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006853#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006854#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006855
6856 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006857
6858#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6859 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6860 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6861#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006863#if defined(MBEDTLS_DHM_C)
6864 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006865#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006866#if defined(MBEDTLS_ECDH_C)
6867 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006868#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006869#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006870 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006871#if defined(MBEDTLS_SSL_CLI_C)
6872 handshake->ecjpake_cache = NULL;
6873 handshake->ecjpake_cache_len = 0;
6874#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006875#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006876
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006877#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02006878 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006879#endif
6880
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006881#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6882 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6883#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006884}
6885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006886static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006887{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006888 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006890 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6891 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006893 mbedtls_md_init( &transform->md_ctx_enc );
6894 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006895}
6896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006897void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006898{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006899 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006900}
6901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006902static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006903{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006904 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006905 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006906 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006907 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006909 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006910 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006911
6912 /*
6913 * Either the pointers are now NULL or cleared properly and can be freed.
6914 * Now allocate missing structures.
6915 */
6916 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006917 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006918 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006919 }
Paul Bakker48916f92012-09-16 19:57:18 +00006920
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006921 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006922 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006923 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006924 }
Paul Bakker48916f92012-09-16 19:57:18 +00006925
Paul Bakker82788fb2014-10-20 13:59:19 +02006926 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006927 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006928 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006929 }
Paul Bakker48916f92012-09-16 19:57:18 +00006930
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006931 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006932 if( ssl->handshake == NULL ||
6933 ssl->transform_negotiate == NULL ||
6934 ssl->session_negotiate == NULL )
6935 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006938 mbedtls_free( ssl->handshake );
6939 mbedtls_free( ssl->transform_negotiate );
6940 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006941
6942 ssl->handshake = NULL;
6943 ssl->transform_negotiate = NULL;
6944 ssl->session_negotiate = NULL;
6945
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006946 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006947 }
6948
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006949 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006951 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006952 ssl_handshake_params_init( ssl->handshake );
6953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006954#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006955 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6956 {
6957 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006958
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006959 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6960 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6961 else
6962 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006963
6964 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006965 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006966#endif
6967
Paul Bakker48916f92012-09-16 19:57:18 +00006968 return( 0 );
6969}
6970
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006971#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006972/* Dummy cookie callbacks for defaults */
6973static int ssl_cookie_write_dummy( void *ctx,
6974 unsigned char **p, unsigned char *end,
6975 const unsigned char *cli_id, size_t cli_id_len )
6976{
6977 ((void) ctx);
6978 ((void) p);
6979 ((void) end);
6980 ((void) cli_id);
6981 ((void) cli_id_len);
6982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006984}
6985
6986static int ssl_cookie_check_dummy( void *ctx,
6987 const unsigned char *cookie, size_t cookie_len,
6988 const unsigned char *cli_id, size_t cli_id_len )
6989{
6990 ((void) ctx);
6991 ((void) cookie);
6992 ((void) cookie_len);
6993 ((void) cli_id);
6994 ((void) cli_id_len);
6995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006996 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006997}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006998#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006999
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007000/* Once ssl->out_hdr as the address of the beginning of the
7001 * next outgoing record is set, deduce the other pointers.
7002 *
7003 * Note: For TLS, we save the implicit record sequence number
7004 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7005 * and the caller has to make sure there's space for this.
7006 */
7007
7008static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7009 mbedtls_ssl_transform *transform )
7010{
7011#if defined(MBEDTLS_SSL_PROTO_DTLS)
7012 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7013 {
7014 ssl->out_ctr = ssl->out_hdr + 3;
7015 ssl->out_len = ssl->out_hdr + 11;
7016 ssl->out_iv = ssl->out_hdr + 13;
7017 }
7018 else
7019#endif
7020 {
7021 ssl->out_ctr = ssl->out_hdr - 8;
7022 ssl->out_len = ssl->out_hdr + 3;
7023 ssl->out_iv = ssl->out_hdr + 5;
7024 }
7025
7026 /* Adjust out_msg to make space for explicit IV, if used. */
7027 if( transform != NULL &&
7028 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7029 {
7030 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7031 }
7032 else
7033 ssl->out_msg = ssl->out_iv;
7034}
7035
7036/* Once ssl->in_hdr as the address of the beginning of the
7037 * next incoming record is set, deduce the other pointers.
7038 *
7039 * Note: For TLS, we save the implicit record sequence number
7040 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7041 * and the caller has to make sure there's space for this.
7042 */
7043
7044static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7045 mbedtls_ssl_transform *transform )
7046{
7047#if defined(MBEDTLS_SSL_PROTO_DTLS)
7048 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7049 {
7050 ssl->in_ctr = ssl->in_hdr + 3;
7051 ssl->in_len = ssl->in_hdr + 11;
7052 ssl->in_iv = ssl->in_hdr + 13;
7053 }
7054 else
7055#endif
7056 {
7057 ssl->in_ctr = ssl->in_hdr - 8;
7058 ssl->in_len = ssl->in_hdr + 3;
7059 ssl->in_iv = ssl->in_hdr + 5;
7060 }
7061
7062 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7063 if( transform != NULL &&
7064 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7065 {
7066 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7067 }
7068 else
7069 ssl->in_msg = ssl->in_iv;
7070}
7071
Paul Bakker5121ce52009-01-03 21:22:43 +00007072/*
7073 * Initialize an SSL context
7074 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007075void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7076{
7077 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7078}
7079
7080/*
7081 * Setup an SSL context
7082 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007083
7084static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7085{
7086 /* Set the incoming and outgoing record pointers. */
7087#if defined(MBEDTLS_SSL_PROTO_DTLS)
7088 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7089 {
7090 ssl->out_hdr = ssl->out_buf;
7091 ssl->in_hdr = ssl->in_buf;
7092 }
7093 else
7094#endif /* MBEDTLS_SSL_PROTO_DTLS */
7095 {
7096 ssl->out_hdr = ssl->out_buf + 8;
7097 ssl->in_hdr = ssl->in_buf + 8;
7098 }
7099
7100 /* Derive other internal pointers. */
7101 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7102 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7103}
7104
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007105int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007106 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007107{
Paul Bakker48916f92012-09-16 19:57:18 +00007108 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007109
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007110 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007111
7112 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007113 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007114 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007115
7116 /* Set to NULL in case of an error condition */
7117 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007118
Angus Grattond8213d02016-05-25 20:56:48 +10007119 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7120 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007121 {
Angus Grattond8213d02016-05-25 20:56:48 +10007122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007123 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007124 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007125 }
7126
7127 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7128 if( ssl->out_buf == NULL )
7129 {
7130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007131 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007132 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007133 }
7134
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007135 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007136
Paul Bakker48916f92012-09-16 19:57:18 +00007137 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007138 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007139
7140 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007141
7142error:
7143 mbedtls_free( ssl->in_buf );
7144 mbedtls_free( ssl->out_buf );
7145
7146 ssl->conf = NULL;
7147
7148 ssl->in_buf = NULL;
7149 ssl->out_buf = NULL;
7150
7151 ssl->in_hdr = NULL;
7152 ssl->in_ctr = NULL;
7153 ssl->in_len = NULL;
7154 ssl->in_iv = NULL;
7155 ssl->in_msg = NULL;
7156
7157 ssl->out_hdr = NULL;
7158 ssl->out_ctr = NULL;
7159 ssl->out_len = NULL;
7160 ssl->out_iv = NULL;
7161 ssl->out_msg = NULL;
7162
k-stachowiak9f7798e2018-07-31 16:52:32 +02007163 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007164}
7165
7166/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007167 * Reset an initialized and used SSL context for re-use while retaining
7168 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007169 *
7170 * If partial is non-zero, keep data in the input buffer and client ID.
7171 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007172 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007173static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007174{
Paul Bakker48916f92012-09-16 19:57:18 +00007175 int ret;
7176
Hanno Becker7e772132018-08-10 12:38:21 +01007177#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7178 !defined(MBEDTLS_SSL_SRV_C)
7179 ((void) partial);
7180#endif
7181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007183
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007184 /* Cancel any possibly running timer */
7185 ssl_set_timer( ssl, 0 );
7186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007187#if defined(MBEDTLS_SSL_RENEGOTIATION)
7188 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007189 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007190
7191 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007192 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7193 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007194#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007195 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007196
Paul Bakker7eb013f2011-10-06 12:37:39 +00007197 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007198 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007199
7200 ssl->in_msgtype = 0;
7201 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007203 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007204 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007205#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007206#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007207 ssl_dtls_replay_reset( ssl );
7208#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007209
7210 ssl->in_hslen = 0;
7211 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007212
7213 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007214
7215 ssl->out_msgtype = 0;
7216 ssl->out_msglen = 0;
7217 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007218#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7219 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007220 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007221#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007222
Hanno Becker19859472018-08-06 09:40:20 +01007223 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7224
Paul Bakker48916f92012-09-16 19:57:18 +00007225 ssl->transform_in = NULL;
7226 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007227
Hanno Becker78640902018-08-13 16:35:15 +01007228 ssl->session_in = NULL;
7229 ssl->session_out = NULL;
7230
Angus Grattond8213d02016-05-25 20:56:48 +10007231 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007232
7233#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007234 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007235#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7236 {
7237 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007238 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007239 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7242 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7245 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7248 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007249 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007250 }
7251#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007252
Paul Bakker48916f92012-09-16 19:57:18 +00007253 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255 mbedtls_ssl_transform_free( ssl->transform );
7256 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007257 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007258 }
Paul Bakker48916f92012-09-16 19:57:18 +00007259
Paul Bakkerc0463502013-02-14 11:19:38 +01007260 if( ssl->session )
7261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262 mbedtls_ssl_session_free( ssl->session );
7263 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007264 ssl->session = NULL;
7265 }
7266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007268 ssl->alpn_chosen = NULL;
7269#endif
7270
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007271#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007272#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007273 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007274#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007275 {
7276 mbedtls_free( ssl->cli_id );
7277 ssl->cli_id = NULL;
7278 ssl->cli_id_len = 0;
7279 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007280#endif
7281
Paul Bakker48916f92012-09-16 19:57:18 +00007282 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7283 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007284
7285 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007286}
7287
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007288/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007289 * Reset an initialized and used SSL context for re-use while retaining
7290 * all application-set variables, function pointers and data.
7291 */
7292int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7293{
7294 return( ssl_session_reset_int( ssl, 0 ) );
7295}
7296
7297/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007298 * SSL set accessors
7299 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007300void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007301{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007302 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007303}
7304
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007305void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007306{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007307 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007308}
7309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007310#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007311void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007312{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007313 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007314}
7315#endif
7316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007318void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007319{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007320 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007321}
7322#endif
7323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007325
Hanno Becker1841b0a2018-08-24 11:13:57 +01007326void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7327 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007328{
7329 ssl->disable_datagram_packing = !allow_packing;
7330}
7331
7332void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7333 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007334{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007335 conf->hs_timeout_min = min;
7336 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007337}
7338#endif
7339
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007340void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007341{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007342 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007343}
7344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007345#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007346void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007347 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007348 void *p_vrfy )
7349{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007350 conf->f_vrfy = f_vrfy;
7351 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007352}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007353#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007354
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007355void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007356 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007357 void *p_rng )
7358{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007359 conf->f_rng = f_rng;
7360 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007361}
7362
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007363void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007364 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007365 void *p_dbg )
7366{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007367 conf->f_dbg = f_dbg;
7368 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007369}
7370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007371void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007372 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007373 mbedtls_ssl_send_t *f_send,
7374 mbedtls_ssl_recv_t *f_recv,
7375 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007376{
7377 ssl->p_bio = p_bio;
7378 ssl->f_send = f_send;
7379 ssl->f_recv = f_recv;
7380 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007381}
7382
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007383#if defined(MBEDTLS_SSL_PROTO_DTLS)
7384void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7385{
7386 ssl->mtu = mtu;
7387}
7388#endif
7389
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007390void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007391{
7392 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007393}
7394
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007395void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7396 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007397 mbedtls_ssl_set_timer_t *f_set_timer,
7398 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007399{
7400 ssl->p_timer = p_timer;
7401 ssl->f_set_timer = f_set_timer;
7402 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007403
7404 /* Make sure we start with no timer running */
7405 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007406}
7407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007408#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007409void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007410 void *p_cache,
7411 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7412 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007413{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007414 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007415 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007416 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007417}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007418#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420#if defined(MBEDTLS_SSL_CLI_C)
7421int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007422{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007423 int ret;
7424
7425 if( ssl == NULL ||
7426 session == NULL ||
7427 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007428 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007430 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007431 }
7432
7433 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7434 return( ret );
7435
Paul Bakker0a597072012-09-25 21:55:46 +00007436 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007437
7438 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007439}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007441
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007442void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007443 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007444{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007445 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7446 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7447 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7448 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007449}
7450
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007451void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007452 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007453 int major, int minor )
7454{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007455 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007456 return;
7457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007458 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007459 return;
7460
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007461 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007462}
7463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007465void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007466 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007467{
7468 conf->cert_profile = profile;
7469}
7470
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007471/* Append a new keycert entry to a (possibly empty) list */
7472static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7473 mbedtls_x509_crt *cert,
7474 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007475{
niisato8ee24222018-06-25 19:05:48 +09007476 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007477
niisato8ee24222018-06-25 19:05:48 +09007478 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7479 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007480 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007481
niisato8ee24222018-06-25 19:05:48 +09007482 new_cert->cert = cert;
7483 new_cert->key = key;
7484 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007485
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007486 /* Update head is the list was null, else add to the end */
7487 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007488 {
niisato8ee24222018-06-25 19:05:48 +09007489 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007490 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007491 else
7492 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007493 mbedtls_ssl_key_cert *cur = *head;
7494 while( cur->next != NULL )
7495 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007496 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007497 }
7498
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007499 return( 0 );
7500}
7501
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007502int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007503 mbedtls_x509_crt *own_cert,
7504 mbedtls_pk_context *pk_key )
7505{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007506 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007507}
7508
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007509void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007510 mbedtls_x509_crt *ca_chain,
7511 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007512{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007513 conf->ca_chain = ca_chain;
7514 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007515}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007516#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007517
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007518#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7519int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7520 mbedtls_x509_crt *own_cert,
7521 mbedtls_pk_context *pk_key )
7522{
7523 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7524 own_cert, pk_key ) );
7525}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007526
7527void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7528 mbedtls_x509_crt *ca_chain,
7529 mbedtls_x509_crl *ca_crl )
7530{
7531 ssl->handshake->sni_ca_chain = ca_chain;
7532 ssl->handshake->sni_ca_crl = ca_crl;
7533}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007534
7535void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7536 int authmode )
7537{
7538 ssl->handshake->sni_authmode = authmode;
7539}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007540#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7541
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007542#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007543/*
7544 * Set EC J-PAKE password for current handshake
7545 */
7546int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7547 const unsigned char *pw,
7548 size_t pw_len )
7549{
7550 mbedtls_ecjpake_role role;
7551
Janos Follath8eb64132016-06-03 15:40:57 +01007552 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7554
7555 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7556 role = MBEDTLS_ECJPAKE_SERVER;
7557 else
7558 role = MBEDTLS_ECJPAKE_CLIENT;
7559
7560 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7561 role,
7562 MBEDTLS_MD_SHA256,
7563 MBEDTLS_ECP_DP_SECP256R1,
7564 pw, pw_len ) );
7565}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007566#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007568#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007569
7570static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
7571{
7572 /* Remove reference to existing PSK, if any. */
7573#if defined(MBEDTLS_USE_PSA_CRYPTO)
7574 if( conf->psk_opaque != 0 )
7575 {
7576 /* The maintenance of the PSK key slot is the
7577 * user's responsibility. */
7578 conf->psk_opaque = 0;
7579 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00007580 /* This and the following branch should never
7581 * be taken simultaenously as we maintain the
7582 * invariant that raw and opaque PSKs are never
7583 * configured simultaneously. As a safeguard,
7584 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007585#endif /* MBEDTLS_USE_PSA_CRYPTO */
7586 if( conf->psk != NULL )
7587 {
7588 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
7589
7590 mbedtls_free( conf->psk );
7591 conf->psk = NULL;
7592 conf->psk_len = 0;
7593 }
7594
7595 /* Remove reference to PSK identity, if any. */
7596 if( conf->psk_identity != NULL )
7597 {
7598 mbedtls_free( conf->psk_identity );
7599 conf->psk_identity = NULL;
7600 conf->psk_identity_len = 0;
7601 }
7602}
7603
Hanno Becker7390c712018-11-15 13:33:04 +00007604/* This function assumes that PSK identity in the SSL config is unset.
7605 * It checks that the provided identity is well-formed and attempts
7606 * to make a copy of it in the SSL config.
7607 * On failure, the PSK identity in the config remains unset. */
7608static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
7609 unsigned char const *psk_identity,
7610 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007611{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007612 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00007613 if( psk_identity == NULL ||
7614 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007615 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007616 {
7617 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7618 }
7619
Hanno Becker7390c712018-11-15 13:33:04 +00007620 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
7621 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007622 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02007623
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007624 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007625 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02007626
7627 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02007628}
7629
Hanno Becker7390c712018-11-15 13:33:04 +00007630int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
7631 const unsigned char *psk, size_t psk_len,
7632 const unsigned char *psk_identity, size_t psk_identity_len )
7633{
7634 int ret;
7635 /* Remove opaque/raw PSK + PSK Identity */
7636 ssl_conf_remove_psk( conf );
7637
7638 /* Check and set raw PSK */
7639 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
7640 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7641 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
7642 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7643 conf->psk_len = psk_len;
7644 memcpy( conf->psk, psk, conf->psk_len );
7645
7646 /* Check and set PSK Identity */
7647 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
7648 if( ret != 0 )
7649 ssl_conf_remove_psk( conf );
7650
7651 return( ret );
7652}
7653
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007654static void ssl_remove_psk( mbedtls_ssl_context *ssl )
7655{
7656#if defined(MBEDTLS_USE_PSA_CRYPTO)
7657 if( ssl->handshake->psk_opaque != 0 )
7658 {
7659 ssl->handshake->psk_opaque = 0;
7660 }
7661 else
7662#endif /* MBEDTLS_USE_PSA_CRYPTO */
7663 if( ssl->handshake->psk != NULL )
7664 {
7665 mbedtls_platform_zeroize( ssl->handshake->psk,
7666 ssl->handshake->psk_len );
7667 mbedtls_free( ssl->handshake->psk );
7668 ssl->handshake->psk_len = 0;
7669 }
7670}
7671
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007672int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7673 const unsigned char *psk, size_t psk_len )
7674{
7675 if( psk == NULL || ssl->handshake == NULL )
7676 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7677
7678 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7679 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7680
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007681 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007682
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007683 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007684 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007685
7686 ssl->handshake->psk_len = psk_len;
7687 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7688
7689 return( 0 );
7690}
7691
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007692#if defined(MBEDTLS_USE_PSA_CRYPTO)
7693int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007694 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007695 const unsigned char *psk_identity,
7696 size_t psk_identity_len )
7697{
Hanno Becker7390c712018-11-15 13:33:04 +00007698 int ret;
7699 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007700 ssl_conf_remove_psk( conf );
7701
Hanno Becker7390c712018-11-15 13:33:04 +00007702 /* Check and set opaque PSK */
7703 if( psk_slot == 0 )
7704 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007705 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00007706
7707 /* Check and set PSK Identity */
7708 ret = ssl_conf_set_psk_identity( conf, psk_identity,
7709 psk_identity_len );
7710 if( ret != 0 )
7711 ssl_conf_remove_psk( conf );
7712
7713 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007714}
7715
7716int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007717 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007718{
7719 if( psk_slot == 0 || ssl->handshake == NULL )
7720 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7721
7722 ssl_remove_psk( ssl );
7723 ssl->handshake->psk_opaque = psk_slot;
7724 return( 0 );
7725}
7726#endif /* MBEDTLS_USE_PSA_CRYPTO */
7727
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007728void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007730 size_t),
7731 void *p_psk )
7732{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007733 conf->f_psk = f_psk;
7734 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007735}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007737
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007738#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007739
7740#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007741int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007742{
7743 int ret;
7744
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007745 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7746 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7747 {
7748 mbedtls_mpi_free( &conf->dhm_P );
7749 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007750 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007751 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007752
7753 return( 0 );
7754}
Hanno Becker470a8c42017-10-04 15:28:46 +01007755#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007756
Hanno Beckera90658f2017-10-04 15:29:08 +01007757int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7758 const unsigned char *dhm_P, size_t P_len,
7759 const unsigned char *dhm_G, size_t G_len )
7760{
7761 int ret;
7762
7763 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7764 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7765 {
7766 mbedtls_mpi_free( &conf->dhm_P );
7767 mbedtls_mpi_free( &conf->dhm_G );
7768 return( ret );
7769 }
7770
7771 return( 0 );
7772}
Paul Bakker5121ce52009-01-03 21:22:43 +00007773
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007774int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007775{
7776 int ret;
7777
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007778 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7779 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7780 {
7781 mbedtls_mpi_free( &conf->dhm_P );
7782 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007783 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007784 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007785
7786 return( 0 );
7787}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007788#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007789
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007790#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7791/*
7792 * Set the minimum length for Diffie-Hellman parameters
7793 */
7794void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7795 unsigned int bitlen )
7796{
7797 conf->dhm_min_bitlen = bitlen;
7798}
7799#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7800
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007801#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007802/*
7803 * Set allowed/preferred hashes for handshake signatures
7804 */
7805void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7806 const int *hashes )
7807{
7808 conf->sig_hashes = hashes;
7809}
Hanno Becker947194e2017-04-07 13:25:49 +01007810#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007811
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007812#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007813/*
7814 * Set the allowed elliptic curves
7815 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007816void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007817 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007818{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007819 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007820}
Hanno Becker947194e2017-04-07 13:25:49 +01007821#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007822
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007823#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007824int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007825{
Hanno Becker947194e2017-04-07 13:25:49 +01007826 /* Initialize to suppress unnecessary compiler warning */
7827 size_t hostname_len = 0;
7828
7829 /* Check if new hostname is valid before
7830 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007831 if( hostname != NULL )
7832 {
7833 hostname_len = strlen( hostname );
7834
7835 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7836 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7837 }
7838
7839 /* Now it's clear that we will overwrite the old hostname,
7840 * so we can free it safely */
7841
7842 if( ssl->hostname != NULL )
7843 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007844 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007845 mbedtls_free( ssl->hostname );
7846 }
7847
7848 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007849
Paul Bakker5121ce52009-01-03 21:22:43 +00007850 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007851 {
7852 ssl->hostname = NULL;
7853 }
7854 else
7855 {
7856 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007857 if( ssl->hostname == NULL )
7858 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007859
Hanno Becker947194e2017-04-07 13:25:49 +01007860 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007861
Hanno Becker947194e2017-04-07 13:25:49 +01007862 ssl->hostname[hostname_len] = '\0';
7863 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007864
7865 return( 0 );
7866}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007867#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007868
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007869#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007870void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007871 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007872 const unsigned char *, size_t),
7873 void *p_sni )
7874{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007875 conf->f_sni = f_sni;
7876 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007877}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007880#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007881int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007882{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007883 size_t cur_len, tot_len;
7884 const char **p;
7885
7886 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007887 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7888 * MUST NOT be truncated."
7889 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007890 */
7891 tot_len = 0;
7892 for( p = protos; *p != NULL; p++ )
7893 {
7894 cur_len = strlen( *p );
7895 tot_len += cur_len;
7896
7897 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007899 }
7900
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007901 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007902
7903 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007904}
7905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007907{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007908 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007909}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007910#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007911
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007912void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007913{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007914 conf->max_major_ver = major;
7915 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007916}
7917
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007918void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007919{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007920 conf->min_major_ver = major;
7921 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007922}
7923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007924#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007925void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007926{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007927 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007928}
7929#endif
7930
Janos Follath088ce432017-04-10 12:42:31 +01007931#if defined(MBEDTLS_SSL_SRV_C)
7932void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7933 char cert_req_ca_list )
7934{
7935 conf->cert_req_ca_list = cert_req_ca_list;
7936}
7937#endif
7938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007940void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007941{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007942 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007943}
7944#endif
7945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007947void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007948{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007949 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007950}
7951#endif
7952
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007953#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007954void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007955{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007956 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007957}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007958#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007961int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007962{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007964 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007967 }
7968
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007969 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007970
7971 return( 0 );
7972}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007976void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007977{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007978 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007979}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007980#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007983void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007984{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007985 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007986}
7987#endif
7988
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007989void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007990{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007991 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007992}
7993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007994#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007995void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007996{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007997 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007998}
7999
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008000void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008001{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008002 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008003}
8004
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008005void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008006 const unsigned char period[8] )
8007{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008008 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008009}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008010#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008013#if defined(MBEDTLS_SSL_CLI_C)
8014void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008015{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008016 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008017}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008018#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008019
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008020#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008021void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8022 mbedtls_ssl_ticket_write_t *f_ticket_write,
8023 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8024 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008025{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008026 conf->f_ticket_write = f_ticket_write;
8027 conf->f_ticket_parse = f_ticket_parse;
8028 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008029}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008030#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008031#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008032
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008033#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8034void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8035 mbedtls_ssl_export_keys_t *f_export_keys,
8036 void *p_export_keys )
8037{
8038 conf->f_export_keys = f_export_keys;
8039 conf->p_export_keys = p_export_keys;
8040}
8041#endif
8042
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008043#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008044void mbedtls_ssl_conf_async_private_cb(
8045 mbedtls_ssl_config *conf,
8046 mbedtls_ssl_async_sign_t *f_async_sign,
8047 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8048 mbedtls_ssl_async_resume_t *f_async_resume,
8049 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008050 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008051{
8052 conf->f_async_sign_start = f_async_sign;
8053 conf->f_async_decrypt_start = f_async_decrypt;
8054 conf->f_async_resume = f_async_resume;
8055 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008056 conf->p_async_config_data = async_config_data;
8057}
8058
Gilles Peskine8f97af72018-04-26 11:46:10 +02008059void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8060{
8061 return( conf->p_async_config_data );
8062}
8063
Gilles Peskine1febfef2018-04-30 11:54:39 +02008064void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008065{
8066 if( ssl->handshake == NULL )
8067 return( NULL );
8068 else
8069 return( ssl->handshake->user_async_ctx );
8070}
8071
Gilles Peskine1febfef2018-04-30 11:54:39 +02008072void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008073 void *ctx )
8074{
8075 if( ssl->handshake != NULL )
8076 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008077}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008078#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008079
Paul Bakker5121ce52009-01-03 21:22:43 +00008080/*
8081 * SSL get accessors
8082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008083size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008084{
8085 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8086}
8087
Hanno Becker8b170a02017-10-10 11:51:19 +01008088int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8089{
8090 /*
8091 * Case A: We're currently holding back
8092 * a message for further processing.
8093 */
8094
8095 if( ssl->keep_current_message == 1 )
8096 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008097 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008098 return( 1 );
8099 }
8100
8101 /*
8102 * Case B: Further records are pending in the current datagram.
8103 */
8104
8105#if defined(MBEDTLS_SSL_PROTO_DTLS)
8106 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8107 ssl->in_left > ssl->next_record_offset )
8108 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008109 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008110 return( 1 );
8111 }
8112#endif /* MBEDTLS_SSL_PROTO_DTLS */
8113
8114 /*
8115 * Case C: A handshake message is being processed.
8116 */
8117
Hanno Becker8b170a02017-10-10 11:51:19 +01008118 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8119 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008120 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008121 return( 1 );
8122 }
8123
8124 /*
8125 * Case D: An application data message is being processed
8126 */
8127 if( ssl->in_offt != NULL )
8128 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008129 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008130 return( 1 );
8131 }
8132
8133 /*
8134 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008135 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008136 * we implement support for multiple alerts in single records.
8137 */
8138
8139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8140 return( 0 );
8141}
8142
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008143uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008144{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008145 if( ssl->session != NULL )
8146 return( ssl->session->verify_result );
8147
8148 if( ssl->session_negotiate != NULL )
8149 return( ssl->session_negotiate->verify_result );
8150
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008151 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008152}
8153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008155{
Paul Bakker926c8e42013-03-06 10:23:34 +01008156 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008157 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008159 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008160}
8161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008162const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008163{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008165 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008166 {
8167 switch( ssl->minor_ver )
8168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008169 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008170 return( "DTLSv1.0" );
8171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008172 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008173 return( "DTLSv1.2" );
8174
8175 default:
8176 return( "unknown (DTLS)" );
8177 }
8178 }
8179#endif
8180
Paul Bakker43ca69c2011-01-15 17:35:19 +00008181 switch( ssl->minor_ver )
8182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008184 return( "SSLv3.0" );
8185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008186 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008187 return( "TLSv1.0" );
8188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008189 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008190 return( "TLSv1.1" );
8191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008192 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008193 return( "TLSv1.2" );
8194
Paul Bakker43ca69c2011-01-15 17:35:19 +00008195 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008196 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008197 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008198}
8199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008200int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008201{
Hanno Becker3136ede2018-08-17 15:28:19 +01008202 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008203 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008204 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008205
Hanno Becker78640902018-08-13 16:35:15 +01008206 if( transform == NULL )
8207 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008209#if defined(MBEDTLS_ZLIB_SUPPORT)
8210 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8211 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008212#endif
8213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008214 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008216 case MBEDTLS_MODE_GCM:
8217 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008218 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008219 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008220 transform_expansion = transform->minlen;
8221 break;
8222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008223 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008224
8225 block_size = mbedtls_cipher_get_block_size(
8226 &transform->cipher_ctx_enc );
8227
Hanno Becker3136ede2018-08-17 15:28:19 +01008228 /* Expansion due to the addition of the MAC. */
8229 transform_expansion += transform->maclen;
8230
8231 /* Expansion due to the addition of CBC padding;
8232 * Theoretically up to 256 bytes, but we never use
8233 * more than the block size of the underlying cipher. */
8234 transform_expansion += block_size;
8235
8236 /* For TLS 1.1 or higher, an explicit IV is added
8237 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008238#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8239 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008240 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008241#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008242
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008243 break;
8244
8245 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008247 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008248 }
8249
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008250 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008251}
8252
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008253#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8254size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8255{
8256 size_t max_len;
8257
8258 /*
8259 * Assume mfl_code is correct since it was checked when set
8260 */
Angus Grattond8213d02016-05-25 20:56:48 +10008261 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008262
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008263 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008264 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008265 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008266 {
Angus Grattond8213d02016-05-25 20:56:48 +10008267 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008268 }
8269
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008270 /* During a handshake, use the value being negotiated */
8271 if( ssl->session_negotiate != NULL &&
8272 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8273 {
8274 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8275 }
8276
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008277 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008278}
8279#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8280
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008281#if defined(MBEDTLS_SSL_PROTO_DTLS)
8282static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8283{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008284 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8285 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8286 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8287 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8288 return ( 0 );
8289
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008290 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8291 return( ssl->mtu );
8292
8293 if( ssl->mtu == 0 )
8294 return( ssl->handshake->mtu );
8295
8296 return( ssl->mtu < ssl->handshake->mtu ?
8297 ssl->mtu : ssl->handshake->mtu );
8298}
8299#endif /* MBEDTLS_SSL_PROTO_DTLS */
8300
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008301int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8302{
8303 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8304
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008305#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8306 !defined(MBEDTLS_SSL_PROTO_DTLS)
8307 (void) ssl;
8308#endif
8309
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008310#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8311 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8312
8313 if( max_len > mfl )
8314 max_len = mfl;
8315#endif
8316
8317#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008318 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008319 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008320 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008321 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8322 const size_t overhead = (size_t) ret;
8323
8324 if( ret < 0 )
8325 return( ret );
8326
8327 if( mtu <= overhead )
8328 {
8329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8330 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8331 }
8332
8333 if( max_len > mtu - overhead )
8334 max_len = mtu - overhead;
8335 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008336#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008337
Hanno Becker0defedb2018-08-10 12:35:02 +01008338#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8339 !defined(MBEDTLS_SSL_PROTO_DTLS)
8340 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008341#endif
8342
8343 return( (int) max_len );
8344}
8345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008346#if defined(MBEDTLS_X509_CRT_PARSE_C)
8347const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008348{
8349 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008350 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008351
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008352 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008353}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008354#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008356#if defined(MBEDTLS_SSL_CLI_C)
8357int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008358{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008359 if( ssl == NULL ||
8360 dst == NULL ||
8361 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008362 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008364 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008365 }
8366
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008367 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008369#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008370
Paul Bakker5121ce52009-01-03 21:22:43 +00008371/*
Paul Bakker1961b702013-01-25 14:49:24 +01008372 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008373 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008374int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008375{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008376 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008377
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008378 if( ssl == NULL || ssl->conf == NULL )
8379 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008381#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008382 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008383 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008384#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008385#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008386 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008387 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008388#endif
8389
Paul Bakker1961b702013-01-25 14:49:24 +01008390 return( ret );
8391}
8392
8393/*
8394 * Perform the SSL handshake
8395 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008396int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008397{
8398 int ret = 0;
8399
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008400 if( ssl == NULL || ssl->conf == NULL )
8401 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008407 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008408
8409 if( ret != 0 )
8410 break;
8411 }
8412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008414
8415 return( ret );
8416}
8417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008418#if defined(MBEDTLS_SSL_RENEGOTIATION)
8419#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008420/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008421 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008422 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008423static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008424{
8425 int ret;
8426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008428
8429 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008430 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8431 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008432
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008433 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008434 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008435 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008436 return( ret );
8437 }
8438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008440
8441 return( 0 );
8442}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008443#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008444
8445/*
8446 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008447 * - any side: calling mbedtls_ssl_renegotiate(),
8448 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8449 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008450 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008451 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008452 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008454static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008455{
8456 int ret;
8457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008459
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008460 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8461 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008462
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008463 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8464 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008465#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008466 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008467 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008468 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008469 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008470 ssl->handshake->out_msg_seq = 1;
8471 else
8472 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008473 }
8474#endif
8475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008476 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8477 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008479 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008481 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008482 return( ret );
8483 }
8484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008486
8487 return( 0 );
8488}
8489
8490/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008491 * Renegotiate current connection on client,
8492 * or request renegotiation on server
8493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008494int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008495{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008496 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008497
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008498 if( ssl == NULL || ssl->conf == NULL )
8499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008501#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008502 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008503 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008505 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8506 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008508 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008509
8510 /* Did we already try/start sending HelloRequest? */
8511 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008512 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008513
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008514 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008515 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008516#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008518#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008519 /*
8520 * On client, either start the renegotiation process or,
8521 * if already in progress, continue the handshake
8522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008523 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008525 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8526 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008527
8528 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008530 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008531 return( ret );
8532 }
8533 }
8534 else
8535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008536 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008538 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008539 return( ret );
8540 }
8541 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008542#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008543
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008544 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008545}
8546
8547/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008548 * Check record counters and renegotiate if they're above the limit.
8549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008550static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008551{
Andres AG2196c7f2016-12-15 17:01:16 +00008552 size_t ep_len = ssl_ep_len( ssl );
8553 int in_ctr_cmp;
8554 int out_ctr_cmp;
8555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008556 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8557 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008558 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008559 {
8560 return( 0 );
8561 }
8562
Andres AG2196c7f2016-12-15 17:01:16 +00008563 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8564 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008565 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008566 ssl->conf->renego_period + ep_len, 8 - ep_len );
8567
8568 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008569 {
8570 return( 0 );
8571 }
8572
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008574 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008576#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008577
8578/*
8579 * Receive application data decrypted from the SSL layer
8580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008581int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008582{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008583 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008584 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008585
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008586 if( ssl == NULL || ssl->conf == NULL )
8587 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008591#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008592 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008594 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008595 return( ret );
8596
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008597 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008598 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008599 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008600 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008601 return( ret );
8602 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008603 }
8604#endif
8605
Hanno Becker4a810fb2017-05-24 16:27:30 +01008606 /*
8607 * Check if renegotiation is necessary and/or handshake is
8608 * in process. If yes, perform/continue, and fall through
8609 * if an unexpected packet is received while the client
8610 * is waiting for the ServerHello.
8611 *
8612 * (There is no equivalent to the last condition on
8613 * the server-side as it is not treated as within
8614 * a handshake while waiting for the ClientHello
8615 * after a renegotiation request.)
8616 */
8617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008618#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008619 ret = ssl_check_ctr_renegotiate( ssl );
8620 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8621 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008623 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008624 return( ret );
8625 }
8626#endif
8627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008628 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008630 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008631 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8632 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008634 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008635 return( ret );
8636 }
8637 }
8638
Hanno Beckere41158b2017-10-23 13:30:32 +01008639 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008640 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008641 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008642 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008643 if( ssl->f_get_timer != NULL &&
8644 ssl->f_get_timer( ssl->p_timer ) == -1 )
8645 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008646 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008647 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008648
Hanno Becker327c93b2018-08-15 13:56:18 +01008649 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008650 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008651 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8652 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008653
Hanno Becker4a810fb2017-05-24 16:27:30 +01008654 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8655 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008656 }
8657
8658 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008659 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008660 {
8661 /*
8662 * OpenSSL sends empty messages to randomize the IV
8663 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008664 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008666 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008667 return( 0 );
8668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008669 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008670 return( ret );
8671 }
8672 }
8673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008674 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008677
Hanno Becker4a810fb2017-05-24 16:27:30 +01008678 /*
8679 * - For client-side, expect SERVER_HELLO_REQUEST.
8680 * - For server-side, expect CLIENT_HELLO.
8681 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8682 */
8683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008684#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008685 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008687 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008690
8691 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008692#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008693 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008694 {
8695 continue;
8696 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008697#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008698 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008699 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008700#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008701
Hanno Becker4a810fb2017-05-24 16:27:30 +01008702#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008703 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008704 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008707
8708 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008709#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008710 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008711 {
8712 continue;
8713 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008714#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008715 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008716 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008717#endif /* MBEDTLS_SSL_SRV_C */
8718
Hanno Becker21df7f92017-10-17 11:03:26 +01008719#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008720 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008721 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8722 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8723 ssl->conf->allow_legacy_renegotiation ==
8724 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8725 {
8726 /*
8727 * Accept renegotiation request
8728 */
Paul Bakker48916f92012-09-16 19:57:18 +00008729
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008730 /* DTLS clients need to know renego is server-initiated */
8731#if defined(MBEDTLS_SSL_PROTO_DTLS)
8732 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8733 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8734 {
8735 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8736 }
8737#endif
8738 ret = ssl_start_renegotiation( ssl );
8739 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8740 ret != 0 )
8741 {
8742 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8743 return( ret );
8744 }
8745 }
8746 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008747#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008748 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008749 /*
8750 * Refuse renegotiation
8751 */
8752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008753 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008755#if defined(MBEDTLS_SSL_PROTO_SSL3)
8756 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008757 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008758 /* SSLv3 does not have a "no_renegotiation" warning, so
8759 we send a fatal alert and abort the connection. */
8760 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8761 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8762 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008763 }
8764 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008765#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8766#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8767 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8768 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008770 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8771 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8772 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008773 {
8774 return( ret );
8775 }
Paul Bakker48916f92012-09-16 19:57:18 +00008776 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008777 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008778#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8779 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8782 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008783 }
Paul Bakker48916f92012-09-16 19:57:18 +00008784 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008785
Hanno Becker90333da2017-10-10 11:27:13 +01008786 /* At this point, we don't know whether the renegotiation has been
8787 * completed or not. The cases to consider are the following:
8788 * 1) The renegotiation is complete. In this case, no new record
8789 * has been read yet.
8790 * 2) The renegotiation is incomplete because the client received
8791 * an application data record while awaiting the ServerHello.
8792 * 3) The renegotiation is incomplete because the client received
8793 * a non-handshake, non-application data message while awaiting
8794 * the ServerHello.
8795 * In each of these case, looping will be the proper action:
8796 * - For 1), the next iteration will read a new record and check
8797 * if it's application data.
8798 * - For 2), the loop condition isn't satisfied as application data
8799 * is present, hence continue is the same as break
8800 * - For 3), the loop condition is satisfied and read_record
8801 * will re-deliver the message that was held back by the client
8802 * when expecting the ServerHello.
8803 */
8804 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008805 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008806#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008807 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008808 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008809 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008810 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008811 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008814 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008815 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008816 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008817 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008818 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008819#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008821 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8822 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008825 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008826 }
8827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008828 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8831 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008832 }
8833
8834 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008835
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008836 /* We're going to return something now, cancel timer,
8837 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008838 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008839 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008840
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008841#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008842 /* If we requested renego but received AppData, resend HelloRequest.
8843 * Do it now, after setting in_offt, to avoid taking this branch
8844 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008845#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008846 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008847 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008848 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008849 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008851 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008852 return( ret );
8853 }
8854 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008855#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008856#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008857 }
8858
8859 n = ( len < ssl->in_msglen )
8860 ? len : ssl->in_msglen;
8861
8862 memcpy( buf, ssl->in_offt, n );
8863 ssl->in_msglen -= n;
8864
8865 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008866 {
8867 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008868 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008869 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008870 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008871 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008872 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008873 /* more data available */
8874 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008875 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008878
Paul Bakker23986e52011-04-24 08:57:21 +00008879 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008880}
8881
8882/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008883 * Send application data to be encrypted by the SSL layer, taking care of max
8884 * fragment length and buffer size.
8885 *
8886 * According to RFC 5246 Section 6.2.1:
8887 *
8888 * Zero-length fragments of Application data MAY be sent as they are
8889 * potentially useful as a traffic analysis countermeasure.
8890 *
8891 * Therefore, it is possible that the input message length is 0 and the
8892 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008893 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008894static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008895 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008896{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008897 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8898 const size_t max_len = (size_t) ret;
8899
8900 if( ret < 0 )
8901 {
8902 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8903 return( ret );
8904 }
8905
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008906 if( len > max_len )
8907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008908#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008909 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008912 "maximum fragment length: %d > %d",
8913 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008914 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008915 }
8916 else
8917#endif
8918 len = max_len;
8919 }
Paul Bakker887bd502011-06-08 13:10:54 +00008920
Paul Bakker5121ce52009-01-03 21:22:43 +00008921 if( ssl->out_left != 0 )
8922 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008923 /*
8924 * The user has previously tried to send the data and
8925 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8926 * written. In this case, we expect the high-level write function
8927 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8928 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008929 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008931 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008932 return( ret );
8933 }
8934 }
Paul Bakker887bd502011-06-08 13:10:54 +00008935 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008936 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008937 /*
8938 * The user is trying to send a message the first time, so we need to
8939 * copy the data into the internal buffers and setup the data structure
8940 * to keep track of partial writes
8941 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008942 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008943 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008944 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008945
Hanno Becker67bc7c32018-08-06 11:33:50 +01008946 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008948 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008949 return( ret );
8950 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008951 }
8952
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008953 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008954}
8955
8956/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008957 * Write application data, doing 1/n-1 splitting if necessary.
8958 *
8959 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008960 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008961 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008962 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008963#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008964static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008965 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008966{
8967 int ret;
8968
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008969 if( ssl->conf->cbc_record_splitting ==
8970 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008971 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008972 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8973 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8974 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008975 {
8976 return( ssl_write_real( ssl, buf, len ) );
8977 }
8978
8979 if( ssl->split_done == 0 )
8980 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008981 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008982 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008983 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008984 }
8985
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008986 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8987 return( ret );
8988 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008989
8990 return( ret + 1 );
8991}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008992#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008993
8994/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008995 * Write application data (public-facing wrapper)
8996 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008997int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008998{
8999 int ret;
9000
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009002
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009003 if( ssl == NULL || ssl->conf == NULL )
9004 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9005
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009006#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009007 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9008 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009009 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009010 return( ret );
9011 }
9012#endif
9013
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009014 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009015 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009016 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009017 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009019 return( ret );
9020 }
9021 }
9022
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009023#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009024 ret = ssl_write_split( ssl, buf, len );
9025#else
9026 ret = ssl_write_real( ssl, buf, len );
9027#endif
9028
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009030
9031 return( ret );
9032}
9033
9034/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009035 * Notify the peer that the connection is being closed
9036 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009037int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009038{
9039 int ret;
9040
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009041 if( ssl == NULL || ssl->conf == NULL )
9042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009045
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009046 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009047 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009049 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009051 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9052 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9053 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009055 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009056 return( ret );
9057 }
9058 }
9059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009061
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009062 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009063}
9064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009065void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009066{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009067 if( transform == NULL )
9068 return;
9069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009070#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009071 deflateEnd( &transform->ctx_deflate );
9072 inflateEnd( &transform->ctx_inflate );
9073#endif
9074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009075 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9076 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009078 mbedtls_md_free( &transform->md_ctx_enc );
9079 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009080
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009081 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009082}
9083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009084#if defined(MBEDTLS_X509_CRT_PARSE_C)
9085static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009086{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009087 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009088
9089 while( cur != NULL )
9090 {
9091 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009092 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009093 cur = next;
9094 }
9095}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009096#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009097
Hanno Becker0271f962018-08-16 13:23:47 +01009098#if defined(MBEDTLS_SSL_PROTO_DTLS)
9099
9100static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9101{
9102 unsigned offset;
9103 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9104
9105 if( hs == NULL )
9106 return;
9107
Hanno Becker283f5ef2018-08-24 09:34:47 +01009108 ssl_free_buffered_record( ssl );
9109
Hanno Becker0271f962018-08-16 13:23:47 +01009110 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009111 ssl_buffering_free_slot( ssl, offset );
9112}
9113
9114static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9115 uint8_t slot )
9116{
9117 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9118 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009119
9120 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9121 return;
9122
Hanno Beckere605b192018-08-21 15:59:07 +01009123 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009124 {
Hanno Beckere605b192018-08-21 15:59:07 +01009125 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009126 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009127 mbedtls_free( hs_buf->data );
9128 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009129 }
9130}
9131
9132#endif /* MBEDTLS_SSL_PROTO_DTLS */
9133
Gilles Peskine9b562d52018-04-25 20:32:43 +02009134void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009135{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009136 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9137
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009138 if( handshake == NULL )
9139 return;
9140
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009141#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9142 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9143 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009144 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009145 handshake->async_in_progress = 0;
9146 }
9147#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9148
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009149#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9150 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9151 mbedtls_md5_free( &handshake->fin_md5 );
9152 mbedtls_sha1_free( &handshake->fin_sha1 );
9153#endif
9154#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9155#if defined(MBEDTLS_SHA256_C)
9156 mbedtls_sha256_free( &handshake->fin_sha256 );
9157#endif
9158#if defined(MBEDTLS_SHA512_C)
9159 mbedtls_sha512_free( &handshake->fin_sha512 );
9160#endif
9161#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009163#if defined(MBEDTLS_DHM_C)
9164 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009165#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009166#if defined(MBEDTLS_ECDH_C)
9167 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009168#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009169#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009170 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009171#if defined(MBEDTLS_SSL_CLI_C)
9172 mbedtls_free( handshake->ecjpake_cache );
9173 handshake->ecjpake_cache = NULL;
9174 handshake->ecjpake_cache_len = 0;
9175#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009176#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009177
Janos Follath4ae5c292016-02-10 11:27:43 +00009178#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9179 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009180 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009181 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009182#endif
9183
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009184#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9185 if( handshake->psk != NULL )
9186 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009187 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009188 mbedtls_free( handshake->psk );
9189 }
9190#endif
9191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009192#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9193 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009194 /*
9195 * Free only the linked list wrapper, not the keys themselves
9196 * since the belong to the SNI callback
9197 */
9198 if( handshake->sni_key_cert != NULL )
9199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009200 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009201
9202 while( cur != NULL )
9203 {
9204 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009205 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009206 cur = next;
9207 }
9208 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009209#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009210
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009211#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009212 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009213#endif
9214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009215#if defined(MBEDTLS_SSL_PROTO_DTLS)
9216 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009217 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009218 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009219#endif
9220
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009221 mbedtls_platform_zeroize( handshake,
9222 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009223}
9224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009225void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009226{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009227 if( session == NULL )
9228 return;
9229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009230#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00009231 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00009232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009233 mbedtls_x509_crt_free( session->peer_cert );
9234 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00009235 }
Paul Bakkered27a042013-04-18 22:46:23 +02009236#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009237
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009238#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009239 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009240#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009241
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009242 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009243}
9244
Paul Bakker5121ce52009-01-03 21:22:43 +00009245/*
9246 * Free an SSL context
9247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009248void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009249{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009250 if( ssl == NULL )
9251 return;
9252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009254
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009255 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009256 {
Angus Grattond8213d02016-05-25 20:56:48 +10009257 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009258 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009259 }
9260
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009261 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009262 {
Angus Grattond8213d02016-05-25 20:56:48 +10009263 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009264 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009265 }
9266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009267#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009268 if( ssl->compress_buf != NULL )
9269 {
Angus Grattond8213d02016-05-25 20:56:48 +10009270 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009271 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009272 }
9273#endif
9274
Paul Bakker48916f92012-09-16 19:57:18 +00009275 if( ssl->transform )
9276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009277 mbedtls_ssl_transform_free( ssl->transform );
9278 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009279 }
9280
9281 if( ssl->handshake )
9282 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009283 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009284 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9285 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009287 mbedtls_free( ssl->handshake );
9288 mbedtls_free( ssl->transform_negotiate );
9289 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009290 }
9291
Paul Bakkerc0463502013-02-14 11:19:38 +01009292 if( ssl->session )
9293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009294 mbedtls_ssl_session_free( ssl->session );
9295 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009296 }
9297
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009298#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009299 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009300 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009301 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009302 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009303 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009304#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009306#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9307 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9310 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009311 }
9312#endif
9313
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009314#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009315 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009316#endif
9317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009319
Paul Bakker86f04f42013-02-14 11:20:09 +01009320 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009321 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009322}
9323
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009324/*
9325 * Initialze mbedtls_ssl_config
9326 */
9327void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9328{
9329 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9330}
9331
Simon Butcherc97b6972015-12-27 23:48:17 +00009332#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009333static int ssl_preset_default_hashes[] = {
9334#if defined(MBEDTLS_SHA512_C)
9335 MBEDTLS_MD_SHA512,
9336 MBEDTLS_MD_SHA384,
9337#endif
9338#if defined(MBEDTLS_SHA256_C)
9339 MBEDTLS_MD_SHA256,
9340 MBEDTLS_MD_SHA224,
9341#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009342#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009343 MBEDTLS_MD_SHA1,
9344#endif
9345 MBEDTLS_MD_NONE
9346};
Simon Butcherc97b6972015-12-27 23:48:17 +00009347#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009348
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009349static int ssl_preset_suiteb_ciphersuites[] = {
9350 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9351 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9352 0
9353};
9354
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009355#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009356static int ssl_preset_suiteb_hashes[] = {
9357 MBEDTLS_MD_SHA256,
9358 MBEDTLS_MD_SHA384,
9359 MBEDTLS_MD_NONE
9360};
9361#endif
9362
9363#if defined(MBEDTLS_ECP_C)
9364static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9365 MBEDTLS_ECP_DP_SECP256R1,
9366 MBEDTLS_ECP_DP_SECP384R1,
9367 MBEDTLS_ECP_DP_NONE
9368};
9369#endif
9370
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009371/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009372 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009373 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009374int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009375 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009376{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009377#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009378 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009379#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009380
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009381 /* Use the functions here so that they are covered in tests,
9382 * but otherwise access member directly for efficiency */
9383 mbedtls_ssl_conf_endpoint( conf, endpoint );
9384 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009385
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009386 /*
9387 * Things that are common to all presets
9388 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009389#if defined(MBEDTLS_SSL_CLI_C)
9390 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9391 {
9392 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9393#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9394 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9395#endif
9396 }
9397#endif
9398
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009399#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009400 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009401#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009402
9403#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9404 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9405#endif
9406
9407#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9408 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9409#endif
9410
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009411#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9412 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9413#endif
9414
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009415#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009416 conf->f_cookie_write = ssl_cookie_write_dummy;
9417 conf->f_cookie_check = ssl_cookie_check_dummy;
9418#endif
9419
9420#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9421 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9422#endif
9423
Janos Follath088ce432017-04-10 12:42:31 +01009424#if defined(MBEDTLS_SSL_SRV_C)
9425 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9426#endif
9427
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009428#if defined(MBEDTLS_SSL_PROTO_DTLS)
9429 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9430 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9431#endif
9432
9433#if defined(MBEDTLS_SSL_RENEGOTIATION)
9434 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009435 memset( conf->renego_period, 0x00, 2 );
9436 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009437#endif
9438
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009439#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9440 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9441 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009442 const unsigned char dhm_p[] =
9443 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9444 const unsigned char dhm_g[] =
9445 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9446
Hanno Beckera90658f2017-10-04 15:29:08 +01009447 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9448 dhm_p, sizeof( dhm_p ),
9449 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009450 {
9451 return( ret );
9452 }
9453 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009454#endif
9455
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009456 /*
9457 * Preset-specific defaults
9458 */
9459 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009460 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009461 /*
9462 * NSA Suite B
9463 */
9464 case MBEDTLS_SSL_PRESET_SUITEB:
9465 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9466 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9467 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9468 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9469
9470 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9471 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9472 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9473 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9474 ssl_preset_suiteb_ciphersuites;
9475
9476#if defined(MBEDTLS_X509_CRT_PARSE_C)
9477 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009478#endif
9479
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009480#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009481 conf->sig_hashes = ssl_preset_suiteb_hashes;
9482#endif
9483
9484#if defined(MBEDTLS_ECP_C)
9485 conf->curve_list = ssl_preset_suiteb_curves;
9486#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009487 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009488
9489 /*
9490 * Default
9491 */
9492 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009493 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9494 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9495 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9496 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9497 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9498 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9499 MBEDTLS_SSL_MIN_MINOR_VERSION :
9500 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009501 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9502 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9503
9504#if defined(MBEDTLS_SSL_PROTO_DTLS)
9505 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9506 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9507#endif
9508
9509 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9510 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9511 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9512 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9513 mbedtls_ssl_list_ciphersuites();
9514
9515#if defined(MBEDTLS_X509_CRT_PARSE_C)
9516 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9517#endif
9518
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009519#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009520 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009521#endif
9522
9523#if defined(MBEDTLS_ECP_C)
9524 conf->curve_list = mbedtls_ecp_grp_id_list();
9525#endif
9526
9527#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9528 conf->dhm_min_bitlen = 1024;
9529#endif
9530 }
9531
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009532 return( 0 );
9533}
9534
9535/*
9536 * Free mbedtls_ssl_config
9537 */
9538void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9539{
9540#if defined(MBEDTLS_DHM_C)
9541 mbedtls_mpi_free( &conf->dhm_P );
9542 mbedtls_mpi_free( &conf->dhm_G );
9543#endif
9544
9545#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9546 if( conf->psk != NULL )
9547 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009548 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009549 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009550 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009551 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009552 }
9553
9554 if( conf->psk_identity != NULL )
9555 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009556 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009557 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009558 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009559 conf->psk_identity_len = 0;
9560 }
9561#endif
9562
9563#if defined(MBEDTLS_X509_CRT_PARSE_C)
9564 ssl_key_cert_free( conf->key_cert );
9565#endif
9566
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009567 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009568}
9569
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009570#if defined(MBEDTLS_PK_C) && \
9571 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009572/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009573 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009575unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009576{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009577#if defined(MBEDTLS_RSA_C)
9578 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9579 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009580#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009581#if defined(MBEDTLS_ECDSA_C)
9582 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9583 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009584#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009585 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009586}
9587
Hanno Becker7e5437a2017-04-28 17:15:26 +01009588unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9589{
9590 switch( type ) {
9591 case MBEDTLS_PK_RSA:
9592 return( MBEDTLS_SSL_SIG_RSA );
9593 case MBEDTLS_PK_ECDSA:
9594 case MBEDTLS_PK_ECKEY:
9595 return( MBEDTLS_SSL_SIG_ECDSA );
9596 default:
9597 return( MBEDTLS_SSL_SIG_ANON );
9598 }
9599}
9600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009601mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009602{
9603 switch( sig )
9604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009605#if defined(MBEDTLS_RSA_C)
9606 case MBEDTLS_SSL_SIG_RSA:
9607 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009608#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009609#if defined(MBEDTLS_ECDSA_C)
9610 case MBEDTLS_SSL_SIG_ECDSA:
9611 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009612#endif
9613 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009614 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009615 }
9616}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009617#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009618
Hanno Becker7e5437a2017-04-28 17:15:26 +01009619#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9620 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9621
9622/* Find an entry in a signature-hash set matching a given hash algorithm. */
9623mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9624 mbedtls_pk_type_t sig_alg )
9625{
9626 switch( sig_alg )
9627 {
9628 case MBEDTLS_PK_RSA:
9629 return( set->rsa );
9630 case MBEDTLS_PK_ECDSA:
9631 return( set->ecdsa );
9632 default:
9633 return( MBEDTLS_MD_NONE );
9634 }
9635}
9636
9637/* Add a signature-hash-pair to a signature-hash set */
9638void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9639 mbedtls_pk_type_t sig_alg,
9640 mbedtls_md_type_t md_alg )
9641{
9642 switch( sig_alg )
9643 {
9644 case MBEDTLS_PK_RSA:
9645 if( set->rsa == MBEDTLS_MD_NONE )
9646 set->rsa = md_alg;
9647 break;
9648
9649 case MBEDTLS_PK_ECDSA:
9650 if( set->ecdsa == MBEDTLS_MD_NONE )
9651 set->ecdsa = md_alg;
9652 break;
9653
9654 default:
9655 break;
9656 }
9657}
9658
9659/* Allow exactly one hash algorithm for each signature. */
9660void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9661 mbedtls_md_type_t md_alg )
9662{
9663 set->rsa = md_alg;
9664 set->ecdsa = md_alg;
9665}
9666
9667#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9668 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9669
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009670/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009671 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009673mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009674{
9675 switch( hash )
9676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009677#if defined(MBEDTLS_MD5_C)
9678 case MBEDTLS_SSL_HASH_MD5:
9679 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009680#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681#if defined(MBEDTLS_SHA1_C)
9682 case MBEDTLS_SSL_HASH_SHA1:
9683 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009684#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009685#if defined(MBEDTLS_SHA256_C)
9686 case MBEDTLS_SSL_HASH_SHA224:
9687 return( MBEDTLS_MD_SHA224 );
9688 case MBEDTLS_SSL_HASH_SHA256:
9689 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009690#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009691#if defined(MBEDTLS_SHA512_C)
9692 case MBEDTLS_SSL_HASH_SHA384:
9693 return( MBEDTLS_MD_SHA384 );
9694 case MBEDTLS_SSL_HASH_SHA512:
9695 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009696#endif
9697 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009698 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009699 }
9700}
9701
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009702/*
9703 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9704 */
9705unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9706{
9707 switch( md )
9708 {
9709#if defined(MBEDTLS_MD5_C)
9710 case MBEDTLS_MD_MD5:
9711 return( MBEDTLS_SSL_HASH_MD5 );
9712#endif
9713#if defined(MBEDTLS_SHA1_C)
9714 case MBEDTLS_MD_SHA1:
9715 return( MBEDTLS_SSL_HASH_SHA1 );
9716#endif
9717#if defined(MBEDTLS_SHA256_C)
9718 case MBEDTLS_MD_SHA224:
9719 return( MBEDTLS_SSL_HASH_SHA224 );
9720 case MBEDTLS_MD_SHA256:
9721 return( MBEDTLS_SSL_HASH_SHA256 );
9722#endif
9723#if defined(MBEDTLS_SHA512_C)
9724 case MBEDTLS_MD_SHA384:
9725 return( MBEDTLS_SSL_HASH_SHA384 );
9726 case MBEDTLS_MD_SHA512:
9727 return( MBEDTLS_SSL_HASH_SHA512 );
9728#endif
9729 default:
9730 return( MBEDTLS_SSL_HASH_NONE );
9731 }
9732}
9733
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009734#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009735/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009736 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009737 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009738 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009739int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009740{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009741 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009742
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009743 if( ssl->conf->curve_list == NULL )
9744 return( -1 );
9745
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009746 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009747 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009748 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009749
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009750 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009751}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009752#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009753
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009754#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009755/*
9756 * Check if a hash proposed by the peer is in our list.
9757 * Return 0 if we're willing to use it, -1 otherwise.
9758 */
9759int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9760 mbedtls_md_type_t md )
9761{
9762 const int *cur;
9763
9764 if( ssl->conf->sig_hashes == NULL )
9765 return( -1 );
9766
9767 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9768 if( *cur == (int) md )
9769 return( 0 );
9770
9771 return( -1 );
9772}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009773#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009775#if defined(MBEDTLS_X509_CRT_PARSE_C)
9776int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9777 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009778 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009779 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009780{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009781 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009782#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009783 int usage = 0;
9784#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009785#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009786 const char *ext_oid;
9787 size_t ext_len;
9788#endif
9789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009790#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9791 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009792 ((void) cert);
9793 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009794 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009795#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009797#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9798 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009799 {
9800 /* Server part of the key exchange */
9801 switch( ciphersuite->key_exchange )
9802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009803 case MBEDTLS_KEY_EXCHANGE_RSA:
9804 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009805 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009806 break;
9807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009808 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9809 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9810 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9811 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009812 break;
9813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009814 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9815 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009816 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009817 break;
9818
9819 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820 case MBEDTLS_KEY_EXCHANGE_NONE:
9821 case MBEDTLS_KEY_EXCHANGE_PSK:
9822 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9823 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009824 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009825 usage = 0;
9826 }
9827 }
9828 else
9829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009830 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9831 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009832 }
9833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009834 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009835 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009836 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009837 ret = -1;
9838 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009839#else
9840 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9844 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009846 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9847 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009848 }
9849 else
9850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009851 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9852 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009853 }
9854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009855 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009856 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009857 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009858 ret = -1;
9859 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009860#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009861
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009862 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009863}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009864#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009865
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009866/*
9867 * Convert version numbers to/from wire format
9868 * and, for DTLS, to/from TLS equivalent.
9869 *
9870 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009871 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009872 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9873 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9874 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009875void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009876 unsigned char ver[2] )
9877{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009878#if defined(MBEDTLS_SSL_PROTO_DTLS)
9879 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009881 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009882 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9883
9884 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9885 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9886 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009887 else
9888#else
9889 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009890#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009891 {
9892 ver[0] = (unsigned char) major;
9893 ver[1] = (unsigned char) minor;
9894 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009895}
9896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009897void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009898 const unsigned char ver[2] )
9899{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009900#if defined(MBEDTLS_SSL_PROTO_DTLS)
9901 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009902 {
9903 *major = 255 - ver[0] + 2;
9904 *minor = 255 - ver[1] + 1;
9905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009906 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009907 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9908 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009909 else
9910#else
9911 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009912#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009913 {
9914 *major = ver[0];
9915 *minor = ver[1];
9916 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009917}
9918
Simon Butcher99000142016-10-13 17:21:01 +01009919int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9920{
9921#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9922 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9923 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9924
9925 switch( md )
9926 {
9927#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9928#if defined(MBEDTLS_MD5_C)
9929 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009930 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009931#endif
9932#if defined(MBEDTLS_SHA1_C)
9933 case MBEDTLS_SSL_HASH_SHA1:
9934 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9935 break;
9936#endif
9937#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9938#if defined(MBEDTLS_SHA512_C)
9939 case MBEDTLS_SSL_HASH_SHA384:
9940 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9941 break;
9942#endif
9943#if defined(MBEDTLS_SHA256_C)
9944 case MBEDTLS_SSL_HASH_SHA256:
9945 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9946 break;
9947#endif
9948 default:
9949 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9950 }
9951
9952 return 0;
9953#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9954 (void) ssl;
9955 (void) md;
9956
9957 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9958#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9959}
9960
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009961#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9962 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9963int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9964 unsigned char *output,
9965 unsigned char *data, size_t data_len )
9966{
9967 int ret = 0;
9968 mbedtls_md5_context mbedtls_md5;
9969 mbedtls_sha1_context mbedtls_sha1;
9970
9971 mbedtls_md5_init( &mbedtls_md5 );
9972 mbedtls_sha1_init( &mbedtls_sha1 );
9973
9974 /*
9975 * digitally-signed struct {
9976 * opaque md5_hash[16];
9977 * opaque sha_hash[20];
9978 * };
9979 *
9980 * md5_hash
9981 * MD5(ClientHello.random + ServerHello.random
9982 * + ServerParams);
9983 * sha_hash
9984 * SHA(ClientHello.random + ServerHello.random
9985 * + ServerParams);
9986 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009987 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009988 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009989 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009990 goto exit;
9991 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009992 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009993 ssl->handshake->randbytes, 64 ) ) != 0 )
9994 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009995 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009996 goto exit;
9997 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009998 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009999 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010000 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010001 goto exit;
10002 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010003 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010004 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010005 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010006 goto exit;
10007 }
10008
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010009 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010010 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010011 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010012 goto exit;
10013 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010014 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010015 ssl->handshake->randbytes, 64 ) ) != 0 )
10016 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010017 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010018 goto exit;
10019 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010020 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010021 data_len ) ) != 0 )
10022 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010023 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010024 goto exit;
10025 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010026 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010027 output + 16 ) ) != 0 )
10028 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010029 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010030 goto exit;
10031 }
10032
10033exit:
10034 mbedtls_md5_free( &mbedtls_md5 );
10035 mbedtls_sha1_free( &mbedtls_sha1 );
10036
10037 if( ret != 0 )
10038 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10039 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10040
10041 return( ret );
10042
10043}
10044#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10045 MBEDTLS_SSL_PROTO_TLS1_1 */
10046
10047#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10048 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10049int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010050 unsigned char *hash, size_t *hashlen,
10051 unsigned char *data, size_t data_len,
10052 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010053{
10054 int ret = 0;
10055 mbedtls_md_context_t ctx;
10056 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010057 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010058
10059 mbedtls_md_init( &ctx );
10060
10061 /*
10062 * digitally-signed struct {
10063 * opaque client_random[32];
10064 * opaque server_random[32];
10065 * ServerDHParams params;
10066 * };
10067 */
10068 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10069 {
10070 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10071 goto exit;
10072 }
10073 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10074 {
10075 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10076 goto exit;
10077 }
10078 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10079 {
10080 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10081 goto exit;
10082 }
10083 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10084 {
10085 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10086 goto exit;
10087 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010088 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010089 {
10090 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10091 goto exit;
10092 }
10093
10094exit:
10095 mbedtls_md_free( &ctx );
10096
10097 if( ret != 0 )
10098 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10099 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10100
10101 return( ret );
10102}
10103#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10104 MBEDTLS_SSL_PROTO_TLS1_2 */
10105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010106#endif /* MBEDTLS_SSL_TLS_C */