blob: d0fadfdc6cd53ecc6227631914589f1c6e3b536c [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;
507 psa_key_slot_t master_slot;
508 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
509
510 status = mbedtls_psa_get_free_key_slot( &master_slot );
511 if( status != PSA_SUCCESS )
512 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
513 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
518 psa_key_policy_init( &policy );
519 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 )
553 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
554
555 status = psa_destroy_key( master_slot );
556 if( status != PSA_SUCCESS )
557 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
558
559 return 0;
560}
561
562#else /* MBEDTLS_USE_PSA_CRYPTO */
563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100565 const unsigned char *secret, size_t slen,
566 const char *label,
567 const unsigned char *random, size_t rlen,
568 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000569{
570 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100571 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000572 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
574 const mbedtls_md_info_t *md_info;
575 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100576 int ret;
577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
581 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100584
585 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000587
588 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100589 memcpy( tmp + md_len, label, nb );
590 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000591 nb += rlen;
592
593 /*
594 * Compute P_<hash>(secret, label + random)[0..dlen]
595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100597 return( ret );
598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
600 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
601 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100602
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100603 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_md_hmac_reset ( &md_ctx );
606 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
607 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_md_hmac_reset ( &md_ctx );
610 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
611 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000612
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100613 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000614
615 for( j = 0; j < k; j++ )
616 dstbuf[i + j] = h_i[j];
617 }
618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100620
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500621 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
622 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000623
624 return( 0 );
625}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500626#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100628static int tls_prf_sha256( const unsigned char *secret, size_t slen,
629 const char *label,
630 const unsigned char *random, size_t rlen,
631 unsigned char *dstbuf, size_t dlen )
632{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100634 label, random, rlen, dstbuf, dlen ) );
635}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200639static int tls_prf_sha384( const unsigned char *secret, size_t slen,
640 const char *label,
641 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000642 unsigned char *dstbuf, size_t dlen )
643{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100645 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000646}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647#endif /* MBEDTLS_SHA512_C */
648#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
653 defined(MBEDTLS_SSL_PROTO_TLS1_1)
654static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200655#endif
Paul Bakker380da532012-04-18 16:10:25 +0000656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657#if defined(MBEDTLS_SSL_PROTO_SSL3)
658static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
659static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200660#endif
661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
663static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
664static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200665#endif
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
668#if defined(MBEDTLS_SHA256_C)
669static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
670static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
671static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200672#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674#if defined(MBEDTLS_SHA512_C)
675static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
676static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
677static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100678#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000680
Hanno Becker7d0a5692018-10-23 15:26:22 +0100681#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
682 defined(MBEDTLS_USE_PSA_CRYPTO)
683static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
684{
685 if( ssl->conf->f_psk != NULL )
686 {
687 /* If we've used a callback to select the PSK,
688 * the static configuration is irrelevant. */
689 if( ssl->handshake->psk_opaque != 0 )
690 return( 1 );
691
692 return( 0 );
693 }
694
695 if( ssl->conf->psk_opaque != 0 )
696 return( 1 );
697
698 return( 0 );
699}
700#endif /* MBEDTLS_USE_PSA_CRYPTO &&
701 MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000704{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200705 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000706#if defined(MBEDTLS_USE_PSA_CRYPTO)
707 int psa_fallthrough;
708#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000709 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000710 unsigned char keyblk[256];
711 unsigned char *key1;
712 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100713 unsigned char *mac_enc;
714 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000715 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200716 size_t iv_copy_len;
Hanno Beckerf704bef2018-11-16 15:21:18 +0000717 size_t taglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 const mbedtls_cipher_info_t *cipher_info;
719 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100720
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000721 /* cf. RFC 5246, Section 8.1:
722 * "The master secret is always exactly 48 bytes in length." */
723 size_t const master_secret_len = 48;
724
Hanno Becker35b23c72018-10-23 12:10:41 +0100725#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
726 unsigned char session_hash[48];
727#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_ssl_session *session = ssl->session_negotiate;
730 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
731 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100736 if( cipher_info == NULL )
737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100739 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100741 }
742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100744 if( md_info == NULL )
745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100747 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100749 }
750
Paul Bakker5121ce52009-01-03 21:22:43 +0000751 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000752 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000753 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754#if defined(MBEDTLS_SSL_PROTO_SSL3)
755 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000756 {
Paul Bakker48916f92012-09-16 19:57:18 +0000757 handshake->tls_prf = ssl3_prf;
758 handshake->calc_verify = ssl_calc_verify_ssl;
759 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000760 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200761 else
762#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
764 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000765 {
Paul Bakker48916f92012-09-16 19:57:18 +0000766 handshake->tls_prf = tls1_prf;
767 handshake->calc_verify = ssl_calc_verify_tls;
768 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000769 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200770 else
771#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
773#if defined(MBEDTLS_SHA512_C)
774 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
775 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000776 {
Paul Bakker48916f92012-09-16 19:57:18 +0000777 handshake->tls_prf = tls_prf_sha384;
778 handshake->calc_verify = ssl_calc_verify_tls_sha384;
779 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000780 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000781 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200782#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783#if defined(MBEDTLS_SHA256_C)
784 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000785 {
Paul Bakker48916f92012-09-16 19:57:18 +0000786 handshake->tls_prf = tls_prf_sha256;
787 handshake->calc_verify = ssl_calc_verify_tls_sha256;
788 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000789 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200790 else
791#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
795 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200796 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000797
798 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000799 * SSLv3:
800 * master =
801 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
802 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
803 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200804 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200805 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000806 * master = PRF( premaster, "master secret", randbytes )[0..47]
807 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100808 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000809 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100810 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
811 }
812 else
813 {
814 /* The label for the KDF used for key expansion.
815 * This is either "master secret" or "extended master secret"
816 * depending on whether the Extended Master Secret extension
817 * is used. */
818 char const *lbl = "master secret";
819
820 /* The salt for the KDF used for key expansion.
821 * - If the Extended Master Secret extension is not used,
822 * this is ClientHello.Random + ServerHello.Random
823 * (see Sect. 8.1 in RFC 5246).
824 * - If the Extended Master Secret extension is used,
825 * this is the transcript of the handshake so far.
826 * (see Sect. 4 in RFC 7627). */
827 unsigned char const *salt = handshake->randbytes;
828 size_t salt_len = 64;
829
830#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
831 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
832 ssl->transform_negotiate->ciphersuite_info;
833 mbedtls_md_type_t const md_type = ciphersuite_info->mac;
834#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Paul Bakker5121ce52009-01-03 21:22:43 +0000835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
837 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200840
Hanno Becker35b23c72018-10-23 12:10:41 +0100841 lbl = "extended master secret";
842 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200843 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
845 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#if defined(MBEDTLS_SHA512_C)
Hanno Becker35b23c72018-10-23 12:10:41 +0100848 if( md_type == MBEDTLS_MD_SHA384 )
849 salt_len = 48;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200850 else
Hanno Becker35b23c72018-10-23 12:10:41 +0100851#endif /* MBEDTLS_SHA512_C */
852 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200853 }
854 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100856 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200857
Hanno Becker35b23c72018-10-23 12:10:41 +0100858 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200859 }
Hanno Becker35b23c72018-10-23 12:10:41 +0100860#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
861
Hanno Becker7d0a5692018-10-23 15:26:22 +0100862#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
863 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
864 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
865 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
866 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100867 {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100868 /* Perform PSK-to-MS expansion in a single step. */
869 psa_status_t status;
870 psa_algorithm_t alg;
871 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500872 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100873
874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
875
876 psk = ssl->conf->psk_opaque;
877 if( ssl->handshake->psk_opaque != 0 )
878 psk = ssl->handshake->psk_opaque;
879
880 if( md_type == MBEDTLS_MD_SHA384 )
881 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
882 else
883 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
884
885 status = psa_key_derivation( &generator, psk, alg,
886 salt, salt_len,
887 (unsigned char const *) lbl,
888 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000889 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100890 if( status != PSA_SUCCESS )
891 {
892 psa_generator_abort( &generator );
893 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
894 }
895
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000896 status = psa_generator_read( &generator, session->master,
897 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100898 if( status != PSA_SUCCESS )
899 {
900 psa_generator_abort( &generator );
901 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
902 }
903
904 status = psa_generator_abort( &generator );
905 if( status != PSA_SUCCESS )
906 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100907 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100908 else
909#endif
910 {
911 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
912 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000913 session->master,
914 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100915 if( ret != 0 )
916 {
917 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
918 return( ret );
919 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200920
Hanno Becker7d0a5692018-10-23 15:26:22 +0100921 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
922 handshake->premaster,
923 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +0100924
Hanno Becker7d0a5692018-10-23 15:26:22 +0100925 mbedtls_platform_zeroize( handshake->premaster,
926 sizeof(handshake->premaster) );
927 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000928 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000929
930 /*
931 * Swap the client and server random values.
932 */
Paul Bakker48916f92012-09-16 19:57:18 +0000933 memcpy( tmp, handshake->randbytes, 64 );
934 memcpy( handshake->randbytes, tmp + 32, 32 );
935 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500936 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000937
938 /*
939 * SSLv3:
940 * key block =
941 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
942 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
943 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
944 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
945 * ...
946 *
947 * TLSv1:
948 * key block = PRF( master, "key expansion", randbytes )
949 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100950 ret = handshake->tls_prf( session->master, 48, "key expansion",
951 handshake->randbytes, 64, keyblk, 256 );
952 if( ret != 0 )
953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100955 return( ret );
956 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
959 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
960 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
961 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
962 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000963
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500964 mbedtls_platform_zeroize( handshake->randbytes,
965 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000966
967 /*
968 * Determine the appropriate key, IV and MAC length.
969 */
Paul Bakker68884e32013-01-07 18:20:04 +0100970
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200971 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200974 cipher_info->mode == MBEDTLS_MODE_CCM ||
975 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000976 {
Hanno Beckerf704bef2018-11-16 15:21:18 +0000977 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200978
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200979 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000980 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200981
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200982 /* All modes haves 96-bit IVs;
983 * GCM and CCM has 4 implicit and 8 explicit bytes
984 * ChachaPoly has all 12 bytes implicit
985 */
Paul Bakker68884e32013-01-07 18:20:04 +0100986 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200987 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
988 transform->fixed_ivlen = 12;
989 else
990 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200991
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200992 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
993 taglen = transform->ciphersuite_info->flags &
994 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
995
996
997 /* Minimum length of encrypted record */
998 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
999 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001000 }
1001 else
1002 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001003 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1005 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001008 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001009 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001010
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001011 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001012 mac_key_len = mbedtls_md_get_size( md_info );
1013 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001016 /*
1017 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1018 * (rfc 6066 page 13 or rfc 2104 section 4),
1019 * so we only need to adjust the length here.
1020 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001024
1025#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1026 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001027 * HMAC implementation which also truncates the key
1028 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001029 mac_key_len = transform->maclen;
1030#endif
1031 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001033
1034 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001035 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001036
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001037 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001039 transform->minlen = transform->maclen;
1040 else
Paul Bakker68884e32013-01-07 18:20:04 +01001041 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001042 /*
1043 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001044 * 1. if EtM is in use: one block plus MAC
1045 * otherwise: * first multiple of blocklen greater than maclen
1046 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001047 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1049 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001050 {
1051 transform->minlen = transform->maclen
1052 + cipher_info->block_size;
1053 }
1054 else
1055#endif
1056 {
1057 transform->minlen = transform->maclen
1058 + cipher_info->block_size
1059 - transform->maclen % cipher_info->block_size;
1060 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1063 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1064 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001065 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001066 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001067#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1069 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1070 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001071 {
1072 transform->minlen += transform->ivlen;
1073 }
1074 else
1075#endif
1076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1078 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001079 }
Paul Bakker68884e32013-01-07 18:20:04 +01001080 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 }
1082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001084 transform->keylen, transform->minlen, transform->ivlen,
1085 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001086
1087 /*
1088 * Finally setup the cipher contexts, IVs and MAC secrets.
1089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001091 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001092 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001093 key1 = keyblk + mac_key_len * 2;
1094 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001095
Paul Bakker68884e32013-01-07 18:20:04 +01001096 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001097 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001098
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001099 /*
1100 * This is not used in TLS v1.1.
1101 */
Paul Bakker48916f92012-09-16 19:57:18 +00001102 iv_copy_len = ( transform->fixed_ivlen ) ?
1103 transform->fixed_ivlen : transform->ivlen;
1104 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
1105 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001106 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001107 }
1108 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109#endif /* MBEDTLS_SSL_CLI_C */
1110#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001111 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001113 key1 = keyblk + mac_key_len * 2 + transform->keylen;
1114 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001115
Hanno Becker81c7b182017-11-09 18:39:33 +00001116 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001117 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001118
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001119 /*
1120 * This is not used in TLS v1.1.
1121 */
Paul Bakker48916f92012-09-16 19:57:18 +00001122 iv_copy_len = ( transform->fixed_ivlen ) ?
1123 transform->fixed_ivlen : transform->ivlen;
1124 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
1125 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001126 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001127 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001128 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1132 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001133 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135#if defined(MBEDTLS_SSL_PROTO_SSL3)
1136 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001137 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001138 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1141 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001142 }
1143
Hanno Becker81c7b182017-11-09 18:39:33 +00001144 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1145 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001146 }
1147 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1149#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1150 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1151 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001152 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001153 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1154 For AEAD-based ciphersuites, there is nothing to do here. */
1155 if( mac_key_len != 0 )
1156 {
1157 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1158 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1159 }
Paul Bakker68884e32013-01-07 18:20:04 +01001160 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001161 else
1162#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1165 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001166 }
Paul Bakker68884e32013-01-07 18:20:04 +01001167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1169 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001170 {
1171 int ret = 0;
1172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001176 transform->iv_enc, transform->iv_dec,
1177 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001178 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001179 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1182 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001183 }
1184 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001186
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001187#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1188 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001189 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001190 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1191 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001192 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001193 iv_copy_len );
1194 }
1195#endif
1196
Hanno Beckerf704bef2018-11-16 15:21:18 +00001197#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001198
1199 /* Only use PSA-based ciphers for TLS-1.2.
1200 * That's relevant at least for TLS-1.0, where
1201 * we assume that mbedtls_cipher_crypt() updates
1202 * the structure field for the IV, which the PSA-based
1203 * implementation currently doesn't. */
1204#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1205 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001206 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001207 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
1208 cipher_info, taglen );
1209 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1210 {
1211 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1212 return( ret );
1213 }
1214
1215 if( ret == 0 )
1216 {
1217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based encryption cipher context" ) );
1218 psa_fallthrough = 0;
1219 }
1220 else
1221 {
1222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1223 psa_fallthrough = 1;
1224 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001225 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001226 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001227 psa_fallthrough = 1;
1228#else
1229 psa_fallthrough = 1;
1230#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001231
Hanno Beckercb1cc802018-11-17 22:27:38 +00001232 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001233#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001234 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001235 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001236 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001237 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001238 return( ret );
1239 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001240
Hanno Beckerf704bef2018-11-16 15:21:18 +00001241#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001242 /* Only use PSA-based ciphers for TLS-1.2.
1243 * That's relevant at least for TLS-1.0, where
1244 * we assume that mbedtls_cipher_crypt() updates
1245 * the structure field for the IV, which the PSA-based
1246 * implementation currently doesn't. */
1247#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1248 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001249 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001250 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
1251 cipher_info, taglen );
1252 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1253 {
1254 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1255 return( ret );
1256 }
1257
1258 if( ret == 0 )
1259 {
1260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based decryption cipher context" ) );
1261 psa_fallthrough = 0;
1262 }
1263 else
1264 {
1265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1266 psa_fallthrough = 1;
1267 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001268 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001269 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001270 psa_fallthrough = 1;
1271#else
1272 psa_fallthrough = 1;
1273#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001274
Hanno Beckercb1cc802018-11-17 22:27:38 +00001275 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001276#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001277 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001278 cipher_info ) ) != 0 )
1279 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001280 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001281 return( ret );
1282 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001285 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001289 return( ret );
1290 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001293 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001297 return( ret );
1298 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#if defined(MBEDTLS_CIPHER_MODE_CBC)
1301 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1304 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001307 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001308 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1311 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001314 return( ret );
1315 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001318
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001319 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001322 // Initialize compression
1323 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001325 {
Paul Bakker16770332013-10-11 09:59:44 +02001326 if( ssl->compress_buf == NULL )
1327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001329 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001330 if( ssl->compress_buf == NULL )
1331 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001333 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001334 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001335 }
1336 }
1337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001339
Paul Bakker48916f92012-09-16 19:57:18 +00001340 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1341 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001342
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001343 if( deflateInit( &transform->ctx_deflate,
1344 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001345 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1348 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001349 }
1350 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001354
1355 return( 0 );
1356}
1357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358#if defined(MBEDTLS_SSL_PROTO_SSL3)
1359void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001360{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001361 mbedtls_md5_context md5;
1362 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001363 unsigned char pad_1[48];
1364 unsigned char pad_2[48];
1365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001367
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001368 mbedtls_md5_init( &md5 );
1369 mbedtls_sha1_init( &sha1 );
1370
1371 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1372 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001373
Paul Bakker380da532012-04-18 16:10:25 +00001374 memset( pad_1, 0x36, 48 );
1375 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001377 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1378 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1379 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001380
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001381 mbedtls_md5_starts_ret( &md5 );
1382 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1383 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1384 mbedtls_md5_update_ret( &md5, hash, 16 );
1385 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001386
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001387 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1388 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1389 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001390
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001391 mbedtls_sha1_starts_ret( &sha1 );
1392 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1393 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1394 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1395 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001399
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001400 mbedtls_md5_free( &md5 );
1401 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001402
Paul Bakker380da532012-04-18 16:10:25 +00001403 return;
1404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1408void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001409{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001410 mbedtls_md5_context md5;
1411 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001414
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001415 mbedtls_md5_init( &md5 );
1416 mbedtls_sha1_init( &sha1 );
1417
1418 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1419 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001420
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001421 mbedtls_md5_finish_ret( &md5, hash );
1422 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001426
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001427 mbedtls_md5_free( &md5 );
1428 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001429
Paul Bakker380da532012-04-18 16:10:25 +00001430 return;
1431}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1435#if defined(MBEDTLS_SHA256_C)
1436void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001437{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001438 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001439
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001440 mbedtls_sha256_init( &sha256 );
1441
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001443
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001444 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001445 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001449
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001450 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001451
Paul Bakker380da532012-04-18 16:10:25 +00001452 return;
1453}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456#if defined(MBEDTLS_SHA512_C)
1457void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001458{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001459 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001460
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001461 mbedtls_sha512_init( &sha512 );
1462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001464
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001465 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001466 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001470
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001471 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001472
Paul Bakker5121ce52009-01-03 21:22:43 +00001473 return;
1474}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475#endif /* MBEDTLS_SHA512_C */
1476#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1479int 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 +02001480{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001481 unsigned char *p = ssl->handshake->premaster;
1482 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001483 const unsigned char *psk = ssl->conf->psk;
1484 size_t psk_len = ssl->conf->psk_len;
1485
1486 /* If the psk callback was called, use its result */
1487 if( ssl->handshake->psk != NULL )
1488 {
1489 psk = ssl->handshake->psk;
1490 psk_len = ssl->handshake->psk_len;
1491 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001492
1493 /*
1494 * PMS = struct {
1495 * opaque other_secret<0..2^16-1>;
1496 * opaque psk<0..2^16-1>;
1497 * };
1498 * with "other_secret" depending on the particular key exchange
1499 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1501 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001502 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001503 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001505
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001506 *(p++) = (unsigned char)( psk_len >> 8 );
1507 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001508
1509 if( end < p || (size_t)( end - p ) < psk_len )
1510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1511
1512 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001513 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001514 }
1515 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1517#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1518 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001519 {
1520 /*
1521 * other_secret already set by the ClientKeyExchange message,
1522 * and is 48 bytes long
1523 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001524 if( end - p < 2 )
1525 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1526
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001527 *p++ = 0;
1528 *p++ = 48;
1529 p += 48;
1530 }
1531 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1533#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1534 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001535 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001536 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001537 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001538
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001539 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001541 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001542 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001545 return( ret );
1546 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001547 *(p++) = (unsigned char)( len >> 8 );
1548 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001549 p += len;
1550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001552 }
1553 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1555#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1556 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001557 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001558 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001559 size_t zlen;
1560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001562 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001563 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001566 return( ret );
1567 }
1568
1569 *(p++) = (unsigned char)( zlen >> 8 );
1570 *(p++) = (unsigned char)( zlen );
1571 p += zlen;
1572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001574 }
1575 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1579 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001580 }
1581
1582 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001583 if( end - p < 2 )
1584 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001585
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001586 *(p++) = (unsigned char)( psk_len >> 8 );
1587 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001588
1589 if( end < p || (size_t)( end - p ) < psk_len )
1590 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1591
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001592 memcpy( p, psk, psk_len );
1593 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001594
1595 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1596
1597 return( 0 );
1598}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001602/*
1603 * SSLv3.0 MAC functions
1604 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001605#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001606static void ssl_mac( mbedtls_md_context_t *md_ctx,
1607 const unsigned char *secret,
1608 const unsigned char *buf, size_t len,
1609 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001610 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001611{
1612 unsigned char header[11];
1613 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001614 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1616 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001617
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001618 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001620 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001621 else
Paul Bakker68884e32013-01-07 18:20:04 +01001622 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001623
1624 memcpy( header, ctr, 8 );
1625 header[ 8] = (unsigned char) type;
1626 header[ 9] = (unsigned char)( len >> 8 );
1627 header[10] = (unsigned char)( len );
1628
Paul Bakker68884e32013-01-07 18:20:04 +01001629 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 mbedtls_md_starts( md_ctx );
1631 mbedtls_md_update( md_ctx, secret, md_size );
1632 mbedtls_md_update( md_ctx, padding, padlen );
1633 mbedtls_md_update( md_ctx, header, 11 );
1634 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001635 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001636
Paul Bakker68884e32013-01-07 18:20:04 +01001637 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 mbedtls_md_starts( md_ctx );
1639 mbedtls_md_update( md_ctx, secret, md_size );
1640 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001641 mbedtls_md_update( md_ctx, out, md_size );
1642 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001643}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1647 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001648 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001649#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001650#endif
1651
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001652/* The function below is only used in the Lucky 13 counter-measure in
1653 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1654#if defined(SSL_SOME_MODES_USE_MAC) && \
1655 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1656 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1657 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1658/* This function makes sure every byte in the memory region is accessed
1659 * (in ascending addresses order) */
1660static void ssl_read_memory( unsigned char *p, size_t len )
1661{
1662 unsigned char acc = 0;
1663 volatile unsigned char force;
1664
1665 for( ; len != 0; p++, len-- )
1666 acc ^= *p;
1667
1668 force = acc;
1669 (void) force;
1670}
1671#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1672
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001673/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001674 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001675 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001677{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001679 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001682
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001683 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1686 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001687 }
1688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001692 ssl->out_msg, ssl->out_msglen );
1693
Paul Bakker5121ce52009-01-03 21:22:43 +00001694 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001695 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001696 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001697#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 if( mode == MBEDTLS_MODE_STREAM ||
1699 ( mode == MBEDTLS_MODE_CBC
1700#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1701 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001702#endif
1703 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705#if defined(MBEDTLS_SSL_PROTO_SSL3)
1706 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001707 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001708 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001709
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001710 ssl_mac( &ssl->transform_out->md_ctx_enc,
1711 ssl->transform_out->mac_enc,
1712 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001713 ssl->out_ctr, ssl->out_msgtype,
1714 mac );
1715
1716 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001717 }
1718 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001719#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1721 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1722 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001723 {
Hanno Becker992b6872017-11-09 18:57:39 +00001724 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1727 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1728 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1729 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001730 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001731 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001733
1734 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001735 }
1736 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001737#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1740 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001741 }
1742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001744 ssl->out_msg + ssl->out_msglen,
1745 ssl->transform_out->maclen );
1746
1747 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001748 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001749 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001750#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001751
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001752 /*
1753 * Encrypt
1754 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1756 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001757 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001758 int ret;
1759 size_t olen = 0;
1760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001762 "including %d bytes of padding",
1763 ssl->out_msglen, 0 ) );
1764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001766 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001767 ssl->transform_out->ivlen,
1768 ssl->out_msg, ssl->out_msglen,
1769 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001772 return( ret );
1773 }
1774
1775 if( ssl->out_msglen != olen )
1776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1778 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001779 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001780 }
Paul Bakker68884e32013-01-07 18:20:04 +01001781 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001783#if defined(MBEDTLS_GCM_C) || \
1784 defined(MBEDTLS_CCM_C) || \
1785 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001787 mode == MBEDTLS_MODE_CCM ||
1788 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001789 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001790 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001791 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001792 unsigned char *enc_msg;
1793 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001794 unsigned char iv[12];
1795 mbedtls_ssl_transform *transform = ssl->transform_out;
1796 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001798 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001799
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001800 /*
1801 * Prepare additional authenticated data
1802 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001803 memcpy( add_data, ssl->out_ctr, 8 );
1804 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001806 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001807 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1808 add_data[12] = ssl->out_msglen & 0xFF;
1809
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001810 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001811
Paul Bakker68884e32013-01-07 18:20:04 +01001812 /*
1813 * Generate IV
1814 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001815 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1816 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001817 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001818 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1819 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1820 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1821
1822 }
1823 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1824 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001825 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001826 unsigned char i;
1827
1828 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1829
1830 for( i = 0; i < 8; i++ )
1831 iv[i+4] ^= ssl->out_ctr[i];
1832 }
1833 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001834 {
1835 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1837 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001838 }
1839
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001840 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1841 iv, transform->ivlen );
1842 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1843 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001844
Paul Bakker68884e32013-01-07 18:20:04 +01001845 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001846 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001847 */
1848 enc_msg = ssl->out_msg;
1849 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001850 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001853 "including 0 bytes of padding",
1854 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001855
Paul Bakker68884e32013-01-07 18:20:04 +01001856 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001857 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001858 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001859 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1860 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001861 add_data, 13,
1862 enc_msg, enc_msglen,
1863 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001864 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001867 return( ret );
1868 }
1869
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001870 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1873 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001874 }
1875
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001876 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001877 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001880 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001881 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1883#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001884 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001886 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001887 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001888 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001889 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001890
Paul Bakker48916f92012-09-16 19:57:18 +00001891 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1892 ssl->transform_out->ivlen;
1893 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001894 padlen = 0;
1895
1896 for( i = 0; i <= padlen; i++ )
1897 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1898
1899 ssl->out_msglen += padlen + 1;
1900
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001901 enc_msglen = ssl->out_msglen;
1902 enc_msg = ssl->out_msg;
1903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001905 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001906 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1907 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001908 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001910 {
1911 /*
1912 * Generate IV
1913 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001914 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001915 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001916 if( ret != 0 )
1917 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001918
Paul Bakker92be97b2013-01-02 17:30:03 +01001919 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001920 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001921
1922 /*
1923 * Fix pointer positions and message length with added IV
1924 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001925 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001926 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001927 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001928 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001932 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001933 ssl->out_msglen, ssl->transform_out->ivlen,
1934 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001937 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001938 ssl->transform_out->ivlen,
1939 enc_msg, enc_msglen,
1940 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001943 return( ret );
1944 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001945
Paul Bakkercca5b812013-08-31 17:40:26 +02001946 if( enc_msglen != olen )
1947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1949 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001950 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1953 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001954 {
1955 /*
1956 * Save IV in SSL3 and TLS1
1957 */
1958 memcpy( ssl->transform_out->iv_enc,
1959 ssl->transform_out->cipher_ctx_enc.iv,
1960 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001961 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001962#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001965 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001966 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001967 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1968
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001969 /*
1970 * MAC(MAC_write_key, seq_num +
1971 * TLSCipherText.type +
1972 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001973 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001974 * IV + // except for TLS 1.0
1975 * ENC(content + padding + padding_length));
1976 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001977 unsigned char pseudo_hdr[13];
1978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001980
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001981 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1982 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001983 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1984 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1989 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001990 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00001991 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001993
Hanno Becker3d8c9072018-01-05 16:24:22 +00001994 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1995 ssl->transform_out->maclen );
1996
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001997 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001998 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001999 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002001 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002002 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002004 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2007 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002008 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002009
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002010 /* Make extra sure authentication was performed, exactly once */
2011 if( auth_done != 1 )
2012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2014 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002015 }
2016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002018
2019 return( 0 );
2020}
2021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002023{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002025 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002026#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002027 size_t padlen = 0, correct = 1;
2028#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002031
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002032 if( ssl->session_in == NULL || ssl->transform_in == NULL )
2033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2035 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002036 }
2037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002039
Paul Bakker48916f92012-09-16 19:57:18 +00002040 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00002043 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 }
2046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2048 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002049 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002050 int ret;
2051 size_t olen = 0;
2052
Paul Bakker68884e32013-01-07 18:20:04 +01002053 padlen = 0;
2054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002056 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002057 ssl->transform_in->ivlen,
2058 ssl->in_msg, ssl->in_msglen,
2059 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002062 return( ret );
2063 }
2064
2065 if( ssl->in_msglen != olen )
2066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2068 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002069 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002070 }
Paul Bakker68884e32013-01-07 18:20:04 +01002071 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002073#if defined(MBEDTLS_GCM_C) || \
2074 defined(MBEDTLS_CCM_C) || \
2075 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002077 mode == MBEDTLS_MODE_CCM ||
2078 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002079 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002080 int ret;
2081 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002082 unsigned char *dec_msg;
2083 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002084 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002085 unsigned char iv[12];
2086 mbedtls_ssl_transform *transform = ssl->transform_in;
2087 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002089 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002090
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002091 /*
2092 * Compute and update sizes
2093 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002094 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002097 "+ taglen (%d)", ssl->in_msglen,
2098 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002100 }
2101 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
2102
Paul Bakker68884e32013-01-07 18:20:04 +01002103 dec_msg = ssl->in_msg;
2104 dec_msg_result = ssl->in_msg;
2105 ssl->in_msglen = dec_msglen;
2106
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002107 /*
2108 * Prepare additional authenticated data
2109 */
Paul Bakker68884e32013-01-07 18:20:04 +01002110 memcpy( add_data, ssl->in_ctr, 8 );
2111 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002113 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002114 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2115 add_data[12] = ssl->in_msglen & 0xFF;
2116
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002117 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002118
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002119 /*
2120 * Prepare IV
2121 */
2122 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2123 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002124 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002125 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2126 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002127
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002128 }
2129 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2130 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002131 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002132 unsigned char i;
2133
2134 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2135
2136 for( i = 0; i < 8; i++ )
2137 iv[i+4] ^= ssl->in_ctr[i];
2138 }
2139 else
2140 {
2141 /* Reminder if we ever add an AEAD mode with a different size */
2142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2143 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2144 }
2145
2146 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002148
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002149 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002150 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002153 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002154 add_data, 13,
2155 dec_msg, dec_msglen,
2156 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002157 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2162 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002163
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002164 return( ret );
2165 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002166 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002167
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002168 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2171 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002172 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002173 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002174 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2176#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002177 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002179 {
Paul Bakker45829992013-01-03 14:52:21 +01002180 /*
2181 * Decrypt and check the padding
2182 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002183 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002184 unsigned char *dec_msg;
2185 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002186 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002187 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002188 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002189
Paul Bakker5121ce52009-01-03 21:22:43 +00002190 /*
Paul Bakker45829992013-01-03 14:52:21 +01002191 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2194 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002195 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002196#endif
Paul Bakker45829992013-01-03 14:52:21 +01002197
2198 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2199 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002202 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2203 ssl->transform_in->ivlen,
2204 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002206 }
2207
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002208 dec_msglen = ssl->in_msglen;
2209 dec_msg = ssl->in_msg;
2210 dec_msg_result = ssl->in_msg;
2211
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002212 /*
2213 * Authenticate before decrypt if enabled
2214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2216 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002217 {
Hanno Becker992b6872017-11-09 18:57:39 +00002218 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002219 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002222
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002223 dec_msglen -= ssl->transform_in->maclen;
2224 ssl->in_msglen -= ssl->transform_in->maclen;
2225
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002226 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2227 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2228 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2229 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2234 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002235 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002236 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002240 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002241 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002242 ssl->transform_in->maclen );
2243
Hanno Becker992b6872017-11-09 18:57:39 +00002244 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2245 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002250 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002251 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002252 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002254
2255 /*
2256 * Check length sanity
2257 */
2258 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002261 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002263 }
2264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002266 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002267 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002268 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002270 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002271 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002272 dec_msglen -= ssl->transform_in->ivlen;
2273 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002274
Paul Bakker48916f92012-09-16 19:57:18 +00002275 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002276 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002277 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002281 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002282 ssl->transform_in->ivlen,
2283 dec_msg, dec_msglen,
2284 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002287 return( ret );
2288 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002289
Paul Bakkercca5b812013-08-31 17:40:26 +02002290 if( dec_msglen != olen )
2291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2293 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002294 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2297 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002298 {
2299 /*
2300 * Save IV in SSL3 and TLS1
2301 */
2302 memcpy( ssl->transform_in->iv_dec,
2303 ssl->transform_in->cipher_ctx_dec.iv,
2304 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002305 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002306#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002307
2308 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002309
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002310 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002311 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313#if defined(MBEDTLS_SSL_DEBUG_ALL)
2314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002315 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002316#endif
Paul Bakker45829992013-01-03 14:52:21 +01002317 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002318 correct = 0;
2319 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321#if defined(MBEDTLS_SSL_PROTO_SSL3)
2322 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002323 {
Paul Bakker48916f92012-09-16 19:57:18 +00002324 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326#if defined(MBEDTLS_SSL_DEBUG_ALL)
2327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002329 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002330#endif
Paul Bakker45829992013-01-03 14:52:21 +01002331 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002332 }
2333 }
2334 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2336#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2337 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2338 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002339 {
2340 /*
Paul Bakker45829992013-01-03 14:52:21 +01002341 * TLSv1+: always check the padding up to the first failure
2342 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002343 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002344 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002345 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002346 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002347
Paul Bakker956c9e02013-12-19 14:42:28 +01002348 /*
2349 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002350 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002351 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002352 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002353 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002354 *
2355 * In both cases we reset padding_idx to a safe value (0) to
2356 * prevent out-of-buffer reads.
2357 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002358 correct &= ( padlen <= ssl->in_msglen );
2359 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002360 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002361
2362 padding_idx *= correct;
2363
Angus Grattonb512bc12018-06-19 15:57:50 +10002364 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002365 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002366 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002367 pad_count += real_count *
2368 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2369 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002370
2371 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002374 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002376#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002377 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002379 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2381 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2384 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002385 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002386
2387 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002388 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002389 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002391 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2394 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002395 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002396
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002397#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002399 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002400#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002401
2402 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002403 * Authenticate if not done yet.
2404 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002405 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002406#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002407 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002408 {
Hanno Becker992b6872017-11-09 18:57:39 +00002409 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002410
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002411 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002412
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002413 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2414 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416#if defined(MBEDTLS_SSL_PROTO_SSL3)
2417 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002418 {
2419 ssl_mac( &ssl->transform_in->md_ctx_dec,
2420 ssl->transform_in->mac_dec,
2421 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002422 ssl->in_ctr, ssl->in_msgtype,
2423 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002424 }
2425 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2427#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2428 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2429 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002430 {
2431 /*
2432 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002433 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002434 *
2435 * Known timing attacks:
2436 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2437 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002438 * To compensate for different timings for the MAC calculation
2439 * depending on how much padding was removed (which is determined
2440 * by padlen), process extra_run more blocks through the hash
2441 * function.
2442 *
2443 * The formula in the paper is
2444 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2445 * where L1 is the size of the header plus the decrypted message
2446 * plus CBC padding and L2 is the size of the header plus the
2447 * decrypted message. This is for an underlying hash function
2448 * with 64-byte blocks.
2449 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2450 * correctly. We round down instead of up, so -56 is the correct
2451 * value for our calculations instead of -55.
2452 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002453 * Repeat the formula rather than defining a block_size variable.
2454 * This avoids requiring division by a variable at runtime
2455 * (which would be marginally less efficient and would require
2456 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002457 */
2458 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002459
2460 /*
2461 * The next two sizes are the minimum and maximum values of
2462 * in_msglen over all padlen values.
2463 *
2464 * They're independent of padlen, since we previously did
2465 * in_msglen -= padlen.
2466 *
2467 * Note that max_len + maclen is never more than the buffer
2468 * length, as we previously did in_msglen -= maclen too.
2469 */
2470 const size_t max_len = ssl->in_msglen + padlen;
2471 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2472
Gilles Peskine20b44082018-05-29 14:06:49 +02002473 switch( ssl->transform_in->ciphersuite_info->mac )
2474 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002475#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2476 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002477 case MBEDTLS_MD_MD5:
2478 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002479 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002480 /* 8 bytes of message size, 64-byte compression blocks */
2481 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2482 ( 13 + ssl->in_msglen + 8 ) / 64;
2483 break;
2484#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002485#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002486 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002487 /* 16 bytes of message size, 128-byte compression blocks */
2488 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2489 ( 13 + ssl->in_msglen + 16 ) / 128;
2490 break;
2491#endif
2492 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002494 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2495 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002496
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002497 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2500 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2501 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2502 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002503 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002504 /* Make sure we access everything even when padlen > 0. This
2505 * makes the synchronisation requirements for just-in-time
2506 * Prime+Probe attacks much tighter and hopefully impractical. */
2507 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002508 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002509
2510 /* Call mbedtls_md_process at least once due to cache attacks
2511 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002512 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002516
2517 /* Make sure we access all the memory that could contain the MAC,
2518 * before we check it in the next code block. This makes the
2519 * synchronisation requirements for just-in-time Prime+Probe
2520 * attacks much tighter and hopefully impractical. */
2521 ssl_read_memory( ssl->in_msg + min_len,
2522 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002523 }
2524 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2526 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2529 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002530 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002531
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002532#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002533 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2534 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2535 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002536#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002537
Hanno Becker992b6872017-11-09 18:57:39 +00002538 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2539 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541#if defined(MBEDTLS_SSL_DEBUG_ALL)
2542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002543#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002544 correct = 0;
2545 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002546 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002547 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002548
2549 /*
2550 * Finally check the correct flag
2551 */
2552 if( correct == 0 )
2553 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002554#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002555
2556 /* Make extra sure authentication was performed, exactly once */
2557 if( auth_done != 1 )
2558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2560 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002561 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002562
2563 if( ssl->in_msglen == 0 )
2564 {
Angus Gratton34817922018-06-19 15:58:22 +10002565#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2566 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2567 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2568 {
2569 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2571 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2572 }
2573#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2574
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 ssl->nb_zero++;
2576
2577 /*
2578 * Three or more empty messages may be a DoS attack
2579 * (excessive CPU consumption).
2580 */
2581 if( ssl->nb_zero > 3 )
2582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 }
2587 }
2588 else
2589 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002592 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002593 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002594 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002595 }
2596 else
2597#endif
2598 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002599 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002600 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2601 if( ++ssl->in_ctr[i - 1] != 0 )
2602 break;
2603
2604 /* The loop goes to its end iff the counter is wrapping */
2605 if( i == ssl_ep_len( ssl ) )
2606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2608 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002609 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002610 }
2611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002613
2614 return( 0 );
2615}
2616
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002617#undef MAC_NONE
2618#undef MAC_PLAINTEXT
2619#undef MAC_CIPHERTEXT
2620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002622/*
2623 * Compression/decompression functions
2624 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002626{
2627 int ret;
2628 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002629 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002630 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002631 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002634
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002635 if( len_pre == 0 )
2636 return( 0 );
2637
Paul Bakker2770fbd2012-07-03 13:30:23 +00002638 memcpy( msg_pre, ssl->out_msg, len_pre );
2639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002641 ssl->out_msglen ) );
2642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002644 ssl->out_msg, ssl->out_msglen );
2645
Paul Bakker48916f92012-09-16 19:57:18 +00002646 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2647 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2648 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002649 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002650
Paul Bakker48916f92012-09-16 19:57:18 +00002651 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002652 if( ret != Z_OK )
2653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2655 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002656 }
2657
Angus Grattond8213d02016-05-25 20:56:48 +10002658 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002659 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002662 ssl->out_msglen ) );
2663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002665 ssl->out_msg, ssl->out_msglen );
2666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002668
2669 return( 0 );
2670}
2671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002673{
2674 int ret;
2675 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002676 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002677 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002678 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002681
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002682 if( len_pre == 0 )
2683 return( 0 );
2684
Paul Bakker2770fbd2012-07-03 13:30:23 +00002685 memcpy( msg_pre, ssl->in_msg, len_pre );
2686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002688 ssl->in_msglen ) );
2689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002691 ssl->in_msg, ssl->in_msglen );
2692
Paul Bakker48916f92012-09-16 19:57:18 +00002693 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2694 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2695 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002696 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002697 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002698
Paul Bakker48916f92012-09-16 19:57:18 +00002699 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002700 if( ret != Z_OK )
2701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2703 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002704 }
2705
Angus Grattond8213d02016-05-25 20:56:48 +10002706 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002707 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002710 ssl->in_msglen ) );
2711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002713 ssl->in_msg, ssl->in_msglen );
2714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002716
2717 return( 0 );
2718}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2722static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724#if defined(MBEDTLS_SSL_PROTO_DTLS)
2725static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002726{
2727 /* If renegotiation is not enforced, retransmit until we would reach max
2728 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002729 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002730 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002731 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002732 unsigned char doublings = 1;
2733
2734 while( ratio != 0 )
2735 {
2736 ++doublings;
2737 ratio >>= 1;
2738 }
2739
2740 if( ++ssl->renego_records_seen > doublings )
2741 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002743 return( 0 );
2744 }
2745 }
2746
2747 return( ssl_write_hello_request( ssl ) );
2748}
2749#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002751
Paul Bakker5121ce52009-01-03 21:22:43 +00002752/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002753 * Fill the input message buffer by appending data to it.
2754 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002755 *
2756 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2757 * available (from this read and/or a previous one). Otherwise, an error code
2758 * is returned (possibly EOF or WANT_READ).
2759 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002760 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2761 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2762 * since we always read a whole datagram at once.
2763 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002764 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002765 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002766 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002768{
Paul Bakker23986e52011-04-24 08:57:21 +00002769 int ret;
2770 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002773
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002774 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002777 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002779 }
2780
Angus Grattond8213d02016-05-25 20:56:48 +10002781 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2784 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002785 }
2786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002788 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002789 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002790 uint32_t timeout;
2791
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002792 /* Just to be sure */
2793 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2794 {
2795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2796 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2797 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2798 }
2799
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002800 /*
2801 * The point is, we need to always read a full datagram at once, so we
2802 * sometimes read more then requested, and handle the additional data.
2803 * It could be the rest of the current record (while fetching the
2804 * header) and/or some other records in the same datagram.
2805 */
2806
2807 /*
2808 * Move to the next record in the already read datagram if applicable
2809 */
2810 if( ssl->next_record_offset != 0 )
2811 {
2812 if( ssl->in_left < ssl->next_record_offset )
2813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2815 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002816 }
2817
2818 ssl->in_left -= ssl->next_record_offset;
2819
2820 if( ssl->in_left != 0 )
2821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002823 ssl->next_record_offset ) );
2824 memmove( ssl->in_hdr,
2825 ssl->in_hdr + ssl->next_record_offset,
2826 ssl->in_left );
2827 }
2828
2829 ssl->next_record_offset = 0;
2830 }
2831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002833 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002834
2835 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002836 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002837 */
2838 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002841 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002842 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002843
2844 /*
2845 * A record can't be split accross datagrams. If we need to read but
2846 * are not at the beginning of a new record, the caller did something
2847 * wrong.
2848 */
2849 if( ssl->in_left != 0 )
2850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2852 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002853 }
2854
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002855 /*
2856 * Don't even try to read if time's out already.
2857 * This avoids by-passing the timer when repeatedly receiving messages
2858 * that will end up being dropped.
2859 */
2860 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002861 {
2862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002863 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002864 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002865 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002866 {
Angus Grattond8213d02016-05-25 20:56:48 +10002867 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002870 timeout = ssl->handshake->retransmit_timeout;
2871 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002872 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002875
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002876 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002877 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2878 timeout );
2879 else
2880 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002883
2884 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002886 }
2887
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002888 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002891 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002893 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002894 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002895 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002898 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002899 }
2900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002904 return( ret );
2905 }
2906
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002907 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002908 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002910 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002912 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002913 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002916 return( ret );
2917 }
2918
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002919 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002920 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002922 }
2923
Paul Bakker5121ce52009-01-03 21:22:43 +00002924 if( ret < 0 )
2925 return( ret );
2926
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002927 ssl->in_left = ret;
2928 }
2929 else
2930#endif
2931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002933 ssl->in_left, nb_want ) );
2934
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002935 while( ssl->in_left < nb_want )
2936 {
2937 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002938
2939 if( ssl_check_timer( ssl ) != 0 )
2940 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2941 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002942 {
2943 if( ssl->f_recv_timeout != NULL )
2944 {
2945 ret = ssl->f_recv_timeout( ssl->p_bio,
2946 ssl->in_hdr + ssl->in_left, len,
2947 ssl->conf->read_timeout );
2948 }
2949 else
2950 {
2951 ret = ssl->f_recv( ssl->p_bio,
2952 ssl->in_hdr + ssl->in_left, len );
2953 }
2954 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002957 ssl->in_left, nb_want ) );
2958 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002959
2960 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002962
2963 if( ret < 0 )
2964 return( ret );
2965
mohammad160352aecb92018-03-28 23:41:40 -07002966 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002967 {
Darryl Green11999bb2018-03-13 15:22:58 +00002968 MBEDTLS_SSL_DEBUG_MSG( 1,
2969 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002970 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002971 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2972 }
2973
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002974 ssl->in_left += ret;
2975 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002976 }
2977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002979
2980 return( 0 );
2981}
2982
2983/*
2984 * Flush any data not yet written
2985 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002987{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002988 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002989 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002992
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002993 if( ssl->f_send == NULL )
2994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002996 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002998 }
2999
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003000 /* Avoid incrementing counter if data is flushed */
3001 if( ssl->out_left == 0 )
3002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003004 return( 0 );
3005 }
3006
Paul Bakker5121ce52009-01-03 21:22:43 +00003007 while( ssl->out_left > 0 )
3008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3010 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003011
Hanno Becker2b1e3542018-08-06 11:19:13 +01003012 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003013 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003016
3017 if( ret <= 0 )
3018 return( ret );
3019
mohammad160352aecb92018-03-28 23:41:40 -07003020 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003021 {
Darryl Green11999bb2018-03-13 15:22:58 +00003022 MBEDTLS_SSL_DEBUG_MSG( 1,
3023 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003024 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003025 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3026 }
3027
Paul Bakker5121ce52009-01-03 21:22:43 +00003028 ssl->out_left -= ret;
3029 }
3030
Hanno Becker2b1e3542018-08-06 11:19:13 +01003031#if defined(MBEDTLS_SSL_PROTO_DTLS)
3032 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003033 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003034 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003035 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003036 else
3037#endif
3038 {
3039 ssl->out_hdr = ssl->out_buf + 8;
3040 }
3041 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003044
3045 return( 0 );
3046}
3047
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003048/*
3049 * Functions to handle the DTLS retransmission state machine
3050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003052/*
3053 * Append current handshake message to current outgoing flight
3054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3059 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3060 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003061
3062 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003063 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003064 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003067 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003068 }
3069
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003070 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003071 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003073 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003074 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003075 }
3076
3077 /* Copy current handshake message with headers */
3078 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3079 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003080 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003081 msg->next = NULL;
3082
3083 /* Append to the current flight */
3084 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003085 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003086 else
3087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003089 while( cur->next != NULL )
3090 cur = cur->next;
3091 cur->next = msg;
3092 }
3093
Hanno Becker3b235902018-08-06 09:54:53 +01003094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003095 return( 0 );
3096}
3097
3098/*
3099 * Free the current flight of handshake messages
3100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003102{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103 mbedtls_ssl_flight_item *cur = flight;
3104 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003105
3106 while( cur != NULL )
3107 {
3108 next = cur->next;
3109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110 mbedtls_free( cur->p );
3111 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003112
3113 cur = next;
3114 }
3115}
3116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3118static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003119#endif
3120
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003121/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003122 * Swap transform_out and out_ctr with the alternative ones
3123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003125{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003127 unsigned char tmp_out_ctr[8];
3128
3129 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003131 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003132 return;
3133 }
3134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003136
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003137 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003138 tmp_transform = ssl->transform_out;
3139 ssl->transform_out = ssl->handshake->alt_transform_out;
3140 ssl->handshake->alt_transform_out = tmp_transform;
3141
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003142 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003143 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3144 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003145 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003146
3147 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003148 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3151 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3156 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003157 }
3158 }
3159#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003160}
3161
3162/*
3163 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003164 */
3165int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3166{
3167 int ret = 0;
3168
3169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3170
3171 ret = mbedtls_ssl_flight_transmit( ssl );
3172
3173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3174
3175 return( ret );
3176}
3177
3178/*
3179 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003180 *
3181 * Need to remember the current message in case flush_output returns
3182 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003183 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003184 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003185int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003186{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003187 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003191 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003193
3194 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003195 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003196 ssl_swap_epochs( ssl );
3197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003199 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003200
3201 while( ssl->handshake->cur_msg != NULL )
3202 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003203 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003204 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003205
Hanno Beckere1dcb032018-08-17 16:47:58 +01003206 int const is_finished =
3207 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3208 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3209
Hanno Becker04da1892018-08-14 13:22:10 +01003210 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3211 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3212
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003213 /* Swap epochs before sending Finished: we can't do it after
3214 * sending ChangeCipherSpec, in case write returns WANT_READ.
3215 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003216 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003217 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003219 ssl_swap_epochs( ssl );
3220 }
3221
Hanno Becker67bc7c32018-08-06 11:33:50 +01003222 ret = ssl_get_remaining_payload_in_datagram( ssl );
3223 if( ret < 0 )
3224 return( ret );
3225 max_frag_len = (size_t) ret;
3226
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003227 /* CCS is copied as is, while HS messages may need fragmentation */
3228 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3229 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003230 if( max_frag_len == 0 )
3231 {
3232 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3233 return( ret );
3234
3235 continue;
3236 }
3237
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003238 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003239 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003240 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003241
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003242 /* Update position inside current message */
3243 ssl->handshake->cur_msg_p += cur->len;
3244 }
3245 else
3246 {
3247 const unsigned char * const p = ssl->handshake->cur_msg_p;
3248 const size_t hs_len = cur->len - 12;
3249 const size_t frag_off = p - ( cur->p + 12 );
3250 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003251 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003252
Hanno Beckere1dcb032018-08-17 16:47:58 +01003253 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003254 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003255 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003256 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003257
Hanno Becker67bc7c32018-08-06 11:33:50 +01003258 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3259 return( ret );
3260
3261 continue;
3262 }
3263 max_hs_frag_len = max_frag_len - 12;
3264
3265 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3266 max_hs_frag_len : rem_len;
3267
3268 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003269 {
3270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003271 (unsigned) cur_hs_frag_len,
3272 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003273 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003274
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003275 /* Messages are stored with handshake headers as if not fragmented,
3276 * copy beginning of headers then fill fragmentation fields.
3277 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3278 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003279
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003280 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3281 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3282 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3283
Hanno Becker67bc7c32018-08-06 11:33:50 +01003284 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3285 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3286 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003287
3288 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3289
Hanno Becker3f7b9732018-08-28 09:53:25 +01003290 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003291 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3292 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003293 ssl->out_msgtype = cur->type;
3294
3295 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003296 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003297 }
3298
3299 /* If done with the current message move to the next one if any */
3300 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3301 {
3302 if( cur->next != NULL )
3303 {
3304 ssl->handshake->cur_msg = cur->next;
3305 ssl->handshake->cur_msg_p = cur->next->p + 12;
3306 }
3307 else
3308 {
3309 ssl->handshake->cur_msg = NULL;
3310 ssl->handshake->cur_msg_p = NULL;
3311 }
3312 }
3313
3314 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003315 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003318 return( ret );
3319 }
3320 }
3321
Hanno Becker67bc7c32018-08-06 11:33:50 +01003322 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3323 return( ret );
3324
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003325 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3327 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003328 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003330 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003331 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3332 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003333
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003335
3336 return( 0 );
3337}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003338
3339/*
3340 * To be called when the last message of an incoming flight is received.
3341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003343{
3344 /* We won't need to resend that one any more */
3345 ssl_flight_free( ssl->handshake->flight );
3346 ssl->handshake->flight = NULL;
3347 ssl->handshake->cur_msg = NULL;
3348
3349 /* The next incoming flight will start with this msg_seq */
3350 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3351
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003352 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003353 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003354
Hanno Becker0271f962018-08-16 13:23:47 +01003355 /* Clear future message buffering structure. */
3356 ssl_buffering_free( ssl );
3357
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003358 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003359 ssl_set_timer( ssl, 0 );
3360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3362 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003365 }
3366 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003367 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003368}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003369
3370/*
3371 * To be called when the last message of an outgoing flight is send.
3372 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003374{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003375 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003376 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3379 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003382 }
3383 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003384 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003385}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003387
Paul Bakker5121ce52009-01-03 21:22:43 +00003388/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003389 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003390 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003391
3392/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003393 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003394 *
3395 * - fill in handshake headers
3396 * - update handshake checksum
3397 * - DTLS: save message for resending
3398 * - then pass to the record layer
3399 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003400 * DTLS: except for HelloRequest, messages are only queued, and will only be
3401 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003402 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003403 * Inputs:
3404 * - ssl->out_msglen: 4 + actual handshake message len
3405 * (4 is the size of handshake headers for TLS)
3406 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3407 * - ssl->out_msg + 4: the handshake message body
3408 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003409 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003410 * - ssl->out_msglen: the length of the record contents
3411 * (including handshake headers but excluding record headers)
3412 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003413 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003414int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003415{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003416 int ret;
3417 const size_t hs_len = ssl->out_msglen - 4;
3418 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003419
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3421
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003422 /*
3423 * Sanity checks
3424 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003425 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003426 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3427 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003428 /* In SSLv3, the client might send a NoCertificate alert. */
3429#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3430 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3431 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3432 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3433#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3434 {
3435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3436 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3437 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003438 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003439
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003440 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3441 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3442 ssl->handshake == NULL )
3443 {
3444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3445 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3446 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003448#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003449 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003450 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003452 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3454 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003455 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003456#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003457
Hanno Beckerb50a2532018-08-06 11:52:54 +01003458 /* Double-check that we did not exceed the bounds
3459 * of the outgoing record buffer.
3460 * This should never fail as the various message
3461 * writing functions must obey the bounds of the
3462 * outgoing record buffer, but better be safe.
3463 *
3464 * Note: We deliberately do not check for the MTU or MFL here.
3465 */
3466 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3467 {
3468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3469 "size %u, maximum %u",
3470 (unsigned) ssl->out_msglen,
3471 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3472 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3473 }
3474
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003475 /*
3476 * Fill handshake headers
3477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003479 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003480 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3481 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3482 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003483
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003484 /*
3485 * DTLS has additional fields in the Handshake layer,
3486 * between the length field and the actual payload:
3487 * uint16 message_seq;
3488 * uint24 fragment_offset;
3489 * uint24 fragment_length;
3490 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003491#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003492 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003493 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003494 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003495 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003496 {
3497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3498 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003499 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003500 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003501 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3502 }
3503
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003504 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003505 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003506
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003507 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003508 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003509 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003510 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3511 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3512 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003513 }
3514 else
3515 {
3516 ssl->out_msg[4] = 0;
3517 ssl->out_msg[5] = 0;
3518 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003519
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003520 /* Handshake hashes are computed without fragmentation,
3521 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003522 memset( ssl->out_msg + 6, 0x00, 3 );
3523 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003524 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003525#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003526
Hanno Becker0207e532018-08-28 10:28:28 +01003527 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003528 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3529 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003530 }
3531
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003532 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003534 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Becker081bd812018-08-22 16:07:59 +01003535 ( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
3536 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003537 {
3538 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003541 return( ret );
3542 }
3543 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003544 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003545#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003546 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003547 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003548 {
3549 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3550 return( ret );
3551 }
3552 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003553
3554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3555
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003556 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003557}
3558
3559/*
3560 * Record layer functions
3561 */
3562
3563/*
3564 * Write current record.
3565 *
3566 * Uses:
3567 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3568 * - ssl->out_msglen: length of the record content (excl headers)
3569 * - ssl->out_msg: record content
3570 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003571int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003572{
3573 int ret, done = 0;
3574 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003575 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003576
3577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003580 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003581 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003582 {
3583 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003586 return( ret );
3587 }
3588
3589 len = ssl->out_msglen;
3590 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003591#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003593#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3594 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003598 ret = mbedtls_ssl_hw_record_write( ssl );
3599 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3602 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003603 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003604
3605 if( ret == 0 )
3606 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003607 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003609 if( !done )
3610 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003611 unsigned i;
3612 size_t protected_record_size;
3613
Paul Bakker05ef8352012-05-08 09:17:57 +00003614 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003615 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003616 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003617
Hanno Becker19859472018-08-06 09:40:20 +01003618 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003619 ssl->out_len[0] = (unsigned char)( len >> 8 );
3620 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003621
Paul Bakker48916f92012-09-16 19:57:18 +00003622 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003623 {
3624 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003627 return( ret );
3628 }
3629
3630 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003631 ssl->out_len[0] = (unsigned char)( len >> 8 );
3632 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003633 }
3634
Hanno Becker2b1e3542018-08-06 11:19:13 +01003635 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3636
3637#if defined(MBEDTLS_SSL_PROTO_DTLS)
3638 /* In case of DTLS, double-check that we don't exceed
3639 * the remaining space in the datagram. */
3640 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3641 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003642 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003643 if( ret < 0 )
3644 return( ret );
3645
3646 if( protected_record_size > (size_t) ret )
3647 {
3648 /* Should never happen */
3649 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3650 }
3651 }
3652#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003655 "version = [%d:%d], msglen = %d",
3656 ssl->out_hdr[0], ssl->out_hdr[1],
3657 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003660 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003661
3662 ssl->out_left += protected_record_size;
3663 ssl->out_hdr += protected_record_size;
3664 ssl_update_out_pointers( ssl, ssl->transform_out );
3665
Hanno Becker04484622018-08-06 09:49:38 +01003666 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3667 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3668 break;
3669
3670 /* The loop goes to its end iff the counter is wrapping */
3671 if( i == ssl_ep_len( ssl ) )
3672 {
3673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3674 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3675 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003676 }
3677
Hanno Becker67bc7c32018-08-06 11:33:50 +01003678#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003679 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3680 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003681 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003682 size_t remaining;
3683 ret = ssl_get_remaining_payload_in_datagram( ssl );
3684 if( ret < 0 )
3685 {
3686 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3687 ret );
3688 return( ret );
3689 }
3690
3691 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003692 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003693 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003694 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003695 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003696 else
3697 {
Hanno Becker513815a2018-08-20 11:56:09 +01003698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003699 }
3700 }
3701#endif /* MBEDTLS_SSL_PROTO_DTLS */
3702
3703 if( ( flush == SSL_FORCE_FLUSH ) &&
3704 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003707 return( ret );
3708 }
3709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003711
3712 return( 0 );
3713}
3714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003716
3717static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3718{
3719 if( ssl->in_msglen < ssl->in_hslen ||
3720 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3721 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3722 {
3723 return( 1 );
3724 }
3725 return( 0 );
3726}
Hanno Becker44650b72018-08-16 12:51:11 +01003727
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003728static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003729{
3730 return( ( ssl->in_msg[9] << 16 ) |
3731 ( ssl->in_msg[10] << 8 ) |
3732 ssl->in_msg[11] );
3733}
3734
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003735static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003736{
3737 return( ( ssl->in_msg[6] << 16 ) |
3738 ( ssl->in_msg[7] << 8 ) |
3739 ssl->in_msg[8] );
3740}
3741
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003742static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003743{
3744 uint32_t msg_len, frag_off, frag_len;
3745
3746 msg_len = ssl_get_hs_total_len( ssl );
3747 frag_off = ssl_get_hs_frag_off( ssl );
3748 frag_len = ssl_get_hs_frag_len( ssl );
3749
3750 if( frag_off > msg_len )
3751 return( -1 );
3752
3753 if( frag_len > msg_len - frag_off )
3754 return( -1 );
3755
3756 if( frag_len + 12 > ssl->in_msglen )
3757 return( -1 );
3758
3759 return( 0 );
3760}
3761
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003762/*
3763 * Mark bits in bitmask (used for DTLS HS reassembly)
3764 */
3765static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3766{
3767 unsigned int start_bits, end_bits;
3768
3769 start_bits = 8 - ( offset % 8 );
3770 if( start_bits != 8 )
3771 {
3772 size_t first_byte_idx = offset / 8;
3773
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003774 /* Special case */
3775 if( len <= start_bits )
3776 {
3777 for( ; len != 0; len-- )
3778 mask[first_byte_idx] |= 1 << ( start_bits - len );
3779
3780 /* Avoid potential issues with offset or len becoming invalid */
3781 return;
3782 }
3783
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003784 offset += start_bits; /* Now offset % 8 == 0 */
3785 len -= start_bits;
3786
3787 for( ; start_bits != 0; start_bits-- )
3788 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3789 }
3790
3791 end_bits = len % 8;
3792 if( end_bits != 0 )
3793 {
3794 size_t last_byte_idx = ( offset + len ) / 8;
3795
3796 len -= end_bits; /* Now len % 8 == 0 */
3797
3798 for( ; end_bits != 0; end_bits-- )
3799 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3800 }
3801
3802 memset( mask + offset / 8, 0xFF, len / 8 );
3803}
3804
3805/*
3806 * Check that bitmask is full
3807 */
3808static int ssl_bitmask_check( unsigned char *mask, size_t len )
3809{
3810 size_t i;
3811
3812 for( i = 0; i < len / 8; i++ )
3813 if( mask[i] != 0xFF )
3814 return( -1 );
3815
3816 for( i = 0; i < len % 8; i++ )
3817 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3818 return( -1 );
3819
3820 return( 0 );
3821}
3822
Hanno Becker56e205e2018-08-16 09:06:12 +01003823/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003824static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003825 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003826{
Hanno Becker56e205e2018-08-16 09:06:12 +01003827 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003828
Hanno Becker56e205e2018-08-16 09:06:12 +01003829 alloc_len = 12; /* Handshake header */
3830 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003831
Hanno Beckerd07df862018-08-16 09:14:58 +01003832 if( add_bitmap )
3833 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003834
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003835 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003836}
Hanno Becker56e205e2018-08-16 09:06:12 +01003837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003838#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003839
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003840static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003841{
3842 return( ( ssl->in_msg[1] << 16 ) |
3843 ( ssl->in_msg[2] << 8 ) |
3844 ssl->in_msg[3] );
3845}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003846
Simon Butcher99000142016-10-13 17:21:01 +01003847int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003848{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003852 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003853 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003854 }
3855
Hanno Becker12555c62018-08-16 12:47:53 +01003856 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003859 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003860 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003862#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003863 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003864 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003865 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003866 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003867
Hanno Becker44650b72018-08-16 12:51:11 +01003868 if( ssl_check_hs_header( ssl ) != 0 )
3869 {
3870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3871 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3872 }
3873
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003874 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003875 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3876 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3877 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3878 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003879 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003880 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3881 {
3882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3883 recv_msg_seq,
3884 ssl->handshake->in_msg_seq ) );
3885 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3886 }
3887
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003888 /* Retransmit only on last message from previous flight, to avoid
3889 * too many retransmissions.
3890 * Besides, No sane server ever retransmits HelloVerifyRequest */
3891 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003892 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003895 "message_seq = %d, start_of_flight = %d",
3896 recv_msg_seq,
3897 ssl->handshake->in_flight_start_seq ) );
3898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003899 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003902 return( ret );
3903 }
3904 }
3905 else
3906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003908 "message_seq = %d, expected = %d",
3909 recv_msg_seq,
3910 ssl->handshake->in_msg_seq ) );
3911 }
3912
Hanno Becker90333da2017-10-10 11:27:13 +01003913 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003914 }
3915 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003916
Hanno Becker6d97ef52018-08-16 13:09:04 +01003917 /* Message reassembly is handled alongside buffering of future
3918 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003919 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003920 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003921 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003924 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003925 }
3926 }
3927 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003928#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003929 /* With TLS we don't handle fragmentation (for now) */
3930 if( ssl->in_msglen < ssl->in_hslen )
3931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3933 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003934 }
3935
Simon Butcher99000142016-10-13 17:21:01 +01003936 return( 0 );
3937}
3938
3939void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3940{
Hanno Becker0271f962018-08-16 13:23:47 +01003941 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003942
Hanno Becker0271f962018-08-16 13:23:47 +01003943 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003944 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003945 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003946 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003947
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003948 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003950 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003951 ssl->handshake != NULL )
3952 {
Hanno Becker0271f962018-08-16 13:23:47 +01003953 unsigned offset;
3954 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003955
Hanno Becker0271f962018-08-16 13:23:47 +01003956 /* Increment handshake sequence number */
3957 hs->in_msg_seq++;
3958
3959 /*
3960 * Clear up handshake buffering and reassembly structure.
3961 */
3962
3963 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003964 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003965
3966 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003967 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3968 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003969 offset++, hs_buf++ )
3970 {
3971 *hs_buf = *(hs_buf + 1);
3972 }
3973
3974 /* Create a fresh last entry */
3975 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003976 }
3977#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003978}
3979
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003980/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003981 * DTLS anti-replay: RFC 6347 4.1.2.6
3982 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003983 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3984 * Bit n is set iff record number in_window_top - n has been seen.
3985 *
3986 * Usually, in_window_top is the last record number seen and the lsb of
3987 * in_window is set. The only exception is the initial state (record number 0
3988 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3991static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003992{
3993 ssl->in_window_top = 0;
3994 ssl->in_window = 0;
3995}
3996
3997static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3998{
3999 return( ( (uint64_t) buf[0] << 40 ) |
4000 ( (uint64_t) buf[1] << 32 ) |
4001 ( (uint64_t) buf[2] << 24 ) |
4002 ( (uint64_t) buf[3] << 16 ) |
4003 ( (uint64_t) buf[4] << 8 ) |
4004 ( (uint64_t) buf[5] ) );
4005}
4006
4007/*
4008 * Return 0 if sequence number is acceptable, -1 otherwise
4009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004010int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004011{
4012 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4013 uint64_t bit;
4014
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004015 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004016 return( 0 );
4017
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004018 if( rec_seqnum > ssl->in_window_top )
4019 return( 0 );
4020
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004021 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004022
4023 if( bit >= 64 )
4024 return( -1 );
4025
4026 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4027 return( -1 );
4028
4029 return( 0 );
4030}
4031
4032/*
4033 * Update replay window on new validated record
4034 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004035void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004036{
4037 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4038
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004039 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004040 return;
4041
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004042 if( rec_seqnum > ssl->in_window_top )
4043 {
4044 /* Update window_top and the contents of the window */
4045 uint64_t shift = rec_seqnum - ssl->in_window_top;
4046
4047 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004048 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004049 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004050 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004051 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004052 ssl->in_window |= 1;
4053 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004054
4055 ssl->in_window_top = rec_seqnum;
4056 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004057 else
4058 {
4059 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004060 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004061
4062 if( bit < 64 ) /* Always true, but be extra sure */
4063 ssl->in_window |= (uint64_t) 1 << bit;
4064 }
4065}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004066#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004067
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004068#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004069/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004070static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4071
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004072/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004073 * Without any SSL context, check if a datagram looks like a ClientHello with
4074 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004075 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004076 *
4077 * - if cookie is valid, return 0
4078 * - if ClientHello looks superficially valid but cookie is not,
4079 * fill obuf and set olen, then
4080 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4081 * - otherwise return a specific error code
4082 */
4083static int ssl_check_dtls_clihlo_cookie(
4084 mbedtls_ssl_cookie_write_t *f_cookie_write,
4085 mbedtls_ssl_cookie_check_t *f_cookie_check,
4086 void *p_cookie,
4087 const unsigned char *cli_id, size_t cli_id_len,
4088 const unsigned char *in, size_t in_len,
4089 unsigned char *obuf, size_t buf_len, size_t *olen )
4090{
4091 size_t sid_len, cookie_len;
4092 unsigned char *p;
4093
4094 if( f_cookie_write == NULL || f_cookie_check == NULL )
4095 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4096
4097 /*
4098 * Structure of ClientHello with record and handshake headers,
4099 * and expected values. We don't need to check a lot, more checks will be
4100 * done when actually parsing the ClientHello - skipping those checks
4101 * avoids code duplication and does not make cookie forging any easier.
4102 *
4103 * 0-0 ContentType type; copied, must be handshake
4104 * 1-2 ProtocolVersion version; copied
4105 * 3-4 uint16 epoch; copied, must be 0
4106 * 5-10 uint48 sequence_number; copied
4107 * 11-12 uint16 length; (ignored)
4108 *
4109 * 13-13 HandshakeType msg_type; (ignored)
4110 * 14-16 uint24 length; (ignored)
4111 * 17-18 uint16 message_seq; copied
4112 * 19-21 uint24 fragment_offset; copied, must be 0
4113 * 22-24 uint24 fragment_length; (ignored)
4114 *
4115 * 25-26 ProtocolVersion client_version; (ignored)
4116 * 27-58 Random random; (ignored)
4117 * 59-xx SessionID session_id; 1 byte len + sid_len content
4118 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4119 * ...
4120 *
4121 * Minimum length is 61 bytes.
4122 */
4123 if( in_len < 61 ||
4124 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4125 in[3] != 0 || in[4] != 0 ||
4126 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4127 {
4128 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4129 }
4130
4131 sid_len = in[59];
4132 if( sid_len > in_len - 61 )
4133 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4134
4135 cookie_len = in[60 + sid_len];
4136 if( cookie_len > in_len - 60 )
4137 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4138
4139 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4140 cli_id, cli_id_len ) == 0 )
4141 {
4142 /* Valid cookie */
4143 return( 0 );
4144 }
4145
4146 /*
4147 * If we get here, we've got an invalid cookie, let's prepare HVR.
4148 *
4149 * 0-0 ContentType type; copied
4150 * 1-2 ProtocolVersion version; copied
4151 * 3-4 uint16 epoch; copied
4152 * 5-10 uint48 sequence_number; copied
4153 * 11-12 uint16 length; olen - 13
4154 *
4155 * 13-13 HandshakeType msg_type; hello_verify_request
4156 * 14-16 uint24 length; olen - 25
4157 * 17-18 uint16 message_seq; copied
4158 * 19-21 uint24 fragment_offset; copied
4159 * 22-24 uint24 fragment_length; olen - 25
4160 *
4161 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4162 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4163 *
4164 * Minimum length is 28.
4165 */
4166 if( buf_len < 28 )
4167 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4168
4169 /* Copy most fields and adapt others */
4170 memcpy( obuf, in, 25 );
4171 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4172 obuf[25] = 0xfe;
4173 obuf[26] = 0xff;
4174
4175 /* Generate and write actual cookie */
4176 p = obuf + 28;
4177 if( f_cookie_write( p_cookie,
4178 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4179 {
4180 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4181 }
4182
4183 *olen = p - obuf;
4184
4185 /* Go back and fill length fields */
4186 obuf[27] = (unsigned char)( *olen - 28 );
4187
4188 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4189 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4190 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4191
4192 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4193 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4194
4195 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4196}
4197
4198/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004199 * Handle possible client reconnect with the same UDP quadruplet
4200 * (RFC 6347 Section 4.2.8).
4201 *
4202 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4203 * that looks like a ClientHello.
4204 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004205 * - if the input looks like a ClientHello without cookies,
4206 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004207 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004208 * - if the input looks like a ClientHello with a valid cookie,
4209 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004210 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004211 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004212 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004213 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004214 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4215 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004216 */
4217static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4218{
4219 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004220 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004221
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004222 ret = ssl_check_dtls_clihlo_cookie(
4223 ssl->conf->f_cookie_write,
4224 ssl->conf->f_cookie_check,
4225 ssl->conf->p_cookie,
4226 ssl->cli_id, ssl->cli_id_len,
4227 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004228 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004229
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004230 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4231
4232 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004233 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004234 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004235 * If the error is permanent we'll catch it later,
4236 * if it's not, then hopefully it'll work next time. */
4237 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4238
4239 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004240 }
4241
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004242 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004243 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004244 /* Got a valid cookie, partially reset context */
4245 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4246 {
4247 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4248 return( ret );
4249 }
4250
4251 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004252 }
4253
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004254 return( ret );
4255}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004256#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004257
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004258/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004259 * ContentType type;
4260 * ProtocolVersion version;
4261 * uint16 epoch; // DTLS only
4262 * uint48 sequence_number; // DTLS only
4263 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004264 *
4265 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004266 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004267 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4268 *
4269 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004270 * 1. proceed with the record if this function returns 0
4271 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4272 * 3. return CLIENT_RECONNECT if this function return that value
4273 * 4. drop the whole datagram if this function returns anything else.
4274 * Point 2 is needed when the peer is resending, and we have already received
4275 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004276 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004277static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004278{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004279 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004281 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 +02004282
Paul Bakker5121ce52009-01-03 21:22:43 +00004283 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004284 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004285 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004287 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004288 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004289 ssl->in_msgtype,
4290 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004291
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004292 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004293 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4294 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4295 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4296 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004299
4300#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004301 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4302 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004303 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4304#endif /* MBEDTLS_SSL_PROTO_DTLS */
4305 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4306 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004308 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004309 }
4310
4311 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004312 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4315 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004316 }
4317
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004318 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4321 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004322 }
4323
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004324 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004325 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004326 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4329 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004330 }
4331
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004332 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004333 * DTLS-related tests.
4334 * Check epoch before checking length constraint because
4335 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4336 * message gets duplicated before the corresponding Finished message,
4337 * the second ChangeCipherSpec should be discarded because it belongs
4338 * to an old epoch, but not because its length is shorter than
4339 * the minimum record length for packets using the new record transform.
4340 * Note that these two kinds of failures are handled differently,
4341 * as an unexpected record is silently skipped but an invalid
4342 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004343 */
4344#if defined(MBEDTLS_SSL_PROTO_DTLS)
4345 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4346 {
4347 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4348
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004349 /* Check epoch (and sequence number) with DTLS */
4350 if( rec_epoch != ssl->in_epoch )
4351 {
4352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4353 "expected %d, received %d",
4354 ssl->in_epoch, rec_epoch ) );
4355
4356#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4357 /*
4358 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4359 * access the first byte of record content (handshake type), as we
4360 * have an active transform (possibly iv_len != 0), so use the
4361 * fact that the record header len is 13 instead.
4362 */
4363 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4364 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4365 rec_epoch == 0 &&
4366 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4367 ssl->in_left > 13 &&
4368 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4369 {
4370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4371 "from the same port" ) );
4372 return( ssl_handle_possible_reconnect( ssl ) );
4373 }
4374 else
4375#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004376 {
4377 /* Consider buffering the record. */
4378 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4379 {
4380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4381 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4382 }
4383
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004384 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004385 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004386 }
4387
4388#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4389 /* Replay detection only works for the current epoch */
4390 if( rec_epoch == ssl->in_epoch &&
4391 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4392 {
4393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4394 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4395 }
4396#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004397
Hanno Becker52c6dc62017-05-26 16:07:36 +01004398 /* Drop unexpected ApplicationData records,
4399 * except at the beginning of renegotiations */
4400 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4401 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4402#if defined(MBEDTLS_SSL_RENEGOTIATION)
4403 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4404 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4405#endif
4406 )
4407 {
4408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4409 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4410 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004411 }
4412#endif /* MBEDTLS_SSL_PROTO_DTLS */
4413
Hanno Becker52c6dc62017-05-26 16:07:36 +01004414
4415 /* Check length against bounds of the current transform and version */
4416 if( ssl->transform_in == NULL )
4417 {
4418 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004419 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004420 {
4421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4422 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4423 }
4424 }
4425 else
4426 {
4427 if( ssl->in_msglen < ssl->transform_in->minlen )
4428 {
4429 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4430 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4431 }
4432
4433#if defined(MBEDTLS_SSL_PROTO_SSL3)
4434 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004435 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004436 {
4437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4438 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4439 }
4440#endif
4441#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4442 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4443 /*
4444 * TLS encrypted messages can have up to 256 bytes of padding
4445 */
4446 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4447 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004448 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004449 {
4450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4451 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4452 }
4453#endif
4454 }
4455
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004456 return( 0 );
4457}
Paul Bakker5121ce52009-01-03 21:22:43 +00004458
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004459/*
4460 * If applicable, decrypt (and decompress) record content
4461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004462static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004463{
4464 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004466 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4467 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004469#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4470 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004474 ret = mbedtls_ssl_hw_record_read( ssl );
4475 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004477 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4478 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004479 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004480
4481 if( ret == 0 )
4482 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004483 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004484#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004485 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004486 {
4487 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004489 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004490 return( ret );
4491 }
4492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004493 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004494 ssl->in_msg, ssl->in_msglen );
4495
Angus Grattond8213d02016-05-25 20:56:48 +10004496 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4499 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004500 }
4501 }
4502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004503#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004504 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004505 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004506 {
4507 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004509 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004510 return( ret );
4511 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004512 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004513#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004515#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004516 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004518 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004519 }
4520#endif
4521
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004522 return( 0 );
4523}
4524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004525static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004526
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004527/*
4528 * Read a record.
4529 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004530 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4531 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4532 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004533 */
Hanno Becker1097b342018-08-15 14:09:41 +01004534
4535/* Helper functions for mbedtls_ssl_read_record(). */
4536static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004537static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4538static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004539
Hanno Becker327c93b2018-08-15 13:56:18 +01004540int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004541 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004542{
4543 int ret;
4544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004546
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004547 if( ssl->keep_current_message == 0 )
4548 {
4549 do {
Simon Butcher99000142016-10-13 17:21:01 +01004550
Hanno Becker26994592018-08-15 14:14:59 +01004551 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004552 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004553 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004554
Hanno Beckere74d5562018-08-15 14:26:08 +01004555 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004556 {
Hanno Becker40f50842018-08-15 14:48:01 +01004557#if defined(MBEDTLS_SSL_PROTO_DTLS)
4558 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004559
Hanno Becker40f50842018-08-15 14:48:01 +01004560 /* We only check for buffered messages if the
4561 * current datagram is fully consumed. */
4562 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004563 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004564 {
Hanno Becker40f50842018-08-15 14:48:01 +01004565 if( ssl_load_buffered_message( ssl ) == 0 )
4566 have_buffered = 1;
4567 }
4568
4569 if( have_buffered == 0 )
4570#endif /* MBEDTLS_SSL_PROTO_DTLS */
4571 {
4572 ret = ssl_get_next_record( ssl );
4573 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4574 continue;
4575
4576 if( ret != 0 )
4577 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004578 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004579 return( ret );
4580 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004581 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004582 }
4583
4584 ret = mbedtls_ssl_handle_message_type( ssl );
4585
Hanno Becker40f50842018-08-15 14:48:01 +01004586#if defined(MBEDTLS_SSL_PROTO_DTLS)
4587 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4588 {
4589 /* Buffer future message */
4590 ret = ssl_buffer_message( ssl );
4591 if( ret != 0 )
4592 return( ret );
4593
4594 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4595 }
4596#endif /* MBEDTLS_SSL_PROTO_DTLS */
4597
Hanno Becker90333da2017-10-10 11:27:13 +01004598 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4599 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004600
4601 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004602 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004603 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004604 return( ret );
4605 }
4606
Hanno Becker327c93b2018-08-15 13:56:18 +01004607 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004608 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004609 {
4610 mbedtls_ssl_update_handshake_status( ssl );
4611 }
Simon Butcher99000142016-10-13 17:21:01 +01004612 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004613 else
Simon Butcher99000142016-10-13 17:21:01 +01004614 {
Hanno Becker02f59072018-08-15 14:00:24 +01004615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004616 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004617 }
4618
4619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4620
4621 return( 0 );
4622}
4623
Hanno Becker40f50842018-08-15 14:48:01 +01004624#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004625static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004626{
Hanno Becker40f50842018-08-15 14:48:01 +01004627 if( ssl->in_left > ssl->next_record_offset )
4628 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004629
Hanno Becker40f50842018-08-15 14:48:01 +01004630 return( 0 );
4631}
4632
4633static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4634{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004635 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004636 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004637 int ret = 0;
4638
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004639 if( hs == NULL )
4640 return( -1 );
4641
Hanno Beckere00ae372018-08-20 09:39:42 +01004642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4643
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004644 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4645 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4646 {
4647 /* Check if we have seen a ChangeCipherSpec before.
4648 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004649 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004650 {
4651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4652 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004653 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004654 }
4655
Hanno Becker39b8bc92018-08-28 17:17:13 +01004656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004657 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4658 ssl->in_msglen = 1;
4659 ssl->in_msg[0] = 1;
4660
4661 /* As long as they are equal, the exact value doesn't matter. */
4662 ssl->in_left = 0;
4663 ssl->next_record_offset = 0;
4664
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004665 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004666 goto exit;
4667 }
Hanno Becker37f95322018-08-16 13:55:32 +01004668
Hanno Beckerb8f50142018-08-28 10:01:34 +01004669#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004670 /* Debug only */
4671 {
4672 unsigned offset;
4673 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4674 {
4675 hs_buf = &hs->buffering.hs[offset];
4676 if( hs_buf->is_valid == 1 )
4677 {
4678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4679 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004680 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004681 }
4682 }
4683 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004684#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004685
4686 /* Check if we have buffered and/or fully reassembled the
4687 * next handshake message. */
4688 hs_buf = &hs->buffering.hs[0];
4689 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4690 {
4691 /* Synthesize a record containing the buffered HS message. */
4692 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4693 ( hs_buf->data[2] << 8 ) |
4694 hs_buf->data[3];
4695
4696 /* Double-check that we haven't accidentally buffered
4697 * a message that doesn't fit into the input buffer. */
4698 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4699 {
4700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4701 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4702 }
4703
4704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4705 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4706 hs_buf->data, msg_len + 12 );
4707
4708 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4709 ssl->in_hslen = msg_len + 12;
4710 ssl->in_msglen = msg_len + 12;
4711 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4712
4713 ret = 0;
4714 goto exit;
4715 }
4716 else
4717 {
4718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4719 hs->in_msg_seq ) );
4720 }
4721
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004722 ret = -1;
4723
4724exit:
4725
4726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4727 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004728}
4729
Hanno Beckera02b0b42018-08-21 17:20:27 +01004730static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4731 size_t desired )
4732{
4733 int offset;
4734 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4736 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004737
Hanno Becker01315ea2018-08-21 17:22:17 +01004738 /* Get rid of future records epoch first, if such exist. */
4739 ssl_free_buffered_record( ssl );
4740
4741 /* Check if we have enough space available now. */
4742 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4743 hs->buffering.total_bytes_buffered ) )
4744 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004746 return( 0 );
4747 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004748
Hanno Becker4f432ad2018-08-28 10:02:32 +01004749 /* We don't have enough space to buffer the next expected handshake
4750 * message. Remove buffers used for future messages to gain space,
4751 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004752 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4753 offset >= 0; offset-- )
4754 {
4755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4756 offset ) );
4757
Hanno Beckerb309b922018-08-23 13:18:05 +01004758 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004759
4760 /* Check if we have enough space available now. */
4761 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4762 hs->buffering.total_bytes_buffered ) )
4763 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004765 return( 0 );
4766 }
4767 }
4768
4769 return( -1 );
4770}
4771
Hanno Becker40f50842018-08-15 14:48:01 +01004772static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4773{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004774 int ret = 0;
4775 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4776
4777 if( hs == NULL )
4778 return( 0 );
4779
4780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4781
4782 switch( ssl->in_msgtype )
4783 {
4784 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004786
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004787 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004788 break;
4789
4790 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004791 {
4792 unsigned recv_msg_seq_offset;
4793 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4794 mbedtls_ssl_hs_buffer *hs_buf;
4795 size_t msg_len = ssl->in_hslen - 12;
4796
4797 /* We should never receive an old handshake
4798 * message - double-check nonetheless. */
4799 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4800 {
4801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4802 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4803 }
4804
4805 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4806 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4807 {
4808 /* Silently ignore -- message too far in the future */
4809 MBEDTLS_SSL_DEBUG_MSG( 2,
4810 ( "Ignore future HS message with sequence number %u, "
4811 "buffering window %u - %u",
4812 recv_msg_seq, ssl->handshake->in_msg_seq,
4813 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4814
4815 goto exit;
4816 }
4817
4818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4819 recv_msg_seq, recv_msg_seq_offset ) );
4820
4821 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4822
4823 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004824 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004825 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004826 size_t reassembly_buf_sz;
4827
Hanno Becker37f95322018-08-16 13:55:32 +01004828 hs_buf->is_fragmented =
4829 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4830
4831 /* We copy the message back into the input buffer
4832 * after reassembly, so check that it's not too large.
4833 * This is an implementation-specific limitation
4834 * and not one from the standard, hence it is not
4835 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004836 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004837 {
4838 /* Ignore message */
4839 goto exit;
4840 }
4841
Hanno Beckere0b150f2018-08-21 15:51:03 +01004842 /* Check if we have enough space to buffer the message. */
4843 if( hs->buffering.total_bytes_buffered >
4844 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4845 {
4846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4847 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4848 }
4849
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004850 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4851 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004852
4853 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4854 hs->buffering.total_bytes_buffered ) )
4855 {
4856 if( recv_msg_seq_offset > 0 )
4857 {
4858 /* If we can't buffer a future message because
4859 * of space limitations -- ignore. */
4860 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",
4861 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4862 (unsigned) hs->buffering.total_bytes_buffered ) );
4863 goto exit;
4864 }
Hanno Beckere1801392018-08-21 16:51:05 +01004865 else
4866 {
4867 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",
4868 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4869 (unsigned) hs->buffering.total_bytes_buffered ) );
4870 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004871
Hanno Beckera02b0b42018-08-21 17:20:27 +01004872 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004873 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004874 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",
4875 (unsigned) msg_len,
4876 (unsigned) reassembly_buf_sz,
4877 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004878 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004879 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4880 goto exit;
4881 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004882 }
4883
4884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4885 msg_len ) );
4886
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004887 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4888 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004889 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004890 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004891 goto exit;
4892 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004893 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004894
4895 /* Prepare final header: copy msg_type, length and message_seq,
4896 * then add standardised fragment_offset and fragment_length */
4897 memcpy( hs_buf->data, ssl->in_msg, 6 );
4898 memset( hs_buf->data + 6, 0, 3 );
4899 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4900
4901 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004902
4903 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004904 }
4905 else
4906 {
4907 /* Make sure msg_type and length are consistent */
4908 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4909 {
4910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4911 /* Ignore */
4912 goto exit;
4913 }
4914 }
4915
Hanno Becker4422bbb2018-08-20 09:40:19 +01004916 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004917 {
4918 size_t frag_len, frag_off;
4919 unsigned char * const msg = hs_buf->data + 12;
4920
4921 /*
4922 * Check and copy current fragment
4923 */
4924
4925 /* Validation of header fields already done in
4926 * mbedtls_ssl_prepare_handshake_record(). */
4927 frag_off = ssl_get_hs_frag_off( ssl );
4928 frag_len = ssl_get_hs_frag_len( ssl );
4929
4930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4931 frag_off, frag_len ) );
4932 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4933
4934 if( hs_buf->is_fragmented )
4935 {
4936 unsigned char * const bitmask = msg + msg_len;
4937 ssl_bitmask_set( bitmask, frag_off, frag_len );
4938 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4939 msg_len ) == 0 );
4940 }
4941 else
4942 {
4943 hs_buf->is_complete = 1;
4944 }
4945
4946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4947 hs_buf->is_complete ? "" : "not yet " ) );
4948 }
4949
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004950 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004951 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004952
4953 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004954 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004955 break;
4956 }
4957
4958exit:
4959
4960 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4961 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004962}
4963#endif /* MBEDTLS_SSL_PROTO_DTLS */
4964
Hanno Becker1097b342018-08-15 14:09:41 +01004965static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004966{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004967 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004968 * Consume last content-layer message and potentially
4969 * update in_msglen which keeps track of the contents'
4970 * consumption state.
4971 *
4972 * (1) Handshake messages:
4973 * Remove last handshake message, move content
4974 * and adapt in_msglen.
4975 *
4976 * (2) Alert messages:
4977 * Consume whole record content, in_msglen = 0.
4978 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004979 * (3) Change cipher spec:
4980 * Consume whole record content, in_msglen = 0.
4981 *
4982 * (4) Application data:
4983 * Don't do anything - the record layer provides
4984 * the application data as a stream transport
4985 * and consumes through mbedtls_ssl_read only.
4986 *
4987 */
4988
4989 /* Case (1): Handshake messages */
4990 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004991 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004992 /* Hard assertion to be sure that no application data
4993 * is in flight, as corrupting ssl->in_msglen during
4994 * ssl->in_offt != NULL is fatal. */
4995 if( ssl->in_offt != NULL )
4996 {
4997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4998 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4999 }
5000
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005001 /*
5002 * Get next Handshake message in the current record
5003 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005004
Hanno Becker4a810fb2017-05-24 16:27:30 +01005005 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005006 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005007 * current handshake content: If DTLS handshake
5008 * fragmentation is used, that's the fragment
5009 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005010 * size here is faulty and should be changed at
5011 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005012 * (2) While it doesn't seem to cause problems, one
5013 * has to be very careful not to assume that in_hslen
5014 * is always <= in_msglen in a sensible communication.
5015 * Again, it's wrong for DTLS handshake fragmentation.
5016 * The following check is therefore mandatory, and
5017 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005018 * Additionally, ssl->in_hslen might be arbitrarily out of
5019 * bounds after handling a DTLS message with an unexpected
5020 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005021 */
5022 if( ssl->in_hslen < ssl->in_msglen )
5023 {
5024 ssl->in_msglen -= ssl->in_hslen;
5025 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5026 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005027
Hanno Becker4a810fb2017-05-24 16:27:30 +01005028 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5029 ssl->in_msg, ssl->in_msglen );
5030 }
5031 else
5032 {
5033 ssl->in_msglen = 0;
5034 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005035
Hanno Becker4a810fb2017-05-24 16:27:30 +01005036 ssl->in_hslen = 0;
5037 }
5038 /* Case (4): Application data */
5039 else if( ssl->in_offt != NULL )
5040 {
5041 return( 0 );
5042 }
5043 /* Everything else (CCS & Alerts) */
5044 else
5045 {
5046 ssl->in_msglen = 0;
5047 }
5048
Hanno Becker1097b342018-08-15 14:09:41 +01005049 return( 0 );
5050}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005051
Hanno Beckere74d5562018-08-15 14:26:08 +01005052static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5053{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005054 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005055 return( 1 );
5056
5057 return( 0 );
5058}
5059
Hanno Becker5f066e72018-08-16 14:56:31 +01005060#if defined(MBEDTLS_SSL_PROTO_DTLS)
5061
5062static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5063{
5064 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5065 if( hs == NULL )
5066 return;
5067
Hanno Becker01315ea2018-08-21 17:22:17 +01005068 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005069 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005070 hs->buffering.total_bytes_buffered -=
5071 hs->buffering.future_record.len;
5072
5073 mbedtls_free( hs->buffering.future_record.data );
5074 hs->buffering.future_record.data = NULL;
5075 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005076}
5077
5078static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5079{
5080 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5081 unsigned char * rec;
5082 size_t rec_len;
5083 unsigned rec_epoch;
5084
5085 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5086 return( 0 );
5087
5088 if( hs == NULL )
5089 return( 0 );
5090
Hanno Becker5f066e72018-08-16 14:56:31 +01005091 rec = hs->buffering.future_record.data;
5092 rec_len = hs->buffering.future_record.len;
5093 rec_epoch = hs->buffering.future_record.epoch;
5094
5095 if( rec == NULL )
5096 return( 0 );
5097
Hanno Becker4cb782d2018-08-20 11:19:05 +01005098 /* Only consider loading future records if the
5099 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005100 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005101 return( 0 );
5102
Hanno Becker5f066e72018-08-16 14:56:31 +01005103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5104
5105 if( rec_epoch != ssl->in_epoch )
5106 {
5107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5108 goto exit;
5109 }
5110
5111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5112
5113 /* Double-check that the record is not too large */
5114 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5115 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5116 {
5117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5118 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5119 }
5120
5121 memcpy( ssl->in_hdr, rec, rec_len );
5122 ssl->in_left = rec_len;
5123 ssl->next_record_offset = 0;
5124
5125 ssl_free_buffered_record( ssl );
5126
5127exit:
5128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5129 return( 0 );
5130}
5131
5132static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5133{
5134 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5135 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005136 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005137
5138 /* Don't buffer future records outside handshakes. */
5139 if( hs == NULL )
5140 return( 0 );
5141
5142 /* Only buffer handshake records (we are only interested
5143 * in Finished messages). */
5144 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5145 return( 0 );
5146
5147 /* Don't buffer more than one future epoch record. */
5148 if( hs->buffering.future_record.data != NULL )
5149 return( 0 );
5150
Hanno Becker01315ea2018-08-21 17:22:17 +01005151 /* Don't buffer record if there's not enough buffering space remaining. */
5152 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5153 hs->buffering.total_bytes_buffered ) )
5154 {
5155 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",
5156 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5157 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005158 return( 0 );
5159 }
5160
Hanno Becker5f066e72018-08-16 14:56:31 +01005161 /* Buffer record */
5162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5163 ssl->in_epoch + 1 ) );
5164 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5165 rec_hdr_len + ssl->in_msglen );
5166
5167 /* ssl_parse_record_header() only considers records
5168 * of the next epoch as candidates for buffering. */
5169 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005170 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005171
5172 hs->buffering.future_record.data =
5173 mbedtls_calloc( 1, hs->buffering.future_record.len );
5174 if( hs->buffering.future_record.data == NULL )
5175 {
5176 /* If we run out of RAM trying to buffer a
5177 * record from the next epoch, just ignore. */
5178 return( 0 );
5179 }
5180
Hanno Becker01315ea2018-08-21 17:22:17 +01005181 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005182
Hanno Becker01315ea2018-08-21 17:22:17 +01005183 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005184 return( 0 );
5185}
5186
5187#endif /* MBEDTLS_SSL_PROTO_DTLS */
5188
Hanno Beckere74d5562018-08-15 14:26:08 +01005189static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005190{
5191 int ret;
5192
Hanno Becker5f066e72018-08-16 14:56:31 +01005193#if defined(MBEDTLS_SSL_PROTO_DTLS)
5194 /* We might have buffered a future record; if so,
5195 * and if the epoch matches now, load it.
5196 * On success, this call will set ssl->in_left to
5197 * the length of the buffered record, so that
5198 * the calls to ssl_fetch_input() below will
5199 * essentially be no-ops. */
5200 ret = ssl_load_buffered_record( ssl );
5201 if( ret != 0 )
5202 return( ret );
5203#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005205 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005207 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005208 return( ret );
5209 }
5210
5211 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005213#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005214 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5215 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005216 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005217 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5218 {
5219 ret = ssl_buffer_future_record( ssl );
5220 if( ret != 0 )
5221 return( ret );
5222
5223 /* Fall through to handling of unexpected records */
5224 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5225 }
5226
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005227 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5228 {
5229 /* Skip unexpected record (but not whole datagram) */
5230 ssl->next_record_offset = ssl->in_msglen
5231 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005232
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5234 "(header)" ) );
5235 }
5236 else
5237 {
5238 /* Skip invalid record and the rest of the datagram */
5239 ssl->next_record_offset = 0;
5240 ssl->in_left = 0;
5241
5242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5243 "(header)" ) );
5244 }
5245
5246 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005247 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005248 }
5249#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005250 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005251 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005252
5253 /*
5254 * Read and optionally decrypt the message contents
5255 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005256 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5257 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005259 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005260 return( ret );
5261 }
5262
5263 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005264#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005265 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005267 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005268 if( ssl->next_record_offset < ssl->in_left )
5269 {
5270 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5271 }
5272 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005273 else
5274#endif
5275 ssl->in_left = 0;
5276
5277 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005279#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005280 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005281 {
5282 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5284 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005285 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005286 /* Except when waiting for Finished as a bad mac here
5287 * probably means something went wrong in the handshake
5288 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5289 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5290 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5291 {
5292#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5293 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5294 {
5295 mbedtls_ssl_send_alert_message( ssl,
5296 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5297 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5298 }
5299#endif
5300 return( ret );
5301 }
5302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005303#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005304 if( ssl->conf->badmac_limit != 0 &&
5305 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5308 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005309 }
5310#endif
5311
Hanno Becker4a810fb2017-05-24 16:27:30 +01005312 /* As above, invalid records cause
5313 * dismissal of the whole datagram. */
5314
5315 ssl->next_record_offset = 0;
5316 ssl->in_left = 0;
5317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005319 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005320 }
5321
5322 return( ret );
5323 }
5324 else
5325#endif
5326 {
5327 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005328#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5329 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005331 mbedtls_ssl_send_alert_message( ssl,
5332 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5333 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005334 }
5335#endif
5336 return( ret );
5337 }
5338 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005339
Simon Butcher99000142016-10-13 17:21:01 +01005340 return( 0 );
5341}
5342
5343int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5344{
5345 int ret;
5346
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005347 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005348 * Handle particular types of records
5349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005350 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005351 {
Simon Butcher99000142016-10-13 17:21:01 +01005352 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5353 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005354 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005355 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005356 }
5357
Hanno Beckere678eaa2018-08-21 14:57:46 +01005358 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005359 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005360 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005361 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5363 ssl->in_msglen ) );
5364 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005365 }
5366
Hanno Beckere678eaa2018-08-21 14:57:46 +01005367 if( ssl->in_msg[0] != 1 )
5368 {
5369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5370 ssl->in_msg[0] ) );
5371 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5372 }
5373
5374#if defined(MBEDTLS_SSL_PROTO_DTLS)
5375 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5376 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5377 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5378 {
5379 if( ssl->handshake == NULL )
5380 {
5381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5382 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5383 }
5384
5385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5386 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5387 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005388#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005389 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005391 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005392 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005393 if( ssl->in_msglen != 2 )
5394 {
5395 /* Note: Standard allows for more than one 2 byte alert
5396 to be packed in a single message, but Mbed TLS doesn't
5397 currently support this. */
5398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5399 ssl->in_msglen ) );
5400 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5401 }
5402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005404 ssl->in_msg[0], ssl->in_msg[1] ) );
5405
5406 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005407 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005409 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005412 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005413 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005414 }
5415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5417 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5420 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005421 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005422
5423#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5424 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5425 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5426 {
Hanno Becker90333da2017-10-10 11:27:13 +01005427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005428 /* Will be handled when trying to parse ServerHello */
5429 return( 0 );
5430 }
5431#endif
5432
5433#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5434 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5435 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5436 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5437 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5438 {
5439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5440 /* Will be handled in mbedtls_ssl_parse_certificate() */
5441 return( 0 );
5442 }
5443#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5444
5445 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005446 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005447 }
5448
Hanno Beckerc76c6192017-06-06 10:03:17 +01005449#if defined(MBEDTLS_SSL_PROTO_DTLS)
5450 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5451 ssl->handshake != NULL &&
5452 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5453 {
5454 ssl_handshake_wrapup_free_hs_transform( ssl );
5455 }
5456#endif
5457
Paul Bakker5121ce52009-01-03 21:22:43 +00005458 return( 0 );
5459}
5460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005461int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005462{
5463 int ret;
5464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005465 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5466 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5467 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005468 {
5469 return( ret );
5470 }
5471
5472 return( 0 );
5473}
5474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005475int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005476 unsigned char level,
5477 unsigned char message )
5478{
5479 int ret;
5480
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005481 if( ssl == NULL || ssl->conf == NULL )
5482 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005485 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005487 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005488 ssl->out_msglen = 2;
5489 ssl->out_msg[0] = level;
5490 ssl->out_msg[1] = message;
5491
Hanno Becker67bc7c32018-08-06 11:33:50 +01005492 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005495 return( ret );
5496 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005498
5499 return( 0 );
5500}
5501
Paul Bakker5121ce52009-01-03 21:22:43 +00005502/*
5503 * Handshake functions
5504 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005505#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5506 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5507 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5508 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5509 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5510 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5511 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005512/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005513int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005514{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005515 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005519 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5520 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005521 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5522 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005525 ssl->state++;
5526 return( 0 );
5527 }
5528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5530 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005531}
5532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005533int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005534{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005535 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005539 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5540 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005541 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5542 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005544 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005545 ssl->state++;
5546 return( 0 );
5547 }
5548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5550 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005551}
Gilles Peskinef9828522017-05-03 12:28:43 +02005552
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005553#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005554/* Some certificate support -> implement write and parse */
5555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005556int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005557{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005558 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005559 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005560 const mbedtls_x509_crt *crt;
5561 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005565 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5566 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005567 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5568 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005571 ssl->state++;
5572 return( 0 );
5573 }
5574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005575#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005576 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005577 {
5578 if( ssl->client_auth == 0 )
5579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005581 ssl->state++;
5582 return( 0 );
5583 }
5584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005586 /*
5587 * If using SSLv3 and got no cert, send an Alert message
5588 * (otherwise an empty Certificate message will be sent).
5589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005590 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5591 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005592 {
5593 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005594 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5595 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5596 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005599 goto write_msg;
5600 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005601#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005602 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005603#endif /* MBEDTLS_SSL_CLI_C */
5604#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005605 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005607 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5610 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005611 }
5612 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005613#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005615 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005616
5617 /*
5618 * 0 . 0 handshake type
5619 * 1 . 3 handshake length
5620 * 4 . 6 length of all certs
5621 * 7 . 9 length of cert. 1
5622 * 10 . n-1 peer certificate
5623 * n . n+2 length of cert. 2
5624 * n+3 . ... upper level cert, etc.
5625 */
5626 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005627 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005628
Paul Bakker29087132010-03-21 21:03:34 +00005629 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005630 {
5631 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005632 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005635 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005636 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005637 }
5638
5639 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5640 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5641 ssl->out_msg[i + 2] = (unsigned char)( n );
5642
5643 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5644 i += n; crt = crt->next;
5645 }
5646
5647 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5648 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5649 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5650
5651 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005652 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5653 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005654
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005655#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005656write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005657#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005658
5659 ssl->state++;
5660
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005661 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005662 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005663 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005664 return( ret );
5665 }
5666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005668
Paul Bakkered27a042013-04-18 22:46:23 +02005669 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005670}
5671
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005672/*
5673 * Once the certificate message is read, parse it into a cert chain and
5674 * perform basic checks, but leave actual verification to the caller
5675 */
5676static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005677{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005678 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005679 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005680 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682#if defined(MBEDTLS_SSL_SRV_C)
5683#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005684 /*
5685 * Check if the client sent an empty certificate
5686 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005687 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005688 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005689 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005690 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005691 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5692 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5693 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005696
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005697 /* The client was asked for a certificate but didn't send
5698 one. The client should know what's going on, so we
5699 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005700 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005701 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005702 }
5703 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005706#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5707 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005708 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005709 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005711 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5712 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5713 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5714 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005717
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005718 /* The client was asked for a certificate but didn't send
5719 one. The client should know what's going on, so we
5720 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005721 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005722 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005723 }
5724 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5726 MBEDTLS_SSL_PROTO_TLS1_2 */
5727#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005729 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005732 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5733 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005734 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005735 }
5736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005737 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5738 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005740 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005741 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5742 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005743 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005744 }
5745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005746 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005747
Paul Bakker5121ce52009-01-03 21:22:43 +00005748 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005749 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005750 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005751 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005752
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005753 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005754 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005757 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5758 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005760 }
5761
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005762 /* In case we tried to reuse a session but it failed */
5763 if( ssl->session_negotiate->peer_cert != NULL )
5764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005765 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5766 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005767 }
5768
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005769 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005770 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005771 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005773 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005774 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5775 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005776 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005777 }
5778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005780
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005781 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005782
5783 while( i < ssl->in_hslen )
5784 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005785 if ( i + 3 > ssl->in_hslen ) {
5786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5787 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5788 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5789 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5790 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005791 if( ssl->in_msg[i] != 0 )
5792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005794 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5795 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005796 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005797 }
5798
5799 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5800 | (unsigned int) ssl->in_msg[i + 2];
5801 i += 3;
5802
5803 if( n < 128 || i + n > ssl->in_hslen )
5804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005806 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5807 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005808 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005809 }
5810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005811 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005812 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005813 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005814 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005815 case 0: /*ok*/
5816 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5817 /* Ignore certificate with an unknown algorithm: maybe a
5818 prior certificate was already trusted. */
5819 break;
5820
5821 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5822 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5823 goto crt_parse_der_failed;
5824
5825 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5826 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5827 goto crt_parse_der_failed;
5828
5829 default:
5830 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5831 crt_parse_der_failed:
5832 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005833 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005834 return( ret );
5835 }
5836
5837 i += n;
5838 }
5839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005840 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005841
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005842 /*
5843 * On client, make sure the server cert doesn't change during renego to
5844 * avoid "triple handshake" attack: https://secure-resumption.com/
5845 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005846#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005847 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005848 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005849 {
5850 if( ssl->session->peer_cert == NULL )
5851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005853 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5854 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005855 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005856 }
5857
5858 if( ssl->session->peer_cert->raw.len !=
5859 ssl->session_negotiate->peer_cert->raw.len ||
5860 memcmp( ssl->session->peer_cert->raw.p,
5861 ssl->session_negotiate->peer_cert->raw.p,
5862 ssl->session->peer_cert->raw.len ) != 0 )
5863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005865 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5866 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005867 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005868 }
5869 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005870#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005871
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005872 return( 0 );
5873}
5874
5875int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5876{
5877 int ret;
5878 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5879 ssl->transform_negotiate->ciphersuite_info;
5880#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5881 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
5882 ? ssl->handshake->sni_authmode
5883 : ssl->conf->authmode;
5884#else
5885 const int authmode = ssl->conf->authmode;
5886#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005887 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005888
5889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5890
5891 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5892 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
5893 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5894 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5895 {
5896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5897 ssl->state++;
5898 return( 0 );
5899 }
5900
5901#if defined(MBEDTLS_SSL_SRV_C)
5902 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5903 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5904 {
5905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5906 ssl->state++;
5907 return( 0 );
5908 }
5909
5910 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5911 authmode == MBEDTLS_SSL_VERIFY_NONE )
5912 {
5913 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005915
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005916 ssl->state++;
5917 return( 0 );
5918 }
5919#endif
5920
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005921#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5922 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005923 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005924 {
5925 goto crt_verify;
5926 }
5927#endif
5928
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02005929 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005930 {
5931 /* mbedtls_ssl_read_record may have sent an alert already. We
5932 let it decide whether to alert. */
5933 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5934 return( ret );
5935 }
5936
5937 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
5938 {
5939#if defined(MBEDTLS_SSL_SRV_C)
5940 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
5941 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
5942 {
5943 ret = 0;
5944 }
5945#endif
5946
5947 ssl->state++;
5948 return( ret );
5949 }
5950
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005951#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5952 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005953 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005954
5955crt_verify:
5956 if( ssl->handshake->ecrs_enabled)
5957 rs_ctx = &ssl->handshake->ecrs_ctx;
5958#endif
5959
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005960 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005961 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005962 mbedtls_x509_crt *ca_chain;
5963 mbedtls_x509_crl *ca_crl;
5964
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005965#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005966 if( ssl->handshake->sni_ca_chain != NULL )
5967 {
5968 ca_chain = ssl->handshake->sni_ca_chain;
5969 ca_crl = ssl->handshake->sni_ca_crl;
5970 }
5971 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005972#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005973 {
5974 ca_chain = ssl->conf->ca_chain;
5975 ca_crl = ssl->conf->ca_crl;
5976 }
5977
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005978 /*
5979 * Main check: verify certificate
5980 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005981 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005982 ssl->session_negotiate->peer_cert,
5983 ca_chain, ca_crl,
5984 ssl->conf->cert_profile,
5985 ssl->hostname,
5986 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005987 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00005988
5989 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005991 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005992 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005993
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005994#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5995 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02005996 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005997#endif
5998
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005999 /*
6000 * Secondary checks: always done, but change 'ret' only if it was 0
6001 */
6002
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006003#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006006
6007 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006009 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006010 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006011 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006014 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006015 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006016 }
6017 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006018#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006020 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006021 ciphersuite_info,
6022 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006023 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006026 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006027 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006028 }
6029
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006030 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6031 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6032 * with details encoded in the verification flags. All other kinds
6033 * of error codes, including those from the user provided f_vrfy
6034 * functions, are treated as fatal and lead to a failure of
6035 * ssl_parse_certificate even if verification was optional. */
6036 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6037 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6038 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6039 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006040 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006041 }
6042
6043 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6044 {
6045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6046 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6047 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006048
6049 if( ret != 0 )
6050 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006051 uint8_t alert;
6052
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006053 /* The certificate may have been rejected for several reasons.
6054 Pick one and send the corresponding alert. Which alert to send
6055 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006056 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6057 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6058 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6059 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6060 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6061 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6062 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6063 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6064 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6065 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6066 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6067 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6068 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6069 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6070 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6071 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6072 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6073 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6074 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6075 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006076 else
6077 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006078 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6079 alert );
6080 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006081
Hanno Beckere6706e62017-05-15 16:05:15 +01006082#if defined(MBEDTLS_DEBUG_C)
6083 if( ssl->session_negotiate->verify_result != 0 )
6084 {
6085 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6086 ssl->session_negotiate->verify_result ) );
6087 }
6088 else
6089 {
6090 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6091 }
6092#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006093 }
6094
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006095 ssl->state++;
6096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006098
6099 return( ret );
6100}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006101#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6102 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6103 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6104 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6105 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6106 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6107 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006110{
6111 int ret;
6112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006113 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006115 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006116 ssl->out_msglen = 1;
6117 ssl->out_msg[0] = 1;
6118
Paul Bakker5121ce52009-01-03 21:22:43 +00006119 ssl->state++;
6120
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006121 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006122 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006123 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006124 return( ret );
6125 }
6126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006128
6129 return( 0 );
6130}
6131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006132int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006133{
6134 int ret;
6135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006137
Hanno Becker327c93b2018-08-15 13:56:18 +01006138 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006141 return( ret );
6142 }
6143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006144 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006147 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6148 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006149 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006150 }
6151
Hanno Beckere678eaa2018-08-21 14:57:46 +01006152 /* CCS records are only accepted if they have length 1 and content '1',
6153 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006154
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006155 /*
6156 * Switch to our negotiated transform and session parameters for inbound
6157 * data.
6158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006159 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006160 ssl->transform_in = ssl->transform_negotiate;
6161 ssl->session_in = ssl->session_negotiate;
6162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006163#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006164 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006166#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006167 ssl_dtls_replay_reset( ssl );
6168#endif
6169
6170 /* Increment epoch */
6171 if( ++ssl->in_epoch == 0 )
6172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006174 /* This is highly unlikely to happen for legitimate reasons, so
6175 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006176 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006177 }
6178 }
6179 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006180#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006181 memset( ssl->in_ctr, 0, 8 );
6182
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006183 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006185#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6186 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006188 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006190 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006191 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6192 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006194 }
6195 }
6196#endif
6197
Paul Bakker5121ce52009-01-03 21:22:43 +00006198 ssl->state++;
6199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006201
6202 return( 0 );
6203}
6204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006205void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6206 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006207{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006208 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006210#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6211 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6212 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006213 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006214 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006215#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006216#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6217#if defined(MBEDTLS_SHA512_C)
6218 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006219 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6220 else
6221#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006222#if defined(MBEDTLS_SHA256_C)
6223 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006224 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006225 else
6226#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006227#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006230 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006231 }
Paul Bakker380da532012-04-18 16:10:25 +00006232}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006236#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6237 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006238 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6239 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006240#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006241#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6242#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006243 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006244#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006245#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006246 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006247#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006248#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006249}
6250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006252 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006253{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6255 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006256 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6257 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006258#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6260#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006261 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006262#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006263#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006264 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006265#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006266#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006267}
6268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006269#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6270 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6271static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006272 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006273{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006274 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6275 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006276}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006277#endif
Paul Bakker380da532012-04-18 16:10:25 +00006278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006279#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6280#if defined(MBEDTLS_SHA256_C)
6281static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006282 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006283{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006284 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006285}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006286#endif
Paul Bakker380da532012-04-18 16:10:25 +00006287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006288#if defined(MBEDTLS_SHA512_C)
6289static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006290 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006291{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006292 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006293}
Paul Bakker769075d2012-11-24 11:26:46 +01006294#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006295#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006297#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006298static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006300{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006301 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006302 mbedtls_md5_context md5;
6303 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006304
Paul Bakker5121ce52009-01-03 21:22:43 +00006305 unsigned char padbuf[48];
6306 unsigned char md5sum[16];
6307 unsigned char sha1sum[20];
6308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006309 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006310 if( !session )
6311 session = ssl->session;
6312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006314
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006315 mbedtls_md5_init( &md5 );
6316 mbedtls_sha1_init( &sha1 );
6317
6318 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6319 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006320
6321 /*
6322 * SSLv3:
6323 * hash =
6324 * MD5( master + pad2 +
6325 * MD5( handshake + sender + master + pad1 ) )
6326 * + SHA1( master + pad2 +
6327 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006328 */
6329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006330#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006331 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6332 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006333#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006335#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006336 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6337 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006338#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006340 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006341 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006342
Paul Bakker1ef83d62012-04-11 12:09:53 +00006343 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006344
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006345 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6346 mbedtls_md5_update_ret( &md5, session->master, 48 );
6347 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6348 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006349
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006350 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6351 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6352 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6353 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006354
Paul Bakker1ef83d62012-04-11 12:09:53 +00006355 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006356
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006357 mbedtls_md5_starts_ret( &md5 );
6358 mbedtls_md5_update_ret( &md5, session->master, 48 );
6359 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6360 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6361 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006362
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006363 mbedtls_sha1_starts_ret( &sha1 );
6364 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6365 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6366 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6367 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006369 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006370
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006371 mbedtls_md5_free( &md5 );
6372 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006373
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006374 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6375 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6376 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006379}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006380#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006382#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006383static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006384 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006385{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006386 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006387 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006388 mbedtls_md5_context md5;
6389 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006390 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006392 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006393 if( !session )
6394 session = ssl->session;
6395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006397
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006398 mbedtls_md5_init( &md5 );
6399 mbedtls_sha1_init( &sha1 );
6400
6401 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6402 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006403
Paul Bakker1ef83d62012-04-11 12:09:53 +00006404 /*
6405 * TLSv1:
6406 * hash = PRF( master, finished_label,
6407 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6408 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006410#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006411 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6412 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006413#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006415#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006416 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6417 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006418#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006421 ? "client finished"
6422 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006423
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006424 mbedtls_md5_finish_ret( &md5, padbuf );
6425 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006426
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006427 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006428 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006430 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006431
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006432 mbedtls_md5_free( &md5 );
6433 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006434
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006435 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006438}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006439#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006441#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6442#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006443static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006444 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006445{
6446 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006447 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006448 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006449 unsigned char padbuf[32];
6450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006451 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006452 if( !session )
6453 session = ssl->session;
6454
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006455 mbedtls_sha256_init( &sha256 );
6456
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006458
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006459 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006460
6461 /*
6462 * TLSv1.2:
6463 * hash = PRF( master, finished_label,
6464 * Hash( handshake ) )[0.11]
6465 */
6466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006467#if !defined(MBEDTLS_SHA256_ALT)
6468 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006469 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006470#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006472 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006473 ? "client finished"
6474 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006475
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006476 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006477
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006478 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006479 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006481 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006482
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006483 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006484
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006485 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006488}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006491#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006492static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006494{
6495 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006496 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006497 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006498 unsigned char padbuf[48];
6499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006501 if( !session )
6502 session = ssl->session;
6503
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006504 mbedtls_sha512_init( &sha512 );
6505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006507
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006508 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006509
6510 /*
6511 * TLSv1.2:
6512 * hash = PRF( master, finished_label,
6513 * Hash( handshake ) )[0.11]
6514 */
6515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006516#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006517 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6518 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006519#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006521 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006522 ? "client finished"
6523 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006524
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006525 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006526
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006527 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006528 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006530 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006531
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006532 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006533
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006534 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006537}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006538#endif /* MBEDTLS_SHA512_C */
6539#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006541static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006542{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006543 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006544
6545 /*
6546 * Free our handshake params
6547 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006548 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006549 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006550 ssl->handshake = NULL;
6551
6552 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006553 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006554 */
6555 if( ssl->transform )
6556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006557 mbedtls_ssl_transform_free( ssl->transform );
6558 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006559 }
6560 ssl->transform = ssl->transform_negotiate;
6561 ssl->transform_negotiate = NULL;
6562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006563 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006564}
6565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006567{
6568 int resume = ssl->handshake->resume;
6569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006570 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006572#if defined(MBEDTLS_SSL_RENEGOTIATION)
6573 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006575 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006576 ssl->renego_records_seen = 0;
6577 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006578#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006579
6580 /*
6581 * Free the previous session and switch in the current one
6582 */
Paul Bakker0a597072012-09-25 21:55:46 +00006583 if( ssl->session )
6584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006585#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006586 /* RFC 7366 3.1: keep the EtM state */
6587 ssl->session_negotiate->encrypt_then_mac =
6588 ssl->session->encrypt_then_mac;
6589#endif
6590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006591 mbedtls_ssl_session_free( ssl->session );
6592 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006593 }
6594 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006595 ssl->session_negotiate = NULL;
6596
Paul Bakker0a597072012-09-25 21:55:46 +00006597 /*
6598 * Add cache entry
6599 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006600 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006601 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006602 resume == 0 )
6603 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006604 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006606 }
Paul Bakker0a597072012-09-25 21:55:46 +00006607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006608#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006609 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006610 ssl->handshake->flight != NULL )
6611 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006612 /* Cancel handshake timer */
6613 ssl_set_timer( ssl, 0 );
6614
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006615 /* Keep last flight around in case we need to resend it:
6616 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006617 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006618 }
6619 else
6620#endif
6621 ssl_handshake_wrapup_free_hs_transform( ssl );
6622
Paul Bakker48916f92012-09-16 19:57:18 +00006623 ssl->state++;
6624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006625 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006626}
6627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006628int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006629{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006630 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006633
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006634 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006635
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006636 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006637
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006638 /*
6639 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6640 * may define some other value. Currently (early 2016), no defined
6641 * ciphersuite does this (and this is unlikely to change as activity has
6642 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006646#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006647 ssl->verify_data_len = hash_len;
6648 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006649#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006650
Paul Bakker5121ce52009-01-03 21:22:43 +00006651 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6653 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006654
6655 /*
6656 * In case of session resuming, invert the client and server
6657 * ChangeCipherSpec messages order.
6658 */
Paul Bakker0a597072012-09-25 21:55:46 +00006659 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006661#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006662 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006664#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006665#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006666 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006667 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006668#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006669 }
6670 else
6671 ssl->state++;
6672
Paul Bakker48916f92012-09-16 19:57:18 +00006673 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006674 * Switch to our negotiated transform and session parameters for outbound
6675 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006676 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006677 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006679#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006680 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006681 {
6682 unsigned char i;
6683
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006684 /* Remember current epoch settings for resending */
6685 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006686 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006687
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006688 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006689 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006690
6691 /* Increment epoch */
6692 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006693 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006694 break;
6695
6696 /* The loop goes to its end iff the counter is wrapping */
6697 if( i == 0 )
6698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6700 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006701 }
6702 }
6703 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006705 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006706
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006707 ssl->transform_out = ssl->transform_negotiate;
6708 ssl->session_out = ssl->session_negotiate;
6709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6711 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6716 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006717 }
6718 }
6719#endif
6720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006722 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006723 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006724#endif
6725
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006726 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006727 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006728 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006729 return( ret );
6730 }
6731
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006732#if defined(MBEDTLS_SSL_PROTO_DTLS)
6733 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6734 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6735 {
6736 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6737 return( ret );
6738 }
6739#endif
6740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006742
6743 return( 0 );
6744}
6745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006746#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006747#define SSL_MAX_HASH_LEN 36
6748#else
6749#define SSL_MAX_HASH_LEN 12
6750#endif
6751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006752int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006753{
Paul Bakker23986e52011-04-24 08:57:21 +00006754 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006755 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006756 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006759
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006760 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006761
Hanno Becker327c93b2018-08-15 13:56:18 +01006762 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006765 return( ret );
6766 }
6767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006768 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006771 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6772 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006773 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006774 }
6775
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006776 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777#if defined(MBEDTLS_SSL_PROTO_SSL3)
6778 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006779 hash_len = 36;
6780 else
6781#endif
6782 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006784 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6785 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006788 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6789 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006791 }
6792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006793 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006794 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006797 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6798 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006799 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006800 }
6801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006802#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006803 ssl->verify_data_len = hash_len;
6804 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006805#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006806
Paul Bakker0a597072012-09-25 21:55:46 +00006807 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006810 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006811 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006812#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006813#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006814 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006815 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006816#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006817 }
6818 else
6819 ssl->state++;
6820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006822 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006823 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006824#endif
6825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006827
6828 return( 0 );
6829}
6830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006831static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006832{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006833 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006835#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6836 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6837 mbedtls_md5_init( &handshake->fin_md5 );
6838 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006839 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6840 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006841#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006842#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6843#if defined(MBEDTLS_SHA256_C)
6844 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006845 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006846#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006847#if defined(MBEDTLS_SHA512_C)
6848 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006849 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006850#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006851#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006852
6853 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006854
6855#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6856 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6857 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6858#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006860#if defined(MBEDTLS_DHM_C)
6861 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006862#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006863#if defined(MBEDTLS_ECDH_C)
6864 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006865#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006866#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006867 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006868#if defined(MBEDTLS_SSL_CLI_C)
6869 handshake->ecjpake_cache = NULL;
6870 handshake->ecjpake_cache_len = 0;
6871#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006872#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006873
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006874#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02006875 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006876#endif
6877
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006878#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6879 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6880#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006881}
6882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006883static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006884{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006885 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006887 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6888 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006890 mbedtls_md_init( &transform->md_ctx_enc );
6891 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006892}
6893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006894void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006895{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006896 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006897}
6898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006899static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006900{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006901 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006902 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006903 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006904 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006905 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006906 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006907 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006908
6909 /*
6910 * Either the pointers are now NULL or cleared properly and can be freed.
6911 * Now allocate missing structures.
6912 */
6913 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006914 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006915 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006916 }
Paul Bakker48916f92012-09-16 19:57:18 +00006917
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006918 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006919 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006920 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006921 }
Paul Bakker48916f92012-09-16 19:57:18 +00006922
Paul Bakker82788fb2014-10-20 13:59:19 +02006923 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006924 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006925 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006926 }
Paul Bakker48916f92012-09-16 19:57:18 +00006927
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006928 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006929 if( ssl->handshake == NULL ||
6930 ssl->transform_negotiate == NULL ||
6931 ssl->session_negotiate == NULL )
6932 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006935 mbedtls_free( ssl->handshake );
6936 mbedtls_free( ssl->transform_negotiate );
6937 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006938
6939 ssl->handshake = NULL;
6940 ssl->transform_negotiate = NULL;
6941 ssl->session_negotiate = NULL;
6942
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006943 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006944 }
6945
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006946 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006947 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006948 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006949 ssl_handshake_params_init( ssl->handshake );
6950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006951#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006952 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6953 {
6954 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006955
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006956 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6957 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6958 else
6959 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006960
6961 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006962 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006963#endif
6964
Paul Bakker48916f92012-09-16 19:57:18 +00006965 return( 0 );
6966}
6967
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006968#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006969/* Dummy cookie callbacks for defaults */
6970static int ssl_cookie_write_dummy( void *ctx,
6971 unsigned char **p, unsigned char *end,
6972 const unsigned char *cli_id, size_t cli_id_len )
6973{
6974 ((void) ctx);
6975 ((void) p);
6976 ((void) end);
6977 ((void) cli_id);
6978 ((void) cli_id_len);
6979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006980 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006981}
6982
6983static int ssl_cookie_check_dummy( void *ctx,
6984 const unsigned char *cookie, size_t cookie_len,
6985 const unsigned char *cli_id, size_t cli_id_len )
6986{
6987 ((void) ctx);
6988 ((void) cookie);
6989 ((void) cookie_len);
6990 ((void) cli_id);
6991 ((void) cli_id_len);
6992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006993 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006994}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006995#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006996
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006997/* Once ssl->out_hdr as the address of the beginning of the
6998 * next outgoing record is set, deduce the other pointers.
6999 *
7000 * Note: For TLS, we save the implicit record sequence number
7001 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7002 * and the caller has to make sure there's space for this.
7003 */
7004
7005static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7006 mbedtls_ssl_transform *transform )
7007{
7008#if defined(MBEDTLS_SSL_PROTO_DTLS)
7009 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7010 {
7011 ssl->out_ctr = ssl->out_hdr + 3;
7012 ssl->out_len = ssl->out_hdr + 11;
7013 ssl->out_iv = ssl->out_hdr + 13;
7014 }
7015 else
7016#endif
7017 {
7018 ssl->out_ctr = ssl->out_hdr - 8;
7019 ssl->out_len = ssl->out_hdr + 3;
7020 ssl->out_iv = ssl->out_hdr + 5;
7021 }
7022
7023 /* Adjust out_msg to make space for explicit IV, if used. */
7024 if( transform != NULL &&
7025 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7026 {
7027 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7028 }
7029 else
7030 ssl->out_msg = ssl->out_iv;
7031}
7032
7033/* Once ssl->in_hdr as the address of the beginning of the
7034 * next incoming record is set, deduce the other pointers.
7035 *
7036 * Note: For TLS, we save the implicit record sequence number
7037 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7038 * and the caller has to make sure there's space for this.
7039 */
7040
7041static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7042 mbedtls_ssl_transform *transform )
7043{
7044#if defined(MBEDTLS_SSL_PROTO_DTLS)
7045 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7046 {
7047 ssl->in_ctr = ssl->in_hdr + 3;
7048 ssl->in_len = ssl->in_hdr + 11;
7049 ssl->in_iv = ssl->in_hdr + 13;
7050 }
7051 else
7052#endif
7053 {
7054 ssl->in_ctr = ssl->in_hdr - 8;
7055 ssl->in_len = ssl->in_hdr + 3;
7056 ssl->in_iv = ssl->in_hdr + 5;
7057 }
7058
7059 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7060 if( transform != NULL &&
7061 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7062 {
7063 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7064 }
7065 else
7066 ssl->in_msg = ssl->in_iv;
7067}
7068
Paul Bakker5121ce52009-01-03 21:22:43 +00007069/*
7070 * Initialize an SSL context
7071 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007072void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7073{
7074 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7075}
7076
7077/*
7078 * Setup an SSL context
7079 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007080
7081static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7082{
7083 /* Set the incoming and outgoing record pointers. */
7084#if defined(MBEDTLS_SSL_PROTO_DTLS)
7085 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7086 {
7087 ssl->out_hdr = ssl->out_buf;
7088 ssl->in_hdr = ssl->in_buf;
7089 }
7090 else
7091#endif /* MBEDTLS_SSL_PROTO_DTLS */
7092 {
7093 ssl->out_hdr = ssl->out_buf + 8;
7094 ssl->in_hdr = ssl->in_buf + 8;
7095 }
7096
7097 /* Derive other internal pointers. */
7098 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7099 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7100}
7101
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007102int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007103 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007104{
Paul Bakker48916f92012-09-16 19:57:18 +00007105 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007106
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007107 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007108
7109 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007110 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007111 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007112
7113 /* Set to NULL in case of an error condition */
7114 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007115
Angus Grattond8213d02016-05-25 20:56:48 +10007116 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7117 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007118 {
Angus Grattond8213d02016-05-25 20:56:48 +10007119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007120 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007121 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007122 }
7123
7124 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7125 if( ssl->out_buf == NULL )
7126 {
7127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007128 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007129 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007130 }
7131
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007132 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007133
Paul Bakker48916f92012-09-16 19:57:18 +00007134 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007135 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007136
7137 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007138
7139error:
7140 mbedtls_free( ssl->in_buf );
7141 mbedtls_free( ssl->out_buf );
7142
7143 ssl->conf = NULL;
7144
7145 ssl->in_buf = NULL;
7146 ssl->out_buf = NULL;
7147
7148 ssl->in_hdr = NULL;
7149 ssl->in_ctr = NULL;
7150 ssl->in_len = NULL;
7151 ssl->in_iv = NULL;
7152 ssl->in_msg = NULL;
7153
7154 ssl->out_hdr = NULL;
7155 ssl->out_ctr = NULL;
7156 ssl->out_len = NULL;
7157 ssl->out_iv = NULL;
7158 ssl->out_msg = NULL;
7159
k-stachowiak9f7798e2018-07-31 16:52:32 +02007160 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007161}
7162
7163/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007164 * Reset an initialized and used SSL context for re-use while retaining
7165 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007166 *
7167 * If partial is non-zero, keep data in the input buffer and client ID.
7168 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007169 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007170static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007171{
Paul Bakker48916f92012-09-16 19:57:18 +00007172 int ret;
7173
Hanno Becker7e772132018-08-10 12:38:21 +01007174#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7175 !defined(MBEDTLS_SSL_SRV_C)
7176 ((void) partial);
7177#endif
7178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007179 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007180
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007181 /* Cancel any possibly running timer */
7182 ssl_set_timer( ssl, 0 );
7183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007184#if defined(MBEDTLS_SSL_RENEGOTIATION)
7185 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007186 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007187
7188 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7190 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007191#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007192 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007193
Paul Bakker7eb013f2011-10-06 12:37:39 +00007194 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007195 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007196
7197 ssl->in_msgtype = 0;
7198 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007199#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007200 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007201 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007202#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007204 ssl_dtls_replay_reset( ssl );
7205#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007206
7207 ssl->in_hslen = 0;
7208 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007209
7210 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007211
7212 ssl->out_msgtype = 0;
7213 ssl->out_msglen = 0;
7214 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007215#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7216 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007217 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007218#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007219
Hanno Becker19859472018-08-06 09:40:20 +01007220 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7221
Paul Bakker48916f92012-09-16 19:57:18 +00007222 ssl->transform_in = NULL;
7223 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007224
Hanno Becker78640902018-08-13 16:35:15 +01007225 ssl->session_in = NULL;
7226 ssl->session_out = NULL;
7227
Angus Grattond8213d02016-05-25 20:56:48 +10007228 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007229
7230#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007231 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007232#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7233 {
7234 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007235 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007236 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7239 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7242 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7245 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007246 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007247 }
7248#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007249
Paul Bakker48916f92012-09-16 19:57:18 +00007250 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252 mbedtls_ssl_transform_free( ssl->transform );
7253 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007254 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007255 }
Paul Bakker48916f92012-09-16 19:57:18 +00007256
Paul Bakkerc0463502013-02-14 11:19:38 +01007257 if( ssl->session )
7258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259 mbedtls_ssl_session_free( ssl->session );
7260 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007261 ssl->session = NULL;
7262 }
7263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007265 ssl->alpn_chosen = NULL;
7266#endif
7267
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007268#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007269#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007270 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007271#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007272 {
7273 mbedtls_free( ssl->cli_id );
7274 ssl->cli_id = NULL;
7275 ssl->cli_id_len = 0;
7276 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007277#endif
7278
Paul Bakker48916f92012-09-16 19:57:18 +00007279 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7280 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007281
7282 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007283}
7284
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007285/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007286 * Reset an initialized and used SSL context for re-use while retaining
7287 * all application-set variables, function pointers and data.
7288 */
7289int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7290{
7291 return( ssl_session_reset_int( ssl, 0 ) );
7292}
7293
7294/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007295 * SSL set accessors
7296 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007297void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007298{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007299 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007300}
7301
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007302void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007303{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007304 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007305}
7306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007307#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007308void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007309{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007310 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007311}
7312#endif
7313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007314#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007315void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007316{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007317 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007318}
7319#endif
7320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007321#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007322
Hanno Becker1841b0a2018-08-24 11:13:57 +01007323void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7324 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007325{
7326 ssl->disable_datagram_packing = !allow_packing;
7327}
7328
7329void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7330 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007331{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007332 conf->hs_timeout_min = min;
7333 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007334}
7335#endif
7336
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007337void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007338{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007339 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007340}
7341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007343void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007344 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007345 void *p_vrfy )
7346{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007347 conf->f_vrfy = f_vrfy;
7348 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007349}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007350#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007351
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007352void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007353 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007354 void *p_rng )
7355{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007356 conf->f_rng = f_rng;
7357 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007358}
7359
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007360void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007361 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007362 void *p_dbg )
7363{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007364 conf->f_dbg = f_dbg;
7365 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007366}
7367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007368void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007369 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007370 mbedtls_ssl_send_t *f_send,
7371 mbedtls_ssl_recv_t *f_recv,
7372 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007373{
7374 ssl->p_bio = p_bio;
7375 ssl->f_send = f_send;
7376 ssl->f_recv = f_recv;
7377 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007378}
7379
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007380#if defined(MBEDTLS_SSL_PROTO_DTLS)
7381void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7382{
7383 ssl->mtu = mtu;
7384}
7385#endif
7386
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007387void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007388{
7389 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007390}
7391
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007392void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7393 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007394 mbedtls_ssl_set_timer_t *f_set_timer,
7395 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007396{
7397 ssl->p_timer = p_timer;
7398 ssl->f_set_timer = f_set_timer;
7399 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007400
7401 /* Make sure we start with no timer running */
7402 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007403}
7404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007406void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007407 void *p_cache,
7408 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7409 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007410{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007411 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007412 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007413 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007414}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007417#if defined(MBEDTLS_SSL_CLI_C)
7418int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007419{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007420 int ret;
7421
7422 if( ssl == NULL ||
7423 session == NULL ||
7424 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007425 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007428 }
7429
7430 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7431 return( ret );
7432
Paul Bakker0a597072012-09-25 21:55:46 +00007433 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007434
7435 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007436}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007437#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007438
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007439void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007440 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007441{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007442 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7443 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7444 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7445 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007446}
7447
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007448void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007449 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007450 int major, int minor )
7451{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007452 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007453 return;
7454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007455 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007456 return;
7457
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007458 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007459}
7460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007461#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007462void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007463 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007464{
7465 conf->cert_profile = profile;
7466}
7467
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007468/* Append a new keycert entry to a (possibly empty) list */
7469static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7470 mbedtls_x509_crt *cert,
7471 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007472{
niisato8ee24222018-06-25 19:05:48 +09007473 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007474
niisato8ee24222018-06-25 19:05:48 +09007475 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7476 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007477 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007478
niisato8ee24222018-06-25 19:05:48 +09007479 new_cert->cert = cert;
7480 new_cert->key = key;
7481 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007482
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007483 /* Update head is the list was null, else add to the end */
7484 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007485 {
niisato8ee24222018-06-25 19:05:48 +09007486 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007487 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007488 else
7489 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007490 mbedtls_ssl_key_cert *cur = *head;
7491 while( cur->next != NULL )
7492 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007493 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007494 }
7495
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007496 return( 0 );
7497}
7498
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007499int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007500 mbedtls_x509_crt *own_cert,
7501 mbedtls_pk_context *pk_key )
7502{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007503 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007504}
7505
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007506void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007507 mbedtls_x509_crt *ca_chain,
7508 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007509{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007510 conf->ca_chain = ca_chain;
7511 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007513#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007514
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007515#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7516int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7517 mbedtls_x509_crt *own_cert,
7518 mbedtls_pk_context *pk_key )
7519{
7520 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7521 own_cert, pk_key ) );
7522}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007523
7524void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7525 mbedtls_x509_crt *ca_chain,
7526 mbedtls_x509_crl *ca_crl )
7527{
7528 ssl->handshake->sni_ca_chain = ca_chain;
7529 ssl->handshake->sni_ca_crl = ca_crl;
7530}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007531
7532void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7533 int authmode )
7534{
7535 ssl->handshake->sni_authmode = authmode;
7536}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007537#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7538
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007539#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007540/*
7541 * Set EC J-PAKE password for current handshake
7542 */
7543int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7544 const unsigned char *pw,
7545 size_t pw_len )
7546{
7547 mbedtls_ecjpake_role role;
7548
Janos Follath8eb64132016-06-03 15:40:57 +01007549 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007550 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7551
7552 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7553 role = MBEDTLS_ECJPAKE_SERVER;
7554 else
7555 role = MBEDTLS_ECJPAKE_CLIENT;
7556
7557 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7558 role,
7559 MBEDTLS_MD_SHA256,
7560 MBEDTLS_ECP_DP_SECP256R1,
7561 pw, pw_len ) );
7562}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007563#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007565#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007566
7567static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
7568{
7569 /* Remove reference to existing PSK, if any. */
7570#if defined(MBEDTLS_USE_PSA_CRYPTO)
7571 if( conf->psk_opaque != 0 )
7572 {
7573 /* The maintenance of the PSK key slot is the
7574 * user's responsibility. */
7575 conf->psk_opaque = 0;
7576 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00007577 /* This and the following branch should never
7578 * be taken simultaenously as we maintain the
7579 * invariant that raw and opaque PSKs are never
7580 * configured simultaneously. As a safeguard,
7581 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007582#endif /* MBEDTLS_USE_PSA_CRYPTO */
7583 if( conf->psk != NULL )
7584 {
7585 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
7586
7587 mbedtls_free( conf->psk );
7588 conf->psk = NULL;
7589 conf->psk_len = 0;
7590 }
7591
7592 /* Remove reference to PSK identity, if any. */
7593 if( conf->psk_identity != NULL )
7594 {
7595 mbedtls_free( conf->psk_identity );
7596 conf->psk_identity = NULL;
7597 conf->psk_identity_len = 0;
7598 }
7599}
7600
Hanno Becker7390c712018-11-15 13:33:04 +00007601/* This function assumes that PSK identity in the SSL config is unset.
7602 * It checks that the provided identity is well-formed and attempts
7603 * to make a copy of it in the SSL config.
7604 * On failure, the PSK identity in the config remains unset. */
7605static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
7606 unsigned char const *psk_identity,
7607 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007608{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007609 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00007610 if( psk_identity == NULL ||
7611 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007612 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007613 {
7614 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7615 }
7616
Hanno Becker7390c712018-11-15 13:33:04 +00007617 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
7618 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007619 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02007620
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007621 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007622 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02007623
7624 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02007625}
7626
Hanno Becker7390c712018-11-15 13:33:04 +00007627int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
7628 const unsigned char *psk, size_t psk_len,
7629 const unsigned char *psk_identity, size_t psk_identity_len )
7630{
7631 int ret;
7632 /* Remove opaque/raw PSK + PSK Identity */
7633 ssl_conf_remove_psk( conf );
7634
7635 /* Check and set raw PSK */
7636 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
7637 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7638 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
7639 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7640 conf->psk_len = psk_len;
7641 memcpy( conf->psk, psk, conf->psk_len );
7642
7643 /* Check and set PSK Identity */
7644 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
7645 if( ret != 0 )
7646 ssl_conf_remove_psk( conf );
7647
7648 return( ret );
7649}
7650
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007651static void ssl_remove_psk( mbedtls_ssl_context *ssl )
7652{
7653#if defined(MBEDTLS_USE_PSA_CRYPTO)
7654 if( ssl->handshake->psk_opaque != 0 )
7655 {
7656 ssl->handshake->psk_opaque = 0;
7657 }
7658 else
7659#endif /* MBEDTLS_USE_PSA_CRYPTO */
7660 if( ssl->handshake->psk != NULL )
7661 {
7662 mbedtls_platform_zeroize( ssl->handshake->psk,
7663 ssl->handshake->psk_len );
7664 mbedtls_free( ssl->handshake->psk );
7665 ssl->handshake->psk_len = 0;
7666 }
7667}
7668
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007669int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7670 const unsigned char *psk, size_t psk_len )
7671{
7672 if( psk == NULL || ssl->handshake == NULL )
7673 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7674
7675 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7676 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7677
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007678 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007679
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007680 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007681 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007682
7683 ssl->handshake->psk_len = psk_len;
7684 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7685
7686 return( 0 );
7687}
7688
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007689#if defined(MBEDTLS_USE_PSA_CRYPTO)
7690int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007691 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007692 const unsigned char *psk_identity,
7693 size_t psk_identity_len )
7694{
Hanno Becker7390c712018-11-15 13:33:04 +00007695 int ret;
7696 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007697 ssl_conf_remove_psk( conf );
7698
Hanno Becker7390c712018-11-15 13:33:04 +00007699 /* Check and set opaque PSK */
7700 if( psk_slot == 0 )
7701 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007702 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00007703
7704 /* Check and set PSK Identity */
7705 ret = ssl_conf_set_psk_identity( conf, psk_identity,
7706 psk_identity_len );
7707 if( ret != 0 )
7708 ssl_conf_remove_psk( conf );
7709
7710 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007711}
7712
7713int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007714 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007715{
7716 if( psk_slot == 0 || ssl->handshake == NULL )
7717 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7718
7719 ssl_remove_psk( ssl );
7720 ssl->handshake->psk_opaque = psk_slot;
7721 return( 0 );
7722}
7723#endif /* MBEDTLS_USE_PSA_CRYPTO */
7724
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007725void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007727 size_t),
7728 void *p_psk )
7729{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007730 conf->f_psk = f_psk;
7731 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007732}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007733#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007734
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007735#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007736
7737#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007738int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007739{
7740 int ret;
7741
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007742 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7743 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7744 {
7745 mbedtls_mpi_free( &conf->dhm_P );
7746 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007747 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007748 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007749
7750 return( 0 );
7751}
Hanno Becker470a8c42017-10-04 15:28:46 +01007752#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007753
Hanno Beckera90658f2017-10-04 15:29:08 +01007754int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7755 const unsigned char *dhm_P, size_t P_len,
7756 const unsigned char *dhm_G, size_t G_len )
7757{
7758 int ret;
7759
7760 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7761 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7762 {
7763 mbedtls_mpi_free( &conf->dhm_P );
7764 mbedtls_mpi_free( &conf->dhm_G );
7765 return( ret );
7766 }
7767
7768 return( 0 );
7769}
Paul Bakker5121ce52009-01-03 21:22:43 +00007770
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007771int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007772{
7773 int ret;
7774
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007775 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7776 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7777 {
7778 mbedtls_mpi_free( &conf->dhm_P );
7779 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007780 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007781 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007782
7783 return( 0 );
7784}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007785#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007786
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007787#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7788/*
7789 * Set the minimum length for Diffie-Hellman parameters
7790 */
7791void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7792 unsigned int bitlen )
7793{
7794 conf->dhm_min_bitlen = bitlen;
7795}
7796#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7797
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007798#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007799/*
7800 * Set allowed/preferred hashes for handshake signatures
7801 */
7802void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7803 const int *hashes )
7804{
7805 conf->sig_hashes = hashes;
7806}
Hanno Becker947194e2017-04-07 13:25:49 +01007807#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007808
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007809#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007810/*
7811 * Set the allowed elliptic curves
7812 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007813void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007814 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007815{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007816 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007817}
Hanno Becker947194e2017-04-07 13:25:49 +01007818#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007819
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007820#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007821int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007822{
Hanno Becker947194e2017-04-07 13:25:49 +01007823 /* Initialize to suppress unnecessary compiler warning */
7824 size_t hostname_len = 0;
7825
7826 /* Check if new hostname is valid before
7827 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007828 if( hostname != NULL )
7829 {
7830 hostname_len = strlen( hostname );
7831
7832 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7833 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7834 }
7835
7836 /* Now it's clear that we will overwrite the old hostname,
7837 * so we can free it safely */
7838
7839 if( ssl->hostname != NULL )
7840 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007841 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007842 mbedtls_free( ssl->hostname );
7843 }
7844
7845 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007846
Paul Bakker5121ce52009-01-03 21:22:43 +00007847 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007848 {
7849 ssl->hostname = NULL;
7850 }
7851 else
7852 {
7853 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007854 if( ssl->hostname == NULL )
7855 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007856
Hanno Becker947194e2017-04-07 13:25:49 +01007857 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007858
Hanno Becker947194e2017-04-07 13:25:49 +01007859 ssl->hostname[hostname_len] = '\0';
7860 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007861
7862 return( 0 );
7863}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007864#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007865
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007866#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007867void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007869 const unsigned char *, size_t),
7870 void *p_sni )
7871{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007872 conf->f_sni = f_sni;
7873 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007874}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007878int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007879{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007880 size_t cur_len, tot_len;
7881 const char **p;
7882
7883 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007884 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7885 * MUST NOT be truncated."
7886 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007887 */
7888 tot_len = 0;
7889 for( p = protos; *p != NULL; p++ )
7890 {
7891 cur_len = strlen( *p );
7892 tot_len += cur_len;
7893
7894 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007895 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007896 }
7897
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007898 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007899
7900 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007901}
7902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007904{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007905 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007906}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007907#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007908
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007909void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007910{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007911 conf->max_major_ver = major;
7912 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007913}
7914
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007915void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007916{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007917 conf->min_major_ver = major;
7918 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007919}
7920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007921#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007922void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007923{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007924 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007925}
7926#endif
7927
Janos Follath088ce432017-04-10 12:42:31 +01007928#if defined(MBEDTLS_SSL_SRV_C)
7929void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7930 char cert_req_ca_list )
7931{
7932 conf->cert_req_ca_list = cert_req_ca_list;
7933}
7934#endif
7935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007937void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007938{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007939 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007940}
7941#endif
7942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007944void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007945{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007946 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007947}
7948#endif
7949
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007950#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007951void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007952{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007953 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007954}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007955#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007957#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007958int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007959{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007961 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007964 }
7965
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007966 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007967
7968 return( 0 );
7969}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007970#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007972#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007973void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007974{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007975 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007976}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007977#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007980void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007981{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007982 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007983}
7984#endif
7985
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007986void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007987{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007988 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007989}
7990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007991#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007992void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007993{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007994 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007995}
7996
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007997void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007998{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007999 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008000}
8001
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008002void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008003 const unsigned char period[8] )
8004{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008005 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008006}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008007#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008009#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008010#if defined(MBEDTLS_SSL_CLI_C)
8011void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008012{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008013 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008014}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008015#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008016
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008017#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008018void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8019 mbedtls_ssl_ticket_write_t *f_ticket_write,
8020 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8021 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008022{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008023 conf->f_ticket_write = f_ticket_write;
8024 conf->f_ticket_parse = f_ticket_parse;
8025 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008026}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008027#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008028#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008029
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008030#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8031void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8032 mbedtls_ssl_export_keys_t *f_export_keys,
8033 void *p_export_keys )
8034{
8035 conf->f_export_keys = f_export_keys;
8036 conf->p_export_keys = p_export_keys;
8037}
8038#endif
8039
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008040#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008041void mbedtls_ssl_conf_async_private_cb(
8042 mbedtls_ssl_config *conf,
8043 mbedtls_ssl_async_sign_t *f_async_sign,
8044 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8045 mbedtls_ssl_async_resume_t *f_async_resume,
8046 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008047 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008048{
8049 conf->f_async_sign_start = f_async_sign;
8050 conf->f_async_decrypt_start = f_async_decrypt;
8051 conf->f_async_resume = f_async_resume;
8052 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008053 conf->p_async_config_data = async_config_data;
8054}
8055
Gilles Peskine8f97af72018-04-26 11:46:10 +02008056void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8057{
8058 return( conf->p_async_config_data );
8059}
8060
Gilles Peskine1febfef2018-04-30 11:54:39 +02008061void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008062{
8063 if( ssl->handshake == NULL )
8064 return( NULL );
8065 else
8066 return( ssl->handshake->user_async_ctx );
8067}
8068
Gilles Peskine1febfef2018-04-30 11:54:39 +02008069void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008070 void *ctx )
8071{
8072 if( ssl->handshake != NULL )
8073 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008074}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008075#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008076
Paul Bakker5121ce52009-01-03 21:22:43 +00008077/*
8078 * SSL get accessors
8079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008080size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008081{
8082 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8083}
8084
Hanno Becker8b170a02017-10-10 11:51:19 +01008085int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8086{
8087 /*
8088 * Case A: We're currently holding back
8089 * a message for further processing.
8090 */
8091
8092 if( ssl->keep_current_message == 1 )
8093 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008094 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008095 return( 1 );
8096 }
8097
8098 /*
8099 * Case B: Further records are pending in the current datagram.
8100 */
8101
8102#if defined(MBEDTLS_SSL_PROTO_DTLS)
8103 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8104 ssl->in_left > ssl->next_record_offset )
8105 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008106 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008107 return( 1 );
8108 }
8109#endif /* MBEDTLS_SSL_PROTO_DTLS */
8110
8111 /*
8112 * Case C: A handshake message is being processed.
8113 */
8114
Hanno Becker8b170a02017-10-10 11:51:19 +01008115 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8116 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008117 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008118 return( 1 );
8119 }
8120
8121 /*
8122 * Case D: An application data message is being processed
8123 */
8124 if( ssl->in_offt != NULL )
8125 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008126 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008127 return( 1 );
8128 }
8129
8130 /*
8131 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008132 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008133 * we implement support for multiple alerts in single records.
8134 */
8135
8136 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8137 return( 0 );
8138}
8139
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008140uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008141{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008142 if( ssl->session != NULL )
8143 return( ssl->session->verify_result );
8144
8145 if( ssl->session_negotiate != NULL )
8146 return( ssl->session_negotiate->verify_result );
8147
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008148 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008149}
8150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008151const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008152{
Paul Bakker926c8e42013-03-06 10:23:34 +01008153 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008154 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008156 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008157}
8158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008159const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008160{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008161#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008162 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008163 {
8164 switch( ssl->minor_ver )
8165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008166 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008167 return( "DTLSv1.0" );
8168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008169 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008170 return( "DTLSv1.2" );
8171
8172 default:
8173 return( "unknown (DTLS)" );
8174 }
8175 }
8176#endif
8177
Paul Bakker43ca69c2011-01-15 17:35:19 +00008178 switch( ssl->minor_ver )
8179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008180 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008181 return( "SSLv3.0" );
8182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008184 return( "TLSv1.0" );
8185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008186 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008187 return( "TLSv1.1" );
8188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008189 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008190 return( "TLSv1.2" );
8191
Paul Bakker43ca69c2011-01-15 17:35:19 +00008192 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008193 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008194 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008195}
8196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008197int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008198{
Hanno Becker3136ede2018-08-17 15:28:19 +01008199 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008200 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008201 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008202
Hanno Becker78640902018-08-13 16:35:15 +01008203 if( transform == NULL )
8204 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008206#if defined(MBEDTLS_ZLIB_SUPPORT)
8207 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8208 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008209#endif
8210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008211 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008213 case MBEDTLS_MODE_GCM:
8214 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008215 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008216 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008217 transform_expansion = transform->minlen;
8218 break;
8219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008220 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008221
8222 block_size = mbedtls_cipher_get_block_size(
8223 &transform->cipher_ctx_enc );
8224
Hanno Becker3136ede2018-08-17 15:28:19 +01008225 /* Expansion due to the addition of the MAC. */
8226 transform_expansion += transform->maclen;
8227
8228 /* Expansion due to the addition of CBC padding;
8229 * Theoretically up to 256 bytes, but we never use
8230 * more than the block size of the underlying cipher. */
8231 transform_expansion += block_size;
8232
8233 /* For TLS 1.1 or higher, an explicit IV is added
8234 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008235#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8236 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008237 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008238#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008239
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008240 break;
8241
8242 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008244 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008245 }
8246
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008247 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008248}
8249
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008250#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8251size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8252{
8253 size_t max_len;
8254
8255 /*
8256 * Assume mfl_code is correct since it was checked when set
8257 */
Angus Grattond8213d02016-05-25 20:56:48 +10008258 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008259
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008260 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008261 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008262 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008263 {
Angus Grattond8213d02016-05-25 20:56:48 +10008264 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008265 }
8266
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008267 /* During a handshake, use the value being negotiated */
8268 if( ssl->session_negotiate != NULL &&
8269 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8270 {
8271 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8272 }
8273
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008274 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008275}
8276#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8277
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008278#if defined(MBEDTLS_SSL_PROTO_DTLS)
8279static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8280{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008281 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8282 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8283 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8284 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8285 return ( 0 );
8286
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008287 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8288 return( ssl->mtu );
8289
8290 if( ssl->mtu == 0 )
8291 return( ssl->handshake->mtu );
8292
8293 return( ssl->mtu < ssl->handshake->mtu ?
8294 ssl->mtu : ssl->handshake->mtu );
8295}
8296#endif /* MBEDTLS_SSL_PROTO_DTLS */
8297
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008298int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8299{
8300 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8301
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008302#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8303 !defined(MBEDTLS_SSL_PROTO_DTLS)
8304 (void) ssl;
8305#endif
8306
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008307#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8308 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8309
8310 if( max_len > mfl )
8311 max_len = mfl;
8312#endif
8313
8314#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008315 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008316 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008317 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008318 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8319 const size_t overhead = (size_t) ret;
8320
8321 if( ret < 0 )
8322 return( ret );
8323
8324 if( mtu <= overhead )
8325 {
8326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8327 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8328 }
8329
8330 if( max_len > mtu - overhead )
8331 max_len = mtu - overhead;
8332 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008333#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008334
Hanno Becker0defedb2018-08-10 12:35:02 +01008335#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8336 !defined(MBEDTLS_SSL_PROTO_DTLS)
8337 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008338#endif
8339
8340 return( (int) max_len );
8341}
8342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008343#if defined(MBEDTLS_X509_CRT_PARSE_C)
8344const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008345{
8346 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008347 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008348
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008349 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008350}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008351#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008353#if defined(MBEDTLS_SSL_CLI_C)
8354int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008355{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008356 if( ssl == NULL ||
8357 dst == NULL ||
8358 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008359 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008361 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008362 }
8363
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008364 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008365}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008366#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008367
Paul Bakker5121ce52009-01-03 21:22:43 +00008368/*
Paul Bakker1961b702013-01-25 14:49:24 +01008369 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008370 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008371int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008372{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008373 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008374
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008375 if( ssl == NULL || ssl->conf == NULL )
8376 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008378#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008379 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008380 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008381#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008382#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008383 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008384 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008385#endif
8386
Paul Bakker1961b702013-01-25 14:49:24 +01008387 return( ret );
8388}
8389
8390/*
8391 * Perform the SSL handshake
8392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008393int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008394{
8395 int ret = 0;
8396
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008397 if( ssl == NULL || ssl->conf == NULL )
8398 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008402 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008404 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008405
8406 if( ret != 0 )
8407 break;
8408 }
8409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008410 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008411
8412 return( ret );
8413}
8414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008415#if defined(MBEDTLS_SSL_RENEGOTIATION)
8416#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008417/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008418 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008420static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008421{
8422 int ret;
8423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008425
8426 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008427 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8428 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008429
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008430 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008431 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008432 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008433 return( ret );
8434 }
8435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008437
8438 return( 0 );
8439}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008440#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008441
8442/*
8443 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008444 * - any side: calling mbedtls_ssl_renegotiate(),
8445 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8446 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008447 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008448 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008449 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008451static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008452{
8453 int ret;
8454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008456
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008457 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8458 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008459
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008460 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8461 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008462#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008463 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008464 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008465 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008466 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008467 ssl->handshake->out_msg_seq = 1;
8468 else
8469 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008470 }
8471#endif
8472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008473 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8474 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008476 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008478 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008479 return( ret );
8480 }
8481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008483
8484 return( 0 );
8485}
8486
8487/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008488 * Renegotiate current connection on client,
8489 * or request renegotiation on server
8490 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008491int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008492{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008493 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008494
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008495 if( ssl == NULL || ssl->conf == NULL )
8496 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008498#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008499 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008500 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008502 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8503 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008505 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008506
8507 /* Did we already try/start sending HelloRequest? */
8508 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008509 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008510
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008511 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008512 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008513#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008515#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008516 /*
8517 * On client, either start the renegotiation process or,
8518 * if already in progress, continue the handshake
8519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008520 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008522 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8523 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008524
8525 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008527 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008528 return( ret );
8529 }
8530 }
8531 else
8532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008533 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008535 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008536 return( ret );
8537 }
8538 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008539#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008540
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008541 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008542}
8543
8544/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008545 * Check record counters and renegotiate if they're above the limit.
8546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008547static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008548{
Andres AG2196c7f2016-12-15 17:01:16 +00008549 size_t ep_len = ssl_ep_len( ssl );
8550 int in_ctr_cmp;
8551 int out_ctr_cmp;
8552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008553 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8554 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008555 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008556 {
8557 return( 0 );
8558 }
8559
Andres AG2196c7f2016-12-15 17:01:16 +00008560 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8561 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008562 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008563 ssl->conf->renego_period + ep_len, 8 - ep_len );
8564
8565 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008566 {
8567 return( 0 );
8568 }
8569
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008571 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008572}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008573#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008574
8575/*
8576 * Receive application data decrypted from the SSL layer
8577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008578int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008579{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008580 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008581 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008582
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008583 if( ssl == NULL || ssl->conf == NULL )
8584 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008588#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008589 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008591 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008592 return( ret );
8593
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008594 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008595 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008596 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008597 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008598 return( ret );
8599 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008600 }
8601#endif
8602
Hanno Becker4a810fb2017-05-24 16:27:30 +01008603 /*
8604 * Check if renegotiation is necessary and/or handshake is
8605 * in process. If yes, perform/continue, and fall through
8606 * if an unexpected packet is received while the client
8607 * is waiting for the ServerHello.
8608 *
8609 * (There is no equivalent to the last condition on
8610 * the server-side as it is not treated as within
8611 * a handshake while waiting for the ClientHello
8612 * after a renegotiation request.)
8613 */
8614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008615#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008616 ret = ssl_check_ctr_renegotiate( ssl );
8617 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8618 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008620 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008621 return( ret );
8622 }
8623#endif
8624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008625 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008627 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008628 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8629 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008631 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008632 return( ret );
8633 }
8634 }
8635
Hanno Beckere41158b2017-10-23 13:30:32 +01008636 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008637 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008638 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008639 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008640 if( ssl->f_get_timer != NULL &&
8641 ssl->f_get_timer( ssl->p_timer ) == -1 )
8642 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008643 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008644 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008645
Hanno Becker327c93b2018-08-15 13:56:18 +01008646 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008647 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008648 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8649 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008650
Hanno Becker4a810fb2017-05-24 16:27:30 +01008651 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8652 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008653 }
8654
8655 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008656 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008657 {
8658 /*
8659 * OpenSSL sends empty messages to randomize the IV
8660 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008661 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008663 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008664 return( 0 );
8665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008666 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008667 return( ret );
8668 }
8669 }
8670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008671 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008674
Hanno Becker4a810fb2017-05-24 16:27:30 +01008675 /*
8676 * - For client-side, expect SERVER_HELLO_REQUEST.
8677 * - For server-side, expect CLIENT_HELLO.
8678 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8679 */
8680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008681#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008682 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008683 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008684 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008687
8688 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008689#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008690 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008691 {
8692 continue;
8693 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008694#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008695 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008696 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008697#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008698
Hanno Becker4a810fb2017-05-24 16:27:30 +01008699#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008700 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008701 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008704
8705 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008706#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008707 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008708 {
8709 continue;
8710 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008711#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008712 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008713 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008714#endif /* MBEDTLS_SSL_SRV_C */
8715
Hanno Becker21df7f92017-10-17 11:03:26 +01008716#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008717 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008718 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8719 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8720 ssl->conf->allow_legacy_renegotiation ==
8721 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8722 {
8723 /*
8724 * Accept renegotiation request
8725 */
Paul Bakker48916f92012-09-16 19:57:18 +00008726
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008727 /* DTLS clients need to know renego is server-initiated */
8728#if defined(MBEDTLS_SSL_PROTO_DTLS)
8729 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8730 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8731 {
8732 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8733 }
8734#endif
8735 ret = ssl_start_renegotiation( ssl );
8736 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8737 ret != 0 )
8738 {
8739 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8740 return( ret );
8741 }
8742 }
8743 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008744#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008745 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008746 /*
8747 * Refuse renegotiation
8748 */
8749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008750 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008752#if defined(MBEDTLS_SSL_PROTO_SSL3)
8753 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008754 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008755 /* SSLv3 does not have a "no_renegotiation" warning, so
8756 we send a fatal alert and abort the connection. */
8757 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8758 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8759 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008760 }
8761 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008762#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8763#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8764 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8765 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008767 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8768 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8769 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008770 {
8771 return( ret );
8772 }
Paul Bakker48916f92012-09-16 19:57:18 +00008773 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008774 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008775#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8776 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8779 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008780 }
Paul Bakker48916f92012-09-16 19:57:18 +00008781 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008782
Hanno Becker90333da2017-10-10 11:27:13 +01008783 /* At this point, we don't know whether the renegotiation has been
8784 * completed or not. The cases to consider are the following:
8785 * 1) The renegotiation is complete. In this case, no new record
8786 * has been read yet.
8787 * 2) The renegotiation is incomplete because the client received
8788 * an application data record while awaiting the ServerHello.
8789 * 3) The renegotiation is incomplete because the client received
8790 * a non-handshake, non-application data message while awaiting
8791 * the ServerHello.
8792 * In each of these case, looping will be the proper action:
8793 * - For 1), the next iteration will read a new record and check
8794 * if it's application data.
8795 * - For 2), the loop condition isn't satisfied as application data
8796 * is present, hence continue is the same as break
8797 * - For 3), the loop condition is satisfied and read_record
8798 * will re-deliver the message that was held back by the client
8799 * when expecting the ServerHello.
8800 */
8801 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008802 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008803#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008804 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008805 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008806 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008807 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008808 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008811 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008812 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008813 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008814 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008815 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008816#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008818 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8819 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008822 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008823 }
8824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008825 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8828 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008829 }
8830
8831 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008832
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008833 /* We're going to return something now, cancel timer,
8834 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008835 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008836 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008837
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008838#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008839 /* If we requested renego but received AppData, resend HelloRequest.
8840 * Do it now, after setting in_offt, to avoid taking this branch
8841 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008842#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008843 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008844 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008845 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008846 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008848 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008849 return( ret );
8850 }
8851 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008852#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008853#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008854 }
8855
8856 n = ( len < ssl->in_msglen )
8857 ? len : ssl->in_msglen;
8858
8859 memcpy( buf, ssl->in_offt, n );
8860 ssl->in_msglen -= n;
8861
8862 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008863 {
8864 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008865 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008866 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008867 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008868 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008869 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008870 /* more data available */
8871 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008872 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008875
Paul Bakker23986e52011-04-24 08:57:21 +00008876 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008877}
8878
8879/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008880 * Send application data to be encrypted by the SSL layer, taking care of max
8881 * fragment length and buffer size.
8882 *
8883 * According to RFC 5246 Section 6.2.1:
8884 *
8885 * Zero-length fragments of Application data MAY be sent as they are
8886 * potentially useful as a traffic analysis countermeasure.
8887 *
8888 * Therefore, it is possible that the input message length is 0 and the
8889 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008890 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008891static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008892 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008893{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008894 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8895 const size_t max_len = (size_t) ret;
8896
8897 if( ret < 0 )
8898 {
8899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8900 return( ret );
8901 }
8902
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008903 if( len > max_len )
8904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008905#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008906 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008909 "maximum fragment length: %d > %d",
8910 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008911 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008912 }
8913 else
8914#endif
8915 len = max_len;
8916 }
Paul Bakker887bd502011-06-08 13:10:54 +00008917
Paul Bakker5121ce52009-01-03 21:22:43 +00008918 if( ssl->out_left != 0 )
8919 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008920 /*
8921 * The user has previously tried to send the data and
8922 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8923 * written. In this case, we expect the high-level write function
8924 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8925 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008926 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008928 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008929 return( ret );
8930 }
8931 }
Paul Bakker887bd502011-06-08 13:10:54 +00008932 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008933 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008934 /*
8935 * The user is trying to send a message the first time, so we need to
8936 * copy the data into the internal buffers and setup the data structure
8937 * to keep track of partial writes
8938 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008939 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008940 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008941 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008942
Hanno Becker67bc7c32018-08-06 11:33:50 +01008943 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008945 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008946 return( ret );
8947 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008948 }
8949
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008950 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008951}
8952
8953/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008954 * Write application data, doing 1/n-1 splitting if necessary.
8955 *
8956 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008957 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008958 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008959 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008960#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008961static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008962 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008963{
8964 int ret;
8965
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008966 if( ssl->conf->cbc_record_splitting ==
8967 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008968 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008969 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8970 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8971 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008972 {
8973 return( ssl_write_real( ssl, buf, len ) );
8974 }
8975
8976 if( ssl->split_done == 0 )
8977 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008978 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008979 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008980 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008981 }
8982
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008983 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8984 return( ret );
8985 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008986
8987 return( ret + 1 );
8988}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008989#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008990
8991/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008992 * Write application data (public-facing wrapper)
8993 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008994int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008995{
8996 int ret;
8997
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008999
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009000 if( ssl == NULL || ssl->conf == NULL )
9001 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9002
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009003#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009004 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9005 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009006 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009007 return( ret );
9008 }
9009#endif
9010
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009011 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009012 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009013 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009014 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009015 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009016 return( ret );
9017 }
9018 }
9019
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009020#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009021 ret = ssl_write_split( ssl, buf, len );
9022#else
9023 ret = ssl_write_real( ssl, buf, len );
9024#endif
9025
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009027
9028 return( ret );
9029}
9030
9031/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009032 * Notify the peer that the connection is being closed
9033 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009034int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009035{
9036 int ret;
9037
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009038 if( ssl == NULL || ssl->conf == NULL )
9039 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009042
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009043 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009044 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009046 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009048 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9049 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9050 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009052 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009053 return( ret );
9054 }
9055 }
9056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009058
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009059 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009060}
9061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009062void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009063{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009064 if( transform == NULL )
9065 return;
9066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009067#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009068 deflateEnd( &transform->ctx_deflate );
9069 inflateEnd( &transform->ctx_inflate );
9070#endif
9071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009072 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9073 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009075 mbedtls_md_free( &transform->md_ctx_enc );
9076 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009077
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009078 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009079}
9080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009081#if defined(MBEDTLS_X509_CRT_PARSE_C)
9082static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009083{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009084 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009085
9086 while( cur != NULL )
9087 {
9088 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009089 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009090 cur = next;
9091 }
9092}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009093#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009094
Hanno Becker0271f962018-08-16 13:23:47 +01009095#if defined(MBEDTLS_SSL_PROTO_DTLS)
9096
9097static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9098{
9099 unsigned offset;
9100 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9101
9102 if( hs == NULL )
9103 return;
9104
Hanno Becker283f5ef2018-08-24 09:34:47 +01009105 ssl_free_buffered_record( ssl );
9106
Hanno Becker0271f962018-08-16 13:23:47 +01009107 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009108 ssl_buffering_free_slot( ssl, offset );
9109}
9110
9111static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9112 uint8_t slot )
9113{
9114 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9115 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009116
9117 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9118 return;
9119
Hanno Beckere605b192018-08-21 15:59:07 +01009120 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009121 {
Hanno Beckere605b192018-08-21 15:59:07 +01009122 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009123 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009124 mbedtls_free( hs_buf->data );
9125 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009126 }
9127}
9128
9129#endif /* MBEDTLS_SSL_PROTO_DTLS */
9130
Gilles Peskine9b562d52018-04-25 20:32:43 +02009131void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009132{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009133 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9134
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009135 if( handshake == NULL )
9136 return;
9137
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009138#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9139 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9140 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009141 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009142 handshake->async_in_progress = 0;
9143 }
9144#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9145
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009146#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9147 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9148 mbedtls_md5_free( &handshake->fin_md5 );
9149 mbedtls_sha1_free( &handshake->fin_sha1 );
9150#endif
9151#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9152#if defined(MBEDTLS_SHA256_C)
9153 mbedtls_sha256_free( &handshake->fin_sha256 );
9154#endif
9155#if defined(MBEDTLS_SHA512_C)
9156 mbedtls_sha512_free( &handshake->fin_sha512 );
9157#endif
9158#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009160#if defined(MBEDTLS_DHM_C)
9161 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009162#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009163#if defined(MBEDTLS_ECDH_C)
9164 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009165#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009166#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009167 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009168#if defined(MBEDTLS_SSL_CLI_C)
9169 mbedtls_free( handshake->ecjpake_cache );
9170 handshake->ecjpake_cache = NULL;
9171 handshake->ecjpake_cache_len = 0;
9172#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009173#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009174
Janos Follath4ae5c292016-02-10 11:27:43 +00009175#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9176 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009177 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009178 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009179#endif
9180
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009181#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9182 if( handshake->psk != NULL )
9183 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009184 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009185 mbedtls_free( handshake->psk );
9186 }
9187#endif
9188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009189#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9190 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009191 /*
9192 * Free only the linked list wrapper, not the keys themselves
9193 * since the belong to the SNI callback
9194 */
9195 if( handshake->sni_key_cert != NULL )
9196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009197 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009198
9199 while( cur != NULL )
9200 {
9201 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009202 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009203 cur = next;
9204 }
9205 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009206#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009207
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009208#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009209 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009210#endif
9211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009212#if defined(MBEDTLS_SSL_PROTO_DTLS)
9213 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009214 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009215 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009216#endif
9217
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009218 mbedtls_platform_zeroize( handshake,
9219 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009220}
9221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009222void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009223{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009224 if( session == NULL )
9225 return;
9226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009227#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00009228 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00009229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009230 mbedtls_x509_crt_free( session->peer_cert );
9231 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00009232 }
Paul Bakkered27a042013-04-18 22:46:23 +02009233#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009234
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009235#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009236 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009237#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009238
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009239 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009240}
9241
Paul Bakker5121ce52009-01-03 21:22:43 +00009242/*
9243 * Free an SSL context
9244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009245void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009246{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009247 if( ssl == NULL )
9248 return;
9249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009251
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009252 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009253 {
Angus Grattond8213d02016-05-25 20:56:48 +10009254 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009255 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009256 }
9257
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009258 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009259 {
Angus Grattond8213d02016-05-25 20:56:48 +10009260 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009261 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009262 }
9263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009264#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009265 if( ssl->compress_buf != NULL )
9266 {
Angus Grattond8213d02016-05-25 20:56:48 +10009267 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009268 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009269 }
9270#endif
9271
Paul Bakker48916f92012-09-16 19:57:18 +00009272 if( ssl->transform )
9273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009274 mbedtls_ssl_transform_free( ssl->transform );
9275 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009276 }
9277
9278 if( ssl->handshake )
9279 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009280 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009281 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9282 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009284 mbedtls_free( ssl->handshake );
9285 mbedtls_free( ssl->transform_negotiate );
9286 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009287 }
9288
Paul Bakkerc0463502013-02-14 11:19:38 +01009289 if( ssl->session )
9290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009291 mbedtls_ssl_session_free( ssl->session );
9292 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009293 }
9294
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009295#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009296 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009297 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009298 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009299 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009300 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009301#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009303#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9304 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9307 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009308 }
9309#endif
9310
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009311#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009312 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009313#endif
9314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009316
Paul Bakker86f04f42013-02-14 11:20:09 +01009317 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009318 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009319}
9320
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009321/*
9322 * Initialze mbedtls_ssl_config
9323 */
9324void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9325{
9326 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9327}
9328
Simon Butcherc97b6972015-12-27 23:48:17 +00009329#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009330static int ssl_preset_default_hashes[] = {
9331#if defined(MBEDTLS_SHA512_C)
9332 MBEDTLS_MD_SHA512,
9333 MBEDTLS_MD_SHA384,
9334#endif
9335#if defined(MBEDTLS_SHA256_C)
9336 MBEDTLS_MD_SHA256,
9337 MBEDTLS_MD_SHA224,
9338#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009339#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009340 MBEDTLS_MD_SHA1,
9341#endif
9342 MBEDTLS_MD_NONE
9343};
Simon Butcherc97b6972015-12-27 23:48:17 +00009344#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009345
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009346static int ssl_preset_suiteb_ciphersuites[] = {
9347 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9348 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9349 0
9350};
9351
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009352#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009353static int ssl_preset_suiteb_hashes[] = {
9354 MBEDTLS_MD_SHA256,
9355 MBEDTLS_MD_SHA384,
9356 MBEDTLS_MD_NONE
9357};
9358#endif
9359
9360#if defined(MBEDTLS_ECP_C)
9361static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9362 MBEDTLS_ECP_DP_SECP256R1,
9363 MBEDTLS_ECP_DP_SECP384R1,
9364 MBEDTLS_ECP_DP_NONE
9365};
9366#endif
9367
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009368/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009369 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009370 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009371int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009372 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009373{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009374#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009375 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009376#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009377
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009378 /* Use the functions here so that they are covered in tests,
9379 * but otherwise access member directly for efficiency */
9380 mbedtls_ssl_conf_endpoint( conf, endpoint );
9381 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009382
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009383 /*
9384 * Things that are common to all presets
9385 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009386#if defined(MBEDTLS_SSL_CLI_C)
9387 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9388 {
9389 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9390#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9391 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9392#endif
9393 }
9394#endif
9395
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009396#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009397 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009398#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009399
9400#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9401 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9402#endif
9403
9404#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9405 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9406#endif
9407
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009408#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9409 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9410#endif
9411
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009412#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009413 conf->f_cookie_write = ssl_cookie_write_dummy;
9414 conf->f_cookie_check = ssl_cookie_check_dummy;
9415#endif
9416
9417#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9418 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9419#endif
9420
Janos Follath088ce432017-04-10 12:42:31 +01009421#if defined(MBEDTLS_SSL_SRV_C)
9422 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9423#endif
9424
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009425#if defined(MBEDTLS_SSL_PROTO_DTLS)
9426 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9427 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9428#endif
9429
9430#if defined(MBEDTLS_SSL_RENEGOTIATION)
9431 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009432 memset( conf->renego_period, 0x00, 2 );
9433 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009434#endif
9435
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009436#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9437 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9438 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009439 const unsigned char dhm_p[] =
9440 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9441 const unsigned char dhm_g[] =
9442 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9443
Hanno Beckera90658f2017-10-04 15:29:08 +01009444 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9445 dhm_p, sizeof( dhm_p ),
9446 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009447 {
9448 return( ret );
9449 }
9450 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009451#endif
9452
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009453 /*
9454 * Preset-specific defaults
9455 */
9456 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009457 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009458 /*
9459 * NSA Suite B
9460 */
9461 case MBEDTLS_SSL_PRESET_SUITEB:
9462 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9463 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9464 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9465 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9466
9467 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9468 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9469 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9470 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9471 ssl_preset_suiteb_ciphersuites;
9472
9473#if defined(MBEDTLS_X509_CRT_PARSE_C)
9474 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009475#endif
9476
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009477#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009478 conf->sig_hashes = ssl_preset_suiteb_hashes;
9479#endif
9480
9481#if defined(MBEDTLS_ECP_C)
9482 conf->curve_list = ssl_preset_suiteb_curves;
9483#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009484 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009485
9486 /*
9487 * Default
9488 */
9489 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009490 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9491 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9492 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9493 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9494 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9495 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9496 MBEDTLS_SSL_MIN_MINOR_VERSION :
9497 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009498 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9499 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9500
9501#if defined(MBEDTLS_SSL_PROTO_DTLS)
9502 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9503 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9504#endif
9505
9506 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9507 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9508 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9509 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9510 mbedtls_ssl_list_ciphersuites();
9511
9512#if defined(MBEDTLS_X509_CRT_PARSE_C)
9513 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9514#endif
9515
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009516#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009517 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009518#endif
9519
9520#if defined(MBEDTLS_ECP_C)
9521 conf->curve_list = mbedtls_ecp_grp_id_list();
9522#endif
9523
9524#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9525 conf->dhm_min_bitlen = 1024;
9526#endif
9527 }
9528
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009529 return( 0 );
9530}
9531
9532/*
9533 * Free mbedtls_ssl_config
9534 */
9535void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9536{
9537#if defined(MBEDTLS_DHM_C)
9538 mbedtls_mpi_free( &conf->dhm_P );
9539 mbedtls_mpi_free( &conf->dhm_G );
9540#endif
9541
9542#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9543 if( conf->psk != NULL )
9544 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009545 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009546 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009547 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009548 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009549 }
9550
9551 if( conf->psk_identity != NULL )
9552 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009553 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009554 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009555 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009556 conf->psk_identity_len = 0;
9557 }
9558#endif
9559
9560#if defined(MBEDTLS_X509_CRT_PARSE_C)
9561 ssl_key_cert_free( conf->key_cert );
9562#endif
9563
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009564 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009565}
9566
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009567#if defined(MBEDTLS_PK_C) && \
9568 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009569/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009570 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009572unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009573{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009574#if defined(MBEDTLS_RSA_C)
9575 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9576 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009577#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009578#if defined(MBEDTLS_ECDSA_C)
9579 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9580 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009581#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009582 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009583}
9584
Hanno Becker7e5437a2017-04-28 17:15:26 +01009585unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9586{
9587 switch( type ) {
9588 case MBEDTLS_PK_RSA:
9589 return( MBEDTLS_SSL_SIG_RSA );
9590 case MBEDTLS_PK_ECDSA:
9591 case MBEDTLS_PK_ECKEY:
9592 return( MBEDTLS_SSL_SIG_ECDSA );
9593 default:
9594 return( MBEDTLS_SSL_SIG_ANON );
9595 }
9596}
9597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009598mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009599{
9600 switch( sig )
9601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009602#if defined(MBEDTLS_RSA_C)
9603 case MBEDTLS_SSL_SIG_RSA:
9604 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009605#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009606#if defined(MBEDTLS_ECDSA_C)
9607 case MBEDTLS_SSL_SIG_ECDSA:
9608 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009609#endif
9610 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009611 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009612 }
9613}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009614#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009615
Hanno Becker7e5437a2017-04-28 17:15:26 +01009616#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9617 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9618
9619/* Find an entry in a signature-hash set matching a given hash algorithm. */
9620mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9621 mbedtls_pk_type_t sig_alg )
9622{
9623 switch( sig_alg )
9624 {
9625 case MBEDTLS_PK_RSA:
9626 return( set->rsa );
9627 case MBEDTLS_PK_ECDSA:
9628 return( set->ecdsa );
9629 default:
9630 return( MBEDTLS_MD_NONE );
9631 }
9632}
9633
9634/* Add a signature-hash-pair to a signature-hash set */
9635void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9636 mbedtls_pk_type_t sig_alg,
9637 mbedtls_md_type_t md_alg )
9638{
9639 switch( sig_alg )
9640 {
9641 case MBEDTLS_PK_RSA:
9642 if( set->rsa == MBEDTLS_MD_NONE )
9643 set->rsa = md_alg;
9644 break;
9645
9646 case MBEDTLS_PK_ECDSA:
9647 if( set->ecdsa == MBEDTLS_MD_NONE )
9648 set->ecdsa = md_alg;
9649 break;
9650
9651 default:
9652 break;
9653 }
9654}
9655
9656/* Allow exactly one hash algorithm for each signature. */
9657void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9658 mbedtls_md_type_t md_alg )
9659{
9660 set->rsa = md_alg;
9661 set->ecdsa = md_alg;
9662}
9663
9664#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9665 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9666
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009667/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009668 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009670mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009671{
9672 switch( hash )
9673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674#if defined(MBEDTLS_MD5_C)
9675 case MBEDTLS_SSL_HASH_MD5:
9676 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009677#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009678#if defined(MBEDTLS_SHA1_C)
9679 case MBEDTLS_SSL_HASH_SHA1:
9680 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009681#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009682#if defined(MBEDTLS_SHA256_C)
9683 case MBEDTLS_SSL_HASH_SHA224:
9684 return( MBEDTLS_MD_SHA224 );
9685 case MBEDTLS_SSL_HASH_SHA256:
9686 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009687#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009688#if defined(MBEDTLS_SHA512_C)
9689 case MBEDTLS_SSL_HASH_SHA384:
9690 return( MBEDTLS_MD_SHA384 );
9691 case MBEDTLS_SSL_HASH_SHA512:
9692 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009693#endif
9694 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009695 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009696 }
9697}
9698
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009699/*
9700 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9701 */
9702unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9703{
9704 switch( md )
9705 {
9706#if defined(MBEDTLS_MD5_C)
9707 case MBEDTLS_MD_MD5:
9708 return( MBEDTLS_SSL_HASH_MD5 );
9709#endif
9710#if defined(MBEDTLS_SHA1_C)
9711 case MBEDTLS_MD_SHA1:
9712 return( MBEDTLS_SSL_HASH_SHA1 );
9713#endif
9714#if defined(MBEDTLS_SHA256_C)
9715 case MBEDTLS_MD_SHA224:
9716 return( MBEDTLS_SSL_HASH_SHA224 );
9717 case MBEDTLS_MD_SHA256:
9718 return( MBEDTLS_SSL_HASH_SHA256 );
9719#endif
9720#if defined(MBEDTLS_SHA512_C)
9721 case MBEDTLS_MD_SHA384:
9722 return( MBEDTLS_SSL_HASH_SHA384 );
9723 case MBEDTLS_MD_SHA512:
9724 return( MBEDTLS_SSL_HASH_SHA512 );
9725#endif
9726 default:
9727 return( MBEDTLS_SSL_HASH_NONE );
9728 }
9729}
9730
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009731#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009732/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009733 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009734 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009735 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009736int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009737{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009738 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009739
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009740 if( ssl->conf->curve_list == NULL )
9741 return( -1 );
9742
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009743 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009744 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009745 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009746
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009747 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009748}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009749#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009750
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009751#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009752/*
9753 * Check if a hash proposed by the peer is in our list.
9754 * Return 0 if we're willing to use it, -1 otherwise.
9755 */
9756int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9757 mbedtls_md_type_t md )
9758{
9759 const int *cur;
9760
9761 if( ssl->conf->sig_hashes == NULL )
9762 return( -1 );
9763
9764 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9765 if( *cur == (int) md )
9766 return( 0 );
9767
9768 return( -1 );
9769}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009770#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009772#if defined(MBEDTLS_X509_CRT_PARSE_C)
9773int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9774 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009775 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009776 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009777{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009778 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009779#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009780 int usage = 0;
9781#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009782#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009783 const char *ext_oid;
9784 size_t ext_len;
9785#endif
9786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009787#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9788 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009789 ((void) cert);
9790 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009791 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009792#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009794#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9795 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009796 {
9797 /* Server part of the key exchange */
9798 switch( ciphersuite->key_exchange )
9799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009800 case MBEDTLS_KEY_EXCHANGE_RSA:
9801 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009802 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009803 break;
9804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009805 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9806 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9807 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9808 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009809 break;
9810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009811 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9812 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009813 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009814 break;
9815
9816 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009817 case MBEDTLS_KEY_EXCHANGE_NONE:
9818 case MBEDTLS_KEY_EXCHANGE_PSK:
9819 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9820 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009821 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009822 usage = 0;
9823 }
9824 }
9825 else
9826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9828 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009829 }
9830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009831 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009832 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009833 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009834 ret = -1;
9835 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009836#else
9837 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009838#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009840#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9841 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9844 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009845 }
9846 else
9847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009848 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9849 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009850 }
9851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009853 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009854 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009855 ret = -1;
9856 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009857#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009858
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009859 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009860}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009861#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009862
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009863/*
9864 * Convert version numbers to/from wire format
9865 * and, for DTLS, to/from TLS equivalent.
9866 *
9867 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009868 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009869 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9870 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9871 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009872void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009873 unsigned char ver[2] )
9874{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009875#if defined(MBEDTLS_SSL_PROTO_DTLS)
9876 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009878 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009879 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9880
9881 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9882 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9883 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009884 else
9885#else
9886 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009887#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009888 {
9889 ver[0] = (unsigned char) major;
9890 ver[1] = (unsigned char) minor;
9891 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009892}
9893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009894void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009895 const unsigned char ver[2] )
9896{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009897#if defined(MBEDTLS_SSL_PROTO_DTLS)
9898 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009899 {
9900 *major = 255 - ver[0] + 2;
9901 *minor = 255 - ver[1] + 1;
9902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009903 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009904 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9905 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009906 else
9907#else
9908 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009909#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009910 {
9911 *major = ver[0];
9912 *minor = ver[1];
9913 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009914}
9915
Simon Butcher99000142016-10-13 17:21:01 +01009916int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9917{
9918#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9919 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9920 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9921
9922 switch( md )
9923 {
9924#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9925#if defined(MBEDTLS_MD5_C)
9926 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009927 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009928#endif
9929#if defined(MBEDTLS_SHA1_C)
9930 case MBEDTLS_SSL_HASH_SHA1:
9931 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9932 break;
9933#endif
9934#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9935#if defined(MBEDTLS_SHA512_C)
9936 case MBEDTLS_SSL_HASH_SHA384:
9937 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9938 break;
9939#endif
9940#if defined(MBEDTLS_SHA256_C)
9941 case MBEDTLS_SSL_HASH_SHA256:
9942 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9943 break;
9944#endif
9945 default:
9946 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9947 }
9948
9949 return 0;
9950#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9951 (void) ssl;
9952 (void) md;
9953
9954 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9955#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9956}
9957
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009958#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9959 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9960int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9961 unsigned char *output,
9962 unsigned char *data, size_t data_len )
9963{
9964 int ret = 0;
9965 mbedtls_md5_context mbedtls_md5;
9966 mbedtls_sha1_context mbedtls_sha1;
9967
9968 mbedtls_md5_init( &mbedtls_md5 );
9969 mbedtls_sha1_init( &mbedtls_sha1 );
9970
9971 /*
9972 * digitally-signed struct {
9973 * opaque md5_hash[16];
9974 * opaque sha_hash[20];
9975 * };
9976 *
9977 * md5_hash
9978 * MD5(ClientHello.random + ServerHello.random
9979 * + ServerParams);
9980 * sha_hash
9981 * SHA(ClientHello.random + ServerHello.random
9982 * + ServerParams);
9983 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009984 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009985 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009986 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009987 goto exit;
9988 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009989 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009990 ssl->handshake->randbytes, 64 ) ) != 0 )
9991 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009992 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009993 goto exit;
9994 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009995 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009996 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009997 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009998 goto exit;
9999 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010000 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010001 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010002 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010003 goto exit;
10004 }
10005
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010006 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010007 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010008 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010009 goto exit;
10010 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010011 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010012 ssl->handshake->randbytes, 64 ) ) != 0 )
10013 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010014 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010015 goto exit;
10016 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010017 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010018 data_len ) ) != 0 )
10019 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010020 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010021 goto exit;
10022 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010023 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010024 output + 16 ) ) != 0 )
10025 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010026 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010027 goto exit;
10028 }
10029
10030exit:
10031 mbedtls_md5_free( &mbedtls_md5 );
10032 mbedtls_sha1_free( &mbedtls_sha1 );
10033
10034 if( ret != 0 )
10035 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10036 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10037
10038 return( ret );
10039
10040}
10041#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10042 MBEDTLS_SSL_PROTO_TLS1_1 */
10043
10044#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10045 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10046int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010047 unsigned char *hash, size_t *hashlen,
10048 unsigned char *data, size_t data_len,
10049 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010050{
10051 int ret = 0;
10052 mbedtls_md_context_t ctx;
10053 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010054 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010055
10056 mbedtls_md_init( &ctx );
10057
10058 /*
10059 * digitally-signed struct {
10060 * opaque client_random[32];
10061 * opaque server_random[32];
10062 * ServerDHParams params;
10063 * };
10064 */
10065 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10066 {
10067 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10068 goto exit;
10069 }
10070 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10071 {
10072 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10073 goto exit;
10074 }
10075 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10076 {
10077 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10078 goto exit;
10079 }
10080 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10081 {
10082 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10083 goto exit;
10084 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010085 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010086 {
10087 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10088 goto exit;
10089 }
10090
10091exit:
10092 mbedtls_md_free( &ctx );
10093
10094 if( ret != 0 )
10095 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10096 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10097
10098 return( ret );
10099}
10100#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10101 MBEDTLS_SSL_PROTO_TLS1_2 */
10102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010103#endif /* MBEDTLS_SSL_TLS_C */