blob: c123c7a3200dcddebe1d5a797eea61035e14a685 [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 Becker12555c62018-08-16 12:47:53 +010058static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context *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 );
121static int ssl_another_record_in_datagram( mbedtls_ssl_context *ssl );
122
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 {
1727 /*
1728 * MAC(MAC_write_key, seq_num +
1729 * TLSCipherText.type +
1730 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001731 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001732 * IV + // except for TLS 1.0
1733 * ENC(content + padding + padding_length));
1734 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001735 unsigned char pseudo_hdr[13];
1736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001738
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001739 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1740 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001741 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1742 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1747 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001748 ssl->out_iv, ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001750 ssl->out_iv + ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001752
1753 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001754 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001755 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001757 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001758 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001760 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1763 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001764 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001766 /* Make extra sure authentication was performed, exactly once */
1767 if( auth_done != 1 )
1768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1770 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001771 }
1772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001774
1775 return( 0 );
1776}
1777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001779{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001781 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001782#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001783 size_t padlen = 0, correct = 1;
1784#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001787
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001788 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1791 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001792 }
1793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001795
Paul Bakker48916f92012-09-16 19:57:18 +00001796 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001799 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001801 }
1802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1804 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001805 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001806 int ret;
1807 size_t olen = 0;
1808
Paul Bakker68884e32013-01-07 18:20:04 +01001809 padlen = 0;
1810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001812 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001813 ssl->transform_in->ivlen,
1814 ssl->in_msg, ssl->in_msglen,
1815 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001818 return( ret );
1819 }
1820
1821 if( ssl->in_msglen != olen )
1822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1824 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001825 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001826 }
Paul Bakker68884e32013-01-07 18:20:04 +01001827 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001829#if defined(MBEDTLS_GCM_C) || \
1830 defined(MBEDTLS_CCM_C) || \
1831 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001833 mode == MBEDTLS_MODE_CCM ||
1834 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001835 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001836 int ret;
1837 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001838 unsigned char *dec_msg;
1839 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001840 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001841 unsigned char iv[12];
1842 mbedtls_ssl_transform *transform = ssl->transform_in;
1843 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001845 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001846
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001847 /*
1848 * Compute and update sizes
1849 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001850 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001853 "+ taglen (%d)", ssl->in_msglen,
1854 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001856 }
1857 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1858
Paul Bakker68884e32013-01-07 18:20:04 +01001859 dec_msg = ssl->in_msg;
1860 dec_msg_result = ssl->in_msg;
1861 ssl->in_msglen = dec_msglen;
1862
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001863 /*
1864 * Prepare additional authenticated data
1865 */
Paul Bakker68884e32013-01-07 18:20:04 +01001866 memcpy( add_data, ssl->in_ctr, 8 );
1867 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001869 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001870 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1871 add_data[12] = ssl->in_msglen & 0xFF;
1872
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001873 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001874
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001875 /*
1876 * Prepare IV
1877 */
1878 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1879 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001880 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001881 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1882 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001883
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001884 }
1885 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1886 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001887 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001888 unsigned char i;
1889
1890 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1891
1892 for( i = 0; i < 8; i++ )
1893 iv[i+4] ^= ssl->in_ctr[i];
1894 }
1895 else
1896 {
1897 /* Reminder if we ever add an AEAD mode with a different size */
1898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1899 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1900 }
1901
1902 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001904
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001905 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001906 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001909 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001910 add_data, 13,
1911 dec_msg, dec_msglen,
1912 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001913 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1918 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001919
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001920 return( ret );
1921 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001922 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001923
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001924 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1927 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001928 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001929 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001930 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1932#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001933 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001935 {
Paul Bakker45829992013-01-03 14:52:21 +01001936 /*
1937 * Decrypt and check the padding
1938 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001939 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001940 unsigned char *dec_msg;
1941 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001942 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001943 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001944 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001945
Paul Bakker5121ce52009-01-03 21:22:43 +00001946 /*
Paul Bakker45829992013-01-03 14:52:21 +01001947 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001948 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1950 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001951 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001952#endif
Paul Bakker45829992013-01-03 14:52:21 +01001953
1954 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1955 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001958 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1959 ssl->transform_in->ivlen,
1960 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001962 }
1963
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001964 dec_msglen = ssl->in_msglen;
1965 dec_msg = ssl->in_msg;
1966 dec_msg_result = ssl->in_msg;
1967
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001968 /*
1969 * Authenticate before decrypt if enabled
1970 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1972 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001973 {
Hanno Becker992b6872017-11-09 18:57:39 +00001974 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001975 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001978
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001979 dec_msglen -= ssl->transform_in->maclen;
1980 ssl->in_msglen -= ssl->transform_in->maclen;
1981
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001982 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1983 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1984 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1985 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1990 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001991 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001992 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001996 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001997 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001998 ssl->transform_in->maclen );
1999
Hanno Becker992b6872017-11-09 18:57:39 +00002000 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2001 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002006 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002007 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002008 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002010
2011 /*
2012 * Check length sanity
2013 */
2014 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002017 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002019 }
2020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002022 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002023 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002026 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002027 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002028 dec_msglen -= ssl->transform_in->ivlen;
2029 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002030
Paul Bakker48916f92012-09-16 19:57:18 +00002031 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002032 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002033 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002037 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002038 ssl->transform_in->ivlen,
2039 dec_msg, dec_msglen,
2040 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002043 return( ret );
2044 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002045
Paul Bakkercca5b812013-08-31 17:40:26 +02002046 if( dec_msglen != olen )
2047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2049 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002050 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2053 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002054 {
2055 /*
2056 * Save IV in SSL3 and TLS1
2057 */
2058 memcpy( ssl->transform_in->iv_dec,
2059 ssl->transform_in->cipher_ctx_dec.iv,
2060 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002061 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002062#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002063
2064 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002065
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002066 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002067 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069#if defined(MBEDTLS_SSL_DEBUG_ALL)
2070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002071 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002072#endif
Paul Bakker45829992013-01-03 14:52:21 +01002073 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002074 correct = 0;
2075 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077#if defined(MBEDTLS_SSL_PROTO_SSL3)
2078 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002079 {
Paul Bakker48916f92012-09-16 19:57:18 +00002080 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082#if defined(MBEDTLS_SSL_DEBUG_ALL)
2083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002084 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002085 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002086#endif
Paul Bakker45829992013-01-03 14:52:21 +01002087 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002088 }
2089 }
2090 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2092#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2093 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2094 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002095 {
2096 /*
Paul Bakker45829992013-01-03 14:52:21 +01002097 * TLSv1+: always check the padding up to the first failure
2098 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002099 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002100 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002101 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002102 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002103
Paul Bakker956c9e02013-12-19 14:42:28 +01002104 /*
2105 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002106 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002107 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002108 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002109 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002110 *
2111 * In both cases we reset padding_idx to a safe value (0) to
2112 * prevent out-of-buffer reads.
2113 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002114 correct &= ( padlen <= ssl->in_msglen );
2115 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002116 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002117
2118 padding_idx *= correct;
2119
Angus Grattonb512bc12018-06-19 15:57:50 +10002120 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002121 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002122 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002123 pad_count += real_count *
2124 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2125 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002126
2127 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002130 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002132#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002133 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002134 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002135 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2137 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2140 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002141 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002142
2143 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002145 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002147 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2150 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002151 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002152
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002153#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002155 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002156#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002157
2158 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002159 * Authenticate if not done yet.
2160 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002161 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002162#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002163 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002164 {
Hanno Becker992b6872017-11-09 18:57:39 +00002165 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002166
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002167 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002168
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002169 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2170 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172#if defined(MBEDTLS_SSL_PROTO_SSL3)
2173 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002174 {
2175 ssl_mac( &ssl->transform_in->md_ctx_dec,
2176 ssl->transform_in->mac_dec,
2177 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002178 ssl->in_ctr, ssl->in_msgtype,
2179 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002180 }
2181 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2183#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2184 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2185 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002186 {
2187 /*
2188 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002189 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002190 *
2191 * Known timing attacks:
2192 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2193 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002194 * To compensate for different timings for the MAC calculation
2195 * depending on how much padding was removed (which is determined
2196 * by padlen), process extra_run more blocks through the hash
2197 * function.
2198 *
2199 * The formula in the paper is
2200 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2201 * where L1 is the size of the header plus the decrypted message
2202 * plus CBC padding and L2 is the size of the header plus the
2203 * decrypted message. This is for an underlying hash function
2204 * with 64-byte blocks.
2205 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2206 * correctly. We round down instead of up, so -56 is the correct
2207 * value for our calculations instead of -55.
2208 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002209 * Repeat the formula rather than defining a block_size variable.
2210 * This avoids requiring division by a variable at runtime
2211 * (which would be marginally less efficient and would require
2212 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002213 */
2214 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002215
2216 /*
2217 * The next two sizes are the minimum and maximum values of
2218 * in_msglen over all padlen values.
2219 *
2220 * They're independent of padlen, since we previously did
2221 * in_msglen -= padlen.
2222 *
2223 * Note that max_len + maclen is never more than the buffer
2224 * length, as we previously did in_msglen -= maclen too.
2225 */
2226 const size_t max_len = ssl->in_msglen + padlen;
2227 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2228
Gilles Peskine20b44082018-05-29 14:06:49 +02002229 switch( ssl->transform_in->ciphersuite_info->mac )
2230 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002231#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2232 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002233 case MBEDTLS_MD_MD5:
2234 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002235 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002236 /* 8 bytes of message size, 64-byte compression blocks */
2237 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2238 ( 13 + ssl->in_msglen + 8 ) / 64;
2239 break;
2240#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002241#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002242 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002243 /* 16 bytes of message size, 128-byte compression blocks */
2244 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2245 ( 13 + ssl->in_msglen + 16 ) / 128;
2246 break;
2247#endif
2248 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002250 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2251 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002252
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002253 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2256 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2257 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2258 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002259 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002260 /* Make sure we access everything even when padlen > 0. This
2261 * makes the synchronisation requirements for just-in-time
2262 * Prime+Probe attacks much tighter and hopefully impractical. */
2263 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002264 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002265
2266 /* Call mbedtls_md_process at least once due to cache attacks
2267 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002268 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002272
2273 /* Make sure we access all the memory that could contain the MAC,
2274 * before we check it in the next code block. This makes the
2275 * synchronisation requirements for just-in-time Prime+Probe
2276 * attacks much tighter and hopefully impractical. */
2277 ssl_read_memory( ssl->in_msg + min_len,
2278 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002279 }
2280 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2282 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2285 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002286 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002287
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002288#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002289 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2290 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2291 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002292#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002293
Hanno Becker992b6872017-11-09 18:57:39 +00002294 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2295 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297#if defined(MBEDTLS_SSL_DEBUG_ALL)
2298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002299#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002300 correct = 0;
2301 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002302 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00002303
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002304 /*
2305 * Finally check the correct flag
2306 */
2307 if( correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002309 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002310#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002311
2312 /* Make extra sure authentication was performed, exactly once */
2313 if( auth_done != 1 )
2314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2316 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002317 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002318
2319 if( ssl->in_msglen == 0 )
2320 {
Angus Gratton34817922018-06-19 15:58:22 +10002321#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2322 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2323 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2324 {
2325 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2327 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2328 }
2329#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2330
Paul Bakker5121ce52009-01-03 21:22:43 +00002331 ssl->nb_zero++;
2332
2333 /*
2334 * Three or more empty messages may be a DoS attack
2335 * (excessive CPU consumption).
2336 */
2337 if( ssl->nb_zero > 3 )
2338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002340 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002342 }
2343 }
2344 else
2345 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002348 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002349 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002350 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002351 }
2352 else
2353#endif
2354 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002355 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002356 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2357 if( ++ssl->in_ctr[i - 1] != 0 )
2358 break;
2359
2360 /* The loop goes to its end iff the counter is wrapping */
2361 if( i == ssl_ep_len( ssl ) )
2362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2364 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002365 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002366 }
2367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002369
2370 return( 0 );
2371}
2372
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002373#undef MAC_NONE
2374#undef MAC_PLAINTEXT
2375#undef MAC_CIPHERTEXT
2376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002378/*
2379 * Compression/decompression functions
2380 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002382{
2383 int ret;
2384 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002385 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002386 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002387 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002390
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002391 if( len_pre == 0 )
2392 return( 0 );
2393
Paul Bakker2770fbd2012-07-03 13:30:23 +00002394 memcpy( msg_pre, ssl->out_msg, len_pre );
2395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002397 ssl->out_msglen ) );
2398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002400 ssl->out_msg, ssl->out_msglen );
2401
Paul Bakker48916f92012-09-16 19:57:18 +00002402 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2403 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2404 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002405 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002406
Paul Bakker48916f92012-09-16 19:57:18 +00002407 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002408 if( ret != Z_OK )
2409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2411 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002412 }
2413
Angus Grattond8213d02016-05-25 20:56:48 +10002414 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002415 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002418 ssl->out_msglen ) );
2419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002421 ssl->out_msg, ssl->out_msglen );
2422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002424
2425 return( 0 );
2426}
2427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002429{
2430 int ret;
2431 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002432 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002433 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002434 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002437
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002438 if( len_pre == 0 )
2439 return( 0 );
2440
Paul Bakker2770fbd2012-07-03 13:30:23 +00002441 memcpy( msg_pre, ssl->in_msg, len_pre );
2442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002444 ssl->in_msglen ) );
2445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002447 ssl->in_msg, ssl->in_msglen );
2448
Paul Bakker48916f92012-09-16 19:57:18 +00002449 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2450 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2451 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002452 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002453 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002454
Paul Bakker48916f92012-09-16 19:57:18 +00002455 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002456 if( ret != Z_OK )
2457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2459 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002460 }
2461
Angus Grattond8213d02016-05-25 20:56:48 +10002462 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002463 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002466 ssl->in_msglen ) );
2467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002469 ssl->in_msg, ssl->in_msglen );
2470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002472
2473 return( 0 );
2474}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2478static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480#if defined(MBEDTLS_SSL_PROTO_DTLS)
2481static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002482{
2483 /* If renegotiation is not enforced, retransmit until we would reach max
2484 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002485 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002486 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002487 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002488 unsigned char doublings = 1;
2489
2490 while( ratio != 0 )
2491 {
2492 ++doublings;
2493 ratio >>= 1;
2494 }
2495
2496 if( ++ssl->renego_records_seen > doublings )
2497 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002499 return( 0 );
2500 }
2501 }
2502
2503 return( ssl_write_hello_request( ssl ) );
2504}
2505#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002507
Paul Bakker5121ce52009-01-03 21:22:43 +00002508/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002509 * Fill the input message buffer by appending data to it.
2510 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002511 *
2512 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2513 * available (from this read and/or a previous one). Otherwise, an error code
2514 * is returned (possibly EOF or WANT_READ).
2515 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002516 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2517 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2518 * since we always read a whole datagram at once.
2519 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002520 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002521 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002524{
Paul Bakker23986e52011-04-24 08:57:21 +00002525 int ret;
2526 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002529
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002530 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002533 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002535 }
2536
Angus Grattond8213d02016-05-25 20:56:48 +10002537 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2540 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002541 }
2542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002544 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002545 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002546 uint32_t timeout;
2547
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002548 /* Just to be sure */
2549 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2550 {
2551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2552 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2554 }
2555
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002556 /*
2557 * The point is, we need to always read a full datagram at once, so we
2558 * sometimes read more then requested, and handle the additional data.
2559 * It could be the rest of the current record (while fetching the
2560 * header) and/or some other records in the same datagram.
2561 */
2562
2563 /*
2564 * Move to the next record in the already read datagram if applicable
2565 */
2566 if( ssl->next_record_offset != 0 )
2567 {
2568 if( ssl->in_left < ssl->next_record_offset )
2569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2571 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002572 }
2573
2574 ssl->in_left -= ssl->next_record_offset;
2575
2576 if( ssl->in_left != 0 )
2577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002579 ssl->next_record_offset ) );
2580 memmove( ssl->in_hdr,
2581 ssl->in_hdr + ssl->next_record_offset,
2582 ssl->in_left );
2583 }
2584
2585 ssl->next_record_offset = 0;
2586 }
2587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002589 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002590
2591 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002592 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002593 */
2594 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002597 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002598 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002599
2600 /*
2601 * A record can't be split accross datagrams. If we need to read but
2602 * are not at the beginning of a new record, the caller did something
2603 * wrong.
2604 */
2605 if( ssl->in_left != 0 )
2606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2608 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002609 }
2610
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002611 /*
2612 * Don't even try to read if time's out already.
2613 * This avoids by-passing the timer when repeatedly receiving messages
2614 * that will end up being dropped.
2615 */
2616 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002617 {
2618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002619 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002620 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002621 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002622 {
Angus Grattond8213d02016-05-25 20:56:48 +10002623 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002626 timeout = ssl->handshake->retransmit_timeout;
2627 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002628 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002631
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002632 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002633 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2634 timeout );
2635 else
2636 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002639
2640 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002642 }
2643
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002644 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002647 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002650 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002651 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002654 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002655 }
2656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002660 return( ret );
2661 }
2662
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002663 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002664 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002666 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002668 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002669 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002672 return( ret );
2673 }
2674
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002675 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002676 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002678 }
2679
Paul Bakker5121ce52009-01-03 21:22:43 +00002680 if( ret < 0 )
2681 return( ret );
2682
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002683 ssl->in_left = ret;
2684 }
2685 else
2686#endif
2687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002689 ssl->in_left, nb_want ) );
2690
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002691 while( ssl->in_left < nb_want )
2692 {
2693 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002694
2695 if( ssl_check_timer( ssl ) != 0 )
2696 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2697 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002698 {
2699 if( ssl->f_recv_timeout != NULL )
2700 {
2701 ret = ssl->f_recv_timeout( ssl->p_bio,
2702 ssl->in_hdr + ssl->in_left, len,
2703 ssl->conf->read_timeout );
2704 }
2705 else
2706 {
2707 ret = ssl->f_recv( ssl->p_bio,
2708 ssl->in_hdr + ssl->in_left, len );
2709 }
2710 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002713 ssl->in_left, nb_want ) );
2714 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002715
2716 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002718
2719 if( ret < 0 )
2720 return( ret );
2721
mohammad160352aecb92018-03-28 23:41:40 -07002722 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002723 {
Darryl Green11999bb2018-03-13 15:22:58 +00002724 MBEDTLS_SSL_DEBUG_MSG( 1,
2725 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002726 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002727 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2728 }
2729
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002730 ssl->in_left += ret;
2731 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002732 }
2733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002735
2736 return( 0 );
2737}
2738
2739/*
2740 * Flush any data not yet written
2741 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002743{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002744 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002745 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002748
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002749 if( ssl->f_send == NULL )
2750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002752 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002754 }
2755
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002756 /* Avoid incrementing counter if data is flushed */
2757 if( ssl->out_left == 0 )
2758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002760 return( 0 );
2761 }
2762
Paul Bakker5121ce52009-01-03 21:22:43 +00002763 while( ssl->out_left > 0 )
2764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2766 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002767
Hanno Becker2b1e3542018-08-06 11:19:13 +01002768 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002769 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002772
2773 if( ret <= 0 )
2774 return( ret );
2775
mohammad160352aecb92018-03-28 23:41:40 -07002776 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002777 {
Darryl Green11999bb2018-03-13 15:22:58 +00002778 MBEDTLS_SSL_DEBUG_MSG( 1,
2779 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002780 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002781 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2782 }
2783
Paul Bakker5121ce52009-01-03 21:22:43 +00002784 ssl->out_left -= ret;
2785 }
2786
Hanno Becker2b1e3542018-08-06 11:19:13 +01002787#if defined(MBEDTLS_SSL_PROTO_DTLS)
2788 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2789 {
2790 ssl->out_hdr = ssl->out_buf;
2791 }
2792 else
2793#endif
2794 {
2795 ssl->out_hdr = ssl->out_buf + 8;
2796 }
2797 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002800
2801 return( 0 );
2802}
2803
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002804/*
2805 * Functions to handle the DTLS retransmission state machine
2806 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002808/*
2809 * Append current handshake message to current outgoing flight
2810 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002812{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2815 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2816 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002817
2818 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002819 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002820 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002823 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002824 }
2825
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002826 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002827 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002830 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002831 }
2832
2833 /* Copy current handshake message with headers */
2834 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2835 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002836 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002837 msg->next = NULL;
2838
2839 /* Append to the current flight */
2840 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002841 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002842 else
2843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002845 while( cur->next != NULL )
2846 cur = cur->next;
2847 cur->next = msg;
2848 }
2849
Hanno Becker3b235902018-08-06 09:54:53 +01002850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002851 return( 0 );
2852}
2853
2854/*
2855 * Free the current flight of handshake messages
2856 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002858{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002859 mbedtls_ssl_flight_item *cur = flight;
2860 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002861
2862 while( cur != NULL )
2863 {
2864 next = cur->next;
2865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866 mbedtls_free( cur->p );
2867 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002868
2869 cur = next;
2870 }
2871}
2872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2874static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002875#endif
2876
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002877/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002878 * Swap transform_out and out_ctr with the alternative ones
2879 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002881{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002883 unsigned char tmp_out_ctr[8];
2884
2885 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002888 return;
2889 }
2890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002892
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002893 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002894 tmp_transform = ssl->transform_out;
2895 ssl->transform_out = ssl->handshake->alt_transform_out;
2896 ssl->handshake->alt_transform_out = tmp_transform;
2897
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002898 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002899 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2900 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002901 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002902
2903 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01002904 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2907 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2912 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002913 }
2914 }
2915#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002916}
2917
2918/*
2919 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002920 */
2921int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2922{
2923 int ret = 0;
2924
2925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2926
2927 ret = mbedtls_ssl_flight_transmit( ssl );
2928
2929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2930
2931 return( ret );
2932}
2933
2934/*
2935 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002936 *
2937 * Need to remember the current message in case flush_output returns
2938 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002939 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002940 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002941int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002942{
Hanno Becker67bc7c32018-08-06 11:33:50 +01002943 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002947 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002949
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002950 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002951 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002952 ssl_swap_epochs( ssl );
2953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002955 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002956
2957 while( ssl->handshake->cur_msg != NULL )
2958 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002959 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002960 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002961
Hanno Beckere1dcb032018-08-17 16:47:58 +01002962 int const is_finished =
2963 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2964 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2965
Hanno Becker04da1892018-08-14 13:22:10 +01002966 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2967 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2968
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002969 /* Swap epochs before sending Finished: we can't do it after
2970 * sending ChangeCipherSpec, in case write returns WANT_READ.
2971 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002972 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002973 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002975 ssl_swap_epochs( ssl );
2976 }
2977
Hanno Becker67bc7c32018-08-06 11:33:50 +01002978 ret = ssl_get_remaining_payload_in_datagram( ssl );
2979 if( ret < 0 )
2980 return( ret );
2981 max_frag_len = (size_t) ret;
2982
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002983 /* CCS is copied as is, while HS messages may need fragmentation */
2984 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2985 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002986 if( max_frag_len == 0 )
2987 {
2988 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2989 return( ret );
2990
2991 continue;
2992 }
2993
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002994 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002995 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002996 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002997
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002998 /* Update position inside current message */
2999 ssl->handshake->cur_msg_p += cur->len;
3000 }
3001 else
3002 {
3003 const unsigned char * const p = ssl->handshake->cur_msg_p;
3004 const size_t hs_len = cur->len - 12;
3005 const size_t frag_off = p - ( cur->p + 12 );
3006 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003007 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003008
Hanno Beckere1dcb032018-08-17 16:47:58 +01003009 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003010 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003011 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003012 ssl_swap_epochs( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003013
3014 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3015 return( ret );
3016
3017 continue;
3018 }
3019 max_hs_frag_len = max_frag_len - 12;
3020
3021 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3022 max_hs_frag_len : rem_len;
3023
3024 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003025 {
3026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003027 (unsigned) cur_hs_frag_len,
3028 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003029 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003030
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003031 /* Messages are stored with handshake headers as if not fragmented,
3032 * copy beginning of headers then fill fragmentation fields.
3033 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3034 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003035
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003036 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3037 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3038 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3039
Hanno Becker67bc7c32018-08-06 11:33:50 +01003040 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3041 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3042 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003043
3044 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3045
Hanno Becker3f7b9732018-08-28 09:53:25 +01003046 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003047 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3048 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003049 ssl->out_msgtype = cur->type;
3050
3051 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003052 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003053 }
3054
3055 /* If done with the current message move to the next one if any */
3056 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3057 {
3058 if( cur->next != NULL )
3059 {
3060 ssl->handshake->cur_msg = cur->next;
3061 ssl->handshake->cur_msg_p = cur->next->p + 12;
3062 }
3063 else
3064 {
3065 ssl->handshake->cur_msg = NULL;
3066 ssl->handshake->cur_msg_p = NULL;
3067 }
3068 }
3069
3070 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003071 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003073 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003074 return( ret );
3075 }
3076 }
3077
Hanno Becker67bc7c32018-08-06 11:33:50 +01003078 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3079 return( ret );
3080
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003081 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3083 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003084 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003087 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3088 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003089
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003091
3092 return( 0 );
3093}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003094
3095/*
3096 * To be called when the last message of an incoming flight is received.
3097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003099{
3100 /* We won't need to resend that one any more */
3101 ssl_flight_free( ssl->handshake->flight );
3102 ssl->handshake->flight = NULL;
3103 ssl->handshake->cur_msg = NULL;
3104
3105 /* The next incoming flight will start with this msg_seq */
3106 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3107
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003108 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003109 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003110
Hanno Becker0271f962018-08-16 13:23:47 +01003111 /* Clear future message buffering structure. */
3112 ssl_buffering_free( ssl );
3113
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003114 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003115 ssl_set_timer( ssl, 0 );
3116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3118 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003121 }
3122 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003124}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003125
3126/*
3127 * To be called when the last message of an outgoing flight is send.
3128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003130{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003131 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003132 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3135 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003138 }
3139 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003141}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003143
Paul Bakker5121ce52009-01-03 21:22:43 +00003144/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003145 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003146 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003147
3148/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003149 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003150 *
3151 * - fill in handshake headers
3152 * - update handshake checksum
3153 * - DTLS: save message for resending
3154 * - then pass to the record layer
3155 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003156 * DTLS: except for HelloRequest, messages are only queued, and will only be
3157 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003158 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003159 * Inputs:
3160 * - ssl->out_msglen: 4 + actual handshake message len
3161 * (4 is the size of handshake headers for TLS)
3162 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3163 * - ssl->out_msg + 4: the handshake message body
3164 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003165 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003166 * - ssl->out_msglen: the length of the record contents
3167 * (including handshake headers but excluding record headers)
3168 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003169 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003170int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003171{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003172 int ret;
3173 const size_t hs_len = ssl->out_msglen - 4;
3174 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003175
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3177
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003178 /*
3179 * Sanity checks
3180 */
Hanno Becker2c98db22018-08-22 16:05:47 +01003181 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003182 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3183 {
Hanno Becker2c98db22018-08-22 16:05:47 +01003184 /* In SSLv3, the client might send a NoCertificate alert. */
3185#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3186 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3187 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3188 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3189#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3190 {
3191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3192 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3193 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003194 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003195
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003196 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3197 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3198 ssl->handshake == NULL )
3199 {
3200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3201 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3202 }
3203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003205 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003206 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003208 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3210 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003211 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003212#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003213
Hanno Beckerb50a2532018-08-06 11:52:54 +01003214 /* Double-check that we did not exceed the bounds
3215 * of the outgoing record buffer.
3216 * This should never fail as the various message
3217 * writing functions must obey the bounds of the
3218 * outgoing record buffer, but better be safe.
3219 *
3220 * Note: We deliberately do not check for the MTU or MFL here.
3221 */
3222 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3223 {
3224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3225 "size %u, maximum %u",
3226 (unsigned) ssl->out_msglen,
3227 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3228 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3229 }
3230
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003231 /*
3232 * Fill handshake headers
3233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003235 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003236 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3237 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3238 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003239
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003240 /*
3241 * DTLS has additional fields in the Handshake layer,
3242 * between the length field and the actual payload:
3243 * uint16 message_seq;
3244 * uint24 fragment_offset;
3245 * uint24 fragment_length;
3246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003248 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003249 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003250 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003251 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003252 {
3253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3254 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003255 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003256 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003257 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3258 }
3259
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003260 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003261 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003262
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003263 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003264 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003265 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003266 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3267 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3268 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003269 }
3270 else
3271 {
3272 ssl->out_msg[4] = 0;
3273 ssl->out_msg[5] = 0;
3274 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003275
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003276 /* Handshake hashes are computed without fragmentation,
3277 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003278 memset( ssl->out_msg + 6, 0x00, 3 );
3279 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003280 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003282
Hanno Becker0207e532018-08-28 10:28:28 +01003283 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003284 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3285 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003286 }
3287
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003288 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003290 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Becker551835d2018-08-22 16:07:59 +01003291 ( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
3292 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003293 {
3294 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003297 return( ret );
3298 }
3299 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003300 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003301#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003302 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003303 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003304 {
3305 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3306 return( ret );
3307 }
3308 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003309
3310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3311
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003312 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003313}
3314
3315/*
3316 * Record layer functions
3317 */
3318
3319/*
3320 * Write current record.
3321 *
3322 * Uses:
3323 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3324 * - ssl->out_msglen: length of the record content (excl headers)
3325 * - ssl->out_msg: record content
3326 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003327int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003328{
3329 int ret, done = 0;
3330 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003331 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003332
3333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
3334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003336 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003338 {
3339 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003342 return( ret );
3343 }
3344
3345 len = ssl->out_msglen;
3346 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3350 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354 ret = mbedtls_ssl_hw_record_write( ssl );
3355 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3358 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003359 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003360
3361 if( ret == 0 )
3362 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003363 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003365 if( !done )
3366 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003367 unsigned i;
3368 size_t protected_record_size;
3369
Paul Bakker05ef8352012-05-08 09:17:57 +00003370 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003372 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003373
Hanno Becker19859472018-08-06 09:40:20 +01003374 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003375 ssl->out_len[0] = (unsigned char)( len >> 8 );
3376 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003377
Paul Bakker48916f92012-09-16 19:57:18 +00003378 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003379 {
3380 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003383 return( ret );
3384 }
3385
3386 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003387 ssl->out_len[0] = (unsigned char)( len >> 8 );
3388 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003389 }
3390
Hanno Becker2b1e3542018-08-06 11:19:13 +01003391 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3392
3393#if defined(MBEDTLS_SSL_PROTO_DTLS)
3394 /* In case of DTLS, double-check that we don't exceed
3395 * the remaining space in the datagram. */
3396 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3397 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003398 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003399 if( ret < 0 )
3400 return( ret );
3401
3402 if( protected_record_size > (size_t) ret )
3403 {
3404 /* Should never happen */
3405 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3406 }
3407 }
3408#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003411 "version = [%d:%d], msglen = %d",
3412 ssl->out_hdr[0], ssl->out_hdr[1],
3413 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003416 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003417
3418 ssl->out_left += protected_record_size;
3419 ssl->out_hdr += protected_record_size;
3420 ssl_update_out_pointers( ssl, ssl->transform_out );
3421
Hanno Becker04484622018-08-06 09:49:38 +01003422 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3423 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3424 break;
3425
3426 /* The loop goes to its end iff the counter is wrapping */
3427 if( i == ssl_ep_len( ssl ) )
3428 {
3429 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3430 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3431 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003432 }
3433
Hanno Becker67bc7c32018-08-06 11:33:50 +01003434#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003435 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3436 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003437 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003438 size_t remaining;
3439 ret = ssl_get_remaining_payload_in_datagram( ssl );
3440 if( ret < 0 )
3441 {
3442 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3443 ret );
3444 return( ret );
3445 }
3446
3447 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003448 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003449 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003450 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003451 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003452 else
3453 {
Hanno Becker513815a2018-08-20 11:56:09 +01003454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003455 }
3456 }
3457#endif /* MBEDTLS_SSL_PROTO_DTLS */
3458
3459 if( ( flush == SSL_FORCE_FLUSH ) &&
3460 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003463 return( ret );
3464 }
3465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003467
3468 return( 0 );
3469}
3470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003471#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003472
3473static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3474{
3475 if( ssl->in_msglen < ssl->in_hslen ||
3476 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3477 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3478 {
3479 return( 1 );
3480 }
3481 return( 0 );
3482}
Hanno Becker44650b72018-08-16 12:51:11 +01003483
3484static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context *ssl )
3485{
3486 return( ( ssl->in_msg[9] << 16 ) |
3487 ( ssl->in_msg[10] << 8 ) |
3488 ssl->in_msg[11] );
3489}
3490
3491static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context *ssl )
3492{
3493 return( ( ssl->in_msg[6] << 16 ) |
3494 ( ssl->in_msg[7] << 8 ) |
3495 ssl->in_msg[8] );
3496}
3497
3498static int ssl_check_hs_header( mbedtls_ssl_context *ssl )
3499{
3500 uint32_t msg_len, frag_off, frag_len;
3501
3502 msg_len = ssl_get_hs_total_len( ssl );
3503 frag_off = ssl_get_hs_frag_off( ssl );
3504 frag_len = ssl_get_hs_frag_len( ssl );
3505
3506 if( frag_off > msg_len )
3507 return( -1 );
3508
3509 if( frag_len > msg_len - frag_off )
3510 return( -1 );
3511
3512 if( frag_len + 12 > ssl->in_msglen )
3513 return( -1 );
3514
3515 return( 0 );
3516}
3517
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003518/*
3519 * Mark bits in bitmask (used for DTLS HS reassembly)
3520 */
3521static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3522{
3523 unsigned int start_bits, end_bits;
3524
3525 start_bits = 8 - ( offset % 8 );
3526 if( start_bits != 8 )
3527 {
3528 size_t first_byte_idx = offset / 8;
3529
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003530 /* Special case */
3531 if( len <= start_bits )
3532 {
3533 for( ; len != 0; len-- )
3534 mask[first_byte_idx] |= 1 << ( start_bits - len );
3535
3536 /* Avoid potential issues with offset or len becoming invalid */
3537 return;
3538 }
3539
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003540 offset += start_bits; /* Now offset % 8 == 0 */
3541 len -= start_bits;
3542
3543 for( ; start_bits != 0; start_bits-- )
3544 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3545 }
3546
3547 end_bits = len % 8;
3548 if( end_bits != 0 )
3549 {
3550 size_t last_byte_idx = ( offset + len ) / 8;
3551
3552 len -= end_bits; /* Now len % 8 == 0 */
3553
3554 for( ; end_bits != 0; end_bits-- )
3555 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3556 }
3557
3558 memset( mask + offset / 8, 0xFF, len / 8 );
3559}
3560
3561/*
3562 * Check that bitmask is full
3563 */
3564static int ssl_bitmask_check( unsigned char *mask, size_t len )
3565{
3566 size_t i;
3567
3568 for( i = 0; i < len / 8; i++ )
3569 if( mask[i] != 0xFF )
3570 return( -1 );
3571
3572 for( i = 0; i < len % 8; i++ )
3573 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3574 return( -1 );
3575
3576 return( 0 );
3577}
3578
Hanno Becker56e205e2018-08-16 09:06:12 +01003579/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003580static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003581 unsigned add_bitmap )
Hanno Becker56e205e2018-08-16 09:06:12 +01003582{
3583 size_t alloc_len;
Hanno Becker56e205e2018-08-16 09:06:12 +01003584
3585 alloc_len = 12; /* Handshake header */
3586 alloc_len += msg_len; /* Content buffer */
Hanno Beckerd07df862018-08-16 09:14:58 +01003587
3588 if( add_bitmap )
3589 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Hanno Becker56e205e2018-08-16 09:06:12 +01003590
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003591 return( alloc_len );
Hanno Becker56e205e2018-08-16 09:06:12 +01003592}
3593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003595
Hanno Becker12555c62018-08-16 12:47:53 +01003596static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context *ssl )
3597{
3598 return( ( ssl->in_msg[1] << 16 ) |
3599 ( ssl->in_msg[2] << 8 ) |
3600 ssl->in_msg[3] );
3601}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003602
Simon Butcher99000142016-10-13 17:21:01 +01003603int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003604{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003608 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003609 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003610 }
3611
Hanno Becker12555c62018-08-16 12:47:53 +01003612 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003615 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003616 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003619 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003620 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003621 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003622 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003623
Hanno Becker44650b72018-08-16 12:51:11 +01003624 if( ssl_check_hs_header( ssl ) != 0 )
3625 {
3626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3627 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3628 }
3629
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003630 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003631 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3632 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3633 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3634 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003635 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003636 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3637 {
3638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3639 recv_msg_seq,
3640 ssl->handshake->in_msg_seq ) );
3641 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3642 }
3643
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003644 /* Retransmit only on last message from previous flight, to avoid
3645 * too many retransmissions.
3646 * Besides, No sane server ever retransmits HelloVerifyRequest */
3647 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003651 "message_seq = %d, start_of_flight = %d",
3652 recv_msg_seq,
3653 ssl->handshake->in_flight_start_seq ) );
3654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003658 return( ret );
3659 }
3660 }
3661 else
3662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003664 "message_seq = %d, expected = %d",
3665 recv_msg_seq,
3666 ssl->handshake->in_msg_seq ) );
3667 }
3668
Hanno Becker90333da2017-10-10 11:27:13 +01003669 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003670 }
3671 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003672
Hanno Becker6d97ef52018-08-16 13:09:04 +01003673 /* Message reassembly is handled alongside buffering of future
3674 * messages; the commonality is that both handshake fragments and
3675 * future messages cannot be forwarded immediately to the handshake
3676 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003677 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003680 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003681 }
3682 }
3683 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003684#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003685 /* With TLS we don't handle fragmentation (for now) */
3686 if( ssl->in_msglen < ssl->in_hslen )
3687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3689 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003690 }
3691
Simon Butcher99000142016-10-13 17:21:01 +01003692 return( 0 );
3693}
3694
3695void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3696{
Hanno Becker0271f962018-08-16 13:23:47 +01003697 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003698
Hanno Becker0271f962018-08-16 13:23:47 +01003699 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003700 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003701 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003702 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003703
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003704 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003705#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003706 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003707 ssl->handshake != NULL )
3708 {
Hanno Becker0271f962018-08-16 13:23:47 +01003709 unsigned offset;
3710 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003711
Hanno Becker0271f962018-08-16 13:23:47 +01003712 /* Increment handshake sequence number */
3713 hs->in_msg_seq++;
3714
3715 /*
3716 * Clear up handshake buffering and reassembly structure.
3717 */
3718
3719 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003720 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003721
3722 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003723 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3724 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003725 offset++, hs_buf++ )
3726 {
3727 *hs_buf = *(hs_buf + 1);
3728 }
3729
3730 /* Create a fresh last entry */
3731 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003732 }
3733#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003734}
3735
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003736/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003737 * DTLS anti-replay: RFC 6347 4.1.2.6
3738 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003739 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3740 * Bit n is set iff record number in_window_top - n has been seen.
3741 *
3742 * Usually, in_window_top is the last record number seen and the lsb of
3743 * in_window is set. The only exception is the initial state (record number 0
3744 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003745 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003746#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3747static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003748{
3749 ssl->in_window_top = 0;
3750 ssl->in_window = 0;
3751}
3752
3753static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3754{
3755 return( ( (uint64_t) buf[0] << 40 ) |
3756 ( (uint64_t) buf[1] << 32 ) |
3757 ( (uint64_t) buf[2] << 24 ) |
3758 ( (uint64_t) buf[3] << 16 ) |
3759 ( (uint64_t) buf[4] << 8 ) |
3760 ( (uint64_t) buf[5] ) );
3761}
3762
3763/*
3764 * Return 0 if sequence number is acceptable, -1 otherwise
3765 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003766int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003767{
3768 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3769 uint64_t bit;
3770
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003771 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003772 return( 0 );
3773
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003774 if( rec_seqnum > ssl->in_window_top )
3775 return( 0 );
3776
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003777 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003778
3779 if( bit >= 64 )
3780 return( -1 );
3781
3782 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3783 return( -1 );
3784
3785 return( 0 );
3786}
3787
3788/*
3789 * Update replay window on new validated record
3790 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003791void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003792{
3793 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3794
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003795 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003796 return;
3797
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003798 if( rec_seqnum > ssl->in_window_top )
3799 {
3800 /* Update window_top and the contents of the window */
3801 uint64_t shift = rec_seqnum - ssl->in_window_top;
3802
3803 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003804 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003805 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003806 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003807 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003808 ssl->in_window |= 1;
3809 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003810
3811 ssl->in_window_top = rec_seqnum;
3812 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003813 else
3814 {
3815 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003816 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003817
3818 if( bit < 64 ) /* Always true, but be extra sure */
3819 ssl->in_window |= (uint64_t) 1 << bit;
3820 }
3821}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003822#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003823
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003824#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003825/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003826static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3827
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003828/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003829 * Without any SSL context, check if a datagram looks like a ClientHello with
3830 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003831 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003832 *
3833 * - if cookie is valid, return 0
3834 * - if ClientHello looks superficially valid but cookie is not,
3835 * fill obuf and set olen, then
3836 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3837 * - otherwise return a specific error code
3838 */
3839static int ssl_check_dtls_clihlo_cookie(
3840 mbedtls_ssl_cookie_write_t *f_cookie_write,
3841 mbedtls_ssl_cookie_check_t *f_cookie_check,
3842 void *p_cookie,
3843 const unsigned char *cli_id, size_t cli_id_len,
3844 const unsigned char *in, size_t in_len,
3845 unsigned char *obuf, size_t buf_len, size_t *olen )
3846{
3847 size_t sid_len, cookie_len;
3848 unsigned char *p;
3849
3850 if( f_cookie_write == NULL || f_cookie_check == NULL )
3851 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3852
3853 /*
3854 * Structure of ClientHello with record and handshake headers,
3855 * and expected values. We don't need to check a lot, more checks will be
3856 * done when actually parsing the ClientHello - skipping those checks
3857 * avoids code duplication and does not make cookie forging any easier.
3858 *
3859 * 0-0 ContentType type; copied, must be handshake
3860 * 1-2 ProtocolVersion version; copied
3861 * 3-4 uint16 epoch; copied, must be 0
3862 * 5-10 uint48 sequence_number; copied
3863 * 11-12 uint16 length; (ignored)
3864 *
3865 * 13-13 HandshakeType msg_type; (ignored)
3866 * 14-16 uint24 length; (ignored)
3867 * 17-18 uint16 message_seq; copied
3868 * 19-21 uint24 fragment_offset; copied, must be 0
3869 * 22-24 uint24 fragment_length; (ignored)
3870 *
3871 * 25-26 ProtocolVersion client_version; (ignored)
3872 * 27-58 Random random; (ignored)
3873 * 59-xx SessionID session_id; 1 byte len + sid_len content
3874 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3875 * ...
3876 *
3877 * Minimum length is 61 bytes.
3878 */
3879 if( in_len < 61 ||
3880 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3881 in[3] != 0 || in[4] != 0 ||
3882 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3883 {
3884 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3885 }
3886
3887 sid_len = in[59];
3888 if( sid_len > in_len - 61 )
3889 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3890
3891 cookie_len = in[60 + sid_len];
3892 if( cookie_len > in_len - 60 )
3893 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3894
3895 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3896 cli_id, cli_id_len ) == 0 )
3897 {
3898 /* Valid cookie */
3899 return( 0 );
3900 }
3901
3902 /*
3903 * If we get here, we've got an invalid cookie, let's prepare HVR.
3904 *
3905 * 0-0 ContentType type; copied
3906 * 1-2 ProtocolVersion version; copied
3907 * 3-4 uint16 epoch; copied
3908 * 5-10 uint48 sequence_number; copied
3909 * 11-12 uint16 length; olen - 13
3910 *
3911 * 13-13 HandshakeType msg_type; hello_verify_request
3912 * 14-16 uint24 length; olen - 25
3913 * 17-18 uint16 message_seq; copied
3914 * 19-21 uint24 fragment_offset; copied
3915 * 22-24 uint24 fragment_length; olen - 25
3916 *
3917 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3918 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3919 *
3920 * Minimum length is 28.
3921 */
3922 if( buf_len < 28 )
3923 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3924
3925 /* Copy most fields and adapt others */
3926 memcpy( obuf, in, 25 );
3927 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3928 obuf[25] = 0xfe;
3929 obuf[26] = 0xff;
3930
3931 /* Generate and write actual cookie */
3932 p = obuf + 28;
3933 if( f_cookie_write( p_cookie,
3934 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3935 {
3936 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3937 }
3938
3939 *olen = p - obuf;
3940
3941 /* Go back and fill length fields */
3942 obuf[27] = (unsigned char)( *olen - 28 );
3943
3944 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3945 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3946 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3947
3948 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3949 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3950
3951 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3952}
3953
3954/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003955 * Handle possible client reconnect with the same UDP quadruplet
3956 * (RFC 6347 Section 4.2.8).
3957 *
3958 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3959 * that looks like a ClientHello.
3960 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003961 * - if the input looks like a ClientHello without cookies,
3962 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003963 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003964 * - if the input looks like a ClientHello with a valid cookie,
3965 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003966 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003967 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003968 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003969 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003970 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3971 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003972 */
3973static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3974{
3975 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003976 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003977
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003978 ret = ssl_check_dtls_clihlo_cookie(
3979 ssl->conf->f_cookie_write,
3980 ssl->conf->f_cookie_check,
3981 ssl->conf->p_cookie,
3982 ssl->cli_id, ssl->cli_id_len,
3983 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003984 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003985
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003986 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3987
3988 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003989 {
Brian J Murray1903fb32016-11-06 04:45:15 -08003990 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003991 * If the error is permanent we'll catch it later,
3992 * if it's not, then hopefully it'll work next time. */
3993 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3994
3995 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003996 }
3997
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003998 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003999 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004000 /* Got a valid cookie, partially reset context */
4001 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4002 {
4003 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4004 return( ret );
4005 }
4006
4007 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004008 }
4009
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004010 return( ret );
4011}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004012#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004013
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004014/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004015 * ContentType type;
4016 * ProtocolVersion version;
4017 * uint16 epoch; // DTLS only
4018 * uint48 sequence_number; // DTLS only
4019 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004020 *
4021 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004022 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004023 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4024 *
4025 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004026 * 1. proceed with the record if this function returns 0
4027 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4028 * 3. return CLIENT_RECONNECT if this function return that value
4029 * 4. drop the whole datagram if this function returns anything else.
4030 * Point 2 is needed when the peer is resending, and we have already received
4031 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004032 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004033static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004034{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004035 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004037 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 +02004038
Paul Bakker5121ce52009-01-03 21:22:43 +00004039 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004040 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004041 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004043 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004044 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004045 ssl->in_msgtype,
4046 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004047
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004048 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004049 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4050 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4051 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4052 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004055
4056#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004057 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4058 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004059 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4060#endif /* MBEDTLS_SSL_PROTO_DTLS */
4061 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4062 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004065 }
4066
4067 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004068 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4071 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004072 }
4073
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004074 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4077 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004078 }
4079
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004080 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004081 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004082 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4085 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004086 }
4087
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004088 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004089 * DTLS-related tests.
4090 * Check epoch before checking length constraint because
4091 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4092 * message gets duplicated before the corresponding Finished message,
4093 * the second ChangeCipherSpec should be discarded because it belongs
4094 * to an old epoch, but not because its length is shorter than
4095 * the minimum record length for packets using the new record transform.
4096 * Note that these two kinds of failures are handled differently,
4097 * as an unexpected record is silently skipped but an invalid
4098 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004099 */
4100#if defined(MBEDTLS_SSL_PROTO_DTLS)
4101 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4102 {
4103 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4104
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004105 /* Check epoch (and sequence number) with DTLS */
4106 if( rec_epoch != ssl->in_epoch )
4107 {
4108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4109 "expected %d, received %d",
4110 ssl->in_epoch, rec_epoch ) );
4111
4112#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4113 /*
4114 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4115 * access the first byte of record content (handshake type), as we
4116 * have an active transform (possibly iv_len != 0), so use the
4117 * fact that the record header len is 13 instead.
4118 */
4119 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4120 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4121 rec_epoch == 0 &&
4122 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4123 ssl->in_left > 13 &&
4124 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4125 {
4126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4127 "from the same port" ) );
4128 return( ssl_handle_possible_reconnect( ssl ) );
4129 }
4130 else
4131#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004132 {
4133 /* Consider buffering the record. */
4134 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4135 {
4136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4137 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4138 }
4139
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004140 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004141 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004142 }
4143
4144#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4145 /* Replay detection only works for the current epoch */
4146 if( rec_epoch == ssl->in_epoch &&
4147 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4148 {
4149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4150 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4151 }
4152#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004153
Hanno Becker52c6dc62017-05-26 16:07:36 +01004154 /* Drop unexpected ApplicationData records,
4155 * except at the beginning of renegotiations */
4156 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4157 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4158#if defined(MBEDTLS_SSL_RENEGOTIATION)
4159 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4160 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4161#endif
4162 )
4163 {
4164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4165 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4166 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004167 }
4168#endif /* MBEDTLS_SSL_PROTO_DTLS */
4169
Hanno Becker52c6dc62017-05-26 16:07:36 +01004170
4171 /* Check length against bounds of the current transform and version */
4172 if( ssl->transform_in == NULL )
4173 {
4174 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004175 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004176 {
4177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4178 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4179 }
4180 }
4181 else
4182 {
4183 if( ssl->in_msglen < ssl->transform_in->minlen )
4184 {
4185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4186 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4187 }
4188
4189#if defined(MBEDTLS_SSL_PROTO_SSL3)
4190 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004191 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004192 {
4193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4194 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4195 }
4196#endif
4197#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4198 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4199 /*
4200 * TLS encrypted messages can have up to 256 bytes of padding
4201 */
4202 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4203 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004204 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004205 {
4206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4207 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4208 }
4209#endif
4210 }
4211
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004212 return( 0 );
4213}
Paul Bakker5121ce52009-01-03 21:22:43 +00004214
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004215/*
4216 * If applicable, decrypt (and decompress) record content
4217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004218static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004219{
4220 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4223 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004225#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4226 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004230 ret = mbedtls_ssl_hw_record_read( ssl );
4231 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4234 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004235 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004236
4237 if( ret == 0 )
4238 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004239 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004240#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004241 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004242 {
4243 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004245 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004246 return( ret );
4247 }
4248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004249 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004250 ssl->in_msg, ssl->in_msglen );
4251
Angus Grattond8213d02016-05-25 20:56:48 +10004252 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4255 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004256 }
4257 }
4258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004260 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004261 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004262 {
4263 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004265 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004266 return( ret );
4267 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004268 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004269#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004271#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004272 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004274 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004275 }
4276#endif
4277
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004278 return( 0 );
4279}
4280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004281static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004282
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004283/*
4284 * Read a record.
4285 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004286 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4287 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4288 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004289 */
Hanno Becker1097b342018-08-15 14:09:41 +01004290
4291/* Helper functions for mbedtls_ssl_read_record(). */
4292static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004293static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4294static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004295
Hanno Becker327c93b2018-08-15 13:56:18 +01004296int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004297 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004298{
4299 int ret;
4300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004302
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004303 if( ssl->keep_current_message == 0 )
4304 {
4305 do {
Simon Butcher99000142016-10-13 17:21:01 +01004306
Hanno Becker26994592018-08-15 14:14:59 +01004307 ret = ssl_consume_current_message( ssl );
4308 if( ret != 0 )
4309 return( ret );
4310
Hanno Beckere74d5562018-08-15 14:26:08 +01004311 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004312 {
Hanno Becker40f50842018-08-15 14:48:01 +01004313#if defined(MBEDTLS_SSL_PROTO_DTLS)
4314 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004315
Hanno Becker40f50842018-08-15 14:48:01 +01004316 /* We only check for buffered messages if the
4317 * current datagram is fully consumed. */
4318 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4319 ssl_another_record_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004320 {
Hanno Becker40f50842018-08-15 14:48:01 +01004321 if( ssl_load_buffered_message( ssl ) == 0 )
4322 have_buffered = 1;
4323 }
4324
4325 if( have_buffered == 0 )
4326#endif /* MBEDTLS_SSL_PROTO_DTLS */
4327 {
4328 ret = ssl_get_next_record( ssl );
4329 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4330 continue;
4331
4332 if( ret != 0 )
4333 {
4334 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4335 return( ret );
4336 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004337 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004338 }
4339
4340 ret = mbedtls_ssl_handle_message_type( ssl );
4341
Hanno Becker40f50842018-08-15 14:48:01 +01004342#if defined(MBEDTLS_SSL_PROTO_DTLS)
4343 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4344 {
4345 /* Buffer future message */
4346 ret = ssl_buffer_message( ssl );
4347 if( ret != 0 )
4348 return( ret );
4349
4350 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4351 }
4352#endif /* MBEDTLS_SSL_PROTO_DTLS */
4353
Hanno Becker90333da2017-10-10 11:27:13 +01004354 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4355 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004356
4357 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004358 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004359 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004360 return( ret );
4361 }
4362
Hanno Becker327c93b2018-08-15 13:56:18 +01004363 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004364 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004365 {
4366 mbedtls_ssl_update_handshake_status( ssl );
4367 }
Simon Butcher99000142016-10-13 17:21:01 +01004368 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004369 else
Simon Butcher99000142016-10-13 17:21:01 +01004370 {
Hanno Becker02f59072018-08-15 14:00:24 +01004371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004372 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004373 }
4374
4375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4376
4377 return( 0 );
4378}
4379
Hanno Becker40f50842018-08-15 14:48:01 +01004380#if defined(MBEDTLS_SSL_PROTO_DTLS)
4381static int ssl_another_record_in_datagram( mbedtls_ssl_context *ssl )
4382{
4383 if( ssl->in_left > ssl->next_record_offset )
4384 return( 1 );
4385
4386 return( 0 );
4387}
4388
4389static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4390{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004391 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004392 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004393 int ret = 0;
4394
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004395 if( hs == NULL )
4396 return( -1 );
4397
Hanno Beckere00ae372018-08-20 09:39:42 +01004398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4399
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004400 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4401 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4402 {
4403 /* Check if we have seen a ChangeCipherSpec before.
4404 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004405 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004406 {
4407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4408 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004409 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004410 }
4411
4412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Inject buffered CCS message" ) );
4413 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4414 ssl->in_msglen = 1;
4415 ssl->in_msg[0] = 1;
4416
4417 /* As long as they are equal, the exact value doesn't matter. */
4418 ssl->in_left = 0;
4419 ssl->next_record_offset = 0;
4420
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004421 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004422 goto exit;
4423 }
Hanno Becker37f95322018-08-16 13:55:32 +01004424
Hanno Beckerb8f50142018-08-28 10:01:34 +01004425#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004426 /* Debug only */
4427 {
4428 unsigned offset;
4429 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4430 {
4431 hs_buf = &hs->buffering.hs[offset];
4432 if( hs_buf->is_valid == 1 )
4433 {
4434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4435 hs->in_msg_seq + offset,
4436 hs_buf->is_complete ? "fully" : "partitially" ) );
4437 }
4438 }
4439 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004440#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004441
4442 /* Check if we have buffered and/or fully reassembled the
4443 * next handshake message. */
4444 hs_buf = &hs->buffering.hs[0];
4445 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4446 {
4447 /* Synthesize a record containing the buffered HS message. */
4448 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4449 ( hs_buf->data[2] << 8 ) |
4450 hs_buf->data[3];
4451
4452 /* Double-check that we haven't accidentally buffered
4453 * a message that doesn't fit into the input buffer. */
4454 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4455 {
4456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4457 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4458 }
4459
4460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4461 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4462 hs_buf->data, msg_len + 12 );
4463
4464 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4465 ssl->in_hslen = msg_len + 12;
4466 ssl->in_msglen = msg_len + 12;
4467 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4468
4469 ret = 0;
4470 goto exit;
4471 }
4472 else
4473 {
4474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4475 hs->in_msg_seq ) );
4476 }
4477
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004478 ret = -1;
4479
4480exit:
4481
4482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4483 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004484}
4485
Hanno Beckera02b0b42018-08-21 17:20:27 +01004486static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4487 size_t desired )
4488{
4489 int offset;
4490 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4492 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004493
Hanno Becker01315ea2018-08-21 17:22:17 +01004494 /* Get rid of future records epoch first, if such exist. */
4495 ssl_free_buffered_record( ssl );
4496
4497 /* Check if we have enough space available now. */
4498 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4499 hs->buffering.total_bytes_buffered ) )
4500 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004502 return( 0 );
4503 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004504
Hanno Becker4f432ad2018-08-28 10:02:32 +01004505 /* We don't have enough space to buffer the next expected handshake
4506 * message. Remove buffers used for future messages to gain space,
4507 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004508 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4509 offset >= 0; offset-- )
4510 {
4511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4512 offset ) );
4513
Hanno Beckerb309b922018-08-23 13:18:05 +01004514 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004515
4516 /* Check if we have enough space available now. */
4517 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4518 hs->buffering.total_bytes_buffered ) )
4519 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004521 return( 0 );
4522 }
4523 }
4524
4525 return( -1 );
4526}
4527
Hanno Becker40f50842018-08-15 14:48:01 +01004528static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4529{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004530 int ret = 0;
4531 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4532
4533 if( hs == NULL )
4534 return( 0 );
4535
4536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4537
4538 switch( ssl->in_msgtype )
4539 {
4540 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004542
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004543 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004544 break;
4545
4546 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004547 {
4548 unsigned recv_msg_seq_offset;
4549 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4550 mbedtls_ssl_hs_buffer *hs_buf;
4551 size_t msg_len = ssl->in_hslen - 12;
4552
4553 /* We should never receive an old handshake
4554 * message - double-check nonetheless. */
4555 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4556 {
4557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4558 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4559 }
4560
4561 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4562 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4563 {
4564 /* Silently ignore -- message too far in the future */
4565 MBEDTLS_SSL_DEBUG_MSG( 2,
4566 ( "Ignore future HS message with sequence number %u, "
4567 "buffering window %u - %u",
4568 recv_msg_seq, ssl->handshake->in_msg_seq,
4569 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4570
4571 goto exit;
4572 }
4573
4574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4575 recv_msg_seq, recv_msg_seq_offset ) );
4576
4577 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4578
4579 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004580 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004581 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004582 size_t reassembly_buf_sz;
4583
Hanno Becker37f95322018-08-16 13:55:32 +01004584 hs_buf->is_fragmented =
4585 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4586
4587 /* We copy the message back into the input buffer
4588 * after reassembly, so check that it's not too large.
4589 * This is an implementation-specific limitation
4590 * and not one from the standard, hence it is not
4591 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004592 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004593 {
4594 /* Ignore message */
4595 goto exit;
4596 }
4597
Hanno Beckere0b150f2018-08-21 15:51:03 +01004598 /* Check if we have enough space to buffer the message. */
4599 if( hs->buffering.total_bytes_buffered >
4600 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4601 {
4602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4603 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4604 }
4605
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004606 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4607 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004608
4609 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4610 hs->buffering.total_bytes_buffered ) )
4611 {
4612 if( recv_msg_seq_offset > 0 )
4613 {
4614 /* If we can't buffer a future message because
4615 * of space limitations -- ignore. */
4616 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",
4617 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4618 (unsigned) hs->buffering.total_bytes_buffered ) );
4619 goto exit;
4620 }
Hanno Beckere1801392018-08-21 16:51:05 +01004621 else
4622 {
4623 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",
4624 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4625 (unsigned) hs->buffering.total_bytes_buffered ) );
4626 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004627
Hanno Beckera02b0b42018-08-21 17:20:27 +01004628 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004629 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004630 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",
4631 (unsigned) msg_len,
4632 (unsigned) reassembly_buf_sz,
4633 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004634 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004635 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4636 goto exit;
4637 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004638 }
4639
4640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4641 msg_len ) );
4642
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004643 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4644 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004645 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004646 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004647 goto exit;
4648 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004649 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004650
4651 /* Prepare final header: copy msg_type, length and message_seq,
4652 * then add standardised fragment_offset and fragment_length */
4653 memcpy( hs_buf->data, ssl->in_msg, 6 );
4654 memset( hs_buf->data + 6, 0, 3 );
4655 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4656
4657 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004658
4659 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004660 }
4661 else
4662 {
4663 /* Make sure msg_type and length are consistent */
4664 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4665 {
4666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4667 /* Ignore */
4668 goto exit;
4669 }
4670 }
4671
Hanno Becker4422bbb2018-08-20 09:40:19 +01004672 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004673 {
4674 size_t frag_len, frag_off;
4675 unsigned char * const msg = hs_buf->data + 12;
4676
4677 /*
4678 * Check and copy current fragment
4679 */
4680
4681 /* Validation of header fields already done in
4682 * mbedtls_ssl_prepare_handshake_record(). */
4683 frag_off = ssl_get_hs_frag_off( ssl );
4684 frag_len = ssl_get_hs_frag_len( ssl );
4685
4686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4687 frag_off, frag_len ) );
4688 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4689
4690 if( hs_buf->is_fragmented )
4691 {
4692 unsigned char * const bitmask = msg + msg_len;
4693 ssl_bitmask_set( bitmask, frag_off, frag_len );
4694 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4695 msg_len ) == 0 );
4696 }
4697 else
4698 {
4699 hs_buf->is_complete = 1;
4700 }
4701
4702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4703 hs_buf->is_complete ? "" : "not yet " ) );
4704 }
4705
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004706 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004707 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004708
4709 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004710 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004711 break;
4712 }
4713
4714exit:
4715
4716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4717 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004718}
4719#endif /* MBEDTLS_SSL_PROTO_DTLS */
4720
Hanno Becker1097b342018-08-15 14:09:41 +01004721static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004722{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004723 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004724 * Consume last content-layer message and potentially
4725 * update in_msglen which keeps track of the contents'
4726 * consumption state.
4727 *
4728 * (1) Handshake messages:
4729 * Remove last handshake message, move content
4730 * and adapt in_msglen.
4731 *
4732 * (2) Alert messages:
4733 * Consume whole record content, in_msglen = 0.
4734 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004735 * (3) Change cipher spec:
4736 * Consume whole record content, in_msglen = 0.
4737 *
4738 * (4) Application data:
4739 * Don't do anything - the record layer provides
4740 * the application data as a stream transport
4741 * and consumes through mbedtls_ssl_read only.
4742 *
4743 */
4744
4745 /* Case (1): Handshake messages */
4746 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004747 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004748 /* Hard assertion to be sure that no application data
4749 * is in flight, as corrupting ssl->in_msglen during
4750 * ssl->in_offt != NULL is fatal. */
4751 if( ssl->in_offt != NULL )
4752 {
4753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4754 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4755 }
4756
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004757 /*
4758 * Get next Handshake message in the current record
4759 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004760
Hanno Becker4a810fb2017-05-24 16:27:30 +01004761 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004762 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004763 * current handshake content: If DTLS handshake
4764 * fragmentation is used, that's the fragment
4765 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004766 * size here is faulty and should be changed at
4767 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004768 * (2) While it doesn't seem to cause problems, one
4769 * has to be very careful not to assume that in_hslen
4770 * is always <= in_msglen in a sensible communication.
4771 * Again, it's wrong for DTLS handshake fragmentation.
4772 * The following check is therefore mandatory, and
4773 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004774 * Additionally, ssl->in_hslen might be arbitrarily out of
4775 * bounds after handling a DTLS message with an unexpected
4776 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004777 */
4778 if( ssl->in_hslen < ssl->in_msglen )
4779 {
4780 ssl->in_msglen -= ssl->in_hslen;
4781 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4782 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004783
Hanno Becker4a810fb2017-05-24 16:27:30 +01004784 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4785 ssl->in_msg, ssl->in_msglen );
4786 }
4787 else
4788 {
4789 ssl->in_msglen = 0;
4790 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004791
Hanno Becker4a810fb2017-05-24 16:27:30 +01004792 ssl->in_hslen = 0;
4793 }
4794 /* Case (4): Application data */
4795 else if( ssl->in_offt != NULL )
4796 {
4797 return( 0 );
4798 }
4799 /* Everything else (CCS & Alerts) */
4800 else
4801 {
4802 ssl->in_msglen = 0;
4803 }
4804
Hanno Becker1097b342018-08-15 14:09:41 +01004805 return( 0 );
4806}
4807
Hanno Beckere74d5562018-08-15 14:26:08 +01004808static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4809{
4810 if( ssl->in_msglen > 0 )
4811 return( 1 );
4812
4813 return( 0 );
4814}
4815
Hanno Becker5f066e72018-08-16 14:56:31 +01004816#if defined(MBEDTLS_SSL_PROTO_DTLS)
4817
4818static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4819{
4820 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4821 if( hs == NULL )
4822 return;
4823
Hanno Becker01315ea2018-08-21 17:22:17 +01004824 if( hs->buffering.future_record.data != NULL )
4825 {
4826 hs->buffering.total_bytes_buffered -=
4827 hs->buffering.future_record.len;
4828
4829 mbedtls_free( hs->buffering.future_record.data );
4830 hs->buffering.future_record.data = NULL;
4831 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004832}
4833
4834static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4835{
4836 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4837 unsigned char * rec;
4838 size_t rec_len;
4839 unsigned rec_epoch;
4840
4841 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4842 return( 0 );
4843
4844 if( hs == NULL )
4845 return( 0 );
4846
Hanno Becker5f066e72018-08-16 14:56:31 +01004847 rec = hs->buffering.future_record.data;
4848 rec_len = hs->buffering.future_record.len;
4849 rec_epoch = hs->buffering.future_record.epoch;
4850
4851 if( rec == NULL )
4852 return( 0 );
4853
Hanno Becker4cb782d2018-08-20 11:19:05 +01004854 /* Only consider loading future records if the
4855 * input buffer is empty. */
4856 if( ssl_another_record_in_datagram( ssl ) == 1 )
4857 return( 0 );
4858
Hanno Becker5f066e72018-08-16 14:56:31 +01004859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4860
4861 if( rec_epoch != ssl->in_epoch )
4862 {
4863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4864 goto exit;
4865 }
4866
4867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4868
4869 /* Double-check that the record is not too large */
4870 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4871 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4872 {
4873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4874 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4875 }
4876
4877 memcpy( ssl->in_hdr, rec, rec_len );
4878 ssl->in_left = rec_len;
4879 ssl->next_record_offset = 0;
4880
4881 ssl_free_buffered_record( ssl );
4882
4883exit:
4884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4885 return( 0 );
4886}
4887
4888static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
4889{
4890 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4891 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01004892 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01004893
4894 /* Don't buffer future records outside handshakes. */
4895 if( hs == NULL )
4896 return( 0 );
4897
4898 /* Only buffer handshake records (we are only interested
4899 * in Finished messages). */
4900 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
4901 return( 0 );
4902
4903 /* Don't buffer more than one future epoch record. */
4904 if( hs->buffering.future_record.data != NULL )
4905 return( 0 );
4906
Hanno Becker01315ea2018-08-21 17:22:17 +01004907 /* Don't buffer record if there's not enough buffering space remaining. */
4908 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4909 hs->buffering.total_bytes_buffered ) )
4910 {
4911 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",
4912 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4913 (unsigned) hs->buffering.total_bytes_buffered ) );
4914 return( 0 );
4915 }
4916
Hanno Becker5f066e72018-08-16 14:56:31 +01004917 /* Buffer record */
4918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4919 ssl->in_epoch + 1 ) );
4920 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
4921 rec_hdr_len + ssl->in_msglen );
4922
4923 /* ssl_parse_record_header() only considers records
4924 * of the next epoch as candidates for buffering. */
4925 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01004926 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004927
4928 hs->buffering.future_record.data =
4929 mbedtls_calloc( 1, hs->buffering.future_record.len );
4930 if( hs->buffering.future_record.data == NULL )
4931 {
4932 /* If we run out of RAM trying to buffer a
4933 * record from the next epoch, just ignore. */
4934 return( 0 );
4935 }
4936
Hanno Becker01315ea2018-08-21 17:22:17 +01004937 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01004938
Hanno Becker01315ea2018-08-21 17:22:17 +01004939 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004940 return( 0 );
4941}
4942
4943#endif /* MBEDTLS_SSL_PROTO_DTLS */
4944
Hanno Beckere74d5562018-08-15 14:26:08 +01004945static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004946{
4947 int ret;
4948
Hanno Becker5f066e72018-08-16 14:56:31 +01004949#if defined(MBEDTLS_SSL_PROTO_DTLS)
4950 /* We might have buffered a future record; if so,
4951 * and if the epoch matches now, load it.
4952 * On success, this call will set ssl->in_left to
4953 * the length of the buffered record, so that
4954 * the calls to ssl_fetch_input() below will
4955 * essentially be no-ops. */
4956 ret = ssl_load_buffered_record( ssl );
4957 if( ret != 0 )
4958 return( ret );
4959#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004961 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004963 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004964 return( ret );
4965 }
4966
4967 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004969#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004970 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4971 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004972 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004973 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4974 {
4975 ret = ssl_buffer_future_record( ssl );
4976 if( ret != 0 )
4977 return( ret );
4978
4979 /* Fall through to handling of unexpected records */
4980 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4981 }
4982
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004983 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4984 {
4985 /* Skip unexpected record (but not whole datagram) */
4986 ssl->next_record_offset = ssl->in_msglen
4987 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004988
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4990 "(header)" ) );
4991 }
4992 else
4993 {
4994 /* Skip invalid record and the rest of the datagram */
4995 ssl->next_record_offset = 0;
4996 ssl->in_left = 0;
4997
4998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4999 "(header)" ) );
5000 }
5001
5002 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005003 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005004 }
5005#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005006 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005007 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005008
5009 /*
5010 * Read and optionally decrypt the message contents
5011 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005012 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5013 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005015 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005016 return( ret );
5017 }
5018
5019 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005020#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005021 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005023 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005024 if( ssl->next_record_offset < ssl->in_left )
5025 {
5026 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5027 }
5028 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005029 else
5030#endif
5031 ssl->in_left = 0;
5032
5033 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005035#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005036 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005037 {
5038 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005039 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5040 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005041 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005042 /* Except when waiting for Finished as a bad mac here
5043 * probably means something went wrong in the handshake
5044 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5045 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5046 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5047 {
5048#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5049 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5050 {
5051 mbedtls_ssl_send_alert_message( ssl,
5052 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5053 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5054 }
5055#endif
5056 return( ret );
5057 }
5058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005059#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005060 if( ssl->conf->badmac_limit != 0 &&
5061 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5064 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005065 }
5066#endif
5067
Hanno Becker4a810fb2017-05-24 16:27:30 +01005068 /* As above, invalid records cause
5069 * dismissal of the whole datagram. */
5070
5071 ssl->next_record_offset = 0;
5072 ssl->in_left = 0;
5073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005075 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005076 }
5077
5078 return( ret );
5079 }
5080 else
5081#endif
5082 {
5083 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5085 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005087 mbedtls_ssl_send_alert_message( ssl,
5088 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5089 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005090 }
5091#endif
5092 return( ret );
5093 }
5094 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005095
Simon Butcher99000142016-10-13 17:21:01 +01005096 return( 0 );
5097}
5098
5099int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5100{
5101 int ret;
5102
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005103 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005104 * Handle particular types of records
5105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005106 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005107 {
Simon Butcher99000142016-10-13 17:21:01 +01005108 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5109 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005110 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005111 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005112 }
5113
Hanno Beckere678eaa2018-08-21 14:57:46 +01005114 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005115 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005116 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005117 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5119 ssl->in_msglen ) );
5120 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005121 }
5122
Hanno Beckere678eaa2018-08-21 14:57:46 +01005123 if( ssl->in_msg[0] != 1 )
5124 {
5125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5126 ssl->in_msg[0] ) );
5127 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5128 }
5129
5130#if defined(MBEDTLS_SSL_PROTO_DTLS)
5131 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5132 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5133 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5134 {
5135 if( ssl->handshake == NULL )
5136 {
5137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5138 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5139 }
5140
5141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5142 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5143 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005144#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005145 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005147 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005148 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005149 if( ssl->in_msglen != 2 )
5150 {
5151 /* Note: Standard allows for more than one 2 byte alert
5152 to be packed in a single message, but Mbed TLS doesn't
5153 currently support this. */
5154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5155 ssl->in_msglen ) );
5156 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5157 }
5158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005160 ssl->in_msg[0], ssl->in_msg[1] ) );
5161
5162 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005163 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005165 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005168 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005169 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005170 }
5171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005172 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5173 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5176 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005177 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005178
5179#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5180 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5181 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5182 {
Hanno Becker90333da2017-10-10 11:27:13 +01005183 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005184 /* Will be handled when trying to parse ServerHello */
5185 return( 0 );
5186 }
5187#endif
5188
5189#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5190 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5191 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5192 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5193 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5194 {
5195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5196 /* Will be handled in mbedtls_ssl_parse_certificate() */
5197 return( 0 );
5198 }
5199#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5200
5201 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005202 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005203 }
5204
Hanno Beckerc76c6192017-06-06 10:03:17 +01005205#if defined(MBEDTLS_SSL_PROTO_DTLS)
5206 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5207 ssl->handshake != NULL &&
5208 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5209 {
5210 ssl_handshake_wrapup_free_hs_transform( ssl );
5211 }
5212#endif
5213
Paul Bakker5121ce52009-01-03 21:22:43 +00005214 return( 0 );
5215}
5216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005217int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005218{
5219 int ret;
5220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005221 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5222 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5223 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005224 {
5225 return( ret );
5226 }
5227
5228 return( 0 );
5229}
5230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005231int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005232 unsigned char level,
5233 unsigned char message )
5234{
5235 int ret;
5236
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005237 if( ssl == NULL || ssl->conf == NULL )
5238 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005241 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005243 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005244 ssl->out_msglen = 2;
5245 ssl->out_msg[0] = level;
5246 ssl->out_msg[1] = message;
5247
Hanno Becker67bc7c32018-08-06 11:33:50 +01005248 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005250 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005251 return( ret );
5252 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005254
5255 return( 0 );
5256}
5257
Paul Bakker5121ce52009-01-03 21:22:43 +00005258/*
5259 * Handshake functions
5260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005261#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5262 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5263 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5264 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5265 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5266 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5267 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005268/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005269int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005270{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005271 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005275 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5276 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005277 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5278 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005281 ssl->state++;
5282 return( 0 );
5283 }
5284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5286 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005287}
5288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005289int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005290{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005291 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005295 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5296 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005297 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5298 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005301 ssl->state++;
5302 return( 0 );
5303 }
5304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5306 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005307}
Gilles Peskinef9828522017-05-03 12:28:43 +02005308
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005309#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005310/* Some certificate support -> implement write and parse */
5311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005312int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005313{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005314 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005315 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005316 const mbedtls_x509_crt *crt;
5317 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005321 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5322 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005323 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5324 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005327 ssl->state++;
5328 return( 0 );
5329 }
5330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005331#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005332 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005333 {
5334 if( ssl->client_auth == 0 )
5335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005337 ssl->state++;
5338 return( 0 );
5339 }
5340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005341#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005342 /*
5343 * If using SSLv3 and got no cert, send an Alert message
5344 * (otherwise an empty Certificate message will be sent).
5345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005346 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5347 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005348 {
5349 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005350 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5351 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5352 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005355 goto write_msg;
5356 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005357#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005358 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005359#endif /* MBEDTLS_SSL_CLI_C */
5360#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005361 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005363 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5366 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005367 }
5368 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005369#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005371 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005372
5373 /*
5374 * 0 . 0 handshake type
5375 * 1 . 3 handshake length
5376 * 4 . 6 length of all certs
5377 * 7 . 9 length of cert. 1
5378 * 10 . n-1 peer certificate
5379 * n . n+2 length of cert. 2
5380 * n+3 . ... upper level cert, etc.
5381 */
5382 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005383 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005384
Paul Bakker29087132010-03-21 21:03:34 +00005385 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005386 {
5387 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005388 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005391 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005393 }
5394
5395 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5396 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5397 ssl->out_msg[i + 2] = (unsigned char)( n );
5398
5399 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5400 i += n; crt = crt->next;
5401 }
5402
5403 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5404 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5405 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5406
5407 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005408 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5409 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005410
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005411#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005412write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005413#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005414
5415 ssl->state++;
5416
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005417 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005418 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005419 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005420 return( ret );
5421 }
5422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005424
Paul Bakkered27a042013-04-18 22:46:23 +02005425 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005426}
5427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005428int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005429{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005430 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00005431 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005432 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005433 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005434 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005438 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5439 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005440 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5441 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005444 ssl->state++;
5445 return( 0 );
5446 }
5447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005448#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005449 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005450 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5451 {
5452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5453 ssl->state++;
5454 return( 0 );
5455 }
5456
5457#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5458 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
5459 authmode = ssl->handshake->sni_authmode;
5460#endif
5461
5462 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5463 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005464 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005465 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005467 ssl->state++;
5468 return( 0 );
5469 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005470#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005471
Hanno Becker327c93b2018-08-15 13:56:18 +01005472 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005473 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005474 /* mbedtls_ssl_read_record may have sent an alert already. We
5475 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005476 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005477 return( ret );
5478 }
5479
5480 ssl->state++;
5481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482#if defined(MBEDTLS_SSL_SRV_C)
5483#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005484 /*
5485 * Check if the client sent an empty certificate
5486 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005487 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005488 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005489 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005490 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005491 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5492 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5493 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005496
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005497 /* The client was asked for a certificate but didn't send
5498 one. The client should know what's going on, so we
5499 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005500 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005501 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005502 return( 0 );
5503 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005504 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005505 }
5506 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005507#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005509#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5510 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005511 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005512 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005514 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5515 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5516 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5517 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005520
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005521 /* The client was asked for a certificate but didn't send
5522 one. The client should know what's going on, so we
5523 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005524 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005525 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005526 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005527 else
5528 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005529 }
5530 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005531#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5532 MBEDTLS_SSL_PROTO_TLS1_2 */
5533#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005535 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005538 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5539 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005540 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005541 }
5542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005543 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5544 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005547 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5548 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005549 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005550 }
5551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005552 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005553
Paul Bakker5121ce52009-01-03 21:22:43 +00005554 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005555 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005556 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005557 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005558
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005559 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005560 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005563 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5564 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005565 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005566 }
5567
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005568 /* In case we tried to reuse a session but it failed */
5569 if( ssl->session_negotiate->peer_cert != NULL )
5570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005571 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5572 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005573 }
5574
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005575 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005576 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005577 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005579 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005580 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5581 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005582 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005583 }
5584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005586
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005587 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005588
5589 while( i < ssl->in_hslen )
5590 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005591 if ( i + 3 > ssl->in_hslen ) {
5592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5593 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5594 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5595 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5596 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005597 if( ssl->in_msg[i] != 0 )
5598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005600 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5601 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005603 }
5604
5605 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5606 | (unsigned int) ssl->in_msg[i + 2];
5607 i += 3;
5608
5609 if( n < 128 || i + n > ssl->in_hslen )
5610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005612 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5613 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005615 }
5616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005617 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005618 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005619 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005620 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005621 case 0: /*ok*/
5622 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5623 /* Ignore certificate with an unknown algorithm: maybe a
5624 prior certificate was already trusted. */
5625 break;
5626
5627 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5628 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5629 goto crt_parse_der_failed;
5630
5631 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5632 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5633 goto crt_parse_der_failed;
5634
5635 default:
5636 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5637 crt_parse_der_failed:
5638 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005639 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005640 return( ret );
5641 }
5642
5643 i += n;
5644 }
5645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005646 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005647
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005648 /*
5649 * On client, make sure the server cert doesn't change during renego to
5650 * avoid "triple handshake" attack: https://secure-resumption.com/
5651 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005652#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005653 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005654 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005655 {
5656 if( ssl->session->peer_cert == NULL )
5657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005659 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5660 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005662 }
5663
5664 if( ssl->session->peer_cert->raw.len !=
5665 ssl->session_negotiate->peer_cert->raw.len ||
5666 memcmp( ssl->session->peer_cert->raw.p,
5667 ssl->session_negotiate->peer_cert->raw.p,
5668 ssl->session->peer_cert->raw.len ) != 0 )
5669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005671 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5672 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005673 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005674 }
5675 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005676#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005677
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005678 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005679 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005680 mbedtls_x509_crt *ca_chain;
5681 mbedtls_x509_crl *ca_crl;
5682
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005683#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005684 if( ssl->handshake->sni_ca_chain != NULL )
5685 {
5686 ca_chain = ssl->handshake->sni_ca_chain;
5687 ca_crl = ssl->handshake->sni_ca_crl;
5688 }
5689 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005690#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005691 {
5692 ca_chain = ssl->conf->ca_chain;
5693 ca_crl = ssl->conf->ca_crl;
5694 }
5695
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005696 /*
5697 * Main check: verify certificate
5698 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005699 ret = mbedtls_x509_crt_verify_with_profile(
5700 ssl->session_negotiate->peer_cert,
5701 ca_chain, ca_crl,
5702 ssl->conf->cert_profile,
5703 ssl->hostname,
5704 &ssl->session_negotiate->verify_result,
5705 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00005706
5707 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005709 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005710 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005711
5712 /*
5713 * Secondary checks: always done, but change 'ret' only if it was 0
5714 */
5715
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005716#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005718 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005719
5720 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005722 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005723 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005724 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005727 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005729 }
5730 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005731#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005733 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005734 ciphersuite_info,
5735 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005736 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005739 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005740 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005741 }
5742
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005743 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5744 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5745 * with details encoded in the verification flags. All other kinds
5746 * of error codes, including those from the user provided f_vrfy
5747 * functions, are treated as fatal and lead to a failure of
5748 * ssl_parse_certificate even if verification was optional. */
5749 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5750 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5751 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5752 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005753 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005754 }
5755
5756 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5757 {
5758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5759 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5760 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005761
5762 if( ret != 0 )
5763 {
5764 /* The certificate may have been rejected for several reasons.
5765 Pick one and send the corresponding alert. Which alert to send
5766 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005767 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5768 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5769 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5770 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5771 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5772 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5773 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5774 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5775 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5776 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5777 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5778 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5779 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5780 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5781 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5782 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5783 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5784 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5785 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5786 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005787 else
5788 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005789 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5790 alert );
5791 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005792
Hanno Beckere6706e62017-05-15 16:05:15 +01005793#if defined(MBEDTLS_DEBUG_C)
5794 if( ssl->session_negotiate->verify_result != 0 )
5795 {
5796 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5797 ssl->session_negotiate->verify_result ) );
5798 }
5799 else
5800 {
5801 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5802 }
5803#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005804 }
5805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005807
5808 return( ret );
5809}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005810#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5811 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5812 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5813 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5814 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5815 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5816 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005818int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005819{
5820 int ret;
5821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005824 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005825 ssl->out_msglen = 1;
5826 ssl->out_msg[0] = 1;
5827
Paul Bakker5121ce52009-01-03 21:22:43 +00005828 ssl->state++;
5829
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005830 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005831 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005832 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005833 return( ret );
5834 }
5835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005837
5838 return( 0 );
5839}
5840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005841int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005842{
5843 int ret;
5844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005846
Hanno Becker327c93b2018-08-15 13:56:18 +01005847 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005849 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005850 return( ret );
5851 }
5852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005853 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005856 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5857 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005858 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005859 }
5860
Hanno Beckere678eaa2018-08-21 14:57:46 +01005861 /* CCS records are only accepted if they have length 1 and content '1',
5862 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005863
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005864 /*
5865 * Switch to our negotiated transform and session parameters for inbound
5866 * data.
5867 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005869 ssl->transform_in = ssl->transform_negotiate;
5870 ssl->session_in = ssl->session_negotiate;
5871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005873 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005875#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005876 ssl_dtls_replay_reset( ssl );
5877#endif
5878
5879 /* Increment epoch */
5880 if( ++ssl->in_epoch == 0 )
5881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005883 /* This is highly unlikely to happen for legitimate reasons, so
5884 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005885 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005886 }
5887 }
5888 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005889#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005890 memset( ssl->in_ctr, 0, 8 );
5891
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005892 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005894#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5895 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005897 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005900 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5901 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005902 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005903 }
5904 }
5905#endif
5906
Paul Bakker5121ce52009-01-03 21:22:43 +00005907 ssl->state++;
5908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005910
5911 return( 0 );
5912}
5913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005914void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5915 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005916{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005917 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005919#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5920 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5921 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005922 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005923 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005924#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005925#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5926#if defined(MBEDTLS_SHA512_C)
5927 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005928 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5929 else
5930#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005931#if defined(MBEDTLS_SHA256_C)
5932 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005933 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005934 else
5935#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005936#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005939 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005940 }
Paul Bakker380da532012-04-18 16:10:25 +00005941}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005943void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005944{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005945#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5946 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005947 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5948 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005949#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005950#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5951#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005952 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005953#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005954#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005955 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005956#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005957#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005958}
5959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005960static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005961 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005962{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005963#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5964 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005965 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5966 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005967#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005968#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5969#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005970 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005971#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005972#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005973 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005974#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005975#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005976}
5977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005978#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5979 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5980static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005981 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005982{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005983 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5984 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005985}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005986#endif
Paul Bakker380da532012-04-18 16:10:25 +00005987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5989#if defined(MBEDTLS_SHA256_C)
5990static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005991 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005992{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005993 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005994}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005995#endif
Paul Bakker380da532012-04-18 16:10:25 +00005996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005997#if defined(MBEDTLS_SHA512_C)
5998static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005999 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006000{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006001 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006002}
Paul Bakker769075d2012-11-24 11:26:46 +01006003#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006004#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006006#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006007static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006009{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006010 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006011 mbedtls_md5_context md5;
6012 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006013
Paul Bakker5121ce52009-01-03 21:22:43 +00006014 unsigned char padbuf[48];
6015 unsigned char md5sum[16];
6016 unsigned char sha1sum[20];
6017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006018 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006019 if( !session )
6020 session = ssl->session;
6021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006023
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006024 mbedtls_md5_init( &md5 );
6025 mbedtls_sha1_init( &sha1 );
6026
6027 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6028 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006029
6030 /*
6031 * SSLv3:
6032 * hash =
6033 * MD5( master + pad2 +
6034 * MD5( handshake + sender + master + pad1 ) )
6035 * + SHA1( master + pad2 +
6036 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006037 */
6038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006040 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6041 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006044#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006045 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6046 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006047#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006049 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006050 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006051
Paul Bakker1ef83d62012-04-11 12:09:53 +00006052 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006053
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006054 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6055 mbedtls_md5_update_ret( &md5, session->master, 48 );
6056 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6057 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006058
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006059 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6060 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6061 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6062 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006063
Paul Bakker1ef83d62012-04-11 12:09:53 +00006064 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006065
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006066 mbedtls_md5_starts_ret( &md5 );
6067 mbedtls_md5_update_ret( &md5, session->master, 48 );
6068 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6069 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6070 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006071
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006072 mbedtls_sha1_starts_ret( &sha1 );
6073 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6074 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6075 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6076 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006079
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006080 mbedtls_md5_free( &md5 );
6081 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006082
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006083 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6084 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6085 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006087 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006088}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006089#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006091#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006092static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006093 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006094{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006095 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006096 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006097 mbedtls_md5_context md5;
6098 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006099 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006101 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006102 if( !session )
6103 session = ssl->session;
6104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006106
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006107 mbedtls_md5_init( &md5 );
6108 mbedtls_sha1_init( &sha1 );
6109
6110 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6111 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006112
Paul Bakker1ef83d62012-04-11 12:09:53 +00006113 /*
6114 * TLSv1:
6115 * hash = PRF( master, finished_label,
6116 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6117 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006119#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006120 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6121 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006122#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006124#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006125 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6126 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006127#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006129 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006130 ? "client finished"
6131 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006132
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006133 mbedtls_md5_finish_ret( &md5, padbuf );
6134 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006135
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006136 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006137 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006140
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006141 mbedtls_md5_free( &md5 );
6142 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006143
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006144 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006147}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006148#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006150#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6151#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006152static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006153 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006154{
6155 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006156 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006157 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006158 unsigned char padbuf[32];
6159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006160 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006161 if( !session )
6162 session = ssl->session;
6163
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006164 mbedtls_sha256_init( &sha256 );
6165
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006167
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006168 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006169
6170 /*
6171 * TLSv1.2:
6172 * hash = PRF( master, finished_label,
6173 * Hash( handshake ) )[0.11]
6174 */
6175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006176#if !defined(MBEDTLS_SHA256_ALT)
6177 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006178 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006179#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006181 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006182 ? "client finished"
6183 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006184
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006185 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006186
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006187 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006188 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006190 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006191
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006192 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006193
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006194 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006197}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006198#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006200#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006201static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006202 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006203{
6204 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006205 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006206 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006207 unsigned char padbuf[48];
6208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006210 if( !session )
6211 session = ssl->session;
6212
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006213 mbedtls_sha512_init( &sha512 );
6214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006216
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006217 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006218
6219 /*
6220 * TLSv1.2:
6221 * hash = PRF( master, finished_label,
6222 * Hash( handshake ) )[0.11]
6223 */
6224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006225#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006226 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6227 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006228#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006231 ? "client finished"
6232 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006233
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006234 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006235
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006236 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006237 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006240
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006241 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006242
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006243 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006246}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006247#endif /* MBEDTLS_SHA512_C */
6248#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006250static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006251{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006252 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006253
6254 /*
6255 * Free our handshake params
6256 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006257 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006258 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006259 ssl->handshake = NULL;
6260
6261 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006262 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006263 */
6264 if( ssl->transform )
6265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006266 mbedtls_ssl_transform_free( ssl->transform );
6267 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006268 }
6269 ssl->transform = ssl->transform_negotiate;
6270 ssl->transform_negotiate = NULL;
6271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006272 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006273}
6274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006275void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006276{
6277 int resume = ssl->handshake->resume;
6278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006279 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006281#if defined(MBEDTLS_SSL_RENEGOTIATION)
6282 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006284 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006285 ssl->renego_records_seen = 0;
6286 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006287#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006288
6289 /*
6290 * Free the previous session and switch in the current one
6291 */
Paul Bakker0a597072012-09-25 21:55:46 +00006292 if( ssl->session )
6293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006294#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006295 /* RFC 7366 3.1: keep the EtM state */
6296 ssl->session_negotiate->encrypt_then_mac =
6297 ssl->session->encrypt_then_mac;
6298#endif
6299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006300 mbedtls_ssl_session_free( ssl->session );
6301 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006302 }
6303 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006304 ssl->session_negotiate = NULL;
6305
Paul Bakker0a597072012-09-25 21:55:46 +00006306 /*
6307 * Add cache entry
6308 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006309 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006310 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006311 resume == 0 )
6312 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006313 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006315 }
Paul Bakker0a597072012-09-25 21:55:46 +00006316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006317#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006318 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006319 ssl->handshake->flight != NULL )
6320 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006321 /* Cancel handshake timer */
6322 ssl_set_timer( ssl, 0 );
6323
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006324 /* Keep last flight around in case we need to resend it:
6325 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006326 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006327 }
6328 else
6329#endif
6330 ssl_handshake_wrapup_free_hs_transform( ssl );
6331
Paul Bakker48916f92012-09-16 19:57:18 +00006332 ssl->state++;
6333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006334 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006335}
6336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006337int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006338{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006339 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006342
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006343 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006344
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006345 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006346
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006347 /*
6348 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6349 * may define some other value. Currently (early 2016), no defined
6350 * ciphersuite does this (and this is unlikely to change as activity has
6351 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006353 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006355#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006356 ssl->verify_data_len = hash_len;
6357 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006358#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006359
Paul Bakker5121ce52009-01-03 21:22:43 +00006360 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006361 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6362 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006363
6364 /*
6365 * In case of session resuming, invert the client and server
6366 * ChangeCipherSpec messages order.
6367 */
Paul Bakker0a597072012-09-25 21:55:46 +00006368 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006370#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006371 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006372 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006373#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006374#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006375 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006376 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006377#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006378 }
6379 else
6380 ssl->state++;
6381
Paul Bakker48916f92012-09-16 19:57:18 +00006382 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006383 * Switch to our negotiated transform and session parameters for outbound
6384 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006386 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006388#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006389 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006390 {
6391 unsigned char i;
6392
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006393 /* Remember current epoch settings for resending */
6394 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006395 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006396
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006397 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006398 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006399
6400 /* Increment epoch */
6401 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006402 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006403 break;
6404
6405 /* The loop goes to its end iff the counter is wrapping */
6406 if( i == 0 )
6407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6409 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006410 }
6411 }
6412 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006413#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006414 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006415
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006416 ssl->transform_out = ssl->transform_negotiate;
6417 ssl->session_out = ssl->session_negotiate;
6418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006419#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6420 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006422 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006424 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6425 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006426 }
6427 }
6428#endif
6429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006430#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006431 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006432 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006433#endif
6434
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006435 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006436 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006437 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006438 return( ret );
6439 }
6440
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006441#if defined(MBEDTLS_SSL_PROTO_DTLS)
6442 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6443 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6444 {
6445 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6446 return( ret );
6447 }
6448#endif
6449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006451
6452 return( 0 );
6453}
6454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006455#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006456#define SSL_MAX_HASH_LEN 36
6457#else
6458#define SSL_MAX_HASH_LEN 12
6459#endif
6460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006461int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006462{
Paul Bakker23986e52011-04-24 08:57:21 +00006463 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006464 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006465 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006468
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006469 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006470
Hanno Becker327c93b2018-08-15 13:56:18 +01006471 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006473 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006474 return( ret );
6475 }
6476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006477 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006480 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6481 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006482 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006483 }
6484
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006485 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006486#if defined(MBEDTLS_SSL_PROTO_SSL3)
6487 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006488 hash_len = 36;
6489 else
6490#endif
6491 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6494 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006497 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6498 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006499 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006500 }
6501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006502 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006503 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006506 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6507 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006508 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006509 }
6510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006512 ssl->verify_data_len = hash_len;
6513 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006514#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006515
Paul Bakker0a597072012-09-25 21:55:46 +00006516 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006518#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006519 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006520 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006521#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006522#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006523 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006524 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006525#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006526 }
6527 else
6528 ssl->state++;
6529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006530#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006531 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006532 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006533#endif
6534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006536
6537 return( 0 );
6538}
6539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006540static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006541{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006542 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006544#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6545 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6546 mbedtls_md5_init( &handshake->fin_md5 );
6547 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006548 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6549 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006550#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006551#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6552#if defined(MBEDTLS_SHA256_C)
6553 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006554 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006555#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006556#if defined(MBEDTLS_SHA512_C)
6557 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006558 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006559#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006560#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006561
6562 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006563
6564#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6565 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6566 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6567#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006569#if defined(MBEDTLS_DHM_C)
6570 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006571#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006572#if defined(MBEDTLS_ECDH_C)
6573 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006574#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006575#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006576 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006577#if defined(MBEDTLS_SSL_CLI_C)
6578 handshake->ecjpake_cache = NULL;
6579 handshake->ecjpake_cache_len = 0;
6580#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006581#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006582
6583#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6584 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6585#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006586}
6587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006588static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006589{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006590 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006592 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6593 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006595 mbedtls_md_init( &transform->md_ctx_enc );
6596 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006597}
6598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006599void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006600{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006601 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006602}
6603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006604static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006605{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006606 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006607 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006608 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006609 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006610 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006611 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006612 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006613
6614 /*
6615 * Either the pointers are now NULL or cleared properly and can be freed.
6616 * Now allocate missing structures.
6617 */
6618 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006619 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006620 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006621 }
Paul Bakker48916f92012-09-16 19:57:18 +00006622
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006623 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006624 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006625 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006626 }
Paul Bakker48916f92012-09-16 19:57:18 +00006627
Paul Bakker82788fb2014-10-20 13:59:19 +02006628 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006629 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006630 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006631 }
Paul Bakker48916f92012-09-16 19:57:18 +00006632
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006633 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006634 if( ssl->handshake == NULL ||
6635 ssl->transform_negotiate == NULL ||
6636 ssl->session_negotiate == NULL )
6637 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006640 mbedtls_free( ssl->handshake );
6641 mbedtls_free( ssl->transform_negotiate );
6642 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006643
6644 ssl->handshake = NULL;
6645 ssl->transform_negotiate = NULL;
6646 ssl->session_negotiate = NULL;
6647
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006648 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006649 }
6650
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006651 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006653 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006654 ssl_handshake_params_init( ssl->handshake );
6655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006656#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006657 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6658 {
6659 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006660
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006661 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6662 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6663 else
6664 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006665
6666 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006667 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006668#endif
6669
Paul Bakker48916f92012-09-16 19:57:18 +00006670 return( 0 );
6671}
6672
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006673#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006674/* Dummy cookie callbacks for defaults */
6675static int ssl_cookie_write_dummy( void *ctx,
6676 unsigned char **p, unsigned char *end,
6677 const unsigned char *cli_id, size_t cli_id_len )
6678{
6679 ((void) ctx);
6680 ((void) p);
6681 ((void) end);
6682 ((void) cli_id);
6683 ((void) cli_id_len);
6684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006685 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006686}
6687
6688static int ssl_cookie_check_dummy( void *ctx,
6689 const unsigned char *cookie, size_t cookie_len,
6690 const unsigned char *cli_id, size_t cli_id_len )
6691{
6692 ((void) ctx);
6693 ((void) cookie);
6694 ((void) cookie_len);
6695 ((void) cli_id);
6696 ((void) cli_id_len);
6697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006698 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006699}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006700#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006701
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006702/* Once ssl->out_hdr as the address of the beginning of the
6703 * next outgoing record is set, deduce the other pointers.
6704 *
6705 * Note: For TLS, we save the implicit record sequence number
6706 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6707 * and the caller has to make sure there's space for this.
6708 */
6709
6710static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6711 mbedtls_ssl_transform *transform )
6712{
6713#if defined(MBEDTLS_SSL_PROTO_DTLS)
6714 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6715 {
6716 ssl->out_ctr = ssl->out_hdr + 3;
6717 ssl->out_len = ssl->out_hdr + 11;
6718 ssl->out_iv = ssl->out_hdr + 13;
6719 }
6720 else
6721#endif
6722 {
6723 ssl->out_ctr = ssl->out_hdr - 8;
6724 ssl->out_len = ssl->out_hdr + 3;
6725 ssl->out_iv = ssl->out_hdr + 5;
6726 }
6727
6728 /* Adjust out_msg to make space for explicit IV, if used. */
6729 if( transform != NULL &&
6730 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6731 {
6732 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6733 }
6734 else
6735 ssl->out_msg = ssl->out_iv;
6736}
6737
6738/* Once ssl->in_hdr as the address of the beginning of the
6739 * next incoming record is set, deduce the other pointers.
6740 *
6741 * Note: For TLS, we save the implicit record sequence number
6742 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6743 * and the caller has to make sure there's space for this.
6744 */
6745
6746static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6747 mbedtls_ssl_transform *transform )
6748{
6749#if defined(MBEDTLS_SSL_PROTO_DTLS)
6750 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6751 {
6752 ssl->in_ctr = ssl->in_hdr + 3;
6753 ssl->in_len = ssl->in_hdr + 11;
6754 ssl->in_iv = ssl->in_hdr + 13;
6755 }
6756 else
6757#endif
6758 {
6759 ssl->in_ctr = ssl->in_hdr - 8;
6760 ssl->in_len = ssl->in_hdr + 3;
6761 ssl->in_iv = ssl->in_hdr + 5;
6762 }
6763
6764 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6765 if( transform != NULL &&
6766 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6767 {
6768 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6769 }
6770 else
6771 ssl->in_msg = ssl->in_iv;
6772}
6773
Paul Bakker5121ce52009-01-03 21:22:43 +00006774/*
6775 * Initialize an SSL context
6776 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006777void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6778{
6779 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6780}
6781
6782/*
6783 * Setup an SSL context
6784 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006785
6786static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6787{
6788 /* Set the incoming and outgoing record pointers. */
6789#if defined(MBEDTLS_SSL_PROTO_DTLS)
6790 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6791 {
6792 ssl->out_hdr = ssl->out_buf;
6793 ssl->in_hdr = ssl->in_buf;
6794 }
6795 else
6796#endif /* MBEDTLS_SSL_PROTO_DTLS */
6797 {
6798 ssl->out_hdr = ssl->out_buf + 8;
6799 ssl->in_hdr = ssl->in_buf + 8;
6800 }
6801
6802 /* Derive other internal pointers. */
6803 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6804 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6805}
6806
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006807int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006808 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006809{
Paul Bakker48916f92012-09-16 19:57:18 +00006810 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006811
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006812 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006813
6814 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006815 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006816 */
Angus Grattond8213d02016-05-25 20:56:48 +10006817 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6818 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006819 {
Angus Grattond8213d02016-05-25 20:56:48 +10006820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6821 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6822 }
6823
6824 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6825 if( ssl->out_buf == NULL )
6826 {
6827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006829 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006830 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006831 }
6832
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006833 ssl_reset_in_out_pointers( ssl );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006834
Paul Bakker48916f92012-09-16 19:57:18 +00006835 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6836 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006837
6838 return( 0 );
6839}
6840
6841/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006842 * Reset an initialized and used SSL context for re-use while retaining
6843 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006844 *
6845 * If partial is non-zero, keep data in the input buffer and client ID.
6846 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006847 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006848static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006849{
Paul Bakker48916f92012-09-16 19:57:18 +00006850 int ret;
6851
Hanno Becker7e772132018-08-10 12:38:21 +01006852#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6853 !defined(MBEDTLS_SSL_SRV_C)
6854 ((void) partial);
6855#endif
6856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006857 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006858
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006859 /* Cancel any possibly running timer */
6860 ssl_set_timer( ssl, 0 );
6861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006862#if defined(MBEDTLS_SSL_RENEGOTIATION)
6863 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006864 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006865
6866 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006867 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6868 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006869#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006870 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006871
Paul Bakker7eb013f2011-10-06 12:37:39 +00006872 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01006873 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006874
6875 ssl->in_msgtype = 0;
6876 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006877#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006878 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006879 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006880#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006881#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006882 ssl_dtls_replay_reset( ssl );
6883#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006884
6885 ssl->in_hslen = 0;
6886 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006887
6888 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006889
6890 ssl->out_msgtype = 0;
6891 ssl->out_msglen = 0;
6892 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006893#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6894 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006895 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006896#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006897
Hanno Becker19859472018-08-06 09:40:20 +01006898 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6899
Paul Bakker48916f92012-09-16 19:57:18 +00006900 ssl->transform_in = NULL;
6901 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006902
Hanno Becker78640902018-08-13 16:35:15 +01006903 ssl->session_in = NULL;
6904 ssl->session_out = NULL;
6905
Angus Grattond8213d02016-05-25 20:56:48 +10006906 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006907
6908#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006909 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006910#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
6911 {
6912 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10006913 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006914 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006916#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6917 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6920 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006922 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6923 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006924 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006925 }
6926#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006927
Paul Bakker48916f92012-09-16 19:57:18 +00006928 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006930 mbedtls_ssl_transform_free( ssl->transform );
6931 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006932 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006933 }
Paul Bakker48916f92012-09-16 19:57:18 +00006934
Paul Bakkerc0463502013-02-14 11:19:38 +01006935 if( ssl->session )
6936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937 mbedtls_ssl_session_free( ssl->session );
6938 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006939 ssl->session = NULL;
6940 }
6941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006942#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006943 ssl->alpn_chosen = NULL;
6944#endif
6945
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006946#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01006947#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006948 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006949#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006950 {
6951 mbedtls_free( ssl->cli_id );
6952 ssl->cli_id = NULL;
6953 ssl->cli_id_len = 0;
6954 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006955#endif
6956
Paul Bakker48916f92012-09-16 19:57:18 +00006957 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6958 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006959
6960 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006961}
6962
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006963/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006964 * Reset an initialized and used SSL context for re-use while retaining
6965 * all application-set variables, function pointers and data.
6966 */
6967int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6968{
6969 return( ssl_session_reset_int( ssl, 0 ) );
6970}
6971
6972/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006973 * SSL set accessors
6974 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006975void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006976{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006977 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006978}
6979
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006980void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006981{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006982 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006983}
6984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006985#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006986void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006987{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006988 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006989}
6990#endif
6991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006992#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006993void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006994{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006995 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006996}
6997#endif
6998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006999#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007000
Hanno Becker1841b0a2018-08-24 11:13:57 +01007001void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7002 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007003{
7004 ssl->disable_datagram_packing = !allow_packing;
7005}
7006
7007void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7008 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007009{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007010 conf->hs_timeout_min = min;
7011 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007012}
7013#endif
7014
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007015void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007016{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007017 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007018}
7019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007020#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007021void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007022 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007023 void *p_vrfy )
7024{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007025 conf->f_vrfy = f_vrfy;
7026 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007027}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007028#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007029
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007030void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007031 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007032 void *p_rng )
7033{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007034 conf->f_rng = f_rng;
7035 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007036}
7037
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007038void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007039 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007040 void *p_dbg )
7041{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007042 conf->f_dbg = f_dbg;
7043 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007044}
7045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007046void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007047 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007048 mbedtls_ssl_send_t *f_send,
7049 mbedtls_ssl_recv_t *f_recv,
7050 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007051{
7052 ssl->p_bio = p_bio;
7053 ssl->f_send = f_send;
7054 ssl->f_recv = f_recv;
7055 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007056}
7057
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007058#if defined(MBEDTLS_SSL_PROTO_DTLS)
7059void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7060{
7061 ssl->mtu = mtu;
7062}
7063#endif
7064
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007065void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007066{
7067 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007068}
7069
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007070void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7071 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007072 mbedtls_ssl_set_timer_t *f_set_timer,
7073 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007074{
7075 ssl->p_timer = p_timer;
7076 ssl->f_set_timer = f_set_timer;
7077 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007078
7079 /* Make sure we start with no timer running */
7080 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007081}
7082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007083#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007084void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007085 void *p_cache,
7086 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7087 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007088{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007089 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007090 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007091 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007092}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007093#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007095#if defined(MBEDTLS_SSL_CLI_C)
7096int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007097{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007098 int ret;
7099
7100 if( ssl == NULL ||
7101 session == NULL ||
7102 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007103 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007106 }
7107
7108 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7109 return( ret );
7110
Paul Bakker0a597072012-09-25 21:55:46 +00007111 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007112
7113 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007114}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007115#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007116
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007117void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007118 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007119{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007120 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7121 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7122 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7123 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007124}
7125
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007126void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007127 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007128 int major, int minor )
7129{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007131 return;
7132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007134 return;
7135
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007136 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007137}
7138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007140void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007141 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007142{
7143 conf->cert_profile = profile;
7144}
7145
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007146/* Append a new keycert entry to a (possibly empty) list */
7147static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7148 mbedtls_x509_crt *cert,
7149 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007150{
niisato8ee24222018-06-25 19:05:48 +09007151 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007152
niisato8ee24222018-06-25 19:05:48 +09007153 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7154 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007155 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007156
niisato8ee24222018-06-25 19:05:48 +09007157 new_cert->cert = cert;
7158 new_cert->key = key;
7159 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007160
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007161 /* Update head is the list was null, else add to the end */
7162 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007163 {
niisato8ee24222018-06-25 19:05:48 +09007164 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007165 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007166 else
7167 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007168 mbedtls_ssl_key_cert *cur = *head;
7169 while( cur->next != NULL )
7170 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007171 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007172 }
7173
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007174 return( 0 );
7175}
7176
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007177int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007178 mbedtls_x509_crt *own_cert,
7179 mbedtls_pk_context *pk_key )
7180{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007181 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007182}
7183
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007184void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007185 mbedtls_x509_crt *ca_chain,
7186 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007187{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007188 conf->ca_chain = ca_chain;
7189 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007190}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007192
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007193#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7194int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7195 mbedtls_x509_crt *own_cert,
7196 mbedtls_pk_context *pk_key )
7197{
7198 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7199 own_cert, pk_key ) );
7200}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007201
7202void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7203 mbedtls_x509_crt *ca_chain,
7204 mbedtls_x509_crl *ca_crl )
7205{
7206 ssl->handshake->sni_ca_chain = ca_chain;
7207 ssl->handshake->sni_ca_crl = ca_crl;
7208}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007209
7210void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7211 int authmode )
7212{
7213 ssl->handshake->sni_authmode = authmode;
7214}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007215#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7216
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007217#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007218/*
7219 * Set EC J-PAKE password for current handshake
7220 */
7221int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7222 const unsigned char *pw,
7223 size_t pw_len )
7224{
7225 mbedtls_ecjpake_role role;
7226
Janos Follath8eb64132016-06-03 15:40:57 +01007227 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007228 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7229
7230 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7231 role = MBEDTLS_ECJPAKE_SERVER;
7232 else
7233 role = MBEDTLS_ECJPAKE_CLIENT;
7234
7235 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7236 role,
7237 MBEDTLS_MD_SHA256,
7238 MBEDTLS_ECP_DP_SECP256R1,
7239 pw, pw_len ) );
7240}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007241#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007244int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007245 const unsigned char *psk, size_t psk_len,
7246 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007247{
Paul Bakker6db455e2013-09-18 17:29:31 +02007248 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7252 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007253
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007254 /* Identity len will be encoded on two bytes */
7255 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007256 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007257 {
7258 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7259 }
7260
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007261 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007262 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007263 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007264
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007265 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007266 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007267 conf->psk_len = 0;
7268 }
7269 if( conf->psk_identity != NULL )
7270 {
7271 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007272 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007273 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007274 }
7275
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007276 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7277 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007278 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007279 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007280 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007281 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007282 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007283 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007284 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007285
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007286 conf->psk_len = psk_len;
7287 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007288
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007289 memcpy( conf->psk, psk, conf->psk_len );
7290 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007291
7292 return( 0 );
7293}
7294
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007295int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7296 const unsigned char *psk, size_t psk_len )
7297{
7298 if( psk == NULL || ssl->handshake == NULL )
7299 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7300
7301 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7303
7304 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007305 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007306 mbedtls_platform_zeroize( ssl->handshake->psk,
7307 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007308 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007309 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007310 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007311
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007312 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007313 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007314
7315 ssl->handshake->psk_len = psk_len;
7316 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7317
7318 return( 0 );
7319}
7320
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007321void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007322 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007323 size_t),
7324 void *p_psk )
7325{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007326 conf->f_psk = f_psk;
7327 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007328}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007329#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007330
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007331#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007332
7333#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007334int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007335{
7336 int ret;
7337
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007338 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7339 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7340 {
7341 mbedtls_mpi_free( &conf->dhm_P );
7342 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007343 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007344 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007345
7346 return( 0 );
7347}
Hanno Becker470a8c42017-10-04 15:28:46 +01007348#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007349
Hanno Beckera90658f2017-10-04 15:29:08 +01007350int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7351 const unsigned char *dhm_P, size_t P_len,
7352 const unsigned char *dhm_G, size_t G_len )
7353{
7354 int ret;
7355
7356 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7357 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7358 {
7359 mbedtls_mpi_free( &conf->dhm_P );
7360 mbedtls_mpi_free( &conf->dhm_G );
7361 return( ret );
7362 }
7363
7364 return( 0 );
7365}
Paul Bakker5121ce52009-01-03 21:22:43 +00007366
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007367int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007368{
7369 int ret;
7370
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007371 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7372 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7373 {
7374 mbedtls_mpi_free( &conf->dhm_P );
7375 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007376 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007377 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007378
7379 return( 0 );
7380}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007381#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007382
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007383#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7384/*
7385 * Set the minimum length for Diffie-Hellman parameters
7386 */
7387void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7388 unsigned int bitlen )
7389{
7390 conf->dhm_min_bitlen = bitlen;
7391}
7392#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7393
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007394#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007395/*
7396 * Set allowed/preferred hashes for handshake signatures
7397 */
7398void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7399 const int *hashes )
7400{
7401 conf->sig_hashes = hashes;
7402}
Hanno Becker947194e2017-04-07 13:25:49 +01007403#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007404
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007405#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007406/*
7407 * Set the allowed elliptic curves
7408 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007409void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007410 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007411{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007412 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007413}
Hanno Becker947194e2017-04-07 13:25:49 +01007414#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007415
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007416#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007417int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007418{
Hanno Becker947194e2017-04-07 13:25:49 +01007419 /* Initialize to suppress unnecessary compiler warning */
7420 size_t hostname_len = 0;
7421
7422 /* Check if new hostname is valid before
7423 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007424 if( hostname != NULL )
7425 {
7426 hostname_len = strlen( hostname );
7427
7428 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7429 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7430 }
7431
7432 /* Now it's clear that we will overwrite the old hostname,
7433 * so we can free it safely */
7434
7435 if( ssl->hostname != NULL )
7436 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007437 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007438 mbedtls_free( ssl->hostname );
7439 }
7440
7441 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007442
Paul Bakker5121ce52009-01-03 21:22:43 +00007443 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007444 {
7445 ssl->hostname = NULL;
7446 }
7447 else
7448 {
7449 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007450 if( ssl->hostname == NULL )
7451 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007452
Hanno Becker947194e2017-04-07 13:25:49 +01007453 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007454
Hanno Becker947194e2017-04-07 13:25:49 +01007455 ssl->hostname[hostname_len] = '\0';
7456 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007457
7458 return( 0 );
7459}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007460#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007461
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007462#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007463void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007465 const unsigned char *, size_t),
7466 void *p_sni )
7467{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007468 conf->f_sni = f_sni;
7469 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007470}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007471#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007473#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007474int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007475{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007476 size_t cur_len, tot_len;
7477 const char **p;
7478
7479 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007480 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7481 * MUST NOT be truncated."
7482 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007483 */
7484 tot_len = 0;
7485 for( p = protos; *p != NULL; p++ )
7486 {
7487 cur_len = strlen( *p );
7488 tot_len += cur_len;
7489
7490 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007492 }
7493
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007494 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007495
7496 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007497}
7498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007499const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007500{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007501 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007502}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007503#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007504
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007505void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007506{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007507 conf->max_major_ver = major;
7508 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007509}
7510
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007511void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007512{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007513 conf->min_major_ver = major;
7514 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007515}
7516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007518void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007519{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007520 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007521}
7522#endif
7523
Janos Follath088ce432017-04-10 12:42:31 +01007524#if defined(MBEDTLS_SSL_SRV_C)
7525void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7526 char cert_req_ca_list )
7527{
7528 conf->cert_req_ca_list = cert_req_ca_list;
7529}
7530#endif
7531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007533void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007534{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007535 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007536}
7537#endif
7538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007539#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007540void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007541{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007542 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007543}
7544#endif
7545
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007546#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007547void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007548{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007549 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007550}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007551#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007554int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007555{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007556 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007557 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007559 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007560 }
7561
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007562 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007563
7564 return( 0 );
7565}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007566#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007568#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007569void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007570{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007571 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007572}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007573#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007575#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007576void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007577{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007578 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007579}
7580#endif
7581
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007582void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007583{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007584 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007585}
7586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007587#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007588void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007589{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007590 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007591}
7592
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007593void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007594{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007595 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007596}
7597
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007598void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007599 const unsigned char period[8] )
7600{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007601 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007602}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007606#if defined(MBEDTLS_SSL_CLI_C)
7607void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007608{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007609 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007610}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007611#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007612
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007613#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007614void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7615 mbedtls_ssl_ticket_write_t *f_ticket_write,
7616 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7617 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007618{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007619 conf->f_ticket_write = f_ticket_write;
7620 conf->f_ticket_parse = f_ticket_parse;
7621 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007622}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007623#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007624#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007625
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007626#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7627void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7628 mbedtls_ssl_export_keys_t *f_export_keys,
7629 void *p_export_keys )
7630{
7631 conf->f_export_keys = f_export_keys;
7632 conf->p_export_keys = p_export_keys;
7633}
7634#endif
7635
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007636#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007637void mbedtls_ssl_conf_async_private_cb(
7638 mbedtls_ssl_config *conf,
7639 mbedtls_ssl_async_sign_t *f_async_sign,
7640 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7641 mbedtls_ssl_async_resume_t *f_async_resume,
7642 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007643 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007644{
7645 conf->f_async_sign_start = f_async_sign;
7646 conf->f_async_decrypt_start = f_async_decrypt;
7647 conf->f_async_resume = f_async_resume;
7648 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007649 conf->p_async_config_data = async_config_data;
7650}
7651
Gilles Peskine8f97af72018-04-26 11:46:10 +02007652void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7653{
7654 return( conf->p_async_config_data );
7655}
7656
Gilles Peskine1febfef2018-04-30 11:54:39 +02007657void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007658{
7659 if( ssl->handshake == NULL )
7660 return( NULL );
7661 else
7662 return( ssl->handshake->user_async_ctx );
7663}
7664
Gilles Peskine1febfef2018-04-30 11:54:39 +02007665void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007666 void *ctx )
7667{
7668 if( ssl->handshake != NULL )
7669 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007670}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007671#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007672
Paul Bakker5121ce52009-01-03 21:22:43 +00007673/*
7674 * SSL get accessors
7675 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007677{
7678 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7679}
7680
Hanno Becker8b170a02017-10-10 11:51:19 +01007681int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7682{
7683 /*
7684 * Case A: We're currently holding back
7685 * a message for further processing.
7686 */
7687
7688 if( ssl->keep_current_message == 1 )
7689 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007690 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007691 return( 1 );
7692 }
7693
7694 /*
7695 * Case B: Further records are pending in the current datagram.
7696 */
7697
7698#if defined(MBEDTLS_SSL_PROTO_DTLS)
7699 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7700 ssl->in_left > ssl->next_record_offset )
7701 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007702 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007703 return( 1 );
7704 }
7705#endif /* MBEDTLS_SSL_PROTO_DTLS */
7706
7707 /*
7708 * Case C: A handshake message is being processed.
7709 */
7710
Hanno Becker8b170a02017-10-10 11:51:19 +01007711 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7712 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007713 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007714 return( 1 );
7715 }
7716
7717 /*
7718 * Case D: An application data message is being processed
7719 */
7720 if( ssl->in_offt != NULL )
7721 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007722 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007723 return( 1 );
7724 }
7725
7726 /*
7727 * In all other cases, the rest of the message can be dropped.
7728 * As in ssl_read_record_layer, this needs to be adapted if
7729 * we implement support for multiple alerts in single records.
7730 */
7731
7732 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7733 return( 0 );
7734}
7735
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007736uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007737{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007738 if( ssl->session != NULL )
7739 return( ssl->session->verify_result );
7740
7741 if( ssl->session_negotiate != NULL )
7742 return( ssl->session_negotiate->verify_result );
7743
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007744 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007745}
7746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007747const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007748{
Paul Bakker926c8e42013-03-06 10:23:34 +01007749 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007750 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007753}
7754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007755const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007756{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007758 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007759 {
7760 switch( ssl->minor_ver )
7761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007763 return( "DTLSv1.0" );
7764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007766 return( "DTLSv1.2" );
7767
7768 default:
7769 return( "unknown (DTLS)" );
7770 }
7771 }
7772#endif
7773
Paul Bakker43ca69c2011-01-15 17:35:19 +00007774 switch( ssl->minor_ver )
7775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007776 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007777 return( "SSLv3.0" );
7778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007779 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007780 return( "TLSv1.0" );
7781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007782 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007783 return( "TLSv1.1" );
7784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007785 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007786 return( "TLSv1.2" );
7787
Paul Bakker43ca69c2011-01-15 17:35:19 +00007788 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007789 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007790 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007791}
7792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007793int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007794{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007795 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007797 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007798
Hanno Becker78640902018-08-13 16:35:15 +01007799 if( transform == NULL )
7800 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802#if defined(MBEDTLS_ZLIB_SUPPORT)
7803 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7804 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007805#endif
7806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007807 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007809 case MBEDTLS_MODE_GCM:
7810 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007811 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007813 transform_expansion = transform->minlen;
7814 break;
7815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007816 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007817
7818 block_size = mbedtls_cipher_get_block_size(
7819 &transform->cipher_ctx_enc );
7820
7821#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7822 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7823 {
7824 /* Expansion due to addition of
7825 * - MAC
7826 * - CBC padding (theoretically up to 256 bytes, but
7827 * we never use more than block_size)
7828 * - explicit IV
7829 */
7830 transform_expansion = transform->maclen + 2 * block_size;
7831 }
7832 else
7833#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7834 {
7835 /* No explicit IV prior to TLS 1.1. */
7836 transform_expansion = transform->maclen + block_size;
7837 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007838 break;
7839
7840 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007843 }
7844
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007845 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007846}
7847
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007848#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7849size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7850{
7851 size_t max_len;
7852
7853 /*
7854 * Assume mfl_code is correct since it was checked when set
7855 */
Angus Grattond8213d02016-05-25 20:56:48 +10007856 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007857
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007858 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007859 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007860 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007861 {
Angus Grattond8213d02016-05-25 20:56:48 +10007862 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007863 }
7864
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007865 /* During a handshake, use the value being negotiated */
7866 if( ssl->session_negotiate != NULL &&
7867 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7868 {
7869 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7870 }
7871
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007872 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007873}
7874#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7875
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007876#if defined(MBEDTLS_SSL_PROTO_DTLS)
7877static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
7878{
7879 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
7880 return( ssl->mtu );
7881
7882 if( ssl->mtu == 0 )
7883 return( ssl->handshake->mtu );
7884
7885 return( ssl->mtu < ssl->handshake->mtu ?
7886 ssl->mtu : ssl->handshake->mtu );
7887}
7888#endif /* MBEDTLS_SSL_PROTO_DTLS */
7889
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007890int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7891{
7892 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7893
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02007894#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7895 !defined(MBEDTLS_SSL_PROTO_DTLS)
7896 (void) ssl;
7897#endif
7898
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007899#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7900 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7901
7902 if( max_len > mfl )
7903 max_len = mfl;
7904#endif
7905
7906#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007907 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007908 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007909 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007910 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7911 const size_t overhead = (size_t) ret;
7912
7913 if( ret < 0 )
7914 return( ret );
7915
7916 if( mtu <= overhead )
7917 {
7918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7919 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7920 }
7921
7922 if( max_len > mtu - overhead )
7923 max_len = mtu - overhead;
7924 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007925#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007926
Hanno Becker0defedb2018-08-10 12:35:02 +01007927#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7928 !defined(MBEDTLS_SSL_PROTO_DTLS)
7929 ((void) ssl);
7930#endif
7931
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007932 return( (int) max_len );
7933}
7934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935#if defined(MBEDTLS_X509_CRT_PARSE_C)
7936const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007937{
7938 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007939 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007940
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007941 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007942}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945#if defined(MBEDTLS_SSL_CLI_C)
7946int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007947{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007948 if( ssl == NULL ||
7949 dst == NULL ||
7950 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007951 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007953 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007954 }
7955
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007956 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007957}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007959
Paul Bakker5121ce52009-01-03 21:22:43 +00007960/*
Paul Bakker1961b702013-01-25 14:49:24 +01007961 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007962 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007964{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007966
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007967 if( ssl == NULL || ssl->conf == NULL )
7968 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007970#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007971 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007972 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007973#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007974#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007975 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007977#endif
7978
Paul Bakker1961b702013-01-25 14:49:24 +01007979 return( ret );
7980}
7981
7982/*
7983 * Perform the SSL handshake
7984 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007985int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007986{
7987 int ret = 0;
7988
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007989 if( ssl == NULL || ssl->conf == NULL )
7990 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007994 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01007997
7998 if( ret != 0 )
7999 break;
8000 }
8001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008003
8004 return( ret );
8005}
8006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008007#if defined(MBEDTLS_SSL_RENEGOTIATION)
8008#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008009/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008010 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008011 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008013{
8014 int ret;
8015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008017
8018 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008019 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8020 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008021
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008022 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008023 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008024 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008025 return( ret );
8026 }
8027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008029
8030 return( 0 );
8031}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008032#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008033
8034/*
8035 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008036 * - any side: calling mbedtls_ssl_renegotiate(),
8037 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8038 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008039 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008040 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008041 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008042 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008043static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008044{
8045 int ret;
8046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008048
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008049 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8050 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008051
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008052 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8053 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008054#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008055 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008056 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008057 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008058 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008059 ssl->handshake->out_msg_seq = 1;
8060 else
8061 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008062 }
8063#endif
8064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008065 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8066 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008068 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008070 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008071 return( ret );
8072 }
8073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008075
8076 return( 0 );
8077}
8078
8079/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008080 * Renegotiate current connection on client,
8081 * or request renegotiation on server
8082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008083int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008084{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008085 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008086
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008087 if( ssl == NULL || ssl->conf == NULL )
8088 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008091 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008092 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008094 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8095 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008097 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008098
8099 /* Did we already try/start sending HelloRequest? */
8100 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008101 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008102
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008103 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008104 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008105#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008107#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008108 /*
8109 * On client, either start the renegotiation process or,
8110 * if already in progress, continue the handshake
8111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008112 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008114 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8115 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008116
8117 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008119 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008120 return( ret );
8121 }
8122 }
8123 else
8124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008125 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008127 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008128 return( ret );
8129 }
8130 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008131#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008132
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008133 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008134}
8135
8136/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008137 * Check record counters and renegotiate if they're above the limit.
8138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008139static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008140{
Andres AG2196c7f2016-12-15 17:01:16 +00008141 size_t ep_len = ssl_ep_len( ssl );
8142 int in_ctr_cmp;
8143 int out_ctr_cmp;
8144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8146 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008147 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008148 {
8149 return( 0 );
8150 }
8151
Andres AG2196c7f2016-12-15 17:01:16 +00008152 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8153 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008154 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008155 ssl->conf->renego_period + ep_len, 8 - ep_len );
8156
8157 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008158 {
8159 return( 0 );
8160 }
8161
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008163 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008164}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008165#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008166
8167/*
8168 * Receive application data decrypted from the SSL layer
8169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008170int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008171{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008172 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008173 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008174
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008175 if( ssl == NULL || ssl->conf == NULL )
8176 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008180#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008181 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008184 return( ret );
8185
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008186 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008187 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008188 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008189 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008190 return( ret );
8191 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008192 }
8193#endif
8194
Hanno Becker4a810fb2017-05-24 16:27:30 +01008195 /*
8196 * Check if renegotiation is necessary and/or handshake is
8197 * in process. If yes, perform/continue, and fall through
8198 * if an unexpected packet is received while the client
8199 * is waiting for the ServerHello.
8200 *
8201 * (There is no equivalent to the last condition on
8202 * the server-side as it is not treated as within
8203 * a handshake while waiting for the ClientHello
8204 * after a renegotiation request.)
8205 */
8206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008207#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008208 ret = ssl_check_ctr_renegotiate( ssl );
8209 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8210 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008212 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008213 return( ret );
8214 }
8215#endif
8216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008217 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008219 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008220 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8221 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008223 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008224 return( ret );
8225 }
8226 }
8227
Hanno Beckere41158b2017-10-23 13:30:32 +01008228 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008229 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008230 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008231 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008232 if( ssl->f_get_timer != NULL &&
8233 ssl->f_get_timer( ssl->p_timer ) == -1 )
8234 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008235 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008236 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008237
Hanno Becker327c93b2018-08-15 13:56:18 +01008238 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008239 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008240 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8241 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008242
Hanno Becker4a810fb2017-05-24 16:27:30 +01008243 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8244 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008245 }
8246
8247 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008248 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008249 {
8250 /*
8251 * OpenSSL sends empty messages to randomize the IV
8252 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008253 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008255 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008256 return( 0 );
8257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008258 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008259 return( ret );
8260 }
8261 }
8262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008263 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008266
Hanno Becker4a810fb2017-05-24 16:27:30 +01008267 /*
8268 * - For client-side, expect SERVER_HELLO_REQUEST.
8269 * - For server-side, expect CLIENT_HELLO.
8270 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8271 */
8272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008273#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008274 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008275 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008276 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008279
8280 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008281#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008282 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008283 {
8284 continue;
8285 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008286#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008287 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008288 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008289#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008290
Hanno Becker4a810fb2017-05-24 16:27:30 +01008291#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008292 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008293 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008296
8297 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008298#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008299 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008300 {
8301 continue;
8302 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008303#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008304 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008305 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008306#endif /* MBEDTLS_SSL_SRV_C */
8307
Hanno Becker21df7f92017-10-17 11:03:26 +01008308#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008309 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008310 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8311 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8312 ssl->conf->allow_legacy_renegotiation ==
8313 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8314 {
8315 /*
8316 * Accept renegotiation request
8317 */
Paul Bakker48916f92012-09-16 19:57:18 +00008318
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008319 /* DTLS clients need to know renego is server-initiated */
8320#if defined(MBEDTLS_SSL_PROTO_DTLS)
8321 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8322 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8323 {
8324 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8325 }
8326#endif
8327 ret = ssl_start_renegotiation( ssl );
8328 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8329 ret != 0 )
8330 {
8331 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8332 return( ret );
8333 }
8334 }
8335 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008336#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008337 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008338 /*
8339 * Refuse renegotiation
8340 */
8341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008342 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008344#if defined(MBEDTLS_SSL_PROTO_SSL3)
8345 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008346 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008347 /* SSLv3 does not have a "no_renegotiation" warning, so
8348 we send a fatal alert and abort the connection. */
8349 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8350 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8351 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008352 }
8353 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008354#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8355#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8356 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8357 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008359 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8360 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8361 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008362 {
8363 return( ret );
8364 }
Paul Bakker48916f92012-09-16 19:57:18 +00008365 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008366 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008367#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8368 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8371 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008372 }
Paul Bakker48916f92012-09-16 19:57:18 +00008373 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008374
Hanno Becker90333da2017-10-10 11:27:13 +01008375 /* At this point, we don't know whether the renegotiation has been
8376 * completed or not. The cases to consider are the following:
8377 * 1) The renegotiation is complete. In this case, no new record
8378 * has been read yet.
8379 * 2) The renegotiation is incomplete because the client received
8380 * an application data record while awaiting the ServerHello.
8381 * 3) The renegotiation is incomplete because the client received
8382 * a non-handshake, non-application data message while awaiting
8383 * the ServerHello.
8384 * In each of these case, looping will be the proper action:
8385 * - For 1), the next iteration will read a new record and check
8386 * if it's application data.
8387 * - For 2), the loop condition isn't satisfied as application data
8388 * is present, hence continue is the same as break
8389 * - For 3), the loop condition is satisfied and read_record
8390 * will re-deliver the message that was held back by the client
8391 * when expecting the ServerHello.
8392 */
8393 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008394 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008395#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008396 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008397 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008398 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008399 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008400 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008403 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008404 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008405 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008406 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008407 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008410 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8411 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008414 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008415 }
8416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008417 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8420 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008421 }
8422
8423 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008424
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008425 /* We're going to return something now, cancel timer,
8426 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008427 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008428 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008429
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008430#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008431 /* If we requested renego but received AppData, resend HelloRequest.
8432 * Do it now, after setting in_offt, to avoid taking this branch
8433 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008434#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008435 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008436 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008437 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008438 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008440 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008441 return( ret );
8442 }
8443 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008444#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008445#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008446 }
8447
8448 n = ( len < ssl->in_msglen )
8449 ? len : ssl->in_msglen;
8450
8451 memcpy( buf, ssl->in_offt, n );
8452 ssl->in_msglen -= n;
8453
8454 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008455 {
8456 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008457 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008458 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008459 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008460 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008461 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008462 /* more data available */
8463 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008464 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008467
Paul Bakker23986e52011-04-24 08:57:21 +00008468 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008469}
8470
8471/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008472 * Send application data to be encrypted by the SSL layer, taking care of max
8473 * fragment length and buffer size.
8474 *
8475 * According to RFC 5246 Section 6.2.1:
8476 *
8477 * Zero-length fragments of Application data MAY be sent as they are
8478 * potentially useful as a traffic analysis countermeasure.
8479 *
8480 * Therefore, it is possible that the input message length is 0 and the
8481 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008482 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008483static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008484 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008485{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008486 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8487 const size_t max_len = (size_t) ret;
8488
8489 if( ret < 0 )
8490 {
8491 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8492 return( ret );
8493 }
8494
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008495 if( len > max_len )
8496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008497#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008498 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008501 "maximum fragment length: %d > %d",
8502 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008503 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008504 }
8505 else
8506#endif
8507 len = max_len;
8508 }
Paul Bakker887bd502011-06-08 13:10:54 +00008509
Paul Bakker5121ce52009-01-03 21:22:43 +00008510 if( ssl->out_left != 0 )
8511 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008512 /*
8513 * The user has previously tried to send the data and
8514 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8515 * written. In this case, we expect the high-level write function
8516 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8517 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008518 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008520 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008521 return( ret );
8522 }
8523 }
Paul Bakker887bd502011-06-08 13:10:54 +00008524 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008525 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008526 /*
8527 * The user is trying to send a message the first time, so we need to
8528 * copy the data into the internal buffers and setup the data structure
8529 * to keep track of partial writes
8530 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008531 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008532 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008533 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008534
Hanno Becker67bc7c32018-08-06 11:33:50 +01008535 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008537 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008538 return( ret );
8539 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008540 }
8541
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008542 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008543}
8544
8545/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008546 * Write application data, doing 1/n-1 splitting if necessary.
8547 *
8548 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008549 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008550 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008552#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008553static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008554 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008555{
8556 int ret;
8557
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008558 if( ssl->conf->cbc_record_splitting ==
8559 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008560 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008561 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8562 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8563 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008564 {
8565 return( ssl_write_real( ssl, buf, len ) );
8566 }
8567
8568 if( ssl->split_done == 0 )
8569 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008570 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008571 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008572 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008573 }
8574
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008575 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8576 return( ret );
8577 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008578
8579 return( ret + 1 );
8580}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008581#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008582
8583/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008584 * Write application data (public-facing wrapper)
8585 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008586int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008587{
8588 int ret;
8589
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008591
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008592 if( ssl == NULL || ssl->conf == NULL )
8593 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8594
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008595#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008596 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8597 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008598 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008599 return( ret );
8600 }
8601#endif
8602
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008603 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008604 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008605 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008606 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008607 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008608 return( ret );
8609 }
8610 }
8611
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008612#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008613 ret = ssl_write_split( ssl, buf, len );
8614#else
8615 ret = ssl_write_real( ssl, buf, len );
8616#endif
8617
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008619
8620 return( ret );
8621}
8622
8623/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008624 * Notify the peer that the connection is being closed
8625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008626int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008627{
8628 int ret;
8629
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008630 if( ssl == NULL || ssl->conf == NULL )
8631 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008634
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008635 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008636 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008638 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008640 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8641 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8642 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008644 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008645 return( ret );
8646 }
8647 }
8648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008650
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008651 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008652}
8653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008654void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008655{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008656 if( transform == NULL )
8657 return;
8658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008659#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008660 deflateEnd( &transform->ctx_deflate );
8661 inflateEnd( &transform->ctx_inflate );
8662#endif
8663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008664 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8665 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008667 mbedtls_md_free( &transform->md_ctx_enc );
8668 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008669
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008670 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008671}
8672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008673#if defined(MBEDTLS_X509_CRT_PARSE_C)
8674static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008675{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008676 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008677
8678 while( cur != NULL )
8679 {
8680 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008681 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008682 cur = next;
8683 }
8684}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008685#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008686
Hanno Becker0271f962018-08-16 13:23:47 +01008687#if defined(MBEDTLS_SSL_PROTO_DTLS)
8688
8689static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8690{
8691 unsigned offset;
8692 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8693
8694 if( hs == NULL )
8695 return;
8696
Hanno Becker283f5ef2018-08-24 09:34:47 +01008697 ssl_free_buffered_record( ssl );
8698
Hanno Becker0271f962018-08-16 13:23:47 +01008699 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01008700 ssl_buffering_free_slot( ssl, offset );
8701}
8702
8703static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
8704 uint8_t slot )
8705{
8706 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8707 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01008708
8709 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
8710 return;
8711
Hanno Beckere605b192018-08-21 15:59:07 +01008712 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01008713 {
Hanno Beckere605b192018-08-21 15:59:07 +01008714 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
8715 mbedtls_free( hs_buf->data );
8716 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01008717 }
8718}
8719
8720#endif /* MBEDTLS_SSL_PROTO_DTLS */
8721
Gilles Peskine9b562d52018-04-25 20:32:43 +02008722void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008723{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008724 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8725
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008726 if( handshake == NULL )
8727 return;
8728
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008729#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8730 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8731 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008732 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008733 handshake->async_in_progress = 0;
8734 }
8735#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8736
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008737#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8738 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8739 mbedtls_md5_free( &handshake->fin_md5 );
8740 mbedtls_sha1_free( &handshake->fin_sha1 );
8741#endif
8742#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8743#if defined(MBEDTLS_SHA256_C)
8744 mbedtls_sha256_free( &handshake->fin_sha256 );
8745#endif
8746#if defined(MBEDTLS_SHA512_C)
8747 mbedtls_sha512_free( &handshake->fin_sha512 );
8748#endif
8749#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008751#if defined(MBEDTLS_DHM_C)
8752 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008753#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008754#if defined(MBEDTLS_ECDH_C)
8755 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008756#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008757#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008758 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008759#if defined(MBEDTLS_SSL_CLI_C)
8760 mbedtls_free( handshake->ecjpake_cache );
8761 handshake->ecjpake_cache = NULL;
8762 handshake->ecjpake_cache_len = 0;
8763#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008764#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008765
Janos Follath4ae5c292016-02-10 11:27:43 +00008766#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8767 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008768 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008769 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008770#endif
8771
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008772#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8773 if( handshake->psk != NULL )
8774 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008775 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008776 mbedtls_free( handshake->psk );
8777 }
8778#endif
8779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008780#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8781 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008782 /*
8783 * Free only the linked list wrapper, not the keys themselves
8784 * since the belong to the SNI callback
8785 */
8786 if( handshake->sni_key_cert != NULL )
8787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008788 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008789
8790 while( cur != NULL )
8791 {
8792 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008793 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008794 cur = next;
8795 }
8796 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008797#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008799#if defined(MBEDTLS_SSL_PROTO_DTLS)
8800 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008801 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01008802 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008803#endif
8804
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008805 mbedtls_platform_zeroize( handshake,
8806 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008807}
8808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008809void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008810{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008811 if( session == NULL )
8812 return;
8813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008814#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008815 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008817 mbedtls_x509_crt_free( session->peer_cert );
8818 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008819 }
Paul Bakkered27a042013-04-18 22:46:23 +02008820#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008821
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008822#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008823 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008824#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008825
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008826 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008827}
8828
Paul Bakker5121ce52009-01-03 21:22:43 +00008829/*
8830 * Free an SSL context
8831 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008832void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008833{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008834 if( ssl == NULL )
8835 return;
8836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008838
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008839 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008840 {
Angus Grattond8213d02016-05-25 20:56:48 +10008841 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008842 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008843 }
8844
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008845 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008846 {
Angus Grattond8213d02016-05-25 20:56:48 +10008847 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008848 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008849 }
8850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008851#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008852 if( ssl->compress_buf != NULL )
8853 {
Angus Grattond8213d02016-05-25 20:56:48 +10008854 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008855 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008856 }
8857#endif
8858
Paul Bakker48916f92012-09-16 19:57:18 +00008859 if( ssl->transform )
8860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008861 mbedtls_ssl_transform_free( ssl->transform );
8862 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008863 }
8864
8865 if( ssl->handshake )
8866 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008867 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008868 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8869 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008871 mbedtls_free( ssl->handshake );
8872 mbedtls_free( ssl->transform_negotiate );
8873 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008874 }
8875
Paul Bakkerc0463502013-02-14 11:19:38 +01008876 if( ssl->session )
8877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008878 mbedtls_ssl_session_free( ssl->session );
8879 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008880 }
8881
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008882#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008883 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008884 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008885 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008886 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008887 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008888#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008890#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8891 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8894 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008895 }
8896#endif
8897
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008898#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008899 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008900#endif
8901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008903
Paul Bakker86f04f42013-02-14 11:20:09 +01008904 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008905 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008906}
8907
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008908/*
8909 * Initialze mbedtls_ssl_config
8910 */
8911void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8912{
8913 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8914}
8915
Simon Butcherc97b6972015-12-27 23:48:17 +00008916#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008917static int ssl_preset_default_hashes[] = {
8918#if defined(MBEDTLS_SHA512_C)
8919 MBEDTLS_MD_SHA512,
8920 MBEDTLS_MD_SHA384,
8921#endif
8922#if defined(MBEDTLS_SHA256_C)
8923 MBEDTLS_MD_SHA256,
8924 MBEDTLS_MD_SHA224,
8925#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008926#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008927 MBEDTLS_MD_SHA1,
8928#endif
8929 MBEDTLS_MD_NONE
8930};
Simon Butcherc97b6972015-12-27 23:48:17 +00008931#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008932
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008933static int ssl_preset_suiteb_ciphersuites[] = {
8934 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8935 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8936 0
8937};
8938
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008939#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008940static int ssl_preset_suiteb_hashes[] = {
8941 MBEDTLS_MD_SHA256,
8942 MBEDTLS_MD_SHA384,
8943 MBEDTLS_MD_NONE
8944};
8945#endif
8946
8947#if defined(MBEDTLS_ECP_C)
8948static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8949 MBEDTLS_ECP_DP_SECP256R1,
8950 MBEDTLS_ECP_DP_SECP384R1,
8951 MBEDTLS_ECP_DP_NONE
8952};
8953#endif
8954
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008955/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008956 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008957 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008958int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008959 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008960{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008961#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008962 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008963#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008964
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008965 /* Use the functions here so that they are covered in tests,
8966 * but otherwise access member directly for efficiency */
8967 mbedtls_ssl_conf_endpoint( conf, endpoint );
8968 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008969
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008970 /*
8971 * Things that are common to all presets
8972 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008973#if defined(MBEDTLS_SSL_CLI_C)
8974 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8975 {
8976 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8977#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8978 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8979#endif
8980 }
8981#endif
8982
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008983#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008984 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008985#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008986
8987#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8988 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8989#endif
8990
8991#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8992 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8993#endif
8994
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008995#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8996 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
8997#endif
8998
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008999#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009000 conf->f_cookie_write = ssl_cookie_write_dummy;
9001 conf->f_cookie_check = ssl_cookie_check_dummy;
9002#endif
9003
9004#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9005 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9006#endif
9007
Janos Follath088ce432017-04-10 12:42:31 +01009008#if defined(MBEDTLS_SSL_SRV_C)
9009 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9010#endif
9011
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009012#if defined(MBEDTLS_SSL_PROTO_DTLS)
9013 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9014 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9015#endif
9016
9017#if defined(MBEDTLS_SSL_RENEGOTIATION)
9018 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009019 memset( conf->renego_period, 0x00, 2 );
9020 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009021#endif
9022
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009023#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9024 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9025 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009026 const unsigned char dhm_p[] =
9027 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9028 const unsigned char dhm_g[] =
9029 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9030
Hanno Beckera90658f2017-10-04 15:29:08 +01009031 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9032 dhm_p, sizeof( dhm_p ),
9033 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009034 {
9035 return( ret );
9036 }
9037 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009038#endif
9039
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009040 /*
9041 * Preset-specific defaults
9042 */
9043 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009044 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009045 /*
9046 * NSA Suite B
9047 */
9048 case MBEDTLS_SSL_PRESET_SUITEB:
9049 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9050 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9051 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9052 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9053
9054 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9055 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9056 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9057 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9058 ssl_preset_suiteb_ciphersuites;
9059
9060#if defined(MBEDTLS_X509_CRT_PARSE_C)
9061 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009062#endif
9063
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009064#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009065 conf->sig_hashes = ssl_preset_suiteb_hashes;
9066#endif
9067
9068#if defined(MBEDTLS_ECP_C)
9069 conf->curve_list = ssl_preset_suiteb_curves;
9070#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009071 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009072
9073 /*
9074 * Default
9075 */
9076 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009077 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9078 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9079 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9080 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9081 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9082 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9083 MBEDTLS_SSL_MIN_MINOR_VERSION :
9084 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009085 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9086 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9087
9088#if defined(MBEDTLS_SSL_PROTO_DTLS)
9089 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9090 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9091#endif
9092
9093 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9094 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9095 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9096 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9097 mbedtls_ssl_list_ciphersuites();
9098
9099#if defined(MBEDTLS_X509_CRT_PARSE_C)
9100 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9101#endif
9102
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009103#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009104 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009105#endif
9106
9107#if defined(MBEDTLS_ECP_C)
9108 conf->curve_list = mbedtls_ecp_grp_id_list();
9109#endif
9110
9111#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9112 conf->dhm_min_bitlen = 1024;
9113#endif
9114 }
9115
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009116 return( 0 );
9117}
9118
9119/*
9120 * Free mbedtls_ssl_config
9121 */
9122void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9123{
9124#if defined(MBEDTLS_DHM_C)
9125 mbedtls_mpi_free( &conf->dhm_P );
9126 mbedtls_mpi_free( &conf->dhm_G );
9127#endif
9128
9129#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9130 if( conf->psk != NULL )
9131 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009132 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009133 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009134 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009135 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009136 }
9137
9138 if( conf->psk_identity != NULL )
9139 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009140 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009141 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009142 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009143 conf->psk_identity_len = 0;
9144 }
9145#endif
9146
9147#if defined(MBEDTLS_X509_CRT_PARSE_C)
9148 ssl_key_cert_free( conf->key_cert );
9149#endif
9150
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009151 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009152}
9153
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009154#if defined(MBEDTLS_PK_C) && \
9155 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009156/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009157 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009159unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009160{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009161#if defined(MBEDTLS_RSA_C)
9162 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9163 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009164#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009165#if defined(MBEDTLS_ECDSA_C)
9166 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9167 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009168#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009169 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009170}
9171
Hanno Becker7e5437a2017-04-28 17:15:26 +01009172unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9173{
9174 switch( type ) {
9175 case MBEDTLS_PK_RSA:
9176 return( MBEDTLS_SSL_SIG_RSA );
9177 case MBEDTLS_PK_ECDSA:
9178 case MBEDTLS_PK_ECKEY:
9179 return( MBEDTLS_SSL_SIG_ECDSA );
9180 default:
9181 return( MBEDTLS_SSL_SIG_ANON );
9182 }
9183}
9184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009185mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009186{
9187 switch( sig )
9188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009189#if defined(MBEDTLS_RSA_C)
9190 case MBEDTLS_SSL_SIG_RSA:
9191 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009192#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009193#if defined(MBEDTLS_ECDSA_C)
9194 case MBEDTLS_SSL_SIG_ECDSA:
9195 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009196#endif
9197 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009198 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009199 }
9200}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009201#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009202
Hanno Becker7e5437a2017-04-28 17:15:26 +01009203#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9204 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9205
9206/* Find an entry in a signature-hash set matching a given hash algorithm. */
9207mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9208 mbedtls_pk_type_t sig_alg )
9209{
9210 switch( sig_alg )
9211 {
9212 case MBEDTLS_PK_RSA:
9213 return( set->rsa );
9214 case MBEDTLS_PK_ECDSA:
9215 return( set->ecdsa );
9216 default:
9217 return( MBEDTLS_MD_NONE );
9218 }
9219}
9220
9221/* Add a signature-hash-pair to a signature-hash set */
9222void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9223 mbedtls_pk_type_t sig_alg,
9224 mbedtls_md_type_t md_alg )
9225{
9226 switch( sig_alg )
9227 {
9228 case MBEDTLS_PK_RSA:
9229 if( set->rsa == MBEDTLS_MD_NONE )
9230 set->rsa = md_alg;
9231 break;
9232
9233 case MBEDTLS_PK_ECDSA:
9234 if( set->ecdsa == MBEDTLS_MD_NONE )
9235 set->ecdsa = md_alg;
9236 break;
9237
9238 default:
9239 break;
9240 }
9241}
9242
9243/* Allow exactly one hash algorithm for each signature. */
9244void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9245 mbedtls_md_type_t md_alg )
9246{
9247 set->rsa = md_alg;
9248 set->ecdsa = md_alg;
9249}
9250
9251#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9252 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9253
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009254/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009255 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009257mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009258{
9259 switch( hash )
9260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009261#if defined(MBEDTLS_MD5_C)
9262 case MBEDTLS_SSL_HASH_MD5:
9263 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009264#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009265#if defined(MBEDTLS_SHA1_C)
9266 case MBEDTLS_SSL_HASH_SHA1:
9267 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009268#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009269#if defined(MBEDTLS_SHA256_C)
9270 case MBEDTLS_SSL_HASH_SHA224:
9271 return( MBEDTLS_MD_SHA224 );
9272 case MBEDTLS_SSL_HASH_SHA256:
9273 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009274#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009275#if defined(MBEDTLS_SHA512_C)
9276 case MBEDTLS_SSL_HASH_SHA384:
9277 return( MBEDTLS_MD_SHA384 );
9278 case MBEDTLS_SSL_HASH_SHA512:
9279 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009280#endif
9281 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009282 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009283 }
9284}
9285
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009286/*
9287 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9288 */
9289unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9290{
9291 switch( md )
9292 {
9293#if defined(MBEDTLS_MD5_C)
9294 case MBEDTLS_MD_MD5:
9295 return( MBEDTLS_SSL_HASH_MD5 );
9296#endif
9297#if defined(MBEDTLS_SHA1_C)
9298 case MBEDTLS_MD_SHA1:
9299 return( MBEDTLS_SSL_HASH_SHA1 );
9300#endif
9301#if defined(MBEDTLS_SHA256_C)
9302 case MBEDTLS_MD_SHA224:
9303 return( MBEDTLS_SSL_HASH_SHA224 );
9304 case MBEDTLS_MD_SHA256:
9305 return( MBEDTLS_SSL_HASH_SHA256 );
9306#endif
9307#if defined(MBEDTLS_SHA512_C)
9308 case MBEDTLS_MD_SHA384:
9309 return( MBEDTLS_SSL_HASH_SHA384 );
9310 case MBEDTLS_MD_SHA512:
9311 return( MBEDTLS_SSL_HASH_SHA512 );
9312#endif
9313 default:
9314 return( MBEDTLS_SSL_HASH_NONE );
9315 }
9316}
9317
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009318#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009319/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009320 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009321 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009322 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009323int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009324{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009325 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009326
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009327 if( ssl->conf->curve_list == NULL )
9328 return( -1 );
9329
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009330 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009331 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009332 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009333
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009334 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009335}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009336#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009337
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009338#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009339/*
9340 * Check if a hash proposed by the peer is in our list.
9341 * Return 0 if we're willing to use it, -1 otherwise.
9342 */
9343int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9344 mbedtls_md_type_t md )
9345{
9346 const int *cur;
9347
9348 if( ssl->conf->sig_hashes == NULL )
9349 return( -1 );
9350
9351 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9352 if( *cur == (int) md )
9353 return( 0 );
9354
9355 return( -1 );
9356}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009357#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359#if defined(MBEDTLS_X509_CRT_PARSE_C)
9360int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9361 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009362 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009363 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009364{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009365 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009366#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009367 int usage = 0;
9368#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009369#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009370 const char *ext_oid;
9371 size_t ext_len;
9372#endif
9373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009374#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9375 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009376 ((void) cert);
9377 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009378 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009379#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009381#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9382 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009383 {
9384 /* Server part of the key exchange */
9385 switch( ciphersuite->key_exchange )
9386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009387 case MBEDTLS_KEY_EXCHANGE_RSA:
9388 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009389 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009390 break;
9391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009392 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9393 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9394 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9395 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009396 break;
9397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009398 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9399 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009400 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009401 break;
9402
9403 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009404 case MBEDTLS_KEY_EXCHANGE_NONE:
9405 case MBEDTLS_KEY_EXCHANGE_PSK:
9406 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9407 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009408 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009409 usage = 0;
9410 }
9411 }
9412 else
9413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009414 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9415 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009416 }
9417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009418 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009419 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009420 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009421 ret = -1;
9422 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009423#else
9424 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009425#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009427#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9428 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009430 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9431 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009432 }
9433 else
9434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009435 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9436 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009437 }
9438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009439 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009440 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009441 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009442 ret = -1;
9443 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009444#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009445
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009446 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009447}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009448#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009449
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009450/*
9451 * Convert version numbers to/from wire format
9452 * and, for DTLS, to/from TLS equivalent.
9453 *
9454 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009455 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009456 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9457 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009459void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009460 unsigned char ver[2] )
9461{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009462#if defined(MBEDTLS_SSL_PROTO_DTLS)
9463 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009465 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009466 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9467
9468 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9469 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9470 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009471 else
9472#else
9473 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009474#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009475 {
9476 ver[0] = (unsigned char) major;
9477 ver[1] = (unsigned char) minor;
9478 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009479}
9480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009481void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009482 const unsigned char ver[2] )
9483{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009484#if defined(MBEDTLS_SSL_PROTO_DTLS)
9485 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009486 {
9487 *major = 255 - ver[0] + 2;
9488 *minor = 255 - ver[1] + 1;
9489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009491 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9492 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009493 else
9494#else
9495 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009496#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009497 {
9498 *major = ver[0];
9499 *minor = ver[1];
9500 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009501}
9502
Simon Butcher99000142016-10-13 17:21:01 +01009503int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9504{
9505#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9506 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9507 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9508
9509 switch( md )
9510 {
9511#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9512#if defined(MBEDTLS_MD5_C)
9513 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009514 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009515#endif
9516#if defined(MBEDTLS_SHA1_C)
9517 case MBEDTLS_SSL_HASH_SHA1:
9518 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9519 break;
9520#endif
9521#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9522#if defined(MBEDTLS_SHA512_C)
9523 case MBEDTLS_SSL_HASH_SHA384:
9524 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9525 break;
9526#endif
9527#if defined(MBEDTLS_SHA256_C)
9528 case MBEDTLS_SSL_HASH_SHA256:
9529 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9530 break;
9531#endif
9532 default:
9533 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9534 }
9535
9536 return 0;
9537#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9538 (void) ssl;
9539 (void) md;
9540
9541 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9542#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9543}
9544
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009545#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9546 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9547int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9548 unsigned char *output,
9549 unsigned char *data, size_t data_len )
9550{
9551 int ret = 0;
9552 mbedtls_md5_context mbedtls_md5;
9553 mbedtls_sha1_context mbedtls_sha1;
9554
9555 mbedtls_md5_init( &mbedtls_md5 );
9556 mbedtls_sha1_init( &mbedtls_sha1 );
9557
9558 /*
9559 * digitally-signed struct {
9560 * opaque md5_hash[16];
9561 * opaque sha_hash[20];
9562 * };
9563 *
9564 * md5_hash
9565 * MD5(ClientHello.random + ServerHello.random
9566 * + ServerParams);
9567 * sha_hash
9568 * SHA(ClientHello.random + ServerHello.random
9569 * + ServerParams);
9570 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009571 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009572 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009573 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009574 goto exit;
9575 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009576 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009577 ssl->handshake->randbytes, 64 ) ) != 0 )
9578 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009580 goto exit;
9581 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009582 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009583 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009584 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009585 goto exit;
9586 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009587 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009588 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009589 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009590 goto exit;
9591 }
9592
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009593 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009594 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009595 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009596 goto exit;
9597 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009598 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009599 ssl->handshake->randbytes, 64 ) ) != 0 )
9600 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009601 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009602 goto exit;
9603 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009604 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009605 data_len ) ) != 0 )
9606 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009607 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009608 goto exit;
9609 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009610 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009611 output + 16 ) ) != 0 )
9612 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009613 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009614 goto exit;
9615 }
9616
9617exit:
9618 mbedtls_md5_free( &mbedtls_md5 );
9619 mbedtls_sha1_free( &mbedtls_sha1 );
9620
9621 if( ret != 0 )
9622 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9623 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9624
9625 return( ret );
9626
9627}
9628#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9629 MBEDTLS_SSL_PROTO_TLS1_1 */
9630
9631#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9632 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9633int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009634 unsigned char *hash, size_t *hashlen,
9635 unsigned char *data, size_t data_len,
9636 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009637{
9638 int ret = 0;
9639 mbedtls_md_context_t ctx;
9640 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009641 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009642
9643 mbedtls_md_init( &ctx );
9644
9645 /*
9646 * digitally-signed struct {
9647 * opaque client_random[32];
9648 * opaque server_random[32];
9649 * ServerDHParams params;
9650 * };
9651 */
9652 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9653 {
9654 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9655 goto exit;
9656 }
9657 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9658 {
9659 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9660 goto exit;
9661 }
9662 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9663 {
9664 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9665 goto exit;
9666 }
9667 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9668 {
9669 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9670 goto exit;
9671 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009672 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009673 {
9674 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9675 goto exit;
9676 }
9677
9678exit:
9679 mbedtls_md_free( &ctx );
9680
9681 if( ret != 0 )
9682 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9683 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9684
9685 return( ret );
9686}
9687#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9688 MBEDTLS_SSL_PROTO_TLS1_2 */
9689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009690#endif /* MBEDTLS_SSL_TLS_C */