blob: 316d537acbebb83ee37350d9026efe73593a1a4c [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"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Becker2a43f6f2018-08-10 11:12:52 +010058static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010059static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010060
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010061/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010063{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020064#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010065 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010066#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020067
68#if defined(MBEDTLS_SSL_PROTO_DTLS)
69 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
70 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020071 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020072#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020073#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020075#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010076}
77
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020078/*
79 * Start a timer.
80 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020084 if( ssl->f_set_timer == NULL )
85 return;
86
87 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
88 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089}
90
91/*
92 * Return -1 is timer is expired, 0 if it isn't.
93 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020095{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020096 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020097 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020098
99 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200100 {
101 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200102 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200104
105 return( 0 );
106}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100108static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
109 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100110static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100111
112#define SSL_DONT_FORCE_FLUSH 0
113#define SSL_FORCE_FLUSH 1
114
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200115#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100116
Hanno Beckera5a2b082019-05-15 14:03:01 +0100117#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100118/* Top-level Connection ID API */
119
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100120int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
121 size_t len,
122 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100123{
124 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
126
Hanno Becker791ec6b2019-05-14 11:45:26 +0100127 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
128 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
129 {
130 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
131 }
132
133 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100134 conf->cid_len = len;
135 return( 0 );
136}
137
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100138int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
139 int enable,
140 unsigned char const *own_cid,
141 size_t own_cid_len )
142{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200143 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
145
Hanno Becker07489862019-04-25 16:01:49 +0100146 ssl->negotiate_cid = enable;
147 if( enable == MBEDTLS_SSL_CID_DISABLED )
148 {
149 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
150 return( 0 );
151 }
152 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100153 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100154
Hanno Beckereec2be92019-05-03 13:06:44 +0100155 if( own_cid_len != ssl->conf->cid_len )
Hanno Becker07489862019-04-25 16:01:49 +0100156 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
158 (unsigned) own_cid_len,
159 (unsigned) ssl->conf->cid_len ) );
Hanno Becker07489862019-04-25 16:01:49 +0100160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
161 }
162
163 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100164 /* Truncation is not an issue here because
165 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
166 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100167
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100168 return( 0 );
169}
170
171int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
172 int *enabled,
173 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
174 size_t *peer_cid_len )
175{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100176 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100177
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200178 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100179 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
180 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100182 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100183
Hanno Beckercb063f52019-05-03 12:54:52 +0100184 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
185 * were used, but client and server requested the empty CID.
186 * This is indistinguishable from not using the CID extension
187 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100188 if( ssl->transform_in->in_cid_len == 0 &&
189 ssl->transform_in->out_cid_len == 0 )
190 {
191 return( 0 );
192 }
193
Hanno Becker633d6042019-05-22 16:50:35 +0100194 if( peer_cid_len != NULL )
195 {
196 *peer_cid_len = ssl->transform_in->out_cid_len;
197 if( peer_cid != NULL )
198 {
199 memcpy( peer_cid, ssl->transform_in->out_cid,
200 ssl->transform_in->out_cid_len );
201 }
202 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100203
204 *enabled = MBEDTLS_SSL_CID_ENABLED;
205
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100206 return( 0 );
207}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100208#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100209
Hanno Beckerd5847772018-08-28 10:09:23 +0100210/* Forward declarations for functions related to message buffering. */
211static void ssl_buffering_free( mbedtls_ssl_context *ssl );
212static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
213 uint8_t slot );
214static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
215static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
216static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
217static int ssl_buffer_message( mbedtls_ssl_context *ssl );
218static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100219static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100220
Hanno Beckera67dee22018-08-22 10:05:20 +0100221static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100222static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100223{
Hanno Becker11682cc2018-08-22 14:41:02 +0100224 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100225
226 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100227 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100228
229 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
230}
231
Hanno Becker67bc7c32018-08-06 11:33:50 +0100232static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
233{
Hanno Becker11682cc2018-08-22 14:41:02 +0100234 size_t const bytes_written = ssl->out_left;
235 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100236
237 /* Double-check that the write-index hasn't gone
238 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100239 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100240 {
241 /* Should never happen... */
242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
243 }
244
245 return( (int) ( mtu - bytes_written ) );
246}
247
248static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
249{
250 int ret;
251 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400252 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100253
254#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
255 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
256
257 if( max_len > mfl )
258 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100259
260 /* By the standard (RFC 6066 Sect. 4), the MFL extension
261 * only limits the maximum record payload size, so in theory
262 * we would be allowed to pack multiple records of payload size
263 * MFL into a single datagram. However, this would mean that there's
264 * no way to explicitly communicate MTU restrictions to the peer.
265 *
266 * The following reduction of max_len makes sure that we never
267 * write datagrams larger than MFL + Record Expansion Overhead.
268 */
269 if( max_len <= ssl->out_left )
270 return( 0 );
271
272 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100273#endif
274
275 ret = ssl_get_remaining_space_in_datagram( ssl );
276 if( ret < 0 )
277 return( ret );
278 remaining = (size_t) ret;
279
280 ret = mbedtls_ssl_get_record_expansion( ssl );
281 if( ret < 0 )
282 return( ret );
283 expansion = (size_t) ret;
284
285 if( remaining <= expansion )
286 return( 0 );
287
288 remaining -= expansion;
289 if( remaining >= max_len )
290 remaining = max_len;
291
292 return( (int) remaining );
293}
294
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200295/*
296 * Double the retransmit timeout value, within the allowed range,
297 * returning -1 if the maximum value has already been reached.
298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200300{
301 uint32_t new_timeout;
302
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200303 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200304 return( -1 );
305
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200306 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
307 * in the following way: after the initial transmission and a first
308 * retransmission, back off to a temporary estimated MTU of 508 bytes.
309 * This value is guaranteed to be deliverable (if not guaranteed to be
310 * delivered) of any compliant IPv4 (and IPv6) network, and should work
311 * on most non-IP stacks too. */
312 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400313 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200314 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
316 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200317
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200318 new_timeout = 2 * ssl->handshake->retransmit_timeout;
319
320 /* Avoid arithmetic overflow and range overflow */
321 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200322 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200323 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200324 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200325 }
326
327 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200329 ssl->handshake->retransmit_timeout ) );
330
331 return( 0 );
332}
333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200335{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200336 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200338 ssl->handshake->retransmit_timeout ) );
339}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200343/*
344 * Convert max_fragment_length codes to length.
345 * RFC 6066 says:
346 * enum{
347 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
348 * } MaxFragmentLength;
349 * and we add 0 -> extension unused
350 */
Angus Grattond8213d02016-05-25 20:56:48 +1000351static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200352{
Angus Grattond8213d02016-05-25 20:56:48 +1000353 switch( mfl )
354 {
355 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
356 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
357 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
358 return 512;
359 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
360 return 1024;
361 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
362 return 2048;
363 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
364 return 4096;
365 default:
366 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
367 }
368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200370
Hanno Becker58fccf22019-02-06 14:30:46 +0000371int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
372 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_ssl_session_free( dst );
375 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000378
379#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200380 if( src->peer_cert != NULL )
381 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200382 int ret;
383
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200384 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200386 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200391 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200394 dst->peer_cert = NULL;
395 return( ret );
396 }
397 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100398#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000399 if( src->peer_cert_digest != NULL )
400 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000401 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000402 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000403 if( dst->peer_cert_digest == NULL )
404 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
405
406 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
407 src->peer_cert_digest_len );
408 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000409 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000410 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100411#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200414
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200415#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200416 if( src->ticket != NULL )
417 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200418 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200419 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200420 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200421
422 memcpy( dst->ticket, src->ticket, src->ticket_len );
423 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200424#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200425
426 return( 0 );
427}
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
430int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200431 const unsigned char *key_enc, const unsigned char *key_dec,
432 size_t keylen,
433 const unsigned char *iv_enc, const unsigned char *iv_dec,
434 size_t ivlen,
435 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200436 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
438int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
439int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
440int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
441int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
442#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000443
Paul Bakker5121ce52009-01-03 21:22:43 +0000444/*
445 * Key material generation
446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200448static int ssl3_prf( const unsigned char *secret, size_t slen,
449 const char *label,
450 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000451 unsigned char *dstbuf, size_t dlen )
452{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100453 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000454 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200455 mbedtls_md5_context md5;
456 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000457 unsigned char padding[16];
458 unsigned char sha1sum[20];
459 ((void)label);
460
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200461 mbedtls_md5_init( &md5 );
462 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200463
Paul Bakker5f70b252012-09-13 14:23:06 +0000464 /*
465 * SSLv3:
466 * block =
467 * MD5( secret + SHA1( 'A' + secret + random ) ) +
468 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
469 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
470 * ...
471 */
472 for( i = 0; i < dlen / 16; i++ )
473 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200474 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000475
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100476 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100477 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100478 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100479 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100480 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100481 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100482 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100483 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100484 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100485 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000486
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100487 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100488 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100489 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100490 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100491 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100492 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100493 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100494 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000495 }
496
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100497exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200498 mbedtls_md5_free( &md5 );
499 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000500
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500501 mbedtls_platform_zeroize( padding, sizeof( padding ) );
502 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000503
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100504 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000505}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200509static int tls1_prf( const unsigned char *secret, size_t slen,
510 const char *label,
511 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000512 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000513{
Paul Bakker23986e52011-04-24 08:57:21 +0000514 size_t nb, hs;
515 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200516 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000517 unsigned char tmp[128];
518 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 const mbedtls_md_info_t *md_info;
520 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100521 int ret;
522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
528 hs = ( slen + 1 ) / 2;
529 S1 = secret;
530 S2 = secret + slen - hs;
531
532 nb = strlen( label );
533 memcpy( tmp + 20, label, nb );
534 memcpy( tmp + 20 + nb, random, rlen );
535 nb += rlen;
536
537 /*
538 * First compute P_md5(secret,label+random)[0..dlen]
539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
541 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100544 return( ret );
545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
547 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
548 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550 for( i = 0; i < dlen; i += 16 )
551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_md_hmac_reset ( &md_ctx );
553 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
554 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_md_hmac_reset ( &md_ctx );
557 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
558 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
560 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
561
562 for( j = 0; j < k; j++ )
563 dstbuf[i + j] = h_i[j];
564 }
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100567
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 /*
569 * XOR out with P_sha1(secret,label+random)[0..dlen]
570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100575 return( ret );
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
578 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
579 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
581 for( i = 0; i < dlen; i += 20 )
582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_md_hmac_reset ( &md_ctx );
584 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
585 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_md_hmac_reset ( &md_ctx );
588 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
589 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
591 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
592
593 for( j = 0; j < k; j++ )
594 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
595 }
596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100598
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500599 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
600 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
602 return( 0 );
603}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
607static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100608 const unsigned char *secret, size_t slen,
609 const char *label,
610 const unsigned char *random, size_t rlen,
611 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000612{
613 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100614 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000615 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
617 const mbedtls_md_info_t *md_info;
618 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100619 int ret;
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
624 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100627
628 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000630
631 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100632 memcpy( tmp + md_len, label, nb );
633 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000634 nb += rlen;
635
636 /*
637 * Compute P_<hash>(secret, label + random)[0..dlen]
638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100640 return( ret );
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
643 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
644 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100645
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100646 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_md_hmac_reset ( &md_ctx );
649 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
650 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_md_hmac_reset ( &md_ctx );
653 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
654 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000655
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100656 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000657
658 for( j = 0; j < k; j++ )
659 dstbuf[i + j] = h_i[j];
660 }
661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100663
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500664 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
665 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000666
667 return( 0 );
668}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100671static int tls_prf_sha256( const unsigned char *secret, size_t slen,
672 const char *label,
673 const unsigned char *random, size_t rlen,
674 unsigned char *dstbuf, size_t dlen )
675{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100677 label, random, rlen, dstbuf, dlen ) );
678}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200682static int tls_prf_sha384( const unsigned char *secret, size_t slen,
683 const char *label,
684 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000685 unsigned char *dstbuf, size_t dlen )
686{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100688 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000689}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690#endif /* MBEDTLS_SHA512_C */
691#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
696 defined(MBEDTLS_SSL_PROTO_TLS1_1)
697static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200698#endif
Paul Bakker380da532012-04-18 16:10:25 +0000699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200701static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200703#endif
704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200706static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200708#endif
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
711#if defined(MBEDTLS_SHA256_C)
712static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200713static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200715#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717#if defined(MBEDTLS_SHA512_C)
718static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200719static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100721#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000723
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200724/* Type for the TLS PRF */
725typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
726 const unsigned char *, size_t,
727 unsigned char *, size_t);
728
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200729/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200730 * Populate a transform structure with session keys and all the other
731 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200732 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200733 * Parameters:
734 * - [in/out]: transform: structure to populate
735 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200736 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200737 * - [in] ciphersuite
738 * - [in] master
739 * - [in] encrypt_then_mac
740 * - [in] trunc_hmac
741 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200742 * - [in] tls_prf: pointer to PRF to use for key derivation
743 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200744 * - [in] minor_ver: SSL/TLS minor version
745 * - [in] endpoint: client or server
746 * - [in] ssl: optionally used for:
747 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
748 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
749 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200750 */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200751static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200752 int ciphersuite,
753 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100754#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200755#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
756 int encrypt_then_mac,
757#endif
758#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
759 int trunc_hmac,
760#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100761#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200762#if defined(MBEDTLS_ZLIB_SUPPORT)
763 int compression,
764#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200765 ssl_tls_prf_t tls_prf,
766 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200767 int minor_ver,
768 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200769 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000770{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200771 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000772 unsigned char keyblk[256];
773 unsigned char *key1;
774 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100775 unsigned char *mac_enc;
776 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000777 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200778 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000779 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000780 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 const mbedtls_cipher_info_t *cipher_info;
782 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100783
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200784#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
785 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
786 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200787 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200788 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000789#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000790
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200791 /* Copy info about negotiated version and extensions */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100792#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
793 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200794 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000795#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200796 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000797
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200798 /*
799 * Get various info structures
800 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200801 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200802 if( ciphersuite_info == NULL )
803 {
804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200805 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
807 }
808
Hanno Becker8759e162017-12-27 21:34:08 +0000809 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100810 if( cipher_info == NULL )
811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000813 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100815 }
816
Hanno Becker8759e162017-12-27 21:34:08 +0000817 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100818 if( md_info == NULL )
819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000821 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100823 }
824
Hanno Beckera5a2b082019-05-15 14:03:01 +0100825#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100826 /* Copy own and peer's CID if the use of the CID
827 * extension has been negotiated. */
828 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
829 {
830 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100831
Hanno Becker4932f9f2019-05-03 15:23:51 +0100832 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100833 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100834 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100835 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100836
837 transform->out_cid_len = ssl->handshake->peer_cid_len;
838 memcpy( transform->out_cid, ssl->handshake->peer_cid,
839 ssl->handshake->peer_cid_len );
840 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
841 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100842 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100843#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100844
Paul Bakker5121ce52009-01-03 21:22:43 +0000845 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200846 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000847 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200848 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100849 if( ret != 0 )
850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100852 return( ret );
853 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200856 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200857 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200858 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000860
Paul Bakker5121ce52009-01-03 21:22:43 +0000861 /*
862 * Determine the appropriate key, IV and MAC length.
863 */
Paul Bakker68884e32013-01-07 18:20:04 +0100864
Hanno Beckere7f2df02017-12-27 08:17:40 +0000865 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200866
Hanno Beckerf1229442018-01-03 15:32:31 +0000867#if defined(MBEDTLS_GCM_C) || \
868 defined(MBEDTLS_CCM_C) || \
869 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200871 cipher_info->mode == MBEDTLS_MODE_CCM ||
872 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000873 {
Hanno Becker8759e162017-12-27 21:34:08 +0000874 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200875
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200876 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000877 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000878 transform->taglen =
879 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200880
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200881 /* All modes haves 96-bit IVs;
882 * GCM and CCM has 4 implicit and 8 explicit bytes
883 * ChachaPoly has all 12 bytes implicit
884 */
Paul Bakker68884e32013-01-07 18:20:04 +0100885 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200886 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
887 transform->fixed_ivlen = 12;
888 else
889 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200890
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200891 /* Minimum length of encrypted record */
892 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000893 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100894 }
895 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000896#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
897#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
898 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
899 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100900 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200901 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
903 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200906 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100907 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200909 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000910 mac_key_len = mbedtls_md_get_size( md_info );
911 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200914 /*
915 * If HMAC is to be truncated, we shall keep the leftmost bytes,
916 * (rfc 6066 page 13 or rfc 2104 section 4),
917 * so we only need to adjust the length here.
918 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200919 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000922
923#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
924 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000925 * HMAC implementation which also truncates the key
926 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000927 mac_key_len = transform->maclen;
928#endif
929 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200931
932 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100933 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000934
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200935 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200937 transform->minlen = transform->maclen;
938 else
Paul Bakker68884e32013-01-07 18:20:04 +0100939 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200940 /*
941 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100942 * 1. if EtM is in use: one block plus MAC
943 * otherwise: * first multiple of blocklen greater than maclen
944 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200945 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200947 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100948 {
949 transform->minlen = transform->maclen
950 + cipher_info->block_size;
951 }
952 else
953#endif
954 {
955 transform->minlen = transform->maclen
956 + cipher_info->block_size
957 - transform->maclen % cipher_info->block_size;
958 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200961 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
962 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200963 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100964 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200965#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200967 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
968 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200969 {
970 transform->minlen += transform->ivlen;
971 }
972 else
973#endif
974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
976 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200977 }
Paul Bakker68884e32013-01-07 18:20:04 +0100978 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000979 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000980 else
981#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
982 {
983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
984 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
985 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000986
Hanno Beckere7f2df02017-12-27 08:17:40 +0000987 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
988 (unsigned) keylen,
989 (unsigned) transform->minlen,
990 (unsigned) transform->ivlen,
991 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000992
993 /*
994 * Finally setup the cipher contexts, IVs and MAC secrets.
995 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200997 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000998 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000999 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001000 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
Paul Bakker68884e32013-01-07 18:20:04 +01001002 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001003 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001004
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001005 /*
1006 * This is not used in TLS v1.1.
1007 */
Paul Bakker48916f92012-09-16 19:57:18 +00001008 iv_copy_len = ( transform->fixed_ivlen ) ?
1009 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001010 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1011 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001012 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001013 }
1014 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015#endif /* MBEDTLS_SSL_CLI_C */
1016#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001017 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001018 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001019 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001020 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Hanno Becker81c7b182017-11-09 18:39:33 +00001022 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001023 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001024
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001025 /*
1026 * This is not used in TLS v1.1.
1027 */
Paul Bakker48916f92012-09-16 19:57:18 +00001028 iv_copy_len = ( transform->fixed_ivlen ) ?
1029 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001030 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1031 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001032 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001033 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001034 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1038 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001039 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001040
Hanno Becker92231322018-01-03 15:32:51 +00001041#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001043 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001044 {
Hanno Becker92231322018-01-03 15:32:51 +00001045 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1048 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001049 }
1050
Hanno Becker81c7b182017-11-09 18:39:33 +00001051 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1052 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001053 }
1054 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1056#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1057 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001058 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001059 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001060 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1061 For AEAD-based ciphersuites, there is nothing to do here. */
1062 if( mac_key_len != 0 )
1063 {
1064 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1065 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1066 }
Paul Bakker68884e32013-01-07 18:20:04 +01001067 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001068 else
1069#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1072 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001073 }
Hanno Becker92231322018-01-03 15:32:51 +00001074#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1077 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001078 {
1079 int ret = 0;
1080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001082
Hanno Beckere7f2df02017-12-27 08:17:40 +00001083 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001084 transform->iv_enc, transform->iv_dec,
1085 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001086 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001087 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1090 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001091 }
1092 }
Hanno Becker92231322018-01-03 15:32:51 +00001093#else
1094 ((void) mac_dec);
1095 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001097
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001098#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1099 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001100 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001101 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001102 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001103 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001104 iv_copy_len );
1105 }
1106#endif
1107
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001108 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001109 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001111 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001112 return( ret );
1113 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001114
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001115 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001116 cipher_info ) ) != 0 )
1117 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001118 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001119 return( ret );
1120 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001123 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001127 return( ret );
1128 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001131 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001135 return( ret );
1136 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138#if defined(MBEDTLS_CIPHER_MODE_CBC)
1139 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1142 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001145 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001146 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1149 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001152 return( ret );
1153 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001156
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001157 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001158
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001159 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001161 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001164
Paul Bakker48916f92012-09-16 19:57:18 +00001165 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1166 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001167
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001168 if( deflateInit( &transform->ctx_deflate,
1169 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001170 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1173 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001174 }
1175 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001177
Paul Bakker5121ce52009-01-03 21:22:43 +00001178 return( 0 );
1179}
1180
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001181/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001182 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001183 *
1184 * Inputs:
1185 * - SSL/TLS minor version
1186 * - hash associated with the ciphersuite (only used by TLS 1.2)
1187 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001188 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001189 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1190 */
1191static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1192 int minor_ver,
1193 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001194{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001195#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1196 (void) hash;
1197#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001198
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001199#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001200 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001201 {
1202 handshake->tls_prf = ssl3_prf;
1203 handshake->calc_verify = ssl_calc_verify_ssl;
1204 handshake->calc_finished = ssl_calc_finished_ssl;
1205 }
1206 else
1207#endif
1208#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001209 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001210 {
1211 handshake->tls_prf = tls1_prf;
1212 handshake->calc_verify = ssl_calc_verify_tls;
1213 handshake->calc_finished = ssl_calc_finished_tls;
1214 }
1215 else
1216#endif
1217#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1218#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001219 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1220 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001221 {
1222 handshake->tls_prf = tls_prf_sha384;
1223 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1224 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1225 }
1226 else
1227#endif
1228#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001229 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001230 {
1231 handshake->tls_prf = tls_prf_sha256;
1232 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1233 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1234 }
1235 else
1236#endif
1237#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1238 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001239 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1240 }
1241
1242 return( 0 );
1243}
1244
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001245/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001246 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001247 *
1248 * Parameters:
1249 * [in/out] handshake
1250 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1251 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001252 * [out] master
1253 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001254 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001255static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001256 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001257 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001258{
1259 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001260
1261#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001262 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001263 (void) ssl;
1264#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001265
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001266#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001267 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001268 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001269 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1270 return( 0 );
1271 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001272#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001273
1274 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1275 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001276
1277#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001278 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1279 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001280 {
1281 unsigned char session_hash[48];
1282 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001283
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001284 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001285
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001286 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1287 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001288
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001289 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001290 "extended master secret",
1291 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001292 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001293 }
1294 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001295#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001296 {
1297 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1298 "master secret",
1299 handshake->randbytes, 64,
1300 master, 48 );
1301 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001302 if( ret != 0 )
1303 {
1304 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1305 return( ret );
1306 }
1307
1308 mbedtls_platform_zeroize( handshake->premaster,
1309 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001310
1311 return( 0 );
1312}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001313
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001314int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1315{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001316 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001317 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1318 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001319
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1321
1322 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001323 ret = ssl_set_handshake_prfs( ssl->handshake,
1324 ssl->minor_ver,
1325 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001326 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001327 {
1328 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001329 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001330 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001331
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001332 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001333 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001334 ssl->session_negotiate->master,
1335 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001336 if( ret != 0 )
1337 {
1338 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1339 return( ret );
1340 }
1341
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001342 /* Swap the client and server random values:
1343 * - MS derivation wanted client+server (RFC 5246 8.1)
1344 * - key derivation wants server+client (RFC 5246 6.3) */
1345 {
1346 unsigned char tmp[64];
1347 memcpy( tmp, ssl->handshake->randbytes, 64 );
1348 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1349 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1350 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1351 }
1352
1353 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001354 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001355 ssl->session_negotiate->ciphersuite,
1356 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001357#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001358#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1359 ssl->session_negotiate->encrypt_then_mac,
1360#endif
1361#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1362 ssl->session_negotiate->trunc_hmac,
1363#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001364#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001365#if defined(MBEDTLS_ZLIB_SUPPORT)
1366 ssl->session_negotiate->compression,
1367#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001368 ssl->handshake->tls_prf,
1369 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001370 ssl->minor_ver,
1371 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001372 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001373 if( ret != 0 )
1374 {
1375 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1376 return( ret );
1377 }
1378
1379 /* We no longer need Server/ClientHello.random values */
1380 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1381 sizeof( ssl->handshake->randbytes ) );
1382
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001383 /* Allocate compression buffer */
1384#if defined(MBEDTLS_ZLIB_SUPPORT)
1385 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1386 ssl->compress_buf == NULL )
1387 {
1388 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1389 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1390 if( ssl->compress_buf == NULL )
1391 {
1392 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001393 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001394 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1395 }
1396 }
1397#endif
1398
Paul Bakker5121ce52009-01-03 21:22:43 +00001399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1400
1401 return( 0 );
1402}
1403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001405void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1406 unsigned char hash[36],
1407 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001408{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001409 mbedtls_md5_context md5;
1410 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001411 unsigned char pad_1[48];
1412 unsigned char pad_2[48];
1413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001415
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001416 mbedtls_md5_init( &md5 );
1417 mbedtls_sha1_init( &sha1 );
1418
1419 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1420 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421
Paul Bakker380da532012-04-18 16:10:25 +00001422 memset( pad_1, 0x36, 48 );
1423 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001424
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001425 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1426 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1427 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001428
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001429 mbedtls_md5_starts_ret( &md5 );
1430 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1431 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1432 mbedtls_md5_update_ret( &md5, hash, 16 );
1433 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001434
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001435 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1436 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1437 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001438
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001439 mbedtls_sha1_starts_ret( &sha1 );
1440 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1441 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1442 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1443 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001444
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001445 *hlen = 36;
1446
1447 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001449
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001450 mbedtls_md5_free( &md5 );
1451 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001452
Paul Bakker380da532012-04-18 16:10:25 +00001453 return;
1454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001458void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1459 unsigned char hash[36],
1460 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001461{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001462 mbedtls_md5_context md5;
1463 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001466
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001467 mbedtls_md5_init( &md5 );
1468 mbedtls_sha1_init( &sha1 );
1469
1470 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1471 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001472
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001473 mbedtls_md5_finish_ret( &md5, hash );
1474 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001475
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001476 *hlen = 36;
1477
1478 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001480
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001481 mbedtls_md5_free( &md5 );
1482 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001483
Paul Bakker380da532012-04-18 16:10:25 +00001484 return;
1485}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1489#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001490void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1491 unsigned char hash[32],
1492 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001493{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001494 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001495
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001496 mbedtls_sha256_init( &sha256 );
1497
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001499
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001500 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001501 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001502
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001503 *hlen = 32;
1504
1505 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001507
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001508 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001509
Paul Bakker380da532012-04-18 16:10:25 +00001510 return;
1511}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001515void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1516 unsigned char hash[48],
1517 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001518{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001519 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001520
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001521 mbedtls_sha512_init( &sha512 );
1522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001524
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001525 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001526 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001527
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001528 *hlen = 48;
1529
1530 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001532
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001533 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001534
Paul Bakker5121ce52009-01-03 21:22:43 +00001535 return;
1536}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#endif /* MBEDTLS_SHA512_C */
1538#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1541int 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 +02001542{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001543 unsigned char *p = ssl->handshake->premaster;
1544 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001545 const unsigned char *psk = ssl->conf->psk;
1546 size_t psk_len = ssl->conf->psk_len;
1547
1548 /* If the psk callback was called, use its result */
1549 if( ssl->handshake->psk != NULL )
1550 {
1551 psk = ssl->handshake->psk;
1552 psk_len = ssl->handshake->psk_len;
1553 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001554
1555 /*
1556 * PMS = struct {
1557 * opaque other_secret<0..2^16-1>;
1558 * opaque psk<0..2^16-1>;
1559 * };
1560 * with "other_secret" depending on the particular key exchange
1561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1563 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001564 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001565 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001567
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001568 *(p++) = (unsigned char)( psk_len >> 8 );
1569 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001570
1571 if( end < p || (size_t)( end - p ) < psk_len )
1572 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1573
1574 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001575 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001576 }
1577 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1579#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1580 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001581 {
1582 /*
1583 * other_secret already set by the ClientKeyExchange message,
1584 * and is 48 bytes long
1585 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001586 if( end - p < 2 )
1587 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1588
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001589 *p++ = 0;
1590 *p++ = 48;
1591 p += 48;
1592 }
1593 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1595#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1596 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001597 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001598 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001599 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001600
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001601 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001603 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001604 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001607 return( ret );
1608 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001609 *(p++) = (unsigned char)( len >> 8 );
1610 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001611 p += len;
1612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001614 }
1615 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1617#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1618 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001619 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001620 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001621 size_t zlen;
1622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001624 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001625 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001628 return( ret );
1629 }
1630
1631 *(p++) = (unsigned char)( zlen >> 8 );
1632 *(p++) = (unsigned char)( zlen );
1633 p += zlen;
1634
Janos Follath3fbdada2018-08-15 10:26:53 +01001635 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1636 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001637 }
1638 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1642 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001643 }
1644
1645 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001646 if( end - p < 2 )
1647 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001648
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001649 *(p++) = (unsigned char)( psk_len >> 8 );
1650 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001651
1652 if( end < p || (size_t)( end - p ) < psk_len )
1653 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1654
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001655 memcpy( p, psk, psk_len );
1656 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001657
1658 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1659
1660 return( 0 );
1661}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001665/*
1666 * SSLv3.0 MAC functions
1667 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001668#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001669static void ssl_mac( mbedtls_md_context_t *md_ctx,
1670 const unsigned char *secret,
1671 const unsigned char *buf, size_t len,
1672 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001673 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001674{
1675 unsigned char header[11];
1676 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001677 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1679 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001680
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001681 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001683 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001684 else
Paul Bakker68884e32013-01-07 18:20:04 +01001685 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001686
1687 memcpy( header, ctr, 8 );
1688 header[ 8] = (unsigned char) type;
1689 header[ 9] = (unsigned char)( len >> 8 );
1690 header[10] = (unsigned char)( len );
1691
Paul Bakker68884e32013-01-07 18:20:04 +01001692 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 mbedtls_md_starts( md_ctx );
1694 mbedtls_md_update( md_ctx, secret, md_size );
1695 mbedtls_md_update( md_ctx, padding, padlen );
1696 mbedtls_md_update( md_ctx, header, 11 );
1697 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001698 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001699
Paul Bakker68884e32013-01-07 18:20:04 +01001700 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 mbedtls_md_starts( md_ctx );
1702 mbedtls_md_update( md_ctx, secret, md_size );
1703 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001704 mbedtls_md_update( md_ctx, out, md_size );
1705 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001706}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001708
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001709/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001710 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001711#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001712 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1713 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1714 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1715/* This function makes sure every byte in the memory region is accessed
1716 * (in ascending addresses order) */
1717static void ssl_read_memory( unsigned char *p, size_t len )
1718{
1719 unsigned char acc = 0;
1720 volatile unsigned char force;
1721
1722 for( ; len != 0; p++, len-- )
1723 acc ^= *p;
1724
1725 force = acc;
1726 (void) force;
1727}
1728#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1729
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001730/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001731 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001732 */
Hanno Becker3307b532017-12-27 21:37:21 +00001733
Hanno Beckera5a2b082019-05-15 14:03:01 +01001734#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001735/* This functions transforms a DTLS plaintext fragment and a record content
1736 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001737 *
1738 * struct {
1739 * opaque content[DTLSPlaintext.length];
1740 * ContentType real_type;
1741 * uint8 zeros[length_of_padding];
1742 * } DTLSInnerPlaintext;
1743 *
1744 * Input:
1745 * - `content`: The beginning of the buffer holding the
1746 * plaintext to be wrapped.
1747 * - `*content_size`: The length of the plaintext in Bytes.
1748 * - `max_len`: The number of Bytes available starting from
1749 * `content`. This must be `>= *content_size`.
1750 * - `rec_type`: The desired record content type.
1751 *
1752 * Output:
1753 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1754 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1755 *
1756 * Returns:
1757 * - `0` on success.
1758 * - A negative error code if `max_len` didn't offer enough space
1759 * for the expansion.
1760 */
1761static int ssl_cid_build_inner_plaintext( unsigned char *content,
1762 size_t *content_size,
1763 size_t remaining,
1764 uint8_t rec_type )
1765{
1766 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001767 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1768 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1769 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001770
1771 /* Write real content type */
1772 if( remaining == 0 )
1773 return( -1 );
1774 content[ len ] = rec_type;
1775 len++;
1776 remaining--;
1777
1778 if( remaining < pad )
1779 return( -1 );
1780 memset( content + len, 0, pad );
1781 len += pad;
1782 remaining -= pad;
1783
1784 *content_size = len;
1785 return( 0 );
1786}
1787
Hanno Becker7dc25772019-05-20 15:08:01 +01001788/* This function parses a DTLSInnerPlaintext structure.
1789 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001790static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1791 size_t *content_size,
1792 uint8_t *rec_type )
1793{
1794 size_t remaining = *content_size;
1795
1796 /* Determine length of padding by skipping zeroes from the back. */
1797 do
1798 {
1799 if( remaining == 0 )
1800 return( -1 );
1801 remaining--;
1802 } while( content[ remaining ] == 0 );
1803
1804 *content_size = remaining;
1805 *rec_type = content[ remaining ];
1806
1807 return( 0 );
1808}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001809#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001810
Hanno Becker99abf512019-05-20 14:50:53 +01001811/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001812 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001813static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001814 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001815 mbedtls_record *rec )
1816{
Hanno Becker99abf512019-05-20 14:50:53 +01001817 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001818 *
1819 * additional_data = seq_num + TLSCompressed.type +
1820 * TLSCompressed.version + TLSCompressed.length;
1821 *
Hanno Becker99abf512019-05-20 14:50:53 +01001822 * For the CID extension, this is extended as follows
1823 * (quoting draft-ietf-tls-dtls-connection-id-05,
1824 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001825 *
1826 * additional_data = seq_num + DTLSPlaintext.type +
1827 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001828 * cid +
1829 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001830 * length_of_DTLSInnerPlaintext;
1831 */
1832
Hanno Becker3307b532017-12-27 21:37:21 +00001833 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1834 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001835 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001836
Hanno Beckera5a2b082019-05-15 14:03:01 +01001837#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001838 if( rec->cid_len != 0 )
1839 {
1840 memcpy( add_data + 11, rec->cid, rec->cid_len );
1841 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1842 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1843 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1844 *add_data_len = 13 + 1 + rec->cid_len;
1845 }
1846 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001847#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001848 {
1849 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1850 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1851 *add_data_len = 13;
1852 }
Hanno Becker3307b532017-12-27 21:37:21 +00001853}
1854
Hanno Becker611a83b2018-01-03 14:27:32 +00001855int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1856 mbedtls_ssl_transform *transform,
1857 mbedtls_record *rec,
1858 int (*f_rng)(void *, unsigned char *, size_t),
1859 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001860{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001862 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001863 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001864 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001865 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001866 size_t post_avail;
1867
1868 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001869#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001870 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001871 ((void) ssl);
1872#endif
1873
1874 /* The PRNG is used for dynamic IV generation that's used
1875 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1876#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1877 ( defined(MBEDTLS_AES_C) || \
1878 defined(MBEDTLS_ARIA_C) || \
1879 defined(MBEDTLS_CAMELLIA_C) ) && \
1880 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1881 ((void) f_rng);
1882 ((void) p_rng);
1883#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001886
Hanno Becker3307b532017-12-27 21:37:21 +00001887 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001888 {
Hanno Becker3307b532017-12-27 21:37:21 +00001889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1890 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1891 }
Hanno Becker505089d2019-05-01 09:45:57 +01001892 if( rec == NULL
1893 || rec->buf == NULL
1894 || rec->buf_len < rec->data_offset
1895 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001896#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001897 || rec->cid_len != 0
1898#endif
1899 )
Hanno Becker3307b532017-12-27 21:37:21 +00001900 {
1901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001903 }
1904
Hanno Becker3307b532017-12-27 21:37:21 +00001905 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001906 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001908 data, rec->data_len );
1909
1910 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1911
1912 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1913 {
1914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1915 (unsigned) rec->data_len,
1916 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1917 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1918 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001919
Hanno Beckera5a2b082019-05-15 14:03:01 +01001920#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001921 /*
1922 * Add CID information
1923 */
1924 rec->cid_len = transform->out_cid_len;
1925 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1926 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001927
1928 if( rec->cid_len != 0 )
1929 {
1930 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001931 * Wrap plaintext into DTLSInnerPlaintext structure.
1932 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001933 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001934 * Note that this changes `rec->data_len`, and hence
1935 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001936 */
1937 if( ssl_cid_build_inner_plaintext( data,
1938 &rec->data_len,
1939 post_avail,
1940 rec->type ) != 0 )
1941 {
1942 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1943 }
1944
1945 rec->type = MBEDTLS_SSL_MSG_CID;
1946 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001947#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001948
Hanno Becker92c930f2019-04-29 17:31:37 +01001949 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1950
Paul Bakker5121ce52009-01-03 21:22:43 +00001951 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001952 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001953 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001954#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 if( mode == MBEDTLS_MODE_STREAM ||
1956 ( mode == MBEDTLS_MODE_CBC
1957#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001958 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001959#endif
1960 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001961 {
Hanno Becker3307b532017-12-27 21:37:21 +00001962 if( post_avail < transform->maclen )
1963 {
1964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1965 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1966 }
1967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001969 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001970 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001971 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001972 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1973 data, rec->data_len, rec->ctr, rec->type, mac );
1974 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001975 }
1976 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001977#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1979 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001980 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001981 {
Hanno Becker992b6872017-11-09 18:57:39 +00001982 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1983
Hanno Beckere83efe62019-04-29 13:52:53 +01001984 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001985
Hanno Becker3307b532017-12-27 21:37:21 +00001986 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001987 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001988 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1989 data, rec->data_len );
1990 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1991 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1992
1993 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001994 }
1995 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001996#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1999 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002000 }
2001
Hanno Becker3307b532017-12-27 21:37:21 +00002002 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2003 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002004
Hanno Becker3307b532017-12-27 21:37:21 +00002005 rec->data_len += transform->maclen;
2006 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002007 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002008 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002009#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002010
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002011 /*
2012 * Encrypt
2013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2015 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002016 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002017 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002018 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002020 "including %d bytes of padding",
2021 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002022
Hanno Becker3307b532017-12-27 21:37:21 +00002023 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2024 transform->iv_enc, transform->ivlen,
2025 data, rec->data_len,
2026 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002029 return( ret );
2030 }
2031
Hanno Becker3307b532017-12-27 21:37:21 +00002032 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2035 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002036 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002037 }
Paul Bakker68884e32013-01-07 18:20:04 +01002038 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002040
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002041#if defined(MBEDTLS_GCM_C) || \
2042 defined(MBEDTLS_CCM_C) || \
2043 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002045 mode == MBEDTLS_MODE_CCM ||
2046 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002047 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002048 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002049 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002050 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002051
Hanno Becker3307b532017-12-27 21:37:21 +00002052 /* Check that there's space for both the authentication tag
2053 * and the explicit IV before and after the record content. */
2054 if( post_avail < transform->taglen ||
2055 rec->data_offset < explicit_iv_len )
2056 {
2057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2058 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2059 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002060
Paul Bakker68884e32013-01-07 18:20:04 +01002061 /*
2062 * Generate IV
2063 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002064 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2065 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002066 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002067 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002068 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2069 explicit_iv_len );
2070 /* Prefix record content with explicit IV. */
2071 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002072 }
2073 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2074 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002075 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002076 unsigned char i;
2077
2078 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2079
2080 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002081 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002082 }
2083 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002084 {
2085 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2087 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002088 }
2089
Hanno Beckere83efe62019-04-29 13:52:53 +01002090 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002091
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002092 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2093 iv, transform->ivlen );
2094 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002095 data - explicit_iv_len, explicit_iv_len );
2096 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002097 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002099 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002100 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002101
Paul Bakker68884e32013-01-07 18:20:04 +01002102 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002103 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002104 */
Hanno Becker3307b532017-12-27 21:37:21 +00002105
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002106 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002107 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002108 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002109 data, rec->data_len, /* source */
2110 data, &rec->data_len, /* destination */
2111 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002114 return( ret );
2115 }
2116
Hanno Becker3307b532017-12-27 21:37:21 +00002117 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2118 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002119
Hanno Becker3307b532017-12-27 21:37:21 +00002120 rec->data_len += transform->taglen + explicit_iv_len;
2121 rec->data_offset -= explicit_iv_len;
2122 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002123 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002124 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2127#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002128 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002131 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002132 size_t padlen, i;
2133 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002134
Hanno Becker3307b532017-12-27 21:37:21 +00002135 /* Currently we're always using minimal padding
2136 * (up to 255 bytes would be allowed). */
2137 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2138 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002139 padlen = 0;
2140
Hanno Becker3307b532017-12-27 21:37:21 +00002141 /* Check there's enough space in the buffer for the padding. */
2142 if( post_avail < padlen + 1 )
2143 {
2144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2145 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2146 }
2147
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002149 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002150
Hanno Becker3307b532017-12-27 21:37:21 +00002151 rec->data_len += padlen + 1;
2152 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002155 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002156 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2157 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002158 */
Hanno Becker3307b532017-12-27 21:37:21 +00002159 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002160 {
Hanno Becker3307b532017-12-27 21:37:21 +00002161 if( f_rng == NULL )
2162 {
2163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2164 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2165 }
2166
2167 if( rec->data_offset < transform->ivlen )
2168 {
2169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2170 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2171 }
2172
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002173 /*
2174 * Generate IV
2175 */
Hanno Becker3307b532017-12-27 21:37:21 +00002176 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002177 if( ret != 0 )
2178 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002179
Hanno Becker3307b532017-12-27 21:37:21 +00002180 memcpy( data - transform->ivlen, transform->iv_enc,
2181 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002182
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002183 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002187 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002188 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002189 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002190
Hanno Becker3307b532017-12-27 21:37:21 +00002191 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2192 transform->iv_enc,
2193 transform->ivlen,
2194 data, rec->data_len,
2195 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002198 return( ret );
2199 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002200
Hanno Becker3307b532017-12-27 21:37:21 +00002201 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2204 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002205 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002208 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002209 {
2210 /*
2211 * Save IV in SSL3 and TLS1
2212 */
Hanno Becker3307b532017-12-27 21:37:21 +00002213 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2214 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002215 }
Hanno Becker3307b532017-12-27 21:37:21 +00002216 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002217#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002218 {
2219 data -= transform->ivlen;
2220 rec->data_offset -= transform->ivlen;
2221 rec->data_len += transform->ivlen;
2222 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002225 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002226 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002227 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2228
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002229 /*
2230 * MAC(MAC_write_key, seq_num +
2231 * TLSCipherText.type +
2232 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002233 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002234 * IV + // except for TLS 1.0
2235 * ENC(content + padding + padding_length));
2236 */
Hanno Becker3307b532017-12-27 21:37:21 +00002237
2238 if( post_avail < transform->maclen)
2239 {
2240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2241 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2242 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002243
Hanno Beckere83efe62019-04-29 13:52:53 +01002244 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002247 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002248 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002249
Hanno Becker3307b532017-12-27 21:37:21 +00002250 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002251 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002252 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2253 data, rec->data_len );
2254 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2255 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002256
Hanno Becker3307b532017-12-27 21:37:21 +00002257 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002258
Hanno Becker3307b532017-12-27 21:37:21 +00002259 rec->data_len += transform->maclen;
2260 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002261 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002262 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002264 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002265 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002267 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2270 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002271 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002272
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002273 /* Make extra sure authentication was performed, exactly once */
2274 if( auth_done != 1 )
2275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2277 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002278 }
2279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002281
2282 return( 0 );
2283}
2284
Hanno Becker611a83b2018-01-03 14:27:32 +00002285int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2286 mbedtls_ssl_transform *transform,
2287 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002288{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002289 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002291 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002292#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002293 size_t padlen = 0, correct = 1;
2294#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002295 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002296 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002297 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002298
Hanno Becker611a83b2018-01-03 14:27:32 +00002299#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002300 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002301 ((void) ssl);
2302#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002305 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002306 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2308 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2309 }
2310 if( rec == NULL ||
2311 rec->buf == NULL ||
2312 rec->buf_len < rec->data_offset ||
2313 rec->buf_len - rec->data_offset < rec->data_len )
2314 {
2315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002317 }
2318
Hanno Becker4c6876b2017-12-27 21:28:58 +00002319 data = rec->buf + rec->data_offset;
2320 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002321
Hanno Beckera5a2b082019-05-15 14:03:01 +01002322#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002323 /*
2324 * Match record's CID with incoming CID.
2325 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002326 if( rec->cid_len != transform->in_cid_len ||
2327 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2328 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002329 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002330 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002331#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2334 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002335 {
2336 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002337 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2338 transform->iv_dec,
2339 transform->ivlen,
2340 data, rec->data_len,
2341 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002344 return( ret );
2345 }
2346
Hanno Becker4c6876b2017-12-27 21:28:58 +00002347 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2350 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002351 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002352 }
Paul Bakker68884e32013-01-07 18:20:04 +01002353 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002355#if defined(MBEDTLS_GCM_C) || \
2356 defined(MBEDTLS_CCM_C) || \
2357 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002359 mode == MBEDTLS_MODE_CCM ||
2360 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002361 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002362 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002363 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002364
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002365 /*
2366 * Compute and update sizes
2367 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002368 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002371 "+ taglen (%d)", rec->data_len,
2372 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002374 }
Paul Bakker68884e32013-01-07 18:20:04 +01002375
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002376 /*
2377 * Prepare IV
2378 */
2379 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2380 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002381 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002382 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002383 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002384
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002385 }
2386 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2387 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002388 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002389 unsigned char i;
2390
2391 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2392
2393 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002394 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002395 }
2396 else
2397 {
2398 /* Reminder if we ever add an AEAD mode with a different size */
2399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2400 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2401 }
2402
Hanno Becker4c6876b2017-12-27 21:28:58 +00002403 data += explicit_iv_len;
2404 rec->data_offset += explicit_iv_len;
2405 rec->data_len -= explicit_iv_len + transform->taglen;
2406
Hanno Beckere83efe62019-04-29 13:52:53 +01002407 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002408 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002409 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002410
2411 memcpy( transform->iv_dec + transform->fixed_ivlen,
2412 data - explicit_iv_len, explicit_iv_len );
2413
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002414 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002415 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002416 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002417
Paul Bakker68884e32013-01-07 18:20:04 +01002418
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002419 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002420 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002421 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002422 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2423 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002424 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002425 data, rec->data_len,
2426 data, &olen,
2427 data + rec->data_len,
2428 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2433 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002434
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002435 return( ret );
2436 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002437 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002438
Hanno Becker4c6876b2017-12-27 21:28:58 +00002439 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2442 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002443 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002444 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002445 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2447#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002448 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002451 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002452
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 /*
Paul Bakker45829992013-01-03 14:52:21 +01002454 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002457 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2458 {
2459 /* The ciphertext is prefixed with the CBC IV. */
2460 minlen += transform->ivlen;
2461 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002462#endif
Paul Bakker45829992013-01-03 14:52:21 +01002463
Hanno Becker4c6876b2017-12-27 21:28:58 +00002464 /* Size considerations:
2465 *
2466 * - The CBC cipher text must not be empty and hence
2467 * at least of size transform->ivlen.
2468 *
2469 * Together with the potential IV-prefix, this explains
2470 * the first of the two checks below.
2471 *
2472 * - The record must contain a MAC, either in plain or
2473 * encrypted, depending on whether Encrypt-then-MAC
2474 * is used or not.
2475 * - If it is, the message contains the IV-prefix,
2476 * the CBC ciphertext, and the MAC.
2477 * - If it is not, the padded plaintext, and hence
2478 * the CBC ciphertext, has at least length maclen + 1
2479 * because there is at least the padding length byte.
2480 *
2481 * As the CBC ciphertext is not empty, both cases give the
2482 * lower bound minlen + maclen + 1 on the record size, which
2483 * we test for in the second check below.
2484 */
2485 if( rec->data_len < minlen + transform->ivlen ||
2486 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002489 "+ 1 ) ( + expl IV )", rec->data_len,
2490 transform->ivlen,
2491 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002493 }
2494
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002495 /*
2496 * Authenticate before decrypt if enabled
2497 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002499 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002500 {
Hanno Becker992b6872017-11-09 18:57:39 +00002501 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002504
Hanno Becker4c6876b2017-12-27 21:28:58 +00002505 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2506 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002507
Hanno Beckere83efe62019-04-29 13:52:53 +01002508 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002509
Hanno Beckere83efe62019-04-29 13:52:53 +01002510 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2511 add_data_len );
2512 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2513 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002514 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2515 data, rec->data_len );
2516 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2517 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002518
Hanno Becker4c6876b2017-12-27 21:28:58 +00002519 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2520 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002521 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002522 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002523
Hanno Becker4c6876b2017-12-27 21:28:58 +00002524 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2525 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002529 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002530 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002531 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002533
2534 /*
2535 * Check length sanity
2536 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002537 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002540 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002542 }
2543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002545 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002546 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002547 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002548 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002549 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002550 /* This is safe because data_len >= minlen + maclen + 1 initially,
2551 * and at this point we have at most subtracted maclen (note that
2552 * minlen == transform->ivlen here). */
2553 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002554
Hanno Becker4c6876b2017-12-27 21:28:58 +00002555 data += transform->ivlen;
2556 rec->data_offset += transform->ivlen;
2557 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002558 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002560
Hanno Becker4c6876b2017-12-27 21:28:58 +00002561 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2562 transform->iv_dec, transform->ivlen,
2563 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002566 return( ret );
2567 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002568
Hanno Becker4c6876b2017-12-27 21:28:58 +00002569 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002573 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002576 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002577 {
2578 /*
2579 * Save IV in SSL3 and TLS1
2580 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002581 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2582 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002584#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002585
Hanno Becker4c6876b2017-12-27 21:28:58 +00002586 /* Safe since data_len >= minlen + maclen + 1, so after having
2587 * subtracted at most minlen and maclen up to this point,
2588 * data_len > 0. */
2589 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002590
Hanno Becker4c6876b2017-12-27 21:28:58 +00002591 if( auth_done == 1 )
2592 {
2593 correct *= ( rec->data_len >= padlen + 1 );
2594 padlen *= ( rec->data_len >= padlen + 1 );
2595 }
2596 else
Paul Bakker45829992013-01-03 14:52:21 +01002597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002599 if( rec->data_len < transform->maclen + padlen + 1 )
2600 {
2601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2602 rec->data_len,
2603 transform->maclen,
2604 padlen + 1 ) );
2605 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002606#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002607
2608 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2609 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002610 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002611
Hanno Becker4c6876b2017-12-27 21:28:58 +00002612 padlen++;
2613
2614 /* Regardless of the validity of the padding,
2615 * we have data_len >= padlen here. */
2616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002618 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002620 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622#if defined(MBEDTLS_SSL_DEBUG_ALL)
2623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002624 "should be no more than %d",
2625 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002626#endif
Paul Bakker45829992013-01-03 14:52:21 +01002627 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002628 }
2629 }
2630 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2632#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2633 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002634 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002635 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002636 /* The padding check involves a series of up to 256
2637 * consecutive memory reads at the end of the record
2638 * plaintext buffer. In order to hide the length and
2639 * validity of the padding, always perform exactly
2640 * `min(256,plaintext_len)` reads (but take into account
2641 * only the last `padlen` bytes for the padding check). */
2642 size_t pad_count = 0;
2643 size_t real_count = 0;
2644 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002645
Hanno Becker4c6876b2017-12-27 21:28:58 +00002646 /* Index of first padding byte; it has been ensured above
2647 * that the subtraction is safe. */
2648 size_t const padding_idx = rec->data_len - padlen;
2649 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2650 size_t const start_idx = rec->data_len - num_checks;
2651 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002652
Hanno Becker4c6876b2017-12-27 21:28:58 +00002653 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002654 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002655 real_count |= ( idx >= padding_idx );
2656 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002657 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002658 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002661 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002663#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002664 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002665 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002666 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2668 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2671 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002672 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002673
Hanno Becker4c6876b2017-12-27 21:28:58 +00002674 /* If the padding was found to be invalid, padlen == 0
2675 * and the subtraction is safe. If the padding was found valid,
2676 * padlen hasn't been changed and the previous assertion
2677 * data_len >= padlen still holds. */
2678 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002679 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002680 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002682 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2685 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002686 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002687
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002688#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002690 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002691#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002692
2693 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002694 * Authenticate if not done yet.
2695 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002697#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002698 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002699 {
Hanno Becker992b6872017-11-09 18:57:39 +00002700 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002701
Hanno Becker4c6876b2017-12-27 21:28:58 +00002702 /* If the initial value of padlen was such that
2703 * data_len < maclen + padlen + 1, then padlen
2704 * got reset to 1, and the initial check
2705 * data_len >= minlen + maclen + 1
2706 * guarantees that at this point we still
2707 * have at least data_len >= maclen.
2708 *
2709 * If the initial value of padlen was such that
2710 * data_len >= maclen + padlen + 1, then we have
2711 * subtracted either padlen + 1 (if the padding was correct)
2712 * or 0 (if the padding was incorrect) since then,
2713 * hence data_len >= maclen in any case.
2714 */
2715 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
Hanno Beckere83efe62019-04-29 13:52:53 +01002717 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002720 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002721 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002722 ssl_mac( &transform->md_ctx_dec,
2723 transform->mac_dec,
2724 data, rec->data_len,
2725 rec->ctr, rec->type,
2726 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002727 }
2728 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2730#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2731 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002732 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002733 {
2734 /*
2735 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002736 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002737 *
2738 * Known timing attacks:
2739 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2740 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002741 * To compensate for different timings for the MAC calculation
2742 * depending on how much padding was removed (which is determined
2743 * by padlen), process extra_run more blocks through the hash
2744 * function.
2745 *
2746 * The formula in the paper is
2747 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2748 * where L1 is the size of the header plus the decrypted message
2749 * plus CBC padding and L2 is the size of the header plus the
2750 * decrypted message. This is for an underlying hash function
2751 * with 64-byte blocks.
2752 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2753 * correctly. We round down instead of up, so -56 is the correct
2754 * value for our calculations instead of -55.
2755 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002756 * Repeat the formula rather than defining a block_size variable.
2757 * This avoids requiring division by a variable at runtime
2758 * (which would be marginally less efficient and would require
2759 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002760 */
2761 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002762 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002763
2764 /*
2765 * The next two sizes are the minimum and maximum values of
2766 * in_msglen over all padlen values.
2767 *
2768 * They're independent of padlen, since we previously did
2769 * in_msglen -= padlen.
2770 *
2771 * Note that max_len + maclen is never more than the buffer
2772 * length, as we previously did in_msglen -= maclen too.
2773 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002774 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002775 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2776
Hanno Becker4c6876b2017-12-27 21:28:58 +00002777 memset( tmp, 0, sizeof( tmp ) );
2778
2779 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002780 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002781#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2782 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002783 case MBEDTLS_MD_MD5:
2784 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002785 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002786 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002787 extra_run =
2788 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2789 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002790 break;
2791#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002792#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002793 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002794 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002795 extra_run =
2796 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2797 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002798 break;
2799#endif
2800 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002802 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2803 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002804
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002805 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002806
Hanno Beckere83efe62019-04-29 13:52:53 +01002807 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2808 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002809 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2810 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002811 /* Make sure we access everything even when padlen > 0. This
2812 * makes the synchronisation requirements for just-in-time
2813 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002814 ssl_read_memory( data + rec->data_len, padlen );
2815 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002816
2817 /* Call mbedtls_md_process at least once due to cache attacks
2818 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002819 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002820 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002821
Hanno Becker4c6876b2017-12-27 21:28:58 +00002822 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002823
2824 /* Make sure we access all the memory that could contain the MAC,
2825 * before we check it in the next code block. This makes the
2826 * synchronisation requirements for just-in-time Prime+Probe
2827 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002828 ssl_read_memory( data + min_len,
2829 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002830 }
2831 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2833 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2836 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002837 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002838
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002839#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002840 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2841 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002842#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002843
Hanno Becker4c6876b2017-12-27 21:28:58 +00002844 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2845 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847#if defined(MBEDTLS_SSL_DEBUG_ALL)
2848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002849#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002850 correct = 0;
2851 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002852 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002853 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002854
2855 /*
2856 * Finally check the correct flag
2857 */
2858 if( correct == 0 )
2859 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002860#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002861
2862 /* Make extra sure authentication was performed, exactly once */
2863 if( auth_done != 1 )
2864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2866 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002867 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002868
Hanno Beckera5a2b082019-05-15 14:03:01 +01002869#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002870 if( rec->cid_len != 0 )
2871 {
2872 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2873 &rec->type );
2874 if( ret != 0 )
2875 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2876 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002877#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002880
2881 return( 0 );
2882}
2883
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002884#undef MAC_NONE
2885#undef MAC_PLAINTEXT
2886#undef MAC_CIPHERTEXT
2887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002889/*
2890 * Compression/decompression functions
2891 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002893{
2894 int ret;
2895 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002896 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002897 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002898 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002901
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002902 if( len_pre == 0 )
2903 return( 0 );
2904
Paul Bakker2770fbd2012-07-03 13:30:23 +00002905 memcpy( msg_pre, ssl->out_msg, len_pre );
2906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002908 ssl->out_msglen ) );
2909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002911 ssl->out_msg, ssl->out_msglen );
2912
Paul Bakker48916f92012-09-16 19:57:18 +00002913 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2914 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2915 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002916 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002917
Paul Bakker48916f92012-09-16 19:57:18 +00002918 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002919 if( ret != Z_OK )
2920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2922 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002923 }
2924
Angus Grattond8213d02016-05-25 20:56:48 +10002925 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002926 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002929 ssl->out_msglen ) );
2930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002932 ssl->out_msg, ssl->out_msglen );
2933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002935
2936 return( 0 );
2937}
2938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002940{
2941 int ret;
2942 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002943 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002944 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002945 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002948
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002949 if( len_pre == 0 )
2950 return( 0 );
2951
Paul Bakker2770fbd2012-07-03 13:30:23 +00002952 memcpy( msg_pre, ssl->in_msg, len_pre );
2953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002955 ssl->in_msglen ) );
2956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002958 ssl->in_msg, ssl->in_msglen );
2959
Paul Bakker48916f92012-09-16 19:57:18 +00002960 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2961 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2962 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002963 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002964 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002965
Paul Bakker48916f92012-09-16 19:57:18 +00002966 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002967 if( ret != Z_OK )
2968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2970 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002971 }
2972
Angus Grattond8213d02016-05-25 20:56:48 +10002973 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002974 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002977 ssl->in_msglen ) );
2978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002980 ssl->in_msg, ssl->in_msglen );
2981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002983
2984 return( 0 );
2985}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2989static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991#if defined(MBEDTLS_SSL_PROTO_DTLS)
2992static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002993{
2994 /* If renegotiation is not enforced, retransmit until we would reach max
2995 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002996 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002997 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002998 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002999 unsigned char doublings = 1;
3000
3001 while( ratio != 0 )
3002 {
3003 ++doublings;
3004 ratio >>= 1;
3005 }
3006
3007 if( ++ssl->renego_records_seen > doublings )
3008 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003010 return( 0 );
3011 }
3012 }
3013
3014 return( ssl_write_hello_request( ssl ) );
3015}
3016#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003018
Paul Bakker5121ce52009-01-03 21:22:43 +00003019/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003020 * Fill the input message buffer by appending data to it.
3021 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003022 *
3023 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3024 * available (from this read and/or a previous one). Otherwise, an error code
3025 * is returned (possibly EOF or WANT_READ).
3026 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003027 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3028 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3029 * since we always read a whole datagram at once.
3030 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003031 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003032 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003033 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003035{
Paul Bakker23986e52011-04-24 08:57:21 +00003036 int ret;
3037 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003040
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003041 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003044 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003046 }
3047
Angus Grattond8213d02016-05-25 20:56:48 +10003048 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3051 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003052 }
3053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003055 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003056 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003057 uint32_t timeout;
3058
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003059 /* Just to be sure */
3060 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3061 {
3062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3063 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3065 }
3066
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003067 /*
3068 * The point is, we need to always read a full datagram at once, so we
3069 * sometimes read more then requested, and handle the additional data.
3070 * It could be the rest of the current record (while fetching the
3071 * header) and/or some other records in the same datagram.
3072 */
3073
3074 /*
3075 * Move to the next record in the already read datagram if applicable
3076 */
3077 if( ssl->next_record_offset != 0 )
3078 {
3079 if( ssl->in_left < ssl->next_record_offset )
3080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3082 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003083 }
3084
3085 ssl->in_left -= ssl->next_record_offset;
3086
3087 if( ssl->in_left != 0 )
3088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003090 ssl->next_record_offset ) );
3091 memmove( ssl->in_hdr,
3092 ssl->in_hdr + ssl->next_record_offset,
3093 ssl->in_left );
3094 }
3095
3096 ssl->next_record_offset = 0;
3097 }
3098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003100 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003101
3102 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003103 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003104 */
3105 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003108 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003109 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003110
3111 /*
3112 * A record can't be split accross datagrams. If we need to read but
3113 * are not at the beginning of a new record, the caller did something
3114 * wrong.
3115 */
3116 if( ssl->in_left != 0 )
3117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3119 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003120 }
3121
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003122 /*
3123 * Don't even try to read if time's out already.
3124 * This avoids by-passing the timer when repeatedly receiving messages
3125 * that will end up being dropped.
3126 */
3127 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003128 {
3129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003130 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003131 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003132 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003133 {
Angus Grattond8213d02016-05-25 20:56:48 +10003134 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003137 timeout = ssl->handshake->retransmit_timeout;
3138 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003139 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003142
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003143 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003144 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3145 timeout );
3146 else
3147 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003150
3151 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003153 }
3154
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003155 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003158 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003161 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003162 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003165 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003166 }
3167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003171 return( ret );
3172 }
3173
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003174 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003175 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003177 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003179 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003180 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003183 return( ret );
3184 }
3185
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003186 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003187 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003189 }
3190
Paul Bakker5121ce52009-01-03 21:22:43 +00003191 if( ret < 0 )
3192 return( ret );
3193
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003194 ssl->in_left = ret;
3195 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003196 MBEDTLS_SSL_TRANSPORT_ELSE
3197#endif /* MBEDTLS_SSL_PROTO_DTLS */
3198#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003201 ssl->in_left, nb_want ) );
3202
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003203 while( ssl->in_left < nb_want )
3204 {
3205 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003206
3207 if( ssl_check_timer( ssl ) != 0 )
3208 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3209 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003210 {
3211 if( ssl->f_recv_timeout != NULL )
3212 {
3213 ret = ssl->f_recv_timeout( ssl->p_bio,
3214 ssl->in_hdr + ssl->in_left, len,
3215 ssl->conf->read_timeout );
3216 }
3217 else
3218 {
3219 ret = ssl->f_recv( ssl->p_bio,
3220 ssl->in_hdr + ssl->in_left, len );
3221 }
3222 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003225 ssl->in_left, nb_want ) );
3226 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003227
3228 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003230
3231 if( ret < 0 )
3232 return( ret );
3233
mohammad160352aecb92018-03-28 23:41:40 -07003234 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003235 {
Darryl Green11999bb2018-03-13 15:22:58 +00003236 MBEDTLS_SSL_DEBUG_MSG( 1,
3237 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003238 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003239 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3240 }
3241
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003242 ssl->in_left += ret;
3243 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003244 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003245#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003248
3249 return( 0 );
3250}
3251
3252/*
3253 * Flush any data not yet written
3254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003256{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003257 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003258 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003261
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003262 if( ssl->f_send == NULL )
3263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003265 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003267 }
3268
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003269 /* Avoid incrementing counter if data is flushed */
3270 if( ssl->out_left == 0 )
3271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003273 return( 0 );
3274 }
3275
Paul Bakker5121ce52009-01-03 21:22:43 +00003276 while( ssl->out_left > 0 )
3277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003279 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003280
Hanno Becker2b1e3542018-08-06 11:19:13 +01003281 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003282 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003285
3286 if( ret <= 0 )
3287 return( ret );
3288
mohammad160352aecb92018-03-28 23:41:40 -07003289 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003290 {
Darryl Green11999bb2018-03-13 15:22:58 +00003291 MBEDTLS_SSL_DEBUG_MSG( 1,
3292 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003293 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003294 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3295 }
3296
Paul Bakker5121ce52009-01-03 21:22:43 +00003297 ssl->out_left -= ret;
3298 }
3299
Hanno Becker2b1e3542018-08-06 11:19:13 +01003300#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003301 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003302 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003303 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003304 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003305 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003306#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003307#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003308 {
3309 ssl->out_hdr = ssl->out_buf + 8;
3310 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003311#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003312 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003315
3316 return( 0 );
3317}
3318
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003319/*
3320 * Functions to handle the DTLS retransmission state machine
3321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003322#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003323/*
3324 * Append current handshake message to current outgoing flight
3325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003327{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3330 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3331 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003332
3333 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003334 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003335 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003338 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003339 }
3340
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003341 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003342 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003345 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003346 }
3347
3348 /* Copy current handshake message with headers */
3349 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3350 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003351 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003352 msg->next = NULL;
3353
3354 /* Append to the current flight */
3355 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003356 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003357 else
3358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003360 while( cur->next != NULL )
3361 cur = cur->next;
3362 cur->next = msg;
3363 }
3364
Hanno Becker3b235902018-08-06 09:54:53 +01003365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003366 return( 0 );
3367}
3368
3369/*
3370 * Free the current flight of handshake messages
3371 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003374 mbedtls_ssl_flight_item *cur = flight;
3375 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003376
3377 while( cur != NULL )
3378 {
3379 next = cur->next;
3380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 mbedtls_free( cur->p );
3382 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003383
3384 cur = next;
3385 }
3386}
3387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3389static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003390#endif
3391
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003392/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003393 * Swap transform_out and out_ctr with the alternative ones
3394 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003396{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003398 unsigned char tmp_out_ctr[8];
3399
3400 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003403 return;
3404 }
3405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003407
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003408 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003409 tmp_transform = ssl->transform_out;
3410 ssl->transform_out = ssl->handshake->alt_transform_out;
3411 ssl->handshake->alt_transform_out = tmp_transform;
3412
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003413 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003414 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3415 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003416 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003417
3418 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003419 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3422 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3427 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003428 }
3429 }
3430#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003431}
3432
3433/*
3434 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003435 */
3436int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3437{
3438 int ret = 0;
3439
3440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3441
3442 ret = mbedtls_ssl_flight_transmit( ssl );
3443
3444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3445
3446 return( ret );
3447}
3448
3449/*
3450 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003451 *
3452 * Need to remember the current message in case flush_output returns
3453 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003454 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003455 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003456int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003457{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003458 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003462 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003464
3465 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003466 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003467 ssl_swap_epochs( ssl );
3468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003470 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003471
3472 while( ssl->handshake->cur_msg != NULL )
3473 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003474 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003475 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003476
Hanno Beckere1dcb032018-08-17 16:47:58 +01003477 int const is_finished =
3478 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3479 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3480
Hanno Becker04da1892018-08-14 13:22:10 +01003481 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3482 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3483
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003484 /* Swap epochs before sending Finished: we can't do it after
3485 * sending ChangeCipherSpec, in case write returns WANT_READ.
3486 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003487 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003488 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003490 ssl_swap_epochs( ssl );
3491 }
3492
Hanno Becker67bc7c32018-08-06 11:33:50 +01003493 ret = ssl_get_remaining_payload_in_datagram( ssl );
3494 if( ret < 0 )
3495 return( ret );
3496 max_frag_len = (size_t) ret;
3497
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003498 /* CCS is copied as is, while HS messages may need fragmentation */
3499 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3500 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003501 if( max_frag_len == 0 )
3502 {
3503 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3504 return( ret );
3505
3506 continue;
3507 }
3508
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003509 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003510 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003511 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003512
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003513 /* Update position inside current message */
3514 ssl->handshake->cur_msg_p += cur->len;
3515 }
3516 else
3517 {
3518 const unsigned char * const p = ssl->handshake->cur_msg_p;
3519 const size_t hs_len = cur->len - 12;
3520 const size_t frag_off = p - ( cur->p + 12 );
3521 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003522 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003523
Hanno Beckere1dcb032018-08-17 16:47:58 +01003524 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003525 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003526 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003527 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003528
Hanno Becker67bc7c32018-08-06 11:33:50 +01003529 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3530 return( ret );
3531
3532 continue;
3533 }
3534 max_hs_frag_len = max_frag_len - 12;
3535
3536 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3537 max_hs_frag_len : rem_len;
3538
3539 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003540 {
3541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003542 (unsigned) cur_hs_frag_len,
3543 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003544 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003545
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003546 /* Messages are stored with handshake headers as if not fragmented,
3547 * copy beginning of headers then fill fragmentation fields.
3548 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3549 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003550
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003551 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3552 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3553 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3554
Hanno Becker67bc7c32018-08-06 11:33:50 +01003555 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3556 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3557 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003558
3559 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3560
Hanno Becker3f7b9732018-08-28 09:53:25 +01003561 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003562 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3563 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003564 ssl->out_msgtype = cur->type;
3565
3566 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003567 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003568 }
3569
3570 /* If done with the current message move to the next one if any */
3571 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3572 {
3573 if( cur->next != NULL )
3574 {
3575 ssl->handshake->cur_msg = cur->next;
3576 ssl->handshake->cur_msg_p = cur->next->p + 12;
3577 }
3578 else
3579 {
3580 ssl->handshake->cur_msg = NULL;
3581 ssl->handshake->cur_msg_p = NULL;
3582 }
3583 }
3584
3585 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003586 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003589 return( ret );
3590 }
3591 }
3592
Hanno Becker67bc7c32018-08-06 11:33:50 +01003593 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3594 return( ret );
3595
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003596 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003597 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3598 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003599 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003602 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3603 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003604
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003606
3607 return( 0 );
3608}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003609
3610/*
3611 * To be called when the last message of an incoming flight is received.
3612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003614{
3615 /* We won't need to resend that one any more */
3616 ssl_flight_free( ssl->handshake->flight );
3617 ssl->handshake->flight = NULL;
3618 ssl->handshake->cur_msg = NULL;
3619
3620 /* The next incoming flight will start with this msg_seq */
3621 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3622
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003623 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003624 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003625
Hanno Becker0271f962018-08-16 13:23:47 +01003626 /* Clear future message buffering structure. */
3627 ssl_buffering_free( ssl );
3628
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003629 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003630 ssl_set_timer( ssl, 0 );
3631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3633 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003636 }
3637 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003639}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003640
3641/*
3642 * To be called when the last message of an outgoing flight is send.
3643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003645{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003646 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003647 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3650 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003653 }
3654 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003656}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003658
Paul Bakker5121ce52009-01-03 21:22:43 +00003659/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003660 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003661 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003662
3663/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003664 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003665 *
3666 * - fill in handshake headers
3667 * - update handshake checksum
3668 * - DTLS: save message for resending
3669 * - then pass to the record layer
3670 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003671 * DTLS: except for HelloRequest, messages are only queued, and will only be
3672 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003673 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003674 * Inputs:
3675 * - ssl->out_msglen: 4 + actual handshake message len
3676 * (4 is the size of handshake headers for TLS)
3677 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3678 * - ssl->out_msg + 4: the handshake message body
3679 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003680 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003681 * - ssl->out_msglen: the length of the record contents
3682 * (including handshake headers but excluding record headers)
3683 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003684 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003685int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003686{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003687 int ret;
3688 const size_t hs_len = ssl->out_msglen - 4;
3689 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003690
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3692
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003693 /*
3694 * Sanity checks
3695 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003696 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003697 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3698 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003699 /* In SSLv3, the client might send a NoCertificate alert. */
3700#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3701 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3702 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3703 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3704#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3705 {
3706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3707 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3708 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003709 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003710
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003711 /* Whenever we send anything different from a
3712 * HelloRequest we should be in a handshake - double check. */
3713 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3714 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003715 ssl->handshake == NULL )
3716 {
3717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3718 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3719 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003722 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003723 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003725 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3727 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003728 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003729#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003730
Hanno Beckerb50a2532018-08-06 11:52:54 +01003731 /* Double-check that we did not exceed the bounds
3732 * of the outgoing record buffer.
3733 * This should never fail as the various message
3734 * writing functions must obey the bounds of the
3735 * outgoing record buffer, but better be safe.
3736 *
3737 * Note: We deliberately do not check for the MTU or MFL here.
3738 */
3739 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3740 {
3741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3742 "size %u, maximum %u",
3743 (unsigned) ssl->out_msglen,
3744 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3745 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3746 }
3747
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003748 /*
3749 * Fill handshake headers
3750 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003751 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003752 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003753 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3754 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3755 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003756
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003757 /*
3758 * DTLS has additional fields in the Handshake layer,
3759 * between the length field and the actual payload:
3760 * uint16 message_seq;
3761 * uint24 fragment_offset;
3762 * uint24 fragment_length;
3763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003765 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003766 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003767 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003768 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003769 {
3770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3771 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003772 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003773 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003774 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3775 }
3776
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003777 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003778 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003779
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003780 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003781 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003782 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003783 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3784 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3785 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003786 }
3787 else
3788 {
3789 ssl->out_msg[4] = 0;
3790 ssl->out_msg[5] = 0;
3791 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003792
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003793 /* Handshake hashes are computed without fragmentation,
3794 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003795 memset( ssl->out_msg + 6, 0x00, 3 );
3796 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003797 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003798#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003799
Hanno Becker0207e532018-08-28 10:28:28 +01003800 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003801 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3802 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003803 }
3804
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003805 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003806#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003807 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003808 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3809 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003810 {
3811 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003813 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003814 return( ret );
3815 }
3816 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003817 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003818#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003819 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003820 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003821 {
3822 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3823 return( ret );
3824 }
3825 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003826
3827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3828
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003829 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003830}
3831
3832/*
3833 * Record layer functions
3834 */
3835
3836/*
3837 * Write current record.
3838 *
3839 * Uses:
3840 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3841 * - ssl->out_msglen: length of the record content (excl headers)
3842 * - ssl->out_msg: record content
3843 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003844int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003845{
3846 int ret, done = 0;
3847 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003848 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003849
3850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003853 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003855 {
3856 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003859 return( ret );
3860 }
3861
3862 len = ssl->out_msglen;
3863 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003864#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3867 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003871 ret = mbedtls_ssl_hw_record_write( ssl );
3872 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3875 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003876 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003877
3878 if( ret == 0 )
3879 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003880 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003881#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003882 if( !done )
3883 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003884 unsigned i;
3885 size_t protected_record_size;
3886
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003887 /* Skip writing the record content type to after the encryption,
3888 * as it may change when using the CID extension. */
3889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003891 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003892
Hanno Becker19859472018-08-06 09:40:20 +01003893 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003894 ssl->out_len[0] = (unsigned char)( len >> 8 );
3895 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003896
Paul Bakker48916f92012-09-16 19:57:18 +00003897 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003898 {
Hanno Becker3307b532017-12-27 21:37:21 +00003899 mbedtls_record rec;
3900
3901 rec.buf = ssl->out_iv;
3902 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3903 ( ssl->out_iv - ssl->out_buf );
3904 rec.data_len = ssl->out_msglen;
3905 rec.data_offset = ssl->out_msg - rec.buf;
3906
3907 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3908 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3909 ssl->conf->transport, rec.ver );
3910 rec.type = ssl->out_msgtype;
3911
Hanno Beckera5a2b082019-05-15 14:03:01 +01003912#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003913 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003914 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003915#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003916
Hanno Becker611a83b2018-01-03 14:27:32 +00003917 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003918 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003920 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003921 return( ret );
3922 }
3923
Hanno Becker3307b532017-12-27 21:37:21 +00003924 if( rec.data_offset != 0 )
3925 {
3926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3927 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3928 }
3929
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003930 /* Update the record content type and CID. */
3931 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003932#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003933 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003934#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003935 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003936 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3937 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003938 }
3939
Hanno Becker43395762019-05-03 14:46:38 +01003940 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003941
3942#if defined(MBEDTLS_SSL_PROTO_DTLS)
3943 /* In case of DTLS, double-check that we don't exceed
3944 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003945 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01003946 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003947 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003948 if( ret < 0 )
3949 return( ret );
3950
3951 if( protected_record_size > (size_t) ret )
3952 {
3953 /* Should never happen */
3954 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3955 }
3956 }
3957#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003958
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003959 /* Now write the potentially updated record content type. */
3960 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003962 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003963 "version = [%d:%d], msglen = %d",
3964 ssl->out_hdr[0], ssl->out_hdr[1],
3965 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003968 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003969
3970 ssl->out_left += protected_record_size;
3971 ssl->out_hdr += protected_record_size;
3972 ssl_update_out_pointers( ssl, ssl->transform_out );
3973
Hanno Becker04484622018-08-06 09:49:38 +01003974 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3975 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3976 break;
3977
3978 /* The loop goes to its end iff the counter is wrapping */
3979 if( i == ssl_ep_len( ssl ) )
3980 {
3981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3982 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3983 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003984 }
3985
Hanno Becker67bc7c32018-08-06 11:33:50 +01003986#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003987 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01003988 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003989 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003990 size_t remaining;
3991 ret = ssl_get_remaining_payload_in_datagram( ssl );
3992 if( ret < 0 )
3993 {
3994 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3995 ret );
3996 return( ret );
3997 }
3998
3999 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004000 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004001 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004002 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004003 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004004 else
4005 {
Hanno Becker513815a2018-08-20 11:56:09 +01004006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004007 }
4008 }
4009#endif /* MBEDTLS_SSL_PROTO_DTLS */
4010
4011 if( ( flush == SSL_FORCE_FLUSH ) &&
4012 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004014 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004015 return( ret );
4016 }
4017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004019
4020 return( 0 );
4021}
4022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004023#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004024
4025static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4026{
4027 if( ssl->in_msglen < ssl->in_hslen ||
4028 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4029 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4030 {
4031 return( 1 );
4032 }
4033 return( 0 );
4034}
Hanno Becker44650b72018-08-16 12:51:11 +01004035
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004036static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004037{
4038 return( ( ssl->in_msg[9] << 16 ) |
4039 ( ssl->in_msg[10] << 8 ) |
4040 ssl->in_msg[11] );
4041}
4042
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004043static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004044{
4045 return( ( ssl->in_msg[6] << 16 ) |
4046 ( ssl->in_msg[7] << 8 ) |
4047 ssl->in_msg[8] );
4048}
4049
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004050static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004051{
4052 uint32_t msg_len, frag_off, frag_len;
4053
4054 msg_len = ssl_get_hs_total_len( ssl );
4055 frag_off = ssl_get_hs_frag_off( ssl );
4056 frag_len = ssl_get_hs_frag_len( ssl );
4057
4058 if( frag_off > msg_len )
4059 return( -1 );
4060
4061 if( frag_len > msg_len - frag_off )
4062 return( -1 );
4063
4064 if( frag_len + 12 > ssl->in_msglen )
4065 return( -1 );
4066
4067 return( 0 );
4068}
4069
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004070/*
4071 * Mark bits in bitmask (used for DTLS HS reassembly)
4072 */
4073static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4074{
4075 unsigned int start_bits, end_bits;
4076
4077 start_bits = 8 - ( offset % 8 );
4078 if( start_bits != 8 )
4079 {
4080 size_t first_byte_idx = offset / 8;
4081
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004082 /* Special case */
4083 if( len <= start_bits )
4084 {
4085 for( ; len != 0; len-- )
4086 mask[first_byte_idx] |= 1 << ( start_bits - len );
4087
4088 /* Avoid potential issues with offset or len becoming invalid */
4089 return;
4090 }
4091
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004092 offset += start_bits; /* Now offset % 8 == 0 */
4093 len -= start_bits;
4094
4095 for( ; start_bits != 0; start_bits-- )
4096 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4097 }
4098
4099 end_bits = len % 8;
4100 if( end_bits != 0 )
4101 {
4102 size_t last_byte_idx = ( offset + len ) / 8;
4103
4104 len -= end_bits; /* Now len % 8 == 0 */
4105
4106 for( ; end_bits != 0; end_bits-- )
4107 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4108 }
4109
4110 memset( mask + offset / 8, 0xFF, len / 8 );
4111}
4112
4113/*
4114 * Check that bitmask is full
4115 */
4116static int ssl_bitmask_check( unsigned char *mask, size_t len )
4117{
4118 size_t i;
4119
4120 for( i = 0; i < len / 8; i++ )
4121 if( mask[i] != 0xFF )
4122 return( -1 );
4123
4124 for( i = 0; i < len % 8; i++ )
4125 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4126 return( -1 );
4127
4128 return( 0 );
4129}
4130
Hanno Becker56e205e2018-08-16 09:06:12 +01004131/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004132static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004133 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004134{
Hanno Becker56e205e2018-08-16 09:06:12 +01004135 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004136
Hanno Becker56e205e2018-08-16 09:06:12 +01004137 alloc_len = 12; /* Handshake header */
4138 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004139
Hanno Beckerd07df862018-08-16 09:14:58 +01004140 if( add_bitmap )
4141 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004142
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004143 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004144}
Hanno Becker56e205e2018-08-16 09:06:12 +01004145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004146#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004147
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004148static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004149{
4150 return( ( ssl->in_msg[1] << 16 ) |
4151 ( ssl->in_msg[2] << 8 ) |
4152 ssl->in_msg[3] );
4153}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004154
Simon Butcher99000142016-10-13 17:21:01 +01004155int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004156{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004157 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004160 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004161 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004162 }
4163
Hanno Becker12555c62018-08-16 12:47:53 +01004164 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004166 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004167 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004168 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004170#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004171 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004172 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004173 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004174 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004175
Hanno Becker44650b72018-08-16 12:51:11 +01004176 if( ssl_check_hs_header( ssl ) != 0 )
4177 {
4178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4179 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4180 }
4181
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004182 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004183 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4184 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4185 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4186 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004187 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004188 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4189 {
4190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4191 recv_msg_seq,
4192 ssl->handshake->in_msg_seq ) );
4193 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4194 }
4195
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004196 /* Retransmit only on last message from previous flight, to avoid
4197 * too many retransmissions.
4198 * Besides, No sane server ever retransmits HelloVerifyRequest */
4199 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004200 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004203 "message_seq = %d, start_of_flight = %d",
4204 recv_msg_seq,
4205 ssl->handshake->in_flight_start_seq ) );
4206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004207 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004209 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004210 return( ret );
4211 }
4212 }
4213 else
4214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004216 "message_seq = %d, expected = %d",
4217 recv_msg_seq,
4218 ssl->handshake->in_msg_seq ) );
4219 }
4220
Hanno Becker90333da2017-10-10 11:27:13 +01004221 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004222 }
4223 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004224
Hanno Becker6d97ef52018-08-16 13:09:04 +01004225 /* Message reassembly is handled alongside buffering of future
4226 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004227 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004228 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004229 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004232 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004233 }
4234 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004235 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004236#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004237#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004238 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004239 /* With TLS we don't handle fragmentation (for now) */
4240 if( ssl->in_msglen < ssl->in_hslen )
4241 {
4242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4243 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4244 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004245 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004246#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004247
Simon Butcher99000142016-10-13 17:21:01 +01004248 return( 0 );
4249}
4250
4251void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4252{
Hanno Becker0271f962018-08-16 13:23:47 +01004253 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004254
Hanno Becker0271f962018-08-16 13:23:47 +01004255 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004256 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004257 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004258 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004259
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004260 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004261#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004262 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004263 ssl->handshake != NULL )
4264 {
Hanno Becker0271f962018-08-16 13:23:47 +01004265 unsigned offset;
4266 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004267
Hanno Becker0271f962018-08-16 13:23:47 +01004268 /* Increment handshake sequence number */
4269 hs->in_msg_seq++;
4270
4271 /*
4272 * Clear up handshake buffering and reassembly structure.
4273 */
4274
4275 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004276 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004277
4278 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004279 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4280 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004281 offset++, hs_buf++ )
4282 {
4283 *hs_buf = *(hs_buf + 1);
4284 }
4285
4286 /* Create a fresh last entry */
4287 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004288 }
4289#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004290}
4291
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004292/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004293 * DTLS anti-replay: RFC 6347 4.1.2.6
4294 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004295 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4296 * Bit n is set iff record number in_window_top - n has been seen.
4297 *
4298 * Usually, in_window_top is the last record number seen and the lsb of
4299 * in_window is set. The only exception is the initial state (record number 0
4300 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004302#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4303static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004304{
4305 ssl->in_window_top = 0;
4306 ssl->in_window = 0;
4307}
4308
4309static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4310{
4311 return( ( (uint64_t) buf[0] << 40 ) |
4312 ( (uint64_t) buf[1] << 32 ) |
4313 ( (uint64_t) buf[2] << 24 ) |
4314 ( (uint64_t) buf[3] << 16 ) |
4315 ( (uint64_t) buf[4] << 8 ) |
4316 ( (uint64_t) buf[5] ) );
4317}
4318
4319/*
4320 * Return 0 if sequence number is acceptable, -1 otherwise
4321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004322int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004323{
4324 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4325 uint64_t bit;
4326
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004327 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004328 return( 0 );
4329
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004330 if( rec_seqnum > ssl->in_window_top )
4331 return( 0 );
4332
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004333 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004334
4335 if( bit >= 64 )
4336 return( -1 );
4337
4338 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4339 return( -1 );
4340
4341 return( 0 );
4342}
4343
4344/*
4345 * Update replay window on new validated record
4346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004347void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004348{
4349 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4350
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004351 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004352 return;
4353
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004354 if( rec_seqnum > ssl->in_window_top )
4355 {
4356 /* Update window_top and the contents of the window */
4357 uint64_t shift = rec_seqnum - ssl->in_window_top;
4358
4359 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004360 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004361 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004362 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004363 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004364 ssl->in_window |= 1;
4365 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004366
4367 ssl->in_window_top = rec_seqnum;
4368 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004369 else
4370 {
4371 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004372 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004373
4374 if( bit < 64 ) /* Always true, but be extra sure */
4375 ssl->in_window |= (uint64_t) 1 << bit;
4376 }
4377}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004378#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004379
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004380#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004381/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004382static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4383
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004384/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004385 * Without any SSL context, check if a datagram looks like a ClientHello with
4386 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004387 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004388 *
4389 * - if cookie is valid, return 0
4390 * - if ClientHello looks superficially valid but cookie is not,
4391 * fill obuf and set olen, then
4392 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4393 * - otherwise return a specific error code
4394 */
4395static int ssl_check_dtls_clihlo_cookie(
4396 mbedtls_ssl_cookie_write_t *f_cookie_write,
4397 mbedtls_ssl_cookie_check_t *f_cookie_check,
4398 void *p_cookie,
4399 const unsigned char *cli_id, size_t cli_id_len,
4400 const unsigned char *in, size_t in_len,
4401 unsigned char *obuf, size_t buf_len, size_t *olen )
4402{
4403 size_t sid_len, cookie_len;
4404 unsigned char *p;
4405
4406 if( f_cookie_write == NULL || f_cookie_check == NULL )
4407 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4408
4409 /*
4410 * Structure of ClientHello with record and handshake headers,
4411 * and expected values. We don't need to check a lot, more checks will be
4412 * done when actually parsing the ClientHello - skipping those checks
4413 * avoids code duplication and does not make cookie forging any easier.
4414 *
4415 * 0-0 ContentType type; copied, must be handshake
4416 * 1-2 ProtocolVersion version; copied
4417 * 3-4 uint16 epoch; copied, must be 0
4418 * 5-10 uint48 sequence_number; copied
4419 * 11-12 uint16 length; (ignored)
4420 *
4421 * 13-13 HandshakeType msg_type; (ignored)
4422 * 14-16 uint24 length; (ignored)
4423 * 17-18 uint16 message_seq; copied
4424 * 19-21 uint24 fragment_offset; copied, must be 0
4425 * 22-24 uint24 fragment_length; (ignored)
4426 *
4427 * 25-26 ProtocolVersion client_version; (ignored)
4428 * 27-58 Random random; (ignored)
4429 * 59-xx SessionID session_id; 1 byte len + sid_len content
4430 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4431 * ...
4432 *
4433 * Minimum length is 61 bytes.
4434 */
4435 if( in_len < 61 ||
4436 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4437 in[3] != 0 || in[4] != 0 ||
4438 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4439 {
4440 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4441 }
4442
4443 sid_len = in[59];
4444 if( sid_len > in_len - 61 )
4445 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4446
4447 cookie_len = in[60 + sid_len];
4448 if( cookie_len > in_len - 60 )
4449 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4450
4451 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4452 cli_id, cli_id_len ) == 0 )
4453 {
4454 /* Valid cookie */
4455 return( 0 );
4456 }
4457
4458 /*
4459 * If we get here, we've got an invalid cookie, let's prepare HVR.
4460 *
4461 * 0-0 ContentType type; copied
4462 * 1-2 ProtocolVersion version; copied
4463 * 3-4 uint16 epoch; copied
4464 * 5-10 uint48 sequence_number; copied
4465 * 11-12 uint16 length; olen - 13
4466 *
4467 * 13-13 HandshakeType msg_type; hello_verify_request
4468 * 14-16 uint24 length; olen - 25
4469 * 17-18 uint16 message_seq; copied
4470 * 19-21 uint24 fragment_offset; copied
4471 * 22-24 uint24 fragment_length; olen - 25
4472 *
4473 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4474 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4475 *
4476 * Minimum length is 28.
4477 */
4478 if( buf_len < 28 )
4479 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4480
4481 /* Copy most fields and adapt others */
4482 memcpy( obuf, in, 25 );
4483 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4484 obuf[25] = 0xfe;
4485 obuf[26] = 0xff;
4486
4487 /* Generate and write actual cookie */
4488 p = obuf + 28;
4489 if( f_cookie_write( p_cookie,
4490 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4491 {
4492 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4493 }
4494
4495 *olen = p - obuf;
4496
4497 /* Go back and fill length fields */
4498 obuf[27] = (unsigned char)( *olen - 28 );
4499
4500 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4501 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4502 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4503
4504 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4505 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4506
4507 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4508}
4509
4510/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004511 * Handle possible client reconnect with the same UDP quadruplet
4512 * (RFC 6347 Section 4.2.8).
4513 *
4514 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4515 * that looks like a ClientHello.
4516 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004517 * - if the input looks like a ClientHello without cookies,
4518 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004519 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004520 * - if the input looks like a ClientHello with a valid cookie,
4521 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004522 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004523 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004524 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004525 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004526 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4527 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004528 */
4529static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4530{
4531 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004532 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004533
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004534 ret = ssl_check_dtls_clihlo_cookie(
4535 ssl->conf->f_cookie_write,
4536 ssl->conf->f_cookie_check,
4537 ssl->conf->p_cookie,
4538 ssl->cli_id, ssl->cli_id_len,
4539 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004540 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004541
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004542 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4543
4544 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004545 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004546 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004547 * If the error is permanent we'll catch it later,
4548 * if it's not, then hopefully it'll work next time. */
4549 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4550
4551 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004552 }
4553
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004554 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004555 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004556 /* Got a valid cookie, partially reset context */
4557 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4558 {
4559 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4560 return( ret );
4561 }
4562
4563 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004564 }
4565
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004566 return( ret );
4567}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004568#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004569
Hanno Becker46483f12019-05-03 13:25:54 +01004570static int ssl_check_record_type( uint8_t record_type )
4571{
4572 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4573 record_type != MBEDTLS_SSL_MSG_ALERT &&
4574 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4575 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4576 {
4577 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4578 }
4579
4580 return( 0 );
4581}
4582
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004583/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004584 * ContentType type;
4585 * ProtocolVersion version;
4586 * uint16 epoch; // DTLS only
4587 * uint48 sequence_number; // DTLS only
4588 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004589 *
4590 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004591 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004592 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4593 *
4594 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004595 * 1. proceed with the record if this function returns 0
4596 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4597 * 3. return CLIENT_RECONNECT if this function return that value
4598 * 4. drop the whole datagram if this function returns anything else.
4599 * Point 2 is needed when the peer is resending, and we have already received
4600 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004602static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004603{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004604 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004605 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004606
Hanno Becker8b09b732019-05-08 12:03:28 +01004607 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004608
Paul Bakker5121ce52009-01-03 21:22:43 +00004609 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004610 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004611
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004612 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004613#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004614 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b09b732019-05-08 12:03:28 +01004615 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4616 ssl->conf->cid_len != 0 )
4617 {
4618 /* Shift pointers to account for record header including CID
4619 * struct {
4620 * ContentType special_type = tls12_cid;
4621 * ProtocolVersion version;
4622 * uint16 epoch;
4623 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004624 * opaque cid[cid_length]; // Additional field compared to
4625 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004626 * uint16 length;
4627 * opaque enc_content[DTLSCiphertext.length];
4628 * } DTLSCiphertext;
4629 */
4630
4631 /* So far, we only support static CID lengths
4632 * fixed in the configuration. */
4633 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4634 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4635 }
4636 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004637#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004638 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004641
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004642#if defined(MBEDTLS_SSL_PROTO_TLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004643 /* Silently ignore invalid DTLS records as recommended by RFC 6347
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004644 * Section 4.1.2.7, that is, send alert only with TLS */
4645 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
4646 {
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004647 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4648 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004649 }
4650#endif /* MBEDTLS_SSL_PROTO_TLS */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004652 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004653 }
4654
4655 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004656 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4659 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004660 }
4661
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004662 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4665 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004666 }
4667
Hanno Becker8b09b732019-05-08 12:03:28 +01004668 /* Now that the total length of the record header is known, ensure
4669 * that the current datagram is large enough to hold it.
4670 * This would fail, for example, if we received a datagram of
4671 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4672 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4673 if( ret != 0 )
4674 {
4675 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4676 return( ret );
4677 }
4678 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4679
4680 /* Parse and validate record length
4681 * This must happen after the CID parsing because
4682 * its position in the record header depends on
4683 * the presence of a CID. */
4684
4685 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004686 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004687 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4690 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004691 }
4692
Hanno Becker8b09b732019-05-08 12:03:28 +01004693 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004694 "version = [%d:%d], msglen = %d",
4695 ssl->in_msgtype,
4696 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004697
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004698 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004699 * DTLS-related tests.
4700 * Check epoch before checking length constraint because
4701 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4702 * message gets duplicated before the corresponding Finished message,
4703 * the second ChangeCipherSpec should be discarded because it belongs
4704 * to an old epoch, but not because its length is shorter than
4705 * the minimum record length for packets using the new record transform.
4706 * Note that these two kinds of failures are handled differently,
4707 * as an unexpected record is silently skipped but an invalid
4708 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004709 */
4710#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004711 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004712 {
4713 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4714
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004715 /* Check epoch (and sequence number) with DTLS */
4716 if( rec_epoch != ssl->in_epoch )
4717 {
4718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4719 "expected %d, received %d",
4720 ssl->in_epoch, rec_epoch ) );
4721
4722#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4723 /*
4724 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4725 * access the first byte of record content (handshake type), as we
4726 * have an active transform (possibly iv_len != 0), so use the
4727 * fact that the record header len is 13 instead.
4728 */
4729 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4730 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4731 rec_epoch == 0 &&
4732 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4733 ssl->in_left > 13 &&
4734 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4735 {
4736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4737 "from the same port" ) );
4738 return( ssl_handle_possible_reconnect( ssl ) );
4739 }
4740 else
4741#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004742 {
4743 /* Consider buffering the record. */
4744 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4745 {
4746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4747 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4748 }
4749
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004750 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004751 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004752 }
4753
4754#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4755 /* Replay detection only works for the current epoch */
4756 if( rec_epoch == ssl->in_epoch &&
4757 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4758 {
4759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4760 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4761 }
4762#endif
4763 }
4764#endif /* MBEDTLS_SSL_PROTO_DTLS */
4765
Hanno Becker52c6dc62017-05-26 16:07:36 +01004766
4767 /* Check length against bounds of the current transform and version */
4768 if( ssl->transform_in == NULL )
4769 {
4770 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004771 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004772 {
4773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4774 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4775 }
4776 }
4777 else
4778 {
4779 if( ssl->in_msglen < ssl->transform_in->minlen )
4780 {
4781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4782 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4783 }
4784
4785#if defined(MBEDTLS_SSL_PROTO_SSL3)
4786 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004787 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004788 {
4789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4790 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4791 }
4792#endif
4793#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4794 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4795 /*
4796 * TLS encrypted messages can have up to 256 bytes of padding
4797 */
4798 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4799 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004800 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004801 {
4802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4803 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4804 }
4805#endif
4806 }
4807
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004808 return( 0 );
4809}
Paul Bakker5121ce52009-01-03 21:22:43 +00004810
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004811/*
4812 * If applicable, decrypt (and decompress) record content
4813 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004814static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004815{
4816 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004819 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4822 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004826 ret = mbedtls_ssl_hw_record_read( ssl );
4827 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004829 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4830 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004831 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004832
4833 if( ret == 0 )
4834 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004835 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004836#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004837 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004838 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004839 mbedtls_record rec;
4840
4841 rec.buf = ssl->in_iv;
4842 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4843 - ( ssl->in_iv - ssl->in_buf );
4844 rec.data_len = ssl->in_msglen;
4845 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004846#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004847 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004848 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004849#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004850
4851 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4852 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4853 ssl->conf->transport, rec.ver );
4854 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004855 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4856 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004858 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004859
Hanno Beckera5a2b082019-05-15 14:03:01 +01004860#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004861 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4862 ssl->conf->ignore_unexpected_cid
4863 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4864 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004865 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004866 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004867 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004868#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004869
Paul Bakker5121ce52009-01-03 21:22:43 +00004870 return( ret );
4871 }
4872
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004873 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004874 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004875 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4876 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004877 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004878
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004879 /* The record content type may change during decryption,
4880 * so re-read it. */
4881 ssl->in_msgtype = rec.type;
4882 /* Also update the input buffer, because unfortunately
4883 * the server-side ssl_parse_client_hello() reparses the
4884 * record header when receiving a ClientHello initiating
4885 * a renegotiation. */
4886 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004887 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004888 ssl->in_msglen = rec.data_len;
4889 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4890 ssl->in_len[1] = (unsigned char)( rec.data_len );
4891
Paul Bakker5121ce52009-01-03 21:22:43 +00004892 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4893 ssl->in_msg, ssl->in_msglen );
4894
Hanno Beckera5a2b082019-05-15 14:03:01 +01004895#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004896 /* We have already checked the record content type
4897 * in ssl_parse_record_header(), failing or silently
4898 * dropping the record in the case of an unknown type.
4899 *
4900 * Since with the use of CIDs, the record content type
4901 * might change during decryption, re-check the record
4902 * content type, but treat a failure as fatal this time. */
4903 if( ssl_check_record_type( ssl->in_msgtype ) )
4904 {
4905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4906 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4907 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004908#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004909
Angus Grattond8213d02016-05-25 20:56:48 +10004910 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4913 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004914 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004915 else if( ssl->in_msglen == 0 )
4916 {
4917#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4918 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4919 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4920 {
4921 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4922 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4923 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4924 }
4925#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4926
4927 ssl->nb_zero++;
4928
4929 /*
4930 * Three or more empty messages may be a DoS attack
4931 * (excessive CPU consumption).
4932 */
4933 if( ssl->nb_zero > 3 )
4934 {
4935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004936 "messages, possible DoS attack" ) );
4937 /* Treat the records as if they were not properly authenticated,
4938 * thereby failing the connection if we see more than allowed
4939 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004940 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4941 }
4942 }
4943 else
4944 ssl->nb_zero = 0;
4945
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004946 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
4947#if defined(MBEDTLS_SSL_PROTO_TLS)
4948 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004949 {
4950 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004951 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004952 if( ++ssl->in_ctr[i - 1] != 0 )
4953 break;
4954
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02004955 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004956 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004957 {
4958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4959 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4960 }
4961 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004962#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004963
Paul Bakker5121ce52009-01-03 21:22:43 +00004964 }
4965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004966#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004967 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004968 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004969 {
4970 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004972 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004973 return( ret );
4974 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004975 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004976#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004978#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004979 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004981 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004982 }
4983#endif
4984
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004985 return( 0 );
4986}
4987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004988static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004989
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004990/*
4991 * Read a record.
4992 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004993 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4994 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4995 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004996 */
Hanno Becker1097b342018-08-15 14:09:41 +01004997
4998/* Helper functions for mbedtls_ssl_read_record(). */
4999static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005000static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5001static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005002
Hanno Becker327c93b2018-08-15 13:56:18 +01005003int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005004 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005005{
5006 int ret;
5007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005009
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005010 if( ssl->keep_current_message == 0 )
5011 {
5012 do {
Simon Butcher99000142016-10-13 17:21:01 +01005013
Hanno Becker26994592018-08-15 14:14:59 +01005014 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005015 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005016 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005017
Hanno Beckere74d5562018-08-15 14:26:08 +01005018 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005019 {
Hanno Becker40f50842018-08-15 14:48:01 +01005020#if defined(MBEDTLS_SSL_PROTO_DTLS)
5021 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005022
Hanno Becker40f50842018-08-15 14:48:01 +01005023 /* We only check for buffered messages if the
5024 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005025 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005026 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005027 {
Hanno Becker40f50842018-08-15 14:48:01 +01005028 if( ssl_load_buffered_message( ssl ) == 0 )
5029 have_buffered = 1;
5030 }
5031
5032 if( have_buffered == 0 )
5033#endif /* MBEDTLS_SSL_PROTO_DTLS */
5034 {
5035 ret = ssl_get_next_record( ssl );
5036 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5037 continue;
5038
5039 if( ret != 0 )
5040 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005041 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005042 return( ret );
5043 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005044 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005045 }
5046
5047 ret = mbedtls_ssl_handle_message_type( ssl );
5048
Hanno Becker40f50842018-08-15 14:48:01 +01005049#if defined(MBEDTLS_SSL_PROTO_DTLS)
5050 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5051 {
5052 /* Buffer future message */
5053 ret = ssl_buffer_message( ssl );
5054 if( ret != 0 )
5055 return( ret );
5056
5057 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5058 }
5059#endif /* MBEDTLS_SSL_PROTO_DTLS */
5060
Hanno Becker90333da2017-10-10 11:27:13 +01005061 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5062 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005063
5064 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005065 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005066 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005067 return( ret );
5068 }
5069
Hanno Becker327c93b2018-08-15 13:56:18 +01005070 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005071 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005072 {
5073 mbedtls_ssl_update_handshake_status( ssl );
5074 }
Simon Butcher99000142016-10-13 17:21:01 +01005075 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005076 else
Simon Butcher99000142016-10-13 17:21:01 +01005077 {
Hanno Becker02f59072018-08-15 14:00:24 +01005078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005079 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005080 }
5081
5082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5083
5084 return( 0 );
5085}
5086
Hanno Becker40f50842018-08-15 14:48:01 +01005087#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005088static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005089{
Hanno Becker40f50842018-08-15 14:48:01 +01005090 if( ssl->in_left > ssl->next_record_offset )
5091 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005092
Hanno Becker40f50842018-08-15 14:48:01 +01005093 return( 0 );
5094}
5095
5096static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5097{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005098 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005099 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005100 int ret = 0;
5101
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005102 if( hs == NULL )
5103 return( -1 );
5104
Hanno Beckere00ae372018-08-20 09:39:42 +01005105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5106
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005107 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5108 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5109 {
5110 /* Check if we have seen a ChangeCipherSpec before.
5111 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005112 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005113 {
5114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5115 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005116 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005117 }
5118
Hanno Becker39b8bc92018-08-28 17:17:13 +01005119 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005120 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5121 ssl->in_msglen = 1;
5122 ssl->in_msg[0] = 1;
5123
5124 /* As long as they are equal, the exact value doesn't matter. */
5125 ssl->in_left = 0;
5126 ssl->next_record_offset = 0;
5127
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005128 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005129 goto exit;
5130 }
Hanno Becker37f95322018-08-16 13:55:32 +01005131
Hanno Beckerb8f50142018-08-28 10:01:34 +01005132#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005133 /* Debug only */
5134 {
5135 unsigned offset;
5136 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5137 {
5138 hs_buf = &hs->buffering.hs[offset];
5139 if( hs_buf->is_valid == 1 )
5140 {
5141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5142 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005143 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005144 }
5145 }
5146 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005147#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005148
5149 /* Check if we have buffered and/or fully reassembled the
5150 * next handshake message. */
5151 hs_buf = &hs->buffering.hs[0];
5152 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5153 {
5154 /* Synthesize a record containing the buffered HS message. */
5155 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5156 ( hs_buf->data[2] << 8 ) |
5157 hs_buf->data[3];
5158
5159 /* Double-check that we haven't accidentally buffered
5160 * a message that doesn't fit into the input buffer. */
5161 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5162 {
5163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5164 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5165 }
5166
5167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5168 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5169 hs_buf->data, msg_len + 12 );
5170
5171 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5172 ssl->in_hslen = msg_len + 12;
5173 ssl->in_msglen = msg_len + 12;
5174 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5175
5176 ret = 0;
5177 goto exit;
5178 }
5179 else
5180 {
5181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5182 hs->in_msg_seq ) );
5183 }
5184
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005185 ret = -1;
5186
5187exit:
5188
5189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5190 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005191}
5192
Hanno Beckera02b0b42018-08-21 17:20:27 +01005193static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5194 size_t desired )
5195{
5196 int offset;
5197 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5199 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005200
Hanno Becker01315ea2018-08-21 17:22:17 +01005201 /* Get rid of future records epoch first, if such exist. */
5202 ssl_free_buffered_record( ssl );
5203
5204 /* Check if we have enough space available now. */
5205 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5206 hs->buffering.total_bytes_buffered ) )
5207 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005209 return( 0 );
5210 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005211
Hanno Becker4f432ad2018-08-28 10:02:32 +01005212 /* We don't have enough space to buffer the next expected handshake
5213 * message. Remove buffers used for future messages to gain space,
5214 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005215 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5216 offset >= 0; offset-- )
5217 {
5218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5219 offset ) );
5220
Hanno Beckerb309b922018-08-23 13:18:05 +01005221 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005222
5223 /* Check if we have enough space available now. */
5224 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5225 hs->buffering.total_bytes_buffered ) )
5226 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005228 return( 0 );
5229 }
5230 }
5231
5232 return( -1 );
5233}
5234
Hanno Becker40f50842018-08-15 14:48:01 +01005235static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5236{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005237 int ret = 0;
5238 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5239
5240 if( hs == NULL )
5241 return( 0 );
5242
5243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5244
5245 switch( ssl->in_msgtype )
5246 {
5247 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005249
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005250 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005251 break;
5252
5253 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005254 {
5255 unsigned recv_msg_seq_offset;
5256 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5257 mbedtls_ssl_hs_buffer *hs_buf;
5258 size_t msg_len = ssl->in_hslen - 12;
5259
5260 /* We should never receive an old handshake
5261 * message - double-check nonetheless. */
5262 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5263 {
5264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5265 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5266 }
5267
5268 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5269 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5270 {
5271 /* Silently ignore -- message too far in the future */
5272 MBEDTLS_SSL_DEBUG_MSG( 2,
5273 ( "Ignore future HS message with sequence number %u, "
5274 "buffering window %u - %u",
5275 recv_msg_seq, ssl->handshake->in_msg_seq,
5276 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5277
5278 goto exit;
5279 }
5280
5281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5282 recv_msg_seq, recv_msg_seq_offset ) );
5283
5284 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5285
5286 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005287 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005288 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005289 size_t reassembly_buf_sz;
5290
Hanno Becker37f95322018-08-16 13:55:32 +01005291 hs_buf->is_fragmented =
5292 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5293
5294 /* We copy the message back into the input buffer
5295 * after reassembly, so check that it's not too large.
5296 * This is an implementation-specific limitation
5297 * and not one from the standard, hence it is not
5298 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005299 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005300 {
5301 /* Ignore message */
5302 goto exit;
5303 }
5304
Hanno Beckere0b150f2018-08-21 15:51:03 +01005305 /* Check if we have enough space to buffer the message. */
5306 if( hs->buffering.total_bytes_buffered >
5307 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5308 {
5309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5310 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5311 }
5312
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005313 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5314 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005315
5316 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5317 hs->buffering.total_bytes_buffered ) )
5318 {
5319 if( recv_msg_seq_offset > 0 )
5320 {
5321 /* If we can't buffer a future message because
5322 * of space limitations -- ignore. */
5323 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",
5324 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5325 (unsigned) hs->buffering.total_bytes_buffered ) );
5326 goto exit;
5327 }
Hanno Beckere1801392018-08-21 16:51:05 +01005328 else
5329 {
5330 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",
5331 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5332 (unsigned) hs->buffering.total_bytes_buffered ) );
5333 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005334
Hanno Beckera02b0b42018-08-21 17:20:27 +01005335 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005336 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005337 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",
5338 (unsigned) msg_len,
5339 (unsigned) reassembly_buf_sz,
5340 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005341 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005342 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5343 goto exit;
5344 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005345 }
5346
5347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5348 msg_len ) );
5349
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005350 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5351 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005352 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005353 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005354 goto exit;
5355 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005356 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005357
5358 /* Prepare final header: copy msg_type, length and message_seq,
5359 * then add standardised fragment_offset and fragment_length */
5360 memcpy( hs_buf->data, ssl->in_msg, 6 );
5361 memset( hs_buf->data + 6, 0, 3 );
5362 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5363
5364 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005365
5366 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005367 }
5368 else
5369 {
5370 /* Make sure msg_type and length are consistent */
5371 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5372 {
5373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5374 /* Ignore */
5375 goto exit;
5376 }
5377 }
5378
Hanno Becker4422bbb2018-08-20 09:40:19 +01005379 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005380 {
5381 size_t frag_len, frag_off;
5382 unsigned char * const msg = hs_buf->data + 12;
5383
5384 /*
5385 * Check and copy current fragment
5386 */
5387
5388 /* Validation of header fields already done in
5389 * mbedtls_ssl_prepare_handshake_record(). */
5390 frag_off = ssl_get_hs_frag_off( ssl );
5391 frag_len = ssl_get_hs_frag_len( ssl );
5392
5393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5394 frag_off, frag_len ) );
5395 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5396
5397 if( hs_buf->is_fragmented )
5398 {
5399 unsigned char * const bitmask = msg + msg_len;
5400 ssl_bitmask_set( bitmask, frag_off, frag_len );
5401 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5402 msg_len ) == 0 );
5403 }
5404 else
5405 {
5406 hs_buf->is_complete = 1;
5407 }
5408
5409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5410 hs_buf->is_complete ? "" : "not yet " ) );
5411 }
5412
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005413 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005414 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005415
5416 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005417 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005418 break;
5419 }
5420
5421exit:
5422
5423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5424 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005425}
5426#endif /* MBEDTLS_SSL_PROTO_DTLS */
5427
Hanno Becker1097b342018-08-15 14:09:41 +01005428static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005429{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005430 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005431 * Consume last content-layer message and potentially
5432 * update in_msglen which keeps track of the contents'
5433 * consumption state.
5434 *
5435 * (1) Handshake messages:
5436 * Remove last handshake message, move content
5437 * and adapt in_msglen.
5438 *
5439 * (2) Alert messages:
5440 * Consume whole record content, in_msglen = 0.
5441 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005442 * (3) Change cipher spec:
5443 * Consume whole record content, in_msglen = 0.
5444 *
5445 * (4) Application data:
5446 * Don't do anything - the record layer provides
5447 * the application data as a stream transport
5448 * and consumes through mbedtls_ssl_read only.
5449 *
5450 */
5451
5452 /* Case (1): Handshake messages */
5453 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005454 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005455 /* Hard assertion to be sure that no application data
5456 * is in flight, as corrupting ssl->in_msglen during
5457 * ssl->in_offt != NULL is fatal. */
5458 if( ssl->in_offt != NULL )
5459 {
5460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5461 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5462 }
5463
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005464 /*
5465 * Get next Handshake message in the current record
5466 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005467
Hanno Becker4a810fb2017-05-24 16:27:30 +01005468 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005469 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005470 * current handshake content: If DTLS handshake
5471 * fragmentation is used, that's the fragment
5472 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005473 * size here is faulty and should be changed at
5474 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005475 * (2) While it doesn't seem to cause problems, one
5476 * has to be very careful not to assume that in_hslen
5477 * is always <= in_msglen in a sensible communication.
5478 * Again, it's wrong for DTLS handshake fragmentation.
5479 * The following check is therefore mandatory, and
5480 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005481 * Additionally, ssl->in_hslen might be arbitrarily out of
5482 * bounds after handling a DTLS message with an unexpected
5483 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005484 */
5485 if( ssl->in_hslen < ssl->in_msglen )
5486 {
5487 ssl->in_msglen -= ssl->in_hslen;
5488 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5489 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005490
Hanno Becker4a810fb2017-05-24 16:27:30 +01005491 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5492 ssl->in_msg, ssl->in_msglen );
5493 }
5494 else
5495 {
5496 ssl->in_msglen = 0;
5497 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005498
Hanno Becker4a810fb2017-05-24 16:27:30 +01005499 ssl->in_hslen = 0;
5500 }
5501 /* Case (4): Application data */
5502 else if( ssl->in_offt != NULL )
5503 {
5504 return( 0 );
5505 }
5506 /* Everything else (CCS & Alerts) */
5507 else
5508 {
5509 ssl->in_msglen = 0;
5510 }
5511
Hanno Becker1097b342018-08-15 14:09:41 +01005512 return( 0 );
5513}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005514
Hanno Beckere74d5562018-08-15 14:26:08 +01005515static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5516{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005517 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005518 return( 1 );
5519
5520 return( 0 );
5521}
5522
Hanno Becker5f066e72018-08-16 14:56:31 +01005523#if defined(MBEDTLS_SSL_PROTO_DTLS)
5524
5525static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5526{
5527 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5528 if( hs == NULL )
5529 return;
5530
Hanno Becker01315ea2018-08-21 17:22:17 +01005531 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005532 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005533 hs->buffering.total_bytes_buffered -=
5534 hs->buffering.future_record.len;
5535
5536 mbedtls_free( hs->buffering.future_record.data );
5537 hs->buffering.future_record.data = NULL;
5538 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005539}
5540
5541static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5542{
5543 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5544 unsigned char * rec;
5545 size_t rec_len;
5546 unsigned rec_epoch;
5547
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005548 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01005549 return( 0 );
5550
5551 if( hs == NULL )
5552 return( 0 );
5553
Hanno Becker5f066e72018-08-16 14:56:31 +01005554 rec = hs->buffering.future_record.data;
5555 rec_len = hs->buffering.future_record.len;
5556 rec_epoch = hs->buffering.future_record.epoch;
5557
5558 if( rec == NULL )
5559 return( 0 );
5560
Hanno Becker4cb782d2018-08-20 11:19:05 +01005561 /* Only consider loading future records if the
5562 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005563 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005564 return( 0 );
5565
Hanno Becker5f066e72018-08-16 14:56:31 +01005566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5567
5568 if( rec_epoch != ssl->in_epoch )
5569 {
5570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5571 goto exit;
5572 }
5573
5574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5575
5576 /* Double-check that the record is not too large */
5577 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5578 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5579 {
5580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5581 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5582 }
5583
5584 memcpy( ssl->in_hdr, rec, rec_len );
5585 ssl->in_left = rec_len;
5586 ssl->next_record_offset = 0;
5587
5588 ssl_free_buffered_record( ssl );
5589
5590exit:
5591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5592 return( 0 );
5593}
5594
5595static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5596{
5597 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5598 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005599 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005600
5601 /* Don't buffer future records outside handshakes. */
5602 if( hs == NULL )
5603 return( 0 );
5604
5605 /* Only buffer handshake records (we are only interested
5606 * in Finished messages). */
5607 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5608 return( 0 );
5609
5610 /* Don't buffer more than one future epoch record. */
5611 if( hs->buffering.future_record.data != NULL )
5612 return( 0 );
5613
Hanno Becker01315ea2018-08-21 17:22:17 +01005614 /* Don't buffer record if there's not enough buffering space remaining. */
5615 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5616 hs->buffering.total_bytes_buffered ) )
5617 {
5618 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",
5619 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5620 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005621 return( 0 );
5622 }
5623
Hanno Becker5f066e72018-08-16 14:56:31 +01005624 /* Buffer record */
5625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5626 ssl->in_epoch + 1 ) );
5627 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5628 rec_hdr_len + ssl->in_msglen );
5629
5630 /* ssl_parse_record_header() only considers records
5631 * of the next epoch as candidates for buffering. */
5632 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005633 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005634
5635 hs->buffering.future_record.data =
5636 mbedtls_calloc( 1, hs->buffering.future_record.len );
5637 if( hs->buffering.future_record.data == NULL )
5638 {
5639 /* If we run out of RAM trying to buffer a
5640 * record from the next epoch, just ignore. */
5641 return( 0 );
5642 }
5643
Hanno Becker01315ea2018-08-21 17:22:17 +01005644 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005645
Hanno Becker01315ea2018-08-21 17:22:17 +01005646 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005647 return( 0 );
5648}
5649
5650#endif /* MBEDTLS_SSL_PROTO_DTLS */
5651
Hanno Beckere74d5562018-08-15 14:26:08 +01005652static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005653{
5654 int ret;
5655
Hanno Becker5f066e72018-08-16 14:56:31 +01005656#if defined(MBEDTLS_SSL_PROTO_DTLS)
5657 /* We might have buffered a future record; if so,
5658 * and if the epoch matches now, load it.
5659 * On success, this call will set ssl->in_left to
5660 * the length of the buffered record, so that
5661 * the calls to ssl_fetch_input() below will
5662 * essentially be no-ops. */
5663 ret = ssl_load_buffered_record( ssl );
5664 if( ret != 0 )
5665 return( ret );
5666#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005667
Hanno Becker8b09b732019-05-08 12:03:28 +01005668 /* Reset in pointers to default state for TLS/DTLS records,
5669 * assuming no CID and no offset between record content and
5670 * record plaintext. */
5671 ssl_update_in_pointers( ssl );
5672
5673 /* Ensure that we have enough space available for the default form
5674 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5675 * with no space for CIDs counted in). */
5676 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5677 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005679 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005680 return( ret );
5681 }
5682
5683 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005685#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005686 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005687 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005688 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005689 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5690 {
5691 ret = ssl_buffer_future_record( ssl );
5692 if( ret != 0 )
5693 return( ret );
5694
5695 /* Fall through to handling of unexpected records */
5696 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5697 }
5698
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005699 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5700 {
5701 /* Skip unexpected record (but not whole datagram) */
5702 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005703 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005704
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5706 "(header)" ) );
5707 }
5708 else
5709 {
5710 /* Skip invalid record and the rest of the datagram */
5711 ssl->next_record_offset = 0;
5712 ssl->in_left = 0;
5713
5714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5715 "(header)" ) );
5716 }
5717
5718 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005719 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005720 }
5721#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005722 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005723 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005724
5725 /*
5726 * Read and optionally decrypt the message contents
5727 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005729 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005732 return( ret );
5733 }
5734
5735 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005736#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005737 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01005738 {
Hanno Becker43395762019-05-03 14:46:38 +01005739 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005740 if( ssl->next_record_offset < ssl->in_left )
5741 {
5742 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5743 }
5744 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005745 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005746#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005747#if defined(MBEDTLS_SSL_PROTO_TLS)
5748 {
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005749 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005750 }
5751#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005752
5753 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005755#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005756 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005757 {
5758 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005759 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005760 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005761 /* Except when waiting for Finished as a bad mac here
5762 * probably means something went wrong in the handshake
5763 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5764 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5765 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5766 {
5767#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5768 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5769 {
5770 mbedtls_ssl_send_alert_message( ssl,
5771 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5772 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5773 }
5774#endif
5775 return( ret );
5776 }
5777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005778#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005779 if( ssl->conf->badmac_limit != 0 &&
5780 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5783 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005784 }
5785#endif
5786
Hanno Becker4a810fb2017-05-24 16:27:30 +01005787 /* As above, invalid records cause
5788 * dismissal of the whole datagram. */
5789
5790 ssl->next_record_offset = 0;
5791 ssl->in_left = 0;
5792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005794 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005795 }
5796
5797 return( ret );
5798 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005799 MBEDTLS_SSL_TRANSPORT_ELSE
5800#endif /* MBEDTLS_SSL_PROTO_DTLS */
5801#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005802 {
5803 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005804#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5805 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005807 mbedtls_ssl_send_alert_message( ssl,
5808 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5809 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005810 }
5811#endif
5812 return( ret );
5813 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005814#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005815 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005816
Simon Butcher99000142016-10-13 17:21:01 +01005817 return( 0 );
5818}
5819
5820int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5821{
5822 int ret;
5823
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005824 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005825 * Handle particular types of records
5826 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005827 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005828 {
Simon Butcher99000142016-10-13 17:21:01 +01005829 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5830 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005831 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005832 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005833 }
5834
Hanno Beckere678eaa2018-08-21 14:57:46 +01005835 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005836 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005837 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005838 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5840 ssl->in_msglen ) );
5841 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005842 }
5843
Hanno Beckere678eaa2018-08-21 14:57:46 +01005844 if( ssl->in_msg[0] != 1 )
5845 {
5846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5847 ssl->in_msg[0] ) );
5848 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5849 }
5850
5851#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005852 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01005853 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5854 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5855 {
5856 if( ssl->handshake == NULL )
5857 {
5858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5859 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5860 }
5861
5862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5863 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5864 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005865#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005866 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005869 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005870 if( ssl->in_msglen != 2 )
5871 {
5872 /* Note: Standard allows for more than one 2 byte alert
5873 to be packed in a single message, but Mbed TLS doesn't
5874 currently support this. */
5875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5876 ssl->in_msglen ) );
5877 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5878 }
5879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005881 ssl->in_msg[0], ssl->in_msg[1] ) );
5882
5883 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005884 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005885 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005886 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005889 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005890 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005891 }
5892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005893 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5894 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5897 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005898 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005899
5900#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5901 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5902 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5903 {
Hanno Becker90333da2017-10-10 11:27:13 +01005904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005905 /* Will be handled when trying to parse ServerHello */
5906 return( 0 );
5907 }
5908#endif
5909
5910#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5911 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5912 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5913 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5914 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5915 {
5916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5917 /* Will be handled in mbedtls_ssl_parse_certificate() */
5918 return( 0 );
5919 }
5920#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5921
5922 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005923 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005924 }
5925
Hanno Beckerc76c6192017-06-06 10:03:17 +01005926#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005927 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005928 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005929 /* Drop unexpected ApplicationData records,
5930 * except at the beginning of renegotiations */
5931 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5932 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5933#if defined(MBEDTLS_SSL_RENEGOTIATION)
5934 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5935 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005936#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005937 )
5938 {
5939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5940 return( MBEDTLS_ERR_SSL_NON_FATAL );
5941 }
5942
5943 if( ssl->handshake != NULL &&
5944 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5945 {
5946 ssl_handshake_wrapup_free_hs_transform( ssl );
5947 }
5948 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005949#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005950
Paul Bakker5121ce52009-01-03 21:22:43 +00005951 return( 0 );
5952}
5953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005954int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005955{
5956 int ret;
5957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005958 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5959 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5960 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005961 {
5962 return( ret );
5963 }
5964
5965 return( 0 );
5966}
5967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005968int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005969 unsigned char level,
5970 unsigned char message )
5971{
5972 int ret;
5973
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005974 if( ssl == NULL || ssl->conf == NULL )
5975 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005978 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005980 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005981 ssl->out_msglen = 2;
5982 ssl->out_msg[0] = level;
5983 ssl->out_msg[1] = message;
5984
Hanno Becker67bc7c32018-08-06 11:33:50 +01005985 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005987 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005988 return( ret );
5989 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005991
5992 return( 0 );
5993}
5994
Hanno Becker17572472019-02-08 07:19:04 +00005995#if defined(MBEDTLS_X509_CRT_PARSE_C)
5996static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5997{
5998#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5999 if( session->peer_cert != NULL )
6000 {
6001 mbedtls_x509_crt_free( session->peer_cert );
6002 mbedtls_free( session->peer_cert );
6003 session->peer_cert = NULL;
6004 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006005#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006006 if( session->peer_cert_digest != NULL )
6007 {
6008 /* Zeroization is not necessary. */
6009 mbedtls_free( session->peer_cert_digest );
6010 session->peer_cert_digest = NULL;
6011 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6012 session->peer_cert_digest_len = 0;
6013 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006014#else
6015 ((void) session);
6016#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006017}
6018#endif /* MBEDTLS_X509_CRT_PARSE_C */
6019
Paul Bakker5121ce52009-01-03 21:22:43 +00006020/*
6021 * Handshake functions
6022 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006023#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006024/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006026{
Hanno Becker8759e162017-12-27 21:34:08 +00006027 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006030
Hanno Becker5097cba2019-02-05 13:36:46 +00006031 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006034 ssl->state++;
6035 return( 0 );
6036 }
6037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6039 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006040}
6041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006042int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006043{
Hanno Becker8759e162017-12-27 21:34:08 +00006044 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006047
Hanno Becker5097cba2019-02-05 13:36:46 +00006048 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006051 ssl->state++;
6052 return( 0 );
6053 }
6054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6056 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006057}
Gilles Peskinef9828522017-05-03 12:28:43 +02006058
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006059#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006060/* Some certificate support -> implement write and parse */
6061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006062int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006063{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006064 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006065 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006066 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006067 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006070
Hanno Becker5097cba2019-02-05 13:36:46 +00006071 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006074 ssl->state++;
6075 return( 0 );
6076 }
6077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006079 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006080 {
6081 if( ssl->client_auth == 0 )
6082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006084 ssl->state++;
6085 return( 0 );
6086 }
6087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006088#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006089 /*
6090 * If using SSLv3 and got no cert, send an Alert message
6091 * (otherwise an empty Certificate message will be sent).
6092 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006093 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6094 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006095 {
6096 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006097 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6098 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6099 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006102 goto write_msg;
6103 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006104#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006105 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006106#endif /* MBEDTLS_SSL_CLI_C */
6107#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006108 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006110 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6113 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006114 }
6115 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006116#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006118 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006119
6120 /*
6121 * 0 . 0 handshake type
6122 * 1 . 3 handshake length
6123 * 4 . 6 length of all certs
6124 * 7 . 9 length of cert. 1
6125 * 10 . n-1 peer certificate
6126 * n . n+2 length of cert. 2
6127 * n+3 . ... upper level cert, etc.
6128 */
6129 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006130 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006131
Paul Bakker29087132010-03-21 21:03:34 +00006132 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006133 {
6134 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006135 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006138 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006140 }
6141
6142 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6143 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6144 ssl->out_msg[i + 2] = (unsigned char)( n );
6145
6146 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6147 i += n; crt = crt->next;
6148 }
6149
6150 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6151 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6152 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6153
6154 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006155 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6156 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006157
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006158#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006159write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006160#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006161
6162 ssl->state++;
6163
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006164 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006165 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006166 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006167 return( ret );
6168 }
6169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006171
Paul Bakkered27a042013-04-18 22:46:23 +02006172 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006173}
6174
Hanno Becker285ff0c2019-01-31 07:44:03 +00006175#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006176
6177#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006178static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6179 unsigned char *crt_buf,
6180 size_t crt_buf_len )
6181{
6182 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6183
6184 if( peer_crt == NULL )
6185 return( -1 );
6186
6187 if( peer_crt->raw.len != crt_buf_len )
6188 return( -1 );
6189
Hanno Becker68b856d2019-02-08 14:00:04 +00006190 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006191}
Hanno Becker5882dd02019-06-06 16:25:57 +01006192#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006193static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6194 unsigned char *crt_buf,
6195 size_t crt_buf_len )
6196{
6197 int ret;
6198 unsigned char const * const peer_cert_digest =
6199 ssl->session->peer_cert_digest;
6200 mbedtls_md_type_t const peer_cert_digest_type =
6201 ssl->session->peer_cert_digest_type;
6202 mbedtls_md_info_t const * const digest_info =
6203 mbedtls_md_info_from_type( peer_cert_digest_type );
6204 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6205 size_t digest_len;
6206
6207 if( peer_cert_digest == NULL || digest_info == NULL )
6208 return( -1 );
6209
6210 digest_len = mbedtls_md_get_size( digest_info );
6211 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6212 return( -1 );
6213
6214 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6215 if( ret != 0 )
6216 return( -1 );
6217
6218 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6219}
Hanno Becker5882dd02019-06-06 16:25:57 +01006220#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006221#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006222
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006223/*
6224 * Once the certificate message is read, parse it into a cert chain and
6225 * perform basic checks, but leave actual verification to the caller
6226 */
Hanno Becker35e41772019-02-05 15:37:23 +00006227static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6228 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006229{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006230 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00006231#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6232 int crt_cnt=0;
6233#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006234 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006235 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006237 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006240 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6241 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006242 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006243 }
6244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006245 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6246 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006249 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6250 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006252 }
6253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006255
Paul Bakker5121ce52009-01-03 21:22:43 +00006256 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006257 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006258 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006259 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006260
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006261 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006262 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006265 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6266 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006267 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006268 }
6269
Hanno Becker33c3dc82019-01-30 14:46:46 +00006270 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6271 i += 3;
6272
Hanno Becker33c3dc82019-01-30 14:46:46 +00006273 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006274 while( i < ssl->in_hslen )
6275 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006276 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006277 if ( i + 3 > ssl->in_hslen ) {
6278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006279 mbedtls_ssl_send_alert_message( ssl,
6280 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6281 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006282 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6283 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006284 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6285 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006286 if( ssl->in_msg[i] != 0 )
6287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006289 mbedtls_ssl_send_alert_message( ssl,
6290 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6291 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006292 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006293 }
6294
Hanno Becker33c3dc82019-01-30 14:46:46 +00006295 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006296 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6297 | (unsigned int) ssl->in_msg[i + 2];
6298 i += 3;
6299
6300 if( n < 128 || i + n > ssl->in_hslen )
6301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006303 mbedtls_ssl_send_alert_message( ssl,
6304 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6305 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006307 }
6308
Hanno Becker33c3dc82019-01-30 14:46:46 +00006309 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006310#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6311 if( crt_cnt++ == 0 &&
6312 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6313 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006314 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006315 /* During client-side renegotiation, check that the server's
6316 * end-CRTs hasn't changed compared to the initial handshake,
6317 * mitigating the triple handshake attack. On success, reuse
6318 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006319 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6320 if( ssl_check_peer_crt_unchanged( ssl,
6321 &ssl->in_msg[i],
6322 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006323 {
Hanno Becker35e41772019-02-05 15:37:23 +00006324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6325 mbedtls_ssl_send_alert_message( ssl,
6326 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6327 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6328 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006329 }
Hanno Becker35e41772019-02-05 15:37:23 +00006330
6331 /* Now we can safely free the original chain. */
6332 ssl_clear_peer_cert( ssl->session );
6333 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006334#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6335
Hanno Becker33c3dc82019-01-30 14:46:46 +00006336 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00006337#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00006338 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00006339#else
Hanno Becker42de8f82019-02-26 11:51:34 +00006340 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00006341 * it in-place from the input buffer instead of making a copy. */
6342 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6343#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006344 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006345 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006346 case 0: /*ok*/
6347 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6348 /* Ignore certificate with an unknown algorithm: maybe a
6349 prior certificate was already trusted. */
6350 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006351
Hanno Becker33c3dc82019-01-30 14:46:46 +00006352 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6353 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6354 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006355
Hanno Becker33c3dc82019-01-30 14:46:46 +00006356 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6357 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6358 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006359
Hanno Becker33c3dc82019-01-30 14:46:46 +00006360 default:
6361 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6362 crt_parse_der_failed:
6363 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6364 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6365 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006366 }
6367
6368 i += n;
6369 }
6370
Hanno Becker35e41772019-02-05 15:37:23 +00006371 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006372 return( 0 );
6373}
6374
Hanno Beckerb8a08572019-02-05 12:49:06 +00006375#if defined(MBEDTLS_SSL_SRV_C)
6376static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6377{
6378 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6379 return( -1 );
6380
6381#if defined(MBEDTLS_SSL_PROTO_SSL3)
6382 /*
6383 * Check if the client sent an empty certificate
6384 */
6385 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6386 {
6387 if( ssl->in_msglen == 2 &&
6388 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6389 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6390 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6391 {
6392 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6393 return( 0 );
6394 }
6395
6396 return( -1 );
6397 }
6398#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6399
6400#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6401 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6402 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6403 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6404 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6405 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6406 {
6407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6408 return( 0 );
6409 }
6410
Hanno Beckerb8a08572019-02-05 12:49:06 +00006411#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6412 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01006413
6414 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00006415}
6416#endif /* MBEDTLS_SSL_SRV_C */
6417
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006418/* Check if a certificate message is expected.
6419 * Return either
6420 * - SSL_CERTIFICATE_EXPECTED, or
6421 * - SSL_CERTIFICATE_SKIP
6422 * indicating whether a Certificate message is expected or not.
6423 */
6424#define SSL_CERTIFICATE_EXPECTED 0
6425#define SSL_CERTIFICATE_SKIP 1
6426static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6427 int authmode )
6428{
6429 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6430 ssl->handshake->ciphersuite_info;
6431
6432 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6433 return( SSL_CERTIFICATE_SKIP );
6434
6435#if defined(MBEDTLS_SSL_SRV_C)
6436 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6437 {
6438 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6439 return( SSL_CERTIFICATE_SKIP );
6440
6441 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6442 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006443 ssl->session_negotiate->verify_result =
6444 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6445 return( SSL_CERTIFICATE_SKIP );
6446 }
6447 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00006448#else
6449 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006450#endif /* MBEDTLS_SSL_SRV_C */
6451
6452 return( SSL_CERTIFICATE_EXPECTED );
6453}
6454
Hanno Becker3cf50612019-02-05 14:36:34 +00006455static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6456 int authmode,
6457 mbedtls_x509_crt *chain,
6458 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006459{
Hanno Becker613d4902019-02-05 13:11:17 +00006460 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006461 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6462 ssl->handshake->ciphersuite_info;
Hanno Becker3cf50612019-02-05 14:36:34 +00006463 mbedtls_x509_crt *ca_chain;
6464 mbedtls_x509_crl *ca_crl;
6465
6466 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6467 return( 0 );
6468
6469#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6470 if( ssl->handshake->sni_ca_chain != NULL )
6471 {
6472 ca_chain = ssl->handshake->sni_ca_chain;
6473 ca_crl = ssl->handshake->sni_ca_crl;
6474 }
6475 else
6476#endif
6477 {
6478 ca_chain = ssl->conf->ca_chain;
6479 ca_crl = ssl->conf->ca_crl;
6480 }
6481
6482 /*
6483 * Main check: verify certificate
6484 */
6485 ret = mbedtls_x509_crt_verify_restartable(
6486 chain,
6487 ca_chain, ca_crl,
6488 ssl->conf->cert_profile,
6489 ssl->hostname,
6490 &ssl->session_negotiate->verify_result,
6491 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6492
6493 if( ret != 0 )
6494 {
6495 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6496 }
6497
6498#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6499 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6500 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6501#endif
6502
6503 /*
6504 * Secondary checks: always done, but change 'ret' only if it was 0
6505 */
6506
6507#if defined(MBEDTLS_ECP_C)
6508 {
6509 const mbedtls_pk_context *pk = &chain->pk;
6510
6511 /* If certificate uses an EC key, make sure the curve is OK */
6512 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6513 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6514 {
6515 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6516
6517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6518 if( ret == 0 )
6519 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6520 }
6521 }
6522#endif /* MBEDTLS_ECP_C */
6523
6524 if( mbedtls_ssl_check_cert_usage( chain,
6525 ciphersuite_info,
6526 ! ssl->conf->endpoint,
6527 &ssl->session_negotiate->verify_result ) != 0 )
6528 {
6529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6530 if( ret == 0 )
6531 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6532 }
6533
6534 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6535 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6536 * with details encoded in the verification flags. All other kinds
6537 * of error codes, including those from the user provided f_vrfy
6538 * functions, are treated as fatal and lead to a failure of
6539 * ssl_parse_certificate even if verification was optional. */
6540 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6541 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6542 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6543 {
6544 ret = 0;
6545 }
6546
6547 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6548 {
6549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6550 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6551 }
6552
6553 if( ret != 0 )
6554 {
6555 uint8_t alert;
6556
6557 /* The certificate may have been rejected for several reasons.
6558 Pick one and send the corresponding alert. Which alert to send
6559 may be a subject of debate in some cases. */
6560 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6561 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6562 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6563 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6564 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6565 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6566 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6567 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6568 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6569 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6570 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6571 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6572 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6573 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6574 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6575 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6576 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6577 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6578 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6579 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6580 else
6581 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6582 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6583 alert );
6584 }
6585
6586#if defined(MBEDTLS_DEBUG_C)
6587 if( ssl->session_negotiate->verify_result != 0 )
6588 {
6589 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6590 ssl->session_negotiate->verify_result ) );
6591 }
6592 else
6593 {
6594 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6595 }
6596#endif /* MBEDTLS_DEBUG_C */
6597
6598 return( ret );
6599}
6600
Hanno Becker34106f62019-02-08 14:59:05 +00006601#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01006602
6603#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00006604static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6605 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006606{
6607 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00006608 /* Remember digest of the peer's end-CRT. */
6609 ssl->session_negotiate->peer_cert_digest =
6610 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6611 if( ssl->session_negotiate->peer_cert_digest == NULL )
6612 {
6613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6614 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6615 mbedtls_ssl_send_alert_message( ssl,
6616 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6617 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6618
6619 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6620 }
6621
6622 ret = mbedtls_md( mbedtls_md_info_from_type(
6623 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6624 start, len,
6625 ssl->session_negotiate->peer_cert_digest );
6626
6627 ssl->session_negotiate->peer_cert_digest_type =
6628 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6629 ssl->session_negotiate->peer_cert_digest_len =
6630 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6631
6632 return( ret );
6633}
Hanno Becker5882dd02019-06-06 16:25:57 +01006634#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00006635
6636static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6637 unsigned char *start, size_t len )
6638{
6639 unsigned char *end = start + len;
6640 int ret;
6641
6642 /* Make a copy of the peer's raw public key. */
6643 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6644 ret = mbedtls_pk_parse_subpubkey( &start, end,
6645 &ssl->handshake->peer_pubkey );
6646 if( ret != 0 )
6647 {
6648 /* We should have parsed the public key before. */
6649 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6650 }
6651
6652 return( 0 );
6653}
6654#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6655
Hanno Becker3cf50612019-02-05 14:36:34 +00006656int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6657{
6658 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006659 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006660#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6661 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6662 ? ssl->handshake->sni_authmode
6663 : ssl->conf->authmode;
6664#else
6665 const int authmode = ssl->conf->authmode;
6666#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006667 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00006668 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006669
6670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6671
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006672 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6673 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006674 {
6675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00006676 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006677 }
6678
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006679#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6680 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006681 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006682 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00006683 chain = ssl->handshake->ecrs_peer_cert;
6684 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006685 goto crt_verify;
6686 }
6687#endif
6688
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006689 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006690 {
6691 /* mbedtls_ssl_read_record may have sent an alert already. We
6692 let it decide whether to alert. */
6693 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006694 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006695 }
6696
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006697#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00006698 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6699 {
6700 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006701
Hanno Beckerb8a08572019-02-05 12:49:06 +00006702 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00006703 ret = 0;
6704 else
6705 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006706
Hanno Becker613d4902019-02-05 13:11:17 +00006707 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006708 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00006709#endif /* MBEDTLS_SSL_SRV_C */
6710
Hanno Becker35e41772019-02-05 15:37:23 +00006711 /* Clear existing peer CRT structure in case we tried to
6712 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00006713 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006714
6715 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6716 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00006717 {
6718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6719 sizeof( mbedtls_x509_crt ) ) );
6720 mbedtls_ssl_send_alert_message( ssl,
6721 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6722 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00006723
Hanno Beckere4aeb762019-02-05 17:19:52 +00006724 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6725 goto exit;
6726 }
6727 mbedtls_x509_crt_init( chain );
6728
6729 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00006730 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006731 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006732
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006733#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6734 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006735 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006736
6737crt_verify:
6738 if( ssl->handshake->ecrs_enabled)
6739 rs_ctx = &ssl->handshake->ecrs_ctx;
6740#endif
6741
Hanno Becker3cf50612019-02-05 14:36:34 +00006742 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00006743 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00006744 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006745 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006746
Hanno Becker3008d282019-02-05 17:02:28 +00006747#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00006748 {
Hanno Becker5882dd02019-06-06 16:25:57 +01006749 size_t pk_len;
6750 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00006751
Hanno Becker34106f62019-02-08 14:59:05 +00006752 /* We parse the CRT chain without copying, so
6753 * these pointers point into the input buffer,
6754 * and are hence still valid after freeing the
6755 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006756
Hanno Becker5882dd02019-06-06 16:25:57 +01006757#if defined(MBEDTLS_SSL_RENEGOTIATION)
6758 unsigned char *crt_start;
6759 size_t crt_len;
6760
Hanno Becker34106f62019-02-08 14:59:05 +00006761 crt_start = chain->raw.p;
6762 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01006763#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006764
Hanno Becker34106f62019-02-08 14:59:05 +00006765 pk_start = chain->pk_raw.p;
6766 pk_len = chain->pk_raw.len;
6767
6768 /* Free the CRT structures before computing
6769 * digest and copying the peer's public key. */
6770 mbedtls_x509_crt_free( chain );
6771 mbedtls_free( chain );
6772 chain = NULL;
6773
Hanno Becker5882dd02019-06-06 16:25:57 +01006774#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00006775 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006776 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00006777 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01006778#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006779
Hanno Becker34106f62019-02-08 14:59:05 +00006780 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006781 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00006782 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006783 }
Hanno Becker34106f62019-02-08 14:59:05 +00006784#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6785 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00006786 ssl->session_negotiate->peer_cert = chain;
6787 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00006788#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006791
Hanno Becker613d4902019-02-05 13:11:17 +00006792exit:
6793
Hanno Beckere4aeb762019-02-05 17:19:52 +00006794 if( ret == 0 )
6795 ssl->state++;
6796
6797#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6798 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6799 {
6800 ssl->handshake->ecrs_peer_cert = chain;
6801 chain = NULL;
6802 }
6803#endif
6804
6805 if( chain != NULL )
6806 {
6807 mbedtls_x509_crt_free( chain );
6808 mbedtls_free( chain );
6809 }
6810
Paul Bakker5121ce52009-01-03 21:22:43 +00006811 return( ret );
6812}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006813#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006815int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006816{
6817 int ret;
6818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006822 ssl->out_msglen = 1;
6823 ssl->out_msg[0] = 1;
6824
Paul Bakker5121ce52009-01-03 21:22:43 +00006825 ssl->state++;
6826
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006827 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006828 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006829 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006830 return( ret );
6831 }
6832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006834
6835 return( 0 );
6836}
6837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006838int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006839{
6840 int ret;
6841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006843
Hanno Becker327c93b2018-08-15 13:56:18 +01006844 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006847 return( ret );
6848 }
6849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006853 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6854 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006855 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006856 }
6857
Hanno Beckere678eaa2018-08-21 14:57:46 +01006858 /* CCS records are only accepted if they have length 1 and content '1',
6859 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006860
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006861 /*
6862 * Switch to our negotiated transform and session parameters for inbound
6863 * data.
6864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006865 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006866 ssl->transform_in = ssl->transform_negotiate;
6867 ssl->session_in = ssl->session_negotiate;
6868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006869#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006870 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006872#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006873 ssl_dtls_replay_reset( ssl );
6874#endif
6875
6876 /* Increment epoch */
6877 if( ++ssl->in_epoch == 0 )
6878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006880 /* This is highly unlikely to happen for legitimate reasons, so
6881 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006882 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006883 }
6884 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006885 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006886#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006887#if defined(MBEDTLS_SSL_PROTO_TLS)
6888 {
6889 memset( ssl->in_ctr, 0, 8 );
6890 }
6891#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006892
Hanno Beckerf5970a02019-05-08 09:38:41 +01006893 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006895#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6896 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006900 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006901 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6902 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006903 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006904 }
6905 }
6906#endif
6907
Paul Bakker5121ce52009-01-03 21:22:43 +00006908 ssl->state++;
6909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006911
6912 return( 0 );
6913}
6914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006915void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6916 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006917{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006918 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6921 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6922 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006923 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006924 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006925#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006926#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6927#if defined(MBEDTLS_SHA512_C)
6928 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006929 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6930 else
6931#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006932#if defined(MBEDTLS_SHA256_C)
6933 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006934 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006935 else
6936#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006940 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006941 }
Paul Bakker380da532012-04-18 16:10:25 +00006942}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006944void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006945{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006946#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6947 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006948 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6949 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006950#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006951#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6952#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006953 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006954#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006956 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006957#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006958#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006959}
6960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006962 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006963{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006964#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6965 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006966 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6967 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006968#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006969#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6970#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006971 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006972#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006973#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006974 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006975#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006976#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006977}
6978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006979#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6980 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6981static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006982 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006983{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006984 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6985 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006986}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006987#endif
Paul Bakker380da532012-04-18 16:10:25 +00006988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006989#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6990#if defined(MBEDTLS_SHA256_C)
6991static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006992 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006993{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006994 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006995}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006996#endif
Paul Bakker380da532012-04-18 16:10:25 +00006997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006998#if defined(MBEDTLS_SHA512_C)
6999static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007000 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007001{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007002 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007003}
Paul Bakker769075d2012-11-24 11:26:46 +01007004#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007005#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007007#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007008static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007009 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007010{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007011 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007012 mbedtls_md5_context md5;
7013 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007014
Paul Bakker5121ce52009-01-03 21:22:43 +00007015 unsigned char padbuf[48];
7016 unsigned char md5sum[16];
7017 unsigned char sha1sum[20];
7018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007020 if( !session )
7021 session = ssl->session;
7022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007024
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007025 mbedtls_md5_init( &md5 );
7026 mbedtls_sha1_init( &sha1 );
7027
7028 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7029 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007030
7031 /*
7032 * SSLv3:
7033 * hash =
7034 * MD5( master + pad2 +
7035 * MD5( handshake + sender + master + pad1 ) )
7036 * + SHA1( master + pad2 +
7037 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007038 */
7039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007040#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007041 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7042 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007045#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007046 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7047 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007048#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007050 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007051 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007052
Paul Bakker1ef83d62012-04-11 12:09:53 +00007053 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007054
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007055 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7056 mbedtls_md5_update_ret( &md5, session->master, 48 );
7057 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7058 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007059
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007060 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7061 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7062 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7063 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007064
Paul Bakker1ef83d62012-04-11 12:09:53 +00007065 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007066
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007067 mbedtls_md5_starts_ret( &md5 );
7068 mbedtls_md5_update_ret( &md5, session->master, 48 );
7069 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7070 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7071 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007072
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007073 mbedtls_sha1_starts_ret( &sha1 );
7074 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7075 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7076 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7077 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007080
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007081 mbedtls_md5_free( &md5 );
7082 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007083
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007084 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7085 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7086 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007089}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007092#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007093static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007095{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007096 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007097 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007098 mbedtls_md5_context md5;
7099 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007100 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007103 if( !session )
7104 session = ssl->session;
7105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007107
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007108 mbedtls_md5_init( &md5 );
7109 mbedtls_sha1_init( &sha1 );
7110
7111 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7112 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007113
Paul Bakker1ef83d62012-04-11 12:09:53 +00007114 /*
7115 * TLSv1:
7116 * hash = PRF( master, finished_label,
7117 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7118 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007120#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007121 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7122 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007123#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007126 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7127 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007128#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007131 ? "client finished"
7132 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007133
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007134 mbedtls_md5_finish_ret( &md5, padbuf );
7135 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007136
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007137 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007138 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007141
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007142 mbedtls_md5_free( &md5 );
7143 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007144
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007145 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007148}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007151#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7152#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007153static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007154 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007155{
7156 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007157 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007158 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007159 unsigned char padbuf[32];
7160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007161 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007162 if( !session )
7163 session = ssl->session;
7164
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007165 mbedtls_sha256_init( &sha256 );
7166
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007168
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007169 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007170
7171 /*
7172 * TLSv1.2:
7173 * hash = PRF( master, finished_label,
7174 * Hash( handshake ) )[0.11]
7175 */
7176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007177#if !defined(MBEDTLS_SHA256_ALT)
7178 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007179 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007180#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007183 ? "client finished"
7184 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007185
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007186 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007187
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007188 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007189 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007192
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007193 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007194
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007195 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007198}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007199#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007201#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007202static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007204{
7205 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007206 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007207 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007208 unsigned char padbuf[48];
7209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007210 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007211 if( !session )
7212 session = ssl->session;
7213
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007214 mbedtls_sha512_init( &sha512 );
7215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007217
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007218 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007219
7220 /*
7221 * TLSv1.2:
7222 * hash = PRF( master, finished_label,
7223 * Hash( handshake ) )[0.11]
7224 */
7225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007226#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007227 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7228 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007229#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007231 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007232 ? "client finished"
7233 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00007234
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007235 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007236
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007237 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007238 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007240 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007241
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007242 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007243
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007244 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007247}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007248#endif /* MBEDTLS_SHA512_C */
7249#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007252{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007253 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007254
7255 /*
7256 * Free our handshake params
7257 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007258 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007260 ssl->handshake = NULL;
7261
7262 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007263 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007264 */
7265 if( ssl->transform )
7266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267 mbedtls_ssl_transform_free( ssl->transform );
7268 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007269 }
7270 ssl->transform = ssl->transform_negotiate;
7271 ssl->transform_negotiate = NULL;
7272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007273 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007274}
7275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007277{
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007278#if !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007279 int resume = ssl->handshake->resume;
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007280#endif /* !MBEDTLS_SSL_NO_SESSION_CACHE */
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007282 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284#if defined(MBEDTLS_SSL_RENEGOTIATION)
7285 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007288 ssl->renego_records_seen = 0;
7289 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007290#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007291
7292 /*
7293 * Free the previous session and switch in the current one
7294 */
Paul Bakker0a597072012-09-25 21:55:46 +00007295 if( ssl->session )
7296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007298 /* RFC 7366 3.1: keep the EtM state */
7299 ssl->session_negotiate->encrypt_then_mac =
7300 ssl->session->encrypt_then_mac;
7301#endif
7302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303 mbedtls_ssl_session_free( ssl->session );
7304 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007305 }
7306 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007307 ssl->session_negotiate = NULL;
7308
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007309#if !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007310 /*
7311 * Add cache entry
7312 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007313 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007314 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007315 resume == 0 )
7316 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007317 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007319 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007320#endif /* !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007322#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007323 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007324 ssl->handshake->flight != NULL )
7325 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007326 /* Cancel handshake timer */
7327 ssl_set_timer( ssl, 0 );
7328
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007329 /* Keep last flight around in case we need to resend it:
7330 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007332 }
7333 else
7334#endif
7335 ssl_handshake_wrapup_free_hs_transform( ssl );
7336
Paul Bakker48916f92012-09-16 19:57:18 +00007337 ssl->state++;
7338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007339 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007340}
7341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007343{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007344 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007347
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007348 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007349
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007350 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007351
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007352 /*
7353 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7354 * may define some other value. Currently (early 2016), no defined
7355 * ciphersuite does this (and this is unlikely to change as activity has
7356 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007358 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007360#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007361 ssl->verify_data_len = hash_len;
7362 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007363#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007364
Paul Bakker5121ce52009-01-03 21:22:43 +00007365 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007366 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7367 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007368
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007369#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00007370 /*
7371 * In case of session resuming, invert the client and server
7372 * ChangeCipherSpec messages order.
7373 */
Paul Bakker0a597072012-09-25 21:55:46 +00007374 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007376#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007377 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007378 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007379#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007380#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007381 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007382 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007383#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007384 }
7385 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007386#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007387 ssl->state++;
7388
Paul Bakker48916f92012-09-16 19:57:18 +00007389 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007390 * Switch to our negotiated transform and session parameters for outbound
7391 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007395#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007396 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007397 {
7398 unsigned char i;
7399
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007400 /* Remember current epoch settings for resending */
7401 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007402 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007403
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007404 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007405 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007406
7407 /* Increment epoch */
7408 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007409 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007410 break;
7411
7412 /* The loop goes to its end iff the counter is wrapping */
7413 if( i == 0 )
7414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7416 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007417 }
7418 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007419 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007421#if defined(MBEDTLS_SSL_PROTO_TLS)
7422 {
7423 memset( ssl->cur_out_ctr, 0, 8 );
7424 }
7425#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007426
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007427 ssl->transform_out = ssl->transform_negotiate;
7428 ssl->session_out = ssl->session_negotiate;
7429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007430#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7431 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7436 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007437 }
7438 }
7439#endif
7440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007441#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007442 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007444#endif
7445
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007446 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007447 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007448 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007449 return( ret );
7450 }
7451
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007452#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007453 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007454 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7455 {
7456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7457 return( ret );
7458 }
7459#endif
7460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007462
7463 return( 0 );
7464}
7465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007466#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007467#define SSL_MAX_HASH_LEN 36
7468#else
7469#define SSL_MAX_HASH_LEN 12
7470#endif
7471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007473{
Paul Bakker23986e52011-04-24 08:57:21 +00007474 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007475 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007476 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007479
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007480 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007481
Hanno Becker327c93b2018-08-15 13:56:18 +01007482 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007485 return( ret );
7486 }
7487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007488 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007491 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7492 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007493 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007494 }
7495
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007496 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007497#if defined(MBEDTLS_SSL_PROTO_SSL3)
7498 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007499 hash_len = 36;
7500 else
7501#endif
7502 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7505 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007508 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7509 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007511 }
7512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007513 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007514 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007517 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7518 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007519 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007520 }
7521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007522#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007523 ssl->verify_data_len = hash_len;
7524 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007525#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007526
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007527#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker0a597072012-09-25 21:55:46 +00007528 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007530#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007531 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007533#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007535 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007536 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007537#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007538 }
7539 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007540#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007541 ssl->state++;
7542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007543#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007544 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007545 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007546#endif
7547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007549
7550 return( 0 );
7551}
7552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007554{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007555 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7558 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7559 mbedtls_md5_init( &handshake->fin_md5 );
7560 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007561 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7562 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007563#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007564#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7565#if defined(MBEDTLS_SHA256_C)
7566 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007567 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007568#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569#if defined(MBEDTLS_SHA512_C)
7570 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007571 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007572#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007573#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007574
7575 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007576
7577#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7578 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7579 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7580#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007582#if defined(MBEDTLS_DHM_C)
7583 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007584#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007585#if defined(MBEDTLS_ECDH_C)
7586 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007587#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007588#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007589 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007590#if defined(MBEDTLS_SSL_CLI_C)
7591 handshake->ecjpake_cache = NULL;
7592 handshake->ecjpake_cache_len = 0;
7593#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007594#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007595
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007596#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007597 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007598#endif
7599
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007600#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7601 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7602#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00007603
7604#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7605 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7606 mbedtls_pk_init( &handshake->peer_pubkey );
7607#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007608}
7609
Hanno Becker611a83b2018-01-03 14:27:32 +00007610void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007611{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007612 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007614 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7615 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007616
Hanno Becker92231322018-01-03 15:32:51 +00007617#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007618 mbedtls_md_init( &transform->md_ctx_enc );
7619 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007620#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007621}
7622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007623void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007624{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007625 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007626}
7627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007629{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007630 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007631 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007632 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007633 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007635 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007636 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007637
7638 /*
7639 * Either the pointers are now NULL or cleared properly and can be freed.
7640 * Now allocate missing structures.
7641 */
7642 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007643 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007644 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007645 }
Paul Bakker48916f92012-09-16 19:57:18 +00007646
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007647 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007648 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007649 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007650 }
Paul Bakker48916f92012-09-16 19:57:18 +00007651
Paul Bakker82788fb2014-10-20 13:59:19 +02007652 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007653 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007654 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007655 }
Paul Bakker48916f92012-09-16 19:57:18 +00007656
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007657 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007658 if( ssl->handshake == NULL ||
7659 ssl->transform_negotiate == NULL ||
7660 ssl->session_negotiate == NULL )
7661 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007664 mbedtls_free( ssl->handshake );
7665 mbedtls_free( ssl->transform_negotiate );
7666 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007667
7668 ssl->handshake = NULL;
7669 ssl->transform_negotiate = NULL;
7670 ssl->session_negotiate = NULL;
7671
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007672 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007673 }
7674
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007675 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007677 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007678 ssl_handshake_params_init( ssl->handshake );
7679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007680#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007681 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007682 {
7683 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007684
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007685 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7686 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7687 else
7688 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007689
7690 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007691 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007692#endif
7693
Paul Bakker48916f92012-09-16 19:57:18 +00007694 return( 0 );
7695}
7696
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007697#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007698/* Dummy cookie callbacks for defaults */
7699static int ssl_cookie_write_dummy( void *ctx,
7700 unsigned char **p, unsigned char *end,
7701 const unsigned char *cli_id, size_t cli_id_len )
7702{
7703 ((void) ctx);
7704 ((void) p);
7705 ((void) end);
7706 ((void) cli_id);
7707 ((void) cli_id_len);
7708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007709 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007710}
7711
7712static int ssl_cookie_check_dummy( void *ctx,
7713 const unsigned char *cookie, size_t cookie_len,
7714 const unsigned char *cli_id, size_t cli_id_len )
7715{
7716 ((void) ctx);
7717 ((void) cookie);
7718 ((void) cookie_len);
7719 ((void) cli_id);
7720 ((void) cli_id_len);
7721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007723}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007724#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007725
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007726/* Once ssl->out_hdr as the address of the beginning of the
7727 * next outgoing record is set, deduce the other pointers.
7728 *
7729 * Note: For TLS, we save the implicit record sequence number
7730 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7731 * and the caller has to make sure there's space for this.
7732 */
7733
7734static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7735 mbedtls_ssl_transform *transform )
7736{
7737#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007738 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007739 {
7740 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007741#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007742 ssl->out_cid = ssl->out_ctr + 8;
7743 ssl->out_len = ssl->out_cid;
7744 if( transform != NULL )
7745 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007746#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007747 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007748#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007749 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007750 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007751 MBEDTLS_SSL_TRANSPORT_ELSE
7752#endif /* MBEDTLS_SSL_PROTO_DTLS */
7753#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007754 {
7755 ssl->out_ctr = ssl->out_hdr - 8;
7756 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007757#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007758 ssl->out_cid = ssl->out_len;
7759#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007760 ssl->out_iv = ssl->out_hdr + 5;
7761 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007762#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007763
7764 /* Adjust out_msg to make space for explicit IV, if used. */
7765 if( transform != NULL &&
7766 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7767 {
7768 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7769 }
7770 else
7771 ssl->out_msg = ssl->out_iv;
7772}
7773
7774/* Once ssl->in_hdr as the address of the beginning of the
7775 * next incoming record is set, deduce the other pointers.
7776 *
7777 * Note: For TLS, we save the implicit record sequence number
7778 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7779 * and the caller has to make sure there's space for this.
7780 */
7781
Hanno Beckerf5970a02019-05-08 09:38:41 +01007782static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007783{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007784 /* This function sets the pointers to match the case
7785 * of unprotected TLS/DTLS records, with both ssl->in_iv
7786 * and ssl->in_msg pointing to the beginning of the record
7787 * content.
7788 *
7789 * When decrypting a protected record, ssl->in_msg
7790 * will be shifted to point to the beginning of the
7791 * record plaintext.
7792 */
7793
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007794#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007795 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007796 {
Hanno Becker70e79282019-05-03 14:34:53 +01007797 /* This sets the header pointers to match records
7798 * without CID. When we receive a record containing
7799 * a CID, the fields are shifted accordingly in
7800 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007801 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007802#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007803 ssl->in_cid = ssl->in_ctr + 8;
7804 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007805#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007806 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007807#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007808 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007809 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007810 MBEDTLS_SSL_TRANSPORT_ELSE
7811#endif /* MBEDTLS_SSL_PROTO_DTLS */
7812#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007813 {
7814 ssl->in_ctr = ssl->in_hdr - 8;
7815 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007816#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007817 ssl->in_cid = ssl->in_len;
7818#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007819 ssl->in_iv = ssl->in_hdr + 5;
7820 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007821#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007822
Hanno Beckerf5970a02019-05-08 09:38:41 +01007823 /* This will be adjusted at record decryption time. */
7824 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007825}
7826
Paul Bakker5121ce52009-01-03 21:22:43 +00007827/*
7828 * Initialize an SSL context
7829 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007830void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7831{
7832 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7833}
7834
7835/*
7836 * Setup an SSL context
7837 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007838
7839static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7840{
7841 /* Set the incoming and outgoing record pointers. */
7842#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007843 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007844 {
7845 ssl->out_hdr = ssl->out_buf;
7846 ssl->in_hdr = ssl->in_buf;
7847 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007848 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007849#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007850#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007851 {
7852 ssl->out_hdr = ssl->out_buf + 8;
7853 ssl->in_hdr = ssl->in_buf + 8;
7854 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007855#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007856
7857 /* Derive other internal pointers. */
7858 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007859 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007860}
7861
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007862int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007863 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007864{
Paul Bakker48916f92012-09-16 19:57:18 +00007865 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007866
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007867 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007868
7869 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007870 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007871 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007872
7873 /* Set to NULL in case of an error condition */
7874 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007875
Angus Grattond8213d02016-05-25 20:56:48 +10007876 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7877 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007878 {
Angus Grattond8213d02016-05-25 20:56:48 +10007879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007880 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007881 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007882 }
7883
7884 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7885 if( ssl->out_buf == NULL )
7886 {
7887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007888 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007889 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007890 }
7891
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007892 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007893
Paul Bakker48916f92012-09-16 19:57:18 +00007894 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007895 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007896
7897 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007898
7899error:
7900 mbedtls_free( ssl->in_buf );
7901 mbedtls_free( ssl->out_buf );
7902
7903 ssl->conf = NULL;
7904
7905 ssl->in_buf = NULL;
7906 ssl->out_buf = NULL;
7907
7908 ssl->in_hdr = NULL;
7909 ssl->in_ctr = NULL;
7910 ssl->in_len = NULL;
7911 ssl->in_iv = NULL;
7912 ssl->in_msg = NULL;
7913
7914 ssl->out_hdr = NULL;
7915 ssl->out_ctr = NULL;
7916 ssl->out_len = NULL;
7917 ssl->out_iv = NULL;
7918 ssl->out_msg = NULL;
7919
k-stachowiak9f7798e2018-07-31 16:52:32 +02007920 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007921}
7922
7923/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007924 * Reset an initialized and used SSL context for re-use while retaining
7925 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007926 *
7927 * If partial is non-zero, keep data in the input buffer and client ID.
7928 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007929 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007930static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007931{
Paul Bakker48916f92012-09-16 19:57:18 +00007932 int ret;
7933
Hanno Becker7e772132018-08-10 12:38:21 +01007934#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7935 !defined(MBEDTLS_SSL_SRV_C)
7936 ((void) partial);
7937#endif
7938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007940
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007941 /* Cancel any possibly running timer */
7942 ssl_set_timer( ssl, 0 );
7943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007944#if defined(MBEDTLS_SSL_RENEGOTIATION)
7945 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007946 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007947
7948 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7950 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007951#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007952 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007953
Paul Bakker7eb013f2011-10-06 12:37:39 +00007954 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007955 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007956
7957 ssl->in_msgtype = 0;
7958 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007960 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007961 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007962#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007964 ssl_dtls_replay_reset( ssl );
7965#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007966
7967 ssl->in_hslen = 0;
7968 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007969
7970 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007971
7972 ssl->out_msgtype = 0;
7973 ssl->out_msglen = 0;
7974 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7976 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007977 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007978#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007979
Hanno Becker19859472018-08-06 09:40:20 +01007980 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7981
Paul Bakker48916f92012-09-16 19:57:18 +00007982 ssl->transform_in = NULL;
7983 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007984
Hanno Becker78640902018-08-13 16:35:15 +01007985 ssl->session_in = NULL;
7986 ssl->session_out = NULL;
7987
Angus Grattond8213d02016-05-25 20:56:48 +10007988 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007989
7990#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007991 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007992#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7993 {
7994 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007995 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007996 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007998#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7999 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8002 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008004 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8005 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008006 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008007 }
8008#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008009
Paul Bakker48916f92012-09-16 19:57:18 +00008010 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012 mbedtls_ssl_transform_free( ssl->transform );
8013 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008014 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008015 }
Paul Bakker48916f92012-09-16 19:57:18 +00008016
Paul Bakkerc0463502013-02-14 11:19:38 +01008017 if( ssl->session )
8018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008019 mbedtls_ssl_session_free( ssl->session );
8020 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008021 ssl->session = NULL;
8022 }
8023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008024#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008025 ssl->alpn_chosen = NULL;
8026#endif
8027
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008028#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008029#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008030 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008031#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008032 {
8033 mbedtls_free( ssl->cli_id );
8034 ssl->cli_id = NULL;
8035 ssl->cli_id_len = 0;
8036 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008037#endif
8038
Paul Bakker48916f92012-09-16 19:57:18 +00008039 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8040 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008041
8042 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008043}
8044
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008045/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008046 * Reset an initialized and used SSL context for re-use while retaining
8047 * all application-set variables, function pointers and data.
8048 */
8049int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8050{
8051 return( ssl_session_reset_int( ssl, 0 ) );
8052}
8053
8054/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008055 * SSL set accessors
8056 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008057void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008058{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008059 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008060}
8061
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008062void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008063{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008064 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008065}
8066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008067#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008068void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008069{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008070 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008071}
8072#endif
8073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008074#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008075void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008076{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008077 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008078}
8079#endif
8080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008082
Hanno Becker1841b0a2018-08-24 11:13:57 +01008083void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8084 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008085{
8086 ssl->disable_datagram_packing = !allow_packing;
8087}
8088
8089void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8090 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008091{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008092 conf->hs_timeout_min = min;
8093 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008094}
8095#endif
8096
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008097void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008098{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008099 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008100}
8101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008102#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008103void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008104 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008105 void *p_vrfy )
8106{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008107 conf->f_vrfy = f_vrfy;
8108 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008109}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008110#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008111
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008112void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008113 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008114 void *p_rng )
8115{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008116 conf->f_rng = f_rng;
8117 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008118}
8119
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008120void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008121 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008122 void *p_dbg )
8123{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008124 conf->f_dbg = f_dbg;
8125 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008126}
8127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008128void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008129 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008130 mbedtls_ssl_send_t *f_send,
8131 mbedtls_ssl_recv_t *f_recv,
8132 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008133{
8134 ssl->p_bio = p_bio;
8135 ssl->f_send = f_send;
8136 ssl->f_recv = f_recv;
8137 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008138}
8139
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008140#if defined(MBEDTLS_SSL_PROTO_DTLS)
8141void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8142{
8143 ssl->mtu = mtu;
8144}
8145#endif
8146
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008147void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008148{
8149 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008150}
8151
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008152void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8153 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008154 mbedtls_ssl_set_timer_t *f_set_timer,
8155 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008156{
8157 ssl->p_timer = p_timer;
8158 ssl->f_set_timer = f_set_timer;
8159 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008160
8161 /* Make sure we start with no timer running */
8162 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008163}
8164
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008165#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008166void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008167 void *p_cache,
8168 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8169 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008170{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008171 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008172 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008173 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008174}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008175#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008176
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008177#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008178int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008179{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008180 int ret;
8181
8182 if( ssl == NULL ||
8183 session == NULL ||
8184 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008185 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008188 }
8189
Hanno Becker58fccf22019-02-06 14:30:46 +00008190 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8191 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008192 return( ret );
8193
Paul Bakker0a597072012-09-25 21:55:46 +00008194 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008195
8196 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008197}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008198#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008199
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008200void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008201 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008202{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008203 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8204 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8205 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8206 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008207}
8208
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008209void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008210 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008211 int major, int minor )
8212{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008213 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008214 return;
8215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008216 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008217 return;
8218
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008219 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008220}
8221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008222#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008223void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008224 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008225{
8226 conf->cert_profile = profile;
8227}
8228
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008229/* Append a new keycert entry to a (possibly empty) list */
8230static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8231 mbedtls_x509_crt *cert,
8232 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008233{
niisato8ee24222018-06-25 19:05:48 +09008234 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008235
niisato8ee24222018-06-25 19:05:48 +09008236 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8237 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008238 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008239
niisato8ee24222018-06-25 19:05:48 +09008240 new_cert->cert = cert;
8241 new_cert->key = key;
8242 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008243
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008244 /* Update head is the list was null, else add to the end */
8245 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008246 {
niisato8ee24222018-06-25 19:05:48 +09008247 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008248 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008249 else
8250 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008251 mbedtls_ssl_key_cert *cur = *head;
8252 while( cur->next != NULL )
8253 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008254 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008255 }
8256
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008257 return( 0 );
8258}
8259
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008260int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008261 mbedtls_x509_crt *own_cert,
8262 mbedtls_pk_context *pk_key )
8263{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008264 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008265}
8266
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008267void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008268 mbedtls_x509_crt *ca_chain,
8269 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008270{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008271 conf->ca_chain = ca_chain;
8272 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008273}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008274#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008275
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008276#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8277int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8278 mbedtls_x509_crt *own_cert,
8279 mbedtls_pk_context *pk_key )
8280{
8281 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8282 own_cert, pk_key ) );
8283}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008284
8285void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8286 mbedtls_x509_crt *ca_chain,
8287 mbedtls_x509_crl *ca_crl )
8288{
8289 ssl->handshake->sni_ca_chain = ca_chain;
8290 ssl->handshake->sni_ca_crl = ca_crl;
8291}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008292
8293void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8294 int authmode )
8295{
8296 ssl->handshake->sni_authmode = authmode;
8297}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008298#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8299
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008300#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008301/*
8302 * Set EC J-PAKE password for current handshake
8303 */
8304int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8305 const unsigned char *pw,
8306 size_t pw_len )
8307{
8308 mbedtls_ecjpake_role role;
8309
Janos Follath8eb64132016-06-03 15:40:57 +01008310 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008311 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8312
8313 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8314 role = MBEDTLS_ECJPAKE_SERVER;
8315 else
8316 role = MBEDTLS_ECJPAKE_CLIENT;
8317
8318 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8319 role,
8320 MBEDTLS_MD_SHA256,
8321 MBEDTLS_ECP_DP_SECP256R1,
8322 pw, pw_len ) );
8323}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008324#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008326#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008327int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008328 const unsigned char *psk, size_t psk_len,
8329 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008330{
Paul Bakker6db455e2013-09-18 17:29:31 +02008331 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008332 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008334 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8335 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008336
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008337 /* Identity len will be encoded on two bytes */
8338 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008339 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008340 {
8341 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8342 }
8343
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008344 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008345 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008346 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008347
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008348 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008349 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008350 conf->psk_len = 0;
8351 }
8352 if( conf->psk_identity != NULL )
8353 {
8354 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008355 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008356 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008357 }
8358
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008359 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8360 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008361 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008362 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008363 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008364 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008365 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008366 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008367 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008368
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008369 conf->psk_len = psk_len;
8370 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008371
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008372 memcpy( conf->psk, psk, conf->psk_len );
8373 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008374
8375 return( 0 );
8376}
8377
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008378int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8379 const unsigned char *psk, size_t psk_len )
8380{
8381 if( psk == NULL || ssl->handshake == NULL )
8382 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8383
8384 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8385 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8386
8387 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008388 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008389 mbedtls_platform_zeroize( ssl->handshake->psk,
8390 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008391 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008392 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008393 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008394
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008395 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008396 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008397
8398 ssl->handshake->psk_len = psk_len;
8399 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8400
8401 return( 0 );
8402}
8403
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008404void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008406 size_t),
8407 void *p_psk )
8408{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008409 conf->f_psk = f_psk;
8410 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008411}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008412#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008413
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008414#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008415
8416#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008417int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008418{
8419 int ret;
8420
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008421 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8422 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8423 {
8424 mbedtls_mpi_free( &conf->dhm_P );
8425 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008426 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008427 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008428
8429 return( 0 );
8430}
Hanno Becker470a8c42017-10-04 15:28:46 +01008431#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008432
Hanno Beckera90658f2017-10-04 15:29:08 +01008433int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8434 const unsigned char *dhm_P, size_t P_len,
8435 const unsigned char *dhm_G, size_t G_len )
8436{
8437 int ret;
8438
8439 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8440 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8441 {
8442 mbedtls_mpi_free( &conf->dhm_P );
8443 mbedtls_mpi_free( &conf->dhm_G );
8444 return( ret );
8445 }
8446
8447 return( 0 );
8448}
Paul Bakker5121ce52009-01-03 21:22:43 +00008449
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008450int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008451{
8452 int ret;
8453
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008454 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8455 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8456 {
8457 mbedtls_mpi_free( &conf->dhm_P );
8458 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008459 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008460 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008461
8462 return( 0 );
8463}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008464#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008465
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008466#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8467/*
8468 * Set the minimum length for Diffie-Hellman parameters
8469 */
8470void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8471 unsigned int bitlen )
8472{
8473 conf->dhm_min_bitlen = bitlen;
8474}
8475#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8476
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008477#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008478/*
8479 * Set allowed/preferred hashes for handshake signatures
8480 */
8481void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8482 const int *hashes )
8483{
8484 conf->sig_hashes = hashes;
8485}
Hanno Becker947194e2017-04-07 13:25:49 +01008486#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008487
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008488#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008489/*
8490 * Set the allowed elliptic curves
8491 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008492void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008493 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008494{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008495 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008496}
Hanno Becker947194e2017-04-07 13:25:49 +01008497#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008498
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008499#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008500int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008501{
Hanno Becker947194e2017-04-07 13:25:49 +01008502 /* Initialize to suppress unnecessary compiler warning */
8503 size_t hostname_len = 0;
8504
8505 /* Check if new hostname is valid before
8506 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008507 if( hostname != NULL )
8508 {
8509 hostname_len = strlen( hostname );
8510
8511 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8512 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8513 }
8514
8515 /* Now it's clear that we will overwrite the old hostname,
8516 * so we can free it safely */
8517
8518 if( ssl->hostname != NULL )
8519 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008520 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008521 mbedtls_free( ssl->hostname );
8522 }
8523
8524 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008525
Paul Bakker5121ce52009-01-03 21:22:43 +00008526 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008527 {
8528 ssl->hostname = NULL;
8529 }
8530 else
8531 {
8532 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008533 if( ssl->hostname == NULL )
8534 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008535
Hanno Becker947194e2017-04-07 13:25:49 +01008536 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008537
Hanno Becker947194e2017-04-07 13:25:49 +01008538 ssl->hostname[hostname_len] = '\0';
8539 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008540
8541 return( 0 );
8542}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008543#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008544
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008545#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008546void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008547 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008548 const unsigned char *, size_t),
8549 void *p_sni )
8550{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008551 conf->f_sni = f_sni;
8552 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008553}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008554#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008556#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008557int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008558{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008559 size_t cur_len, tot_len;
8560 const char **p;
8561
8562 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008563 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8564 * MUST NOT be truncated."
8565 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008566 */
8567 tot_len = 0;
8568 for( p = protos; *p != NULL; p++ )
8569 {
8570 cur_len = strlen( *p );
8571 tot_len += cur_len;
8572
8573 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008574 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008575 }
8576
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008577 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008578
8579 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008580}
8581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008582const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008583{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008584 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008585}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008586#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008587
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008588void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008589{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008590 conf->max_major_ver = major;
8591 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008592}
8593
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008594void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008595{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008596 conf->min_major_ver = major;
8597 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008598}
8599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008600#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008601void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008602{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008603 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008604}
8605#endif
8606
Janos Follath088ce432017-04-10 12:42:31 +01008607#if defined(MBEDTLS_SSL_SRV_C)
8608void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8609 char cert_req_ca_list )
8610{
8611 conf->cert_req_ca_list = cert_req_ca_list;
8612}
8613#endif
8614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008615#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008616void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008617{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008618 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008619}
8620#endif
8621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008622#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01008623#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008624void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008625{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008626 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008627}
Hanno Beckerf765ce62019-06-21 13:17:14 +01008628#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
8629#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008630void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03008631 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008632{
8633 conf->enforce_extended_master_secret = ems_enf;
8634}
Hanno Beckerf765ce62019-06-21 13:17:14 +01008635#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01008636#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008637
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008638#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008639void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008640{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008641 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008642}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008643#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008645#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008646int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008647{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008648 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008649 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008652 }
8653
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008654 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008655
8656 return( 0 );
8657}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008658#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008660#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008661void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008662{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008663 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008664}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008665#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008667#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008668void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008669{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008670 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008671}
8672#endif
8673
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008674void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008675{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008676 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008677}
8678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008679#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008680void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008681{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008682 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008683}
8684
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008685void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008686{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008687 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008688}
8689
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008690void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008691 const unsigned char period[8] )
8692{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008693 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008694}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008695#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008697#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008698#if defined(MBEDTLS_SSL_CLI_C)
8699void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008700{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008701 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008702}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008703#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008704
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008705#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008706void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8707 mbedtls_ssl_ticket_write_t *f_ticket_write,
8708 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8709 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008710{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008711 conf->f_ticket_write = f_ticket_write;
8712 conf->f_ticket_parse = f_ticket_parse;
8713 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008714}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008715#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008716#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008717
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008718#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8719void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8720 mbedtls_ssl_export_keys_t *f_export_keys,
8721 void *p_export_keys )
8722{
8723 conf->f_export_keys = f_export_keys;
8724 conf->p_export_keys = p_export_keys;
8725}
8726#endif
8727
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008728#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008729void mbedtls_ssl_conf_async_private_cb(
8730 mbedtls_ssl_config *conf,
8731 mbedtls_ssl_async_sign_t *f_async_sign,
8732 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8733 mbedtls_ssl_async_resume_t *f_async_resume,
8734 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008735 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008736{
8737 conf->f_async_sign_start = f_async_sign;
8738 conf->f_async_decrypt_start = f_async_decrypt;
8739 conf->f_async_resume = f_async_resume;
8740 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008741 conf->p_async_config_data = async_config_data;
8742}
8743
Gilles Peskine8f97af72018-04-26 11:46:10 +02008744void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8745{
8746 return( conf->p_async_config_data );
8747}
8748
Gilles Peskine1febfef2018-04-30 11:54:39 +02008749void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008750{
8751 if( ssl->handshake == NULL )
8752 return( NULL );
8753 else
8754 return( ssl->handshake->user_async_ctx );
8755}
8756
Gilles Peskine1febfef2018-04-30 11:54:39 +02008757void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008758 void *ctx )
8759{
8760 if( ssl->handshake != NULL )
8761 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008762}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008763#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008764
Paul Bakker5121ce52009-01-03 21:22:43 +00008765/*
8766 * SSL get accessors
8767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008768size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008769{
8770 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8771}
8772
Hanno Becker8b170a02017-10-10 11:51:19 +01008773int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8774{
8775 /*
8776 * Case A: We're currently holding back
8777 * a message for further processing.
8778 */
8779
8780 if( ssl->keep_current_message == 1 )
8781 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008782 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008783 return( 1 );
8784 }
8785
8786 /*
8787 * Case B: Further records are pending in the current datagram.
8788 */
8789
8790#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008791 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01008792 ssl->in_left > ssl->next_record_offset )
8793 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008794 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008795 return( 1 );
8796 }
8797#endif /* MBEDTLS_SSL_PROTO_DTLS */
8798
8799 /*
8800 * Case C: A handshake message is being processed.
8801 */
8802
Hanno Becker8b170a02017-10-10 11:51:19 +01008803 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8804 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008805 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008806 return( 1 );
8807 }
8808
8809 /*
8810 * Case D: An application data message is being processed
8811 */
8812 if( ssl->in_offt != NULL )
8813 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008814 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008815 return( 1 );
8816 }
8817
8818 /*
8819 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008820 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008821 * we implement support for multiple alerts in single records.
8822 */
8823
8824 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8825 return( 0 );
8826}
8827
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008828uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008829{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008830 if( ssl->session != NULL )
8831 return( ssl->session->verify_result );
8832
8833 if( ssl->session_negotiate != NULL )
8834 return( ssl->session_negotiate->verify_result );
8835
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008836 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008837}
8838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008839const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008840{
Paul Bakker926c8e42013-03-06 10:23:34 +01008841 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008842 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008844 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008845}
8846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008847const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008848{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008849#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008850 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008851 {
8852 switch( ssl->minor_ver )
8853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008854 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008855 return( "DTLSv1.0" );
8856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008857 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008858 return( "DTLSv1.2" );
8859
8860 default:
8861 return( "unknown (DTLS)" );
8862 }
8863 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008864 MBEDTLS_SSL_TRANSPORT_ELSE
8865#endif /* MBEDTLS_SSL_PROTO_DTLS */
8866#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00008867 {
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008868 switch( ssl->minor_ver )
8869 {
8870 case MBEDTLS_SSL_MINOR_VERSION_0:
8871 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008872
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008873 case MBEDTLS_SSL_MINOR_VERSION_1:
8874 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008875
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008876 case MBEDTLS_SSL_MINOR_VERSION_2:
8877 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008878
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008879 case MBEDTLS_SSL_MINOR_VERSION_3:
8880 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008881
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008882 default:
8883 return( "unknown" );
8884 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008885 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008886#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00008887}
8888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008889int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008890{
Hanno Becker3136ede2018-08-17 15:28:19 +01008891 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008892 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008893 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008894
Hanno Becker43395762019-05-03 14:46:38 +01008895 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8896
Hanno Becker78640902018-08-13 16:35:15 +01008897 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008898 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008900#if defined(MBEDTLS_ZLIB_SUPPORT)
8901 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8902 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008903#endif
8904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008905 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008907 case MBEDTLS_MODE_GCM:
8908 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008909 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008910 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008911 transform_expansion = transform->minlen;
8912 break;
8913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008914 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008915
8916 block_size = mbedtls_cipher_get_block_size(
8917 &transform->cipher_ctx_enc );
8918
Hanno Becker3136ede2018-08-17 15:28:19 +01008919 /* Expansion due to the addition of the MAC. */
8920 transform_expansion += transform->maclen;
8921
8922 /* Expansion due to the addition of CBC padding;
8923 * Theoretically up to 256 bytes, but we never use
8924 * more than the block size of the underlying cipher. */
8925 transform_expansion += block_size;
8926
8927 /* For TLS 1.1 or higher, an explicit IV is added
8928 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008929#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8930 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008931 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008932#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008933
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008934 break;
8935
8936 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008938 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008939 }
8940
Hanno Beckera5a2b082019-05-15 14:03:01 +01008941#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008942 if( transform->out_cid_len != 0 )
8943 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008944#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008945
Hanno Becker43395762019-05-03 14:46:38 +01008946 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008947}
8948
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008949#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8950size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8951{
8952 size_t max_len;
8953
8954 /*
8955 * Assume mfl_code is correct since it was checked when set
8956 */
Angus Grattond8213d02016-05-25 20:56:48 +10008957 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008958
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008959 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008960 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008961 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008962 {
Angus Grattond8213d02016-05-25 20:56:48 +10008963 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008964 }
8965
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008966 /* During a handshake, use the value being negotiated */
8967 if( ssl->session_negotiate != NULL &&
8968 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8969 {
8970 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8971 }
8972
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008973 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008974}
8975#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8976
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008977#if defined(MBEDTLS_SSL_PROTO_DTLS)
8978static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8979{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008980 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8981 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8982 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8983 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8984 return ( 0 );
8985
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008986 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8987 return( ssl->mtu );
8988
8989 if( ssl->mtu == 0 )
8990 return( ssl->handshake->mtu );
8991
8992 return( ssl->mtu < ssl->handshake->mtu ?
8993 ssl->mtu : ssl->handshake->mtu );
8994}
8995#endif /* MBEDTLS_SSL_PROTO_DTLS */
8996
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008997int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8998{
8999 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9000
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009001#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9002 !defined(MBEDTLS_SSL_PROTO_DTLS)
9003 (void) ssl;
9004#endif
9005
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009006#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9007 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9008
9009 if( max_len > mfl )
9010 max_len = mfl;
9011#endif
9012
9013#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009014 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009015 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009016 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009017 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9018 const size_t overhead = (size_t) ret;
9019
9020 if( ret < 0 )
9021 return( ret );
9022
9023 if( mtu <= overhead )
9024 {
9025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9026 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9027 }
9028
9029 if( max_len > mtu - overhead )
9030 max_len = mtu - overhead;
9031 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009032#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009033
Hanno Becker0defedb2018-08-10 12:35:02 +01009034#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9035 !defined(MBEDTLS_SSL_PROTO_DTLS)
9036 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009037#endif
9038
9039 return( (int) max_len );
9040}
9041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009042#if defined(MBEDTLS_X509_CRT_PARSE_C)
9043const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009044{
9045 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009046 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009047
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009048#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009049 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009050#else
9051 return( NULL );
9052#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009053}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009054#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009056#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009057int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9058 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009059{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009060 if( ssl == NULL ||
9061 dst == NULL ||
9062 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009063 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009065 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009066 }
9067
Hanno Becker58fccf22019-02-06 14:30:46 +00009068 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009069}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009070#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009071
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009072const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9073{
9074 if( ssl == NULL )
9075 return( NULL );
9076
9077 return( ssl->session );
9078}
9079
Paul Bakker5121ce52009-01-03 21:22:43 +00009080/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009081 * Define ticket header determining Mbed TLS version
9082 * and structure of the ticket.
9083 */
9084
Hanno Becker41527622019-05-16 12:50:45 +01009085/*
Hanno Becker26829e92019-05-28 14:30:45 +01009086 * Define bitflag determining compile-time settings influencing
9087 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009088 */
9089
Hanno Becker26829e92019-05-28 14:30:45 +01009090#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009091#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009092#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009093#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009094#endif /* MBEDTLS_HAVE_TIME */
9095
9096#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009097#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009098#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009099#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009100#endif /* MBEDTLS_X509_CRT_PARSE_C */
9101
9102#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009103#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009104#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009105#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009106#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9107
9108#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009109#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009110#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009111#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009112#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9113
9114#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009115#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009116#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009117#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009118#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9119
9120#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009121#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009122#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009123#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009124#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9125
Hanno Becker41527622019-05-16 12:50:45 +01009126#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9127#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9128#else
9129#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9130#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9131
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009132#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9133#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9134#else
9135#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9136#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9137
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009138#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9139#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9140#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9141#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9142#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9143#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9144#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009145#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009146
Hanno Becker26829e92019-05-28 14:30:45 +01009147#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009148 ( (uint16_t) ( \
9149 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9150 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9151 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9152 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9153 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9154 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009155 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
9156 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009157
Hanno Becker557fe9f2019-05-16 12:41:07 +01009158static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009159 MBEDTLS_VERSION_MAJOR,
9160 MBEDTLS_VERSION_MINOR,
9161 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009162 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9163 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009164};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009165
9166/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009167 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009168 * (in the presentation language of TLS, RFC 8446 section 3)
9169 *
Hanno Becker26829e92019-05-28 14:30:45 +01009170 * opaque mbedtls_version[3]; // major, minor, patch
9171 * opaque session_format[2]; // version-specific 16-bit field determining
9172 * // the format of the remaining
9173 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009174 *
9175 * Note: When updating the format, remember to keep
9176 * these version+format bytes.
9177 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009178 * // In this version, `session_format` determines
9179 * // the setting of those compile-time
9180 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009181 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009182 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009183 * uint8 ciphersuite[2]; // defined by the standard
9184 * uint8 compression; // 0 or 1
9185 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009186 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009187 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009188 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009189 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9190 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9191 * case disabled: uint8_t peer_cert_digest_type;
9192 * opaque peer_cert_digest<0..2^8-1>;
9193 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009194 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009195 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009196 * uint8 mfl_code; // up to 255 according to standard
9197 * uint8 trunc_hmac; // 0 or 1
9198 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009199 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009200 * The order is the same as in the definition of the structure, except
9201 * verify_result is put before peer_cert so that all mandatory fields come
9202 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009203 */
9204int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9205 unsigned char *buf,
9206 size_t buf_len,
9207 size_t *olen )
9208{
9209 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009210 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009211#if defined(MBEDTLS_HAVE_TIME)
9212 uint64_t start;
9213#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009214#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009215#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009216 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009217#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009218#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009219
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009220 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009221 * Add version identifier
9222 */
9223
9224 used += sizeof( ssl_serialized_session_header );
9225
9226 if( used <= buf_len )
9227 {
9228 memcpy( p, ssl_serialized_session_header,
9229 sizeof( ssl_serialized_session_header ) );
9230 p += sizeof( ssl_serialized_session_header );
9231 }
9232
9233 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009234 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009235 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009236#if defined(MBEDTLS_HAVE_TIME)
9237 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009238
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009239 if( used <= buf_len )
9240 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009241 start = (uint64_t) session->start;
9242
9243 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9244 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9245 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9246 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9247 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9248 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9249 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9250 *p++ = (unsigned char)( ( start ) & 0xFF );
9251 }
9252#endif /* MBEDTLS_HAVE_TIME */
9253
9254 /*
9255 * Basic mandatory fields
9256 */
9257 used += 2 /* ciphersuite */
9258 + 1 /* compression */
9259 + 1 /* id_len */
9260 + sizeof( session->id )
9261 + sizeof( session->master )
9262 + 4; /* verify_result */
9263
9264 if( used <= buf_len )
9265 {
9266 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
9267 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
9268
9269 *p++ = (unsigned char)( session->compression & 0xFF );
9270
9271 *p++ = (unsigned char)( session->id_len & 0xFF );
9272 memcpy( p, session->id, 32 );
9273 p += 32;
9274
9275 memcpy( p, session->master, 48 );
9276 p += 48;
9277
9278 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9279 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9280 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9281 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009282 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009283
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009284 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009285 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009286 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009287#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009288#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009289 if( session->peer_cert == NULL )
9290 cert_len = 0;
9291 else
9292 cert_len = session->peer_cert->raw.len;
9293
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009294 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009295
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009296 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009297 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009298 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9299 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9300 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9301
9302 if( session->peer_cert != NULL )
9303 {
9304 memcpy( p, session->peer_cert->raw.p, cert_len );
9305 p += cert_len;
9306 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009307 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009308
Hanno Becker5882dd02019-06-06 16:25:57 +01009309#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009310 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009311 if( session->peer_cert_digest != NULL )
9312 {
9313 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9314 if( used <= buf_len )
9315 {
9316 *p++ = (unsigned char) session->peer_cert_digest_type;
9317 *p++ = (unsigned char) session->peer_cert_digest_len;
9318 memcpy( p, session->peer_cert_digest,
9319 session->peer_cert_digest_len );
9320 p += session->peer_cert_digest_len;
9321 }
9322 }
9323 else
9324 {
9325 used += 2;
9326 if( used <= buf_len )
9327 {
9328 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9329 *p++ = 0;
9330 }
9331 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009332#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009333#endif /* MBEDTLS_X509_CRT_PARSE_C */
9334
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009335 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009336 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009337 */
9338#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009339 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009340
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009341 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009342 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009343 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9344 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9345 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9346
9347 if( session->ticket != NULL )
9348 {
9349 memcpy( p, session->ticket, session->ticket_len );
9350 p += session->ticket_len;
9351 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009352
9353 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9354 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9355 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9356 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009357 }
9358#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9359
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009360 /*
9361 * Misc extension-related info
9362 */
9363#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9364 used += 1;
9365
9366 if( used <= buf_len )
9367 *p++ = session->mfl_code;
9368#endif
9369
9370#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9371 used += 1;
9372
9373 if( used <= buf_len )
9374 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9375#endif
9376
9377#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9378 used += 1;
9379
9380 if( used <= buf_len )
9381 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9382#endif
9383
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009384 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009385 *olen = used;
9386
9387 if( used > buf_len )
9388 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009389
9390 return( 0 );
9391}
9392
9393/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009394 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009395 *
9396 * This internal version is wrapped by a public function that cleans up in
9397 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009398 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009399static int ssl_session_load( mbedtls_ssl_session *session,
9400 const unsigned char *buf,
9401 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009402{
9403 const unsigned char *p = buf;
9404 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009405#if defined(MBEDTLS_HAVE_TIME)
9406 uint64_t start;
9407#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009408#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009409#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009410 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009411#endif
9412#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009413
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009414 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009415 * Check version identifier
9416 */
9417
9418 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009419 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009420
9421 if( memcmp( p, ssl_serialized_session_header,
9422 sizeof( ssl_serialized_session_header ) ) != 0 )
9423 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009424 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009425 }
9426 p += sizeof( ssl_serialized_session_header );
9427
9428 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009429 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009430 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009431#if defined(MBEDTLS_HAVE_TIME)
9432 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009433 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9434
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009435 start = ( (uint64_t) p[0] << 56 ) |
9436 ( (uint64_t) p[1] << 48 ) |
9437 ( (uint64_t) p[2] << 40 ) |
9438 ( (uint64_t) p[3] << 32 ) |
9439 ( (uint64_t) p[4] << 24 ) |
9440 ( (uint64_t) p[5] << 16 ) |
9441 ( (uint64_t) p[6] << 8 ) |
9442 ( (uint64_t) p[7] );
9443 p += 8;
9444
9445 session->start = (time_t) start;
9446#endif /* MBEDTLS_HAVE_TIME */
9447
9448 /*
9449 * Basic mandatory fields
9450 */
9451 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9452 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9453
9454 session->ciphersuite = ( p[0] << 8 ) | p[1];
9455 p += 2;
9456
9457 session->compression = *p++;
9458
9459 session->id_len = *p++;
9460 memcpy( session->id, p, 32 );
9461 p += 32;
9462
9463 memcpy( session->master, p, 48 );
9464 p += 48;
9465
9466 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9467 ( (uint32_t) p[1] << 16 ) |
9468 ( (uint32_t) p[2] << 8 ) |
9469 ( (uint32_t) p[3] );
9470 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009471
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009472 /* Immediately clear invalid pointer values that have been read, in case
9473 * we exit early before we replaced them with valid ones. */
9474#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009475#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009476 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +01009477#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009478 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +01009479#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009480#endif
9481#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9482 session->ticket = NULL;
9483#endif
9484
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009485 /*
9486 * Peer certificate
9487 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009488#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009489#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009490 if( 3 > (size_t)( end - p ) )
9491 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9492
9493 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9494 p += 3;
9495
9496 if( cert_len == 0 )
9497 {
9498 session->peer_cert = NULL;
9499 }
9500 else
9501 {
9502 int ret;
9503
9504 if( cert_len > (size_t)( end - p ) )
9505 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9506
9507 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9508
9509 if( session->peer_cert == NULL )
9510 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9511
9512 mbedtls_x509_crt_init( session->peer_cert );
9513
9514 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9515 p, cert_len ) ) != 0 )
9516 {
9517 mbedtls_x509_crt_free( session->peer_cert );
9518 mbedtls_free( session->peer_cert );
9519 session->peer_cert = NULL;
9520 return( ret );
9521 }
9522
9523 p += cert_len;
9524 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009525#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009526 /* Deserialize CRT digest from the end of the ticket. */
9527 if( 2 > (size_t)( end - p ) )
9528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9529
9530 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9531 session->peer_cert_digest_len = (size_t) *p++;
9532
9533 if( session->peer_cert_digest_len != 0 )
9534 {
Hanno Becker2326d202019-06-06 14:54:55 +01009535 const mbedtls_md_info_t *md_info =
9536 mbedtls_md_info_from_type( session->peer_cert_digest_type );
9537 if( md_info == NULL )
9538 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9539 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
9540 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9541
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009542 if( session->peer_cert_digest_len > (size_t)( end - p ) )
9543 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9544
9545 session->peer_cert_digest =
9546 mbedtls_calloc( 1, session->peer_cert_digest_len );
9547 if( session->peer_cert_digest == NULL )
9548 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9549
9550 memcpy( session->peer_cert_digest, p,
9551 session->peer_cert_digest_len );
9552 p += session->peer_cert_digest_len;
9553 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009554#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009555#endif /* MBEDTLS_X509_CRT_PARSE_C */
9556
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009557 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009558 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009559 */
9560#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9561 if( 3 > (size_t)( end - p ) )
9562 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9563
9564 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9565 p += 3;
9566
9567 if( session->ticket_len != 0 )
9568 {
9569 if( session->ticket_len > (size_t)( end - p ) )
9570 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9571
9572 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9573 if( session->ticket == NULL )
9574 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9575
9576 memcpy( session->ticket, p, session->ticket_len );
9577 p += session->ticket_len;
9578 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009579
9580 if( 4 > (size_t)( end - p ) )
9581 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9582
9583 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9584 ( (uint32_t) p[1] << 16 ) |
9585 ( (uint32_t) p[2] << 8 ) |
9586 ( (uint32_t) p[3] );
9587 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009588#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9589
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009590 /*
9591 * Misc extension-related info
9592 */
9593#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9594 if( 1 > (size_t)( end - p ) )
9595 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9596
9597 session->mfl_code = *p++;
9598#endif
9599
9600#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9601 if( 1 > (size_t)( end - p ) )
9602 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9603
9604 session->trunc_hmac = *p++;
9605#endif
9606
9607#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9608 if( 1 > (size_t)( end - p ) )
9609 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9610
9611 session->encrypt_then_mac = *p++;
9612#endif
9613
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009614 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009615 if( p != end )
9616 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9617
9618 return( 0 );
9619}
9620
9621/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009622 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009623 */
9624int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9625 const unsigned char *buf,
9626 size_t len )
9627{
9628 int ret = ssl_session_load( session, buf, len );
9629
9630 if( ret != 0 )
9631 mbedtls_ssl_session_free( session );
9632
9633 return( ret );
9634}
9635
9636/*
Paul Bakker1961b702013-01-25 14:49:24 +01009637 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009639int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009640{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009641 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009642
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009643 if( ssl == NULL || ssl->conf == NULL )
9644 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009646#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009647 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009648 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009649#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009650#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009651 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009652 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009653#endif
9654
Paul Bakker1961b702013-01-25 14:49:24 +01009655 return( ret );
9656}
9657
9658/*
9659 * Perform the SSL handshake
9660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009661int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009662{
9663 int ret = 0;
9664
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009665 if( ssl == NULL || ssl->conf == NULL )
9666 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009670 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009672 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009673
9674 if( ret != 0 )
9675 break;
9676 }
9677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009679
9680 return( ret );
9681}
9682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009683#if defined(MBEDTLS_SSL_RENEGOTIATION)
9684#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009685/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009686 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009687 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009688static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009689{
9690 int ret;
9691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009693
9694 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009695 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9696 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009697
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009698 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009699 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009700 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009701 return( ret );
9702 }
9703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009705
9706 return( 0 );
9707}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009708#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009709
9710/*
9711 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009712 * - any side: calling mbedtls_ssl_renegotiate(),
9713 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9714 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009715 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009716 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009717 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009719static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009720{
9721 int ret;
9722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009724
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009725 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9726 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009727
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009728 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9729 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009730#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009731 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009732 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009733 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009734 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009735 ssl->handshake->out_msg_seq = 1;
9736 else
9737 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009738 }
9739#endif
9740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009741 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9742 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009744 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009746 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009747 return( ret );
9748 }
9749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009751
9752 return( 0 );
9753}
9754
9755/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009756 * Renegotiate current connection on client,
9757 * or request renegotiation on server
9758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009759int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009760{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009761 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009762
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009763 if( ssl == NULL || ssl->conf == NULL )
9764 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009766#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009767 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009768 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009770 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9771 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009773 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009774
9775 /* Did we already try/start sending HelloRequest? */
9776 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009777 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009778
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009779 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009780 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009781#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009783#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009784 /*
9785 * On client, either start the renegotiation process or,
9786 * if already in progress, continue the handshake
9787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009788 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009790 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9791 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009792
9793 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009795 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009796 return( ret );
9797 }
9798 }
9799 else
9800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009801 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009803 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009804 return( ret );
9805 }
9806 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009807#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009808
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009809 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009810}
9811
9812/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009813 * Check record counters and renegotiate if they're above the limit.
9814 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009815static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009816{
Andres AG2196c7f2016-12-15 17:01:16 +00009817 size_t ep_len = ssl_ep_len( ssl );
9818 int in_ctr_cmp;
9819 int out_ctr_cmp;
9820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009821 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9822 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009823 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009824 {
9825 return( 0 );
9826 }
9827
Andres AG2196c7f2016-12-15 17:01:16 +00009828 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9829 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009830 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009831 ssl->conf->renego_period + ep_len, 8 - ep_len );
9832
9833 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009834 {
9835 return( 0 );
9836 }
9837
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009840}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009842
9843/*
9844 * Receive application data decrypted from the SSL layer
9845 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009846int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009847{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009848 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009849 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009850
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009851 if( ssl == NULL || ssl->conf == NULL )
9852 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009856#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009857 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009859 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009860 return( ret );
9861
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009862 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009863 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009864 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009865 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009866 return( ret );
9867 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009868 }
9869#endif
9870
Hanno Becker4a810fb2017-05-24 16:27:30 +01009871 /*
9872 * Check if renegotiation is necessary and/or handshake is
9873 * in process. If yes, perform/continue, and fall through
9874 * if an unexpected packet is received while the client
9875 * is waiting for the ServerHello.
9876 *
9877 * (There is no equivalent to the last condition on
9878 * the server-side as it is not treated as within
9879 * a handshake while waiting for the ClientHello
9880 * after a renegotiation request.)
9881 */
9882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009883#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009884 ret = ssl_check_ctr_renegotiate( ssl );
9885 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9886 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009888 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009889 return( ret );
9890 }
9891#endif
9892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009893 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009895 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009896 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9897 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009900 return( ret );
9901 }
9902 }
9903
Hanno Beckere41158b2017-10-23 13:30:32 +01009904 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009905 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009906 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009907 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009908 if( ssl->f_get_timer != NULL &&
9909 ssl->f_get_timer( ssl->p_timer ) == -1 )
9910 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009911 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009912 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009913
Hanno Becker327c93b2018-08-15 13:56:18 +01009914 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009915 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009916 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9917 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009918
Hanno Becker4a810fb2017-05-24 16:27:30 +01009919 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9920 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009921 }
9922
9923 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009924 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009925 {
9926 /*
9927 * OpenSSL sends empty messages to randomize the IV
9928 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009929 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009931 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009932 return( 0 );
9933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009934 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009935 return( ret );
9936 }
9937 }
9938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009939 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009942
Hanno Becker4a810fb2017-05-24 16:27:30 +01009943 /*
9944 * - For client-side, expect SERVER_HELLO_REQUEST.
9945 * - For server-side, expect CLIENT_HELLO.
9946 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9947 */
9948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009949#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009950 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009951 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009952 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009955
9956 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009957#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009958 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009959 {
9960 continue;
9961 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009962 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009963#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009964#if defined(MBEDTLS_SSL_PROTO_TLS)
9965 {
9966 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9967 }
9968#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009969 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009970#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009971
Hanno Becker4a810fb2017-05-24 16:27:30 +01009972#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009973 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009974 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009977
9978 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009979#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009980 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009981 {
9982 continue;
9983 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009984 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009985#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009986#if defined(MBEDTLS_SSL_PROTO_TLS)
9987 {
9988 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9989 }
9990#endif
Paul Bakker48916f92012-09-16 19:57:18 +00009991 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009992#endif /* MBEDTLS_SSL_SRV_C */
9993
Hanno Becker21df7f92017-10-17 11:03:26 +01009994#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009995 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009996 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9997 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9998 ssl->conf->allow_legacy_renegotiation ==
9999 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10000 {
10001 /*
10002 * Accept renegotiation request
10003 */
Paul Bakker48916f92012-09-16 19:57:18 +000010004
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010005 /* DTLS clients need to know renego is server-initiated */
10006#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010007 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010008 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10009 {
10010 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10011 }
10012#endif
10013 ret = ssl_start_renegotiation( ssl );
10014 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10015 ret != 0 )
10016 {
10017 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10018 return( ret );
10019 }
10020 }
10021 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010022#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010023 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010024 /*
10025 * Refuse renegotiation
10026 */
10027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010028 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010030#if defined(MBEDTLS_SSL_PROTO_SSL3)
10031 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010032 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010033 /* SSLv3 does not have a "no_renegotiation" warning, so
10034 we send a fatal alert and abort the connection. */
10035 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10036 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10037 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010038 }
10039 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010040#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10041#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10042 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10043 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010045 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10046 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10047 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010048 {
10049 return( ret );
10050 }
Paul Bakker48916f92012-09-16 19:57:18 +000010051 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010052 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010053#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10054 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10057 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010058 }
Paul Bakker48916f92012-09-16 19:57:18 +000010059 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010060
Hanno Becker90333da2017-10-10 11:27:13 +010010061 /* At this point, we don't know whether the renegotiation has been
10062 * completed or not. The cases to consider are the following:
10063 * 1) The renegotiation is complete. In this case, no new record
10064 * has been read yet.
10065 * 2) The renegotiation is incomplete because the client received
10066 * an application data record while awaiting the ServerHello.
10067 * 3) The renegotiation is incomplete because the client received
10068 * a non-handshake, non-application data message while awaiting
10069 * the ServerHello.
10070 * In each of these case, looping will be the proper action:
10071 * - For 1), the next iteration will read a new record and check
10072 * if it's application data.
10073 * - For 2), the loop condition isn't satisfied as application data
10074 * is present, hence continue is the same as break
10075 * - For 3), the loop condition is satisfied and read_record
10076 * will re-deliver the message that was held back by the client
10077 * when expecting the ServerHello.
10078 */
10079 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010080 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010081#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010082 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010083 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010084 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010085 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010086 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010088 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010089 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010090 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010091 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010092 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010093 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010094#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010096 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10097 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010100 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010101 }
10102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010103 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10106 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010107 }
10108
10109 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010110
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010111 /* We're going to return something now, cancel timer,
10112 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010113 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010114 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010115
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010116#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010117 /* If we requested renego but received AppData, resend HelloRequest.
10118 * Do it now, after setting in_offt, to avoid taking this branch
10119 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010120#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010121 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010122 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010123 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010124 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010126 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010127 return( ret );
10128 }
10129 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010130#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010131#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010132 }
10133
10134 n = ( len < ssl->in_msglen )
10135 ? len : ssl->in_msglen;
10136
10137 memcpy( buf, ssl->in_offt, n );
10138 ssl->in_msglen -= n;
10139
10140 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010141 {
10142 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010143 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010144 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010145 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010146 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010147 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010148 /* more data available */
10149 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010150 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010153
Paul Bakker23986e52011-04-24 08:57:21 +000010154 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010155}
10156
10157/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010158 * Send application data to be encrypted by the SSL layer, taking care of max
10159 * fragment length and buffer size.
10160 *
10161 * According to RFC 5246 Section 6.2.1:
10162 *
10163 * Zero-length fragments of Application data MAY be sent as they are
10164 * potentially useful as a traffic analysis countermeasure.
10165 *
10166 * Therefore, it is possible that the input message length is 0 and the
10167 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010168 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010169static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010170 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010171{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010172 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10173 const size_t max_len = (size_t) ret;
10174
10175 if( ret < 0 )
10176 {
10177 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10178 return( ret );
10179 }
10180
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010181 if( len > max_len )
10182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010183#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010184 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010187 "maximum fragment length: %d > %d",
10188 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010189 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010190 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010191 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010192#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010193#if defined(MBEDTLS_SSL_PROTO_TLS)
10194 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010195 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010196 }
10197#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010198 }
Paul Bakker887bd502011-06-08 13:10:54 +000010199
Paul Bakker5121ce52009-01-03 21:22:43 +000010200 if( ssl->out_left != 0 )
10201 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010202 /*
10203 * The user has previously tried to send the data and
10204 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10205 * written. In this case, we expect the high-level write function
10206 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010208 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010210 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010211 return( ret );
10212 }
10213 }
Paul Bakker887bd502011-06-08 13:10:54 +000010214 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010215 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010216 /*
10217 * The user is trying to send a message the first time, so we need to
10218 * copy the data into the internal buffers and setup the data structure
10219 * to keep track of partial writes
10220 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010221 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010222 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010223 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010224
Hanno Becker67bc7c32018-08-06 11:33:50 +010010225 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010227 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010228 return( ret );
10229 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010230 }
10231
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010232 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010233}
10234
10235/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010236 * Write application data, doing 1/n-1 splitting if necessary.
10237 *
10238 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010239 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010240 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010242#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010243static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010244 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010245{
10246 int ret;
10247
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010248 if( ssl->conf->cbc_record_splitting ==
10249 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010250 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010251 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10252 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10253 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010254 {
10255 return( ssl_write_real( ssl, buf, len ) );
10256 }
10257
10258 if( ssl->split_done == 0 )
10259 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010260 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010261 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010262 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010263 }
10264
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010265 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10266 return( ret );
10267 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010268
10269 return( ret + 1 );
10270}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010271#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010272
10273/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010274 * Write application data (public-facing wrapper)
10275 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010276int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010277{
10278 int ret;
10279
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010281
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010282 if( ssl == NULL || ssl->conf == NULL )
10283 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10284
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010285#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010286 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10287 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010288 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010289 return( ret );
10290 }
10291#endif
10292
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010293 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010294 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010295 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010296 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010297 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010298 return( ret );
10299 }
10300 }
10301
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010302#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010303 ret = ssl_write_split( ssl, buf, len );
10304#else
10305 ret = ssl_write_real( ssl, buf, len );
10306#endif
10307
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010309
10310 return( ret );
10311}
10312
10313/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010314 * Notify the peer that the connection is being closed
10315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010316int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010317{
10318 int ret;
10319
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010320 if( ssl == NULL || ssl->conf == NULL )
10321 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010324
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010325 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010326 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010328 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010330 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10331 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10332 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010335 return( ret );
10336 }
10337 }
10338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010340
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010341 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010342}
10343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010344void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010345{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010346 if( transform == NULL )
10347 return;
10348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010349#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010350 deflateEnd( &transform->ctx_deflate );
10351 inflateEnd( &transform->ctx_inflate );
10352#endif
10353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010354 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10355 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010356
Hanno Becker92231322018-01-03 15:32:51 +000010357#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010358 mbedtls_md_free( &transform->md_ctx_enc );
10359 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010360#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010361
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010362 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010363}
10364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010365#if defined(MBEDTLS_X509_CRT_PARSE_C)
10366static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010367{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010368 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010369
10370 while( cur != NULL )
10371 {
10372 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010373 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010374 cur = next;
10375 }
10376}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010377#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010378
Hanno Becker0271f962018-08-16 13:23:47 +010010379#if defined(MBEDTLS_SSL_PROTO_DTLS)
10380
10381static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10382{
10383 unsigned offset;
10384 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10385
10386 if( hs == NULL )
10387 return;
10388
Hanno Becker283f5ef2018-08-24 09:34:47 +010010389 ssl_free_buffered_record( ssl );
10390
Hanno Becker0271f962018-08-16 13:23:47 +010010391 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010392 ssl_buffering_free_slot( ssl, offset );
10393}
10394
10395static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10396 uint8_t slot )
10397{
10398 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10399 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010400
10401 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10402 return;
10403
Hanno Beckere605b192018-08-21 15:59:07 +010010404 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010405 {
Hanno Beckere605b192018-08-21 15:59:07 +010010406 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010407 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010408 mbedtls_free( hs_buf->data );
10409 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010410 }
10411}
10412
10413#endif /* MBEDTLS_SSL_PROTO_DTLS */
10414
Gilles Peskine9b562d52018-04-25 20:32:43 +020010415void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010416{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010417 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10418
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010419 if( handshake == NULL )
10420 return;
10421
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010422#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10423 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10424 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010425 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010426 handshake->async_in_progress = 0;
10427 }
10428#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10429
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010430#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10431 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10432 mbedtls_md5_free( &handshake->fin_md5 );
10433 mbedtls_sha1_free( &handshake->fin_sha1 );
10434#endif
10435#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10436#if defined(MBEDTLS_SHA256_C)
10437 mbedtls_sha256_free( &handshake->fin_sha256 );
10438#endif
10439#if defined(MBEDTLS_SHA512_C)
10440 mbedtls_sha512_free( &handshake->fin_sha512 );
10441#endif
10442#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010444#if defined(MBEDTLS_DHM_C)
10445 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010446#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010447#if defined(MBEDTLS_ECDH_C)
10448 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010449#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010450#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010451 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010452#if defined(MBEDTLS_SSL_CLI_C)
10453 mbedtls_free( handshake->ecjpake_cache );
10454 handshake->ecjpake_cache = NULL;
10455 handshake->ecjpake_cache_len = 0;
10456#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010457#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010458
Janos Follath4ae5c292016-02-10 11:27:43 +000010459#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10460 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010461 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010462 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010463#endif
10464
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010465#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10466 if( handshake->psk != NULL )
10467 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010468 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010469 mbedtls_free( handshake->psk );
10470 }
10471#endif
10472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010473#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10474 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010475 /*
10476 * Free only the linked list wrapper, not the keys themselves
10477 * since the belong to the SNI callback
10478 */
10479 if( handshake->sni_key_cert != NULL )
10480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010481 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010482
10483 while( cur != NULL )
10484 {
10485 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010486 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010487 cur = next;
10488 }
10489 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010490#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010491
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010492#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010493 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000010494 if( handshake->ecrs_peer_cert != NULL )
10495 {
10496 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10497 mbedtls_free( handshake->ecrs_peer_cert );
10498 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010499#endif
10500
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000010501#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10502 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10503 mbedtls_pk_free( &handshake->peer_pubkey );
10504#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010506#if defined(MBEDTLS_SSL_PROTO_DTLS)
10507 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010508 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010509 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010510#endif
10511
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010512 mbedtls_platform_zeroize( handshake,
10513 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010514}
10515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010516void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010517{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010518 if( session == NULL )
10519 return;
10520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010521#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000010522 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010523#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010524
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010525#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010526 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010527#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010528
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010529 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010530}
10531
Paul Bakker5121ce52009-01-03 21:22:43 +000010532/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010533 * Serialize a full SSL context
10534 */
10535int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
10536 unsigned char *buf,
10537 size_t buf_len,
10538 size_t *olen )
10539{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010540 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010541 (void) ssl;
10542
10543 if( buf != NULL )
10544 memset( buf, 0, buf_len );
10545
10546 *olen = 0;
10547
10548 return( 0 );
10549}
10550
10551/*
10552 * Deserialize a full SSL context
10553 */
10554int mbedtls_ssl_context_load( mbedtls_ssl_context *ssl,
10555 const unsigned char *buf,
10556 size_t len )
10557{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010558 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010559 (void) ssl;
10560 (void) buf;
10561 (void) len;
10562
10563 return( 0 );
10564}
10565
10566/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010567 * Free an SSL context
10568 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010569void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010570{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010571 if( ssl == NULL )
10572 return;
10573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010575
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010576 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010577 {
Angus Grattond8213d02016-05-25 20:56:48 +100010578 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010579 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010580 }
10581
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010582 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010583 {
Angus Grattond8213d02016-05-25 20:56:48 +100010584 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010585 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010586 }
10587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010588#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010589 if( ssl->compress_buf != NULL )
10590 {
Angus Grattond8213d02016-05-25 20:56:48 +100010591 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010592 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010593 }
10594#endif
10595
Paul Bakker48916f92012-09-16 19:57:18 +000010596 if( ssl->transform )
10597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010598 mbedtls_ssl_transform_free( ssl->transform );
10599 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010600 }
10601
10602 if( ssl->handshake )
10603 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010604 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010605 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10606 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010608 mbedtls_free( ssl->handshake );
10609 mbedtls_free( ssl->transform_negotiate );
10610 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010611 }
10612
Paul Bakkerc0463502013-02-14 11:19:38 +010010613 if( ssl->session )
10614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010615 mbedtls_ssl_session_free( ssl->session );
10616 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010617 }
10618
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010619#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010620 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010621 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010622 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010623 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010624 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010625#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010627#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10628 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10631 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010632 }
10633#endif
10634
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010635#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010636 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010637#endif
10638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010639 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010640
Paul Bakker86f04f42013-02-14 11:20:09 +010010641 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010642 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010643}
10644
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010645/*
10646 * Initialze mbedtls_ssl_config
10647 */
10648void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10649{
10650 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010010651
10652#if !defined(MBEDTLS_SSL_PROTO_TLS)
10653 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
10654#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010655}
10656
Simon Butcherc97b6972015-12-27 23:48:17 +000010657#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010658static int ssl_preset_default_hashes[] = {
10659#if defined(MBEDTLS_SHA512_C)
10660 MBEDTLS_MD_SHA512,
10661 MBEDTLS_MD_SHA384,
10662#endif
10663#if defined(MBEDTLS_SHA256_C)
10664 MBEDTLS_MD_SHA256,
10665 MBEDTLS_MD_SHA224,
10666#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010667#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010668 MBEDTLS_MD_SHA1,
10669#endif
10670 MBEDTLS_MD_NONE
10671};
Simon Butcherc97b6972015-12-27 23:48:17 +000010672#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010673
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010674static int ssl_preset_suiteb_ciphersuites[] = {
10675 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10676 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10677 0
10678};
10679
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010680#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010681static int ssl_preset_suiteb_hashes[] = {
10682 MBEDTLS_MD_SHA256,
10683 MBEDTLS_MD_SHA384,
10684 MBEDTLS_MD_NONE
10685};
10686#endif
10687
10688#if defined(MBEDTLS_ECP_C)
10689static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10690 MBEDTLS_ECP_DP_SECP256R1,
10691 MBEDTLS_ECP_DP_SECP384R1,
10692 MBEDTLS_ECP_DP_NONE
10693};
10694#endif
10695
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010696/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010697 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010698 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010699int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010700 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010701{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010702#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010703 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010704#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010705
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010706 /* Use the functions here so that they are covered in tests,
10707 * but otherwise access member directly for efficiency */
10708 mbedtls_ssl_conf_endpoint( conf, endpoint );
10709 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010710
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010711 /*
10712 * Things that are common to all presets
10713 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010714#if defined(MBEDTLS_SSL_CLI_C)
10715 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10716 {
10717 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10718#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10719 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10720#endif
10721 }
10722#endif
10723
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010724#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010725 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010726#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010727
10728#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10729 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10730#endif
10731
10732#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010010733#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010734 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010010735#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
10736#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030010737 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030010738 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010010739#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010740#endif
10741
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010742#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10743 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10744#endif
10745
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010746#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010747 conf->f_cookie_write = ssl_cookie_write_dummy;
10748 conf->f_cookie_check = ssl_cookie_check_dummy;
10749#endif
10750
10751#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10752 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10753#endif
10754
Janos Follath088ce432017-04-10 12:42:31 +010010755#if defined(MBEDTLS_SSL_SRV_C)
10756 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10757#endif
10758
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010759#if defined(MBEDTLS_SSL_PROTO_DTLS)
10760 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10761 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10762#endif
10763
10764#if defined(MBEDTLS_SSL_RENEGOTIATION)
10765 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010766 memset( conf->renego_period, 0x00, 2 );
10767 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010768#endif
10769
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010770#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10771 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10772 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010773 const unsigned char dhm_p[] =
10774 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10775 const unsigned char dhm_g[] =
10776 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10777
Hanno Beckera90658f2017-10-04 15:29:08 +010010778 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10779 dhm_p, sizeof( dhm_p ),
10780 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010781 {
10782 return( ret );
10783 }
10784 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010785#endif
10786
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010787 /*
10788 * Preset-specific defaults
10789 */
10790 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010791 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010792 /*
10793 * NSA Suite B
10794 */
10795 case MBEDTLS_SSL_PRESET_SUITEB:
10796 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10797 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10798 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10799 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10800
10801 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10802 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10803 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10804 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10805 ssl_preset_suiteb_ciphersuites;
10806
10807#if defined(MBEDTLS_X509_CRT_PARSE_C)
10808 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010809#endif
10810
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010811#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010812 conf->sig_hashes = ssl_preset_suiteb_hashes;
10813#endif
10814
10815#if defined(MBEDTLS_ECP_C)
10816 conf->curve_list = ssl_preset_suiteb_curves;
10817#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010818 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010819
10820 /*
10821 * Default
10822 */
10823 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010824 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10825 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10826 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10827 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10828 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10829 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10830 MBEDTLS_SSL_MIN_MINOR_VERSION :
10831 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010832 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10833 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10834
10835#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010836 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010837 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10838#endif
10839
10840 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10841 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10842 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10843 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10844 mbedtls_ssl_list_ciphersuites();
10845
10846#if defined(MBEDTLS_X509_CRT_PARSE_C)
10847 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10848#endif
10849
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010850#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010851 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010852#endif
10853
10854#if defined(MBEDTLS_ECP_C)
10855 conf->curve_list = mbedtls_ecp_grp_id_list();
10856#endif
10857
10858#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10859 conf->dhm_min_bitlen = 1024;
10860#endif
10861 }
10862
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010863 return( 0 );
10864}
10865
10866/*
10867 * Free mbedtls_ssl_config
10868 */
10869void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10870{
10871#if defined(MBEDTLS_DHM_C)
10872 mbedtls_mpi_free( &conf->dhm_P );
10873 mbedtls_mpi_free( &conf->dhm_G );
10874#endif
10875
10876#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10877 if( conf->psk != NULL )
10878 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010879 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010880 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010881 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010882 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010883 }
10884
10885 if( conf->psk_identity != NULL )
10886 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010887 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010888 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010889 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010890 conf->psk_identity_len = 0;
10891 }
10892#endif
10893
10894#if defined(MBEDTLS_X509_CRT_PARSE_C)
10895 ssl_key_cert_free( conf->key_cert );
10896#endif
10897
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010898 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010899}
10900
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010901#if defined(MBEDTLS_PK_C) && \
10902 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010903/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010904 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010905 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010906unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010907{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010908#if defined(MBEDTLS_RSA_C)
10909 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10910 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010911#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010912#if defined(MBEDTLS_ECDSA_C)
10913 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10914 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010915#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010916 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010917}
10918
Hanno Becker7e5437a2017-04-28 17:15:26 +010010919unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10920{
10921 switch( type ) {
10922 case MBEDTLS_PK_RSA:
10923 return( MBEDTLS_SSL_SIG_RSA );
10924 case MBEDTLS_PK_ECDSA:
10925 case MBEDTLS_PK_ECKEY:
10926 return( MBEDTLS_SSL_SIG_ECDSA );
10927 default:
10928 return( MBEDTLS_SSL_SIG_ANON );
10929 }
10930}
10931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010932mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010933{
10934 switch( sig )
10935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010936#if defined(MBEDTLS_RSA_C)
10937 case MBEDTLS_SSL_SIG_RSA:
10938 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010939#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010940#if defined(MBEDTLS_ECDSA_C)
10941 case MBEDTLS_SSL_SIG_ECDSA:
10942 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010943#endif
10944 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010945 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010946 }
10947}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010948#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010949
Hanno Becker7e5437a2017-04-28 17:15:26 +010010950#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10951 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10952
10953/* Find an entry in a signature-hash set matching a given hash algorithm. */
10954mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10955 mbedtls_pk_type_t sig_alg )
10956{
10957 switch( sig_alg )
10958 {
10959 case MBEDTLS_PK_RSA:
10960 return( set->rsa );
10961 case MBEDTLS_PK_ECDSA:
10962 return( set->ecdsa );
10963 default:
10964 return( MBEDTLS_MD_NONE );
10965 }
10966}
10967
10968/* Add a signature-hash-pair to a signature-hash set */
10969void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10970 mbedtls_pk_type_t sig_alg,
10971 mbedtls_md_type_t md_alg )
10972{
10973 switch( sig_alg )
10974 {
10975 case MBEDTLS_PK_RSA:
10976 if( set->rsa == MBEDTLS_MD_NONE )
10977 set->rsa = md_alg;
10978 break;
10979
10980 case MBEDTLS_PK_ECDSA:
10981 if( set->ecdsa == MBEDTLS_MD_NONE )
10982 set->ecdsa = md_alg;
10983 break;
10984
10985 default:
10986 break;
10987 }
10988}
10989
10990/* Allow exactly one hash algorithm for each signature. */
10991void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10992 mbedtls_md_type_t md_alg )
10993{
10994 set->rsa = md_alg;
10995 set->ecdsa = md_alg;
10996}
10997
10998#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10999 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11000
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011001/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011002 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011003 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011004mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011005{
11006 switch( hash )
11007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011008#if defined(MBEDTLS_MD5_C)
11009 case MBEDTLS_SSL_HASH_MD5:
11010 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011011#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011012#if defined(MBEDTLS_SHA1_C)
11013 case MBEDTLS_SSL_HASH_SHA1:
11014 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011015#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011016#if defined(MBEDTLS_SHA256_C)
11017 case MBEDTLS_SSL_HASH_SHA224:
11018 return( MBEDTLS_MD_SHA224 );
11019 case MBEDTLS_SSL_HASH_SHA256:
11020 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011021#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011022#if defined(MBEDTLS_SHA512_C)
11023 case MBEDTLS_SSL_HASH_SHA384:
11024 return( MBEDTLS_MD_SHA384 );
11025 case MBEDTLS_SSL_HASH_SHA512:
11026 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011027#endif
11028 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011029 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011030 }
11031}
11032
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011033/*
11034 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11035 */
11036unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11037{
11038 switch( md )
11039 {
11040#if defined(MBEDTLS_MD5_C)
11041 case MBEDTLS_MD_MD5:
11042 return( MBEDTLS_SSL_HASH_MD5 );
11043#endif
11044#if defined(MBEDTLS_SHA1_C)
11045 case MBEDTLS_MD_SHA1:
11046 return( MBEDTLS_SSL_HASH_SHA1 );
11047#endif
11048#if defined(MBEDTLS_SHA256_C)
11049 case MBEDTLS_MD_SHA224:
11050 return( MBEDTLS_SSL_HASH_SHA224 );
11051 case MBEDTLS_MD_SHA256:
11052 return( MBEDTLS_SSL_HASH_SHA256 );
11053#endif
11054#if defined(MBEDTLS_SHA512_C)
11055 case MBEDTLS_MD_SHA384:
11056 return( MBEDTLS_SSL_HASH_SHA384 );
11057 case MBEDTLS_MD_SHA512:
11058 return( MBEDTLS_SSL_HASH_SHA512 );
11059#endif
11060 default:
11061 return( MBEDTLS_SSL_HASH_NONE );
11062 }
11063}
11064
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011065#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011066/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011067 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011068 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011069 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011070int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011072 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011073
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011074 if( ssl->conf->curve_list == NULL )
11075 return( -1 );
11076
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011077 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011078 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011079 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011080
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011081 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011082}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011083#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011084
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011085#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011086/*
11087 * Check if a hash proposed by the peer is in our list.
11088 * Return 0 if we're willing to use it, -1 otherwise.
11089 */
11090int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11091 mbedtls_md_type_t md )
11092{
11093 const int *cur;
11094
11095 if( ssl->conf->sig_hashes == NULL )
11096 return( -1 );
11097
11098 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11099 if( *cur == (int) md )
11100 return( 0 );
11101
11102 return( -1 );
11103}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011104#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011106#if defined(MBEDTLS_X509_CRT_PARSE_C)
11107int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11108 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011109 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011110 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011111{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011112 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011113#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011114 int usage = 0;
11115#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011116#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011117 const char *ext_oid;
11118 size_t ext_len;
11119#endif
11120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011121#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11122 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011123 ((void) cert);
11124 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011125 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011126#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011128#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11129 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011130 {
11131 /* Server part of the key exchange */
11132 switch( ciphersuite->key_exchange )
11133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011134 case MBEDTLS_KEY_EXCHANGE_RSA:
11135 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011136 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011137 break;
11138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011139 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11140 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11141 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11142 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011143 break;
11144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011145 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11146 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011147 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011148 break;
11149
11150 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011151 case MBEDTLS_KEY_EXCHANGE_NONE:
11152 case MBEDTLS_KEY_EXCHANGE_PSK:
11153 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11154 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011155 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011156 usage = 0;
11157 }
11158 }
11159 else
11160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011161 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11162 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011163 }
11164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011165 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011166 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011167 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011168 ret = -1;
11169 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011170#else
11171 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011172#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011174#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11175 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011177 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11178 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011179 }
11180 else
11181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011182 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11183 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011184 }
11185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011186 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011187 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011188 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011189 ret = -1;
11190 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011191#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011192
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011193 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011194}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011195#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011196
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011197/*
11198 * Convert version numbers to/from wire format
11199 * and, for DTLS, to/from TLS equivalent.
11200 *
11201 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011202 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011203 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11204 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011206void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011207 unsigned char ver[2] )
11208{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011209#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11210 ((void) transport);
11211#endif
11212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011213#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011214 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011216 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011217 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11218
11219 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11220 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11221 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011222 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011223#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011224#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011225 {
11226 ver[0] = (unsigned char) major;
11227 ver[1] = (unsigned char) minor;
11228 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011229#endif
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011230}
11231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011232void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011233 const unsigned char ver[2] )
11234{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011235#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11236 ((void) transport);
11237#endif
11238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011239#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011240 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011241 {
11242 *major = 255 - ver[0] + 2;
11243 *minor = 255 - ver[1] + 1;
11244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011245 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011246 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11247 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011248 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +020011249#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011250#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011251 {
11252 *major = ver[0];
11253 *minor = ver[1];
11254 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +020011255#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011256}
11257
Simon Butcher99000142016-10-13 17:21:01 +010011258int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11259{
11260#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11261 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11262 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11263
11264 switch( md )
11265 {
11266#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11267#if defined(MBEDTLS_MD5_C)
11268 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011269 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011270#endif
11271#if defined(MBEDTLS_SHA1_C)
11272 case MBEDTLS_SSL_HASH_SHA1:
11273 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11274 break;
11275#endif
11276#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11277#if defined(MBEDTLS_SHA512_C)
11278 case MBEDTLS_SSL_HASH_SHA384:
11279 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11280 break;
11281#endif
11282#if defined(MBEDTLS_SHA256_C)
11283 case MBEDTLS_SSL_HASH_SHA256:
11284 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11285 break;
11286#endif
11287 default:
11288 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11289 }
11290
11291 return 0;
11292#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11293 (void) ssl;
11294 (void) md;
11295
11296 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11297#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11298}
11299
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011300#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11301 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11302int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11303 unsigned char *output,
11304 unsigned char *data, size_t data_len )
11305{
11306 int ret = 0;
11307 mbedtls_md5_context mbedtls_md5;
11308 mbedtls_sha1_context mbedtls_sha1;
11309
11310 mbedtls_md5_init( &mbedtls_md5 );
11311 mbedtls_sha1_init( &mbedtls_sha1 );
11312
11313 /*
11314 * digitally-signed struct {
11315 * opaque md5_hash[16];
11316 * opaque sha_hash[20];
11317 * };
11318 *
11319 * md5_hash
11320 * MD5(ClientHello.random + ServerHello.random
11321 * + ServerParams);
11322 * sha_hash
11323 * SHA(ClientHello.random + ServerHello.random
11324 * + ServerParams);
11325 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011326 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011327 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011328 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011329 goto exit;
11330 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011331 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011332 ssl->handshake->randbytes, 64 ) ) != 0 )
11333 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011335 goto exit;
11336 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011337 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011338 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011340 goto exit;
11341 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011342 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011343 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011344 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011345 goto exit;
11346 }
11347
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011348 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011349 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011350 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011351 goto exit;
11352 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011353 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011354 ssl->handshake->randbytes, 64 ) ) != 0 )
11355 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011356 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011357 goto exit;
11358 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011359 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011360 data_len ) ) != 0 )
11361 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011362 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011363 goto exit;
11364 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011365 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011366 output + 16 ) ) != 0 )
11367 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011368 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011369 goto exit;
11370 }
11371
11372exit:
11373 mbedtls_md5_free( &mbedtls_md5 );
11374 mbedtls_sha1_free( &mbedtls_sha1 );
11375
11376 if( ret != 0 )
11377 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11378 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11379
11380 return( ret );
11381
11382}
11383#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11384 MBEDTLS_SSL_PROTO_TLS1_1 */
11385
11386#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11387 defined(MBEDTLS_SSL_PROTO_TLS1_2)
11388int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011389 unsigned char *hash, size_t *hashlen,
11390 unsigned char *data, size_t data_len,
11391 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011392{
11393 int ret = 0;
11394 mbedtls_md_context_t ctx;
11395 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011396 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011397
11398 mbedtls_md_init( &ctx );
11399
11400 /*
11401 * digitally-signed struct {
11402 * opaque client_random[32];
11403 * opaque server_random[32];
11404 * ServerDHParams params;
11405 * };
11406 */
11407 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11408 {
11409 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11410 goto exit;
11411 }
11412 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11413 {
11414 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11415 goto exit;
11416 }
11417 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11418 {
11419 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11420 goto exit;
11421 }
11422 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11423 {
11424 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11425 goto exit;
11426 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011427 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011428 {
11429 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11430 goto exit;
11431 }
11432
11433exit:
11434 mbedtls_md_free( &ctx );
11435
11436 if( ret != 0 )
11437 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11438 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11439
11440 return( ret );
11441}
11442#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11443 MBEDTLS_SSL_PROTO_TLS1_2 */
11444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011445#endif /* MBEDTLS_SSL_TLS_C */