blob: b671c14ac0af203b8f2432fc140ff5203ee0086d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Janos Follath23bdca02016-10-07 14:47:14 +010053#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020055#endif
56
Hanno Becker2a43f6f2018-08-10 11:12:52 +010057static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010058static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010059
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010060/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010062{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020064 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010065 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010066#else
67 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010068#endif
69 return( 0 );
70}
71
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020072/*
73 * Start a timer.
74 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020077{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020078 if( ssl->f_set_timer == NULL )
79 return;
80
81 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
82 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083}
84
85/*
86 * Return -1 is timer is expired, 0 if it isn't.
87 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020090 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020091 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020092
93 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020094 {
95 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020096 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020097 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098
99 return( 0 );
100}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200101
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100102static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
103 mbedtls_ssl_transform *transform );
104static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
105 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100106
107#define SSL_DONT_FORCE_FLUSH 0
108#define SSL_FORCE_FLUSH 1
109
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200110#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100111
Hanno Beckerd5847772018-08-28 10:09:23 +0100112/* Forward declarations for functions related to message buffering. */
113static void ssl_buffering_free( mbedtls_ssl_context *ssl );
114static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
115 uint8_t slot );
116static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
117static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
118static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
119static int ssl_buffer_message( mbedtls_ssl_context *ssl );
120static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100121static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100122
Hanno Beckera67dee22018-08-22 10:05:20 +0100123static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100124static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100125{
Hanno Becker11682cc2018-08-22 14:41:02 +0100126 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100127
128 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100129 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100130
131 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
132}
133
Hanno Becker67bc7c32018-08-06 11:33:50 +0100134static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
135{
Hanno Becker11682cc2018-08-22 14:41:02 +0100136 size_t const bytes_written = ssl->out_left;
137 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100138
139 /* Double-check that the write-index hasn't gone
140 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100141 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100142 {
143 /* Should never happen... */
144 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
145 }
146
147 return( (int) ( mtu - bytes_written ) );
148}
149
150static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
151{
152 int ret;
153 size_t remaining, expansion;
154 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
155
156#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
157 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
158
159 if( max_len > mfl )
160 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100161
162 /* By the standard (RFC 6066 Sect. 4), the MFL extension
163 * only limits the maximum record payload size, so in theory
164 * we would be allowed to pack multiple records of payload size
165 * MFL into a single datagram. However, this would mean that there's
166 * no way to explicitly communicate MTU restrictions to the peer.
167 *
168 * The following reduction of max_len makes sure that we never
169 * write datagrams larger than MFL + Record Expansion Overhead.
170 */
171 if( max_len <= ssl->out_left )
172 return( 0 );
173
174 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100175#endif
176
177 ret = ssl_get_remaining_space_in_datagram( ssl );
178 if( ret < 0 )
179 return( ret );
180 remaining = (size_t) ret;
181
182 ret = mbedtls_ssl_get_record_expansion( ssl );
183 if( ret < 0 )
184 return( ret );
185 expansion = (size_t) ret;
186
187 if( remaining <= expansion )
188 return( 0 );
189
190 remaining -= expansion;
191 if( remaining >= max_len )
192 remaining = max_len;
193
194 return( (int) remaining );
195}
196
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200197/*
198 * Double the retransmit timeout value, within the allowed range,
199 * returning -1 if the maximum value has already been reached.
200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200202{
203 uint32_t new_timeout;
204
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200205 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200206 return( -1 );
207
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200208 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
209 * in the following way: after the initial transmission and a first
210 * retransmission, back off to a temporary estimated MTU of 508 bytes.
211 * This value is guaranteed to be deliverable (if not guaranteed to be
212 * delivered) of any compliant IPv4 (and IPv6) network, and should work
213 * on most non-IP stacks too. */
214 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
215 ssl->handshake->mtu = 508;
216
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200217 new_timeout = 2 * ssl->handshake->retransmit_timeout;
218
219 /* Avoid arithmetic overflow and range overflow */
220 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200221 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200222 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200223 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200224 }
225
226 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200228 ssl->handshake->retransmit_timeout ) );
229
230 return( 0 );
231}
232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200234{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200235 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200237 ssl->handshake->retransmit_timeout ) );
238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200242/*
243 * Convert max_fragment_length codes to length.
244 * RFC 6066 says:
245 * enum{
246 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
247 * } MaxFragmentLength;
248 * and we add 0 -> extension unused
249 */
Angus Grattond8213d02016-05-25 20:56:48 +1000250static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200251{
Angus Grattond8213d02016-05-25 20:56:48 +1000252 switch( mfl )
253 {
254 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
255 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
256 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
257 return 512;
258 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
259 return 1024;
260 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
261 return 2048;
262 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
263 return 4096;
264 default:
265 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
266 }
267}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200269
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200270#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200272{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 mbedtls_ssl_session_free( dst );
274 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200277 if( src->peer_cert != NULL )
278 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200279 int ret;
280
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200281 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200282 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200283 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200288 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200291 dst->peer_cert = NULL;
292 return( ret );
293 }
294 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200296
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200297#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200298 if( src->ticket != NULL )
299 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200300 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200301 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200302 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303
304 memcpy( dst->ticket, src->ticket, src->ticket_len );
305 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200306#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200307
308 return( 0 );
309}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200310#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
313int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200314 const unsigned char *key_enc, const unsigned char *key_dec,
315 size_t keylen,
316 const unsigned char *iv_enc, const unsigned char *iv_dec,
317 size_t ivlen,
318 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200319 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
321int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
322int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
323int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
324int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
325#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000326
Paul Bakker5121ce52009-01-03 21:22:43 +0000327/*
328 * Key material generation
329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200331static int ssl3_prf( const unsigned char *secret, size_t slen,
332 const char *label,
333 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000334 unsigned char *dstbuf, size_t dlen )
335{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100336 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000337 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200338 mbedtls_md5_context md5;
339 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000340 unsigned char padding[16];
341 unsigned char sha1sum[20];
342 ((void)label);
343
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200344 mbedtls_md5_init( &md5 );
345 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200346
Paul Bakker5f70b252012-09-13 14:23:06 +0000347 /*
348 * SSLv3:
349 * block =
350 * MD5( secret + SHA1( 'A' + secret + random ) ) +
351 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
352 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
353 * ...
354 */
355 for( i = 0; i < dlen / 16; i++ )
356 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200357 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000358
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100359 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100360 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100361 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100362 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100363 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100364 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100365 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100366 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100367 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100368 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000369
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100370 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100371 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100372 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100373 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100374 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100375 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100376 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100377 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000378 }
379
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100380exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200381 mbedtls_md5_free( &md5 );
382 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000383
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500384 mbedtls_platform_zeroize( padding, sizeof( padding ) );
385 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000386
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100387 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000388}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200392static int tls1_prf( const unsigned char *secret, size_t slen,
393 const char *label,
394 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000395 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000396{
Paul Bakker23986e52011-04-24 08:57:21 +0000397 size_t nb, hs;
398 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200399 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000400 unsigned char tmp[128];
401 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 const mbedtls_md_info_t *md_info;
403 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100404 int ret;
405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000407
408 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000410
411 hs = ( slen + 1 ) / 2;
412 S1 = secret;
413 S2 = secret + slen - hs;
414
415 nb = strlen( label );
416 memcpy( tmp + 20, label, nb );
417 memcpy( tmp + 20 + nb, random, rlen );
418 nb += rlen;
419
420 /*
421 * First compute P_md5(secret,label+random)[0..dlen]
422 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
424 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100427 return( ret );
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
430 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
431 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000432
433 for( i = 0; i < dlen; i += 16 )
434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 mbedtls_md_hmac_reset ( &md_ctx );
436 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
437 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_md_hmac_reset ( &md_ctx );
440 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
441 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000442
443 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
444
445 for( j = 0; j < k; j++ )
446 dstbuf[i + j] = h_i[j];
447 }
448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100450
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 /*
452 * XOR out with P_sha1(secret,label+random)[0..dlen]
453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
455 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100458 return( ret );
459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
461 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
462 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
464 for( i = 0; i < dlen; i += 20 )
465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_md_hmac_reset ( &md_ctx );
467 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
468 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_md_hmac_reset ( &md_ctx );
471 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
472 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
474 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
475
476 for( j = 0; j < k; j++ )
477 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
478 }
479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100481
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500482 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
483 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
485 return( 0 );
486}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
490static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100491 const unsigned char *secret, size_t slen,
492 const char *label,
493 const unsigned char *random, size_t rlen,
494 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000495{
496 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100497 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000498 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
500 const mbedtls_md_info_t *md_info;
501 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100502 int ret;
503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
507 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100510
511 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000513
514 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100515 memcpy( tmp + md_len, label, nb );
516 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000517 nb += rlen;
518
519 /*
520 * Compute P_<hash>(secret, label + random)[0..dlen]
521 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100523 return( ret );
524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
526 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
527 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100528
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100529 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 mbedtls_md_hmac_reset ( &md_ctx );
532 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
533 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_md_hmac_reset ( &md_ctx );
536 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
537 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000538
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100539 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000540
541 for( j = 0; j < k; j++ )
542 dstbuf[i + j] = h_i[j];
543 }
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100546
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500547 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
548 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000549
550 return( 0 );
551}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100554static int tls_prf_sha256( const unsigned char *secret, size_t slen,
555 const char *label,
556 const unsigned char *random, size_t rlen,
557 unsigned char *dstbuf, size_t dlen )
558{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100560 label, random, rlen, dstbuf, dlen ) );
561}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200565static int tls_prf_sha384( const unsigned char *secret, size_t slen,
566 const char *label,
567 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000568 unsigned char *dstbuf, size_t dlen )
569{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100571 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000572}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#endif /* MBEDTLS_SHA512_C */
574#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
579 defined(MBEDTLS_SSL_PROTO_TLS1_1)
580static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200581#endif
Paul Bakker380da532012-04-18 16:10:25 +0000582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583#if defined(MBEDTLS_SSL_PROTO_SSL3)
584static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
585static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200586#endif
587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
589static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
590static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200591#endif
592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
594#if defined(MBEDTLS_SHA256_C)
595static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
596static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
597static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200598#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600#if defined(MBEDTLS_SHA512_C)
601static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
602static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
603static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100604#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000608{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200609 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000610 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 unsigned char keyblk[256];
612 unsigned char *key1;
613 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100614 unsigned char *mac_enc;
615 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000616 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200617 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 const mbedtls_cipher_info_t *cipher_info;
619 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_ssl_session *session = ssl->session_negotiate;
622 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
623 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100628 if( cipher_info == NULL )
629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100631 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100633 }
634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100636 if( md_info == NULL )
637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100639 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100641 }
642
Paul Bakker5121ce52009-01-03 21:22:43 +0000643 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000644 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000645 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646#if defined(MBEDTLS_SSL_PROTO_SSL3)
647 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000648 {
Paul Bakker48916f92012-09-16 19:57:18 +0000649 handshake->tls_prf = ssl3_prf;
650 handshake->calc_verify = ssl_calc_verify_ssl;
651 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000652 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200653 else
654#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
656 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000657 {
Paul Bakker48916f92012-09-16 19:57:18 +0000658 handshake->tls_prf = tls1_prf;
659 handshake->calc_verify = ssl_calc_verify_tls;
660 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000661 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200662 else
663#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
665#if defined(MBEDTLS_SHA512_C)
666 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
667 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000668 {
Paul Bakker48916f92012-09-16 19:57:18 +0000669 handshake->tls_prf = tls_prf_sha384;
670 handshake->calc_verify = ssl_calc_verify_tls_sha384;
671 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000672 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000673 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200674#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#if defined(MBEDTLS_SHA256_C)
676 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000677 {
Paul Bakker48916f92012-09-16 19:57:18 +0000678 handshake->tls_prf = tls_prf_sha256;
679 handshake->calc_verify = ssl_calc_verify_tls_sha256;
680 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000681 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200682 else
683#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
687 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200688 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000689
690 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000691 * SSLv3:
692 * master =
693 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
694 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
695 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200696 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200697 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 * master = PRF( premaster, "master secret", randbytes )[0..47]
699 */
Paul Bakker0a597072012-09-25 21:55:46 +0000700 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000703 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
706 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200707 {
708 unsigned char session_hash[48];
709 size_t hash_len;
710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200712
713 ssl->handshake->calc_verify( ssl, session_hash );
714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
716 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200719 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200721 {
722 hash_len = 48;
723 }
724 else
725#endif
726 hash_len = 32;
727 }
728 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200730 hash_len = 36;
731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200733
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100734 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
735 "extended master secret",
736 session_hash, hash_len,
737 session->master, 48 );
738 if( ret != 0 )
739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100741 return( ret );
742 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200743
744 }
745 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200746#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100747 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
748 "master secret",
749 handshake->randbytes, 64,
750 session->master, 48 );
751 if( ret != 0 )
752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100754 return( ret );
755 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200756
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500757 mbedtls_platform_zeroize( handshake->premaster,
758 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000759 }
760 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000762
763 /*
764 * Swap the client and server random values.
765 */
Paul Bakker48916f92012-09-16 19:57:18 +0000766 memcpy( tmp, handshake->randbytes, 64 );
767 memcpy( handshake->randbytes, tmp + 32, 32 );
768 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500769 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000770
771 /*
772 * SSLv3:
773 * key block =
774 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
775 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
776 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
777 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
778 * ...
779 *
780 * TLSv1:
781 * key block = PRF( master, "key expansion", randbytes )
782 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100783 ret = handshake->tls_prf( session->master, 48, "key expansion",
784 handshake->randbytes, 64, keyblk, 256 );
785 if( ret != 0 )
786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100788 return( ret );
789 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
792 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
793 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
794 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
795 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000796
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500797 mbedtls_platform_zeroize( handshake->randbytes,
798 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000799
800 /*
801 * Determine the appropriate key, IV and MAC length.
802 */
Paul Bakker68884e32013-01-07 18:20:04 +0100803
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200804 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200807 cipher_info->mode == MBEDTLS_MODE_CCM ||
808 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000809 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200810 size_t taglen, explicit_ivlen;
811
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200812 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000813 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200814
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200815 /* All modes haves 96-bit IVs;
816 * GCM and CCM has 4 implicit and 8 explicit bytes
817 * ChachaPoly has all 12 bytes implicit
818 */
Paul Bakker68884e32013-01-07 18:20:04 +0100819 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200820 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
821 transform->fixed_ivlen = 12;
822 else
823 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200824
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200825 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
826 taglen = transform->ciphersuite_info->flags &
827 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
828
829
830 /* Minimum length of encrypted record */
831 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
832 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100833 }
834 else
835 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200836 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
838 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200841 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100842 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000843
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200844 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000845 mac_key_len = mbedtls_md_get_size( md_info );
846 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200849 /*
850 * If HMAC is to be truncated, we shall keep the leftmost bytes,
851 * (rfc 6066 page 13 or rfc 2104 section 4),
852 * so we only need to adjust the length here.
853 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000857
858#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
859 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000860 * HMAC implementation which also truncates the key
861 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000862 mac_key_len = transform->maclen;
863#endif
864 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200866
867 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100868 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000869
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200870 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200872 transform->minlen = transform->maclen;
873 else
Paul Bakker68884e32013-01-07 18:20:04 +0100874 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200875 /*
876 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100877 * 1. if EtM is in use: one block plus MAC
878 * otherwise: * first multiple of blocklen greater than maclen
879 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200880 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
882 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100883 {
884 transform->minlen = transform->maclen
885 + cipher_info->block_size;
886 }
887 else
888#endif
889 {
890 transform->minlen = transform->maclen
891 + cipher_info->block_size
892 - transform->maclen % cipher_info->block_size;
893 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
896 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
897 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200898 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100899 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200900#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
902 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
903 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200904 {
905 transform->minlen += transform->ivlen;
906 }
907 else
908#endif
909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
911 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200912 }
Paul Bakker68884e32013-01-07 18:20:04 +0100913 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000914 }
915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000917 transform->keylen, transform->minlen, transform->ivlen,
918 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000919
920 /*
921 * Finally setup the cipher contexts, IVs and MAC secrets.
922 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200924 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000925 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000926 key1 = keyblk + mac_key_len * 2;
927 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000928
Paul Bakker68884e32013-01-07 18:20:04 +0100929 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000930 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000931
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000932 /*
933 * This is not used in TLS v1.1.
934 */
Paul Bakker48916f92012-09-16 19:57:18 +0000935 iv_copy_len = ( transform->fixed_ivlen ) ?
936 transform->fixed_ivlen : transform->ivlen;
937 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
938 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000939 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000940 }
941 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#endif /* MBEDTLS_SSL_CLI_C */
943#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200944 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000945 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000946 key1 = keyblk + mac_key_len * 2 + transform->keylen;
947 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000948
Hanno Becker81c7b182017-11-09 18:39:33 +0000949 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100950 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000951
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000952 /*
953 * This is not used in TLS v1.1.
954 */
Paul Bakker48916f92012-09-16 19:57:18 +0000955 iv_copy_len = ( transform->fixed_ivlen ) ?
956 transform->fixed_ivlen : transform->ivlen;
957 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
958 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000959 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000960 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100961 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
965 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100966 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968#if defined(MBEDTLS_SSL_PROTO_SSL3)
969 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100970 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000971 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
974 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100975 }
976
Hanno Becker81c7b182017-11-09 18:39:33 +0000977 memcpy( transform->mac_enc, mac_enc, mac_key_len );
978 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100979 }
980 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981#endif /* MBEDTLS_SSL_PROTO_SSL3 */
982#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
983 defined(MBEDTLS_SSL_PROTO_TLS1_2)
984 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100985 {
Gilles Peskine039fd122018-03-19 19:06:08 +0100986 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
987 For AEAD-based ciphersuites, there is nothing to do here. */
988 if( mac_key_len != 0 )
989 {
990 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
991 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
992 }
Paul Bakker68884e32013-01-07 18:20:04 +0100993 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200994 else
995#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
998 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200999 }
Paul Bakker68884e32013-01-07 18:20:04 +01001000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1002 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001003 {
1004 int ret = 0;
1005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001009 transform->iv_enc, transform->iv_dec,
1010 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001011 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001012 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1015 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001016 }
1017 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001019
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001020#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1021 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001022 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001023 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1024 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001025 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001026 iv_copy_len );
1027 }
1028#endif
1029
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001030 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001031 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001032 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001033 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001034 return( ret );
1035 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001036
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001037 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001038 cipher_info ) ) != 0 )
1039 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001040 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001041 return( ret );
1042 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001045 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001049 return( ret );
1050 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001053 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001057 return( ret );
1058 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#if defined(MBEDTLS_CIPHER_MODE_CBC)
1061 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1064 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001067 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001068 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1071 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001074 return( ret );
1075 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001076 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001078
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001079 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001082 // Initialize compression
1083 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001085 {
Paul Bakker16770332013-10-11 09:59:44 +02001086 if( ssl->compress_buf == NULL )
1087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001089 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001090 if( ssl->compress_buf == NULL )
1091 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001093 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001094 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001095 }
1096 }
1097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001099
Paul Bakker48916f92012-09-16 19:57:18 +00001100 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1101 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001102
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001103 if( deflateInit( &transform->ctx_deflate,
1104 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001105 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1108 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001109 }
1110 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001114
1115 return( 0 );
1116}
1117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118#if defined(MBEDTLS_SSL_PROTO_SSL3)
1119void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001120{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001121 mbedtls_md5_context md5;
1122 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001123 unsigned char pad_1[48];
1124 unsigned char pad_2[48];
1125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001127
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001128 mbedtls_md5_init( &md5 );
1129 mbedtls_sha1_init( &sha1 );
1130
1131 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1132 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001133
Paul Bakker380da532012-04-18 16:10:25 +00001134 memset( pad_1, 0x36, 48 );
1135 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001136
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001137 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1138 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1139 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001140
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001141 mbedtls_md5_starts_ret( &md5 );
1142 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1143 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1144 mbedtls_md5_update_ret( &md5, hash, 16 );
1145 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001146
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001147 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1148 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1149 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001150
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001151 mbedtls_sha1_starts_ret( &sha1 );
1152 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1153 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1154 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1155 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001159
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001160 mbedtls_md5_free( &md5 );
1161 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001162
Paul Bakker380da532012-04-18 16:10:25 +00001163 return;
1164}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1168void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001169{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001170 mbedtls_md5_context md5;
1171 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001174
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001175 mbedtls_md5_init( &md5 );
1176 mbedtls_sha1_init( &sha1 );
1177
1178 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1179 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001180
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001181 mbedtls_md5_finish_ret( &md5, hash );
1182 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001186
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001187 mbedtls_md5_free( &md5 );
1188 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001189
Paul Bakker380da532012-04-18 16:10:25 +00001190 return;
1191}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1195#if defined(MBEDTLS_SHA256_C)
1196void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001197{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001198 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001199
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001200 mbedtls_sha256_init( &sha256 );
1201
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001203
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001204 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001205 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001209
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001210 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001211
Paul Bakker380da532012-04-18 16:10:25 +00001212 return;
1213}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216#if defined(MBEDTLS_SHA512_C)
1217void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001218{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001219 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001220
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001221 mbedtls_sha512_init( &sha512 );
1222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001224
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001225 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001226 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001230
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001231 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001232
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 return;
1234}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235#endif /* MBEDTLS_SHA512_C */
1236#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1239int 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 +02001240{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001241 unsigned char *p = ssl->handshake->premaster;
1242 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001243 const unsigned char *psk = ssl->conf->psk;
1244 size_t psk_len = ssl->conf->psk_len;
1245
1246 /* If the psk callback was called, use its result */
1247 if( ssl->handshake->psk != NULL )
1248 {
1249 psk = ssl->handshake->psk;
1250 psk_len = ssl->handshake->psk_len;
1251 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001252
1253 /*
1254 * PMS = struct {
1255 * opaque other_secret<0..2^16-1>;
1256 * opaque psk<0..2^16-1>;
1257 * };
1258 * with "other_secret" depending on the particular key exchange
1259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1261 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001262 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001263 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001265
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001266 *(p++) = (unsigned char)( psk_len >> 8 );
1267 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001268
1269 if( end < p || (size_t)( end - p ) < psk_len )
1270 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1271
1272 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001273 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001274 }
1275 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1277#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1278 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001279 {
1280 /*
1281 * other_secret already set by the ClientKeyExchange message,
1282 * and is 48 bytes long
1283 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001284 if( end - p < 2 )
1285 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1286
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001287 *p++ = 0;
1288 *p++ = 48;
1289 p += 48;
1290 }
1291 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1293#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1294 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001295 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001296 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001297 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001298
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001299 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001301 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001302 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001305 return( ret );
1306 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001307 *(p++) = (unsigned char)( len >> 8 );
1308 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001309 p += len;
1310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001312 }
1313 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1315#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1316 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001317 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001318 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001319 size_t zlen;
1320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001322 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001323 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001326 return( ret );
1327 }
1328
1329 *(p++) = (unsigned char)( zlen >> 8 );
1330 *(p++) = (unsigned char)( zlen );
1331 p += zlen;
1332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001334 }
1335 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1339 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001340 }
1341
1342 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001343 if( end - p < 2 )
1344 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001345
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001346 *(p++) = (unsigned char)( psk_len >> 8 );
1347 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001348
1349 if( end < p || (size_t)( end - p ) < psk_len )
1350 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1351
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001352 memcpy( p, psk, psk_len );
1353 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001354
1355 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1356
1357 return( 0 );
1358}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001362/*
1363 * SSLv3.0 MAC functions
1364 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001365#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001366static void ssl_mac( mbedtls_md_context_t *md_ctx,
1367 const unsigned char *secret,
1368 const unsigned char *buf, size_t len,
1369 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001370 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001371{
1372 unsigned char header[11];
1373 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001374 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1376 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001377
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001378 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001380 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001381 else
Paul Bakker68884e32013-01-07 18:20:04 +01001382 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001383
1384 memcpy( header, ctr, 8 );
1385 header[ 8] = (unsigned char) type;
1386 header[ 9] = (unsigned char)( len >> 8 );
1387 header[10] = (unsigned char)( len );
1388
Paul Bakker68884e32013-01-07 18:20:04 +01001389 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 mbedtls_md_starts( md_ctx );
1391 mbedtls_md_update( md_ctx, secret, md_size );
1392 mbedtls_md_update( md_ctx, padding, padlen );
1393 mbedtls_md_update( md_ctx, header, 11 );
1394 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001395 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001396
Paul Bakker68884e32013-01-07 18:20:04 +01001397 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 mbedtls_md_starts( md_ctx );
1399 mbedtls_md_update( md_ctx, secret, md_size );
1400 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001401 mbedtls_md_update( md_ctx, out, md_size );
1402 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001403}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1407 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001408 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001409#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001410#endif
1411
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001412/* The function below is only used in the Lucky 13 counter-measure in
1413 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1414#if defined(SSL_SOME_MODES_USE_MAC) && \
1415 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1416 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1417 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1418/* This function makes sure every byte in the memory region is accessed
1419 * (in ascending addresses order) */
1420static void ssl_read_memory( unsigned char *p, size_t len )
1421{
1422 unsigned char acc = 0;
1423 volatile unsigned char force;
1424
1425 for( ; len != 0; p++, len-- )
1426 acc ^= *p;
1427
1428 force = acc;
1429 (void) force;
1430}
1431#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1432
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001433/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001434 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001437{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001439 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001442
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001443 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1446 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001447 }
1448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001452 ssl->out_msg, ssl->out_msglen );
1453
Paul Bakker5121ce52009-01-03 21:22:43 +00001454 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001455 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001457#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 if( mode == MBEDTLS_MODE_STREAM ||
1459 ( mode == MBEDTLS_MODE_CBC
1460#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1461 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001462#endif
1463 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465#if defined(MBEDTLS_SSL_PROTO_SSL3)
1466 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001467 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001468 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001469
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001470 ssl_mac( &ssl->transform_out->md_ctx_enc,
1471 ssl->transform_out->mac_enc,
1472 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001473 ssl->out_ctr, ssl->out_msgtype,
1474 mac );
1475
1476 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001477 }
1478 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001479#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1481 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1482 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001483 {
Hanno Becker992b6872017-11-09 18:57:39 +00001484 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1487 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1488 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1489 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001490 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001491 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001493
1494 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001495 }
1496 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001497#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1500 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001501 }
1502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001504 ssl->out_msg + ssl->out_msglen,
1505 ssl->transform_out->maclen );
1506
1507 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001508 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001509 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001510#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001511
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001512 /*
1513 * Encrypt
1514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1516 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001517 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001518 int ret;
1519 size_t olen = 0;
1520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001522 "including %d bytes of padding",
1523 ssl->out_msglen, 0 ) );
1524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001526 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001527 ssl->transform_out->ivlen,
1528 ssl->out_msg, ssl->out_msglen,
1529 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001532 return( ret );
1533 }
1534
1535 if( ssl->out_msglen != olen )
1536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1538 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001539 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001540 }
Paul Bakker68884e32013-01-07 18:20:04 +01001541 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001543#if defined(MBEDTLS_GCM_C) || \
1544 defined(MBEDTLS_CCM_C) || \
1545 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001547 mode == MBEDTLS_MODE_CCM ||
1548 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001549 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001550 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001551 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001552 unsigned char *enc_msg;
1553 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001554 unsigned char iv[12];
1555 mbedtls_ssl_transform *transform = ssl->transform_out;
1556 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001558 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001559
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001560 /*
1561 * Prepare additional authenticated data
1562 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001563 memcpy( add_data, ssl->out_ctr, 8 );
1564 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001566 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001567 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1568 add_data[12] = ssl->out_msglen & 0xFF;
1569
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001570 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001571
Paul Bakker68884e32013-01-07 18:20:04 +01001572 /*
1573 * Generate IV
1574 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001575 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1576 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001577 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001578 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1579 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1580 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1581
1582 }
1583 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1584 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001585 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001586 unsigned char i;
1587
1588 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1589
1590 for( i = 0; i < 8; i++ )
1591 iv[i+4] ^= ssl->out_ctr[i];
1592 }
1593 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001594 {
1595 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1597 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001598 }
1599
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001600 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1601 iv, transform->ivlen );
1602 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1603 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001604
Paul Bakker68884e32013-01-07 18:20:04 +01001605 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001606 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001607 */
1608 enc_msg = ssl->out_msg;
1609 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001610 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001613 "including 0 bytes of padding",
1614 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001615
Paul Bakker68884e32013-01-07 18:20:04 +01001616 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001617 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001618 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001619 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1620 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001621 add_data, 13,
1622 enc_msg, enc_msglen,
1623 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001624 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001627 return( ret );
1628 }
1629
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001630 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1633 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001634 }
1635
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001636 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001637 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001640 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001641 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1643#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001644 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001646 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001647 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001648 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001649 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001650
Paul Bakker48916f92012-09-16 19:57:18 +00001651 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1652 ssl->transform_out->ivlen;
1653 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001654 padlen = 0;
1655
1656 for( i = 0; i <= padlen; i++ )
1657 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1658
1659 ssl->out_msglen += padlen + 1;
1660
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001661 enc_msglen = ssl->out_msglen;
1662 enc_msg = ssl->out_msg;
1663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001665 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001666 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1667 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001668 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001670 {
1671 /*
1672 * Generate IV
1673 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001674 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001675 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001676 if( ret != 0 )
1677 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001678
Paul Bakker92be97b2013-01-02 17:30:03 +01001679 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001680 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001681
1682 /*
1683 * Fix pointer positions and message length with added IV
1684 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001685 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001686 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001687 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001688 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001692 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001693 ssl->out_msglen, ssl->transform_out->ivlen,
1694 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001697 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001698 ssl->transform_out->ivlen,
1699 enc_msg, enc_msglen,
1700 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001703 return( ret );
1704 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001705
Paul Bakkercca5b812013-08-31 17:40:26 +02001706 if( enc_msglen != olen )
1707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1709 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001710 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1713 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001714 {
1715 /*
1716 * Save IV in SSL3 and TLS1
1717 */
1718 memcpy( ssl->transform_out->iv_enc,
1719 ssl->transform_out->cipher_ctx_enc.iv,
1720 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001721 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001722#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001725 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001726 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001727 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1728
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001729 /*
1730 * MAC(MAC_write_key, seq_num +
1731 * TLSCipherText.type +
1732 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001733 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001734 * IV + // except for TLS 1.0
1735 * ENC(content + padding + padding_length));
1736 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001737 unsigned char pseudo_hdr[13];
1738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001740
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001741 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1742 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001743 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1744 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1749 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001750 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00001751 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001753
Hanno Becker3d8c9072018-01-05 16:24:22 +00001754 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1755 ssl->transform_out->maclen );
1756
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001757 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001758 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001759 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001761 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001762 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001764 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1767 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001768 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001769
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001770 /* Make extra sure authentication was performed, exactly once */
1771 if( auth_done != 1 )
1772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1774 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001775 }
1776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001778
1779 return( 0 );
1780}
1781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001783{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001785 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001786#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001787 size_t padlen = 0, correct = 1;
1788#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001791
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001792 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1795 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001796 }
1797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001799
Paul Bakker48916f92012-09-16 19:57:18 +00001800 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001803 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001805 }
1806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1808 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001809 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001810 int ret;
1811 size_t olen = 0;
1812
Paul Bakker68884e32013-01-07 18:20:04 +01001813 padlen = 0;
1814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001816 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001817 ssl->transform_in->ivlen,
1818 ssl->in_msg, ssl->in_msglen,
1819 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001822 return( ret );
1823 }
1824
1825 if( ssl->in_msglen != olen )
1826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1828 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001829 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001830 }
Paul Bakker68884e32013-01-07 18:20:04 +01001831 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001833#if defined(MBEDTLS_GCM_C) || \
1834 defined(MBEDTLS_CCM_C) || \
1835 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001837 mode == MBEDTLS_MODE_CCM ||
1838 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001839 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001840 int ret;
1841 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001842 unsigned char *dec_msg;
1843 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001844 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001845 unsigned char iv[12];
1846 mbedtls_ssl_transform *transform = ssl->transform_in;
1847 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001849 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001850
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001851 /*
1852 * Compute and update sizes
1853 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001854 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001857 "+ taglen (%d)", ssl->in_msglen,
1858 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001860 }
1861 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1862
Paul Bakker68884e32013-01-07 18:20:04 +01001863 dec_msg = ssl->in_msg;
1864 dec_msg_result = ssl->in_msg;
1865 ssl->in_msglen = dec_msglen;
1866
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001867 /*
1868 * Prepare additional authenticated data
1869 */
Paul Bakker68884e32013-01-07 18:20:04 +01001870 memcpy( add_data, ssl->in_ctr, 8 );
1871 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001873 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001874 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1875 add_data[12] = ssl->in_msglen & 0xFF;
1876
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001877 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001878
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001879 /*
1880 * Prepare IV
1881 */
1882 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1883 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001884 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001885 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1886 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001887
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001888 }
1889 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1890 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001891 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001892 unsigned char i;
1893
1894 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1895
1896 for( i = 0; i < 8; i++ )
1897 iv[i+4] ^= ssl->in_ctr[i];
1898 }
1899 else
1900 {
1901 /* Reminder if we ever add an AEAD mode with a different size */
1902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1903 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1904 }
1905
1906 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001908
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001909 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001910 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001911 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001913 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001914 add_data, 13,
1915 dec_msg, dec_msglen,
1916 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001917 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1922 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001923
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001924 return( ret );
1925 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001926 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001927
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001928 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1931 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001932 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001933 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001934 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1936#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001937 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001939 {
Paul Bakker45829992013-01-03 14:52:21 +01001940 /*
1941 * Decrypt and check the padding
1942 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001943 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001944 unsigned char *dec_msg;
1945 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001946 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001947 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001948 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001949
Paul Bakker5121ce52009-01-03 21:22:43 +00001950 /*
Paul Bakker45829992013-01-03 14:52:21 +01001951 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001952 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1954 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001955 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001956#endif
Paul Bakker45829992013-01-03 14:52:21 +01001957
1958 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1959 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001962 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1963 ssl->transform_in->ivlen,
1964 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001966 }
1967
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001968 dec_msglen = ssl->in_msglen;
1969 dec_msg = ssl->in_msg;
1970 dec_msg_result = ssl->in_msg;
1971
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001972 /*
1973 * Authenticate before decrypt if enabled
1974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1976 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001977 {
Hanno Becker992b6872017-11-09 18:57:39 +00001978 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001979 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001982
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001983 dec_msglen -= ssl->transform_in->maclen;
1984 ssl->in_msglen -= ssl->transform_in->maclen;
1985
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001986 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1987 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1988 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1989 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1994 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001995 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001996 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002000 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002001 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002002 ssl->transform_in->maclen );
2003
Hanno Becker992b6872017-11-09 18:57:39 +00002004 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2005 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002010 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002011 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002012 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002014
2015 /*
2016 * Check length sanity
2017 */
2018 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002021 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002023 }
2024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002026 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002027 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002028 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002030 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002031 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002032 dec_msglen -= ssl->transform_in->ivlen;
2033 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002034
Paul Bakker48916f92012-09-16 19:57:18 +00002035 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002036 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002037 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002041 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002042 ssl->transform_in->ivlen,
2043 dec_msg, dec_msglen,
2044 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002047 return( ret );
2048 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002049
Paul Bakkercca5b812013-08-31 17:40:26 +02002050 if( dec_msglen != olen )
2051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2053 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002054 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2057 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002058 {
2059 /*
2060 * Save IV in SSL3 and TLS1
2061 */
2062 memcpy( ssl->transform_in->iv_dec,
2063 ssl->transform_in->cipher_ctx_dec.iv,
2064 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002066#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002067
2068 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002069
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002070 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002071 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073#if defined(MBEDTLS_SSL_DEBUG_ALL)
2074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002075 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002076#endif
Paul Bakker45829992013-01-03 14:52:21 +01002077 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002078 correct = 0;
2079 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081#if defined(MBEDTLS_SSL_PROTO_SSL3)
2082 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 {
Paul Bakker48916f92012-09-16 19:57:18 +00002084 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086#if defined(MBEDTLS_SSL_DEBUG_ALL)
2087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002088 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002089 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002090#endif
Paul Bakker45829992013-01-03 14:52:21 +01002091 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 }
2093 }
2094 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2096#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2097 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2098 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002099 {
2100 /*
Paul Bakker45829992013-01-03 14:52:21 +01002101 * TLSv1+: always check the padding up to the first failure
2102 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002104 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002105 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002106 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002107
Paul Bakker956c9e02013-12-19 14:42:28 +01002108 /*
2109 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002110 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002111 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002112 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002113 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002114 *
2115 * In both cases we reset padding_idx to a safe value (0) to
2116 * prevent out-of-buffer reads.
2117 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002118 correct &= ( padlen <= ssl->in_msglen );
2119 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002120 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002121
2122 padding_idx *= correct;
2123
Angus Grattonb512bc12018-06-19 15:57:50 +10002124 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002125 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002126 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002127 pad_count += real_count *
2128 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2129 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002130
2131 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002134 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002136#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002137 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002139 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2141 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2144 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002145 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002146
2147 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002149 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002151 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2154 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002155 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002156
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002157#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002159 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002160#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002161
2162 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002163 * Authenticate if not done yet.
2164 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002165 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002166#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002167 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002168 {
Hanno Becker992b6872017-11-09 18:57:39 +00002169 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002170
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002171 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002172
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002173 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2174 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176#if defined(MBEDTLS_SSL_PROTO_SSL3)
2177 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002178 {
2179 ssl_mac( &ssl->transform_in->md_ctx_dec,
2180 ssl->transform_in->mac_dec,
2181 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002182 ssl->in_ctr, ssl->in_msgtype,
2183 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002184 }
2185 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2187#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2188 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2189 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002190 {
2191 /*
2192 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002193 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002194 *
2195 * Known timing attacks:
2196 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2197 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002198 * To compensate for different timings for the MAC calculation
2199 * depending on how much padding was removed (which is determined
2200 * by padlen), process extra_run more blocks through the hash
2201 * function.
2202 *
2203 * The formula in the paper is
2204 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2205 * where L1 is the size of the header plus the decrypted message
2206 * plus CBC padding and L2 is the size of the header plus the
2207 * decrypted message. This is for an underlying hash function
2208 * with 64-byte blocks.
2209 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2210 * correctly. We round down instead of up, so -56 is the correct
2211 * value for our calculations instead of -55.
2212 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002213 * Repeat the formula rather than defining a block_size variable.
2214 * This avoids requiring division by a variable at runtime
2215 * (which would be marginally less efficient and would require
2216 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002217 */
2218 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002219
2220 /*
2221 * The next two sizes are the minimum and maximum values of
2222 * in_msglen over all padlen values.
2223 *
2224 * They're independent of padlen, since we previously did
2225 * in_msglen -= padlen.
2226 *
2227 * Note that max_len + maclen is never more than the buffer
2228 * length, as we previously did in_msglen -= maclen too.
2229 */
2230 const size_t max_len = ssl->in_msglen + padlen;
2231 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2232
Gilles Peskine20b44082018-05-29 14:06:49 +02002233 switch( ssl->transform_in->ciphersuite_info->mac )
2234 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002235#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2236 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002237 case MBEDTLS_MD_MD5:
2238 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002239 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002240 /* 8 bytes of message size, 64-byte compression blocks */
2241 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2242 ( 13 + ssl->in_msglen + 8 ) / 64;
2243 break;
2244#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002245#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002246 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002247 /* 16 bytes of message size, 128-byte compression blocks */
2248 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2249 ( 13 + ssl->in_msglen + 16 ) / 128;
2250 break;
2251#endif
2252 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002254 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2255 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002256
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002257 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2260 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2261 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2262 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002263 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002264 /* Make sure we access everything even when padlen > 0. This
2265 * makes the synchronisation requirements for just-in-time
2266 * Prime+Probe attacks much tighter and hopefully impractical. */
2267 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002268 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002269
2270 /* Call mbedtls_md_process at least once due to cache attacks
2271 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002272 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002276
2277 /* Make sure we access all the memory that could contain the MAC,
2278 * before we check it in the next code block. This makes the
2279 * synchronisation requirements for just-in-time Prime+Probe
2280 * attacks much tighter and hopefully impractical. */
2281 ssl_read_memory( ssl->in_msg + min_len,
2282 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002283 }
2284 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2286 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2289 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002290 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002291
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002292#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002293 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2294 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2295 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002296#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002297
Hanno Becker992b6872017-11-09 18:57:39 +00002298 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2299 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301#if defined(MBEDTLS_SSL_DEBUG_ALL)
2302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002303#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002304 correct = 0;
2305 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002306 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00002307
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002308 /*
2309 * Finally check the correct flag
2310 */
2311 if( correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002313 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002314#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002315
2316 /* Make extra sure authentication was performed, exactly once */
2317 if( auth_done != 1 )
2318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2320 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002321 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002322
2323 if( ssl->in_msglen == 0 )
2324 {
Angus Gratton34817922018-06-19 15:58:22 +10002325#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2326 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2327 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2328 {
2329 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2331 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2332 }
2333#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2334
Paul Bakker5121ce52009-01-03 21:22:43 +00002335 ssl->nb_zero++;
2336
2337 /*
2338 * Three or more empty messages may be a DoS attack
2339 * (excessive CPU consumption).
2340 */
2341 if( ssl->nb_zero > 3 )
2342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002344 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002346 }
2347 }
2348 else
2349 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002352 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002353 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002354 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002355 }
2356 else
2357#endif
2358 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002359 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002360 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2361 if( ++ssl->in_ctr[i - 1] != 0 )
2362 break;
2363
2364 /* The loop goes to its end iff the counter is wrapping */
2365 if( i == ssl_ep_len( ssl ) )
2366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2368 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002369 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002370 }
2371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002373
2374 return( 0 );
2375}
2376
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002377#undef MAC_NONE
2378#undef MAC_PLAINTEXT
2379#undef MAC_CIPHERTEXT
2380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002382/*
2383 * Compression/decompression functions
2384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002386{
2387 int ret;
2388 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002389 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002390 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002391 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002394
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002395 if( len_pre == 0 )
2396 return( 0 );
2397
Paul Bakker2770fbd2012-07-03 13:30:23 +00002398 memcpy( msg_pre, ssl->out_msg, len_pre );
2399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002401 ssl->out_msglen ) );
2402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002404 ssl->out_msg, ssl->out_msglen );
2405
Paul Bakker48916f92012-09-16 19:57:18 +00002406 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2407 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2408 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002409 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002410
Paul Bakker48916f92012-09-16 19:57:18 +00002411 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002412 if( ret != Z_OK )
2413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2415 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002416 }
2417
Angus Grattond8213d02016-05-25 20:56:48 +10002418 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002419 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002422 ssl->out_msglen ) );
2423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002425 ssl->out_msg, ssl->out_msglen );
2426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002428
2429 return( 0 );
2430}
2431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002433{
2434 int ret;
2435 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002436 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002437 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002438 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002441
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002442 if( len_pre == 0 )
2443 return( 0 );
2444
Paul Bakker2770fbd2012-07-03 13:30:23 +00002445 memcpy( msg_pre, ssl->in_msg, len_pre );
2446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002448 ssl->in_msglen ) );
2449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002451 ssl->in_msg, ssl->in_msglen );
2452
Paul Bakker48916f92012-09-16 19:57:18 +00002453 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2454 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2455 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002456 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002457 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002458
Paul Bakker48916f92012-09-16 19:57:18 +00002459 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002460 if( ret != Z_OK )
2461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2463 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002464 }
2465
Angus Grattond8213d02016-05-25 20:56:48 +10002466 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002467 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002470 ssl->in_msglen ) );
2471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002473 ssl->in_msg, ssl->in_msglen );
2474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002476
2477 return( 0 );
2478}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2482static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484#if defined(MBEDTLS_SSL_PROTO_DTLS)
2485static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002486{
2487 /* If renegotiation is not enforced, retransmit until we would reach max
2488 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002489 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002490 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002491 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002492 unsigned char doublings = 1;
2493
2494 while( ratio != 0 )
2495 {
2496 ++doublings;
2497 ratio >>= 1;
2498 }
2499
2500 if( ++ssl->renego_records_seen > doublings )
2501 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002503 return( 0 );
2504 }
2505 }
2506
2507 return( ssl_write_hello_request( ssl ) );
2508}
2509#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002511
Paul Bakker5121ce52009-01-03 21:22:43 +00002512/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002513 * Fill the input message buffer by appending data to it.
2514 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002515 *
2516 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2517 * available (from this read and/or a previous one). Otherwise, an error code
2518 * is returned (possibly EOF or WANT_READ).
2519 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002520 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2521 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2522 * since we always read a whole datagram at once.
2523 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002524 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002525 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002528{
Paul Bakker23986e52011-04-24 08:57:21 +00002529 int ret;
2530 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002533
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002534 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002537 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002539 }
2540
Angus Grattond8213d02016-05-25 20:56:48 +10002541 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2544 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002545 }
2546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002548 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002549 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002550 uint32_t timeout;
2551
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002552 /* Just to be sure */
2553 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2554 {
2555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2556 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2557 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2558 }
2559
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002560 /*
2561 * The point is, we need to always read a full datagram at once, so we
2562 * sometimes read more then requested, and handle the additional data.
2563 * It could be the rest of the current record (while fetching the
2564 * header) and/or some other records in the same datagram.
2565 */
2566
2567 /*
2568 * Move to the next record in the already read datagram if applicable
2569 */
2570 if( ssl->next_record_offset != 0 )
2571 {
2572 if( ssl->in_left < ssl->next_record_offset )
2573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2575 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002576 }
2577
2578 ssl->in_left -= ssl->next_record_offset;
2579
2580 if( ssl->in_left != 0 )
2581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002583 ssl->next_record_offset ) );
2584 memmove( ssl->in_hdr,
2585 ssl->in_hdr + ssl->next_record_offset,
2586 ssl->in_left );
2587 }
2588
2589 ssl->next_record_offset = 0;
2590 }
2591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002593 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002594
2595 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002596 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002597 */
2598 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002601 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002602 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002603
2604 /*
2605 * A record can't be split accross datagrams. If we need to read but
2606 * are not at the beginning of a new record, the caller did something
2607 * wrong.
2608 */
2609 if( ssl->in_left != 0 )
2610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2612 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002613 }
2614
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002615 /*
2616 * Don't even try to read if time's out already.
2617 * This avoids by-passing the timer when repeatedly receiving messages
2618 * that will end up being dropped.
2619 */
2620 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002621 {
2622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002623 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002624 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002625 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002626 {
Angus Grattond8213d02016-05-25 20:56:48 +10002627 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002630 timeout = ssl->handshake->retransmit_timeout;
2631 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002632 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002635
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002636 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002637 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2638 timeout );
2639 else
2640 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002643
2644 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002646 }
2647
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002648 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002651 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002654 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002655 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002658 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002659 }
2660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002663 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002664 return( ret );
2665 }
2666
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002667 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002668 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002670 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002672 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002673 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002676 return( ret );
2677 }
2678
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002679 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002680 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002682 }
2683
Paul Bakker5121ce52009-01-03 21:22:43 +00002684 if( ret < 0 )
2685 return( ret );
2686
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002687 ssl->in_left = ret;
2688 }
2689 else
2690#endif
2691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002693 ssl->in_left, nb_want ) );
2694
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002695 while( ssl->in_left < nb_want )
2696 {
2697 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002698
2699 if( ssl_check_timer( ssl ) != 0 )
2700 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2701 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002702 {
2703 if( ssl->f_recv_timeout != NULL )
2704 {
2705 ret = ssl->f_recv_timeout( ssl->p_bio,
2706 ssl->in_hdr + ssl->in_left, len,
2707 ssl->conf->read_timeout );
2708 }
2709 else
2710 {
2711 ret = ssl->f_recv( ssl->p_bio,
2712 ssl->in_hdr + ssl->in_left, len );
2713 }
2714 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002717 ssl->in_left, nb_want ) );
2718 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002719
2720 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002722
2723 if( ret < 0 )
2724 return( ret );
2725
mohammad160352aecb92018-03-28 23:41:40 -07002726 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002727 {
Darryl Green11999bb2018-03-13 15:22:58 +00002728 MBEDTLS_SSL_DEBUG_MSG( 1,
2729 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002730 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002731 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2732 }
2733
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002734 ssl->in_left += ret;
2735 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002736 }
2737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002739
2740 return( 0 );
2741}
2742
2743/*
2744 * Flush any data not yet written
2745 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002746int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002747{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002748 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002749 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002752
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002753 if( ssl->f_send == NULL )
2754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002756 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002757 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002758 }
2759
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002760 /* Avoid incrementing counter if data is flushed */
2761 if( ssl->out_left == 0 )
2762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002764 return( 0 );
2765 }
2766
Paul Bakker5121ce52009-01-03 21:22:43 +00002767 while( ssl->out_left > 0 )
2768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2770 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002771
Hanno Becker2b1e3542018-08-06 11:19:13 +01002772 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002773 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002776
2777 if( ret <= 0 )
2778 return( ret );
2779
mohammad160352aecb92018-03-28 23:41:40 -07002780 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002781 {
Darryl Green11999bb2018-03-13 15:22:58 +00002782 MBEDTLS_SSL_DEBUG_MSG( 1,
2783 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002784 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002785 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2786 }
2787
Paul Bakker5121ce52009-01-03 21:22:43 +00002788 ssl->out_left -= ret;
2789 }
2790
Hanno Becker2b1e3542018-08-06 11:19:13 +01002791#if defined(MBEDTLS_SSL_PROTO_DTLS)
2792 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002793 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002794 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002795 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002796 else
2797#endif
2798 {
2799 ssl->out_hdr = ssl->out_buf + 8;
2800 }
2801 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002804
2805 return( 0 );
2806}
2807
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002808/*
2809 * Functions to handle the DTLS retransmission state machine
2810 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002812/*
2813 * Append current handshake message to current outgoing flight
2814 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002816{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2819 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2820 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002821
2822 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002823 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002824 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002827 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002828 }
2829
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002830 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002831 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002834 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002835 }
2836
2837 /* Copy current handshake message with headers */
2838 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2839 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002840 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002841 msg->next = NULL;
2842
2843 /* Append to the current flight */
2844 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002845 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002846 else
2847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002849 while( cur->next != NULL )
2850 cur = cur->next;
2851 cur->next = msg;
2852 }
2853
Hanno Becker3b235902018-08-06 09:54:53 +01002854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002855 return( 0 );
2856}
2857
2858/*
2859 * Free the current flight of handshake messages
2860 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002862{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 mbedtls_ssl_flight_item *cur = flight;
2864 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002865
2866 while( cur != NULL )
2867 {
2868 next = cur->next;
2869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 mbedtls_free( cur->p );
2871 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002872
2873 cur = next;
2874 }
2875}
2876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2878static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002879#endif
2880
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002881/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002882 * Swap transform_out and out_ctr with the alternative ones
2883 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002884static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002885{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002887 unsigned char tmp_out_ctr[8];
2888
2889 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002892 return;
2893 }
2894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002896
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002897 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002898 tmp_transform = ssl->transform_out;
2899 ssl->transform_out = ssl->handshake->alt_transform_out;
2900 ssl->handshake->alt_transform_out = tmp_transform;
2901
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002902 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002903 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2904 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002905 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002906
2907 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01002908 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2911 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2916 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002917 }
2918 }
2919#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002920}
2921
2922/*
2923 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002924 */
2925int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2926{
2927 int ret = 0;
2928
2929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2930
2931 ret = mbedtls_ssl_flight_transmit( ssl );
2932
2933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2934
2935 return( ret );
2936}
2937
2938/*
2939 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002940 *
2941 * Need to remember the current message in case flush_output returns
2942 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002943 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002944 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002945int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002946{
Hanno Becker67bc7c32018-08-06 11:33:50 +01002947 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002951 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002953
2954 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002955 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002956 ssl_swap_epochs( ssl );
2957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002959 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002960
2961 while( ssl->handshake->cur_msg != NULL )
2962 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002963 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002964 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002965
Hanno Beckere1dcb032018-08-17 16:47:58 +01002966 int const is_finished =
2967 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2968 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2969
Hanno Becker04da1892018-08-14 13:22:10 +01002970 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2971 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2972
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002973 /* Swap epochs before sending Finished: we can't do it after
2974 * sending ChangeCipherSpec, in case write returns WANT_READ.
2975 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002976 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002977 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002979 ssl_swap_epochs( ssl );
2980 }
2981
Hanno Becker67bc7c32018-08-06 11:33:50 +01002982 ret = ssl_get_remaining_payload_in_datagram( ssl );
2983 if( ret < 0 )
2984 return( ret );
2985 max_frag_len = (size_t) ret;
2986
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002987 /* CCS is copied as is, while HS messages may need fragmentation */
2988 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2989 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002990 if( max_frag_len == 0 )
2991 {
2992 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2993 return( ret );
2994
2995 continue;
2996 }
2997
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002998 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002999 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003000 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003001
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003002 /* Update position inside current message */
3003 ssl->handshake->cur_msg_p += cur->len;
3004 }
3005 else
3006 {
3007 const unsigned char * const p = ssl->handshake->cur_msg_p;
3008 const size_t hs_len = cur->len - 12;
3009 const size_t frag_off = p - ( cur->p + 12 );
3010 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003011 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003012
Hanno Beckere1dcb032018-08-17 16:47:58 +01003013 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003014 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003015 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003016 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003017
Hanno Becker67bc7c32018-08-06 11:33:50 +01003018 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3019 return( ret );
3020
3021 continue;
3022 }
3023 max_hs_frag_len = max_frag_len - 12;
3024
3025 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3026 max_hs_frag_len : rem_len;
3027
3028 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003029 {
3030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003031 (unsigned) cur_hs_frag_len,
3032 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003033 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003034
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003035 /* Messages are stored with handshake headers as if not fragmented,
3036 * copy beginning of headers then fill fragmentation fields.
3037 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3038 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003039
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003040 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3041 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3042 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3043
Hanno Becker67bc7c32018-08-06 11:33:50 +01003044 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3045 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3046 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003047
3048 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3049
Hanno Becker3f7b9732018-08-28 09:53:25 +01003050 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003051 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3052 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003053 ssl->out_msgtype = cur->type;
3054
3055 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003056 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003057 }
3058
3059 /* If done with the current message move to the next one if any */
3060 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3061 {
3062 if( cur->next != NULL )
3063 {
3064 ssl->handshake->cur_msg = cur->next;
3065 ssl->handshake->cur_msg_p = cur->next->p + 12;
3066 }
3067 else
3068 {
3069 ssl->handshake->cur_msg = NULL;
3070 ssl->handshake->cur_msg_p = NULL;
3071 }
3072 }
3073
3074 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003075 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003078 return( ret );
3079 }
3080 }
3081
Hanno Becker67bc7c32018-08-06 11:33:50 +01003082 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3083 return( ret );
3084
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003085 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3087 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003088 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003091 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3092 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003093
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003095
3096 return( 0 );
3097}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003098
3099/*
3100 * To be called when the last message of an incoming flight is received.
3101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003103{
3104 /* We won't need to resend that one any more */
3105 ssl_flight_free( ssl->handshake->flight );
3106 ssl->handshake->flight = NULL;
3107 ssl->handshake->cur_msg = NULL;
3108
3109 /* The next incoming flight will start with this msg_seq */
3110 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3111
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003112 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003113 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003114
Hanno Becker0271f962018-08-16 13:23:47 +01003115 /* Clear future message buffering structure. */
3116 ssl_buffering_free( ssl );
3117
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003118 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003119 ssl_set_timer( ssl, 0 );
3120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003121 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3122 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003125 }
3126 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003128}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003129
3130/*
3131 * To be called when the last message of an outgoing flight is send.
3132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003134{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003135 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003136 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3139 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003142 }
3143 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003145}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003147
Paul Bakker5121ce52009-01-03 21:22:43 +00003148/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003149 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003150 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003151
3152/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003153 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003154 *
3155 * - fill in handshake headers
3156 * - update handshake checksum
3157 * - DTLS: save message for resending
3158 * - then pass to the record layer
3159 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003160 * DTLS: except for HelloRequest, messages are only queued, and will only be
3161 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003162 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003163 * Inputs:
3164 * - ssl->out_msglen: 4 + actual handshake message len
3165 * (4 is the size of handshake headers for TLS)
3166 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3167 * - ssl->out_msg + 4: the handshake message body
3168 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003169 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003170 * - ssl->out_msglen: the length of the record contents
3171 * (including handshake headers but excluding record headers)
3172 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003173 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003174int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003175{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003176 int ret;
3177 const size_t hs_len = ssl->out_msglen - 4;
3178 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003179
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3181
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003182 /*
3183 * Sanity checks
3184 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003185 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003186 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3187 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003188 /* In SSLv3, the client might send a NoCertificate alert. */
3189#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3190 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3191 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3192 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3193#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3194 {
3195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3196 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3197 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003198 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003199
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003200 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3201 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3202 ssl->handshake == NULL )
3203 {
3204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3205 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3206 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003209 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003210 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003211 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003212 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3214 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003215 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003216#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003217
Hanno Beckerb50a2532018-08-06 11:52:54 +01003218 /* Double-check that we did not exceed the bounds
3219 * of the outgoing record buffer.
3220 * This should never fail as the various message
3221 * writing functions must obey the bounds of the
3222 * outgoing record buffer, but better be safe.
3223 *
3224 * Note: We deliberately do not check for the MTU or MFL here.
3225 */
3226 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3227 {
3228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3229 "size %u, maximum %u",
3230 (unsigned) ssl->out_msglen,
3231 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3232 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3233 }
3234
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003235 /*
3236 * Fill handshake headers
3237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003239 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003240 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3241 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3242 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003243
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003244 /*
3245 * DTLS has additional fields in the Handshake layer,
3246 * between the length field and the actual payload:
3247 * uint16 message_seq;
3248 * uint24 fragment_offset;
3249 * uint24 fragment_length;
3250 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003252 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003253 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003254 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003255 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003256 {
3257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3258 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003259 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003260 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003261 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3262 }
3263
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003264 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003265 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003266
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003267 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003268 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003269 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003270 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3271 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3272 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003273 }
3274 else
3275 {
3276 ssl->out_msg[4] = 0;
3277 ssl->out_msg[5] = 0;
3278 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003279
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003280 /* Handshake hashes are computed without fragmentation,
3281 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003282 memset( ssl->out_msg + 6, 0x00, 3 );
3283 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003284 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003285#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003286
Hanno Becker0207e532018-08-28 10:28:28 +01003287 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003288 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3289 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003290 }
3291
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003292 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003294 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Becker081bd812018-08-22 16:07:59 +01003295 ( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
3296 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003297 {
3298 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003301 return( ret );
3302 }
3303 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003304 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003305#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003306 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003307 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003308 {
3309 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3310 return( ret );
3311 }
3312 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003313
3314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3315
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003316 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003317}
3318
3319/*
3320 * Record layer functions
3321 */
3322
3323/*
3324 * Write current record.
3325 *
3326 * Uses:
3327 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3328 * - ssl->out_msglen: length of the record content (excl headers)
3329 * - ssl->out_msg: record content
3330 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003331int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003332{
3333 int ret, done = 0;
3334 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003335 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003336
3337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003340 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003342 {
3343 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003346 return( ret );
3347 }
3348
3349 len = ssl->out_msglen;
3350 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003353#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3354 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358 ret = mbedtls_ssl_hw_record_write( ssl );
3359 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3362 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003363 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003364
3365 if( ret == 0 )
3366 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003367 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003369 if( !done )
3370 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003371 unsigned i;
3372 size_t protected_record_size;
3373
Paul Bakker05ef8352012-05-08 09:17:57 +00003374 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003376 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003377
Hanno Becker19859472018-08-06 09:40:20 +01003378 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003379 ssl->out_len[0] = (unsigned char)( len >> 8 );
3380 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003381
Paul Bakker48916f92012-09-16 19:57:18 +00003382 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003383 {
3384 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003387 return( ret );
3388 }
3389
3390 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003391 ssl->out_len[0] = (unsigned char)( len >> 8 );
3392 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003393 }
3394
Hanno Becker2b1e3542018-08-06 11:19:13 +01003395 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3396
3397#if defined(MBEDTLS_SSL_PROTO_DTLS)
3398 /* In case of DTLS, double-check that we don't exceed
3399 * the remaining space in the datagram. */
3400 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3401 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003402 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003403 if( ret < 0 )
3404 return( ret );
3405
3406 if( protected_record_size > (size_t) ret )
3407 {
3408 /* Should never happen */
3409 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3410 }
3411 }
3412#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003415 "version = [%d:%d], msglen = %d",
3416 ssl->out_hdr[0], ssl->out_hdr[1],
3417 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003420 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003421
3422 ssl->out_left += protected_record_size;
3423 ssl->out_hdr += protected_record_size;
3424 ssl_update_out_pointers( ssl, ssl->transform_out );
3425
Hanno Becker04484622018-08-06 09:49:38 +01003426 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3427 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3428 break;
3429
3430 /* The loop goes to its end iff the counter is wrapping */
3431 if( i == ssl_ep_len( ssl ) )
3432 {
3433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3434 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3435 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003436 }
3437
Hanno Becker67bc7c32018-08-06 11:33:50 +01003438#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003439 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3440 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003441 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003442 size_t remaining;
3443 ret = ssl_get_remaining_payload_in_datagram( ssl );
3444 if( ret < 0 )
3445 {
3446 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3447 ret );
3448 return( ret );
3449 }
3450
3451 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003452 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003453 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003454 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003455 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003456 else
3457 {
Hanno Becker513815a2018-08-20 11:56:09 +01003458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003459 }
3460 }
3461#endif /* MBEDTLS_SSL_PROTO_DTLS */
3462
3463 if( ( flush == SSL_FORCE_FLUSH ) &&
3464 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003466 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003467 return( ret );
3468 }
3469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003471
3472 return( 0 );
3473}
3474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003475#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003476
3477static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3478{
3479 if( ssl->in_msglen < ssl->in_hslen ||
3480 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3481 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3482 {
3483 return( 1 );
3484 }
3485 return( 0 );
3486}
Hanno Becker44650b72018-08-16 12:51:11 +01003487
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003488static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003489{
3490 return( ( ssl->in_msg[9] << 16 ) |
3491 ( ssl->in_msg[10] << 8 ) |
3492 ssl->in_msg[11] );
3493}
3494
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003495static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003496{
3497 return( ( ssl->in_msg[6] << 16 ) |
3498 ( ssl->in_msg[7] << 8 ) |
3499 ssl->in_msg[8] );
3500}
3501
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003502static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003503{
3504 uint32_t msg_len, frag_off, frag_len;
3505
3506 msg_len = ssl_get_hs_total_len( ssl );
3507 frag_off = ssl_get_hs_frag_off( ssl );
3508 frag_len = ssl_get_hs_frag_len( ssl );
3509
3510 if( frag_off > msg_len )
3511 return( -1 );
3512
3513 if( frag_len > msg_len - frag_off )
3514 return( -1 );
3515
3516 if( frag_len + 12 > ssl->in_msglen )
3517 return( -1 );
3518
3519 return( 0 );
3520}
3521
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003522/*
3523 * Mark bits in bitmask (used for DTLS HS reassembly)
3524 */
3525static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3526{
3527 unsigned int start_bits, end_bits;
3528
3529 start_bits = 8 - ( offset % 8 );
3530 if( start_bits != 8 )
3531 {
3532 size_t first_byte_idx = offset / 8;
3533
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003534 /* Special case */
3535 if( len <= start_bits )
3536 {
3537 for( ; len != 0; len-- )
3538 mask[first_byte_idx] |= 1 << ( start_bits - len );
3539
3540 /* Avoid potential issues with offset or len becoming invalid */
3541 return;
3542 }
3543
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003544 offset += start_bits; /* Now offset % 8 == 0 */
3545 len -= start_bits;
3546
3547 for( ; start_bits != 0; start_bits-- )
3548 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3549 }
3550
3551 end_bits = len % 8;
3552 if( end_bits != 0 )
3553 {
3554 size_t last_byte_idx = ( offset + len ) / 8;
3555
3556 len -= end_bits; /* Now len % 8 == 0 */
3557
3558 for( ; end_bits != 0; end_bits-- )
3559 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3560 }
3561
3562 memset( mask + offset / 8, 0xFF, len / 8 );
3563}
3564
3565/*
3566 * Check that bitmask is full
3567 */
3568static int ssl_bitmask_check( unsigned char *mask, size_t len )
3569{
3570 size_t i;
3571
3572 for( i = 0; i < len / 8; i++ )
3573 if( mask[i] != 0xFF )
3574 return( -1 );
3575
3576 for( i = 0; i < len % 8; i++ )
3577 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3578 return( -1 );
3579
3580 return( 0 );
3581}
3582
Hanno Becker56e205e2018-08-16 09:06:12 +01003583/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003584static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003585 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003586{
Hanno Becker56e205e2018-08-16 09:06:12 +01003587 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003588
Hanno Becker56e205e2018-08-16 09:06:12 +01003589 alloc_len = 12; /* Handshake header */
3590 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003591
Hanno Beckerd07df862018-08-16 09:14:58 +01003592 if( add_bitmap )
3593 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003594
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003595 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003596}
Hanno Becker56e205e2018-08-16 09:06:12 +01003597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003598#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003599
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003600static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003601{
3602 return( ( ssl->in_msg[1] << 16 ) |
3603 ( ssl->in_msg[2] << 8 ) |
3604 ssl->in_msg[3] );
3605}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003606
Simon Butcher99000142016-10-13 17:21:01 +01003607int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003608{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003609 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003612 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003614 }
3615
Hanno Becker12555c62018-08-16 12:47:53 +01003616 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003619 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003620 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003623 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003624 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003625 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003626 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003627
Hanno Becker44650b72018-08-16 12:51:11 +01003628 if( ssl_check_hs_header( ssl ) != 0 )
3629 {
3630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3631 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3632 }
3633
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003634 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003635 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3636 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3637 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3638 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003639 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003640 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3641 {
3642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3643 recv_msg_seq,
3644 ssl->handshake->in_msg_seq ) );
3645 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3646 }
3647
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003648 /* Retransmit only on last message from previous flight, to avoid
3649 * too many retransmissions.
3650 * Besides, No sane server ever retransmits HelloVerifyRequest */
3651 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003655 "message_seq = %d, start_of_flight = %d",
3656 recv_msg_seq,
3657 ssl->handshake->in_flight_start_seq ) );
3658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003662 return( ret );
3663 }
3664 }
3665 else
3666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003668 "message_seq = %d, expected = %d",
3669 recv_msg_seq,
3670 ssl->handshake->in_msg_seq ) );
3671 }
3672
Hanno Becker90333da2017-10-10 11:27:13 +01003673 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003674 }
3675 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003676
Hanno Becker6d97ef52018-08-16 13:09:04 +01003677 /* Message reassembly is handled alongside buffering of future
3678 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003679 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003680 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003681 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003684 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003685 }
3686 }
3687 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003689 /* With TLS we don't handle fragmentation (for now) */
3690 if( ssl->in_msglen < ssl->in_hslen )
3691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3693 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003694 }
3695
Simon Butcher99000142016-10-13 17:21:01 +01003696 return( 0 );
3697}
3698
3699void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3700{
Hanno Becker0271f962018-08-16 13:23:47 +01003701 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003702
Hanno Becker0271f962018-08-16 13:23:47 +01003703 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003704 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003705 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003706 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003707
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003708 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003709#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003710 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003711 ssl->handshake != NULL )
3712 {
Hanno Becker0271f962018-08-16 13:23:47 +01003713 unsigned offset;
3714 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003715
Hanno Becker0271f962018-08-16 13:23:47 +01003716 /* Increment handshake sequence number */
3717 hs->in_msg_seq++;
3718
3719 /*
3720 * Clear up handshake buffering and reassembly structure.
3721 */
3722
3723 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003724 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003725
3726 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003727 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3728 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003729 offset++, hs_buf++ )
3730 {
3731 *hs_buf = *(hs_buf + 1);
3732 }
3733
3734 /* Create a fresh last entry */
3735 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003736 }
3737#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003738}
3739
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003740/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003741 * DTLS anti-replay: RFC 6347 4.1.2.6
3742 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003743 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3744 * Bit n is set iff record number in_window_top - n has been seen.
3745 *
3746 * Usually, in_window_top is the last record number seen and the lsb of
3747 * in_window is set. The only exception is the initial state (record number 0
3748 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003750#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3751static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003752{
3753 ssl->in_window_top = 0;
3754 ssl->in_window = 0;
3755}
3756
3757static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3758{
3759 return( ( (uint64_t) buf[0] << 40 ) |
3760 ( (uint64_t) buf[1] << 32 ) |
3761 ( (uint64_t) buf[2] << 24 ) |
3762 ( (uint64_t) buf[3] << 16 ) |
3763 ( (uint64_t) buf[4] << 8 ) |
3764 ( (uint64_t) buf[5] ) );
3765}
3766
3767/*
3768 * Return 0 if sequence number is acceptable, -1 otherwise
3769 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003771{
3772 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3773 uint64_t bit;
3774
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003775 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003776 return( 0 );
3777
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003778 if( rec_seqnum > ssl->in_window_top )
3779 return( 0 );
3780
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003781 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003782
3783 if( bit >= 64 )
3784 return( -1 );
3785
3786 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3787 return( -1 );
3788
3789 return( 0 );
3790}
3791
3792/*
3793 * Update replay window on new validated record
3794 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003796{
3797 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3798
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003799 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003800 return;
3801
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003802 if( rec_seqnum > ssl->in_window_top )
3803 {
3804 /* Update window_top and the contents of the window */
3805 uint64_t shift = rec_seqnum - ssl->in_window_top;
3806
3807 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003808 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003809 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003810 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003811 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003812 ssl->in_window |= 1;
3813 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003814
3815 ssl->in_window_top = rec_seqnum;
3816 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003817 else
3818 {
3819 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003820 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003821
3822 if( bit < 64 ) /* Always true, but be extra sure */
3823 ssl->in_window |= (uint64_t) 1 << bit;
3824 }
3825}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003826#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003827
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003828#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003829/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003830static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3831
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003832/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003833 * Without any SSL context, check if a datagram looks like a ClientHello with
3834 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003835 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003836 *
3837 * - if cookie is valid, return 0
3838 * - if ClientHello looks superficially valid but cookie is not,
3839 * fill obuf and set olen, then
3840 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3841 * - otherwise return a specific error code
3842 */
3843static int ssl_check_dtls_clihlo_cookie(
3844 mbedtls_ssl_cookie_write_t *f_cookie_write,
3845 mbedtls_ssl_cookie_check_t *f_cookie_check,
3846 void *p_cookie,
3847 const unsigned char *cli_id, size_t cli_id_len,
3848 const unsigned char *in, size_t in_len,
3849 unsigned char *obuf, size_t buf_len, size_t *olen )
3850{
3851 size_t sid_len, cookie_len;
3852 unsigned char *p;
3853
3854 if( f_cookie_write == NULL || f_cookie_check == NULL )
3855 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3856
3857 /*
3858 * Structure of ClientHello with record and handshake headers,
3859 * and expected values. We don't need to check a lot, more checks will be
3860 * done when actually parsing the ClientHello - skipping those checks
3861 * avoids code duplication and does not make cookie forging any easier.
3862 *
3863 * 0-0 ContentType type; copied, must be handshake
3864 * 1-2 ProtocolVersion version; copied
3865 * 3-4 uint16 epoch; copied, must be 0
3866 * 5-10 uint48 sequence_number; copied
3867 * 11-12 uint16 length; (ignored)
3868 *
3869 * 13-13 HandshakeType msg_type; (ignored)
3870 * 14-16 uint24 length; (ignored)
3871 * 17-18 uint16 message_seq; copied
3872 * 19-21 uint24 fragment_offset; copied, must be 0
3873 * 22-24 uint24 fragment_length; (ignored)
3874 *
3875 * 25-26 ProtocolVersion client_version; (ignored)
3876 * 27-58 Random random; (ignored)
3877 * 59-xx SessionID session_id; 1 byte len + sid_len content
3878 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3879 * ...
3880 *
3881 * Minimum length is 61 bytes.
3882 */
3883 if( in_len < 61 ||
3884 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3885 in[3] != 0 || in[4] != 0 ||
3886 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3887 {
3888 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3889 }
3890
3891 sid_len = in[59];
3892 if( sid_len > in_len - 61 )
3893 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3894
3895 cookie_len = in[60 + sid_len];
3896 if( cookie_len > in_len - 60 )
3897 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3898
3899 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3900 cli_id, cli_id_len ) == 0 )
3901 {
3902 /* Valid cookie */
3903 return( 0 );
3904 }
3905
3906 /*
3907 * If we get here, we've got an invalid cookie, let's prepare HVR.
3908 *
3909 * 0-0 ContentType type; copied
3910 * 1-2 ProtocolVersion version; copied
3911 * 3-4 uint16 epoch; copied
3912 * 5-10 uint48 sequence_number; copied
3913 * 11-12 uint16 length; olen - 13
3914 *
3915 * 13-13 HandshakeType msg_type; hello_verify_request
3916 * 14-16 uint24 length; olen - 25
3917 * 17-18 uint16 message_seq; copied
3918 * 19-21 uint24 fragment_offset; copied
3919 * 22-24 uint24 fragment_length; olen - 25
3920 *
3921 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3922 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3923 *
3924 * Minimum length is 28.
3925 */
3926 if( buf_len < 28 )
3927 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3928
3929 /* Copy most fields and adapt others */
3930 memcpy( obuf, in, 25 );
3931 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3932 obuf[25] = 0xfe;
3933 obuf[26] = 0xff;
3934
3935 /* Generate and write actual cookie */
3936 p = obuf + 28;
3937 if( f_cookie_write( p_cookie,
3938 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3939 {
3940 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3941 }
3942
3943 *olen = p - obuf;
3944
3945 /* Go back and fill length fields */
3946 obuf[27] = (unsigned char)( *olen - 28 );
3947
3948 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3949 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3950 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3951
3952 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3953 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3954
3955 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3956}
3957
3958/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003959 * Handle possible client reconnect with the same UDP quadruplet
3960 * (RFC 6347 Section 4.2.8).
3961 *
3962 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3963 * that looks like a ClientHello.
3964 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003965 * - if the input looks like a ClientHello without cookies,
3966 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003967 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003968 * - if the input looks like a ClientHello with a valid cookie,
3969 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003970 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003971 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003972 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003973 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003974 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3975 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003976 */
3977static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3978{
3979 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003980 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003981
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003982 ret = ssl_check_dtls_clihlo_cookie(
3983 ssl->conf->f_cookie_write,
3984 ssl->conf->f_cookie_check,
3985 ssl->conf->p_cookie,
3986 ssl->cli_id, ssl->cli_id_len,
3987 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003988 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003989
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003990 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3991
3992 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003993 {
Brian J Murray1903fb32016-11-06 04:45:15 -08003994 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003995 * If the error is permanent we'll catch it later,
3996 * if it's not, then hopefully it'll work next time. */
3997 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3998
3999 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004000 }
4001
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004002 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004003 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004004 /* Got a valid cookie, partially reset context */
4005 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4006 {
4007 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4008 return( ret );
4009 }
4010
4011 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004012 }
4013
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004014 return( ret );
4015}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004016#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004017
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004018/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004019 * ContentType type;
4020 * ProtocolVersion version;
4021 * uint16 epoch; // DTLS only
4022 * uint48 sequence_number; // DTLS only
4023 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004024 *
4025 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004026 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004027 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4028 *
4029 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004030 * 1. proceed with the record if this function returns 0
4031 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4032 * 3. return CLIENT_RECONNECT if this function return that value
4033 * 4. drop the whole datagram if this function returns anything else.
4034 * Point 2 is needed when the peer is resending, and we have already received
4035 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004036 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004037static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004038{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004039 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004041 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 +02004042
Paul Bakker5121ce52009-01-03 21:22:43 +00004043 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004044 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004045 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004047 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004048 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004049 ssl->in_msgtype,
4050 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004051
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004052 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004053 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4054 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4055 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4056 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004058 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004059
4060#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004061 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4062 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004063 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4064#endif /* MBEDTLS_SSL_PROTO_DTLS */
4065 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4066 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004068 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004069 }
4070
4071 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004072 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4075 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004076 }
4077
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004078 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4081 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004082 }
4083
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004084 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004085 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004086 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004088 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4089 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004090 }
4091
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004092 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004093 * DTLS-related tests.
4094 * Check epoch before checking length constraint because
4095 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4096 * message gets duplicated before the corresponding Finished message,
4097 * the second ChangeCipherSpec should be discarded because it belongs
4098 * to an old epoch, but not because its length is shorter than
4099 * the minimum record length for packets using the new record transform.
4100 * Note that these two kinds of failures are handled differently,
4101 * as an unexpected record is silently skipped but an invalid
4102 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004103 */
4104#if defined(MBEDTLS_SSL_PROTO_DTLS)
4105 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4106 {
4107 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4108
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004109 /* Check epoch (and sequence number) with DTLS */
4110 if( rec_epoch != ssl->in_epoch )
4111 {
4112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4113 "expected %d, received %d",
4114 ssl->in_epoch, rec_epoch ) );
4115
4116#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4117 /*
4118 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4119 * access the first byte of record content (handshake type), as we
4120 * have an active transform (possibly iv_len != 0), so use the
4121 * fact that the record header len is 13 instead.
4122 */
4123 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4124 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4125 rec_epoch == 0 &&
4126 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4127 ssl->in_left > 13 &&
4128 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4129 {
4130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4131 "from the same port" ) );
4132 return( ssl_handle_possible_reconnect( ssl ) );
4133 }
4134 else
4135#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004136 {
4137 /* Consider buffering the record. */
4138 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4139 {
4140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4141 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4142 }
4143
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004144 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004145 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004146 }
4147
4148#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4149 /* Replay detection only works for the current epoch */
4150 if( rec_epoch == ssl->in_epoch &&
4151 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4152 {
4153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4154 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4155 }
4156#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004157
Hanno Becker52c6dc62017-05-26 16:07:36 +01004158 /* Drop unexpected ApplicationData records,
4159 * except at the beginning of renegotiations */
4160 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4161 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4162#if defined(MBEDTLS_SSL_RENEGOTIATION)
4163 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4164 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4165#endif
4166 )
4167 {
4168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4169 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4170 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004171 }
4172#endif /* MBEDTLS_SSL_PROTO_DTLS */
4173
Hanno Becker52c6dc62017-05-26 16:07:36 +01004174
4175 /* Check length against bounds of the current transform and version */
4176 if( ssl->transform_in == NULL )
4177 {
4178 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004179 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004180 {
4181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4182 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4183 }
4184 }
4185 else
4186 {
4187 if( ssl->in_msglen < ssl->transform_in->minlen )
4188 {
4189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4190 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4191 }
4192
4193#if defined(MBEDTLS_SSL_PROTO_SSL3)
4194 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004195 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004196 {
4197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4198 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4199 }
4200#endif
4201#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4202 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4203 /*
4204 * TLS encrypted messages can have up to 256 bytes of padding
4205 */
4206 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4207 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004208 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004209 {
4210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4211 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4212 }
4213#endif
4214 }
4215
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004216 return( 0 );
4217}
Paul Bakker5121ce52009-01-03 21:22:43 +00004218
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004219/*
4220 * If applicable, decrypt (and decompress) record content
4221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004223{
4224 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004226 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4227 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004229#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4230 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234 ret = mbedtls_ssl_hw_record_read( ssl );
4235 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4238 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004239 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004240
4241 if( ret == 0 )
4242 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004243 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004244#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004245 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004246 {
4247 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004249 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004250 return( ret );
4251 }
4252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004253 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004254 ssl->in_msg, ssl->in_msglen );
4255
Angus Grattond8213d02016-05-25 20:56:48 +10004256 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4259 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004260 }
4261 }
4262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004263#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004264 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004265 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004266 {
4267 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004269 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004270 return( ret );
4271 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004272 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004273#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004275#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004276 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004278 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004279 }
4280#endif
4281
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004282 return( 0 );
4283}
4284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004285static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004286
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004287/*
4288 * Read a record.
4289 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004290 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4291 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4292 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004293 */
Hanno Becker1097b342018-08-15 14:09:41 +01004294
4295/* Helper functions for mbedtls_ssl_read_record(). */
4296static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004297static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4298static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004299
Hanno Becker327c93b2018-08-15 13:56:18 +01004300int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004301 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004302{
4303 int ret;
4304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004306
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004307 if( ssl->keep_current_message == 0 )
4308 {
4309 do {
Simon Butcher99000142016-10-13 17:21:01 +01004310
Hanno Becker26994592018-08-15 14:14:59 +01004311 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004312 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004313 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004314
Hanno Beckere74d5562018-08-15 14:26:08 +01004315 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004316 {
Hanno Becker40f50842018-08-15 14:48:01 +01004317#if defined(MBEDTLS_SSL_PROTO_DTLS)
4318 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004319
Hanno Becker40f50842018-08-15 14:48:01 +01004320 /* We only check for buffered messages if the
4321 * current datagram is fully consumed. */
4322 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004323 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004324 {
Hanno Becker40f50842018-08-15 14:48:01 +01004325 if( ssl_load_buffered_message( ssl ) == 0 )
4326 have_buffered = 1;
4327 }
4328
4329 if( have_buffered == 0 )
4330#endif /* MBEDTLS_SSL_PROTO_DTLS */
4331 {
4332 ret = ssl_get_next_record( ssl );
4333 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4334 continue;
4335
4336 if( ret != 0 )
4337 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004338 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004339 return( ret );
4340 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004341 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004342 }
4343
4344 ret = mbedtls_ssl_handle_message_type( ssl );
4345
Hanno Becker40f50842018-08-15 14:48:01 +01004346#if defined(MBEDTLS_SSL_PROTO_DTLS)
4347 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4348 {
4349 /* Buffer future message */
4350 ret = ssl_buffer_message( ssl );
4351 if( ret != 0 )
4352 return( ret );
4353
4354 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4355 }
4356#endif /* MBEDTLS_SSL_PROTO_DTLS */
4357
Hanno Becker90333da2017-10-10 11:27:13 +01004358 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4359 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004360
4361 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004362 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004363 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004364 return( ret );
4365 }
4366
Hanno Becker327c93b2018-08-15 13:56:18 +01004367 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004368 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004369 {
4370 mbedtls_ssl_update_handshake_status( ssl );
4371 }
Simon Butcher99000142016-10-13 17:21:01 +01004372 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004373 else
Simon Butcher99000142016-10-13 17:21:01 +01004374 {
Hanno Becker02f59072018-08-15 14:00:24 +01004375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004376 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004377 }
4378
4379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4380
4381 return( 0 );
4382}
4383
Hanno Becker40f50842018-08-15 14:48:01 +01004384#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004385static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004386{
Hanno Becker40f50842018-08-15 14:48:01 +01004387 if( ssl->in_left > ssl->next_record_offset )
4388 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004389
Hanno Becker40f50842018-08-15 14:48:01 +01004390 return( 0 );
4391}
4392
4393static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4394{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004395 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004396 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004397 int ret = 0;
4398
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004399 if( hs == NULL )
4400 return( -1 );
4401
Hanno Beckere00ae372018-08-20 09:39:42 +01004402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4403
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004404 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4405 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4406 {
4407 /* Check if we have seen a ChangeCipherSpec before.
4408 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004409 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004410 {
4411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4412 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004413 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004414 }
4415
Hanno Becker39b8bc92018-08-28 17:17:13 +01004416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004417 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4418 ssl->in_msglen = 1;
4419 ssl->in_msg[0] = 1;
4420
4421 /* As long as they are equal, the exact value doesn't matter. */
4422 ssl->in_left = 0;
4423 ssl->next_record_offset = 0;
4424
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004425 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004426 goto exit;
4427 }
Hanno Becker37f95322018-08-16 13:55:32 +01004428
Hanno Beckerb8f50142018-08-28 10:01:34 +01004429#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004430 /* Debug only */
4431 {
4432 unsigned offset;
4433 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4434 {
4435 hs_buf = &hs->buffering.hs[offset];
4436 if( hs_buf->is_valid == 1 )
4437 {
4438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4439 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004440 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004441 }
4442 }
4443 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004444#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004445
4446 /* Check if we have buffered and/or fully reassembled the
4447 * next handshake message. */
4448 hs_buf = &hs->buffering.hs[0];
4449 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4450 {
4451 /* Synthesize a record containing the buffered HS message. */
4452 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4453 ( hs_buf->data[2] << 8 ) |
4454 hs_buf->data[3];
4455
4456 /* Double-check that we haven't accidentally buffered
4457 * a message that doesn't fit into the input buffer. */
4458 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4459 {
4460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4461 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4462 }
4463
4464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4465 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4466 hs_buf->data, msg_len + 12 );
4467
4468 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4469 ssl->in_hslen = msg_len + 12;
4470 ssl->in_msglen = msg_len + 12;
4471 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4472
4473 ret = 0;
4474 goto exit;
4475 }
4476 else
4477 {
4478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4479 hs->in_msg_seq ) );
4480 }
4481
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004482 ret = -1;
4483
4484exit:
4485
4486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4487 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004488}
4489
Hanno Beckera02b0b42018-08-21 17:20:27 +01004490static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4491 size_t desired )
4492{
4493 int offset;
4494 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4496 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004497
Hanno Becker01315ea2018-08-21 17:22:17 +01004498 /* Get rid of future records epoch first, if such exist. */
4499 ssl_free_buffered_record( ssl );
4500
4501 /* Check if we have enough space available now. */
4502 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4503 hs->buffering.total_bytes_buffered ) )
4504 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004506 return( 0 );
4507 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004508
Hanno Becker4f432ad2018-08-28 10:02:32 +01004509 /* We don't have enough space to buffer the next expected handshake
4510 * message. Remove buffers used for future messages to gain space,
4511 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004512 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4513 offset >= 0; offset-- )
4514 {
4515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4516 offset ) );
4517
Hanno Beckerb309b922018-08-23 13:18:05 +01004518 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004519
4520 /* Check if we have enough space available now. */
4521 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4522 hs->buffering.total_bytes_buffered ) )
4523 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004525 return( 0 );
4526 }
4527 }
4528
4529 return( -1 );
4530}
4531
Hanno Becker40f50842018-08-15 14:48:01 +01004532static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4533{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004534 int ret = 0;
4535 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4536
4537 if( hs == NULL )
4538 return( 0 );
4539
4540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4541
4542 switch( ssl->in_msgtype )
4543 {
4544 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004546
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004547 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004548 break;
4549
4550 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004551 {
4552 unsigned recv_msg_seq_offset;
4553 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4554 mbedtls_ssl_hs_buffer *hs_buf;
4555 size_t msg_len = ssl->in_hslen - 12;
4556
4557 /* We should never receive an old handshake
4558 * message - double-check nonetheless. */
4559 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4560 {
4561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4562 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4563 }
4564
4565 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4566 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4567 {
4568 /* Silently ignore -- message too far in the future */
4569 MBEDTLS_SSL_DEBUG_MSG( 2,
4570 ( "Ignore future HS message with sequence number %u, "
4571 "buffering window %u - %u",
4572 recv_msg_seq, ssl->handshake->in_msg_seq,
4573 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4574
4575 goto exit;
4576 }
4577
4578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4579 recv_msg_seq, recv_msg_seq_offset ) );
4580
4581 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4582
4583 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004584 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004585 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004586 size_t reassembly_buf_sz;
4587
Hanno Becker37f95322018-08-16 13:55:32 +01004588 hs_buf->is_fragmented =
4589 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4590
4591 /* We copy the message back into the input buffer
4592 * after reassembly, so check that it's not too large.
4593 * This is an implementation-specific limitation
4594 * and not one from the standard, hence it is not
4595 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004596 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004597 {
4598 /* Ignore message */
4599 goto exit;
4600 }
4601
Hanno Beckere0b150f2018-08-21 15:51:03 +01004602 /* Check if we have enough space to buffer the message. */
4603 if( hs->buffering.total_bytes_buffered >
4604 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4605 {
4606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4607 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4608 }
4609
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004610 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4611 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004612
4613 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4614 hs->buffering.total_bytes_buffered ) )
4615 {
4616 if( recv_msg_seq_offset > 0 )
4617 {
4618 /* If we can't buffer a future message because
4619 * of space limitations -- ignore. */
4620 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",
4621 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4622 (unsigned) hs->buffering.total_bytes_buffered ) );
4623 goto exit;
4624 }
Hanno Beckere1801392018-08-21 16:51:05 +01004625 else
4626 {
4627 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",
4628 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4629 (unsigned) hs->buffering.total_bytes_buffered ) );
4630 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004631
Hanno Beckera02b0b42018-08-21 17:20:27 +01004632 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004633 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004634 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",
4635 (unsigned) msg_len,
4636 (unsigned) reassembly_buf_sz,
4637 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004638 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004639 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4640 goto exit;
4641 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004642 }
4643
4644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4645 msg_len ) );
4646
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004647 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4648 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004649 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004650 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004651 goto exit;
4652 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004653 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004654
4655 /* Prepare final header: copy msg_type, length and message_seq,
4656 * then add standardised fragment_offset and fragment_length */
4657 memcpy( hs_buf->data, ssl->in_msg, 6 );
4658 memset( hs_buf->data + 6, 0, 3 );
4659 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4660
4661 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004662
4663 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004664 }
4665 else
4666 {
4667 /* Make sure msg_type and length are consistent */
4668 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4669 {
4670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4671 /* Ignore */
4672 goto exit;
4673 }
4674 }
4675
Hanno Becker4422bbb2018-08-20 09:40:19 +01004676 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004677 {
4678 size_t frag_len, frag_off;
4679 unsigned char * const msg = hs_buf->data + 12;
4680
4681 /*
4682 * Check and copy current fragment
4683 */
4684
4685 /* Validation of header fields already done in
4686 * mbedtls_ssl_prepare_handshake_record(). */
4687 frag_off = ssl_get_hs_frag_off( ssl );
4688 frag_len = ssl_get_hs_frag_len( ssl );
4689
4690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4691 frag_off, frag_len ) );
4692 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4693
4694 if( hs_buf->is_fragmented )
4695 {
4696 unsigned char * const bitmask = msg + msg_len;
4697 ssl_bitmask_set( bitmask, frag_off, frag_len );
4698 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4699 msg_len ) == 0 );
4700 }
4701 else
4702 {
4703 hs_buf->is_complete = 1;
4704 }
4705
4706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4707 hs_buf->is_complete ? "" : "not yet " ) );
4708 }
4709
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004710 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004711 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004712
4713 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004714 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004715 break;
4716 }
4717
4718exit:
4719
4720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4721 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004722}
4723#endif /* MBEDTLS_SSL_PROTO_DTLS */
4724
Hanno Becker1097b342018-08-15 14:09:41 +01004725static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004726{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004727 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004728 * Consume last content-layer message and potentially
4729 * update in_msglen which keeps track of the contents'
4730 * consumption state.
4731 *
4732 * (1) Handshake messages:
4733 * Remove last handshake message, move content
4734 * and adapt in_msglen.
4735 *
4736 * (2) Alert messages:
4737 * Consume whole record content, in_msglen = 0.
4738 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004739 * (3) Change cipher spec:
4740 * Consume whole record content, in_msglen = 0.
4741 *
4742 * (4) Application data:
4743 * Don't do anything - the record layer provides
4744 * the application data as a stream transport
4745 * and consumes through mbedtls_ssl_read only.
4746 *
4747 */
4748
4749 /* Case (1): Handshake messages */
4750 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004751 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004752 /* Hard assertion to be sure that no application data
4753 * is in flight, as corrupting ssl->in_msglen during
4754 * ssl->in_offt != NULL is fatal. */
4755 if( ssl->in_offt != NULL )
4756 {
4757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4758 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4759 }
4760
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004761 /*
4762 * Get next Handshake message in the current record
4763 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004764
Hanno Becker4a810fb2017-05-24 16:27:30 +01004765 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004766 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004767 * current handshake content: If DTLS handshake
4768 * fragmentation is used, that's the fragment
4769 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004770 * size here is faulty and should be changed at
4771 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004772 * (2) While it doesn't seem to cause problems, one
4773 * has to be very careful not to assume that in_hslen
4774 * is always <= in_msglen in a sensible communication.
4775 * Again, it's wrong for DTLS handshake fragmentation.
4776 * The following check is therefore mandatory, and
4777 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004778 * Additionally, ssl->in_hslen might be arbitrarily out of
4779 * bounds after handling a DTLS message with an unexpected
4780 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004781 */
4782 if( ssl->in_hslen < ssl->in_msglen )
4783 {
4784 ssl->in_msglen -= ssl->in_hslen;
4785 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4786 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004787
Hanno Becker4a810fb2017-05-24 16:27:30 +01004788 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4789 ssl->in_msg, ssl->in_msglen );
4790 }
4791 else
4792 {
4793 ssl->in_msglen = 0;
4794 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004795
Hanno Becker4a810fb2017-05-24 16:27:30 +01004796 ssl->in_hslen = 0;
4797 }
4798 /* Case (4): Application data */
4799 else if( ssl->in_offt != NULL )
4800 {
4801 return( 0 );
4802 }
4803 /* Everything else (CCS & Alerts) */
4804 else
4805 {
4806 ssl->in_msglen = 0;
4807 }
4808
Hanno Becker1097b342018-08-15 14:09:41 +01004809 return( 0 );
4810}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004811
Hanno Beckere74d5562018-08-15 14:26:08 +01004812static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4813{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004814 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004815 return( 1 );
4816
4817 return( 0 );
4818}
4819
Hanno Becker5f066e72018-08-16 14:56:31 +01004820#if defined(MBEDTLS_SSL_PROTO_DTLS)
4821
4822static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4823{
4824 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4825 if( hs == NULL )
4826 return;
4827
Hanno Becker01315ea2018-08-21 17:22:17 +01004828 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004829 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004830 hs->buffering.total_bytes_buffered -=
4831 hs->buffering.future_record.len;
4832
4833 mbedtls_free( hs->buffering.future_record.data );
4834 hs->buffering.future_record.data = NULL;
4835 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004836}
4837
4838static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4839{
4840 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4841 unsigned char * rec;
4842 size_t rec_len;
4843 unsigned rec_epoch;
4844
4845 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4846 return( 0 );
4847
4848 if( hs == NULL )
4849 return( 0 );
4850
Hanno Becker5f066e72018-08-16 14:56:31 +01004851 rec = hs->buffering.future_record.data;
4852 rec_len = hs->buffering.future_record.len;
4853 rec_epoch = hs->buffering.future_record.epoch;
4854
4855 if( rec == NULL )
4856 return( 0 );
4857
Hanno Becker4cb782d2018-08-20 11:19:05 +01004858 /* Only consider loading future records if the
4859 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004860 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004861 return( 0 );
4862
Hanno Becker5f066e72018-08-16 14:56:31 +01004863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4864
4865 if( rec_epoch != ssl->in_epoch )
4866 {
4867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4868 goto exit;
4869 }
4870
4871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4872
4873 /* Double-check that the record is not too large */
4874 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4875 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4876 {
4877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4878 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4879 }
4880
4881 memcpy( ssl->in_hdr, rec, rec_len );
4882 ssl->in_left = rec_len;
4883 ssl->next_record_offset = 0;
4884
4885 ssl_free_buffered_record( ssl );
4886
4887exit:
4888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4889 return( 0 );
4890}
4891
4892static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
4893{
4894 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4895 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01004896 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01004897
4898 /* Don't buffer future records outside handshakes. */
4899 if( hs == NULL )
4900 return( 0 );
4901
4902 /* Only buffer handshake records (we are only interested
4903 * in Finished messages). */
4904 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
4905 return( 0 );
4906
4907 /* Don't buffer more than one future epoch record. */
4908 if( hs->buffering.future_record.data != NULL )
4909 return( 0 );
4910
Hanno Becker01315ea2018-08-21 17:22:17 +01004911 /* Don't buffer record if there's not enough buffering space remaining. */
4912 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4913 hs->buffering.total_bytes_buffered ) )
4914 {
4915 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",
4916 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4917 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004918 return( 0 );
4919 }
4920
Hanno Becker5f066e72018-08-16 14:56:31 +01004921 /* Buffer record */
4922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4923 ssl->in_epoch + 1 ) );
4924 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
4925 rec_hdr_len + ssl->in_msglen );
4926
4927 /* ssl_parse_record_header() only considers records
4928 * of the next epoch as candidates for buffering. */
4929 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01004930 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004931
4932 hs->buffering.future_record.data =
4933 mbedtls_calloc( 1, hs->buffering.future_record.len );
4934 if( hs->buffering.future_record.data == NULL )
4935 {
4936 /* If we run out of RAM trying to buffer a
4937 * record from the next epoch, just ignore. */
4938 return( 0 );
4939 }
4940
Hanno Becker01315ea2018-08-21 17:22:17 +01004941 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01004942
Hanno Becker01315ea2018-08-21 17:22:17 +01004943 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004944 return( 0 );
4945}
4946
4947#endif /* MBEDTLS_SSL_PROTO_DTLS */
4948
Hanno Beckere74d5562018-08-15 14:26:08 +01004949static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004950{
4951 int ret;
4952
Hanno Becker5f066e72018-08-16 14:56:31 +01004953#if defined(MBEDTLS_SSL_PROTO_DTLS)
4954 /* We might have buffered a future record; if so,
4955 * and if the epoch matches now, load it.
4956 * On success, this call will set ssl->in_left to
4957 * the length of the buffered record, so that
4958 * the calls to ssl_fetch_input() below will
4959 * essentially be no-ops. */
4960 ret = ssl_load_buffered_record( ssl );
4961 if( ret != 0 )
4962 return( ret );
4963#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004965 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004967 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004968 return( ret );
4969 }
4970
4971 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004973#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004974 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4975 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004976 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004977 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4978 {
4979 ret = ssl_buffer_future_record( ssl );
4980 if( ret != 0 )
4981 return( ret );
4982
4983 /* Fall through to handling of unexpected records */
4984 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4985 }
4986
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004987 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4988 {
4989 /* Skip unexpected record (but not whole datagram) */
4990 ssl->next_record_offset = ssl->in_msglen
4991 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004992
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4994 "(header)" ) );
4995 }
4996 else
4997 {
4998 /* Skip invalid record and the rest of the datagram */
4999 ssl->next_record_offset = 0;
5000 ssl->in_left = 0;
5001
5002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5003 "(header)" ) );
5004 }
5005
5006 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005007 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005008 }
5009#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005010 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005011 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005012
5013 /*
5014 * Read and optionally decrypt the message contents
5015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005016 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5017 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005020 return( ret );
5021 }
5022
5023 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005024#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005025 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005027 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005028 if( ssl->next_record_offset < ssl->in_left )
5029 {
5030 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5031 }
5032 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005033 else
5034#endif
5035 ssl->in_left = 0;
5036
5037 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005039#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005040 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005041 {
5042 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005043 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5044 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005045 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005046 /* Except when waiting for Finished as a bad mac here
5047 * probably means something went wrong in the handshake
5048 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5049 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5050 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5051 {
5052#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5053 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5054 {
5055 mbedtls_ssl_send_alert_message( ssl,
5056 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5057 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5058 }
5059#endif
5060 return( ret );
5061 }
5062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005063#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005064 if( ssl->conf->badmac_limit != 0 &&
5065 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5068 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005069 }
5070#endif
5071
Hanno Becker4a810fb2017-05-24 16:27:30 +01005072 /* As above, invalid records cause
5073 * dismissal of the whole datagram. */
5074
5075 ssl->next_record_offset = 0;
5076 ssl->in_left = 0;
5077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005079 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005080 }
5081
5082 return( ret );
5083 }
5084 else
5085#endif
5086 {
5087 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005088#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5089 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005091 mbedtls_ssl_send_alert_message( ssl,
5092 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5093 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005094 }
5095#endif
5096 return( ret );
5097 }
5098 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005099
Simon Butcher99000142016-10-13 17:21:01 +01005100 return( 0 );
5101}
5102
5103int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5104{
5105 int ret;
5106
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005107 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005108 * Handle particular types of records
5109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005110 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005111 {
Simon Butcher99000142016-10-13 17:21:01 +01005112 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5113 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005114 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005115 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005116 }
5117
Hanno Beckere678eaa2018-08-21 14:57:46 +01005118 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005119 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005120 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005121 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5123 ssl->in_msglen ) );
5124 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005125 }
5126
Hanno Beckere678eaa2018-08-21 14:57:46 +01005127 if( ssl->in_msg[0] != 1 )
5128 {
5129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5130 ssl->in_msg[0] ) );
5131 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5132 }
5133
5134#if defined(MBEDTLS_SSL_PROTO_DTLS)
5135 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5136 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5137 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5138 {
5139 if( ssl->handshake == NULL )
5140 {
5141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5142 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5143 }
5144
5145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5146 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5147 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005148#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005149 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005151 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005152 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005153 if( ssl->in_msglen != 2 )
5154 {
5155 /* Note: Standard allows for more than one 2 byte alert
5156 to be packed in a single message, but Mbed TLS doesn't
5157 currently support this. */
5158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5159 ssl->in_msglen ) );
5160 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5161 }
5162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005164 ssl->in_msg[0], ssl->in_msg[1] ) );
5165
5166 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005167 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005169 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005172 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005173 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005174 }
5175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005176 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5177 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5180 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005181 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005182
5183#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5184 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5185 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5186 {
Hanno Becker90333da2017-10-10 11:27:13 +01005187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005188 /* Will be handled when trying to parse ServerHello */
5189 return( 0 );
5190 }
5191#endif
5192
5193#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5194 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5195 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5196 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5197 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5198 {
5199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5200 /* Will be handled in mbedtls_ssl_parse_certificate() */
5201 return( 0 );
5202 }
5203#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5204
5205 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005206 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005207 }
5208
Hanno Beckerc76c6192017-06-06 10:03:17 +01005209#if defined(MBEDTLS_SSL_PROTO_DTLS)
5210 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5211 ssl->handshake != NULL &&
5212 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5213 {
5214 ssl_handshake_wrapup_free_hs_transform( ssl );
5215 }
5216#endif
5217
Paul Bakker5121ce52009-01-03 21:22:43 +00005218 return( 0 );
5219}
5220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005221int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005222{
5223 int ret;
5224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005225 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5226 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5227 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005228 {
5229 return( ret );
5230 }
5231
5232 return( 0 );
5233}
5234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005235int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005236 unsigned char level,
5237 unsigned char message )
5238{
5239 int ret;
5240
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005241 if( ssl == NULL || ssl->conf == NULL )
5242 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005245 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005247 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005248 ssl->out_msglen = 2;
5249 ssl->out_msg[0] = level;
5250 ssl->out_msg[1] = message;
5251
Hanno Becker67bc7c32018-08-06 11:33:50 +01005252 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005254 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005255 return( ret );
5256 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005258
5259 return( 0 );
5260}
5261
Paul Bakker5121ce52009-01-03 21:22:43 +00005262/*
5263 * Handshake functions
5264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005265#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5266 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5267 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5268 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5269 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5270 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5271 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005272/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005273int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005274{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005275 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005279 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5280 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005281 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5282 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005285 ssl->state++;
5286 return( 0 );
5287 }
5288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5290 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005291}
5292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005293int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005294{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005295 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005297 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005299 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5300 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005301 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5302 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005305 ssl->state++;
5306 return( 0 );
5307 }
5308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5310 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005311}
Gilles Peskinef9828522017-05-03 12:28:43 +02005312
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005313#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005314/* Some certificate support -> implement write and parse */
5315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005316int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005317{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005318 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005319 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005320 const mbedtls_x509_crt *crt;
5321 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005325 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5326 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005327 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5328 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005331 ssl->state++;
5332 return( 0 );
5333 }
5334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005335#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005336 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005337 {
5338 if( ssl->client_auth == 0 )
5339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005341 ssl->state++;
5342 return( 0 );
5343 }
5344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005345#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005346 /*
5347 * If using SSLv3 and got no cert, send an Alert message
5348 * (otherwise an empty Certificate message will be sent).
5349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005350 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5351 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005352 {
5353 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005354 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5355 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5356 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005359 goto write_msg;
5360 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005361#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005362 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005363#endif /* MBEDTLS_SSL_CLI_C */
5364#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005365 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005367 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5370 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005371 }
5372 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005373#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005375 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005376
5377 /*
5378 * 0 . 0 handshake type
5379 * 1 . 3 handshake length
5380 * 4 . 6 length of all certs
5381 * 7 . 9 length of cert. 1
5382 * 10 . n-1 peer certificate
5383 * n . n+2 length of cert. 2
5384 * n+3 . ... upper level cert, etc.
5385 */
5386 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005387 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005388
Paul Bakker29087132010-03-21 21:03:34 +00005389 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005390 {
5391 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005392 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005395 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005396 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005397 }
5398
5399 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5400 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5401 ssl->out_msg[i + 2] = (unsigned char)( n );
5402
5403 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5404 i += n; crt = crt->next;
5405 }
5406
5407 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5408 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5409 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5410
5411 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005412 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5413 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005414
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005415#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005416write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005417#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005418
5419 ssl->state++;
5420
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005421 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005422 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005424 return( ret );
5425 }
5426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005428
Paul Bakkered27a042013-04-18 22:46:23 +02005429 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005430}
5431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005432int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005433{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005434 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00005435 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005436 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005437 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005438 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005442 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5443 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005444 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5445 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005448 ssl->state++;
5449 return( 0 );
5450 }
5451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005453 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005454 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5455 {
5456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5457 ssl->state++;
5458 return( 0 );
5459 }
5460
5461#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5462 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
5463 authmode = ssl->handshake->sni_authmode;
5464#endif
5465
5466 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5467 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005468 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005469 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005471 ssl->state++;
5472 return( 0 );
5473 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005474#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005475
Hanno Becker327c93b2018-08-15 13:56:18 +01005476 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005477 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005478 /* mbedtls_ssl_read_record may have sent an alert already. We
5479 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005481 return( ret );
5482 }
5483
5484 ssl->state++;
5485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005486#if defined(MBEDTLS_SSL_SRV_C)
5487#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005488 /*
5489 * Check if the client sent an empty certificate
5490 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005491 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005492 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005493 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005494 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005495 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5496 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5497 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005500
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005501 /* The client was asked for a certificate but didn't send
5502 one. The client should know what's going on, so we
5503 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005504 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005505 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005506 return( 0 );
5507 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005508 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005509 }
5510 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005511#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005513#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5514 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005515 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005516 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005518 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5519 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5520 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5521 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005524
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005525 /* The client was asked for a certificate but didn't send
5526 one. The client should know what's going on, so we
5527 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005528 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005529 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005530 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005531 else
5532 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005533 }
5534 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005535#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5536 MBEDTLS_SSL_PROTO_TLS1_2 */
5537#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005539 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005542 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5543 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005544 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005545 }
5546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005547 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5548 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005551 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5552 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005553 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005554 }
5555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005556 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005557
Paul Bakker5121ce52009-01-03 21:22:43 +00005558 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005559 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005560 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005561 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005562
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005563 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005564 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005567 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5568 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005569 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005570 }
5571
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005572 /* In case we tried to reuse a session but it failed */
5573 if( ssl->session_negotiate->peer_cert != NULL )
5574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005575 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5576 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005577 }
5578
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005579 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005580 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005581 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005583 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005584 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5585 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005586 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005587 }
5588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005589 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005590
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005591 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005592
5593 while( i < ssl->in_hslen )
5594 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005595 if ( i + 3 > ssl->in_hslen ) {
5596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5597 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5598 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5599 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5600 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005601 if( ssl->in_msg[i] != 0 )
5602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005604 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5605 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005606 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005607 }
5608
5609 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5610 | (unsigned int) ssl->in_msg[i + 2];
5611 i += 3;
5612
5613 if( n < 128 || i + n > ssl->in_hslen )
5614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005616 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5617 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005618 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005619 }
5620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005621 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005622 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005623 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005624 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005625 case 0: /*ok*/
5626 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5627 /* Ignore certificate with an unknown algorithm: maybe a
5628 prior certificate was already trusted. */
5629 break;
5630
5631 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5632 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5633 goto crt_parse_der_failed;
5634
5635 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5636 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5637 goto crt_parse_der_failed;
5638
5639 default:
5640 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5641 crt_parse_der_failed:
5642 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005643 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005644 return( ret );
5645 }
5646
5647 i += n;
5648 }
5649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005650 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005651
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005652 /*
5653 * On client, make sure the server cert doesn't change during renego to
5654 * avoid "triple handshake" attack: https://secure-resumption.com/
5655 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005656#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005657 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005658 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005659 {
5660 if( ssl->session->peer_cert == NULL )
5661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005663 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5664 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005665 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005666 }
5667
5668 if( ssl->session->peer_cert->raw.len !=
5669 ssl->session_negotiate->peer_cert->raw.len ||
5670 memcmp( ssl->session->peer_cert->raw.p,
5671 ssl->session_negotiate->peer_cert->raw.p,
5672 ssl->session->peer_cert->raw.len ) != 0 )
5673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005675 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5676 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005678 }
5679 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005680#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005681
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005682 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005683 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005684 mbedtls_x509_crt *ca_chain;
5685 mbedtls_x509_crl *ca_crl;
5686
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005687#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005688 if( ssl->handshake->sni_ca_chain != NULL )
5689 {
5690 ca_chain = ssl->handshake->sni_ca_chain;
5691 ca_crl = ssl->handshake->sni_ca_crl;
5692 }
5693 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005694#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005695 {
5696 ca_chain = ssl->conf->ca_chain;
5697 ca_crl = ssl->conf->ca_crl;
5698 }
5699
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005700 /*
5701 * Main check: verify certificate
5702 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005703 ret = mbedtls_x509_crt_verify_with_profile(
5704 ssl->session_negotiate->peer_cert,
5705 ca_chain, ca_crl,
5706 ssl->conf->cert_profile,
5707 ssl->hostname,
5708 &ssl->session_negotiate->verify_result,
5709 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00005710
5711 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005713 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005714 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005715
5716 /*
5717 * Secondary checks: always done, but change 'ret' only if it was 0
5718 */
5719
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005720#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005722 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005723
5724 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005726 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005727 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005728 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005731 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005732 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005733 }
5734 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005735#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005737 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005738 ciphersuite_info,
5739 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005740 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005743 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005744 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005745 }
5746
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005747 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5748 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5749 * with details encoded in the verification flags. All other kinds
5750 * of error codes, including those from the user provided f_vrfy
5751 * functions, are treated as fatal and lead to a failure of
5752 * ssl_parse_certificate even if verification was optional. */
5753 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5754 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5755 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5756 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005757 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005758 }
5759
5760 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5761 {
5762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5763 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5764 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005765
5766 if( ret != 0 )
5767 {
5768 /* The certificate may have been rejected for several reasons.
5769 Pick one and send the corresponding alert. Which alert to send
5770 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005771 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5772 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5773 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5774 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5775 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5776 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5777 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5778 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5779 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5780 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5781 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5782 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5783 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5784 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5785 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5786 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5787 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5788 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5789 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5790 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005791 else
5792 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005793 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5794 alert );
5795 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005796
Hanno Beckere6706e62017-05-15 16:05:15 +01005797#if defined(MBEDTLS_DEBUG_C)
5798 if( ssl->session_negotiate->verify_result != 0 )
5799 {
5800 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5801 ssl->session_negotiate->verify_result ) );
5802 }
5803 else
5804 {
5805 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5806 }
5807#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005808 }
5809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005811
5812 return( ret );
5813}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005814#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5815 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5816 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5817 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5818 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5819 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5820 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005822int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005823{
5824 int ret;
5825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005828 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005829 ssl->out_msglen = 1;
5830 ssl->out_msg[0] = 1;
5831
Paul Bakker5121ce52009-01-03 21:22:43 +00005832 ssl->state++;
5833
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005834 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005835 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005836 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005837 return( ret );
5838 }
5839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005841
5842 return( 0 );
5843}
5844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005845int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005846{
5847 int ret;
5848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005850
Hanno Becker327c93b2018-08-15 13:56:18 +01005851 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005853 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005854 return( ret );
5855 }
5856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005857 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005860 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5861 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005862 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005863 }
5864
Hanno Beckere678eaa2018-08-21 14:57:46 +01005865 /* CCS records are only accepted if they have length 1 and content '1',
5866 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005867
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005868 /*
5869 * Switch to our negotiated transform and session parameters for inbound
5870 * data.
5871 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005873 ssl->transform_in = ssl->transform_negotiate;
5874 ssl->session_in = ssl->session_negotiate;
5875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005876#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005877 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005879#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005880 ssl_dtls_replay_reset( ssl );
5881#endif
5882
5883 /* Increment epoch */
5884 if( ++ssl->in_epoch == 0 )
5885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005887 /* This is highly unlikely to happen for legitimate reasons, so
5888 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005889 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005890 }
5891 }
5892 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005893#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005894 memset( ssl->in_ctr, 0, 8 );
5895
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005896 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005898#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5899 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005901 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005903 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005904 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5905 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005906 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005907 }
5908 }
5909#endif
5910
Paul Bakker5121ce52009-01-03 21:22:43 +00005911 ssl->state++;
5912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005914
5915 return( 0 );
5916}
5917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005918void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5919 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005920{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005921 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005923#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5924 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5925 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005926 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005927 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005928#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005929#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5930#if defined(MBEDTLS_SHA512_C)
5931 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005932 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5933 else
5934#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005935#if defined(MBEDTLS_SHA256_C)
5936 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005937 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005938 else
5939#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005940#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005943 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005944 }
Paul Bakker380da532012-04-18 16:10:25 +00005945}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005947void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005948{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005949#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5950 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005951 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5952 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005953#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005954#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5955#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005956 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005957#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005958#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005959 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005960#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005961#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005962}
5963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005964static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005965 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005966{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005967#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5968 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005969 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5970 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005971#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005972#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5973#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005974 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005975#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005976#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005977 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005978#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005979#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005980}
5981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5983 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5984static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005985 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005986{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005987 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5988 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005989}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005990#endif
Paul Bakker380da532012-04-18 16:10:25 +00005991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005992#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5993#if defined(MBEDTLS_SHA256_C)
5994static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005995 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005996{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005997 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005998}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005999#endif
Paul Bakker380da532012-04-18 16:10:25 +00006000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006001#if defined(MBEDTLS_SHA512_C)
6002static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006003 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006004{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006005 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006006}
Paul Bakker769075d2012-11-24 11:26:46 +01006007#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006010#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006011static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006012 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006013{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006014 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006015 mbedtls_md5_context md5;
6016 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006017
Paul Bakker5121ce52009-01-03 21:22:43 +00006018 unsigned char padbuf[48];
6019 unsigned char md5sum[16];
6020 unsigned char sha1sum[20];
6021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006023 if( !session )
6024 session = ssl->session;
6025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006027
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006028 mbedtls_md5_init( &md5 );
6029 mbedtls_sha1_init( &sha1 );
6030
6031 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6032 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006033
6034 /*
6035 * SSLv3:
6036 * hash =
6037 * MD5( master + pad2 +
6038 * MD5( handshake + sender + master + pad1 ) )
6039 * + SHA1( master + pad2 +
6040 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006041 */
6042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006043#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006044 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6045 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006046#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006048#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006049 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6050 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006051#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006053 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006054 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006055
Paul Bakker1ef83d62012-04-11 12:09:53 +00006056 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006057
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006058 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6059 mbedtls_md5_update_ret( &md5, session->master, 48 );
6060 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6061 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006062
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006063 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6064 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6065 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6066 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006067
Paul Bakker1ef83d62012-04-11 12:09:53 +00006068 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006069
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006070 mbedtls_md5_starts_ret( &md5 );
6071 mbedtls_md5_update_ret( &md5, session->master, 48 );
6072 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6073 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6074 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006075
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006076 mbedtls_sha1_starts_ret( &sha1 );
6077 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6078 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6079 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6080 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006082 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006083
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006084 mbedtls_md5_free( &md5 );
6085 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006086
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006087 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6088 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6089 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006092}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006093#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006095#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006096static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006097 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006098{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006099 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006100 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006101 mbedtls_md5_context md5;
6102 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006103 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006106 if( !session )
6107 session = ssl->session;
6108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006110
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006111 mbedtls_md5_init( &md5 );
6112 mbedtls_sha1_init( &sha1 );
6113
6114 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6115 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006116
Paul Bakker1ef83d62012-04-11 12:09:53 +00006117 /*
6118 * TLSv1:
6119 * hash = PRF( master, finished_label,
6120 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6121 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006123#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006124 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6125 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006126#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006128#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006129 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6130 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006131#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006133 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006134 ? "client finished"
6135 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006136
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006137 mbedtls_md5_finish_ret( &md5, padbuf );
6138 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006139
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006140 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006141 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006143 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006144
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006145 mbedtls_md5_free( &md5 );
6146 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006147
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006148 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006151}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006152#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006154#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6155#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006156static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006157 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006158{
6159 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006160 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006161 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006162 unsigned char padbuf[32];
6163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006164 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006165 if( !session )
6166 session = ssl->session;
6167
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006168 mbedtls_sha256_init( &sha256 );
6169
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006171
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006172 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006173
6174 /*
6175 * TLSv1.2:
6176 * hash = PRF( master, finished_label,
6177 * Hash( handshake ) )[0.11]
6178 */
6179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006180#if !defined(MBEDTLS_SHA256_ALT)
6181 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006182 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006183#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006185 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006186 ? "client finished"
6187 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006188
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006189 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006190
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006191 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006192 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006194 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006195
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006196 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006197
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006198 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006201}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006202#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006204#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006205static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006206 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006207{
6208 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006209 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006210 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006211 unsigned char padbuf[48];
6212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006213 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006214 if( !session )
6215 session = ssl->session;
6216
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006217 mbedtls_sha512_init( &sha512 );
6218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006220
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006221 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006222
6223 /*
6224 * TLSv1.2:
6225 * hash = PRF( master, finished_label,
6226 * Hash( handshake ) )[0.11]
6227 */
6228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006229#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006230 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6231 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006232#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006235 ? "client finished"
6236 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006237
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006238 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006239
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006240 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006241 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006243 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006244
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006245 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006246
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006247 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006250}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251#endif /* MBEDTLS_SHA512_C */
6252#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006255{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006256 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006257
6258 /*
6259 * Free our handshake params
6260 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006261 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006262 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006263 ssl->handshake = NULL;
6264
6265 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006266 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006267 */
6268 if( ssl->transform )
6269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006270 mbedtls_ssl_transform_free( ssl->transform );
6271 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006272 }
6273 ssl->transform = ssl->transform_negotiate;
6274 ssl->transform_negotiate = NULL;
6275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006276 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006277}
6278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006279void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006280{
6281 int resume = ssl->handshake->resume;
6282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006283 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006285#if defined(MBEDTLS_SSL_RENEGOTIATION)
6286 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006288 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006289 ssl->renego_records_seen = 0;
6290 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006291#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006292
6293 /*
6294 * Free the previous session and switch in the current one
6295 */
Paul Bakker0a597072012-09-25 21:55:46 +00006296 if( ssl->session )
6297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006298#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006299 /* RFC 7366 3.1: keep the EtM state */
6300 ssl->session_negotiate->encrypt_then_mac =
6301 ssl->session->encrypt_then_mac;
6302#endif
6303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006304 mbedtls_ssl_session_free( ssl->session );
6305 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006306 }
6307 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006308 ssl->session_negotiate = NULL;
6309
Paul Bakker0a597072012-09-25 21:55:46 +00006310 /*
6311 * Add cache entry
6312 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006313 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006314 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006315 resume == 0 )
6316 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006317 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006319 }
Paul Bakker0a597072012-09-25 21:55:46 +00006320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006321#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006322 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006323 ssl->handshake->flight != NULL )
6324 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006325 /* Cancel handshake timer */
6326 ssl_set_timer( ssl, 0 );
6327
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006328 /* Keep last flight around in case we need to resend it:
6329 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006330 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006331 }
6332 else
6333#endif
6334 ssl_handshake_wrapup_free_hs_transform( ssl );
6335
Paul Bakker48916f92012-09-16 19:57:18 +00006336 ssl->state++;
6337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006338 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006339}
6340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006341int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006342{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006343 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006346
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006347 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006348
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006349 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006350
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006351 /*
6352 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6353 * may define some other value. Currently (early 2016), no defined
6354 * ciphersuite does this (and this is unlikely to change as activity has
6355 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006357 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006359#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006360 ssl->verify_data_len = hash_len;
6361 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006362#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006363
Paul Bakker5121ce52009-01-03 21:22:43 +00006364 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006365 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6366 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006367
6368 /*
6369 * In case of session resuming, invert the client and server
6370 * ChangeCipherSpec messages order.
6371 */
Paul Bakker0a597072012-09-25 21:55:46 +00006372 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006374#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006375 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006376 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006377#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006378#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006379 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006380 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006381#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006382 }
6383 else
6384 ssl->state++;
6385
Paul Bakker48916f92012-09-16 19:57:18 +00006386 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006387 * Switch to our negotiated transform and session parameters for outbound
6388 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006390 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006392#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006393 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006394 {
6395 unsigned char i;
6396
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006397 /* Remember current epoch settings for resending */
6398 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006399 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006400
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006401 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006402 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006403
6404 /* Increment epoch */
6405 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006406 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006407 break;
6408
6409 /* The loop goes to its end iff the counter is wrapping */
6410 if( i == 0 )
6411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6413 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006414 }
6415 }
6416 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006417#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006418 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006419
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006420 ssl->transform_out = ssl->transform_negotiate;
6421 ssl->session_out = ssl->session_negotiate;
6422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006423#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6424 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006426 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006428 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6429 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006430 }
6431 }
6432#endif
6433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006434#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006435 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006436 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006437#endif
6438
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006439 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006440 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006441 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006442 return( ret );
6443 }
6444
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006445#if defined(MBEDTLS_SSL_PROTO_DTLS)
6446 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6447 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6448 {
6449 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6450 return( ret );
6451 }
6452#endif
6453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006455
6456 return( 0 );
6457}
6458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006459#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006460#define SSL_MAX_HASH_LEN 36
6461#else
6462#define SSL_MAX_HASH_LEN 12
6463#endif
6464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006465int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006466{
Paul Bakker23986e52011-04-24 08:57:21 +00006467 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006468 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006469 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006472
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006473 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006474
Hanno Becker327c93b2018-08-15 13:56:18 +01006475 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006477 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006478 return( ret );
6479 }
6480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006481 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006484 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6485 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006486 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006487 }
6488
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006489 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006490#if defined(MBEDTLS_SSL_PROTO_SSL3)
6491 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006492 hash_len = 36;
6493 else
6494#endif
6495 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006497 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6498 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006501 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6502 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006504 }
6505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006506 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006507 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006510 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6511 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006512 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006513 }
6514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006515#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006516 ssl->verify_data_len = hash_len;
6517 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006518#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006519
Paul Bakker0a597072012-09-25 21:55:46 +00006520 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006522#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006523 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006524 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006525#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006526#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006527 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006528 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006529#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006530 }
6531 else
6532 ssl->state++;
6533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006534#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006535 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006536 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006537#endif
6538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006540
6541 return( 0 );
6542}
6543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006544static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006545{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006548#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6549 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6550 mbedtls_md5_init( &handshake->fin_md5 );
6551 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006552 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6553 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006554#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006555#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6556#if defined(MBEDTLS_SHA256_C)
6557 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006558 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006559#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006560#if defined(MBEDTLS_SHA512_C)
6561 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006562 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006563#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006564#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006565
6566 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006567
6568#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6569 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6570 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6571#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573#if defined(MBEDTLS_DHM_C)
6574 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006575#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006576#if defined(MBEDTLS_ECDH_C)
6577 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006578#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006579#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006580 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006581#if defined(MBEDTLS_SSL_CLI_C)
6582 handshake->ecjpake_cache = NULL;
6583 handshake->ecjpake_cache_len = 0;
6584#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006585#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006586
6587#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6588 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6589#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006590}
6591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006592static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006593{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6597 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006599 mbedtls_md_init( &transform->md_ctx_enc );
6600 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006601}
6602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006603void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006604{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006605 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006606}
6607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006608static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006609{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006610 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006611 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006612 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006613 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006614 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006615 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006616 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006617
6618 /*
6619 * Either the pointers are now NULL or cleared properly and can be freed.
6620 * Now allocate missing structures.
6621 */
6622 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006623 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006624 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006625 }
Paul Bakker48916f92012-09-16 19:57:18 +00006626
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006627 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006628 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006629 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006630 }
Paul Bakker48916f92012-09-16 19:57:18 +00006631
Paul Bakker82788fb2014-10-20 13:59:19 +02006632 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006633 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006634 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006635 }
Paul Bakker48916f92012-09-16 19:57:18 +00006636
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006637 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006638 if( ssl->handshake == NULL ||
6639 ssl->transform_negotiate == NULL ||
6640 ssl->session_negotiate == NULL )
6641 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006642 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644 mbedtls_free( ssl->handshake );
6645 mbedtls_free( ssl->transform_negotiate );
6646 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006647
6648 ssl->handshake = NULL;
6649 ssl->transform_negotiate = NULL;
6650 ssl->session_negotiate = NULL;
6651
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006652 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006653 }
6654
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006655 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006656 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006657 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006658 ssl_handshake_params_init( ssl->handshake );
6659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006660#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006661 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6662 {
6663 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006664
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006665 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6666 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6667 else
6668 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006669
6670 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006671 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006672#endif
6673
Paul Bakker48916f92012-09-16 19:57:18 +00006674 return( 0 );
6675}
6676
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006677#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006678/* Dummy cookie callbacks for defaults */
6679static int ssl_cookie_write_dummy( void *ctx,
6680 unsigned char **p, unsigned char *end,
6681 const unsigned char *cli_id, size_t cli_id_len )
6682{
6683 ((void) ctx);
6684 ((void) p);
6685 ((void) end);
6686 ((void) cli_id);
6687 ((void) cli_id_len);
6688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006689 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006690}
6691
6692static int ssl_cookie_check_dummy( void *ctx,
6693 const unsigned char *cookie, size_t cookie_len,
6694 const unsigned char *cli_id, size_t cli_id_len )
6695{
6696 ((void) ctx);
6697 ((void) cookie);
6698 ((void) cookie_len);
6699 ((void) cli_id);
6700 ((void) cli_id_len);
6701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006703}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006704#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006705
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006706/* Once ssl->out_hdr as the address of the beginning of the
6707 * next outgoing record is set, deduce the other pointers.
6708 *
6709 * Note: For TLS, we save the implicit record sequence number
6710 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6711 * and the caller has to make sure there's space for this.
6712 */
6713
6714static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6715 mbedtls_ssl_transform *transform )
6716{
6717#if defined(MBEDTLS_SSL_PROTO_DTLS)
6718 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6719 {
6720 ssl->out_ctr = ssl->out_hdr + 3;
6721 ssl->out_len = ssl->out_hdr + 11;
6722 ssl->out_iv = ssl->out_hdr + 13;
6723 }
6724 else
6725#endif
6726 {
6727 ssl->out_ctr = ssl->out_hdr - 8;
6728 ssl->out_len = ssl->out_hdr + 3;
6729 ssl->out_iv = ssl->out_hdr + 5;
6730 }
6731
6732 /* Adjust out_msg to make space for explicit IV, if used. */
6733 if( transform != NULL &&
6734 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6735 {
6736 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6737 }
6738 else
6739 ssl->out_msg = ssl->out_iv;
6740}
6741
6742/* Once ssl->in_hdr as the address of the beginning of the
6743 * next incoming record is set, deduce the other pointers.
6744 *
6745 * Note: For TLS, we save the implicit record sequence number
6746 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6747 * and the caller has to make sure there's space for this.
6748 */
6749
6750static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6751 mbedtls_ssl_transform *transform )
6752{
6753#if defined(MBEDTLS_SSL_PROTO_DTLS)
6754 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6755 {
6756 ssl->in_ctr = ssl->in_hdr + 3;
6757 ssl->in_len = ssl->in_hdr + 11;
6758 ssl->in_iv = ssl->in_hdr + 13;
6759 }
6760 else
6761#endif
6762 {
6763 ssl->in_ctr = ssl->in_hdr - 8;
6764 ssl->in_len = ssl->in_hdr + 3;
6765 ssl->in_iv = ssl->in_hdr + 5;
6766 }
6767
6768 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6769 if( transform != NULL &&
6770 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6771 {
6772 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6773 }
6774 else
6775 ssl->in_msg = ssl->in_iv;
6776}
6777
Paul Bakker5121ce52009-01-03 21:22:43 +00006778/*
6779 * Initialize an SSL context
6780 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006781void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6782{
6783 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6784}
6785
6786/*
6787 * Setup an SSL context
6788 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006789
6790static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6791{
6792 /* Set the incoming and outgoing record pointers. */
6793#if defined(MBEDTLS_SSL_PROTO_DTLS)
6794 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6795 {
6796 ssl->out_hdr = ssl->out_buf;
6797 ssl->in_hdr = ssl->in_buf;
6798 }
6799 else
6800#endif /* MBEDTLS_SSL_PROTO_DTLS */
6801 {
6802 ssl->out_hdr = ssl->out_buf + 8;
6803 ssl->in_hdr = ssl->in_buf + 8;
6804 }
6805
6806 /* Derive other internal pointers. */
6807 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6808 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6809}
6810
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006811int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006812 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006813{
Paul Bakker48916f92012-09-16 19:57:18 +00006814 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006815
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006816 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006817
6818 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006819 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006820 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02006821
6822 /* Set to NULL in case of an error condition */
6823 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02006824
Angus Grattond8213d02016-05-25 20:56:48 +10006825 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6826 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006827 {
Angus Grattond8213d02016-05-25 20:56:48 +10006828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02006829 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02006830 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10006831 }
6832
6833 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6834 if( ssl->out_buf == NULL )
6835 {
6836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02006837 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02006838 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00006839 }
6840
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006841 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006842
Paul Bakker48916f92012-09-16 19:57:18 +00006843 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02006844 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00006845
6846 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02006847
6848error:
6849 mbedtls_free( ssl->in_buf );
6850 mbedtls_free( ssl->out_buf );
6851
6852 ssl->conf = NULL;
6853
6854 ssl->in_buf = NULL;
6855 ssl->out_buf = NULL;
6856
6857 ssl->in_hdr = NULL;
6858 ssl->in_ctr = NULL;
6859 ssl->in_len = NULL;
6860 ssl->in_iv = NULL;
6861 ssl->in_msg = NULL;
6862
6863 ssl->out_hdr = NULL;
6864 ssl->out_ctr = NULL;
6865 ssl->out_len = NULL;
6866 ssl->out_iv = NULL;
6867 ssl->out_msg = NULL;
6868
k-stachowiak9f7798e2018-07-31 16:52:32 +02006869 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006870}
6871
6872/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006873 * Reset an initialized and used SSL context for re-use while retaining
6874 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006875 *
6876 * If partial is non-zero, keep data in the input buffer and client ID.
6877 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006878 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006879static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006880{
Paul Bakker48916f92012-09-16 19:57:18 +00006881 int ret;
6882
Hanno Becker7e772132018-08-10 12:38:21 +01006883#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6884 !defined(MBEDTLS_SSL_SRV_C)
6885 ((void) partial);
6886#endif
6887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006888 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006889
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006890 /* Cancel any possibly running timer */
6891 ssl_set_timer( ssl, 0 );
6892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006893#if defined(MBEDTLS_SSL_RENEGOTIATION)
6894 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006895 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006896
6897 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6899 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006900#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006901 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006902
Paul Bakker7eb013f2011-10-06 12:37:39 +00006903 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01006904 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006905
6906 ssl->in_msgtype = 0;
6907 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006909 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006910 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006911#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006912#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006913 ssl_dtls_replay_reset( ssl );
6914#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006915
6916 ssl->in_hslen = 0;
6917 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006918
6919 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006920
6921 ssl->out_msgtype = 0;
6922 ssl->out_msglen = 0;
6923 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006924#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6925 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006926 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006927#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006928
Hanno Becker19859472018-08-06 09:40:20 +01006929 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6930
Paul Bakker48916f92012-09-16 19:57:18 +00006931 ssl->transform_in = NULL;
6932 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006933
Hanno Becker78640902018-08-13 16:35:15 +01006934 ssl->session_in = NULL;
6935 ssl->session_out = NULL;
6936
Angus Grattond8213d02016-05-25 20:56:48 +10006937 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006938
6939#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006940 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006941#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
6942 {
6943 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10006944 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006945 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006947#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6948 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6951 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6954 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006955 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006956 }
6957#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006958
Paul Bakker48916f92012-09-16 19:57:18 +00006959 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961 mbedtls_ssl_transform_free( ssl->transform );
6962 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006963 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006964 }
Paul Bakker48916f92012-09-16 19:57:18 +00006965
Paul Bakkerc0463502013-02-14 11:19:38 +01006966 if( ssl->session )
6967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968 mbedtls_ssl_session_free( ssl->session );
6969 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006970 ssl->session = NULL;
6971 }
6972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006973#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006974 ssl->alpn_chosen = NULL;
6975#endif
6976
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006977#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01006978#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006979 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006980#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006981 {
6982 mbedtls_free( ssl->cli_id );
6983 ssl->cli_id = NULL;
6984 ssl->cli_id_len = 0;
6985 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006986#endif
6987
Paul Bakker48916f92012-09-16 19:57:18 +00006988 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6989 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006990
6991 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006992}
6993
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006994/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006995 * Reset an initialized and used SSL context for re-use while retaining
6996 * all application-set variables, function pointers and data.
6997 */
6998int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6999{
7000 return( ssl_session_reset_int( ssl, 0 ) );
7001}
7002
7003/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007004 * SSL set accessors
7005 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007006void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007007{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007008 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007009}
7010
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007011void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007012{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007013 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007014}
7015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007016#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007017void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007018{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007019 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007020}
7021#endif
7022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007024void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007025{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007026 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007027}
7028#endif
7029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007030#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007031
Hanno Becker1841b0a2018-08-24 11:13:57 +01007032void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7033 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007034{
7035 ssl->disable_datagram_packing = !allow_packing;
7036}
7037
7038void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7039 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007040{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007041 conf->hs_timeout_min = min;
7042 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007043}
7044#endif
7045
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007046void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007047{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007048 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007049}
7050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007052void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007053 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007054 void *p_vrfy )
7055{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007056 conf->f_vrfy = f_vrfy;
7057 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007058}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007059#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007060
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007061void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007062 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007063 void *p_rng )
7064{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007065 conf->f_rng = f_rng;
7066 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007067}
7068
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007069void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007070 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007071 void *p_dbg )
7072{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007073 conf->f_dbg = f_dbg;
7074 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007075}
7076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007078 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007079 mbedtls_ssl_send_t *f_send,
7080 mbedtls_ssl_recv_t *f_recv,
7081 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007082{
7083 ssl->p_bio = p_bio;
7084 ssl->f_send = f_send;
7085 ssl->f_recv = f_recv;
7086 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007087}
7088
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007089#if defined(MBEDTLS_SSL_PROTO_DTLS)
7090void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7091{
7092 ssl->mtu = mtu;
7093}
7094#endif
7095
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007096void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007097{
7098 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007099}
7100
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007101void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7102 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007103 mbedtls_ssl_set_timer_t *f_set_timer,
7104 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007105{
7106 ssl->p_timer = p_timer;
7107 ssl->f_set_timer = f_set_timer;
7108 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007109
7110 /* Make sure we start with no timer running */
7111 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007112}
7113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007115void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007116 void *p_cache,
7117 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7118 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007119{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007120 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007121 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007122 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007123}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126#if defined(MBEDTLS_SSL_CLI_C)
7127int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007128{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007129 int ret;
7130
7131 if( ssl == NULL ||
7132 session == NULL ||
7133 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007134 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007136 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007137 }
7138
7139 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7140 return( ret );
7141
Paul Bakker0a597072012-09-25 21:55:46 +00007142 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007143
7144 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007145}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007147
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007148void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007149 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007150{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007151 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7152 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7153 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7154 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007155}
7156
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007157void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007158 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007159 int major, int minor )
7160{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007161 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007162 return;
7163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007164 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007165 return;
7166
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007167 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007168}
7169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007170#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007171void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007172 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007173{
7174 conf->cert_profile = profile;
7175}
7176
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007177/* Append a new keycert entry to a (possibly empty) list */
7178static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7179 mbedtls_x509_crt *cert,
7180 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007181{
niisato8ee24222018-06-25 19:05:48 +09007182 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007183
niisato8ee24222018-06-25 19:05:48 +09007184 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7185 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007186 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007187
niisato8ee24222018-06-25 19:05:48 +09007188 new_cert->cert = cert;
7189 new_cert->key = key;
7190 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007191
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007192 /* Update head is the list was null, else add to the end */
7193 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007194 {
niisato8ee24222018-06-25 19:05:48 +09007195 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007196 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007197 else
7198 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007199 mbedtls_ssl_key_cert *cur = *head;
7200 while( cur->next != NULL )
7201 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007202 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007203 }
7204
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007205 return( 0 );
7206}
7207
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007208int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007209 mbedtls_x509_crt *own_cert,
7210 mbedtls_pk_context *pk_key )
7211{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007212 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007213}
7214
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007215void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007216 mbedtls_x509_crt *ca_chain,
7217 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007218{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007219 conf->ca_chain = ca_chain;
7220 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007221}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007222#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007223
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007224#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7225int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7226 mbedtls_x509_crt *own_cert,
7227 mbedtls_pk_context *pk_key )
7228{
7229 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7230 own_cert, pk_key ) );
7231}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007232
7233void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7234 mbedtls_x509_crt *ca_chain,
7235 mbedtls_x509_crl *ca_crl )
7236{
7237 ssl->handshake->sni_ca_chain = ca_chain;
7238 ssl->handshake->sni_ca_crl = ca_crl;
7239}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007240
7241void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7242 int authmode )
7243{
7244 ssl->handshake->sni_authmode = authmode;
7245}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007246#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7247
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007248#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007249/*
7250 * Set EC J-PAKE password for current handshake
7251 */
7252int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7253 const unsigned char *pw,
7254 size_t pw_len )
7255{
7256 mbedtls_ecjpake_role role;
7257
Janos Follath8eb64132016-06-03 15:40:57 +01007258 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007259 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7260
7261 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7262 role = MBEDTLS_ECJPAKE_SERVER;
7263 else
7264 role = MBEDTLS_ECJPAKE_CLIENT;
7265
7266 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7267 role,
7268 MBEDTLS_MD_SHA256,
7269 MBEDTLS_ECP_DP_SECP256R1,
7270 pw, pw_len ) );
7271}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007272#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007275int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007276 const unsigned char *psk, size_t psk_len,
7277 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007278{
Paul Bakker6db455e2013-09-18 17:29:31 +02007279 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007280 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007282 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7283 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007284
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007285 /* Identity len will be encoded on two bytes */
7286 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007287 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007288 {
7289 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7290 }
7291
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007292 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007293 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007294 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007295
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007296 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007297 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007298 conf->psk_len = 0;
7299 }
7300 if( conf->psk_identity != NULL )
7301 {
7302 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007303 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007304 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007305 }
7306
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007307 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7308 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007309 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007310 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007311 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007312 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007313 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007314 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007315 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007316
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007317 conf->psk_len = psk_len;
7318 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007319
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007320 memcpy( conf->psk, psk, conf->psk_len );
7321 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007322
7323 return( 0 );
7324}
7325
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007326int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7327 const unsigned char *psk, size_t psk_len )
7328{
7329 if( psk == NULL || ssl->handshake == NULL )
7330 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7331
7332 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7333 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7334
7335 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007336 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007337 mbedtls_platform_zeroize( ssl->handshake->psk,
7338 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007339 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007340 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007341 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007342
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007343 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007344 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007345
7346 ssl->handshake->psk_len = psk_len;
7347 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7348
7349 return( 0 );
7350}
7351
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007352void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007353 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007354 size_t),
7355 void *p_psk )
7356{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007357 conf->f_psk = f_psk;
7358 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007359}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007360#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007361
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007362#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007363
7364#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007365int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007366{
7367 int ret;
7368
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007369 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7370 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7371 {
7372 mbedtls_mpi_free( &conf->dhm_P );
7373 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007374 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007375 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007376
7377 return( 0 );
7378}
Hanno Becker470a8c42017-10-04 15:28:46 +01007379#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007380
Hanno Beckera90658f2017-10-04 15:29:08 +01007381int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7382 const unsigned char *dhm_P, size_t P_len,
7383 const unsigned char *dhm_G, size_t G_len )
7384{
7385 int ret;
7386
7387 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7388 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7389 {
7390 mbedtls_mpi_free( &conf->dhm_P );
7391 mbedtls_mpi_free( &conf->dhm_G );
7392 return( ret );
7393 }
7394
7395 return( 0 );
7396}
Paul Bakker5121ce52009-01-03 21:22:43 +00007397
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007398int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007399{
7400 int ret;
7401
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007402 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7403 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7404 {
7405 mbedtls_mpi_free( &conf->dhm_P );
7406 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007407 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007408 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007409
7410 return( 0 );
7411}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007412#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007413
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007414#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7415/*
7416 * Set the minimum length for Diffie-Hellman parameters
7417 */
7418void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7419 unsigned int bitlen )
7420{
7421 conf->dhm_min_bitlen = bitlen;
7422}
7423#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7424
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007425#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007426/*
7427 * Set allowed/preferred hashes for handshake signatures
7428 */
7429void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7430 const int *hashes )
7431{
7432 conf->sig_hashes = hashes;
7433}
Hanno Becker947194e2017-04-07 13:25:49 +01007434#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007435
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007436#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007437/*
7438 * Set the allowed elliptic curves
7439 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007440void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007441 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007442{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007443 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007444}
Hanno Becker947194e2017-04-07 13:25:49 +01007445#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007446
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007447#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007448int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007449{
Hanno Becker947194e2017-04-07 13:25:49 +01007450 /* Initialize to suppress unnecessary compiler warning */
7451 size_t hostname_len = 0;
7452
7453 /* Check if new hostname is valid before
7454 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007455 if( hostname != NULL )
7456 {
7457 hostname_len = strlen( hostname );
7458
7459 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7460 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7461 }
7462
7463 /* Now it's clear that we will overwrite the old hostname,
7464 * so we can free it safely */
7465
7466 if( ssl->hostname != NULL )
7467 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007468 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007469 mbedtls_free( ssl->hostname );
7470 }
7471
7472 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007473
Paul Bakker5121ce52009-01-03 21:22:43 +00007474 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007475 {
7476 ssl->hostname = NULL;
7477 }
7478 else
7479 {
7480 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007481 if( ssl->hostname == NULL )
7482 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007483
Hanno Becker947194e2017-04-07 13:25:49 +01007484 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007485
Hanno Becker947194e2017-04-07 13:25:49 +01007486 ssl->hostname[hostname_len] = '\0';
7487 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007488
7489 return( 0 );
7490}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007491#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007492
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007493#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007494void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007496 const unsigned char *, size_t),
7497 void *p_sni )
7498{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007499 conf->f_sni = f_sni;
7500 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007501}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007505int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007506{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007507 size_t cur_len, tot_len;
7508 const char **p;
7509
7510 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007511 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7512 * MUST NOT be truncated."
7513 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007514 */
7515 tot_len = 0;
7516 for( p = protos; *p != NULL; p++ )
7517 {
7518 cur_len = strlen( *p );
7519 tot_len += cur_len;
7520
7521 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007522 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007523 }
7524
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007525 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007526
7527 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007528}
7529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007530const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007531{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007532 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007533}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007535
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007536void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007537{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007538 conf->max_major_ver = major;
7539 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007540}
7541
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007542void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007543{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007544 conf->min_major_ver = major;
7545 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007546}
7547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007548#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007549void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007550{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007551 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007552}
7553#endif
7554
Janos Follath088ce432017-04-10 12:42:31 +01007555#if defined(MBEDTLS_SSL_SRV_C)
7556void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7557 char cert_req_ca_list )
7558{
7559 conf->cert_req_ca_list = cert_req_ca_list;
7560}
7561#endif
7562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007564void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007565{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007566 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007567}
7568#endif
7569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007570#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007571void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007572{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007573 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007574}
7575#endif
7576
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007577#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007578void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007579{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007580 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007581}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007582#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007584#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007585int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007586{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007587 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007588 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007591 }
7592
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007593 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007594
7595 return( 0 );
7596}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007597#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007600void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007601{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007602 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007603}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007607void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007608{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007609 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007610}
7611#endif
7612
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007613void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007614{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007615 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007616}
7617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007618#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007619void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007620{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007621 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007622}
7623
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007624void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007625{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007626 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007627}
7628
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007629void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007630 const unsigned char period[8] )
7631{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007632 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007633}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007637#if defined(MBEDTLS_SSL_CLI_C)
7638void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007639{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007640 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007641}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007642#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007643
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007644#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007645void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7646 mbedtls_ssl_ticket_write_t *f_ticket_write,
7647 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7648 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007649{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007650 conf->f_ticket_write = f_ticket_write;
7651 conf->f_ticket_parse = f_ticket_parse;
7652 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007653}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007654#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007656
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007657#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7658void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7659 mbedtls_ssl_export_keys_t *f_export_keys,
7660 void *p_export_keys )
7661{
7662 conf->f_export_keys = f_export_keys;
7663 conf->p_export_keys = p_export_keys;
7664}
7665#endif
7666
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007667#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007668void mbedtls_ssl_conf_async_private_cb(
7669 mbedtls_ssl_config *conf,
7670 mbedtls_ssl_async_sign_t *f_async_sign,
7671 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7672 mbedtls_ssl_async_resume_t *f_async_resume,
7673 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007674 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007675{
7676 conf->f_async_sign_start = f_async_sign;
7677 conf->f_async_decrypt_start = f_async_decrypt;
7678 conf->f_async_resume = f_async_resume;
7679 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007680 conf->p_async_config_data = async_config_data;
7681}
7682
Gilles Peskine8f97af72018-04-26 11:46:10 +02007683void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7684{
7685 return( conf->p_async_config_data );
7686}
7687
Gilles Peskine1febfef2018-04-30 11:54:39 +02007688void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007689{
7690 if( ssl->handshake == NULL )
7691 return( NULL );
7692 else
7693 return( ssl->handshake->user_async_ctx );
7694}
7695
Gilles Peskine1febfef2018-04-30 11:54:39 +02007696void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007697 void *ctx )
7698{
7699 if( ssl->handshake != NULL )
7700 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007701}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007702#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007703
Paul Bakker5121ce52009-01-03 21:22:43 +00007704/*
7705 * SSL get accessors
7706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007708{
7709 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7710}
7711
Hanno Becker8b170a02017-10-10 11:51:19 +01007712int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7713{
7714 /*
7715 * Case A: We're currently holding back
7716 * a message for further processing.
7717 */
7718
7719 if( ssl->keep_current_message == 1 )
7720 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007721 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007722 return( 1 );
7723 }
7724
7725 /*
7726 * Case B: Further records are pending in the current datagram.
7727 */
7728
7729#if defined(MBEDTLS_SSL_PROTO_DTLS)
7730 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7731 ssl->in_left > ssl->next_record_offset )
7732 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007733 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007734 return( 1 );
7735 }
7736#endif /* MBEDTLS_SSL_PROTO_DTLS */
7737
7738 /*
7739 * Case C: A handshake message is being processed.
7740 */
7741
Hanno Becker8b170a02017-10-10 11:51:19 +01007742 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7743 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007744 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007745 return( 1 );
7746 }
7747
7748 /*
7749 * Case D: An application data message is being processed
7750 */
7751 if( ssl->in_offt != NULL )
7752 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007753 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007754 return( 1 );
7755 }
7756
7757 /*
7758 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01007759 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01007760 * we implement support for multiple alerts in single records.
7761 */
7762
7763 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7764 return( 0 );
7765}
7766
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007767uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007768{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007769 if( ssl->session != NULL )
7770 return( ssl->session->verify_result );
7771
7772 if( ssl->session_negotiate != NULL )
7773 return( ssl->session_negotiate->verify_result );
7774
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007775 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007776}
7777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007778const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007779{
Paul Bakker926c8e42013-03-06 10:23:34 +01007780 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007781 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007783 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007784}
7785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007786const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007787{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007788#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007789 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007790 {
7791 switch( ssl->minor_ver )
7792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007793 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007794 return( "DTLSv1.0" );
7795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007797 return( "DTLSv1.2" );
7798
7799 default:
7800 return( "unknown (DTLS)" );
7801 }
7802 }
7803#endif
7804
Paul Bakker43ca69c2011-01-15 17:35:19 +00007805 switch( ssl->minor_ver )
7806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007807 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007808 return( "SSLv3.0" );
7809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007810 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007811 return( "TLSv1.0" );
7812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007813 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007814 return( "TLSv1.1" );
7815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007816 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007817 return( "TLSv1.2" );
7818
Paul Bakker43ca69c2011-01-15 17:35:19 +00007819 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007820 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007821 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007822}
7823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007824int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007825{
Hanno Becker3136ede2018-08-17 15:28:19 +01007826 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007827 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007828 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007829
Hanno Becker78640902018-08-13 16:35:15 +01007830 if( transform == NULL )
7831 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007833#if defined(MBEDTLS_ZLIB_SUPPORT)
7834 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7835 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007836#endif
7837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840 case MBEDTLS_MODE_GCM:
7841 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007842 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007844 transform_expansion = transform->minlen;
7845 break;
7846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007847 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007848
7849 block_size = mbedtls_cipher_get_block_size(
7850 &transform->cipher_ctx_enc );
7851
Hanno Becker3136ede2018-08-17 15:28:19 +01007852 /* Expansion due to the addition of the MAC. */
7853 transform_expansion += transform->maclen;
7854
7855 /* Expansion due to the addition of CBC padding;
7856 * Theoretically up to 256 bytes, but we never use
7857 * more than the block size of the underlying cipher. */
7858 transform_expansion += block_size;
7859
7860 /* For TLS 1.1 or higher, an explicit IV is added
7861 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01007862#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7863 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01007864 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007865#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01007866
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007867 break;
7868
7869 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007871 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007872 }
7873
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007874 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007875}
7876
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007877#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7878size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7879{
7880 size_t max_len;
7881
7882 /*
7883 * Assume mfl_code is correct since it was checked when set
7884 */
Angus Grattond8213d02016-05-25 20:56:48 +10007885 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007886
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007887 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007888 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007889 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007890 {
Angus Grattond8213d02016-05-25 20:56:48 +10007891 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007892 }
7893
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007894 /* During a handshake, use the value being negotiated */
7895 if( ssl->session_negotiate != NULL &&
7896 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7897 {
7898 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7899 }
7900
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007901 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007902}
7903#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7904
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007905#if defined(MBEDTLS_SSL_PROTO_DTLS)
7906static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
7907{
7908 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
7909 return( ssl->mtu );
7910
7911 if( ssl->mtu == 0 )
7912 return( ssl->handshake->mtu );
7913
7914 return( ssl->mtu < ssl->handshake->mtu ?
7915 ssl->mtu : ssl->handshake->mtu );
7916}
7917#endif /* MBEDTLS_SSL_PROTO_DTLS */
7918
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007919int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7920{
7921 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7922
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02007923#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7924 !defined(MBEDTLS_SSL_PROTO_DTLS)
7925 (void) ssl;
7926#endif
7927
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007928#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7929 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7930
7931 if( max_len > mfl )
7932 max_len = mfl;
7933#endif
7934
7935#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007936 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007937 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007938 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007939 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7940 const size_t overhead = (size_t) ret;
7941
7942 if( ret < 0 )
7943 return( ret );
7944
7945 if( mtu <= overhead )
7946 {
7947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7948 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7949 }
7950
7951 if( max_len > mtu - overhead )
7952 max_len = mtu - overhead;
7953 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007954#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007955
Hanno Becker0defedb2018-08-10 12:35:02 +01007956#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7957 !defined(MBEDTLS_SSL_PROTO_DTLS)
7958 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007959#endif
7960
7961 return( (int) max_len );
7962}
7963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964#if defined(MBEDTLS_X509_CRT_PARSE_C)
7965const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007966{
7967 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007968 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007969
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007970 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007971}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007972#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007974#if defined(MBEDTLS_SSL_CLI_C)
7975int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007976{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007977 if( ssl == NULL ||
7978 dst == NULL ||
7979 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007980 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007983 }
7984
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007985 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007986}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007987#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007988
Paul Bakker5121ce52009-01-03 21:22:43 +00007989/*
Paul Bakker1961b702013-01-25 14:49:24 +01007990 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007991 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007992int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007993{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007994 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007995
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007996 if( ssl == NULL || ssl->conf == NULL )
7997 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008000 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008001 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008002#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008003#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008004 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008005 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008006#endif
8007
Paul Bakker1961b702013-01-25 14:49:24 +01008008 return( ret );
8009}
8010
8011/*
8012 * Perform the SSL handshake
8013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008014int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008015{
8016 int ret = 0;
8017
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008018 if( ssl == NULL || ssl->conf == NULL )
8019 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008023 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008026
8027 if( ret != 0 )
8028 break;
8029 }
8030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008032
8033 return( ret );
8034}
8035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008036#if defined(MBEDTLS_SSL_RENEGOTIATION)
8037#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008038/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008039 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008040 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008041static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008042{
8043 int ret;
8044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008046
8047 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008048 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8049 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008050
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008051 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008052 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008053 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008054 return( ret );
8055 }
8056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008058
8059 return( 0 );
8060}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008061#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008062
8063/*
8064 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008065 * - any side: calling mbedtls_ssl_renegotiate(),
8066 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8067 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008068 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008069 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008070 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008072static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008073{
8074 int ret;
8075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008077
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008078 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8079 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008080
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008081 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8082 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008083#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008084 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008085 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008086 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008087 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008088 ssl->handshake->out_msg_seq = 1;
8089 else
8090 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008091 }
8092#endif
8093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008094 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8095 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008097 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008100 return( ret );
8101 }
8102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008104
8105 return( 0 );
8106}
8107
8108/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008109 * Renegotiate current connection on client,
8110 * or request renegotiation on server
8111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008112int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008113{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008114 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008115
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008116 if( ssl == NULL || ssl->conf == NULL )
8117 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008119#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008120 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008121 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008123 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008126 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008127
8128 /* Did we already try/start sending HelloRequest? */
8129 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008130 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008131
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008132 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008133 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008134#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008136#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008137 /*
8138 * On client, either start the renegotiation process or,
8139 * if already in progress, continue the handshake
8140 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008141 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008143 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008145
8146 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008148 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008149 return( ret );
8150 }
8151 }
8152 else
8153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008156 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008157 return( ret );
8158 }
8159 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008160#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008161
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008162 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008163}
8164
8165/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008166 * Check record counters and renegotiate if they're above the limit.
8167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008168static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008169{
Andres AG2196c7f2016-12-15 17:01:16 +00008170 size_t ep_len = ssl_ep_len( ssl );
8171 int in_ctr_cmp;
8172 int out_ctr_cmp;
8173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008174 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8175 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008176 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008177 {
8178 return( 0 );
8179 }
8180
Andres AG2196c7f2016-12-15 17:01:16 +00008181 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8182 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008183 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008184 ssl->conf->renego_period + ep_len, 8 - ep_len );
8185
8186 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008187 {
8188 return( 0 );
8189 }
8190
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008192 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008193}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008195
8196/*
8197 * Receive application data decrypted from the SSL layer
8198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008199int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008200{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008201 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008202 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008203
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008204 if( ssl == NULL || ssl->conf == NULL )
8205 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008209#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008210 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008212 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008213 return( ret );
8214
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008215 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008216 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008217 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008218 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008219 return( ret );
8220 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008221 }
8222#endif
8223
Hanno Becker4a810fb2017-05-24 16:27:30 +01008224 /*
8225 * Check if renegotiation is necessary and/or handshake is
8226 * in process. If yes, perform/continue, and fall through
8227 * if an unexpected packet is received while the client
8228 * is waiting for the ServerHello.
8229 *
8230 * (There is no equivalent to the last condition on
8231 * the server-side as it is not treated as within
8232 * a handshake while waiting for the ClientHello
8233 * after a renegotiation request.)
8234 */
8235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008236#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008237 ret = ssl_check_ctr_renegotiate( ssl );
8238 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8239 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008241 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008242 return( ret );
8243 }
8244#endif
8245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008246 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008248 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008249 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8250 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008252 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008253 return( ret );
8254 }
8255 }
8256
Hanno Beckere41158b2017-10-23 13:30:32 +01008257 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008258 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008259 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008260 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008261 if( ssl->f_get_timer != NULL &&
8262 ssl->f_get_timer( ssl->p_timer ) == -1 )
8263 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008264 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008265 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008266
Hanno Becker327c93b2018-08-15 13:56:18 +01008267 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008268 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008269 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8270 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008271
Hanno Becker4a810fb2017-05-24 16:27:30 +01008272 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8273 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008274 }
8275
8276 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008277 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008278 {
8279 /*
8280 * OpenSSL sends empty messages to randomize the IV
8281 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008282 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008284 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008285 return( 0 );
8286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008287 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008288 return( ret );
8289 }
8290 }
8291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008292 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008295
Hanno Becker4a810fb2017-05-24 16:27:30 +01008296 /*
8297 * - For client-side, expect SERVER_HELLO_REQUEST.
8298 * - For server-side, expect CLIENT_HELLO.
8299 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8300 */
8301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008302#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008303 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008304 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008305 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008308
8309 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008310#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008311 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008312 {
8313 continue;
8314 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008315#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008317 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008318#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008319
Hanno Becker4a810fb2017-05-24 16:27:30 +01008320#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008321 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008322 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008325
8326 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008327#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008328 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008329 {
8330 continue;
8331 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008332#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008333 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008334 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008335#endif /* MBEDTLS_SSL_SRV_C */
8336
Hanno Becker21df7f92017-10-17 11:03:26 +01008337#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008338 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008339 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8340 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8341 ssl->conf->allow_legacy_renegotiation ==
8342 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8343 {
8344 /*
8345 * Accept renegotiation request
8346 */
Paul Bakker48916f92012-09-16 19:57:18 +00008347
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008348 /* DTLS clients need to know renego is server-initiated */
8349#if defined(MBEDTLS_SSL_PROTO_DTLS)
8350 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8351 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8352 {
8353 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8354 }
8355#endif
8356 ret = ssl_start_renegotiation( ssl );
8357 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8358 ret != 0 )
8359 {
8360 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8361 return( ret );
8362 }
8363 }
8364 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008365#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008366 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008367 /*
8368 * Refuse renegotiation
8369 */
8370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008371 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008373#if defined(MBEDTLS_SSL_PROTO_SSL3)
8374 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008375 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008376 /* SSLv3 does not have a "no_renegotiation" warning, so
8377 we send a fatal alert and abort the connection. */
8378 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8379 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8380 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008381 }
8382 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008383#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8384#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8385 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8386 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8389 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8390 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008391 {
8392 return( ret );
8393 }
Paul Bakker48916f92012-09-16 19:57:18 +00008394 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008395 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008396#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8397 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8400 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008401 }
Paul Bakker48916f92012-09-16 19:57:18 +00008402 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008403
Hanno Becker90333da2017-10-10 11:27:13 +01008404 /* At this point, we don't know whether the renegotiation has been
8405 * completed or not. The cases to consider are the following:
8406 * 1) The renegotiation is complete. In this case, no new record
8407 * has been read yet.
8408 * 2) The renegotiation is incomplete because the client received
8409 * an application data record while awaiting the ServerHello.
8410 * 3) The renegotiation is incomplete because the client received
8411 * a non-handshake, non-application data message while awaiting
8412 * the ServerHello.
8413 * In each of these case, looping will be the proper action:
8414 * - For 1), the next iteration will read a new record and check
8415 * if it's application data.
8416 * - For 2), the loop condition isn't satisfied as application data
8417 * is present, hence continue is the same as break
8418 * - For 3), the loop condition is satisfied and read_record
8419 * will re-deliver the message that was held back by the client
8420 * when expecting the ServerHello.
8421 */
8422 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008423 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008424#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008425 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008426 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008427 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008428 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008429 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008432 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008433 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008434 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008435 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008436 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008437#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008439 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8440 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008443 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008444 }
8445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008446 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8449 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008450 }
8451
8452 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008453
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008454 /* We're going to return something now, cancel timer,
8455 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008456 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008457 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008458
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008459#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008460 /* If we requested renego but received AppData, resend HelloRequest.
8461 * Do it now, after setting in_offt, to avoid taking this branch
8462 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008463#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008464 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008465 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008466 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008467 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008469 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008470 return( ret );
8471 }
8472 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008473#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008474#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008475 }
8476
8477 n = ( len < ssl->in_msglen )
8478 ? len : ssl->in_msglen;
8479
8480 memcpy( buf, ssl->in_offt, n );
8481 ssl->in_msglen -= n;
8482
8483 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008484 {
8485 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008486 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008487 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008488 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008489 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008490 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008491 /* more data available */
8492 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008493 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008496
Paul Bakker23986e52011-04-24 08:57:21 +00008497 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008498}
8499
8500/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008501 * Send application data to be encrypted by the SSL layer, taking care of max
8502 * fragment length and buffer size.
8503 *
8504 * According to RFC 5246 Section 6.2.1:
8505 *
8506 * Zero-length fragments of Application data MAY be sent as they are
8507 * potentially useful as a traffic analysis countermeasure.
8508 *
8509 * Therefore, it is possible that the input message length is 0 and the
8510 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008511 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008512static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008513 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008514{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008515 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8516 const size_t max_len = (size_t) ret;
8517
8518 if( ret < 0 )
8519 {
8520 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8521 return( ret );
8522 }
8523
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008524 if( len > max_len )
8525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008526#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008527 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008530 "maximum fragment length: %d > %d",
8531 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008532 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008533 }
8534 else
8535#endif
8536 len = max_len;
8537 }
Paul Bakker887bd502011-06-08 13:10:54 +00008538
Paul Bakker5121ce52009-01-03 21:22:43 +00008539 if( ssl->out_left != 0 )
8540 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008541 /*
8542 * The user has previously tried to send the data and
8543 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8544 * written. In this case, we expect the high-level write function
8545 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008547 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008549 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008550 return( ret );
8551 }
8552 }
Paul Bakker887bd502011-06-08 13:10:54 +00008553 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008554 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008555 /*
8556 * The user is trying to send a message the first time, so we need to
8557 * copy the data into the internal buffers and setup the data structure
8558 * to keep track of partial writes
8559 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008560 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008561 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008562 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008563
Hanno Becker67bc7c32018-08-06 11:33:50 +01008564 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008566 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008567 return( ret );
8568 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008569 }
8570
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008571 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008572}
8573
8574/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008575 * Write application data, doing 1/n-1 splitting if necessary.
8576 *
8577 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008578 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008579 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008581#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008582static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008583 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008584{
8585 int ret;
8586
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008587 if( ssl->conf->cbc_record_splitting ==
8588 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008589 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008590 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8591 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8592 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008593 {
8594 return( ssl_write_real( ssl, buf, len ) );
8595 }
8596
8597 if( ssl->split_done == 0 )
8598 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008599 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008600 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008601 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008602 }
8603
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008604 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8605 return( ret );
8606 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008607
8608 return( ret + 1 );
8609}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008610#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008611
8612/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008613 * Write application data (public-facing wrapper)
8614 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008615int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008616{
8617 int ret;
8618
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008620
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008621 if( ssl == NULL || ssl->conf == NULL )
8622 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8623
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008624#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008625 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8626 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008627 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008628 return( ret );
8629 }
8630#endif
8631
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008632 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008633 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008634 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008635 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008636 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008637 return( ret );
8638 }
8639 }
8640
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008641#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008642 ret = ssl_write_split( ssl, buf, len );
8643#else
8644 ret = ssl_write_real( ssl, buf, len );
8645#endif
8646
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008648
8649 return( ret );
8650}
8651
8652/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008653 * Notify the peer that the connection is being closed
8654 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008655int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008656{
8657 int ret;
8658
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008659 if( ssl == NULL || ssl->conf == NULL )
8660 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008663
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008664 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008665 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008667 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008669 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8670 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8671 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008673 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008674 return( ret );
8675 }
8676 }
8677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008679
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008680 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008681}
8682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008683void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008684{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008685 if( transform == NULL )
8686 return;
8687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008688#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008689 deflateEnd( &transform->ctx_deflate );
8690 inflateEnd( &transform->ctx_inflate );
8691#endif
8692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008693 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8694 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008696 mbedtls_md_free( &transform->md_ctx_enc );
8697 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008698
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008699 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008700}
8701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008702#if defined(MBEDTLS_X509_CRT_PARSE_C)
8703static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008704{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008705 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008706
8707 while( cur != NULL )
8708 {
8709 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008710 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008711 cur = next;
8712 }
8713}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008714#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008715
Hanno Becker0271f962018-08-16 13:23:47 +01008716#if defined(MBEDTLS_SSL_PROTO_DTLS)
8717
8718static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8719{
8720 unsigned offset;
8721 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8722
8723 if( hs == NULL )
8724 return;
8725
Hanno Becker283f5ef2018-08-24 09:34:47 +01008726 ssl_free_buffered_record( ssl );
8727
Hanno Becker0271f962018-08-16 13:23:47 +01008728 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01008729 ssl_buffering_free_slot( ssl, offset );
8730}
8731
8732static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
8733 uint8_t slot )
8734{
8735 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8736 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01008737
8738 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
8739 return;
8740
Hanno Beckere605b192018-08-21 15:59:07 +01008741 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01008742 {
Hanno Beckere605b192018-08-21 15:59:07 +01008743 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01008744 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01008745 mbedtls_free( hs_buf->data );
8746 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01008747 }
8748}
8749
8750#endif /* MBEDTLS_SSL_PROTO_DTLS */
8751
Gilles Peskine9b562d52018-04-25 20:32:43 +02008752void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008753{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008754 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8755
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008756 if( handshake == NULL )
8757 return;
8758
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008759#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8760 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8761 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008762 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008763 handshake->async_in_progress = 0;
8764 }
8765#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8766
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008767#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8768 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8769 mbedtls_md5_free( &handshake->fin_md5 );
8770 mbedtls_sha1_free( &handshake->fin_sha1 );
8771#endif
8772#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8773#if defined(MBEDTLS_SHA256_C)
8774 mbedtls_sha256_free( &handshake->fin_sha256 );
8775#endif
8776#if defined(MBEDTLS_SHA512_C)
8777 mbedtls_sha512_free( &handshake->fin_sha512 );
8778#endif
8779#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781#if defined(MBEDTLS_DHM_C)
8782 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008783#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008784#if defined(MBEDTLS_ECDH_C)
8785 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008786#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008787#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008788 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008789#if defined(MBEDTLS_SSL_CLI_C)
8790 mbedtls_free( handshake->ecjpake_cache );
8791 handshake->ecjpake_cache = NULL;
8792 handshake->ecjpake_cache_len = 0;
8793#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008794#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008795
Janos Follath4ae5c292016-02-10 11:27:43 +00008796#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8797 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008798 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008799 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008800#endif
8801
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008802#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8803 if( handshake->psk != NULL )
8804 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008805 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008806 mbedtls_free( handshake->psk );
8807 }
8808#endif
8809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008810#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8811 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008812 /*
8813 * Free only the linked list wrapper, not the keys themselves
8814 * since the belong to the SNI callback
8815 */
8816 if( handshake->sni_key_cert != NULL )
8817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008818 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008819
8820 while( cur != NULL )
8821 {
8822 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008823 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008824 cur = next;
8825 }
8826 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008827#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008829#if defined(MBEDTLS_SSL_PROTO_DTLS)
8830 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008831 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01008832 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008833#endif
8834
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008835 mbedtls_platform_zeroize( handshake,
8836 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008837}
8838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008839void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008840{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008841 if( session == NULL )
8842 return;
8843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008844#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008845 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008847 mbedtls_x509_crt_free( session->peer_cert );
8848 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008849 }
Paul Bakkered27a042013-04-18 22:46:23 +02008850#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008851
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008852#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008853 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008854#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008855
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008856 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008857}
8858
Paul Bakker5121ce52009-01-03 21:22:43 +00008859/*
8860 * Free an SSL context
8861 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008862void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008863{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008864 if( ssl == NULL )
8865 return;
8866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008868
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008869 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008870 {
Angus Grattond8213d02016-05-25 20:56:48 +10008871 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008872 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008873 }
8874
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008875 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008876 {
Angus Grattond8213d02016-05-25 20:56:48 +10008877 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008878 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008879 }
8880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008881#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008882 if( ssl->compress_buf != NULL )
8883 {
Angus Grattond8213d02016-05-25 20:56:48 +10008884 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008885 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008886 }
8887#endif
8888
Paul Bakker48916f92012-09-16 19:57:18 +00008889 if( ssl->transform )
8890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008891 mbedtls_ssl_transform_free( ssl->transform );
8892 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008893 }
8894
8895 if( ssl->handshake )
8896 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008897 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008898 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8899 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008901 mbedtls_free( ssl->handshake );
8902 mbedtls_free( ssl->transform_negotiate );
8903 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008904 }
8905
Paul Bakkerc0463502013-02-14 11:19:38 +01008906 if( ssl->session )
8907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008908 mbedtls_ssl_session_free( ssl->session );
8909 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008910 }
8911
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008912#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008913 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008914 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008915 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008916 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008917 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008918#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008920#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8921 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8924 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008925 }
8926#endif
8927
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008928#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008929 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008930#endif
8931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008933
Paul Bakker86f04f42013-02-14 11:20:09 +01008934 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008935 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008936}
8937
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008938/*
8939 * Initialze mbedtls_ssl_config
8940 */
8941void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8942{
8943 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8944}
8945
Simon Butcherc97b6972015-12-27 23:48:17 +00008946#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008947static int ssl_preset_default_hashes[] = {
8948#if defined(MBEDTLS_SHA512_C)
8949 MBEDTLS_MD_SHA512,
8950 MBEDTLS_MD_SHA384,
8951#endif
8952#if defined(MBEDTLS_SHA256_C)
8953 MBEDTLS_MD_SHA256,
8954 MBEDTLS_MD_SHA224,
8955#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008956#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008957 MBEDTLS_MD_SHA1,
8958#endif
8959 MBEDTLS_MD_NONE
8960};
Simon Butcherc97b6972015-12-27 23:48:17 +00008961#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008962
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008963static int ssl_preset_suiteb_ciphersuites[] = {
8964 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8965 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8966 0
8967};
8968
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008969#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008970static int ssl_preset_suiteb_hashes[] = {
8971 MBEDTLS_MD_SHA256,
8972 MBEDTLS_MD_SHA384,
8973 MBEDTLS_MD_NONE
8974};
8975#endif
8976
8977#if defined(MBEDTLS_ECP_C)
8978static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8979 MBEDTLS_ECP_DP_SECP256R1,
8980 MBEDTLS_ECP_DP_SECP384R1,
8981 MBEDTLS_ECP_DP_NONE
8982};
8983#endif
8984
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008985/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008986 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008987 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008988int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008989 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008990{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008991#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008992 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008993#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008994
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008995 /* Use the functions here so that they are covered in tests,
8996 * but otherwise access member directly for efficiency */
8997 mbedtls_ssl_conf_endpoint( conf, endpoint );
8998 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008999
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009000 /*
9001 * Things that are common to all presets
9002 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009003#if defined(MBEDTLS_SSL_CLI_C)
9004 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9005 {
9006 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9007#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9008 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9009#endif
9010 }
9011#endif
9012
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009013#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009014 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009015#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009016
9017#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9018 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9019#endif
9020
9021#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9022 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9023#endif
9024
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009025#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9026 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9027#endif
9028
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009029#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009030 conf->f_cookie_write = ssl_cookie_write_dummy;
9031 conf->f_cookie_check = ssl_cookie_check_dummy;
9032#endif
9033
9034#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9035 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9036#endif
9037
Janos Follath088ce432017-04-10 12:42:31 +01009038#if defined(MBEDTLS_SSL_SRV_C)
9039 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9040#endif
9041
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009042#if defined(MBEDTLS_SSL_PROTO_DTLS)
9043 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9044 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9045#endif
9046
9047#if defined(MBEDTLS_SSL_RENEGOTIATION)
9048 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009049 memset( conf->renego_period, 0x00, 2 );
9050 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009051#endif
9052
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009053#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9054 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9055 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009056 const unsigned char dhm_p[] =
9057 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9058 const unsigned char dhm_g[] =
9059 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9060
Hanno Beckera90658f2017-10-04 15:29:08 +01009061 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9062 dhm_p, sizeof( dhm_p ),
9063 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009064 {
9065 return( ret );
9066 }
9067 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009068#endif
9069
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009070 /*
9071 * Preset-specific defaults
9072 */
9073 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009074 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009075 /*
9076 * NSA Suite B
9077 */
9078 case MBEDTLS_SSL_PRESET_SUITEB:
9079 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9080 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9081 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9082 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9083
9084 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9085 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9086 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9087 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9088 ssl_preset_suiteb_ciphersuites;
9089
9090#if defined(MBEDTLS_X509_CRT_PARSE_C)
9091 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009092#endif
9093
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009094#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009095 conf->sig_hashes = ssl_preset_suiteb_hashes;
9096#endif
9097
9098#if defined(MBEDTLS_ECP_C)
9099 conf->curve_list = ssl_preset_suiteb_curves;
9100#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009101 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009102
9103 /*
9104 * Default
9105 */
9106 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009107 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9108 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9109 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9110 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9111 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9112 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9113 MBEDTLS_SSL_MIN_MINOR_VERSION :
9114 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009115 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9116 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9117
9118#if defined(MBEDTLS_SSL_PROTO_DTLS)
9119 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9120 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9121#endif
9122
9123 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9124 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9125 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9126 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9127 mbedtls_ssl_list_ciphersuites();
9128
9129#if defined(MBEDTLS_X509_CRT_PARSE_C)
9130 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9131#endif
9132
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009133#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009134 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009135#endif
9136
9137#if defined(MBEDTLS_ECP_C)
9138 conf->curve_list = mbedtls_ecp_grp_id_list();
9139#endif
9140
9141#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9142 conf->dhm_min_bitlen = 1024;
9143#endif
9144 }
9145
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009146 return( 0 );
9147}
9148
9149/*
9150 * Free mbedtls_ssl_config
9151 */
9152void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9153{
9154#if defined(MBEDTLS_DHM_C)
9155 mbedtls_mpi_free( &conf->dhm_P );
9156 mbedtls_mpi_free( &conf->dhm_G );
9157#endif
9158
9159#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9160 if( conf->psk != NULL )
9161 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009162 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009163 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009164 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009165 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009166 }
9167
9168 if( conf->psk_identity != NULL )
9169 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009170 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009171 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009172 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009173 conf->psk_identity_len = 0;
9174 }
9175#endif
9176
9177#if defined(MBEDTLS_X509_CRT_PARSE_C)
9178 ssl_key_cert_free( conf->key_cert );
9179#endif
9180
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009181 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009182}
9183
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009184#if defined(MBEDTLS_PK_C) && \
9185 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009186/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009187 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009189unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009190{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009191#if defined(MBEDTLS_RSA_C)
9192 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9193 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009194#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009195#if defined(MBEDTLS_ECDSA_C)
9196 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9197 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009198#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009199 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009200}
9201
Hanno Becker7e5437a2017-04-28 17:15:26 +01009202unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9203{
9204 switch( type ) {
9205 case MBEDTLS_PK_RSA:
9206 return( MBEDTLS_SSL_SIG_RSA );
9207 case MBEDTLS_PK_ECDSA:
9208 case MBEDTLS_PK_ECKEY:
9209 return( MBEDTLS_SSL_SIG_ECDSA );
9210 default:
9211 return( MBEDTLS_SSL_SIG_ANON );
9212 }
9213}
9214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009215mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009216{
9217 switch( sig )
9218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009219#if defined(MBEDTLS_RSA_C)
9220 case MBEDTLS_SSL_SIG_RSA:
9221 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009222#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009223#if defined(MBEDTLS_ECDSA_C)
9224 case MBEDTLS_SSL_SIG_ECDSA:
9225 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009226#endif
9227 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009228 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009229 }
9230}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009231#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009232
Hanno Becker7e5437a2017-04-28 17:15:26 +01009233#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9234 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9235
9236/* Find an entry in a signature-hash set matching a given hash algorithm. */
9237mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9238 mbedtls_pk_type_t sig_alg )
9239{
9240 switch( sig_alg )
9241 {
9242 case MBEDTLS_PK_RSA:
9243 return( set->rsa );
9244 case MBEDTLS_PK_ECDSA:
9245 return( set->ecdsa );
9246 default:
9247 return( MBEDTLS_MD_NONE );
9248 }
9249}
9250
9251/* Add a signature-hash-pair to a signature-hash set */
9252void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9253 mbedtls_pk_type_t sig_alg,
9254 mbedtls_md_type_t md_alg )
9255{
9256 switch( sig_alg )
9257 {
9258 case MBEDTLS_PK_RSA:
9259 if( set->rsa == MBEDTLS_MD_NONE )
9260 set->rsa = md_alg;
9261 break;
9262
9263 case MBEDTLS_PK_ECDSA:
9264 if( set->ecdsa == MBEDTLS_MD_NONE )
9265 set->ecdsa = md_alg;
9266 break;
9267
9268 default:
9269 break;
9270 }
9271}
9272
9273/* Allow exactly one hash algorithm for each signature. */
9274void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9275 mbedtls_md_type_t md_alg )
9276{
9277 set->rsa = md_alg;
9278 set->ecdsa = md_alg;
9279}
9280
9281#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9282 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9283
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009284/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009285 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009287mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009288{
9289 switch( hash )
9290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009291#if defined(MBEDTLS_MD5_C)
9292 case MBEDTLS_SSL_HASH_MD5:
9293 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009294#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009295#if defined(MBEDTLS_SHA1_C)
9296 case MBEDTLS_SSL_HASH_SHA1:
9297 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009298#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009299#if defined(MBEDTLS_SHA256_C)
9300 case MBEDTLS_SSL_HASH_SHA224:
9301 return( MBEDTLS_MD_SHA224 );
9302 case MBEDTLS_SSL_HASH_SHA256:
9303 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009304#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009305#if defined(MBEDTLS_SHA512_C)
9306 case MBEDTLS_SSL_HASH_SHA384:
9307 return( MBEDTLS_MD_SHA384 );
9308 case MBEDTLS_SSL_HASH_SHA512:
9309 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009310#endif
9311 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009312 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009313 }
9314}
9315
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009316/*
9317 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9318 */
9319unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9320{
9321 switch( md )
9322 {
9323#if defined(MBEDTLS_MD5_C)
9324 case MBEDTLS_MD_MD5:
9325 return( MBEDTLS_SSL_HASH_MD5 );
9326#endif
9327#if defined(MBEDTLS_SHA1_C)
9328 case MBEDTLS_MD_SHA1:
9329 return( MBEDTLS_SSL_HASH_SHA1 );
9330#endif
9331#if defined(MBEDTLS_SHA256_C)
9332 case MBEDTLS_MD_SHA224:
9333 return( MBEDTLS_SSL_HASH_SHA224 );
9334 case MBEDTLS_MD_SHA256:
9335 return( MBEDTLS_SSL_HASH_SHA256 );
9336#endif
9337#if defined(MBEDTLS_SHA512_C)
9338 case MBEDTLS_MD_SHA384:
9339 return( MBEDTLS_SSL_HASH_SHA384 );
9340 case MBEDTLS_MD_SHA512:
9341 return( MBEDTLS_SSL_HASH_SHA512 );
9342#endif
9343 default:
9344 return( MBEDTLS_SSL_HASH_NONE );
9345 }
9346}
9347
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009348#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009349/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009350 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009351 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009352 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009353int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009354{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009355 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009356
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009357 if( ssl->conf->curve_list == NULL )
9358 return( -1 );
9359
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009360 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009361 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009362 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009363
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009364 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009365}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009366#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009367
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009368#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009369/*
9370 * Check if a hash proposed by the peer is in our list.
9371 * Return 0 if we're willing to use it, -1 otherwise.
9372 */
9373int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9374 mbedtls_md_type_t md )
9375{
9376 const int *cur;
9377
9378 if( ssl->conf->sig_hashes == NULL )
9379 return( -1 );
9380
9381 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9382 if( *cur == (int) md )
9383 return( 0 );
9384
9385 return( -1 );
9386}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009387#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009389#if defined(MBEDTLS_X509_CRT_PARSE_C)
9390int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9391 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009392 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009393 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009394{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009395 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009396#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009397 int usage = 0;
9398#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009399#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009400 const char *ext_oid;
9401 size_t ext_len;
9402#endif
9403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009404#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9405 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009406 ((void) cert);
9407 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009408 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009409#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009411#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9412 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009413 {
9414 /* Server part of the key exchange */
9415 switch( ciphersuite->key_exchange )
9416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009417 case MBEDTLS_KEY_EXCHANGE_RSA:
9418 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009419 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009420 break;
9421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009422 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9423 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9424 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9425 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009426 break;
9427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009428 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9429 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009430 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009431 break;
9432
9433 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009434 case MBEDTLS_KEY_EXCHANGE_NONE:
9435 case MBEDTLS_KEY_EXCHANGE_PSK:
9436 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9437 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009438 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009439 usage = 0;
9440 }
9441 }
9442 else
9443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009444 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9445 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009446 }
9447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009448 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009449 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009450 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009451 ret = -1;
9452 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009453#else
9454 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009455#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009457#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9458 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009460 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9461 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009462 }
9463 else
9464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009465 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9466 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009467 }
9468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009469 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009470 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009471 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009472 ret = -1;
9473 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009474#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009475
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009476 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009477}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009478#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009479
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009480/*
9481 * Convert version numbers to/from wire format
9482 * and, for DTLS, to/from TLS equivalent.
9483 *
9484 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009485 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009486 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9487 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009489void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009490 unsigned char ver[2] )
9491{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009492#if defined(MBEDTLS_SSL_PROTO_DTLS)
9493 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009495 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009496 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9497
9498 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9499 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9500 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009501 else
9502#else
9503 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009504#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009505 {
9506 ver[0] = (unsigned char) major;
9507 ver[1] = (unsigned char) minor;
9508 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009509}
9510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009512 const unsigned char ver[2] )
9513{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009514#if defined(MBEDTLS_SSL_PROTO_DTLS)
9515 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009516 {
9517 *major = 255 - ver[0] + 2;
9518 *minor = 255 - ver[1] + 1;
9519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009521 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9522 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009523 else
9524#else
9525 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009526#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009527 {
9528 *major = ver[0];
9529 *minor = ver[1];
9530 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009531}
9532
Simon Butcher99000142016-10-13 17:21:01 +01009533int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9534{
9535#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9536 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9537 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9538
9539 switch( md )
9540 {
9541#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9542#if defined(MBEDTLS_MD5_C)
9543 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009544 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009545#endif
9546#if defined(MBEDTLS_SHA1_C)
9547 case MBEDTLS_SSL_HASH_SHA1:
9548 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9549 break;
9550#endif
9551#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9552#if defined(MBEDTLS_SHA512_C)
9553 case MBEDTLS_SSL_HASH_SHA384:
9554 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9555 break;
9556#endif
9557#if defined(MBEDTLS_SHA256_C)
9558 case MBEDTLS_SSL_HASH_SHA256:
9559 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9560 break;
9561#endif
9562 default:
9563 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9564 }
9565
9566 return 0;
9567#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9568 (void) ssl;
9569 (void) md;
9570
9571 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9572#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9573}
9574
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009575#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9576 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9577int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9578 unsigned char *output,
9579 unsigned char *data, size_t data_len )
9580{
9581 int ret = 0;
9582 mbedtls_md5_context mbedtls_md5;
9583 mbedtls_sha1_context mbedtls_sha1;
9584
9585 mbedtls_md5_init( &mbedtls_md5 );
9586 mbedtls_sha1_init( &mbedtls_sha1 );
9587
9588 /*
9589 * digitally-signed struct {
9590 * opaque md5_hash[16];
9591 * opaque sha_hash[20];
9592 * };
9593 *
9594 * md5_hash
9595 * MD5(ClientHello.random + ServerHello.random
9596 * + ServerParams);
9597 * sha_hash
9598 * SHA(ClientHello.random + ServerHello.random
9599 * + ServerParams);
9600 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009601 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009602 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009603 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009604 goto exit;
9605 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009606 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009607 ssl->handshake->randbytes, 64 ) ) != 0 )
9608 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009609 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009610 goto exit;
9611 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009612 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009613 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009614 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009615 goto exit;
9616 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009617 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009618 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009619 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009620 goto exit;
9621 }
9622
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009623 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009624 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009625 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009626 goto exit;
9627 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009628 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009629 ssl->handshake->randbytes, 64 ) ) != 0 )
9630 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009631 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009632 goto exit;
9633 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009634 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009635 data_len ) ) != 0 )
9636 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009637 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009638 goto exit;
9639 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009640 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009641 output + 16 ) ) != 0 )
9642 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009643 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009644 goto exit;
9645 }
9646
9647exit:
9648 mbedtls_md5_free( &mbedtls_md5 );
9649 mbedtls_sha1_free( &mbedtls_sha1 );
9650
9651 if( ret != 0 )
9652 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9653 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9654
9655 return( ret );
9656
9657}
9658#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9659 MBEDTLS_SSL_PROTO_TLS1_1 */
9660
9661#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9662 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9663int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009664 unsigned char *hash, size_t *hashlen,
9665 unsigned char *data, size_t data_len,
9666 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009667{
9668 int ret = 0;
9669 mbedtls_md_context_t ctx;
9670 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009671 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009672
9673 mbedtls_md_init( &ctx );
9674
9675 /*
9676 * digitally-signed struct {
9677 * opaque client_random[32];
9678 * opaque server_random[32];
9679 * ServerDHParams params;
9680 * };
9681 */
9682 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9683 {
9684 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9685 goto exit;
9686 }
9687 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9688 {
9689 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9690 goto exit;
9691 }
9692 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9693 {
9694 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9695 goto exit;
9696 }
9697 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9698 {
9699 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9700 goto exit;
9701 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009702 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009703 {
9704 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9705 goto exit;
9706 }
9707
9708exit:
9709 mbedtls_md_free( &ctx );
9710
9711 if( ret != 0 )
9712 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9713 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9714
9715 return( ret );
9716}
9717#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9718 MBEDTLS_SSL_PROTO_TLS1_2 */
9719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009720#endif /* MBEDTLS_SSL_TLS_C */